Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / chromium / memory-cache.html
blobe5aabe5d8cac413551593b5266932209396a704a
1 <!DOCTYPE html>
2 <html>
3 <title>Service Worker: Memory Cache</title>
4 <script src="../../resources/testharness.js"></script>
5 <script src="../../resources/testharnessreport.js"></script>
6 <script src="../resources/test-helpers.js"></script>
7 <script>
8 var callback;
10 function getJSONP(url) {
11 var sc = document.createElement('script');
12 sc.src = url;
13 document.body.appendChild(sc);
14 return new Promise(function(resolve) {
15 // This callback function is called by appending a script element.
16 callback = function(data) { resolve(data); }
17 });
20 async_test(function(t) {
21 var scope = 'resources/memory-cache-controlled.html';
22 var worker = 'resources/memory-cache-worker.js';
23 var json_url = '/serviceworker/chromium/resources/memory-cache.jsonp';
24 var registration;
25 var frame;
26 var promises = [];
28 service_worker_unregister_and_register(t, worker, scope)
29 .then(function(r) {
30 registration = r;
31 return wait_for_state(t, r.installing, 'activated');
33 .then(function() { return with_iframe(scope); })
34 .then(function(f) {
35 frame = f;
37 // Request a json file from controlled page.
38 assert_false(
39 frame.contentWindow.internals.isLoadingFromMemoryCache(json_url),
40 'Cache for controlled page should be empty');
41 promises.push(frame.contentWindow.getJSONP(json_url));
43 // Request a json file from non-controlled page.
44 assert_false(
45 internals.isLoadingFromMemoryCache(json_url),
46 'Cache for non-controlled page should be empty');
47 promises.push(getJSONP(json_url));
49 return Promise.all(promises);
51 .then(function(results) {
52 assert_equals(
53 results[0].src,
54 'service worker',
55 'Response for controlled page should be served by Service Worker');
56 assert_equals(
57 results[1].src,
58 'network',
59 'Response for non-controlled page should be served by network');
60 assert_true(
61 frame.contentWindow.internals.isLoadingFromMemoryCache(json_url),
62 'Response for controlled page should be cached');
63 assert_true(
64 internals.isLoadingFromMemoryCache(json_url),
65 'Response for non-controlled page should be cached');
66 frame.remove();
67 return registration.unregister();
69 .then(function() {
70 t.done();
72 .catch(unreached_rejection(t));
73 }, 'Non-controlled page should not use a cache filled by Service Worker');
74 </script>