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.ListPreference;
29 import androidx.preference.PreferenceScreen;
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 DebugNonRectClipOperationsPreferenceControllerTest {
41 
42     @Mock
43     private ListPreference mPreference;
44     @Mock
45     private PreferenceScreen mScreen;
46 
47     /**
48      * 0: off
49      * 1: Draw non-rectangular clip region in blue
50      * 2: Highlight tested drawing commands in green
51      */
52     private String[] mListValues;
53     private String[] mListSummaries;
54     private Context mContext;
55     private DebugNonRectClipOperationsPreferenceController mController;
56 
57     @Before
setup()58     public void setup() {
59         MockitoAnnotations.initMocks(this);
60         mContext = RuntimeEnvironment.application;
61         mListValues = mContext.getResources().getStringArray(
62                 com.android.settingslib.R.array.show_non_rect_clip_values);
63         mListSummaries = mContext.getResources().getStringArray(
64                 com.android.settingslib.R.array.show_non_rect_clip_entries);
65         mController = new DebugNonRectClipOperationsPreferenceController(mContext);
66         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
67         mController.displayPreference(mScreen);
68     }
69 
70     @Test
onPreferenceChange_noValueSet_shouldSetEmptyString()71     public void onPreferenceChange_noValueSet_shouldSetEmptyString() {
72         mController.onPreferenceChange(mPreference, null /* new value */);
73 
74         String mode = SystemProperties.get(
75                 ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY);
76         assertThat(mode).isEqualTo("");
77     }
78 
79     @Test
onPreferenceChange_option1Selected_shouldSetOption1()80     public void onPreferenceChange_option1Selected_shouldSetOption1() {
81         mController.onPreferenceChange(mPreference, mListValues[1]);
82 
83         String mode = SystemProperties.get(
84                 ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY);
85         assertThat(mode).isEqualTo(mListValues[1]);
86     }
87 
88     @Test
updateState_option1Set_shouldUpdatePreferenceToOption1()89     public void updateState_option1Set_shouldUpdatePreferenceToOption1() {
90         SystemProperties.set(ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY,
91                 mListValues[1]);
92 
93         mController.updateState(mPreference);
94 
95         verify(mPreference).setValue(mListValues[1]);
96         verify(mPreference).setSummary(mListSummaries[1]);
97     }
98 
99     @Test
updateState_option2Set_shouldUpdatePreferenceToOption2()100     public void updateState_option2Set_shouldUpdatePreferenceToOption2() {
101         SystemProperties.set(ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY,
102                 mListValues[2]);
103 
104         mController.updateState(mPreference);
105 
106         verify(mPreference).setValue(mListValues[2]);
107         verify(mPreference).setSummary(mListSummaries[2]);
108     }
109 
110     @Test
updateState_noOptionSet_shouldDefaultToOption0()111     public void updateState_noOptionSet_shouldDefaultToOption0() {
112         SystemProperties.set(ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY, null);
113 
114         mController.updateState(mPreference);
115 
116         verify(mPreference).setValue(mListValues[0]);
117         verify(mPreference).setSummary(mListSummaries[0]);
118     }
119 }
120