michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const RELATIVE_DIR = "browser/extensions/pdfjs/test/"; michael@0: const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR; michael@0: michael@0: function test() { michael@0: var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true); michael@0: var tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf"); michael@0: // michael@0: // Test: "Open with" dialog comes up when pdf.js is not selected as the default michael@0: // handler. michael@0: // michael@0: addWindowListener('chrome://mozapps/content/downloads/unknownContentType.xul', finish); michael@0: michael@0: waitForExplicitFinish(); michael@0: registerCleanupFunction(function() { michael@0: changeMimeHandler(oldAction[0], oldAction[1]); michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: } michael@0: michael@0: function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) { michael@0: let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService); michael@0: let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); michael@0: let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf'); michael@0: var oldAction = [handlerInfo.preferredAction, handlerInfo.alwaysAskBeforeHandling]; michael@0: michael@0: // Change and save mime handler settings michael@0: handlerInfo.alwaysAskBeforeHandling = alwaysAskBeforeHandling; michael@0: handlerInfo.preferredAction = preferredAction; michael@0: handlerService.store(handlerInfo); michael@0: michael@0: Services.obs.notifyObservers(null, 'pdfjs:handlerChanged', null); michael@0: michael@0: // Refresh data michael@0: handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf'); michael@0: michael@0: // michael@0: // Test: Mime handler was updated michael@0: // michael@0: is(handlerInfo.alwaysAskBeforeHandling, alwaysAskBeforeHandling, 'always-ask prompt change successful'); michael@0: is(handlerInfo.preferredAction, preferredAction, 'mime handler change successful'); michael@0: michael@0: return oldAction; michael@0: } michael@0: michael@0: function addWindowListener(aURL, aCallback) { michael@0: Services.wm.addListener({ michael@0: onOpenWindow: function(aXULWindow) { michael@0: info("window opened, waiting for focus"); michael@0: Services.wm.removeListener(this); michael@0: michael@0: var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindow); michael@0: waitForFocus(function() { michael@0: is(domwindow.document.location.href, aURL, "should have seen the right window open"); michael@0: domwindow.close(); michael@0: aCallback(); michael@0: }, domwindow); michael@0: }, michael@0: onCloseWindow: function(aXULWindow) { }, michael@0: onWindowTitleChange: function(aXULWindow, aNewTitle) { } michael@0: }); michael@0: }