Path: blob/master/test/jdk/sanity/client/SwingSet/src/ButtonDemoScreenshotTest.java
41153 views
/*1* Copyright (c) 2011, 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 com.sun.swingset3.demos.button.ButtonDemo;24import org.jtregext.GuiTestListener;25import org.netbeans.jemmy.ClassReference;26import org.netbeans.jemmy.operators.JButtonOperator;27import org.netbeans.jemmy.operators.JFrameOperator;28import org.testng.annotations.Listeners;29import org.testng.annotations.Test;3031import java.awt.Dimension;32import java.awt.Point;33import java.awt.Rectangle;34import java.awt.Toolkit;35import java.awt.image.BufferedImage;3637import javax.swing.UIManager;38import static com.sun.swingset3.demos.button.ButtonDemo.DEMO_TITLE;39import static org.jemmy2ext.JemmyExt.*;4041/*42* @test43* @key headful screenshots44* @summary Verifies buttons on SwingSet3 ButtonDemo page by clicking each45* button, taking its screenshots and checking that pressed button46* image is different from initial button image.47*48* @library /sanity/client/lib/jemmy/src49* @library /sanity/client/lib/Extensions/src50* @library /sanity/client/lib/SwingSet3/src51* @modules java.desktop52* java.logging53* @build org.jemmy2ext.JemmyExt54* @build com.sun.swingset3.demos.button.ButtonDemo55* @run testng/othervm/timeout=600 -Dswing.disablevistaanimation=true ButtonDemoScreenshotTest56*/57@Listeners(GuiTestListener.class)58public class ButtonDemoScreenshotTest {5960private static final int[] BUTTONS = {0, 1, 2, 3, 4, 5}; // "open browser" buttons (6, 7) open a browser, so ignore6162@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)63public void test(String lookAndFeel) throws Exception {64UIManager.setLookAndFeel(lookAndFeel);6566//capture some of the background67Dimension screeSize = Toolkit.getDefaultToolkit().getScreenSize();68Point screenCenter = new Point(screeSize.width / 2, screeSize.height / 2);69Rectangle center = new Rectangle(70screenCenter.x - 50, screenCenter.y - 50,71100, 100);72BufferedImage background = getRobot().createScreenCapture(center);7374new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication();7576JFrameOperator mainFrame = new JFrameOperator(DEMO_TITLE);77mainFrame.waitComponentShowing(true);7879//make sure the frame is already painted80waitChangedImage(() -> getRobot().createScreenCapture(center),81background, mainFrame.getTimeouts(), "background");82//make sure the frame is painted completely83waitStillImage(mainFrame, "frame");8485// Check all the buttons86for (int i : BUTTONS) {87checkButton(mainFrame, i);88}89}9091private void checkButton(JFrameOperator jfo, int i) throws InterruptedException {92JButtonOperator button = new JButtonOperator(jfo, i);9394//additional instrumentation for JDK-8198920. To be removed after the bug is fixed95java.util.concurrent.atomic.AtomicBoolean actionListenerCalled = new java.util.concurrent.atomic.AtomicBoolean(false);96button.addActionListener(e -> actionListenerCalled.set(true));97//end of instrumentation for JDK-81989209899button.moveMouse(button.getCenterX(), button.getCenterY());100101BufferedImage notPressed, pressed = null;102notPressed = waitStillImage(button, "not-pressed-" + i);103104button.pressMouse();105//additional instrumentation for JDK-8198920. To be removed after the bug is fixed106button.getOutput().printTrace("JDK-8198920: Button pressed at " + System.currentTimeMillis());107//end of instrumentation for JDK-8198920108try {109waitPressed(button);110//additional instrumentation for JDK-8198920. To be removed after the bug is fixed111button.getOutput().printTrace("JDK-8198920: Button press confirmed by " + System.currentTimeMillis());112//end of instrumentation for JDK-8198920113waitChangedImage(() -> capture(button), notPressed,114button.getTimeouts(), "after-press-" + i);115pressed = waitStillImage(button, "pressed-" + i);116} finally {117button.releaseMouse();118if(pressed != null) {119waitChangedImage(() -> capture(button), pressed,120button.getTimeouts(), "released-" + i);121}122//additional instrumentation for JDK-8198920. To be removed after the bug is fixed123button.getOutput().printTrace("JDK-8198920: Button released at " + System.currentTimeMillis());124try {125button.waitState(comp -> actionListenerCalled.get());126button.getOutput().printTrace("JDK-8198920: Action listener was called by " + System.currentTimeMillis());127} catch (org.netbeans.jemmy.TimeoutExpiredException e) {128button.getOutput().printTrace("JDK-8198920: Action listener was not called by " + System.currentTimeMillis());129}130//end of instrumentation for JDK-8198920131}132}133}134135136