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 17package com.android.uiautomator.testrunner; 18 19import android.app.Instrumentation; 20import android.os.Bundle; 21import android.os.SystemClock; 22import android.test.InstrumentationTestCase; 23 24import com.android.uiautomator.core.InstrumentationUiAutomatorBridge; 25import com.android.uiautomator.core.UiDevice; 26 27/** 28 * UI Automator test case that is executed on the device. 29 * @deprecated New tests should be written using UI Automator 2.0 which is available as part of the 30 * Android Testing Support Library. 31 */ 32@Deprecated 33public class UiAutomatorTestCase extends InstrumentationTestCase { 34 35 private Bundle mParams; 36 private IAutomationSupport mAutomationSupport; 37 38 /** 39 * Get current instance of {@link UiDevice}. Works similar to calling the static 40 * {@link UiDevice#getInstance()} from anywhere in the test classes. 41 * @since API Level 16 42 */ 43 public UiDevice getUiDevice() { 44 return UiDevice.getInstance(); 45 } 46 47 /** 48 * Get command line parameters. On the command line when passing <code>-e key value</code> 49 * pairs, the {@link Bundle} will have the key value pairs conveniently available to the 50 * tests. 51 * @since API Level 16 52 */ 53 public Bundle getParams() { 54 return mParams; 55 } 56 57 void setAutomationSupport(IAutomationSupport automationSupport) { 58 mAutomationSupport = automationSupport; 59 } 60 61 /** 62 * Provides support for running tests to report interim status 63 * 64 * @return IAutomationSupport 65 * @since API Level 16 66 * @deprecated Use {@link Instrumentation#sendStatus(int, Bundle)} instead 67 */ 68 public IAutomationSupport getAutomationSupport() { 69 if (mAutomationSupport == null) { 70 mAutomationSupport = new InstrumentationAutomationSupport(getInstrumentation()); 71 } 72 return mAutomationSupport; 73 } 74 75 /** 76 * Initializes this test case. 77 * 78 * @param params Instrumentation arguments. 79 */ 80 void initialize(Bundle params) { 81 mParams = params; 82 83 // check if this is a monkey test mode 84 String monkeyVal = mParams.getString("monkey"); 85 if (monkeyVal != null) { 86 // only if the monkey key is specified, we alter the state of monkey 87 // else we should leave things as they are. 88 getInstrumentation().getUiAutomation().setRunAsMonkey(Boolean.valueOf(monkeyVal)); 89 } 90 91 UiDevice.getInstance().initialize(new InstrumentationUiAutomatorBridge( 92 getInstrumentation().getContext(), 93 getInstrumentation().getUiAutomation())); 94 } 95 96 /** 97 * Calls {@link SystemClock#sleep(long)} to sleep 98 * @param ms is in milliseconds. 99 * @since API Level 16 100 */ 101 public void sleep(long ms) { 102 SystemClock.sleep(ms); 103 } 104} 105