BooleanPreference extends Preference<Boolean> { @Nullable @Override public Boolean get() { if (contains()) { return readSharedPreferences().getBoolean(getKey(), false); } else { return getDefaultValue(); } } BooleanPreference(final String name, final String key,
            @Nullable final Boolean defaultValue); @Override boolean exists(); @Nullable @Override Boolean get(); void toggle();  }
@Test public void testAddListener() throws Exception { final Boolean[] booleans = { Boolean.FALSE }; mNullPref.addListener(new OnPreferenceChangeListener<Boolean>() { @Override public void onPreferenceChange(final Preference<Boolean> preference) { booleans[0] = preference.get(); } }); assertFalse(booleans[0]); mNullPref.set(Boolean.TRUE); assertTrue(booleans[0]); mNullPref.set(Boolean.FALSE, true); assertFalse(booleans[0]); mNullPref.set((Boolean) null, true); assertNull(booleans[0]); }
@Test public void testGet() throws Exception { assertNull(mNullPref.get()); mNullPref.set(Boolean.TRUE); assertTrue(mNullPref.get()); assertFalse(mFalsePref.get()); mFalsePref.set(Boolean.FALSE); assertFalse(mFalsePref.get()); assertTrue(mTruePref.get()); mTruePref.set(Boolean.FALSE); assertFalse(mTruePref.get()); mTruePref.set((Boolean) null); assertTrue(mTruePref.get()); }
@Test public void testRemoveListener() { final Boolean[] booleans = { Boolean.FALSE }; final OnPreferenceChangeListener<Boolean> opcl = new OnPreferenceChangeListener<Boolean>() { @Override public void onPreferenceChange(final Preference<Boolean> preference) { booleans[0] = preference.get(); } }; mNullPref.addListener(opcl); assertFalse(booleans[0]); mNullPref.set(Boolean.TRUE); assertTrue(booleans[0]); mNullPref.set(Boolean.FALSE); assertFalse(booleans[0]); mNullPref.removeListener(opcl); mNullPref.set(Boolean.TRUE); assertFalse(booleans[0]); }
@Test public void testSet() throws Exception { assertNull(mNullPref.get()); mNullPref.set(Boolean.TRUE); assertTrue(mNullPref.get()); assertFalse(mFalsePref.get()); mFalsePref.set(Boolean.FALSE); assertFalse(mFalsePref.get()); mFalsePref.set(Boolean.TRUE); assertTrue(mFalsePref.get()); assertTrue(mTruePref.get()); mTruePref.set(Boolean.TRUE); assertTrue(mTruePref.get()); mTruePref.set(Boolean.FALSE); assertFalse(mTruePref.get()); }