• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.settings.location;
18 
19 import static com.android.car.settings.common.PreferenceController.AVAILABLE;
20 import static com.android.car.settings.common.PreferenceController.CONDITIONALLY_UNAVAILABLE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.anyBoolean;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.any;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 import static org.mockito.Mockito.withSettings;
35 
36 import android.app.AlertDialog;
37 import android.car.drivingstate.CarUxRestrictions;
38 import android.content.BroadcastReceiver;
39 import android.content.Context;
40 import android.content.DialogInterface;
41 import android.content.Intent;
42 import android.content.IntentFilter;
43 import android.location.LocationManager;
44 import android.platform.test.annotations.DisableFlags;
45 import android.platform.test.annotations.EnableFlags;
46 import android.platform.test.flag.junit.SetFlagsRule;
47 import android.widget.Toast;
48 
49 import androidx.lifecycle.LifecycleOwner;
50 import androidx.test.core.app.ApplicationProvider;
51 import androidx.test.ext.junit.runners.AndroidJUnit4;
52 import androidx.test.platform.app.InstrumentationRegistry;
53 import androidx.test.rule.ActivityTestRule;
54 
55 import com.android.car.settings.Flags;
56 import com.android.car.settings.R;
57 import com.android.car.settings.common.ConfirmationDialogFragment;
58 import com.android.car.settings.common.FragmentController;
59 import com.android.car.settings.common.PreferenceControllerTestUtil;
60 import com.android.car.settings.testutils.BaseCarSettingsTestActivity;
61 import com.android.car.settings.testutils.TestLifecycleOwner;
62 import com.android.car.ui.preference.CarUiSwitchPreference;
63 import com.android.dx.mockito.inline.extended.ExtendedMockito;
64 
65 import org.junit.After;
66 import org.junit.Before;
67 import org.junit.Rule;
68 import org.junit.Test;
69 import org.junit.runner.RunWith;
70 import org.mockito.ArgumentCaptor;
71 import org.mockito.Mock;
72 import org.mockito.MockitoAnnotations;
73 import org.mockito.MockitoSession;
74 
75 import java.util.List;
76 
77 @RunWith(AndroidJUnit4.class)
78 public class AdasLocationSwitchPreferenceControllerTest {
79     private LifecycleOwner mLifecycleOwner;
80     private Context mContext = spy(ApplicationProvider.getApplicationContext());
81     private CarUiSwitchPreference mSwitchPreference;
82     private AdasLocationSwitchPreferenceController mPreferenceController;
83     private CarUxRestrictions mCarUxRestrictions;
84     private MockitoSession mSession;
85 
86     @Mock
87     private FragmentController mFragmentController;
88 
89     @Mock
90     private LocationManager mLocationManager;
91 
92     @Mock
93     private Toast mToast;
94 
95     @Rule
96     public ActivityTestRule<BaseCarSettingsTestActivity> mActivityTestRule =
97             new ActivityTestRule<>(BaseCarSettingsTestActivity.class);
98 
99     @Rule
100     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
101 
102     @Before
setUp()103     public void setUp() {
104         mLifecycleOwner = new TestLifecycleOwner();
105         MockitoAnnotations.initMocks(this);
106 
107         when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager);
108 
109         mSession = ExtendedMockito.mockitoSession().mockStatic(Toast.class,
110                 withSettings().lenient()).startMocking();
111 
112         mCarUxRestrictions = new CarUxRestrictions.Builder(/* reqOpt= */ true,
113                 CarUxRestrictions.UX_RESTRICTIONS_BASELINE, /* timestamp= */ 0).build();
114 
115         mSwitchPreference = new CarUiSwitchPreference(mContext);
116     }
117 
118     @After
tearDown()119     public void tearDown() {
120         mSession.finishMocking();
121     }
122 
123     @Test
124     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
unclickable_switchDisabled()125     public void unclickable_switchDisabled() throws Throwable {
126         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
127         setIsClickable(false);
128 
129         assertThat(mSwitchPreference.isEnabled()).isFalse();
130         assertThat(mSwitchPreference.isChecked()).isTrue();
131     }
132 
133     @Test
134     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
notVisible_switchHidden()135     public void notVisible_switchHidden() throws Throwable {
136         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
137         setIsVisible(false);
138 
139         PreferenceControllerTestUtil.assertAvailability(
140                 mPreferenceController.getAvailabilityStatus(), CONDITIONALLY_UNAVAILABLE);
141     }
142 
143     @Test
144     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
isVisible_switchShown()145     public void isVisible_switchShown() throws Throwable {
146         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
147         setIsVisible(true);
148 
149         PreferenceControllerTestUtil.assertAvailability(
150                 mPreferenceController.getAvailabilityStatus(), AVAILABLE);
151     }
152 
153     @Test
154     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
unclickable_onPreferenceClicked_noChange_showToggleDisabledDialog()155     public void unclickable_onPreferenceClicked_noChange_showToggleDisabledDialog() {
156         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
157         setIsClickable(false);
158 
159         mSwitchPreference.performClick();
160 
161         assertThat(mSwitchPreference.isEnabled()).isFalse();
162         assertThat(mSwitchPreference.isChecked()).isTrue();
163         verify(mLocationManager, never()).setAdasGnssLocationEnabled(anyBoolean());
164         verify(mFragmentController)
165                 .showDialog(any(ConfirmationDialogFragment.class),
166                         eq(ConfirmationDialogFragment.TAG));
167     }
168 
169     @Test
170     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
unclickable_powerPolicyOff_onPreferenceClicked_showToggleDisabledDialog()171     public void unclickable_powerPolicyOff_onPreferenceClicked_showToggleDisabledDialog() {
172         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
173         setIsClickable(false);
174         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
175                 .handlePolicyChange(/* isOn= */ false);
176 
177         mSwitchPreference.performClick();
178 
179         verify(mLocationManager, never()).setAdasGnssLocationEnabled(anyBoolean());
180         verify(mFragmentController)
181                 .showDialog(any(ConfirmationDialogFragment.class),
182                         eq(ConfirmationDialogFragment.TAG));
183     }
184 
185     @Test
186     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
powerPolicyOff_onPreferenceClicked_showCorrectToast()187     public void powerPolicyOff_onPreferenceClicked_showCorrectToast() throws Throwable {
188         int correctToastId = R.string.power_component_disabled;
189         mActivityTestRule.runOnUiThread(() -> {
190             ExtendedMockito.when(Toast.makeText(any(), eq(correctToastId), anyInt()))
191                     .thenReturn(mToast);
192         });
193         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
194         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
195         setIsClickable(true);
196         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
197                 .handlePolicyChange(/* isOn= */ false);
198 
199         mSwitchPreference.performClick();
200 
201         verify(mToast).show();
202     }
203 
204     @Test
205     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
powerPolicyOff_onPreferenceClicked_showCorrectToast_flagReq()206     public void powerPolicyOff_onPreferenceClicked_showCorrectToast_flagReq() throws Throwable {
207         int correctToastId = R.string.power_component_disabled;
208         mActivityTestRule.runOnUiThread(() -> {
209             ExtendedMockito.when(Toast.makeText(any(), eq(correctToastId), anyInt()))
210                     .thenReturn(mToast);
211         });
212         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
213         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
214         setIsVisible(true);
215         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
216                 .handlePolicyChange(/* isOn= */ false);
217 
218         mSwitchPreference.performClick();
219 
220         verify(mToast).show();
221     }
222 
223     @Test
224     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onAdasIntentReceived_updateUi()225     public void onAdasIntentReceived_updateUi() {
226         initializePreference(/* isAdasLocationEnabled= */false, /* isMainLocationEnabled= */false);
227         setIsClickable(true);
228 
229         ArgumentCaptor<BroadcastReceiver> broadcastReceiverArgumentCaptor = ArgumentCaptor.forClass(
230                 BroadcastReceiver.class);
231         ArgumentCaptor<IntentFilter> intentFilterCaptor = ArgumentCaptor.forClass(
232                 IntentFilter.class);
233         verify(mContext, times(1))
234                 .registerReceiver(broadcastReceiverArgumentCaptor.capture(),
235                         intentFilterCaptor.capture(), eq(Context.RECEIVER_NOT_EXPORTED));
236         List<IntentFilter> actions = intentFilterCaptor.getAllValues();
237         assertTrue(actions.get(0).hasAction(LocationManager.ACTION_ADAS_GNSS_ENABLED_CHANGED));
238 
239         when(mLocationManager.isAdasGnssLocationEnabled()).thenReturn(true);
240         broadcastReceiverArgumentCaptor.getValue().onReceive(mContext,
241                 new Intent(LocationManager.ACTION_ADAS_GNSS_ENABLED_CHANGED));
242 
243         assertThat(mSwitchPreference.isEnabled()).isTrue();
244         assertThat(mSwitchPreference.isChecked()).isTrue();
245     }
246 
247     @Test
248     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onAdasIntentReceived_updateUi_flagReq()249     public void onAdasIntentReceived_updateUi_flagReq() {
250         initializePreference(/* isAdasLocationEnabled= */false, /* isMainLocationEnabled= */false);
251         setIsVisible(true);
252 
253         ArgumentCaptor<BroadcastReceiver> broadcastReceiverArgumentCaptor = ArgumentCaptor.forClass(
254                 BroadcastReceiver.class);
255         ArgumentCaptor<IntentFilter> intentFilterCaptor = ArgumentCaptor.forClass(
256                 IntentFilter.class);
257         verify(mContext, times(1))
258                 .registerReceiver(broadcastReceiverArgumentCaptor.capture(),
259                         intentFilterCaptor.capture(), eq(Context.RECEIVER_NOT_EXPORTED));
260         List<IntentFilter> actions = intentFilterCaptor.getAllValues();
261         assertTrue(actions.get(0).hasAction(LocationManager.ACTION_ADAS_GNSS_ENABLED_CHANGED));
262 
263         when(mLocationManager.isAdasGnssLocationEnabled()).thenReturn(true);
264         broadcastReceiverArgumentCaptor.getValue().onReceive(mContext,
265                 new Intent(LocationManager.ACTION_ADAS_GNSS_ENABLED_CHANGED));
266 
267         assertThat(mSwitchPreference.isEnabled()).isTrue();
268         assertThat(mSwitchPreference.isChecked()).isTrue();
269     }
270 
271     @Test
272     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onLocationIntentReceived_updateUi()273     public void onLocationIntentReceived_updateUi() {
274         initializePreference(/* isAdasLocationEnabled= */false, /* isMainLocationEnabled= */false);
275         setIsClickable(true);
276 
277         ArgumentCaptor<BroadcastReceiver> broadcastReceiverArgumentCaptor = ArgumentCaptor.forClass(
278                 BroadcastReceiver.class);
279         ArgumentCaptor<IntentFilter> intentFilterCaptor = ArgumentCaptor.forClass(
280                 IntentFilter.class);
281 
282         verify(mContext, times(1))
283                 .registerReceiver(broadcastReceiverArgumentCaptor.capture(),
284                         intentFilterCaptor.capture(), eq(Context.RECEIVER_NOT_EXPORTED));
285         List<IntentFilter> actions = intentFilterCaptor.getAllValues();
286         assertTrue(actions.get(0).hasAction(LocationManager.MODE_CHANGED_ACTION));
287 
288         when(mLocationManager.isLocationEnabled()).thenReturn(true);
289         when(mLocationManager.isAdasGnssLocationEnabled()).thenReturn(true);
290         broadcastReceiverArgumentCaptor.getValue().onReceive(mContext,
291                 new Intent(LocationManager.MODE_CHANGED_ACTION));
292 
293         assertThat(mSwitchPreference.isEnabled()).isFalse();
294         assertThat(mSwitchPreference.isChecked()).isTrue();
295     }
296 
297     @Test
298     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onLocationIntentReceived_updateUi_flagReq()299     public void onLocationIntentReceived_updateUi_flagReq() {
300         initializePreference(/* isAdasLocationEnabled= */false, /* isMainLocationEnabled= */false);
301         setIsVisible(true);
302 
303         ArgumentCaptor<BroadcastReceiver> broadcastReceiverArgumentCaptor = ArgumentCaptor.forClass(
304                 BroadcastReceiver.class);
305         ArgumentCaptor<IntentFilter> intentFilterCaptor = ArgumentCaptor.forClass(
306                 IntentFilter.class);
307 
308         verify(mContext, times(1))
309                 .registerReceiver(broadcastReceiverArgumentCaptor.capture(),
310                         intentFilterCaptor.capture(), eq(Context.RECEIVER_NOT_EXPORTED));
311         List<IntentFilter> actions = intentFilterCaptor.getAllValues();
312         assertTrue(actions.get(0).hasAction(LocationManager.MODE_CHANGED_ACTION));
313 
314         when(mLocationManager.isLocationEnabled()).thenReturn(true);
315         when(mLocationManager.isAdasGnssLocationEnabled()).thenReturn(true);
316         broadcastReceiverArgumentCaptor.getValue().onReceive(mContext,
317                 new Intent(LocationManager.MODE_CHANGED_ACTION));
318 
319         assertThat(mSwitchPreference.isEnabled()).isFalse();
320         assertThat(mSwitchPreference.isChecked()).isTrue();
321     }
322 
323     @Test
324     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPreferenceClicked_adasDisabled_shouldEnable_notShowDialog()325     public void onPreferenceClicked_adasDisabled_shouldEnable_notShowDialog() {
326         initializePreference(/* isAdasLocationEnabled= */false, /* isMainLocationEnabled= */false);
327         setIsClickable(true);
328 
329         mSwitchPreference.performClick();
330 
331         assertThat(mSwitchPreference.isEnabled()).isTrue();
332         assertThat(mSwitchPreference.isChecked()).isTrue();
333         verify(mLocationManager).setAdasGnssLocationEnabled(true);
334         verify(mFragmentController, never())
335                 .showDialog(any(ConfirmationDialogFragment.class), any());
336     }
337 
338     @Test
339     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPreferenceClicked_adasDisabled_shouldEnable()340     public void onPreferenceClicked_adasDisabled_shouldEnable() {
341         initializePreference(/* isAdasLocationEnabled= */false, /* isMainLocationEnabled= */false);
342         setIsVisible(true);
343 
344         mSwitchPreference.performClick();
345 
346         assertThat(mSwitchPreference.isEnabled()).isTrue();
347         assertThat(mSwitchPreference.isChecked()).isTrue();
348         verify(mLocationManager).setAdasGnssLocationEnabled(true);
349     }
350 
351     @Test
352     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPreferenceClicked_adasEnabled_shouldStayEnable_showDialog()353     public void onPreferenceClicked_adasEnabled_shouldStayEnable_showDialog() {
354         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
355         setIsClickable(true);
356 
357         mSwitchPreference.performClick();
358 
359         assertThat(mSwitchPreference.isEnabled()).isTrue();
360         assertThat(mSwitchPreference.isChecked()).isTrue();
361         verify(mLocationManager, never()).setLocationEnabledForUser(anyBoolean(), any());
362         verify(mFragmentController)
363                 .showDialog(any(ConfirmationDialogFragment.class),
364                         eq(ConfirmationDialogFragment.TAG));
365     }
366 
367     @Test
368     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPreferenceClicked_adasEnabled_shouldDisable_flagReq()369     public void onPreferenceClicked_adasEnabled_shouldDisable_flagReq() {
370         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
371         setIsVisible(true);
372 
373         mSwitchPreference.performClick();
374 
375         assertThat(mSwitchPreference.isEnabled()).isTrue();
376         assertThat(mSwitchPreference.isChecked()).isFalse();
377         verify(mLocationManager).setAdasGnssLocationEnabled(false);
378     }
379 
380     @Test
381     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
confirmDialog_turnOffDriverAssistance()382     public void confirmDialog_turnOffDriverAssistance() throws Throwable {
383         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
384         setIsClickable(true);
385 
386         mSwitchPreference.performClick();
387 
388         // Capture the dialog that is shown on toggle.
389         ArgumentCaptor<ConfirmationDialogFragment> dialogCaptor = ArgumentCaptor.forClass(
390                 ConfirmationDialogFragment.class);
391         verify(mFragmentController).showDialog(dialogCaptor.capture(),
392                 eq(ConfirmationDialogFragment.TAG));
393 
394         // Show the captured dialog on press the confirmation button.
395         ConfirmationDialogFragment dialog = dialogCaptor.getValue();
396         assertThat(dialogCaptor).isNotNull();
397         AlertDialog alertDialog = showDialog(dialog);
398 
399         // Confirm action is the listener of negative button.
400         mActivityTestRule.runOnUiThread(() -> {
401             alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
402         });
403         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
404 
405         verify(mLocationManager).setAdasGnssLocationEnabled(false);
406     }
407 
408     @Test
409     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPowerPolicyChange_isEnabledChanges()410     public void onPowerPolicyChange_isEnabledChanges() {
411         initializePreference(/* isAdasLocationEnabled= */ true, /* isMainLocationEnabled= */ false);
412         setIsClickable(true);
413 
414         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
415                 .handlePolicyChange(/* isOn= */ false);
416 
417         assertThat(mSwitchPreference.isEnabled()).isFalse();
418 
419         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
420                 .handlePolicyChange(/* isOn= */ true);
421 
422         assertThat(mSwitchPreference.isEnabled()).isTrue();
423     }
424 
425     @Test
426     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPowerPolicyChange_isEnabledChanges_flagReq()427     public void onPowerPolicyChange_isEnabledChanges_flagReq() {
428         initializePreference(/* isAdasLocationEnabled= */ true, /* isMainLocationEnabled= */ false);
429         setIsVisible(true);
430 
431         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
432                 .handlePolicyChange(/* isOn= */ false);
433 
434         assertThat(mSwitchPreference.isEnabled()).isFalse();
435 
436         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
437                 .handlePolicyChange(/* isOn= */ true);
438 
439         assertThat(mSwitchPreference.isEnabled()).isTrue();
440     }
441 
442     @Test
443     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPowerPolicyChange_enabled_locationEnabled_switchStaysDisabled()444     public void onPowerPolicyChange_enabled_locationEnabled_switchStaysDisabled() {
445         initializePreference(/* isAdasLocationEnabled= */ true, /* isMainLocationEnabled= */ true);
446         setIsClickable(true);
447 
448         assertThat(mSwitchPreference.isEnabled()).isFalse();
449 
450         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
451                 .handlePolicyChange(/* isOn= */ true);
452 
453         assertThat(mSwitchPreference.isEnabled()).isFalse();
454     }
455 
456     @Test
457     @EnableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
onPowerPolicyChange_enabled_locationEnabled_switchStaysDisabled_flagReq()458     public void onPowerPolicyChange_enabled_locationEnabled_switchStaysDisabled_flagReq() {
459         initializePreference(/* isAdasLocationEnabled= */ true, /* isMainLocationEnabled= */ true);
460         setIsVisible(true);
461 
462         assertThat(mSwitchPreference.isEnabled()).isFalse();
463 
464         mPreferenceController.mPowerPolicyListener.getPolicyChangeHandler()
465                 .handlePolicyChange(/* isOn= */ true);
466 
467         assertThat(mSwitchPreference.isEnabled()).isFalse();
468     }
469 
470     @Test
471     @DisableFlags(Flags.FLAG_REQUIRED_INFOTAINMENT_APPS_SETTINGS_PAGE)
cancelDialog_DriverAssistanceStaysOn()472     public void cancelDialog_DriverAssistanceStaysOn() throws Throwable {
473         initializePreference(/* isAdasLocationEnabled= */true, /* isMainLocationEnabled= */false);
474         setIsClickable(true);
475 
476         mSwitchPreference.performClick();
477 
478         // Capture the dialog that is shown on toggle.
479         ArgumentCaptor<ConfirmationDialogFragment> dialogCaptor = ArgumentCaptor.forClass(
480                 ConfirmationDialogFragment.class);
481         verify(mFragmentController).showDialog(dialogCaptor.capture(),
482                 eq(ConfirmationDialogFragment.TAG));
483 
484         // Show the captured dialog on press the confirmation button.
485         ConfirmationDialogFragment dialog = dialogCaptor.getValue();
486         assertThat(dialogCaptor).isNotNull();
487         AlertDialog alertDialog = showDialog(dialog);
488 
489         // Cancel action is the listener of positive button.
490         mActivityTestRule.runOnUiThread(() -> {
491             alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
492         });
493         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
494 
495         verify(mLocationManager, never()).setAdasGnssLocationEnabled(false);
496     }
497 
initializePreference( boolean isAdasLocationEnabled, boolean isMainLocationEnabled)498     private void initializePreference(
499             boolean isAdasLocationEnabled, boolean isMainLocationEnabled) {
500         when(mLocationManager.isAdasGnssLocationEnabled()).thenReturn(isAdasLocationEnabled);
501         when(mLocationManager.isLocationEnabled()).thenReturn(isMainLocationEnabled);
502         mPreferenceController = new AdasLocationSwitchPreferenceController(mContext,
503                 /* preferenceKey= */ "key", mFragmentController, mCarUxRestrictions);
504         PreferenceControllerTestUtil.assignPreference(mPreferenceController, mSwitchPreference);
505     }
506 
setIsClickable(boolean isClickable)507     private void setIsClickable(boolean isClickable) {
508         mPreferenceController.onCreate(mLifecycleOwner);
509         mPreferenceController.mIsClickable = isClickable;
510         mPreferenceController.onStart(mLifecycleOwner);
511     }
512 
setIsVisible(boolean isVisible)513     private void setIsVisible(boolean isVisible) {
514         mPreferenceController.onCreate(mLifecycleOwner);
515         mPreferenceController.mIsVisible = isVisible;
516         mPreferenceController.onStart(mLifecycleOwner);
517     }
518 
showDialog(ConfirmationDialogFragment dialog)519     private AlertDialog showDialog(ConfirmationDialogFragment dialog) throws Throwable {
520         mActivityTestRule.runOnUiThread(() -> {
521             dialog.show(mActivityTestRule.getActivity().getSupportFragmentManager(),
522                     /* tag= */ null);
523         });
524         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
525         return (AlertDialog) dialog.getDialog();
526     }
527 }
528