1 /*
   2  * Copyright (c) 2014, 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 package javafx.scene.control.test.combobox;
  26 
  27 import javafx.geometry.Orientation;
  28 import javafx.scene.Node;
  29 import javafx.scene.Scene;
  30 import javafx.scene.control.ComboBox;
  31 import javafx.scene.control.ListView;
  32 import javafx.scene.control.TextField;
  33 import static javafx.scene.control.test.combobox.ComboBoxApp.*;
  34 import javafx.scene.control.test.util.UtilTestFunctions;
  35 import javafx.stage.PopupWindow;
  36 import org.jemmy.Point;
  37 import org.jemmy.action.GetAction;
  38 import org.jemmy.control.Wrap;
  39 import org.jemmy.env.Environment;
  40 import org.jemmy.fx.ByID;
  41 import org.jemmy.fx.ByWindowType;
  42 import org.jemmy.fx.Root;
  43 import org.jemmy.interfaces.Parent;
  44 import org.jemmy.lookup.LookupCriteria;
  45 import org.junit.After;
  46 import org.junit.Assert;
  47 import static org.junit.Assert.assertEquals;
  48 import org.junit.Before;
  49 import org.junit.BeforeClass;
  50 import org.junit.runner.RunWith;
  51 import test.javaclient.shared.FilteredTestRunner;
  52 import static test.javaclient.shared.TestUtil.isEmbedded;
  53 
  54 /**
  55  * @author Alexander Kirov
  56  */
  57 @RunWith(FilteredTestRunner.class)
  58 public class TestBase extends UtilTestFunctions {
  59 
  60     static Wrap<? extends ComboBox<String>> testedControl;
  61     static Wrap<? extends Scene> scene;
  62     static Wrap<? extends Scene> popupScene;
  63     protected boolean resetHardByDefault = true;//switcher of hard and soft reset mode.
  64     protected boolean doNextResetHard = resetHardByDefault;
  65 
  66     @Before
  67     public void setUp() {
  68         initWrappers();
  69         Environment.getEnvironment().setTimeout("wait.state", isEmbedded() ? 60000 : 2000);
  70         Environment.getEnvironment().setTimeout("wait.control", isEmbedded() ? 60000 : 1000);
  71         scene.mouse().move(new Point(0, 0));
  72     }
  73 
  74     @BeforeClass
  75     public static void setUpClass() throws Exception {
  76         ComboBoxApp.main(null);
  77         currentSettingOption = SettingOption.PROGRAM;
  78     }
  79 
  80     @After
  81     public void tearDown() {
  82         if (doNextResetHard) {
  83             resetSceneHard();
  84         } else {
  85             resetSceneSoft();
  86         }
  87 
  88         doNextResetHard = resetHardByDefault;
  89     }
  90 
  91     protected void initWrappers() {
  92         scene = Root.ROOT.lookup().wrap();
  93         parent = scene.as(Parent.class, Node.class);
  94 
  95         testedControl = (Wrap<? extends ComboBox<String>>) parent.lookup(ComboBox.class, new ByID<ComboBox>(TESTED_COMBOBOX_ID)).wrap();
  96     }
  97 
  98     //                   ACTIONS
  99     protected void resetSceneSoft() {
 100         clickButtonForTestPurpose(SOFT_RESET_BUTTON_ID);
 101     }
 102 
 103     protected void resetSceneHard() {
 104         clickButtonForTestPurpose(HARD_RESET_BUTTON_ID);
 105     }
 106 
 107     protected void doNextResetHard() {
 108         doNextResetHard = true;
 109     }
 110 
 111     protected void doNextResetSoft() {
 112         doNextResetHard = false;
 113     }
 114 
 115     protected void applyCustomCellAPI() throws InterruptedException {
 116         clickButtonForTestPurpose(APPLY_CUSTOM_CELL_FACTORY_BUTTON_ID);
 117         Thread.sleep(SLEEP);
 118     }
 119 
 120     protected void applyCustomSelectionModel() {
 121         clickButtonForTestPurpose(APPLY_CUSTOM_SELECTION_MODEL_BUTTON_ID);
 122     }
 123 
 124     protected void applyCustomStringConverter() {
 125         clickButtonForTestPurpose(APPLY_CUSTOM_STRING_CONVERTER_BUTTON_ID);
 126     }
 127 
 128     protected void removeFromPos(int position) {
 129         setText(findTextField(REMOVE_ITEM_POS_TEXT_FIELD_ID), position);
 130         clickButtonForTestPurpose(REMOVE_BUTTON_ID);
 131     }
 132 
 133     protected void addElement(String element, int position) {
 134         setText(findTextField(ADD_ITEM_POSITION_TEXT_FIELD_ID), position);
 135         setText(findTextField(ADD_ITEM_TEXT_FIELD_ID), element);
 136         clickButtonForTestPurpose(ADD_ITEM_BUTTON_ID);
 137     }
 138 
 139     protected void addElements(Object... elements) {
 140         for (int i = 0; i < elements.length; i++) {
 141             addElement(String.valueOf(elements[i]), i);
 142         }
 143     }
 144 
 145     protected void clickDropDownButton() {
 146         testedControl.mouse().click(1, new Point(testedControl.getScreenBounds().width - 10, testedControl.getScreenBounds().height - 10));
 147     }
 148 
 149     protected Wrap<? extends Scene> getPopupWrap() {
 150         Wrap<? extends Scene> temp;
 151         try {
 152             temp = Root.ROOT.lookup(new ByWindowType(PopupWindow.class)).lookup(Scene.class).wrap(0);
 153         } catch (Exception e) {
 154             return null;
 155         }
 156         return temp;
 157     }
 158 
 159     protected boolean isPopupVisible() {
 160         return isPopupVisible(true);
 161     }
 162 
 163     protected boolean isPopupVisible(boolean checkCoordinates) {
 164         if ((popupScene = getPopupWrap()) != null) {
 165             Boolean res = new GetAction<Boolean>() {
 166                 @Override
 167                 public void run(Object... os) throws Exception {
 168                     setResult(popupScene.getControl().getWindow().showingProperty().getValue());
 169                 }
 170             }.dispatch(Root.ROOT.getEnvironment());
 171             if (res && checkCoordinates) {
 172                 checkPopupCoordinates();
 173             }
 174             return res;
 175         } else {
 176             return false;
 177         }
 178     }
 179 
 180     protected Wrap<? extends TextField> getEditTextFieldWrap() {
 181         return testedControl.as(Parent.class, Node.class).lookup(TextField.class).wrap(0);
 182     }
 183 
 184     protected String getCurrentValue() {
 185         return new GetAction<String>() {
 186 
 187             @Override
 188             public void run(Object... os) throws Exception {
 189                 setResult(testedControl.getControl().getValue().toString());
 190             }
 191         }.dispatch(Root.ROOT.getEnvironment());
 192     }
 193 
 194     protected String getTextFieldPrompt() {
 195         return new GetAction<String>() {
 196 
 197             @Override
 198             public void run(Object... os) throws Exception {
 199                 setResult(getEditTextFieldWrap().getControl().getPromptText());
 200             }
 201         }.dispatch(Root.ROOT.getEnvironment());
 202     }
 203 
 204     protected String getTextFieldText() {
 205         return new GetAction<String>() {
 206 
 207             @Override
 208             public void run(Object... os) throws Exception {
 209                 setResult(getEditTextFieldWrap().getControl().getText());
 210             }
 211         }.dispatch(Root.ROOT.getEnvironment());
 212     }
 213 
 214     protected String getValue() {
 215         return new GetAction<String>() {
 216 
 217             @Override
 218             public void run(Object... os) throws Exception {
 219                 Object res = testedControl.getControl().getValue();
 220                 if (res != null) {
 221                     setResult(testedControl.getControl().getValue().toString());
 222                 } else {
 223                     setResult("null");
 224                 }
 225             }
 226         }.dispatch(Root.ROOT.getEnvironment());
 227     }
 228 
 229     protected void checkValue(String str) {
 230         assertEquals(str, getValue());
 231         checkTextFieldText(Properties.value, str);
 232     }
 233 
 234     //                  CHECKERS
 235     protected void checkEditable(boolean editable) {
 236         checkTextFieldText(Properties.editable, String.valueOf(editable));
 237         Assert.assertEquals(checkTextFieldVisibility(), editable);
 238     }
 239 
 240     protected void checkPopupShowing(boolean showing) throws InterruptedException {
 241         checkSimpleListenerValue(Properties.showing, String.valueOf(showing));
 242         Assert.assertEquals(isPopupVisible(), showing);
 243     }
 244 
 245     protected void checkScrollBarVisibility(boolean visible) throws InterruptedException {
 246         Wrap<? extends Scene> popup = getPopupWrap();
 247         if (popup != null) {
 248             Assert.assertTrue(findScrollBar(((Wrap<? extends ListView>) popup.as(Parent.class, Node.class).lookup(ListView.class).wrap()).as(Parent.class, Node.class), Orientation.VERTICAL, visible) != null);

 249         }
 250     }
 251 
 252     protected boolean checkTextFieldVisibility() {
 253         Parent<Node> parentWrap = testedControl.as(Parent.class, Node.class);
 254         LookupCriteria<Node> lc = new LookupCriteria<Node>() {
 255 
 256             public boolean check(Node cntrl) {
 257                 return cntrl instanceof TextField && cntrl.isVisible();
 258             }
 259         };
 260         return parentWrap.lookup(lc).size() != 0;
 261     }
 262 
 263     public void checkPopupCoordinates() {
 264         Wrap listViewWrap = getPopupWrap().as(Parent.class, Node.class).lookup(ListView.class).wrap();
 265         assertEquals(listViewWrap.getScreenBounds().x, testedControl.getScreenBounds().x, 2);
 266         assertEquals(listViewWrap.getScreenBounds().y, testedControl.getScreenBounds().y + testedControl.getScreenBounds().height, 2);
 267     }
 268 
 269     protected void checkSelectionState(int currentIndex, int currentItem) {
 270         checkSimpleListenerValue(Properties.selectedIndex, currentIndex);
 271         checkSimpleListenerValue(Properties.selectedItem, currentItem);
 272     }
 273     protected final static int ITER = 30;
 274 
 275     static protected enum Properties {
 276 
 277         prefWidth, prefHeight, visibleRowCount, promptText, visible, disable, showing, armed, editable, selectedIndex, selectedItem, value, text, parent, placeholder
 278     };
 279 }
--- EOF ---