summaryrefslogtreecommitdiff
path: root/storage/test/unit/vacuumParticipant.js
blob: 01b980178c50aeccd9925700d966c847957d6a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// This testing component is used in test_vacuum* tests.

const Cc = Components.classes;
const Ci = Components.interfaces;

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");

/**
 * Returns a new nsIFile reference for a profile database.
 * @param filename for the database, excluded the .sqlite extension.
 */
function new_db_file(name)
{
  let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
  file.append(name + ".sqlite");
  return file;
}

/**
 * Opens and returns a connection to the provided database file.
 * @param nsIFile interface to the database file.
 */
function getDatabase(aFile)
{
  return Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService)
                                             .openDatabase(aFile);
}

function vacuumParticipant()
{
  this._dbConn = getDatabase(new_db_file("testVacuum"));
  Services.obs.addObserver(this, "test-options", false);
}

vacuumParticipant.prototype =
{
  classDescription: "vacuumParticipant",
  classID: Components.ID("{52aa0b22-b82f-4e38-992a-c3675a3355d2}"),
  contractID: "@unit.test.com/test-vacuum-participant;1",

  get expectedDatabasePageSize() {
    return this._dbConn.defaultPageSize;
  },
  get databaseConnection() {
    return this._dbConn;
  },

  _grant: true,
  onBeginVacuum: function TVP_onBeginVacuum()
  {
    if (!this._grant) {
      this._grant = true;
      return false;
    }
    Services.obs.notifyObservers(null, "test-begin-vacuum", null);
    return true;
  },
  onEndVacuum: function TVP_EndVacuum(aSucceeded)
  {
    if (this._stmt) {
      this._stmt.finalize();
    }
    Services.obs.notifyObservers(null, "test-end-vacuum", aSucceeded);
  },

  observe: function TVP_observe(aSubject, aTopic, aData)
  {
    if (aData == "opt-out") {
      this._grant = false;
    }
    else if (aData == "wal") {
      try {
        this._dbConn.close();
      } catch (e) {
        // Do nothing.
      }
      this._dbConn = getDatabase(new_db_file("testVacuum2"));
    }
    else if (aData == "wal-fail") {
      try {
        this._dbConn.close();
      } catch (e) {
        // Do nothing.
      }
      this._dbConn = getDatabase(new_db_file("testVacuum3"));
      // Use WAL journal mode.
      this._dbConn.executeSimpleSQL("PRAGMA journal_mode = WAL");
      // Create a not finalized statement.
      this._stmt = this._dbConn.createStatement("SELECT :test");
      this._stmt.params.test = 1;
      this._stmt.executeStep();
    }
    else if (aData == "memory") {
      try {
        this._dbConn.asyncClose();
      } catch (e) {
        // Do nothing.
      }
      this._dbConn = Cc["@mozilla.org/storage/service;1"].
                     getService(Ci.mozIStorageService).
                     openSpecialDatabase("memory");
    }
    else if (aData == "dispose") {
      Services.obs.removeObserver(this, "test-options");
      try {
        this._dbConn.asyncClose();
      } catch (e) {
        // Do nothing.
      }
    }
  },

  QueryInterface: XPCOMUtils.generateQI([
    Ci.mozIStorageVacuumParticipant,
    Ci.nsIObserver,
  ])
};

var gComponentsArray = [vacuumParticipant];
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);