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;
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 DebugViewAttributesPreferenceControllerTest {
39
40    @Mock
41    private SwitchPreference mPreference;
42    @Mock
43    private PreferenceScreen mPreferenceScreen;
44
45    private Context mContext;
46    private DebugViewAttributesPreferenceController mController;
47
48    @Before
49    public void setup() {
50        MockitoAnnotations.initMocks(this);
51        mContext = RuntimeEnvironment.application;
52        mController = new DebugViewAttributesPreferenceController(mContext);
53        when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
54            .thenReturn(mPreference);
55        mController.displayPreference(mPreferenceScreen);
56    }
57
58    @Test
59    public void onPreferenceChanged_turnOnViewAttributes() {
60        mController.onPreferenceChange(null, true);
61
62        final int mode = Settings.System.getInt(mContext.getContentResolver(),
63                Settings.Global.DEBUG_VIEW_ATTRIBUTES, -1);
64
65        assertThat(mode).isEqualTo(DebugViewAttributesPreferenceController.SETTING_VALUE_ON);
66    }
67
68    @Test
69    public void onPreferenceChanged_turnOffViewAttributes() {
70        mController.onPreferenceChange(null, false);
71
72        final int mode = Settings.System.getInt(mContext.getContentResolver(),
73                Settings.Global.DEBUG_VIEW_ATTRIBUTES, -1);
74
75        assertThat(mode).isEqualTo(DebugViewAttributesPreferenceController.SETTING_VALUE_OFF);
76    }
77
78    @Test
79    public void updateState_preferenceShouldBeChecked() {
80        Settings.System.putInt(mContext.getContentResolver(), Settings.Global.DEBUG_VIEW_ATTRIBUTES,
81                DebugViewAttributesPreferenceController.SETTING_VALUE_ON);
82        mController.updateState(mPreference);
83
84        verify(mPreference).setChecked(true);
85    }
86
87    @Test
88    public void updateState_preferenceShouldNotBeChecked() {
89        Settings.System.putInt(mContext.getContentResolver(), Settings.Global.DEBUG_VIEW_ATTRIBUTES,
90                DebugViewAttributesPreferenceController.SETTING_VALUE_OFF);
91        mController.updateState(mPreference);
92
93        verify(mPreference).setChecked(false);
94    }
95
96    @Test
97    public void onDeveloperOptionsDisabled_shouldDisablePreference() {
98        mController.onDeveloperOptionsDisabled();
99        final int mode = Settings.System.getInt(mContext.getContentResolver(),
100                Settings.Global.DEBUG_VIEW_ATTRIBUTES, -1);
101
102        assertThat(mode).isEqualTo(DebugViewAttributesPreferenceController.SETTING_VALUE_OFF);
103        verify(mPreference).setEnabled(false);
104        verify(mPreference).setChecked(false);
105    }
106
107}
108