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

Commit f09d89ab authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "New AppListSwitchItem" into main

parents 47b86012 722d6088
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 * 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.
@@ -19,26 +19,26 @@ package com.android.settingslib.spaprivileged.template.app
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.widget.preference.SwitchPreference
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
import com.android.settingslib.spa.widget.preference.TwoTargetSwitchPreference
import com.android.settingslib.spaprivileged.model.app.AppRecord

@Composable
fun <T : AppRecord> AppListItemModel<T>.AppListSwitchItem(
    onClick: () -> Unit,
    checked: State<Boolean?>,
    changeable: State<Boolean>,
    onCheckedChange: ((newChecked: Boolean) -> Unit)?,
) {
    TwoTargetSwitchPreference(
    SwitchPreference(
        model = object : SwitchPreferenceModel {
            override val title = label
            override val summary = this@AppListSwitchItem.summary
            override val icon = @Composable {
                AppIcon(record.app, SettingsDimension.appIconItemSize)
            }
            override val checked = checked
            override val changeable = changeable
            override val onCheckedChange = onCheckedChange
        },
        icon = { AppIcon(record.app, SettingsDimension.appIconItemSize) },
        onClick = onClick,
    )
}
+44 −0
Original line number Diff line number Diff line
/*
 * 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.settingslib.spaprivileged.template.app

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
import com.android.settingslib.spa.widget.preference.TwoTargetSwitchPreference
import com.android.settingslib.spaprivileged.model.app.AppRecord

@Composable
fun <T : AppRecord> AppListItemModel<T>.AppListTwoTargetSwitchItem(
    onClick: () -> Unit,
    checked: State<Boolean?>,
    changeable: State<Boolean>,
    onCheckedChange: ((newChecked: Boolean) -> Unit)?,
) {
    TwoTargetSwitchPreference(
        model = object : SwitchPreferenceModel {
            override val title = label
            override val summary = this@AppListTwoTargetSwitchItem.summary
            override val checked = checked
            override val changeable = changeable
            override val onCheckedChange = onCheckedChange
        },
        icon = { AppIcon(record.app, SettingsDimension.appIconItemSize) },
        onClick = onClick,
    )
}
+1 −26
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 * 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.
@@ -43,7 +43,6 @@ class AppListSwitchItemTest {
    fun appLabel_displayed() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(null),
                changeable = stateOf(false),
                onCheckedChange = {},
@@ -57,7 +56,6 @@ class AppListSwitchItemTest {
    fun summary_displayed() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(null),
                changeable = stateOf(false),
                onCheckedChange = {},
@@ -67,28 +65,10 @@ class AppListSwitchItemTest {
        composeTestRule.onNodeWithText(SUMMARY).assertIsDisplayed()
    }

    @Test
    fun title_onClick() {
        var titleClicked = false
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = { titleClicked = true },
                checked = stateOf(false),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNodeWithText(LABEL).performClick()

        assertThat(titleClicked).isTrue()
    }

    @Test
    fun switch_checkIsNull() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(null),
                changeable = stateOf(false),
                onCheckedChange = {},
@@ -102,7 +82,6 @@ class AppListSwitchItemTest {
    fun switch_checked() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(true),
                changeable = stateOf(false),
                onCheckedChange = {},
@@ -116,7 +95,6 @@ class AppListSwitchItemTest {
    fun switch_notChecked() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(false),
                onCheckedChange = {},
@@ -130,7 +108,6 @@ class AppListSwitchItemTest {
    fun switch_changeable() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(true),
                onCheckedChange = {},
@@ -144,7 +121,6 @@ class AppListSwitchItemTest {
    fun switch_notChangeable() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(false),
                onCheckedChange = {},
@@ -159,7 +135,6 @@ class AppListSwitchItemTest {
        var switchClicked = false
        composeTestRule.setContent {
            ITEM_MODEL.AppListSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(true),
                onCheckedChange = { switchClicked = true },
+189 −0
Original line number Diff line number Diff line
/*
 * 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.settingslib.spaprivileged.template.app

import android.content.pm.ApplicationInfo
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.assertIsOff
import androidx.compose.ui.test.assertIsOn
import androidx.compose.ui.test.isToggleable
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spaprivileged.model.app.AppRecord
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

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

    @Test
    fun appLabel_displayed() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(null),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNodeWithText(LABEL).assertIsDisplayed()
    }

    @Test
    fun summary_displayed() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(null),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNodeWithText(SUMMARY).assertIsDisplayed()
    }

    @Test
    fun title_onClick() {
        var titleClicked = false
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = { titleClicked = true },
                checked = stateOf(false),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNodeWithText(LABEL).performClick()

        assertThat(titleClicked).isTrue()
    }

    @Test
    fun switch_checkIsNull() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(null),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNode(isToggleable()).assertDoesNotExist()
    }

    @Test
    fun switch_checked() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(true),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNode(isToggleable()).assertIsOn()
    }

    @Test
    fun switch_notChecked() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNode(isToggleable()).assertIsOff()
    }

    @Test
    fun switch_changeable() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(true),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNode(isToggleable()).assertIsEnabled()
    }

    @Test
    fun switch_notChangeable() {
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(false),
                onCheckedChange = {},
            )
        }

        composeTestRule.onNode(isToggleable()).assertIsNotEnabled()
    }

    @Test
    fun switch_onClick() {
        var switchClicked = false
        composeTestRule.setContent {
            ITEM_MODEL.AppListTwoTargetSwitchItem(
                onClick = {},
                checked = stateOf(false),
                changeable = stateOf(true),
                onCheckedChange = { switchClicked = true },
            )
        }

        composeTestRule.onNode(isToggleable()).performClick()

        assertThat(switchClicked).isTrue()
    }

    private companion object {
        const val PACKAGE_NAME = "package.name"
        const val LABEL = "Label"
        const val SUMMARY = "Summary"
        val APP = ApplicationInfo().apply {
            packageName = PACKAGE_NAME
        }
        val ITEM_MODEL = AppListItemModel(
            record = object : AppRecord {
                override val app = APP
            },
            label = LABEL,
            summary = stateOf(SUMMARY),
        )
    }
}