2 <title>Tests registering regions and receiving events.
</title>
3 <script src=
"../resources/testharness.js"></script>
4 <script src=
"../resources/testharnessreport.js"></script>
5 <script src=
"../resources/testharness-helpers.js"></script>
6 <script src=
"../serviceworker/resources/test-helpers.js"></script>
8 var sw_url
= 'resources/worker-passes-events-back.js';
9 var sw_scope
= 'resources/service-worker-scope' + window
.location
.pathname
;
12 assert_true(window
.testRunner
instanceof Object
);
14 }, 'window.testRunner is required for the following tests.');
16 testRunner
.setGeofencingMockProvider(true);
18 // Returns a promise that resolves to the first message received by |port|.
19 // Removes any message event handlers that might exist on the |port|.
20 function wait_for_reply(t
, port
) {
21 return new Promise(function(resolve
) {
22 port
.onmessage
= t
.step_func(function(event
) {
23 port
.onmessage
= null;
29 promise_test(function(test
) {
32 return service_worker_unregister_and_register(test
, sw_url
, sw_scope
)
33 .then(test
.step_func(function(r
) {
35 return wait_for_state(test
, r
.installing
, 'activated');
36 })).then(test
.step_func(function() {
37 var channel
= new MessageChannel();
39 registration
.active
.postMessage({port
: channel
.port2
}, [channel
.port2
]);
40 return wait_for_reply(test
, port
);
41 })).then(test
.step_func(function(reply
) {
42 assert_equals(reply
, 'setup');
43 return registration
.geofencing
.registerRegion(
44 new CircularGeofencingRegion({id
: 'myid',
46 longitude
: -122.084015,
48 })).then(test
.step_func(function() {
49 testRunner
.setGeofencingMockPosition(37.422, -122.084015);
50 return wait_for_reply(test
, port
);
51 })).then(test
.step_func(function(reply
) {
52 assert_equals(reply
.event
, 'geofenceenter');
53 assert_equals(reply
.id
, 'myid');
54 testRunner
.setGeofencingMockPosition(37.423, -122.084015);
55 return wait_for_reply(test
, port
);
56 })).then(test
.step_func(function(reply
) {
57 assert_equals(reply
.event
, 'geofenceleave');
58 assert_equals(reply
.id
, 'myid');
59 return registration
.geofencing
.registerRegion(
60 new CircularGeofencingRegion({id
: 'bigregion',
62 longitude
: -122.084015,
64 })).then(test
.step_func(function() {
65 return wait_for_reply(test
, port
);
66 })).then(test
.step_func(function(reply
) {
67 assert_equals(reply
.event
, 'geofenceenter');
68 assert_equals(reply
.id
, 'bigregion');
69 return service_worker_unregister(test
, sw_scope
);
71 }, 'Tests basic enter and leave events.');