Skip to content

Commit 25970ef

Browse files
committed
Update & fix tests
1 parent a11e407 commit 25970ef

File tree

7 files changed

+1964
-1167
lines changed

7 files changed

+1964
-1167
lines changed

compose/foundation/foundation/build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ if (AndroidXComposePlugin.isMultiplatformEnabled(project)) {
152152
dependsOn(jsWasmMain)
153153
}
154154

155-
commonTest {
156-
dependencies {
157-
implementation(kotlin("test"))
158-
}
159-
}
160-
161155
// TODO(b/214407011): These dependencies leak into instrumented tests as well. If you
162156
// need to add Robolectric (which must be kept out of androidAndroidTest), use a top
163157
// level dependencies block instead:

compose/foundation/foundation/src/desktopTest/kotlin/androidx/compose/foundation/text/selection/DesktopTextFieldSelectionManagerTest.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ class DesktopTextFieldSelectionManagerTest {
124124

125125
@Test
126126
fun TextFieldSelectionManager_mouseSelectionObserver_onStart() {
127-
manager.mouseSelectionObserver.onStart(dragBeginPosition, SelectionAdjustment.None)
127+
manager.mouseSelectionObserver.onStart(dragBeginPosition, SelectionAdjustment.None, 1)
128128

129129
assertThat(value.selection).isEqualTo(TextRange(0, 0))
130130

131131
manager.mouseSelectionObserver.onStart(
132132
dragBeginPosition + dragDistance,
133-
SelectionAdjustment.None
133+
SelectionAdjustment.None,
134+
1
134135
)
135136
assertThat(value.selection).isEqualTo(TextRange(8, 8))
136137
}
@@ -148,7 +149,7 @@ class DesktopTextFieldSelectionManagerTest {
148149
@Test
149150
fun TextFieldSelectionManager_mouseSelectionObserver_onDrag() {
150151
val observer = manager.mouseSelectionObserver
151-
observer.onStart(dragBeginPosition, SelectionAdjustment.None)
152+
observer.onStart(dragBeginPosition, SelectionAdjustment.None, 1)
152153
observer.onDrag(dragDistance, SelectionAdjustment.None)
153154

154155
assertThat(value.selection).isEqualTo(TextRange(0, 8))
@@ -157,7 +158,7 @@ class DesktopTextFieldSelectionManagerTest {
157158
@Test
158159
fun TextFieldSelectionManager_mouseSelectionObserver_copy() = runTest {
159160
val observer = manager.mouseSelectionObserver
160-
observer.onStart(dragBeginPosition, SelectionAdjustment.None)
161+
observer.onStart(dragBeginPosition, SelectionAdjustment.None, 1)
161162
observer.onDrag(dragDistance, SelectionAdjustment.None)
162163

163164
manager.value = value

compose/foundation/foundation/src/skikoTest/kotlin/androidx/compose/foundation/Assert.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import kotlin.math.abs
2020
import kotlin.test.assertContains
2121
import kotlin.test.assertContentEquals
2222
import kotlin.test.assertEquals
23+
import kotlin.test.assertFalse
2324
import kotlin.test.assertNotEquals
2425
import kotlin.test.assertTrue
2526

@@ -36,18 +37,18 @@ internal fun <T> AssertThat<T>.isEqualTo(a: Any?) {
3637
assertEquals(a, t)
3738
}
3839

39-
internal fun AssertThat<Float>.isEqualTo(f: Float, eps: Float = 0f) {
40-
if (eps != 0f) {
41-
assertTrue(message = message ?: "|$t - $f| exceeds $eps") { abs(t!! - f) <= eps }
42-
} else {
43-
assertEquals(f, t!!, message = message)
44-
}
40+
internal fun AssertThat<Float>.isEqualTo(f: Float, absoluteTolerance: Float = 0.001f) {
41+
assertEquals(f, t!!, absoluteTolerance, message = message)
4542
}
4643

4744
internal fun <T> AssertThat<T>.isNotEqualTo(a: Any?) {
4845
assertNotEquals(a, t)
4946
}
5047

48+
internal fun AssertThat<Float>.isNotEqualTo(f: Float, absoluteTolerance: Float = 0.001f) {
49+
assertNotEquals(f, t!!, absoluteTolerance, message = message)
50+
}
51+
5152
internal fun AssertThat<Int>.isEqualTo(i: Int, d: Int = 0) {
5253
if (d != 0) {
5354
assertTrue(message = message ?: "|$t - $i| exceeds $d") { abs(t!! - i) <= d }
@@ -110,6 +111,10 @@ internal fun AssertThat<Float>.isLessThan(n: Int) {
110111
assertTrue(t!! < n, message ?: "$t is not less than $n")
111112
}
112113

114+
internal fun AssertThat<Float>.isNotNaN() {
115+
assertFalse(t!!.isNaN(), message ?: "$t is NaN")
116+
}
117+
113118
internal fun <T : Collection<*>> AssertThat<T>.isEmpty() {
114119
assertTrue(t!!.isEmpty(), message ?: "$t is not empty")
115120
}
@@ -126,7 +131,7 @@ internal fun <K, T : Collection<*>> AssertThat<T>.contains(vararg items: K) {
126131

127132
internal fun AssertThat<*>.isZero() = isEqualTo(0)
128133

129-
internal fun AssertThat<*>.isNonZero() = isNotEqualTo(0)
134+
internal fun AssertThat<Float>.isNonZero() = isNotEqualTo(0f, absoluteTolerance = 0.001f)
130135

131136
internal fun AssertThat<*>.isNull() {
132137
assertEquals(null, t, message ?: "$t expected to be null")
@@ -144,4 +149,4 @@ internal fun assertWithMessage(message: String) = AssertMessage(message)
144149

145150
internal fun AssertThat<Int>.isGreaterThan(other: Int, d: Int = 0) {
146151
assertTrue(message ?: "The was expected to be greater than $other. But value = $t") { t!! > other }
147-
}
152+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.compose.foundation
18+
19+
import androidx.compose.ui.Modifier
20+
import kotlin.test.assertEquals
21+
import kotlin.test.assertNotEquals
22+
23+
// A copy from compose/test-utils/src/commonMain/kotlin/androidx/compose/testutils/ModifierTestUtils.kt
24+
// TODO commonize compose/test-utils properly
25+
26+
fun Modifier.toList(): List<Modifier.Element> =
27+
foldIn(mutableListOf()) { acc, e -> acc.apply { acc.add(e) } }
28+
29+
@Suppress("ModifierFactoryReturnType") fun Modifier.first(): Modifier.Element = toList().first()
30+
31+
/**
32+
* Asserts that creating two modifier with the same inputs will resolve to true when compared. In a
33+
* similar fashion, toggling the inputs will resolve to false. Ideally, all modifier elements should
34+
* preserve this property so when creating a modifier, an additional test should be included to
35+
* guarantee that.
36+
*/
37+
fun assertModifierIsPure(createModifier: (toggle: Boolean) -> Modifier) {
38+
val first = createModifier(true)
39+
val second = createModifier(false)
40+
val third = createModifier(true)
41+
42+
assertEquals(first, third, "Modifier with same inputs should resolve true to equals call")
43+
assertNotEquals(first, second, "Modifier with different inputs should resolve false to equals call")
44+
}

0 commit comments

Comments
 (0)