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. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.netbeans.jemmy.operators;
  27 
  28 import static org.netbeans.jemmy.operators.JInternalFrameOperator.CLOSE_BUTTON_TOOLTIP;
  29 import static org.netbeans.jemmy.operators.JInternalFrameOperator.MAXIMIZE_BUTTON_TOOLTIP;
  30 import static org.netbeans.jemmy.operators.JInternalFrameOperator.MINIMIZE_BUTTON_TOOLTIP;
  31 import static org.testng.Assert.assertEquals;
  32 import static org.testng.Assert.fail;
  33 
  34 import java.awt.Dimension;
  35 import java.awt.Point;
  36 
  37 import javax.swing.JDesktopPane;
  38 import javax.swing.JFrame;
  39 import javax.swing.JInternalFrame;
  40 import javax.swing.UIManager;
  41 
  42 import org.netbeans.jemmy.JemmyProperties;
  43 import org.netbeans.jemmy.LookAndFeelProvider;
  44 import org.netbeans.jemmy.util.Platform;
  45 import org.testng.annotations.AfterMethod;
  46 import org.testng.annotations.Test;
  47 
  48 public class JInternalFrameOperatorTest {
  49 
  50     private JFrameOperator frameOper;
  51 
  52     private JInternalFrameOperator internalFrameOper;
  53 
  54     private JDesktopPane desktop;
  55 
  56     private final static String OSX_EXCEPT_MESSAGE = "Jemmy doesn't support"
  57             + " getting or initializing title related operators on Mac OSx";
  58 
  59     private void setUp() throws Exception {
  60         JFrame frame = new JFrame();
  61         desktop = new JDesktopPane();
  62         frame.setContentPane(desktop);
  63         JemmyProperties.setCurrentDispatchingModel(
  64                 JemmyProperties.getCurrentDispatchingModel());
  65         JInternalFrame internalFrame = new JInternalFrame(
  66                 "JInternalFrameOperatorTest", true, true, true, true);
  67         internalFrame.setName("JInternalFrameOperatorTest");
  68         internalFrame.setSize(200, 200);
  69         internalFrame.setVisible(true);
  70         desktop.add(internalFrame);
  71         frame.setSize(400,400);
  72         frame.setLocationRelativeTo(null);
  73         frame.setVisible(true);
  74         frameOper = new JFrameOperator();
  75         internalFrameOper = new JInternalFrameOperator(internalFrame);
  76         internalFrameOper.setVerification(true);
  77     }
  78 
  79     @AfterMethod
  80     protected void tearDown() throws Exception {
  81         frameOper.setVisible(false);
  82         frameOper.dispose();
  83     }
  84 
  85     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
  86     public void testConstructors(String lookAndFeel) throws Exception {
  87         UIManager.setLookAndFeel(lookAndFeel);
  88         setUp();
  89         new JInternalFrameOperator(frameOper);
  90         new JInternalFrameOperator(frameOper, "JInternalFrameOperatorTest");
  91         new JInternalFrameOperator(frameOper,
  92                 comp -> "JInternalFrameOperatorTest".equals(comp.getName()));
  93 
  94     }
  95 
  96     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
  97     public void testIconify(String lookAndFeel) throws Exception {
  98         UIManager.setLookAndFeel(lookAndFeel);
  99         setUp();
 100         internalFrameOper.iconify();
 101         internalFrameOper.deiconify();
 102     }
 103 
 104     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
 105     public void testMaximize(String lookAndFeel) throws Exception {
 106         UIManager.setLookAndFeel(lookAndFeel);
 107         setUp();
 108         internalFrameOper.maximize();
 109         internalFrameOper.demaximize();
 110     }
 111 
 112     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
 113     public void testMove(String lookAndFeel) throws Exception {
 114         UIManager.setLookAndFeel(lookAndFeel);
 115         setUp();
 116         Point originalLocation = internalFrameOper.getLocation();
 117         internalFrameOper.move(150, 150);
 118         internalFrameOper.move(originalLocation.x, originalLocation.y);
 119     }
 120 
 121     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
 122     public void testResize(String lookAndFeel) throws Exception {
 123         UIManager.setLookAndFeel(lookAndFeel);
 124         setUp();
 125         Dimension originaSize = internalFrameOper.getSize();
 126         internalFrameOper.resize(250, 250);
 127         internalFrameOper.resize(originaSize.width, originaSize.height);
 128     }
 129 
 130     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
 131     public void testActivate(String lookAndFeel) throws Exception {
 132         UIManager.setLookAndFeel(lookAndFeel);
 133         setUp();
 134         internalFrameOper.activate();
 135     }
 136 
 137     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
 138     public void testTitleButtons(String lookAndFeel) throws Exception {
 139         UIManager.setLookAndFeel(lookAndFeel);
 140         setUp();
 141         if(!Platform.isOSX() && !"Motif".equals(UIManager.getLookAndFeel().getID())) {
 142             // Close, Maximize, and Minimize buttons are adding along with the
 143             // construction of internal frame itself
 144             JInternalFrame interanlFrame1 = new JInternalFrame(
 145                     "JInternalFrameButtonTest1", true, true, true, true);
 146             verifyTitleButtons(interanlFrame1);
 147 
 148             // Close, Maximize, and Minimize buttons are adding after the
 149             // construction of internal frame by using APIs
 150             JInternalFrame interanlFrame2 = new JInternalFrame(
 151                     "JInternalFrameButtonTest2", false, false, false, false);
 152             interanlFrame2.setClosable(true);
 153             interanlFrame2.setMaximizable(true);
 154             interanlFrame2.setIconifiable(true);
 155             verifyTitleButtons(interanlFrame2);
 156         } else if (Platform.isOSX()){
 157             JInternalFrame interanlFrame = new JInternalFrame(
 158                     "JInternalFrameButtonTest3", true, true, true, true);
 159             interanlFrame.setSize(200, 200);
 160             interanlFrame.setVisible(true);
 161             desktop.add(interanlFrame);
 162             JInternalFrameOperator interanlFrameOper = new JInternalFrameOperator(interanlFrame);
 163             try {
 164                 interanlFrameOper.getMaximizeButton();
 165                 fail();
 166             } catch (UnsupportedOperationException e) {
 167                 assertEquals(e.getMessage(), OSX_EXCEPT_MESSAGE);
 168             }
 169             try {
 170                 interanlFrameOper.getMinimizeButton();
 171                 fail();
 172             } catch (UnsupportedOperationException e) {
 173                 assertEquals(e.getMessage(), OSX_EXCEPT_MESSAGE);
 174             }
 175             try {
 176                 interanlFrameOper.getCloseButton();
 177                 fail();
 178             } catch (UnsupportedOperationException e) {
 179                 assertEquals(e.getMessage(), OSX_EXCEPT_MESSAGE);
 180             }
 181         }
 182     }
 183 
 184     private void verifyTitleButtons(JInternalFrame interanlFrame) {
 185         interanlFrame.setSize(200, 200);
 186         interanlFrame.setVisible(true);
 187         desktop.add(interanlFrame);
 188         JInternalFrameOperator interanlFrameOper = new JInternalFrameOperator(interanlFrame);
 189 
 190         // Verify title buttons tooltip texts
 191         assertEquals(interanlFrameOper.getCloseButton().getToolTipText(),
 192                 CLOSE_BUTTON_TOOLTIP);
 193         assertEquals(interanlFrameOper.getMaximizeButton().getToolTipText(),
 194                 MAXIMIZE_BUTTON_TOOLTIP);
 195         assertEquals(interanlFrameOper.getMinimizeButton().getToolTipText(),
 196                 MINIMIZE_BUTTON_TOOLTIP);
 197 
 198         // Verify different actions using title buttons
 199         interanlFrameOper.getMaximizeButton().push();
 200         interanlFrameOper.waitMaximum(true);
 201         interanlFrameOper.getMaximizeButton().push();
 202         interanlFrameOper.waitMaximum(false);
 203         interanlFrameOper.getMinimizeButton().push();
 204         interanlFrameOper.waitIcon(true);
 205         interanlFrameOper.deiconify();
 206         interanlFrameOper.waitIcon(false);
 207         interanlFrameOper.getCloseButton().push();
 208 
 209         desktop.remove(interanlFrame);
 210         interanlFrame.dispose();
 211     }
 212 
 213 }