What's new? | Help | Directory | Sign in
Google
gears
Improving Your Web Browser
  
  
  
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 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 USING_CCTESTS

#include <assert.h>
#include "gears/base/common/event.h"
#include "gears/base/common/js_types.h"
#include "gears/base/common/js_runner.h"
#include "gears/base/common/scoped_refptr.h"
#include "gears/base/common/thread.h"
#include "gears/blob/blob.h"
#include "gears/geolocation/geolocation.h"
#include "gears/geolocation/device_data_provider.h"
#include "gears/geolocation/network_location_request.h"
#include "third_party/scoped_ptr/scoped_ptr.h"

static const char16 *kAgeString = STRING16(L"age");
static const char16 *kCellIdString = STRING16(L"cell_id");
static const char16 *kLocationAreaCodeString =
STRING16(L"location_area_code");
static const char16 *kMobileNetworkCodeString =
STRING16(L"mobile_network_code");
static const char16 *kMobileCountryCodeString =
STRING16(L"mobile_country_code");
static const char16 *kRadioSignalStrengthString =
STRING16(L"radio_signal_strength");
static const char16 *kTimingAdvanceString = STRING16(L"timing_advance");

// Local function
// Retrives a named integer property from a JavaScript object. Does nothing if
// the property is undefined but throws an exception if the property is of any
// other non-integer type.
static void GetIntegerPropertyIfDefined(JsCallContext *context,
const JsObject& jsObject,
const std::string16& name,
int *value);

// A mock implementation of DeviceDataProviderImplBase for testing. It simply
// calls back once every second with constant data.
template<typename DataType>
class MockDeviceDataProviderImpl
: public DeviceDataProviderImplBase<DataType>,
public Thread {
public:
MockDeviceDataProviderImpl() {
Start();
}
virtual ~MockDeviceDataProviderImpl() {
stop_event_.Signal();
Join();
}

private:
// Thread implementation.
virtual void Run() {
while (!stop_event_.WaitWithTimeout(1000)) {
DeviceDataProviderImplBase<DataType>::NotifyListeners();
}
}

Event stop_event_;
DISALLOW_EVIL_CONSTRUCTORS(MockDeviceDataProviderImpl);
};

// Implements MockDeviceDataProviderImpl for RadioData and provides sample data.
class MockRadioDataProviderImpl : public MockDeviceDataProviderImpl<RadioData> {
public:
MockRadioDataProviderImpl() {}
virtual ~MockRadioDataProviderImpl() {}

// Factory method for use with DeviceDataProvider::SetFactory.
static RadioDataProviderImplBase *Create() {
return new MockRadioDataProviderImpl();
}

// DeviceDataProviderImplBase implementation.
virtual bool GetData(RadioData *data) {
assert(data);
*data = radio_data;
// We always have all the data we can get, so return true.
return true;
}

static void SetData(const RadioData &radio_data_in) {
radio_data = radio_data_in;
}

private:
static RadioData radio_data;

DISALLOW_EVIL_CONSTRUCTORS(MockRadioDataProviderImpl);
};

// static
RadioData MockRadioDataProviderImpl::radio_data;

// Implements MockDeviceDataProviderImpl for WifiData and provides sample data.
class MockWifiDataProviderImpl : public MockDeviceDataProviderImpl<WifiData> {
public:
MockWifiDataProviderImpl() {}
virtual ~MockWifiDataProviderImpl() {}

// Factory method for use with DeviceDataProvider::SetFactory.
static WifiDataProviderImplBase *Create() {
return new MockWifiDataProviderImpl();
}

virtual bool GetData(WifiData *data) {
assert(data);
// The following are real-world values, captured in May 2008.
AccessPointData access_point_data;
data->access_point_data.clear();
access_point_data.mac_address = STRING16(L"00-0b-86-ca-bb-c8");
data->access_point_data.push_back(access_point_data);
access_point_data.mac_address = STRING16(L"00-0b-86-ca-bb-c9");
data->access_point_data.push_back(access_point_data);
access_point_data.mac_address = STRING16(L"00-0b-86-ce-51-80");
data->access_point_data.push_back(access_point_data);
access_point_data.mac_address = STRING16(L"00-0d-97-04-85-9d");
data->access_point_data.push_back(access_point_data);
// We always have all the data we can get, so return true.
return true;
}

private:
DISALLOW_EVIL_CONSTRUCTORS(MockWifiDataProviderImpl);
};

