1 /* 2 * Copyright (C) 2017 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.settings.development; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_START; 20 import static androidx.lifecycle.Lifecycle.Event.ON_STOP; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.widget.CompoundButton.OnCheckedChangeListener; 28 29 import androidx.lifecycle.LifecycleOwner; 30 31 import com.android.settings.testutils.shadow.ShadowUserManager; 32 import com.android.settings.testutils.shadow.ShadowUtils; 33 import com.android.settings.widget.SettingsMainSwitchBar; 34 import com.android.settingslib.core.lifecycle.Lifecycle; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Ignore; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.annotation.Config; 46 import org.robolectric.util.ReflectionHelpers; 47 48 import java.util.List; 49 50 @RunWith(RobolectricTestRunner.class) 51 @Config(shadows = {ShadowUtils.class, ShadowUserManager.class}) 52 public class DevelopmentSwitchBarControllerTest { 53 54 @Mock 55 private DevelopmentSettingsDashboardFragment mSettings; 56 private LifecycleOwner mLifecycleOwner; 57 private Lifecycle mLifecycle; 58 private SettingsMainSwitchBar mSwitchBar; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 final Context context = RuntimeEnvironment.application; 64 ShadowUserManager.getShadow().setIsAdminUser(true); 65 mLifecycleOwner = () -> mLifecycle; 66 mLifecycle = new Lifecycle(mLifecycleOwner); 67 mSwitchBar = new SettingsMainSwitchBar(context); 68 when(mSettings.getContext()).thenReturn(context); 69 } 70 71 @After tearDown()72 public void tearDown() { 73 ShadowUtils.reset(); 74 } 75 76 @Test runThroughLifecycle_v2_isMonkeyRun_shouldNotRegisterListener()77 public void runThroughLifecycle_v2_isMonkeyRun_shouldNotRegisterListener() { 78 ShadowUtils.setIsUserAMonkey(true); 79 new DevelopmentSwitchBarController(mSettings, mSwitchBar, 80 true /* isAvailable */, mLifecycle); 81 final List<OnCheckedChangeListener> listeners = 82 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners"); 83 84 mLifecycle.handleLifecycleEvent(ON_START); 85 assertThat(listeners).doesNotContain(mSettings); 86 87 mLifecycle.handleLifecycleEvent(ON_STOP); 88 assertThat(listeners).doesNotContain(mSettings); 89 } 90 91 @Test runThroughLifecycle_isNotMonkeyRun_shouldRegisterAndRemoveListener()92 public void runThroughLifecycle_isNotMonkeyRun_shouldRegisterAndRemoveListener() { 93 ShadowUtils.setIsUserAMonkey(false); 94 new DevelopmentSwitchBarController(mSettings, mSwitchBar, 95 true /* isAvailable */, mLifecycle); 96 final List<OnCheckedChangeListener> listeners = 97 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners"); 98 99 mLifecycle.handleLifecycleEvent(ON_START); 100 assertThat(listeners).contains(mSettings); 101 102 mLifecycle.handleLifecycleEvent(ON_STOP); 103 assertThat(listeners).doesNotContain(mSettings); 104 } 105 106 @Test runThroughLifecycle_v2_isNotMonkeyRun_shouldRegisterAndRemoveListener()107 public void runThroughLifecycle_v2_isNotMonkeyRun_shouldRegisterAndRemoveListener() { 108 when(mSettings.getContext()).thenReturn(RuntimeEnvironment.application); 109 ShadowUtils.setIsUserAMonkey(false); 110 new DevelopmentSwitchBarController(mSettings, mSwitchBar, 111 true /* isAvailable */, mLifecycle); 112 final List<OnCheckedChangeListener> listeners = 113 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners"); 114 115 mLifecycle.handleLifecycleEvent(ON_START); 116 assertThat(listeners).contains(mSettings); 117 118 mLifecycle.handleLifecycleEvent(ON_STOP); 119 assertThat(listeners).doesNotContain(mSettings); 120 } 121 122 @Test 123 @Ignore buildController_unavailable_shouldDisableSwitchBar()124 public void buildController_unavailable_shouldDisableSwitchBar() { 125 ShadowUtils.setIsUserAMonkey(false); 126 new DevelopmentSwitchBarController(mSettings, mSwitchBar, 127 false /* isAvailable */, mLifecycle); 128 129 assertThat(mSwitchBar.isEnabled()).isFalse(); 130 } 131 } 132