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