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