27 Components.utils.import(
"resource://gre/modules/XPCOMUtils.jsm");
40 var fsWatcher =
Cc[
"@songbirdnest.com/filesystem/watcher;1"]
41 .createInstance(
Ci.sbIFileSystemWatcher);
42 if (!fsWatcher.isSupported) {
47 var watchDir =
Cc[
"@mozilla.org/file/directory_service;1"]
48 .getService(
Ci.nsIProperties)
49 .get(
"ProfD",
Ci.nsIFile);
52 watchDir.append(
"watch_dir");
53 if (!watchDir.exists() || !watchDir.isDirectory()) {
54 watchDir.create(
Ci.nsIFile.DIRECTORY_TYPE, 0777);
68 this._watchDir = aWatchDir;
69 this._fsWatcher = aFSWatcher;
75 _savedSessionID:
null,
84 _receivedAddedEvent:
false,
85 _receivedChangedEvent:
false,
86 _receivedRemovedEvent:
false,
88 _log:
function(aMessage)
90 dump(
"----------------------------------------------------------\n");
91 dump(
" " + aMessage +
"\n");
92 dump(
"----------------------------------------------------------\n");
97 this._addedFile.remove(
false);
98 this._addedFile =
null;
100 this._removeFile =
null;
102 this._changeFile.remove(
false);
103 this._changeFile =
null;
105 this._watchDir.remove(
true);
106 this._watchDir =
null;
108 this._shutdownTimer =
null;
109 this._restartTimer =
null;
111 this._fsWatcher =
null;
120 this.
_log(
"Starting 'filesystemsession' test");
122 this._shutdownTimer =
Cc[
"@mozilla.org/timer;1"]
123 .createInstance(
Ci.nsITimer);
124 this._restartTimer =
Cc[
"@mozilla.org/timer;1"]
125 .createInstance(
Ci.nsITimer);
128 this._addedFile = this._watchDir.clone();
129 this._addedFile.append(
"added.file");
130 if (this._addedFile.exists()) {
131 this._addedFile.remove(
false);
134 this._changeFile = this._watchDir.clone();
135 this._changeFile.append(
"changed.file");
136 if (!this._changeFile.exists()) {
137 this._changeFile.create(
Ci.nsIFile.NORMAL_FILE_TYPE, 0777);
140 this._removeFile = this._watchDir.clone();
141 this._removeFile.append(
"removed.file");
142 if (!this._removeFile.exists()) {
143 this._removeFile.create(
Ci.nsIFile.NORMAL_FILE_TYPE, 0777);
148 this.
_log(this._state +
": Starting");
150 this._fsWatcher.init(
this, this._watchDir.path,
true);
151 this._fsWatcher.startWatching();
155 onWatcherStarted:
function()
157 switch (this._state) {
160 this.
_log(this._state +
": Watcher has started");
166 this.
_log(this._state +
": Stopping watcher, saving session.");
167 this._savedSessionID = this._fsWatcher.sessionGuid;
168 this._shutdownTimer.initWithCallback(
this,
170 Ci.nsITimerCallback.TYPE_ONE_SHOT);
176 this.
_log(this._state +
": Watcher has started");
187 this.
_log(this._state +
": Starting");
188 this._fsWatcher.stopWatching(
true);
197 this.
_log(this._state +
198 ": ERROR: The session was not reported as an error!");
205 onWatcherStopped:
function()
207 switch (this._state) {
210 this.
_log(this._state +
": Watcher has stopped");
215 this.
_log(this._state +
": Starting");
218 this._addedFile.create(
Ci.nsIFile.NORMAL_FILE_TYPE, 0755);
221 this._removeFile.remove(
false);
224 var foStream =
Cc[
"@mozilla.org/network/file-output-stream;1"]
225 .createInstance(
Ci.nsIFileOutputStream);
226 foStream.init(this._changeFile, -1, -1, 0);
227 var junk =
"garbage garbage garbage";
228 foStream.write(junk, junk.length);
231 this.
_log(this._state +
": File system events created.");
235 this._restartTimer.initWithCallback(
this,
237 Ci.nsITimerCallback.TYPE_ONE_SHOT);
243 this.
_log(this._state +
": Watcher has stopped");
247 this._restartTimer.initWithCallback(
this,
249 Ci.nsITimerCallback.TYPE_ONE_SHOT);
255 onWatcherError:
function(aErrorType, aDescription)
257 switch (aErrorType) {
258 case Ci.sbIFileSystemListener.SESSION_LOAD_ERROR:
260 this.
_log(this._state +
261 ": ERROR: received session load error, unexpected!");
265 this.
_log(
"PHASE 3: Got the session load error.");
269 case Ci.sbIFileSystemListener.ROOT_PATH_MISSING:
270 doFail(
"ERROR: The root watch path is missing!!");
273 case Ci.sbIFileSystemListener.INVALID_DIRECTORY:
274 doFail(
"ERROR: Invalid directory was passed to watcher!");
281 onFileSystemChanged:
function(aFilePath)
283 this.
_log(
"CHANGED: " + aFilePath);
284 this._receivedChangedEvent =
true;
287 onFileSystemRemoved:
function(aFilePath)
289 this.
_log(
"REMOVED: " + aFilePath);
290 this._receivedRemovedEvent =
true;
293 onFileSystemAdded:
function(aFilePath)
295 this.
_log(
"ADDED: " + aFilePath);
296 this._receivedAddedEvent =
true;
302 if (aTimer == this._restartTimer) {
303 this.
_log(this._state +
": Re-starting the watcher with session " +
304 this._savedSessionID);
306 this._fsWatcher =
null;
307 this._fsWatcher =
Cc[
"@songbirdnest.com/filesystem/watcher;1"]
308 .createInstance(
Ci.sbIFileSystemWatcher);
313 this._fsWatcher.deleteSession(this._savedSessionID);
314 this.
_log(this._state +
": Session " + this._savedSessionID +
315 " has been deleted");
319 this._fsWatcher.initWithSession(this._savedSessionID,
this);
320 this._fsWatcher.startWatching();
323 this.
_log(this._state +
": Stopping watcher.");
324 this._fsWatcher.stopWatching(
true);
329 XPCOMUtils.generateQI( [
Ci.sbIFileSystemListener,
Ci.nsITimerCallback] )
function runTest()
Advanced DataRemote unit tests.
sbOSDControlService prototype QueryInterface
function assertTrue(aTest, aMessage)
TimerLoop prototype notify
function _log(aMsg, aMenuID)
function sbFSListener(aWatchDir, aFSWatcher)