Test for setSplashScreenTheme API

Test: atest CtsWindowManagerDeviceTestCases:SplashscreenTests#testOverrideSplashscreenTheme
Bug: 185109768
Change-Id: I0db3131639488193307f10f81927b1cb0d39a421
diff --git a/tests/framework/base/windowmanager/app/AndroidManifest.xml b/tests/framework/base/windowmanager/app/AndroidManifest.xml
index 942644d..d1d76c7 100755
--- a/tests/framework/base/windowmanager/app/AndroidManifest.xml
+++ b/tests/framework/base/windowmanager/app/AndroidManifest.xml
@@ -603,6 +603,9 @@
         <activity android:name=".SplashScreenReplaceIconActivity"
                   android:exported="true"
                   android:theme="@style/ReplaceIconTheme"/>
+        <activity android:name=".SplashScreenReplaceThemeActivity"
+                  android:exported="true"
+                  android:theme="@style/ReplaceIconTheme"/>
 
         <service android:name=".OverlayTestService"
                  android:exported="true" />
diff --git a/tests/framework/base/windowmanager/app/res/values/styles.xml b/tests/framework/base/windowmanager/app/res/values/styles.xml
index 974a736..a214998 100644
--- a/tests/framework/base/windowmanager/app/res/values/styles.xml
+++ b/tests/framework/base/windowmanager/app/res/values/styles.xml
@@ -86,4 +86,8 @@
         <item name="android:windowSplashScreenBrandingImage">@drawable/branding</item>
         <item name="android:windowSplashScreenIconBackgroundColor">@drawable/blue</item>
     </style>
+
+    <style name="SplashScreenOverrideTheme" parent="ReplaceIconTheme">
+        <item name="android:windowSplashScreenBackground">@drawable/red</item>
+    </style>
 </resources>
diff --git a/tests/framework/base/windowmanager/app/src/android/server/wm/app/Components.java b/tests/framework/base/windowmanager/app/src/android/server/wm/app/Components.java
index b8e3958..d9d51e1 100644
--- a/tests/framework/base/windowmanager/app/src/android/server/wm/app/Components.java
+++ b/tests/framework/base/windowmanager/app/src/android/server/wm/app/Components.java
@@ -236,6 +236,8 @@
             component("HandleSplashScreenExitActivity");
     public static final ComponentName SPLASH_SCREEN_REPLACE_ICON_ACTIVITY =
             component("SplashScreenReplaceIconActivity");
+    public static final ComponentName SPLASH_SCREEN_REPLACE_THEME_ACTIVITY =
+            component("SplashScreenReplaceThemeActivity");
 
     public static final ComponentName TEST_DREAM_SERVICE =
             component("TestDream");
@@ -286,6 +288,10 @@
         public static final String REQUEST_SET_NIGHT_MODE_ON_CREATE = "night_mode_onCreate";
         public static final String GET_NIGHT_MODE_ACTIVITY_CHANGED = "get_night_mode_activity";
         public static final String DELAY_RESUME = "delay_resume";
