1 /*
2  * Copyright (C) 2023 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.server.healthconnect;
18 
19 import android.app.UiAutomation;
20 import android.os.UserHandle;
21 
22 import androidx.annotation.NonNull;
23 import androidx.test.platform.app.InstrumentationRegistry;
24 
25 import java.time.Instant;
26 import java.time.temporal.ChronoUnit;
27 import java.util.concurrent.TimeoutException;
28 import java.util.function.Predicate;
29 
30 public final class TestUtils {
31     public static final UserHandle TEST_USER = UserHandle.of(UserHandle.myUserId());
32 
waitForTaskToFinishSuccessfully(Runnable task)33     public static void waitForTaskToFinishSuccessfully(Runnable task) throws TimeoutException {
34         Instant startTime = Instant.now();
35         while (true) {
36             try {
37                 task.run();
38                 return;
39             } catch (Exception e) {
40                 // ignore
41             } finally {
42                 if (ChronoUnit.SECONDS.between(startTime, Instant.now()) > 3) {
43                     throw new TimeoutException();
44                 }
45             }
46         }
47     }
48 
waitForCondition(Predicate<Void> predicate, int timeoutSeconds)49     static void waitForCondition(Predicate<Void> predicate, int timeoutSeconds)
50             throws TimeoutException {
51         Instant startTime = Instant.now();
52         while (!predicate.test(null)) {
53             if (ChronoUnit.SECONDS.between(startTime, Instant.now()) > timeoutSeconds) {
54                 throw new TimeoutException();
55             }
56         }
57     }
58 
waitForAllScheduledTasksToComplete()59     public static void waitForAllScheduledTasksToComplete() throws TimeoutException {
60         waitForCondition(
61                 (unused) ->
62                         (HealthConnectThreadScheduler.sInternalBackgroundExecutor.getTaskCount()
63                                         == HealthConnectThreadScheduler.sInternalBackgroundExecutor
64                                                 .getCompletedTaskCount())
65                                 || (HealthConnectThreadScheduler.sControllerExecutor.getTaskCount()
66                                         == HealthConnectThreadScheduler.sControllerExecutor
67                                                 .getCompletedTaskCount())
68                                 || (HealthConnectThreadScheduler.sBackgroundThreadExecutor
69                                                 .getTaskCount()
70                                         == HealthConnectThreadScheduler.sBackgroundThreadExecutor
71                                                 .getCompletedTaskCount())
72                                 || (HealthConnectThreadScheduler.sForegroundExecutor.getTaskCount()
73                                         == HealthConnectThreadScheduler.sForegroundExecutor
74                                                 .getCompletedTaskCount()),
75                 15);
76     }
77 
getInternalBackgroundExecutorTaskCount()78     public static long getInternalBackgroundExecutorTaskCount() {
79         return HealthConnectThreadScheduler.sInternalBackgroundExecutor.getTaskCount();
80     }
81 
82     /** Runs a {@link Runnable} adopting a subset of Shell's permissions. */
runWithShellPermissionIdentity( @onNull Runnable runnable, String... permissions)83     public static void runWithShellPermissionIdentity(
84             @NonNull Runnable runnable, String... permissions) {
85         final UiAutomation uiAutomation =
86                 InstrumentationRegistry.getInstrumentation().getUiAutomation();
87         uiAutomation.adoptShellPermissionIdentity(permissions);
88         try {
89             runnable.run();
90         } catch (Exception e) {
91             throw new RuntimeException("Caught exception", e);
92         } finally {
93             uiAutomation.dropShellPermissionIdentity();
94         }
95     }
96 }
97