• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 com.android.providers.media.util;
18 
19 import static androidx.test.InstrumentationRegistry.getInstrumentation;
20 
21 import android.support.test.uiautomator.UiDevice;
22 
23 import androidx.annotation.NonNull;
24 
25 import java.io.IOException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 
29 public class TestUtils {
30     public static final String QUERY_TYPE = "com.android.providers.media.util.QUERY_TYPE";
31     public static final String RUN_INFINITE_ACTIVITY =
32             "com.android.providers.media.util.RUN_INFINITE_ACTIVITY";
33 
34     private static final long POLLING_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(20);
35     private static final long POLLING_SLEEP_MILLIS = 100;
36 
adoptShellPermission(@onNull String... perms)37     public static void adoptShellPermission(@NonNull String... perms) {
38         getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(perms);
39     }
40 
dropShellPermission()41     public static void dropShellPermission() {
42         getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
43     }
44 
getPid(String packageName)45     public static int getPid(String packageName)
46             throws IOException, InterruptedException, TimeoutException {
47         UiDevice uiDevice = UiDevice.getInstance(getInstrumentation());
48         for (int i = 0; i < POLLING_TIMEOUT_MILLIS / POLLING_SLEEP_MILLIS; i++) {
49             String pid = uiDevice.executeShellCommand("pidof " + packageName).trim();
50             if (pid.length() > 0) {
51                 return new Integer(pid);
52             }
53             Thread.sleep(POLLING_SLEEP_MILLIS);
54         }
55 
56         throw new TimeoutException("Timed out waiting for pid");
57     }
58 }
59