Merge "Unifying various tracing calls" into ub-launcher3-master
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/QuickstepTestInformationHandler.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/QuickstepTestInformationHandler.java
index daaa95b..ce533a6 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/QuickstepTestInformationHandler.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/QuickstepTestInformationHandler.java
@@ -4,7 +4,10 @@
 
 import android.content.Context;
 import android.os.Bundle;
+import android.util.Log;
 
+import com.android.launcher3.Launcher;
+import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.testing.TestInformationHandler;
 import com.android.launcher3.testing.TestProtocol;
 import com.android.launcher3.uioverrides.states.OverviewState;
@@ -109,6 +112,11 @@
 
     @Override
     protected boolean isLauncherInitialized() {
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE,
+                    "isLauncherInitialized.TouchInteractionService.isInitialized=" +
+                            TouchInteractionService.isInitialized());
+        }
         return super.isLauncherInitialized() && TouchInteractionService.isInitialized();
     }
 }
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java
index d4e59c2..556f481 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java
@@ -53,6 +53,8 @@
 import androidx.annotation.WorkerThread;
 
 import com.android.launcher3.BaseDraggingActivity;
+import com.android.launcher3.Launcher;
+import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.Utilities;
 import com.android.launcher3.allapps.DiscoveryBounce;
 import com.android.launcher3.config.FeatureFlags;
@@ -131,6 +133,9 @@
                     .setProxy(proxy));
             MAIN_EXECUTOR.execute(TouchInteractionService.this::initInputMonitor);
             MAIN_EXECUTOR.execute(() -> preloadOverview(true /* fromInit */));
+            if (TestProtocol.sDebugTracing) {
+                Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE, "TIS initialized");
+            }
             sIsInitialized = true;
         }
 
@@ -380,6 +385,9 @@
 
     @Override
     public void onDestroy() {
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE, "TIS destroyed");
+        }
         sIsInitialized = false;
         if (mDeviceState.isUserUnlocked()) {
             mInputConsumer.unregisterInputConsumer();
diff --git a/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java b/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java
index 885fdbf..41f4a82 100644
--- a/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java
+++ b/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java
@@ -17,6 +17,7 @@
 package com.android.quickstep;
 
 import static com.android.launcher3.ui.TaplTestsLauncher3.getAppPackageName;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -34,6 +35,7 @@
 import com.android.launcher3.tapl.AllApps;
 import com.android.launcher3.tapl.AllAppsFromOverview;
 import com.android.launcher3.tapl.Background;
+import com.android.launcher3.tapl.LauncherInstrumentation.NavigationModel;
 import com.android.launcher3.tapl.Overview;
 import com.android.launcher3.tapl.OverviewTask;
 import com.android.launcher3.tapl.TestHelpers;
@@ -210,16 +212,21 @@
     @PortraitLandscape
     public void testBackground() throws Exception {
         startAppFast(resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR));
+        final Background background = getAndAssertBackground();
+
+        assertNotNull("Background.switchToOverview() returned null", background.switchToOverview());
+        assertTrue("Launcher internal state didn't switch to Overview",
+                isInState(LauncherState.OVERVIEW));
+    }
+
+    private Background getAndAssertBackground() {
         final Background background = mLauncher.getBackground();
         assertNotNull("Launcher.getBackground() returned null", background);
         executeOnLauncher(launcher -> assertTrue(
                 "Launcher activity is the top activity; expecting another activity to be the top "
                         + "one",
                 isInBackground(launcher)));
-
-        assertNotNull("Background.switchToOverview() returned null", background.switchToOverview());
-        assertTrue("Launcher internal state didn't switch to Overview",
-                isInState(LauncherState.OVERVIEW));
+        return background;
     }
 
     @Test
@@ -237,4 +244,47 @@
         assertTrue("Launcher internal state is not Home", isInState(LauncherState.NORMAL));
         assertNotNull("getHome returned null", mLauncher.getWorkspace());
     }
+
+    @Test
+    @NavigationModeSwitch
+    @PortraitLandscape
+    public void testQuickSwitchFromApp() throws Exception {
+        startAppFast(getAppPackageName());
+        startTestActivity(2);
+        String calculatorPackage = resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR);
+        startAppFast(calculatorPackage);
+
+        Background background = getAndAssertBackground();
+        background.quickSwitchToPreviousApp();
+        assertTrue("The first app we should have quick switched to is not running",
+                isTestActivityRunning("TestActivity2"));
+
+        background = getAndAssertBackground();
+        background.quickSwitchToPreviousApp();
+        if (mLauncher.getNavigationModel() == NavigationModel.THREE_BUTTON) {
+            // 3-button mode toggles between 2 apps, rather than going back further.
+            assertTrue("Second quick switch should have returned to the first app.",
+                    mDevice.wait(Until.hasObject(By.pkg(calculatorPackage)), DEFAULT_UI_TIMEOUT));
+        } else {
+            assertTrue("The second app we should have quick switched to is not running",
+                    isTestActivityRunning("Test Pin Item"));
+        }
+        getAndAssertBackground();
+    }
+
+    private boolean isTestActivityRunning(String activityLabel) {
+        return mDevice.wait(Until.hasObject(By.pkg(getAppPackageName()).text(activityLabel)),
+                DEFAULT_UI_TIMEOUT);
+    }
+
+    @Test
+    @NavigationModeSwitch
+    @PortraitLandscape
+    public void testQuickSwitchFromHome() throws Exception {
+        startTestActivity(2);
+        mLauncher.pressHome().quickSwitchToPreviousApp();
+        assertTrue("The most recent task is not running after quick switching from home",
+                isTestActivityRunning("TestActivity2"));
+        getAndAssertBackground();
+    }
 }
