Path: blob/master/test/jdk/sanity/client/SwingSet/src/SwingSet2DemoTest.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 org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;24import static org.testng.Assert.assertTrue;2526import javax.swing.JCheckBoxMenuItem;27import javax.swing.JRadioButtonMenuItem;28import javax.swing.ToolTipManager;29import javax.swing.UIManager;30import javax.swing.plaf.metal.MetalLookAndFeel;3132import org.jtregext.GuiTestListener;33import org.netbeans.jemmy.ClassReference;34import org.netbeans.jemmy.ComponentChooser;35import org.netbeans.jemmy.TimeoutExpiredException;36import org.netbeans.jemmy.Timeouts;37import org.netbeans.jemmy.operators.ComponentOperator;38import org.netbeans.jemmy.operators.JButtonOperator;39import org.netbeans.jemmy.operators.JCheckBoxMenuItemOperator;40import org.netbeans.jemmy.operators.JFrameOperator;41import org.netbeans.jemmy.operators.JMenuOperator;42import org.netbeans.jemmy.operators.JRadioButtonMenuItemOperator;43import org.netbeans.jemmy.operators.JToggleButtonOperator;44import org.netbeans.jemmy.util.NameComponentChooser;45import org.testng.annotations.Listeners;46import org.testng.annotations.Test;4748/*49* @test50* @key headful51* @summary Verifies check box menu item, radio button menu item, nested menus52* and themes using SwingSet2 main window. Testing of other swing components53* are covered in SwingSet3 demo tests.54*55* @library /sanity/client/lib/jemmy/src56* @library /sanity/client/lib/Extensions/src57* @library /sanity/client/lib/SwingSet2/src58* @modules java.desktop59* java.logging60* @build org.jemmy2ext.JemmyExt61* @build SwingSet262* @run testng/timeout=600 SwingSet2DemoTest63*/64@Listeners(GuiTestListener.class)65public class SwingSet2DemoTest {6667private static final String OCEAN_THEME_NAME = "Ocean";68private static final String STEEL_THEME_NAME = "Steel";69private static final int TOOLTIP_DISMISS_DELAY = 60000;70private final static long TOOLTIP_TIMEOUT = 5000;7172/**73* Testing check box menu item, radio button menu item, nested menus and74* themes. Testing of all other main swing components are covered in75* SwingSet3 demo tests.76*77* @throws Exception78*/79@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)80public void test(String lookAndFeel) throws Exception {81UIManager.setLookAndFeel(lookAndFeel);8283new ClassReference(SwingSet2.class.getCanonicalName()).startApplication();84JFrameOperator frameOperator = new JFrameOperator(SwingSet2.FRAME_TITLE);85frameOperator.setComparator(EXACT_STRING_COMPARATOR);8687checkCheckBoxMenuItem(frameOperator);88JMenuOperator themesMenu = new JMenuOperator(frameOperator, SwingSet2.THEMES_MENU_TEXT);89// Themes menu will be enabled only on MetalLookAndFeel90if(themesMenu.isEnabled()) {91checkMenuOnMenuAndRadioButtonMenuItem(frameOperator, themesMenu);92checkThemes(frameOperator, themesMenu);93}94}9596/**97* Verifies the properties of nested menu and radio button menu item98* @param frameOperator99* @param themesMenu100*/101private void checkMenuOnMenuAndRadioButtonMenuItem(102JFrameOperator frameOperator, JMenuOperator themesMenu) {103104themesMenu.push();105themesMenu.waitSelected(true);106// Font menu is a nested menu, selecting that and verifying its107// sub menu items are launched108JMenuOperator fontMenu = new JMenuOperator(frameOperator, SwingSet2.FONT_MENU_TEXT);109JRadioButtonMenuItemOperator fontPlainMenuItem = new JRadioButtonMenuItemOperator(110(JRadioButtonMenuItem)fontMenu.showMenuItem(111SwingSet2.FONT_PLAIN_MENU_TEXT, "/").getSource());112JRadioButtonMenuItemOperator fontBoldMenuItem = new JRadioButtonMenuItemOperator(113(JRadioButtonMenuItem)fontMenu.showMenuItem(114SwingSet2.FONT_BOLD_MENU_TEXT, "/").getSource());115JButtonOperator testComp =116new JButtonOperator(frameOperator, OptionPaneDemo.INPUT_BUTTON);117checkRadioButtonMenuItem(fontBoldMenuItem, fontPlainMenuItem, testComp,118component -> component.getFont().isBold());119checkRadioButtonMenuItem(fontPlainMenuItem, fontBoldMenuItem, testComp,120component -> component.getFont().isPlain());121}122123/**124* Verifies the properties of radio button menu item125* @param pressMenuItem126* @param unPressMenuItem127* @param testComp128* @param validator129*/130private void checkRadioButtonMenuItem(JRadioButtonMenuItemOperator pressMenuItem,131JRadioButtonMenuItemOperator unPressMenuItem,132ComponentOperator testComp, ComponentChooser validator){133134// Selecting a radio button menu item and verifying the pressed menu135// item is selected, other one is unselected and corresponding ui136// changes also happened137pressMenuItem.push();138pressMenuItem.waitSelected(true);139unPressMenuItem.waitSelected(false);140testComp.waitStateOnQueue(validator);141}142143/**144* Verifies the properties of check box menu item145* @param frameOperator146*/147private void checkCheckBoxMenuItem(JFrameOperator frameOperator) {148149ToolTipManager.sharedInstance().setDismissDelay(TOOLTIP_DISMISS_DELAY);150JToggleButtonOperator testComp = new JToggleButtonOperator(151frameOperator, new NameComponentChooser(SwingSet2.getString(152OptionPaneDemo.DEMO_NAME + SwingSet2.NAME_PROP_SUFFIX)));153JMenuOperator optionsMenu = new JMenuOperator(frameOperator, SwingSet2.OPTIONS_MENU_TEXT);154JCheckBoxMenuItemOperator toolTipMenuItem = new JCheckBoxMenuItemOperator(155(JCheckBoxMenuItem)optionsMenu.showMenuItem(156SwingSet2.TOOLTIP_MENU_TEXT, "/").getSource());157// Selecting and deselecting tooltip checkbox menu item and verifying158// tooltip is showing for demo toggle button159toolTipMenuItem.push();160toolTipMenuItem.waitSelected(false);161// Set tooltip timeout as 5 seconds162testComp.getTimeouts().setTimeout("JToolTipOperator.WaitToolTipTimeout", TOOLTIP_TIMEOUT);163boolean isToolTipTimeout = false;164try {165testComp.showToolTip();166} catch (TimeoutExpiredException e) {167isToolTipTimeout = true;168}169assertTrue(isToolTipTimeout, "Tooltip is showing even after unchecking the checkbox menu"170+ " item 'Enable Tool Tips'");171toolTipMenuItem.push();172toolTipMenuItem.waitSelected(true);173testComp.showToolTip();174}175176/**177* Verifies the different themes by applying different themes178* @param frameOperator179* @param themesMenu180*/181private void checkThemes(JFrameOperator frameOperator, JMenuOperator themesMenu) {182String themeMenuNames [] = {SwingSet2.OCEAN_MENU_TEXT, SwingSet2.AQUA_MENU_TEXT,183SwingSet2.STEEL_MENU_TEXT, SwingSet2.CHARCOAL_MENU_TEXT,184SwingSet2.CONTRAST_MENU_TEXT, SwingSet2.EMERALD_MENU_TEXT, SwingSet2.RUBY_MENU_TEXT};185String themeNames [] = {OCEAN_THEME_NAME, AquaTheme.NAME, STEEL_THEME_NAME,186CharcoalTheme.NAME, ContrastTheme.NAME, EmeraldTheme.NAME, RubyTheme.NAME};187188for (int i = 0; i < themeMenuNames.length; i++) {189int themeIndex = i;190JRadioButtonMenuItemOperator menuItem = new JRadioButtonMenuItemOperator(191(JRadioButtonMenuItem)themesMenu.showMenuItem(192themeMenuNames[themeIndex], "/").getSource());193menuItem.push();194menuItem.waitSelected(true);195menuItem.waitStateOnQueue(comp -> themeNames[themeIndex].196equals(MetalLookAndFeel.getCurrentTheme().getName()));197}198}199200}201202203