Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | var MockFilePicker = SpecialPowers.MockFilePicker; |
michael@0 | 5 | MockFilePicker.init(window); |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * TestCase for bug 564387 |
michael@0 | 9 | * <https://bugzilla.mozilla.org/show_bug.cgi?id=564387> |
michael@0 | 10 | */ |
michael@0 | 11 | function test() { |
michael@0 | 12 | waitForExplicitFinish(); |
michael@0 | 13 | var fileName; |
michael@0 | 14 | |
michael@0 | 15 | gBrowser.loadURI("http://mochi.test:8888/browser/browser/base/content/test/general/bug564387.html"); |
michael@0 | 16 | |
michael@0 | 17 | gBrowser.addEventListener("pageshow", function pageShown(event) { |
michael@0 | 18 | if (event.target.location == "about:blank") |
michael@0 | 19 | return; |
michael@0 | 20 | gBrowser.removeEventListener("pageshow", pageShown); |
michael@0 | 21 | |
michael@0 | 22 | executeSoon(function () { |
michael@0 | 23 | document.addEventListener("popupshown", contextMenuOpened); |
michael@0 | 24 | |
michael@0 | 25 | var video1 = gBrowser.contentDocument.getElementById("video1"); |
michael@0 | 26 | EventUtils.synthesizeMouseAtCenter(video1, |
michael@0 | 27 | { type: "contextmenu", button: 2 }, |
michael@0 | 28 | gBrowser.contentWindow); |
michael@0 | 29 | }); |
michael@0 | 30 | }); |
michael@0 | 31 | |
michael@0 | 32 | function contextMenuOpened(event) { |
michael@0 | 33 | event.currentTarget.removeEventListener("popupshown", contextMenuOpened); |
michael@0 | 34 | |
michael@0 | 35 | // Create the folder the video will be saved into. |
michael@0 | 36 | var destDir = createTemporarySaveDirectory(); |
michael@0 | 37 | var destFile = destDir.clone(); |
michael@0 | 38 | |
michael@0 | 39 | MockFilePicker.displayDirectory = destDir; |
michael@0 | 40 | MockFilePicker.showCallback = function(fp) { |
michael@0 | 41 | fileName = fp.defaultString; |
michael@0 | 42 | destFile.append (fileName); |
michael@0 | 43 | MockFilePicker.returnFiles = [destFile]; |
michael@0 | 44 | MockFilePicker.filterIndex = 1; // kSaveAsType_URL |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | mockTransferCallback = onTransferComplete; |
michael@0 | 48 | mockTransferRegisterer.register(); |
michael@0 | 49 | |
michael@0 | 50 | registerCleanupFunction(function () { |
michael@0 | 51 | mockTransferRegisterer.unregister(); |
michael@0 | 52 | MockFilePicker.cleanup(); |
michael@0 | 53 | destDir.remove(true); |
michael@0 | 54 | }); |
michael@0 | 55 | |
michael@0 | 56 | // Select "Save Video As" option from context menu |
michael@0 | 57 | var saveVideoCommand = document.getElementById("context-savevideo"); |
michael@0 | 58 | saveVideoCommand.doCommand(); |
michael@0 | 59 | |
michael@0 | 60 | event.target.hidePopup(); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function onTransferComplete(downloadSuccess) { |
michael@0 | 64 | ok(downloadSuccess, "Video file should have been downloaded successfully"); |
michael@0 | 65 | |
michael@0 | 66 | is(fileName, "Bug564387-expectedName.ogv", |
michael@0 | 67 | "Video file name is correctly retrieved from Content-Disposition http header"); |
michael@0 | 68 | |
michael@0 | 69 | finish(); |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | Cc["@mozilla.org/moz/jssubscript-loader;1"] |
michael@0 | 74 | .getService(Ci.mozIJSSubScriptLoader) |
michael@0 | 75 | .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", |
michael@0 | 76 | this); |
michael@0 | 77 | |
michael@0 | 78 | function createTemporarySaveDirectory() { |
michael@0 | 79 | var saveDir = Cc["@mozilla.org/file/directory_service;1"] |
michael@0 | 80 | .getService(Ci.nsIProperties) |
michael@0 | 81 | .get("TmpD", Ci.nsIFile); |
michael@0 | 82 | saveDir.append("testsavedir"); |
michael@0 | 83 | if (!saveDir.exists()) |
michael@0 | 84 | saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); |
michael@0 | 85 | return saveDir; |
michael@0 | 86 | } |