Bug 482260, Add more comprehensive tests, patch=dougt, r=mfinkle,jmaher,ctalbert
authorClint Talbert <ctalbert@mozilla.com>
Fri, 14 Aug 2009 16:12:09 -0700
changeset 31577 fb8e2e65e9170750cf34f699203316053149fb24
parent 31576 6d99ad3d9e9a94f300384e638b8963164499d845
child 31578 6269562b98ceb15c1169d532e1c5674c6078fb98
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle, jmaher, ctalbert
bugs482260
milestone1.9.3a1pre
Bug 482260, Add more comprehensive tests, patch=dougt, r=mfinkle,jmaher,ctalbert
build/automation.py.in
dom/src/geolocation/NetworkGeolocationProvider.js
dom/src/geolocation/nsGeolocation.cpp
dom/tests/mochitest/geolocation/Makefile.in
dom/tests/mochitest/geolocation/geolocation_common.js
dom/tests/mochitest/geolocation/network_geolocation.sjs
dom/tests/mochitest/geolocation/test_allowCurrent.html
dom/tests/mochitest/geolocation/test_cancelWatch.html
dom/tests/mochitest/geolocation/test_timeoutWatch.html
--- a/build/automation.py.in
+++ b/build/automation.py.in
@@ -255,16 +255,18 @@ user_pref("network.manage-offline-status
 user_pref("test.mousescroll", true);
 user_pref("security.default_personal_cert", "Select Automatically"); // Need to client auth test be w/o any dialogs
 user_pref("network.http.prompt-temp-redirect", false);
 user_pref("svg.smil.enabled", true); // Needed for SMIL mochitests until bug 482402 lands
 user_pref("media.cache_size", 100);
 user_pref("security.warn_viewing_mixed", false);
 
 user_pref("geo.wifi.uri", "http://localhost:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs");
+user_pref("geo.wifi.testing", true);
+
 user_pref("camino.warn_when_closing", false); // Camino-only, harmless to others
 
 // Make url-classifier updates so rare that they won't affect tests
 user_pref("urlclassifier.updateinterval", 172800);
 // Point the url-classifier to the local testing server for fast failures
 user_pref("browser.safebrowsing.provider.0.gethashURL", "http://localhost:8888/safebrowsing-dummy/gethash");
 user_pref("browser.safebrowsing.provider.0.keyURL", "http://localhost:8888/safebrowsing-dummy/newkey");
 user_pref("browser.safebrowsing.provider.0.lookupURL", "http://localhost:8888/safebrowsing-dummy/lookup");
old mode 100644
new mode 100755
--- a/dom/src/geolocation/NetworkGeolocationProvider.js
+++ b/dom/src/geolocation/NetworkGeolocationProvider.js
@@ -1,28 +1,28 @@
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 const Ci = Components.interfaces;
 const Cc = Components.classes;
 
-var gLoggingEnabled = false;
+var gLoggingEnabled = true;
+var gTestingEnabled = false;
 
 function nowInSeconds()
 {
     return Date.now() / 1000;
 }
 
 function LOG(aMsg) {
-
-    if (gLoggingEnabled)
-    {
-        aMsg = ("*** WIFI GEO: " + aMsg);
-        Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
-        dump(aMsg);
-    }
+  if (gLoggingEnabled)
+  {
+    aMsg = ("*** WIFI GEO: " + aMsg);
+    Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
+    dump(aMsg);
+  }
 }
 
 function WifiGeoAddressObject(streetNumber, street, premises, city, county, region, country, countryCode, postalCode) {
 
   this.streetNumber = streetNumber;
   this.street       = street;
   this.premises     = premises;
   this.city         = city;
@@ -46,20 +46,22 @@ WifiGeoAddressObject.prototype = {
     getHelperForLanguage: function(language) null,
     contractID: "",
     classDescription: "wifi geo position address object",
     classID: null,
     implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
     flags: Ci.nsIClassInfo.DOM_OBJECT,
 };
 
-function WifiGeoCoordsObject(lat, lon, acc) {
+function WifiGeoCoordsObject(lat, lon, acc, alt, altacc) {
     this.latitude = lat;
     this.longitude = lon;
     this.accuracy = acc;
+    this.altitude = alt;
+    this.altitudeAccuracy = altacc;
 };
 
 WifiGeoCoordsObject.prototype = {
 
     QueryInterface:   XPCOMUtils.generateQI([Ci.nsIDOMGeoPositionCoords, Ci.nsIClassInfo]),
 
     getInterfaces: function(countRef) {
         var interfaces = [Ci.nsIDOMGeoPositionCoords, Ci.nsIClassInfo, Ci.nsISupports];
@@ -72,37 +74,39 @@ WifiGeoCoordsObject.prototype = {
     classDescription: "wifi geo position coords object",
     classID: null,
     implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
     flags: Ci.nsIClassInfo.DOM_OBJECT,
 
     latitude: 0,
     longitude: 0,
     accuracy: 0,
-
     altitude: 0,
     altitudeAccuracy: 0,
-    heading: 0,
-    speed: 0,
+
 };
 
-function WifiGeoPositionObject(lat, lon, acc, address) {
+function WifiGeoPositionObject(location, address) {
 
-    this.coords = new WifiGeoCoordsObject(lat, lon, acc);
+    this.coords = new WifiGeoCoordsObject(location.latitude,
+                                          location.longitude,
+                                          location.accuracy || 12450, // .5 * circumference of earth.
+                                          location.altitude || 0,
+                                          location.altitude_accuracy || 0);
 
     if (address) {
-        this.address = new WifiGeoAddressObject(address.street_number,
-                                                address.street,
-                                                address.premises,
-                                                address.city,
-                                                address.county,
-                                                address.region,
-                                                address.country,
-                                                address.country_code,
-                                                address.postal_code);
+        this.address = new WifiGeoAddressObject(address.street_number || null,
+                                                address.street || null,
+                                                address.premises || null,
+                                                address.city || null,
+                                                address.county || null,
+                                                address.region || null,
+                                                address.country || null,
+                                                address.country_code || null,
+                                                address.postal_code || null);
     }
     else
       this.address = null;
 
     this.timestamp = Date.now();
 };
 
 WifiGeoPositionObject.prototype = {
@@ -127,16 +131,21 @@ WifiGeoPositionObject.prototype = {
     timestamp: 0,
 };
 
 function WifiGeoPositionProvider() {
     this.prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch).QueryInterface(Ci.nsIPrefService);
     try {
         gLoggingEnabled = this.prefService.getBoolPref("geo.wifi.logging.enabled");
     } catch (e) {}
+
+    try {
+        gTestingEnabled = this.prefService.getBoolPref("geo.wifi.testing");
+    } catch (e) {}
+
 };
 
 WifiGeoPositionProvider.prototype = {
     classDescription: "A component that returns a geolocation based on WIFI",
     classID:          Components.ID("{77DA64D3-7458-4920-9491-86CC9914F904}"),
     contractID:       "@mozilla.org/geolocation/provider;1",
     QueryInterface:   XPCOMUtils.generateQI([Ci.nsIGeolocationProvider, Ci.nsIWifiListener, Ci.nsITimerCallback]),
 
@@ -160,19 +169,25 @@ WifiGeoPositionProvider.prototype = {
 
     startup:         function() {
         LOG("startup called");
 
         this.provider_url = this.prefService.getCharPref("geo.wifi.uri");
         LOG("provider url = " + this.provider_url);
 
         // if we don't see anything in 5 seconds, kick of one IP geo lookup.
+        // if we are testing, just hammer this callback so that we are more or less
+        // always sending data.  It doesn't matter if we have an access point or not.
         this.hasSeenWiFi = false;
         this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-        this.timer.initWithCallback(this, 5000, this.timer.TYPE_ONE_SHOT);
+        if (gTestingEnabled == false)
+            this.timer.initWithCallback(this, 5000, this.timer.TYPE_ONE_SHOT);
+        else
+            this.timer.initWithCallback(this, 200, this.timer.TYPE_REPEATING_SLACK);
+
 
         let os = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
         os.addObserver(this, "private-browsing", false);
     },
 
     isReady:         function() {
         LOG("isReady called");
         return true
@@ -287,20 +302,19 @@ WifiGeoPositionProvider.prototype = {
 
             var address = null;
             try {
                 address = response.location.address;
             } catch (e) {
                 LOG("No address in response");
             }
 
-            var newLocation = new WifiGeoPositionObject(response.location.latitude,
-                                                        response.location.longitude,
-                                                        response.location.accuracy,
-                                                        address);
+            LOG("sending update to geolocation.");
+
+            var newLocation = new WifiGeoPositionObject(response.location, address);
 
             var update = Cc["@mozilla.org/geolocation/service;1"].getService(Ci.nsIGeolocationUpdate);
             update.update(newLocation);
         };
 
         var accessToken = this.getAccessTokenForURL(this.provider_url);
 
         var request = {
--- a/dom/src/geolocation/nsGeolocation.cpp
+++ b/dom/src/geolocation/nsGeolocation.cpp
@@ -270,17 +270,17 @@ nsGeolocationRequest::Allow()
   // -or-
   // b) the cached position time is some reasonable value to return to the user (<30s)
   
   PRUint32 maximumAge = 30 * PR_MSEC_PER_SEC;
   if (mOptions) {
     PRInt32 tempAge;
     nsresult rv = mOptions->GetMaximumAge(&tempAge);
     if (NS_SUCCEEDED(rv)) {
-      if (tempAge > 0)
+      if (tempAge >= 0)
         maximumAge = tempAge;
     }
   }
 
   if (lastPosition && maximumAge > 0 && ( (PR_Now() / PR_USEC_PER_MSEC ) - maximumAge <= cachedPositionTime) ) {
     // okay, we can return a cached position
     mAllowed = PR_TRUE;
     
--- a/dom/tests/mochitest/geolocation/Makefile.in
+++ b/dom/tests/mochitest/geolocation/Makefile.in
@@ -40,26 +40,31 @@ srcdir		= @srcdir@
 VPATH		= @srcdir@
 relativesrcdir	= dom/tests/mochitest/geolocation
 
 include $(DEPTH)/config/autoconf.mk
 
 include $(topsrcdir)/config/rules.mk
 
 _TEST_FILES	= \
+        test_manyCurrentSerial.html \
+        test_manyCurrentConcurrent.html \
+		test_garbageWatch.html \
+		test_manyWatchConcurrent.html \
+		test_manyWatchSerial.html \
 		test_manyWindows.html \
 		test_allowCurrent.html \
 		test_allowWatch.html \
 		test_cancelCurrent.html \
 		test_cancelWatch.html \
 		test_clearWatch.html \
 		test_clearWatch_invalid.html \
 		test_timeoutWatch.html \
 		geolocation_common.js  \
 		geolocation.html \
 		test_optional_api_params.html \
-                network_geolocation.sjs \
+        network_geolocation.sjs \
 		$(NULL)
 
 
 libs:: 	$(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
 
--- a/dom/tests/mochitest/geolocation/geolocation_common.js
+++ b/dom/tests/mochitest/geolocation/geolocation_common.js
@@ -1,23 +1,36 @@
+
+
+function start_sending_garbage()
+{
+  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
+  prefs.setCharPref("geo.wifi.uri", "http://localhost:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs?action=respond-garbage");
+}
+
+function stop_sending_garbage()
+{
+  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
+  prefs.setCharPref("geo.wifi.uri", "http://localhost:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs");
+}
 
 function stop_geolocationProvider()
 {
-  var baseURL = "http://localhost:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs";
-  var xhr = new XMLHttpRequest();
-  xhr.open("GET", baseURL + "?action=stop-responding&latitude=3.14", false);
-  xhr.send(null);
+  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
+  prefs.setCharPref("geo.wifi.uri", "http://localhost:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs?action=stop-responding");
 }
 
 function resume_geolocationProvider()
 {
-  var baseURL = "http://localhost:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs";
-  var xhr = new XMLHttpRequest();
-  xhr.open("GET", baseURL + "?action=start-responding", false);
-  xhr.send(null);
+  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
+  prefs.setCharPref("geo.wifi.uri", "http://localhost:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs");
 }
 
 function check_geolocation(location) {
 
   ok(location, "Check to see if this location is non-null");
 
   ok("timestamp" in location, "Check to see if there is a timestamp");
 
@@ -26,18 +39,25 @@ function check_geolocation(location) {
 
   var coords = location.coords;
 
   ok("latitude" in coords, "Check to see if there is a latitude");
   ok("longitude" in coords, "Check to see if there is a longitude");
   ok("altitude" in coords, "Check to see if there is a altitude");
   ok("accuracy" in coords, "Check to see if there is a accuracy");
   ok("altitudeAccuracy" in coords, "Check to see if there is a alt accuracy");
-  ok("heading" in coords, "Check to see if there is a heading");
-  ok("speed" in coords, "Check to see if there is a speed");
+
+  // optional ok("heading" in coords, "Check to see if there is a heading");
+  // optional ok("speed" in coords, "Check to see if there is a speed");
+
+  ok (location.coords.latitude  == 37.41857, "lat matches known value");
+  ok (location.coords.longitude == -122.08769, "lon matches known value");
+  ok(location.coords.altitude == 42, "alt matches known value");
+  ok(location.coords.altitudeAccuracy == 42, "alt acc matches known value");
+
 }
 
 
 function getNotificationBox()
 {
   const Ci = Components.interfaces;
   
   function getChromeWindow(aWindow) {
--- a/dom/tests/mochitest/geolocation/network_geolocation.sjs
+++ b/dom/tests/mochitest/geolocation/network_geolocation.sjs
@@ -12,90 +12,70 @@ function parseQueryString(str)
     if (!match)
       throw "Bad parameter in queryString!  '" + paramArray[i] + "'";
     params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
   }
 
   return params;
 }
 
-
-function controlResponse(params)
-{
-  if (getState("sendresponse") == "") {
-    setState("sendresponse", "true");
-  }
+function getPosition()
+{  
+    // this isnt' the w3c data structure, it is the network location provider structure.
 
-  if (params.action == "stop-responding") {
-    setState("sendresponse", "false");
-  }
+  var address = {
+      street_number: "street_number",
+      street: "street",
+      premises: "premises",
+      city: "city",
+      county: "county",
+      region: "region",
+      country: "country",
+      country_code: "country_code",
+      postal_code: "postal_code",
+  };
 
-  if (params.action == "start-responding") {
-    setState("sendresponse", "true");
-  }
-}
 
-function setPosition(params)
-{
   var coords = {
     latitude: 37.41857,
     longitude: -122.08769,
 
     altitude: 42,
     accuracy: 42,
-    altitudeAccuracy: 42,
-    heading: 42,
-    speed: 42,
+    altitude_accuracy: 42,
   };
   
   var geoposition = {
     location: coords,
   };
 
-  if (getState("coords") == "") {
-    setState("coords", JSON.stringify(geoposition));
-  }
-
-  var position = JSON.parse(getState("coords"));
-
-  if (params.latitude != null) {
-    position.location.latitude = params.latitude;
-  }
-  if (params.longitude != null) {
-    position.location.longitude = params.longitude;
-  }
-  if (params.altitude != null) {
-    position.location.altitude = params.altitude;
-  }
-  if (params.accuracy != null) {
-    position.location.accuracy = params.accuracy;
-  }
-  if (params.altitudeAccuracy != null) {
-    position.location.altitudeAccuracy = params.altitudeAccuracy;
-  }
-  if (params.heading != null) {
-    position.location.heading = params.heading;
-  }
-  if (params.speed != null) {
-    position.location.speed = params.speed;
-  }
-
-  setState("coords", JSON.stringify(position));
+  return JSON.stringify(geoposition);
 }
 
 function handleRequest(request, response)
 {
   var params = parseQueryString(request.queryString);
 
-  controlResponse(params);
-  setPosition(params);
+  if (params.action == "stop-responding") {
+      return;
+  }
+
+  var position = getPosition();
 
-  var sendresponse = getState("sendresponse");
-  var position = getState("coords");
+  if (params.action == "respond-garbage") {
+     // better way?
+    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
+    position = "";
+    var len = Math.floor(Math.random() * 5000);
 
-  if (sendresponse == "true") {
-    response.setStatusLine("1.0", 200, "OK");
-    response.setHeader("Cache-Control", "no-cache", false);
-    response.setHeader("Content-Type", "aplication/x-javascript", false);
-    response.write(position);
+    for (var i=0; i< len; i++) {
+        var c = Math.floor(Math.random() * chars.length);
+        position += chars.substring(c, c+1);
+    }
   }
+
+  var response;
+  response.setStatusLine("1.0", 200, "OK");
+  response.setHeader("Cache-Control", "no-cache", false);
+  response.setHeader("Content-Type", "aplication/x-javascript", false);
+  response.write(position);
 }
 
--- a/dom/tests/mochitest/geolocation/test_allowCurrent.html
+++ b/dom/tests/mochitest/geolocation/test_allowCurrent.html
@@ -30,18 +30,22 @@ function successCallback(position) {
 
 function accept() {
   hasAccepted = true;
   clickNotificationButton(kAcceptButton);
 }
 
 SimpleTest.waitForExplicitFinish();
 
+var options = {
+    maximumAge: 0,
+};
+
 // one-shot position requests
-navigator.geolocation.getCurrentPosition(successCallback, null, null);
+navigator.geolocation.getCurrentPosition(successCallback, null, options);
 
 setTimeout(accept, 50);
 
 </script>
 </pre>
 </body>
 </html>
 
--- a/dom/tests/mochitest/geolocation/test_cancelWatch.html
+++ b/dom/tests/mochitest/geolocation/test_cancelWatch.html
@@ -31,17 +31,17 @@ function successCallback(position){
   ok(0, "Success was called when it shouldn't have been.  major problem");
   SimpleTest.finish();
 }
 
 /** Test for Bug  **/
 
 SimpleTest.waitForExplicitFinish();
 
-watchID = navigator.geolocation.getCurrentPosition(successCallback, failureCallback, null);
+watchID = navigator.geolocation.watchPosition(successCallback, failureCallback, null);
 
 // click deny
 setTimeout(clickNotificationButton, 50, kDenyButton);
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/tests/mochitest/geolocation/test_timeoutWatch.html
+++ b/dom/tests/mochitest/geolocation/test_timeoutWatch.html
@@ -37,18 +37,18 @@ function errorCallback(err) {
   else
     ok(err.code == err.TIMEOUT, "ensure error is a timeout.");
   resume_geolocationProvider();
   SimpleTest.finish();
 }
 
 
 var options = {
-    maximumAge: 1,
-    timeout: 100
+    maximumAge: 0,
+    timeout: 10
 };
 
 navigator.geolocation.watchPosition(successCallback,
                                     errorCallback,
                                     options);
 
 setTimeout(clickNotificationButton, 10, kAcceptButton);