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