ButtonDemoTest.java revision 14305:566a5f5a9a5a
1/* 2 * Copyright (c) 2011, 2016, 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 24import org.jtregext.GuiTestListener; 25import com.sun.swingset3.demos.JHyperlink; 26import com.sun.swingset3.demos.button.ButtonDemo; 27import java.util.concurrent.ArrayBlockingQueue; 28import java.util.concurrent.BlockingQueue; 29import javax.swing.ButtonModel; 30import javax.swing.JButton; 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; 43 44/* 45 * @test 46 * @key headful 47 * @summary Verifies buttons on SwingSet3 ButtonDemo page by clicking each button 48 * and checking model change events. It also verifies tooltips and text 49 * on buttons before and after click. 50 * 51 * @library /sanity/client/lib/jemmy/src 52 * @library /sanity/client/lib/Extensions/src 53 * @library /sanity/client/lib/SwingSet3/src 54 * @build org.jemmy2ext.JemmyExt 55 * @build com.sun.swingset3.demos.button.ButtonDemo 56 * @run testng ButtonDemoTest 57 */ 58@Listeners(GuiTestListener.class) 59public class ButtonDemoTest { 60 61 private static final String[] BUTTON_TEXT_AFTER = { 62 DO_IT_AGAIN,}; 63 64 private static final String[] BUTTON_TEXT_BEFORE = { 65 DO_IT, 66 "", 67 FIND, 68 GO, 69 CONNECT, 70 "", 71 GET_MORE_INFO, 72 null 73 }; 74 75 private static final String[] BUTTON_TOOLTIP = { 76 SIMPLE_BUTTON, 77 IMAGE_BUTTON, 78 BUTTON_WITH_TEXT_AND_IMAGE, 79 BUTTON_WITH_BACKGROUND_COLOR, 80 BUTTON_WITH_NO_BORDER, 81 BUTTON_WITH_ROLLOVER_IMAGE, 82 JAVA_SE_URL, 83 JAVA_BLOGS_URL 84 }; 85 86 private static final String[] GOLDEN = { 87 "isArmed = false, isEnabled = true, isPressed = false, isSelected = false", 88 "isArmed = true, isEnabled = true, isPressed = false, isSelected = false", 89 "isArmed = true, isEnabled = true, isPressed = true, isSelected = false", 90 "isArmed = true, isEnabled = true, isPressed = false, isSelected = false", 91 "isArmed = false, isEnabled = true, isPressed = false, isSelected = false" 92 }; 93 94 @Test 95 public void test() throws Exception { 96 97 new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication(); 98 99 JFrameOperator mainFrame = new JFrameOperator(DEMO_TITLE); 100 mainFrame.setComparator(EXACT_STRING_COMPARATOR); 101 102 // Check all the buttons 103 for (int i = 0; i < BUTTON_TOOLTIP.length; i++) { 104 String tooltip = BUTTON_TOOLTIP[i]; 105 106 JButtonOperator button = new JButtonOperator(mainFrame, new ByToolTipChooser(tooltip)); 107 108 assertEquals(BUTTON_TEXT_BEFORE[i], button.getText()); 109 110 // Two buttons are hyperlinks, we don't want to click them 111 if (!button.getSource().getClass().equals(JHyperlink.class)) { 112 checkButton(button); 113 } 114 115 if (BUTTON_TEXT_AFTER.length > i) { 116 assertEquals(BUTTON_TEXT_AFTER[i], button.getText()); 117 } else { 118 assertEquals(BUTTON_TEXT_BEFORE[i], button.getText()); 119 } 120 } 121 } 122 123 private void checkButton(JButtonOperator button) throws Exception { 124 MultiThreadedTryCatch tryCatch = new JemmyExt.MultiThreadedTryCatch(); 125 try { 126 BlockingQueue<String> modelStateChanges = new ArrayBlockingQueue<>(GOLDEN.length); 127 button.getQueueTool().invokeAndWait(() -> { 128 try { 129 JButton jButton = (JButton) button.getSource(); 130 ButtonModel model = jButton.getModel(); 131 String line = toString(model); 132 System.out.println("Inital: " + line); 133 modelStateChanges.add(line); 134 model.addChangeListener((ChangeEvent e) -> { 135 try { 136 String line2 = toString(model); 137 System.out.println("ChangeEvent: " + line2); 138 139 // We are only interested in the first GOLDEN.length events 140 if (modelStateChanges.remainingCapacity() > 0) { 141 modelStateChanges.add(line2); 142 } 143 } catch (RuntimeException | Error t) { 144 tryCatch.register(t); 145 } 146 }); 147 } catch (Error error) { 148 // All exceptions are already handled by Jemmy but Errors are not 149 tryCatch.register(error); 150 throw error; 151 } 152 }); 153 154 assertEquals("Initial state check", GOLDEN[0], modelStateChanges.take()); 155 156 button.clickMouse(); 157 158 for (int state = 1; state < GOLDEN.length; state++) { 159 assertEquals(GOLDEN[state], modelStateChanges.take()); 160 } 161 } catch (RuntimeException | Error | InterruptedException t) { 162 tryCatch.registerRoot(t); 163 } finally { 164 tryCatch.throwRegistered(); 165 } 166 } 167 168 private static String toString(ButtonModel model) { 169 return "isArmed = " + model.isArmed() 170 + ", isEnabled = " + model.isEnabled() 171 + ", isPressed = " + model.isPressed() 172 + ", isSelected = " + model.isSelected(); 173 } 174 175} 176