browser_scratchpad_recent_files.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /* Bug 651942 */
  4. // Reference to the Scratchpad object.
  5. var gScratchpad;
  6. // References to the temporary nsIFiles.
  7. var gFile01;
  8. var gFile02;
  9. var gFile03;
  10. var gFile04;
  11. // lists of recent files.
  12. var lists = {
  13. recentFiles01: null,
  14. recentFiles02: null,
  15. recentFiles03: null,
  16. recentFiles04: null,
  17. };
  18. // Temporary file names.
  19. var gFileName01 = "file01_ForBug651942.tmp";
  20. var gFileName02 = "☕"; // See bug 783858 for more information
  21. var gFileName03 = "file03_ForBug651942.tmp";
  22. var gFileName04 = "file04_ForBug651942.tmp";
  23. // Content for the temporary files.
  24. var gFileContent;
  25. var gFileContent01 = "hello.world.01('bug651942');";
  26. var gFileContent02 = "hello.world.02('bug651942');";
  27. var gFileContent03 = "hello.world.03('bug651942');";
  28. var gFileContent04 = "hello.world.04('bug651942');";
  29. function startTest()
  30. {
  31. gScratchpad = gScratchpadWindow.Scratchpad;
  32. gFile01 = createAndLoadTemporaryFile(gFile01, gFileName01, gFileContent01);
  33. gFile02 = createAndLoadTemporaryFile(gFile02, gFileName02, gFileContent02);
  34. gFile03 = createAndLoadTemporaryFile(gFile03, gFileName03, gFileContent03);
  35. }
  36. // Test to see if the three files we created in the 'startTest()'-method have
  37. // been added to the list of recent files.
  38. function testAddedToRecent()
  39. {
  40. lists.recentFiles01 = gScratchpad.getRecentFiles();
  41. is(lists.recentFiles01.length, 3,
  42. "Temporary files created successfully and added to list of recent files.");
  43. // Create a 4th file, this should clear the oldest file.
  44. gFile04 = createAndLoadTemporaryFile(gFile04, gFileName04, gFileContent04);
  45. }
  46. // We have opened a 4th file. Test to see if the oldest recent file was removed,
  47. // and that the other files were reordered successfully.
  48. function testOverwriteRecent()
  49. {
  50. lists.recentFiles02 = gScratchpad.getRecentFiles();
  51. is(lists.recentFiles02[0], lists.recentFiles01[1],
  52. "File02 was reordered successfully in the 'recent files'-list.");
  53. is(lists.recentFiles02[1], lists.recentFiles01[2],
  54. "File03 was reordered successfully in the 'recent files'-list.");
  55. isnot(lists.recentFiles02[2], lists.recentFiles01[2],
  56. "File04: was added successfully.");
  57. // Open the oldest recent file.
  58. gScratchpad.openFile(0);
  59. }
  60. // We have opened the "oldest"-recent file. Test to see if it is now the most
  61. // recent file, and that the other files were reordered successfully.
  62. function testOpenOldestRecent()
  63. {
  64. lists.recentFiles03 = gScratchpad.getRecentFiles();
  65. is(lists.recentFiles02[0], lists.recentFiles03[2],
  66. "File04 was reordered successfully in the 'recent files'-list.");
  67. is(lists.recentFiles02[1], lists.recentFiles03[0],
  68. "File03 was reordered successfully in the 'recent files'-list.");
  69. is(lists.recentFiles02[2], lists.recentFiles03[1],
  70. "File02 was reordered successfully in the 'recent files'-list.");
  71. Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 0);
  72. }
  73. // The "devtools.scratchpad.recentFilesMax"-preference was set to zero (0).
  74. // This should disable the "Open Recent"-menu by hiding it (this should not
  75. // remove any files from the list). Test to see if it's been hidden.
  76. function testHideMenu()
  77. {
  78. let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu");
  79. ok(menu.hasAttribute("hidden"), "The menu was hidden successfully.");
  80. Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 2);
  81. }
  82. // We have set the recentFilesMax-pref to one (1), this enables the feature,
  83. // removes the two oldest files, rebuilds the menu and removes the
  84. // "hidden"-attribute from it. Test to see if this works.
  85. function testChangedMaxRecent()
  86. {
  87. let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu");
  88. ok(!menu.hasAttribute("hidden"), "The menu is visible. \\o/");
  89. lists.recentFiles04 = gScratchpad.getRecentFiles();
  90. is(lists.recentFiles04.length, 2,
  91. "Two recent files were successfully removed from the 'recent files'-list");
  92. let doc = gScratchpadWindow.document;
  93. let popup = doc.getElementById("sp-menu-open_recentPopup");
  94. let menuitemLabel = popup.children[0].getAttribute("label");
  95. let correctMenuItem = false;
  96. if (menuitemLabel === lists.recentFiles03[2] &&
  97. menuitemLabel === lists.recentFiles04[1]) {
  98. correctMenuItem = true;
  99. }
  100. is(correctMenuItem, true,
  101. "Two recent files were successfully removed from the 'Open Recent'-menu");
  102. // We now remove one file from the harddrive and use the recent-menuitem for
  103. // it to make sure the user is notified that the file no longer exists.
  104. // This is tested in testOpenDeletedFile().
  105. gFile04.remove(false);
  106. // Make sure the file has been deleted before continuing to avoid
  107. // intermittent oranges.
  108. waitForFileDeletion();
  109. }
  110. function waitForFileDeletion() {
  111. if (gFile04.exists()) {
  112. executeSoon(waitForFileDeletion);
  113. return;
  114. }
  115. gFile04 = null;
  116. gScratchpad.openFile(0);
  117. }
  118. // By now we should have two recent files stored in the list but one of the
  119. // files should be missing on the harddrive.
  120. function testOpenDeletedFile() {
  121. let doc = gScratchpadWindow.document;
  122. let popup = doc.getElementById("sp-menu-open_recentPopup");
  123. is(gScratchpad.getRecentFiles().length, 1,
  124. "The missing file was successfully removed from the list.");
  125. // The number of recent files stored, plus the separator and the
  126. // clearRecentMenuItems-item.
  127. is(popup.children.length, 3,
  128. "The missing file was successfully removed from the menu.");
  129. ok(gScratchpad.notificationBox.currentNotification,
  130. "The notification was successfully displayed.");
  131. is(gScratchpad.notificationBox.currentNotification.label,
  132. gScratchpad.strings.GetStringFromName("fileNoLongerExists.notification"),
  133. "The notification label is correct.");
  134. gScratchpad.clearRecentFiles();
  135. }
  136. // We have cleared the last file. Test to see if the last file was removed,
  137. // the menu is empty and was disabled successfully.
  138. function testClearedAll()
  139. {
  140. let doc = gScratchpadWindow.document;
  141. let menu = doc.getElementById("sp-open_recent-menu");
  142. let popup = doc.getElementById("sp-menu-open_recentPopup");
  143. is(gScratchpad.getRecentFiles().length, 0,
  144. "All recent files removed successfully.");
  145. is(popup.children.length, 0, "All menuitems removed successfully.");
  146. ok(menu.hasAttribute("disabled"),
  147. "No files in the menu, it was disabled successfully.");
  148. finishTest();
  149. }
  150. function createAndLoadTemporaryFile(aFile, aFileName, aFileContent)
  151. {
  152. // Create a temporary file.
  153. aFile = FileUtils.getFile("TmpD", [aFileName]);
  154. aFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
  155. // Write the temporary file.
  156. let fout = Cc["@mozilla.org/network/file-output-stream;1"].
  157. createInstance(Ci.nsIFileOutputStream);
  158. fout.init(aFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
  159. 0o644, fout.DEFER_OPEN);
  160. gScratchpad.setFilename(aFile.path);
  161. gScratchpad.importFromFile(aFile.QueryInterface(Ci.nsILocalFile), true,
  162. fileImported);
  163. gScratchpad.saveFile(fileSaved);
  164. return aFile;
  165. }
  166. function fileImported(aStatus)
  167. {
  168. ok(Components.isSuccessCode(aStatus),
  169. "the temporary file was imported successfully with Scratchpad");
  170. }
  171. function fileSaved(aStatus)
  172. {
  173. ok(Components.isSuccessCode(aStatus),
  174. "the temporary file was saved successfully with Scratchpad");
  175. checkIfMenuIsPopulated();
  176. }
  177. function checkIfMenuIsPopulated()
  178. {
  179. let doc = gScratchpadWindow.document;
  180. let expectedMenuitemCount = doc.getElementById("sp-menu-open_recentPopup").
  181. children.length;
  182. // The number of recent files stored, plus the separator and the
  183. // clearRecentMenuItems-item.
  184. let recentFilesPlusExtra = gScratchpad.getRecentFiles().length + 2;
  185. if (expectedMenuitemCount > 2) {
  186. is(expectedMenuitemCount, recentFilesPlusExtra,
  187. "the recent files menu was populated successfully.");
  188. }
  189. }
  190. /**
  191. * The PreferenceObserver listens for preference changes while Scratchpad is
  192. * running.
  193. */
  194. var PreferenceObserver = {
  195. _initialized: false,
  196. _timesFired: 0,
  197. set timesFired(aNewValue) {
  198. this._timesFired = aNewValue;
  199. },
  200. get timesFired() {
  201. return this._timesFired;
  202. },
  203. init: function PO_init()
  204. {
  205. if (this._initialized) {
  206. return;
  207. }
  208. this.branch = Services.prefs.getBranch("devtools.scratchpad.");
  209. this.branch.addObserver("", this, false);
  210. this._initialized = true;
  211. },
  212. observe: function PO_observe(aMessage, aTopic, aData)
  213. {
  214. if (aTopic != "nsPref:changed") {
  215. return;
  216. }
  217. switch (this.timesFired) {
  218. case 0:
  219. this.timesFired = 1;
  220. break;
  221. case 1:
  222. this.timesFired = 2;
  223. break;
  224. case 2:
  225. this.timesFired = 3;
  226. testAddedToRecent();
  227. break;
  228. case 3:
  229. this.timesFired = 4;
  230. testOverwriteRecent();
  231. break;
  232. case 4:
  233. this.timesFired = 5;
  234. testOpenOldestRecent();
  235. break;
  236. case 5:
  237. this.timesFired = 6;
  238. testHideMenu();
  239. break;
  240. case 6:
  241. this.timesFired = 7;
  242. testChangedMaxRecent();
  243. break;
  244. case 7:
  245. this.timesFired = 8;
  246. testOpenDeletedFile();
  247. break;
  248. case 8:
  249. this.timesFired = 9;
  250. testClearedAll();
  251. break;
  252. }
  253. },
  254. uninit: function PO_uninit() {
  255. this.branch.removeObserver("", this);
  256. }
  257. };
  258. function test()
  259. {
  260. waitForExplicitFinish();
  261. registerCleanupFunction(function () {
  262. gFile01.remove(false);
  263. gFile01 = null;
  264. gFile02.remove(false);
  265. gFile02 = null;
  266. gFile03.remove(false);
  267. gFile03 = null;
  268. // gFile04 was removed earlier.
  269. lists.recentFiles01 = null;
  270. lists.recentFiles02 = null;
  271. lists.recentFiles03 = null;
  272. lists.recentFiles04 = null;
  273. gScratchpad = null;
  274. PreferenceObserver.uninit();
  275. Services.prefs.clearUserPref("devtools.scratchpad.recentFilesMax");
  276. });
  277. Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 3);
  278. // Initiate the preference observer after we have set the temporary recent
  279. // files max for this test.
  280. PreferenceObserver.init();
  281. gBrowser.selectedTab = gBrowser.addTab();
  282. gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
  283. gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
  284. openScratchpad(startTest);
  285. }, true);
  286. content.location = "data:text/html,<p>test recent files in Scratchpad";
  287. }
  288. function finishTest()
  289. {
  290. finish();
  291. }