Hello andreip,
I'd like you to do a code review. Please execute
g4 diff -c 8203035
or point your web browser to
http://mondrian/8203035
to review the following code:
Change 8203035 by [EMAIL PROTECTED] on 2008/09/08 15:56:30 *pending*
Updates Geolocation tests.
- Moves tests that do not require test object to geolocation_tests.js
- Moves tests that do require test object to
internal_geolocation_tests.js
- Adds tests that make use of mock network location provider.
R=andreip
[EMAIL PROTECTED]
DELTA=848 (448 added, 388 deleted, 12 changed)
OCL=8203035
Affected files ...
... //depot/googleclient/gears/opensource/gears/test/testcases/config.js#33 edit
...
//depot/googleclient/gears/opensource/gears/test/testcases/geolocation_tests.js#2
edit
...
//depot/googleclient/gears/opensource/gears/test/testcases/internal_geolocation_tests.js#1
add
...
//depot/googleclient/gears/opensource/gears/test/testcases/internal_tests.js#37
edit
848 delta lines: 448 added, 388 deleted, 12 changed
If you can't do the review, please let me know as soon as possible. During
your review, please ensure that all new code has corresponding unit tests and
that existing unit tests are updated appropriately. Visit
http://www/eng/code_review.html for more information.
This is a semiautomated message from "g4 mail". Complaints or suggestions?
Mail [EMAIL PROTECTED]
Change 8203035 by [EMAIL PROTECTED] on 2008/09/08 15:56:30 *pending*
Updates Geolocation tests.
- Moves tests that do not require test object to geolocation_tests.js
- Moves tests that do require test object to
internal_geolocation_tests.js
- Adds tests that make use of mock network location provider.
Affected files ...
... //depot/googleclient/gears/opensource/gears/test/testcases/config.js#33 edit
...
//depot/googleclient/gears/opensource/gears/test/testcases/geolocation_tests.js#2
edit
...
//depot/googleclient/gears/opensource/gears/test/testcases/internal_geolocation_tests.js#1
add
...
//depot/googleclient/gears/opensource/gears/test/testcases/internal_tests.js#37
edit
==== //depot/googleclient/gears/opensource/gears/test/testcases/config.js#33 -
c:\MyDocs\Gears1/googleclient/gears/opensource/gears/test/testcases/config.js
====
# action=edit type=text
--- googleclient/gears/opensource/gears/test/testcases/config.js
2008-09-18 21:48:55.000000000 +0100
+++ googleclient/gears/opensource/gears/test/testcases/config.js
2008-09-11 14:03:59.000000000 +0100
@@ -133,6 +133,8 @@
internalTestSuite.addFile('../testcases/internal_audio_recorder_tests.js',
{useWorker: true, useIFrame: true});
}
+internalTestSuite.addFile('../testcases/internal_geolocation_tests.js',
+ {useWorker: true, useIFrame: true});
suites.push(internalTestSuite);
var localServerSuite = new TestSuite('LocalServer');
====
//depot/googleclient/gears/opensource/gears/test/testcases/geolocation_tests.js#2
-
c:\MyDocs\Gears1/googleclient/gears/opensource/gears/test/testcases/geolocation_tests.js
====
# action=edit type=text
--- googleclient/gears/opensource/gears/test/testcases/geolocation_tests.js
2008-09-18 21:48:55.000000000 +0100
+++ googleclient/gears/opensource/gears/test/testcases/geolocation_tests.js
2008-09-22 14:27:03.000000000 +0100
@@ -23,18 +23,61 @@
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-// TODO(steveblock): Other Geolocation tests require the test object, but
-// consider moving to this file.
+function testGeolocationArguments() {
+ var geolocation = google.gears.factory.create('beta.geolocation');
+ var dummyFunction = function() {};
-function testGeolocationNoProviders() {
- var geolocation = google.gears.factory.create('beta.geolocation');
- assertError(
- function() {
+ // All good.
+ geolocation.getCurrentPosition(dummyFunction);
+ geolocation.getCurrentPosition(dummyFunction, dummyFunction);
+ geolocation.getCurrentPosition(dummyFunction, null);
+ geolocation.getCurrentPosition(dummyFunction, null, {});
+ geolocation.getCurrentPosition(dummyFunction, dummyFunction, {});
+
+ // Test correct types.
+ // Missing success callback.
+ assertError(function() { geolocation.getCurrentPosition() });
+ // Wrong type for success callback.
+ assertError(function() { geolocation.getCurrentPosition(42) });
+ // Wrong type for error callback.
+ assertError(function() { geolocation.getCurrentPosition(dummyFunction, 42)
});
+ // Wrong type for options.
+ assertError(function() {
+ geolocation.getCurrentPosition(dummyFunction, dummyFunction, 42) });
+ // Wrong type for enableHighAccuracy.
+ assertError(function() {
geolocation.getCurrentPosition(
- function() {},
- function() {},
- { gearsLocationProviderUrls: [], enableHighAccuracy: false } ); },
- 'Fix request has no location providers.',
- 'Calling getCurrentPosition() should fail if no location providers are ' +
- 'specified.');
+ dummyFunction, dummyFunction, { enableHighAccuracy: 42 }) },
+ 'options.enableHighAccuracy should be a boolean.');
+ // Wrong type for gearsRequestAddress.
+ assertError(function() {
+ geolocation.getCurrentPosition(
+ dummyFunction, dummyFunction, { gearsRequestAddress: 42 }) },
+ 'options.gearsRequestAddress should be a boolean.');
+ // Wrong type for gearsAddressLanguage.
+ assertError(function() {
+ geolocation.getCurrentPosition(
+ dummyFunction, dummyFunction, { gearsAddressLanguage: 42 }) },
+ 'options.gearsAddressLanguage should be a string.');
+ // Wrong type for gearsLocationProviderUrls.
+ assertError(function() {
+ geolocation.getCurrentPosition(
+ dummyFunction, dummyFunction, { gearsLocationProviderUrls: 42 }) },
+ 'options.gearsLocationProviderUrls should be null or an array of ' +
+ 'strings');
+ assertError(function() {
+ geolocation.getCurrentPosition(
+ dummyFunction, dummyFunction, { gearsLocationProviderUrls: [42] }) },
+ 'options.gearsLocationProviderUrls should be null or an array of ' +
+ 'strings');
+
+ // No providers.
+ assertError(function() {
+ geolocation.getCurrentPosition(
+ dummyFunction,
+ dummyFunction,
+ { gearsLocationProviderUrls: [], enableHighAccuracy: false } ); },
+ 'Fix request has no location providers.',
+ 'Calling getCurrentPosition() should fail if no location providers are '
+
+ 'specified.');
}
====
//depot/googleclient/gears/opensource/gears/test/testcases/internal_geolocation_tests.js#1
-
c:\MyDocs\Gears1/googleclient/gears/opensource/gears/test/testcases/internal_geolocation_tests.js
====
# action=add type=text
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++
googleclient/gears/opensource/gears/test/testcases/internal_geolocation_tests.js
2008-09-22 14:49:12.000000000 +0100
@@ -0,0 +1,403 @@
+// Copyright 2008, Google Inc.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright
notice,
+// this list of conditions and the following disclaimer in the
documentation
+// and/or other materials provided with the distribution.
+// 3. Neither the name of Google Inc. nor the names of its contributors may be
+// used to endorse or promote products derived from this software without
+// specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+if (isUsingCCTests) {
+ var internalTests = google.gears.factory.create('beta.test');
+}
+
+// Tests the parsing of the options passed to Geolocation.GetCurrentPosition
and
+// Geolocation.WatchPosition.
+function testParseOptions() {
+ if (isUsingCCTests) {
+ var dummyFunction = function() {};
+
+ // Test correct parsing.
+ var defaultUrlArray = ['http://www.google.com/loc/json'];
+ var urls = ['url1', 'url2'];
+ var parsedOptions;
+ // No options.
+ parsedOptions = internalTests.testParseGeolocationOptions(dummyFunction);
+ assertEqual(false, parsedOptions.repeats);
+ assertEqual(false, parsedOptions.enableHighAccuracy);
+ assertEqual(false, parsedOptions.gearsRequestAddress);
+ assertEqual("", parsedOptions.gearsAddressLanguage);
+ assertArrayEqual(defaultUrlArray, parsedOptions.gearsLocationProviderUrls);
+ // Empty options.
+ parsedOptions = internalTests.testParseGeolocationOptions(
+ dummyFunction, dummyFunction, {});
+ assertEqual(false, parsedOptions.repeats);
+ assertEqual(false, parsedOptions.enableHighAccuracy);
+ assertEqual(false, parsedOptions.gearsRequestAddress);
+ assertEqual("", parsedOptions.gearsAddressLanguage);
+ assertArrayEqual(defaultUrlArray, parsedOptions.gearsLocationProviderUrls);
+ // Empty provider URLs.
+ parsedOptions = internalTests.testParseGeolocationOptions(
+ dummyFunction, dummyFunction, { gearsLocationProviderUrls: [] });
+ assertEqual(false, parsedOptions.repeats);
+ assertEqual(false, parsedOptions.enableHighAccuracy);
+ assertEqual(false, parsedOptions.gearsRequestAddress);
+ assertEqual("", parsedOptions.gearsAddressLanguage);
+ assertArrayEqual([], parsedOptions.gearsLocationProviderUrls);
+ // Null provider URLs.
+ parsedOptions = internalTests.testParseGeolocationOptions(
+ dummyFunction, dummyFunction, { gearsLocationProviderUrls: null });
+ assertEqual(false, parsedOptions.repeats);
+ assertEqual(false, parsedOptions.enableHighAccuracy);
+ assertEqual(false, parsedOptions.gearsRequestAddress);
+ assertEqual("", parsedOptions.gearsAddressLanguage);
+ assertArrayEqual([], parsedOptions.gearsLocationProviderUrls);
+ // All properties false, provider URLs set.
+ parsedOptions = internalTests.testParseGeolocationOptions(
+ dummyFunction,
+ dummyFunction,
+ { enableHighAccuracy: false,
+ gearsRequestAddress: false,
+ gearsAddressLanguage: "",
+ gearsLocationProviderUrls: urls
+ });
+ assertEqual(false, parsedOptions.repeats);
+ assertEqual(false, parsedOptions.enableHighAccuracy);
+ assertEqual(false, parsedOptions.gearsRequestAddress);
+ assertEqual("", parsedOptions.gearsAddressLanguage);
+ assertArrayEqual(urls, parsedOptions.gearsLocationProviderUrls);
+ // All properties true, provider URLs set.
+ parsedOptions = internalTests.testParseGeolocationOptions(
+ dummyFunction,
+ dummyFunction,
+ { enableHighAccuracy: true,
+ gearsRequestAddress: true,
+ gearsAddressLanguage: "test",
+ gearsLocationProviderUrls: urls
+ });
+ assertEqual(false, parsedOptions.repeats);
+ assertEqual(true, parsedOptions.enableHighAccuracy);
+ assertEqual(true, parsedOptions.gearsRequestAddress);
+ assertEqual("test", parsedOptions.gearsAddressLanguage);
+ assertArrayEqual(urls, parsedOptions.gearsLocationProviderUrls);
+ }
+}
+
+// Tests forming the JSON request body for a network location provider. Note
+// that the inputs to the conversion are set on the C++ side.
+function testFormRequestBody() {
+ if (isUsingCCTests) {
+ var body = internalTests.testGeolocationFormRequestBody();
+ var correctBody = '{ ' +
+ '"address_language" : "en-GB", ' +
+ '"cell_towers" : [ { ' +
+ '"cell_id" : 23874, ' +
+ '"location_area_code" : 98, ' +
+ '"mobile_country_code" : 234, ' +
+ '"mobile_network_code" : 15, ' +
+ '"signal_strength" : -65 ' +
+ '} ], ' +
+ '"host" : "www.google.com", ' +
+ '"location" : { ' +
+ '"latitude" : 53.1, ' +
+ '"longitude" : -0.1 ' +
+ '}, ' +
+ '"radio_type" : "gsm", ' +
+ '"request_address" : true, ' +
+ '"version" : "1.0.1", ' +
+ '"wifi_towers" : [ { ' +
+ '"age" : 15, ' +
+ '"channel" : 19, ' +
+ '"mac_address" : "00-0b-86-d7-6a-42", ' +
+ '"signal_strength" : -50, ' +
+ '"signal_to_noise" : 10, ' +
+ '"ssid" : "Test SSID" ' +
+ '} ] ' +
+ '}\n'; // Note trailing line break.
+ assertEqual(correctBody, body);
+ }
+}
+
+// Tests extracting a position object from the JSON reposnse from a network
+// location provider.
+function testGetLocationFromResponse() {
+ if (isUsingCCTests) {
+ var dummy_server = 'http://test.server.com';
+ var position;
+ var correctPosition;
+
+ var locationAcquisitionErrorCode = 2;
+ var locationNotFoundErrorCode = 3;
+
+ // Test good response with valid position.
+ var responseBody = '{ ' +
+ '"location" : { ' +
+ '"latitude" : 53.1, ' +
+ '"longitude" : -0.1, ' +
+ '"altitude" : 30.1, ' +
+ '"horizontal_accuracy" : 1200.1, ' +
+ '"vertical_accuracy" : 10.1, ' +
+ '"address" : { ' +
+ '"street_number": "100", ' +
+ '"street": "Amphibian Walkway", ' +
+ '"city": "Mountain View", ' +
+ '"county": "Mountain View County", ' +
+ '"region": "California", ' +
+ '"country": "United States of America", ' +
+ '"country_code": "US", ' +
+ '"postal_code": "94043" ' +
+ '} ' +
+ '} ' +
+ '}';
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ true, // HttpPost result
+ 200, // status code
+ responseBody,
+ 42, // timestamp
+ ''); // server URL
+ correctPosition = new Object();
+ correctPosition.latitude = 53.1;
+ correctPosition.longitude = -0.1;
+ correctPosition.altitude = 30.1;
+ correctPosition.accuracy = 1200.1;
+ correctPosition.altitudeAccuracy = 10.1;
+ correctPosition.gearsAddress = new Object();
+ correctPosition.gearsAddress.streetNumber = '100';
+ correctPosition.gearsAddress.street = 'Amphibian Walkway';
+ correctPosition.gearsAddress.city = 'Mountain View';
+ correctPosition.gearsAddress.county = 'Mountain View County';
+ correctPosition.gearsAddress.region = 'California';
+ correctPosition.gearsAddress.country = 'United States of America';
+ correctPosition.gearsAddress.countryCode = 'US';
+ correctPosition.gearsAddress.postalCode = '94043';
+ correctPosition.timestamp = new Date(42);
+ assertObjectEqual(correctPosition, position);
+
+ // We should also accept integer values for floating point fields.
+ var responseBody = '{ ' +
+ '"location" : { ' +
+ '"latitude" : 53, ' +
+ '"longitude" : 0, ' +
+ '"altitude" : 30, ' +
+ '"horizontal_accuracy" : 1200, ' +
+ '"vertical_accuracy" : 10, ' +
+ '"address" : { ' +
+ '"street_number": "100", ' +
+ '"street": "Amphibian Walkway", ' +
+ '"city": "Mountain View", ' +
+ '"county": "Mountain View County", ' +
+ '"region": "California", ' +
+ '"country": "United States of America", ' +
+ '"country_code": "US", ' +
+ '"postal_code": "94043" ' +
+ '} ' +
+ '} ' +
+ '}';
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ true, // HttpPost result
+ 200, // status code
+ responseBody,
+ 42, // timestamp
+ ''); // server URL
+ correctPosition.latitude = 53;
+ correctPosition.longitude = 0;
+ correctPosition.altitude = 30;
+ correctPosition.accuracy = 1200;
+ correctPosition.altitudeAccuracy = 10;
+ assertObjectEqual(correctPosition, position);
+
+ // Test no response.
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ false, // HttpPost result
+ 0, // status code
+ '', // response body
+ 0, // timestamp
+ dummy_server);
+ correctPosition = new Object();
+ correctPosition.code = locationAcquisitionErrorCode;
+ correctPosition.message = 'No response from network provider at ' +
+ dummy_server +
+ '.';
+ assertObjectEqual(correctPosition, position);
+
+ // Test bad response.
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ true, // HttpPost result
+ 400, // status code
+ '', // response body
+ 0, // timestamp
+ dummy_server);
+ correctPosition = new Object();
+ correctPosition.code = locationAcquisitionErrorCode;
+ correctPosition.message = 'Network provider at ' +
+ dummy_server +
+ ' returned error code 400.';
+ assertObjectEqual(correctPosition, position);
+
+ // Test good response with malformed body.
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ true, // HttpPost result
+ 200, // status code
+ 'malformed reposnse body',
+ 0, // timestamp
+ dummy_server);
+ correctPosition = new Object();
+ correctPosition.code = locationAcquisitionErrorCode;
+ correctPosition.message = 'Response from network provider at ' +
+ dummy_server +
+ ' was malformed.';
+ assertObjectEqual(correctPosition, position);
+
+ // Test good response with empty body.
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ true, // HttpPost result
+ 200, // status code
+ '', // response body
+ 0, // timestamp
+ dummy_server);
+ correctPosition = new Object();
+ correctPosition.code = locationAcquisitionErrorCode;
+ correctPosition.message = 'Response from network provider at ' +
+ dummy_server +
+ ' was malformed.';
+ assertObjectEqual(correctPosition, position);
+
+ // Test good response with unknown position.
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ true, // HttpPost result
+ 200, // status code
+ '{}', // response body
+ 0, // timestamp
+ dummy_server);
+ correctPosition = new Object();
+ correctPosition.code = locationNotFoundErrorCode;
+ correctPosition.message = 'Network provider at ' +
+ dummy_server +
+ ' did not provide a good position fix.'
+ assertObjectEqual(correctPosition, position);
+
+ // Test good response with explicit unknown position.
+ position = internalTests.testGeolocationGetLocationFromResponse(
+ true, // HttpPost result
+ 200, // status code
+ '{"position": null}',
+ 0, // timestamp
+ dummy_server);
+ correctPosition = new Object();
+ correctPosition.code = locationNotFoundErrorCode;
+ correctPosition.message = 'Network provider at ' +
+ dummy_server +
+ ' did not provide a good position fix.'
+ assertObjectEqual(correctPosition, position);
+ }
+}
+
+function testMockDeviceDataProvider() {
+ if (isUsingCCTests) {
+ var geolocation = google.gears.factory.create('beta.geolocation');
+ var mockNetworkLocationProvider = '/testcases/cgi/location_provider.py';
+
+ function makeSuccessfulRequest() {
+ internalTests.configureGeolocationWifiDataProviderForTest(
+ { mac_address: "good_mac_address" });
+ geolocation.getCurrentPosition(
+ successCallback,
+ function() { assert(false, 'makeSuccessfulRequest failed'); },
+ { gearsRequestAddress: true,
+ gearsLocationProviderUrls: [ mockNetworkLocationProvider ] });
+ }
+
+ function makeUnsuccessfulRequest() {
+ internalTests.configureGeolocationWifiDataProviderForTest(
+ { mac_address: "no_location_mac_address" });
+ geolocation.getCurrentPosition(
+ function() { assert(false, 'makeUnsuccessfulRequest succeeded'); },
+ noPositionErrorCallback,
+ { gearsLocationProviderUrls: [ mockNetworkLocationProvider ] });
+ }
+
+ function makeMalformedRequest() {
+ internalTests.configureGeolocationWifiDataProviderForTest(
+ { mac_address: "00-00-00-00-00-00" });
+ internalTests.configureGeolocationRadioDataProviderForTest(
+ { cell_id: 88 });
+ geolocation.getCurrentPosition(
+ function() { assert(false, 'makeMalformedRequest succeeded'); },
+ malformedRequestErrorCallback,
+ { gearsLocationProviderUrls: [ mockNetworkLocationProvider ] });
+ }
+
+ function successCallback(position) {
+ var correctPosition = { latitude: 51.59,
+ longitude: -1.49,
+ altitude: 30,
+ accuracy: 1200,
+ altitudeAccuracy: 10,
+ gearsAddress: { streetNumber: "76",
+ street: "Buckingham Palace Road",
+ postalCode: "SW1W 9TQ",
+ city: "London",
+ county: "London",
+ region: "London",
+ country: "United Kingdom",
+ countryCode: "uk" } };
+ position.timestamp = undefined;
+ assertObjectEqual(correctPosition, position);
+ makeUnsuccessfulRequest();
+ };
+
+ function noPositionErrorCallback(error) {
+ assertEqual(3, error.code); // Location not found error code.
+ assert(error.message.search('did not provide a good position fix') > 0);
+ makeMalformedRequest();
+ };
+
+ function malformedRequestErrorCallback(error) {
+ assertEqual(2, error.code); // Location acquistion error code.
+ assert(error.message.search('returned error code 400') > 0);
+ completeAsync();
+ };
+
+ startAsync();
+ makeSuccessfulRequest();
+ }
+}
+
+function testMockProvider() {
+ if (isUsingCCTests) {
+ // Use mock location provider.
+ var mockPosition = {
+ latitude: 51.0,
+ longitude: -0.1,
+ accuracy: 100.1
+ };
+
internalTests.configureGeolocationMockLocationProviderForTest(mockPosition);
+ var locationAvailable = function(position) {
+ assertEqualAnyType(mockPosition.latitude, position.latitude);
+ assertEqualAnyType(mockPosition.longitude, position.longitude);
+ assertEqualAnyType(mockPosition.accuracy, position.accuracy);
+ internalTests.removeGeolocationMockLocationProvider();
+ completeAsync();
+ };
+ var geolocation = google.gears.factory.create('beta.geolocation');
+ startAsync();
+ geolocation.getCurrentPosition(
+ locationAvailable, function() {}, { gearsLocationProviderUrls: [] });
+ }
+}
====
//depot/googleclient/gears/opensource/gears/test/testcases/internal_tests.js#37
-
c:\MyDocs\Gears1/googleclient/gears/opensource/gears/test/testcases/internal_tests.js
====
# action=edit type=text
--- googleclient/gears/opensource/gears/test/testcases/internal_tests.js
2008-09-22 14:46:39.000000000 +0100
+++ googleclient/gears/opensource/gears/test/testcases/internal_tests.js
2008-09-22 14:17:52.000000000 +0100
@@ -265,394 +265,6 @@
completeAsync();
}
});
- }
-}
-
-// Tests the parsing of the options passed to Geolocation.GetCurrentPosition
and
-// Geolocation.WatchPosition.
-function testParseGeolocationOptions() {
- // TODO(steveblock): Some of these tests do not require the test object so
- // should be moved to geolocation_tests.js.
- if (isUsingCCTests) {
- var dummyFunction = function() {};
-
- // All good.
- internalTests.testParseGeolocationOptions(dummyFunction);
- internalTests.testParseGeolocationOptions(dummyFunction, dummyFunction);
- internalTests.testParseGeolocationOptions(dummyFunction, null);
- internalTests.testParseGeolocationOptions(dummyFunction, null, {});
- internalTests.testParseGeolocationOptions(dummyFunction, dummyFunction,
{});
-
- // Test correct types.
- // Missing success callback.
- assertError(function() { internalTests.testParseGeolocationOptions() });
- // Wrong type for success callback.
- assertError(function() { internalTests.testParseGeolocationOptions(42) });
- // Wrong type for error callback.
- assertError(function() {
- internalTests.testParseGeolocationOptions(dummyFunction, 42) });
- // Wrong type for options.
- assertError(function() {
- internalTests.testParseGeolocationOptions(dummyFunction,
- dummyFunction,
- 42) });
- // Wrong type for enableHighAccuracy.
- assertError(function() {
- internalTests.testParseGeolocationOptions(dummyFunction,
- dummyFunction,
- { enableHighAccuracy: 42 }) },
- 'options.enableHighAccuracy should be a boolean.');
- // Wrong type for gearsRequestAddress.
- assertError(function() {
- internalTests.testParseGeolocationOptions(dummyFunction,
- dummyFunction,
- { gearsRequestAddress: 42 }) },
- 'options.gearsRequestAddress should be a boolean.');
- // Wrong type for gearsAddressLanguage.
- assertError(function() {
- internalTests.testParseGeolocationOptions(dummyFunction,
- dummyFunction,
- { gearsAddressLanguage: 42 })
},
- 'options.gearsAddressLanguage should be a string.');
- // Wrong type for gearsLocationProviderUrls.
- assertError(function() {
- internalTests.testParseGeolocationOptions(
- dummyFunction,
- dummyFunction,
- { gearsLocationProviderUrls: 42 }) },
- 'options.gearsLocationProviderUrls should be null or an array of ' +
- 'strings');
- assertError(function() {
- internalTests.testParseGeolocationOptions(
- dummyFunction,
- dummyFunction,
- { gearsLocationProviderUrls: [42] }) },
- 'options.gearsLocationProviderUrls should be null or an array of ' +
- 'strings');
-
- // Test correct parsing.
- var defaultUrlArray = ['http://www.google.com/loc/json'];
- var urls = ['url1', 'url2'];
- var parsedOptions;
- // No options.
- parsedOptions = internalTests.testParseGeolocationOptions(dummyFunction);
- assertEqual(false, parsedOptions.repeats);
- assertEqual(false, parsedOptions.enableHighAccuracy);
- assertEqual(false, parsedOptions.gearsRequestAddress);
- assertEqual("", parsedOptions.gearsAddressLanguage);
- assertArrayEqual(defaultUrlArray, parsedOptions.gearsLocationProviderUrls);
- // Empty options.
- parsedOptions = internalTests.testParseGeolocationOptions(dummyFunction,
- dummyFunction,
- {});
- assertEqual(false, parsedOptions.repeats);
- assertEqual(false, parsedOptions.enableHighAccuracy);
- assertEqual(false, parsedOptions.gearsRequestAddress);
- assertEqual("", parsedOptions.gearsAddressLanguage);
- assertArrayEqual(defaultUrlArray, parsedOptions.gearsLocationProviderUrls);
- // Empty provider URLs.
- parsedOptions = internalTests.testParseGeolocationOptions(
- dummyFunction,
- dummyFunction,
- { gearsLocationProviderUrls: [] });
- assertEqual(false, parsedOptions.repeats);
- assertEqual(false, parsedOptions.enableHighAccuracy);
- assertEqual(false, parsedOptions.gearsRequestAddress);
- assertEqual("", parsedOptions.gearsAddressLanguage);
- assertArrayEqual([], parsedOptions.gearsLocationProviderUrls);
- // Null provider URLs.
- parsedOptions = internalTests.testParseGeolocationOptions(
- dummyFunction,
- dummyFunction,
- { gearsLocationProviderUrls: null });
- assertEqual(false, parsedOptions.repeats);
- assertEqual(false, parsedOptions.enableHighAccuracy);
- assertEqual(false, parsedOptions.gearsRequestAddress);
- assertEqual("", parsedOptions.gearsAddressLanguage);
- assertArrayEqual([], parsedOptions.gearsLocationProviderUrls);
- // All properties false, provider URLs set.
- parsedOptions = internalTests.testParseGeolocationOptions(
- dummyFunction,
- dummyFunction,
- { enableHighAccuracy: false,
- gearsRequestAddress: false,
- gearsAddressLanguage: "",
- gearsLocationProviderUrls: urls
- });
- assertEqual(false, parsedOptions.repeats);
- assertEqual(false, parsedOptions.enableHighAccuracy);
- assertEqual(false, parsedOptions.gearsRequestAddress);
- assertEqual("", parsedOptions.gearsAddressLanguage);
- assertArrayEqual(urls, parsedOptions.gearsLocationProviderUrls);
- // All properties true, provider URLs set.
- parsedOptions = internalTests.testParseGeolocationOptions(
- dummyFunction,
- dummyFunction,
- { enableHighAccuracy: true,
- gearsRequestAddress: true,
- gearsAddressLanguage: "test",
- gearsLocationProviderUrls: urls
- });
- assertEqual(false, parsedOptions.repeats);
- assertEqual(true, parsedOptions.enableHighAccuracy);
- assertEqual(true, parsedOptions.gearsRequestAddress);
- assertEqual("test", parsedOptions.gearsAddressLanguage);
- assertArrayEqual(urls, parsedOptions.gearsLocationProviderUrls);
- }
-}
-
-// Tests forming the JSON request body for a network location provider. Note
-// that the inputs to the conversion are set on the C++ side.
-function testGeolocationFormRequestBody() {
- if (isUsingCCTests) {
- var body = internalTests.testGeolocationFormRequestBody();
- var correctBody = '{ ' +
- '"address_language" : "en-GB", ' +
- '"cell_towers" : [ { ' +
- '"cell_id" : 23874, ' +
- '"location_area_code" : 98, ' +
- '"mobile_country_code" : 234, ' +
- '"mobile_network_code" : 15, ' +
- '"signal_strength" : -65 ' +
- '} ], ' +
- '"host" : "www.google.com", ' +
- '"location" : { ' +
- '"latitude" : 53.1, ' +
- '"longitude" : -0.1 ' +
- '}, ' +
- '"radio_type" : "gsm", ' +
- '"request_address" : true, ' +
- '"version" : "1.0.1", ' +
- '"wifi_towers" : [ { ' +
- '"age" : 15, ' +
- '"channel" : 19, ' +
- '"mac_address" : "00-0b-86-d7-6a-42", ' +
- '"signal_strength" : -50, ' +
- '"signal_to_noise" : 10, ' +
- '"ssid" : "Test SSID" ' +
- '} ] ' +
- '}\n'; // Note trailing line break.
- assertEqual(correctBody, body);
- }
-}
-
-// Tests extracting a position object from the JSON reposnse from a network
-// location provider.
-function testGeolocationGetLocationFromResponse() {
- if (isUsingCCTests) {
- var dummy_server = 'http://test.server.com';
- var position;
- var correctPosition;
-
- var locationAcquisitionErrorCode = 2;
- var locationNotFoundErrorCode = 3;
-
- // Test good response with valid position.
- var responseBody = '{ ' +
- '"location" : { ' +
- '"latitude" : 53.1, ' +
- '"longitude" : -0.1, ' +
- '"altitude" : 30.1, ' +
- '"horizontal_accuracy" : 1200.1, ' +
- '"vertical_accuracy" : 10.1, ' +
- '"address" : { ' +
- '"street_number": "100", ' +
- '"street": "Amphibian Walkway", ' +
- '"city": "Mountain View", ' +
- '"county": "Mountain View County", ' +
- '"region": "California", ' +
- '"country": "United States of America", ' +
- '"country_code": "US", ' +
- '"postal_code": "94043" ' +
- '} ' +
- '} ' +
- '}';
- position = internalTests.testGeolocationGetLocationFromResponse(
- true, // HttpPost result
- 200, // status code
- responseBody,
- 42, // timestamp
- ''); // server URL
- correctPosition = new Object();
- correctPosition.latitude = 53.1;
- correctPosition.longitude = -0.1;
- correctPosition.altitude = 30.1;
- correctPosition.accuracy = 1200.1;
- correctPosition.altitudeAccuracy = 10.1;
- correctPosition.gearsAddress = new Object();
- correctPosition.gearsAddress.streetNumber = '100';
- correctPosition.gearsAddress.street = 'Amphibian Walkway';
- correctPosition.gearsAddress.city = 'Mountain View';
- correctPosition.gearsAddress.county = 'Mountain View County';
- correctPosition.gearsAddress.region = 'California';
- correctPosition.gearsAddress.country = 'United States of America';
- correctPosition.gearsAddress.countryCode = 'US';
- correctPosition.gearsAddress.postalCode = '94043';
- correctPosition.timestamp = new Date(42);
- assertObjectEqual(correctPosition, position);
-
- // We should also accept integer values for floating point fields.
- var responseBody = '{ ' +
- '"location" : { ' +
- '"latitude" : 53, ' +
- '"longitude" : 0, ' +
- '"altitude" : 30, ' +
- '"horizontal_accuracy" : 1200, ' +
- '"vertical_accuracy" : 10, ' +
- '"address" : { ' +
- '"street_number": "100", ' +
- '"street": "Amphibian Walkway", ' +
- '"city": "Mountain View", ' +
- '"county": "Mountain View County", ' +
- '"region": "California", ' +
- '"country": "United States of America", ' +
- '"country_code": "US", ' +
- '"postal_code": "94043" ' +
- '} ' +
- '} ' +
- '}';
- position = internalTests.testGeolocationGetLocationFromResponse(
- true, // HttpPost result
- 200, // status code
- responseBody,
- 42, // timestamp
- ''); // server URL
- correctPosition.latitude = 53;
- correctPosition.longitude = 0;
- correctPosition.altitude = 30;
- correctPosition.accuracy = 1200;
- correctPosition.altitudeAccuracy = 10;
- assertObjectEqual(correctPosition, position);
-
- // Test no response.
- position = internalTests.testGeolocationGetLocationFromResponse(
- false, // HttpPost result
- 0, // status code
- '', // response body
- 0, // timestamp
- dummy_server);
- correctPosition = new Object();
- correctPosition.code = locationAcquisitionErrorCode;
- correctPosition.message = 'No response from network provider at ' +
- dummy_server +
- '.';
- assertObjectEqual(correctPosition, position);
-
- // Test bad response.
- position = internalTests.testGeolocationGetLocationFromResponse(
- true, // HttpPost result
- 400, // status code
- '', // response body
- 0, // timestamp
- dummy_server);
- correctPosition = new Object();
- correctPosition.code = locationAcquisitionErrorCode;
- correctPosition.message = 'Network provider at ' +
- dummy_server +
- ' returned error code 400.';
- assertObjectEqual(correctPosition, position);
-
- // Test good response with malformed body.
- position = internalTests.testGeolocationGetLocationFromResponse(
- true, // HttpPost result
- 200, // status code
- 'malformed reposnse body',
- 0, // timestamp
- dummy_server);
- correctPosition = new Object();
- correctPosition.code = locationAcquisitionErrorCode;
- correctPosition.message = 'Response from network provider at ' +
- dummy_server +
- ' was malformed.';
- assertObjectEqual(correctPosition, position);
-
- // Test good response with empty body.
- position = internalTests.testGeolocationGetLocationFromResponse(
- true, // HttpPost result
- 200, // status code
- '', // response body
- 0, // timestamp
- dummy_server);
- correctPosition = new Object();
- correctPosition.code = locationAcquisitionErrorCode;
- correctPosition.message = 'Response from network provider at ' +
- dummy_server +
- ' was malformed.';
- assertObjectEqual(correctPosition, position);
-
- // Test good response with unknown position.
- position = internalTests.testGeolocationGetLocationFromResponse(
- true, // HttpPost result
- 200, // status code
- '{}', // response body
- 0, // timestamp
- dummy_server);
- correctPosition = new Object();
- correctPosition.code = locationNotFoundErrorCode;
- correctPosition.message = 'Network provider at ' +
- dummy_server +
- ' did not provide a good position fix.'
- assertObjectEqual(correctPosition, position);
-
- // Test good response with explicit unknown position.
- position = internalTests.testGeolocationGetLocationFromResponse(
- true, // HttpPost result
- 200, // status code
- '{"position": null}',
- 0, // timestamp
- dummy_server);
- correctPosition = new Object();
- correctPosition.code = locationNotFoundErrorCode;
- correctPosition.message = 'Network provider at ' +
- dummy_server +
- ' did not provide a good position fix.'
- assertObjectEqual(correctPosition, position);
- }
-}
-
-function testGeolocationGetCurrentPosition() {
- if (isUsingCCTests) {
-
- // Use mock radio and WiFi device data providers.
- internalTests.configureGeolocationRadioDataProviderForTest(
- { cell_id: 23874,
- location_area_code: 98,
- mobile_network_code: 15,
- mobile_country_code: 234,
- radio_signal_strength: -65
- });
- internalTests.configureGeolocationWifiDataProviderForTest(
- { mac_address: "00-0b-86-ca-bb-c8" });
- // TODO(steveblock): Complete this test once we have a working, public
- // network location provider.
- //var geolocation = google.gears.factory.create('beta.geolocation');
- //var locationAvailable = function(position) {
- // completeAsync();
- //};
- //startAsync();
- //geolocation.getCurrentPosition(locationAvailable,
- // function() {},
- // { gearsLocationProviderUrls: ['TODO'] });
-
- // Use mock location provider.
- var mockPosition = {
- latitude: 51.0,
- longitude: -0.1,
- accuracy: 100.1
- };
-
internalTests.configureGeolocationMockLocationProviderForTest(mockPosition);
- var locationAvailable2 = function(position) {
- assertEqualAnyType(mockPosition.latitude, position.latitude);
- assertEqualAnyType(mockPosition.longitude, position.longitude);
- assertEqualAnyType(mockPosition.accuracy, position.accuracy);
- internalTests.removeGeolocationMockLocationProvider();
- completeAsync();
- };
- var geolocation2 = google.gears.factory.create('beta.geolocation');
- startAsync();
- geolocation2.getCurrentPosition(locationAvailable2,
- function() {},
- { gearsLocationProviderUrls: [] });
}
}