1/*
2 * Copyright (C) 2013 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.app.Instrumentation;
20import android.os.Bundle;
21import android.os.SystemClock;
22import android.test.InstrumentationTestCase;
23
24import com.android.uiautomator.core.InstrumentationUiAutomatorBridge;
25import com.android.uiautomator.core.UiDevice;
26
27/**
28 * UI Automator test case that is executed on the device.
29 */
30public class UiAutomatorTestCase extends InstrumentationTestCase {
31
32    private Bundle mParams;
33    private IAutomationSupport mAutomationSupport;
34
35    /**
36     * Get current instance of {@link UiDevice}. Works similar to calling the static
37     * {@link UiDevice#getInstance()} from anywhere in the test classes.
38     * @since API Level 16
39     */
40    public UiDevice getUiDevice() {
41        return UiDevice.getInstance();
42    }
43
44    /**
45     * Get command line parameters. On the command line when passing <code>-e key value</code>
46     * pairs, the {@link Bundle} will have the key value pairs conveniently available to the
47     * tests.
48     * @since API Level 16
49     */
50    public Bundle getParams() {
51        return mParams;
52    }
53
54    void setAutomationSupport(IAutomationSupport automationSupport) {
55        mAutomationSupport = automationSupport;
56    }
57
58    /**
59     * Provides support for running tests to report interim status
60     *
61     * @return IAutomationSupport
62     * @since API Level 16
63     * @deprecated Use {@link Instrumentation#sendStatus(int, Bundle)} instead
64     */
65    public IAutomationSupport getAutomationSupport() {
66        if (mAutomationSupport == null) {
67            mAutomationSupport = new InstrumentationAutomationSupport(getInstrumentation());
68        }
69        return mAutomationSupport;
70    }
71
72    /**
73     * Initializes this test case.
74     *
75     * @param params Instrumentation arguments.
76     */
77    void initialize(Bundle params) {
78        mParams = params;
79
80        // check if this is a monkey test mode
81        String monkeyVal = mParams.getString("monkey");
82        if (monkeyVal != null) {
83            // only if the monkey key is specified, we alter the state of monkey
84            // else we should leave things as they are.
85            getInstrumentation().getUiAutomation().setRunAsMonkey(Boolean.valueOf(monkeyVal));
86        }
87
88        UiDevice.getInstance().initialize(new InstrumentationUiAutomatorBridge(
89                getInstrumentation().getContext(),
90                getInstrumentation().getUiAutomation()));
91    }
92
93    /**
94     * Calls {@link SystemClock#sleep(long)} to sleep
95     * @param ms is in milliseconds.
96     * @since API Level 16
97     */
98    public void sleep(long ms) {
99        SystemClock.sleep(ms);
100    }
101}
102