diff --git a/quickstep/tests/src/com/android/quickstep/ViewInflationDuringSwipeUp.java b/quickstep/tests/src/com/android/quickstep/ViewInflationDuringSwipeUp.java
index 8b7c7b7..f8f22a1 100644
--- a/quickstep/tests/src/com/android/quickstep/ViewInflationDuringSwipeUp.java
+++ b/quickstep/tests/src/com/android/quickstep/ViewInflationDuringSwipeUp.java
@@ -42,6 +42,7 @@
 import android.widget.RemoteViews;
 
 import androidx.test.filters.LargeTest;
+import androidx.test.filters.Suppress;
 import androidx.test.runner.AndroidJUnit4;
 import androidx.test.uiautomator.By;
 import androidx.test.uiautomator.UiDevice;
@@ -77,7 +78,10 @@
  *        directly (ex: new LinearLayout)
  *    Using ExtendedMockito: Mocking static methods from platform classes (loaded in zygote) makes
  *        the main thread extremely slow and untestable
+ *
+ * Suppressed until b/141579810 is resolved
  */
+@Suppress
 @LargeTest
 @RunWith(AndroidJUnit4.class)
 public class ViewInflationDuringSwipeUp extends AbstractQuickStepTest {
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index bea25e0..b22d137 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -53,6 +53,7 @@
 import com.android.launcher3.pm.InstallSessionTracker;
 import com.android.launcher3.pm.PackageInstallInfo;
 import com.android.launcher3.shortcuts.DeepShortcutManager;
+import com.android.launcher3.testing.TestProtocol;
 import com.android.launcher3.util.IntSparseArrayMap;
 import com.android.launcher3.util.ItemInfoMatcher;
 import com.android.launcher3.util.PackageUserKey;
@@ -92,6 +93,10 @@
     private boolean mModelLoaded;
     public boolean isModelLoaded() {
         synchronized (mLock) {
+            if (TestProtocol.sDebugTracing) {
+                Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE,
+                        "isModelLoaded: " + mModelLoaded + ", " + mLoaderTask);
+            }
             return mModelLoaded && mLoaderTask == null;
         }
     }
diff --git a/src/com/android/launcher3/testing/TestInformationHandler.java b/src/com/android/launcher3/testing/TestInformationHandler.java
index 8296cf9..d0e648f 100644
--- a/src/com/android/launcher3/testing/TestInformationHandler.java
+++ b/src/com/android/launcher3/testing/TestInformationHandler.java
@@ -24,6 +24,7 @@
 import android.graphics.Color;
 import android.os.Bundle;
 import android.os.Debug;
+import android.util.Log;
 import android.view.View;
 
 import com.android.launcher3.DeviceProfile;
@@ -176,6 +177,11 @@
     }
 
     protected boolean isLauncherInitialized() {
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE,
+                    "isLauncherInitialized " + Launcher.ACTIVITY_TRACKER.getCreatedActivity() + ", "
+                            + LauncherAppState.getInstance(mContext).getModel().isModelLoaded());
+        }
         return Launcher.ACTIVITY_TRACKER.getCreatedActivity() == null
                 || LauncherAppState.getInstance(mContext).getModel().isModelLoaded();
     }
diff --git a/src/com/android/launcher3/testing/TestProtocol.java b/src/com/android/launcher3/testing/TestProtocol.java
index 5110977..faf0ff6 100644
--- a/src/com/android/launcher3/testing/TestProtocol.java
+++ b/src/com/android/launcher3/testing/TestProtocol.java
@@ -85,4 +85,5 @@
     public static final String NO_DRAG_TO_WORKSPACE = "b/138729456";
     public static final String APP_NOT_DISABLED = "b/139891609";
     public static final String NO_CONTEXT_MENU = "b/141770616";
+    public static final String LAUNCHER_DIDNT_INITIALIZE = "b/142514365";
 }
