Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 09d5eda4 authored by Matt Sziklay's avatar Matt Sziklay Committed by Android (Google) Code Review
Browse files

Merge "Add tests for desktop mode unhandled drags." into main

parents 0b2828ce ffd138ea
Loading
Loading
Loading
Loading
+94 −1
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ package com.android.wm.shell.desktopmode

import android.app.ActivityManager.RecentTaskInfo
import android.app.ActivityManager.RunningTaskInfo
import android.app.ActivityOptions
import android.app.KeyguardManager
import android.app.PendingIntent
import android.app.WindowConfiguration.ACTIVITY_TYPE_HOME
import android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD
import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM
@@ -43,6 +45,7 @@ import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import android.testing.AndroidTestingRunner
import android.view.Display.DEFAULT_DISPLAY
import android.view.DragEvent
import android.view.Gravity
import android.view.SurfaceControl
import android.view.WindowManager
@@ -107,6 +110,7 @@ import com.android.wm.shell.transition.Transitions.ENABLE_SHELL_TRANSITIONS
import com.android.wm.shell.transition.Transitions.TransitionHandler
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import java.util.function.Consumer
import java.util.Optional
import junit.framework.Assert.assertFalse
import junit.framework.Assert.assertTrue
@@ -131,8 +135,8 @@ import org.mockito.Mockito.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.atLeastOnce
import org.mockito.kotlin.eq
import org.mockito.kotlin.capture
import org.mockito.kotlin.eq
import org.mockito.kotlin.whenever
import org.mockito.quality.Strictness

@@ -3082,6 +3086,95 @@ class DesktopTasksControllerTest : ShellTestCase() {
    assertThat(taskRepository.removeBoundsBeforeMaximize(task.taskId)).isNull()
  }


  @Test
  fun onUnhandledDrag_newFreeformIntent() {
    testOnUnhandledDrag(DesktopModeVisualIndicator.IndicatorType.TO_DESKTOP_INDICATOR,
      PointF(1200f, 700f),
      Rect(240, 700, 2160, 1900))
  }

  @Test
  fun onUnhandledDrag_newFreeformIntentSplitLeft() {
    testOnUnhandledDrag(DesktopModeVisualIndicator.IndicatorType.TO_SPLIT_LEFT_INDICATOR,
      PointF(50f, 700f),
      Rect(0, 0, 500, 1000))
  }

  @Test
  fun onUnhandledDrag_newFreeformIntentSplitRight() {
    testOnUnhandledDrag(DesktopModeVisualIndicator.IndicatorType.TO_SPLIT_RIGHT_INDICATOR,
      PointF(2500f, 700f),
      Rect(500, 0, 1000, 1000))
  }

  @Test
  fun onUnhandledDrag_newFullscreenIntent() {
    testOnUnhandledDrag(DesktopModeVisualIndicator.IndicatorType.TO_FULLSCREEN_INDICATOR,
      PointF(1200f, 50f),
      Rect())
  }

  /**
   * Assert that an unhandled drag event launches a PendingIntent with the
   * windowing mode and bounds we are expecting.
   */
  private fun testOnUnhandledDrag(
    indicatorType: DesktopModeVisualIndicator.IndicatorType,
    inputCoordinate: PointF,
    expectedBounds: Rect
  ) {
    setUpLandscapeDisplay()
    val task = setUpFreeformTask()
    markTaskVisible(task)
    task.isFocused = true
    val runningTasks = ArrayList<RunningTaskInfo>()
    runningTasks.add(task)
    val spyController = spy(controller)
    val mockPendingIntent = mock(PendingIntent::class.java)
    val mockDragEvent = mock(DragEvent::class.java)
    val mockCallback = mock(Consumer::class.java)
    val b = SurfaceControl.Builder()
    b.setName("test surface")
    val dragSurface = b.build()
    whenever(shellTaskOrganizer.runningTasks).thenReturn(runningTasks)
    whenever(mockDragEvent.dragSurface).thenReturn(dragSurface)
    whenever(mockDragEvent.x).thenReturn(inputCoordinate.x)
    whenever(mockDragEvent.y).thenReturn(inputCoordinate.y)
    whenever(multiInstanceHelper.supportsMultiInstanceSplit(anyOrNull())).thenReturn(true)
    whenever(spyController.getVisualIndicator()).thenReturn(desktopModeVisualIndicator)
    doReturn(indicatorType)
      .whenever(spyController).updateVisualIndicator(
        eq(task),
        anyOrNull(),
        anyOrNull(),
        anyOrNull(),
        eq(DesktopModeVisualIndicator.DragStartState.DRAGGED_INTENT)
      )

    spyController.onUnhandledDrag(
      mockPendingIntent,
      mockDragEvent,
      mockCallback as Consumer<Boolean>
    )
    val arg: ArgumentCaptor<WindowContainerTransaction> =
      ArgumentCaptor.forClass(WindowContainerTransaction::class.java)
    var expectedWindowingMode: Int
      if (indicatorType == DesktopModeVisualIndicator.IndicatorType.TO_FULLSCREEN_INDICATOR) {
        expectedWindowingMode = WINDOWING_MODE_FULLSCREEN
        // Fullscreen launches currently use default transitions
        verify(transitions).startTransition(any(), capture(arg), anyOrNull())
      } else {
        expectedWindowingMode = WINDOWING_MODE_FREEFORM
        // All other launches use a special handler.
        verify(dragAndDropTransitionHandler).handleDropEvent(capture(arg))
      }
    assertThat(ActivityOptions.fromBundle(arg.value.hierarchyOps[0].launchOptions)
      .launchWindowingMode).isEqualTo(expectedWindowingMode)
    assertThat(ActivityOptions.fromBundle(arg.value.hierarchyOps[0].launchOptions)
      .launchBounds).isEqualTo(expectedBounds)
  }

  private val desktopWallpaperIntent: Intent
    get() = Intent(context, DesktopWallpaperActivity::class.java)