blob: 03358a8267236fcfca14afa6b729efb100d19480 [file] [log] [blame]
Abodunrinwa Toki223c8392015-07-24 20:55:59 -07001/*
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 */
16
17package android.widget;
18
19import com.android.frameworks.coretests.R;
20
21import android.app.Activity;
22import android.os.Bundle;
Seigo Nonakaff3bfd52016-01-21 19:47:21 +090023import android.os.SystemClock;
24import android.util.Log;
Abodunrinwa Toki223c8392015-07-24 20:55:59 -070025
26/**
27 * An activity for testing the TextView widget.
Seigo Nonakaff3bfd52016-01-21 19:47:21 +090028 *
29 * This class is copied from {@link android.text.method.cts.KeyListenerCtsActivity} in
30 * CtsTextTestCase. The original class is located at
31 * cts/tests/tests/text/src/android/text/method/cts/KeyListenerCtsActivity.java
Abodunrinwa Toki223c8392015-07-24 20:55:59 -070032 */
33public class TextViewActivity extends Activity {
Seigo Nonakaff3bfd52016-01-21 19:47:21 +090034 private boolean mHasWindowFocus = false;
35 private Object mHasWindowFocusLock = new Object();
Abodunrinwa Toki223c8392015-07-24 20:55:59 -070036
37 @Override
38 protected void onCreate(Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
40 setContentView(R.layout.activity_text_view);
41 }
Seigo Nonakaff3bfd52016-01-21 19:47:21 +090042
43 @Override
44 public void onWindowFocusChanged(boolean hasFocus) {
45 super.onWindowFocusChanged(hasFocus);
46 if (!hasFocus) {
47 Log.w("TextViewActivity", "TextViewActivity lost window focus");
48 }
49 synchronized(mHasWindowFocusLock) {
50 mHasWindowFocus = hasFocus;
51 mHasWindowFocusLock.notify();
52 }
53 }
54
55 /**
56 * Blocks the calling thread until the {@link KeyListenerCtsActivity} has window focus or the
57 * specified duration (in milliseconds) has passed.
58 */
59 public boolean waitForWindowFocus(long durationMillis) {
60 long elapsedMillis = SystemClock.elapsedRealtime();
61 synchronized(mHasWindowFocusLock) {
62 mHasWindowFocus = hasWindowFocus();
63 while (!mHasWindowFocus && durationMillis > 0) {
64 long newElapsedMillis = SystemClock.elapsedRealtime();
65 durationMillis -= (newElapsedMillis - elapsedMillis);
66 elapsedMillis = newElapsedMillis;
67 if (durationMillis > 0) {
68 try {
69 mHasWindowFocusLock.wait(durationMillis);
70 } catch (InterruptedException e) {
71 }
72 }
73 }
74 return mHasWindowFocus;
75 }
76 }
Abodunrinwa Toki223c8392015-07-24 20:55:59 -070077}