118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu/*
218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Copyright (C) 2013 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.app.Instrumentation;
2018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.Bundle;
2118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.SystemClock;
2218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.test.InstrumentationTestCase;
2318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
2418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport com.android.uiautomator.core.InstrumentationUiAutomatorBridge;
2518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport com.android.uiautomator.core.UiDevice;
2618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
2718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu/**
2818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * UI Automator test case that is executed on the device.
293fb03313e9d0e41f7c14b648c0f4dcfdab78bff3Allen Hair * @deprecated New tests should be written using UI Automator 2.0 which is available as part of the
303fb03313e9d0e41f7c14b648c0f4dcfdab78bff3Allen Hair * Android Testing Support Library.
3118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */
323fb03313e9d0e41f7c14b648c0f4dcfdab78bff3Allen Hair@Deprecated
3318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhupublic class UiAutomatorTestCase extends InstrumentationTestCase {
3418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
3518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    private Bundle mParams;
3618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    private IAutomationSupport mAutomationSupport;
3718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
3818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    /**
3918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * Get current instance of {@link UiDevice}. Works similar to calling the static
4018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * {@link UiDevice#getInstance()} from anywhere in the test classes.
4118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @since API Level 16
4218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     */
4318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    public UiDevice getUiDevice() {
4418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        return UiDevice.getInstance();
4518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
4618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
4718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    /**
4818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * Get command line parameters. On the command line when passing <code>-e key value</code>
4918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * pairs, the {@link Bundle} will have the key value pairs conveniently available to the
5018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * tests.
5118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @since API Level 16
5218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     */
5318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    public Bundle getParams() {
5418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        return mParams;
5518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
5618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
5718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    void setAutomationSupport(IAutomationSupport automationSupport) {
5818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        mAutomationSupport = automationSupport;
5918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
6018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
6118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    /**
6218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * Provides support for running tests to report interim status
6318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     *
6418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @return IAutomationSupport
6518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @since API Level 16
6618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @deprecated Use {@link Instrumentation#sendStatus(int, Bundle)} instead
6718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     */
6818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    public IAutomationSupport getAutomationSupport() {
6918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        if (mAutomationSupport == null) {
7018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            mAutomationSupport = new InstrumentationAutomationSupport(getInstrumentation());
7118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        }
7218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        return mAutomationSupport;
7318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
7418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
7518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    /**
7618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * Initializes this test case.
7718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     *
7818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @param params Instrumentation arguments.
7918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     */
8018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    void initialize(Bundle params) {
8118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        mParams = params;
8218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
8318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        // check if this is a monkey test mode
8418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        String monkeyVal = mParams.getString("monkey");
8518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        if (monkeyVal != null) {
8618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            // only if the monkey key is specified, we alter the state of monkey
8718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            // else we should leave things as they are.
8818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            getInstrumentation().getUiAutomation().setRunAsMonkey(Boolean.valueOf(monkeyVal));
8918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        }
9018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
9118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        UiDevice.getInstance().initialize(new InstrumentationUiAutomatorBridge(
9218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                getInstrumentation().getContext(),
9318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                getInstrumentation().getUiAutomation()));
9418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
9518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
9618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    /**
9718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * Calls {@link SystemClock#sleep(long)} to sleep
9818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @param ms is in milliseconds.
9918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     * @since API Level 16
10018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu     */
10118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    public void sleep(long ms) {
10218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        SystemClock.sleep(ms);
10318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
10418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu}
105