• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.settingslib.drawer;
17 
18 import static com.android.settingslib.drawer.SwitchesProvider.EXTRA_SWITCH_CHECKED_STATE;
19 import static com.android.settingslib.drawer.SwitchesProvider.EXTRA_SWITCH_DATA;
20 import static com.android.settingslib.drawer.SwitchesProvider.EXTRA_SWITCH_SET_CHECKED_ERROR;
21 import static com.android.settingslib.drawer.SwitchesProvider.EXTRA_SWITCH_SET_CHECKED_ERROR_MESSAGE;
22 import static com.android.settingslib.drawer.SwitchesProvider.METHOD_GET_DYNAMIC_SUMMARY;
23 import static com.android.settingslib.drawer.SwitchesProvider.METHOD_GET_DYNAMIC_TITLE;
24 import static com.android.settingslib.drawer.SwitchesProvider.METHOD_GET_PROVIDER_ICON;
25 import static com.android.settingslib.drawer.SwitchesProvider.METHOD_GET_SWITCH_DATA;
26 import static com.android.settingslib.drawer.SwitchesProvider.METHOD_IS_CHECKED;
27 import static com.android.settingslib.drawer.SwitchesProvider.METHOD_ON_CHECKED_CHANGED;
28 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYHINT;
29 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMMARY;
30 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_TITLE;
31 
32 import static com.google.common.truth.Truth.assertThat;
33 
34 import android.content.Context;
35 import android.content.pm.ProviderInfo;
36 import android.os.Bundle;
37 
38 import com.android.settingslib.drawer.PrimarySwitchControllerTest.TestPrimarySwitchController;
39 import com.android.settingslib.drawer.SwitchController.MetaData;
40 
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.junit.runner.RunWith;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 
49 import java.util.ArrayList;
50 import java.util.List;
51 
52 @RunWith(RobolectricTestRunner.class)
53 public class SwitchesProviderTest {
54 
55     @Rule
56     public final ExpectedException thrown = ExpectedException.none();
57 
58     private Context mContext;
59     private ProviderInfo mProviderInfo;
60 
61     private TestSwitchesProvider mSwitchesProvider;
62 
63     @Before
setUp()64     public void setUp() {
65         mContext = RuntimeEnvironment.application;
66         mSwitchesProvider = new TestSwitchesProvider();
67         mProviderInfo = new ProviderInfo();
68         mProviderInfo.authority = "auth";
69     }
70 
71     @Test
attachInfo_noController_shouldThrowIllegalArgumentException()72     public void attachInfo_noController_shouldThrowIllegalArgumentException() {
73         thrown.expect(IllegalArgumentException.class);
74 
75         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
76     }
77 
78     @Test
attachInfo_NoSwitchKeyInController_shouldThrowNullPointerException()79     public void attachInfo_NoSwitchKeyInController_shouldThrowNullPointerException() {
80         thrown.expect(NullPointerException.class);
81         final TestSwitchController controller = new TestSwitchController();
82         mSwitchesProvider.addSwitchController(controller);
83 
84         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
85     }
86 
87     @Test
attachInfo_NoMetaDataInController_shouldThrowNullPointerException()88     public void attachInfo_NoMetaDataInController_shouldThrowNullPointerException() {
89         thrown.expect(NullPointerException.class);
90         final TestSwitchController controller = new TestSwitchController();
91         controller.setKey("123");
92         mSwitchesProvider.addSwitchController(controller);
93 
94         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
95     }
96 
97     @Test
attachInfo_duplicateSwitchKey_shouldThrowIllegalArgumentException()98     public void attachInfo_duplicateSwitchKey_shouldThrowIllegalArgumentException() {
99         thrown.expect(IllegalArgumentException.class);
100         final TestSwitchController controller1 = new TestSwitchController();
101         final TestSwitchController controller2 = new TestSwitchController();
102         controller1.setKey("123");
103         controller2.setKey("123");
104         controller1.setMetaData(new MetaData("category"));
105         controller2.setMetaData(new MetaData("category"));
106         mSwitchesProvider.addSwitchController(controller1);
107         mSwitchesProvider.addSwitchController(controller2);
108 
109         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
110     }
111 
112     @Test
attachInfo_hasDifferentControllers_shouldNotThrowException()113     public void attachInfo_hasDifferentControllers_shouldNotThrowException() {
114         final TestSwitchController controller1 = new TestSwitchController();
115         final TestSwitchController controller2 = new TestSwitchController();
116         controller1.setKey("123");
117         controller2.setKey("456");
118         controller1.setMetaData(new MetaData("category"));
119         controller2.setMetaData(new MetaData("category"));
120         mSwitchesProvider.addSwitchController(controller1);
121         mSwitchesProvider.addSwitchController(controller2);
122 
123         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
124     }
125 
126     @Test
getSwitchData_shouldNotReturnPrimarySwitchData()127     public void getSwitchData_shouldNotReturnPrimarySwitchData() {
128         final SwitchController controller = new TestPrimarySwitchController("123");
129         mSwitchesProvider.addSwitchController(controller);
130         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
131 
132         final Bundle switchData = mSwitchesProvider.call(METHOD_GET_SWITCH_DATA, "uri" ,
133                 null /* extras*/);
134 
135         final ArrayList<Bundle> dataList = switchData.getParcelableArrayList(EXTRA_SWITCH_DATA);
136         assertThat(dataList).isEmpty();
137     }
138 
139     @Test
getSwitchData_shouldReturnDataList()140     public void getSwitchData_shouldReturnDataList() {
141         final TestSwitchController controller = new TestSwitchController();
142         controller.setKey("123");
143         controller.setMetaData(new MetaData("category"));
144         mSwitchesProvider.addSwitchController(controller);
145         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
146 
147         final Bundle switchData = mSwitchesProvider.call(METHOD_GET_SWITCH_DATA, "uri" ,
148                 null /* extras*/);
149 
150         final ArrayList<Bundle> dataList = switchData.getParcelableArrayList(EXTRA_SWITCH_DATA);
151         assertThat(dataList).hasSize(1);
152         assertThat(dataList.get(0).getString(META_DATA_PREFERENCE_KEYHINT)).isEqualTo("123");
153     }
154 
155     @Test
getSwitchDataByKey_shouldReturnData()156     public void getSwitchDataByKey_shouldReturnData() {
157         final Bundle extras = new Bundle();
158         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
159         final TestSwitchController controller = new TestSwitchController();
160         controller.setKey("123");
161         controller.setMetaData(new MetaData("category"));
162         mSwitchesProvider.addSwitchController(controller);
163         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
164 
165         final Bundle switchData = mSwitchesProvider.call(METHOD_GET_SWITCH_DATA, "uri" , extras);
166 
167         assertThat(switchData.getString(META_DATA_PREFERENCE_KEYHINT)).isEqualTo("123");
168     }
169 
170     @Test
isChecked_shouldReturnCheckedState()171     public void isChecked_shouldReturnCheckedState() {
172         final Bundle extras = new Bundle();
173         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
174         final TestSwitchController controller = new TestSwitchController();
175         controller.setKey("123");
176         controller.setMetaData(new MetaData("category"));
177         mSwitchesProvider.addSwitchController(controller);
178         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
179 
180         controller.setChecked(true);
181         Bundle result = mSwitchesProvider.call(METHOD_IS_CHECKED, "uri" , extras);
182 
183         assertThat(result.getBoolean(EXTRA_SWITCH_CHECKED_STATE)).isTrue();
184 
185         controller.setChecked(false);
186         result = mSwitchesProvider.call(METHOD_IS_CHECKED, "uri" , extras);
187 
188         assertThat(result.getBoolean(EXTRA_SWITCH_CHECKED_STATE)).isFalse();
189     }
190 
191     @Test
getProviderIcon_noImplementInterface_shouldReturnNull()192     public void getProviderIcon_noImplementInterface_shouldReturnNull() {
193         final Bundle extras = new Bundle();
194         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
195         final TestSwitchController controller = new TestSwitchController();
196         controller.setKey("123");
197         controller.setMetaData(new MetaData("category"));
198         mSwitchesProvider.addSwitchController(controller);
199         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
200 
201         final Bundle iconBundle = mSwitchesProvider.call(METHOD_GET_PROVIDER_ICON, "uri" , extras);
202 
203         assertThat(iconBundle).isNull();
204     }
205 
206     @Test
getProviderIcon_implementInterface_shouldReturnIcon()207     public void getProviderIcon_implementInterface_shouldReturnIcon() {
208         final Bundle extras = new Bundle();
209         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
210         final TestSwitchController controller = new TestDynamicSwitchController();
211         controller.setKey("123");
212         controller.setMetaData(new MetaData("category"));
213         mSwitchesProvider.addSwitchController(controller);
214         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
215 
216         final Bundle iconBundle = mSwitchesProvider.call(METHOD_GET_PROVIDER_ICON, "uri" , extras);
217 
218         assertThat(iconBundle).isEqualTo(TestDynamicSwitchController.ICON_BUNDLE);
219     }
220 
221     @Test
getDynamicTitle_noImplementInterface_shouldReturnNull()222     public void getDynamicTitle_noImplementInterface_shouldReturnNull() {
223         final Bundle extras = new Bundle();
224         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
225         final TestSwitchController controller = new TestSwitchController();
226         controller.setKey("123");
227         controller.setMetaData(new MetaData("category"));
228         mSwitchesProvider.addSwitchController(controller);
229         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
230 
231         final Bundle result = mSwitchesProvider.call(METHOD_GET_DYNAMIC_TITLE, "uri" , extras);
232 
233         assertThat(result).isNull();
234     }
235 
236     @Test
getDynamicTitle_implementInterface_shouldReturnTitle()237     public void getDynamicTitle_implementInterface_shouldReturnTitle() {
238         final Bundle extras = new Bundle();
239         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
240         final TestSwitchController controller = new TestDynamicSwitchController();
241         controller.setKey("123");
242         controller.setMetaData(new MetaData("category"));
243         mSwitchesProvider.addSwitchController(controller);
244         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
245 
246         final Bundle result = mSwitchesProvider.call(METHOD_GET_DYNAMIC_TITLE, "uri" , extras);
247 
248         assertThat(result.getString(META_DATA_PREFERENCE_TITLE))
249                 .isEqualTo(TestDynamicSwitchController.TITLE);
250     }
251 
252     @Test
getDynamicSummary_noImplementInterface_shouldReturnNull()253     public void getDynamicSummary_noImplementInterface_shouldReturnNull() {
254         final Bundle extras = new Bundle();
255         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
256         final TestSwitchController controller = new TestSwitchController();
257         controller.setKey("123");
258         controller.setMetaData(new MetaData("category"));
259         mSwitchesProvider.addSwitchController(controller);
260         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
261 
262         final Bundle result = mSwitchesProvider.call(METHOD_GET_DYNAMIC_SUMMARY, "uri" , extras);
263 
264         assertThat(result).isNull();
265     }
266 
267     @Test
getDynamicSummary_implementInterface_shouldReturnSummary()268     public void getDynamicSummary_implementInterface_shouldReturnSummary() {
269         final Bundle extras = new Bundle();
270         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
271         final TestSwitchController controller = new TestDynamicSwitchController();
272         controller.setKey("123");
273         controller.setMetaData(new MetaData("category"));
274         mSwitchesProvider.addSwitchController(controller);
275         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
276 
277         final Bundle result = mSwitchesProvider.call(METHOD_GET_DYNAMIC_SUMMARY, "uri" , extras);
278 
279         assertThat(result.getString(META_DATA_PREFERENCE_SUMMARY))
280                 .isEqualTo(TestDynamicSwitchController.SUMMARY);
281     }
282 
283     @Test
onCheckedChangedSuccess_shouldReturnNoError()284     public void onCheckedChangedSuccess_shouldReturnNoError() {
285         final Bundle extras = new Bundle();
286         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
287         final TestSwitchController controller = new TestSwitchController();
288         controller.setKey("123");
289         controller.setMetaData(new MetaData("category"));
290         mSwitchesProvider.addSwitchController(controller);
291         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
292 
293         final Bundle result = mSwitchesProvider.call(METHOD_ON_CHECKED_CHANGED, "uri" , extras);
294 
295         assertThat(result.getBoolean(EXTRA_SWITCH_SET_CHECKED_ERROR)).isFalse();
296     }
297 
298     @Test
onCheckedChangedFailed_shouldReturnErrorMessage()299     public void onCheckedChangedFailed_shouldReturnErrorMessage() {
300         final Bundle extras = new Bundle();
301         extras.putString(META_DATA_PREFERENCE_KEYHINT, "123");
302         final TestSwitchController controller = new TestSwitchController();
303         controller.setKey("123");
304         controller.setMetaData(new MetaData("category"));
305         controller.setErrorMessage("error");
306         mSwitchesProvider.addSwitchController(controller);
307         mSwitchesProvider.attachInfo(mContext, mProviderInfo);
308 
309         final Bundle result = mSwitchesProvider.call(METHOD_ON_CHECKED_CHANGED, "uri" , extras);
310 
311         assertThat(result.getBoolean(EXTRA_SWITCH_SET_CHECKED_ERROR)).isTrue();
312         assertThat(result.getString(EXTRA_SWITCH_SET_CHECKED_ERROR_MESSAGE)).isEqualTo("error");
313     }
314 
315     private class TestSwitchesProvider extends SwitchesProvider {
316 
317         private List<SwitchController> mControllers;
318 
319         @Override
createSwitchControllers()320         protected List<SwitchController> createSwitchControllers() {
321             return mControllers;
322         }
323 
addSwitchController(SwitchController controller)324         void addSwitchController(SwitchController controller) {
325             if (mControllers == null) {
326                 mControllers = new ArrayList<>();
327             }
328             mControllers.add(controller);
329         }
330     }
331 
332     private static class TestSwitchController extends SwitchController {
333 
334         private String mKey;
335         private MetaData mMetaData;
336         private boolean mChecked;
337         private String mErrorMsg;
338 
339         @Override
getSwitchKey()340         public String getSwitchKey() {
341             return mKey;
342         }
343 
344         @Override
getMetaData()345         protected MetaData getMetaData() {
346             return mMetaData;
347         }
348 
349         @Override
isChecked()350         protected boolean isChecked() {
351             return mChecked;
352         }
353 
354         @Override
onCheckedChanged(boolean checked)355         protected boolean onCheckedChanged(boolean checked) {
356             return mErrorMsg == null ? true : false;
357         }
358 
359         @Override
getErrorMessage(boolean attemptedChecked)360         protected String getErrorMessage(boolean attemptedChecked) {
361             return mErrorMsg;
362         }
363 
setKey(String key)364         void setKey(String key) {
365             mKey = key;
366         }
367 
setMetaData(MetaData metaData)368         void setMetaData(MetaData metaData) {
369             mMetaData = metaData;
370         }
371 
setChecked(boolean checked)372         void setChecked(boolean checked) {
373             mChecked = checked;
374         }
375 
setErrorMessage(String errorMsg)376         void setErrorMessage(String errorMsg) {
377             mErrorMsg = errorMsg;
378         }
379     }
380 
381     private static class TestDynamicSwitchController extends TestSwitchController
382             implements ProviderIcon, DynamicTitle, DynamicSummary {
383 
384         static final String TITLE = "title";
385         static final String SUMMARY = "summary";
386         static final Bundle ICON_BUNDLE = new Bundle();
387 
388         @Override
getProviderIcon()389         public Bundle getProviderIcon() {
390             return ICON_BUNDLE;
391         }
392 
393         @Override
getDynamicTitle()394         public String getDynamicTitle() {
395             return TITLE;
396         }
397 
398         @Override
getDynamicSummary()399         public String getDynamicSummary() {
400             return SUMMARY;
401         }
402     }
403 }
404