1package com.xtremelabs.robolectric.shadows; 2 3import static org.hamcrest.CoreMatchers.equalTo; 4import static org.hamcrest.CoreMatchers.instanceOf; 5import static org.hamcrest.CoreMatchers.nullValue; 6import static org.hamcrest.CoreMatchers.notNullValue; 7import static org.junit.Assert.assertThat; 8 9import org.junit.Before; 10import org.junit.Test; 11import org.junit.runner.RunWith; 12 13import android.app.Activity; 14import android.preference.PreferenceActivity; 15import android.widget.ListView; 16 17import com.xtremelabs.robolectric.R; 18import com.xtremelabs.robolectric.Robolectric; 19import com.xtremelabs.robolectric.WithTestDefaultsRunner; 20 21@RunWith(WithTestDefaultsRunner.class) 22public class PreferenceActivityTest { 23 24 private TestPreferenceActivity activity; 25 private ShadowPreferenceActivity shadow; 26 27 @Before 28 public void setUp() throws Exception { 29 activity = new TestPreferenceActivity(); 30 shadow = Robolectric.shadowOf(activity); 31 } 32 33 @Test 34 public void shouldGetListView() { 35 shadow.setListView( new ListView( new Activity() ) ); 36 assertThat( activity.getListView(), notNullValue() ); 37 } 38 39 @Test 40 public void shouldInheritFromListActivity() { 41 assertThat(shadow, instanceOf(ShadowListActivity.class)); 42 } 43 44 @Test 45 public void shouldNotInitializePreferenceScreen() { 46 assertThat(activity.getPreferenceScreen(), nullValue()); 47 } 48 49 @Test 50 public void shouldRecordPreferencesResourceId() { 51 assertThat(shadow.getPreferencesResId(), equalTo(-1)); 52 activity.addPreferencesFromResource(R.xml.preferences); 53 assertThat(shadow.getPreferencesResId(), equalTo(R.xml.preferences)); 54 } 55 56 @Test 57 public void shouldLoadPreferenceScreen() { 58 activity.addPreferencesFromResource(R.xml.preferences); 59 assertThat( activity.getPreferenceScreen().getPreferenceCount(), equalTo(6)); 60 } 61 62 private static class TestPreferenceActivity extends PreferenceActivity { 63 } 64} 65