1
1
package com .lukekorth .deviceautomator ;
2
2
3
+ import android .content .Context ;
3
4
import android .content .Intent ;
4
5
import android .content .pm .PackageManager ;
5
6
import android .os .Build ;
6
7
import android .os .RemoteException ;
8
+
9
+ import androidx .test .core .app .ApplicationProvider ;
7
10
import androidx .test .uiautomator .By ;
8
11
import androidx .test .uiautomator .UiDevice ;
9
12
import androidx .test .uiautomator .UiObject ;
14
17
import android .view .KeyCharacterMap ;
15
18
import android .view .KeyEvent ;
16
19
17
- import static androidx .test .InstrumentationRegistry .getContext ;
18
- import static androidx .test .InstrumentationRegistry .getInstrumentation ;
19
- import static androidx .test .InstrumentationRegistry .getTargetContext ;
20
+ import static androidx .test .platform .app .InstrumentationRegistry .getInstrumentation ;
20
21
import static androidx .test .uiautomator .Until .hasObject ;
21
22
import static junit .framework .Assert .assertTrue ;
22
23
import static org .hamcrest .MatcherAssert .assertThat ;
@@ -121,7 +122,7 @@ public DeviceAutomator launchApp(String packageName) {
121
122
* @return {@link DeviceAutomator} for method chaining.
122
123
*/
123
124
public DeviceAutomator launchApp (String packageName , long timeout ) {
124
- return launchApp (getContext ().getPackageManager ().getLaunchIntentForPackage (packageName ), timeout );
125
+ return launchApp (getInstrumentation (). getContext ().getPackageManager ().getLaunchIntentForPackage (packageName ), timeout );
125
126
}
126
127
127
128
/**
@@ -144,7 +145,7 @@ public DeviceAutomator launchApp(Intent intent) {
144
145
public DeviceAutomator launchApp (Intent intent , long timeout ) {
145
146
intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK )
146
147
.addFlags (Intent .FLAG_ACTIVITY_CLEAR_TASK );
147
- getContext ().startActivity (intent );
148
+ getInstrumentation (). getContext ().startActivity (intent );
148
149
149
150
mDevice .wait (hasObject (By .pkg (intent .getPackage ()).depth (0 )), timeout );
150
151
@@ -279,8 +280,9 @@ public DeviceAutomator denyRuntimePermission(String permission) {
279
280
}
280
281
281
282
private void clickPermissionDialogButton (String permission , int buttonIndex ) {
283
+ Context targetContext = ApplicationProvider .getApplicationContext ();
282
284
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M &&
283
- ContextCompat .checkSelfPermission (getTargetContext () , permission ) != PackageManager .PERMISSION_GRANTED ) {
285
+ ContextCompat .checkSelfPermission (targetContext , permission ) != PackageManager .PERMISSION_GRANTED ) {
284
286
try {
285
287
UiObject allowPermissions = mDevice .findObject (new UiSelector ()
286
288
.clickable (true )
0 commit comments