1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/testPrefsObserver.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +package org.mozilla.gecko.tests; 1.5 + 1.6 +import org.json.JSONException; 1.7 +import org.json.JSONObject; 1.8 +import org.mozilla.gecko.Actions; 1.9 + 1.10 +/** 1.11 + * Basic test to check bounce-back from overscroll. 1.12 + * - Load the page and verify it draws 1.13 + * - Drag page downwards by 100 pixels into overscroll, verify it snaps back. 1.14 + * - Drag page rightwards by 100 pixels into overscroll, verify it snaps back. 1.15 + */ 1.16 +public class testPrefsObserver extends BaseTest { 1.17 + private static final String PREF_TEST_PREF = "robocop.tests.dummy"; 1.18 + private static final int PREF_OBSERVE_REQUEST_ID = 0x7357; 1.19 + private static final long PREF_TIMEOUT = 10000; 1.20 + 1.21 + private Actions.RepeatedEventExpecter mExpecter; 1.22 + 1.23 + public void setPref(boolean value) throws JSONException { 1.24 + mAsserter.dumpLog("Setting pref"); 1.25 + 1.26 + JSONObject jsonPref = new JSONObject(); 1.27 + jsonPref.put("name", PREF_TEST_PREF); 1.28 + jsonPref.put("type", "bool"); 1.29 + jsonPref.put("value", value); 1.30 + mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString()); 1.31 + } 1.32 + 1.33 + public void waitAndCheckPref(boolean value) throws JSONException { 1.34 + mAsserter.dumpLog("Waiting to check pref"); 1.35 + 1.36 + JSONObject data = null; 1.37 + int requestId = -1; 1.38 + 1.39 + while (requestId != PREF_OBSERVE_REQUEST_ID) { 1.40 + data = new JSONObject(mExpecter.blockForEventData()); 1.41 + if (!mExpecter.eventReceived()) { 1.42 + mAsserter.ok(false, "Checking pref is correct value", "Didn't receive pref"); 1.43 + return; 1.44 + } 1.45 + requestId = data.getInt("requestId"); 1.46 + } 1.47 + 1.48 + JSONObject pref = data.getJSONArray("preferences").getJSONObject(0); 1.49 + mAsserter.is(pref.getString("name"), PREF_TEST_PREF, "Pref name is correct"); 1.50 + mAsserter.is(pref.getString("type"), "bool", "Pref type is correct"); 1.51 + mAsserter.is(pref.getBoolean("value"), value, "Pref value is correct"); 1.52 + } 1.53 + 1.54 + public void verifyDisconnect() throws JSONException { 1.55 + mAsserter.dumpLog("Checking pref observer is removed"); 1.56 + 1.57 + JSONObject pref = null; 1.58 + int requestId = -1; 1.59 + 1.60 + while (requestId != PREF_OBSERVE_REQUEST_ID) { 1.61 + String data = mExpecter.blockForEventDataWithTimeout(PREF_TIMEOUT); 1.62 + if (data == null) { 1.63 + mAsserter.ok(true, "Verifying pref is unobserved", "Didn't get unobserved pref"); 1.64 + return; 1.65 + } 1.66 + pref = new JSONObject(data); 1.67 + requestId = pref.getInt("requestId"); 1.68 + } 1.69 + 1.70 + mAsserter.ok(false, "Received unobserved pref change", ""); 1.71 + } 1.72 + 1.73 + public void observePref() throws JSONException { 1.74 + mAsserter.dumpLog("Setting up pref observer"); 1.75 + 1.76 + // Setup the pref observer 1.77 + mExpecter = mActions.expectGeckoEvent("Preferences:Data"); 1.78 + mActions.sendPreferencesObserveEvent(PREF_OBSERVE_REQUEST_ID, new String[] { PREF_TEST_PREF }); 1.79 + } 1.80 + 1.81 + public void removePrefObserver() { 1.82 + mAsserter.dumpLog("Removing pref observer"); 1.83 + 1.84 + mActions.sendPreferencesRemoveObserversEvent(PREF_OBSERVE_REQUEST_ID); 1.85 + } 1.86 + 1.87 + public void testPrefsObserver() { 1.88 + blockForGeckoReady(); 1.89 + 1.90 + try { 1.91 + setPref(false); 1.92 + observePref(); 1.93 + waitAndCheckPref(false); 1.94 + 1.95 + setPref(true); 1.96 + waitAndCheckPref(true); 1.97 + 1.98 + removePrefObserver(); 1.99 + setPref(false); 1.100 + verifyDisconnect(); 1.101 + } catch (Exception ex) { 1.102 + mAsserter.ok(false, "exception in testPrefsObserver", ex.toString()); 1.103 + } finally { 1.104 + // Make sure we remove the observer - if it's already removed, this 1.105 + // will do nothing. 1.106 + removePrefObserver(); 1.107 + } 1.108 + mExpecter.unregisterListener(); 1.109 + } 1.110 +} 1.111 +