1package com.android.settings.development;
2
3/*
4 * Copyright (C) 2017 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.when;
22
23import android.content.Context;
24import android.provider.Settings;
25import android.support.v14.preference.SwitchPreference;
26import android.support.v7.preference.PreferenceScreen;
27
28import com.android.settings.testutils.SettingsRobolectricTestRunner;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35import org.robolectric.RuntimeEnvironment;
36
37@RunWith(SettingsRobolectricTestRunner.class)
38public class ResizableActivityPreferenceControllerTest {
39
40    @Mock
41    private SwitchPreference mPreference;
42    @Mock
43    private PreferenceScreen mScreen;
44
45    private Context mContext;
46    private ResizableActivityPreferenceController mController;
47
48    @Before
49    public void setup() {
50        MockitoAnnotations.initMocks(this);
51        mContext = RuntimeEnvironment.application;
52        mController = new ResizableActivityPreferenceController(mContext);
53        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
54        mController.displayPreference(mScreen);
55    }
56
57    @Test
58    public void onPreferenceChange_settingEnabled_shouldEnableResizableActivities() {
59        mController.onPreferenceChange(mPreference, true /* new value */);
60
61        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
62                Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, -1 /* default */);
63
64        assertThat(mode).isEqualTo(
65                ResizableActivityPreferenceController.SETTING_VALUE_ON);
66    }
67
68    @Test
69    public void onPreferenceChange_settingDisabled_shouldDisableResizableActivities() {
70        mController.onPreferenceChange(mPreference, false /* new value */);
71
72        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
73                Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, -1 /* default */);
74
75        assertThat(mode).isEqualTo(
76                ResizableActivityPreferenceController.SETTING_VALUE_OFF);
77    }
78
79    @Test
80    public void updateState_settingEnabled_preferenceShouldBeChecked() {
81        Settings.Global.putInt(mContext.getContentResolver(),
82                Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES,
83                ResizableActivityPreferenceController.SETTING_VALUE_ON);
84
85        mController.updateState(mPreference);
86
87        verify(mPreference).setChecked(true);
88    }
89
90    @Test
91    public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
92        Settings.Global.putInt(mContext.getContentResolver(),
93                Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES,
94                ResizableActivityPreferenceController.SETTING_VALUE_OFF);
95
96        mController.updateState(mPreference);
97
98        verify(mPreference).setChecked(false);
99    }
100
101    @Test
102    public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeDisabled() {
103        mController.onDeveloperOptionsSwitchDisabled();
104        final int mode = Settings.Global.getInt(mContext.getContentResolver(),
105                Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, -1 /* default */);
106
107        assertThat(mode).isEqualTo(
108                ResizableActivityPreferenceController.SETTING_VALUE_OFF);
109        verify(mPreference).setChecked(false);
110        verify(mPreference).setEnabled(false);
111    }
112}
113