Files
Lawnchair/tests/src/com/android/launcher3/responsive/SizeSpecTest.kt
T
Jordan Silva 1b336110a4 Update SizeSpec attributes to accept max size threshold
Add maxSize attribute to SizeSpec to limit the cell size up to a max size when matchWorkspace is true. The same validation was added when using fixedSize, ofAvailableSpace and ofRemainderSpace, so they could have a maxSize as a threshold.

Bug: 284155638
Flag: ENABLE_RESPONSIVE_WORKSPACE
Test: SizeSpecTest
Test: WorkspaceSpecsTest
Change-Id: I113657c241e6618eb3e501243412d8c5626fc3d5
2023-06-13 18:09:37 +01:00

143 lines
5.0 KiB
Kotlin

/*
* Copyright (C) 2023 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 com.android.launcher3.responsive
import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
import com.android.launcher3.AbstractDeviceProfileTest
import com.google.common.truth.Truth.assertThat
import kotlin.math.roundToInt
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@SmallTest
@RunWith(AndroidJUnit4::class)
class SizeSpecTest : AbstractDeviceProfileTest() {
override val runningContext: Context = InstrumentationRegistry.getInstrumentation().context
@Before
fun setup() {
initializeVarsForPhone(deviceSpecs["phone"]!!)
}
@Test
fun valid_values() {
val combinations =
listOf(
SizeSpec(100f, 0f, 0f, false),
SizeSpec(0f, 1f, 0f, false),
SizeSpec(0f, 0f, 1f, false),
SizeSpec(0f, 0f, 0f, false),
SizeSpec(0f, 0f, 0f, true),
SizeSpec(100f, 0f, 0f, false, 100),
SizeSpec(0f, 1f, 0f, false, 100),
SizeSpec(0f, 0f, 1f, false, 100),
SizeSpec(0f, 0f, 0f, false, 100),
SizeSpec(0f, 0f, 0f, true, 100)
)
for (instance in combinations) {
assertThat(instance.isValid()).isEqualTo(true)
}
}
@Test
fun validate_getCalculatedValue() {
val availableSpace = 100
val matchWorkspaceValue = 101
val combinations =
listOf(
SizeSpec(100f) to 100,
SizeSpec(ofAvailableSpace = .5f) to (availableSpace * .5f).roundToInt(),
SizeSpec(ofRemainderSpace = .5f) to 0,
SizeSpec(matchWorkspace = true) to matchWorkspaceValue,
// Restricts max size up to 10 (calculated value > 10)
SizeSpec(100f, maxSize = 10) to 10,
SizeSpec(ofAvailableSpace = .5f, maxSize = 10) to 10,
SizeSpec(ofRemainderSpace = .5f, maxSize = 10) to 0,
SizeSpec(matchWorkspace = true, maxSize = 10) to 10
)
for ((sizeSpec, expectedValue) in combinations) {
val value = sizeSpec.getCalculatedValue(availableSpace, matchWorkspaceValue)
assertThat(value).isEqualTo(expectedValue)
}
}
@Test
fun validate_getRemainderSpaceValue() {
val remainderSpace = 100
val defaultValue = 50
val combinations =
listOf(
SizeSpec(100f) to defaultValue,
SizeSpec(ofAvailableSpace = .5f) to defaultValue,
SizeSpec(ofRemainderSpace = .5f) to (remainderSpace * .5f).roundToInt(),
SizeSpec(matchWorkspace = true) to defaultValue,
// Restricts max size up to 10 (defaultValue > 10)
SizeSpec(100f, maxSize = 10) to 10,
SizeSpec(ofAvailableSpace = .5f, maxSize = 10) to 10,
SizeSpec(ofRemainderSpace = .5f, maxSize = 10) to 10,
SizeSpec(matchWorkspace = true, maxSize = 10) to 10,
)
for ((sizeSpec, expectedValue) in combinations) {
val value = sizeSpec.getRemainderSpaceValue(remainderSpace, defaultValue)
assertThat(value).isEqualTo(expectedValue)
}
}
@Test
fun multiple_values_assigned() {
val combinations =
listOf(
SizeSpec(1f, 1f, 0f, false),
SizeSpec(1f, 0f, 1f, false),
SizeSpec(1f, 0f, 0f, true),
SizeSpec(0f, 1f, 1f, false),
SizeSpec(0f, 1f, 0f, true),
SizeSpec(0f, 0f, 1f, true),
SizeSpec(1f, 1f, 1f, true)
)
for (instance in combinations) {
assertThat(instance.isValid()).isEqualTo(false)
}
}
@Test
fun invalid_values() {
val combinations =
listOf(
SizeSpec(-1f, 0f, 0f, false),
SizeSpec(0f, 1.1f, 0f, false),
SizeSpec(0f, -0.1f, 0f, false),
SizeSpec(0f, 0f, 1.1f, false),
SizeSpec(0f, 0f, -0.1f, false),
SizeSpec(0f, 0f, 0f, false, -10),
SizeSpec(50f, 0f, 0f, false, 10)
)
for (instance in combinations) {
assertThat(instance.isValid()).isEqualTo(false)
}
}
}