1 /*
   2  * Copyright (c) 2007, 2017 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 org.jemmy.input;
  26 
  27 import org.jemmy.action.Action;
  28 import org.jemmy.control.Wrap;
  29 import org.jemmy.interfaces.Focusable;
  30 import org.jemmy.interfaces.Keyboard;
  31 import org.jemmy.interfaces.Keyboard.KeyboardButtons;
  32 import org.jemmy.interfaces.Keyboard.KeyboardModifiers;
  33 import org.jemmy.interfaces.Text;
  34 import org.jemmy.timing.State;
  35 import static org.jemmy.interfaces.Keyboard.KeyboardButtons.*;
  36 
  37 /**
  38  *
  39  * @author shura
  40  */
  41 public abstract class TextImpl implements Text {
  42 
  43     private static final int DEFAULT_SELECT_ALL_CLICK_COUNT = 3;
  44     private Wrap<?> target;
  45     private int selectAllClickCount = DEFAULT_SELECT_ALL_CLICK_COUNT;
  46     boolean keyboardSelection;
  47 
  48     /**
  49      *
  50      * @param target
  51      * @param keyboardSelection
  52      */
  53     protected TextImpl(Wrap<?> target, boolean keyboardSelection) {
  54         this.target = target;
  55         this.keyboardSelection = keyboardSelection;
  56     }
  57 
  58     /**
  59      *
  60      * @param target
  61      */
  62     protected TextImpl(Wrap<?> target) {
  63         this(target, false);
  64     }
  65 
  66     /**
  67      *
  68      * @return
  69      */
  70     public Wrap<?> getWrap() {
  71         return target;
  72     }
  73 
  74     /**
  75      * Types text into the control. Wrap may implement Focusable.
  76      *
  77      * @see Focusable
  78      * @param newText
  79      */
  80     public void type(final String newText) {
  81         target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, new Action() {
  82             public void run(Object... parameters) {
  83                 if (target.is(Focusable.class)) {
  84                     target.as(Focusable.class).focuser().focus();
  85                 }
  86                 char[] chars = newText.toCharArray();
  87                 Keyboard kb = target.keyboard();
  88                 for (char c : chars) {
  89                     kb.typeChar(c);
  90                 }
  91                 target.getEnvironment().getWaiter(Wrap.WAIT_STATE_TIMEOUT.getName()).ensureState(new State<Object>() {
  92                     public Object reached() {
  93                         return text().contains(newText) ? "" : null;
  94                     }
  95 
  96                     @Override
  97                     public String toString() {
  98                         return "text() equals '" + newText + "', text() = '" + text() + "'";
  99                     }
 100                 });
 101             }
 102 
 103             @Override
 104             public String toString() {
 105                 return "typing text \"" + newText + "\"";
 106             }
 107         });
 108     }
 109 
 110     /**
 111      * Selects all text within component by clicking 3 times on it or using
 112      * keyboard depending on the second argument passed to {@linkplain
 113      * #TextImpl(org.jemmy.control.Wrap, boolean) constructor}. Override if
 114      * needed.<p>
 115      *
 116      * <b>Warning!</b> In Java keyboard selection doesn't work with NumLock
 117      * turned On due to CR 4966137 'Robot presses Numpad del key instead of
 118      * normal Del key'.
 119      */
 120     protected void selectAll() {
 121         if (!keyboardSelection) {
 122             target.mouse().click(selectAllClickCount, target.getClickPoint());
 123         } else {
 124             Keyboard kbrd = target.keyboard();
 125             kbrd.pushKey(KeyboardButtons.HOME);
 126             kbrd.pushKey(KeyboardButtons.END, KeyboardModifiers.SHIFT_DOWN_MASK);
 127             kbrd.pushKey(KeyboardButtons.DELETE);
 128         }
 129     }
 130 
 131     /**
 132      * Clears text by pressing End and then Delete and Backspace until the text
 133      * is cleared.
 134      */
 135     public void clear() {
 136         target.getEnvironment().getExecutor().execute(target.getEnvironment(),
 137                 false, new Action() {
 138             public void run(Object... parameters) {
 139                 if (target.is(Focusable.class)) {
 140                     target.as(Focusable.class).focuser().focus();
 141                 }
 142                 String text = text();
 143                 if (text == null) {
 144                     return;
 145                 }
 146                 target.keyboard().pushKey(END);
 147                 while (!text.isEmpty() && withinAllowedTime()) {
 148                     target.keyboard().pushKey(BACK_SPACE);
 149                     target.keyboard().pushKey(DELETE);
 150                     text = text();
 151                 }
 152             }
 153 
 154             @Override
 155             public String toString() {
 156                 return "clearing text";
 157             }
 158         });
 159     }
 160 }