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

Commit 757690a3 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Add initial tests for TogglePermissionAppList

Bug: 260660819
Test: Gradle Sync
Test: Unit test
Change-Id: Ia61c15b019a8bf4f3ff5fa0cc1b1f7066069f383
parent 4aaca3ac
Loading
Loading
Loading
Loading
+26 −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.
  -->

<resources>
    <!-- Test Permission title. [DO NOT TRANSLATE] -->
    <string name="test_permission_title" translatable="false">Test Permission</string>

    <!-- Test Permission switch title. [DO NOT TRANSLATE] -->
    <string name="test_permission_switch_title" translatable="false">Allow Test Permission</string>

    <!-- Test Permission footer. [DO NOT TRANSLATE] -->
    <string name="test_permission_footer" translatable="false">Test Permission is for demo.</string>
</resources>
+67 −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.spaprivileged.template.app

import android.content.Context
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spaprivileged.R
import com.android.settingslib.spaprivileged.tests.testutils.TestTogglePermissionAppListModel
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class TogglePermissionAppListPageTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    private var context: Context = ApplicationProvider.getApplicationContext()

    @Test
    fun appListInjectEntry_titleDisplayed() {
        val entry = TogglePermissionAppListPageProvider.buildInjectEntry(PERMISSION_TYPE) {
            TestTogglePermissionAppListModel()
        }.build()

        composeTestRule.setContent {
            CompositionLocalProvider(LocalContext provides context) {
                entry.UiLayout()
            }
        }

        composeTestRule.onNodeWithText(context.getString(R.string.test_permission_title))
            .assertIsDisplayed()
    }

    @Test
    fun appListRoute() {
        val route = TogglePermissionAppListPageProvider.getRoute(PERMISSION_TYPE)

        assertThat(route).isEqualTo("TogglePermissionAppList/test.PERMISSION")
    }

    private companion object {
        const val PERMISSION_TYPE = "test.PERMISSION"
    }
}
+77 −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.spaprivileged.template.app

import android.content.Context
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spaprivileged.R
import com.android.settingslib.spaprivileged.tests.testutils.TestTogglePermissionAppListModel
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class TogglePermissionAppListTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    private var context: Context = ApplicationProvider.getApplicationContext()

    @Test
    fun appListInjectEntry_titleDisplayed() {
        val entry = TestTogglePermissionAppListProvider.buildAppListInjectEntry().build()
        composeTestRule.setContent {
            CompositionLocalProvider(LocalContext provides context) {
                entry.UiLayout()
            }
        }

        composeTestRule.onNodeWithText(context.getString(R.string.test_permission_title))
            .assertIsDisplayed()
    }

    @Test
    fun appListRoute() {
        val route = TestTogglePermissionAppListProvider.getAppListRoute()

        assertThat(route).isEqualTo("TogglePermissionAppList/test.PERMISSION")
    }

    @Test
    fun togglePermissionAppListTemplate_createPageProviders() {
        val togglePermissionAppListTemplate =
            TogglePermissionAppListTemplate(listOf(TestTogglePermissionAppListProvider))

        val createPageProviders = togglePermissionAppListTemplate.createPageProviders()

        assertThat(createPageProviders).hasSize(2)
        assertThat(createPageProviders.any { it is TogglePermissionAppListPageProvider }).isTrue()
        assertThat(createPageProviders.any { it is TogglePermissionAppInfoPageProvider }).isTrue()
    }
}

private object TestTogglePermissionAppListProvider : TogglePermissionAppListProvider {
    override val permissionType = "test.PERMISSION"
    override fun createModel(context: Context) = TestTogglePermissionAppListModel()
}
+42 −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.spaprivileged.tests.testutils

import android.content.pm.ApplicationInfo
import androidx.compose.runtime.Composable
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spaprivileged.R
import com.android.settingslib.spaprivileged.template.app.TogglePermissionAppListModel
import kotlinx.coroutines.flow.Flow

class TestTogglePermissionAppListModel : TogglePermissionAppListModel<TestAppRecord> {
    override val pageTitleResId = R.string.test_permission_title
    override val switchTitleResId = R.string.test_permission_switch_title
    override val footerResId = R.string.test_permission_footer

    override fun transformItem(app: ApplicationInfo) = TestAppRecord(app = app)

    override fun filter(userIdFlow: Flow<Int>, recordListFlow: Flow<List<TestAppRecord>>) =
        recordListFlow

    @Composable
    override fun isAllowed(record: TestAppRecord) = stateOf(null)

    override fun isChangeable(record: TestAppRecord) = false

    override fun setAllowed(record: TestAppRecord, newAllowed: Boolean) {}
}