void TestParseGeolocationOptions(JsCallContext *context,
JsRunnerInterface *js_runner) {
std::vector<std::string16> urls;
GearsGeolocation::FixRequestInfo info;
if (!GearsGeolocation::ParseArguments(context, false, &urls, &info)) {
if (!context->is_exception_set()) {
context->SetException(
STRING16(L"Internal error parsing geolocation options."));
}
return;
}
// Add the urls as a property of the returned object.
scoped_ptr<JsObject> return_object(js_runner->NewObject());
assert(return_object.get());
scoped_ptr<JsArray> url_array(js_runner->NewArray());
assert(url_array.get());
for (int i = 0; i < static_cast<int>(urls.size()); ++i) {
if (!url_array->SetElementString(i, urls[i])) {
context->SetException(STRING16(L"Failed to set return value."));
return;
}
}
if (!return_object->SetPropertyBool(STRING16(L"repeats"), info.repeats) ||
!return_object->SetPropertyBool(STRING16(L"enableHighAccuracy"),
info.enable_high_accuracy) ||
!return_object->SetPropertyBool(STRING16(L"requestAddress"),
info.request_address) ||
!return_object->SetPropertyString(STRING16(L"addressLanguage"),
info.address_language) ||
!return_object->SetPropertyArray(STRING16(L"gearsLocationProviderUrls"),
url_array.get())) {
context->SetException(STRING16(L"Failed to set return value."));
return;
}
context->SetReturnValue(JSPARAM_OBJECT, return_object.get());
}

void TestGeolocationFormRequestBody(JsCallContext *context) {
// Parsing radio and wifi data from JavaScript would involve more complex code
// than that which we're trying to test. Instead we hard-code these values.
// The values must be kept in sync with internal_tests.js.

RadioData radio_data;
CellData cell_data;
cell_data.cell_id = 23874;
cell_data.location_area_code = 98;
cell_data.mobile_network_code = 15;
cell_data.mobile_country_code = 234;
cell_data.radio_signal_strength = -65;
radio_data.cell_data.push_back(cell_data);
radio_data.radio_type = RADIO_TYPE_GSM;

WifiData wifi_data;
AccessPointData access_point_data;
access_point_data.mac_address = STRING16(L"00-0b-86-d7-6a-42");
access_point_data.radio_signal_strength = -50;
access_point_data.age = 15;
access_point_data.channel = 19;
access_point_data.signal_to_noise = 10;
access_point_data.ssid = STRING16(L"Test SSID");
wifi_data.access_point_data.push_back(access_point_data);

scoped_refptr<BlobInterface> blob;
if (!NetworkLocationRequest::FormRequestBody(STRING16(L"www.google.com"),
radio_data,
wifi_data,
true,
STRING16(L"en-GB"),
53.1,
-0.1,
&blob)) {
context->SetException(STRING16(L"Failed to form request body."));
return;
}
assert(blob.get());
int64 length = blob->Length();
// We only handle the case where the size of the blob is less than kuint32max.
// All good request bodies should be much smaller than this.
assert(length > 0 && length < kuint32max);
std::vector<uint8> request_body_vector;
request_body_vector.resize(static_cast<unsigned int>(length));
if (!blob->Read(&request_body_vector[0], 0, length)) {
context->SetException(STRING16(L"Failed to extract request string from "
L"blob."));
return;
}
std::string16 request_body;
if (!UTF8ToString16(reinterpret_cast<char*>(&request_body_vector[0]),
request_body_vector.size(),
&request_body)) {
context->SetException(STRING16(L"Failed to convert request string from "
L"UTF8."));
return;
}
context->SetReturnValue(JSPARAM_STRING16, &request_body);
}

void TestGeolocationGetLocationFromResponse(JsCallContext *context,
JsRunnerInterface *js_runner) {
bool http_post_result;
int status_code;
std::string16 response_body;
int64 timestamp;
std::string16 server_url;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_BOOL, &http_post_result },
{ JSPARAM_REQUIRED, JSPARAM_INT, &status_code },
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &response_body },
{ JSPARAM_REQUIRED, JSPARAM_INT64, &timestamp },
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &server_url },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set()) {
return;
}

