changeset 16718:e67a9a0f1561

8174161: [TESTBUG] Create test for SwingSet DialogDemo Reviewed-by: serb, mrkam Contributed-by: Vikrant Agarwal <vikrant.v.agarwal@oracle.com>
author mrkam
date Wed, 15 Feb 2017 23:35:52 -0800
parents 656ccef366ac
children 460f7166ddc9
files test/sanity/client/SwingSet/src/DialogDemoTest.java test/sanity/client/SwingSet/src/TestHelpers.java test/sanity/client/lib/SwingSet3/src/com/sun/swingset3/demos/dialog/DialogDemo.java test/sanity/client/lib/jemmy/src/org/netbeans/jemmy/WindowWaiter.java test/sanity/client/lib/jemmy/src/org/netbeans/jemmy/operators/ComponentOperator.java
diffstat 5 files changed, 459 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sanity/client/SwingSet/src/DialogDemoTest.java	Wed Feb 15 23:35:52 2017 -0800
@@ -0,0 +1,104 @@
+
+/*
+ * Copyright (c) 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+import org.jtregext.GuiTestListener;
+import com.sun.swingset3.demos.dialog.DialogDemo;
+import static com.sun.swingset3.demos.dialog.DialogDemo.*;
+import java.awt.Dimension;
+import java.awt.Point;
+import javax.swing.JDialog;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+import static org.jemmy2ext.JemmyExt.isIconified;
+import static org.jemmy2ext.JemmyExt.ByClassChooser;
+import org.netbeans.jemmy.ClassReference;
+import org.netbeans.jemmy.ComponentChooser;
+import static org.netbeans.jemmy.WindowWaiter.countWindows;
+import org.netbeans.jemmy.operators.JFrameOperator;
+import org.netbeans.jemmy.operators.JDialogOperator;
+import org.netbeans.jemmy.operators.JLabelOperator;
+import org.netbeans.jemmy.operators.JButtonOperator;
+import org.testng.annotations.Listeners;
+
+/*
+ * @test
+ * @key headful
+ * @summary Verifies SwingSet3 DialogDemo by checking that separate JDialog is
+ *          shown, it contains predefined label and no new dialogs are opened
+ *          when the "Show JDialog..." button is clicked.
+ *
+ * @library /sanity/client/lib/jemmy/src
+ * @library /sanity/client/lib/Extensions/src
+ * @library /sanity/client/lib/SwingSet3/src
+ * @modules java.desktop
+ *          java.logging
+ * @build org.jemmy2ext.JemmyExt
+ * @build com.sun.swingset3.demos.dialog.DialogDemo
+ * @run testng DialogDemoTest
+ */
+@Listeners(GuiTestListener.class)
+public class DialogDemoTest {
+
+    private final ComponentChooser jDialogClassChooser = new ByClassChooser(JDialog.class);
+
+    @Test
+    public void test() throws Exception {
+        new ClassReference(DialogDemo.class.getCanonicalName()).startApplication();
+        JFrameOperator mainFrame = new JFrameOperator(DIALOG_DEMO_TITLE);
+        JDialogOperator dialog = new JDialogOperator(DIALOG_TITLE);
+        JButtonOperator showJDialogButton = new JButtonOperator(mainFrame, SHOW_BUTTON_TITLE);
+        initialCheckWithLabel(mainFrame, dialog);
+        checkShowDialogButton(dialog, showJDialogButton);
+        TestHelpers.checkChangeSize(dialog, new Dimension(dialog.getSize().width * 2,
+                dialog.getSize().height * 2));
+        TestHelpers.checkChangeLocation(dialog, new Point(dialog.getLocation().x + 100,
+                dialog.getLocation().y + 100));
+    }
+
+    private void initialCheckWithLabel(JFrameOperator frame, JDialogOperator jdo) {
+        JLabelOperator label = new JLabelOperator(jdo);
+        assertFalse("JFrame is not iconified", isIconified(frame));
+        assertEquals("Only one JDialog is present", 1,
+                countWindows(jDialogClassChooser));
+        assertEquals(LABEL_CONTENT, label.getText());
+    }
+
+    private void checkShowDialogButton(JDialogOperator jdo, JButtonOperator jbo)
+            throws InterruptedException {
+        //Check that the button does not change the number of JDialog
+        jbo.push();
+        Thread.sleep(500);
+        assertEquals("Only one JDialog is present", 1,
+                countWindows(jDialogClassChooser));
+        assertTrue("Check JDialog is visible", jdo.isVisible());
+        jdo.requestClose();
+        jdo.waitClosed();
+        //Check that the button makes the JDialog visible
+        //and that 1 jDialog is present.
+        jbo.push();
+        jdo.waitComponentVisible(true);
+        Thread.sleep(500);
+        assertEquals("Only one JDialog is present", 1,
+                countWindows(jDialogClassChooser));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sanity/client/SwingSet/src/TestHelpers.java	Wed Feb 15 23:35:52 2017 -0800
@@ -0,0 +1,24 @@
+
+import java.awt.Dimension;
+import java.awt.Point;
+
+import org.netbeans.jemmy.operators.ComponentOperator;
+
+public class TestHelpers {
+
+    public static void checkChangeLocation(ComponentOperator component,
+            Point finalLocation) {
+        Point initialLocation = component.getLocation();
+        component.setLocation(finalLocation);
+        component.waitComponentLocation(finalLocation);
+        component.setLocation(initialLocation);
+    }
+
+    public static void checkChangeSize(ComponentOperator component,
+            Dimension dimensionFinal) {
+        Dimension dimensionInitial = component.getSize();
+        component.setSize(dimensionFinal);
+        component.waitComponentSize(dimensionFinal);
+        component.setSize(dimensionInitial);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sanity/client/lib/SwingSet3/src/com/sun/swingset3/demos/dialog/DialogDemo.java	Wed Feb 15 23:35:52 2017 -0800
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2007, 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.sun.swingset3.demos.dialog;
+
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+import com.sun.swingset3.DemoProperties;
+import com.sun.swingset3.demos.DemoUtilities;
+import com.sun.swingset3.demos.ResourceManager;
+import com.sun.swingset3.demos.slider.SliderDemo;
+
+/**
+ *
+ * @author aim
+ */
+@DemoProperties(
+        value = "JDialog Demo",
+        category = "Toplevel Containers",
+        description = "Demonstrates JDialog, Swing's top-level dialog container.",
+        sourceFiles = {
+            "com/sun/swingset3/demos/dialog/DialogDemo.java",
+            "com/sun/swingset3/demos/DemoUtilities.java",
+            "com/sun/swingset3/demos/dialog/resources/images/DialogDemo.gif"
+        }
+)
+public class DialogDemo extends JPanel {
+
+    private JDialog dialog;
+    private JComponent dialogSpaceholder;
+
+    public static final String DIALOG_TITLE = "Demo JDialog";
+    public static final String SHOW_BUTTON_TITLE = "Show JDialog...";
+    public static final String LABEL_CONTENT = "I'm content.";
+    public static final String DIALOG_DEMO_TITLE = DialogDemo.class
+            .getAnnotation(DemoProperties.class).value();
+
+    public DialogDemo() {
+        initComponents();
+    }
+
+    protected void initComponents() {
+        dialog = createDialog();
+
+        setLayout(new BorderLayout());
+
+        add(createControlPanel(), BorderLayout.WEST);
+        dialogSpaceholder = createDialogSpaceholder(dialog);
+        add(dialogSpaceholder, BorderLayout.CENTER);
+    }
+
+    private static JComponent createDialogSpaceholder(JDialog dialog) {
+        // Create placeholder panel to provide space in which to
+        // display the toplevel dialog so that the control panel is not
+        // obscured by it.
+        JPanel placeholder = new JPanel();
+        Dimension prefSize = dialog.getPreferredSize();
+        prefSize.width += 12;
+        prefSize.height += 12;
+        placeholder.setPreferredSize(prefSize);
+        return placeholder;
+    }
+
+    protected JComponent createControlPanel() {
+        // Create control panel on Left
+        Box panel = Box.createVerticalBox();
+        panel.setBorder(new EmptyBorder(8, 8, 8, 8));
+
+        // Create button to control visibility of frame
+        JButton showButton = new JButton(SHOW_BUTTON_TITLE);
+        showButton.addActionListener(new ShowActionListener());
+        panel.add(showButton);
+
+        return panel;
+    }
+
+    private static JDialog createDialog() {
+
+        //<snip>Create dialog
+        JDialog dialog = new JDialog(new JFrame(), DIALOG_TITLE, false);
+        //</snip>
+
+        //<snip>Add dialog's content
+        JLabel label = new JLabel(LABEL_CONTENT);
+        label.setHorizontalAlignment(JLabel.CENTER);
+        label.setPreferredSize(new Dimension(200, 140));
+        dialog.add(label);
+        //</snip>
+
+        //<snip>Initialize dialog's size
+        // which will shrink-to-fit its contents
+        dialog.pack();
+        //</snip>
+
+        return dialog;
+    }
+
+    public void start() {
+        DemoUtilities.setToplevelLocation(dialog, dialogSpaceholder, SwingConstants.CENTER);
+        showDialog();
+    }
+
+    public void stop() {
+        //<snip>Hide dialog
+        dialog.setVisible(false);
+        //</snip>
+    }
+
+    public void showDialog() {
+        //<snip>Show dialog
+        // if dialog already visible, then bring to the front
+        if (dialog.isShowing()) {
+            dialog.toFront();
+        } else {
+            dialog.setVisible(true);
+        }
+        //</snip>
+    }
+
+    private class ShowActionListener implements ActionListener {
+
+        public void actionPerformed(ActionEvent actionEvent) {
+            showDialog();
+        }
+    }
+
+    public static void main(String args[]) {
+        EventQueue.invokeLater(new Runnable() {
+            public void run() {
+                JFrame frame = new JFrame(DIALOG_DEMO_TITLE);
+                DialogDemo demo = new DialogDemo();
+                frame.add(demo);
+                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                frame.pack();
+                frame.setVisible(true);
+                demo.start();
+            }
+        });
+    }
+}
--- a/test/sanity/client/lib/jemmy/src/org/netbeans/jemmy/WindowWaiter.java	Wed Feb 15 18:22:39 2017 -0800
+++ b/test/sanity/client/lib/jemmy/src/org/netbeans/jemmy/WindowWaiter.java	Wed Feb 15 23:35:52 2017 -0800
@@ -25,6 +25,7 @@
 import java.awt.Component;
 import java.awt.Frame;
 import java.awt.Window;
+import java.util.stream.Stream;
 
 /**
  * A WindowWaiter is a utility class used to look or wait for Windows. It
@@ -282,6 +283,91 @@
         return waitWindow(o, ch, 0);
     }
 
+    /**
+     * Wait till the count of windows which meet the the search criteria becomes
+     * equal to count.
+     *
+     * @param ch a component chooser used to define and apply the search
+     * criteria.
+     * @param count the number of expected windows meeting the search criteria.
+     * @throws InterruptedException
+     */
+    public static void waitWindowCount(ComponentChooser ch, int count)
+            throws InterruptedException {
+        waitWindowCount(null, ch, count);
+    }
+
+    /**
+     * Wait till the count of windows which meet the the search criteria becomes
+     * equal to count.
+     *
+     * @param owner The owner window of all the windows to be checked
+     * @param ch a component chooser used to define and apply the search
+     * criteria.
+     * @param count the number of expected windows meeting the search criteria.
+     * @throws InterruptedException
+     */
+    public static void waitWindowCount(Window owner, ComponentChooser ch, int count)
+            throws InterruptedException {
+        Waiter<String, Void> stateWaiter = new Waiter<>(new Waitable<String, Void>() {
+            @Override
+            public String actionProduced(Void obj) {
+                return countWindows(owner, ch) == count ? "" : null;
+            }
+
+            @Override
+            public String getDescription() {
+                return "Wait till the count of windows matching the criteria "
+                        + "specified by ComponentChooser reaches :" + count;
+            }
+
+            @Override
+            public String toString() {
+                return "Operator.waitState.Waitable{description = "
+                        + getDescription() + '}';
+            }
+        });
+        stateWaiter.waitAction(null);
+    }
+
+    /**
+     * Counts all the windows owned by the owner window which match the
+     * criterion specified by component chooser.
+     *
+     * @param owner The owner window of all the windows to be checked
+     * @param ch A component chooser used to define and apply the search
+     * criteria
+     * @return the number of matched windows
+     */
+    public static int countWindows(Window owner, ComponentChooser ch) {
+        return new QueueTool().invokeAndWait(new QueueTool.QueueAction<Integer>(null) {
+
+            @Override
+            public Integer launch() {
+                Window[] windows;
+                if (owner == null) {
+                    windows = Window.getWindows();
+                } else {
+                    windows = owner.getOwnedWindows();
+                }
+                return (int) Stream.of(windows)
+                        .filter(x -> ch.checkComponent(x)).count();
+            }
+        });
+    }
+
+    /**
+     * Counts all the windows which match the criterion specified by component
+     * chooser.
+     *
+     * @param ch A component chooser used to define and apply the search
+     * criteria
+     * @return the number of matched windows
+     */
+    public static int countWindows(ComponentChooser ch) {
+        return countWindows(null, ch);
+    }
+
     @Override
     public String getDescription() {
         return chooser.getDescription();
--- a/test/sanity/client/lib/jemmy/src/org/netbeans/jemmy/operators/ComponentOperator.java	Wed Feb 15 18:22:39 2017 -0800
+++ b/test/sanity/client/lib/jemmy/src/org/netbeans/jemmy/operators/ComponentOperator.java	Wed Feb 15 23:35:52 2017 -0800
@@ -57,6 +57,8 @@
 import java.util.Hashtable;
 import java.util.Locale;
 
+import static java.lang.Math.abs;
+
 import org.netbeans.jemmy.CharBindingMap;
 import org.netbeans.jemmy.ComponentChooser;
 import org.netbeans.jemmy.ComponentSearcher;
@@ -1137,6 +1139,87 @@
     }
 
     /**
+     * Wait till the Size of the component becomes as expected.
+     *
+     * @param exactSize the exact expected size.
+     */
+    public void waitComponentSize(Dimension exactSize) {
+        waitComponentSize(exactSize, exactSize);
+    }
+
+    /**
+     * Wait till the Size of the component becomes between minSize and maxSize.
+     *
+     * @param minSize the minimum allowed size.
+     * @param maxSize the maximum allowed size.
+     */
+    public void waitComponentSize(Dimension minSize, Dimension maxSize) {
+        waitState(new ComponentChooser() {
+            @Override
+            public boolean checkComponent(Component comp) {
+                Dimension componentSize = comp.getSize();
+                return componentSize.height >= minSize.height
+                        && componentSize.height <= maxSize.height
+                        && componentSize.width >= minSize.width
+                        && componentSize.width <= maxSize.width;
+            }
+
+            @Override
+            public String getDescription() {
+                return "Component Size becomes between: " + minSize
+                        + "and " + maxSize;
+            }
+
+            @Override
+            public String toString() {
+                return "ComponentOperator.waitComponentSize"
+                        + ".Waitable{description = " + getDescription() + '}';
+            }
+        });
+    }
+
+    /**
+     * Wait till the component reaches exact location.
+     *
+     * @param exactlocation exact expected location.
+     */
+    public void waitComponentLocation(Point exactlocation) {
+        waitComponentLocation(exactlocation, exactlocation);
+    }
+
+    /**
+     * Wait till the component reaches location between minLocation and
+     * maxLocation
+     *
+     * @param minLocation minimum expected location.
+     * @param maxLocation maximum expected location.
+     */
+    public void waitComponentLocation(Point minLocation, Point maxLocation) {
+        waitState(new ComponentChooser() {
+            @Override
+            public boolean checkComponent(Component comp) {
+                Point componentLocation = comp.getLocation();
+                return componentLocation.x >= minLocation.x
+                        && componentLocation.x <= maxLocation.x
+                        && componentLocation.y >= minLocation.y
+                        && componentLocation.y <= maxLocation.y;
+            }
+
+            @Override
+            public String getDescription() {
+                return "Component reaches location between :" + minLocation
+                        + "and " + maxLocation;
+            }
+
+            @Override
+            public String toString() {
+                return "ComponentOperator.waitComponentLocation"
+                        + ".Waitable{description = " + getDescription() + '}';
+            }
+        });
+    }
+
+    /**
      * Returns information about component.
      */
     @Override