IndependenceAWTTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26import java.awt.*;
27import java.awt.datatransfer.*;
28import java.awt.event.*;
29import java.util.Properties;
30
31/*
32 * @test
33 * @key headful
34 * @summary To make sure that System & Primary clipboards should behave independently
35 * @author Jitender(jitender.singh@eng.sun.com) area=AWT
36 * @author dmitriy.ermashov@oracle.com
37 * @library ../../../../lib/testlibrary
38 * @build ExtendedRobot
39 * @run main IndependenceAWTTest
40 */
41
42public class IndependenceAWTTest {
43
44    Frame frame;
45    Panel panel;
46    TextField tf1, tf2, tf3;
47    Clipboard sClip, pClip;
48
49    public static void main (String[] args) throws Exception {
50        new IndependenceAWTTest().doTest();
51    }
52
53    public IndependenceAWTTest() {
54
55        frame = new Frame();
56        frame.setSize(200, 200);
57
58        // This textfield will be used to update the contents of clipboards
59        tf1 = new TextField();
60        tf1.addFocusListener(new FocusAdapter() {
61            public void focusGained(FocusEvent fe) {
62                tf1.setText("Clipboards_Independance_Testing");
63            }
64        });
65
66        // TextFields to get the contents of clipboard
67        tf2 = new TextField();
68        tf3 = new TextField();
69
70        panel = new Panel();
71        panel.setLayout(new BorderLayout());
72
73        panel.add(tf2, BorderLayout.NORTH);
74        panel.add(tf3, BorderLayout.SOUTH);
75
76        frame.add(tf1, BorderLayout.NORTH);
77        frame.add(panel, BorderLayout.CENTER);
78
79        frame.setVisible(true);
80        tf1.requestFocus();
81    }
82
83    public void checkSecurity() {
84        SecurityManager sm = System.getSecurityManager();
85        if (sm == null)  {
86            System.out.println("security manager is not there");
87            getPrimaryClipboard();
88        } else {
89            sm.checkPermission(new AWTPermission("accessClipboard"));
90            getPrimaryClipboard();
91        }
92    }
93
94    // Get System Selection i.e. Primary Clipboard
95    private void getPrimaryClipboard() {
96        Properties ps = System.getProperties();
97        String operSys = ps.getProperty("os.name");
98        try {
99            pClip = Toolkit.getDefaultToolkit().getSystemSelection();
100            if (pClip == null)
101                if ((operSys.substring(0,3)).equalsIgnoreCase("Win") || operSys.toLowerCase().contains("os x"))
102                    System.out.println(operSys + "Operating system does not support system selection ");
103                else
104                    throw new RuntimeException("Method getSystemSelection() is returning null on X11 platform");
105        } catch(HeadlessException e) {
106            System.out.println("Headless exception thrown " + e);
107        }
108    }
109
110    // Method to get the contents of both of the clipboards
111    public void getClipboardsContent() throws Exception {
112        sClip = Toolkit.getDefaultToolkit().getSystemClipboard();
113        Transferable tp;
114        Transferable ts;
115
116        StringSelection content = new StringSelection(tf1.getText());
117        sClip.setContents(content,content);
118
119        tp = pClip.getContents(this);
120        ts = sClip.getContents(this);
121
122        // Paste the contents of System clipboard on textfield tf2 while the paste the contents of
123        // of primary clipboard on textfiled tf3
124        if ((ts != null) && (ts.isDataFlavorSupported(DataFlavor.stringFlavor))) {
125            tf2.setBackground(Color.white);
126            tf2.setForeground(Color.black);
127            tf2.setText((String) ts.getTransferData(DataFlavor.stringFlavor));
128        }
129
130        if ((tp != null) && (tp.isDataFlavorSupported(DataFlavor.stringFlavor))) {
131            tf3.setBackground(Color.white);
132            tf3.setForeground(Color.black);
133            tf3.setText((String) tp.getTransferData(DataFlavor.stringFlavor));
134        }
135    }
136
137    // Method to compare the Contents return by system & primary clipboard
138    public void compareText (boolean mustEqual) {
139        if ((tf2.getText()).equals(tf3.getText())) {
140            if (mustEqual)
141                System.out.println("Selected text & clipboard contents are same\n");
142            else
143                throw new RuntimeException("Selected text & clipboard contents are same\n");
144        } else {
145            if (mustEqual)
146                throw new RuntimeException("Selected text & clipboard contents differs\n");
147            else
148                System.out.println("Selected text & clipboard contents differs\n");
149        }
150    }
151
152    public void doTest() throws Exception {
153        checkSecurity();
154        ExtendedRobot robot = new ExtendedRobot();
155        robot.waitForIdle(1000);
156        frame.setLocation(100, 100);
157        robot.waitForIdle(1000);
158
159        if (pClip != null) {
160            Point ttf1Center = tf1.getLocationOnScreen();
161            ttf1Center.translate(tf1.getWidth()/2, tf1.getHeight()/2);
162
163            robot.glide(new Point(0, 0), ttf1Center);
164            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
165            robot.waitForIdle(20);
166            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
167            robot.waitForIdle(20);
168            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
169            robot.waitForIdle(20);
170            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
171            robot.waitForIdle(2000);
172
173            getClipboardsContent();
174            compareText(true);
175
176            //Change the text selection to update the contents of primary clipboard
177            robot.mouseMove(ttf1Center);
178            robot.mousePress(MouseEvent.BUTTON1_MASK);
179            robot.delay(200);
180            robot.mouseMove(ttf1Center.x + 15, ttf1Center.y);
181            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
182            robot.waitForIdle(2000);
183
184            getClipboardsContent();
185            compareText(false);
186        }
187    }
188}
189
190