Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for testing drag-and-drop in skiko #1639

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.draganddrop.DragAndDropTransferData
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeCanvas
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.node.RootForTest
import androidx.compose.ui.platform.InfiniteAnimationPolicy
import androidx.compose.ui.platform.PlatformContext
import androidx.compose.ui.platform.PlatformDragAndDropManager
import androidx.compose.ui.platform.PlatformDragAndDropSource
import androidx.compose.ui.platform.WindowInfo
import androidx.compose.ui.scene.ComposeScene
import androidx.compose.ui.scene.CanvasLayersComposeScene
Expand Down Expand Up @@ -446,6 +451,33 @@ class SkikoComposeUiTest @InternalTestApi constructor(
override fun updateState(oldValue: TextFieldValue?, newValue: TextFieldValue) = Unit
}

private inner class TestDragAndDropManager : PlatformDragAndDropManager {
override val isRequestDragAndDropTransferRequired: Boolean
get() = true

override fun requestDragAndDropTransfer(
source: PlatformDragAndDropSource,
offset: Offset
) {
var isTransferStarted = false
val startTransferScope = object : PlatformDragAndDropSource.StartTransferScope {
override fun startDragAndDropTransfer(
transferData: DragAndDropTransferData,
decorationSize: Size,
drawDragDecoration: DrawScope.() -> Unit
): Boolean {
isTransferStarted = true
return true
}
}
with(source) {
startTransferScope.startDragAndDropTransfer(offset) {
isTransferStarted
}
}
}
}

private inner class TestContext : PlatformContext by PlatformContext.Empty {
override val windowInfo: WindowInfo = TestWindowInfo()

Expand All @@ -456,6 +488,8 @@ class SkikoComposeUiTest @InternalTestApi constructor(

override val semanticsOwnerListener: PlatformContext.SemanticsOwnerListener?
get() = [email protected]

override val dragAndDropManager: PlatformDragAndDropManager = TestDragAndDropManager()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
* limitations under the License.
*/

package androidx.compose.ui
package androidx.compose.ui.draganddrop

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.draganddrop.dragAndDropTarget
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.draganddrop.DragAndDropEvent
import androidx.compose.ui.draganddrop.DragAndDropTarget
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.Window
Expand All @@ -43,9 +42,8 @@ import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import org.junit.Test

class DragAndDropTest {
class DesktopDragAndDropTest {

@OptIn(ExperimentalFoundationApi::class)
@Test
fun testDragAndDropTarget() = runApplicationTest {
lateinit var window: ComposeWindow
Expand Down Expand Up @@ -114,7 +112,6 @@ class DragAndDropTest {
* Tests that drag-target works in the presence of multiple compositions (ComposeScenes)
* attached to the same root component.
*/
@OptIn(ExperimentalFoundationApi::class)
@Test
fun dragAndDropTargetWorksAfterShowingPopup() = runApplicationTest {
lateinit var window: ComposeWindow
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2024 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 androidx.compose.ui.draganddrop

import androidx.compose.foundation.draganddrop.dragAndDropSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.dragAndDrop
import androidx.compose.ui.test.longClick
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performMouseInput
import androidx.compose.ui.test.performTouchInput
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.unit.dp
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@OptIn(ExperimentalTestApi::class)
class DragAndDropTest {
@Test
fun mouseDragTriggersDragStart() = runComposeUiTest {
var transferDataCreated = false
setContent {
Box(
Modifier
.testTag("dragSource")
.size(100.dp)
.dragAndDropSource(
transferData = {
transferDataCreated = true
null
}
)
)
}

assertFalse(transferDataCreated)

onNodeWithTag("dragSource").performMouseInput {
dragAndDrop(
start = Offset(1f, 1f),
end = Offset(99f, 99f),
)
}
assertTrue(transferDataCreated)
}

@Test
fun mouseLongPressDoesNotTriggerDragStart() = runComposeUiTest {
var transferDataCreated = false
setContent {
Box(
Modifier
.testTag("dragSource")
.size(100.dp)
.dragAndDropSource(
drawDragDecoration = {},
transferData = {
println("Triggered")
transferDataCreated = true
null
}
)
)
}

assertFalse(transferDataCreated)

onNodeWithTag("dragSource").performMouseInput {
longClick()
}
assertFalse(transferDataCreated)
}

@Test
fun touchLongPressTriggersDragStart() = runComposeUiTest {
var transferDataCreated = false
setContent {
Box(
Modifier
.testTag("dragSource")
.size(100.dp)
.dragAndDropSource(
transferData = {
transferDataCreated = true
null
}
)
)
}

assertFalse(transferDataCreated)

onNodeWithTag("dragSource").performTouchInput {
longClick()
}
assertTrue(transferDataCreated)
}

@Test
fun touchDragDoesNotTriggerDragStart() = runComposeUiTest {
var transferDataCreated = false
setContent {
Box(
Modifier
.testTag("dragSource")
.size(100.dp)
.dragAndDropSource(
drawDragDecoration = {},
transferData = {
println("Triggered")
transferDataCreated = true
null
}
)
)
}

assertFalse(transferDataCreated)

onNodeWithTag("dragSource").performTouchInput {
down(Offset(1f, 1f))
moveTo(Offset(99f, 99f))
up()
}
assertFalse(transferDataCreated)
}
}
Loading