2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.development;
19 import static android.arch.lifecycle.Lifecycle.Event.ON_CREATE;
20 import static android.arch.lifecycle.Lifecycle.Event.ON_DESTROY;
21 import static com.google.common.truth.Truth.assertThat;
23 import static junit.framework.Assert.assertFalse;
24 import static junit.framework.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyBoolean;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 import android.arch.lifecycle.LifecycleOwner;
37 import android.content.Context;
38 import android.content.pm.PackageManager;
39 import android.hardware.usb.UsbManager;
40 import android.support.v7.preference.ListPreference;
41 import android.support.v7.preference.PreferenceScreen;
43 import com.android.settings.R;
44 import com.android.settings.TestConfig;
45 import com.android.settings.testutils.SettingsRobolectricTestRunner;
46 import com.android.settings.testutils.shadow.ShadowUtils;
47 import com.android.settingslib.core.lifecycle.Lifecycle;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.Mock;
54 import org.mockito.MockitoAnnotations;
55 import org.robolectric.RuntimeEnvironment;
56 import org.robolectric.annotation.Config;
58 @RunWith(SettingsRobolectricTestRunner.class)
59 @Config(manifest = TestConfig.MANIFEST_PATH,
60 sdk = TestConfig.SDK_VERSION,
61 shadows = {ShadowUtils.class})
62 public class SelectUsbConfigPreferenceControllerTest {
65 private ListPreference mPreference;
67 private PreferenceScreen mScreen;
69 private UsbManager mUsbManager;
71 private PackageManager mPackageManager;
73 private Context mContext;
74 private LifecycleOwner mLifecycleOwner;
75 private Lifecycle mLifecycle;
76 private SelectUsbConfigPreferenceController mController;
88 private String[] mValues;
89 private String[] mSummaries;
93 MockitoAnnotations.initMocks(this);
94 mLifecycleOwner = () -> mLifecycle;
95 mLifecycle = new Lifecycle(mLifecycleOwner);
96 mContext = spy(RuntimeEnvironment.application);
97 doReturn(mUsbManager).when(mContext).getSystemService(Context.USB_SERVICE);
98 doReturn(mPackageManager).when(mContext).getPackageManager();
99 mValues = mContext.getResources().getStringArray(R.array.usb_configuration_values);
100 mSummaries = mContext.getResources().getStringArray(R.array.usb_configuration_titles);
101 mController = spy(new SelectUsbConfigPreferenceController(mContext, mLifecycle));
102 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
103 mController.displayPreference(mScreen);
108 public void teardown() {
113 public void onPreferenceChange_setCharging_shouldEnableCharging() {
114 when(mUsbManager.isFunctionEnabled(mValues[0])).thenReturn(true);
115 doNothing().when(mController).setCurrentFunction(anyString(), anyBoolean());
116 mController.onPreferenceChange(mPreference, mValues[0]);
118 verify(mController).setCurrentFunction(mValues[0], false /* usb data unlock */);
122 public void onUsbAccessoryAndHostDisabled_shouldNotBeAvailable() {
123 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(false);
124 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
126 assertFalse(mController.isAvailable());
130 public void onUsbHostEnabled_shouldBeAvailable() {
131 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(true);
132 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
134 assertTrue(mController.isAvailable());
138 public void onUsbAccessoryEnabled_shouldBeAvailable() {
139 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(false);
140 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
142 assertTrue(mController.isAvailable());
146 public void onPreferenceChange_setMtp_shouldEnableMtp() {
147 when(mUsbManager.isFunctionEnabled(mValues[1])).thenReturn(true);
148 doNothing().when(mController).setCurrentFunction(anyString(), anyBoolean());
149 mController.onPreferenceChange(mPreference, mValues[1]);
151 verify(mController).setCurrentFunction(mValues[1], true /* usb data unlock */);
155 public void onPreferenceChange_monkeyUser_shouldReturnFalse() {
156 when(mUsbManager.isFunctionEnabled(mValues[1])).thenReturn(true);
157 ShadowUtils.setIsUserAMonkey(true);
158 doNothing().when(mController).setCurrentFunction(anyString(), anyBoolean());
160 final boolean isHandled = mController.onPreferenceChange(mPreference, mValues[1]);
162 assertThat(isHandled).isFalse();
163 verify(mController, never()).setCurrentFunction(any(), anyBoolean());
167 public void updateState_chargingEnabled_shouldSetPreferenceToCharging() {
168 when(mUsbManager.isFunctionEnabled(mValues[0])).thenReturn(true);
170 mController.updateState(mPreference);
172 verify(mPreference).setValue(mValues[0]);
173 verify(mPreference).setSummary(mSummaries[0]);
177 public void updateState_RndisEnabled_shouldEnableRndis() {
178 when(mUsbManager.isFunctionEnabled(mValues[3])).thenReturn(true);
180 mController.updateState(mPreference);
182 verify(mPreference).setValue(mValues[3]);
183 verify(mPreference).setSummary(mSummaries[3]);
187 public void updateState_noValueSet_shouldEnableChargingAsDefault() {
188 mController.updateState(mPreference);
190 verify(mPreference).setValue(mValues[0]);
191 verify(mPreference).setSummary(mSummaries[0]);
195 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
196 mController.onDeveloperOptionsSwitchDisabled();
198 verify(mPreference).setEnabled(false);
202 public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() {
203 mController.onDeveloperOptionsSwitchEnabled();
205 verify(mPreference).setEnabled(true);
209 public void onCreate_shouldRegisterReceiver() {
210 mLifecycle.onCreate(null /* bundle */);
211 mLifecycle.handleLifecycleEvent(ON_CREATE);
213 verify(mContext).registerReceiver(any(), any());
217 public void onDestroy_shouldUnregisterReceiver() {
218 doNothing().when(mContext).unregisterReceiver(any());
219 mLifecycle.handleLifecycleEvent(ON_CREATE);
220 mLifecycle.handleLifecycleEvent(ON_DESTROY);
222 verify(mContext).unregisterReceiver(any());