summaryrefslogtreecommitdiffstats
path: root/tests/UiBench/src/com/android/test/uibench/EditTextTypeActivity.java
blob: 08ab5105a5e8d83087a3c6e18ac89db31feaa52e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.test.uibench;

import android.app.Instrumentation;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.EditText;

import java.util.concurrent.Semaphore;

/**
 * Note: currently incomplete, complexity of input continuously grows, instead of looping
 * over a stable amount of work.
 *
 * Simulates typing continuously into an EditText.
 */
public class EditTextTypeActivity extends AppCompatActivity {
    Thread mThread;

    private static String sSeedText = "";
    static {
        final int count = 100;
        final String string = "hello ";

        StringBuilder builder = new StringBuilder(count * string.length());
        for (int i = 0; i < count; i++) {
            builder.append(string);
        }
        sSeedText = builder.toString();
    }

    final Object mLock = new Object();
    boolean mShouldStop = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EditText editText = new EditText(this);
        editText.setText(sSeedText);
        setContentView(editText);

        final Instrumentation instrumentation = new Instrumentation();
        final Semaphore sem = new Semaphore(0);
        MessageQueue.IdleHandler handler = new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                // TODO: consider other signaling approaches
                sem.release();
                return true;
            }
        };
        Looper.myQueue().addIdleHandler(handler);
        synchronized (mLock) {
            mShouldStop = false;
        }
        mThread = new Thread(new Runnable() {
            int codes[] = { KeyEvent.KEYCODE_H, KeyEvent.KEYCODE_E, KeyEvent.KEYCODE_L,
                    KeyEvent.KEYCODE_L, KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_SPACE };
            int i = 0;
            @Override
            public void run() {
                while (true) {
                    try {
                        sem.acquire();
                    } catch (InterruptedException e) {
                        // TODO, maybe
                    }
                    int code = codes[i % codes.length];
                    if (i % 100 == 99) code = KeyEvent.KEYCODE_ENTER;

                    synchronized (mLock) {
                        if (mShouldStop) break;
                    }

                    // TODO: bit of a race here, since the event can arrive after pause/stop.
                    // (Can't synchronize on key send, since it's synchronous.)
                    instrumentation.sendKeyDownUpSync(code);
                    i++;
                }
            }
        });
        mThread.start();
    }

    @Override
    protected void onPause() {
        synchronized (mLock) {
            mShouldStop = true;
        }
        super.onPause();
    }
}