Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | var DEBUG = false; |
michael@0 | 2 | |
michael@0 | 3 | var clientID = "javascript"; |
michael@0 | 4 | var key = "theme:button"; |
michael@0 | 5 | var nsICache = Components.interfaces.nsICache; |
michael@0 | 6 | |
michael@0 | 7 | function getCacheService() |
michael@0 | 8 | { |
michael@0 | 9 | var nsCacheService = Components.classes["@mozilla.org/network/cache-service;1"]; |
michael@0 | 10 | var service = nsCacheService.getService(Components.interfaces.nsICacheService); |
michael@0 | 11 | return service; |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | function createCacheSession(clientID, storagePolicy, streamable) |
michael@0 | 15 | { |
michael@0 | 16 | var service = getCacheService(); |
michael@0 | 17 | var session = service.createSession(clientID, storagePolicy, streamable); |
michael@0 | 18 | return session; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function openCacheEntry(mode) |
michael@0 | 22 | { |
michael@0 | 23 | var session = createCacheSession(clientID, nsICache.STORE_ON_DISK, true); |
michael@0 | 24 | var entry = session.openCacheEntry(key, mode); |
michael@0 | 25 | return entry; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | function wrapInputStream(input) |
michael@0 | 29 | { |
michael@0 | 30 | var nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream; |
michael@0 | 31 | var factory = Components.classes["@mozilla.org/scriptableinputstream;1"]; |
michael@0 | 32 | var wrapper = factory.createInstance(nsIScriptableInputStream); |
michael@0 | 33 | wrapper.init(input); |
michael@0 | 34 | return wrapper; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function test() |
michael@0 | 38 | { |
michael@0 | 39 | var outputEntry = openCacheEntry(nsICache.ACCESS_WRITE); |
michael@0 | 40 | var output = outputEntry.transport.openOutputStream(0, -1, 0); |
michael@0 | 41 | if (output.write("foo", 3) == 3) |
michael@0 | 42 | print("disk cache write works!"); |
michael@0 | 43 | else |
michael@0 | 44 | print("disk cache write broken!"); |
michael@0 | 45 | |
michael@0 | 46 | // store some metadata. |
michael@0 | 47 | outputEntry.setMetaDataElement("size", "3"); |
michael@0 | 48 | |
michael@0 | 49 | output.close(); |
michael@0 | 50 | outputEntry.markValid(); |
michael@0 | 51 | outputEntry.close(); |
michael@0 | 52 | |
michael@0 | 53 | var inputEntry = openCacheEntry(nsICache.ACCESS_READ); |
michael@0 | 54 | var input = wrapInputStream(inputEntry.transport.openInputStream(0, -1, 0)); |
michael@0 | 55 | |
michael@0 | 56 | if (input.read(input.available()) == "foo") |
michael@0 | 57 | print("disk cache read works!"); |
michael@0 | 58 | else |
michael@0 | 59 | print("disk cache read broken!"); |
michael@0 | 60 | |
michael@0 | 61 | if (inputEntry.getMetaDataElement("size") == "3") |
michael@0 | 62 | print("disk cache metadata works!"); |
michael@0 | 63 | else |
michael@0 | 64 | print("disk cache metadata broken!"); |
michael@0 | 65 | |
michael@0 | 66 | input.close(); |
michael@0 | 67 | inputEntry.close(); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function doom() |
michael@0 | 71 | { |
michael@0 | 72 | var doomedEntry = openCacheEntry(nsICache.ACCESS_READ_WRITE); |
michael@0 | 73 | doomedEntry.doom(); |
michael@0 | 74 | doomedEntry.close(); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | if (DEBUG) { |
michael@0 | 78 | getCacheService(); |
michael@0 | 79 | print("cache service loaded."); |
michael@0 | 80 | } else { |
michael@0 | 81 | print("running disk cache test."); |
michael@0 | 82 | test(); |
michael@0 | 83 | print("disk cache test complete."); |
michael@0 | 84 | } |