Use binary resource support in robolectric

The resources available to tests are now exactly the merged resources
located in the APK under test.

Bug: 74359828
Test: make -j56 RunSettingsRoboTests
Change-Id: I050db81a92decefea23314b5ec7a62f77ff4bb2b
This commit is contained in:
James Lemieux
2018-12-07 12:56:49 -08:00
committed by Fan Zhang
parent 1f5fab1480
commit f1dade40d2
1005 changed files with 3187 additions and 5478 deletions

View File

@@ -21,8 +21,8 @@ import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_HIGH;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
@@ -48,23 +48,20 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settingslib.RestrictedLockUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = SettingsShadowResources.class)
@RunWith(RobolectricTestRunner.class)
public class SoundPreferenceControllerTest {
private Context mContext;
@@ -89,20 +86,13 @@ public class SoundPreferenceControllerTest {
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
SettingsShadowResources.overrideResource(com.android.internal.R.string.ringtone_silent,
"silent");
mContext = RuntimeEnvironment.application;
mController = spy(new SoundPreferenceController(
mContext, mFragment, mImportanceListener, mBackend));
}
@After
public void tearDown() {
SettingsShadowResources.reset();
}
@Test
public void testNoCrashIfNoOnResume() throws Exception {
public void testNoCrashIfNoOnResume() {
mController.isAvailable();
mController.updateState(mock(NotificationSoundPreference.class));
mController.onPreferenceChange(mock(NotificationSoundPreference.class), Uri.EMPTY);
@@ -112,14 +102,14 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testIsAvailable_notIfChannelNull() throws Exception {
public void testIsAvailable_notIfChannelNull() {
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
mController.onResume(appRow, null, null, null);
assertFalse(mController.isAvailable());
}
@Test
public void testIsAvailable_notIfNotImportant() throws Exception {
public void testIsAvailable_notIfNotImportant() {
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_LOW);
mController.onResume(appRow, channel, null, null);
@@ -127,7 +117,7 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testIsAvailable_notIfDefaultChannel() throws Exception {
public void testIsAvailable_notIfDefaultChannel() {
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
NotificationChannel channel =
new NotificationChannel(DEFAULT_CHANNEL_ID, "", IMPORTANCE_DEFAULT);
@@ -136,7 +126,7 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testIsAvailable() throws Exception {
public void testIsAvailable() {
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_DEFAULT);
mController.onResume(appRow, channel, null, null);
@@ -144,7 +134,7 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testDisplayPreference_savesPreference() throws Exception {
public void testDisplayPreference_savesPreference() {
NotificationSoundPreference pref = mock(NotificationSoundPreference.class);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(pref);
mController.displayPreference(mScreen);
@@ -154,20 +144,21 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testUpdateState_disabledByAdmin() throws Exception {
public void testUpdateState_disabledByAdmin() {
NotificationChannel channel = mock(NotificationChannel.class);
when(channel.getId()).thenReturn("something");
mController.onResume(new NotificationBackend.AppRow(), channel, null, mock(
RestrictedLockUtils.EnforcedAdmin.class));
Preference pref = new NotificationSoundPreference(mContext, mock(AttributeSet.class));
AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
Preference pref = new NotificationSoundPreference(mContext, attributeSet);
mController.updateState(pref);
assertFalse(pref.isEnabled());
}
@Test
public void testUpdateState_notConfigurable() throws Exception {
public void testUpdateState_notConfigurable() {
String lockedId = "locked";
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
appRow.lockedChannelId = lockedId;
@@ -175,14 +166,15 @@ public class SoundPreferenceControllerTest {
when(channel.getId()).thenReturn(lockedId);
mController.onResume(appRow, channel, null, null);
Preference pref = new NotificationSoundPreference(mContext, mock(AttributeSet.class));
AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
Preference pref = new NotificationSoundPreference(mContext, attributeSet);
mController.updateState(pref);
assertFalse(pref.isEnabled());
}
@Test
public void testUpdateState_configurable() throws Exception {
public void testUpdateState_configurable() {
Uri sound = Settings.System.DEFAULT_ALARM_ALERT_URI;
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
NotificationChannel channel = mock(NotificationChannel.class);
@@ -190,8 +182,8 @@ public class SoundPreferenceControllerTest {
when(channel.getSound()).thenReturn(sound);
mController.onResume(appRow, channel, null, null);
NotificationSoundPreference pref =
new NotificationSoundPreference(mContext, mock(AttributeSet.class));
AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
NotificationSoundPreference pref = new NotificationSoundPreference(mContext, attributeSet);
mController.updateState(pref);
assertEquals(sound, pref.onRestoreRingtone());
@@ -199,15 +191,16 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testOnPreferenceChange() throws Exception {
public void testOnPreferenceChange() {
Uri sound = Settings.System.DEFAULT_ALARM_ALERT_URI;
NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
NotificationChannel channel = new NotificationChannel("", "", IMPORTANCE_HIGH);
channel.setSound(sound, Notification.AUDIO_ATTRIBUTES_DEFAULT);
mController.onResume(appRow, channel, null, null);
AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
NotificationSoundPreference pref =
new NotificationSoundPreference(mContext, mock(AttributeSet.class));
new NotificationSoundPreference(mContext, attributeSet);
mController.updateState(pref);
mController.onPreferenceChange(pref, Uri.EMPTY);
@@ -217,7 +210,7 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testOnPreferenceTreeClick_incorrectPref() throws Exception {
public void testOnPreferenceTreeClick_incorrectPref() {
NotificationSoundPreference pref = mock(NotificationSoundPreference.class);
mController.handlePreferenceTreeClick(pref);
@@ -226,9 +219,10 @@ public class SoundPreferenceControllerTest {
}
@Test
public void testOnPreferenceTreeClick_correctPref() throws Exception {
public void testOnPreferenceTreeClick_correctPref() {
AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
NotificationSoundPreference pref =
spy(new NotificationSoundPreference(mContext, mock(AttributeSet.class)));
spy(new NotificationSoundPreference(mContext, attributeSet));
pref.setKey(mController.getPreferenceKey());
mController.handlePreferenceTreeClick(pref);