|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 const RELATIVE_DIR = "browser/extensions/pdfjs/test/"; |
|
5 const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR; |
|
6 |
|
7 function test() { |
|
8 var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true); |
|
9 var tab = gBrowser.addTab(TESTROOT + "file_pdfjs_test.pdf"); |
|
10 // |
|
11 // Test: "Open with" dialog comes up when pdf.js is not selected as the default |
|
12 // handler. |
|
13 // |
|
14 addWindowListener('chrome://mozapps/content/downloads/unknownContentType.xul', finish); |
|
15 |
|
16 waitForExplicitFinish(); |
|
17 registerCleanupFunction(function() { |
|
18 changeMimeHandler(oldAction[0], oldAction[1]); |
|
19 gBrowser.removeTab(tab); |
|
20 }); |
|
21 } |
|
22 |
|
23 function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) { |
|
24 let handlerService = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService); |
|
25 let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService); |
|
26 let handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf'); |
|
27 var oldAction = [handlerInfo.preferredAction, handlerInfo.alwaysAskBeforeHandling]; |
|
28 |
|
29 // Change and save mime handler settings |
|
30 handlerInfo.alwaysAskBeforeHandling = alwaysAskBeforeHandling; |
|
31 handlerInfo.preferredAction = preferredAction; |
|
32 handlerService.store(handlerInfo); |
|
33 |
|
34 Services.obs.notifyObservers(null, 'pdfjs:handlerChanged', null); |
|
35 |
|
36 // Refresh data |
|
37 handlerInfo = mimeService.getFromTypeAndExtension('application/pdf', 'pdf'); |
|
38 |
|
39 // |
|
40 // Test: Mime handler was updated |
|
41 // |
|
42 is(handlerInfo.alwaysAskBeforeHandling, alwaysAskBeforeHandling, 'always-ask prompt change successful'); |
|
43 is(handlerInfo.preferredAction, preferredAction, 'mime handler change successful'); |
|
44 |
|
45 return oldAction; |
|
46 } |
|
47 |
|
48 function addWindowListener(aURL, aCallback) { |
|
49 Services.wm.addListener({ |
|
50 onOpenWindow: function(aXULWindow) { |
|
51 info("window opened, waiting for focus"); |
|
52 Services.wm.removeListener(this); |
|
53 |
|
54 var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
|
55 .getInterface(Ci.nsIDOMWindow); |
|
56 waitForFocus(function() { |
|
57 is(domwindow.document.location.href, aURL, "should have seen the right window open"); |
|
58 domwindow.close(); |
|
59 aCallback(); |
|
60 }, domwindow); |
|
61 }, |
|
62 onCloseWindow: function(aXULWindow) { }, |
|
63 onWindowTitleChange: function(aXULWindow, aNewTitle) { } |
|
64 }); |
|
65 } |