• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.server.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.provider.DeviceConfig;
22 import android.provider.DeviceConfig.Properties;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 @SmallTest
31 @RunWith(AndroidJUnit4.class)
32 public class BluetoothDeviceConfigChangeTrackerTest {
33     @Test
testNoProperties()34     public void testNoProperties() {
35         BluetoothDeviceConfigChangeTracker changeTracker =
36                 new BluetoothDeviceConfigChangeTracker(
37                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH).build());
38 
39         boolean shouldRestart =
40                 changeTracker.shouldRestartWhenPropertiesUpdated(
41                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH).build());
42 
43         assertThat(shouldRestart).isFalse();
44     }
45 
46     @Test
testNewFlag()47     public void testNewFlag() {
48         BluetoothDeviceConfigChangeTracker changeTracker =
49                 new BluetoothDeviceConfigChangeTracker(
50                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
51                                 .setString("INIT_a", "true")
52                                 .build());
53 
54         boolean shouldRestart =
55                 changeTracker.shouldRestartWhenPropertiesUpdated(
56                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
57                                 .setString("INIT_a", "true")
58                                 .setString("INIT_b", "true")
59                                 .build());
60 
61         assertThat(shouldRestart).isTrue();
62     }
63 
64     @Test
testChangedFlag()65     public void testChangedFlag() {
66         BluetoothDeviceConfigChangeTracker changeTracker =
67                 new BluetoothDeviceConfigChangeTracker(
68                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
69                                 .setString("INIT_a", "true")
70                                 .build());
71 
72         boolean shouldRestart =
73                 changeTracker.shouldRestartWhenPropertiesUpdated(
74                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
75                                 .setString("INIT_a", "false")
76                                 .build());
77 
78         assertThat(shouldRestart).isTrue();
79     }
80 
81     @Test
testUnchangedInitFlag()82     public void testUnchangedInitFlag() {
83         BluetoothDeviceConfigChangeTracker changeTracker =
84                 new BluetoothDeviceConfigChangeTracker(
85                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
86                                 .setString("INIT_a", "true")
87                                 .build());
88 
89         boolean shouldRestart =
90                 changeTracker.shouldRestartWhenPropertiesUpdated(
91                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
92                                 .setString("INIT_a", "true")
93                                 .build());
94 
95         assertThat(shouldRestart).isFalse();
96     }
97 
98     @Test
testRepeatedChangeInitFlag()99     public void testRepeatedChangeInitFlag() {
100         BluetoothDeviceConfigChangeTracker changeTracker =
101                 new BluetoothDeviceConfigChangeTracker(
102                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH).build());
103 
104         changeTracker.shouldRestartWhenPropertiesUpdated(
105                 new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
106                         .setString("INIT_a", "true")
107                         .build());
108 
109         boolean shouldRestart =
110                 changeTracker.shouldRestartWhenPropertiesUpdated(
111                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
112                                 .setString("INIT_a", "true")
113                                 .build());
114 
115         assertThat(shouldRestart).isFalse();
116     }
117 
118     @Test
testWrongNamespace()119     public void testWrongNamespace() {
120         BluetoothDeviceConfigChangeTracker changeTracker =
121                 new BluetoothDeviceConfigChangeTracker(
122                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH).build());
123 
124         boolean shouldRestart =
125                 changeTracker.shouldRestartWhenPropertiesUpdated(
126                         new Properties.Builder("another_namespace")
127                                 .setString("INIT_a", "true")
128                                 .build());
129 
130         assertThat(shouldRestart).isFalse();
131     }
132 
133     @Test
testSkipProperty()134     public void testSkipProperty() {
135         BluetoothDeviceConfigChangeTracker changeTracker =
136                 new BluetoothDeviceConfigChangeTracker(
137                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
138                                 .setString("INIT_a", "true")
139                                 .setString("INIT_b", "false")
140                                 .build());
141 
142         boolean shouldRestart =
143                 changeTracker.shouldRestartWhenPropertiesUpdated(
144                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
145                                 .setString("INIT_b", "false")
146                                 .build());
147 
148         assertThat(shouldRestart).isFalse();
149     }
150 
151     @Test
testNonInitFlag()152     public void testNonInitFlag() {
153         BluetoothDeviceConfigChangeTracker changeTracker =
154                 new BluetoothDeviceConfigChangeTracker(
155                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
156                                 .setString("a", "true")
157                                 .build());
158 
159         boolean shouldRestart =
160                 changeTracker.shouldRestartWhenPropertiesUpdated(
161                         new Properties.Builder(DeviceConfig.NAMESPACE_BLUETOOTH)
162                                 .setString("a", "false")
163                                 .build());
164 
165         assertThat(shouldRestart).isFalse();
166     }
167 }
168