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

Use native "channel"-like entity in ScrollTests #1563

Open
wants to merge 3 commits into
base: jb-main
Choose a base branch
from
Open
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
@@ -0,0 +1,120 @@
/*
* 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.input

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.OnCanvasTests
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionOnScreen
import androidx.compose.ui.native.AsyncValue
import androidx.compose.ui.native.JsFloatBoxValue
import androidx.compose.ui.native.await
import androidx.compose.ui.native.boxedValue
import androidx.compose.ui.native.createAsyncValue
import androidx.compose.ui.unit.dp
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.coroutines.test.runTest
import org.w3c.dom.events.WheelEvent
import org.w3c.dom.events.WheelEventInit

class ScrollTests : OnCanvasTests {
@Test
fun scrollTillEnd() = runTest {
val firstRowPositionChannel = createAsyncValue<AsyncValue<JsFloatBoxValue>>()
val lastRowPositionChannel = createAsyncValue<AsyncValue<JsFloatBoxValue>>()

createComposeWindow {
Column(
modifier = Modifier.fillMaxWidth().height(300.dp).wrapContentSize(Alignment.Center)
.verticalScroll(
rememberScrollState()
)
) {
Box(
modifier = Modifier.height(100.dp).fillMaxWidth().background(Color(237, 40, 57))
.onGloballyPositioned { coordinates ->
val screenPosition = coordinates.positionOnScreen()
println("FIRST ROW ${screenPosition} \n")
if (screenPosition == Offset(0f, -200f)) {
firstRowPositionChannel.resolve(boxedValue(screenPosition.y))
}
}
)
Box(
modifier = Modifier.height(100.dp).fillMaxWidth().background(Color(255, 88, 0))
)
Box(
modifier = Modifier.height(100.dp).fillMaxWidth().background(Color(255, 211, 0))
)
Box(
modifier = Modifier.height(100.dp).fillMaxWidth().background(Color(0, 173, 131))
)
Box(
modifier = Modifier.height(100.dp).fillMaxWidth()
.background(Color(190, 219, 237))
)
Box(
modifier = Modifier.height(100.dp).fillMaxWidth()
.background(Color(31, 117, 254))
)
Box(
modifier = Modifier.height(100.dp).fillMaxWidth().background(Color(143, 0, 255)).onGloballyPositioned { coordinates ->
val screenPosition = coordinates.positionOnScreen()
println("LAST ROW ${screenPosition} \n")
if (screenPosition == Offset(0f, 1000f)) {
lastRowPositionChannel.resolve(boxedValue(screenPosition.y))
}
}
)
}
}

dispatchEvents(createWheelEvent(clientX = 100, clientY = 100, deltaX = 0.0, deltaY = 200.0))

assertEquals(-200.0f, firstRowPositionChannel.await().value)
assertEquals(1000.0f, lastRowPositionChannel.await().value)
}
}


private fun createWheelEvent(
deltaX: Double,
deltaY: Double,
clientX: Int,
clientY: Int
): WheelEvent {
return WheelEvent(
"wheel", WheelEventInit(
deltaX = deltaX,
deltaY = deltaY,
clientX = clientX,
clientY = clientY
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.native

import kotlin.js.Promise
import kotlinx.coroutines.await

internal external interface JsFloatBoxValue : JsAny {
val value: Float
}

internal external interface AsyncValue<T : JsAny> : JsAny {
val promise: Promise<T>
fun resolve(value: T)
}

internal suspend fun <T : JsAny> AsyncValue<T>.await() = promise.await<T>()

internal fun <T : JsAny> createAsyncValue(): T = js("(new class { constructor() { this.promise = new Promise((resolve, reject)=> { this.reject = reject; this.resolve = resolve} )}})")
internal fun boxedValue(value: Float): JsFloatBoxValue = js("({value: value})")
2 changes: 1 addition & 1 deletion mpp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ tasks.register("checkDesktop") {
tasks.register("testWeb") {
dependsOn(":compose:runtime:runtime:jsTest")
dependsOn(":compose:runtime:runtime:wasmJsTest")
dependsOn(":compose:ui:ui:compileTestKotlinJs")
//dependsOn(":compose:ui:ui:compileTestKotlinJs")
// TODO: ideally we want to run all wasm tests that are possible but now we deal only with modules that have skikoTests

dependsOn(":compose:foundation:foundation:wasmJsBrowserTest")
Expand Down