Merge "Fix adding non-freeform tasks to repo." into main
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/freeform/FreeformTaskListener.java b/libs/WindowManager/Shell/src/com/android/wm/shell/freeform/FreeformTaskListener.java
index a80241e..f2bdcae 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/freeform/FreeformTaskListener.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/freeform/FreeformTaskListener.java
@@ -16,6 +16,8 @@
 
 package com.android.wm.shell.freeform;
 
+import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
+
 import static com.android.wm.shell.ShellTaskOrganizer.TASK_LISTENER_TYPE_FREEFORM;
 
 import android.app.ActivityManager.RunningTaskInfo;
@@ -151,6 +153,9 @@
 
     @Override
     public void onFocusTaskChanged(RunningTaskInfo taskInfo) {
+        if (taskInfo.getWindowingMode() != WINDOWING_MODE_FREEFORM) {
+            return;
+        }
         ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TASK_ORG,
                 "Freeform Task Focus Changed: #%d focused=%b",
                 taskInfo.taskId, taskInfo.isFocused);
diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/freeform/FreeformTaskListenerTests.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/freeform/FreeformTaskListenerTests.java
new file mode 100644
index 0000000..71eea4b
--- /dev/null
+++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/freeform/FreeformTaskListenerTests.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2020 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.wm.shell.freeform;
+
+import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
+import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
+
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
+
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.ActivityManager;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import com.android.dx.mockito.inline.extended.StaticMockitoSession;
+import com.android.wm.shell.ShellTaskOrganizer;
+import com.android.wm.shell.ShellTestCase;
+import com.android.wm.shell.TestRunningTaskInfoBuilder;
+import com.android.wm.shell.desktopmode.DesktopModeStatus;
+import com.android.wm.shell.desktopmode.DesktopModeTaskRepository;
+import com.android.wm.shell.sysui.ShellInit;
+import com.android.wm.shell.windowdecor.WindowDecorViewModel;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.quality.Strictness;
+
+import java.util.Optional;
+
+/**
+ * Tests for {@link FreeformTaskListener}
+ * Build/Install/Run:
+ * atest WMShellUnitTests:FreeformTaskListenerTests
+ */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public final class FreeformTaskListenerTests extends ShellTestCase {
+
+    @Mock
+    private ShellTaskOrganizer mTaskOrganizer;
+    @Mock
+    private ShellInit mShellInit;
+    @Mock
+    private WindowDecorViewModel mWindowDecorViewModel;
+    @Mock
+    private DesktopModeTaskRepository mDesktopModeTaskRepository;
+    private FreeformTaskListener mFreeformTaskListener;
+    private StaticMockitoSession mMockitoSession;
+
+    @Before
+    public void setup() {
+        mMockitoSession = mockitoSession().initMocks(this)
+                .strictness(Strictness.LENIENT).mockStatic(DesktopModeStatus.class).startMocking();
+        when(DesktopModeStatus.isEnabled()).thenReturn(true);
+        mFreeformTaskListener = new FreeformTaskListener(
+                mShellInit,
+                mTaskOrganizer,
+                Optional.of(mDesktopModeTaskRepository),
+                mWindowDecorViewModel);
+    }
+
+    @Test
+    public void testFocusTaskChanged_freeformTaskIsAddedToRepo() {
+        ActivityManager.RunningTaskInfo task = new TestRunningTaskInfoBuilder()
+                .setWindowingMode(WINDOWING_MODE_FREEFORM).build();
+        task.isFocused = true;
+
+        mFreeformTaskListener.onFocusTaskChanged(task);
+
+        verify(mDesktopModeTaskRepository).addOrMoveFreeformTaskToTop(task.taskId);
+    }
+
+    @Test
+    public void testFocusTaskChanged_fullscreenTaskIsNotAddedToRepo() {
+        ActivityManager.RunningTaskInfo fullscreenTask = new TestRunningTaskInfoBuilder()
+                .setWindowingMode(WINDOWING_MODE_FULLSCREEN).build();
+        fullscreenTask.isFocused = true;
+
+        mFreeformTaskListener.onFocusTaskChanged(fullscreenTask);
+
+        verify(mDesktopModeTaskRepository, never())
+                .addOrMoveFreeformTaskToTop(fullscreenTask.taskId);
+    }
+
+    @After
+    public void tearDown() {
+        mMockitoSession.finishMocking();
+    }
+}