• 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 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.os.SystemProperties;
24 import android.support.v14.preference.SwitchPreference;
25 import android.support.v7.preference.PreferenceScreen;
26 import android.view.View;
27 
28 import com.android.settings.testutils.SettingsRobolectricTestRunner;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RuntimeEnvironment;
36 
37 @RunWith(SettingsRobolectricTestRunner.class)
38 public class ShowLayoutBoundsPreferenceControllerTest {
39 
40     @Mock
41     private SwitchPreference mPreference;
42     @Mock
43     private PreferenceScreen mPreferenceScreen;
44 
45     private ShowLayoutBoundsPreferenceController mController;
46 
47     @Before
setup()48     public void setup() {
49         MockitoAnnotations.initMocks(this);
50         mController = new ShowLayoutBoundsPreferenceController(RuntimeEnvironment.application);
51         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
52             .thenReturn(mPreference);
53         mController.displayPreference(mPreferenceScreen);
54     }
55 
56     @Test
onPreferenceChanged_settingEnabled_turnOnShowLayoutBounds()57     public void onPreferenceChanged_settingEnabled_turnOnShowLayoutBounds() {
58         mController.onPreferenceChange(mPreference, true /* new value */);
59 
60         final boolean mode =
61             SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false /* default */);
62 
63         assertThat(mode).isTrue();
64     }
65 
66     @Test
onPreferenceChanged_settingDisabled_turnOffShowLayoutBounds()67     public void onPreferenceChanged_settingDisabled_turnOffShowLayoutBounds() {
68         mController.onPreferenceChange(mPreference, false /* new value */);
69 
70         final boolean mode =
71             SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false /* default */);
72 
73         assertThat(mode).isFalse();
74     }
75 
76     @Test
updateState_settingEnabled_preferenceShouldBeChecked()77     public void updateState_settingEnabled_preferenceShouldBeChecked() {
78         SystemProperties.set(View.DEBUG_LAYOUT_PROPERTY, Boolean.toString(true));
79         mController.updateState(mPreference);
80 
81         verify(mPreference).setChecked(true);
82     }
83 
84     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()85     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
86         SystemProperties.set(View.DEBUG_LAYOUT_PROPERTY, Boolean.toString(false));
87         mController.updateState(mPreference);
88 
89         verify(mPreference).setChecked(false);
90     }
91 
92     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()93     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
94         mController.onDeveloperOptionsDisabled();
95 
96         final boolean mode =
97             SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false /* default */);
98 
99         assertThat(mode).isFalse();
100         verify(mPreference).setEnabled(false);
101         verify(mPreference).setChecked(false);
102     }
103 }
104