From 94b269683ee4363479b9ac5db21f6f5e790a8512 Mon Sep 17 00:00:00 2001 From: Daniel Holbert Date: Mon, 11 Jul 2011 17:49:03 -0700 Subject: [PATCH] Backout f2390732b6a4 (Bug 668154) for apparently turning debug mochitest-oth & mochitest-4 perma-orange --- content/html/content/test/test_bug500885.html | 46 +++- content/html/content/test/test_bug592802.html | 137 ++++++++++- layout/forms/test/bug536567_subframe.html | 64 ++++- layout/forms/test/test_bug36619.html | 70 +++++- layout/forms/test/test_bug377624.html | 218 ++++++++++++++---- layout/forms/test/test_bug536567.html | 28 ++- testing/mochitest/MockFilePicker.jsm | 136 ----------- testing/mochitest/jar.mn | 2 - .../specialpowers/content/specialpowers.js | 13 +- .../browser/browser_save_resend_postdata.js | 10 +- .../content/tests/browser/common/Makefile.in | 1 + .../content/tests/browser/common/_loadAll.js | 1 + .../tests/browser/common/mockFilePicker.js | 116 ++++++++++ .../tests/browser/common/toolkitFunctions.js | 12 +- toolkit/mozapps/downloads/tests/Makefile.in | 3 - .../test_privatebrowsing_downloadLastDir.js | 112 ++++++++- .../test/browser/browser_bug567127.js | 45 +++- .../test/browser/browser_inlinesettings.js | 88 +++++-- 18 files changed, 818 insertions(+), 284 deletions(-) delete mode 100644 testing/mochitest/MockFilePicker.jsm create mode 100644 toolkit/content/tests/browser/common/mockFilePicker.js diff --git a/content/html/content/test/test_bug500885.html b/content/html/content/test/test_bug500885.html index 5ff60f27f2f0..1b76f25dadf6 100644 --- a/content/html/content/test/test_bug500885.html +++ b/content/html/content/test/test_bug500885.html @@ -8,6 +8,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=500885 + @@ -18,13 +19,41 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=500885 + @@ -26,8 +27,125 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=592802 SimpleTest.waitForExplicitFinish(); -var MockFilePicker = SpecialPowers.MockFilePicker; -MockFilePicker.reset(); +netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +function simpleEnumerator(items) +{ + this._items = items; + this._nextIndex = 0; +} + +simpleEnumerator.prototype = { + QueryInterface: function(aIID) + { + if (Ci.nsISimpleEnumerator.equals(aIID) || + Ci.nsISupports.equals(aIID)) { + return this; + } + + throw Cr.NS_ERROR_NO_INTERFACE; + }, + + hasMoreElements: function() + { + return this._nextIndex < this._items.length; + }, + + getNext: function() + { + if (!this.hasMoreElements()) { + throw Cr.NS_ERROR_FAILURE; + } + + return this._items[this._nextIndex++]; + } +}; + +function MockFilePicker() +{ +} + +MockFilePicker.prototype = { + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), + + // Constants + returnOK: 0, // the user hits OK (select a file) + returnCancel: 1, // the user cancel the file selection + returnReplace: 2, // the user replace the selection + + // Properties + defaultExtension: "", + defaultString: "", + get displayDirectory() { return null; }, + set displayDirectory(val) { }, + get fileURL() { return null; }, + filterIndex: 0, + + get file() { + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + + var fileName = "592808_file"; + var fileData = "file content"; + + var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"] + .getService(Components.interfaces.nsIProperties); + var testFile = dirSvc.get("ProfD", Components.interfaces.nsIFile); + testFile.append(fileName); + var outStream = Components. + classes["@mozilla.org/network/file-output-stream;1"]. + createInstance(Components.interfaces.nsIFileOutputStream); + outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate + 0666, 0); + outStream.write(fileData, fileData.length); + outStream.close(); + + return testFile; + }, + + get files() { + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + + var fileName = "592808_file"; + var fileData = "file content"; + + var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"] + .getService(Components.interfaces.nsIProperties); + var testFile = dirSvc.get("ProfD", Components.interfaces.nsIFile); + testFile.append(fileName); + var outStream = Components. + classes["@mozilla.org/network/file-output-stream;1"]. + createInstance(Components.interfaces.nsIFileOutputStream); + outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate + 0666, 0); + outStream.write(fileData, fileData.length); + outStream.close(); + + return new simpleEnumerator([testFile]); + }, + + appendFilter: function(val) {}, + + appendFilters: function(val) {}, + + init: function() {}, + + show: function() + { + if (firstFilePickerShow) { + firstFilePickerShow = false; + return this.returnCancel; + } else { + return this.returnOK; + } + } +}; + +var mockFilePickerRegisterer = + new MockObjectRegisterer("@mozilla.org/filepicker;1",MockFilePicker); + +mockFilePickerRegisterer.register(); var testData = [ /* visibility | display | multiple */ @@ -42,27 +160,26 @@ var testData = [ var testCounter = 0; var testNb = testData.length; +var firstFilePickerShow = true; + function finished() { + mockFilePickerRegisterer.unregister(); + SimpleTest.finish(); } SimpleTest.waitForFocus(function() { // mockFilePicker will simulate a cancel for the first time the file picker will be shown. - MockFilePicker.returnValue = MockFilePicker.returnCancel; - var b2 = document.getElementById('b2'); b2.focus(); // Be sure the element is visible. document.getElementById('b2').addEventListener("change", function(aEvent) { aEvent.target.removeEventListener("change", arguments.callee, false); ok(false, "When cancel is received, change should not fire"); }, false); - b2.click(); + synthesizeMouse(b2, 2, 2, {}); // Now, we can launch tests when file picker isn't canceled. - MockFilePicker.useAnyFile(); - MockFilePicker.returnValue = MockFilePicker.returnOK; - var b = document.getElementById('b'); b.focus(); // Be sure the element is visible. @@ -83,12 +200,12 @@ SimpleTest.waitForFocus(function() { a.multiple = data[2]; SimpleTest.executeSoon(function() { - b.click(); + synthesizeMouse(b, 2, 2, {}); }); } }, false); - b.click(); + synthesizeMouse(b, 2, 2, {}); }); diff --git a/layout/forms/test/bug536567_subframe.html b/layout/forms/test/bug536567_subframe.html index c1271becf064..13348fa03bb4 100644 --- a/layout/forms/test/bug536567_subframe.html +++ b/layout/forms/test/bug536567_subframe.html @@ -1,12 +1,72 @@ + + + + diff --git a/layout/forms/test/test_bug36619.html b/layout/forms/test/test_bug36619.html index 9f675c912fde..4caed05bb8cb 100644 --- a/layout/forms/test/test_bug36619.html +++ b/layout/forms/test/test_bug36619.html @@ -8,6 +8,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=36619 + @@ -24,29 +25,76 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=36619 SimpleTest.waitForExplicitFinish(); -var MockFilePicker = SpecialPowers.MockFilePicker; -MockFilePicker.reset(); +netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +function MockFilePicker() +{ +} + +MockFilePicker.prototype = { + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), + + // properties + defaultExtension: "", + defaultString: "", + get displayDirectory() { return null; }, + set displayDirectory(val) { }, + file: null, + get files() { return null; }, + get fileURL() { return null; }, + filterIndex: 0, + + // methods + appendFilter: function(val) + { + }, + + appendFilters: function(val) + { + }, + + init: function() { + }, + + show: function() + { + shown = true; + } +}; + +var mockFilePickerRegisterer = + new MockObjectRegisterer("@mozilla.org/filepicker;1",MockFilePicker); + +mockFilePickerRegisterer.register(); // enable popups the first time -var oldAllow = SpecialPowers.getBoolPref("dom.disable_open_during_load"); -var oldShowBar = SpecialPowers.getBoolPref("privacy.popups.showBrowserMessage"); -SpecialPowers.setBoolPref("dom.disable_open_during_load", true); -SpecialPowers.setBoolPref("privacy.popups.showBrowserMessage", false); +var pbi = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2); +var oldAllow = pbi.getBoolPref("dom.disable_open_during_load"); +var oldShowBar = pbi.getBoolPref("privacy.popups.showBrowserMessage"); +pbi.setBoolPref("dom.disable_open_during_load", true); +pbi.setBoolPref("privacy.popups.showBrowserMessage", false); + +var shown = false; function checkFirst() { - ok(MockFilePicker.shown, "File picker show method should have been called"); + netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + ok(shown, "File picker show method should have been called"); + shown = false; document.getElementById("a").click(); setTimeout(finishTest, 1000); } function finishTest() { - MockFilePicker.reset(); - ok(!MockFilePicker.shown, "File picker show method should not have been called"); + netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + ok(!shown, "File picker show method should not have been called"); - SpecialPowers.setBoolPref("dom.disable_open_during_load", oldAllow); - SpecialPowers.setBoolPref("privacy.popups.showBrowserMessage", oldShowBar); + mockFilePickerRegisterer.unregister(); + + pbi.setBoolPref("dom.disable_open_during_load", oldAllow); + pbi.setBoolPref("privacy.popups.showBrowserMessage", oldShowBar); SimpleTest.finish(); } diff --git a/layout/forms/test/test_bug377624.html b/layout/forms/test/test_bug377624.html index 17f36a650562..b8efaab430ac 100644 --- a/layout/forms/test/test_bug377624.html +++ b/layout/forms/test/test_bug377624.html @@ -33,78 +33,198 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=377624 SimpleTest.waitForExplicitFinish(); -var MockFilePicker = SpecialPowers.MockFilePicker; -MockFilePicker.reset(); +netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + +const Cc = Components.classes; +const Ci = Components.interfaces; +const Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); +const Cu = Components.utils; + +const FILE_PICKER_CID = "@mozilla.org/filepicker;1"; +const FILE_PICKER_ID = Components.ID(Cc[FILE_PICKER_CID].number); +const CUSTOM_FILE_PICKER_ID = Components.ID("d63dee33-fb6d-4547-a8d1-c12197655cce"); +const FILE_PICKER_DESCRIPTION = "File Picker Test Service"; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); // disable popups to make sure that the popup blocker does not interfere // with manually opened file pickers. -var oldAllow = SpecialPowers.getBoolPref("dom.disable_open_during_load"); -SpecialPowers.setBoolPref("dom.disable_open_during_load", false); +var pbi = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2); +var oldAllow = pbi.getBoolPref("dom.disable_open_during_load"); +pbi.setBoolPref("dom.disable_open_during_load", true); -var testData = [["a", MockFilePicker.filterImages, 1], - ["b", MockFilePicker.filterAudio, 1], - ["c", MockFilePicker.filterVideo, 1], +function FilePickerService() +{ +} + +FilePickerService.prototype = { + _obs: Cc["@mozilla.org/observer-service;1"]. + getService(Ci.nsIObserverService), + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), + + // constants + modeOpen: 0, + modeSave: 1, + modeGetFolder: 2, + modeOpenMultiple: 3, + returnOK: 0, + returnCancel: 1, + returnReplace: 2, + filterAll: 1, + filterHTML: 2, + filterText: 4, + filterImages: 8, + filterXML: 16, + filterXUL: 32, + filterApps: 64, + filterAllowURLs: 128, + filterAudio: 256, + filterVideo: 512, + + // properties + defaultExtension: "", + defaultString: "", + get displayDirectory() { return null; }, + set displayDirectory(val) { }, + file: null, + get files() { return null; }, + get fileURL() { return null; }, + filterIndex: 0, + + // methods + appendFilter: function(val) + { + this._obs.notifyObservers(null, "TEST_FILEPICKER_APPENDFILTER", val); + }, + + appendFilters: function(val) + { + netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + this._obs.notifyObservers(null, "TEST_FILEPICKER_APPENDFILTERS", val); + }, + + init: function() {}, + + show: function() + { + netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + this._obs.notifyObservers(null, "TEST_FILEPICKER_SHOW", this.filterIndex); + return this.returnOK; + } +}; + +factory = { + createInstance: function(aOuter, aIid) { + if (aOuter != null) + throw Cr.NS_ERROR_NO_AGGREGATION; + return new FilePickerService().QueryInterface(aIid); + } +}; + +var testData = [["a", FilePickerService.prototype.filterImages, 1], + ["b", FilePickerService.prototype.filterAudio, 1], + ["c", FilePickerService.prototype.filterVideo, 1], ["d", 0, 0], ["e", 0, 0], ["f", 0, 0], ["g", 0, 0], - ["h", MockFilePicker.filterImages, 1], - ["i", MockFilePicker.filterVideo, 1], - ["j", MockFilePicker.filterAudio, 1], + ["h", FilePickerService.prototype.filterImages, 1], + ["i", FilePickerService.prototype.filterVideo, 1], + ["j", FilePickerService.prototype.filterAudio, 1], ["z", 0, 0]]; +testData.forEach(function (datum) { +document.getElementById(datum[0]).addEventListener("focus", function (aEvent) { + aEvent.target.removeEventListener("focus", arguments.callee, false); + synthesizeKey('VK_SPACE', {}); + }, false); +}); + var currentTest = 0; -var appendFilterCalled; -var filters; -var filterIndex; -function launchNextTest() +function launchNextTest(aObserver) { - MockFilePicker.shown = false; - appendFilterCalled = false; - filters = []; - filterIndex = 0; + aObserver.shown = false; + aObserver.appendFilterCalled = false; + aObserver.filters = []; + aObserver.filterIndex = 0; - document.getElementById(testData[currentTest][0]).click(); + document.getElementById(testData[currentTest][0]).focus(); } function runTests() { - MockFilePicker.appendFilterCallback = function(filepicker, title, val) - { - this.appendFilterCalled = true; - }; - MockFilePicker.appendFiltersCallback = function(filepicker, val) - { - filters.push(val); - }; - MockFilePicker.showCallback = function(filepicker) - { - filterIndex = filepicker.filterIndex; + netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); - SimpleTest.executeSoon(function () { - ok(MockFilePicker.shown, - "File picker show method should have been called"); - ok(!appendFilterCalled, - "appendFilter should not have been called"); - is(filters.length, 1, - "appendFilters should have been called once"); - is(filters[0], MockFilePicker.filterAll + - testData[currentTest][1], - "Correct filters should have been added"); - is(filterIndex, testData[currentTest][2], - "File picker should show the correct filter index"); + Cm.registerFactory(CUSTOM_FILE_PICKER_ID, + FILE_PICKER_DESCRIPTION, + FILE_PICKER_CID, + factory); - if (++currentTest == testData.length) { - SpecialPowers.setBoolPref("dom.disable_open_during_load", oldAllow); - SimpleTest.finish(); - } else { - launchNextTest(); + var obs = Cc["@mozilla.org/observer-service;1"]. + getService(Ci.nsIObserverService); + + var observer = { + observe: function(aSubject, aTopic, aData) { + switch (aTopic) { + case "TEST_FILEPICKER_APPENDFILTER": + this.appendFilterCalled = true; + break; + case "TEST_FILEPICKER_APPENDFILTERS": + this.filters.push(aData); + break; + case "TEST_FILEPICKER_SHOW": + this.shown = true; + this.filterIndex = aData; + + SimpleTest.executeSoon(function () { + netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + + ok(observer.shown, + "File picker show method should have been called"); + ok(!observer.appendFilterCalled, + "appendFilter should not have been called"); + is(observer.filters.length, 1, + "appendFilters should have been called once"); + is(observer.filters[0], FilePickerService.prototype.filterAll + + testData[currentTest][1], + "Correct filters should have been added"); + is(observer.filterIndex, testData[currentTest][2], + "File picker should show the correct filter index"); + + if (++currentTest == testData.length) { + obs.removeObserver(observer, "TEST_FILEPICKER_APPENDFILTER", false); + obs.removeObserver(observer, "TEST_FILEPICKER_APPENDFILTERS", false); + obs.removeObserver(observer, "TEST_FILEPICKER_SHOW", false); + Cm.unregisterFactory(CUSTOM_FILE_PICKER_ID, factory); + + Cm.registerFactory(FILE_PICKER_ID, + "File Picker Service", + FILE_PICKER_CID, + null); + + pbi.setBoolPref("dom.disable_open_during_load", oldAllow); + SimpleTest.finish(); + } else { + launchNextTest(observer); + } + } ); + break; } - } ); + }, + shown: false, + appendFilterCalled: false, + filters: [], + filterIndex: 0 }; - launchNextTest(); + obs.addObserver(observer, "TEST_FILEPICKER_APPENDFILTER", false); + obs.addObserver(observer, "TEST_FILEPICKER_APPENDFILTERS", false); + obs.addObserver(observer, "TEST_FILEPICKER_SHOW", false); + + // We are simulating a focus then a SPACE key press to open the file picker. + // We were not able to do this with |synthesizeMouse|. + launchNextTest(observer); } diff --git a/layout/forms/test/test_bug536567.html b/layout/forms/test/test_bug536567.html index 8f0c04e15052..16bfd49673bb 100644 --- a/layout/forms/test/test_bug536567.html +++ b/layout/forms/test/test_bug536567.html @@ -8,7 +8,6 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=536567 - Mozilla Bug 536567 @@ -19,14 +18,12 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=536567 /** Test for Bug 536567 **/ +netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); const Cc = Components.classes; const Ci = Components.interfaces; const Cu = Components.utils; const Cm = Components.manager; -Cu.import("resource://mochikit/MockFilePicker.jsm"); -MockFilePicker.reset(); - var ioSvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); var prefSvc = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); @@ -40,6 +37,8 @@ var homeDir = dirSvc.get("Desk", Ci.nsILocalFile); prefSvc.setBoolPref("browser.privatebrowsing.keep_current_session", true); function newDir() { + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + var dir = tmpDir.clone(); dir.append("testdir" + Math.floor(Math.random() * 10000)); dir.QueryInterface(Ci.nsILocalFile); @@ -97,6 +96,7 @@ var tests = [ "pb off" ]; +var index; var testIndex = 0; var content = document.getElementById('content'); @@ -122,9 +122,7 @@ function runTest() { testIndex++; runTest(); } else { - var file = dirs[test[2]].clone(); - file.append("file.file"); - MockFilePicker.returnFiles = [file]; + index = test[2]; content.setAttribute ('src', domains[test[0]] + '/tests/layout/forms/test/bug536567_subframe.html'); } } @@ -138,11 +136,13 @@ function endTest() { SimpleTest.finish(); } -MockFilePicker.showCallback = function(filepicker) { +window.addEventListener("message", function(event) { + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + var test = tests[testIndex]; var returned = -1; for (var i = 0; i < dirs.length; i++) { - if (dirs[i].path == MockFilePicker.displayDirectory.path) { + if (dirs[i].path == event.data) { returned = i; break; } @@ -152,12 +152,10 @@ MockFilePicker.showCallback = function(filepicker) { } else { is(returned, test[1], 'test ' + testIndex); } - - SimpleTest.executeSoon(function() { - testIndex++; - runTest(); - }); -}; + + testIndex++; + runTest(); +}, false); window.onload = function() { SimpleTest.waitForExplicitFinish(); diff --git a/testing/mochitest/MockFilePicker.jsm b/testing/mochitest/MockFilePicker.jsm deleted file mode 100644 index 8d8dbae3e386..000000000000 --- a/testing/mochitest/MockFilePicker.jsm +++ /dev/null @@ -1,136 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.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 - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Mochitest Reusable Mock File Picker. - * - * The Initial Developer of the Original Code is - * Geoff Lankow . - * Portions created by the Initial Developer are Copyright (C) 2011 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * ***** END LICENSE BLOCK ***** */ - -var EXPORTED_SYMBOLS = ["MockFilePicker"]; - -const Cc = Components.classes; -const Ci = Components.interfaces; -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 = { - createInstance: function(aOuter, aIID) { - if (aOuter) - throw Components.results.NS_ERROR_NO_AGGREGATION; - return new MockFilePickerInstance().QueryInterface(aIID); - }, - lockFactory: function(aLock) { - throw Components.results.NS_ERROR_NOT_IMPLEMENTED; - }, - QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) -}; - -var MockFilePicker = { - returnOK: Ci.nsIFilePicker.returnOK, - returnCancel: Ci.nsIFilePicker.returnCancel, - returnReplace: Ci.nsIFilePicker.returnReplace, - - filterAll: Ci.nsIFilePicker.filterAll, - filterHTML: Ci.nsIFilePicker.filterHTML, - filterText: Ci.nsIFilePicker.filterText, - filterImages: Ci.nsIFilePicker.filterImages, - filterXML: Ci.nsIFilePicker.filterXML, - filterXUL: Ci.nsIFilePicker.filterXUL, - filterApps: Ci.nsIFilePicker.filterApps, - filterAllowURLs: Ci.nsIFilePicker.filterAllowURLs, - filterAudio: Ci.nsIFilePicker.filterAudio, - filterVideo: Ci.nsIFilePicker.filterVideo, - - reset: function() { - this.appendFilterCallback = null; - this.appendFiltersCallback = null; - this.displayDirectory = null; - this.mode = null; - this.returnFiles = []; - this.returnValue = null; - this.showCallback = null; - this.shown = false; - if (!registrar.isCIDRegistered(CLASS_ID)) - registrar.registerFactory(CLASS_ID, "", CONTRACT_ID, MockFilePickerFactory); - }, - - useAnyFile: function() { - var file = FileUtils.getFile("TmpD", ["testfile"]); - file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0644); - this.returnFiles = [file]; - } -}; - -function MockFilePickerInstance() { }; -MockFilePickerInstance.prototype = { - QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), - init: function(aParent, aTitle, aMode) { - MockFilePicker.mode = aMode; - }, - appendFilter: function(aTitle, aFilter) { - if (typeof MockFilePicker.appendFilterCallback == "function") - MockFilePicker.appendFilterCallback(this, aTitle, aFilter); - }, - appendFilters: function(aFilterMask) { - if (typeof MockFilePicker.appendFiltersCallback == "function") - MockFilePicker.appendFiltersCallback(this, aFilterMask); - }, - defaultString: "", - defaultExtension: "", - filterIndex: 0, - displayDirectory: null, - get file() { - if (MockFilePicker.returnFiles.length >= 1) - return MockFilePicker.returnFiles[0]; - return null; - }, - get fileURL() { - if (MockFilePicker.returnFiles.length >= 1) - return Services.io.newFileURI(MockFilePicker.returnFiles[0]); - return null; - }, - get files() { - return { - index: 0, - QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]), - hasMoreElements: function() { - return this.index < MockFilePicker.returnFiles.length; - }, - getNext: function() { - return MockFilePicker.returnFiles[this.index++]; - } - }; - }, - show: function() { - MockFilePicker.displayDirectory = this.displayDirectory; - MockFilePicker.shown = true; - if (typeof MockFilePicker.showCallback == "function") - MockFilePicker.showCallback(this); - return MockFilePicker.returnValue; - } -}; diff --git a/testing/mochitest/jar.mn b/testing/mochitest/jar.mn index ae7414f8d930..933a56792d87 100644 --- a/testing/mochitest/jar.mn +++ b/testing/mochitest/jar.mn @@ -28,5 +28,3 @@ mochikit.jar: content/MochiKit/packed.js (MochiKit/packed.js) -% resource mochikit %modules/ - modules/MockFilePicker.jsm (MockFilePicker.jsm) diff --git a/testing/mochitest/specialpowers/content/specialpowers.js b/testing/mochitest/specialpowers/content/specialpowers.js index 21ad6da26342..5baaa0806f67 100644 --- a/testing/mochitest/specialpowers/content/specialpowers.js +++ b/testing/mochitest/specialpowers/content/specialpowers.js @@ -38,13 +38,8 @@ * order to be used as a replacement for UniversalXPConnect */ -var Cc = Components.classes; var Ci = Components.interfaces; -var Cu = Components.utils; - -var MockFilePicker; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +var Cc = Components.classes; function SpecialPowers(window) { this.window = window; @@ -316,12 +311,6 @@ SpecialPowers.prototype = { Components.classes["@mozilla.org/eventlistenerservice;1"]. getService(Components.interfaces.nsIEventListenerService). removeSystemEventListener(target, type, listener, useCapture); - }, - - get MockFilePicker() { - if (!MockFilePicker) - Cu.import("resource://mochikit/MockFilePicker.jsm"); - return MockFilePicker; } }; diff --git a/toolkit/content/tests/browser/browser_save_resend_postdata.js b/toolkit/content/tests/browser/browser_save_resend_postdata.js index 22d19279008d..a14a7fe3fcb1 100644 --- a/toolkit/content/tests/browser/browser_save_resend_postdata.js +++ b/toolkit/content/tests/browser/browser_save_resend_postdata.js @@ -34,9 +34,6 @@ * * ***** END LICENSE BLOCK ***** */ -Components.utils.import("resource://mochikit/MockFilePicker.jsm"); -MockFilePicker.reset(); - /** * Test for bug 471962 : * When saving an inner frame as file only, the POST data of the outer page is @@ -113,11 +110,10 @@ function test() { // Create the folder the page will be saved into. var destDir = createTemporarySaveDirectory(); - var file = destDir.clone(); - file.append("no_default_file_name"); - MockFilePicker.returnFiles = [file]; try { // Call the appropriate save function defined in contentAreaUtils.js. + mockFilePickerSettings.destDir = destDir; + mockFilePickerSettings.filterIndex = 1; // kSaveAsType_URL callSaveWithMockObjects(function() { var docToSave = innerFrame.contentDocument; // We call internalSave instead of saveDocument to bypass the history @@ -134,7 +130,7 @@ function test() { throw "Unexpected failure, the inner frame couldn't be saved!"; // Read the entire saved file. - var fileContents = readShortFile(file); + var fileContents = readShortFile(mockFilePickerResults.selectedFile); // Check if outer POST data is found (bug 471962). ok(fileContents.indexOf("inputfield=outer") === -1, diff --git a/toolkit/content/tests/browser/common/Makefile.in b/toolkit/content/tests/browser/common/Makefile.in index d935925dca45..b026931f32f4 100644 --- a/toolkit/content/tests/browser/common/Makefile.in +++ b/toolkit/content/tests/browser/common/Makefile.in @@ -47,6 +47,7 @@ include $(topsrcdir)/config/rules.mk # If you add files here, add them to "_loadAll.js" too. _COMMON_FILES = \ _loadAll.js \ + mockFilePicker.js \ mockObjects.js \ mockTransferForContinuing.js \ testRunner.js \ diff --git a/toolkit/content/tests/browser/common/_loadAll.js b/toolkit/content/tests/browser/common/_loadAll.js index e00e5a9fa9fd..91bf6d459887 100644 --- a/toolkit/content/tests/browser/common/_loadAll.js +++ b/toolkit/content/tests/browser/common/_loadAll.js @@ -56,6 +56,7 @@ void(function (scriptScope) { "testRunner.js", // To be included after the files above. + "mockFilePicker.js", "mockTransferForContinuing.js", "toolkitFunctions.js", ]; diff --git a/toolkit/content/tests/browser/common/mockFilePicker.js b/toolkit/content/tests/browser/common/mockFilePicker.js new file mode 100644 index 000000000000..8c0a0006fc79 --- /dev/null +++ b/toolkit/content/tests/browser/common/mockFilePicker.js @@ -0,0 +1,116 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * 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 + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla XUL Toolkit Testing Code. + * + * The Initial Developer of the Original Code is + * Paolo Amadini . + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * 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 ***** */ + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +var mockFilePickerSettings = { + /** + * File object pointing to the directory where the files will be saved. + * The files will be saved with the default name, and will be overwritten + * if they exist. + */ + destDir: null, + + /** + * Index of the filter to be returned by the file picker, or -1 to return + * the filter proposed by the caller. + */ + filterIndex: -1 +}; + +var mockFilePickerResults = { + /** + * File object corresponding to the last automatically selected file. + */ + selectedFile: null, + + /** + * Index of the filter that was set on the file picker by the caller. + */ + proposedFilterIndex: -1 +}; + +/** + * This file picker implementation uses the global settings defined in + * mockFilePickerSettings, and updates the mockFilePickerResults object + * when its "show" method is called. + */ +function MockFilePicker() { }; +MockFilePicker.prototype = { + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), + init: function(aParent, aTitle, aMode) { }, + appendFilters: function(aFilterMask) { }, + appendFilter: function(aTitle, aFilter) { }, + defaultString: "", + defaultExtension: "", + filterIndex: 0, + displayDirectory: null, + file: null, + get fileURL() { + return Cc["@mozilla.org/network/io-service;1"]. + getService(Ci.nsIIOService).newFileURI(this.file); + }, + get files() { + throw Cr.NS_ERROR_NOT_IMPLEMENTED; + }, + show: function MFP_show() { + // Select the destination file with the specified default file name. If the + // default file name was never specified or was set to an empty string by + // the caller, ensure that a fallback file name is used. + this.file = mockFilePickerSettings.destDir.clone(); + this.file.append(this.defaultString || "no_default_file_name"); + // Store the current file picker settings for testing them later. + mockFilePickerResults.selectedFile = this.file.clone(); + mockFilePickerResults.proposedFilterIndex = this.filterIndex; + // Select a different file filter if required. + if (mockFilePickerSettings.filterIndex != -1) + this.filterIndex = mockFilePickerSettings.filterIndex; + // Assume we overwrite the file if it exists. + return (this.file.exists() ? + Ci.nsIFilePicker.returnReplace : + Ci.nsIFilePicker.returnOK); + } +}; + +// Create an instance of a MockObjectRegisterer whose methods can be used to +// temporarily replace the default "@mozilla.org/filepicker;1" object factory +// with one that provides the mock implementation above. To activate the mock +// object factory, call the "register" method. Starting from that moment, all +// the file picker objects that are requested will be mock objects, until the +// "unregister" method is called. +var mockFilePickerRegisterer = + new MockObjectRegisterer("@mozilla.org/filepicker;1", + MockFilePicker); diff --git a/toolkit/content/tests/browser/common/toolkitFunctions.js b/toolkit/content/tests/browser/common/toolkitFunctions.js index a8c4755abe4a..196be3c5abb7 100644 --- a/toolkit/content/tests/browser/common/toolkitFunctions.js +++ b/toolkit/content/tests/browser/common/toolkitFunctions.js @@ -64,12 +64,18 @@ function callSaveWithMockObjects(aSaveFunction) { // ensure that, even in case of exceptions during the function's execution, // the mock object factories are unregistered before proceeding with the other // tests in the suite. - mockTransferForContinuingRegisterer.register(); + mockFilePickerRegisterer.register(); try { - aSaveFunction(); + mockTransferForContinuingRegisterer.register(); + try { + aSaveFunction(); + } + finally { + mockTransferForContinuingRegisterer.unregister(); + } } finally { - mockTransferForContinuingRegisterer.unregister(); + mockFilePickerRegisterer.unregister(); } } diff --git a/toolkit/mozapps/downloads/tests/Makefile.in b/toolkit/mozapps/downloads/tests/Makefile.in index 8e8ab4129693..f57fbf1bfd8e 100644 --- a/toolkit/mozapps/downloads/tests/Makefile.in +++ b/toolkit/mozapps/downloads/tests/Makefile.in @@ -54,6 +54,3 @@ XPCSHELL_TESTS = \ DIRS += chrome include $(topsrcdir)/config/rules.mk - -libs:: - $(INSTALL) $(topsrcdir)/testing/mochitest/MockFilePicker.jsm $(DEPTH)/_tests/xpcshell/$(relativesrcdir)/unit diff --git a/toolkit/mozapps/downloads/tests/unit/test_privatebrowsing_downloadLastDir.js b/toolkit/mozapps/downloads/tests/unit/test_privatebrowsing_downloadLastDir.js index 060116bb585e..980c5d17bcdf 100644 --- a/toolkit/mozapps/downloads/tests/unit/test_privatebrowsing_downloadLastDir.js +++ b/toolkit/mozapps/downloads/tests/unit/test_privatebrowsing_downloadLastDir.js @@ -40,6 +40,11 @@ const Ci = Components.interfaces; const Cc = Components.classes; const Cu = Components.utils; const Cr = Components.results; +const Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); + +const FILE_PICKER_CID = "@mozilla.org/filepicker;1"; +const FILE_PICKER_ID = Components.ID("fa71ce55-6524-4744-ba75-71a4c126cfa3"); +const FILE_PICKER_DESCRIPTION = "File Picker Test Service"; // Code borrowed from toolkit/components/downloadmgr/test/unit/head_download_manager.js var dirSvc = Cc["@mozilla.org/file/directory_service;1"]. @@ -77,14 +82,70 @@ if (!profileDir) { Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/DownloadLastDir.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + let context = { QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor]), getInterface: XPCOMUtils.generateQI([Ci.nsIDOMWindowInternal]) }; -Cu.import("resource://test/MockFilePicker.jsm"); -MockFilePicker.reset(); -MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK; +function FilePickerService() { +} + +FilePickerService.prototype = { + _obs: Cc["@mozilla.org/observer-service;1"]. + getService(Ci.nsIObserverService), + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), + + // constants + modeOpen: 0, + modeSave: 1, + modeGetFolder: 2, + modeOpenMultiple: 3, + returnOK: 0, + returnCancel: 1, + returnReplace: 2, + filterAll: 1, + filterHTML: 2, + filterText: 4, + filterImages: 8, + filterXML: 16, + filterXUL: 32, + filterApps: 64, + + // properties + defaultExtension: "", + defaultString: "", + get displayDirectory() { return null; }, + set displayDirectory(val) { + this._obs.notifyObservers(val, "TEST_FILEPICKER_SETDISPLAYDIRECTORY", ""); + }, + file: null, + get files() { return null; }, + get fileURL() { return null; }, + filterIndex: 0, + + // methods + appendFilter: function() {}, + appendFilters: function() {}, + init: function() { + var fileptr = Cc["@mozilla.org/supports-interface-pointer;1"]. + createInstance(Ci.nsISupportsInterfacePointer); + this._obs.notifyObservers(fileptr, "TEST_FILEPICKER_GETFILE", ""); + this.file = fileptr.data.QueryInterface(fileptr.dataIID); + }, + show: function() { + return this.returnOK; + } +}; + +let factory = { + createInstance: function(aOuter, aIid) { + if (aOuter != null) + throw Cr.NS_ERROR_NO_AGGREGATION; + return new FilePickerService().QueryInterface(aIid); + } +}; function run_test() { @@ -97,11 +158,19 @@ function run_test() return; } + //do_load_module("filepicker.js"); + Cm.registerFactory(FILE_PICKER_ID, + FILE_PICKER_DESCRIPTION, + FILE_PICKER_CID, + factory); + let prefsService = Cc["@mozilla.org/preferences-service;1"]. getService(Ci.nsIPrefService). QueryInterface(Ci.nsIPrefBranch); prefsService.setBoolPref("browser.privatebrowsing.keep_current_session", true); let prefs = prefsService.getBranch("browser.download."); + let obs = Cc["@mozilla.org/observer-service;1"]. + getService(Ci.nsIObserverService); let launcher = Cc["@mozilla.org/helperapplauncherdialog;1"]. getService(Ci.nsIHelperAppLauncherDialog); let dirSvc = Cc["@mozilla.org/file/directory_service;1"]. @@ -126,13 +195,32 @@ function run_test() let file2 = newFileInDirectory(dir2); let file3 = newFileInDirectory(dir3); + let observer = { + observe: function(aSubject, aTopic, aData) { + switch (aTopic) { + case "TEST_FILEPICKER_GETFILE": + let fileptr = aSubject.QueryInterface(Ci.nsISupportsInterfacePointer); + fileptr.data = this.file; + fileptr.dataIID = Ci.nsILocalFile; + break; + case "TEST_FILEPICKER_SETDISPLAYDIRECTORY": + this.displayDirectory = aSubject.QueryInterface(Ci.nsILocalFile); + break; + } + }, + file: null, + displayDirectory: null + }; + obs.addObserver(observer, "TEST_FILEPICKER_GETFILE", false); + obs.addObserver(observer, "TEST_FILEPICKER_SETDISPLAYDIRECTORY", false); + prefs.setComplexValue("lastDir", Ci.nsILocalFile, tmpDir); - MockFilePicker.returnFiles = [file1]; + observer.file = file1; let file = launcher.promptForSaveToFile(null, context, null, null, null); do_check_true(!!file); // file picker should start with browser.download.lastDir - do_check_eq(MockFilePicker.displayDirectory.path, tmpDir.path); + do_check_eq(observer.displayDirectory.path, tmpDir.path); // browser.download.lastDir should be modified before entering the private browsing mode do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path); // gDownloadLastDir should be usable outside of the private browsing mode @@ -140,12 +228,12 @@ function run_test() pb.privateBrowsingEnabled = true; do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path); - MockFilePicker.returnFiles = [file2]; - MockFilePicker.displayDirectory = null; + observer.file = file2; + observer.displayDirectory = null; file = launcher.promptForSaveToFile(null, context, null, null, null); do_check_true(!!file); // file picker should start with browser.download.lastDir as set before entering the private browsing mode - do_check_eq(MockFilePicker.displayDirectory.path, dir1.path); + do_check_eq(observer.displayDirectory.path, dir1.path); // browser.download.lastDir should not be modified inside the private browsing mode do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir1.path); // but gDownloadLastDir should be modified @@ -154,12 +242,12 @@ function run_test() pb.privateBrowsingEnabled = false; // gDownloadLastDir should be cleared after leaving the private browsing mode do_check_eq(gDownloadLastDir.file.path, dir1.path); - MockFilePicker.returnFiles = [file3]; - MockFilePicker.displayDirectory = null; + observer.file = file3; + observer.displayDirectory = null; file = launcher.promptForSaveToFile(null, context, null, null, null); do_check_true(!!file); // file picker should start with browser.download.lastDir as set before entering the private browsing mode - do_check_eq(MockFilePicker.displayDirectory.path, dir1.path); + do_check_eq(observer.displayDirectory.path, dir1.path); // browser.download.lastDir should be modified after leaving the private browsing mode do_check_eq(prefs.getComplexValue("lastDir", Ci.nsILocalFile).path, dir3.path); // gDownloadLastDir should be usable after leaving the private browsing mode @@ -169,4 +257,6 @@ function run_test() prefsService.clearUserPref("browser.privatebrowsing.keep_current_session"); [dir1, dir2, dir3].forEach(function(dir) dir.remove(true)); dirSvc.QueryInterface(Ci.nsIDirectoryService).unregisterProvider(provider); + obs.removeObserver(observer, "TEST_FILEPICKER_GETFILE", false); + obs.removeObserver(observer, "TEST_FILEPICKER_SETDISPLAYDIRECTORY", false); } diff --git a/toolkit/mozapps/extensions/test/browser/browser_bug567127.js b/toolkit/mozapps/extensions/test/browser/browser_bug567127.js index 6abdb5d77f5e..bed62f04f231 100644 --- a/toolkit/mozapps/extensions/test/browser/browser_bug567127.js +++ b/toolkit/mozapps/extensions/test/browser/browser_bug567127.js @@ -4,11 +4,39 @@ // Tests bug 567127 - Add install button to the add-ons manager -Components.utils.import("resource://mochikit/MockFilePicker.jsm"); -MockFilePicker.reset(); +var gFilePickerFiles = []; +var gMockFilePickerFactory; +var gMockFilePickerFactoryCID; var gManagerWindow; +function MockFilePicker() { } + +MockFilePicker.prototype = { + QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIFilePicker]), + init: function(aParent, aTitle, aMode) { }, + appendFilters: function(aFilterMask) { }, + appendFilter: function(aTitle, aFilter) { }, + defaultString: "", + defaultExtension: "", + filterIndex: 0, + displayDirectory: null, + file: null, + fileURL: null, + get files() { + var i = 0; + return { + getNext: function() gFilePickerFiles[i++], + hasMoreElements: function() gFilePickerFiles.length > i + }; + }, + show: function() { + return gFilePickerFiles.length == 0 ? + Components.interfaces.nsIFilePicker.returnCancel : + Components.interfaces.nsIFilePicker.returnOK; + } +}; + // This listens for the next opened window and checks it is of the right url. // opencallback is called when the new window is fully loaded // closecallback is called when the window is closed @@ -92,6 +120,14 @@ function test_confirmation(aWindow, aExpectedURLs) { function test() { waitForExplicitFinish(); + gMockFilePickerFactoryCID = Components.ID("{4f595df2-9108-42c6-9910-0dc392a310c9}"); + gMockFilePickerFactory = XPCOMUtils._getFactory(MockFilePicker); + var compReg = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); + compReg.registerFactory(gMockFilePickerFactoryCID, + "Mock FilePicker", + "@mozilla.org/filepicker;1", + gMockFilePickerFactory); + open_manager("addons://list/extension", function(aWindow) { gManagerWindow = aWindow; run_next_test(); @@ -99,6 +135,9 @@ function test() { } function end_test() { + var compReg = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); + compReg.unregisterFactory(gMockFilePickerFactoryCID, + gMockFilePickerFactory); close_manager(gManagerWindow, function() { finish(); }); @@ -110,7 +149,7 @@ add_test(function() { get_addon_file_url("browser_bug567127_1.xpi"), get_addon_file_url("browser_bug567127_2.xpi") ]; - MockFilePicker.returnFiles = filePaths.map(function(aPath) aPath.file); + gFilePickerFiles = filePaths.map(function(aPath) aPath.file); new WindowOpenListener(INSTALL_URI, function(aWindow) { test_confirmation(aWindow, filePaths.map(function(aPath) aPath.spec)); diff --git a/toolkit/mozapps/extensions/test/browser/browser_inlinesettings.js b/toolkit/mozapps/extensions/test/browser/browser_inlinesettings.js index 562ed5ce7e49..1421bbd0963e 100644 --- a/toolkit/mozapps/extensions/test/browser/browser_inlinesettings.js +++ b/toolkit/mozapps/extensions/test/browser/browser_inlinesettings.js @@ -10,9 +10,6 @@ var gProvider; const SETTINGS_ROWS = 8; -Components.utils.import("resource://mochikit/MockFilePicker.jsm"); -MockFilePicker.reset(); - var observer = { lastData: null, observe: function(aSubject, aTopic, aData) { @@ -204,6 +201,8 @@ add_test(function() { is(Services.prefs.getCharPref("extensions.inlinesettings1.color"), "#FF9900", "Color pref should have been updated"); try { + mockFilePickerFactory.register(); + ok(!settings[6].hasAttribute("first-row"), "Not the first row"); var button = gManagerWindow.document.getAnonymousElementByAttribute(settings[6], "anonid", "button"); input = gManagerWindow.document.getAnonymousElementByAttribute(settings[6], "anonid", "input"); @@ -212,17 +211,17 @@ add_test(function() { var profD = Services.dirsvc.get("ProfD", Ci.nsIFile); var curProcD = Services.dirsvc.get("CurProcD", Ci.nsIFile); - MockFilePicker.returnFiles = [profD]; - MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK; + _returnFile = profD; + _returnValue = Ci.nsIFilePicker.returnOK; EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow); - is(MockFilePicker.mode, Ci.nsIFilePicker.modeOpen, "File picker mode should be open file"); + is(_mode, Ci.nsIFilePicker.modeOpen, "File picker mode should be open file"); is(input.value, profD.path, "Label value should match file chosen"); is(Services.prefs.getCharPref("extensions.inlinesettings1.file"), profD.path, "File pref should match file chosen"); - MockFilePicker.returnFiles = [curProcD]; - MockFilePicker.returnValue = Ci.nsIFilePicker.returnCancel; + _returnFile = curProcD; + _returnValue = Ci.nsIFilePicker.returnCancel; EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow); - is(MockFilePicker.mode, Ci.nsIFilePicker.modeOpen, "File picker mode should be open file"); + is(_mode, Ci.nsIFilePicker.modeOpen, "File picker mode should be open file"); is(input.value, profD.path, "Label value should not have changed"); is(Services.prefs.getCharPref("extensions.inlinesettings1.file"), profD.path, "File pref should not have changed"); @@ -231,21 +230,23 @@ add_test(function() { input = gManagerWindow.document.getAnonymousElementByAttribute(settings[7], "anonid", "input"); is(input.value, "", "Label value should be empty"); - MockFilePicker.returnFiles = [profD]; - MockFilePicker.returnValue = Ci.nsIFilePicker.returnOK; + _returnFile = profD; + _returnValue = Ci.nsIFilePicker.returnOK; EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow); - is(MockFilePicker.mode, Ci.nsIFilePicker.modeGetFolder, "File picker mode should be directory"); + is(_mode, Ci.nsIFilePicker.modeGetFolder, "File picker mode should be directory"); is(input.value, profD.path, "Label value should match file chosen"); is(Services.prefs.getCharPref("extensions.inlinesettings1.directory"), profD.path, "Directory pref should match file chosen"); - MockFilePicker.returnFiles = [curProcD]; - MockFilePicker.returnValue = Ci.nsIFilePicker.returnCancel; + _returnFile = curProcD; + _returnValue = Ci.nsIFilePicker.returnCancel; EventUtils.synthesizeMouseAtCenter(button, { clickCount: 1 }, gManagerWindow); - is(MockFilePicker.mode, Ci.nsIFilePicker.modeGetFolder, "File picker mode should be directory"); + is(_mode, Ci.nsIFilePicker.modeGetFolder, "File picker mode should be directory"); is(input.value, profD.path, "Label value should not have changed"); is(Services.prefs.getCharPref("extensions.inlinesettings1.directory"), profD.path, "Directory pref should not have changed"); } finally { + mockFilePickerFactory.unregister(); + button = gManagerWindow.document.getElementById("detail-prefs-btn"); is_element_hidden(button, "Preferences button should not be visible"); @@ -397,3 +398,60 @@ add_test(function() { }); }); }); + +var _returnFile, _returnValue, _mode; + +function MockFilePicker() { }; +MockFilePicker.prototype = { + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]), + init: function(aParent, aTitle, aMode) { + _mode = aMode; + }, + appendFilters: function(aFilterMask) { }, + appendFilter: function(aTitle, aFilter) { }, + defaultString: "", + defaultExtension: "", + filterIndex: 0, + displayDirectory: null, + get file() { + return _returnFile; + }, + get fileURL() { + throw Cr.NS_ERROR_NOT_IMPLEMENTED; + }, + get files() { + throw Cr.NS_ERROR_NOT_IMPLEMENTED; + }, + show: function() { + return _returnValue; + } +}; +var mockFilePickerFactory = { + registrar: Components.manager.QueryInterface(Ci.nsIComponentRegistrar), + contractID: "@mozilla.org/filepicker;1", + classID: Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID(), + + register: function() { + this.registrar.registerFactory(this.classID, "", this.contractID, this); + }, + + unregister: function() { + this.registrar.unregisterFactory(this.classID, this); + }, + + // nsIFactory + createInstance: function(aOuter, aIID) { + if (aOuter) { + throw Components.results.NS_ERROR_NO_AGGREGATION; + } + return new MockFilePicker().QueryInterface(aIID); + }, + + lockFactory: function(aLock) { + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; + }, + + QueryInterface: XPCOMUtils.generateQI([ + Ci.nsIFactory + ]) +};