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 
  26 package javafx.scene.control.test.textinput;
  27 
  28 import javafx.scene.Node;
  29 import javafx.scene.control.TextInputControl;
  30 import javafx.scene.control.test.textinput.TextInputChanger.TextInputControlWrapInterface;
  31 import javafx.scene.text.Text;
  32 import org.jemmy.action.GetAction;
  33 import org.jemmy.control.Wrap;
  34 import org.jemmy.fx.Root;
  35 import org.jemmy.interfaces.Keyboard;
  36 import org.jemmy.interfaces.Keyboard.KeyboardButtons;
  37 import org.jemmy.interfaces.Keyboard.KeyboardModifier;
  38 import org.jemmy.interfaces.Keyboard.KeyboardModifiers;
  39 import org.jemmy.interfaces.Parent;
  40 import org.jemmy.lookup.LookupCriteria;
  41 import test.javaclient.shared.Utils;
  42 
  43 public class TextInputExternalWrap implements TextInputControlWrapInterface {
  44 
  45     protected Wrap<? extends TextInputControl> wrap = null;
  46     protected Wrap<? extends Text> textWrap = null;
  47     protected Keyboard kbd = null;
  48     protected static KeyboardModifier CTRL_DOWN_MASK_OS = Utils.isMacOS() ? CTRL_DOWN_MASK_OS = KeyboardModifiers.META_DOWN_MASK : KeyboardModifiers.CTRL_DOWN_MASK;
  49 
  50     public TextInputExternalWrap(Wrap<? extends TextInputControl> _wrap) {
  51         wrap = _wrap;
  52         kbd = wrap.keyboard();
  53         Parent<Node> parent = wrap.as(Parent.class, Node.class);
  54         textWrap = parent.lookup(Text.class, new LookupCriteria<Text>(){
  55             public boolean check(Text cntrl) {
  56                 return cntrl.isVisible();
  57             }
  58         }).wrap();
  59 
  60         wrap.getControl().requestFocus();
  61     }
  62     public TextInputControl getControl() {
  63         return wrap.getControl();
  64     }
  65     public Wrap<? extends TextInputControl> getWrap() {
  66         return wrap;
  67     }
  68     public void backward() {
  69         kbd.pushKey(KeyboardButtons.LEFT);
  70     }
  71     public void copy() {
  72         kbd.pushKey(KeyboardButtons.C, CTRL_DOWN_MASK_OS);
  73     }
  74     public void cut() {
  75         kbd.pushKey(KeyboardButtons.X, CTRL_DOWN_MASK_OS);
  76     }
  77     public boolean deleteNextChar() {
  78         boolean res = new GetAction<Boolean>() {
  79             @Override
  80             public void run(Object... parameters) throws Exception {
  81                 setResult(wrap.getControl().getSelection().getStart() < wrap.getControl().getText().length());
  82             }
  83 
  84         }.dispatch(Root.ROOT.getEnvironment());
  85         kbd.pushKey(KeyboardButtons.DELETE);
  86         return res;
  87     }
  88     public boolean deletePreviousChar() {
  89         boolean res = new GetAction<Boolean>() {
  90             @Override
  91             public void run(Object... parameters) throws Exception {
  92                 setResult(wrap.getControl().getSelection().getStart() != 0);
  93             }
  94         }.dispatch(Root.ROOT.getEnvironment());
  95         kbd.pushKey(KeyboardButtons.BACK_SPACE);
  96         return res;
  97     }
  98     public void end() {
  99         kbd.pushKey(KeyboardButtons.END);
 100         // workaround
 101         //textWrap.mouse().click(1, new Point(textWrap.getScreenBounds().width, 0));
 102         //HitInfo hit = textWrap.getControl().impl_hitTestChar(new Point2D(0, 0));
 103     }
 104     public void forward() {
 105         kbd.pushKey(KeyboardButtons.RIGHT);
 106     }
 107     public void home() {
 108         kbd.pushKey(KeyboardButtons.HOME);
 109         // workaround
 110         //textWrap.mouse().click(1, new Point(0, 0));
 111     }
 112     public void nextWord() {
 113         if (Utils.isMacOS()) {
 114             kbd.pushKey(KeyboardButtons.RIGHT, KeyboardModifiers.ALT_DOWN_MASK);
 115         } else {
 116             kbd.pushKey(KeyboardButtons.RIGHT, CTRL_DOWN_MASK_OS);
 117         }
 118     }
 119     public void paste() {
 120         kbd.pushKey(KeyboardButtons.V, CTRL_DOWN_MASK_OS);
 121     }
 122     public void previousWord() {
 123         if (Utils.isMacOS()) {
 124             kbd.pushKey(KeyboardButtons.LEFT, KeyboardModifiers.ALT_DOWN_MASK);
 125         } else {
 126             kbd.pushKey(KeyboardButtons.LEFT, CTRL_DOWN_MASK_OS);
 127         }
 128     }
 129     public void selectAll() {
 130         kbd.pushKey(KeyboardButtons.A, CTRL_DOWN_MASK_OS);
 131     }
 132     public void selectBackward() {
 133         kbd.pushKey(KeyboardButtons.LEFT, KeyboardModifiers.SHIFT_DOWN_MASK);
 134     }
 135     public void selectEnd() {
 136         kbd.pushKey(KeyboardButtons.END, KeyboardModifiers.SHIFT_DOWN_MASK);
 137     }
 138     public void selectForward() {
 139         kbd.pushKey(KeyboardButtons.RIGHT, KeyboardModifiers.SHIFT_DOWN_MASK);
 140     }
 141     public void selectHome() {
 142         kbd.pushKey(KeyboardButtons.HOME, KeyboardModifiers.SHIFT_DOWN_MASK);
 143     }
 144     public void selectNextWord() {
 145         if (Utils.isMacOS()) {
 146             KeyboardModifier modifiers[] = {KeyboardModifiers.SHIFT_DOWN_MASK, KeyboardModifiers.ALT_DOWN_MASK};
 147             kbd.pushKey(KeyboardButtons.RIGHT, modifiers);
 148         } else {
 149             KeyboardModifier modifiers[] = {KeyboardModifiers.SHIFT_DOWN_MASK, CTRL_DOWN_MASK_OS};
 150             kbd.pushKey(KeyboardButtons.RIGHT, modifiers);
 151         }
 152     }
 153     public void selectPreviousWord() {
 154         if (Utils.isMacOS()) {
 155             KeyboardModifier modifiers[] = {KeyboardModifiers.SHIFT_DOWN_MASK, KeyboardModifiers.ALT_DOWN_MASK};
 156             kbd.pushKey(KeyboardButtons.LEFT, modifiers);
 157         } else {
 158             KeyboardModifier modifiers[] = {KeyboardModifiers.SHIFT_DOWN_MASK, CTRL_DOWN_MASK_OS};
 159             kbd.pushKey(KeyboardButtons.LEFT, modifiers);
 160         }
 161     }
 162 //    // temporary workaround
 163 //    protected void push(final Node target, final KeyCode key_code, final boolean shift, final boolean ctrl) {
 164 //        new GetAction() {
 165 //            @Override
 166 //            public void run(Object... parameters) {
 167 //                try {
 168 //                    KeyEvent ke = KeyEvent.impl_keyEvent(target, " ", "", key_code.impl_getCode(), shift, ctrl, false, false, KeyEvent.KEY_PRESSED);
 169 //                    target.fireEvent(ke);
 170 //                    ke = KeyEvent.impl_keyEvent(target, " ", "", key_code.impl_getCode(), shift, ctrl, false, false, KeyEvent.KEY_RELEASED);
 171 //                    target.fireEvent(ke);
 172 //                } catch (Exception ex) {
 173 //                    int i = 0;
 174 //                }
 175 //            }
 176 //        }.dispatch(wrap.getEnvironment());
 177 //    }
 178     public void setText(String str) {
 179         selectAll();
 180         type(str);
 181     }
 182     public void type(String str) {
 183         for (int i = 0; i < str.length(); i++)
 184             kbd.typeChar(str.charAt(i));
 185     }
 186 }