Merge "Add tests."

This commit is contained in:
Zekan Qian
2022-12-10 08:53:33 +00:00
committed by Android (Google) Code Review
4 changed files with 76 additions and 1 deletions

View File

@@ -101,6 +101,10 @@ task coverageReport(type: JacocoReport, dependsOn: "connectedDebugAndroidTest")
excludes: [
"com/android/settingslib/spa/debug/**",
// Excludes inline functions, which is not covered in Jacoco reports.
"com/android/settingslib/spa/framework/util/Collections*",
"com/android/settingslib/spa/framework/util/Flows*",
// Excludes files forked from AndroidX.
"com/android/settingslib/spa/widget/scaffold/CustomizedAppBar*",
"com/android/settingslib/spa/widget/scaffold/TopAppBarColors*",

View File

@@ -151,7 +151,7 @@ data class SettingsEntry(
}
@Composable
fun provideLocalEntryData(arguments: Bundle): ProvidedValue<EntryData> {
private fun provideLocalEntryData(arguments: Bundle): ProvidedValue<EntryData> {
val controller = LocalNavController.current
return LocalEntryDataProvider provides remember {
object : EntryData {

View File

@@ -16,10 +16,13 @@
package com.android.settingslib.spa.framework.common
import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.core.os.bundleOf
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.slice.appendSpaParams
import com.android.settingslib.spa.slice.getEntryId
import com.android.settingslib.spa.tests.testutils.getUniqueEntryId
import com.android.settingslib.spa.tests.testutils.getUniquePageId
import com.google.common.truth.Truth.assertThat
@@ -169,4 +172,25 @@ class SettingsEntryTest {
assertThat(statusData?.isDisabled).isTrue()
assertThat(statusData?.isSwitchOff).isTrue()
}
@Test
fun testSetSliceDataFn() {
val owner = SettingsPage.create("mySpp")
val entryId = getUniqueEntryId("myEntry", owner)
val emptySliceData = EntrySliceData()
val entryBuilder = SettingsEntryBuilder.create(owner, "myEntry")
.setSliceDataFn { uri, _ ->
return@setSliceDataFn if (uri.getEntryId() == entryId) emptySliceData else null
}
val entry = entryBuilder.build()
assertThat(entry.id).isEqualTo(entryId)
assertThat(entry.hasSliceSupport).isTrue()
assertThat(entry.getSliceData(Uri.EMPTY)).isNull()
assertThat(
entry.getSliceData(
Uri.Builder().scheme("content").appendSpaParams(entryId = entryId).build()
)
).isEqualTo(emptySliceData)
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2022 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.settingslib.spa.framework.util
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class CollectionsTest {
@Test
fun testAsyncForEach() = runTest {
var sum = 0
listOf(1, 2, 3).asyncForEach { sum += it }
Truth.assertThat(sum).isEqualTo(6)
}
@Test
fun testAsyncFilter() = runTest {
val res = listOf(1, 2, 3).asyncFilter { it >= 2 }
Truth.assertThat(res).containsExactly(2, 3).inOrder()
}
@Test
fun testAsyncMap() = runTest {
val res = listOf(1, 2, 3).asyncMap { it + 1 }
Truth.assertThat(res).containsExactly(2, 3, 4).inOrder()
}
}