browser_scratchpad_unsaved.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /* Bug 669612 */
  4. // only finish() when correct number of tests are done
  5. const expected = 4;
  6. var count = 0;
  7. function done()
  8. {
  9. if (++count == expected) {
  10. finish();
  11. }
  12. }
  13. function test()
  14. {
  15. waitForExplicitFinish();
  16. testListeners();
  17. testRestoreNotFromFile();
  18. testRestoreFromFileSaved();
  19. testRestoreFromFileUnsaved();
  20. gBrowser.selectedTab = gBrowser.addTab();
  21. content.location = "data:text/html,<p>test star* UI for unsaved file changes";
  22. }
  23. function testListeners()
  24. {
  25. openScratchpad(function (aWin, aScratchpad) {
  26. aScratchpad.setText("new text");
  27. ok(isStar(aWin), "show star if scratchpad text changes");
  28. aScratchpad.dirty = false;
  29. ok(!isStar(aWin), "no star before changing text");
  30. aScratchpad.setFilename("foo.js");
  31. aScratchpad.setText("new text2");
  32. ok(isStar(aWin), "shows star if scratchpad text changes");
  33. aScratchpad.dirty = false;
  34. ok(!isStar(aWin), "no star if scratchpad was just saved");
  35. aScratchpad.setText("new text3");
  36. ok(isStar(aWin), "shows star if scratchpad has more changes");
  37. aScratchpad.undo();
  38. ok(!isStar(aWin), "no star if scratchpad undo to save point");
  39. aScratchpad.undo();
  40. ok(isStar(aWin), "star if scratchpad undo past save point");
  41. aWin.close();
  42. done();
  43. }, {noFocus: true});
  44. }
  45. function testRestoreNotFromFile()
  46. {
  47. let session = [{
  48. text: "test1",
  49. executionContext: 1
  50. }];
  51. let [win] = ScratchpadManager.restoreSession(session);
  52. openScratchpad(function (aWin, aScratchpad) {
  53. aScratchpad.setText("new text");
  54. ok(isStar(win), "show star if restored scratchpad isn't from a file");
  55. win.close();
  56. done();
  57. }, {window: win, noFocus: true});
  58. }
  59. function testRestoreFromFileSaved()
  60. {
  61. let session = [{
  62. filename: "test.js",
  63. text: "test1",
  64. executionContext: 1,
  65. saved: true
  66. }];
  67. let [win] = ScratchpadManager.restoreSession(session);
  68. openScratchpad(function (aWin, aScratchpad) {
  69. ok(!isStar(win), "no star before changing text in scratchpad restored from file");
  70. aScratchpad.setText("new text");
  71. ok(isStar(win), "star when text changed from scratchpad restored from file");
  72. win.close();
  73. done();
  74. }, {window: win, noFocus: true});
  75. }
  76. function testRestoreFromFileUnsaved()
  77. {
  78. let session = [{
  79. filename: "test.js",
  80. text: "test1",
  81. executionContext: 1,
  82. saved: false
  83. }];
  84. let [win] = ScratchpadManager.restoreSession(session);
  85. openScratchpad(function () {
  86. ok(isStar(win), "star with scratchpad restored with unsaved text");
  87. win.close();
  88. done();
  89. }, {window: win, noFocus: true});
  90. }
  91. function isStar(win)
  92. {
  93. return win.document.title.match(/^\*[^\*]/);
  94. }