1 package com.xtremelabs.robolectric.res; 2 3 import static com.xtremelabs.robolectric.util.TestUtil.resourceFile; 4 5 import org.junit.Before; 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 9 import android.app.Activity; 10 import android.preference.CheckBoxPreference; 11 import android.preference.EditTextPreference; 12 import android.preference.ListPreference; 13 import android.preference.Preference; 14 import android.preference.PreferenceCategory; 15 import android.preference.PreferenceScreen; 16 import android.preference.RingtonePreference; 17 18 import com.xtremelabs.robolectric.R; 19 import com.xtremelabs.robolectric.Robolectric; 20 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 21 import com.xtremelabs.robolectric.util.I18nException; 22 23 import static org.hamcrest.CoreMatchers.equalTo; 24 import static org.hamcrest.CoreMatchers.instanceOf; 25 import static org.junit.Assert.assertThat; 26 27 @RunWith(WithTestDefaultsRunner.class) 28 public class PreferenceLoaderTest { 29 private PreferenceLoader prefLoader; 30 31 @Before setUp()32 public void setUp() throws Exception { 33 Robolectric.bindDefaultShadowClasses(); 34 35 ResourceExtractor resourceExtractor = new ResourceExtractor(); 36 resourceExtractor.addLocalRClass(R.class); 37 StringResourceLoader stringResourceLoader = new StringResourceLoader(resourceExtractor); 38 new DocumentLoader(stringResourceLoader).loadResourceXmlDir(resourceFile("res", "values")); 39 prefLoader = new PreferenceLoader(resourceExtractor); 40 new DocumentLoader(prefLoader).loadResourceXmlDir(resourceFile("res", "xml")); 41 } 42 43 @Test shouldCreateCorrectClasses()44 public void shouldCreateCorrectClasses() { 45 PreferenceScreen screen = prefLoader.inflatePreferences(new Activity(), "xml/preferences"); 46 assertThatScreenMatchesExpected(screen); 47 } 48 49 @Test shouldLoadByResourceId()50 public void shouldLoadByResourceId() { 51 PreferenceScreen screen = prefLoader.inflatePreferences(new Activity(), R.xml.preferences); 52 assertThatScreenMatchesExpected(screen); 53 } 54 55 @Test(expected=I18nException.class) shouldThrowI18nExceptionOnPrefsWithBareStrings()56 public void shouldThrowI18nExceptionOnPrefsWithBareStrings() throws Exception { 57 ResourceExtractor resourceExtractor = new ResourceExtractor(); 58 resourceExtractor.addLocalRClass(R.class); 59 StringResourceLoader stringResourceLoader = new StringResourceLoader(resourceExtractor); 60 new DocumentLoader(stringResourceLoader).loadResourceXmlDir(resourceFile("res", "values")); 61 prefLoader = new PreferenceLoader(resourceExtractor); 62 prefLoader.setStrictI18n(true); 63 new DocumentLoader(prefLoader).loadResourceXmlDir(resourceFile("res", "xml")); 64 65 prefLoader.inflatePreferences(Robolectric.application, R.xml.preferences); 66 } 67 assertThatScreenMatchesExpected(PreferenceScreen screen)68 protected void assertThatScreenMatchesExpected(PreferenceScreen screen) { 69 assertThat(screen.getPreferenceCount(), equalTo(6)); 70 71 assertThat(screen.getPreference(0), instanceOf(PreferenceCategory.class)); 72 assertThat(((PreferenceCategory)screen.getPreference(0)).getPreference(0), instanceOf(Preference.class)); 73 74 assertThat(screen.getPreference(1), instanceOf(CheckBoxPreference.class)); 75 assertThat(screen.getPreference(2), instanceOf(EditTextPreference.class)); 76 assertThat(screen.getPreference(3), instanceOf(ListPreference.class)); 77 assertThat(screen.getPreference(4), instanceOf(Preference.class)); 78 assertThat(screen.getPreference(5), instanceOf(RingtonePreference.class)); 79 } 80 } 81