2 <title>Service Worker: Client.id
</title>
3 <script src=
"../resources/testharness.js"></script>
4 <script src=
"../resources/testharnessreport.js"></script>
5 <script src=
"resources/test-helpers.js"></script>
8 var scope
= 'resources/blank.html?client-id';
11 async_test(function(t
) {
13 service_worker_unregister_and_register(
14 t
, 'resources/client-id-worker.js', scope
)
15 .then(function(registration
) {
16 return wait_for_state(t
, registration
.installing
, 'activated');
18 .then(function() { return with_iframe(scope
+ '#1'); })
21 // To be sure Clients.matchAll() iterates in the same order.
23 return with_iframe(scope
+ '#2');
27 var channel
= new MessageChannel();
28 channel
.port1
.onmessage
= t
.step_func(on_message
);
29 f
.contentWindow
.navigator
.serviceWorker
.controller
.postMessage(
30 {port
:channel
.port2
}, [channel
.port2
]);
32 .catch(unreached_rejection(t
));
33 }, 'Client.id returns the client\'s UUID.');
35 // A regex object for UUID(http://tools.ietf.org/html/rfc4122) validation.
36 var pattern
= new RegExp(['^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-',
37 '9a-f]{3}-[0-9a-f]{12}$'].join(''), 'i');
39 function validate_uuid(id
) {
40 return !!id
.match(pattern
);
43 function on_message(e
) {
44 // The result of two sequential clients.matchAll() calls in the SW.
45 // 1st matchAll() results in e.data[0], e.data[1].
46 // 2nd matchAll() results in e.data[2], e.data[3].
47 assert_equals(e
.data
.length
, 4);
48 // All should be valid UUIDs.
49 assert_true(validate_uuid(e
.data
[0]));
50 assert_true(validate_uuid(e
.data
[1]));
51 assert_true(validate_uuid(e
.data
[2]));
52 assert_true(validate_uuid(e
.data
[3]));
53 // Different clients should have different ids.
54 assert_not_equals(e
.data
[0], e
.data
[1]);
55 assert_not_equals(e
.data
[2], e
.data
[3]);
56 // Same clients should have an identical id.
57 assert_equals(e
.data
[0], e
.data
[2]);
58 assert_equals(e
.data
[1], e
.data
[3]);
61 service_worker_unregister_and_done(test
, scope
);