EditTextTypeActivity.java revision baf412994612ae72a3db9698b1d216078297b48a
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.android.test.uibench; 17 18import android.app.Instrumentation; 19import android.os.Bundle; 20import android.os.Looper; 21import android.os.MessageQueue; 22import android.support.v7.app.AppCompatActivity; 23import android.view.KeyEvent; 24import android.widget.EditText; 25 26import java.util.concurrent.Semaphore; 27 28/** 29 * Note: currently incomplete, complexity of input continuously grows, instead of looping 30 * over a stable amount of work. 31 * 32 * Simulates typing continuously into an EditText. 33 */ 34public class EditTextTypeActivity extends AppCompatActivity { 35 Thread mThread; 36 37 private static String sSeedText = ""; 38 static { 39 final int count = 100; 40 final String string = "hello "; 41 42 StringBuilder builder = new StringBuilder(count * string.length()); 43 for (int i = 0; i < count; i++) { 44 builder.append(string); 45 } 46 sSeedText = builder.toString(); 47 } 48 49 final Object mLock = new Object(); 50 boolean mShouldStop = false; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 56 EditText editText = new EditText(this); 57 editText.setText(sSeedText); 58 setContentView(editText); 59 60 final Instrumentation instrumentation = new Instrumentation(); 61 final Semaphore sem = new Semaphore(0); 62 MessageQueue.IdleHandler handler = new MessageQueue.IdleHandler() { 63 @Override 64 public boolean queueIdle() { 65 // TODO: consider other signaling approaches 66 sem.release(); 67 return true; 68 } 69 }; 70 Looper.myQueue().addIdleHandler(handler); 71 synchronized (mLock) { 72 mShouldStop = false; 73 } 74 mThread = new Thread(new Runnable() { 75 int codes[] = { KeyEvent.KEYCODE_H, KeyEvent.KEYCODE_E, KeyEvent.KEYCODE_L, 76 KeyEvent.KEYCODE_L, KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_SPACE }; 77 int i = 0; 78 @Override 79 public void run() { 80 while (true) { 81 try { 82 sem.acquire(); 83 } catch (InterruptedException e) { 84 // TODO, maybe 85 } 86 int code = codes[i % codes.length]; 87 if (i % 100 == 99) code = KeyEvent.KEYCODE_ENTER; 88 89 synchronized (mLock) { 90 if (mShouldStop) break; 91 } 92 93 // TODO: bit of a race here, since the event can arrive after pause/stop. 94 // (Can't synchronize on key send, since it's synchronous.) 95 instrumentation.sendKeyDownUpSync(code); 96 i++; 97 } 98 } 99 }); 100 mThread.start(); 101 } 102 103 @Override 104 protected void onPause() { 105 synchronized (mLock) { 106 mShouldStop = true; 107 } 108 super.onPause(); 109 } 110} 111