• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.cts.overlay.target;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import android.os.UserHandle;
23 
24 import com.android.compatibility.common.util.PollingCheck;
25 import com.android.compatibility.common.util.SystemUtil;
26 
27 import java.util.concurrent.TimeUnit;
28 
29 public class Utils {
30     // Overlay states
31     private static final String STATE_DISABLED = "STATE_DISABLED";
32     private static final String STATE_ENABLED = "STATE_ENABLED";
33     private static final long TIMEOUT_MS = TimeUnit.SECONDS.toMillis(5);
34 
35     /**
36      * A util function that is commonly used in app overlay scenarios to enable or disable a RRO.
37      */
setOverlayEnabled(String overlayPackage, boolean enabled)38     public static void setOverlayEnabled(String overlayPackage, boolean enabled)
39             throws Exception {
40         final String current = getStateForOverlay(overlayPackage);
41         final String expected = enabled ? STATE_ENABLED : STATE_DISABLED;
42         assertThat(current).isNotEqualTo(expected);
43         SystemUtil.runShellCommand("cmd overlay "
44                 + (enabled ? "enable" : "disable")
45                 + " --user current "
46                 + overlayPackage);
47         PollingCheck.check("Fail to wait overlay enabled state " + expected
48                         + " for " + overlayPackage, TIMEOUT_MS,
49                 () -> expected.equals(getStateForOverlay(overlayPackage)));
50     }
51 
getStateForOverlay(String overlayPackage)52     private static String getStateForOverlay(String overlayPackage) {
53         final String errorMsg = "Fail to parse the state of overlay package " + overlayPackage;
54         final String result = SystemUtil.runShellCommand("cmd overlay dump");
55         final String overlayPackageForCurrentUser = overlayPackage + ":" + UserHandle.myUserId();
56         final int startIndex = result.indexOf(overlayPackageForCurrentUser);
57         assertWithMessage(errorMsg).that(startIndex).isAtLeast(0);
58 
59         final int endIndex = result.indexOf('}', startIndex);
60         assertWithMessage(errorMsg).that(endIndex).isGreaterThan(startIndex);
61 
62         final int stateIndex = result.indexOf("mState", startIndex);
63         assertWithMessage(errorMsg).that(startIndex).isLessThan(stateIndex);
64         assertWithMessage(errorMsg).that(stateIndex).isLessThan(endIndex);
65 
66         final int colonIndex = result.indexOf(':', stateIndex);
67         assertWithMessage(errorMsg).that(stateIndex).isLessThan(colonIndex);
68         assertWithMessage(errorMsg).that(colonIndex).isLessThan(endIndex);
69 
70         final int endLineIndex = result.indexOf('\n', colonIndex);
71         assertWithMessage(errorMsg).that(colonIndex).isLessThan(endLineIndex);
72         assertWithMessage(errorMsg).that(endLineIndex).isLessThan(endIndex);
73 
74         return result.substring(colonIndex + 2, endLineIndex);
75     }
76 }
77