Bug 1328868 - Part 7 - Move MakeObserver() into head.js r?esawin draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sun, 05 Feb 2017 17:11:02 +0100
changeset 551357 4e913d41ac5588e293980cb175d49de3f0aa95f7
parent 551356 67cfe2d426a1e2bd34534a78286d37666313d83c
child 551358 71bc3df34612602a6ca28c0b734070c1af5ce18f
push id51034
push usermozilla@buttercookie.de
push dateSat, 25 Mar 2017 19:48:16 +0000
reviewersesawin
bugs1328868
milestone55.0a1
Bug 1328868 - Part 7 - Move MakeObserver() into head.js r?esawin We can use this function for our upcoming test as well, so we should move it into the common header. MozReview-Commit-ID: H5ANDAlnpmm
mobile/android/tests/browser/chrome/head.js
mobile/android/tests/browser/chrome/test_shared_preferences.html
--- a/mobile/android/tests/browser/chrome/head.js
+++ b/mobile/android/tests/browser/chrome/head.js
@@ -65,8 +65,28 @@ function promiseLinkVisit(url) {
       info("Visited URL " + uri.spec + " is desired URL " + url);
       Services.obs.removeObserver(observe, topic);
       resolve();
     };
     Services.obs.addObserver(observe, topic, false);
     info("Now waiting for " + topic + " notification from Gecko with URL " + url);
   });
 }
+
+function makeObserver(observerId) {
+  let deferred = Promise.defer();
+
+  let ret = {
+    id: observerId,
+    count: 0,
+    promise: deferred.promise,
+    observe: function (subject, topic, data) {
+      ret.count += 1;
+      let msg = { subject: subject,
+                  topic: topic,
+                  data: data };
+      deferred.resolve(msg);
+    },
+  };
+
+  return ret;
+};
+
--- a/mobile/android/tests/browser/chrome/test_shared_preferences.html
+++ b/mobile/android/tests/browser/chrome/test_shared_preferences.html
@@ -6,43 +6,25 @@ Migrated from Robocop: https://bugzilla.
 -->
 <head>
   <meta charset="utf-8">
   <title>Test for Bug 866271</title>
   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
   <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+  <script type="application/javascript" src="head.js"></script>
   <script type="application/javascript">
 
   Components.utils.import("resource://gre/modules/SharedPreferences.jsm");
   Components.utils.import("resource://gre/modules/Promise.jsm");
   Components.utils.import("resource://gre/modules/Task.jsm");
 
   let _observerId = 0;
 
-  function makeObserver() {
-    let deferred = Promise.defer();
-
-    let ret = {
-      id: _observerId++,
-      count: 0,
-      promise: deferred.promise,
-      observe: function (subject, topic, data) {
-        ret.count += 1;
-        let msg = { subject: subject,
-                    topic: topic,
-                    data: data };
-        deferred.resolve(msg);
-      },
-    };
-
-    return ret;
-  };
-
   add_task(function* test_get_set() {
     let branch = SharedPreferences.forAndroid("test");
 
     branch.setBoolPref("boolKey", true);
     branch.setCharPref("charKey", "string value");
     branch.setIntPref("intKey", 1000);
 
     is(branch.getBoolPref("boolKey"), true);
@@ -104,17 +86,17 @@ Migrated from Robocop: https://bugzilla.
   });
 
   add_task(function* test_add_remove_observer() {
     let branch = SharedPreferences.forAndroid("test");
 
     branch.setBoolPref("boolKey", false);
     is(branch.getBoolPref("boolKey"), false);
 
-    let obs1 = makeObserver();
+    let obs1 = makeObserver(_observerId++);
     branch.addObserver("boolKey", obs1);
 
     try {
       branch.setBoolPref("boolKey", true);
       is(branch.getBoolPref("boolKey"), true);
 
       let value1 = yield obs1.promise;
       is(obs1.count, 1);
@@ -128,17 +110,17 @@ Migrated from Robocop: https://bugzilla.
     }
 
     // Make sure the original observer is really gone, or as close as
     // we: install a second observer, wait for it to be notified, and
     // then verify the original observer was *not* notified.  This
     // depends, of course, on the order that observers are notified, but
     // is better than nothing.
 
-    let obs2 = makeObserver();
+    let obs2 = makeObserver(_observerId++);
     branch.addObserver("boolKey", obs2);
 
     try {
       branch.setBoolPref("boolKey", false);
       is(branch.getBoolPref("boolKey"), false);
 
       let value2 = yield obs2.promise;
       is(obs2.count, 1);
@@ -156,17 +138,17 @@ Migrated from Robocop: https://bugzilla.
   });
 
   add_task(function* test_observer_ignores() {
     let branch = SharedPreferences.forAndroid("test");
 
     branch.setCharPref("charKey", "first value");
     is(branch.getCharPref("charKey"), "first value");
 
-    let obs = makeObserver();
+    let obs = makeObserver(_observerId++);
     branch.addObserver("charKey", obs);
 
     try {
       // These should all be ignored.
       branch.setBoolPref("boolKey", true);
       branch.setBoolPref("boolKey", false);
       branch.setIntPref("intKey", -3000);
       branch.setIntPref("intKey", 4000);
@@ -187,17 +169,17 @@ Migrated from Robocop: https://bugzilla.
   });
 
   add_task(function* test_observer_ignores_branches() {
     let branch = SharedPreferences.forAndroid("test");
 
     branch.setCharPref("charKey", "first value");
     is(branch.getCharPref("charKey"), "first value");
 
-    let obs = makeObserver();
+    let obs = makeObserver(_observerId++);
     branch.addObserver("charKey", obs);
 
     try {
       // These should all be ignored.
       let branch2 = SharedPreferences.forAndroid("test2");
       branch2.setCharPref("charKey", "a wrong value");
       let branch3 = SharedPreferences.forAndroid("test.2");
       branch3.setCharPref("charKey", "a different wrong value");