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