1 /* 2 * Copyright (C) 2022 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.pixel.tests; 18 19 import android.os.SystemClock; 20 import android.support.test.uiautomator.By; 21 import android.support.test.uiautomator.Until; 22 23 import androidx.test.ext.junit.runners.AndroidJUnit4; 24 25 import org.junit.Assert; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 @RunWith(AndroidJUnit4.class) 31 public class AppLaunchRecentAppTest extends PixelAppCompatTestBase { 32 private static final int WAIT_FIFTEEN_SECONDS_IN_MS = 15000; 33 private static final long WAIT_ONE_SECOND_IN_MS = 1000; 34 private static final String CLEAR_ALL = "Clear all"; 35 private static final String NO_RECENT_ITEMS = "No recent items"; 36 37 @Override 38 @Before setUp()39 public void setUp() throws Exception { 40 super.setUp(); 41 getUiDevice().pressRecentApps(); 42 getUiDevice() 43 .waitForWindowUpdate( 44 getUiDevice().getLauncherPackageName(), WAIT_FIFTEEN_SECONDS_IN_MS); 45 getUiDevice().wait(Until.findObject(By.text(NO_RECENT_ITEMS)), WAIT_FIFTEEN_SECONDS_IN_MS); 46 if (!getUiDevice().hasObject(By.text(NO_RECENT_ITEMS))) { 47 int midY = getUiDevice().getDisplayHeight() / 2; 48 int startX = getUiDevice().getDisplayWidth() * 1 / 10; 49 int endX = getUiDevice().getDisplayWidth() * 9 / 10; 50 for (int i = 0; i < 20; i++) { 51 getUiDevice().swipe(startX, midY, endX, midY, (endX - startX) / 100); 52 getUiDevice().waitForIdle(); 53 if (getUiDevice().hasObject(By.text(CLEAR_ALL))) { 54 break; 55 } 56 } 57 if (getUiDevice().hasObject(By.text(CLEAR_ALL))) { 58 getUiDevice().findObject(By.text(CLEAR_ALL)).click(); 59 } 60 SystemClock.sleep(WAIT_ONE_SECOND_IN_MS); 61 } 62 getDeviceUtils().backToHome(getUiDevice().getLauncherPackageName()); 63 } 64 65 @Test testLaunchFromRecentApps()66 public void testLaunchFromRecentApps() throws Exception { 67 launchAndWaitAppOpen(WAIT_FIFTEEN_SECONDS_IN_MS); 68 69 getUiDevice().pressRecentApps(); 70 getUiDevice().wait(Until.hasObject(By.text("Screenshot")), WAIT_FIFTEEN_SECONDS_IN_MS); 71 getDeviceUtils().takeScreenshot(getPackage(), "press_recent_apps_1"); 72 Assert.assertTrue( 73 "3P app should be in background", getUiDevice().hasObject(By.text("Screenshot"))); 74 75 getUiDevice().pressRecentApps(); 76 getUiDevice() 77 .wait(Until.hasObject(By.pkg(getPackage()).depth(0)), WAIT_FIFTEEN_SECONDS_IN_MS); 78 getDeviceUtils().takeScreenshot(getPackage(), "press_recent_apps_2"); 79 Assert.assertTrue( 80 "3P app main page should be re-launched", 81 getUiDevice().hasObject(By.pkg(getPackage()).depth(0))); 82 } 83 } 84