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