michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: /* Bug 644413 */ michael@0: michael@0: let tempScope = {}; michael@0: Cu.import("resource://gre/modules/NetUtil.jsm", tempScope); michael@0: Cu.import("resource://gre/modules/FileUtils.jsm", tempScope); michael@0: let NetUtil = tempScope.NetUtil; michael@0: let FileUtils = tempScope.FileUtils; michael@0: michael@0: michael@0: let gScratchpad; // Reference to the Scratchpad object. michael@0: let gFile; // Reference to the temporary nsIFile we will work with. michael@0: let DEVTOOLS_CHROME_ENABLED = "devtools.chrome.enabled"; michael@0: michael@0: // The temporary file content. michael@0: let gFileContent = "function main() { return 0; }"; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: openScratchpad(runTests); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html,
test file open and save in Scratchpad"; michael@0: } michael@0: michael@0: function runTests() { michael@0: gScratchpad = gScratchpadWindow.Scratchpad; michael@0: function size(obj) { return Object.keys(obj).length; } michael@0: michael@0: // Test Scratchpad._scanModeLine method. michael@0: let obj = gScratchpad._scanModeLine(); michael@0: is(size(obj), 0, "Mode-line object has no properties"); michael@0: michael@0: obj = gScratchpad._scanModeLine("/* This is not a mode-line comment */"); michael@0: is(size(obj), 0, "Mode-line object has no properties"); michael@0: michael@0: obj = gScratchpad._scanModeLine("/* -sp-context:browser */"); michael@0: is(size(obj), 1, "Mode-line object has one property"); michael@0: is(obj["-sp-context"], "browser"); michael@0: michael@0: obj = gScratchpad._scanModeLine("/* -sp-context: browser */"); michael@0: is(size(obj), 1, "Mode-line object has one property"); michael@0: is(obj["-sp-context"], "browser"); michael@0: michael@0: obj = gScratchpad._scanModeLine("// -sp-context: browser"); michael@0: is(size(obj), 1, "Mode-line object has one property"); michael@0: is(obj["-sp-context"], "browser"); michael@0: michael@0: obj = gScratchpad._scanModeLine("/* -sp-context:browser, other:true */"); michael@0: is(size(obj), 2, "Mode-line object has two properties"); michael@0: is(obj["-sp-context"], "browser"); michael@0: is(obj["other"], "true"); michael@0: michael@0: // Test importing files with a mode-line in them. michael@0: let content = "/* -sp-context:browser */\n" + gFileContent; michael@0: createTempFile("fileForBug644413.tmp", content, function(aStatus, aFile) { michael@0: ok(Components.isSuccessCode(aStatus), "File was saved successfully"); michael@0: michael@0: gFile = aFile; michael@0: gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, fileImported); michael@0: }); michael@0: } michael@0: michael@0: function fileImported(status, content) { michael@0: ok(Components.isSuccessCode(status), "File was imported successfully"); michael@0: michael@0: // Since devtools.chrome.enabled is off, Scratchpad should still be in michael@0: // the content context. michael@0: is(gScratchpad.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT); michael@0: michael@0: // Set the pref and try again. michael@0: Services.prefs.setBoolPref(DEVTOOLS_CHROME_ENABLED, true); michael@0: michael@0: gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, function(status, content) { michael@0: ok(Components.isSuccessCode(status), "File was imported successfully"); michael@0: is(gScratchpad.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_BROWSER); michael@0: michael@0: gFile.remove(false); michael@0: gFile = null; michael@0: gScratchpad = null; michael@0: finish(); michael@0: }); michael@0: } michael@0: michael@0: registerCleanupFunction(function () { michael@0: Services.prefs.clearUserPref(DEVTOOLS_CHROME_ENABLED); michael@0: });