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