browser_scratchpad_open.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. // only finish() when correct number of tests are done
  4. const expected = 4;
  5. var count = 0;
  6. var lastUniqueName = null;
  7. function done()
  8. {
  9. if (++count == expected) {
  10. finish();
  11. }
  12. }
  13. function test()
  14. {
  15. waitForExplicitFinish();
  16. testOpen();
  17. testOpenWithState();
  18. testOpenInvalidState();
  19. testOpenTestFile();
  20. }
  21. function testUniqueName(name)
  22. {
  23. ok(name, "Scratchpad has a uniqueName");
  24. if (lastUniqueName === null) {
  25. lastUniqueName = name;
  26. return;
  27. }
  28. ok(name !== lastUniqueName,
  29. "Unique name for this instance differs from the last one.");
  30. }
  31. function testOpen()
  32. {
  33. openScratchpad(function (win) {
  34. is(win.Scratchpad.filename, undefined, "Default filename is undefined");
  35. isnot(win.Scratchpad.getText(), null, "Default text should not be null");
  36. is(win.Scratchpad.executionContext, win.SCRATCHPAD_CONTEXT_CONTENT,
  37. "Default execution context is content");
  38. testUniqueName(win.Scratchpad.uniqueName);
  39. win.close();
  40. done();
  41. }, {noFocus: true});
  42. }
  43. function testOpenWithState()
  44. {
  45. let state = {
  46. filename: "testfile",
  47. executionContext: 2,
  48. text: "test text"
  49. };
  50. openScratchpad(function (win) {
  51. is(win.Scratchpad.filename, state.filename, "Filename loaded from state");
  52. is(win.Scratchpad.executionContext, state.executionContext, "Execution context loaded from state");
  53. is(win.Scratchpad.getText(), state.text, "Content loaded from state");
  54. testUniqueName(win.Scratchpad.uniqueName);
  55. win.close();
  56. done();
  57. }, {state: state, noFocus: true});
  58. }
  59. function testOpenInvalidState()
  60. {
  61. let win = openScratchpad(null, {state: 7});
  62. ok(!win, "no scratchpad opened if state is not an object");
  63. done();
  64. }
  65. function testOpenTestFile()
  66. {
  67. let win = openScratchpad(function (win) {
  68. ok(win, "scratchpad opened for file open");
  69. try {
  70. win.Scratchpad.importFromFile(
  71. "http://example.com/browser/devtools/client/scratchpad/test/NS_ERROR_ILLEGAL_INPUT.txt",
  72. "silent",
  73. function (aStatus, content) {
  74. let nb = win.document.querySelector("#scratchpad-notificationbox");
  75. is(nb.querySelectorAll("notification").length, 1, "There is just one notification");
  76. let cn = nb.currentNotification;
  77. is(cn.priority, nb.PRIORITY_WARNING_HIGH, "notification priority is correct");
  78. is(cn.value, "file-import-convert-failed", "notification value is corrent");
  79. is(cn.type, "warning", "notification type is correct");
  80. done();
  81. });
  82. ok(true, "importFromFile does not cause exception");
  83. } catch (exception) {
  84. ok(false, "importFromFile causes exception " + DevToolsUtils.safeErrorString(exception));
  85. }
  86. }, {noFocus: true});
  87. }