1 /*
2  * Copyright (C) 2020 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 
17 package com.android.settings.development;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.provider.Settings;
26 
27 import androidx.preference.PreferenceScreen;
28 import androidx.preference.SwitchPreference;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class WifiEnhancedMacRandomizationPreferenceControllerTest {
40     private static final String ENHANCED_MAC_RANDOMIZATION_FEATURE_FLAG =
41             "enhanced_mac_randomization_force_enabled";
42     @Mock
43     private SwitchPreference mPreference;
44     @Mock
45     private PreferenceScreen mPreferenceScreen;
46     private Context mContext;
47     private WifiEnhancedMacRandomizationPreferenceController mController;
48 
49     @Before
setup()50     public void setup() {
51         MockitoAnnotations.initMocks(this);
52         mContext = RuntimeEnvironment.application;
53         mController = new WifiEnhancedMacRandomizationPreferenceController(mContext);
54         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
55                 .thenReturn(mPreference);
56         mController.displayPreference(mPreferenceScreen);
57     }
58 
59     @Test
onPreferenceChanged_enabled_shouldTurnOnEnhancedRandomization()60     public void onPreferenceChanged_enabled_shouldTurnOnEnhancedRandomization() {
61         mController.onPreferenceChange(mPreference, true /* new value */);
62 
63         int mode = Settings.Global.getInt(mContext.getContentResolver(),
64                 ENHANCED_MAC_RANDOMIZATION_FEATURE_FLAG, -1);
65         assertThat(mode).isEqualTo(1);
66     }
67 
68     @Test
onPreferenceChanged_disabled_shouldTurnOffEnhancedRandomization()69     public void onPreferenceChanged_disabled_shouldTurnOffEnhancedRandomization() {
70         mController.onPreferenceChange(mPreference, false /* new value */);
71 
72         int mode = Settings.Global.getInt(mContext.getContentResolver(),
73                 ENHANCED_MAC_RANDOMIZATION_FEATURE_FLAG, -1);
74         assertThat(mode).isEqualTo(0);
75     }
76 
77     @Test
updateState_preferenceShouldBeChecked()78     public void updateState_preferenceShouldBeChecked() {
79         Settings.Global.putInt(mContext.getContentResolver(),
80                 ENHANCED_MAC_RANDOMIZATION_FEATURE_FLAG, 1);
81         mController.updateState(mPreference);
82 
83         verify(mPreference).setChecked(true);
84     }
85 
86     @Test
updateState_preferenceShouldNotBeChecked()87     public void updateState_preferenceShouldNotBeChecked() {
88         Settings.Global.putInt(mContext.getContentResolver(),
89                 ENHANCED_MAC_RANDOMIZATION_FEATURE_FLAG, 0);
90         mController.updateState(mPreference);
91 
92         verify(mPreference).setChecked(false);
93     }
94 
95     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()96     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
97         mController.onDeveloperOptionsSwitchDisabled();
98 
99         int mode = Settings.Global.getInt(mContext.getContentResolver(),
100                 ENHANCED_MAC_RANDOMIZATION_FEATURE_FLAG, -1);
101 
102         assertThat(mode).isEqualTo(0);
103         assertThat(mPreference.isChecked()).isFalse();
104         assertThat(mPreference.isEnabled()).isFalse();
105     }
106 }
107