1/*
2 * Copyright (C) 2018 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.settingslib.development;
18
19import static org.mockito.Mockito.doReturn;
20import static org.mockito.Mockito.verify;
21
22import android.support.v7.preference.Preference;
23import android.support.v7.preference.PreferenceScreen;
24
25import com.android.settingslib.SettingsLibRobolectricTestRunner;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.mockito.Mock;
31import org.mockito.MockitoAnnotations;
32import org.robolectric.RuntimeEnvironment;
33
34@RunWith(SettingsLibRobolectricTestRunner.class)
35public class DeveloperOptionsPreferenceControllerTest {
36
37    private static final String TEST_KEY = "Test_pref_key";
38
39    @Mock
40    private Preference mPreference;
41    @Mock
42    private PreferenceScreen mPreferenceScreen;
43
44    private DeveloperOptionsPreferenceController mController;
45
46    @Before
47    public void setUp() {
48        MockitoAnnotations.initMocks(this);
49        mController = new DeveloperOptionsPreferenceControllerTestable();
50        doReturn(mPreference).when(mPreferenceScreen).findPreference(TEST_KEY);
51        mController.displayPreference(mPreferenceScreen);
52    }
53
54    @Test
55    public void onDeveloperOptionsEnabled_shouldEnablePreference() {
56        mController.onDeveloperOptionsEnabled();
57
58        verify(mPreference).setEnabled(true);
59    }
60
61    @Test
62    public void onDeveloperOptionsDisabled_shouldDisablePreference() {
63        mController.onDeveloperOptionsDisabled();
64
65        verify(mPreference).setEnabled(false);
66    }
67
68    private class DeveloperOptionsPreferenceControllerTestable extends
69            DeveloperOptionsPreferenceController {
70        DeveloperOptionsPreferenceControllerTestable() {
71            super(RuntimeEnvironment.application);
72        }
73
74        @Override
75        public String getPreferenceKey() {
76            return TEST_KEY;
77        }
78    }
79}
80
81