+        public static final String OVERRIDE_THEME_ENABLED = "override_theme_enabled";
+        public static final String OVERRIDE_THEME_COLOR = "override_theme_color";
+        public static final String OVERRIDE_THEME_COMPONENT = "override_theme_component";
+
     }
 
     /**
diff --git a/tests/framework/base/windowmanager/app/src/android/server/wm/app/SplashScreenReplaceThemeActivity.java b/tests/framework/base/windowmanager/app/src/android/server/wm/app/SplashScreenReplaceThemeActivity.java
new file mode 100644
index 0000000..052e8d7
--- /dev/null
+++ b/tests/framework/base/windowmanager/app/src/android/server/wm/app/SplashScreenReplaceThemeActivity.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.server.wm.app;
+
+import static android.server.wm.app.Components.TestStartingWindowKeys.OVERRIDE_THEME_COLOR;
+import static android.server.wm.app.Components.TestStartingWindowKeys.OVERRIDE_THEME_COMPONENT;
+import static android.server.wm.app.Components.TestStartingWindowKeys.OVERRIDE_THEME_ENABLED;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.drawable.ColorDrawable;
+import android.os.Bundle;
+import android.server.wm.TestJournalProvider;
+import android.window.SplashScreen;
+import android.window.SplashScreenView;
+
+public class SplashScreenReplaceThemeActivity extends Activity {
+    private final SplashScreen.OnExitAnimationListener mSplashScreenExitHandler =
+            this::onSplashScreenExit;
+    private boolean mOverrideTheme;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        getSplashScreen().setOnExitAnimationListener(mSplashScreenExitHandler);
+        mOverrideTheme = getIntent().getBooleanExtra(OVERRIDE_THEME_ENABLED, false);
+    }
+
+    @Override
+    protected void onStop() {
+        super.onStop();
+        if (mOverrideTheme) {
+            getSplashScreen().setSplashScreenTheme(R.style.SplashScreenOverrideTheme);
+        } else {
+            getSplashScreen().setSplashScreenTheme(Resources.ID_NULL);
+        }
+    }
+
+    private void onSplashScreenExit(SplashScreenView view) {
+        final Context baseContext = getBaseContext();
+        TestJournalProvider.putExtras(baseContext, OVERRIDE_THEME_COMPONENT,
+                bundle -> bundle.putInt(OVERRIDE_THEME_COLOR,
+                        ((ColorDrawable) view.getBackground()).getColor()));
+        view.remove();
+        finish();
+    }
+}
diff --git a/tests/framework/base/windowmanager/app/src/android/server/wm/app/TrampolineActivity.java b/tests/framework/base/windowmanager/app/src/android/server/wm/app/TrampolineActivity.java
index d7ca41d..7d602f05 100644
--- a/tests/framework/base/windowmanager/app/src/android/server/wm/app/TrampolineActivity.java
+++ b/tests/framework/base/windowmanager/app/src/android/server/wm/app/TrampolineActivity.java
@@ -35,7 +35,7 @@
         // activity is visible before it's launched, because its task is being brought
         // to foreground. We need to verify that 'am start' is unblocked correctly.
         SystemClock.sleep(2000);
-        Intent intent = new Intent(this, SingleTaskActivity.class);
+        Intent intent = new Intent(this,  SingleTaskActivity.class);
         intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_NEW_TASK);
 
         startActivity(intent);
diff --git a/tests/framework/base/windowmanager/src/android/server/wm/SplashscreenTests.java b/tests/framework/base/windowmanager/src/android/server/wm/SplashscreenTests.java
index 75e3296..93ee484 100644
--- a/tests/framework/base/windowmanager/src/android/server/wm/SplashscreenTests.java
+++ b/tests/framework/base/windowmanager/src/android/server/wm/SplashscreenTests.java
@@ -23,9 +23,11 @@
 import static android.server.wm.CliIntentExtra.extraBool;
 import static android.server.wm.CliIntentExtra.extraString;
 import static android.server.wm.WindowManagerState.STATE_RESUMED;
+import static android.server.wm.WindowManagerState.STATE_STOPPED;
 import static android.server.wm.app.Components.HANDLE_SPLASH_SCREEN_EXIT_ACTIVITY;
 import static android.server.wm.app.Components.SPLASHSCREEN_ACTIVITY;
 import static android.server.wm.app.Components.SPLASH_SCREEN_REPLACE_ICON_ACTIVITY;
+import static android.server.wm.app.Components.SPLASH_SCREEN_REPLACE_THEME_ACTIVITY;
 import static android.server.wm.app.Components.TestStartingWindowKeys.CANCEL_HANDLE_EXIT;
 import static android.server.wm.app.Components.TestStartingWindowKeys.CONTAINS_BRANDING_VIEW;
 import static android.server.wm.app.Components.TestStartingWindowKeys.CONTAINS_CENTER_VIEW;
@@ -35,6 +37,9 @@
 import static android.server.wm.app.Components.TestStartingWindowKeys.ICON_ANIMATION_DURATION;
 import static android.server.wm.app.Components.TestStartingWindowKeys.ICON_ANIMATION_START;
 import static android.server.wm.app.Components.TestStartingWindowKeys.ICON_BACKGROUND_COLOR;
+import static android.server.wm.app.Components.TestStartingWindowKeys.OVERRIDE_THEME_COLOR;
+import static android.server.wm.app.Components.TestStartingWindowKeys.OVERRIDE_THEME_COMPONENT;
+import static android.server.wm.app.Components.TestStartingWindowKeys.OVERRIDE_THEME_ENABLED;
 import static android.server.wm.app.Components.TestStartingWindowKeys.RECEIVE_SPLASH_SCREEN_EXIT;
 import static android.server.wm.app.Components.TestStartingWindowKeys.REPLACE_ICON_EXIT;
 import static android.server.wm.app.Components.TestStartingWindowKeys.REQUEST_HANDLE_EXIT_ON_CREATE;
@@ -281,4 +286,38 @@
             shortcutManager.removeDynamicShortcuts(Collections.singletonList(shortCutId));
         }
     }
+
+    @Test
+    public void testOverrideSplashscreenTheme() {
+        // Launch the activity a first time, check that the splashscreen use the default theme,
+        // and override the theme for the next launch
+        launchActivity(SPLASH_SCREEN_REPLACE_THEME_ACTIVITY,
+                extraBool(OVERRIDE_THEME_ENABLED, true));
+
+        mWmState.waitForActivityState(SPLASH_SCREEN_REPLACE_THEME_ACTIVITY, STATE_STOPPED);
+
+        TestJournalProvider.TestJournal journal = TestJournalProvider.TestJournalContainer.get(
+                OVERRIDE_THEME_COMPONENT);
+        assertEquals(Integer.toHexString(Color.BLUE),
+                Integer.toHexString(journal.extras.getInt(OVERRIDE_THEME_COLOR)));
+
+        // Launch the activity a second time, check that the theme has been overridden and reset
+        // to the default theme
+        launchActivity(SPLASH_SCREEN_REPLACE_THEME_ACTIVITY);
+
+        mWmState.waitForActivityState(SPLASH_SCREEN_REPLACE_THEME_ACTIVITY, STATE_STOPPED);
+
+        journal = TestJournalProvider.TestJournalContainer.get(OVERRIDE_THEME_COMPONENT);
+        assertEquals(Integer.toHexString(Color.RED),
+                Integer.toHexString(journal.extras.getInt(OVERRIDE_THEME_COLOR)));
+
+        // Launch the activity a third time just to check that the theme has indeed been reset.
+        launchActivity(SPLASH_SCREEN_REPLACE_THEME_ACTIVITY);
+
+        mWmState.waitForActivityState(SPLASH_SCREEN_REPLACE_THEME_ACTIVITY, STATE_STOPPED);
+
+        journal = TestJournalProvider.TestJournalContainer.get(OVERRIDE_THEME_COMPONENT);
+        assertEquals(Integer.toHexString(Color.BLUE),
+                Integer.toHexString(journal.extras.getInt(OVERRIDE_THEME_COLOR)));
+    }
 }