SystemSelectionSwingTest.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 javax.swing.*;
27import java.awt.*;
28import java.awt.datatransfer.Clipboard;
29import java.awt.datatransfer.DataFlavor;
30import java.awt.datatransfer.Transferable;
31import java.awt.event.FocusAdapter;
32import java.awt.event.FocusEvent;
33import java.awt.event.InputEvent;
34import java.util.Properties;
35
36/*
37 * @test
38 * @key headful
39 * @summary To check the functionality of newly added API getSystemSelection & make sure
40 *          that it's mapped to primary clipboard
41 * @author Jitender(jitender.singh@eng.sun.com) area=AWT
42 * @library ../../../../lib/testlibrary
43 * @build ExtendedRobot
44 * @run main SystemSelectionSwingTest
45 */
46
47public class SystemSelectionSwingTest {
48
49    JFrame jframe;
50    JTextField jtf1, jtf2;
51    Clipboard clip;
52    Transferable t;
53
54    public static void main(String[] args) throws Exception {
55        new SystemSelectionSwingTest().doTest();
56    }
57
58    SystemSelectionSwingTest() {
59        jframe = new JFrame();
60        jframe.setSize(200, 200);
61
62        jtf1 = new JTextField();
63        jtf1.addFocusListener( new FocusAdapter() {
64            public void focusGained(FocusEvent fe) {
65                fe.getSource();
66            }
67        });
68
69        jtf2 = new JTextField();
70
71        jframe.add(jtf2, BorderLayout.NORTH);
72        jframe.add(jtf1, BorderLayout.CENTER);
73
74        jframe.setVisible(true);
75        jframe.toFront();
76        jtf1.requestFocus();
77        jtf1.setText("Selection Testing");
78    }
79
80    // Check whether Security manager is there
81    public void checkSecurity() {
82        SecurityManager sm = System.getSecurityManager();
83
84        if (sm == null) {
85            System.out.println("security manager is not there");
86            getPrimaryClipboard();
87        } else {
88            try {
89                sm.checkPermission(new AWTPermission("accessClipboard"));
90                getPrimaryClipboard();
91            } catch(SecurityException e) {
92                clip = null;
93                System.out.println("Access to System selection is not allowed");
94            }
95        }
96    }
97
98    // Get the contents from the clipboard
99    void getClipboardContent() throws Exception {
100        t = clip.getContents(this);
101        if ( (t != null) && (t.isDataFlavorSupported(DataFlavor.stringFlavor) )) {
102            jtf2.setBackground(Color.red);
103            jtf2.setForeground(Color.black);
104            jtf2.setText((String) t.getTransferData(DataFlavor.stringFlavor));
105        }
106    }
107
108
109    // Get System Selection i.e. Primary Clipboard
110    private void getPrimaryClipboard() {
111        Properties ps = System.getProperties();
112        String operSys = ps.getProperty("os.name");
113        clip = Toolkit.getDefaultToolkit().getSystemSelection();
114        if (clip == null) {
115            if ((operSys.substring(0,3)).equalsIgnoreCase("Win") ||
116                    (operSys.substring(0,3)).equalsIgnoreCase("Mac"))
117                System.out.println(operSys + " operating system does not support system selection ");
118            else
119                throw new RuntimeException("Method getSystemSelection() is returning null on X11 platform");
120        }
121    }
122
123    // Compare the selected text with one pasted from the clipboard
124    public void compareText() {
125        if ((jtf2.getText()).equals(jtf1.getSelectedText()) &&
126                System.getProperties().getProperty("os.name").substring(0,3) != "Win") {
127            System.out.println("Selected text & clipboard contents are same\n");
128        } else  {
129            throw new RuntimeException("Selected text & clipboard contents differs\n");
130        }
131    }
132
133    public void doTest() throws Exception {
134        ExtendedRobot robot = new ExtendedRobot();
135
136        jframe.setLocation(100, 100);
137        robot.waitForIdle(2000);
138
139        Point tf1Location = jtf1.getLocationOnScreen();
140        Dimension tf1Size = jtf1.getSize();
141        checkSecurity();
142
143        if (clip != null) {
144            robot.mouseMove(tf1Location.x + 5, tf1Location.y + tf1Size.height / 2);
145            robot.waitForIdle(2000);
146            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
147            robot.waitForIdle(20);
148            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
149            robot.waitForIdle(20);
150            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
151            robot.waitForIdle(20);
152            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
153            robot.waitForIdle(2000);
154
155            getClipboardContent();
156            compareText();
157
158            robot.mouseMove(tf1Location.x + tf1Size.width / 2, tf1Location.y + tf1Size.height / 2);
159            robot.waitForIdle(2000);
160            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
161            robot.waitForIdle(20);
162            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
163            robot.waitForIdle(20);
164            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
165            robot.waitForIdle(20);
166            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
167            robot.waitForIdle(2000);
168
169            getClipboardContent();
170            compareText();
171        }
172    }
173}
174
175