michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.Actions; michael@0: michael@0: /** michael@0: * Basic test to check bounce-back from overscroll. michael@0: * - Load the page and verify it draws michael@0: * - Drag page downwards by 100 pixels into overscroll, verify it snaps back. michael@0: * - Drag page rightwards by 100 pixels into overscroll, verify it snaps back. michael@0: */ michael@0: public class testPrefsObserver extends BaseTest { michael@0: private static final String PREF_TEST_PREF = "robocop.tests.dummy"; michael@0: private static final int PREF_OBSERVE_REQUEST_ID = 0x7357; michael@0: private static final long PREF_TIMEOUT = 10000; michael@0: michael@0: private Actions.RepeatedEventExpecter mExpecter; michael@0: michael@0: public void setPref(boolean value) throws JSONException { michael@0: mAsserter.dumpLog("Setting pref"); michael@0: michael@0: JSONObject jsonPref = new JSONObject(); michael@0: jsonPref.put("name", PREF_TEST_PREF); michael@0: jsonPref.put("type", "bool"); michael@0: jsonPref.put("value", value); michael@0: mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString()); michael@0: } michael@0: michael@0: public void waitAndCheckPref(boolean value) throws JSONException { michael@0: mAsserter.dumpLog("Waiting to check pref"); michael@0: michael@0: JSONObject data = null; michael@0: int requestId = -1; michael@0: michael@0: while (requestId != PREF_OBSERVE_REQUEST_ID) { michael@0: data = new JSONObject(mExpecter.blockForEventData()); michael@0: if (!mExpecter.eventReceived()) { michael@0: mAsserter.ok(false, "Checking pref is correct value", "Didn't receive pref"); michael@0: return; michael@0: } michael@0: requestId = data.getInt("requestId"); michael@0: } michael@0: michael@0: JSONObject pref = data.getJSONArray("preferences").getJSONObject(0); michael@0: mAsserter.is(pref.getString("name"), PREF_TEST_PREF, "Pref name is correct"); michael@0: mAsserter.is(pref.getString("type"), "bool", "Pref type is correct"); michael@0: mAsserter.is(pref.getBoolean("value"), value, "Pref value is correct"); michael@0: } michael@0: michael@0: public void verifyDisconnect() throws JSONException { michael@0: mAsserter.dumpLog("Checking pref observer is removed"); michael@0: michael@0: JSONObject pref = null; michael@0: int requestId = -1; michael@0: michael@0: while (requestId != PREF_OBSERVE_REQUEST_ID) { michael@0: String data = mExpecter.blockForEventDataWithTimeout(PREF_TIMEOUT); michael@0: if (data == null) { michael@0: mAsserter.ok(true, "Verifying pref is unobserved", "Didn't get unobserved pref"); michael@0: return; michael@0: } michael@0: pref = new JSONObject(data); michael@0: requestId = pref.getInt("requestId"); michael@0: } michael@0: michael@0: mAsserter.ok(false, "Received unobserved pref change", ""); michael@0: } michael@0: michael@0: public void observePref() throws JSONException { michael@0: mAsserter.dumpLog("Setting up pref observer"); michael@0: michael@0: // Setup the pref observer michael@0: mExpecter = mActions.expectGeckoEvent("Preferences:Data"); michael@0: mActions.sendPreferencesObserveEvent(PREF_OBSERVE_REQUEST_ID, new String[] { PREF_TEST_PREF }); michael@0: } michael@0: michael@0: public void removePrefObserver() { michael@0: mAsserter.dumpLog("Removing pref observer"); michael@0: michael@0: mActions.sendPreferencesRemoveObserversEvent(PREF_OBSERVE_REQUEST_ID); michael@0: } michael@0: michael@0: public void testPrefsObserver() { michael@0: blockForGeckoReady(); michael@0: michael@0: try { michael@0: setPref(false); michael@0: observePref(); michael@0: waitAndCheckPref(false); michael@0: michael@0: setPref(true); michael@0: waitAndCheckPref(true); michael@0: michael@0: removePrefObserver(); michael@0: setPref(false); michael@0: verifyDisconnect(); michael@0: } catch (Exception ex) { michael@0: mAsserter.ok(false, "exception in testPrefsObserver", ex.toString()); michael@0: } finally { michael@0: // Make sure we remove the observer - if it's already removed, this michael@0: // will do nothing. michael@0: removePrefObserver(); michael@0: } michael@0: mExpecter.unregisterListener(); michael@0: } michael@0: } michael@0: