|
1 /* vim: set ts=2 et sw=2 tw=80: */ |
|
2 /* Any copyright is dedicated to the Public Domain. |
|
3 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
4 /* Bug 644413 */ |
|
5 |
|
6 let tempScope = {}; |
|
7 Cu.import("resource://gre/modules/NetUtil.jsm", tempScope); |
|
8 Cu.import("resource://gre/modules/FileUtils.jsm", tempScope); |
|
9 let NetUtil = tempScope.NetUtil; |
|
10 let FileUtils = tempScope.FileUtils; |
|
11 |
|
12 |
|
13 let gScratchpad; // Reference to the Scratchpad object. |
|
14 let gFile; // Reference to the temporary nsIFile we will work with. |
|
15 let DEVTOOLS_CHROME_ENABLED = "devtools.chrome.enabled"; |
|
16 |
|
17 // The temporary file content. |
|
18 let gFileContent = "function main() { return 0; }"; |
|
19 |
|
20 function test() { |
|
21 waitForExplicitFinish(); |
|
22 |
|
23 gBrowser.selectedTab = gBrowser.addTab(); |
|
24 gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
|
25 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
|
26 openScratchpad(runTests); |
|
27 }, true); |
|
28 |
|
29 content.location = "data:text/html,<p>test file open and save in Scratchpad"; |
|
30 } |
|
31 |
|
32 function runTests() { |
|
33 gScratchpad = gScratchpadWindow.Scratchpad; |
|
34 function size(obj) { return Object.keys(obj).length; } |
|
35 |
|
36 // Test Scratchpad._scanModeLine method. |
|
37 let obj = gScratchpad._scanModeLine(); |
|
38 is(size(obj), 0, "Mode-line object has no properties"); |
|
39 |
|
40 obj = gScratchpad._scanModeLine("/* This is not a mode-line comment */"); |
|
41 is(size(obj), 0, "Mode-line object has no properties"); |
|
42 |
|
43 obj = gScratchpad._scanModeLine("/* -sp-context:browser */"); |
|
44 is(size(obj), 1, "Mode-line object has one property"); |
|
45 is(obj["-sp-context"], "browser"); |
|
46 |
|
47 obj = gScratchpad._scanModeLine("/* -sp-context: browser */"); |
|
48 is(size(obj), 1, "Mode-line object has one property"); |
|
49 is(obj["-sp-context"], "browser"); |
|
50 |
|
51 obj = gScratchpad._scanModeLine("// -sp-context: browser"); |
|
52 is(size(obj), 1, "Mode-line object has one property"); |
|
53 is(obj["-sp-context"], "browser"); |
|
54 |
|
55 obj = gScratchpad._scanModeLine("/* -sp-context:browser, other:true */"); |
|
56 is(size(obj), 2, "Mode-line object has two properties"); |
|
57 is(obj["-sp-context"], "browser"); |
|
58 is(obj["other"], "true"); |
|
59 |
|
60 // Test importing files with a mode-line in them. |
|
61 let content = "/* -sp-context:browser */\n" + gFileContent; |
|
62 createTempFile("fileForBug644413.tmp", content, function(aStatus, aFile) { |
|
63 ok(Components.isSuccessCode(aStatus), "File was saved successfully"); |
|
64 |
|
65 gFile = aFile; |
|
66 gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, fileImported); |
|
67 }); |
|
68 } |
|
69 |
|
70 function fileImported(status, content) { |
|
71 ok(Components.isSuccessCode(status), "File was imported successfully"); |
|
72 |
|
73 // Since devtools.chrome.enabled is off, Scratchpad should still be in |
|
74 // the content context. |
|
75 is(gScratchpad.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT); |
|
76 |
|
77 // Set the pref and try again. |
|
78 Services.prefs.setBoolPref(DEVTOOLS_CHROME_ENABLED, true); |
|
79 |
|
80 gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, function(status, content) { |
|
81 ok(Components.isSuccessCode(status), "File was imported successfully"); |
|
82 is(gScratchpad.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_BROWSER); |
|
83 |
|
84 gFile.remove(false); |
|
85 gFile = null; |
|
86 gScratchpad = null; |
|
87 finish(); |
|
88 }); |
|
89 } |
|
90 |
|
91 registerCleanupFunction(function () { |
|
92 Services.prefs.clearUserPref(DEVTOOLS_CHROME_ENABLED); |
|
93 }); |