1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.quickstep; 18 19 import static org.junit.Assert.assertTrue; 20 21 import android.os.SystemProperties; 22 23 import androidx.test.uiautomator.By; 24 import androidx.test.uiautomator.Until; 25 26 import com.android.launcher3.tapl.LaunchedAppState; 27 import com.android.launcher3.tapl.TestHelpers; 28 import com.android.launcher3.ui.AbstractLauncherUiTest; 29 import com.android.launcher3.uioverrides.QuickstepLauncher; 30 import com.android.launcher3.util.TestUtil; 31 import com.android.launcher3.util.Wait; 32 import com.android.quickstep.fallback.RecentsState; 33 import com.android.quickstep.fallback.window.RecentsWindowManager; 34 import com.android.quickstep.views.RecentsView; 35 36 import org.junit.rules.RuleChain; 37 import org.junit.rules.TestRule; 38 39 import java.util.function.Consumer; 40 import java.util.function.Function; 41 import java.util.function.Supplier; 42 43 /** 44 * Base class for all instrumentation tests that deal with Quickstep. 45 */ 46 public abstract class AbstractQuickStepTest extends AbstractLauncherUiTest<QuickstepLauncher> { 47 public static final boolean ENABLE_SHELL_TRANSITIONS = 48 SystemProperties.getBoolean("persist.wm.debug.shell_transit", true); 49 @Override getRulesInsideActivityMonitor()50 protected TestRule getRulesInsideActivityMonitor() { 51 return RuleChain. 52 outerRule(new NavigationModeSwitchRule(mLauncher)). 53 around(new TaskbarModeSwitchRule(mLauncher)). 54 around(super.getRulesInsideActivityMonitor()); 55 } 56 57 @Override onLauncherActivityClose(QuickstepLauncher launcher)58 protected void onLauncherActivityClose(QuickstepLauncher launcher) { 59 RecentsView recentsView = launcher.getOverviewPanel(); 60 if (recentsView != null) { 61 recentsView.finishRecentsAnimation(false /* toRecents */, null); 62 } 63 } 64 65 // Cannot be used in TaplTests between a Tapl call injecting a gesture and a tapl call 66 // expecting the results of that gesture because the wait can hide flakeness. waitForRecentsWindowState(String message, Supplier<RecentsState> state)67 protected void waitForRecentsWindowState(String message, Supplier<RecentsState> state) { 68 waitForRecentsWindowCondition(message, recentsWindow -> 69 recentsWindow.getStateManager().getCurrentStableState() == state.get()); 70 } 71 72 // Cannot be used in TaplTests after injecting any gesture using Tapl because this can hide 73 // flakiness. waitForRecentsWindowCondition(String message, Function<RecentsWindowManager, Boolean> condition)74 protected void waitForRecentsWindowCondition(String 75 message, Function<RecentsWindowManager, Boolean> condition) { 76 waitForRecentsWindowCondition(message, condition, TestUtil.DEFAULT_UI_TIMEOUT); 77 } 78 getFromRecentsWindow(Function<RecentsWindowManager, T> f)79 protected <T> T getFromRecentsWindow(Function<RecentsWindowManager, T> f) { 80 if (!TestHelpers.isInLauncherProcess()) return null; 81 return getOnUiThread(() -> { 82 RecentsWindowManager recentsWindowManager = 83 RecentsWindowManager.getRecentsWindowTracker().getCreatedContext(); 84 return recentsWindowManager != null ? f.apply(recentsWindowManager) : null; 85 }); 86 } 87 88 // Cannot be used in TaplTests after injecting any gesture using Tapl because this can hide 89 // flakiness. waitForRecentsWindowCondition( String message, Function<RecentsWindowManager, Boolean> condition, long timeout)90 protected void waitForRecentsWindowCondition( 91 String message, Function<RecentsWindowManager, Boolean> condition, long timeout) { 92 verifyKeyguardInvisible(); 93 if (!TestHelpers.isInLauncherProcess()) return; 94 Wait.atMost(message, () -> getFromRecentsWindow(condition), mLauncher, timeout); 95 } 96 isInRecentsWindowState(Supplier<RecentsState> state)97 protected boolean isInRecentsWindowState(Supplier<RecentsState> state) { 98 if (!TestHelpers.isInLauncherProcess()) return true; 99 return getFromRecentsWindow( 100 recentsWindow -> recentsWindow.getStateManager().getState() == state.get()); 101 } 102 assertTestActivityIsRunning(int activityNumber, String message)103 protected void assertTestActivityIsRunning(int activityNumber, String message) { 104 assertTrue(message, mDevice.wait( 105 Until.hasObject(By.pkg(getAppPackageName()).text("TestActivity" + activityNumber)), 106 TestUtil.DEFAULT_UI_TIMEOUT)); 107 } 108 getAndAssertLaunchedApp()109 protected LaunchedAppState getAndAssertLaunchedApp() { 110 final LaunchedAppState launchedAppState = mLauncher.getLaunchedAppState(); 111 executeOnLauncher(launcher -> assertTrue( 112 "Launcher activity is the top activity; expecting another activity to be the top " 113 + "one", 114 isInLaunchedApp(launcher))); 115 return launchedAppState; 116 } 117 } 118