changeset 23:93cfbd0e4d48

7902339: Handle different look and feels in JFileChooserOperator Reviewed-by: shurailine Contributed-by: abdul.kolarkunnu@oracle.com
author akolarkunnu
date Mon, 29 Oct 2018 21:52:18 -0700
parents 00c9f753cd0a
children 079c447a1c3e
files src/org/netbeans/jemmy/operators/JFileChooserOperator.java src/org/netbeans/jemmy/version_info test/org/netbeans/jemmy/operators/FileChooserTest.java test/org/netbeans/jemmy/operators/JInternalFrameOperatorCloseTest.java test/org/netbeans/jemmy/operators/JInternalFrameOperatorTest.java
diffstat 5 files changed, 76 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/netbeans/jemmy/operators/JFileChooserOperator.java	Tue Sep 25 08:48:10 2018 -0700
+++ b/src/org/netbeans/jemmy/operators/JFileChooserOperator.java	Mon Oct 29 21:52:18 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -43,6 +43,7 @@
 import javax.swing.JTextField;
 import javax.swing.JToggleButton;
 import javax.swing.ListModel;
+import javax.swing.UIManager;
 import javax.swing.filechooser.FileFilter;
 import javax.swing.filechooser.FileSystemView;
 import javax.swing.filechooser.FileView;
@@ -340,6 +341,13 @@
      * @return a component being used to display directory content.
      */
     public Component getFileList() {
+        int index = 0;
+        // In GTK and Motif L&F, there are two JLists, one is to list folders
+        // and second one one is to list files
+        if (UIManager.getLookAndFeel().getID().equals("Motif")
+                || UIManager.getLookAndFeel().getID().equals("GTK")) {
+            index =1;
+        }
         return innerSearcher.
                 findComponent(new ComponentChooser() {
                     @Override
@@ -357,7 +365,7 @@
                     public String toString() {
                         return "JFileChooserOperator.getFileList.ComponentChooser{description = " + getDescription() + '}';
                     }
-                });
+                }, index);
     }
 
     /**
@@ -433,7 +441,15 @@
         getQueueTool().waitEmpty();
         output.printTrace("Go home in JFileChooser\n    : "
                 + toStringSource());
-        JButtonOperator homeOper = new JButtonOperator(getHomeButton());
+        AbstractButtonOperator homeOper;
+        // In Windows and Windows Classic L&F, there is no 'Go Home' button,
+        // but there is a toggle button to go desktop. In Windows platform
+        // 'Go Home' button usually navigates to Desktop only.
+        if(UIManager.getLookAndFeel().getID().equals("Windows")) {
+            homeOper =new JToggleButtonOperator(this, 1);
+        } else {
+            homeOper = new JButtonOperator(getHomeButton());
+        }
         homeOper.copyEnvironment(this);
         homeOper.setOutput(output.createErrorOutput());
         homeOper.push();
--- a/src/org/netbeans/jemmy/version_info	Tue Sep 25 08:48:10 2018 -0700
+++ b/src/org/netbeans/jemmy/version_info	Mon Oct 29 21:52:18 2018 -0700
@@ -1,6 +1,6 @@
 Manifest-version: 1.0
 Main-Class: org.netbeans.jemmy.JemmyProperties
 Jemmy-MajorVersion: 3.0
-Jemmy-MinorVersion: 5.0
+Jemmy-MinorVersion: 6.0
 Jemmy-Build: @BUILD_NUMBER@
 
--- a/test/org/netbeans/jemmy/operators/FileChooserTest.java	Tue Sep 25 08:48:10 2018 -0700
+++ b/test/org/netbeans/jemmy/operators/FileChooserTest.java	Mon Oct 29 21:52:18 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,27 +24,30 @@
  */
 package org.netbeans.jemmy.operators;
 
-import org.netbeans.jemmy.ComponentChooser;
-import org.netbeans.jemmy.util.Dumper;
-import org.testng.ITestResult;
-import org.testng.annotations.AfterClass;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
+import static org.testng.Assert.assertTrue;
 
-import javax.swing.JFileChooser;
 import java.awt.Component;
 import java.awt.Container;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertTrue;
+import javax.swing.JFileChooser;
+import javax.swing.UIManager;
+import javax.swing.filechooser.FileSystemView;
+
+import org.netbeans.jemmy.ComponentChooser;
+import org.netbeans.jemmy.LookAndFeelProvider;
+import org.netbeans.jemmy.util.Dumper;
+import org.testng.ITestResult;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
 
 public class FileChooserTest {
 
+    private JFrameOperator frame;
     private JFileChooserOperator fileChooser;
     private File dir, file;
 
@@ -56,18 +59,28 @@
         file = File.createTempFile("testFile", ".txt", dir);
         file.deleteOnExit();
         File.createTempFile("zestFile", ".txt", dir).deleteOnExit();
+    }
+
+    @BeforeMethod
+    public void setUpBeforeMethod(Object[] args) throws Exception {
+        UIManager.setLookAndFeel((String)args[0]);
         FileChooserApp.show(dir);
-        JFrameOperator frame = new JFrameOperator("Sample File Chooser");
+        frame = new JFrameOperator("Sample File Chooser");
         fileChooser = new JFileChooserOperator(
                 JFileChooserOperator.findJFileChooser((Container) frame.getSource()));
     }
