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