1eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreypackage com.xtremelabs.robolectric.shadows; 2eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphreyimport static org.hamcrest.CoreMatchers.equalTo; 4eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport static org.hamcrest.CoreMatchers.instanceOf; 565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphreyimport static org.hamcrest.CoreMatchers.sameInstance; 6eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport static org.junit.Assert.assertThat; 7eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 8eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport java.util.HashMap; 9eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 10eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport org.junit.Before; 11eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport org.junit.Test; 12eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport org.junit.runner.RunWith; 13eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 14eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport android.app.Activity; 15eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport android.content.Context; 1665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphreyimport android.preference.Preference; 17eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport android.preference.PreferenceGroup; 18eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport android.util.AttributeSet; 19eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 20eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport com.xtremelabs.robolectric.Robolectric; 21eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport com.xtremelabs.robolectric.WithTestDefaultsRunner; 22eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreyimport com.xtremelabs.robolectric.tester.android.util.TestAttributeSet; 23eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 24eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey@RunWith(WithTestDefaultsRunner.class) 25eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphreypublic class PreferenceGroupTest { 26eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 27eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey private TestPreferenceGroup group; 28eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey private ShadowPreferenceGroup shadow; 2965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey private Context context; 3065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey private TestAttributeSet attrs; 3165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey private Preference pref1, pref2; 32eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 33eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey @Before 34eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey public void setUp() throws Exception { 3565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey context = new Activity(); 3665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey attrs = new TestAttributeSet( new HashMap<String, String>() ); 37eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 38eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey group = new TestPreferenceGroup(context, attrs); 39eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey shadow = Robolectric.shadowOf(group); 4065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 4165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey pref1 = new Preference(context); 4265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey pref1.setKey("pref1"); 4365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 4465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey pref2 = new Preference(context); 4565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey pref2.setKey("pref2"); 46eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey } 47eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 48eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey @Test 49eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey public void shouldInheritFromPreference() { 50eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey assertThat(shadow, instanceOf(ShadowPreference.class)); 51eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey } 52eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey 5365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 5465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldAddPreferences() { 5565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(0)); 5665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 5765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // First add succeeds 5865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.addPreference(pref1), equalTo(true)); 5965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(1)); 6065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 6165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // Dupe add fails silently 6265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.addPreference(pref1), equalTo(true)); 6365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(1)); 6465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 6565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // Second add succeeds 6665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.addPreference(pref2), equalTo(true)); 6765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(2)); 6865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 6965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 7065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 7165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldAddItemFromInflater() { 7265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(0)); 7365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 7465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // First add succeeds 7565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addItemFromInflater(pref1); 7665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(1)); 7765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 7865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // Dupe add fails silently 7965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addItemFromInflater(pref1); 8065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(1)); 8165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 8265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // Second add succeeds 8365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addItemFromInflater(pref2); 8465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(2)); 8565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 8665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 8765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 8865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldGetPreference() { 8965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref1); 9065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref2); 9165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 9265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreference(0), sameInstance(pref1)); 9365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreference(1), sameInstance(pref2)); 9465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 9565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 9665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 9765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldGetPreferenceCount() { 9865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(0)); 9965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref1); 10065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(1)); 10165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref2); 10265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(2)); 10365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 10465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 10565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 10665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldRemovePreference() { 10765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref1); 10865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref2); 10965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(2)); 11065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 11165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // First remove succeeds 11265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.removePreference(pref1), equalTo(true)); 11365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(1)); 11465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 11565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // Dupe remove fails 11665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.removePreference(pref1), equalTo(false)); 11765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(1)); 11865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 11965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey // Second remove succeeds 12065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.removePreference(pref2), equalTo(true)); 12165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(0)); 12265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 12365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 12465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 12565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldRemoveAll() { 12665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref1); 12765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref2); 12865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(2)); 12965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 13065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.removeAll(); 13165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.getPreferenceCount(), equalTo(0)); 13265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 13365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 13465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 13565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldFindPreference() { 13665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref1); 13765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref2); 13865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 13965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.findPreference(pref1.getKey()), sameInstance(pref1)); 14065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.findPreference(pref2.getKey()), sameInstance(pref2)); 14165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 14265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 14365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 14465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldFindPreferenceRecursively() { 14565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey TestPreferenceGroup group2 = new TestPreferenceGroup(context, attrs); 14665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group2.addPreference(pref2); 14765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 14865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref1); 14965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(group2); 15065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 15165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat( group.findPreference(pref2.getKey()), sameInstance(pref2)); 15265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 15365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 15465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey @Test 15565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey public void shouldSetEnabledRecursively() { 15665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey boolean[] values = { false, true }; 15765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 15865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey TestPreferenceGroup group2 = new TestPreferenceGroup(context, attrs); 15965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group2.addPreference(pref2); 16065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 16165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(pref1); 16265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.addPreference(group2); 16365b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 16465b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey for( boolean enabled : values ) { 16565b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey group.setEnabled(enabled); 16665b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey 16765b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat(group.isEnabled(), equalTo(enabled)); 16865b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat(group2.isEnabled(), equalTo(enabled)); 16965b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat(pref1.isEnabled(), equalTo(enabled)); 17065b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey assertThat(pref2.isEnabled(), equalTo(enabled)); 17165b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 17265b198f024aea2f29ce34287bbe6027a632d37b1Rich Humphrey } 1737598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz 1747598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz private static class TestPreferenceGroup extends PreferenceGroup { 175eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey public TestPreferenceGroup(Context context, AttributeSet attrs) { 176eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey super(context, attrs); 177eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey } 178eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey } 179eb47a8c4f989358eb087720918ff77a8e1e6f260Rich Humphrey} 180