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 android.cts.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 private static final String TAG = "DocumentsClientTestCase"; 48 49 protected UiDevice mDevice; 50 protected MyActivity mActivity; 51 52 protected static final long TIMEOUT = 30 * DateUtils.SECOND_IN_MILLIS; 53 protected static final int REQUEST_CODE = 42; 54 55 @Override setUp()56 public void setUp() throws Exception { 57 super.setUp(); 58 59 Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_FINGER); 60 61 mDevice = UiDevice.getInstance(getInstrumentation()); 62 mActivity = launchActivity(getInstrumentation().getTargetContext().getPackageName(), 63 MyActivity.class, null); 64 mDevice.waitForIdle(); 65 } 66 67 @Override tearDown()68 public void tearDown() throws Exception { 69 super.tearDown(); 70 mActivity.finish(); 71 } 72 getColumn(Uri uri, String column)73 protected String getColumn(Uri uri, String column) { 74 final ContentResolver resolver = getInstrumentation().getContext().getContentResolver(); 75 final Cursor cursor = resolver.query(uri, new String[] { column }, null, null, null); 76 try { 77 assertTrue(cursor.moveToFirst()); 78 return cursor.getString(0); 79 } finally { 80 cursor.close(); 81 } 82 } 83 readFully(Uri uri)84 protected byte[] readFully(Uri uri) throws IOException { 85 final InputStream in = getInstrumentation().getContext().getContentResolver() 86 .openInputStream(uri); 87 return readFully(in); 88 } 89 readTypedFully(Uri uri, String mimeType)90 protected byte[] readTypedFully(Uri uri, String mimeType) throws IOException { 91 final AssetFileDescriptor descriptor = 92 getInstrumentation().getContext().getContentResolver() 93 .openTypedAssetFileDescriptor(uri, mimeType, null, null); 94 try (AutoCloseInputStream in = new AutoCloseInputStream(descriptor)) { 95 return readFully(in); 96 } 97 } 98 readFully(InputStream in)99 protected byte[] readFully(InputStream in) throws IOException { 100 try { 101 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 102 byte[] buffer = new byte[1024]; 103 int count; 104 while ((count = in.read(buffer)) != -1) { 105 bytes.write(buffer, 0, count); 106 } 107 return bytes.toByteArray(); 108 } finally { 109 in.close(); 110 } 111 } 112 writeFully(Uri uri, byte[] data)113 protected void writeFully(Uri uri, byte[] data) throws IOException { 114 OutputStream out = getInstrumentation().getContext().getContentResolver() 115 .openOutputStream(uri); 116 try { 117 out.write(data); 118 } finally { 119 out.close(); 120 } 121 } 122 supportedHardware()123 protected boolean supportedHardware() { 124 final PackageManager pm = getInstrumentation().getContext().getPackageManager(); 125 if (pm.hasSystemFeature("android.hardware.type.television") 126 || pm.hasSystemFeature("android.hardware.type.watch")) { 127 return false; 128 } 129 return true; 130 } 131 assertActivityFailed()132 protected void assertActivityFailed() { 133 final Result result = mActivity.getResult(); 134 assertEquals(REQUEST_CODE, result.requestCode); 135 assertEquals(Activity.RESULT_CANCELED, result.resultCode); 136 assertNull(result.data); 137 } 138 assertActivitySucceeded(String prefix)139 protected Intent assertActivitySucceeded(String prefix) { 140 final Result result = mActivity.getResult(); 141 assertEquals(prefix + ": invalid request code", REQUEST_CODE, result.requestCode); 142 assertEquals(prefix + ": invalid result code", Activity.RESULT_OK, result.resultCode); 143 assertNotNull(prefix + ": null data on result", result.data); 144 return result.data; 145 } 146 executeShellCommand(String command)147 protected String executeShellCommand(String command) throws Exception { 148 final String result = runShellCommand(getInstrumentation(), command).trim(); 149 Log.d(TAG, "Command '" + command + "' returned '" + result + "'"); 150 return result; 151 } 152 clearDocumentsUi()153 protected void clearDocumentsUi() throws Exception { 154 executeShellCommand("pm clear com.android.documentsui"); 155 } 156 } 157