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