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 package com.android.settings.bluetooth; 17 18 import android.content.ComponentName; 19 import android.content.Context; 20 import android.os.UserManager; 21 22 import com.android.settings.SettingsRobolectricTestRunner; 23 import com.android.settings.TestConfig; 24 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 25 import com.android.settings.widget.MasterSwitchController; 26 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 27 import com.android.settingslib.bluetooth.LocalBluetoothManager; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.annotation.Config; 35 36 import static com.google.common.truth.Truth.assertThat; 37 import static junit.framework.TestCase.assertNotNull; 38 import static junit.framework.TestCase.assertEquals; 39 import static org.mockito.Mockito.mock; 40 import static org.mockito.Matchers.any; 41 import static org.mockito.Matchers.anyBoolean; 42 import static org.mockito.Mockito.never; 43 import static org.mockito.Mockito.verify; 44 import static org.mockito.Mockito.verifyNoMoreInteractions; 45 import static org.mockito.Mockito.when; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 49 public class BluetoothEnablerTest { 50 51 private static final EnforcedAdmin FAKE_ENFORCED_ADMIN = 52 new EnforcedAdmin(new ComponentName("test.package", "test.Class"), 10); 53 54 @Mock 55 private MetricsFeatureProvider mMetricsFeatureProvider; 56 @Mock 57 private Context mContext; 58 @Mock 59 private MasterSwitchController mMasterSwitchController; 60 @Mock 61 private RestrictionUtils mRestrictionUtils; 62 63 private BluetoothEnabler mBluetoothEnabler; 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 mBluetoothEnabler = new BluetoothEnabler( 69 mContext, 70 mMasterSwitchController, 71 mMetricsFeatureProvider, 72 mock(LocalBluetoothManager.class), 73 123, 74 mRestrictionUtils); 75 } 76 77 @Test onSwitchToggled_shouldLogActionWithSuppliedEvent()78 public void onSwitchToggled_shouldLogActionWithSuppliedEvent() { 79 // WHEN the switch is toggled... 80 mBluetoothEnabler.onSwitchToggled(false); 81 82 // THEN the corresponding metrics action is logged. 83 verify(mMetricsFeatureProvider).action(mContext, 123, false); 84 } 85 86 @Test maybeEnforceRestrictions_noRestrictions()87 public void maybeEnforceRestrictions_noRestrictions() { 88 // GIVEN there are no restrictions set... 89 when(mRestrictionUtils.checkIfRestrictionEnforced(any(Context.class), any(String.class))) 90 .thenReturn(null); 91 92 // WHEN the maybeEnforceRestrictions is called... 93 // THEN false is returned to indicate there was no restriction to enforce 94 assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isFalse(); 95 96 // THEN a null EnfoceAdmin is set. 97 verify(mMasterSwitchController).setDisabledByAdmin(null); 98 // THEN the state of the switch isn't changed. 99 verify(mMasterSwitchController, never()).setChecked(anyBoolean()); 100 } 101 102 @Test maybeEnforceRestrictions_disallowBluetoothRestrictionSet()103 public void maybeEnforceRestrictions_disallowBluetoothRestrictionSet() { 104 // GIVEN Bluetooth has been disallowed... 105 when(mRestrictionUtils.checkIfRestrictionEnforced( 106 mContext, UserManager.DISALLOW_BLUETOOTH)).thenReturn(FAKE_ENFORCED_ADMIN); 107 when(mRestrictionUtils.checkIfRestrictionEnforced( 108 mContext, UserManager.DISALLOW_CONFIG_BLUETOOTH)).thenReturn(null); 109 110 // WHEN the maybeEnforceRestrictions is called... 111 // THEN true is returned to indicate there was a restriction to enforce. 112 assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue(); 113 114 // THEN the expected EnfoceAdmin is set. 115 verify(mMasterSwitchController).setDisabledByAdmin(FAKE_ENFORCED_ADMIN); 116 117 // THEN the switch is unchecked. 118 verify(mMasterSwitchController).setChecked(false); 119 } 120 121 @Test maybeEnforceRestrictions_disallowConfigBluetoothRestrictionSet()122 public void maybeEnforceRestrictions_disallowConfigBluetoothRestrictionSet() { 123 // GIVEN configuring Bluetooth has been disallowed... 124 when(mRestrictionUtils.checkIfRestrictionEnforced( 125 mContext, UserManager.DISALLOW_BLUETOOTH)).thenReturn(null); 126 when(mRestrictionUtils.checkIfRestrictionEnforced( 127 mContext, UserManager.DISALLOW_CONFIG_BLUETOOTH)).thenReturn(FAKE_ENFORCED_ADMIN); 128 129 // WHEN the maybeEnforceRestrictions is called... 130 // THEN true is returned to indicate there was a restriction to enforce. 131 assertThat(mBluetoothEnabler.maybeEnforceRestrictions()).isTrue(); 132 133 // THEN the expected EnfoceAdmin is set. 134 verify(mMasterSwitchController).setDisabledByAdmin(FAKE_ENFORCED_ADMIN); 135 136 // THEN the switch is unchecked. 137 verify(mMasterSwitchController).setChecked(false); 138 } 139 140 } 141