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.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.debug.IAdbManager; 28 import android.os.RemoteException; 29 import android.provider.Settings.Global; 30 31 import androidx.lifecycle.LifecycleOwner; 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settings.testutils.shadow.ShadowUtils; 35 import com.android.settings.testutils.shadow.ShadowWirelessDebuggingPreferenceController; 36 import com.android.settingslib.PrimarySwitchPreference; 37 import com.android.settingslib.core.lifecycle.Lifecycle; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.annotation.Config; 48 import org.robolectric.util.ReflectionHelpers; 49 50 @RunWith(RobolectricTestRunner.class) 51 @Config(shadows = {ShadowUtils.class, ShadowWirelessDebuggingPreferenceController.class}) 52 public class WirelessDebuggingPreferenceControllerTest { 53 54 @Mock 55 private PreferenceScreen mScreen; 56 @Mock 57 private PrimarySwitchPreference mPreference; 58 @Mock 59 private IAdbManager mAdbManager; 60 @Mock 61 private DevelopmentSettingsDashboardFragment mFragment; 62 63 private WirelessDebuggingPreferenceController mController; 64 private LifecycleOwner mLifecycleOwner; 65 private Lifecycle mLifecycle; 66 private Context mContext; 67 private ContentResolver mContentResolver; 68 69 @Before setUp()70 public void setUp() { 71 MockitoAnnotations.initMocks(this); 72 mContext = RuntimeEnvironment.application; 73 mContentResolver = mContext.getContentResolver(); 74 mLifecycleOwner = () -> mLifecycle; 75 mLifecycle = new Lifecycle(mLifecycleOwner); 76 mController = spy(new WirelessDebuggingPreferenceController(mContext, mLifecycle)); 77 ReflectionHelpers.setField(mController, "mAdbManager", mAdbManager); 78 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 79 Global.putInt(mContentResolver, Global.ADB_WIFI_ENABLED, 0); 80 } 81 82 @After tearDown()83 public void tearDown() { 84 ShadowWirelessDebuggingPreferenceController.reset(); 85 } 86 87 @Test isAvailable_isAdbWifiSupported_yes_shouldBeTrue()88 public void isAvailable_isAdbWifiSupported_yes_shouldBeTrue() throws RemoteException { 89 when(mAdbManager.isAdbWifiSupported()).thenReturn(true); 90 assertThat(mController.isAvailable()).isTrue(); 91 } 92 93 @Test isAvailable_isAdbWifiSupported_shouldBeFalse()94 public void isAvailable_isAdbWifiSupported_shouldBeFalse() throws RemoteException { 95 when(mAdbManager.isAdbWifiSupported()).thenReturn(false); 96 assertThat(mController.isAvailable()).isFalse(); 97 } 98 99 @Test updateState_adbWifiEnabled_preferenceShouldBeChecked()100 public void updateState_adbWifiEnabled_preferenceShouldBeChecked() { 101 Global.putInt(mContentResolver, 102 Global.ADB_WIFI_ENABLED, 1 /* setting enabled */); 103 mController.updateState(mPreference); 104 105 verify(mPreference).setChecked(true); 106 } 107 108 @Test updateState_adbWifiDisabled_preferenceShouldNotBeChecked()109 public void updateState_adbWifiDisabled_preferenceShouldNotBeChecked() { 110 Global.putInt(mContentResolver, 111 Global.ADB_WIFI_ENABLED, 0 /* setting disabled */); 112 mController.updateState(mPreference); 113 114 verify(mPreference).setChecked(false); 115 } 116 117 @Test onPreferenceChange_turnOn_wifiConnected_adbWifiEnabledTrue()118 public void onPreferenceChange_turnOn_wifiConnected_adbWifiEnabledTrue() { 119 ShadowWirelessDebuggingPreferenceController.setIsWifiConnected(true); 120 mController.onPreferenceChange(null, true); 121 122 assertThat(Global.getInt(mContentResolver, Global.ADB_WIFI_ENABLED, -1)).isEqualTo(1); 123 } 124 125 @Test onPreferenceChange_turnOff_wifiConnected_adbWifiEnabledFalse()126 public void onPreferenceChange_turnOff_wifiConnected_adbWifiEnabledFalse() { 127 ShadowWirelessDebuggingPreferenceController.setIsWifiConnected(true); 128 mController.onPreferenceChange(null, false); 129 130 assertThat(Global.getInt(mContentResolver, Global.ADB_WIFI_ENABLED, -1)).isEqualTo(0); 131 } 132 133 @Test onPreferenceChange_turnOn_wifiNotConnected_adbWifiEnabledFalse()134 public void onPreferenceChange_turnOn_wifiNotConnected_adbWifiEnabledFalse() { 135 // Should not be able to enable wifi debugging without being connected to a wifi network 136 ShadowWirelessDebuggingPreferenceController.setIsWifiConnected(false); 137 mController.onPreferenceChange(null, true); 138 139 assertThat(Global.getInt(mContentResolver, Global.ADB_WIFI_ENABLED, -1)).isEqualTo(0); 140 } 141 142 @Test onPreferenceChange_turnOff_wifiNotConnected_adbWifiEnabledFalse()143 public void onPreferenceChange_turnOff_wifiNotConnected_adbWifiEnabledFalse() { 144 ShadowWirelessDebuggingPreferenceController.setIsWifiConnected(false); 145 mController.onPreferenceChange(null, false); 146 147 assertThat(Global.getInt(mContentResolver, Global.ADB_WIFI_ENABLED, -1)).isEqualTo(0); 148 } 149 } 150