1 /*
2  * Copyright (c) 2018, 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 
24 import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
25 import static org.testng.Assert.assertTrue;
26 
27 import javax.swing.JCheckBoxMenuItem;
28 import javax.swing.JRadioButtonMenuItem;
29 import javax.swing.ToolTipManager;
30 import javax.swing.UIManager;
31 import javax.swing.plaf.metal.MetalLookAndFeel;
32 
33 import org.jtregext.GuiTestListener;
34 import org.netbeans.jemmy.ClassReference;
35 import org.netbeans.jemmy.ComponentChooser;
36 import org.netbeans.jemmy.TimeoutExpiredException;
37 import org.netbeans.jemmy.Timeouts;
38 import org.netbeans.jemmy.operators.ComponentOperator;
39 import org.netbeans.jemmy.operators.JButtonOperator;
40 import org.netbeans.jemmy.operators.JCheckBoxMenuItemOperator;
41 import org.netbeans.jemmy.operators.JFrameOperator;
42 import org.netbeans.jemmy.operators.JMenuOperator;
43 import org.netbeans.jemmy.operators.JRadioButtonMenuItemOperator;
44 import org.netbeans.jemmy.operators.JToggleButtonOperator;
45 import org.netbeans.jemmy.util.NameComponentChooser;
46 import org.testng.annotations.Listeners;
47 import org.testng.annotations.Test;
48 
49 /*
50  * @test
51  * @key headful
52  * @summary Verifies check box menu item, radio button menu item, nested menus
53  * and themes using SwingSet2 main window. Testing of other swing components
54  * are covered in SwingSet3 demo tests.
55  *
56  * @library /sanity/client/lib/jemmy/src
57  * @library /sanity/client/lib/Extensions/src
58  * @library /sanity/client/lib/SwingSet2/src
59  * @modules java.desktop
60  *          java.logging
61  * @build org.jemmy2ext.JemmyExt
62  * @build SwingSet2
63  * @run testng/timeout=600 SwingSet2DemoTest
64  */
65 @Listeners(GuiTestListener.class)
66 public class SwingSet2DemoTest {
67 
68     private static final String OCEAN_THEME_NAME = "Ocean";
69     private static final String STEEL_THEME_NAME = "Steel";
70     private static final int TOOLTIP_DISMISS_DELAY = 60000;
71     private final static long TOOLTIP_TIMEOUT = 5000;
72 
73     /**
74      * Testing check box menu item, radio button menu item, nested menus and
75      * themes. Testing of all other main swing components are covered in
76      * SwingSet3 demo tests.
77      *
78      * @throws Exception
79      */
80     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
test(String lookAndFeel)81     public void test(String lookAndFeel) throws Exception {
82         UIManager.setLookAndFeel(lookAndFeel);
83 
84         new ClassReference(SwingSet2.class.getCanonicalName()).startApplication();
85         JFrameOperator frameOperator = new JFrameOperator(SwingSet2.FRAME_TITLE);
86         frameOperator.setComparator(EXACT_STRING_COMPARATOR);
87 
88         checkCheckBoxMenuItem(frameOperator);
89         JMenuOperator themesMenu = new JMenuOperator(frameOperator, SwingSet2.THEMES_MENU_TEXT);
90         // Themes menu will be enabled only on MetalLookAndFeel
91         if(themesMenu.isEnabled()) {
92             checkMenuOnMenuAndRadioButtonMenuItem(frameOperator, themesMenu);
93             checkThemes(frameOperator, themesMenu);
94         }
95     }
96 
97     /**
98      * Verifies the properties of nested menu and radio button menu item
99      * @param frameOperator
100      * @param themesMenu
101      */
checkMenuOnMenuAndRadioButtonMenuItem( JFrameOperator frameOperator, JMenuOperator themesMenu)102     private void checkMenuOnMenuAndRadioButtonMenuItem(
103             JFrameOperator frameOperator, JMenuOperator themesMenu) {
104 
105         themesMenu.push();
106         themesMenu.waitSelected(true);
107         // Font menu is a nested menu, selecting that and verifying its
108         // sub menu items are launched
109         JMenuOperator fontMenu = new JMenuOperator(frameOperator, SwingSet2.FONT_MENU_TEXT);
110         JRadioButtonMenuItemOperator fontPlainMenuItem = new JRadioButtonMenuItemOperator(
111                 (JRadioButtonMenuItem)fontMenu.showMenuItem(
112                         SwingSet2.FONT_PLAIN_MENU_TEXT, "/").getSource());
113         JRadioButtonMenuItemOperator fontBoldMenuItem = new JRadioButtonMenuItemOperator(
114                 (JRadioButtonMenuItem)fontMenu.showMenuItem(
115                         SwingSet2.FONT_BOLD_MENU_TEXT, "/").getSource());
116         JButtonOperator testComp =
117                 new JButtonOperator(frameOperator, OptionPaneDemo.INPUT_BUTTON);
118         checkRadioButtonMenuItem(fontBoldMenuItem, fontPlainMenuItem, testComp,
119                 component -> component.getFont().isBold());
120         checkRadioButtonMenuItem(fontPlainMenuItem, fontBoldMenuItem, testComp,
121                 component -> component.getFont().isPlain());
122     }
123 
124     /**
125      * Verifies the properties of radio button menu item
126      * @param pressMenuItem
127      * @param unPressMenuItem
128      * @param testComp
129      * @param validator
130      */
checkRadioButtonMenuItem(JRadioButtonMenuItemOperator pressMenuItem, JRadioButtonMenuItemOperator unPressMenuItem, ComponentOperator testComp, ComponentChooser validator)131     private void checkRadioButtonMenuItem(JRadioButtonMenuItemOperator pressMenuItem,
132             JRadioButtonMenuItemOperator unPressMenuItem,
133             ComponentOperator testComp, ComponentChooser validator){
134 
135         // Selecting a radio button menu item and verifying the pressed menu
136         // item is selected, other one is unselected and corresponding ui
137         // changes also happened
138         pressMenuItem.push();
139         pressMenuItem.waitSelected(true);
140         unPressMenuItem.waitSelected(false);
141         testComp.waitStateOnQueue(validator);
142     }
143 
144     /**
145      * Verifies the properties of check box menu item
146      * @param frameOperator
147      */
checkCheckBoxMenuItem(JFrameOperator frameOperator)148     private void checkCheckBoxMenuItem(JFrameOperator frameOperator) {
149 
150         ToolTipManager.sharedInstance().setDismissDelay(TOOLTIP_DISMISS_DELAY);
151         JToggleButtonOperator testComp = new JToggleButtonOperator(
152                 frameOperator, new NameComponentChooser(SwingSet2.getString(
153                         OptionPaneDemo.DEMO_NAME + SwingSet2.NAME_PROP_SUFFIX)));
154         JMenuOperator optionsMenu = new JMenuOperator(frameOperator, SwingSet2.OPTIONS_MENU_TEXT);
155         JCheckBoxMenuItemOperator toolTipMenuItem = new JCheckBoxMenuItemOperator(
156                 (JCheckBoxMenuItem)optionsMenu.showMenuItem(
157                         SwingSet2.TOOLTIP_MENU_TEXT, "/").getSource());
158         // Selecting and deselecting tooltip checkbox menu item and verifying
159         // tooltip is showing for demo toggle button
160         toolTipMenuItem.push();
161         toolTipMenuItem.waitSelected(false);
162         // Set tooltip timeout as 5 seconds
163         testComp.getTimeouts().setTimeout("JToolTipOperator.WaitToolTipTimeout", TOOLTIP_TIMEOUT);
164         boolean isToolTipTimeout = false;
165         try {
166             testComp.showToolTip();
167         } catch (TimeoutExpiredException e) {
168             isToolTipTimeout = true;
169         }
170         assertTrue(isToolTipTimeout, "Tooltip is showing even after unchecking the checkbox menu"
171                 + " item 'Enable Tool Tips'");
172         toolTipMenuItem.push();
173         toolTipMenuItem.waitSelected(true);
174         testComp.showToolTip();
175     }
176 
177     /**
178      * Verifies the different themes by applying different themes
179      * @param frameOperator
180      * @param themesMenu
181      */
checkThemes(JFrameOperator frameOperator, JMenuOperator themesMenu)182     private void checkThemes(JFrameOperator frameOperator, JMenuOperator themesMenu) {
183         String themeMenuNames [] = {SwingSet2.OCEAN_MENU_TEXT, SwingSet2.AQUA_MENU_TEXT,
184                 SwingSet2.STEEL_MENU_TEXT, SwingSet2.CHARCOAL_MENU_TEXT,
185                 SwingSet2.CONTRAST_MENU_TEXT, SwingSet2.EMERALD_MENU_TEXT, SwingSet2.RUBY_MENU_TEXT};
186         String themeNames [] = {OCEAN_THEME_NAME, AquaTheme.NAME, STEEL_THEME_NAME,
187                 CharcoalTheme.NAME, ContrastTheme.NAME, EmeraldTheme.NAME, RubyTheme.NAME};
188 
189         for (int i = 0; i < themeMenuNames.length; i++) {
190             int themeIndex = i;
191             JRadioButtonMenuItemOperator menuItem = new JRadioButtonMenuItemOperator(
192                     (JRadioButtonMenuItem)themesMenu.showMenuItem(
193                             themeMenuNames[themeIndex], "/").getSource());
194             menuItem.push();
195             menuItem.waitSelected(true);
196             menuItem.waitStateOnQueue(comp -> themeNames[themeIndex].
197                     equals(MetalLookAndFeel.getCurrentTheme().getName()));
198         }
199     }
200 
201 }
202