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