[adb-wireless] Add Wireless Debugging Switch in Developer options.
Bug: 111434128 Bug: 119492574 Test: make RunSettingsRoboTests ROBOTEST_FILTER=WirelessDebugging Change-Id: I188badb43035172642cf235bb27e56d3a1dea169 Merged-In: I188badb43035172642cf235bb27e56d3a1dea169
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.development;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.provider.Settings.Global;
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||
import com.android.settings.widget.SwitchBar;
|
||||
import com.android.settings.widget.SwitchBarController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = ShadowUtils.class)
|
||||
public class WirelessDebuggingEnablerTest {
|
||||
|
||||
@Mock
|
||||
private SettingsActivity mActivity;
|
||||
@Mock
|
||||
private SwitchBar mSwitchBar;
|
||||
@Mock
|
||||
private WirelessDebuggingEnabler.OnEnabledListener mListener;
|
||||
|
||||
private WirelessDebuggingEnabler mWirelessDebuggingEnabler;
|
||||
private Context mContext;
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private Lifecycle mLifecycle;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mLifecycleOwner = () -> mLifecycle;
|
||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||
mWirelessDebuggingEnabler = spy(new WirelessDebuggingEnabler(
|
||||
mContext, new SwitchBarController(mSwitchBar), mListener, mLifecycle));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreation_shouldShowSwitchBar() {
|
||||
verify(mSwitchBar).show();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void teardownSwitchController_shouldHideSwitchBar() {
|
||||
mWirelessDebuggingEnabler.teardownSwitchController();
|
||||
|
||||
verify(mSwitchBar).hide();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adbWifiEnabled_switchBarShouldBeChecked() {
|
||||
// Set to disabled first otherwise we won't get the onChange() event
|
||||
Global.putInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, 0 /* setting disabled */);
|
||||
mWirelessDebuggingEnabler.onResume();
|
||||
|
||||
verify(mSwitchBar).setChecked(false);
|
||||
verify(mListener).onEnabled(false);
|
||||
|
||||
Global.putInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, 1 /* setting enabled */);
|
||||
final ContentObserver observer =
|
||||
ReflectionHelpers.getField(mWirelessDebuggingEnabler, "mSettingsObserver");
|
||||
observer.onChange(true, Global.getUriFor(Global.ADB_WIFI_ENABLED));
|
||||
|
||||
verify(mSwitchBar).setChecked(true);
|
||||
// Should also get a callback
|
||||
verify(mListener).onEnabled(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adbWifiEnabled_switchBarShouldNotBeChecked() {
|
||||
Global.putInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, 1 /* setting enabled */);
|
||||
mWirelessDebuggingEnabler.onResume();
|
||||
|
||||
verify(mSwitchBar).setChecked(true);
|
||||
verify(mListener).onEnabled(true);
|
||||
|
||||
Global.putInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, 0 /* setting disabled */);
|
||||
final ContentObserver observer =
|
||||
ReflectionHelpers.getField(mWirelessDebuggingEnabler, "mSettingsObserver");
|
||||
observer.onChange(true, Global.getUriFor(Global.ADB_WIFI_ENABLED));
|
||||
|
||||
verify(mSwitchBar).setChecked(false);
|
||||
// Should also get a callback
|
||||
verify(mListener).onEnabled(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSwitchToggled_true_shouldSetAdbWifiEnabledTrue() {
|
||||
Global.putInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, 0 /* setting disabled */);
|
||||
mWirelessDebuggingEnabler.onResume();
|
||||
|
||||
verify(mSwitchBar).setChecked(false);
|
||||
verify(mListener).onEnabled(false);
|
||||
|
||||
mWirelessDebuggingEnabler.onSwitchToggled(true);
|
||||
|
||||
verify(mSwitchBar).setChecked(true);
|
||||
verify(mListener).onEnabled(true);
|
||||
assertThat(Global.getInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, -1)).isEqualTo(1);
|
||||
// Should also get a callback
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSwitchToggled_false_shouldSetAdbWifiEnabledFalse() {
|
||||
Global.putInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, 1 /* setting disabled */);
|
||||
mWirelessDebuggingEnabler.onResume();
|
||||
|
||||
verify(mSwitchBar).setChecked(true);
|
||||
verify(mListener).onEnabled(true);
|
||||
|
||||
mWirelessDebuggingEnabler.onSwitchToggled(false);
|
||||
|
||||
verify(mSwitchBar).setChecked(false);
|
||||
verify(mListener).onEnabled(false);
|
||||
assertThat(Global.getInt(mContext.getContentResolver(),
|
||||
Global.ADB_WIFI_ENABLED, -1)).isEqualTo(0);
|
||||
// Should also get a callback
|
||||
}
|
||||
}
|
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.development;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.debug.IAdbManager;
|
||||
import android.os.RemoteException;
|
||||
import android.provider.Settings.Global;
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||
import com.android.settings.widget.MasterSwitchPreference;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = ShadowUtils.class)
|
||||
public class WirelessDebuggingPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private MasterSwitchPreference mPreference;
|
||||
@Mock
|
||||
private IAdbManager mAdbManager;
|
||||
@Mock
|
||||
private DevelopmentSettingsDashboardFragment mFragment;
|
||||
|
||||
private WirelessDebuggingPreferenceController mController;
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private Lifecycle mLifecycle;
|
||||
private Context mContext;
|
||||
private ContentResolver mContentResolver;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mContentResolver = mContext.getContentResolver();
|
||||
mLifecycleOwner = () -> mLifecycle;
|
||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||
mController = spy(new WirelessDebuggingPreferenceController(mContext, mLifecycle));
|
||||
ReflectionHelpers.setField(mController, "mAdbManager", mAdbManager);
|
||||
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_isAdbWifiSupported_yes_shouldBeTrue() throws RemoteException {
|
||||
when(mAdbManager.isAdbWifiSupported()).thenReturn(true);
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_isAdbWifiSupported_shouldBeFalse() throws RemoteException {
|
||||
when(mAdbManager.isAdbWifiSupported()).thenReturn(false);
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_adbWifiEnabled_preferenceShouldBeChecked() {
|
||||
Global.putInt(mContentResolver,
|
||||
Global.ADB_WIFI_ENABLED, 1 /* setting enabled */);
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_adbWifiDisabled_preferenceShouldNotBeChecked() {
|
||||
Global.putInt(mContentResolver,
|
||||
Global.ADB_WIFI_ENABLED, 0 /* setting disabled */);
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_turnOn_adbWifiEnabledTrue() {
|
||||
mController.onPreferenceChange(null, true);
|
||||
|
||||
assertThat(Global.getInt(mContentResolver, Global.ADB_WIFI_ENABLED, -1)).isEqualTo(1);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user