1/* 2 * Copyright (C) 2012 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.content.Context; 20import android.os.Bundle; 21import android.os.RemoteException; 22import android.os.ServiceManager; 23import android.os.SystemClock; 24import android.view.inputmethod.InputMethodInfo; 25 26import com.android.internal.view.IInputMethodManager; 27import com.android.uiautomator.core.UiDevice; 28 29import junit.framework.TestCase; 30 31import java.util.List; 32 33/** 34 * UI automation test should extend this class. This class provides access 35 * to the following: 36 * {@link UiDevice} instance 37 * {@link Bundle} for command line parameters. 38 */ 39public class UiAutomatorTestCase extends TestCase { 40 41 private static final String DISABLE_IME = "disable_ime"; 42 private static final String DUMMY_IME_PACKAGE = "com.android.testing.dummyime"; 43 private UiDevice mUiDevice; 44 private Bundle mParams; 45 private IAutomationSupport mAutomationSupport; 46 private boolean mShouldDisableIme = false; 47 48 @Override 49 protected void setUp() throws Exception { 50 super.setUp(); 51 mShouldDisableIme = "true".equals(mParams.getString(DISABLE_IME)); 52 if (mShouldDisableIme) { 53 setDummyIme(); 54 } 55 } 56 57 @Override 58 protected void tearDown() throws Exception { 59 if (mShouldDisableIme) { 60 restoreActiveIme(); 61 } 62 super.tearDown(); 63 } 64 65 /** 66 * Get current instance of {@link UiDevice}. Works similar to calling the static 67 * {@link UiDevice#getInstance()} from anywhere in the test classes. 68 */ 69 public UiDevice getUiDevice() { 70 return mUiDevice; 71 } 72 73 /** 74 * Get command line parameters. On the command line when passing <code>-e key value</code> 75 * pairs, the {@link Bundle} will have the key value pairs conveniently available to the 76 * tests. 77 */ 78 public Bundle getParams() { 79 return mParams; 80 } 81 82 /** 83 * Provides support for running tests to report interim status 84 * 85 * @return 86 */ 87 public IAutomationSupport getAutomationSupport() { 88 return mAutomationSupport; 89 } 90 91 /** 92 * package private 93 * @param uiDevice 94 */ 95 void setUiDevice(UiDevice uiDevice) { 96 mUiDevice = uiDevice; 97 } 98 99 /** 100 * package private 101 * @param params 102 */ 103 void setParams(Bundle params) { 104 mParams = params; 105 } 106 107 void setAutomationSupport(IAutomationSupport automationSupport) { 108 mAutomationSupport = automationSupport; 109 } 110 111 /** 112 * Calls {@link SystemClock#sleep(long)} to sleep 113 * @param ms is in milliseconds. 114 */ 115 public void sleep(long ms) { 116 SystemClock.sleep(ms); 117 } 118 119 private void setDummyIme() throws RemoteException { 120 IInputMethodManager im = IInputMethodManager.Stub.asInterface(ServiceManager 121 .getService(Context.INPUT_METHOD_SERVICE)); 122 List<InputMethodInfo> infos = im.getInputMethodList(); 123 String id = null; 124 for (InputMethodInfo info : infos) { 125 if (DUMMY_IME_PACKAGE.equals(info.getComponent().getPackageName())) { 126 id = info.getId(); 127 } 128 } 129 if (id == null) { 130 throw new RuntimeException(String.format( 131 "Required testing fixture missing: IME package (%s)", DUMMY_IME_PACKAGE)); 132 } 133 im.setInputMethod(null, id); 134 } 135 136 private void restoreActiveIme() throws RemoteException { 137 // TODO: figure out a way to restore active IME 138 // Currently retrieving active IME requires querying secure settings provider, which is hard 139 // to do without a Context; so the caveat here is that to make the post test device usable, 140 // the active IME needs to be manually switched. 141 } 142} 143