1 package com.android.settings.connecteddevice;
2 
3 import android.content.Context;
4 import android.content.Intent;
5 import android.hardware.usb.UsbManager;
6 import android.support.v7.preference.Preference;
7 import android.support.v7.preference.PreferenceScreen;
8 
9 import com.android.settings.R;
10 import com.android.settings.SettingsRobolectricTestRunner;
11 import com.android.settings.TestConfig;
12 import com.android.settings.deviceinfo.UsbBackend;
13 import com.android.settings.deviceinfo.UsbModeChooserActivity;
14 
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.robolectric.annotation.Config;
21 import org.robolectric.shadows.ShadowApplication;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.anyInt;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Mockito.when;
29 
30 @RunWith(SettingsRobolectricTestRunner.class)
31 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
32 public class UsbModePreferenceControllerTest {
33 
34     @Mock(answer = RETURNS_DEEP_STUBS)
35     private UsbBackend mUsbBackend;
36     @Mock(answer = RETURNS_DEEP_STUBS)
37     private PreferenceScreen mScreen;
38 
39     private Context mContext;
40     private UsbModePreferenceController mController;
41 
42     @Before
setUp()43     public void setUp() {
44         MockitoAnnotations.initMocks(this);
45         mContext = ShadowApplication.getInstance().getApplicationContext();
46         mController = new UsbModePreferenceController(mContext, mUsbBackend);
47     }
48 
49     @Test
testGetSummary_chargeDevice()50     public void testGetSummary_chargeDevice() {
51         assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[0]))
52                 .isEqualTo(R.string.usb_summary_charging_only);
53     }
54 
55     @Test
testGetSummary_supplyPower()56     public void testGetSummary_supplyPower() {
57         assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[1]))
58                 .isEqualTo(R.string.usb_summary_power_only);
59     }
60 
61     @Test
testGetSummary_TransferFiles()62     public void testGetSummary_TransferFiles() {
63         assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[2]))
64                 .isEqualTo(R.string.usb_summary_file_transfers);
65     }
66 
67     @Test
testGetSummary_TransferPhoto()68     public void testGetSummary_TransferPhoto() {
69         assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[3]))
70                 .isEqualTo(R.string.usb_summary_photo_transfers);
71     }
72 
73     @Test
testGetSummary_MIDI()74     public void testGetSummary_MIDI() {
75         assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[4]))
76                 .isEqualTo(R.string.usb_summary_MIDI);
77     }
78 
79     @Test
testPreferenceSummary_usbDisconnected()80     public void testPreferenceSummary_usbDisconnected() {
81         final Preference preference = new Preference(mContext);
82         preference.setKey("usb_mode");
83         preference.setEnabled(true);
84         mController.updateState(preference);
85         assertThat(preference.getSummary()).isEqualTo(
86                 mContext.getString(R.string.disconnected));
87     }
88 
89     @Test
testUsbBoradcastReceiver_usbConnected_shouldUpdateSummary()90     public void testUsbBoradcastReceiver_usbConnected_shouldUpdateSummary() {
91         final Preference preference = new Preference(mContext);
92         preference.setKey("usb_mode");
93         preference.setEnabled(true);
94         when(mUsbBackend.getCurrentMode()).thenReturn(UsbModeChooserActivity.DEFAULT_MODES[0]);
95         when(mScreen.findPreference("usb_mode")).thenReturn(preference);
96 
97         mController.displayPreference(mScreen);
98         mController.onResume();
99         final Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
100         intent.putExtra(UsbManager.USB_CONNECTED, true);
101         mContext.sendStickyBroadcast(intent);
102 
103         assertThat(preference.getSummary()).isEqualTo(
104                 mContext.getString(R.string.usb_summary_charging_only));
105     }
106 
107 }