Bug 1243049 - Update robocop tests to use new prefs API; r=gbrown

Change old robocop prefs API to the new API and add helper classes for
getting prefs. Also switch all tests that use prefs to use the new API.
This commit is contained in:
Jim Chen 2016-02-01 17:38:14 -05:00
parent d9e0ce33d2
commit 5bffc62e04
10 changed files with 253 additions and 280 deletions

View File

@ -51,28 +51,39 @@ public interface Actions {
*/
void sendGeckoEvent(String geckoEvent, String data);
/**
* Sends a preferences get event to Gecko.
*
* @param requestId The id of this request.
* @param prefNames The preferences being requested.
*/
void sendPreferencesGetEvent(int requestId, String[] prefNames);
public interface PrefWaiter {
boolean isFinished();
void waitForFinish();
void waitForFinish(long timeoutMillis, boolean failOnTimeout);
}
/**
* Sends a preferences observe event to Gecko.
*
* @param requestId The id of this request.
* @param prefNames The preferences being requested.
*/
void sendPreferencesObserveEvent(int requestId, String[] prefNames);
public abstract static class PrefHandlerBase implements PrefsHelper.PrefHandler {
/* package */ Assert asserter;
/**
* Sends a preferences remove observers event to Gecko.
*
* @param requestId The id of this request.
*/
void sendPreferencesRemoveObserversEvent(int requestid);
@Override // PrefsHelper.PrefHandler
public void prefValue(String pref, boolean value) {
asserter.ok(false, "Unexpected pref callback", "");
}
@Override // PrefsHelper.PrefHandler
public void prefValue(String pref, int value) {
asserter.ok(false, "Unexpected pref callback", "");
}
@Override // PrefsHelper.PrefHandler
public void prefValue(String pref, String value) {
asserter.ok(false, "Unexpected pref callback", "");
}
@Override // PrefsHelper.PrefHandler
public void finish() {
}
}
PrefWaiter getPrefs(String[] prefNames, PrefHandlerBase handler);
void setPref(String pref, Object value, boolean flush);
PrefWaiter addPrefsObserver(String[] prefNames, PrefHandlerBase handler);
void removePrefsObserver(PrefWaiter handler);
/**
* Listens for a gecko event to be sent from the Gecko instance.

View File

@ -4,6 +4,7 @@
package org.mozilla.gecko;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
@ -190,16 +191,112 @@ public class FennecNativeActions implements Actions {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(geckoEvent, data));
}
public void sendPreferencesGetEvent(int requestId, String[] prefNames) {
PrefsHelper.getPrefsById(requestId, prefNames, /* observe */ false);
public static final class PrefProxy implements PrefsHelper.PrefHandler, PrefWaiter {
public static final int MAX_WAIT_MS = 180000;
/* package */ final PrefHandlerBase target;
private final String[] expectedPrefs;
private final ArrayList<String> seenPrefs = new ArrayList<>();
private boolean finished = false;
/* package */ PrefProxy(PrefHandlerBase target, String[] expectedPrefs, Assert asserter) {
this.target = target;
this.expectedPrefs = expectedPrefs;
target.asserter = asserter;
}
@Override // PrefsHelper.PrefHandler
public void prefValue(String pref, boolean value) {
target.prefValue(pref, value);
seenPrefs.add(pref);
}
@Override // PrefsHelper.PrefHandler
public void prefValue(String pref, int value) {
target.prefValue(pref, value);
seenPrefs.add(pref);
}
@Override // PrefsHelper.PrefHandler
public void prefValue(String pref, String value) {
target.prefValue(pref, value);
seenPrefs.add(pref);
}
@Override // PrefsHelper.PrefHandler
public synchronized void finish() {
target.finish();
for (String pref : expectedPrefs) {
target.asserter.ok(seenPrefs.remove(pref), "Checking pref was seen", pref);
}
target.asserter.ok(seenPrefs.isEmpty(), "Checking unexpected prefs",
TextUtils.join(", ", seenPrefs));
finished = true;
this.notifyAll();
}
@Override // PrefWaiter
public synchronized boolean isFinished() {
return finished;
}
@Override // PrefWaiter
public void waitForFinish() {
waitForFinish(MAX_WAIT_MS, /* failOnTimeout */ true);
}
@Override // PrefWaiter
public synchronized void waitForFinish(long timeoutMillis, boolean failOnTimeout) {
final long startTime = System.nanoTime();
while (!finished) {
if (System.nanoTime() - startTime
>= timeoutMillis * 1e6 /* ns per ms */) {
final String prefsLog = "expected " +
TextUtils.join(", ", expectedPrefs) + "; got " +
TextUtils.join(", ", seenPrefs.toArray()) + ".";
if (failOnTimeout) {
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
target.asserter.ok(false, "Timeout waiting for pref", prefsLog);
} else {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"Pref timeout (" + prefsLog + ")");
}
break;
}
try {
this.wait(1000); // Wait for 1 second at a time.
} catch (final InterruptedException e) {
// Attempt waiting again.
}
}
finished = false;
}
}
public void sendPreferencesObserveEvent(int requestId, String[] prefNames) {
PrefsHelper.getPrefsById(requestId, prefNames, /* observe */ true);
@Override // Actions
public PrefWaiter getPrefs(String[] prefNames, PrefHandlerBase handler) {
final PrefProxy proxy = new PrefProxy(handler, prefNames, mAsserter);
PrefsHelper.getPrefs(prefNames, proxy);
return proxy;
}
public void sendPreferencesRemoveObserversEvent(int requestId) {
PrefsHelper.removePrefsObserver(requestId);
@Override // Actions
public void setPref(String pref, Object value, boolean flush) {
PrefsHelper.setPref(pref, value, flush);
}
@Override // Actions
public PrefWaiter addPrefsObserver(String[] prefNames, PrefHandlerBase handler) {
final PrefProxy proxy = new PrefProxy(handler, prefNames, mAsserter);
PrefsHelper.addObserver(prefNames, proxy);
return proxy;
}
@Override // Actions
public void removePrefsObserver(PrefWaiter proxy) {
PrefsHelper.removeObserver((PrefProxy) proxy);
}
class PaintExpecter implements RepeatedEventExpecter {

View File

@ -62,11 +62,9 @@ abstract class BaseTest extends BaseRobocopTest {
public static final int MAX_WAIT_MS = 4500;
public static final int LONG_PRESS_TIME = 6000;
private static final int GECKO_READY_WAIT_MS = 180000;
public static final int MAX_WAIT_BLOCK_FOR_EVENT_DATA_MS = 90000;
protected static final String URL_HTTP_PREFIX = "http://";
private int mPreferenceRequestID = 0;
public Device mDevice;
protected DatabaseHelper mDatabaseHelper;
protected int mScreenMidWidth;
@ -886,60 +884,34 @@ abstract class BaseTest extends BaseRobocopTest {
/**
* Set the preference and wait for it to change before proceeding with the test.
*/
public void setPreferenceAndWaitForChange(final JSONObject jsonPref) {
public void setPreferenceAndWaitForChange(final String name, final Object value) {
blockForGeckoReady();
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
// Get the preference name from the json and store it in an array. This array
// will be used later while fetching the preference data.
String[] prefNames = new String[1];
try {
prefNames[0] = jsonPref.getString("name");
} catch (JSONException e) {
mAsserter.ok(false, "Exception in setPreferenceAndWaitForChange", getStackTraceString(e));
}
mActions.setPref(name, value, /* flush */ false);
// Wait for confirmation of the pref change before proceeding with the test.
final int ourRequestID = mPreferenceRequestID--;
final Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestID, prefNames);
mActions.getPrefs(new String[] { name }, new Actions.PrefHandlerBase() {
// Wait until we get the correct "Preferences:Data" event
waitForCondition(new Condition() {
final long endTime = SystemClock.elapsedRealtime() + MAX_WAIT_BLOCK_FOR_EVENT_DATA_MS;
@Override
public boolean isSatisfied() {
try {
long timeout = endTime - SystemClock.elapsedRealtime();
if (timeout < 0) {
timeout = 0;
}
JSONObject data = new JSONObject(eventExpecter.blockForEventDataWithTimeout(timeout));
int requestID = data.getInt("requestId");
if (requestID != ourRequestID) {
return false;
}
JSONArray preferences = data.getJSONArray("preferences");
mAsserter.is(preferences.length(), 1, "Expecting preference array to have one element");
JSONObject prefs = (JSONObject) preferences.get(0);
mAsserter.is(prefs.getString("name"), jsonPref.getString("name"),
"Expecting returned preference name to be the same as the set name");
mAsserter.is(prefs.getString("type"), jsonPref.getString("type"),
"Expecting returned preference type to be the same as the set type");
mAsserter.is(prefs.get("value"), jsonPref.get("value"),
"Expecting returned preference value to be the same as the set value");
return true;
} catch(JSONException e) {
mAsserter.ok(false, "Exception in setPreferenceAndWaitForChange", getStackTraceString(e));
// Please the java compiler
return false;
}
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, boolean changedValue) {
mAsserter.is(pref, name, "Expecting correct pref name");
mAsserter.ok(value instanceof Boolean, "Expecting boolean pref", "");
mAsserter.is(changedValue, value, "Expecting matching pref value");
}
}, MAX_WAIT_BLOCK_FOR_EVENT_DATA_MS);
eventExpecter.unregisterListener();
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, int changedValue) {
mAsserter.is(pref, name, "Expecting correct pref name");
mAsserter.ok(value instanceof Integer, "Expecting int pref", "");
mAsserter.is(changedValue, value, "Expecting matching pref value");
}
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, String changedValue) {
mAsserter.is(pref, name, "Expecting correct pref name");
mAsserter.ok(value instanceof CharSequence, "Expecting string pref", "");
mAsserter.is(changedValue, value, "Expecting matching pref value");
}
}).waitForFinish();
}
}

View File

@ -23,15 +23,7 @@ public class testAdobeFlash extends PixelTest {
}
// Enable plugins
JSONObject jsonPref = new JSONObject();
try {
jsonPref.put("name", "plugin.enable");
jsonPref.put("type", "string");
jsonPref.put("value", "1");
setPreferenceAndWaitForChange(jsonPref);
} catch (Exception ex) {
mAsserter.ok(false, "exception in testAdobeFlash", ex.toString());
}
setPreferenceAndWaitForChange("plugin.enable", "1");
blockForGeckoReady();

View File

@ -16,15 +16,7 @@ public class testCheck2 extends PixelTest {
String url = getAbsoluteUrl("/startup_test/fennecmark/cnn/cnn.com/index.html");
// Enable double-tap zooming
JSONObject jsonPref = new JSONObject();
try {
jsonPref.put("name", "browser.ui.zoom.force-user-scalable");
jsonPref.put("type", "bool");
jsonPref.put("value", true);
setPreferenceAndWaitForChange(jsonPref);
} catch (Exception ex) {
mAsserter.ok(false, "exception in testCheck2", ex.toString());
}
setPreferenceAndWaitForChange("browser.ui.zoom.force-user-scalable", true);
blockForGeckoReady();
loadAndPaint(url);

View File

@ -16,15 +16,7 @@ public class testCheck3 extends PixelTest {
String url = getAbsoluteUrl("/facebook.com/www.facebook.com/barackobama.html");
// Enable double-tap zooming
JSONObject jsonPref = new JSONObject();
try {
jsonPref.put("name", "browser.ui.zoom.force-user-scalable");
jsonPref.put("type", "bool");
jsonPref.put("value", true);
setPreferenceAndWaitForChange(jsonPref);
} catch (Exception ex) {
mAsserter.ok(false, "exception in testCheck3", ex.toString());
}
setPreferenceAndWaitForChange("browser.ui.zoom.force-user-scalable", true);
blockForGeckoReady();
loadAndPaint(url);

View File

@ -392,20 +392,36 @@ public class testDistribution extends ContentProviderTest {
}
private JSONArray getPrefs(String[] prefNames) throws JSONException {
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(PREF_REQUEST_ID, prefNames);
final JSONArray result = new JSONArray();
JSONObject data = null;
int requestId = -1;
mActions.getPrefs(prefNames, new Actions.PrefHandlerBase() {
private void addItem(String pref, Object value) {
try {
final JSONObject item = new JSONObject();
item.put("name", pref).put("value", value);
result.put(item);
} catch (final JSONException e) {
mAsserter.ok(false, "exception getting prefs", e.toString());
}
}
// Wait until we get the correct "Preferences:Data" event
while (requestId != PREF_REQUEST_ID) {
data = new JSONObject(eventExpecter.blockForEventData());
requestId = data.getInt("requestId");
}
eventExpecter.unregisterListener();
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, boolean value) {
addItem(pref, value);
}
return data.getJSONArray("preferences");
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, int value) {
addItem(pref, value);
}
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, String value) {
addItem(pref, value);
}
}).waitForFinish();
return result;
}
// Sets the distribution locale preference for the test.
@ -414,56 +430,39 @@ public class testDistribution extends ContentProviderTest {
}
// Test localized distribution and preferences values stored in preferences.json
private void checkLocalizedPreferences(String aLocale) {
String prefAbout = "distribution.about";
String prefLocalizeable = "distribution.test.localizeable";
String prefLocalizeableOverride = "distribution.test.localizeable-override";
try {
final String[] prefNames = { prefAbout, prefLocalizeable, prefLocalizeableOverride };
Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(PREF_REQUEST_ID, prefNames);
JSONObject data = null;
int requestId = -1;
// Wait until we get the correct "Preferences:Data" event
while (requestId != PREF_REQUEST_ID) {
data = new JSONObject(eventExpecter.blockForEventData());
requestId = data.getInt("requestId");
}
eventExpecter.unregisterListener();
JSONArray preferences = data.getJSONArray("preferences");
for (int i = 0; i < preferences.length(); i++) {
JSONObject pref = (JSONObject) preferences.get(i);
String name = pref.getString("name");
private void checkLocalizedPreferences(final String aLocale) {
final String prefAbout = "distribution.about";
final String prefLocalizeable = "distribution.test.localizeable";
final String prefLocalizeableOverride = "distribution.test.localizeable-override";
final String[] prefNames = { prefAbout, prefLocalizeable, prefLocalizeableOverride };
mActions.getPrefs(prefNames, new Actions.PrefHandlerBase() {
@Override // Actions.PrefHandlerBase
public void prefValue(String name, String value) {
if (name.equals(prefAbout)) {
if (aLocale.equals("en-US")) {
mAsserter.is(pref.getString("value"), "Test Partner", "check " + prefAbout);
mAsserter.is(value, "Test Partner", "check " + prefAbout);
} else if (aLocale.equals("es-MX")) {
mAsserter.is(pref.getString("value"), "Afiliado de Prueba", "check " + prefAbout);
mAsserter.is(value, "Afiliado de Prueba", "check " + prefAbout);
}
} else if (name.equals(prefLocalizeable)) {
if (aLocale.equals("en-US")) {
mAsserter.is(pref.getString("value"), "http://test.org/en-US/en-US/", "check " + prefLocalizeable);
mAsserter.is(value, "http://test.org/en-US/en-US/", "check " + prefLocalizeable);
} else if (aLocale.equals("es-MX")) {
mAsserter.is(pref.getString("value"), "http://test.org/es-MX/es-MX/", "check " + prefLocalizeable);
mAsserter.is(value, "http://test.org/es-MX/es-MX/", "check " + prefLocalizeable);
}
} else if (name.equals(prefLocalizeableOverride)) {
if (aLocale.equals("en-US")) {
mAsserter.is(pref.getString("value"), "http://cheese.com", "check " + prefLocalizeableOverride);
mAsserter.is(value, "http://cheese.com", "check " + prefLocalizeableOverride);
} else if (aLocale.equals("es-MX")) {
mAsserter.is(pref.getString("value"), "http://test.org/es-MX/", "check " + prefLocalizeableOverride);
mAsserter.is(value, "http://test.org/es-MX/", "check " + prefLocalizeableOverride);
}
} else {
// Raise exception.
super.prefValue(name, value);
}
}
} catch (JSONException e) {
mAsserter.ok(false, "exception getting preferences", e.toString());
}
}).waitForFinish();
}
// Copies the mock package to the data directory and returns the file path to it.

View File

@ -20,6 +20,8 @@ import org.mozilla.gecko.Actions;
* Password Manager doorhangers - Remember and Not Now options dismiss the doorhanger
*/
public class testDoorHanger extends BaseTest {
private boolean offlineAllowedByDefault = true;
public void testDoorHanger() {
String GEO_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_GEOLOCATION_URL);
String BLANK_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_BLANK_PAGE_01_URL);
@ -62,38 +64,17 @@ public class testDoorHanger extends BaseTest {
mAsserter.is(mSolo.searchText(GEO_MESSAGE), false, "Geolocation doorhanger notification is hidden when opening a new tab");
*/
boolean offlineAllowedByDefault = true;
// Save offline-allow-by-default preferences first
final String[] prefNames = { "offline-apps.allow_by_default" };
final int ourRequestId = 0x7357;
final Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesGetEvent(ourRequestId, prefNames);
try {
JSONObject data = null;
int requestId = -1;
// Wait until we get the correct "Preferences:Data" event
while (requestId != ourRequestId) {
data = new JSONObject(eventExpecter.blockForEventData());
requestId = data.getInt("requestId");
mActions.getPrefs(new String[] { "offline-apps.allow_by_default" },
new Actions.PrefHandlerBase() {
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, boolean value) {
mAsserter.is(pref, "offline-apps.allow_by_default", "Expecting correct pref name");
offlineAllowedByDefault = value;
}
eventExpecter.unregisterListener();
}).waitForFinish();
JSONArray preferences = data.getJSONArray("preferences");
if (preferences.length() > 0) {
JSONObject pref = (JSONObject) preferences.get(0);
offlineAllowedByDefault = pref.getBoolean("value");
}
// Turn off offline-allow-by-default
JSONObject jsonPref = new JSONObject();
jsonPref.put("name", "offline-apps.allow_by_default");
jsonPref.put("type", "bool");
jsonPref.put("value", false);
setPreferenceAndWaitForChange(jsonPref);
} catch (JSONException e) {
mAsserter.ok(false, "exception getting preference", e.toString());
}
setPreferenceAndWaitForChange("offline-apps.allow_by_default", false);
// Load offline storage page
loadUrlAndWait(OFFLINE_STORAGE_URL);
@ -117,16 +98,8 @@ public class testDoorHanger extends BaseTest {
loadUrlAndWait(OFFLINE_STORAGE_URL);
mAsserter.is(mSolo.searchText(mStringHelper.OFFLINE_MESSAGE), false, "Offline storage doorhanger is no longer triggered");
try {
// Revert offline setting
JSONObject jsonPref = new JSONObject();
jsonPref.put("name", "offline-apps.allow_by_default");
jsonPref.put("type", "bool");
jsonPref.put("value", offlineAllowedByDefault);
setPreferenceAndWaitForChange(jsonPref);
} catch (JSONException e) {
mAsserter.ok(false, "exception setting preference", e.toString());
}
// Revert offline setting
setPreferenceAndWaitForChange("offline-apps.allow_by_default", offlineAllowedByDefault);
// Load new login page
loadUrlAndWait(getAbsoluteUrl(mStringHelper.ROBOCOP_LOGIN_01_URL));
@ -152,15 +125,7 @@ public class testDoorHanger extends BaseTest {
private void testPopupBlocking() {
String POPUP_URL = getAbsoluteUrl(mStringHelper.ROBOCOP_POPUP_URL);
try {
JSONObject jsonPref = new JSONObject();
jsonPref.put("name", "dom.disable_open_during_load");
jsonPref.put("type", "bool");
jsonPref.put("value", true);
setPreferenceAndWaitForChange(jsonPref);
} catch (JSONException e) {
mAsserter.ok(false, "exception setting preference", e.toString());
}
setPreferenceAndWaitForChange("dom.disable_open_during_load", true);
// Load page with popup
loadUrlAndWait(POPUP_URL);
@ -204,15 +169,7 @@ public class testDoorHanger extends BaseTest {
// Check that we're on the same page to verify that the popup was not shown.
verifyUrl(POPUP_URL);
try {
JSONObject jsonPref = new JSONObject();
jsonPref.put("name", "dom.disable_open_during_load");
jsonPref.put("type", "bool");
jsonPref.put("value", false);
setPreferenceAndWaitForChange(jsonPref);
} catch (JSONException e) {
mAsserter.ok(false, "exception setting preference", e.toString());
}
setPreferenceAndWaitForChange("dom.disable_open_during_load", false);
}
// wait for a CheckBox view that is clickable

View File

@ -106,15 +106,7 @@ public class testPasswordEncrypt extends BaseTest {
}
private void toggleMasterPassword(String passwd) {
JSONObject jsonPref = new JSONObject();
try {
jsonPref.put("name", "privacy.masterpassword.enabled");
jsonPref.put("type", "string");
jsonPref.put("value", passwd);
setPreferenceAndWaitForChange(jsonPref);
} catch (Exception ex) {
mAsserter.ok(false, "exception in toggleMasterPassword", ex.toString());
}
setPreferenceAndWaitForChange("privacy.masterpassword.enabled", passwd);
}
@Override

View File

@ -4,8 +4,6 @@
package org.mozilla.gecko.tests;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.Actions;
/**
@ -16,97 +14,68 @@ import org.mozilla.gecko.Actions;
*/
public class testPrefsObserver extends BaseTest {
private static final String PREF_TEST_PREF = "robocop.tests.dummy";
private static final int PREF_OBSERVE_REQUEST_ID = 0x7357;
private static final long PREF_TIMEOUT = 10000;
private Actions.RepeatedEventExpecter mExpecter;
private Actions.PrefWaiter prefWaiter;
private boolean prefValue;
public void setPref(boolean value) throws JSONException {
public void setPref(boolean value) {
mAsserter.dumpLog("Setting pref");
JSONObject jsonPref = new JSONObject();
jsonPref.put("name", PREF_TEST_PREF);
jsonPref.put("type", "bool");
jsonPref.put("value", value);
mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
mActions.setPref(PREF_TEST_PREF, value, /* flush */ false);
}
public void waitAndCheckPref(boolean value) throws JSONException {
public void waitAndCheckPref(boolean value) {
mAsserter.dumpLog("Waiting to check pref");
JSONObject data = null;
int requestId = -1;
mAsserter.isnot(prefWaiter, null, "Check pref waiter is not null");
prefWaiter.waitForFinish();
while (requestId != PREF_OBSERVE_REQUEST_ID) {
data = new JSONObject(mExpecter.blockForEventData());
if (!mExpecter.eventReceived()) {
mAsserter.ok(false, "Checking pref is correct value", "Didn't receive pref");
return;
}
requestId = data.getInt("requestId");
}
JSONObject pref = data.getJSONArray("preferences").getJSONObject(0);
mAsserter.is(pref.getString("name"), PREF_TEST_PREF, "Pref name is correct");
mAsserter.is(pref.getString("type"), "bool", "Pref type is correct");
mAsserter.is(pref.getBoolean("value"), value, "Pref value is correct");
mAsserter.is(prefValue, value, "Check correct pref value");
}
public void verifyDisconnect() throws JSONException {
public void verifyDisconnect() {
mAsserter.dumpLog("Checking pref observer is removed");
JSONObject pref = null;
int requestId = -1;
while (requestId != PREF_OBSERVE_REQUEST_ID) {
String data = mExpecter.blockForEventDataWithTimeout(PREF_TIMEOUT);
if (data == null) {
mAsserter.ok(true, "Verifying pref is unobserved", "Didn't get unobserved pref");
return;
}
pref = new JSONObject(data);
requestId = pref.getInt("requestId");
}
mAsserter.ok(false, "Received unobserved pref change", "");
final boolean newValue = !prefValue;
setPreferenceAndWaitForChange(PREF_TEST_PREF, newValue);
mAsserter.isnot(prefValue, newValue, "Check pref value did not change");
}
public void observePref() throws JSONException {
public void observePref() {
mAsserter.dumpLog("Setting up pref observer");
// Setup the pref observer
mExpecter = mActions.expectGeckoEvent("Preferences:Data");
mActions.sendPreferencesObserveEvent(PREF_OBSERVE_REQUEST_ID, new String[] { PREF_TEST_PREF });
mAsserter.is(prefWaiter, null, "Check pref waiter is null");
prefWaiter = mActions.addPrefsObserver(
new String[] { PREF_TEST_PREF }, new Actions.PrefHandlerBase() {
@Override // Actions.PrefHandlerBase
public void prefValue(String pref, boolean value) {
mAsserter.is(pref, PREF_TEST_PREF, "Check correct pref name");
prefValue = value;
}
});
}
public void removePrefObserver() {
mAsserter.dumpLog("Removing pref observer");
mActions.sendPreferencesRemoveObserversEvent(PREF_OBSERVE_REQUEST_ID);
mActions.removePrefsObserver(prefWaiter);
}
public void testPrefsObserver() {
blockForGeckoReady();
try {
setPref(false);
observePref();
waitAndCheckPref(false);
setPref(false);
observePref();
waitAndCheckPref(false);
setPref(true);
waitAndCheckPref(true);
setPref(true);
waitAndCheckPref(true);
removePrefObserver();
setPref(false);
verifyDisconnect();
} catch (Exception ex) {
mAsserter.ok(false, "exception in testPrefsObserver", ex.toString());
} finally {
// Make sure we remove the observer - if it's already removed, this
// will do nothing.
removePrefObserver();
}
mExpecter.unregisterListener();
removePrefObserver();
verifyDisconnect();
// Removing again should be a no-op.
removePrefObserver();
}
}