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