Path: blob/master/test/jdk/sanity/client/SwingSet/src/ButtonDemoTest.java
41153 views
/*1* Copyright (c) 2011, 2018, 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 org.jtregext.GuiTestListener;24import com.sun.swingset3.demos.JHyperlink;25import com.sun.swingset3.demos.button.ButtonDemo;26import java.util.concurrent.ArrayBlockingQueue;27import java.util.concurrent.BlockingQueue;28import javax.swing.ButtonModel;29import javax.swing.JButton;30import javax.swing.UIManager;31import javax.swing.event.ChangeEvent;32import static org.testng.AssertJUnit.*;33import org.testng.annotations.Test;34import org.jemmy2ext.JemmyExt.ByToolTipChooser;35import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;36import org.netbeans.jemmy.ClassReference;37import org.netbeans.jemmy.operators.JButtonOperator;38import org.netbeans.jemmy.operators.JFrameOperator;39import static com.sun.swingset3.demos.button.ButtonDemo.*;40import org.jemmy2ext.JemmyExt;41import org.jemmy2ext.JemmyExt.MultiThreadedTryCatch;42import org.testng.annotations.Listeners;4344/*45* @test46* @key headful47* @summary Verifies buttons on SwingSet3 ButtonDemo page by clicking each button48* and checking model change events. It also verifies tooltips and text49* on buttons before and after click.50*51* @library /sanity/client/lib/jemmy/src52* @library /sanity/client/lib/Extensions/src53* @library /sanity/client/lib/SwingSet3/src54* @modules java.desktop55* java.logging56* @build org.jemmy2ext.JemmyExt57* @build com.sun.swingset3.demos.button.ButtonDemo58* @run testng/timeout=600 ButtonDemoTest59*/60@Listeners(GuiTestListener.class)61public class ButtonDemoTest {6263private static final String[] BUTTON_TEXT_AFTER = {64DO_IT_AGAIN,};6566private static final String[] BUTTON_TEXT_BEFORE = {67DO_IT,68"",69FIND,70GO,71CONNECT,72"",73GET_MORE_INFO,74null75};7677private static final String[] BUTTON_TOOLTIP = {78SIMPLE_BUTTON,79IMAGE_BUTTON,80BUTTON_WITH_TEXT_AND_IMAGE,81BUTTON_WITH_BACKGROUND_COLOR,82BUTTON_WITH_NO_BORDER,83BUTTON_WITH_ROLLOVER_IMAGE,84JAVA_SE_URL,85JAVA_BLOGS_URL86};8788private static final String[] GOLDEN = {89"isArmed = false, isEnabled = true, isPressed = false, isSelected = false",90"isArmed = true, isEnabled = true, isPressed = false, isSelected = false",91"isArmed = true, isEnabled = true, isPressed = true, isSelected = false",92"isArmed = true, isEnabled = true, isPressed = false, isSelected = false",93"isArmed = false, isEnabled = true, isPressed = false, isSelected = false"94};9596@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)97public void test(String lookAndFeel) throws Exception {98UIManager.setLookAndFeel(lookAndFeel);99100new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication();101102JFrameOperator mainFrame = new JFrameOperator(DEMO_TITLE);103mainFrame.setComparator(EXACT_STRING_COMPARATOR);104105// Check all the buttons106for (int i = 0; i < BUTTON_TOOLTIP.length; i++) {107String tooltip = BUTTON_TOOLTIP[i];108109JButtonOperator button = new JButtonOperator(mainFrame, new ByToolTipChooser(tooltip));110111assertEquals(BUTTON_TEXT_BEFORE[i], button.getText());112113// Two buttons are hyperlinks, we don't want to click them114if (!button.getSource().getClass().equals(JHyperlink.class)) {115checkButton(button);116}117118if (BUTTON_TEXT_AFTER.length > i) {119assertEquals(BUTTON_TEXT_AFTER[i], button.getText());120} else {121assertEquals(BUTTON_TEXT_BEFORE[i], button.getText());122}123}124}125126private void checkButton(JButtonOperator button) throws Exception {127MultiThreadedTryCatch tryCatch = new JemmyExt.MultiThreadedTryCatch();128try {129BlockingQueue<String> modelStateChanges = new ArrayBlockingQueue<>(GOLDEN.length);130button.getQueueTool().invokeAndWait(() -> {131try {132JButton jButton = (JButton) button.getSource();133ButtonModel model = jButton.getModel();134String line = toString(model);135System.out.println("Inital: " + line);136modelStateChanges.add(line);137model.addChangeListener((ChangeEvent e) -> {138try {139String line2 = toString(model);140System.out.println("ChangeEvent: " + line2);141142// We are only interested in the first GOLDEN.length events143if (modelStateChanges.remainingCapacity() > 0) {144modelStateChanges.add(line2);145}146} catch (RuntimeException | Error t) {147tryCatch.register(t);148}149});150} catch (Error error) {151// All exceptions are already handled by Jemmy but Errors are not152tryCatch.register(error);153throw error;154}155});156157assertEquals("Initial state check", GOLDEN[0], modelStateChanges.take());158159button.clickMouse();160161for (int state = 1; state < GOLDEN.length; state++) {162assertEquals(GOLDEN[state], modelStateChanges.take());163}164} catch (RuntimeException | Error | InterruptedException t) {165tryCatch.registerRoot(t);166} finally {167tryCatch.throwRegistered();168}169}170171private static String toString(ButtonModel model) {172return "isArmed = " + model.isArmed()173+ ", isEnabled = " + model.isEnabled()174+ ", isPressed = " + model.isPressed()175+ ", isSelected = " + model.isSelected();176}177178}179180181