1 /*
2  * Copyright (C) 2019 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 android.provider.Settings.Global.DEVELOPMENT_ENABLE_NON_RESIZABLE_MULTI_WINDOW;
20 
21 import static com.android.settings.development.NonResizableMultiWindowPreferenceController.SETTING_VALUE_OFF;
22 import static com.android.settings.development.NonResizableMultiWindowPreferenceController.SETTING_VALUE_ON;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.Context;
30 import android.provider.Settings;
31 
32 import androidx.preference.PreferenceScreen;
33 import androidx.preference.SwitchPreference;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 
43 @RunWith(RobolectricTestRunner.class)
44 public class NonResizableMultiWindowPreferenceControllerTest {
45 
46     @Mock
47     private SwitchPreference mPreference;
48     @Mock
49     private PreferenceScreen mScreen;
50 
51     private Context mContext;
52     private NonResizableMultiWindowPreferenceController mController;
53 
54     @Before
setup()55     public void setup() {
56         MockitoAnnotations.initMocks(this);
57         mContext = RuntimeEnvironment.application;
58         mController = new NonResizableMultiWindowPreferenceController(mContext);
59         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
60         mController.displayPreference(mScreen);
61     }
62 
63     @Test
onPreferenceChange_switchEnabled_shouldEnableNonResizableMultiWindow()64     public void onPreferenceChange_switchEnabled_shouldEnableNonResizableMultiWindow() {
65         mController.onPreferenceChange(mPreference, true /* new value */);
66 
67         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
68                 DEVELOPMENT_ENABLE_NON_RESIZABLE_MULTI_WINDOW, -1 /* default */);
69         assertThat(mode).isEqualTo(SETTING_VALUE_ON);
70     }
71 
72     @Test
onPreferenceChange_switchDisabled_shouldDisableNonResizableMultiWindow()73     public void onPreferenceChange_switchDisabled_shouldDisableNonResizableMultiWindow() {
74         mController.onPreferenceChange(mPreference, false /* new value */);
75 
76         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
77                 DEVELOPMENT_ENABLE_NON_RESIZABLE_MULTI_WINDOW, -1 /* default */);
78         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
79     }
80 
81     @Test
updateState_settingEnabled_preferenceShouldBeChecked()82     public void updateState_settingEnabled_preferenceShouldBeChecked() {
83         Settings.Global.putInt(mContext.getContentResolver(),
84                 DEVELOPMENT_ENABLE_NON_RESIZABLE_MULTI_WINDOW, SETTING_VALUE_ON);
85 
86         mController.updateState(mPreference);
87 
88         verify(mPreference).setChecked(true);
89     }
90 
91     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()92     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
93         Settings.Global.putInt(mContext.getContentResolver(),
94                 DEVELOPMENT_ENABLE_NON_RESIZABLE_MULTI_WINDOW, SETTING_VALUE_OFF);
95 
96         mController.updateState(mPreference);
97 
98         verify(mPreference).setChecked(false);
99     }
100 
101     @Test
onDeveloperOptionsSwitchDisabled_shouldDisablePreference()102     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
103         mController.onDeveloperOptionsSwitchDisabled();
104 
105         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
106                 DEVELOPMENT_ENABLE_NON_RESIZABLE_MULTI_WINDOW, -1 /* default */);
107         assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
108         verify(mPreference).setEnabled(false);
109     }
110 }
111