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

Commit bce00725 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[Spa] Add CategoryButton" into main

parents e7045220 fdb947f4
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -17,9 +17,12 @@
package com.android.settingslib.spa.gallery.preference

import android.os.Bundle
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.runtime.Composable
import com.android.settingslib.spa.framework.common.SettingsPageProvider
import com.android.settingslib.spa.framework.compose.navigator
import com.android.settingslib.spa.widget.button.CategoryButton
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.PreferenceModel
import com.android.settingslib.spa.widget.scaffold.RegularScaffold
@@ -38,6 +41,7 @@ object PreferenceMainPageProvider : SettingsPageProvider {
                ListPreferencePageProvider.Entry()
                CheckBoxPreferencePageProvider.Entry()
            }
            CategoryButton(icon = Icons.Outlined.Add, text = "Sample Category Button") {}
            Category {
                SwitchPreferencePageProvider.Entry()
                MainSwitchPreferencePageProvider.Entry()
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.widget.button

import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.tooling.preview.Preview
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.framework.theme.SettingsTheme

@Composable
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
fun CategoryButton(
    icon: ImageVector,
    text: String,
    onClick: () -> Unit,
) {
    val size = ButtonDefaults.MediumContainerHeight
    TextButton(
        onClick = onClick,
        modifier = Modifier
            .heightIn(size)
            .padding(horizontal = SettingsDimension.paddingLarge),
        colors = ButtonDefaults.textButtonColors().copy(
            containerColor = MaterialTheme.colorScheme.surfaceBright,
            contentColor = MaterialTheme.colorScheme.primary,
        ),
        contentPadding = ButtonDefaults.contentPaddingFor(size)
    ) {
        Icon(
            icon,
            contentDescription = null,
            modifier = Modifier.size(ButtonDefaults.iconSizeFor(size))
        )
        Spacer(Modifier.size(ButtonDefaults.iconSpacingFor(size)))
        Text(text = text, style = ButtonDefaults.textStyleFor(size))
    }
}

@Preview
@Composable
private fun CategoryButtonPreview() {
    SettingsTheme {
        CategoryButton(icon = Icons.Outlined.Add, text = "Add SIM") {}
    }
}
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.widget.button

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.ui.test.assertIsDisplayed
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.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

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

    @Test
    fun categoryButton_textDisplayed() {
        composeTestRule.setContent {
            CategoryButton(icon = Icons.Outlined.Add, text = TEXT) {}
        }

        composeTestRule.onNodeWithText(TEXT).assertIsDisplayed()
    }

    @Test
    fun categoryButton_onClick() {
        var clicked = false

        composeTestRule.setContent {
            CategoryButton(icon = Icons.Outlined.Add, text = TEXT) { clicked = true }
        }
        composeTestRule.onNodeWithText(TEXT).performClick()

        assertThat(clicked).isTrue()
    }

    private companion object {
        const val TEXT = "Text"
    }
}