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 android.app.cts.wallpapers;
18 
19 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
20 
21 import android.content.Context;
22 import android.server.wm.WindowManagerState;
23 import android.server.wm.WindowManagerStateHelper;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * Utility class to check the status of the wallpaper windows.
30  * Includes tool to handle the keyguard state to check both home and lock screen wallpapers.
31  */
32 public class WallpaperWindowsTestUtils {
33     private static Context sContext;
34 
setContext(Context context)35     public static void setContext(Context context) {
36         sContext = context;
37     }
38 
39     public static class WallpaperWindowsHelper {
40         private final WindowManagerStateHelper mWmState;
41         private List<WindowManagerState.WindowState> mWallpaperWindows = new ArrayList<>();
42         private List<String> mPackageNames = new ArrayList<>();
43 
WallpaperWindowsHelper(WindowManagerStateHelper windowManagerStateHelper)44         public WallpaperWindowsHelper(WindowManagerStateHelper windowManagerStateHelper) {
45             mWmState = windowManagerStateHelper;
46         }
47 
dumpWindows()48         public String dumpWindows() {
49             StringBuilder msgWindows = new StringBuilder();
50             mWallpaperWindows.forEach(w -> msgWindows.append(w.toLongString()).append(", "));
51             return "[" + msgWindows + "]";
52         }
53 
dumpPackages()54         public String dumpPackages() {
55             StringBuilder msgWindows = new StringBuilder();
56             mPackageNames.forEach(p -> msgWindows.append(p).append(", "));
57             return "[" + msgWindows + "]";
58         }
59 
60         /**
61          * Wait until the visibility of the window is the one expected,
62          * and return false if it does not happen within N iterations
63          */
waitForMatchingWindowVisibility(String name, boolean expectedVisibility)64         public boolean waitForMatchingWindowVisibility(String name,
65                 boolean expectedVisibility) {
66             return mWmState.waitFor(
67                     (wmState) -> checkMatchingWindowVisibility(name, expectedVisibility),
68                     "Visibility of " + name + " is not " + expectedVisibility);
69         }
70 
71         /**
72          * Wait until the packages of the wallpapers match exactly the expected ones,
73          * and return false if it does not happen within N iterations
74          */
waitForMatchingPackages(List<String> expected)75         public boolean waitForMatchingPackages(List<String> expected) {
76             return mWmState.waitFor(
77                     (wmState) -> checkMatchingPackages(expected),
78                     "Provided packages and observed ones do not match");
79         }
80 
checkMatchingWindowVisibility(String name, boolean expectedVisibility)81         private boolean checkMatchingWindowVisibility(String name, boolean expectedVisibility) {
82             updateWindows();
83             return mWallpaperWindows.stream().anyMatch(
84                     w -> w.getName().equals(name) && w.isSurfaceShown() == expectedVisibility);
85         }
86 
checkMatchingPackages(List<String> expected)87         private boolean checkMatchingPackages(List<String> expected) {
88             updateWindows();
89             if (expected.size() != mPackageNames.size()) {
90                 return false;
91             }
92             for (int i = 0; i < expected.size(); i++) {
93                 if (!expected.get(i).equals(mPackageNames.get(i))) {
94                     return false;
95                 }
96             }
97             return true;
98         }
99 
updateWindows()100         private void updateWindows() {
101             mWmState.waitForAppTransitionIdleOnDisplay(sContext.getDisplayId());
102             mWmState.computeState();
103             mWallpaperWindows = mWmState.getMatchingWindowType(TYPE_WALLPAPER);
104             mPackageNames = new ArrayList<>();
105             mWallpaperWindows.forEach(w -> mPackageNames.add(w.getPackageName()));
106         }
107     }
108 }
109