• 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 com.google.common.truth.Truth.assertThat;
20 
21 import com.android.settings.TestConfig;
22 import com.android.settings.testutils.SettingsRobolectricTestRunner;
23 import com.android.settings.testutils.shadow.ShadowUtils;
24 import com.android.settings.widget.SwitchBar;
25 import com.android.settingslib.core.lifecycle.Lifecycle;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.RuntimeEnvironment;
34 import org.robolectric.annotation.Config;
35 import org.robolectric.util.ReflectionHelpers;
36 
37 import java.util.ArrayList;
38 
39 @RunWith(SettingsRobolectricTestRunner.class)
40 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
41         shadows = {
42                 ShadowUtils.class
43         })
44 public class DevelopmentSwitchBarControllerTest {
45 
46     @Mock
47     private DevelopmentSettings mSettings;
48     private Lifecycle mLifecycle;
49     private SwitchBar mSwitchBar;
50     private DevelopmentSwitchBarController mController;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         mLifecycle = new Lifecycle();
56         mSwitchBar = new SwitchBar(RuntimeEnvironment.application);
57     }
58 
59     @After
tearDown()60     public void tearDown() {
61         ShadowUtils.reset();
62     }
63 
64     @Test
runThroughLifecycle_isMonkeyRun_shouldNotRegisterListener()65     public void runThroughLifecycle_isMonkeyRun_shouldNotRegisterListener() {
66         ShadowUtils.setIsUserAMonkey(true);
67         mController = new DevelopmentSwitchBarController(mSettings, mSwitchBar,
68                 true /* isAvailable */, mLifecycle);
69         final ArrayList<SwitchBar.OnSwitchChangeListener> listeners =
70                 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners");
71 
72         mLifecycle.onStart();
73         assertThat(listeners).doesNotContain(mSettings);
74 
75         mLifecycle.onStop();
76         assertThat(listeners).doesNotContain(mSettings);
77     }
78 
79     @Test
runThroughLifecycle_isNotMonkeyRun_shouldRegisterAndRemoveListener()80     public void runThroughLifecycle_isNotMonkeyRun_shouldRegisterAndRemoveListener() {
81         ShadowUtils.setIsUserAMonkey(false);
82         mController = new DevelopmentSwitchBarController(mSettings, mSwitchBar,
83                 true /* isAvailable */, mLifecycle);
84         final ArrayList<SwitchBar.OnSwitchChangeListener> listeners =
85                 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners");
86 
87         mLifecycle.onStart();
88         assertThat(listeners).contains(mSettings);
89 
90         mLifecycle.onStop();
91         assertThat(listeners).doesNotContain(mSettings);
92     }
93 
94     @Test
buildController_unavailable_shouldDisableSwitchBar()95     public void buildController_unavailable_shouldDisableSwitchBar() {
96         ShadowUtils.setIsUserAMonkey(false);
97         mController = new DevelopmentSwitchBarController(mSettings, mSwitchBar,
98                 false /* isAvailable */, mLifecycle);
99 
100         assertThat(mSwitchBar.isEnabled()).isFalse();
101     }
102 }
103