147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu/* 247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * Copyright (C) 2013 The Android Open Source Project 347d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * 447d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * Licensed under the Apache License, Version 2.0 (the "License"); 547d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * you may not use this file except in compliance with the License. 647d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * You may obtain a copy of the License at 747d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * 847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * http://www.apache.org/licenses/LICENSE-2.0 947d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * 1047d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * Unless required by applicable law or agreed to in writing, software 1147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * distributed under the License is distributed on an "AS IS" BASIS, 1247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1347d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * See the License for the specific language governing permissions and 1447d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * limitations under the License. 1547d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu */ 1647d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 1747d54d40309083809bf04ba07a5b7191fbca9351Guang Zhupackage com.android.uiautomator.testrunner; 1847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 193f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhuimport android.app.Instrumentation; 2047d54d40309083809bf04ba07a5b7191fbca9351Guang Zhuimport android.os.Bundle; 2147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhuimport android.os.SystemClock; 2247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhuimport android.test.InstrumentationTestCase; 2347d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 2447d54d40309083809bf04ba07a5b7191fbca9351Guang Zhuimport com.android.uiautomator.core.InstrumentationUiAutomatorBridge; 2547d54d40309083809bf04ba07a5b7191fbca9351Guang Zhuimport com.android.uiautomator.core.UiDevice; 2647d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 2747d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu/** 2847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * UI Automator test case that is executed on the device. 2947d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu */ 3047d54d40309083809bf04ba07a5b7191fbca9351Guang Zhupublic class UiAutomatorTestCase extends InstrumentationTestCase { 3147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 3247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu private Bundle mParams; 333f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu private IAutomationSupport mAutomationSupport; 3447d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 3547d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu /** 3647d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * Get current instance of {@link UiDevice}. Works similar to calling the static 3747d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * {@link UiDevice#getInstance()} from anywhere in the test classes. 3847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * @since API Level 16 3947d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu */ 4047d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu public UiDevice getUiDevice() { 4147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu return UiDevice.getInstance(); 4247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu } 4347d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 4447d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu /** 4547d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * Get command line parameters. On the command line when passing <code>-e key value</code> 4647d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * pairs, the {@link Bundle} will have the key value pairs conveniently available to the 4747d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * tests. 4847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * @since API Level 16 4947d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu */ 5047d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu public Bundle getParams() { 5147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu return mParams; 5247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu } 5347d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 543f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu void setAutomationSupport(IAutomationSupport automationSupport) { 553f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu mAutomationSupport = automationSupport; 563f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu } 573f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu 583f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu /** 593f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu * Provides support for running tests to report interim status 603f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu * 613f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu * @return IAutomationSupport 623f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu * @since API Level 16 633f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu * @deprecated Use {@link Instrumentation#sendStatus(int, Bundle)} instead 643f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu */ 653f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu public IAutomationSupport getAutomationSupport() { 663f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu if (mAutomationSupport == null) { 673f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu mAutomationSupport = new InstrumentationAutomationSupport(getInstrumentation()); 683f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu } 693f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu return mAutomationSupport; 703f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu } 713f81054f180782104652aca9f83e94cc93a1a6cdGuang Zhu 7247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu /** 7347d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * Initializes this test case. 7447d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * 7547d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * @param params Instrumentation arguments. 7647d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu */ 7747d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu void initialize(Bundle params) { 7847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu mParams = params; 79a90a115409fe406e465a2a8431108791d4181502Adam Momtaz 80a90a115409fe406e465a2a8431108791d4181502Adam Momtaz // check if this is a monkey test mode 81a90a115409fe406e465a2a8431108791d4181502Adam Momtaz String monkeyVal = mParams.getString("monkey"); 82a90a115409fe406e465a2a8431108791d4181502Adam Momtaz if (monkeyVal != null) { 83a90a115409fe406e465a2a8431108791d4181502Adam Momtaz // only if the monkey key is specified, we alter the state of monkey 84a90a115409fe406e465a2a8431108791d4181502Adam Momtaz // else we should leave things as they are. 85a90a115409fe406e465a2a8431108791d4181502Adam Momtaz getInstrumentation().getUiAutomation().setRunAsMonkey(Boolean.valueOf(monkeyVal)); 86a90a115409fe406e465a2a8431108791d4181502Adam Momtaz } 87a90a115409fe406e465a2a8431108791d4181502Adam Momtaz 8847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu UiDevice.getInstance().initialize(new InstrumentationUiAutomatorBridge( 8947d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu getInstrumentation().getContext(), 9047d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu getInstrumentation().getUiAutomation())); 9147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu } 9247d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu 9347d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu /** 9447d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * Calls {@link SystemClock#sleep(long)} to sleep 9547d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * @param ms is in milliseconds. 9647d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu * @since API Level 16 9747d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu */ 9847d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu public void sleep(long ms) { 9947d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu SystemClock.sleep(ms); 10047d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu } 10147d54d40309083809bf04ba07a5b7191fbca9351Guang Zhu} 102