std::string response_body_utf8;
if (!String16ToUTF8(response_body.c_str(), response_body.size(),
&response_body_utf8)) {
context->SetException(
STRING16(L"Failed to convert response body to UTF8."));
return;
}

Position position;
NetworkLocationRequest::GetLocationFromResponse(http_post_result,
status_code,
response_body_utf8,
timestamp,
server_url,
&position);

scoped_ptr<JsObject> position_object(js_runner->NewObject());
if (!GearsGeolocation::ConvertPositionToJavaScriptObject(
position,
true, // Use address if present
js_runner,
position_object.get())) {
context->SetException(STRING16(L"Failed to convert position to JavaScript"
L"object."));
return;
}
context->SetReturnValue(JSPARAM_OBJECT, position_object.get());
}

void ConfigureGeolocationRadioDataProviderForTest(JsCallContext *context) {
assert(context);

// Get the arguments provided from JavaScript.
JsObject jsObject;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_OBJECT, &jsObject },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set()) {
return;
}

CellData cell_data;
// Update the fields of cell_data, if they have been provided.
GetIntegerPropertyIfDefined(context, jsObject, kCellIdString,
&cell_data.cell_id);
GetIntegerPropertyIfDefined(context, jsObject, kLocationAreaCodeString,
&cell_data.location_area_code);
GetIntegerPropertyIfDefined(context, jsObject, kMobileNetworkCodeString,
&cell_data.mobile_network_code);
GetIntegerPropertyIfDefined(context, jsObject, kMobileCountryCodeString,
&cell_data.mobile_country_code);
GetIntegerPropertyIfDefined(context, jsObject, kAgeString, &cell_data.age);
GetIntegerPropertyIfDefined(context, jsObject, kRadioSignalStrengthString,
&cell_data.radio_signal_strength);
GetIntegerPropertyIfDefined(context, jsObject, kTimingAdvanceString,
&cell_data.timing_advance);

RadioData radio_data;
radio_data.cell_data.push_back(cell_data);
MockRadioDataProviderImpl::SetData(radio_data);
RadioDataProvider::SetFactory(MockRadioDataProviderImpl::Create);
}

void ConfigureGeolocationWifiDataProviderForTest(JsCallContext *context) {
// TODO(baran): Enable passing a wifi data object as a JavaScript object, so
// that callers of this method can specify the wifi data.
WifiDataProvider::SetFactory(MockWifiDataProviderImpl::Create);
}

// Local function.
static void GetIntegerPropertyIfDefined(JsCallContext *context,
const JsObject& jsObject,
const std::string16& name,
int *value) {
// GetProperty should always succeed, but will get a token of type
// JSPARAM_UNDEFINED if the requested property is not present.
JsScopedToken token;
if (!jsObject.GetProperty(name, &token)) {
assert(false);
return;
}
if (JsTokenGetType(token, context->js_context()) == JSPARAM_UNDEFINED) {
return;
}
if (!JsTokenToInt_NoCoerce(token, context->js_context(), value)) {
std::string16 error = STRING16(L"cell_data.");
error += name;
error += STRING16(L" should be an integer.");
context->SetException(error);
}
}

#endif // USING_CCTESTS
Show details Hide details

Change log

r2433 by gears.daemon on Jul 21 (3 days ago)   Diff
[Author: steveblock]

Prepares Geolocation API for the big time.

R=andreip
CC=gears-eng@googlegroups.com
APPROVED=zakcohen
DELTA=144  (6 added, 134 deleted, 4
changed)
OCL=7613748
SCL=7758916
Go to: 
Project members, sign in to write a code review

Older revisions

r2359 by gears.daemon on Jul 15, 2008   Diff
[Author: steveblock]

Adds ability to set from JavaScript
the data provided by mock device data
providers.
...
r2247 by gears.daemon on Jul 05, 2008   Diff
[Author: jianli]

Move Thread class from geolocation to
base/common.
Support starting notifier process
...
r2201 by gears.daemon on Jul 01, 2008   Diff
[Author: steveblock]

Adds some tests for extracting a
position from a location server
response.
...
All revisions of this file

File info

Size: 14361 bytes, 361 lines