1 package com.xtremelabs.robolectric.shadows; 2 3 import static org.hamcrest.CoreMatchers.equalTo; 4 import static org.hamcrest.CoreMatchers.instanceOf; 5 import static org.hamcrest.CoreMatchers.sameInstance; 6 import static org.junit.Assert.assertThat; 7 8 import java.util.HashMap; 9 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.junit.runner.RunWith; 13 14 import android.app.Activity; 15 import android.content.Context; 16 import android.preference.Preference; 17 import android.preference.PreferenceGroup; 18 import android.util.AttributeSet; 19 20 import com.xtremelabs.robolectric.Robolectric; 21 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 22 import com.xtremelabs.robolectric.tester.android.util.TestAttributeSet; 23 24 @RunWith(WithTestDefaultsRunner.class) 25 public class PreferenceGroupTest { 26 27 private TestPreferenceGroup group; 28 private ShadowPreferenceGroup shadow; 29 private Context context; 30 private TestAttributeSet attrs; 31 private Preference pref1, pref2; 32 33 @Before setUp()34 public void setUp() throws Exception { 35 context = new Activity(); 36 attrs = new TestAttributeSet( new HashMap<String, String>() ); 37 38 group = new TestPreferenceGroup(context, attrs); 39 shadow = Robolectric.shadowOf(group); 40 41 pref1 = new Preference(context); 42 pref1.setKey("pref1"); 43 44 pref2 = new Preference(context); 45 pref2.setKey("pref2"); 46 } 47 48 @Test shouldInheritFromPreference()49 public void shouldInheritFromPreference() { 50 assertThat(shadow, instanceOf(ShadowPreference.class)); 51 } 52 53 @Test shouldAddPreferences()54 public void shouldAddPreferences() { 55 assertThat( group.getPreferenceCount(), equalTo(0)); 56 57 // First add succeeds 58 assertThat( group.addPreference(pref1), equalTo(true)); 59 assertThat( group.getPreferenceCount(), equalTo(1)); 60 61 // Dupe add fails silently 62 assertThat( group.addPreference(pref1), equalTo(true)); 63 assertThat( group.getPreferenceCount(), equalTo(1)); 64 65 // Second add succeeds 66 assertThat( group.addPreference(pref2), equalTo(true)); 67 assertThat( group.getPreferenceCount(), equalTo(2)); 68 } 69 70 @Test shouldAddItemFromInflater()71 public void shouldAddItemFromInflater() { 72 assertThat( group.getPreferenceCount(), equalTo(0)); 73 74 // First add succeeds 75 group.addItemFromInflater(pref1); 76 assertThat( group.getPreferenceCount(), equalTo(1)); 77 78 // Dupe add fails silently 79 group.addItemFromInflater(pref1); 80 assertThat( group.getPreferenceCount(), equalTo(1)); 81 82 // Second add succeeds 83 group.addItemFromInflater(pref2); 84 assertThat( group.getPreferenceCount(), equalTo(2)); 85 } 86 87 @Test shouldGetPreference()88 public void shouldGetPreference() { 89 group.addPreference(pref1); 90 group.addPreference(pref2); 91 92 assertThat( group.getPreference(0), sameInstance(pref1)); 93 assertThat( group.getPreference(1), sameInstance(pref2)); 94 } 95 96 @Test shouldGetPreferenceCount()97 public void shouldGetPreferenceCount() { 98 assertThat( group.getPreferenceCount(), equalTo(0)); 99 group.addPreference(pref1); 100 assertThat( group.getPreferenceCount(), equalTo(1)); 101 group.addPreference(pref2); 102 assertThat( group.getPreferenceCount(), equalTo(2)); 103 } 104 105 @Test shouldRemovePreference()106 public void shouldRemovePreference() { 107 group.addPreference(pref1); 108 group.addPreference(pref2); 109 assertThat( group.getPreferenceCount(), equalTo(2)); 110 111 // First remove succeeds 112 assertThat( group.removePreference(pref1), equalTo(true)); 113 assertThat( group.getPreferenceCount(), equalTo(1)); 114 115 // Dupe remove fails 116 assertThat( group.removePreference(pref1), equalTo(false)); 117 assertThat( group.getPreferenceCount(), equalTo(1)); 118 119 // Second remove succeeds 120 assertThat( group.removePreference(pref2), equalTo(true)); 121 assertThat( group.getPreferenceCount(), equalTo(0)); 122 } 123 124 @Test shouldRemoveAll()125 public void shouldRemoveAll() { 126 group.addPreference(pref1); 127 group.addPreference(pref2); 128 assertThat( group.getPreferenceCount(), equalTo(2)); 129 130 group.removeAll(); 131 assertThat( group.getPreferenceCount(), equalTo(0)); 132 } 133 134 @Test shouldFindPreference()135 public void shouldFindPreference() { 136 group.addPreference(pref1); 137 group.addPreference(pref2); 138 139 assertThat( group.findPreference(pref1.getKey()), sameInstance(pref1)); 140 assertThat( group.findPreference(pref2.getKey()), sameInstance(pref2)); 141 } 142 143 @Test shouldFindPreferenceRecursively()144 public void shouldFindPreferenceRecursively() { 145 TestPreferenceGroup group2 = new TestPreferenceGroup(context, attrs); 146 group2.addPreference(pref2); 147 148 group.addPreference(pref1); 149 group.addPreference(group2); 150 151 assertThat( group.findPreference(pref2.getKey()), sameInstance(pref2)); 152 } 153 154 @Test shouldSetEnabledRecursively()155 public void shouldSetEnabledRecursively() { 156 boolean[] values = { false, true }; 157 158 TestPreferenceGroup group2 = new TestPreferenceGroup(context, attrs); 159 group2.addPreference(pref2); 160 161 group.addPreference(pref1); 162 group.addPreference(group2); 163 164 for( boolean enabled : values ) { 165 group.setEnabled(enabled); 166 167 assertThat(group.isEnabled(), equalTo(enabled)); 168 assertThat(group2.isEnabled(), equalTo(enabled)); 169 assertThat(pref1.isEnabled(), equalTo(enabled)); 170 assertThat(pref2.isEnabled(), equalTo(enabled)); 171 } 172 } 173 174 private static class TestPreferenceGroup extends PreferenceGroup { TestPreferenceGroup(Context context, AttributeSet attrs)175 public TestPreferenceGroup(Context context, AttributeSet attrs) { 176 super(context, attrs); 177 } 178 } 179 } 180