1 package com.android.systemui.qs
2 
3 import android.content.ComponentName
4 import android.service.quicksettings.Tile
5 import android.testing.AndroidTestingRunner
6 import androidx.test.filters.SmallTest
7 import com.android.systemui.SysuiTestCase
8 import com.android.systemui.plugins.qs.QSTile
9 import com.android.systemui.qs.external.CustomTile
10 import com.google.common.truth.Truth.assertThat
11 import org.junit.Test
12 import org.junit.runner.RunWith
13 
14 @RunWith(AndroidTestingRunner::class)
15 @SmallTest
16 class TileStateToProtoTest : SysuiTestCase() {
17 
18     companion object {
19         private const val TEST_LABEL = "label"
20         private const val TEST_SUBTITLE = "subtitle"
21         private const val TEST_SPEC = "spec"
22         private val TEST_COMPONENT = ComponentName("test_pkg", "test_cls")
23     }
24 
25     @Test
26     fun platformTile_INACTIVE() {
27         val state =
28             QSTile.State().apply {
29                 spec = TEST_SPEC
30                 label = TEST_LABEL
31                 secondaryLabel = TEST_SUBTITLE
32                 state = Tile.STATE_INACTIVE
33             }
34         val proto = state.toProto()
35 
36         assertThat(proto).isNotNull()
37         assertThat(proto?.hasSpec()).isTrue()
38         assertThat(proto?.spec).isEqualTo(TEST_SPEC)
39         assertThat(proto?.hasComponentName()).isFalse()
40         assertThat(proto?.label).isEqualTo(TEST_LABEL)
41         assertThat(proto?.secondaryLabel).isEqualTo(TEST_SUBTITLE)
42         assertThat(proto?.state).isEqualTo(Tile.STATE_INACTIVE)
43         assertThat(proto?.hasBooleanState()).isFalse()
44     }
45 
46     @Test
47     fun componentTile_UNAVAILABLE() {
48         val state =
49             QSTile.State().apply {
50                 spec = CustomTile.toSpec(TEST_COMPONENT)
51                 label = TEST_LABEL
52                 secondaryLabel = TEST_SUBTITLE
53                 state = Tile.STATE_UNAVAILABLE
54             }
55         val proto = state.toProto()
56 
57         assertThat(proto).isNotNull()
58         assertThat(proto?.hasSpec()).isFalse()
59         assertThat(proto?.hasComponentName()).isTrue()
60         val componentName = proto?.componentName
61         assertThat(componentName?.packageName).isEqualTo(TEST_COMPONENT.packageName)
62         assertThat(componentName?.className).isEqualTo(TEST_COMPONENT.className)
63         assertThat(proto?.label).isEqualTo(TEST_LABEL)
64         assertThat(proto?.secondaryLabel).isEqualTo(TEST_SUBTITLE)
65         assertThat(proto?.state).isEqualTo(Tile.STATE_UNAVAILABLE)
66         assertThat(proto?.hasBooleanState()).isFalse()
67     }
68 
69     @Test
70     fun booleanState_ACTIVE() {
71         val state =
72             QSTile.BooleanState().apply {
73                 spec = TEST_SPEC
74                 label = TEST_LABEL
75                 secondaryLabel = TEST_SUBTITLE
76                 state = Tile.STATE_ACTIVE
77                 value = true
78             }
79         val proto = state.toProto()
80 
81         assertThat(proto).isNotNull()
82         assertThat(proto?.hasSpec()).isTrue()
83         assertThat(proto?.spec).isEqualTo(TEST_SPEC)
84         assertThat(proto?.hasComponentName()).isFalse()
85         assertThat(proto?.label).isEqualTo(TEST_LABEL)
86         assertThat(proto?.secondaryLabel).isEqualTo(TEST_SUBTITLE)
87         assertThat(proto?.state).isEqualTo(Tile.STATE_ACTIVE)
88         assertThat(proto?.hasBooleanState()).isTrue()
89         assertThat(proto?.booleanState).isTrue()
90     }
91 
92     @Test
93     fun noSpec_returnsNull() {
94         val state =
95             QSTile.State().apply {
96                 label = TEST_LABEL
97                 secondaryLabel = TEST_SUBTITLE
98                 state = Tile.STATE_ACTIVE
99             }
100         val proto = state.toProto()
101 
102         assertThat(proto).isNull()
103     }
104 }
105