cid#1607171 Data race condition
[LibreOffice.git] / qadevOOo / tests / java / ifc / datatransfer / clipboard / _XClipboardNotifier.java
blobb26e89333e40f828f452fb93564eee9b4dcb8dbd
1 /*
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;
35 /**
36 * Testing <code>com.sun.star.datatransfer.clipboard.XClipboardNotifier</code>
37 * interface methods :
38 * <ul>
39 * <li><code> addClipboardListener()</code></li>
40 * <li><code> removeClipboardListener()</code></li>
41 * </ul> <p>
42 * The object <b>must also implement</b> <code>XClipboard</code>
43 * interface. <p>
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;
52 /**
53 * <code>XClipboardOwner</code> interface implementation which
54 * stores parameters passed to <code>lostOwnership</code> method.
56 static class MyOwner implements XClipboardOwner {
58 public void lostOwnership(XClipboard board, XTransferable contents) {
62 /**
63 * Simplest <code>XTransferable</code> interface implementation
64 * which supports "text/html" data type.
66 static class MyTransferable implements XTransferable {
67 DataFlavor[] supportedFlavors;
69 public MyTransferable() {
70 supportedFlavors = new DataFlavor[] {
71 new DataFlavor("text/plain", "Plain text", new Type(String.class))
75 public Object getTransferData(DataFlavor dataFlavor) {
76 return "";
79 public DataFlavor[] getTransferDataFlavors() {
80 return supportedFlavors;
83 public boolean isDataFlavorSupported(DataFlavor dataFlavor) {
84 return supportedFlavors[0].MimeType.equals(dataFlavor.MimeType);
88 /**
89 * Implementation of listener which registers its method calls.
91 class MyClipboardListener implements XClipboardListener {
92 boolean called = false;
94 public void changedContents(ClipboardEvent evt) {
95 called = true;
98 public void disposing(EventObject wvt) {
99 log.println("");
103 MyClipboardListener myListener;
106 * Adds a listener and put a new contents into clipboard. <p>
107 * Has <b> OK </b> status if the listener was called on contents changing.
109 public void _addClipboardListener() {
110 oObj.addClipboardListener(myListener = new MyClipboardListener());
112 XClipboard board = UnoRuntime.queryInterface(
113 XClipboard.class, tEnv.getTestObject());
115 board.setContents(new MyTransferable(), new MyOwner());
117 log.println("sleeping for 1 second");
119 try {
120 Thread.sleep(1000);
121 } catch (InterruptedException e) {
122 log.println("interrupted");
123 e.printStackTrace(log);
124 throw new StatusException("Operation interrupted", e);
127 tRes.tested("addClipboardListener()", myListener.called);
131 * Removes the listener and put a new contents into clipboard. <p>
132 * Has <b> OK </b> status if the listener was not called on contents
133 * changing.
134 * The following method tests are to be completed successfully before :
135 * <ul>
136 * <li> <code>addClipboardListener()</code> </li>
137 * </ul>
139 public void _removeClipboardListener() {
140 try {
141 requiredMethod("addClipboardListener()");
142 myListener.called = false;
143 } finally {
144 oObj.removeClipboardListener(myListener);
147 XClipboard board = UnoRuntime.queryInterface(
148 XClipboard.class, oObj);
150 board.setContents(new MyTransferable(), new MyOwner());
152 try {
153 Thread.sleep(1000);
154 } catch (InterruptedException e) {
155 log.println("interrupted");
156 e.printStackTrace(log);
157 throw new StatusException("Operation interrupted", e);
160 tRes.tested("removeClipboardListener()", !myListener.called);