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 org.mockito.Mockito.verify;
20import static org.mockito.Mockito.when;
21
22import android.content.Context;
23import android.net.wifi.WifiManager;
24import android.support.v14.preference.SwitchPreference;
25import android.support.v7.preference.PreferenceScreen;
26
27import com.android.settings.testutils.SettingsRobolectricTestRunner;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.mockito.Mock;
33import org.mockito.MockitoAnnotations;
34
35@RunWith(SettingsRobolectricTestRunner.class)
36public class WifiVerboseLoggingPreferenceControllerTest {
37    @Mock
38    private Context mContext;
39    @Mock
40    private WifiManager mWifiManager;
41    @Mock
42    private SwitchPreference mPreference;
43    @Mock
44    private PreferenceScreen mPreferenceScreen;
45
46    private WifiVerboseLoggingPreferenceController mController;
47
48    @Before
49    public void setup() {
50        MockitoAnnotations.initMocks(this);
51        when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
52        mController = new WifiVerboseLoggingPreferenceController(mContext);
53        when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
54            .thenReturn(mPreference);
55        mController.displayPreference(mPreferenceScreen);
56    }
57
58    @Test
59    public void onPreferenceChange_settingEnabled_shouldEnableVerboseLogging() {
60        mController.onPreferenceChange(mPreference, true /* new value */);
61
62        verify(mWifiManager).enableVerboseLogging(
63                WifiVerboseLoggingPreferenceController.SETTING_VALUE_ON);
64    }
65
66    @Test
67    public void onPreferenceChange_settingDisabled_shouldDisablVerboseLogging() {
68        mController.onPreferenceChange(mPreference, false /* new value */);
69
70        verify(mWifiManager).enableVerboseLogging(
71                WifiVerboseLoggingPreferenceController.SETTING_VALUE_OFF);
72    }
73
74    @Test
75    public void updateState_settingEnabled_shouldEnablePreference() {
76        when(mWifiManager.getVerboseLoggingLevel()).thenReturn(1);
77        mController.updateState(mPreference);
78
79        verify(mPreference).setChecked(true);
80    }
81
82    @Test
83    public void updateState_settingDisabled_shouldDisablePreference() {
84        when(mWifiManager.getVerboseLoggingLevel()).thenReturn(0);
85        mController.updateState(mPreference);
86
87        verify(mPreference).setChecked(false);
88    }
89
90    @Test
91    public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
92        mController.onDeveloperOptionsSwitchDisabled();
93
94        verify(mWifiManager)
95            .enableVerboseLogging(WifiVerboseLoggingPreferenceController.SETTING_VALUE_OFF);
96        verify(mPreference).setEnabled(false);
97        verify(mPreference).setChecked(false);
98    }
99}
100