• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.deviceinfo;
18 
19 import android.content.Context;
20 import android.content.IntentFilter;
21 import android.content.pm.PackageManager;
22 import android.hardware.usb.UsbManager;
23 
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentMatcher;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.annotation.Config;
34 
35 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
36 import static org.mockito.Matchers.argThat;
37 import static org.mockito.Mockito.eq;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
40 
41 @RunWith(SettingsRobolectricTestRunner.class)
42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43 public class UsbBackendTest {
44 
45     @Mock(answer = RETURNS_DEEP_STUBS)
46     private Context mContext;
47     @Mock
48     private UsbManager mUsbManager;
49     @Mock
50     private UsbBackend.UserRestrictionUtil mUserRestrictionUtil;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         when(mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI))
56             .thenReturn(true);
57         when((Object)mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager);
58     }
59 
60     @Test
constructor_noUsbPort_shouldNotCrash()61     public void constructor_noUsbPort_shouldNotCrash() {
62         UsbBackend usbBackend = new UsbBackend(mContext, mUserRestrictionUtil);
63         // Should not crash
64     }
65 
66     @Test
getCurrentMode_shouldRegisterReceiverToGetUsbState()67     public void getCurrentMode_shouldRegisterReceiverToGetUsbState() {
68         UsbBackend usbBackend = new UsbBackend(mContext, mUserRestrictionUtil);
69 
70         usbBackend.getCurrentMode();
71 
72         verify(mContext).registerReceiver(eq(null),
73             argThat(new ArgumentMatcher<IntentFilter>() {
74                 @Override
75                 public boolean matches(Object i) {
76                     final IntentFilter intentFilter = (IntentFilter) i;
77                     return intentFilter != null &&
78                         UsbManager.ACTION_USB_STATE.equals(intentFilter.getAction(0));
79                 }
80             }));
81     }
82 }
83