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 
28 package ifc.datatransfer.clipboard;
29 
30 import lib.MultiMethodTest;
31 import lib.StatusException;
32 
33 import com.sun.star.datatransfer.DataFlavor;
34 import com.sun.star.datatransfer.XTransferable;
35 import com.sun.star.datatransfer.clipboard.ClipboardEvent;
36 import com.sun.star.datatransfer.clipboard.XClipboard;
37 import com.sun.star.datatransfer.clipboard.XClipboardListener;
38 import com.sun.star.datatransfer.clipboard.XClipboardNotifier;
39 import com.sun.star.datatransfer.clipboard.XClipboardOwner;
40 import com.sun.star.lang.EventObject;
41 import com.sun.star.uno.Type;
42 import com.sun.star.uno.UnoRuntime;
43 
44 /**
45 * Testing <code>com.sun.star.datatransfer.clipboard.XClipboardNotifier</code>
46 * interface methods :
47 * <ul>
48 *  <li><code> addClipboardListener()</code></li>
49 *  <li><code> removeClipboardListener()</code></li>
50 * </ul> <p>
51 * The object <b>must also implement</b> <code>XClipboard</code>
52 * interface. <p>
53 * Test is <b> NOT </b> multithread compilant. <p>
54 * @see com.sun.star.datatransfer.clipboard.XClipboardNotifier
55 * @see com.sun.star.datatransfer.clipboard.XClipboard
56 */
57 public class _XClipboardNotifier extends MultiMethodTest {
58 
59     public XClipboardNotifier oObj;
60 
61     /**
62      * <code>XClipboardOwner</code> interface implementation which
63      * stores parameters passed to <code>lostOwnership</code> method.
64      */
65     class MyOwner implements XClipboardOwner {
66         XClipboard board;
67         XTransferable contents;
68 
69         public void lostOwnership(XClipboard board, XTransferable contents) {
70             this.board = board;
71             this.contents = contents;
72         }
73     }
74 
75     /**
76      * Simpliest <code>XTransferable</code> interface implementation
77      * which supports "text/htmp" data type.
78      */
79     class MyTransferable implements XTransferable {
80         DataFlavor[] supportedFlavors;
81 
82         public MyTransferable() {
83             supportedFlavors = new DataFlavor[] {
84                 new DataFlavor("text/plain", "Plain text", new Type(String.class))
85             };
86         }
87 
88         public Object getTransferData(DataFlavor dataFlavor) {
89             return "";
90         }
91 
92         public DataFlavor[] getTransferDataFlavors() {
93             return supportedFlavors;
94         }
95 
96         public boolean isDataFlavorSupported(DataFlavor dataFlavor) {
97             return supportedFlavors[0].MimeType.equals(dataFlavor.MimeType);
98         }
99     }
100 
101     /**
102      * Implementation of listener which registers its method calls.
103      */
104     class MyClipboardListener implements XClipboardListener {
105         boolean called = false;
106 
107         public void changedContents(ClipboardEvent evt) {
108             called = true;
109         }
110 
111         public void disposing(EventObject wvt) {
112             log.println("");
113         }
114     }
115 
116     MyClipboardListener myListener;
117 
118     /**
119     * Adds a listener and put a new contents into clipboard. <p>
120     * Has <b> OK </b> status if the listener was called on contents changing.
121     */
122     public void _addClipboardListener() {
123         oObj.addClipboardListener(myListener = new MyClipboardListener());
124 
125         XClipboard board = (XClipboard)UnoRuntime.queryInterface(
126                 XClipboard.class, tEnv.getTestObject());
127 
128         board.setContents(new MyTransferable(), new MyOwner());
129 
130         log.println("sleeping for 1 second");
131 
132         try {
133             Thread.sleep(1000);
134         } catch (InterruptedException e) {
135             log.println("interrupted");
136             e.printStackTrace(log);
137             throw new StatusException("Operation interrupted", e);
138         }
139 
140         tRes.tested("addClipboardListener()", myListener.called);
141     }
142 
143     /**
144     * Removes the listener and put a new contents into clipboard. <p>
145     * Has <b> OK </b> status if the listener was not called on contents
146     * changing.
147     * The following method tests are to be completed successfully before :
148     * <ul>
149     *  <li> <code>addClipboardListener()</code> </li>
150     * </ul>
151     */
152     public void _removeClipboardListener() {
153         try {
154             requiredMethod("addClipboardListener()");
155             myListener.called = false;
156         } finally {
157             oObj.removeClipboardListener(myListener);
158         }
159 
160         XClipboard board = (XClipboard)UnoRuntime.queryInterface(
161                 XClipboard.class, oObj);
162 
163         board.setContents(new MyTransferable(), new MyOwner());
164 
165         try {
166             Thread.sleep(1000);
167         } catch (InterruptedException e) {
168             log.println("interrupted");
169             e.printStackTrace(log);
170             throw new StatusException("Operation interrupted", e);
171         }
172 
173         tRes.tested("removeClipboardListener()", !myListener.called);
174     }
175 }
176