Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | package org.mozilla.gecko.tests; |
michael@0 | 2 | |
michael@0 | 3 | import org.json.JSONException; |
michael@0 | 4 | import org.json.JSONObject; |
michael@0 | 5 | import org.mozilla.gecko.Actions; |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * Basic test to check bounce-back from overscroll. |
michael@0 | 9 | * - Load the page and verify it draws |
michael@0 | 10 | * - Drag page downwards by 100 pixels into overscroll, verify it snaps back. |
michael@0 | 11 | * - Drag page rightwards by 100 pixels into overscroll, verify it snaps back. |
michael@0 | 12 | */ |
michael@0 | 13 | public class testPrefsObserver extends BaseTest { |
michael@0 | 14 | private static final String PREF_TEST_PREF = "robocop.tests.dummy"; |
michael@0 | 15 | private static final int PREF_OBSERVE_REQUEST_ID = 0x7357; |
michael@0 | 16 | private static final long PREF_TIMEOUT = 10000; |
michael@0 | 17 | |
michael@0 | 18 | private Actions.RepeatedEventExpecter mExpecter; |
michael@0 | 19 | |
michael@0 | 20 | public void setPref(boolean value) throws JSONException { |
michael@0 | 21 | mAsserter.dumpLog("Setting pref"); |
michael@0 | 22 | |
michael@0 | 23 | JSONObject jsonPref = new JSONObject(); |
michael@0 | 24 | jsonPref.put("name", PREF_TEST_PREF); |
michael@0 | 25 | jsonPref.put("type", "bool"); |
michael@0 | 26 | jsonPref.put("value", value); |
michael@0 | 27 | mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString()); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | public void waitAndCheckPref(boolean value) throws JSONException { |
michael@0 | 31 | mAsserter.dumpLog("Waiting to check pref"); |
michael@0 | 32 | |
michael@0 | 33 | JSONObject data = null; |
michael@0 | 34 | int requestId = -1; |
michael@0 | 35 | |
michael@0 | 36 | while (requestId != PREF_OBSERVE_REQUEST_ID) { |
michael@0 | 37 | data = new JSONObject(mExpecter.blockForEventData()); |
michael@0 | 38 | if (!mExpecter.eventReceived()) { |
michael@0 | 39 | mAsserter.ok(false, "Checking pref is correct value", "Didn't receive pref"); |
michael@0 | 40 | return; |
michael@0 | 41 | } |
michael@0 | 42 | requestId = data.getInt("requestId"); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | JSONObject pref = data.getJSONArray("preferences").getJSONObject(0); |
michael@0 | 46 | mAsserter.is(pref.getString("name"), PREF_TEST_PREF, "Pref name is correct"); |
michael@0 | 47 | mAsserter.is(pref.getString("type"), "bool", "Pref type is correct"); |
michael@0 | 48 | mAsserter.is(pref.getBoolean("value"), value, "Pref value is correct"); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | public void verifyDisconnect() throws JSONException { |
michael@0 | 52 | mAsserter.dumpLog("Checking pref observer is removed"); |
michael@0 | 53 | |
michael@0 | 54 | JSONObject pref = null; |
michael@0 | 55 | int requestId = -1; |
michael@0 | 56 | |
michael@0 | 57 | while (requestId != PREF_OBSERVE_REQUEST_ID) { |
michael@0 | 58 | String data = mExpecter.blockForEventDataWithTimeout(PREF_TIMEOUT); |
michael@0 | 59 | if (data == null) { |
michael@0 | 60 | mAsserter.ok(true, "Verifying pref is unobserved", "Didn't get unobserved pref"); |
michael@0 | 61 | return; |
michael@0 | 62 | } |
michael@0 | 63 | pref = new JSONObject(data); |
michael@0 | 64 | requestId = pref.getInt("requestId"); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | mAsserter.ok(false, "Received unobserved pref change", ""); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | public void observePref() throws JSONException { |
michael@0 | 71 | mAsserter.dumpLog("Setting up pref observer"); |
michael@0 | 72 | |
michael@0 | 73 | // Setup the pref observer |
michael@0 | 74 | mExpecter = mActions.expectGeckoEvent("Preferences:Data"); |
michael@0 | 75 | mActions.sendPreferencesObserveEvent(PREF_OBSERVE_REQUEST_ID, new String[] { PREF_TEST_PREF }); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | public void removePrefObserver() { |
michael@0 | 79 | mAsserter.dumpLog("Removing pref observer"); |
michael@0 | 80 | |
michael@0 | 81 | mActions.sendPreferencesRemoveObserversEvent(PREF_OBSERVE_REQUEST_ID); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | public void testPrefsObserver() { |
michael@0 | 85 | blockForGeckoReady(); |
michael@0 | 86 | |
michael@0 | 87 | try { |
michael@0 | 88 | setPref(false); |
michael@0 | 89 | observePref(); |
michael@0 | 90 | waitAndCheckPref(false); |
michael@0 | 91 | |
michael@0 | 92 | setPref(true); |
michael@0 | 93 | waitAndCheckPref(true); |
michael@0 | 94 | |
michael@0 | 95 | removePrefObserver(); |
michael@0 | 96 | setPref(false); |
michael@0 | 97 | verifyDisconnect(); |
michael@0 | 98 | } catch (Exception ex) { |
michael@0 | 99 | mAsserter.ok(false, "exception in testPrefsObserver", ex.toString()); |
michael@0 | 100 | } finally { |
michael@0 | 101 | // Make sure we remove the observer - if it's already removed, this |
michael@0 | 102 | // will do nothing. |
michael@0 | 103 | removePrefObserver(); |
michael@0 | 104 | } |
michael@0 | 105 | mExpecter.unregisterListener(); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 |