- since the choose usb mode dialog and the preference summary use separate strings now, the two classes can have their own handling for getting the title/summary text and hence revert the earlier change that move the setTitle() and setSummary() from the activity to controller - only show summary for the option for "supply power", and don't set summary for all other options - update the controller to use the new summary text. - update test correspondingly. Change-Id: Ie56eb33ba24262e6f3a2259e4ee5ad6f3bfb8061 Fix: 36234108 Test: make RunSettingsRoboTests
107 lines
3.8 KiB
Java
107 lines
3.8 KiB
Java
package com.android.settings.connecteddevice;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.hardware.usb.UsbManager;
|
|
import android.support.v7.preference.Preference;
|
|
import android.support.v7.preference.PreferenceScreen;
|
|
|
|
import com.android.settings.R;
|
|
import com.android.settings.SettingsRobolectricTestRunner;
|
|
import com.android.settings.TestConfig;
|
|
import com.android.settings.deviceinfo.UsbBackend;
|
|
import com.android.settings.deviceinfo.UsbModeChooserActivity;
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.Mock;
|
|
import org.mockito.MockitoAnnotations;
|
|
import org.robolectric.annotation.Config;
|
|
import org.robolectric.shadows.ShadowApplication;
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
|
import static org.mockito.Matchers.any;
|
|
import static org.mockito.Matchers.anyInt;
|
|
import static org.mockito.Matchers.anyString;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
@RunWith(SettingsRobolectricTestRunner.class)
|
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
|
public class UsbModePreferenceControllerTest {
|
|
|
|
@Mock(answer = RETURNS_DEEP_STUBS)
|
|
private UsbBackend mUsbBackend;
|
|
@Mock(answer = RETURNS_DEEP_STUBS)
|
|
private PreferenceScreen mScreen;
|
|
|
|
private Context mContext;
|
|
private UsbModePreferenceController mController;
|
|
|
|
@Before
|
|
public void setUp() {
|
|
MockitoAnnotations.initMocks(this);
|
|
mContext = ShadowApplication.getInstance().getApplicationContext();
|
|
mController = new UsbModePreferenceController(mContext, mUsbBackend);
|
|
}
|
|
|
|
@Test
|
|
public void testGetSummary_chargeDevice() {
|
|
assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[0]))
|
|
.isEqualTo(R.string.usb_summary_charging_only);
|
|
}
|
|
|
|
@Test
|
|
public void testGetSummary_supplyPower() {
|
|
assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[1]))
|
|
.isEqualTo(R.string.usb_summary_power_only);
|
|
}
|
|
|
|
@Test
|
|
public void testGetSummary_TransferFiles() {
|
|
assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[2]))
|
|
.isEqualTo(R.string.usb_summary_file_transfers);
|
|
}
|
|
|
|
@Test
|
|
public void testGetSummary_TransferPhoto() {
|
|
assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[3]))
|
|
.isEqualTo(R.string.usb_summary_photo_transfers);
|
|
}
|
|
|
|
@Test
|
|
public void testGetSummary_MIDI() {
|
|
assertThat(mController.getSummary(UsbModeChooserActivity.DEFAULT_MODES[4]))
|
|
.isEqualTo(R.string.usb_summary_MIDI);
|
|
}
|
|
|
|
@Test
|
|
public void testPreferenceSummary_usbDisconnected() {
|
|
final Preference preference = new Preference(mContext);
|
|
preference.setKey("usb_mode");
|
|
preference.setEnabled(true);
|
|
mController.updateState(preference);
|
|
assertThat(preference.getSummary()).isEqualTo(
|
|
mContext.getString(R.string.disconnected));
|
|
}
|
|
|
|
@Test
|
|
public void testUsbBoradcastReceiver_usbConnected_shouldUpdateSummary() {
|
|
final Preference preference = new Preference(mContext);
|
|
preference.setKey("usb_mode");
|
|
preference.setEnabled(true);
|
|
when(mUsbBackend.getCurrentMode()).thenReturn(UsbModeChooserActivity.DEFAULT_MODES[0]);
|
|
when(mScreen.findPreference("usb_mode")).thenReturn(preference);
|
|
|
|
mController.displayPreference(mScreen);
|
|
mController.onResume();
|
|
final Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
|
|
intent.putExtra(UsbManager.USB_CONNECTED, true);
|
|
mContext.sendStickyBroadcast(intent);
|
|
|
|
assertThat(preference.getSummary()).isEqualTo(
|
|
mContext.getString(R.string.usb_summary_charging_only));
|
|
}
|
|
|
|
} |