Path: blob/master/test/jdk/sanity/client/SwingSet/src/FileChooserDemoTest.java
41153 views
/*1* Copyright (c) 2018, 2019, 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 org.jtregext.GuiTestListener;24import com.sun.swingset3.demos.filechooser.FileChooserDemo;25import static com.sun.swingset3.demos.filechooser.FileChooserDemo.APPLY_FILTER_TOOLTIP;26import static com.sun.swingset3.demos.filechooser.FileChooserDemo.DEMO_TITLE;27import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_CANCEL_TEXT;28import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SAVEQUESTION_TITLE;29import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SAVE_TEXT;30import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SELECT_TEXT;31import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FILE_CHOOSER_DEMO_SELECT_WITH_PREVIEW;32import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FLIP_HORIZONTAL_TOOLTIP;33import static com.sun.swingset3.demos.filechooser.FileChooserDemo.FLIP_VERTICAL_TOOLTIP;34import static com.sun.swingset3.demos.filechooser.FileChooserDemo.GRAY;35import static com.sun.swingset3.demos.filechooser.FileChooserDemo.ROTATE_LEFT_TOOLTIP;36import static com.sun.swingset3.demos.filechooser.FileChooserDemo.ROTATE_RIGHT_TOOLTIP;37import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getFlipHorizontalCount;38import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getFlipVerticalCount;39import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getLastAppliedFilterId;40import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getRotateLeftCount;41import static com.sun.swingset3.demos.filechooser.FileChooserDemo.getRotateRightCount;4243import java.awt.Container;44import java.awt.event.KeyEvent;45import java.io.File;46import java.nio.file.Files;47import java.nio.file.Path;48import java.nio.file.Paths;49import org.testng.annotations.Test;50import javax.swing.UIManager;5152import org.netbeans.jemmy.ClassReference;53import org.netbeans.jemmy.operators.JFrameOperator;54import org.netbeans.jemmy.operators.JPopupMenuOperator;55import org.netbeans.jemmy.operators.JRadioButtonMenuItemOperator;56import org.netbeans.jemmy.operators.JButtonOperator;57import org.netbeans.jemmy.operators.JComboBoxOperator;58import org.netbeans.jemmy.operators.JToggleButtonOperator;59import org.netbeans.jemmy.operators.JFileChooserOperator;60import org.netbeans.jemmy.operators.JDialogOperator;61import org.netbeans.jemmy.operators.JComponentOperator.JComponentByTipFinder;62import org.netbeans.jemmy.util.Platform;63import org.netbeans.jemmy.util.LookAndFeel;64import org.testng.annotations.BeforeTest;65import org.testng.annotations.Listeners;66import org.jemmy2ext.JemmyExt.ByToolTipChooser;6768/*69* @test70* @key headful71* @summary Verifies SwingSet3 FileChooserDemo by selecting an image file72* using Select Image and Select with preview, performing image73* transformation operations on the selected image and saving it.74*75* @library /sanity/client/lib/jemmy/src76* @library /sanity/client/lib/Extensions/src77* @library /sanity/client/lib/SwingSet3/src78* @modules java.desktop79* java.logging80* @build org.jemmy2ext.JemmyExt81* @build com.sun.swingset3.demos.filechooser.FileChooserDemo82* @run testng/timeout=600 FileChooserDemoTest83*/84@Listeners(GuiTestListener.class)85public class FileChooserDemoTest {8687public static final Path TEST_SRC = Paths.get(System.getProperty("test.src"));88public static final Path TEST_WORK_DIR = Paths.get(System.getProperty("user.dir"));89public static final Path IMAGE_DIR = TEST_SRC.resolve("resources").resolve("images");90public static final String IMAGE = "duke.jpg";91private static final String YES = "Yes";92private static final String NO = "No";93private static final String OPEN = "Open";94private static final String OK = "OK";95private static final String CANCEL = "Cancel";96private static final String USER_HOME = "user.home";97private static final String DESKTOP = "Desktop";98private static final int greyFilterID = 7;99100private JFrameOperator frame;101private JButtonOperator selectImageButton;102private JButtonOperator selectWithPreviewButton;103private JFileChooserOperator fileChooser;104private JButtonOperator upLevelButton;105private JButtonOperator rotateLeftButton;106private JButtonOperator rotateRightButton;107private JButtonOperator flipHorizontalButton;108private JButtonOperator flipVerticalButton;109private JButtonOperator applyFilterButton;110private JButtonOperator saveButton;111private JButtonOperator cancelButton;112private JButtonOperator yesButton;113private JButtonOperator noButton;114private JButtonOperator openButton;115private JComboBoxOperator filterComboBox;116private JDialogOperator confirmationDialog;117private JToggleButtonOperator getDetailsToggleButton;118private JToggleButtonOperator getListToggleButton;119private JDialogOperator fileChooserDialog;120121@BeforeTest122public void beforeTest() throws Exception {123Files.copy(IMAGE_DIR.resolve(IMAGE), TEST_WORK_DIR.resolve(IMAGE));124}125126@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)127public void test(String lookAndFeel) throws Exception {128UIManager.setLookAndFeel(lookAndFeel);129new ClassReference(FileChooserDemo.class.getCanonicalName()).startApplication();130frame = new JFrameOperator(DEMO_TITLE);131initializeSelectImageButtons();132initializeImageOperationButtons();133checkSelectImage();134checkImageOperations();135checkSelectWithPreview();136checkImageOperations();137}138139private void checkSelectImage() throws Exception {140selectImageButton.push();141fileChooser = new JFileChooserOperator(JFileChooserOperator.findJFileChooser((Container) frame.getSource()));142// In Aqua, GTK and Motif L&Fs, JFileChooser does not have143// "Go Home", "Up One Level", "Get Details", "Get List" buttons.144if (!LookAndFeel.isAqua() && !LookAndFeel.isMotif() && !LookAndFeel.isGTK()) {145File previousDirectory = fileChooser.getCurrentDirectory();146fileChooser.goHome();147// In Windows, pressing goHome navigates to Desktop inside the home directory.148// This is the expected behavior for windows.149if (!Platform.isWindows()) {150waitCurrentPath(Paths.get(System.getProperty(USER_HOME)));151} else {152waitCurrentPath(Paths.get(System.getProperty(USER_HOME)).resolve(DESKTOP));153}154fileChooser.setCurrentDirectory(previousDirectory);155fileChooser.rescanCurrentDirectory();156// In Windows and Windows Classic L&F, List and Details views are157// implemented as a popup menu item158if(LookAndFeel.isWindows() || LookAndFeel.isWindowsClassic()) {159JButtonOperator popupButton = new JButtonOperator(fileChooser, new JComponentByTipFinder(160UIManager.getString("FileChooser.viewMenuButtonToolTipText", fileChooser.getLocale())));161popupButton.push();162JPopupMenuOperator popup = new JPopupMenuOperator();163popup.pushKey(KeyEvent.VK_ENTER);164JRadioButtonMenuItemOperator detailsMenuItem = new JRadioButtonMenuItemOperator(popup, 1);165detailsMenuItem.push();166detailsMenuItem.waitSelected(true);167popupButton.push();168JRadioButtonMenuItemOperator listMenuItem = new JRadioButtonMenuItemOperator(popup);169listMenuItem.push();170listMenuItem.waitSelected(true);171} else {172initializeFileChooserButtons();173upLevelButton.push();174waitCurrentPath(previousDirectory.getParentFile().toPath());175fileChooser.setCurrentDirectory(previousDirectory);176fileChooser.rescanCurrentDirectory();177getDetailsToggleButton.push();178getDetailsToggleButton.waitSelected(true);179getListToggleButton.push();180getListToggleButton.waitSelected(true);181}182// Wait for the count of number of files to be 1183fileChooser.waitFileCount(1);184fileChooser.selectFile(IMAGE);185} else {186fileChooser.setCurrentDirectory(TEST_WORK_DIR.toFile());187fileChooser.selectFile(IMAGE);188}189selectImageButton.push();190applyFilterButton.waitComponentEnabled();191}192193private void checkSelectWithPreview() {194selectWithPreviewButton.pushNoBlock();195initializeSelectWithPreviewDialog();196cancelButton.push();197fileChooserDialog.waitClosed();198selectWithPreviewButton.pushNoBlock();199initializeSelectWithPreviewDialog();200fileChooser.selectFile(IMAGE);201openButton.push();202fileChooserDialog.waitClosed();203}204205private void checkImageOperations() throws Exception {206// Check Rotate Left Button207{208int initialValue = getRotateLeftCount();209// Push Button and wait for rotateLeftCount count to increment.210rotateLeftButton.push();211rotateLeftButton.waitState(button -> getRotateLeftCount() == initialValue + 1);212}213// Check Rotate Right Button214{215int initialValue = getRotateRightCount();216// Push Button and wait for rotateRightCount count to increment.217rotateRightButton.push();218rotateRightButton.waitState(button -> getRotateRightCount() == initialValue + 1);219}220// Check Apply Filter Button221filterComboBox.selectItem(GRAY);222applyFilterButton.push();223applyFilterButton.waitState(button -> getLastAppliedFilterId() == greyFilterID);224// Check Save and Yes Buttons225initializeSaveAndCancelButton();226saveButton.pushNoBlock();227//Wait for the dialog to show and initialize it228waitAndinitializeConfirmationDialog();229yesButton.push();230waitButtonDisabled(saveButton);231232// Check Flip Vertical Button233{234int initialValue = getFlipVerticalCount();235// Push Button and wait for flipVerticalCount count to increment.236flipVerticalButton.push();237flipVerticalButton.waitState(button -> getFlipVerticalCount() == initialValue + 1);238239}240// Check Save and No Buttons241saveButton.pushNoBlock();242//Wait for the dialog to show and initialize it243waitAndinitializeConfirmationDialog();244noButton.push();245confirmationDialog.waitClosed();246saveButton.waitComponentEnabled();247// Check Flip Horizontal Button248{249int initialValue = getFlipHorizontalCount();250// Push Button and wait for flipHorizontalCount count to increment.251flipHorizontalButton.push();252flipHorizontalButton.waitState(button -> getFlipHorizontalCount() == initialValue + 1);253}254// Check Cancel Button255cancelButton.push();256waitButtonDisabled(saveButton);257}258259private void initializeFileChooserButtons() {260upLevelButton = new JButtonOperator(fileChooser.getUpLevelButton());261getDetailsToggleButton = new JToggleButtonOperator(fileChooser.getDetailsToggleButton());262getListToggleButton = new JToggleButtonOperator(fileChooser.getListToggleButton());263}264265private void initializeSelectImageButtons() {266selectImageButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_SELECT_TEXT);267selectWithPreviewButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_SELECT_WITH_PREVIEW);268}269270private void initializeSelectWithPreviewDialog() {271fileChooser = new JFileChooserOperator();272fileChooserDialog = new JDialogOperator(OPEN);273String openButtonText = OPEN;274// In GTK and Motif L&F, open button text is 'OK'275if (LookAndFeel.isMotif() || LookAndFeel.isGTK()) {276openButtonText = OK;277}278openButton = new JButtonOperator(fileChooser, openButtonText);279cancelButton = new JButtonOperator(fileChooser, CANCEL);280}281282private void initializeImageOperationButtons() {283rotateLeftButton = new JButtonOperator(frame,284new ByToolTipChooser(ROTATE_LEFT_TOOLTIP));285rotateRightButton = new JButtonOperator(frame,286new ByToolTipChooser(ROTATE_RIGHT_TOOLTIP));287flipHorizontalButton = new JButtonOperator(frame,288new ByToolTipChooser(FLIP_HORIZONTAL_TOOLTIP));289flipVerticalButton = new JButtonOperator(frame,290new ByToolTipChooser(FLIP_VERTICAL_TOOLTIP));291applyFilterButton = new JButtonOperator(frame,292new ByToolTipChooser(APPLY_FILTER_TOOLTIP));293filterComboBox = new JComboBoxOperator(frame);294}295296private void initializeSaveAndCancelButton() {297saveButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_SAVE_TEXT);298cancelButton = new JButtonOperator(frame, FILE_CHOOSER_DEMO_CANCEL_TEXT);299}300301private void waitAndinitializeConfirmationDialog() {302//Wait for the dialog to show303JDialogOperator.waitJDialog(FILE_CHOOSER_DEMO_SAVEQUESTION_TITLE, true, true);304confirmationDialog = new JDialogOperator(FILE_CHOOSER_DEMO_SAVEQUESTION_TITLE);305yesButton = new JButtonOperator(confirmationDialog, YES);306noButton = new JButtonOperator(confirmationDialog, NO);307}308309private void waitButtonDisabled(JButtonOperator button) {310button.waitState(b -> b.isEnabled() == false);311}312313private void waitCurrentPath(Path expectedPath) {314//Wait for the current path to be same as expected path315fileChooser.waitState(chooser -> fileChooser.getCurrentDirectory().toPath().equals(expectedPath));316}317318}319320321322