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 static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.sysprop.DisplayProperties;
25 import android.view.View;
26 
27 import androidx.preference.PreferenceScreen;
28 import androidx.preference.SwitchPreference;
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.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class ShowLayoutBoundsPreferenceControllerTest {
40 
41     @Mock
42     private SwitchPreference mPreference;
43     @Mock
44     private PreferenceScreen mPreferenceScreen;
45 
46     private ShowLayoutBoundsPreferenceController mController;
47 
48     @Before
setup()49     public void setup() {
50         MockitoAnnotations.initMocks(this);
51         mController = new ShowLayoutBoundsPreferenceController(RuntimeEnvironment.application);
52         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
53             .thenReturn(mPreference);
54         mController.displayPreference(mPreferenceScreen);
55     }
56 
57     @Test
onPreferenceChanged_settingEnabled_turnOnShowLayoutBounds()58     public void onPreferenceChanged_settingEnabled_turnOnShowLayoutBounds() {
59         mController.onPreferenceChange(mPreference, true /* new value */);
60 
61         final boolean mode = DisplayProperties.debug_layout().orElse(false);
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 = DisplayProperties.debug_layout().orElse(false);
71 
72         assertThat(mode).isFalse();
73     }
74 
75     @Test
updateState_settingEnabled_preferenceShouldBeChecked()76     public void updateState_settingEnabled_preferenceShouldBeChecked() {
77         DisplayProperties.debug_layout(true);
78         mController.updateState(mPreference);
79 
80         verify(mPreference).setChecked(true);
81     }
82 
83     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()84     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
85         DisplayProperties.debug_layout(false);
86         mController.updateState(mPreference);
87 
88         verify(mPreference).setChecked(false);
89     }
90 
91     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()92     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
93         mController.onDeveloperOptionsDisabled();
94 
95         final boolean mode = DisplayProperties.debug_layout().orElse(false);
96 
97         assertThat(mode).isFalse();
98         verify(mPreference).setEnabled(false);
99         verify(mPreference).setChecked(false);
100     }
101 }
102