Merge "We should prevent starting home activity before setup" into qt-dev
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index 7e706ad..da87b2f 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -5199,6 +5199,11 @@
             awakenDreams();
         }
 
+        if (!isUserSetupComplete()) {
+            Slog.i(TAG, "Not going home because user setup is in progress.");
+            return;
+        }
+
         // Start dock.
         Intent dock = createHomeDockIntent();
         if (dock != null) {
diff --git a/services/tests/wmtests/src/com/android/server/policy/PhoneWindowManagerTests.java b/services/tests/wmtests/src/com/android/server/policy/PhoneWindowManagerTests.java
new file mode 100644
index 0000000..fc24f5e
--- /dev/null
+++ b/services/tests/wmtests/src/com/android/server/policy/PhoneWindowManagerTests.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2019 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 com.android.server.policy;
+
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.never;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.reset;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+
+import android.app.ActivityManager;
+
+import androidx.test.filters.SmallTest;
+
+import com.android.server.wm.ActivityTaskManagerInternal;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@link PhoneWindowManager}.
+ *
+ * Build/Install/Run:
+ *  atest WmTests:PhoneWindowManagerTests
+ */
+@SmallTest
+public class PhoneWindowManagerTests {
+
+    PhoneWindowManager mPhoneWindowManager;
+
+    @Before
+    public void setUp() {
+        mPhoneWindowManager = spy(new PhoneWindowManager());
+        spyOn(ActivityManager.getService());
+    }
+
+    @After
+    public void tearDown() {
+        reset(ActivityManager.getService());
+    }
+
+    @Test
+    public void testShouldNotStartDockOrHomeWhenSetup() throws Exception {
+        mockStartDockOrHome();
+        doReturn(false).when(mPhoneWindowManager).isUserSetupComplete();
+
+        mPhoneWindowManager.startDockOrHome(
+                0 /* displayId */, false /* fromHomeKey */, false /* awakenFromDreams */);
+
+        verify(mPhoneWindowManager, never()).createHomeDockIntent();
+    }
+
+    @Test
+    public void testShouldStartDockOrHomeAfterSetup() throws Exception {
+        mockStartDockOrHome();
+        doReturn(true).when(mPhoneWindowManager).isUserSetupComplete();
+
+        mPhoneWindowManager.startDockOrHome(
+                0 /* displayId */, false /* fromHomeKey */, false /* awakenFromDreams */);
+
+        verify(mPhoneWindowManager).createHomeDockIntent();
+    }
+
+    private void mockStartDockOrHome() throws Exception {
+        doNothing().when(ActivityManager.getService()).stopAppSwitches();
+        ActivityTaskManagerInternal mMockActivityTaskManagerInternal =
+                mock(ActivityTaskManagerInternal.class);
+        when(mMockActivityTaskManagerInternal.startHomeOnDisplay(
+                anyInt(), anyString(), anyInt(), anyBoolean(), anyBoolean())).thenReturn(false);
+        mPhoneWindowManager.mActivityTaskManagerInternal = mMockActivityTaskManagerInternal;
+    }
+}