118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu/* 218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Copyright (C) 2012 The Android Open Source Project 318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * 418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Licensed under the Apache License, Version 2.0 (the "License"); 518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * you may not use this file except in compliance with the License. 618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * You may obtain a copy of the License at 718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * 818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * http://www.apache.org/licenses/LICENSE-2.0 918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * 1018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Unless required by applicable law or agreed to in writing, software 1118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * distributed under the License is distributed on an "AS IS" BASIS, 1218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * See the License for the specific language governing permissions and 1418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * limitations under the License. 1518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 1618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 1718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhupackage com.android.uiautomator.testrunner; 1818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 1918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.content.Context; 2018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.Bundle; 2118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.RemoteException; 2218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.ServiceManager; 2318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.SystemClock; 2418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.view.inputmethod.InputMethodInfo; 2518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 2618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport com.android.internal.view.IInputMethodManager; 2718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport com.android.uiautomator.core.UiDevice; 2818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 2918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport junit.framework.TestCase; 3018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 3118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport java.util.List; 3218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 3318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu/** 3418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * UI automation test should extend this class. This class provides access 3518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * to the following: 3618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * {@link UiDevice} instance 3718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * {@link Bundle} for command line parameters. 3818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @since API Level 16 3918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 4018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhupublic class UiAutomatorTestCase extends TestCase { 4118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 4218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private static final String DISABLE_IME = "disable_ime"; 4318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private static final String DUMMY_IME_PACKAGE = "com.android.testing.dummyime"; 4418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private UiDevice mUiDevice; 4518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private Bundle mParams; 4618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private IAutomationSupport mAutomationSupport; 4718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private boolean mShouldDisableIme = false; 4818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 4918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu @Override 5018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu protected void setUp() throws Exception { 5118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu super.setUp(); 5218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu mShouldDisableIme = "true".equals(mParams.getString(DISABLE_IME)); 5318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu if (mShouldDisableIme) { 5418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu setDummyIme(); 5518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 5618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 5718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 5818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu @Override 5918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu protected void tearDown() throws Exception { 6018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu if (mShouldDisableIme) { 6118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu restoreActiveIme(); 6218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 6318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu super.tearDown(); 6418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 6518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 6618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu /** 6718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Get current instance of {@link UiDevice}. Works similar to calling the static 6818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * {@link UiDevice#getInstance()} from anywhere in the test classes. 6918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @since API Level 16 7018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 7118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu public UiDevice getUiDevice() { 7218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu return mUiDevice; 7318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 7418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 7518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu /** 7618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Get command line parameters. On the command line when passing <code>-e key value</code> 7718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * pairs, the {@link Bundle} will have the key value pairs conveniently available to the 7818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * tests. 7918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @since API Level 16 8018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 8118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu public Bundle getParams() { 8218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu return mParams; 8318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 8418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 8518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu /** 8618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Provides support for running tests to report interim status 8718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * 8818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @return IAutomationSupport 8918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @since API Level 16 9018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 9118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu public IAutomationSupport getAutomationSupport() { 9218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu return mAutomationSupport; 9318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 9418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 9518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu /** 9618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * package private 9718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @param uiDevice 9818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 9918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu void setUiDevice(UiDevice uiDevice) { 10018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu mUiDevice = uiDevice; 10118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 10218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 10318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu /** 10418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * package private 10518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @param params 10618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 10718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu void setParams(Bundle params) { 10818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu mParams = params; 10918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 11018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 11118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu void setAutomationSupport(IAutomationSupport automationSupport) { 11218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu mAutomationSupport = automationSupport; 11318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 11418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 11518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu /** 11618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Calls {@link SystemClock#sleep(long)} to sleep 11718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @param ms is in milliseconds. 11818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * @since API Level 16 11918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */ 12018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu public void sleep(long ms) { 12118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu SystemClock.sleep(ms); 12218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 12318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 12418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private void setDummyIme() throws RemoteException { 12518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu IInputMethodManager im = IInputMethodManager.Stub.asInterface(ServiceManager 12618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu .getService(Context.INPUT_METHOD_SERVICE)); 12718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu List<InputMethodInfo> infos = im.getInputMethodList(); 12818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu String id = null; 12918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu for (InputMethodInfo info : infos) { 13018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu if (DUMMY_IME_PACKAGE.equals(info.getComponent().getPackageName())) { 13118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu id = info.getId(); 13218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 13318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 13418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu if (id == null) { 13518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu throw new RuntimeException(String.format( 13618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu "Required testing fixture missing: IME package (%s)", DUMMY_IME_PACKAGE)); 13718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 13818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu im.setInputMethod(null, id); 13918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 14018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu 14118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu private void restoreActiveIme() throws RemoteException { 14218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu // TODO: figure out a way to restore active IME 14318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu // Currently retrieving active IME requires querying secure settings provider, which is hard 14418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu // to do without a Context; so the caveat here is that to make the post test device usable, 14518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu // the active IME needs to be manually switched. 14618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu } 14718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu} 148