Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a1dba6fb authored by Zekan Qian's avatar Zekan Qian
Browse files

Add tests.

Excludes inline function from Jacoco reports.

Bug: 256582545
Test: unit-test & local build gallery
Change-Id: Id735ee6a2f9ca0186fbf1b8c90b308e782e8797a
parent ba2f009a
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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*",
+1 −1
Original line number Diff line number Diff line
@@ -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 {
+24 −0
Original line number Diff line number Diff line
@@ -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)
    }
}
+47 −0
Original line number Diff line number Diff line
/*
 * 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()
    }
}