1
1
/*
2
- * Copyright (c) 1999, 2016 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1999, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
- import java .awt .*;
25
- import java .awt .datatransfer .*;
26
- import java .awt .event .*;
27
- import java .util .Properties ;
28
24
29
25
/*
30
26
* @test
31
27
* @key headful
28
+ * @requires (os.family == "linux")
32
29
* @summary To make sure that System & Primary clipboards should behave independently
33
- * @author Jitender([email protected] ) area=AWT
34
-
35
30
* @library /lib/client
36
31
* @build ExtendedRobot
37
32
* @run main IndependenceAWTTest
38
33
*/
39
34
40
- public class IndependenceAWTTest {
35
+ import java .awt .BorderLayout ;
36
+ import java .awt .Color ;
37
+ import java .awt .EventQueue ;
38
+ import java .awt .Frame ;
39
+ import java .awt .HeadlessException ;
40
+ import java .awt .Panel ;
41
+ import java .awt .Point ;
42
+ import java .awt .TextField ;
43
+ import java .awt .Toolkit ;
44
+ import java .awt .datatransfer .Clipboard ;
45
+ import java .awt .datatransfer .DataFlavor ;
46
+ import java .awt .datatransfer .StringSelection ;
47
+ import java .awt .datatransfer .Transferable ;
48
+ import java .awt .event .FocusAdapter ;
49
+ import java .awt .event .FocusEvent ;
50
+ import java .awt .event .InputEvent ;
51
+ import java .awt .event .MouseEvent ;
41
52
42
- Frame frame ;
43
- Panel panel ;
44
- TextField tf1 , tf2 , tf3 ;
45
- Clipboard sClip , pClip ;
53
+ public class IndependenceAWTTest {
54
+ private static Frame frame ;
55
+ private static TextField tf1 ;
56
+ private static TextField tf2 ;
57
+ private static TextField tf3 ;
58
+ private static Clipboard systemClip ;
59
+ private static Clipboard primaryClip ;
60
+ private static ExtendedRobot robot ;
61
+ private static volatile Point ttf1Center ;
62
+ private static volatile Point glideStartLocation ;
46
63
47
64
public static void main (String [] args ) throws Exception {
48
- new IndependenceAWTTest ().doTest ();
65
+ try {
66
+ robot = new ExtendedRobot ();
67
+ EventQueue .invokeAndWait (IndependenceAWTTest ::createAndShowUI );
68
+ robot .waitForIdle ();
69
+ robot .delay (1000 );
70
+ test ();
71
+ } finally {
72
+ EventQueue .invokeAndWait (frame ::dispose );
73
+ }
49
74
}
50
75
51
- public IndependenceAWTTest () {
52
-
53
- frame = new Frame ();
76
+ private static void createAndShowUI () {
77
+ frame = new Frame ("IndependenceAWTTest" );
54
78
frame .setSize (200 , 200 );
55
79
56
80
// This textfield will be used to update the contents of clipboards
57
81
tf1 = new TextField ();
58
82
tf1 .addFocusListener (new FocusAdapter () {
59
83
public void focusGained (FocusEvent fe ) {
60
- tf1 .setText ("Clipboards_Independance_Testing " );
84
+ tf1 .setText ("Clipboards_Independence_Testing " );
61
85
}
62
86
});
63
87
64
88
// TextFields to get the contents of clipboard
65
89
tf2 = new TextField ();
66
90
tf3 = new TextField ();
67
91
68
- panel = new Panel ();
92
+ Panel panel = new Panel ();
69
93
panel .setLayout (new BorderLayout ());
70
94
71
95
panel .add (tf2 , BorderLayout .NORTH );
72
96
panel .add (tf3 , BorderLayout .SOUTH );
73
97
74
98
frame .add (tf1 , BorderLayout .NORTH );
75
99
frame .add (panel , BorderLayout .CENTER );
100
+ frame .setLocationRelativeTo (null );
76
101
77
102
frame .setVisible (true );
78
103
tf1 .requestFocus ();
79
104
}
80
105
81
106
// Get System Selection i.e. Primary Clipboard
82
- private void getPrimaryClipboard () {
83
- Properties ps = System .getProperties ();
84
- String operSys = ps .getProperty ("os.name" );
107
+ private static void getPrimaryClipboard () {
85
108
try {
86
- pClip = Toolkit .getDefaultToolkit ().getSystemSelection ();
87
- if (pClip == null )
88
- if ((operSys .substring (0 ,3 )).equalsIgnoreCase ("Win" ) || operSys .toLowerCase ().contains ("os x" ))
89
- System .out .println (operSys + "Operating system does not support system selection " );
90
- else
91
- throw new RuntimeException ("Method getSystemSelection() is returning null on X11 platform" );
92
- } catch (HeadlessException e ) {
109
+ primaryClip = Toolkit .getDefaultToolkit ().getSystemSelection ();
110
+ if (primaryClip == null ) {
111
+ throw new RuntimeException ("Method getSystemSelection() is returning null"
112
+ + " on Linux platform" );
113
+ }
114
+ } catch (HeadlessException e ) {
93
115
System .out .println ("Headless exception thrown " + e );
94
116
}
95
117
}
96
118
97
119
// Method to get the contents of both of the clipboards
98
- public void getClipboardsContent () throws Exception {
99
- sClip = Toolkit .getDefaultToolkit ().getSystemClipboard ();
120
+ private static void getClipboardsContent () throws Exception {
121
+ systemClip = Toolkit .getDefaultToolkit ().getSystemClipboard ();
100
122
Transferable tp ;
101
123
Transferable ts ;
102
124
103
125
StringSelection content = new StringSelection (tf1 .getText ());
104
- sClip .setContents (content ,content );
126
+ systemClip .setContents (content , content );
105
127
106
- tp = pClip .getContents (this );
107
- ts = sClip .getContents (this );
128
+ tp = primaryClip .getContents (null );
129
+ ts = systemClip .getContents (null );
108
130
109
131
// Paste the contents of System clipboard on textfield tf2 while the paste the contents of
110
132
// of primary clipboard on textfiled tf3
@@ -122,7 +144,7 @@ public void getClipboardsContent() throws Exception {
122
144
}
123
145
124
146
// Method to compare the Contents return by system & primary clipboard
125
- public void compareText (boolean mustEqual ) {
147
+ private static void compareText (boolean mustEqual ) {
126
148
if ((tf2 .getText ()).equals (tf3 .getText ())) {
127
149
if (mustEqual )
128
150
System .out .println ("Selected text & clipboard contents are same\n " );
@@ -136,40 +158,39 @@ public void compareText (boolean mustEqual) {
136
158
}
137
159
}
138
160
139
- public void doTest () throws Exception {
161
+ private static void test () throws Exception {
140
162
getPrimaryClipboard ();
141
- ExtendedRobot robot = new ExtendedRobot ();
142
- robot .waitForIdle (1000 );
143
- frame .setLocation (100 , 100 );
144
- robot .waitForIdle (1000 );
145
-
146
- if (pClip != null ) {
147
- Point ttf1Center = tf1 .getLocationOnScreen ();
148
- ttf1Center .translate (tf1 .getWidth ()/2 , tf1 .getHeight ()/2 );
149
-
150
- robot .glide (new Point (0 , 0 ), ttf1Center );
151
- robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
152
- robot .waitForIdle (20 );
153
- robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
154
- robot .waitForIdle (20 );
155
- robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
156
- robot .waitForIdle (20 );
157
- robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
158
- robot .waitForIdle (2000 );
159
-
160
- getClipboardsContent ();
161
- compareText (true );
162
-
163
- //Change the text selection to update the contents of primary clipboard
164
- robot .mouseMove (ttf1Center );
165
- robot .mousePress (MouseEvent .BUTTON1_MASK );
166
- robot .delay (200 );
167
- robot .mouseMove (ttf1Center .x + 15 , ttf1Center .y );
168
- robot .mouseRelease (MouseEvent .BUTTON1_MASK );
169
- robot .waitForIdle (2000 );
170
-
171
- getClipboardsContent ();
172
- compareText (false );
173
- }
163
+ robot .waitForIdle (500 );
164
+
165
+ EventQueue .invokeAndWait (() -> {
166
+ Point center = tf1 .getLocationOnScreen ();
167
+ center .translate (tf1 .getWidth () / 2 , tf1 .getHeight () / 2 );
168
+ ttf1Center = center ;
169
+
170
+ glideStartLocation = frame .getLocationOnScreen ();
171
+ glideStartLocation .x -= 10 ;
172
+ });
173
+
174
+ robot .glide (glideStartLocation , ttf1Center );
175
+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
176
+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
177
+ robot .waitForIdle (20 );
178
+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
179
+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
180
+ robot .waitForIdle (500 );
181
+
182
+ getClipboardsContent ();
183
+ compareText (true );
184
+
185
+ //Change the text selection to update the contents of primary clipboard
186
+ robot .mouseMove (ttf1Center );
187
+ robot .mousePress (MouseEvent .BUTTON1_DOWN_MASK );
188
+ robot .delay (20 );
189
+ robot .mouseMove (ttf1Center .x + 15 , ttf1Center .y );
190
+ robot .mouseRelease (MouseEvent .BUTTON1_DOWN_MASK );
191
+ robot .waitForIdle (500 );
192
+
193
+ getClipboardsContent ();
194
+ compareText (false );
174
195
}
175
196
}
0 commit comments