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