Path: blob/master/test/jdk/sanity/client/SwingSet/src/EditorPaneDemoTest.java
41153 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import static com.sun.swingset3.demos.editorpane.EditorPaneDemo.DEMO_TITLE;24import static com.sun.swingset3.demos.editorpane.EditorPaneDemo.SOURCE_FILES;25import static org.jemmy2ext.JemmyExt.*;26import static org.testng.Assert.assertFalse;2728import java.awt.Color;29import java.awt.Dimension;30import java.awt.Insets;31import java.awt.Point;32import java.awt.Rectangle;33import java.awt.image.BufferedImage;34import java.beans.PropertyChangeListener;35import java.net.URL;36import java.util.concurrent.atomic.AtomicReference;3738import javax.swing.UIManager;3940import org.jemmy2ext.JemmyExt;41import org.jtregext.GuiTestListener;42import org.netbeans.jemmy.ClassReference;43import org.netbeans.jemmy.image.ImageTool;44import org.netbeans.jemmy.operators.JEditorPaneOperator;45import org.netbeans.jemmy.operators.JFrameOperator;46import org.testng.annotations.Listeners;47import org.testng.annotations.Test;4849import com.sun.swingset3.demos.editorpane.EditorPaneDemo;5051/*52* @test53* @key headful screenshots54* @summary Verifies SwingSet3 EditorPaneDemo by navigating and and validating55* the page contents in all pages56*57* @library /sanity/client/lib/jemmy/src58* @library /sanity/client/lib/Extensions/src59* @library /sanity/client/lib/SwingSet3/src60* @modules java.desktop61* java.logging62* @build org.jemmy2ext.JemmyExt63* @build com.sun.swingset3.demos.editorpane.EditorPaneDemo64* @run testng/timeout=600 EditorPaneDemoTest65*/66@Listeners(GuiTestListener.class)67public class EditorPaneDemoTest {6869private final static String PROPERTY_NAME_PAGE = "page";70private final static String INDEX_PAGE_NAME = "index.html";71private final static String TEXT_IN_INDEX_PAGE = "Octavo Corporation";72private final static Dimension INDEX_IMAGE_DIMENSION = new Dimension(550, 428);73private final static Dimension imageDimensions[] = {new Dimension(320, 342),74new Dimension(420, 290), new Dimension(381, 384),75new Dimension(316, 498), new Dimension(481 ,325),76new Dimension(516, 445)};77private final static String REFERENCE_NAMES[] =78{"title", "king", "preface", "seaweed", "ant", "bug"};79private final static String TEXTS_IN_PAGES[] =80{"Physiological Descriptions", "ROBERT HOOKE",81"Mankind above other Creatures", "Area A B C D",82"Observ. XLIX", "Cylinder F F F"};83private final AtomicReference<URL> newPageURL = new AtomicReference<>();8485/**86* Testing the navigation through all html pages in EditorPaneDemo by87* clicking on different references and validating the page contents.88*89* @throws Exception90*/91@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)92public void test(String lookAndFeel) throws Exception {93UIManager.setLookAndFeel(lookAndFeel);94new ClassReference(EditorPaneDemo.class.getCanonicalName()).startApplication();9596JFrameOperator frameOperator = new JFrameOperator(DEMO_TITLE);97frameOperator.setComparator(EXACT_STRING_COMPARATOR);98PropertyChangeListener pageChangeListener =99event -> newPageURL.set((URL) event.getNewValue());100JEditorPaneOperator editorPaneOperator = new JEditorPaneOperator(frameOperator);101102try {103editorPaneOperator.addPropertyChangeListener(104PROPERTY_NAME_PAGE, pageChangeListener);105// Validation of initial or index page106URL indexURL = getPageURL(INDEX_PAGE_NAME);107editorPaneOperator.waitStateOnQueue(comp108-> indexURL.equals(editorPaneOperator.getPage()));109checkImage(editorPaneOperator, INDEX_IMAGE_DIMENSION, INDEX_PAGE_NAME);110checkTextPresence(editorPaneOperator, TEXT_IN_INDEX_PAGE);111112// Clicking on different references and validating pages by selecting113// unique texts in each page114for (int i = 0; i < REFERENCE_NAMES.length; i++) {115editorPaneOperator.clickOnReference(REFERENCE_NAMES[i]);116validatePage(editorPaneOperator, i);117}118} finally {119editorPaneOperator.removePropertyChangeListener(120PROPERTY_NAME_PAGE, pageChangeListener);121}122}123124private void checkTextPresence(125JEditorPaneOperator editorPaneOperator, String text) {126editorPaneOperator.selectText(text);127editorPaneOperator.waitStateOnQueue(comp128-> text.equals(editorPaneOperator.getSelectedText()));129}130131private void validatePage(JEditorPaneOperator editorPaneOperator,132int i) throws Exception {133URL expectedPageURL = getPageURL(REFERENCE_NAMES[i] + ".html");134editorPaneOperator.waitStateOnQueue(comp135-> expectedPageURL.equals(newPageURL.get()));136checkImage(editorPaneOperator, imageDimensions[i], REFERENCE_NAMES[i]);137checkTextPresence(editorPaneOperator, TEXTS_IN_PAGES[i]);138}139140private void checkImage(JEditorPaneOperator editorPaneOperator,141Dimension imageDim, String pageName) throws Exception {142// Captures image screen shot and checking some 10 pixels from inner143// area of the image are not default background color144Point compLoc = editorPaneOperator.getLocationOnScreen();145Insets insets = editorPaneOperator.getInsets();146Rectangle imageRect = new Rectangle(new Point(compLoc.x + insets.left,147compLoc.y + insets.top), imageDim);148final int xGap = 100, yGap = 40, columns = 2, rows = 5;149editorPaneOperator.waitState(comp -> {150BufferedImage capturedImage = ImageTool.getImage(imageRect);151save(capturedImage, "editor");152assertFalse(isBlack(capturedImage), "image blackness");153int x = 0, y = 0, i = 0, j;154for (; i < columns; i++) {155x += xGap;156y = 0;157for (j = 0; j < rows; j++) {158y += yGap;159if(capturedImage.getRGB(x, y) == Color.WHITE.getRGB()) {160// saving image for failure case161save(capturedImage, "capturedimage-" + pageName);162return false;163}164}165}166return true;167});168}169170/**171* Gets the URL corresponding to a page name172*173* @param pageName : name of the page174* @return : URL corresponding to page175*/176private URL getPageURL(String pageName) {177String url = null;178for (String sourceFile : SOURCE_FILES) {179if(sourceFile.endsWith(pageName)) {180url = sourceFile;181}182}183return getClass().getResource(url);184}185}186187188