Permalink
Browse files

Bug 712518 - Improve MockFilePicker.jsm; r=jmaher

  • Loading branch information...
1 parent 80a36e7 commit 3cbff3dcdbadceb9dd627e59ea09e76ee1d56c1f @darktrojan darktrojan committed Dec 22, 2011
@@ -2,7 +2,7 @@
http://creativecommons.org/publicdomain/zero/1.0/ */
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
/**
* TestCase for bug 564387
@@ -54,7 +54,7 @@ function test() {
registerCleanupFunction(function () {
mockTransferRegisterer.unregister();
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
destDir.remove(true);
});
@@ -18,7 +18,7 @@
<script type="text/javascript">
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
MockFilePicker.returnValue = MockFilePicker.returnOK;
function test() {
@@ -47,7 +47,10 @@
is(domActivateEvents, 1, "click on button should fire 1 DOMActivate event");
} finally {
- SimpleTest.executeSoon(SimpleTest.finish);
+ SimpleTest.executeSoon(function() {
+ MockFilePicker.cleanup();
+ SimpleTest.finish();
+ });
}
}
@@ -26,7 +26,7 @@
SimpleTest.waitForExplicitFinish();
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
var testData = [
/* visibility | display | multiple */
@@ -43,6 +43,7 @@
function finished()
{
+ MockFilePicker.cleanup();
SimpleTest.finish();
}
@@ -24,7 +24,7 @@
SimpleTest.waitForExplicitFinish();
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
// enable popups the first time
SpecialPowers.pushPrefEnv({'set': [
@@ -45,7 +45,7 @@
document.getElementById("a").click();
SimpleTest.executeSoon(function() {
ok(!MockFilePicker.shown, "File picker show method should not have been called");
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
SimpleTest.finish();
});
@@ -33,7 +33,7 @@
SimpleTest.waitForExplicitFinish();
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
var testData = [["a", MockFilePicker.filterImages, 1],
["b", MockFilePicker.filterAudio, 1],
@@ -88,7 +88,7 @@
"File picker should show the correct filter index");
if (++currentTest == testData.length) {
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
SimpleTest.finish();
} else {
launchNextTest();
@@ -24,7 +24,7 @@
const Cm = Components.manager;
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
var ioSvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
@@ -134,7 +134,7 @@
dirs[i].remove(true);
}
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
SimpleTest.finish();
}
@@ -1,5 +1,5 @@
/* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
@@ -11,7 +11,7 @@
* for the specific language governing rights and limitations under the
* License.
*
- * The Original Code is Mochitest Reusable Mock File Picker.
+ * The Original Code is Reusable Mock File Picker.
*
* The Initial Developer of the Original Code is
* Geoff Lankow <geoff@darktrojan.net>.
@@ -20,6 +20,18 @@
*
* Contributor(s):
*
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
* ***** END LICENSE BLOCK ***** */
var EXPORTED_SYMBOLS = ["MockFilePicker"];
@@ -30,15 +42,15 @@ const Cm = Components.manager;
const Cu = Components.utils;
const CONTRACT_ID = "@mozilla.org/filepicker;1";
-const CLASS_ID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID();
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
-
-var MockFilePickerFactory = {
+var oldClassID, oldFactory;
+var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID();
+var newFactory = {
createInstance: function(aOuter, aIID) {
if (aOuter)
throw Components.results.NS_ERROR_NO_AGGREGATION;
@@ -66,6 +78,16 @@ var MockFilePicker = {
filterAudio: Ci.nsIFilePicker.filterAudio,
filterVideo: Ci.nsIFilePicker.filterVideo,
+ init: function() {
+ this.reset();
+ if (!registrar.isCIDRegistered(newClassID)) {
+ oldClassID = registrar.contractIDToCID(CONTRACT_ID);
+ oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
+ registrar.unregisterFactory(oldClassID, oldFactory);
+ registrar.registerFactory(newClassID, "", CONTRACT_ID, newFactory);
+ }
+ },
+
reset: function() {
this.appendFilterCallback = null;
this.appendFiltersCallback = null;
@@ -76,8 +98,14 @@ var MockFilePicker = {
this.returnValue = null;
this.showCallback = null;
this.shown = false;
- if (!registrar.isCIDRegistered(CLASS_ID))
- registrar.registerFactory(CLASS_ID, "", CONTRACT_ID, MockFilePickerFactory);
+ },
+
+ cleanup: function() {
+ this.reset();
+ if (oldFactory) {
+ registrar.unregisterFactory(newClassID, newFactory);
+ registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory);
+ }
},
useAnyFile: function() {
@@ -131,8 +159,11 @@ MockFilePickerInstance.prototype = {
show: function() {
MockFilePicker.displayDirectory = this.displayDirectory;
MockFilePicker.shown = true;
- if (typeof MockFilePicker.showCallback == "function")
- MockFilePicker.showCallback(this);
+ if (typeof MockFilePicker.showCallback == "function") {
+ var returnValue = MockFilePicker.showCallback(this);
+ if (typeof returnValue != "undefined")
+ return returnValue;
+ }
return MockFilePicker.returnValue;
}
};
@@ -35,7 +35,7 @@
* ***** END LICENSE BLOCK ***** */
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
/**
* Test for bug 471962 <https://bugzilla.mozilla.org/show_bug.cgi?id=471962>:
@@ -100,7 +100,7 @@ function test() {
registerCleanupFunction(function () {
mockTransferRegisterer.unregister();
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
destDir.remove(true);
});
@@ -57,7 +57,7 @@ let launcher = {
};
Cu.import("resource://test/MockFilePicker.jsm");
-MockFilePicker.reset();
+MockFilePicker.init();
MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK;
function run_test()
@@ -143,5 +143,5 @@ function run_test()
prefsService.clearUserPref("browser.privatebrowsing.keep_current_session");
[dir1, dir2, dir3].forEach(function(dir) dir.remove(true));
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
}
@@ -5,7 +5,7 @@
// Tests bug 567127 - Add install button to the add-ons manager
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
var gManagerWindow;
var gSawInstallNotification = false;
@@ -112,7 +112,7 @@ function test() {
function end_test() {
is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
close_manager(gManagerWindow, function() {
finish();
});
@@ -11,7 +11,7 @@ var gProvider;
const SETTINGS_ROWS = 8;
var MockFilePicker = SpecialPowers.MockFilePicker;
-MockFilePicker.reset();
+MockFilePicker.init();
var observer = {
lastData: null,
@@ -91,7 +91,7 @@ function end_test() {
Services.prefs.clearUserPref("extensions.inlinesettings3.radioString");
Services.prefs.clearUserPref("extensions.inlinesettings3.menulist");
- MockFilePicker.reset();
+ MockFilePicker.cleanup();
close_manager(gManagerWindow, function() {
AddonManager.getAddonByID("inlinesettings1@tests.mozilla.org", function(aAddon) {

0 comments on commit 3cbff3d

Please sign in to comment.