• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 
17 package android.text.method.cts;
18 
19 import static android.provider.Settings.System.TEXT_AUTO_CAPS;
20 
21 import android.app.AppOpsManager;
22 import android.app.Instrumentation;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.provider.Settings;
26 import android.text.cts.R;
27 import android.text.method.KeyListener;
28 import android.util.Log;
29 import android.view.KeyEvent;
30 import android.widget.EditText;
31 
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.rule.ActivityTestRule;
34 
35 import com.android.compatibility.common.util.PollingCheck;
36 import com.android.compatibility.common.util.SystemUtil;
37 
38 import org.junit.Before;
39 import org.junit.Rule;
40 
41 import java.io.IOException;
42 
43 /**
44  * Base class for various KeyListener tests.
45  */
46 public abstract class KeyListenerTestCase {
47     private static final String TAG = "KeyListenerTestCase";
48 
49     protected KeyListenerCtsActivity mActivity;
50     protected Instrumentation mInstrumentation;
51     protected EditText mTextView;
52     private int mAutoCapSetting;
53 
54     @Rule
55     public ActivityTestRule<KeyListenerCtsActivity> mActivityRule =
56             new ActivityTestRule<>(KeyListenerCtsActivity.class);
57 
58     @Before
setup()59     public void setup() throws IOException {
60         mInstrumentation = InstrumentationRegistry.getInstrumentation();
61         mActivity = mActivityRule.getActivity();
62         mTextView = mActivity.findViewById(R.id.keylistener_textview);
63 
64         PollingCheck.waitFor(5000, mActivity::hasWindowFocus);
65     }
66 
enableAutoCapSettings()67     protected void enableAutoCapSettings() throws IOException {
68         grantWriteSettingsPermission();
69         final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
70         final Context context = instrumentation.getContext();
71         instrumentation.runOnMainSync(() -> {
72             final ContentResolver resolver = context.getContentResolver();
73             mAutoCapSetting = Settings.System.getInt(resolver, TEXT_AUTO_CAPS, 1);
74             try {
75                 Settings.System.putInt(resolver, TEXT_AUTO_CAPS, 1);
76             } catch (SecurityException e) {
77                 Log.e(TAG, "Cannot set TEXT_AUTO_CAPS to 1", e);
78                 // ignore
79             }
80         });
81         instrumentation.waitForIdleSync();
82     }
83 
resetAutoCapSettings()84     protected void resetAutoCapSettings() throws IOException {
85         grantWriteSettingsPermission();
86         final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
87         final Context context = instrumentation.getContext();
88         instrumentation.runOnMainSync(() -> {
89             final ContentResolver resolver = context.getContentResolver();
90             try {
91                 Settings.System.putInt(resolver, TEXT_AUTO_CAPS, mAutoCapSetting);
92             } catch (SecurityException e) {
93                 Log.e(TAG, "Cannot set TEXT_AUTO_CAPS to previous value", e);
94                 // ignore
95             }
96         });
97         instrumentation.waitForIdleSync();
98     }
99 
100     /**
101      * Synchronously sets mTextView's key listener on the UI thread.
102      */
setKeyListenerSync(final KeyListener keyListener)103     protected void setKeyListenerSync(final KeyListener keyListener) {
104         mInstrumentation.runOnMainSync(() -> mTextView.setKeyListener(keyListener));
105         mInstrumentation.waitForIdleSync();
106     }
107 
getKey(int keycode, int metaState)108     protected static KeyEvent getKey(int keycode, int metaState) {
109         long currentTime = System.currentTimeMillis();
110         return new KeyEvent(currentTime, currentTime, KeyEvent.ACTION_DOWN, keycode,
111                 0 /* repeat */, metaState);
112     }
113 
grantWriteSettingsPermission()114     private void grantWriteSettingsPermission() throws IOException {
115         SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(),
116                 "appops set " + mActivity.getPackageName() + " "
117                         + AppOpsManager.OPSTR_WRITE_SETTINGS + " allow");
118     }
119 }
120