making preference reading code more robust

This commit is contained in:
alunny 2012-01-02 22:40:08 -08:00
parent 3af4d6b139
commit ffa76246e3
3 changed files with 27 additions and 2 deletions

View File

@ -329,7 +329,7 @@ public class DroidGap extends PhonegapActivity {
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
if (preferences.pref("fullscreen").equals("true")) {
if (preferences.prefMatches("fullscreen","true")) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
@ -1901,7 +1901,8 @@ public class DroidGap extends PhonegapActivity {
String value = xml.getAttributeValue(null, "value");
String readonlyString = xml.getAttributeValue(null, "readonly");
boolean readonly = (readonlyString.equals("true"));
boolean readonly = (readonlyString != null &&
readonlyString.equals("true"));
LOG.i("PhoneGapLog", "Found preference for %s", name);

View File

@ -30,4 +30,14 @@ public class PreferenceSet {
return null;
}
public boolean prefMatches(String prefName, String prefValue) {
String value = pref(prefName);
if (value == null) {
return false;
} else {
return value.equals(prefValue);
}
}
}

View File

@ -38,4 +38,18 @@ public class PreferenceSetTest {
// return null if the preference is not defined
assertEquals(null, preferences.pref("antigravity"));
}
@Test
public void testUnsetPreferenceChecking() {
PreferenceSet emptySet = new PreferenceSet();
boolean value = emptySet.prefMatches("fullscreen", "true");
assertEquals(false, value);
}
@Test
public void testSetPreferenceChecking() {
preferences.add(screen);
boolean value = preferences.prefMatches("fullscreen", "true");
assertEquals(true, value);
}
}