1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package ifc.view;
28 
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.view.PrintJobEvent;
32 import com.sun.star.view.XPrintJobBroadcaster;
33 import com.sun.star.view.XPrintJobListener;
34 import com.sun.star.view.XPrintable;
35 import java.io.File;
36 import lib.MultiMethodTest;
37 import lib.Status;
38 import lib.StatusException;
39 
40 /**
41  * Test the XPrintJobBroadcaster interface
42  */
43 public class _XPrintJobBroadcaster extends MultiMethodTest {
44     public XPrintJobBroadcaster oObj = null;
45     MyPrintJobListener listenerImpl = null;
46 
47     /**
48      * Get an object implementation of the _XPrintJobListener interface from the
49      * test environment.
50      */
51     public void before() {
52         listenerImpl = (MyPrintJobListener)tEnv.getObjRelation("XPrintJobBroadcaster.XPrintJobListener");
53         if (listenerImpl == null) {
54             throw new StatusException(Status.failed(" No test possible. The XPrintJobListener interface has to be implemented."));
55         }
56     }
57 
58     /**
59      * add the listener, see if it's called.
60      */
61     public void _addPrintJobListener() {
62         oObj.addPrintJobListener(listenerImpl);
63         listenerImpl.fireEvent();
64         util.utils.shortWait(1000);
65         tRes.tested("addPrintJobListener()", listenerImpl.actionTriggered());
66     }
67 
68     /**
69      * remove the listener, see if it's still caleed.
70      */
71     public void _removePrintJobListener() {
72         requiredMethod("addPrintJobListener");
73         oObj.removePrintJobListener(listenerImpl);
74 
75             util.utils.shortWait(5000);
76 
77         listenerImpl.reset();
78         listenerImpl.fireEvent();
79         tRes.tested("removePrintJobListener()", !listenerImpl.actionTriggered());
80     }
81 
82     /**
83      * Implementation for testing the XPrintJobBroadcaster interface:
84      * a listener to add.
85      */
86     public static class MyPrintJobListener implements XPrintJobListener {
87         boolean eventCalled = false;
88         // object to trigger the event
89         XPrintable xPrintable = null;
90         PropertyValue[]printProps = null;
91         String printFileName = null;
92 
93         /**
94          * Constructor
95          * @param An object that can be cast to an XPrintable.
96          */
97         public MyPrintJobListener(Object printable, String printFileName) {
98             this.printFileName = printFileName;
99             xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, printable);
100             printProps = new PropertyValue[2];
101             printProps[0] = new PropertyValue();
102             printProps[0].Name = "FileName";
103             printProps[0].Value = printFileName;
104             printProps[0].State = com.sun.star.beans.PropertyState.DEFAULT_VALUE;
105             printProps[1] = new PropertyValue();
106             printProps[1].Name = "Wait";
107             printProps[1].Value = new Boolean(true);
108         }
109 
110         /**
111          * Has the action been triggered?
112          * @return True if "printJobEvent" has been called.
113          */
114         public boolean actionTriggered() {
115             return eventCalled;
116         }
117 
118         /**
119          * Fire the event that calls the printJobEvent
120          */
121         public void fireEvent() {
122             try {
123                 xPrintable.print(printProps);
124             }
125             catch(com.sun.star.lang.IllegalArgumentException e) {
126             }
127         }
128 
129         public void reset() {
130             File f = new File(printFileName);
131             if (f.exists())
132                 f.delete();
133             eventCalled = false;
134         }
135 
136         /**
137          * The print job event: has to be called when the action is triggered.
138          */
139         public void printJobEvent(PrintJobEvent printJobEvent) {
140             eventCalled = true;
141         }
142 
143         /**
144          * Disposing event: ignore.
145          */
146         public void disposing(com.sun.star.lang.EventObject eventObject) {
147         }
148     }
149 
150 }
151