Home | History | Annotate | Download | only in documentclient
      1 /*
      2  * Copyright (C) 2016 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 com.android.cts.documentclient;
     18 
     19 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
     20 
     21 import java.io.ByteArrayOutputStream;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 import java.io.OutputStream;
     25 
     26 import com.android.cts.documentclient.MyActivity.Result;
     27 
     28 import android.app.Activity;
     29 import android.content.ContentResolver;
     30 import android.content.Intent;
     31 import android.content.pm.PackageManager;
     32 import android.content.res.AssetFileDescriptor;
     33 import android.content.res.AssetFileDescriptor.AutoCloseInputStream;
     34 import android.database.Cursor;
     35 import android.net.Uri;
     36 import android.support.test.uiautomator.Configurator;
     37 import android.support.test.uiautomator.UiDevice;
     38 import android.test.InstrumentationTestCase;
     39 import android.text.format.DateUtils;
     40 import android.util.Log;
     41 import android.view.MotionEvent;
     42 
     43 /**
     44  * Base class for DocumentsUI test cases.
     45  */
     46 abstract class DocumentsClientTestCase extends InstrumentationTestCase {
     47     protected static final long TIMEOUT = 30 * DateUtils.SECOND_IN_MILLIS;
     48     protected static final int REQUEST_CODE = 42;
     49 
     50     static final String PROVIDER_PACKAGE = "com.android.cts.documentprovider";
     51 
     52     private static final String TAG = "DocumentsClientTestCase";
     53 
     54     protected UiDevice mDevice;
     55     protected MyActivity mActivity;
     56 
     57     private String[] mDisabledImes;
     58 
     59     @Override
     60     public void setUp() throws Exception {
     61         super.setUp();
     62 
     63         Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_FINGER);
     64 
     65         // Disable IME's to avoid virtual keyboards from showing up. Occasionally IME draws some UI
     66         // controls out of its boundary for some first time setup that obscures the text edit and/or
     67         // save/select button. This will constantly fail some of our tests.
     68         disableImes();
     69 
     70         mDevice = UiDevice.getInstance(getInstrumentation());
     71         mActivity = launchActivity(getInstrumentation().getTargetContext().getPackageName(),
     72                 MyActivity.class, null);
     73         mDevice.waitForIdle();
     74     }
     75 
     76     @Override
     77     public void tearDown() throws Exception {
     78         super.tearDown();
     79         mActivity.finish();
     80 
     81         enableImes();
     82     }
     83 
     84     protected String getColumn(Uri uri, String column) {
     85         final ContentResolver resolver = getInstrumentation().getContext().getContentResolver();
     86         final Cursor cursor = resolver.query(uri, new String[] { column }, null, null, null);
     87         try {
     88             assertTrue(cursor.moveToFirst());
     89             return cursor.getString(0);
     90         } finally {
     91             cursor.close();
     92         }
     93     }
     94 
     95     protected byte[] readFully(Uri uri) throws IOException {
     96         final InputStream in = getInstrumentation().getContext().getContentResolver()
     97                 .openInputStream(uri);
     98         return readFully(in);
     99     }
    100 
    101     protected byte[] readTypedFully(Uri uri, String mimeType) throws IOException {
    102         final AssetFileDescriptor descriptor =
    103                 getInstrumentation().getContext().getContentResolver()
    104                         .openTypedAssetFileDescriptor(uri, mimeType, null, null);
    105         try (AutoCloseInputStream in = new AutoCloseInputStream(descriptor)) {
    106             return readFully(in);
    107         }
    108     }
    109 
    110     protected byte[] readFully(InputStream in) throws IOException {
    111         try {
    112             ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    113             byte[] buffer = new byte[1024];
    114             int count;
    115             while ((count = in.read(buffer)) != -1) {
    116                 bytes.write(buffer, 0, count);
    117             }
    118             return bytes.toByteArray();
    119         } finally {
    120             in.close();
    121         }
    122     }
    123 
    124     protected void writeFully(Uri uri, byte[] data) throws IOException {
    125         OutputStream out = getInstrumentation().getContext().getContentResolver()
    126                 .openOutputStream(uri);
    127         try {
    128             out.write(data);
    129         } finally {
    130             out.close();
    131         }
    132     }
    133 
    134     protected boolean supportedHardware() {
    135         final PackageManager pm = getInstrumentation().getContext().getPackageManager();
    136         if (pm.hasSystemFeature("android.hardware.type.television")
    137                 || pm.hasSystemFeature("android.hardware.type.watch")) {
    138             return false;
    139         }
    140         return true;
    141     }
    142 
    143     protected void assertActivityFailed() {
    144         final Result result = mActivity.getResult();
    145         assertEquals(REQUEST_CODE, result.requestCode);
    146         assertEquals(Activity.RESULT_CANCELED, result.resultCode);
    147         assertNull(result.data);
    148     }
    149 
    150     protected Intent assertActivitySucceeded(String prefix) {
    151         final Result result = mActivity.getResult();
    152         assertEquals(prefix + ": invalid request code", REQUEST_CODE, result.requestCode);
    153         assertEquals(prefix + ": invalid result code", Activity.RESULT_OK, result.resultCode);
    154         assertNotNull(prefix + ": null data on result", result.data);
    155         return result.data;
    156     }
    157 
    158     protected String executeShellCommand(String command) throws Exception {
    159         final String result = runShellCommand(getInstrumentation(), command).trim();
    160         Log.d(TAG, "Command '" + command + "' returned '" + result + "'");
    161         return result;
    162     }
    163 
    164     /**
    165      * Clears the DocumentsUI package data, unless test name ends on {@code DoNotClear}.
    166      */
    167     protected void clearDocumentsUi() throws Exception {
    168         final String testName = getName();
    169         if (testName.endsWith("DoNotClear")) {
    170             Log.d(TAG, "Not clearing DocumentsUI due to test name: " + testName);
    171             return;
    172         }
    173         executeShellCommand("pm clear com.android.documentsui");
    174     }
    175 
    176     private void disableImes() throws Exception {
    177         mDisabledImes = executeShellCommand("ime list -s").split("\n");
    178         for (String ime : mDisabledImes) {
    179             executeShellCommand("ime disable " + ime);
    180         }
    181     }
    182 
    183     private void enableImes() throws Exception {
    184         for (String ime : mDisabledImes) {
    185             executeShellCommand("ime enable " + ime);
    186         }
    187     }
    188 }
    189