xref: /aosp_15_r20/frameworks/base/packages/SettingsLib/Spa/tests/src/com/android/settingslib/spa/widget/chart/ChartTest.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib.spa.widget.chart
18 
19 import androidx.compose.ui.Modifier
20 import androidx.compose.ui.graphics.Color
21 import androidx.compose.ui.semantics.SemanticsPropertyKey
22 import androidx.compose.ui.semantics.SemanticsPropertyReceiver
23 import androidx.compose.ui.semantics.semantics
24 import androidx.compose.ui.test.SemanticsMatcher
25 import androidx.compose.ui.test.assertIsDisplayed
26 import androidx.compose.ui.test.captureToImage
27 import androidx.compose.ui.test.junit4.createComposeRule
28 import androidx.compose.ui.test.onRoot
29 import androidx.test.ext.junit.runners.AndroidJUnit4
30 import com.android.settingslib.spa.testutils.assertContainsColor
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 @RunWith(AndroidJUnit4::class)
36 class ChartTest {
37     @get:Rule
38     val composeTestRule = createComposeRule()
39 
40     private val chart = SemanticsPropertyKey<String>("Chart")
41     private var SemanticsPropertyReceiver.chart by chart
hasChartnull42     private fun hasChart(chart: String): SemanticsMatcher =
43         SemanticsMatcher.expectValue(this.chart, chart)
44 
45     @Test
46     fun line_chart_displayed() {
47         composeTestRule.setContent {
48             LineChart(
49                 chartDataList = listOf(
50                     LineChartData(x = 0f, y = 0f),
51                     LineChartData(x = 1f, y = 0.1f),
52                     LineChartData(x = 2f, y = 0.2f),
53                     LineChartData(x = 3f, y = 0.7f),
54                     LineChartData(x = 4f, y = 0.9f),
55                     LineChartData(x = 5f, y = 1.0f),
56                     LineChartData(x = 6f, y = 0.8f),
57                 ),
58                 modifier = Modifier.semantics { chart = "line" }
59             )
60         }
61 
62         composeTestRule.onNode(hasChart("line")).assertIsDisplayed()
63     }
64 
65     @Test
bar_chart_displayednull66     fun bar_chart_displayed() {
67         composeTestRule.setContent {
68             BarChart(object : BarChartModel {
69                 override val chartDataList = listOf(
70                     BarChartData(x = 0f, y = listOf(12f)),
71                     BarChartData(x = 1f, y = listOf(5f)),
72                     BarChartData(x = 2f, y = listOf(21f)),
73                     BarChartData(x = 3f, y = listOf(5f)),
74                     BarChartData(x = 4f, y = listOf(10f)),
75                     BarChartData(x = 5f, y = listOf(9f)),
76                     BarChartData(x = 6f, y = listOf(1f)),
77                 )
78                 override val colors = listOf(Color.Blue)
79             })
80         }
81 
82         composeTestRule.onRoot().captureToImage().assertContainsColor(Color.Blue)
83     }
84 
85     @Test
pie_chart_displayednull86     fun pie_chart_displayed() {
87         composeTestRule.setContent {
88             PieChart(
89                 chartDataList = listOf(
90                     PieChartData(label = "Settings", value = 20f),
91                     PieChartData(label = "Chrome", value = 5f),
92                     PieChartData(label = "Gmail", value = 3f),
93                 ),
94                 centerText = "Today",
95                 modifier = Modifier.semantics { chart = "pie" }
96             )
97         }
98 
99         composeTestRule.onNode(hasChart("pie")).assertIsDisplayed()
100     }
101 }
102