storage/test/unit/vacuumParticipant.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/unit/vacuumParticipant.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// This testing component is used in test_vacuum* tests.
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +
    1.13 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.14 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +/**
    1.17 + * Returns a new nsIFile reference for a profile database.
    1.18 + * @param filename for the database, excluded the .sqlite extension.
    1.19 + */
    1.20 +function new_db_file(name)
    1.21 +{
    1.22 +  let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
    1.23 +  file.append(name + ".sqlite");
    1.24 +  return file;
    1.25 +}
    1.26 +
    1.27 +/**
    1.28 + * Opens and returns a connection to the provided database file.
    1.29 + * @param nsIFile interface to the database file.
    1.30 + */
    1.31 +function getDatabase(aFile)
    1.32 +{
    1.33 +  return Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService)
    1.34 +                                             .openDatabase(aFile);
    1.35 +}
    1.36 +
    1.37 +function vacuumParticipant()
    1.38 +{
    1.39 +  this._dbConn = getDatabase(new_db_file("testVacuum"));
    1.40 +  Services.obs.addObserver(this, "test-options", false);
    1.41 +}
    1.42 +
    1.43 +vacuumParticipant.prototype =
    1.44 +{
    1.45 +  classDescription: "vacuumParticipant",
    1.46 +  classID: Components.ID("{52aa0b22-b82f-4e38-992a-c3675a3355d2}"),
    1.47 +  contractID: "@unit.test.com/test-vacuum-participant;1",
    1.48 +
    1.49 +  get expectedDatabasePageSize() this._dbConn.defaultPageSize,
    1.50 +  get databaseConnection() this._dbConn,
    1.51 +
    1.52 +  _grant: true,
    1.53 +  onBeginVacuum: function TVP_onBeginVacuum()
    1.54 +  {
    1.55 +    if (!this._grant) {
    1.56 +      this._grant = true;
    1.57 +      return false;
    1.58 +    }
    1.59 +    Services.obs.notifyObservers(null, "test-begin-vacuum", null);
    1.60 +    return true;
    1.61 +  },
    1.62 +  onEndVacuum: function TVP_EndVacuum(aSucceeded)
    1.63 +  {
    1.64 +    if (this._stmt) {
    1.65 +      this._stmt.finalize();
    1.66 +    }
    1.67 +    Services.obs.notifyObservers(null, "test-end-vacuum", aSucceeded);
    1.68 +  },
    1.69 +
    1.70 +  observe: function TVP_observe(aSubject, aTopic, aData)
    1.71 +  {
    1.72 +    if (aData == "opt-out") {
    1.73 +      this._grant = false;
    1.74 +    }
    1.75 +    else if (aData == "wal") {
    1.76 +      try {
    1.77 +        this._dbConn.close();
    1.78 +      }
    1.79 +      catch(e) {}
    1.80 +      this._dbConn = getDatabase(new_db_file("testVacuum2"));
    1.81 +    }
    1.82 +    else if (aData == "wal-fail") {
    1.83 +      try {
    1.84 +        this._dbConn.close();
    1.85 +      }
    1.86 +      catch(e) {}
    1.87 +      this._dbConn = getDatabase(new_db_file("testVacuum3"));
    1.88 +      // Use WAL journal mode.
    1.89 +      this._dbConn.executeSimpleSQL("PRAGMA journal_mode = WAL");
    1.90 +      // Create a not finalized statement.
    1.91 +      this._stmt = this._dbConn.createStatement("SELECT :test");
    1.92 +      this._stmt.params.test = 1;
    1.93 +      this._stmt.executeStep();
    1.94 +    }
    1.95 +    else if (aData == "memory") {
    1.96 +      try {
    1.97 +        this._dbConn.asyncClose();
    1.98 +      }
    1.99 +      catch(e) {}
   1.100 +      this._dbConn = Cc["@mozilla.org/storage/service;1"].
   1.101 +                     getService(Ci.mozIStorageService).
   1.102 +                     openSpecialDatabase("memory");
   1.103 +    }
   1.104 +    else if (aData == "dispose") {
   1.105 +      Services.obs.removeObserver(this, "test-options");
   1.106 +      try {
   1.107 +        this._dbConn.asyncClose();
   1.108 +      }
   1.109 +      catch(e) {}
   1.110 +    }
   1.111 +  },
   1.112 +
   1.113 +  QueryInterface: XPCOMUtils.generateQI([
   1.114 +    Ci.mozIStorageVacuumParticipant
   1.115 +  , Ci.nsIObserver
   1.116 +  ])
   1.117 +};
   1.118 +
   1.119 +let gComponentsArray = [vacuumParticipant];
   1.120 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);

mercurial