browser_dbg_sources-cache.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. /**
  5. * Tests if the sources cache knows how to cache sources when prompted.
  6. */
  7. const TAB_URL = EXAMPLE_URL + "doc_function-search.html";
  8. const TOTAL_SOURCES = 4;
  9. function test() {
  10. let options = {
  11. source: EXAMPLE_URL + "code_function-search-01.js",
  12. line: 1
  13. };
  14. initDebugger(TAB_URL, options).then(([aTab, aDebuggee, aPanel]) => {
  15. const gTab = aTab;
  16. const gDebuggee = aDebuggee;
  17. const gPanel = aPanel;
  18. const gDebugger = gPanel.panelWin;
  19. const gEditor = gDebugger.DebuggerView.editor;
  20. const gSources = gDebugger.DebuggerView.Sources;
  21. const gPrevLabelsCache = gDebugger.SourceUtils._labelsCache;
  22. const gPrevGroupsCache = gDebugger.SourceUtils._groupsCache;
  23. const getState = gDebugger.DebuggerController.getState;
  24. const queries = gDebugger.require("./content/queries");
  25. const actions = bindActionCreators(gPanel);
  26. function initialChecks() {
  27. ok(gEditor.getText().includes("First source!"),
  28. "Editor text contents appears to be correct.");
  29. is(gSources.selectedItem.attachment.label, "code_function-search-01.js",
  30. "The currently selected label in the sources container is correct.");
  31. ok(getSelectedSourceURL(gSources).includes("code_function-search-01.js"),
  32. "The currently selected value in the sources container appears to be correct.");
  33. is(gSources.itemCount, TOTAL_SOURCES,
  34. "There should be " + TOTAL_SOURCES + " sources present in the sources list.");
  35. is(gSources.visibleItems.length, TOTAL_SOURCES,
  36. "There should be " + TOTAL_SOURCES + " sources visible in the sources list.");
  37. is(gSources.attachments.length, TOTAL_SOURCES,
  38. "There should be " + TOTAL_SOURCES + " attachments stored in the sources container model.");
  39. is(gSources.values.length, TOTAL_SOURCES,
  40. "There should be " + TOTAL_SOURCES + " values stored in the sources container model.");
  41. info("Source labels: " + gSources.attachments.toSource());
  42. info("Source values: " + gSources.values.toSource());
  43. is(gSources.attachments[0].label, "code_function-search-01.js",
  44. "The first source label is correct.");
  45. ok(gSources.attachments[0].source.url.includes("code_function-search-01.js"),
  46. "The first source value appears to be correct.");
  47. is(gSources.attachments[1].label, "code_function-search-02.js",
  48. "The second source label is correct.");
  49. ok(gSources.attachments[1].source.url.includes("code_function-search-02.js"),
  50. "The second source value appears to be correct.");
  51. is(gSources.attachments[2].label, "code_function-search-03.js",
  52. "The third source label is correct.");
  53. ok(gSources.attachments[2].source.url.includes("code_function-search-03.js"),
  54. "The third source value appears to be correct.");
  55. is(gSources.attachments[3].label, "doc_function-search.html",
  56. "The third source label is correct.");
  57. ok(gSources.attachments[3].source.url.includes("doc_function-search.html"),
  58. "The third source value appears to be correct.");
  59. is(gDebugger.SourceUtils._labelsCache.size, TOTAL_SOURCES,
  60. "There should be " + TOTAL_SOURCES + " labels cached.");
  61. is(gDebugger.SourceUtils._groupsCache.size, TOTAL_SOURCES,
  62. "There should be " + TOTAL_SOURCES + " groups cached.");
  63. }
  64. function performReloadAndTestState() {
  65. gDebugger.gTarget.once("will-navigate", testStateBeforeReload);
  66. gDebugger.gTarget.once("navigate", testStateAfterReload);
  67. return reloadActiveTab(gPanel, gDebugger.EVENTS.SOURCE_SHOWN);
  68. }
  69. function testCacheIntegrity(cachedSources) {
  70. const contents = {
  71. [EXAMPLE_URL + "code_function-search-01.js"]: "First source!",
  72. [EXAMPLE_URL + "code_function-search-02.js"]: "Second source!",
  73. [EXAMPLE_URL + "code_function-search-03.js"]: "Third source!",
  74. [EXAMPLE_URL + "doc_function-search.html"]: "Peanut butter jelly time!"
  75. };
  76. const sourcesText = getState().sources.sourcesText;
  77. is(Object.keys(sourcesText).length, cachedSources.length,
  78. "The right number of sources is cached");
  79. cachedSources.forEach(sourceUrl => {
  80. const source = queries.getSourceByURL(getState(), EXAMPLE_URL + sourceUrl);
  81. const content = queries.getSourceText(getState(), source.actor);
  82. ok(content, "Source text is cached");
  83. ok(content.text.includes(contents[source.url]), "Source text is correct");
  84. });
  85. }
  86. function fetchAllSources() {
  87. const sources = queries.getSources(getState());
  88. return Promise.all(Object.keys(sources).map(k => {
  89. const source = sources[k];
  90. return actions.loadSourceText(source);
  91. }));
  92. }
  93. function testStateBeforeReload() {
  94. is(gSources.itemCount, 0,
  95. "There should be no sources present in the sources list during reload.");
  96. is(gDebugger.SourceUtils._labelsCache, gPrevLabelsCache,
  97. "The labels cache has been refreshed during reload and no new objects were created.");
  98. is(gDebugger.SourceUtils._groupsCache, gPrevGroupsCache,
  99. "The groups cache has been refreshed during reload and no new objects were created.");
  100. is(gDebugger.SourceUtils._labelsCache.size, 0,
  101. "There should be no labels cached during reload");
  102. is(gDebugger.SourceUtils._groupsCache.size, 0,
  103. "There should be no groups cached during reload");
  104. }
  105. function testStateAfterReload() {
  106. is(gSources.itemCount, TOTAL_SOURCES,
  107. "There should be " + TOTAL_SOURCES + " sources present in the sources list.");
  108. is(gDebugger.SourceUtils._labelsCache.size, TOTAL_SOURCES,
  109. "There should be " + TOTAL_SOURCES + " labels cached after reload.");
  110. is(gDebugger.SourceUtils._groupsCache.size, TOTAL_SOURCES,
  111. "There should be " + TOTAL_SOURCES + " groups cached after reload.");
  112. }
  113. Task.spawn(function* () {
  114. yield initialChecks();
  115. yield testCacheIntegrity(["code_function-search-01.js"]);
  116. yield fetchAllSources();
  117. yield testCacheIntegrity([
  118. "code_function-search-01.js",
  119. "code_function-search-02.js",
  120. "code_function-search-03.js",
  121. "doc_function-search.html"
  122. ]);
  123. yield performReloadAndTestState();
  124. closeDebuggerAndFinish(gPanel);
  125. });
  126. });
  127. }