OSDN Git Service

Zen Condition text and primary click changes
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / development / SelectUsbConfigPreferenceControllerTest.java
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
17 package com.android.settings.development;
18
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;
22
23 import static junit.framework.Assert.assertFalse;
24 import static junit.framework.Assert.assertTrue;
25
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;
35
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;
42
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;
48
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;
57
58 @RunWith(SettingsRobolectricTestRunner.class)
59 @Config(manifest = TestConfig.MANIFEST_PATH,
60         sdk = TestConfig.SDK_VERSION,
61         shadows = {ShadowUtils.class})
62 public class SelectUsbConfigPreferenceControllerTest {
63
64     @Mock
65     private ListPreference mPreference;
66     @Mock
67     private PreferenceScreen mScreen;
68     @Mock
69     private UsbManager mUsbManager;
70     @Mock
71     private PackageManager mPackageManager;
72
73     private Context mContext;
74     private LifecycleOwner mLifecycleOwner;
75     private Lifecycle mLifecycle;
76     private SelectUsbConfigPreferenceController mController;
77
78     /**
79      * Array Values Key
80      *
81      * 0: Charging
82      * 1: MTP
83      * 2: PTP
84      * 3: RNDIS
85      * 4: Audio Source
86      * 5: MIDI
87      */
88     private String[] mValues;
89     private String[] mSummaries;
90
91     @Before
92     public void setup() {
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);
104
105     }
106
107     @After
108     public void teardown() {
109         ShadowUtils.reset();
110     }
111
112     @Test
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]);
117
118         verify(mController).setCurrentFunction(mValues[0], false /* usb data unlock */);
119     }
120
121     @Test
122     public void onUsbAccessoryAndHostDisabled_shouldNotBeAvailable() {
123         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(false);
124         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
125                 false);
126         assertFalse(mController.isAvailable());
127     }
128
129     @Test
130     public void onUsbHostEnabled_shouldBeAvailable() {
131         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(true);
132         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
133                 false);
134         assertTrue(mController.isAvailable());
135     }
136
137     @Test
138     public void onUsbAccessoryEnabled_shouldBeAvailable() {
139         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(false);
140         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
141                 true);
142         assertTrue(mController.isAvailable());
143     }
144
145     @Test
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]);
150
151         verify(mController).setCurrentFunction(mValues[1], true /* usb data unlock */);
152     }
153
154     @Test
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());
159
160         final boolean isHandled = mController.onPreferenceChange(mPreference, mValues[1]);
161
162         assertThat(isHandled).isFalse();
163         verify(mController, never()).setCurrentFunction(any(), anyBoolean());
164     }
165
166     @Test
167     public void updateState_chargingEnabled_shouldSetPreferenceToCharging() {
168         when(mUsbManager.isFunctionEnabled(mValues[0])).thenReturn(true);
169
170         mController.updateState(mPreference);
171
172         verify(mPreference).setValue(mValues[0]);
173         verify(mPreference).setSummary(mSummaries[0]);
174     }
175
176     @Test
177     public void updateState_RndisEnabled_shouldEnableRndis() {
178         when(mUsbManager.isFunctionEnabled(mValues[3])).thenReturn(true);
179
180         mController.updateState(mPreference);
181
182         verify(mPreference).setValue(mValues[3]);
183         verify(mPreference).setSummary(mSummaries[3]);
184     }
185
186     @Test
187     public void updateState_noValueSet_shouldEnableChargingAsDefault() {
188         mController.updateState(mPreference);
189
190         verify(mPreference).setValue(mValues[0]);
191         verify(mPreference).setSummary(mSummaries[0]);
192     }
193
194     @Test
195     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
196         mController.onDeveloperOptionsSwitchDisabled();
197
198         verify(mPreference).setEnabled(false);
199     }
200
201     @Test
202     public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() {
203         mController.onDeveloperOptionsSwitchEnabled();
204
205         verify(mPreference).setEnabled(true);
206     }
207
208     @Test
209     public void onCreate_shouldRegisterReceiver() {
210         mLifecycle.onCreate(null /* bundle */);
211         mLifecycle.handleLifecycleEvent(ON_CREATE);
212
213         verify(mContext).registerReceiver(any(), any());
214     }
215
216     @Test
217     public void onDestroy_shouldUnregisterReceiver() {
218         doNothing().when(mContext).unregisterReceiver(any());
219         mLifecycle.handleLifecycleEvent(ON_CREATE);
220         mLifecycle.handleLifecycleEvent(ON_DESTROY);
221
222         verify(mContext).unregisterReceiver(any());
223     }
224 }