Fix "rotation" action is not shown problem and fix the problems of IndividualPickerActivityTest.

- Avoids the "rotation" action is not shown correctly when onCategoryLoaded() is called after onBottomActionBarReady().
- [Test case] Change the behavior of clicking the tile.
- [Test case] Remove the tests for rotation tile UI.
- [Test case] Change the trigger point of the daily rotation from the rotation tile to the rotation action.
- [Test case] Fix the RuntimeException below for testShowStartRotationDialog_WifiOnly_ClickOK_Fails_ShowsErrorDialog(), testStartRotation_WifiOnly_FailOnce_Retry(), and testStartRotation_WifiOnly_FailOnce_Retry_ShouldStillHaveWifiTurnedOff().

java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not request layout for 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus.

Bug: 157259520
Test: adb shell am instrument -w -r    -e debug false -e class 'com.android.wallpaper.picker.individual.IndividualPickerActivityTest' com.google.android.apps.wallpaper.tests/androidx.test.runner.AndroidJUnitRunner
Change-Id: I14cf96e0a4a8b6ffef01b932d6cc4d29e267da7d
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index 6da3372..2c57496 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -363,6 +363,12 @@
 
     protected void onCategoryLoaded() {
         mWallpaperRotationInitializer = mCategory.getWallpaperRotationInitializer();
+        // Avoids the "rotation" action is not shown correctly
+        // in a rare case : onCategoryLoaded() is called after onBottomActionBarReady().
+        if (isRotationEnabled() && mBottomActionBar != null
+                && !mBottomActionBar.areActionsShown(ROTATION)) {
+            mBottomActionBar.showActions(ROTATION);
+        }
         fetchWallpapers(false);
 
         if (mCategory.supportsThirdParty()) {
diff --git a/src/com/android/wallpaper/widget/BottomActionBar.java b/src/com/android/wallpaper/widget/BottomActionBar.java
index b0b9ec6..078aeba 100644
--- a/src/com/android/wallpaper/widget/BottomActionBar.java
+++ b/src/com/android/wallpaper/widget/BottomActionBar.java
@@ -260,6 +260,20 @@
     }
 
     /**
+     * Checks if the specific actions are shown.
+     *
+     * @param actions the specific actions to be verified
+     * @return {@code true} if the actions are shown; {@code false} otherwise
+     */
+    public boolean areActionsShown(BottomAction... actions) {
+        final Set<BottomAction> actionsSet = new HashSet<>(Arrays.asList(actions));
+        return actionsSet.stream().allMatch(bottomAction -> {
+            View view = mActionMap.get(bottomAction);
+            return view != null && view.getVisibility() == VISIBLE;
+        });
+    }
+
+    /**
      * All actions will be hidden.
      */
     public void hideAllActions() {
diff --git a/tests/src/com/android/wallpaper/picker/individual/IndividualPickerActivityTest.java b/tests/src/com/android/wallpaper/picker/individual/IndividualPickerActivityTest.java
index 2a0fbc4..c9170bc 100644
--- a/tests/src/com/android/wallpaper/picker/individual/IndividualPickerActivityTest.java
+++ b/tests/src/com/android/wallpaper/picker/individual/IndividualPickerActivityTest.java
@@ -19,12 +19,9 @@
 import static androidx.test.espresso.action.ViewActions.click;
 import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
 import static androidx.test.espresso.assertion.ViewAssertions.matches;
-import static androidx.test.espresso.intent.Intents.intended;
 import static androidx.test.espresso.intent.Intents.intending;
-import static androidx.test.espresso.intent.matcher.ComponentNameMatchers.hasClassName;
 import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
-import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
-import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra;
+import static androidx.test.espresso.matcher.RootMatchers.isDialog;
 import static androidx.test.espresso.matcher.ViewMatchers.isChecked;
 import static androidx.test.espresso.matcher.ViewMatchers.isClickable;
 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
@@ -33,8 +30,6 @@
 
 import static junit.framework.TestCase.assertFalse;
 
-import static org.hamcrest.Matchers.allOf;
-import static org.hamcrest.Matchers.equalTo;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -45,8 +40,6 @@
 import android.app.WallpaperManager;
 import android.content.Context;
 import android.content.Intent;
-import android.graphics.drawable.ColorDrawable;
-import android.widget.FrameLayout;
 
 import androidx.recyclerview.widget.RecyclerView;
 import androidx.test.espresso.contrib.RecyclerViewActions;
@@ -57,7 +50,6 @@
 import androidx.test.rule.ActivityTestRule;
 
 import com.android.wallpaper.R;
-import com.android.wallpaper.config.Flags;
 import com.android.wallpaper.model.Category;
 import com.android.wallpaper.model.PickerIntentFactory;
 import com.android.wallpaper.model.WallpaperInfo;
@@ -69,7 +61,6 @@
 import com.android.wallpaper.testing.TestCategoryProvider;
 import com.android.wallpaper.testing.TestFormFactorChecker;
 import com.android.wallpaper.testing.TestInjector;
-import com.android.wallpaper.testing.TestUserEventLogger;
 import com.android.wallpaper.testing.TestWallpaperCategory;
 import com.android.wallpaper.testing.TestWallpaperInfo;
 import com.android.wallpaper.testing.TestWallpaperRotationInitializer;
@@ -185,7 +176,7 @@
     }
 
     @Test
-    public void testClickTile_Mobile_LaunchesPreviewActivityWithWallpaper() {
+    public void testClickTile_Mobile_showsBottomActions() {
         mTestFormFactorChecker.setFormFactor(FormFactorChecker.FORM_FACTOR_MOBILE);
 
         setActivityWithMockWallpapers(false /* isRotationEnabled */,
@@ -194,121 +185,19 @@
 
         onView(withId(R.id.wallpaper_grid)).perform(
                 RecyclerViewActions.actionOnItemAtPosition(0, click()));
-        intended(allOf(
-                hasComponent(hasClassName("com.android.wallpaper.picker.PreviewActivity")),
-                hasExtra(equalTo(EXTRA_WALLPAPER_INFO), equalTo(sWallpaperInfo1))));
 
-        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
-        TestUserEventLogger eventLogger = (TestUserEventLogger) mInjector.getUserEventLogger(
-                context);
-        assertEquals(1, eventLogger.getNumIndividualWallpaperSelectedEvents());
-        assertEquals(sWallpaperInfo1.getCollectionId(context), eventLogger.getLastCollectionId());
+        onView(withId(R.id.action_back)).check(matches(isDisplayed()));
+        onView(withId(R.id.action_information)).check(matches(isDisplayed()));
+        onView(withId(R.id.action_apply)).check(matches(isDisplayed()));
     }
 
-    /**
-     * Tests that the static daily rotation tile (with flag dynamicStartRotationTileEnabled=false)
-     * has a background of the light blue accent color and says "Tap to turn on".
-     */
     @Test
-    public void testRotationEnabled_StaticDailyRotationTile() {
-        Flags.dynamicStartRotationTileEnabled = false;
+    public void testClickDailyRefreshAction_ShowsStartRotationDialog() {
         setActivityWithMockWallpapers(true /* isRotationEnabled */,
                 WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED);
         getActivity();
 
-        onView(withText(R.string.daily_refresh_tile_title)).check(matches(isDisplayed()));
-        onView(withText(R.string.daily_refresh_tile_subtitle)).check(matches(isDisplayed()));
-
-        // Check that the background color of the "daily refresh" tile is the blue accent color.
-        FrameLayout rotationTile = getActivity().findViewById(R.id.daily_refresh);
-        int backgroundColor = ((ColorDrawable) rotationTile.getBackground()).getColor();
-        assertEquals(getActivity().getResources().getColor(R.color.accent_color),
-                backgroundColor);
-    }
-
-    /**
-     * Tests that when rotation is enabled and the rotation for this category is already in effect
-     * on both home & lock screens, then the "start rotation" tile reads "Home & Lock".
-     */
-    @Test
-    public void testRotationEnabled_RotationInitializedHomeAndLock() {
-        Flags.dynamicStartRotationTileEnabled = true;
-
-        setActivityWithMockWallpapers(true /* isRotationEnabled */,
-                WallpaperRotationInitializer.ROTATION_HOME_AND_LOCK);
-        getActivity();
-
-        onView(withText(R.string.daily_refresh_tile_title)).check(matches(isDisplayed()));
-        onView(withText(R.string.home_and_lock_short_label)).check(matches(isDisplayed()));
-    }
-
-    /**
-     * Tests that when rotation is enabled and the rotation is aleady for this category is already
-     * in
-     * effect on the home-screen only, then the "start rotation" tile reads "Home screen".
-     */
-    @Test
-    public void testRotationEnabled_RotationInitializedHomeScreenOnly() {
-        Flags.dynamicStartRotationTileEnabled = true;
-
-        setActivityWithMockWallpapers(true /* isRotationEnabled */,
-                WallpaperRotationInitializer.ROTATION_HOME_ONLY);
-        getActivity();
-
-        onView(withText(R.string.daily_refresh_tile_title)).check(matches(isDisplayed()));
-        onView(withText(R.string.home_screen_message)).check(matches(isDisplayed()));
-    }
-
-    /**
-     * Tests that after the IndividualPickerActivity loads, if the state of the current category's
-     * rotation changes while the activity is restarted, then the UI for the "start rotation" tile
-     * changes to reflect that new state.
-     */
-    @Test
-    public void testActivityRestarted_RotationStateChanges_StartRotationTileUpdates()
-            throws Throwable {
-        Flags.dynamicStartRotationTileEnabled = true;
-
-        setActivityWithMockWallpapers(true /* isRotationEnabled */,
-                WallpaperRotationInitializer.ROTATION_HOME_ONLY);
-
-        onView(withText(R.string.daily_refresh_tile_title)).check(matches(isDisplayed()));
-        onView(withText(R.string.home_screen_message)).check(matches(isDisplayed()));
-
-        // Now change the rotation initialization state such that the tile should say
-        // "Tap to turn on" after the activity resumes.
-        setUpFragmentForTesting();
-        TestWallpaperRotationInitializer
-                testWPRotationInitializer = (TestWallpaperRotationInitializer)
-                mTestCategory.getWallpaperRotationInitializer();
-        testWPRotationInitializer.setRotationInitializationState(
-                WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED);
-
-        // Restart the activity.
-        IndividualPickerActivity activity = getActivity();
-        mActivityRule.runOnUiThread(activity::recreate);
-
-        onView(withText(R.string.daily_refresh_tile_title)).check(matches(isDisplayed()));
-        onView(withText(R.string.daily_refresh_tile_subtitle)).check(matches(isDisplayed()));
-    }
-
-    @Test
-    public void testRotationDisabled_DoesNotRenderDailyRefreshTile() {
-        setActivityWithMockWallpapers(false /* isRotationEnabled */,
-                WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED);
-        getActivity();
-
-        onView(withText(R.string.daily_refresh_tile_title)).check(doesNotExist());
-        onView(withText(R.string.daily_refresh_tile_subtitle)).check(doesNotExist());
-    }
-
-    @Test
-    public void testClickDailyRefreshTile_ShowsStartRotationDialog() {
-        setActivityWithMockWallpapers(true /* isRotationEnabled */,
-                WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED);
-        getActivity();
-
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
 
         onView(withId(R.id.start_rotation_wifi_only_checkbox))
                 .check(matches(isDisplayed()));
@@ -323,7 +212,7 @@
                 WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED);
         getActivity();
 
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
 
         onView(withText(R.string.start_rotation_dialog_body)).check(matches(isDisplayed()));
         onView(withText(android.R.string.cancel)).check(matches(isDisplayed()));
@@ -354,7 +243,7 @@
         Matcher<Intent> expectedIntent = hasAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
         intending(expectedIntent).respondWith(new ActivityResult(Activity.RESULT_OK, null));
 
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
 
         onView(withText(android.R.string.ok)).perform(click());
         mActivityRule.runOnUiThread(() -> {
@@ -379,15 +268,17 @@
                 mTestCategory.getWallpaperRotationInitializer();
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
         onView(withText(android.R.string.ok)).perform(click());
 
         testWPRotationInitializer.finishDownloadingFirstWallpaper(false /* isSuccessful */);
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
         // Error dialog should be shown with retry option.
-        onView(withText(R.string.start_rotation_error_message)).check(matches(isDisplayed()));
-        onView(withText(R.string.try_again)).check(matches(isDisplayed()));
+        onView(withText(R.string.start_rotation_error_message))
+                .inRoot(isDialog()).check(matches(isDisplayed()));
+        onView(withText(R.string.try_again))
+                .inRoot(isDialog()).check(matches(isDisplayed()));
     }
 
     @Test
@@ -402,14 +293,14 @@
                 mTestCategory.getWallpaperRotationInitializer();
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
         onView(withText(android.R.string.ok)).perform(click());
 
         testWPRotationInitializer.finishDownloadingFirstWallpaper(false /* isSuccessful */);
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
         // Click try again to retry.
-        onView(withText(R.string.try_again)).perform(click());
+        onView(withText(R.string.try_again)).inRoot(isDialog()).perform(click());
 
         mActivityRule.runOnUiThread(() -> {
             testWPRotationInitializer.finishDownloadingFirstWallpaper(true /* isSuccessful */);
@@ -431,7 +322,7 @@
                 mTestCategory.getWallpaperRotationInitializer();
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
         // Click on WiFi-only option to toggle it off.
         onView(withId(R.id.start_rotation_wifi_only_checkbox)).perform(click());
         onView(withText(android.R.string.ok)).perform(click());
@@ -456,7 +347,7 @@
                 mTestCategory.getWallpaperRotationInitializer();
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
         // Click on WiFi-only option to toggle it off.
         onView(withId(R.id.start_rotation_wifi_only_checkbox)).perform(click());
         onView(withText(android.R.string.ok)).perform(click());
@@ -465,7 +356,7 @@
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
         // Click try again to retry.
-        onView(withText(R.string.try_again)).perform(click());
+        onView(withText(R.string.try_again)).inRoot(isDialog()).perform(click());
 
         mActivityRule.runOnUiThread(() -> {
             testWPRotationInitializer.finishDownloadingFirstWallpaper(true /* isSuccessful */);
@@ -486,7 +377,7 @@
                 mTestCategory.getWallpaperRotationInitializer();
         assertFalse(testWPRotationInitializer.isRotationInitialized());
 
-        onView(withId(R.id.daily_refresh)).perform(click());
+        onView(withId(R.id.action_rotation)).perform(click());
         onView(withId(R.id.start_rotation_wifi_only_checkbox)).check(matches(isDisplayed()));
         // WiFi-only option should be checked by default.
         onView(withId(R.id.start_rotation_wifi_only_checkbox)).check(matches(isChecked()));