dom/devicestorage/test/devicestorage_common.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /**
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 4 */
michael@0 5
michael@0 6 var oldVal = false;
michael@0 7
michael@0 8 Object.defineProperty(Array.prototype, "remove", {
michael@0 9 enumerable: false,
michael@0 10 configurable: false,
michael@0 11 writable: false,
michael@0 12 value: function(from, to) {
michael@0 13 // Array Remove - By John Resig (MIT Licensed)
michael@0 14 var rest = this.slice((to || from) + 1 || this.length);
michael@0 15 this.length = from < 0 ? this.length + from : from;
michael@0 16 return this.push.apply(this, rest);
michael@0 17 }
michael@0 18 });
michael@0 19
michael@0 20 function devicestorage_setup() {
michael@0 21
michael@0 22 // ensure that the directory we are writing into is empty
michael@0 23 try {
michael@0 24 const Cc = SpecialPowers.Cc;
michael@0 25 const Ci = SpecialPowers.Ci;
michael@0 26 var directoryService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
michael@0 27 var f = directoryService.get("TmpD", Ci.nsIFile);
michael@0 28 f.appendRelativePath("device-storage-testing");
michael@0 29 f.remove(true);
michael@0 30 } catch(e) {}
michael@0 31
michael@0 32 SimpleTest.waitForExplicitFinish();
michael@0 33 if (SpecialPowers.isMainProcess()) {
michael@0 34 try {
michael@0 35 oldVal = SpecialPowers.getBoolPref("device.storage.enabled");
michael@0 36 } catch(e) {}
michael@0 37 SpecialPowers.setBoolPref("device.storage.enabled", true);
michael@0 38 SpecialPowers.setBoolPref("device.storage.testing", true);
michael@0 39 SpecialPowers.setBoolPref("device.storage.prompt.testing", true);
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 function devicestorage_cleanup() {
michael@0 44 if (SpecialPowers.isMainProcess()) {
michael@0 45 SpecialPowers.setBoolPref("device.storage.enabled", oldVal);
michael@0 46 SpecialPowers.setBoolPref("device.storage.testing", false);
michael@0 47 SpecialPowers.setBoolPref("device.storage.prompt.testing", false);
michael@0 48 }
michael@0 49 SimpleTest.finish();
michael@0 50 }
michael@0 51
michael@0 52 function getRandomBuffer() {
michael@0 53 var size = 1024;
michael@0 54 var buffer = new ArrayBuffer(size);
michael@0 55 var view = new Uint8Array(buffer);
michael@0 56 for (var i = 0; i < size; i++) {
michael@0 57 view[i] = parseInt(Math.random() * 255);
michael@0 58 }
michael@0 59 return buffer;
michael@0 60 }
michael@0 61
michael@0 62 function createRandomBlob(mime) {
michael@0 63 return blob = new Blob([getRandomBuffer()], {type: mime});
michael@0 64 }
michael@0 65
michael@0 66 function randomFilename(l) {
michael@0 67 var set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
michael@0 68 var result = "";
michael@0 69 for (var i=0; i<l; i++) {
michael@0 70 var r = Math.floor(set.length * Math.random());
michael@0 71 result += set.substring(r, r + 1);
michael@0 72 }
michael@0 73 return result;
michael@0 74 }
michael@0 75
michael@0 76 function reportErrorAndQuit(e) {
michael@0 77 ok(false, "handleError was called : " + e.target.error.name);
michael@0 78 devicestorage_cleanup();
michael@0 79 }

mercurial