1package org.robolectric.android; 2 3import static org.assertj.core.api.Assertions.assertThat; 4import static org.robolectric.Shadows.shadowOf; 5 6import android.content.Intent; 7import android.net.Uri; 8import android.os.Bundle; 9import android.preference.CheckBoxPreference; 10import android.preference.EditTextPreference; 11import android.preference.ListPreference; 12import android.preference.Preference; 13import android.preference.PreferenceActivity; 14import android.preference.PreferenceCategory; 15import android.preference.PreferenceScreen; 16import android.preference.RingtonePreference; 17import android.util.MutableBoolean; 18import org.junit.Test; 19import org.junit.runner.RunWith; 20import org.robolectric.R; 21import org.robolectric.Robolectric; 22import org.robolectric.RobolectricTestRunner; 23 24@RunWith(RobolectricTestRunner.class) 25public class PreferenceIntegrationTest { 26 27 @Test 28 public void inflate_shouldCreateCorrectClasses() throws Exception { 29 final PreferenceScreen screen = inflatePreferenceActivity(); 30 assertThat(screen.getPreference(0)).isInstanceOf(PreferenceCategory.class); 31 32 PreferenceCategory category = (PreferenceCategory) screen.getPreference(0); 33 assertThat(category.getPreference(0)).isInstanceOf(Preference.class); 34 35 PreferenceScreen innerScreen = (PreferenceScreen) screen.getPreference(1); 36 assertThat(innerScreen).isInstanceOf(PreferenceScreen.class); 37 assertThat(innerScreen.getKey()).isEqualTo("screen"); 38 assertThat(innerScreen.getTitle().toString()).isEqualTo("Screen Test"); 39 assertThat(innerScreen.getSummary()).isEqualTo("Screen summary"); 40 assertThat(innerScreen.getPreference(0)).isInstanceOf(Preference.class); 41 42 assertThat(screen.getPreference(2)).isInstanceOf(CheckBoxPreference.class); 43 assertThat(screen.getPreference(3)).isInstanceOf(EditTextPreference.class); 44 assertThat(screen.getPreference(4)).isInstanceOf(ListPreference.class); 45 assertThat(screen.getPreference(5)).isInstanceOf(Preference.class); 46 assertThat(screen.getPreference(6)).isInstanceOf(RingtonePreference.class); 47 assertThat(screen.getPreference(7)).isInstanceOf(Preference.class); 48 } 49 50 @Test 51 public void inflate_shouldParseIntentContainedInPreference() throws Exception { 52 final PreferenceScreen screen = inflatePreferenceActivity(); 53 final Preference intentPreference = screen.findPreference("intent"); 54 55 Intent intent = intentPreference.getIntent(); 56 assertThat(intent).isNotNull(); 57 assertThat(intent.getAction()).isEqualTo("action"); 58 assertThat(intent.getData()).isEqualTo(Uri.parse("tel://1235")); 59 assertThat(intent.getType()).isEqualTo("application/text"); 60 assertThat(intent.getComponent().getClassName()).isEqualTo("org.robolectric.test.Intent"); 61 assertThat(intent.getComponent().getPackageName()).isEqualTo("org.robolectric"); 62 } 63 64 @Test 65 public void inflate_shouldBindPreferencesToPreferenceManager() throws Exception { 66 final PreferenceScreen screen = inflatePreferenceActivity(); 67 final Preference preference = screen.findPreference("preference"); 68 assertThat(preference.getPreferenceManager().findPreference("preference")).isNotNull(); 69 } 70 71 @Test 72 public void setPersistent_shouldMarkThePreferenceAsPersistent() throws Exception { 73 final PreferenceScreen screen = inflatePreferenceActivity(); 74 final Preference preference = screen.findPreference("preference"); 75 76 preference.setPersistent(true); 77 assertThat(preference.isPersistent()).isTrue(); 78 79 preference.setPersistent(false); 80 assertThat(preference.isPersistent()).isFalse(); 81 } 82 83 @Test 84 public void setEnabled_shouldEnableThePreference() throws Exception { 85 final PreferenceScreen screen = inflatePreferenceActivity(); 86 final Preference preference = screen.findPreference("preference"); 87 88 preference.setEnabled(true); 89 assertThat(preference.isEnabled()).isTrue(); 90 91 preference.setEnabled(false); 92 assertThat(preference.isEnabled()).isFalse(); 93 } 94 95 @Test 96 public void setOrder_shouldSetOrderOnPreference() throws Exception { 97 final PreferenceScreen screen = inflatePreferenceActivity(); 98 final Preference preference = screen.findPreference("preference"); 99 100 preference.setOrder(100); 101 assertThat(preference.getOrder()).isEqualTo(100); 102 103 preference.setOrder(50); 104 assertThat(preference.getOrder()).isEqualTo(50); 105 } 106 107 @Test 108 public void setDependency_shouldSetDependencyBetweenPreferences() throws Exception { 109 final PreferenceScreen screen = inflatePreferenceActivity(); 110 final Preference dependant = screen.findPreference("dependant"); 111 assertThat(dependant.getDependency()).isEqualTo("preference"); 112 113 dependant.setDependency(null); 114 assertThat(dependant.getDependency()).isNull(); 115 } 116 117 @Test 118 public void click_shouldCallPreferenceClickListener() throws Exception { 119 final PreferenceScreen screen = inflatePreferenceActivity(); 120 final Preference preference = screen.findPreference("preference"); 121 122 boolean[] holder = new boolean[1]; 123 preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { 124 @Override 125 public boolean onPreferenceClick(Preference preference) { 126 holder[0] = true; 127 return true; 128 } 129 }); 130 131 shadowOf(preference).click(); 132 assertThat(holder[0]).isTrue(); 133 } 134 135 private PreferenceScreen inflatePreferenceActivity() { 136 TestPreferenceActivity activity = Robolectric.setupActivity(TestPreferenceActivity.class); 137 return activity.getPreferenceScreen(); 138 } 139 140 @SuppressWarnings("FragmentInjection") 141 private static class TestPreferenceActivity extends PreferenceActivity { 142 @Override 143 protected void onCreate(Bundle savedInstanceState) { 144 super.onCreate(savedInstanceState); 145 addPreferencesFromResource(R.xml.preferences); 146 } 147 } 148} 149