1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XClipboardNotifier.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc
.datatransfer
.clipboard
;
33 import lib
.MultiMethodTest
;
34 import lib
.StatusException
;
36 import com
.sun
.star
.datatransfer
.DataFlavor
;
37 import com
.sun
.star
.datatransfer
.XTransferable
;
38 import com
.sun
.star
.datatransfer
.clipboard
.ClipboardEvent
;
39 import com
.sun
.star
.datatransfer
.clipboard
.XClipboard
;
40 import com
.sun
.star
.datatransfer
.clipboard
.XClipboardListener
;
41 import com
.sun
.star
.datatransfer
.clipboard
.XClipboardNotifier
;
42 import com
.sun
.star
.datatransfer
.clipboard
.XClipboardOwner
;
43 import com
.sun
.star
.lang
.EventObject
;
44 import com
.sun
.star
.uno
.Type
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
48 * Testing <code>com.sun.star.datatransfer.clipboard.XClipboardNotifier</code>
51 * <li><code> addClipboardListener()</code></li>
52 * <li><code> removeClipboardListener()</code></li>
54 * The object <b>must also implement</b> <code>XClipboard</code>
56 * Test is <b> NOT </b> multithread compilant. <p>
57 * @see com.sun.star.datatransfer.clipboard.XClipboardNotifier
58 * @see com.sun.star.datatransfer.clipboard.XClipboard
60 public class _XClipboardNotifier
extends MultiMethodTest
{
62 public XClipboardNotifier oObj
;
65 * <code>XClipboardOwner</code> interface implementation which
66 * stores parameters passed to <code>lostOwnership</code> method.
68 class MyOwner
implements XClipboardOwner
{
70 XTransferable contents
;
72 public void lostOwnership(XClipboard board
, XTransferable contents
) {
74 this.contents
= contents
;
79 * Simpliest <code>XTransferable</code> interface implementation
80 * which supports "text/htmp" data type.
82 class MyTransferable
implements XTransferable
{
83 DataFlavor
[] supportedFlavors
;
85 public MyTransferable() {
86 supportedFlavors
= new DataFlavor
[] {
87 new DataFlavor("text/plain", "Plain text", new Type(String
.class))
91 public Object
getTransferData(DataFlavor dataFlavor
) {
95 public DataFlavor
[] getTransferDataFlavors() {
96 return supportedFlavors
;
99 public boolean isDataFlavorSupported(DataFlavor dataFlavor
) {
100 return supportedFlavors
[0].MimeType
.equals(dataFlavor
.MimeType
);
105 * Implementation of listener which registers its method calls.
107 class MyClipboardListener
implements XClipboardListener
{
108 boolean called
= false;
110 public void changedContents(ClipboardEvent evt
) {
114 public void disposing(EventObject wvt
) {
119 MyClipboardListener myListener
;
122 * Adds a listener and put a new contents into clipboard. <p>
123 * Has <b> OK </b> status if the listener was called on contents changing.
125 public void _addClipboardListener() {
126 oObj
.addClipboardListener(myListener
= new MyClipboardListener());
128 XClipboard board
= (XClipboard
)UnoRuntime
.queryInterface(
129 XClipboard
.class, tEnv
.getTestObject());
131 board
.setContents(new MyTransferable(), new MyOwner());
133 log
.println("sleeping for 1 second");
137 } catch (InterruptedException e
) {
138 log
.println("interrupted");
139 e
.printStackTrace(log
);
140 throw new StatusException("Operation interrupted", e
);
143 tRes
.tested("addClipboardListener()", myListener
.called
);
147 * Removes the listener and put a new contents into clipboard. <p>
148 * Has <b> OK </b> status if the listener was not called on contents
150 * The following method tests are to be completed successfully before :
152 * <li> <code>addClipboardListener()</code> </li>
155 public void _removeClipboardListener() {
157 requiredMethod("addClipboardListener()");
158 myListener
.called
= false;
160 oObj
.removeClipboardListener(myListener
);
163 XClipboard board
= (XClipboard
)UnoRuntime
.queryInterface(
164 XClipboard
.class, oObj
);
166 board
.setContents(new MyTransferable(), new MyOwner());
170 } catch (InterruptedException e
) {
171 log
.println("interrupted");
172 e
.printStackTrace(log
);
173 throw new StatusException("Operation interrupted", e
);
176 tRes
.tested("removeClipboardListener()", !myListener
.called
);