browser_scratchpad_modeline.js 3.1 KB

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