1 /*
2  * Copyright (C) 2020 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.overlaytest.remounted;
18 
19 import static org.junit.Assert.fail;
20 
21 import com.android.internal.util.test.SystemPreparer;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
24 
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.rules.RuleChain;
28 import org.junit.rules.TemporaryFolder;
29 
30 public class OverlayRemountedTestBase extends BaseHostJUnit4Test {
31     private static final long ASSERT_RESOURCE_TIMEOUT_MS = 30000;
32     static final String TARGET_APK = "OverlayRemountedTest_Target.apk";
33     static final String TARGET_PACKAGE = "com.android.overlaytest.remounted.target";
34     static final String OVERLAY_APK = "OverlayRemountedTest_Overlay.apk";
35     static final String OVERLAY_PACKAGE = "com.android.overlaytest.remounted.target.overlay";
36 
37     private final TemporaryFolder mTemporaryFolder = new TemporaryFolder();
38     protected final SystemPreparer mPreparer = new SystemPreparer(mTemporaryFolder,
39             this::getDevice);
40 
41     @Rule
42     public final RuleChain ruleChain = RuleChain.outerRule(mTemporaryFolder).around(mPreparer);
43 
44     @Before
startBefore()45     public void startBefore() throws DeviceNotAvailableException {
46         getDevice().waitForDeviceAvailable();
47     }
48 
49     /** Builds the full name of a resource in the form package:type/entry. */
resourceName(String pkg, String type, String entry)50     String resourceName(String pkg, String type, String entry) {
51         return String.format("%s:%s/%s", pkg, type, entry);
52     }
53 
assertResource(String resourceName, String expectedValue)54     void assertResource(String resourceName, String expectedValue)
55             throws DeviceNotAvailableException {
56         String result = null;
57 
58         final long endMillis = System.currentTimeMillis() + ASSERT_RESOURCE_TIMEOUT_MS;
59         while (System.currentTimeMillis() <= endMillis) {
60             result = getDevice().executeShellCommand(
61                     String.format("cmd overlay lookup %s %s", TARGET_PACKAGE, resourceName));
62             if (result.equals(expectedValue + "\n") ||
63                     result.endsWith("-> " + expectedValue + "\n")) {
64                 return;
65             }
66 
67             try {
68                 Thread.sleep(200);
69             } catch (InterruptedException ignore) {
70             }
71         }
72 
73         fail(String.format("expected: <[%s]> in: <[%s]>", expectedValue, result));
74     }
75 }
76