Path: blob/master/test/jdk/sanity/client/SwingSet/src/FrameDemoTest.java
41153 views
/*1* Copyright (c) 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 static com.sun.swingset3.demos.frame.FrameDemo.BUSY_CHECKBOX;24import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL;25import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL_COLOR;26import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL_SIZE;27import static com.sun.swingset3.demos.frame.FrameDemo.DEMO_TITLE;28import static com.sun.swingset3.demos.frame.FrameDemo.INTERNAL_FRAME;29import static com.sun.swingset3.demos.frame.FrameDemo.MENU;30import static com.sun.swingset3.demos.frame.FrameDemo.MENU_ITEM1;31import static com.sun.swingset3.demos.frame.FrameDemo.MENU_ITEM2;32import static com.sun.swingset3.demos.frame.FrameDemo.SHOW_BUTTON;33import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL;34import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL_BORDER;35import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL_HOR_ALIGNMENT;36import static com.sun.swingset3.demos.frame.FrameDemo.TOOLBAR_BUTTON;37import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;38import static org.testng.AssertJUnit.assertEquals;3940import java.awt.Component;41import java.awt.Cursor;42import java.awt.Dimension;43import java.awt.Frame;44import java.awt.Point;45import java.util.concurrent.atomic.AtomicBoolean;4647import javax.swing.JLabel;48import javax.swing.JMenu;49import javax.swing.JMenuBar;50import javax.swing.JMenuItem;51import javax.swing.UIManager;52import javax.swing.event.MenuEvent;53import javax.swing.event.MenuListener;5455import org.jtregext.GuiTestListener;56import org.netbeans.jemmy.ClassReference;57import org.netbeans.jemmy.ComponentChooser;58import org.netbeans.jemmy.WindowWaiter;59import org.netbeans.jemmy.operators.ComponentOperator;60import org.netbeans.jemmy.operators.FrameOperator;61import org.netbeans.jemmy.operators.JButtonOperator;62import org.netbeans.jemmy.operators.JCheckBoxOperator;63import org.netbeans.jemmy.operators.JFrameOperator;64import org.netbeans.jemmy.operators.JLabelOperator;65import org.netbeans.jemmy.operators.JMenuBarOperator;66import org.netbeans.jemmy.operators.JMenuItemOperator;67import org.netbeans.jemmy.operators.JMenuOperator;68import org.testng.annotations.Listeners;69import org.testng.annotations.Test;7071import com.sun.swingset3.demos.frame.FrameDemo;7273/*74* @test75* @key headful76* @summary Verifies SwingSet3 FrameDemo page by checking the different actions77* on the frame, properties and different actions on subcomponents of frame78* and control panel actions by checking and unchecking the busy check box and79* pressing the show button.80*81* @library /sanity/client/lib/jemmy/src82* @library /sanity/client/lib/Extensions/src83* @library /sanity/client/lib/SwingSet3/src84* @modules java.desktop85* java.logging86* @build org.jemmy2ext.JemmyExt87* @build com.sun.swingset3.demos.frame.FrameDemo88* @run testng/timeout=600 FrameDemoTest89*/90@Listeners(GuiTestListener.class)91public class FrameDemoTest {9293private final static Dimension NEW_SIZE = new Dimension(500, 500);94private final static Point NEW_LOCATION = new Point(200, 200);95private final static int NUMBER_OF_MENUS = 1;96private final static int NUMBER_OF_MENU_ITEMS = 2;97private final static int MAX_NUMBER_OF_FRAMES = 1;98private final static int DELAY_AFTER_SHOW_BUTTON_PRESS = 500;99100/**101* Testing the different actions on the frame, properties and different102* actions on subcomponents of the frame and control panel action by103* checking and unchecking the busy check box and pressing the show button.104*105* @throws Exception106*/107@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)108public void test(String lookAndFeel) throws Exception {109UIManager.setLookAndFeel(lookAndFeel);110new ClassReference(FrameDemo.class.getCanonicalName()).startApplication();111112JFrameOperator masterFrameOperator = new JFrameOperator(DEMO_TITLE);113masterFrameOperator.setComparator(EXACT_STRING_COMPARATOR);114115JFrameOperator internalFrameOperator = new JFrameOperator(INTERNAL_FRAME);116internalFrameOperator.setComparator(EXACT_STRING_COMPARATOR);117internalFrameOperator.setVerification(true);118internalFrameOperator.waitComponentVisible(true);119120checkSubComponents(internalFrameOperator);121checkFrameActions(internalFrameOperator);122checkControlPanelActions(masterFrameOperator, internalFrameOperator);123}124125/**126* Verifying the status of added components to the frame127* @param internalFrameOperator128*/129private void checkSubComponents(JFrameOperator internalFrameOperator) {130// Verifying the properties of added button to the frame131JButtonOperator buttonOperator =132new JButtonOperator(internalFrameOperator, TOOLBAR_BUTTON);133AtomicBoolean buttonActionStatus = new AtomicBoolean(false);134buttonOperator.addActionListener(event -> buttonActionStatus.set(true));135buttonOperator.push();136buttonOperator.waitStateOnQueue(comp -> buttonActionStatus.get());137138// Verifying the properties of added labels to the frame139JLabelOperator contentLabelOperator =140new JLabelOperator(internalFrameOperator, CONTENT_LABEL);141contentLabelOperator.waitStateOnQueue(comp142-> CONTENT_LABEL_SIZE.equals(comp.getSize()));143contentLabelOperator.waitStateOnQueue(comp144-> CONTENT_LABEL_COLOR.equals(comp.getBackground()));145JLabelOperator statusLabelOperator =146new JLabelOperator(internalFrameOperator, STATUS_LABEL);147statusLabelOperator.waitStateOnQueue(comp148-> STATUS_LABEL_BORDER.equals(((JLabel)comp).getBorder()));149statusLabelOperator.waitStateOnQueue((component) -> STATUS_LABEL_HOR_ALIGNMENT150== ((JLabel)component).getHorizontalAlignment());151152// Verifying the properties of added menu to the frame153JMenuBarOperator menuBarOperator = new JMenuBarOperator(internalFrameOperator);154menuBarOperator.waitStateOnQueue(comp -> NUMBER_OF_MENUS155== ((JMenuBar)comp).getMenuCount());156JMenuOperator menuOperator = new JMenuOperator(internalFrameOperator, MENU);157menuOperator.waitStateOnQueue(comp -> NUMBER_OF_MENU_ITEMS158== ((JMenu)comp).getMenuComponentCount());159AtomicBoolean menuActionStatus = new AtomicBoolean(false);160addMenuListener(menuOperator, menuActionStatus);161menuOperator.push();162menuOperator.waitStateOnQueue(comp -> menuActionStatus.get());163164// Verifying the properties of the menu items165checkMenuItem((JMenuItem) menuOperator.getMenuComponent(0), MENU_ITEM1);166checkMenuItem((JMenuItem) menuOperator.getMenuComponent(1), MENU_ITEM2);167}168169/**170* Verifying different actions on the frame171* @param internalFrameOperator172*/173private void checkFrameActions(JFrameOperator internalFrameOperator)174throws InterruptedException {175// Verifying the maximized status176internalFrameOperator.maximize();177// TODO This is a workaround for JDK-8210638, this delay has to remove178// after fixing this bug, this is an unstable code.179TestHelpers.delayBetweenFrameStateChange();180internalFrameOperator.demaximize();181// TODO This is a workaround for JDK-8210638, this delay has to remove182// after fixing this bug, this is an unstable code.183TestHelpers.delayBetweenFrameStateChange();184185// Verifying the iconified status186internalFrameOperator.iconify();187// TODO This is a workaround for JDK-8210638, this delay has to remove188// after fixing this bug, this is an unstable code.189TestHelpers.delayBetweenFrameStateChange();190internalFrameOperator.deiconify();191// TODO This is a workaround for JDK-8210638, this delay has to remove192// after fixing this bug, this is an unstable code.193TestHelpers.delayBetweenFrameStateChange();194195// Verifying the resize of the frame196TestHelpers.checkChangeSize(internalFrameOperator, NEW_SIZE);197198// Verifying the change of location of the frame199TestHelpers.checkChangeLocation(internalFrameOperator, NEW_LOCATION);200}201202/**203* Verifying control panel actions on the frame204* @param masterFrameOprator205* @param internalFrameOperator206* @throws InterruptedException207*/208private void checkControlPanelActions(JFrameOperator masterFrameOprator,209JFrameOperator internalFrameOperator) throws InterruptedException {210// Verifying the visibility and number of frames after pressing Show Button211internalFrameOperator.requestClose();212internalFrameOperator.waitClosed();213JButtonOperator showButtonOperator = new JButtonOperator(masterFrameOprator, SHOW_BUTTON);214showButtonOperator.push();215internalFrameOperator.waitComponentVisible(true);216showButtonOperator.push();217showButtonOperator.push();218Thread.sleep(DELAY_AFTER_SHOW_BUTTON_PRESS);219int count = WindowWaiter.countWindows(masterFrameOprator.getOwner(),220new FrameOperator.FrameFinder(new ComponentChooser() {221222@Override223public String getDescription() {224return "frames with name != " + DEMO_TITLE;225}226227@Override228public boolean checkComponent(Component comp) {229return comp.isShowing()230&& ((Frame) comp).getTitle() != DEMO_TITLE;231}232}));233assertEquals("Number of frames after clicking Show Button two times"234+ " validation failed,", MAX_NUMBER_OF_FRAMES, count);235236// Verifying the visibility and cursor type after selecting busy check box237JCheckBoxOperator busyCheckBoxOperator =238new JCheckBoxOperator(masterFrameOprator, BUSY_CHECKBOX);239busyCheckBoxOperator.setVerification(true);240checkBusyCheckBox(internalFrameOperator, busyCheckBoxOperator, true);241internalFrameOperator.waitStateOnQueue(comp -> Cursor.WAIT_CURSOR242== internalFrameOperator.getGlassPane().getCursor().getType());243244checkBusyCheckBox(internalFrameOperator, busyCheckBoxOperator, false);245internalFrameOperator.waitStateOnQueue(comp -> Cursor.DEFAULT_CURSOR246== internalFrameOperator.getCursor().getType());247}248249private void checkBusyCheckBox(JFrameOperator internalFrameOperator,250JCheckBoxOperator busyCheckBoxOperator, boolean isSelect) {251busyCheckBoxOperator.changeSelection(isSelect);252new ComponentOperator(internalFrameOperator.253getGlassPane()).waitComponentVisible(isSelect);254}255256/**257* Verifying the properties of the menu item258* @param menuItem : menu item component259* @param menuExpectedName : expected menu item name/text260*/261private void checkMenuItem(JMenuItem menuItem, String menuExpectedName) {262JMenuItemOperator menuItemOperator = new JMenuItemOperator(menuItem);263AtomicBoolean menuItemActionStatus = new AtomicBoolean(false);264menuItemOperator.addActionListener(event -> menuItemActionStatus.set(true));265menuItemOperator.waitStateOnQueue((component)266-> menuExpectedName.equals(((JMenuItem)component).getText()));267menuItemOperator.push();268menuItemOperator.waitStateOnQueue(comp -> menuItemActionStatus.get());269}270271/**272* Add menu listener to the operator273* @param menuOperator : JMenuOperator on which menu listener has to be added274* @param menuActionStatus : menu action status variable275*/276private void addMenuListener(JMenuOperator menuOperator,277AtomicBoolean menuActionStatus) {278menuOperator.addMenuListener(new MenuListener() {279280@Override281public void menuSelected(MenuEvent e) {282menuActionStatus.set(true);283}284285@Override286public void menuDeselected(MenuEvent e) {287}288289@Override290public void menuCanceled(MenuEvent e) {291}292});293}294295}296297298