Bug 482260, Add more comprehensive tests, patch=dougt, r=mfinkle,jmaher,ctalbert

This commit is contained in:
Clint Talbert 2009-08-14 16:12:09 -07:00
Родитель 20f1e3c4e7
Коммит 7e3d566b33
9 изменённых файлов: 127 добавлений и 102 удалений

Просмотреть файл

@ -260,6 +260,8 @@ 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

70
dom/src/geolocation/NetworkGeolocationProvider.js Normal file → Executable file
Просмотреть файл

@ -3,7 +3,8 @@ 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()
{
@ -11,13 +12,12 @@ function nowInSeconds()
}
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) {
@ -51,10 +51,12 @@ WifiGeoAddressObject.prototype = {
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 = {
@ -77,27 +79,29 @@ WifiGeoCoordsObject.prototype = {
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;
@ -132,6 +136,11 @@ function WifiGeoPositionProvider() {
try {
gLoggingEnabled = this.prefService.getBoolPref("geo.wifi.logging.enabled");
} catch (e) {}
try {
gTestingEnabled = this.prefService.getBoolPref("geo.wifi.testing");
} catch (e) {}
};
WifiGeoPositionProvider.prototype = {
@ -165,9 +174,15 @@ WifiGeoPositionProvider.prototype = {
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);
@ -292,10 +307,9 @@ WifiGeoPositionProvider.prototype = {
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);

Просмотреть файл

@ -275,7 +275,7 @@ nsGeolocationRequest::Allow()
PRInt32 tempAge;
nsresult rv = mOptions->GetMaximumAge(&tempAge);
if (NS_SUCCEEDED(rv)) {
if (tempAge > 0)
if (tempAge >= 0)
maximumAge = tempAge;
}
}

Просмотреть файл

@ -45,6 +45,11 @@ 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 \
@ -56,7 +61,7 @@ _TEST_FILES = \
geolocation_common.js \
geolocation.html \
test_optional_api_params.html \
network_geolocation.sjs \
network_geolocation.sjs \
$(NULL)

Просмотреть файл

@ -1,18 +1,31 @@
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) {
@ -31,8 +44,15 @@ function check_geolocation(location) {
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");
}

Просмотреть файл

@ -17,85 +17,65 @@ function parseQueryString(str)
return params;
}
function getPosition()
{
// this isnt' the w3c data structure, it is the network location provider structure.
function controlResponse(params)
{
if (getState("sendresponse") == "") {
setState("sendresponse", "true");
}
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 == "stop-responding") {
setState("sendresponse", "false");
}
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);
var sendresponse = getState("sendresponse");
var position = getState("coords");
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);
if (params.action == "stop-responding") {
return;
}
var position = getPosition();
if (params.action == "respond-garbage") {
// better way?
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
position = "";
var len = Math.floor(Math.random() * 5000);
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);
}

Просмотреть файл

@ -35,8 +35,12 @@ function accept() {
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);

Просмотреть файл

@ -36,7 +36,7 @@ function successCallback(position){
SimpleTest.waitForExplicitFinish();
watchID = navigator.geolocation.getCurrentPosition(successCallback, failureCallback, null);
watchID = navigator.geolocation.watchPosition(successCallback, failureCallback, null);
// click deny
setTimeout(clickNotificationButton, 50, kDenyButton);

Просмотреть файл

@ -42,8 +42,8 @@ function errorCallback(err) {
var options = {
maximumAge: 1,
timeout: 100
maximumAge: 0,
timeout: 10
};
navigator.geolocation.watchPosition(successCallback,