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