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