1 package com.android.settings.connecteddevice.usb;
3 import android.content.Context;
4 import android.support.v7.preference.Preference;
5 import android.support.v7.preference.PreferenceScreen;
7 import com.android.settings.R;
8 import com.android.settings.testutils.SettingsRobolectricTestRunner;
9 import com.android.settings.TestConfig;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.Mock;
15 import org.mockito.MockitoAnnotations;
16 import org.robolectric.annotation.Config;
17 import org.robolectric.shadows.ShadowApplication;
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.when;
23 import static org.mockito.Mockito.verify;
25 @RunWith(SettingsRobolectricTestRunner.class)
26 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
27 public class UsbModePreferenceControllerTest {
29 @Mock(answer = RETURNS_DEEP_STUBS)
30 private UsbBackend mUsbBackend;
31 @Mock(answer = RETURNS_DEEP_STUBS)
32 private PreferenceScreen mScreen;
34 private UsbConnectionBroadcastReceiver mUsbConnectionBroadcastReceiver;
36 private Context mContext;
37 private UsbModePreferenceController mController;
41 MockitoAnnotations.initMocks(this);
42 mContext = ShadowApplication.getInstance().getApplicationContext();
43 mController = new UsbModePreferenceController(mContext, mUsbBackend);
44 mController.mUsbReceiver = mUsbConnectionBroadcastReceiver;
48 public void testGetSummary_chargeDevice() {
49 assertThat(mController.getSummary(0))
50 .isEqualTo(R.string.usb_summary_charging_only);
54 public void testGetSummary_supplyPower() {
55 assertThat(mController.getSummary(UsbBackend.MODE_POWER_SOURCE))
56 .isEqualTo(R.string.usb_summary_power_only);
60 public void testGetSummary_TransferFiles() {
61 assertThat(mController.getSummary(UsbBackend.MODE_DATA_MTP))
62 .isEqualTo(R.string.usb_summary_file_transfers);
66 public void testGetSummary_TransferPhoto() {
67 assertThat(mController.getSummary(UsbBackend.MODE_DATA_PTP))
68 .isEqualTo(R.string.usb_summary_photo_transfers);
72 public void testGetSummary_MIDI() {
73 assertThat(mController.getSummary(UsbBackend.MODE_DATA_MIDI))
74 .isEqualTo(R.string.usb_summary_MIDI);
78 public void testGetSummary_Tethering() {
79 assertThat(mController.getSummary(UsbBackend.MODE_DATA_TETHER))
80 .isEqualTo(R.string.usb_summary_tether);
84 public void testPreferenceSummary_usbDisconnected() {
85 final Preference preference = new Preference(mContext);
86 preference.setKey("usb_mode");
87 preference.setEnabled(true);
88 when(mUsbBackend.getCurrentMode()).thenReturn(UsbBackend.MODE_POWER_SINK);
89 when(mUsbConnectionBroadcastReceiver.isConnected()).thenReturn(false);
90 mController.updateState(preference);
92 assertThat(preference.getKey()).isEqualTo("usb_mode");
93 assertThat(preference.getSummary()).isEqualTo(
94 mContext.getString(R.string.disconnected));
98 public void testUsbBroadcastReceiver_usbConnected_shouldUpdateSummary() {
99 final Preference preference = new Preference(mContext);
100 preference.setKey("usb_mode");
101 preference.setEnabled(true);
102 when(mUsbBackend.getCurrentMode()).thenReturn(UsbBackend.MODE_POWER_SINK);
103 when(mUsbConnectionBroadcastReceiver.isConnected()).thenReturn(true);
104 mController.updateState(preference);
106 assertThat(preference.getSummary()).isEqualTo(
107 mContext.getString(R.string.usb_summary_charging_only));