dom/devicestorage/test/devicestorage_common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/devicestorage/test/devicestorage_common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,79 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +var oldVal = false;
    1.10 +  
    1.11 +Object.defineProperty(Array.prototype, "remove", {
    1.12 +  enumerable: false,
    1.13 +  configurable: false,
    1.14 +  writable: false,
    1.15 +  value: function(from, to) {
    1.16 +    // Array Remove - By John Resig (MIT Licensed)
    1.17 +    var rest = this.slice((to || from) + 1 || this.length);
    1.18 +    this.length = from < 0 ? this.length + from : from;
    1.19 +    return this.push.apply(this, rest);
    1.20 +  }
    1.21 +});
    1.22 +
    1.23 +function devicestorage_setup() {
    1.24 +
    1.25 +  // ensure that the directory we are writing into is empty
    1.26 +  try {
    1.27 +    const Cc = SpecialPowers.Cc;
    1.28 +    const Ci = SpecialPowers.Ci;
    1.29 +    var directoryService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
    1.30 +    var f = directoryService.get("TmpD", Ci.nsIFile);
    1.31 +    f.appendRelativePath("device-storage-testing");
    1.32 +    f.remove(true);
    1.33 +  } catch(e) {}
    1.34 +
    1.35 +  SimpleTest.waitForExplicitFinish();
    1.36 +  if (SpecialPowers.isMainProcess()) {
    1.37 +    try {
    1.38 +      oldVal = SpecialPowers.getBoolPref("device.storage.enabled");
    1.39 +    } catch(e) {}
    1.40 +    SpecialPowers.setBoolPref("device.storage.enabled", true);
    1.41 +    SpecialPowers.setBoolPref("device.storage.testing", true);
    1.42 +    SpecialPowers.setBoolPref("device.storage.prompt.testing", true);
    1.43 +  }
    1.44 +}
    1.45 +
    1.46 +function devicestorage_cleanup() {
    1.47 +  if (SpecialPowers.isMainProcess()) {
    1.48 +    SpecialPowers.setBoolPref("device.storage.enabled", oldVal);
    1.49 +    SpecialPowers.setBoolPref("device.storage.testing", false);
    1.50 +    SpecialPowers.setBoolPref("device.storage.prompt.testing", false);
    1.51 +  }
    1.52 +  SimpleTest.finish();
    1.53 +}
    1.54 +
    1.55 +function getRandomBuffer() {
    1.56 +  var size = 1024;
    1.57 +  var buffer = new ArrayBuffer(size);
    1.58 +  var view = new Uint8Array(buffer);
    1.59 +  for (var i = 0; i < size; i++) {
    1.60 +    view[i] = parseInt(Math.random() * 255);
    1.61 +  }
    1.62 +  return buffer;
    1.63 +}
    1.64 +
    1.65 +function createRandomBlob(mime) {
    1.66 +  return blob = new Blob([getRandomBuffer()], {type: mime});
    1.67 +}
    1.68 +
    1.69 +function randomFilename(l) {
    1.70 +  var set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
    1.71 +  var result = "";
    1.72 +  for (var i=0; i<l; i++) {
    1.73 +    var r = Math.floor(set.length * Math.random());
    1.74 +    result += set.substring(r, r + 1);
    1.75 +  }
    1.76 +  return result;
    1.77 +}
    1.78 +
    1.79 +function reportErrorAndQuit(e) {
    1.80 +  ok(false, "handleError was called : " + e.target.error.name);
    1.81 +  devicestorage_cleanup();
    1.82 +}

mercurial