OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 var initialize_CacheStorageTest = function() { |
| 6 InspectorTest.preloadPanel("resources"); |
| 7 |
| 8 InspectorTest.dumpCacheTree = function() |
| 9 { |
| 10 WebInspector.panels.resources.cacheStorageListTreeElement.expand(); |
| 11 InspectorTest.addResult("Dumping CacheStorage tree:"); |
| 12 var cachesTreeElement = WebInspector.panels.resources.cacheStorageListTreeEl
ement; |
| 13 var promise = new Promise(function(resolve, reject) { |
| 14 InspectorTest.addSnifferPromise(WebInspector.ServiceWorkerCacheModel.pro
totype, "_updateCacheNames").then(crawlCacheTree).catch(reject); |
| 15 |
| 16 function crawlCacheTree() |
| 17 { |
| 18 if (!cachesTreeElement.childCount()) { |
| 19 InspectorTest.addResult(" (empty)"); |
| 20 return resolve(); |
| 21 } |
| 22 |
| 23 WebInspector.panels.resources.cacheStorageListTreeElement._refreshCa
ches(); |
| 24 queryView(0); |
| 25 |
| 26 function queryView(i) |
| 27 { |
| 28 var cacheTreeElement = cachesTreeElement.childAt(i); |
| 29 InspectorTest.addResult(" cache: " + cacheTreeElement.titleTe
xt); |
| 30 var view = cacheTreeElement._view; |
| 31 InspectorTest.addSniffer(WebInspector.ServiceWorkerCacheView.pro
totype, "_updateDataCallback", addDataResult, false); |
| 32 if (!view) |
| 33 cacheTreeElement.onselect(false); |
| 34 else |
| 35 view._updateData(true); |
| 36 view = cacheTreeElement._view; |
| 37 |
| 38 function addDataResult() |
| 39 { |
| 40 if (view._entries.length == 0) { |
| 41 InspectorTest.addResult(" (cache empty)"); |
| 42 nextOrResolve(); |
| 43 return; |
| 44 } |
| 45 for (var entry of view._entries) |
| 46 InspectorTest.addResult(" '" + entry.request._val
ue + "': '" + entry.response._value + "'"); |
| 47 nextOrResolve(); |
| 48 } |
| 49 |
| 50 function nextOrResolve() |
| 51 { |
| 52 var next = i + 1; |
| 53 if (next < cachesTreeElement.childCount()) |
| 54 queryView(next); |
| 55 else |
| 56 resolve(); |
| 57 } |
| 58 } |
| 59 } |
| 60 }); |
| 61 WebInspector.panels.resources.cacheStorageListTreeElement._refreshCaches(); |
| 62 return promise; |
| 63 } |
| 64 |
| 65 InspectorTest.deleteCacheFromInspector = function(cacheName) |
| 66 { |
| 67 WebInspector.panels.resources.cacheStorageListTreeElement.expand(); |
| 68 InspectorTest.addResult("Deleting CacheStorage cache " + cacheName); |
| 69 var cachesTreeElement = WebInspector.panels.resources.cacheStorageListTreeEl
ement; |
| 70 var promise = new Promise(function(resolve, reject) { |
| 71 InspectorTest.addSnifferPromise(WebInspector.ServiceWorkerCacheModel.pro
totype, "_updateCacheNames") |
| 72 .then(function() { |
| 73 if (!cachesTreeElement.childCount()) |
| 74 return resolve(); |
| 75 for (var i = 0; i < cachesTreeElement.childCount(); i++) { |
| 76 var cacheTreeElement = cachesTreeElement.childAt(i); |
| 77 var title = cacheTreeElement.titleText; |
| 78 var elementCacheName = title.substring(0, title.lastIndexOf(
" - ")); |
| 79 if (elementCacheName != cacheName) |
| 80 continue; |
| 81 InspectorTest.addSniffer(WebInspector.ServiceWorkerCacheMode
l.prototype, "_cacheRemoved", resolve) |
| 82 cacheTreeElement._clearCache(); |
| 83 return; |
| 84 } |
| 85 InspectorTest.addResult("Error: Could not find cache to delete."
); |
| 86 reject(); |
| 87 }).catch(reject); |
| 88 }); |
| 89 WebInspector.panels.resources.cacheStorageListTreeElement._refreshCaches(); |
| 90 return promise; |
| 91 } |
| 92 |
| 93 InspectorTest.waitForCacheRefresh = function(callback) |
| 94 { |
| 95 InspectorTest.addSniffer(WebInspector.ServiceWorkerCacheModel.prototype, "_u
pdateCacheNames", callback, false); |
| 96 } |
| 97 |
| 98 InspectorTest.createCache = function(cacheName) |
| 99 { |
| 100 return InspectorTest.invokePageFunctionPromise("createCache", [cacheName]); |
| 101 } |
| 102 |
| 103 InspectorTest.addCacheEntry = function(cacheName, requestUrl, responseText) |
| 104 { |
| 105 return InspectorTest.invokePageFunctionPromise("addCacheEntry", [cacheName,
requestUrl, responseText]); |
| 106 } |
| 107 |
| 108 InspectorTest.deleteCache = function(cacheName) |
| 109 { |
| 110 return InspectorTest.invokePageFunctionPromise("deleteCache", [cacheName]); |
| 111 } |
| 112 |
| 113 InspectorTest.clearAllCaches = function() |
| 114 { |
| 115 return InspectorTest.invokePageFunctionPromise("clearAllCaches", []); |
| 116 } |
| 117 } |
| 118 |
| 119 function onCacheStorageError(reject, e) |
| 120 { |
| 121 console.error("CacheStorage error: " + e); |
| 122 reject(); |
| 123 } |
| 124 |
| 125 function createCache(resolve, reject, cacheName) |
| 126 { |
| 127 caches.open(cacheName).then(resolve).catch(onCacheStorageError.bind(this, re
ject)); |
| 128 } |
| 129 |
| 130 function addCacheEntry(resolve, reject, cacheName, requestUrl, responseText) |
| 131 { |
| 132 caches.open(cacheName) |
| 133 .then(function(cache) { |
| 134 var request = new Request(requestUrl); |
| 135 var myBlob = new Blob(); |
| 136 var init = { "status" : 200 , "statusText" : responseText }; |
| 137 var response = new Response(myBlob, init); |
| 138 return cache.put(request, response); |
| 139 }) |
| 140 .then(resolve) |
| 141 .catch(onCacheStorageError.bind(this, reject)); |
| 142 } |
| 143 |
| 144 function deleteCache(resolve, reject, cacheName) |
| 145 { |
| 146 caches.delete(cacheName) |
| 147 .then(function(success) { |
| 148 if (success) |
| 149 resolve(); |
| 150 else |
| 151 onCacheStorageError(reject, "Could not find cache " + cacheName)
; |
| 152 }).catch(onCacheStorageError.bind(this, reject)); |
| 153 } |
| 154 |
| 155 function clearAllCaches(resolve, reject) |
| 156 { |
| 157 caches.keys() |
| 158 .then(function(keys) { |
| 159 return Promise.all(keys.map(function(key) { |
| 160 return caches.delete(key); |
| 161 })); |
| 162 }) |
| 163 .then(resolve) |
| 164 .catch(onCacheStorageError.bind(this, reject)); |
| 165 } |
OLD | NEW |