OLD | NEW |
1 var TestUtils = (function() { | 1 var TestUtils = (function() { |
2 function randomString() { | 2 function randomString() { |
3 var result = ""; | 3 var result = ""; |
4 for (var i = 0; i < 5; i++) | 4 for (var i = 0; i < 5; i++) |
5 result += String.fromCharCode(97 + Math.floor(Math.random() * 26)); | 5 result += String.fromCharCode(97 + Math.floor(Math.random() * 26)); |
6 return result; | 6 return result; |
7 }; | 7 }; |
8 | 8 |
| 9 /** @string The base URL this test. */ |
| 10 var base_url = location.origin + "/clear-site-data/"; |
| 11 |
| 12 /** @string Name of the resource to be tested for cache emptiness. */ |
| 13 var cached_resource_name; |
| 14 |
9 /** | 15 /** |
10 * Representation of one datatype. | 16 * Representation of one datatype. |
11 * @typedef Datatype | 17 * @typedef Datatype |
12 * @type{object} | 18 * @type{object} |
13 * @property{string} name Name of the datatype. | 19 * @property{string} name Name of the datatype. |
14 * @method{function():Void} add A function to add an instance of the datatype. | 20 * @method{function():Void} add A function to add an instance of the datatype. |
15 * @method{function():boolean} isEmpty A function that tests whether | 21 * @method{function():boolean} isEmpty A function that tests whether |
16 * the datatype's storage backend is empty. | 22 * the datatype's storage backend is empty. |
17 */ | 23 */ |
18 var Datatype; | 24 var Datatype; |
19 | 25 |
20 var TestUtils = {}; | 26 var TestUtils = {}; |
21 | 27 |
22 /** | 28 /** |
| 29 * Sets the name of the resource used to test cache emptiness. |
| 30 * @param{string} name Name of the resource. |
| 31 */ |
| 32 TestUtils.setCachedResourceName = function(name) { |
| 33 cached_resource_name = name; |
| 34 }; |
| 35 |
| 36 /** Randomly changes the name of the resource used to test cache emptiness. */ |
| 37 TestUtils.bustCache = function() { |
| 38 TestUtils.setCachedResourceName(randomString()); |
| 39 }; |
| 40 |
| 41 /** |
23 * All datatypes supported by Clear-Site-Data. | 42 * All datatypes supported by Clear-Site-Data. |
24 * @param{Array.<Datatype>} | 43 * @param{Array.<Datatype>} |
25 */ | 44 */ |
26 TestUtils.DATATYPES = [ | 45 TestUtils.DATATYPES = [ |
27 { | 46 { |
28 "name": "cookies", | 47 "name": "cookies", |
29 "add": function() { | 48 "add": function() { |
30 return new Promise(function(resolve, reject) { | 49 return new Promise(function(resolve, reject) { |
31 document.cookie = randomString() + "=" + randomString(); | 50 document.cookie = randomString() + "=" + randomString(); |
32 resolve(); | 51 resolve(); |
(...skipping 11 matching lines...) Expand all Loading... |
44 return new Promise(function(resolve, reject) { | 63 return new Promise(function(resolve, reject) { |
45 localStorage.setItem(randomString(), randomString()); | 64 localStorage.setItem(randomString(), randomString()); |
46 resolve(); | 65 resolve(); |
47 }); | 66 }); |
48 }, | 67 }, |
49 "isEmpty": function() { | 68 "isEmpty": function() { |
50 return new Promise(function(resolve, reject) { | 69 return new Promise(function(resolve, reject) { |
51 resolve(!localStorage.length); | 70 resolve(!localStorage.length); |
52 }); | 71 }); |
53 } | 72 } |
54 } | 73 }, |
| 74 { |
| 75 "name": "cache", |
| 76 "add": function() { |
| 77 // Request a resource from the get_resource_to_cache.py server. |
| 78 // The server is instructed to return a high "Cache-Control: max-age" |
| 79 // header value, to ensure that this resource will really be added |
| 80 // to the cache. |
| 81 return fetch(base_url + "support/get-resource-to-cache.py?" + |
| 82 cached_resource_name); |
| 83 }, |
| 84 "isEmpty": function() { |
| 85 return new Promise(function(resolve, reject) { |
| 86 // Request the resource with the "Cache-Control: only-if-cached" |
| 87 // header. If the request suceeds, the resource must have been found |
| 88 // in the cache. Since not all user agents support this header value, |
| 89 // the get-resource-to-cache.py server is instructed to respond with |
| 90 // status code 500 if it sees such a request. |
| 91 var fetch_options = { |
| 92 "headers": new Headers({ "Cache-Control": "only-if-cached" }), |
| 93 }; |
| 94 |
| 95 fetch(base_url + "support/get-resource-to-cache.py?" + |
| 96 cached_resource_name, |
| 97 fetch_options) |
| 98 .then(function(response) { |
| 99 resolve(response.status == 500 /* Internal server error. */); |
| 100 }).catch(function() { |
| 101 resolve(true); |
| 102 }); |
| 103 }); |
| 104 } |
| 105 }, |
55 ]; | 106 ]; |
56 | 107 |
57 /** | 108 /** |
58 * All possible combinations of datatypes. | 109 * All possible combinations of datatypes. |
59 * @property {Array.<Array.<Datatype>>} | 110 * @property {Array.<Array.<Datatype>>} |
60 */ | 111 */ |
61 TestUtils.COMBINATIONS = (function() { | 112 TestUtils.COMBINATIONS = (function() { |
62 var combinations = []; | 113 var combinations = []; |
63 for (var mask = 0; mask < (1 << TestUtils.DATATYPES.length); mask++) { | 114 for (var mask = 0; mask < (1 << TestUtils.DATATYPES.length); mask++) { |
64 var combination = []; | 115 var combination = []; |
(...skipping 10 matching lines...) Expand all Loading... |
75 })(); | 126 })(); |
76 | 127 |
77 /** | 128 /** |
78 * Get the support server URL that returns a Clear-Site-Data header | 129 * Get the support server URL that returns a Clear-Site-Data header |
79 * to clear |datatypes|. | 130 * to clear |datatypes|. |
80 * @param{Array.<Datatype>} datatypes The list of datatypes to be deleted. | 131 * @param{Array.<Datatype>} datatypes The list of datatypes to be deleted. |
81 * @return string The URL to be queried. | 132 * @return string The URL to be queried. |
82 */ | 133 */ |
83 TestUtils.getClearSiteDataUrl = function(datatypes) { | 134 TestUtils.getClearSiteDataUrl = function(datatypes) { |
84 names = datatypes.map(function(e) { return e.name }); | 135 names = datatypes.map(function(e) { return e.name }); |
85 return "support/echo-clear-site-data.py?" + names.join("&"); | 136 return base_url + "support/echo-clear-site-data.py?cached_resource_name=" + |
| 137 cached_resource_name + "&" + names.join("&"); |
86 } | 138 } |
87 | 139 |
88 return TestUtils; | 140 return TestUtils; |
89 })(); | 141 })(); |
OLD | NEW |