+
     @AfterMethod
     public void tearDown(ITestResult result) throws FileNotFoundException {
+        frame.setVisible(false);
+        frame.dispose();
         if(!result.isSuccess())
-            Dumper.dumpAll(new File(result.getMethod() + "-dump.xml").getAbsolutePath());
+            Dumper.dumpAll(new File(UIManager.getLookAndFeel().getClass().getSimpleName()
+                    + "_" + result.getMethod() + "-dump.xml").getAbsolutePath());
     }
-    @Test
-    public void testSelection() {
+
+    @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
+    public void testSelection(String lookAndFeel) throws Exception {
         fileChooser.selectFile(file.getName());
         fileChooser.waitState(new ComponentChooser() {
             @Override
@@ -82,8 +95,25 @@
             }
         });
     }
-    @Test
-    public void testCount() {
+
+    @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
+    public void testCount(String lookAndFeel) throws Exception {
         assertTrue(fileChooser.getFileCount() >= 3);
     }
+
+    @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
+    public void testGoHome(String lookAndFeel) throws Exception {
+        // In Aqua, GTK and Motif L&Fs, JFileChooser does not have
+        // "Go Home" button.
+        if (!UIManager.getLookAndFeel().getID().equals("Aqua")
+                && !UIManager.getLookAndFeel().getID().equals("Motif")
+                && !UIManager.getLookAndFeel().getID().equals("GTK")) {
+            File previousDirectory = fileChooser.getCurrentDirectory();
+            fileChooser.goHome();
+            fileChooser.waitState(chooser -> fileChooser.getCurrentDirectory().getPath().equals(
+                    FileSystemView.getFileSystemView().getHomeDirectory().getPath()));
+            fileChooser.setCurrentDirectory(previousDirectory);
+            fileChooser.rescanCurrentDirectory();
+        }
+    }
 }
--- a/test/org/netbeans/jemmy/operators/JInternalFrameOperatorCloseTest.java	Tue Sep 25 08:48:10 2018 -0700
+++ b/test/org/netbeans/jemmy/operators/JInternalFrameOperatorCloseTest.java	Mon Oct 29 21:52:18 2018 -0700
@@ -38,6 +38,7 @@
 import org.netbeans.jemmy.LookAndFeelProvider;
 import org.netbeans.jemmy.TimeoutExpiredException;
 import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 public class JInternalFrameOperatorCloseTest {
@@ -46,7 +47,9 @@
 
     private JInternalFrameOperator internalFrameOper;
 
-    private void setUp() throws Exception {
+    @BeforeMethod
+    private void setUp(Object[] args) throws Exception {
+        UIManager.setLookAndFeel((String)args[0]);
         JFrame frame = new JFrame();
         JDesktopPane desktop = new JDesktopPane();
         frame.setContentPane(desktop);
@@ -73,8 +76,6 @@
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testClose(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         InternalFrameListener listener = new InternalFrameListener() {
 
             @Override
--- a/test/org/netbeans/jemmy/operators/JInternalFrameOperatorTest.java	Tue Sep 25 08:48:10 2018 -0700
+++ b/test/org/netbeans/jemmy/operators/JInternalFrameOperatorTest.java	Mon Oct 29 21:52:18 2018 -0700
@@ -43,6 +43,7 @@
 import org.netbeans.jemmy.LookAndFeelProvider;
 import org.netbeans.jemmy.util.Platform;
 import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 public class JInternalFrameOperatorTest {
@@ -56,7 +57,9 @@
     private final static String OSX_EXCEPT_MESSAGE = "Jemmy doesn't support"
             + " getting or initializing title related operators on Mac OSx";
 
-    private void setUp() throws Exception {
+    @BeforeMethod
+    private void setUp(Object[] args) throws Exception {
+        UIManager.setLookAndFeel((String)args[0]);
         JFrame frame = new JFrame();
         desktop = new JDesktopPane();
         frame.setContentPane(desktop);
@@ -84,8 +87,6 @@
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testConstructors(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         new JInternalFrameOperator(frameOper);
         new JInternalFrameOperator(frameOper, "JInternalFrameOperatorTest");
         new JInternalFrameOperator(frameOper,
@@ -95,24 +96,18 @@
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testIconify(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         internalFrameOper.iconify();
         internalFrameOper.deiconify();
     }
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testMaximize(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         internalFrameOper.maximize();
         internalFrameOper.demaximize();
     }
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testMove(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         Point originalLocation = internalFrameOper.getLocation();
         internalFrameOper.move(150, 150);
         internalFrameOper.move(originalLocation.x, originalLocation.y);
@@ -120,8 +115,6 @@
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testResize(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         Dimension originaSize = internalFrameOper.getSize();
         internalFrameOper.resize(250, 250);
         internalFrameOper.resize(originaSize.width, originaSize.height);
@@ -129,15 +122,11 @@
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testActivate(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         internalFrameOper.activate();
     }
 
     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
     public void testTitleButtons(String lookAndFeel) throws Exception {
-        UIManager.setLookAndFeel(lookAndFeel);
-        setUp();
         if(!Platform.isOSX() && !"Motif".equals(UIManager.getLookAndFeel().getID())) {
             // Close, Maximize, and Minimize buttons are adding along with the
             // construction of internal frame itself