Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sanity/client/SwingSet/src/EditorPaneDemoTest.java
41153 views
1
/*
2
* Copyright (c) 2018, 2020, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import static com.sun.swingset3.demos.editorpane.EditorPaneDemo.DEMO_TITLE;
25
import static com.sun.swingset3.demos.editorpane.EditorPaneDemo.SOURCE_FILES;
26
import static org.jemmy2ext.JemmyExt.*;
27
import static org.testng.Assert.assertFalse;
28
29
import java.awt.Color;
30
import java.awt.Dimension;
31
import java.awt.Insets;
32
import java.awt.Point;
33
import java.awt.Rectangle;
34
import java.awt.image.BufferedImage;
35
import java.beans.PropertyChangeListener;
36
import java.net.URL;
37
import java.util.concurrent.atomic.AtomicReference;
38
39
import javax.swing.UIManager;
40
41
import org.jemmy2ext.JemmyExt;
42
import org.jtregext.GuiTestListener;
43
import org.netbeans.jemmy.ClassReference;
44
import org.netbeans.jemmy.image.ImageTool;
45
import org.netbeans.jemmy.operators.JEditorPaneOperator;
46
import org.netbeans.jemmy.operators.JFrameOperator;
47
import org.testng.annotations.Listeners;
48
import org.testng.annotations.Test;
49
50
import com.sun.swingset3.demos.editorpane.EditorPaneDemo;
51
52
/*
53
* @test
54
* @key headful screenshots
55
* @summary Verifies SwingSet3 EditorPaneDemo by navigating and and validating
56
* the page contents in all pages
57
*
58
* @library /sanity/client/lib/jemmy/src
59
* @library /sanity/client/lib/Extensions/src
60
* @library /sanity/client/lib/SwingSet3/src
61
* @modules java.desktop
62
* java.logging
63
* @build org.jemmy2ext.JemmyExt
64
* @build com.sun.swingset3.demos.editorpane.EditorPaneDemo
65
* @run testng/timeout=600 EditorPaneDemoTest
66
*/
67
@Listeners(GuiTestListener.class)
68
public class EditorPaneDemoTest {
69
70
private final static String PROPERTY_NAME_PAGE = "page";
71
private final static String INDEX_PAGE_NAME = "index.html";
72
private final static String TEXT_IN_INDEX_PAGE = "Octavo Corporation";
73
private final static Dimension INDEX_IMAGE_DIMENSION = new Dimension(550, 428);
74
private final static Dimension imageDimensions[] = {new Dimension(320, 342),
75
new Dimension(420, 290), new Dimension(381, 384),
76
new Dimension(316, 498), new Dimension(481 ,325),
77
new Dimension(516, 445)};
78
private final static String REFERENCE_NAMES[] =
79
{"title", "king", "preface", "seaweed", "ant", "bug"};
80
private final static String TEXTS_IN_PAGES[] =
81
{"Physiological Descriptions", "ROBERT HOOKE",
82
"Mankind above other Creatures", "Area A B C D",
83
"Observ. XLIX", "Cylinder F F F"};
84
private final AtomicReference<URL> newPageURL = new AtomicReference<>();
85
86
/**
87
* Testing the navigation through all html pages in EditorPaneDemo by
88
* clicking on different references and validating the page contents.
89
*
90
* @throws Exception
91
*/
92
@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
93
public void test(String lookAndFeel) throws Exception {
94
UIManager.setLookAndFeel(lookAndFeel);
95
new ClassReference(EditorPaneDemo.class.getCanonicalName()).startApplication();
96
97
JFrameOperator frameOperator = new JFrameOperator(DEMO_TITLE);
98
frameOperator.setComparator(EXACT_STRING_COMPARATOR);
99
PropertyChangeListener pageChangeListener =
100
event -> newPageURL.set((URL) event.getNewValue());
101
JEditorPaneOperator editorPaneOperator = new JEditorPaneOperator(frameOperator);
102
103
try {
104
editorPaneOperator.addPropertyChangeListener(
105
PROPERTY_NAME_PAGE, pageChangeListener);
106
// Validation of initial or index page
107
URL indexURL = getPageURL(INDEX_PAGE_NAME);
108
editorPaneOperator.waitStateOnQueue(comp
109
-> indexURL.equals(editorPaneOperator.getPage()));
110
checkImage(editorPaneOperator, INDEX_IMAGE_DIMENSION, INDEX_PAGE_NAME);
111
checkTextPresence(editorPaneOperator, TEXT_IN_INDEX_PAGE);
112
113
// Clicking on different references and validating pages by selecting
114
// unique texts in each page
115
for (int i = 0; i < REFERENCE_NAMES.length; i++) {
116
editorPaneOperator.clickOnReference(REFERENCE_NAMES[i]);
117
validatePage(editorPaneOperator, i);
118
}
119
} finally {
120
editorPaneOperator.removePropertyChangeListener(
121
PROPERTY_NAME_PAGE, pageChangeListener);
122
}
123
}
124
125
private void checkTextPresence(
126
JEditorPaneOperator editorPaneOperator, String text) {
127
editorPaneOperator.selectText(text);
128
editorPaneOperator.waitStateOnQueue(comp
129
-> text.equals(editorPaneOperator.getSelectedText()));
130
}
131
132
private void validatePage(JEditorPaneOperator editorPaneOperator,
133
int i) throws Exception {
134
URL expectedPageURL = getPageURL(REFERENCE_NAMES[i] + ".html");
135
editorPaneOperator.waitStateOnQueue(comp
136
-> expectedPageURL.equals(newPageURL.get()));
137
checkImage(editorPaneOperator, imageDimensions[i], REFERENCE_NAMES[i]);
138
checkTextPresence(editorPaneOperator, TEXTS_IN_PAGES[i]);
139
}
140
141
private void checkImage(JEditorPaneOperator editorPaneOperator,
142
Dimension imageDim, String pageName) throws Exception {
143
// Captures image screen shot and checking some 10 pixels from inner
144
// area of the image are not default background color
145
Point compLoc = editorPaneOperator.getLocationOnScreen();
146
Insets insets = editorPaneOperator.getInsets();
147
Rectangle imageRect = new Rectangle(new Point(compLoc.x + insets.left,
148
compLoc.y + insets.top), imageDim);
149
final int xGap = 100, yGap = 40, columns = 2, rows = 5;
150
editorPaneOperator.waitState(comp -> {
151
BufferedImage capturedImage = ImageTool.getImage(imageRect);
152
save(capturedImage, "editor");
153
assertFalse(isBlack(capturedImage), "image blackness");
154
int x = 0, y = 0, i = 0, j;
155
for (; i < columns; i++) {
156
x += xGap;
157
y = 0;
158
for (j = 0; j < rows; j++) {
159
y += yGap;
160
if(capturedImage.getRGB(x, y) == Color.WHITE.getRGB()) {
161
// saving image for failure case
162
save(capturedImage, "capturedimage-" + pageName);
163
return false;
164
}
165
}
166
}
167
return true;
168
});
169
}
170
171
/**
172
* Gets the URL corresponding to a page name
173
*
174
* @param pageName : name of the page
175
* @return : URL corresponding to page
176
*/
177
private URL getPageURL(String pageName) {
178
String url = null;
179
for (String sourceFile : SOURCE_FILES) {
180
if(sourceFile.endsWith(pageName)) {
181
url = sourceFile;
182
}
183
}
184
return getClass().getResource(url);
185
}
186
}
187
188