Skip to content

Commit 73b8b34

Browse files
author
Harshitha Onkar
committed
8344368: IndependenceSwingTest.java and IndependenceAWTTest.java failed: Selected text & clipboard contents differs
Reviewed-by: azvegint, dnguyen, prr, kizune
1 parent 9267dfa commit 73b8b34

File tree

2 files changed

+178
-143
lines changed

2 files changed

+178
-143
lines changed
Lines changed: 89 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,90 +21,112 @@
2121
* questions.
2222
*/
2323

24-
import java.awt.*;
25-
import java.awt.datatransfer.*;
26-
import java.awt.event.*;
27-
import java.util.Properties;
2824

2925
/*
3026
* @test
3127
* @key headful
28+
* @requires (os.family == "linux")
3229
* @summary To make sure that System & Primary clipboards should behave independently
33-
* @author Jitender([email protected]) area=AWT
34-
3530
* @library /lib/client
3631
* @build ExtendedRobot
3732
* @run main IndependenceAWTTest
3833
*/
3934

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;
4152

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;
4663

4764
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+
}
4974
}
5075

51-
public IndependenceAWTTest() {
52-
53-
frame = new Frame();
76+
private static void createAndShowUI() {
77+
frame = new Frame("IndependenceAWTTest");
5478
frame.setSize(200, 200);
5579

5680
// This textfield will be used to update the contents of clipboards
5781
tf1 = new TextField();
5882
tf1.addFocusListener(new FocusAdapter() {
5983
public void focusGained(FocusEvent fe) {
60-
tf1.setText("Clipboards_Independance_Testing");
84+
tf1.setText("Clipboards_Independence_Testing");
6185
}
6286
});
6387

6488
// TextFields to get the contents of clipboard
6589
tf2 = new TextField();
6690
tf3 = new TextField();
6791

68-
panel = new Panel();
92+
Panel panel = new Panel();
6993
panel.setLayout(new BorderLayout());
7094

7195
panel.add(tf2, BorderLayout.NORTH);
7296
panel.add(tf3, BorderLayout.SOUTH);
7397

7498
frame.add(tf1, BorderLayout.NORTH);
7599
frame.add(panel, BorderLayout.CENTER);
100+
frame.setLocationRelativeTo(null);
76101

77102
frame.setVisible(true);
78103
tf1.requestFocus();
79104
}
80105

81106
// 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() {
85108
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) {
93115
System.out.println("Headless exception thrown " + e);
94116
}
95117
}
96118

97119
// 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();
100122
Transferable tp;
101123
Transferable ts;
102124

103125
StringSelection content = new StringSelection(tf1.getText());
104-
sClip.setContents(content,content);
126+
systemClip.setContents(content, content);
105127

106-
tp = pClip.getContents(this);
107-
ts = sClip.getContents(this);
128+
tp = primaryClip.getContents(null);
129+
ts = systemClip.getContents(null);
108130

109131
// Paste the contents of System clipboard on textfield tf2 while the paste the contents of
110132
// of primary clipboard on textfiled tf3
@@ -122,7 +144,7 @@ public void getClipboardsContent() throws Exception {
122144
}
123145

124146
// Method to compare the Contents return by system & primary clipboard
125-
public void compareText (boolean mustEqual) {
147+
private static void compareText (boolean mustEqual) {
126148
if ((tf2.getText()).equals(tf3.getText())) {
127149
if (mustEqual)
128150
System.out.println("Selected text & clipboard contents are same\n");
@@ -136,40 +158,39 @@ public void compareText (boolean mustEqual) {
136158
}
137159
}
138160

139-
public void doTest() throws Exception {
161+
private static void test() throws Exception {
140162
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);
174195
}
175196
}

0 commit comments

Comments
 (0)