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