1 /*
   2  * Copyright (c) 2010, 2016, 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 com.sun.swingset3.demos.splitpane.SplitPaneDemo;
  25 import static com.sun.swingset3.demos.splitpane.SplitPaneDemo.*;
  26 
  27 import java.awt.Component;
  28 import java.awt.event.KeyEvent;
  29 import javax.swing.JSplitPane;
  30 
  31 import static org.jemmy2ext.JemmyExt.*;
  32 
  33 import org.jtregext.GuiTestListener;
  34 
  35 import org.netbeans.jemmy.ClassReference;
  36 import org.netbeans.jemmy.ComponentChooser;
  37 import org.netbeans.jemmy.operators.JButtonOperator;
  38 import org.netbeans.jemmy.operators.JCheckBoxOperator;
  39 import org.netbeans.jemmy.operators.JFrameOperator;
  40 import org.netbeans.jemmy.operators.JRadioButtonOperator;
  41 import org.netbeans.jemmy.operators.JSplitPaneOperator;
  42 import org.netbeans.jemmy.operators.JTextFieldOperator;
  43 
  44 import org.testng.annotations.Listeners;
  45 import org.testng.annotations.Test;
  46 import static org.testng.AssertJUnit.*;
  47 
  48 /*
  49  * @test
  50  * @key headful
  51  * @summary Verifies SwingSet3 SplitPaneDemo by performing OneClick expansion,
  52  *          changing size of the divier, moving the divider to different positions
  53  *          and changing the divider orientation.
  54  *
  55  * @library /sanity/client/lib/jemmy/src
  56  * @library /sanity/client/lib/Extensions/src
  57  * @library /sanity/client/lib/SwingSet3/src
  58  * @modules java.desktop
  59  * @build org.jemmy2ext.JemmyExt
  60  * @build com.sun.swingset3.demos.splitpane.SplitPaneDemo
  61  * @run testng SplitPaneDemoTest
  62  */
  63 @Listeners(GuiTestListener.class)
  64 public class SplitPaneDemoTest {
  65 
  66     @Test
  67     public void test() throws Exception {
  68 
  69         new ClassReference(SplitPaneDemo.class.getCanonicalName()).startApplication();
  70 
  71         JFrameOperator frame = new JFrameOperator(DEMO_TITLE);
  72 
  73         JSplitPaneOperator splitPane = new JSplitPaneOperator(frame);
  74 
  75         // Toggle OneTouch Expandable
  76         checkOneTouch(frame, splitPane, true);
  77         checkOneTouch(frame, splitPane, false);
  78 
  79         // Check changing divider size to minimum and maximum values
  80         changeDividerSize(frame, splitPane, 50);
  81         changeDividerSize(frame, splitPane, 6);
  82 
  83         // Check moving the divider
  84         checkDividerMoves(frame, splitPane, false);
  85         checkDividerMoves(frame, splitPane, true);
  86 
  87         // Check different minumum Day/Night sizes
  88         changeMinimumSizes(frame, splitPane, 100);
  89         changeMinimumSizes(frame, splitPane, 0);
  90     }
  91 
  92     // Check for different day and night minimum size
  93     public void changeMinimumSizes(JFrameOperator frame, JSplitPaneOperator splitPane, int amount) throws Exception {
  94         for (String label : new String[]{FIRST_COMPONENT_MIN_SIZE, SECOND_COMPONENT_MIN_SIZE}) {
  95             JTextFieldOperator size = new JTextFieldOperator(getLabeledContainerOperator(frame, label));
  96             size.enterText(Integer.toString(amount));
  97             size.pressKey(KeyEvent.VK_ENTER);
  98         }
  99         checkDividerMoves(frame, splitPane, false);
 100         checkDividerMoves(frame, splitPane, true);
 101     }
 102 
 103     // Check moving of divider
 104     public void checkDividerMoves(JFrameOperator frame, JSplitPaneOperator splitPane, boolean isVertical) throws Exception {
 105         if (isVertical) {
 106             new JRadioButtonOperator(frame, VERTICAL_SPLIT).doClick();
 107         } else {
 108             new JRadioButtonOperator(frame, HORIZONTAL_SPLIT).doClick();
 109         }
 110 
 111         splitPane.moveDivider(0.0);
 112         assertEquals("Move Minimum, dividerLocation is at minimumDividerLocation",
 113                 splitPane.getMinimumDividerLocation(), splitPane.getDividerLocation());
 114 
 115         // use getMaximumDividerLocation() to move divider to here because using proportion 1.0 does not work
 116         splitPane.moveDivider(1.0);
 117 
 118         assertEquals("Move Maximum, dividerLocation is at maximumDividerLocation",
 119                 splitPane.getMaximumDividerLocation(), splitPane.getDividerLocation());
 120 
 121         splitPane.moveDivider(0.5);
 122         assertEquals("Move Middle, dividerLocation is at the artithmetic average of minimum and maximum DividerLocations",
 123                 (splitPane.getMaximumDividerLocation() + splitPane.getMinimumDividerLocation()) / 2, splitPane.getDividerLocation());
 124     }
 125 
 126     private void waitDividerSize(JSplitPaneOperator splitPane, int size) {
 127         splitPane.waitState(new ComponentChooser() {
 128             public boolean checkComponent(Component c) {
 129                 return splitPane.getDividerSize() == size;
 130             }
 131             public String getDescription() {
 132                 return "Divider size to be " + size;
 133             }
 134         });
 135     }
 136 
 137     // Check changing the size of the divider
 138     public void changeDividerSize(JFrameOperator frame, JSplitPaneOperator splitPane, int amount) throws Exception {
 139         JTextFieldOperator size = new JTextFieldOperator(getLabeledContainerOperator(frame, DIVIDER_SIZE));
 140         size.enterText(Integer.toString(amount));
 141         waitDividerSize(splitPane, amount);
 142     }
 143 
 144     private void waitDividerLocation(JSplitPaneOperator splitPane, int location) {
 145         splitPane.waitState(new ComponentChooser() {
 146             public boolean checkComponent(Component c) {
 147                 return splitPane.getDividerLocation() == location;
 148             }
 149             public String getDescription() {
 150                 return "Divider location to be " + location;
 151             }
 152         });
 153     }
 154 
 155     public void checkOneTouch(JFrameOperator frame, JSplitPaneOperator splitPane, boolean oneTouch) throws Exception {
 156         JCheckBoxOperator checkBox = new JCheckBoxOperator(frame, ONE_TOUCH_EXPANDABLE);
 157         JButtonOperator buttonLeft = new JButtonOperator(splitPane.getDivider(), 0);
 158         JButtonOperator buttonRight = new JButtonOperator(splitPane.getDivider(), 1);
 159         int initDividerLocation = splitPane.getDividerLocation();
 160 
 161         if (oneTouch) {
 162             if (!checkBox.isSelected()) {
 163                 // uncheck
 164                 checkBox.doClick();
 165             }
 166 
 167             int left = getUIValue(splitPane, (JSplitPane sp) -> sp.getInsets().left);
 168             System.out.println("left = " + left);
 169             int right = getUIValue(splitPane, (JSplitPane sp) -> sp.getInsets().right);
 170             System.out.println("right = " + right);
 171 
 172             // expand full left
 173             buttonLeft.push();
 174             waitDividerLocation(splitPane, left);
 175 
 176             // expand back from full left
 177             buttonRight.push();
 178             waitDividerLocation(splitPane, initDividerLocation);
 179 
 180             // expand all the way right
 181             buttonRight.push();
 182             waitDividerLocation(splitPane, splitPane.getWidth() - splitPane.getDividerSize() - right);
 183 
 184             // Click to move back from right expansion
 185             buttonLeft.push();
 186             waitDividerLocation(splitPane, initDividerLocation);
 187         }
 188 
 189         // Test for case where one touch expandable is disabled
 190         if (!oneTouch) {
 191             if (checkBox.isSelected()) {
 192                 // uncheck
 193                 checkBox.doClick();
 194             }
 195             splitPane.waitState(new ComponentChooser() {
 196                 public boolean checkComponent(Component c) {
 197                     return !splitPane.isOneTouchExpandable();
 198                 }
 199                 public String getDescription() {
 200                     return "Split pane not to be one touch expandable";
 201                 }
 202             });
 203         }
 204     }
 205 
 206 }