Path: blob/master/test/jdk/sanity/client/SwingSet/src/GridBagLayoutDemoTest.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import org.jtregext.GuiTestListener;23import com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo;24import static com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo.*;25import static com.sun.swingset3.demos.gridbaglayout.Calculator.*;26import java.awt.Component;27import java.awt.Dimension;28import java.awt.Point;29import javax.swing.JButton;30import javax.swing.UIManager;31import org.testng.annotations.Test;32import org.netbeans.jemmy.ClassReference;33import org.netbeans.jemmy.operators.JFrameOperator;34import org.netbeans.jemmy.operators.JButtonOperator;35import org.netbeans.jemmy.operators.JTextFieldOperator;36import org.testng.annotations.Listeners;37import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;38import static org.jemmy2ext.JemmyExt.getUIValue;39/*40* @test41* @key headful42* @summary Verifies SwingSet3 GridBagLayoutDemo by checking the relative43* location of all the components before and after resizing the frame,44* interacting with all the controls and checking this interaction on the45* text field display.46*47* @library /sanity/client/lib/jemmy/src48* @library /sanity/client/lib/Extensions/src49* @library /sanity/client/lib/SwingSet3/src50* @modules java.desktop51* java.logging52* @build org.jemmy2ext.JemmyExt53* @build com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo54* @run testng/timeout=600 GridBagLayoutDemoTest55*/5657@Listeners(GuiTestListener.class)58public class GridBagLayoutDemoTest {5960private JTextFieldOperator tfScreen;61private JButtonOperator buttonZero;62private JButtonOperator buttonOne;63private JButtonOperator buttonTwo;64private JButtonOperator buttonThree;65private JButtonOperator buttonFour;66private JButtonOperator buttonFive;67private JButtonOperator buttonSix;68private JButtonOperator buttonSeven;69private JButtonOperator buttonEight;70private JButtonOperator buttonNine;71private JButtonOperator buttonPlus;72private JButtonOperator buttonMinus;73private JButtonOperator buttonMultiply;74private JButtonOperator buttonDivide;75private JButtonOperator buttonComma;76private JButtonOperator buttonSqrt;77private JButtonOperator buttonReciprocal;78private JButtonOperator buttonToggleSign;79private JButtonOperator buttonEquals;80private JButtonOperator backspaceButton;81private JButtonOperator resetButton;82private JFrameOperator mainFrame;8384@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)85public void test(String lookAndFeel) throws Exception {86initializeUIComponents(lookAndFeel);87// Check that the relative location of buttons is as expected.88checkRelativeLocations();89// Interact with buttons and check the result on the display.90checkInteractionOnDisplay();91// Change the location and check that the relative location of buttons is same as before.92checkChangeLocation();93// Change the size and check that the relative location of buttons is same as before.94checkChangeSize();95}9697private double x(Component component) {98return component.getLocation().getX();99}100101private double y(Component component) {102return component.getLocation().getY();103}104105private void checkRight(JButtonOperator currentButton, JButtonOperator rightButton) {106// Check that x coordinate of right button is greater than that of right107// end of current button108currentButton.waitStateOnQueue(button -> x(button) + button.getWidth() < x(rightButton.getSource()));109// Check that the y coordinate of the button is same as the button to110// the right111currentButton.waitStateOnQueue(button -> y(button) == y(rightButton.getSource()));112// Check that the height of the button is same as the button to the113// right114currentButton.waitStateOnQueue(button -> button.getHeight() == rightButton.getHeight());115}116117private void checkBelow(JButtonOperator currentButton, JButtonOperator buttonBelow) {118// Check that y coordinate of button below is greater than that of119// bottom end of current button120currentButton.waitStateOnQueue(button -> y(button) + button.getHeight() < y(buttonBelow.getSource()));121// Check that the x coordinate of the button is same as the button below122currentButton.waitStateOnQueue(button -> x(button) == x(buttonBelow.getSource()));123// Check that the width of the button is same as the button below124currentButton.waitStateOnQueue(button -> button.getWidth() == buttonBelow.getWidth());125}126127private void checkRelativeLocations() {128// Check relative location of button 7129checkRight(buttonSeven, buttonEight);130checkBelow(buttonSeven, buttonFour);131132// Check relative location of button 8133checkRight(buttonEight, buttonNine);134checkBelow(buttonEight, buttonFive);135136// Check relative location of button 9137checkRight(buttonNine, buttonDivide);138checkBelow(buttonNine, buttonSix);139140// Check relative location of Division button141checkRight(buttonDivide, buttonReciprocal);142checkBelow(buttonDivide, buttonMultiply);143144// Check relative location of Reciprocal and Sqrt buttons145checkBelow(buttonReciprocal, buttonSqrt);146147// Check relative location of button 4148checkRight(buttonFour, buttonFive);149checkBelow(buttonFour, buttonOne);150151// Check relative location of button 5152checkRight(buttonFive, buttonSix);153checkBelow(buttonFive, buttonTwo);154155// Check relative location of button 6156checkRight(buttonSix, buttonMultiply);157checkBelow(buttonSix, buttonThree);158159// Check relative location of Multiply button160checkRight(buttonMultiply, buttonSqrt);161checkBelow(buttonMultiply, buttonMinus);162163// Check relative location of button 1164checkRight(buttonOne, buttonTwo);165checkBelow(buttonOne, buttonZero);166167// Check relative location of button 2168checkRight(buttonTwo, buttonThree);169checkBelow(buttonTwo, buttonToggleSign);170171// Check relative location of button 3172checkRight(buttonThree, buttonMinus);173checkBelow(buttonThree, buttonComma);174175// Check relative location of Minus button176checkBelow(buttonMinus, buttonPlus);177178// Check relative location of button 0179checkRight(buttonZero, buttonToggleSign);180181// Check relative location of Sign Toggle Button182checkRight(buttonToggleSign, buttonComma);183184// Check relative location of Comma button185checkRight(buttonComma, buttonPlus);186187// Check relative location of plus and Equals buttons188checkRight(buttonPlus, buttonEquals);189190// Check relative location of JPanel containing Backspace and Reset191// buttons192Point parentLocation = getUIValue(backspaceButton, (JButton button) -> button.getParent().getLocation());193// Check that the y coordinate of bottom of the screen is194// less than that of parent of backspace button195tfScreen.waitStateOnQueue(screen -> y(screen) + screen.getHeight() < parentLocation.getY());196// Check that the y coordinate of the button 7 is greater197// than that of parent of backspace198buttonSeven.waitStateOnQueue(button -> parentLocation.getY() < y(button));199// Check that the y coordinate of reciprocal button is greater200// than that of parent of backspace201buttonReciprocal.waitStateOnQueue(button -> parentLocation.getY() < y(button));202// Check that x coordinate of screen is same as that of parent of203// backspace204tfScreen.waitStateOnQueue(screen -> x(screen) == parentLocation.getX());205// Check that x coordinate of button 7 is same as that of parent of206// backspace207buttonSeven.waitStateOnQueue(button -> x(button) == parentLocation.getX());208209// Check relative location of Backspace button210// Check that the x coordinate of right of backspace button211// is less than that of reset button212backspaceButton.waitStateOnQueue(button -> x(button) + button.getWidth() < x(resetButton.getSource()));213// Check that the height of backspace button is same as that of reset214// button215backspaceButton.waitStateOnQueue(button -> button.getHeight() == resetButton.getHeight());216// Check that the y coordinate bottom of backspace button is less that217// that of button 7218backspaceButton.waitStateOnQueue(219button -> parentLocation.getY() + button.getParent().getHeight() < y(buttonSeven.getSource()));220221// Check that the x coordinate of reset button is greater than that222// of right of backspace button223resetButton.waitStateOnQueue(button -> x(backspaceButton.getSource()) + backspaceButton.getWidth() < x(button));224225// Check that the height of reset button is same as that of backspace226// button227resetButton.waitStateOnQueue(button -> backspaceButton.getHeight() == button.getHeight());228229// Check that the y coordinate of bottom of reset button is less230// than that of divide button.231resetButton.waitStateOnQueue(232button -> parentLocation.getY() + button.getParent().getHeight() < y(buttonDivide.getSource()));233234// Check that the y coordinate of top of screen is lower235// than that of parent of backspace button236tfScreen.waitStateOnQueue(screen -> y(screen) + screen.getHeight() < parentLocation.getY());237}238239private void checkInteractionOnDisplay() {240// Check buttons: 1,2,+,=,C241buttonOne.push();242tfScreen.waitText("1");243buttonPlus.push();244tfScreen.waitText("1");245buttonTwo.push();246tfScreen.waitText("2");247buttonEquals.push();248tfScreen.waitText("3");249resetButton.push();250tfScreen.waitText("0");251252// Check buttons: 3,4,-253buttonFour.push();254tfScreen.waitText("4");255buttonMinus.push();256tfScreen.waitText("4");257buttonThree.push();258tfScreen.waitText("3");259buttonEquals.push();260tfScreen.waitText("1");261reset();262263// Check buttons: 5,6,*264buttonFive.push();265tfScreen.waitText("5");266buttonMultiply.push();267tfScreen.waitText("5");268buttonSix.push();269tfScreen.waitText("6");270buttonEquals.push();271tfScreen.waitText("30");272reset();273274// Check buttons: 8,9,/275buttonNine.push();276buttonNine.push();277tfScreen.waitText("99");278buttonDivide.push();279tfScreen.waitText("99");280buttonEight.push();281tfScreen.waitText("8");282buttonEquals.push();283tfScreen.waitText("12.375");284reset();285286// Check buttons: 7,0,[+/-],Backspace287buttonSeven.push();288tfScreen.waitText("7");289buttonZero.push();290tfScreen.waitText("70");291buttonToggleSign.push();292tfScreen.waitText("-70");293buttonToggleSign.push();294tfScreen.waitText("70");295backspaceButton.push();296tfScreen.waitText("7");297reset();298299// Check Sqrt Button300buttonFour.push();301buttonNine.push();302tfScreen.waitText("49");303buttonSqrt.push();304tfScreen.waitText("7");305reset();306307// Check Reciprocal Button308buttonFour.push();309tfScreen.waitText("4");310buttonReciprocal.push();311tfScreen.waitText("0.25");312reset();313314// Check Comma button315buttonFour.push();316buttonComma.push();317tfScreen.waitText("4,");318}319320private void reset() {321resetButton.push();322tfScreen.waitText("0");323}324325private void initializeUIComponents(String lookAndFeel) throws Exception {326UIManager.setLookAndFeel(lookAndFeel);327new ClassReference(GridBagLayoutDemo.class.getCanonicalName()).startApplication();328mainFrame = new JFrameOperator(GRID_BAG_LAYOUT_DEMO_TITLE);329mainFrame.setComparator(EXACT_STRING_COMPARATOR);330buttonZero = new JButtonOperator(mainFrame, ZERO_BUTTON_TITLE);331buttonOne = new JButtonOperator(mainFrame, ONE_BUTTON_TITLE);332buttonTwo = new JButtonOperator(mainFrame, TWO_BUTTON_TITLE);333buttonThree = new JButtonOperator(mainFrame, THREE_BUTTON_TITLE);334buttonFour = new JButtonOperator(mainFrame, FOUR_BUTTON_TITLE);335buttonFive = new JButtonOperator(mainFrame, FIVE_BUTTON_TITLE);336buttonSix = new JButtonOperator(mainFrame, SIX_BUTTON_TITLE);337buttonSeven = new JButtonOperator(mainFrame, SEVEN_BUTTON_TITLE);338buttonEight = new JButtonOperator(mainFrame, EIGHT_BUTTON_TITLE);339buttonNine = new JButtonOperator(mainFrame, NINE_BUTTON_TITLE);340buttonPlus = new JButtonOperator(mainFrame, PLUS_BUTTON_TITLE);341buttonMinus = new JButtonOperator(mainFrame, MINUS_BUTTON_TITLE);342buttonMultiply = new JButtonOperator(mainFrame, MULTIPLY_BUTTON_TITLE);343buttonDivide = new JButtonOperator(mainFrame, DIVIDE_BUTTON_TITLE);344buttonComma = new JButtonOperator(mainFrame, ",");345buttonSqrt = new JButtonOperator(mainFrame, SQRT_BUTTON_TITLE);346buttonReciprocal = new JButtonOperator(mainFrame, INVERSE_BUTTON_TITLE);347buttonToggleSign = new JButtonOperator(mainFrame, SWAPSIGN_BUTTON_TITLE);348buttonEquals = new JButtonOperator(mainFrame, EQUALS_BUTTON_TITLE);349resetButton = new JButtonOperator(mainFrame, C_BUTTON_TITLE);350backspaceButton = new JButtonOperator(mainFrame, BACKSPACE_BUTTON_TITLE);351tfScreen = new JTextFieldOperator(mainFrame, 0);352}353354private void checkChangeLocation() {355Point startingPoint = new Point(100, 100);356mainFrame.setLocation(startingPoint);357mainFrame.waitComponentLocation(startingPoint);358checkRelativeLocations();359}360361private void checkChangeSize() {362Dimension newSize = new Dimension((int) mainFrame.getToolkit().getScreenSize().getWidth() / 2,363(int) mainFrame.getToolkit().getScreenSize().getHeight() / 2);364mainFrame.setSize(newSize);365mainFrame.waitComponentSize(newSize);366checkRelativeLocations();367}368}369370371