1 /* 2 * Copyright (C) 2018 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.connecteddevice.usb; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.verifyZeroInteractions; 22 import static org.mockito.Mockito.when; 23 24 import android.hardware.usb.UsbManager; 25 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.testutils.shadow.ShadowUtils; 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 @RunWith(SettingsRobolectricTestRunner.class) 37 public class UsbDefaultFragmentTest { 38 39 @Mock 40 private UsbBackend mUsbBackend; 41 42 private UsbDefaultFragment mFragment; 43 44 @Before 45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 mFragment = new UsbDefaultFragment(); 48 mFragment.mUsbBackend = mUsbBackend; 49 } 50 51 @Test 52 public void getDefaultKey_isNone_shouldReturnNone() { 53 when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_NONE); 54 assertThat(mFragment.getDefaultKey()) 55 .isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_NONE)); 56 } 57 58 @Test 59 public void getDefaultKey_isMtp_shouldReturnMtp() { 60 when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_MTP); 61 assertThat(mFragment.getDefaultKey()) 62 .isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP)); 63 } 64 65 @Test 66 public void getDefaultKey_isPtp_shouldReturnPtp() { 67 when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_PTP); 68 assertThat(mFragment.getDefaultKey()) 69 .isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP)); 70 } 71 72 @Test 73 public void getDefaultKey_isRndis_shouldReturnRndis() { 74 when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_RNDIS); 75 assertThat(mFragment.getDefaultKey()) 76 .isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_RNDIS)); 77 } 78 79 @Test 80 public void getDefaultKey_isMidi_shouldReturnMidi() { 81 when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_MIDI); 82 assertThat(mFragment.getDefaultKey()) 83 .isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MIDI)); 84 } 85 86 @Test 87 public void setDefaultKey_isNone_shouldSetNone() { 88 mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_NONE)); 89 verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_NONE); 90 } 91 92 @Test 93 public void setDefaultKey_isMtp_shouldSetMtp() { 94 mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP)); 95 verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_MTP); 96 } 97 98 @Test 99 public void setDefaultKey_isPtp_shouldSetPtp() { 100 mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP)); 101 verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_PTP); 102 } 103 104 @Test 105 public void setDefaultKey_isRndis_shouldSetRndis() { 106 mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_RNDIS)); 107 verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_RNDIS); 108 } 109 110 @Test 111 public void setDefaultKey_isMidi_shouldSetMidi() { 112 mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MIDI)); 113 verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_MIDI); 114 } 115 116 @Test 117 @Config(shadows = ShadowUtils.class) 118 public void setDefaultKey_isMonkey_shouldDoNothing() { 119 ShadowUtils.setIsUserAMonkey(true); 120 mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP)); 121 verifyZeroInteractions(mUsbBackend); 122 } 123 } 124