0

I need to have non-ASCII character (like chinese character) inputted in EditText, I found the UiObject.setText() method can not do this work.

So I get a method: copy the character to the clipboard, then paste it to the EditText. Now I have achieved the copy work, but don't know how to achieve the paste action using code.

I searched on the web, and find paste action can be achieved using hotkeys: "menu" + "v"

so I go to the UiDevice api, and find a method: pressKeycode(), but it can only press one key code a time.

Does anyone knows how to press "menu" and "v" at same time using uiautomator or there is some original code to achieve this?

Thanks very much!

Community
  • 1
  • 1
Eddy
  • 3,623
  • 37
  • 44

3 Answers3

2

This works for me:

UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressKeyCode(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_MASK)

MxJ
  • 41
  • 3
0

You have the pressMenu() method as well. see here

I suppose you could also just setText with the text you copied.

Inês
  • 882
  • 3
  • 13
  • 28
  • Thanks for your reply. But the setText() method can only accept ASCII characters. As you know, I have to test other language like Chinese language(zh-CN), so I get a method: copy the characters to clipboard then paste it the the EditText. Now I have achieved the copy work, but don't know how to paste using command. – Eddy May 22 '15 at 02:21
  • sorry for poor question description, I have enriched it.-:) – Eddy May 22 '15 at 02:37
  • Hm... I don't really know how to press two keys at the same time and I'm not familiar with working with non-ASCII characters. However, I'd guess that if you long press the EditText field a pop-up appears with options being one of them paste and you could use it. At this point I'm thinking in the perspective of the user as that's what UiAutomator is suppose to simulate. So I suppose this should work. If it does tell me and I edit my answer :) – Inês May 22 '15 at 11:16
  • 1
    Yes, it work. I think you are right. we should simulate a long press at the EditText and wait for a paste button appear then press it. but it's hard to press the paste button because the paste button can not be found in uiautomatorviewer :( – Eddy May 25 '15 at 07:42
0

I have resolved this issue. see code below:

public class MyTest  extends TestCase{

    /**
    * Paste text to an EditText feild which is currentlly get focused.
    *
    * @param: text the text(Non-ASCII) you want to paste into EditText feild. 
    */
    IClipboard clipboard = IClipboard.Stub.asInterface(ServiceManager.getService(Context.CLIPBOARD_SERVICE));
    IInputManager iInputManager = IInputManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_SERVICE)); 
    private void pastText(String text) throws UiObjectNotFoundException{
        try {
            //copy the text to clipboard.
            clipboard.setPrimaryClip(ClipData.newPlainText("NonASCII", text), text);   

            //inject event: press Menu + V         
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU, 0),1); 
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_V, 0),1);              
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0),1);
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_V, 0),1);              

            //After "Menu"+"V" pressed, A "Menu" will show if exist in current Activicy.
            //Then press menu again, to make it down just for bug fixing.
            sleep(300);
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU, 0),1); 
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0),1);

        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}

Thanks.

Eddy
  • 3,623
  • 37
  • 44