1 /*
2  * Copyright (C) 2022 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 android.adservices.cts;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import android.os.Build;
22 import android.util.Log;
23 
24 import com.android.modules.utils.build.SdkLevel;
25 
26 import org.junit.AfterClass;
27 import org.junit.BeforeClass;
28 
29 import java.time.Duration;
30 import java.util.concurrent.TimeoutException;
31 
32 abstract class ForegroundCtsTestCase extends CtsAdServicesDeviceTestCase {
33 
34     private static final String TAG = ForegroundCtsTestCase.class.getSimpleName();
35 
36     // NOTICE: if the context used by tests is initialized in the setup method the importance of our
37     // foreground service will be IMPORTANCE_FOREGROUND_SERVICE (125) instead of
38     // IMPORTANCE_FOREGROUND (100) on some platforms only.
39     // This class is indirectly extending AdServicesCtsTestCase - which sets sContext outside any
40     // JUnit @Before / @BeforeClass method - so the process has the proper importance.
41 
42     private static boolean sSimpleActivityStarted;
43 
44     /**
45      * Starts a foreground activity to make the test process a foreground one to pass PPAPI and SDK
46      * Sandbox checks
47      */
makeTestProcessForeground()48     protected static void makeTestProcessForeground() throws TimeoutException {
49         // PPAPI foreground checks are not done on S-, so no need for the SimpleActivity
50         if (SdkLevel.isAtLeastT()) {
51             Log.d(TAG, "Starting activity on T+ (and waiting for 2s)");
52             SimpleActivity.startAndWait(sContext, Duration.ofSeconds(2));
53             Log.d(TAG, "Activity started");
54             sSimpleActivityStarted = true;
55         } else {
56             Log.d(TAG, "Not starting activity on device running " + Build.VERSION.SDK_INT);
57         }
58     }
59 
60     /** Terminates the SimpleActivity */
shutdownForegroundActivity()61     protected static void shutdownForegroundActivity() {
62         if (SdkLevel.isAtLeastT()) {
63             Log.d(TAG, "Stopping activity on T+");
64             SimpleActivity.stop(sContext);
65         } else {
66             Log.d(TAG, "Not stopping activity on device running " + Build.VERSION.SDK_INT);
67         }
68     }
69 
70     @BeforeClass
prepareSuite()71     public static void prepareSuite() throws TimeoutException {
72         makeTestProcessForeground();
73     }
74 
75     @AfterClass
tearDownSuite()76     public static void tearDownSuite() {
77         shutdownForegroundActivity();
78     }
79 
assertForegroundActivityStarted()80     protected static void assertForegroundActivityStarted() {
81         assertWithMessage("Foreground activity started successfully")
82                 .that(sSimpleActivityStarted)
83                 .isTrue();
84     }
85 }
86