DebugViewAttributesPreferenceControllerTest.java revision 4fbf04cd1056928d1f2ef3c648b11112f80915de
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
17package com.android.settings.development;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.content.Context;
25import android.provider.Settings;
26import android.support.v14.preference.SwitchPreference;
27import android.support.v7.preference.PreferenceScreen;
28
29import com.android.settings.TestConfig;
30import com.android.settings.testutils.SettingsRobolectricTestRunner;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37import org.robolectric.RuntimeEnvironment;
38import org.robolectric.annotation.Config;
39
40@RunWith(SettingsRobolectricTestRunner.class)
41@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
42public class DebugViewAttributesPreferenceControllerTest {
43
44    @Mock
45    private SwitchPreference mPreference;
46    @Mock
47    private PreferenceScreen mPreferenceScreen;
48
49    private Context mContext;
50    private DebugViewAttributesPreferenceController mController;
51
52    @Before
53    public void setup() {
54        MockitoAnnotations.initMocks(this);
55        mContext = RuntimeEnvironment.application;
56        mController = new DebugViewAttributesPreferenceController(mContext);
57        when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
58                mPreference);
59        mController.displayPreference(mPreferenceScreen);
60    }
61
62    @Test
63    public void onPreferenceChanged_turnOnViewAttributes() {
64        mController.onPreferenceChange(null, true);
65
66        final int mode = Settings.System.getInt(mContext.getContentResolver(),
67                Settings.Global.DEBUG_VIEW_ATTRIBUTES, -1);
68
69        assertThat(mode).isEqualTo(DebugViewAttributesPreferenceController.SETTING_VALUE_ON);
70    }
71
72    @Test
73    public void onPreferenceChanged_turnOffViewAttributes() {
74        mController.onPreferenceChange(null, false);
75
76        final int mode = Settings.System.getInt(mContext.getContentResolver(),
77                Settings.Global.DEBUG_VIEW_ATTRIBUTES, -1);
78
79        assertThat(mode).isEqualTo(DebugViewAttributesPreferenceController.SETTING_VALUE_OFF);
80    }
81
82    @Test
83    public void updateState_preferenceShouldBeChecked() {
84        Settings.System.putInt(mContext.getContentResolver(), Settings.Global.DEBUG_VIEW_ATTRIBUTES,
85                DebugViewAttributesPreferenceController.SETTING_VALUE_ON);
86        mController.updateState(mPreference);
87
88        verify(mPreference).setChecked(true);
89    }
90
91    @Test
92    public void updateState_preferenceShouldNotBeChecked() {
93        Settings.System.putInt(mContext.getContentResolver(), Settings.Global.DEBUG_VIEW_ATTRIBUTES,
94                DebugViewAttributesPreferenceController.SETTING_VALUE_OFF);
95        mController.updateState(mPreference);
96
97        verify(mPreference).setChecked(false);
98    }
99
100    @Test
101    public void onDeveloperOptionsDisabled_shouldDisablePreference() {
102        mController.onDeveloperOptionsDisabled();
103        final int mode = Settings.System.getInt(mContext.getContentResolver(),
104                Settings.Global.DEBUG_VIEW_ATTRIBUTES, -1);
105
106        assertThat(mode).isEqualTo(DebugViewAttributesPreferenceController.SETTING_VALUE_OFF);
107        verify(mPreference).setEnabled(false);
108        verify(mPreference).setChecked(false);
109    }
110
111}
112