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