browser/devtools/scratchpad/test/browser_scratchpad_modeline.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial