| Kunihiko Sakamoto | 828d028 | 2017-11-10 07:01:59 | [diff] [blame^] | 1 | <!DOCTYPE html> |
| 2 | <script src="/resources/testharness.js"></script> |
| 3 | <script src="/resources/testharnessreport.js"></script> |
| 4 | <body> |
| 5 | <script> |
| 6 | |
| 7 | function verifyNumberOfDownloads(url, number) { |
| 8 | var numDownloads = 0; |
| 9 | let absoluteURL = new URL(url, location.href).href; |
| 10 | performance.getEntriesByName(absoluteURL).forEach(entry => { |
| 11 | if (entry.transferSize > 0) { |
| 12 | numDownloads++; |
| 13 | } |
| 14 | }); |
| 15 | assert_equals(numDownloads, number, url); |
| 16 | } |
| 17 | |
| 18 | function attachAndWaitForLoad(element) { |
| 19 | return new Promise((resolve, reject) => { |
| 20 | element.onload = resolve; |
| 21 | element.onerror = reject; |
| 22 | document.body.appendChild(element); |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | function attachAndWaitForError(element) { |
| 27 | return new Promise((resolve, reject) => { |
| 28 | element.onload = reject; |
| 29 | element.onerror = resolve; |
| 30 | document.body.appendChild(element); |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | promise_test(function(t) { |
| 35 | var link = document.createElement('link'); |
| 36 | link.rel = 'modulepreload'; |
| 37 | link.href = 'resources/dummy.js'; |
| 38 | return attachAndWaitForLoad(link).then(() => { |
| 39 | verifyNumberOfDownloads('resources/dummy.js', 1); |
| 40 | |
| 41 | // Verify that <script> doesn't fetch the module again. |
| 42 | var script = document.createElement('script'); |
| 43 | script.type = 'module'; |
| 44 | script.src = 'resources/dummy.js'; |
| 45 | return attachAndWaitForLoad(script); |
| 46 | }).then(() => { |
| 47 | verifyNumberOfDownloads('resources/dummy.js', 1); |
| 48 | }); |
| 49 | }, 'link rel=modulepreload'); |
| 50 | |
| 51 | promise_test(function(t) { |
| 52 | var link = document.createElement('link'); |
| 53 | link.rel = 'modulepreload'; |
| 54 | link.href = 'resources/module1.js'; |
| 55 | return attachAndWaitForLoad(link).then(() => { |
| 56 | // Currently, modulepreload doesn't perform submodules fetch. |
| 57 | verifyNumberOfDownloads('resources/module1.js', 1); |
| 58 | verifyNumberOfDownloads('resources/module2.js', 0); |
| 59 | |
| 60 | var script = document.createElement('script'); |
| 61 | script.type = 'module'; |
| 62 | script.src = 'resources/module1.js'; |
| 63 | return attachAndWaitForLoad(script); |
| 64 | }).then(() => { |
| 65 | verifyNumberOfDownloads('resources/module1.js', 1); |
| 66 | verifyNumberOfDownloads('resources/module2.js', 1); |
| 67 | }); |
| 68 | }, 'link rel=modulepreload with submodules'); |
| 69 | |
| 70 | promise_test(function(t) { |
| 71 | var link = document.createElement('link'); |
| 72 | link.rel = 'modulepreload'; |
| 73 | link.href = 'resources/syntax-error.js'; |
| 74 | return attachAndWaitForError(link); |
| 75 | }, 'link rel=modulepreload for a module with syntax error'); |
| 76 | |
| 77 | promise_test(function(t) { |
| 78 | var link = document.createElement('link'); |
| 79 | link.rel = 'modulepreload'; |
| 80 | link.href = 'resources/not-exist.js'; |
| 81 | return attachAndWaitForError(link); |
| 82 | }, 'link rel=modulepreload for a module with network error'); |
| 83 | |
| 84 | promise_test(function(t) { |
| 85 | var link = document.createElement('link'); |
| 86 | link.rel = 'modulepreload'; |
| 87 | link.href = null; |
| 88 | return attachAndWaitForError(link); |
| 89 | }, 'link rel=modulepreload with bad href attribute'); |
| 90 | |
| 91 | </script> |
| 92 | </body> |