1 /*
2  * Copyright (c) 2018, 2019, 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 org.jtregext.GuiTestListener;
25 import com.sun.swingset3.demos.filechooser.FileChooserDemo;
26 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.APPLY_FILTER_TOOLTIP;
27 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.DEMO_TITLE;
28 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_CANCEL_TEXT;
29 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SAVEQUESTION_TITLE;
30 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SAVE_TEXT;
31 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SELECT_TEXT;
32 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SELECT_WITH_PREVIEW;
33 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FLIP_HORIZONTAL_TOOLTIP;
34 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FLIP_VERTICAL_TOOLTIP;
35 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.GRAY;
36 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.ROTATE_LEFT_TOOLTIP;
37 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.ROTATE_RIGHT_TOOLTIP;
38 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getFlipHorizontalCount;
39 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getFlipVerticalCount;
40 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getLastAppliedFilterId;
41 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getRotateLeftCount;
42 import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getRotateRightCount;
43 
44 import java.awt.Container;
45 import java.awt.event.KeyEvent;
46 import java.io.File;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.Paths;
50 import org.testng.annotations.Test;
51 import javax.swing.UIManager;
52 
53 import org.netbeans.jemmy.ClassReference;
54 import org.netbeans.jemmy.operators.JFrameOperator;
55 import org.netbeans.jemmy.operators.JPopupMenuOperator;
56 import org.netbeans.jemmy.operators.JRadioButtonMenuItemOperator;
57 import org.netbeans.jemmy.operators.JButtonOperator;
58 import org.netbeans.jemmy.operators.JComboBoxOperator;
59 import org.netbeans.jemmy.operators.JToggleButtonOperator;
60 import org.netbeans.jemmy.operators.JFileChooserOperator;
61 import org.netbeans.jemmy.operators.JDialogOperator;
62 import org.netbeans.jemmy.operators.JComponentOperator.JComponentByTipFinder;
63 import org.netbeans.jemmy.util.Platform;
64 import org.netbeans.jemmy.util.LookAndFeel;
65 import org.testng.annotations.BeforeTest;
66 import org.testng.annotations.Listeners;
67 import org.jemmy2ext.JemmyExt.ByToolTipChooser;
68 
69 /*
70  * @test
71  * @key headful
72  * @summary Verifies SwingSet3 FileChooserDemo by selecting an image file
73  *  using Select Image and Select with preview, performing image
74  *  transformation operations on the selected image and saving it.
75  *
76  * @library /sanity/client/lib/jemmy/src
77  * @library /sanity/client/lib/Extensions/src
78  * @library /sanity/client/lib/SwingSet3/src
79  * @modules java.desktop
80  *          java.logging
81  * @build org.jemmy2ext.JemmyExt
82  * @build com.sun.swingset3.demos.filechooser.FileChooserDemo
83  * @run testng/timeout=600 FileChooserDemoTest
84  */
85 @Listeners(GuiTestListener.class)
86 public class FileChooserDemoTest {
87 
88     public static final Path TEST_SRC = Paths.get(System.getProperty("test.src"));
89     public static final Path TEST_WORK_DIR = Paths.get(System.getProperty("user.dir"));
90     public static final Path IMAGE_DIR = TEST_SRC.resolve("resources").resolve("images");
91     public static final String IMAGE = "duke.jpg";
92     private static final String YES = "Yes";
93     private static final String NO = "No";
94     private static final String OPEN = "Open";
95     private static final String OK = "OK";
96     private static final String CANCEL = "Cancel";
97     private static final String USER_HOME = "user.home";
98     private static final String DESKTOP = "Desktop";
99     private static final int greyFilterID = 7;
100 
101     private JFrameOperator frame;
102     private JButtonOperator selectImageButton;
103     private JButtonOperator selectWithPreviewButton;
104     private JFileChooserOperator fileChooser;
105     private JButtonOperator upLevelButton;
106     private JButtonOperator rotateLeftButton;
107     private JButtonOperator rotateRightButton;
108     private JButtonOperator flipHorizontalButton;
109     private JButtonOperator flipVerticalButton;
110     private JButtonOperator applyFilterButton;
111     private JButtonOperator saveButton;
112     private JButtonOperator cancelButton;
113     private JButtonOperator yesButton;
114     private JButtonOperator noButton;
115     private JButtonOperator openButton;
116     private JComboBoxOperator filterComboBox;
117     private JDialogOperator confirmationDialog;
118     private JToggleButtonOperator getDetailsToggleButton;
119     private JToggleButtonOperator getListToggleButton;
120     private JDialogOperator fileChooserDialog;
121 
122     @BeforeTest
beforeTest()123     public void beforeTest() throws Exception {
124         Files.copy(IMAGE_DIR.resolve(IMAGE), TEST_WORK_DIR.resolve(IMAGE));
125     }
126 
127     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
test(String lookAndFeel)128     public void test(String lookAndFeel) throws Exception {
129         UIManager.setLookAndFeel(lookAndFeel);
130         new ClassReference(FileChooserDemo.class.getCanonicalName()).startApplication();
131         frame = new JFrameOperator(DEMO_TITLE);
132         initializeSelectImageButtons();
133         initializeImageOperationButtons();
134         checkSelectImage();
135         checkImageOperations();
136         checkSelectWithPreview();
137         checkImageOperations();
138     }
139 
checkSelectImage()140     private void checkSelectImage() throws Exception {
141         selectImageButton.push();
142         fileChooser = new JFileChooserOperator(JFileChooserOperator.findJFileChooser((Container) frame.getSource()));
143         // In Aqua, GTK and Motif L&Fs, JFileChooser does not have
144         // "Go Home", "Up One Level", "Get Details", "Get List" buttons.
145         if (!LookAndFeel.isAqua() && !LookAndFeel.isMotif() && !LookAndFeel.isGTK()) {
146             File previousDirectory = fileChooser.getCurrentDirectory();
147             fileChooser.goHome();
148             // In Windows, pressing goHome navigates to Desktop inside the home directory.
149             // This is the expected behavior for windows.
150             if (!Platform.isWindows()) {
151                 waitCurrentPath(Paths.get(System.getProperty(USER_HOME)));
152             } else {
153                 waitCurrentPath(Paths.get(System.getProperty(USER_HOME)).resolve(DESKTOP));
154             }
155             fileChooser.setCurrentDirectory(previousDirectory);
156             fileChooser.rescanCurrentDirectory();
157             // In Windows and Windows Classic L&F, List and Details views are
158             // implemented as a popup menu item
159             if(LookAndFeel.isWindows() || LookAndFeel.isWindowsClassic()) {
160                 JButtonOperator popupButton = new JButtonOperator(fileChooser, new JComponentByTipFinder(
161                         UIManager.getString("FileChooser.viewMenuButtonToolTipText", fileChooser.getLocale())));
162                 popupButton.push();
163                 JPopupMenuOperator popup = new JPopupMenuOperator();
164                 popup.pushKey(KeyEvent.VK_ENTER);
165                 JRadioButtonMenuItemOperator detailsMenuItem = new JRadioButtonMenuItemOperator(popup, 1);
166                 detailsMenuItem.push();
167                 detailsMenuItem.waitSelected(true);
168                 popupButton.push();
169                 JRadioButtonMenuItemOperator listMenuItem = new JRadioButtonMenuItemOperator(popup);
170                 listMenuItem.push();
171                 listMenuItem.waitSelected(true);
172             } else {
173                 initializeFileChooserButtons();
174                 upLevelButton.push();
175                 waitCurrentPath(previousDirectory.getParentFile().toPath());
176                 fileChooser.setCurrentDirectory(previousDirectory);
177                 fileChooser.rescanCurrentDirectory();
178                 getDetailsToggleButton.push();
179                 getDetailsToggleButton.waitSelected(true);
180                 getListToggleButton.push();
181                 getListToggleButton.waitSelected(true);
182             }
183             // Wait for the count of number of files to be 1
184             fileChooser.waitFileCount(1);
185             fileChooser.selectFile(IMAGE);
186         } else {
187             fileChooser.setCurrentDirectory(TEST_WORK_DIR.toFile());
188             fileChooser.selectFile(IMAGE);
189         }
190         selectImageButton.push();
191         applyFilterButton.waitComponentEnabled();
192     }
193 
checkSelectWithPreview()194     private void checkSelectWithPreview() {
195         selectWithPreviewButton.pushNoBlock();
196         initializeSelectWithPreviewDialog();
197         cancelButton.push();
198         fileChooserDialog.waitClosed();
199         selectWithPreviewButton.pushNoBlock();
200         initializeSelectWithPreviewDialog();
201         fileChooser.selectFile(IMAGE);
202         openButton.push();
203         fileChooserDialog.waitClosed();
204     }
205 
checkImageOperations()206     private void checkImageOperations() throws Exception {
207         // Check Rotate Left Button
208         {
209             int initialValue = getRotateLeftCount();
210             // Push Button and wait for rotateLeftCount count to increment.
211             rotateLeftButton.push();
212             rotateLeftButton.waitState(button -> getRotateLeftCount() == initialValue + 1);
213         }
214         // Check Rotate Right Button
215         {
216             int initialValue = getRotateRightCount();
217             // Push Button and wait for rotateRightCount count to increment.
218             rotateRightButton.push();
219             rotateRightButton.waitState(button -> getRotateRightCount() == initialValue + 1);
220         }
221         // Check Apply Filter Button
222         filterComboBox.selectItem(GRAY);
223         applyFilterButton.push();
224         applyFilterButton.waitState(button -> getLastAppliedFilterId() == greyFilterID);
225         // Check Save and Yes Buttons
226         initializeSaveAndCancelButton();
227         saveButton.pushNoBlock();
228         //Wait for the dialog to show and initialize it
229         waitAndinitializeConfirmationDialog();
230         yesButton.push();
231         waitButtonDisabled(saveButton);
232 
233         // Check Flip Vertical Button
234         {
235             int initialValue = getFlipVerticalCount();
236             // Push Button and wait for flipVerticalCount count to increment.
237             flipVerticalButton.push();
238             flipVerticalButton.waitState(button -> getFlipVerticalCount() == initialValue + 1);
239 
240         }
241         // Check Save and No Buttons
242         saveButton.pushNoBlock();
243         //Wait for the dialog to show and initialize it
244         waitAndinitializeConfirmationDialog();
245         noButton.push();
246         confirmationDialog.waitClosed();
247         saveButton.waitComponentEnabled();
248         // Check Flip Horizontal Button
249         {
250             int initialValue = getFlipHorizontalCount();
251             // Push Button and wait for flipHorizontalCount count to increment.
252             flipHorizontalButton.push();
253             flipHorizontalButton.waitState(button -> getFlipHorizontalCount() == initialValue + 1);
254         }
255         // Check Cancel Button
256         cancelButton.push();
257         waitButtonDisabled(saveButton);
258     }
259 
initializeFileChooserButtons()260     private void initializeFileChooserButtons() {
261         upLevelButton = new JButtonOperator(fileChooser.getUpLevelButton());
262         getDetailsToggleButton = new JToggleButtonOperator(fileChooser.getDetailsToggleButton());
263         getListToggleButton = new JToggleButtonOperator(fileChooser.getListToggleButton());
264     }
265 
initializeSelectImageButtons()266     private void initializeSelectImageButtons() {
267         selectImageButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_SELECT_TEXT);
268         selectWithPreviewButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_SELECT_WITH_PREVIEW);
269     }
270 
initializeSelectWithPreviewDialog()271     private void initializeSelectWithPreviewDialog() {
272         fileChooser = new JFileChooserOperator();
273         fileChooserDialog = new JDialogOperator(OPEN);
274         String openButtonText = OPEN;
275         // In GTK and Motif L&F, open button text is 'OK'
276         if (LookAndFeel.isMotif() || LookAndFeel.isGTK()) {
277             openButtonText = OK;
278         }
279         openButton = new JButtonOperator(fileChooser, openButtonText);
280         cancelButton = new JButtonOperator(fileChooser, CANCEL);
281     }
282 
initializeImageOperationButtons()283     private void initializeImageOperationButtons() {
284         rotateLeftButton = new JButtonOperator(frame,
285                 new ByToolTipChooser(ROTATE_LEFT_TOOLTIP));
286         rotateRightButton = new JButtonOperator(frame,
287                 new ByToolTipChooser(ROTATE_RIGHT_TOOLTIP));
288         flipHorizontalButton = new JButtonOperator(frame,
289                 new ByToolTipChooser(FLIP_HORIZONTAL_TOOLTIP));
290         flipVerticalButton = new JButtonOperator(frame,
291                 new ByToolTipChooser(FLIP_VERTICAL_TOOLTIP));
292         applyFilterButton = new JButtonOperator(frame,
293                 new ByToolTipChooser(APPLY_FILTER_TOOLTIP));
294         filterComboBox = new JComboBoxOperator(frame);
295     }
296 
initializeSaveAndCancelButton()297     private void initializeSaveAndCancelButton() {
298         saveButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_SAVE_TEXT);
299         cancelButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_CANCEL_TEXT);
300     }
301 
waitAndinitializeConfirmationDialog()302     private void waitAndinitializeConfirmationDialog() {
303         //Wait for the dialog to show
304         JDialogOperator.waitJDialog(FILE_CHOOSER_DEMO_SAVEQUESTION_TITLE, true, true);
305         confirmationDialog = new JDialogOperator(FILE_CHOOSER_DEMO_SAVEQUESTION_TITLE);
306         yesButton = new JButtonOperator(confirmationDialog, YES);
307         noButton = new JButtonOperator(confirmationDialog, NO);
308     }
309 
waitButtonDisabled(JButtonOperator button)310     private void waitButtonDisabled(JButtonOperator button) {
311         button.waitState(b -> b.isEnabled() == false);
312     }
313 
waitCurrentPath(Path expectedPath)314     private void waitCurrentPath(Path expectedPath) {
315         //Wait for the current path to be same as expected path
316         fileChooser.waitState(chooser -> fileChooser.getCurrentDirectory().toPath().equals(expectedPath));
317     }
318 
319 }
320 
321