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