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