1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package ifc.view;
19 
20 import com.sun.star.beans.PropertyValue;
21 import com.sun.star.uno.UnoRuntime;
22 import com.sun.star.view.PrintJobEvent;
23 import com.sun.star.view.XPrintJobBroadcaster;
24 import com.sun.star.view.XPrintJobListener;
25 import com.sun.star.view.XPrintable;
26 import java.io.File;
27 import lib.MultiMethodTest;
28 import lib.Status;
29 import lib.StatusException;
30 
31 /**
32  * Test the XPrintJobBroadcaster interface
33  */
34 public class _XPrintJobBroadcaster extends MultiMethodTest {
35     public XPrintJobBroadcaster oObj = null;
36     MyPrintJobListener listenerImpl = null;
37 
38     /**
39      * Get an object implementation of the _XPrintJobListener interface from the
40      * test environment.
41      */
42     @Override
before()43     public void before() {
44         listenerImpl = (MyPrintJobListener)tEnv.getObjRelation("XPrintJobBroadcaster.XPrintJobListener");
45         if (listenerImpl == null) {
46             throw new StatusException(Status.failed(" No test possible. The XPrintJobListener interface has to be implemented."));
47         }
48     }
49 
50     /**
51      * add the listener, see if it's called.
52      */
_addPrintJobListener()53     public void _addPrintJobListener() {
54         oObj.addPrintJobListener(listenerImpl);
55         listenerImpl.fireEvent();
56         waitForEventIdle();
57         tRes.tested("addPrintJobListener()", listenerImpl.actionTriggered());
58     }
59 
60     /**
61      * remove the listener, see if it's still called.
62      */
_removePrintJobListener()63     public void _removePrintJobListener() {
64         requiredMethod("addPrintJobListener");
65         oObj.removePrintJobListener(listenerImpl);
66 
67         waitForEventIdle();
68 
69         listenerImpl.reset();
70         listenerImpl.fireEvent();
71         tRes.tested("removePrintJobListener()", !listenerImpl.actionTriggered());
72     }
73 
74     /**
75      * Implementation for testing the XPrintJobBroadcaster interface:
76      * a listener to add.
77      */
78     public static class MyPrintJobListener implements XPrintJobListener {
79         boolean eventCalled = false;
80         // object to trigger the event
81         XPrintable xPrintable = null;
82         PropertyValue[]printProps = null;
83         String printFileName = null;
84 
85         /**
86          * Constructor
87          * @param printable An object that can be cast to an XPrintable.
88          */
MyPrintJobListener(Object printable, String printFileName)89         public MyPrintJobListener(Object printable, String printFileName) {
90             this.printFileName = printFileName;
91             xPrintable = UnoRuntime.queryInterface(XPrintable.class, printable);
92             printProps = new PropertyValue[2];
93             printProps[0] = new PropertyValue();
94             printProps[0].Name = "FileName";
95             printProps[0].Value = printFileName;
96             printProps[0].State = com.sun.star.beans.PropertyState.DEFAULT_VALUE;
97             printProps[1] = new PropertyValue();
98             printProps[1].Name = "Wait";
99             printProps[1].Value = Boolean.TRUE;
100         }
101 
102         /**
103          * Has the action been triggered?
104          * @return True if "printJobEvent" has been called.
105          */
actionTriggered()106         public boolean actionTriggered() {
107             return eventCalled;
108         }
109 
110         /**
111          * Fire the event that calls the printJobEvent
112          */
fireEvent()113         public void fireEvent() {
114             try {
115                 xPrintable.print(printProps);
116             }
117             catch(com.sun.star.lang.IllegalArgumentException e) {
118             }
119         }
120 
reset()121         public void reset() {
122             File f = new File(printFileName);
123             if (f.exists()) {
124                 boolean bDeleteOk = f.delete();
125                 if (!bDeleteOk) {
126                     System.out.println("delete failed");
127                 }
128             }
129             eventCalled = false;
130         }
131 
132         /**
133          * The print job event: has to be called when the action is triggered.
134          */
printJobEvent(PrintJobEvent printJobEvent)135         public void printJobEvent(PrintJobEvent printJobEvent) {
136             eventCalled = true;
137         }
138 
139         /**
140          * Disposing event: ignore.
141          */
disposing(com.sun.star.lang.EventObject eventObject)142         public void disposing(com.sun.star.lang.EventObject eventObject) {
143         }
144     }
145 
146 }
147