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 com.sun.swingset3.demos.frame.FrameDemo.BUSY_CHECKBOX;
25 import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL;
26 import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL_COLOR;
27 import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL_SIZE;
28 import static com.sun.swingset3.demos.frame.FrameDemo.DEMO_TITLE;
29 import static com.sun.swingset3.demos.frame.FrameDemo.INTERNAL_FRAME;
30 import static com.sun.swingset3.demos.frame.FrameDemo.MENU;
31 import static com.sun.swingset3.demos.frame.FrameDemo.MENU_ITEM1;
32 import static com.sun.swingset3.demos.frame.FrameDemo.MENU_ITEM2;
33 import static com.sun.swingset3.demos.frame.FrameDemo.SHOW_BUTTON;
34 import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL;
35 import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL_BORDER;
36 import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL_HOR_ALIGNMENT;
37 import static com.sun.swingset3.demos.frame.FrameDemo.TOOLBAR_BUTTON;
38 import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
39 import static org.testng.AssertJUnit.assertEquals;
40 
41 import java.awt.Component;
42 import java.awt.Cursor;
43 import java.awt.Dimension;
44 import java.awt.Frame;
45 import java.awt.Point;
46 import java.util.concurrent.atomic.AtomicBoolean;
47 
48 import javax.swing.JLabel;
49 import javax.swing.JMenu;
50 import javax.swing.JMenuBar;
51 import javax.swing.JMenuItem;
52 import javax.swing.UIManager;
53 import javax.swing.event.MenuEvent;
54 import javax.swing.event.MenuListener;
55 
56 import org.jtregext.GuiTestListener;
57 import org.netbeans.jemmy.ClassReference;
58 import org.netbeans.jemmy.ComponentChooser;
59 import org.netbeans.jemmy.WindowWaiter;
60 import org.netbeans.jemmy.operators.ComponentOperator;
61 import org.netbeans.jemmy.operators.FrameOperator;
62 import org.netbeans.jemmy.operators.JButtonOperator;
63 import org.netbeans.jemmy.operators.JCheckBoxOperator;
64 import org.netbeans.jemmy.operators.JFrameOperator;
65 import org.netbeans.jemmy.operators.JLabelOperator;
66 import org.netbeans.jemmy.operators.JMenuBarOperator;
67 import org.netbeans.jemmy.operators.JMenuItemOperator;
68 import org.netbeans.jemmy.operators.JMenuOperator;
69 import org.testng.annotations.Listeners;
70 import org.testng.annotations.Test;
71 
72 import com.sun.swingset3.demos.frame.FrameDemo;
73 
74 /*
75  * @test
76  * @key headful
77  * @summary Verifies SwingSet3 FrameDemo page by checking the different actions
78  *  on the frame, properties and different actions on subcomponents of frame
79  *  and control panel actions by checking and unchecking the busy check box and
80  *  pressing the show button.
81  *
82  * @library /sanity/client/lib/jemmy/src
83  * @library /sanity/client/lib/Extensions/src
84  * @library /sanity/client/lib/SwingSet3/src
85  * @modules java.desktop
86  *          java.logging
87  * @build org.jemmy2ext.JemmyExt
88  * @build com.sun.swingset3.demos.frame.FrameDemo
89  * @run testng/timeout=600 FrameDemoTest
90  */
91 @Listeners(GuiTestListener.class)
92 public class FrameDemoTest {
93 
94     private final static Dimension NEW_SIZE = new Dimension(500, 500);
95     private final static Point NEW_LOCATION = new Point(200, 200);
96     private final static int NUMBER_OF_MENUS = 1;
97     private final static int NUMBER_OF_MENU_ITEMS = 2;
98     private final static int MAX_NUMBER_OF_FRAMES = 1;
99     private final static int DELAY_AFTER_SHOW_BUTTON_PRESS = 500;
100 
101     /**
102      * Testing the different actions on the frame, properties and different
103      * actions on subcomponents of the frame and control panel action by
104      * checking and unchecking the busy check box and pressing the show button.
105      *
106      * @throws Exception
107      */
108     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
test(String lookAndFeel)109     public void test(String lookAndFeel) throws Exception {
110         UIManager.setLookAndFeel(lookAndFeel);
111         new ClassReference(FrameDemo.class.getCanonicalName()).startApplication();
112 
113         JFrameOperator masterFrameOperator = new JFrameOperator(DEMO_TITLE);
114         masterFrameOperator.setComparator(EXACT_STRING_COMPARATOR);
115 
116         JFrameOperator internalFrameOperator = new JFrameOperator(INTERNAL_FRAME);
117         internalFrameOperator.setComparator(EXACT_STRING_COMPARATOR);
118         internalFrameOperator.setVerification(true);
119         internalFrameOperator.waitComponentVisible(true);
120 
121         checkSubComponents(internalFrameOperator);
122         checkFrameActions(internalFrameOperator);
123         checkControlPanelActions(masterFrameOperator, internalFrameOperator);
124     }
125 
126     /**
127      * Verifying the status of added components to the frame
128      * @param internalFrameOperator
129      */
checkSubComponents(JFrameOperator internalFrameOperator)130     private void checkSubComponents(JFrameOperator internalFrameOperator) {
131         // Verifying the properties of added button to the frame
132         JButtonOperator buttonOperator =
133                 new JButtonOperator(internalFrameOperator, TOOLBAR_BUTTON);
134         AtomicBoolean buttonActionStatus = new AtomicBoolean(false);
135         buttonOperator.addActionListener(event -> buttonActionStatus.set(true));
136         buttonOperator.push();
137         buttonOperator.waitStateOnQueue(comp -> buttonActionStatus.get());
138 
139         // Verifying the properties of added labels to the frame
140         JLabelOperator contentLabelOperator =
141                 new JLabelOperator(internalFrameOperator, CONTENT_LABEL);
142         contentLabelOperator.waitStateOnQueue(comp
143                 -> CONTENT_LABEL_SIZE.equals(comp.getSize()));
144         contentLabelOperator.waitStateOnQueue(comp
145                 -> CONTENT_LABEL_COLOR.equals(comp.getBackground()));
146         JLabelOperator statusLabelOperator =
147                 new JLabelOperator(internalFrameOperator, STATUS_LABEL);
148         statusLabelOperator.waitStateOnQueue(comp
149                 -> STATUS_LABEL_BORDER.equals(((JLabel)comp).getBorder()));
150         statusLabelOperator.waitStateOnQueue((component) -> STATUS_LABEL_HOR_ALIGNMENT
151                 == ((JLabel)component).getHorizontalAlignment());
152 
153         // Verifying the properties of added menu to the frame
154         JMenuBarOperator menuBarOperator = new JMenuBarOperator(internalFrameOperator);
155         menuBarOperator.waitStateOnQueue(comp -> NUMBER_OF_MENUS
156                 == ((JMenuBar)comp).getMenuCount());
157         JMenuOperator menuOperator = new JMenuOperator(internalFrameOperator, MENU);
158         menuOperator.waitStateOnQueue(comp -> NUMBER_OF_MENU_ITEMS
159                 == ((JMenu)comp).getMenuComponentCount());
160         AtomicBoolean menuActionStatus = new AtomicBoolean(false);
161         addMenuListener(menuOperator, menuActionStatus);
162         menuOperator.push();
163         menuOperator.waitStateOnQueue(comp -> menuActionStatus.get());
164 
165         // Verifying the properties of the menu items
166         checkMenuItem((JMenuItem) menuOperator.getMenuComponent(0), MENU_ITEM1);
167         checkMenuItem((JMenuItem) menuOperator.getMenuComponent(1), MENU_ITEM2);
168     }
169 
170     /**
171      * Verifying different actions on the frame
172      * @param internalFrameOperator
173      */
checkFrameActions(JFrameOperator internalFrameOperator)174     private void checkFrameActions(JFrameOperator internalFrameOperator)
175             throws InterruptedException {
176         // Verifying the maximized status
177         internalFrameOperator.maximize();
178         // TODO This is a workaround for JDK-8210638, this delay has to remove
179         // after fixing this bug, this is an unstable code.
180         TestHelpers.delayBetweenFrameStateChange();
181         internalFrameOperator.demaximize();
182         // TODO This is a workaround for JDK-8210638, this delay has to remove
183         // after fixing this bug, this is an unstable code.
184         TestHelpers.delayBetweenFrameStateChange();
185 
186         // Verifying the iconified status
187         internalFrameOperator.iconify();
188         // TODO This is a workaround for JDK-8210638, this delay has to remove
189         // after fixing this bug, this is an unstable code.
190         TestHelpers.delayBetweenFrameStateChange();
191         internalFrameOperator.deiconify();
192         // TODO This is a workaround for JDK-8210638, this delay has to remove
193         // after fixing this bug, this is an unstable code.
194         TestHelpers.delayBetweenFrameStateChange();
195 
196         // Verifying the resize of the frame
197         TestHelpers.checkChangeSize(internalFrameOperator, NEW_SIZE);
198 
199         // Verifying the change of location of the frame
200         TestHelpers.checkChangeLocation(internalFrameOperator, NEW_LOCATION);
201     }
202 
203     /**
204      * Verifying control panel actions on the frame
205      * @param masterFrameOprator
206      * @param internalFrameOperator
207      * @throws InterruptedException
208      */
checkControlPanelActions(JFrameOperator masterFrameOprator, JFrameOperator internalFrameOperator)209     private void checkControlPanelActions(JFrameOperator masterFrameOprator,
210             JFrameOperator internalFrameOperator) throws InterruptedException {
211         // Verifying the visibility and number of frames after pressing Show Button
212         internalFrameOperator.requestClose();
213         internalFrameOperator.waitClosed();
214         JButtonOperator showButtonOperator = new JButtonOperator(masterFrameOprator, SHOW_BUTTON);
215         showButtonOperator.push();
216         internalFrameOperator.waitComponentVisible(true);
217         showButtonOperator.push();
218         showButtonOperator.push();
219         Thread.sleep(DELAY_AFTER_SHOW_BUTTON_PRESS);
220         int count = WindowWaiter.countWindows(masterFrameOprator.getOwner(),
221                 new FrameOperator.FrameFinder(new ComponentChooser() {
222 
223             @Override
224             public String getDescription() {
225                 return "frames with name != " + DEMO_TITLE;
226             }
227 
228             @Override
229             public boolean checkComponent(Component comp) {
230                 return comp.isShowing()
231                         && ((Frame) comp).getTitle() != DEMO_TITLE;
232             }
233         }));
234         assertEquals("Number of frames after clicking Show Button two times"
235                 + " validation failed,", MAX_NUMBER_OF_FRAMES, count);
236 
237         // Verifying the visibility and cursor type after selecting busy check box
238         JCheckBoxOperator busyCheckBoxOperator =
239                 new JCheckBoxOperator(masterFrameOprator, BUSY_CHECKBOX);
240         busyCheckBoxOperator.setVerification(true);
241         checkBusyCheckBox(internalFrameOperator, busyCheckBoxOperator, true);
242         internalFrameOperator.waitStateOnQueue(comp -> Cursor.WAIT_CURSOR
243                 == internalFrameOperator.getGlassPane().getCursor().getType());
244 
245         checkBusyCheckBox(internalFrameOperator, busyCheckBoxOperator, false);
246         internalFrameOperator.waitStateOnQueue(comp -> Cursor.DEFAULT_CURSOR
247                 == internalFrameOperator.getCursor().getType());
248     }
249 
checkBusyCheckBox(JFrameOperator internalFrameOperator, JCheckBoxOperator busyCheckBoxOperator, boolean isSelect)250     private void checkBusyCheckBox(JFrameOperator internalFrameOperator,
251             JCheckBoxOperator busyCheckBoxOperator, boolean isSelect) {
252         busyCheckBoxOperator.changeSelection(isSelect);
253         new ComponentOperator(internalFrameOperator.
254                 getGlassPane()).waitComponentVisible(isSelect);
255     }
256 
257     /**
258      * Verifying the properties of the menu item
259      * @param menuItem : menu item component
260      * @param menuExpectedName : expected menu item name/text
261      */
checkMenuItem(JMenuItem menuItem, String menuExpectedName)262     private void checkMenuItem(JMenuItem menuItem, String menuExpectedName) {
263         JMenuItemOperator menuItemOperator = new JMenuItemOperator(menuItem);
264         AtomicBoolean menuItemActionStatus = new AtomicBoolean(false);
265         menuItemOperator.addActionListener(event -> menuItemActionStatus.set(true));
266         menuItemOperator.waitStateOnQueue((component)
267                 -> menuExpectedName.equals(((JMenuItem)component).getText()));
268         menuItemOperator.push();
269         menuItemOperator.waitStateOnQueue(comp -> menuItemActionStatus.get());
270     }
271 
272     /**
273      * Add menu listener to the operator
274      * @param menuOperator : JMenuOperator on which menu listener has to be added
275      * @param menuActionStatus : menu action status variable
276      */
addMenuListener(JMenuOperator menuOperator, AtomicBoolean menuActionStatus)277     private void addMenuListener(JMenuOperator menuOperator,
278             AtomicBoolean menuActionStatus) {
279         menuOperator.addMenuListener(new MenuListener() {
280 
281             @Override
282             public void menuSelected(MenuEvent e) {
283                 menuActionStatus.set(true);
284             }
285 
286             @Override
287             public void menuDeselected(MenuEvent e) {
288             }
289 
290             @Override
291             public void menuCanceled(MenuEvent e) {
292             }
293         });
294     }
295 
296 }
297