storage/test/unit/vacuumParticipant.js

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:6b132282bd45
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
4
5 // This testing component is used in test_vacuum* tests.
6
7 const Cc = Components.classes;
8 const Ci = Components.interfaces;
9
10 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
11 Components.utils.import("resource://gre/modules/Services.jsm");
12
13 /**
14 * Returns a new nsIFile reference for a profile database.
15 * @param filename for the database, excluded the .sqlite extension.
16 */
17 function new_db_file(name)
18 {
19 let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
20 file.append(name + ".sqlite");
21 return file;
22 }
23
24 /**
25 * Opens and returns a connection to the provided database file.
26 * @param nsIFile interface to the database file.
27 */
28 function getDatabase(aFile)
29 {
30 return Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService)
31 .openDatabase(aFile);
32 }
33
34 function vacuumParticipant()
35 {
36 this._dbConn = getDatabase(new_db_file("testVacuum"));
37 Services.obs.addObserver(this, "test-options", false);
38 }
39
40 vacuumParticipant.prototype =
41 {
42 classDescription: "vacuumParticipant",
43 classID: Components.ID("{52aa0b22-b82f-4e38-992a-c3675a3355d2}"),
44 contractID: "@unit.test.com/test-vacuum-participant;1",
45
46 get expectedDatabasePageSize() this._dbConn.defaultPageSize,
47 get databaseConnection() this._dbConn,
48
49 _grant: true,
50 onBeginVacuum: function TVP_onBeginVacuum()
51 {
52 if (!this._grant) {
53 this._grant = true;
54 return false;
55 }
56 Services.obs.notifyObservers(null, "test-begin-vacuum", null);
57 return true;
58 },
59 onEndVacuum: function TVP_EndVacuum(aSucceeded)
60 {
61 if (this._stmt) {
62 this._stmt.finalize();
63 }
64 Services.obs.notifyObservers(null, "test-end-vacuum", aSucceeded);
65 },
66
67 observe: function TVP_observe(aSubject, aTopic, aData)
68 {
69 if (aData == "opt-out") {
70 this._grant = false;
71 }
72 else if (aData == "wal") {
73 try {
74 this._dbConn.close();
75 }
76 catch(e) {}
77 this._dbConn = getDatabase(new_db_file("testVacuum2"));
78 }
79 else if (aData == "wal-fail") {
80 try {
81 this._dbConn.close();
82 }
83 catch(e) {}
84 this._dbConn = getDatabase(new_db_file("testVacuum3"));
85 // Use WAL journal mode.
86 this._dbConn.executeSimpleSQL("PRAGMA journal_mode = WAL");
87 // Create a not finalized statement.
88 this._stmt = this._dbConn.createStatement("SELECT :test");
89 this._stmt.params.test = 1;
90 this._stmt.executeStep();
91 }
92 else if (aData == "memory") {
93 try {
94 this._dbConn.asyncClose();
95 }
96 catch(e) {}
97 this._dbConn = Cc["@mozilla.org/storage/service;1"].
98 getService(Ci.mozIStorageService).
99 openSpecialDatabase("memory");
100 }
101 else if (aData == "dispose") {
102 Services.obs.removeObserver(this, "test-options");
103 try {
104 this._dbConn.asyncClose();
105 }
106 catch(e) {}
107 }
108 },
109
110 QueryInterface: XPCOMUtils.generateQI([
111 Ci.mozIStorageVacuumParticipant
112 , Ci.nsIObserver
113 ])
114 };
115
116 let gComponentsArray = [vacuumParticipant];
117 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);

mercurial