forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CfW: add a test for Text Baseline (#1314)
The issue JetBrains/compose-multiplatform#4078 was fixed earlier. Probably in skiko: JetBrains/skiko#846
- Loading branch information
Showing
3 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/TestHelpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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 | ||
|
||
import kotlin.math.abs | ||
import kotlinx.browser.document | ||
import org.w3c.dom.HTMLCanvasElement | ||
|
||
/** | ||
* An interface with helper functions to initialise the tests | ||
*/ | ||
internal interface OnCanvasTests { | ||
|
||
val canvasId: String | ||
get() = "canvas1" | ||
|
||
fun createCanvasAndAttach(id: String = canvasId): HTMLCanvasElement { | ||
val canvas = document.createElement("canvas") as HTMLCanvasElement | ||
canvas.setAttribute("id", canvasId) | ||
canvas.setAttribute("tabindex", "0") | ||
|
||
document.body!!.appendChild(canvas) | ||
return canvas | ||
} | ||
|
||
fun commonAfterTest() { | ||
document.getElementById(canvasId)?.remove() | ||
} | ||
|
||
fun assertApproximatelyEqual(expected: Float, actual: Float, tolerance: Float = 1f) { | ||
if (abs(expected - actual) > tolerance) { | ||
throw AssertionError("Expected $expected but got $actual. Difference is more than the allowed delta $tolerance") | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/TextTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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 | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.ui.layout.FirstBaseline | ||
import androidx.compose.ui.layout.onGloballyPositioned | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.window.CanvasBasedWindow | ||
import kotlin.test.AfterTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.test.runTest | ||
|
||
class TextTests : OnCanvasTests { | ||
|
||
@AfterTest | ||
fun cleanup() { | ||
commonAfterTest() | ||
} | ||
|
||
@Test | ||
// https://github.com/JetBrains/compose-multiplatform/issues/4078 | ||
fun baselineShouldBeNotZero() = runTest { | ||
val canvas = createCanvasAndAttach() | ||
|
||
val headingOnPositioned = Channel<Float>(10) | ||
val subtitleOnPositioned = Channel<Float>(10) | ||
CanvasBasedWindow( | ||
canvasElementId = canvasId, | ||
content = { | ||
val density = LocalDensity.current.density | ||
Row { | ||
Text( | ||
"Heading", | ||
modifier = Modifier.alignByBaseline() | ||
.onGloballyPositioned { | ||
headingOnPositioned.trySend(it[FirstBaseline] / density) | ||
}, | ||
style = MaterialTheme.typography.h4 | ||
) | ||
Text( | ||
" — Subtitle", | ||
modifier = Modifier.alignByBaseline() | ||
.onGloballyPositioned { | ||
subtitleOnPositioned.trySend(it[FirstBaseline] / density) | ||
}, | ||
style = MaterialTheme.typography.subtitle1 | ||
) | ||
} | ||
} | ||
) | ||
|
||
val headingAlignment = headingOnPositioned.receive() | ||
val subtitleAlignment = subtitleOnPositioned.receive() | ||
|
||
assertApproximatelyEqual(29f, headingAlignment) | ||
assertApproximatelyEqual(19f, subtitleAlignment) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters