2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc
.datatransfer
.clipboard
;
21 import lib
.MultiMethodTest
;
22 import lib
.StatusException
;
24 import com
.sun
.star
.datatransfer
.DataFlavor
;
25 import com
.sun
.star
.datatransfer
.XTransferable
;
26 import com
.sun
.star
.datatransfer
.clipboard
.ClipboardEvent
;
27 import com
.sun
.star
.datatransfer
.clipboard
.XClipboard
;
28 import com
.sun
.star
.datatransfer
.clipboard
.XClipboardListener
;
29 import com
.sun
.star
.datatransfer
.clipboard
.XClipboardNotifier
;
30 import com
.sun
.star
.datatransfer
.clipboard
.XClipboardOwner
;
31 import com
.sun
.star
.lang
.EventObject
;
32 import com
.sun
.star
.uno
.Type
;
33 import com
.sun
.star
.uno
.UnoRuntime
;
36 * Testing <code>com.sun.star.datatransfer.clipboard.XClipboardNotifier</code>
39 * <li><code> addClipboardListener()</code></li>
40 * <li><code> removeClipboardListener()</code></li>
42 * The object <b>must also implement</b> <code>XClipboard</code>
44 * Test is <b> NOT </b> multithread compliant. <p>
45 * @see com.sun.star.datatransfer.clipboard.XClipboardNotifier
46 * @see com.sun.star.datatransfer.clipboard.XClipboard
48 public class _XClipboardNotifier
extends MultiMethodTest
{
50 public XClipboardNotifier oObj
;
53 * <code>XClipboardOwner</code> interface implementation which
54 * stores parameters passed to <code>lostOwnership</code> method.
56 class MyOwner
implements XClipboardOwner
{
58 XTransferable contents
;
60 public void lostOwnership(XClipboard board
, XTransferable contents
) {
62 this.contents
= contents
;
67 * Simpliest <code>XTransferable</code> interface implementation
68 * which supports "text/htmp" data type.
70 class MyTransferable
implements XTransferable
{
71 DataFlavor
[] supportedFlavors
;
73 public MyTransferable() {
74 supportedFlavors
= new DataFlavor
[] {
75 new DataFlavor("text/plain", "Plain text", new Type(String
.class))
79 public Object
getTransferData(DataFlavor dataFlavor
) {
83 public DataFlavor
[] getTransferDataFlavors() {
84 return supportedFlavors
;
87 public boolean isDataFlavorSupported(DataFlavor dataFlavor
) {
88 return supportedFlavors
[0].MimeType
.equals(dataFlavor
.MimeType
);
93 * Implementation of listener which registers its method calls.
95 class MyClipboardListener
implements XClipboardListener
{
96 boolean called
= false;
98 public void changedContents(ClipboardEvent evt
) {
102 public void disposing(EventObject wvt
) {
107 MyClipboardListener myListener
;
110 * Adds a listener and put a new contents into clipboard. <p>
111 * Has <b> OK </b> status if the listener was called on contents changing.
113 public void _addClipboardListener() {
114 oObj
.addClipboardListener(myListener
= new MyClipboardListener());
116 XClipboard board
= UnoRuntime
.queryInterface(
117 XClipboard
.class, tEnv
.getTestObject());
119 board
.setContents(new MyTransferable(), new MyOwner());
121 log
.println("sleeping for 1 second");
125 } catch (InterruptedException e
) {
126 log
.println("interrupted");
127 e
.printStackTrace(log
);
128 throw new StatusException("Operation interrupted", e
);
131 tRes
.tested("addClipboardListener()", myListener
.called
);
135 * Removes the listener and put a new contents into clipboard. <p>
136 * Has <b> OK </b> status if the listener was not called on contents
138 * The following method tests are to be completed successfully before :
140 * <li> <code>addClipboardListener()</code> </li>
143 public void _removeClipboardListener() {
145 requiredMethod("addClipboardListener()");
146 myListener
.called
= false;
148 oObj
.removeClipboardListener(myListener
);
151 XClipboard board
= UnoRuntime
.queryInterface(
152 XClipboard
.class, oObj
);
154 board
.setContents(new MyTransferable(), new MyOwner());
158 } catch (InterruptedException e
) {
159 log
.println("interrupted");
160 e
.printStackTrace(log
);
161 throw new StatusException("Operation interrupted", e
);
164 tRes
.tested("removeClipboardListener()", !myListener
.called
);