michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: var MockFilePicker = SpecialPowers.MockFilePicker; michael@0: MockFilePicker.init(window); michael@0: michael@0: /** michael@0: * TestCase for bug 564387 michael@0: * michael@0: */ michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: var fileName; michael@0: michael@0: gBrowser.loadURI("http://mochi.test:8888/browser/browser/base/content/test/general/bug564387.html"); michael@0: michael@0: gBrowser.addEventListener("pageshow", function pageShown(event) { michael@0: if (event.target.location == "about:blank") michael@0: return; michael@0: gBrowser.removeEventListener("pageshow", pageShown); michael@0: michael@0: executeSoon(function () { michael@0: document.addEventListener("popupshown", contextMenuOpened); michael@0: michael@0: var video1 = gBrowser.contentDocument.getElementById("video1"); michael@0: EventUtils.synthesizeMouseAtCenter(video1, michael@0: { type: "contextmenu", button: 2 }, michael@0: gBrowser.contentWindow); michael@0: }); michael@0: }); michael@0: michael@0: function contextMenuOpened(event) { michael@0: event.currentTarget.removeEventListener("popupshown", contextMenuOpened); michael@0: michael@0: // Create the folder the video will be saved into. michael@0: var destDir = createTemporarySaveDirectory(); michael@0: var destFile = destDir.clone(); michael@0: michael@0: MockFilePicker.displayDirectory = destDir; michael@0: MockFilePicker.showCallback = function(fp) { michael@0: fileName = fp.defaultString; michael@0: destFile.append (fileName); michael@0: MockFilePicker.returnFiles = [destFile]; michael@0: MockFilePicker.filterIndex = 1; // kSaveAsType_URL michael@0: }; michael@0: michael@0: mockTransferCallback = onTransferComplete; michael@0: mockTransferRegisterer.register(); michael@0: michael@0: registerCleanupFunction(function () { michael@0: mockTransferRegisterer.unregister(); michael@0: MockFilePicker.cleanup(); michael@0: destDir.remove(true); michael@0: }); michael@0: michael@0: // Select "Save Video As" option from context menu michael@0: var saveVideoCommand = document.getElementById("context-savevideo"); michael@0: saveVideoCommand.doCommand(); michael@0: michael@0: event.target.hidePopup(); michael@0: } michael@0: michael@0: function onTransferComplete(downloadSuccess) { michael@0: ok(downloadSuccess, "Video file should have been downloaded successfully"); michael@0: michael@0: is(fileName, "Bug564387-expectedName.ogv", michael@0: "Video file name is correctly retrieved from Content-Disposition http header"); michael@0: michael@0: finish(); michael@0: } michael@0: } michael@0: michael@0: Cc["@mozilla.org/moz/jssubscript-loader;1"] michael@0: .getService(Ci.mozIJSSubScriptLoader) michael@0: .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", michael@0: this); michael@0: michael@0: function createTemporarySaveDirectory() { michael@0: var saveDir = Cc["@mozilla.org/file/directory_service;1"] michael@0: .getService(Ci.nsIProperties) michael@0: .get("TmpD", Ci.nsIFile); michael@0: saveDir.append("testsavedir"); michael@0: if (!saveDir.exists()) michael@0: saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); michael@0: return saveDir; michael@0: }