diff --git a/src/com/android/launcher3/util/ActivityTracker.java b/src/com/android/launcher3/util/ActivityTracker.java
index e85a4e8..b4f361f 100644
--- a/src/com/android/launcher3/util/ActivityTracker.java
+++ b/src/com/android/launcher3/util/ActivityTracker.java
@@ -20,10 +20,12 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.util.Log;
 
 import androidx.annotation.Nullable;
 
 import com.android.launcher3.BaseActivity;
+import com.android.launcher3.testing.TestProtocol;
 
 import java.lang.ref.WeakReference;
 
@@ -43,7 +45,13 @@
     }
 
     public void onActivityDestroyed(T activity) {
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE, "onActivityDestroyed");
+        }
         if (mCurrentActivity.get() == activity) {
+            if (TestProtocol.sDebugTracing) {
+                Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE, "onActivityDestroyed: clear");
+            }
             mCurrentActivity.clear();
         }
     }
@@ -89,6 +97,10 @@
     }
 
     public boolean handleCreate(T activity) {
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.LAUNCHER_DIDNT_INITIALIZE,
+                    "ActivityTracker.handleCreate " + mCurrentActivity.get() + " => " + activity);
+        }
         mCurrentActivity = new WeakReference<>(activity);
         return handleIntent(activity, activity.getIntent(), false, false);
     }
diff --git a/tests/src/com/android/launcher3/compat/PromiseIconUiTest.java b/tests/src/com/android/launcher3/compat/PromiseIconUiTest.java
index 7ef946d..efbd9c9 100644
--- a/tests/src/com/android/launcher3/compat/PromiseIconUiTest.java
+++ b/tests/src/com/android/launcher3/compat/PromiseIconUiTest.java
@@ -48,6 +48,7 @@
     public void setUp() throws Exception {
         super.setUp();
         mDevice.pressHome();
+        waitForLauncherCondition("Launcher didn't start", launcher -> launcher != null);
         waitForState("Launcher internal state didn't switch to Home", LauncherState.NORMAL);
         mSessionId = -1;
     }
diff --git a/tests/tapl/com/android/launcher3/tapl/Background.java b/tests/tapl/com/android/launcher3/tapl/Background.java
index 9e66740..8b5792c 100644
--- a/tests/tapl/com/android/launcher3/tapl/Background.java
+++ b/tests/tapl/com/android/launcher3/tapl/Background.java
@@ -23,6 +23,7 @@
 import android.view.MotionEvent;
 
 import androidx.annotation.NonNull;
+import androidx.test.uiautomator.UiObject2;
 
 import com.android.launcher3.testing.TestProtocol;
 
@@ -119,6 +120,46 @@
         }
     }
 
+    /**
+     * Swipes right or double presses the square button to switch to the previous app.
+     */
+    public Background quickSwitchToPreviousApp() {
+        try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
+                "want to quick switch to the previous app")) {
+            verifyActiveContainer();
+            quickSwitchToPreviousApp(getExpectedStateForQuickSwitch());
+            return new Background(mLauncher);
+        }
+    }
+
+    protected int getExpectedStateForQuickSwitch() {
+        return BACKGROUND_APP_STATE_ORDINAL;
+    }
+
+    protected void quickSwitchToPreviousApp(int expectedState) {
+        switch (mLauncher.getNavigationModel()) {
+            case ZERO_BUTTON:
+                // Fall through, zero button and two button modes behave the same.
+            case TWO_BUTTON: {
+                // Swipe from the bottom left to the bottom right of the screen.
+                final int startX = 0;
+                final int startY = getSwipeStartY();
+                final int endX = mLauncher.getDevice().getDisplayWidth();
+                final int endY = startY;
+                mLauncher.swipeToState(startX, startY, endX, endY, 20, expectedState);
+                break;
+            }
+
+            case THREE_BUTTON:
+                // Double press the recents button.
+                UiObject2 recentsButton = mLauncher.waitForSystemUiObject("recent_apps");
+                recentsButton.click();
+                mLauncher.getOverview();
+                recentsButton.click();
+                break;
+        }
+    }
+
     protected String getSwipeHeightRequestName() {
         return TestProtocol.REQUEST_BACKGROUND_TO_OVERVIEW_SWIPE_HEIGHT;
     }
diff --git a/tests/tapl/com/android/launcher3/tapl/Home.java b/tests/tapl/com/android/launcher3/tapl/Home.java
index cfc4374..e0fe933 100644
--- a/tests/tapl/com/android/launcher3/tapl/Home.java
+++ b/tests/tapl/com/android/launcher3/tapl/Home.java
@@ -17,6 +17,7 @@
 package com.android.launcher3.tapl;
 
 import static com.android.launcher3.testing.TestProtocol.OVERVIEW_STATE_ORDINAL;
+import static com.android.launcher3.testing.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
 
 import androidx.annotation.NonNull;
 
@@ -58,4 +59,9 @@
             }
         }
     }
+
+    @Override
+    protected int getExpectedStateForQuickSwitch() {
+        return QUICK_SWITCH_STATE_ORDINAL;
+    }
 }
\ No newline at end of file