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 javax.swing.*;
27import java.awt.*;
28import java.awt.datatransfer.Clipboard;
29import java.awt.datatransfer.DataFlavor;
30import java.awt.datatransfer.StringSelection;
31import java.awt.datatransfer.Transferable;
32import java.awt.event.FocusAdapter;
33import java.awt.event.FocusEvent;
34import java.awt.event.InputEvent;
35import java.awt.event.MouseEvent;
36import java.util.Properties;
37
38/*
39 * @test
40 * @key headful
41 * @summary To make sure that System & Primary clipboards should behave independently
42 * @author Jitender(jitender.singh@eng.sun.com) area=AWT
43 * @author dmitriy.ermashov@oracle.com
44 * @library ../../../../lib/testlibrary
45 * @build ExtendedRobot
46 * @run main IndependenceSwingTest
47 */
48
49public class IndependenceSwingTest {
50
51    JFrame frame;
52    JPanel panel;
53    JTextField tf1, tf2, tf3;
54    Clipboard sClip, pClip;
55
56    public static void main (String[] args) throws Exception {
57        new IndependenceSwingTest().doTest();
58    }
59
60    public IndependenceSwingTest() {
61
62        frame = new JFrame();
63        frame.setSize(200, 200);
64
65        // This textfield will be used to update the contents of clipboards
66        tf1 = new JTextField();
67        tf1.addFocusListener(new FocusAdapter() {
68            public void focusGained(FocusEvent fe) {
69                tf1.setText("Clipboards_Independance_Testing");
70            }
71        });
72
73        // TextFields to get the contents of clipboard
74        tf2 = new JTextField();
75        tf3 = new JTextField();
76
77        panel = new JPanel();
78        panel.setLayout(new BorderLayout());
79
80        panel.add(tf2, BorderLayout.NORTH);
81        panel.add(tf3, BorderLayout.SOUTH);
82
83        frame.add(tf1, BorderLayout.NORTH);
84        frame.add(panel, BorderLayout.CENTER);
85
86        frame.setVisible(true);
87        tf1.requestFocus();
88    }
89
90    public void checkSecurity() {
91        SecurityManager sm = System.getSecurityManager();
92        if (sm == null)  {
93            System.out.println("security manager is not there");
94            getPrimaryClipboard();
95        } else {
96            sm.checkPermission(new AWTPermission("accessClipboard"));
97            getPrimaryClipboard();
98        }
99    }
100
101    // Get System Selection i.e. Primary Clipboard
102    private void getPrimaryClipboard() {
103        Properties ps = System.getProperties();
104        String operSys = ps.getProperty("os.name");
105        try {
106            pClip = Toolkit.getDefaultToolkit().getSystemSelection();
107            if (pClip == null)
108                if ((operSys.substring(0,3)).equalsIgnoreCase("Win") || operSys.toLowerCase().contains("os x"))
109                    System.out.println(operSys + "Operating system does not support system selection ");
110                else
111                    throw new RuntimeException("Method getSystemSelection() is returning null on X11 platform");
112        } catch(HeadlessException e) {
113            System.out.println("Headless exception thrown " + e);
114        }
115    }
116
117    // Method to get the contents of both of the clipboards
118    public void getClipboardsContent() throws Exception {
119        sClip = Toolkit.getDefaultToolkit().getSystemClipboard();
120        Transferable tp;
121        Transferable ts;
122
123        StringSelection content = new StringSelection(tf1.getText());
124        sClip.setContents(content,content);
125
126        tp = pClip.getContents(this);
127        ts = sClip.getContents(this);
128
129        // Paste the contents of System clipboard on textfield tf2 while the paste the contents of
130        // of primary clipboard on textfiled tf3
131        if ((ts != null) && (ts.isDataFlavorSupported(DataFlavor.stringFlavor))) {
132            tf2.setBackground(Color.white);
133            tf2.setForeground(Color.black);
134            tf2.setText((String) ts.getTransferData(DataFlavor.stringFlavor));
135        }
136
137        if ((tp != null) && (tp.isDataFlavorSupported(DataFlavor.stringFlavor))) {
138            tf3.setBackground(Color.white);
139            tf3.setForeground(Color.black);
140            tf3.setText((String) tp.getTransferData(DataFlavor.stringFlavor));
141        }
142    }
143
144    // Method to compare the Contents return by system & primary clipboard
145    public void compareText (boolean mustEqual) {
146        if ((tf2.getText()).equals(tf3.getText())) {
147            if (mustEqual)
148                System.out.println("Selected text & clipboard contents are same\n");
149            else
150                throw new RuntimeException("Selected text & clipboard contents are same\n");
151        } else {
152            if (mustEqual)
153                throw new RuntimeException("Selected text & clipboard contents differs\n");
154            else
155                System.out.println("Selected text & clipboard contents differs\n");
156        }
157    }
158
159    public void doTest() throws Exception {
160        checkSecurity();
161        ExtendedRobot robot = new ExtendedRobot();
162        robot.waitForIdle(1000);
163        frame.setLocation(100, 100);
164        robot.waitForIdle(1000);
165
166        if (pClip != null) {
167            Point ttf1Center = tf1.getLocationOnScreen();
168            ttf1Center.translate(tf1.getWidth()/2, tf1.getHeight()/2);
169
170            robot.glide(new Point(0, 0), ttf1Center);
171            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
172            robot.waitForIdle(20);
173            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
174            robot.waitForIdle(20);
175            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
176            robot.waitForIdle(20);
177            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
178            robot.waitForIdle(2000);
179
180            getClipboardsContent();
181            compareText(true);
182
183            //Change the text selection to update the contents of primary clipboard
184            robot.mouseMove(ttf1Center);
185            robot.mousePress(MouseEvent.BUTTON1_MASK);
186            robot.delay(200);
187            robot.mouseMove(ttf1Center.x + 15, ttf1Center.y);
188            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
189            robot.waitForIdle(2000);
190
191            getClipboardsContent();
192            compareText(false);
193        }
194    }
195}
196
197