summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_sources-cache.js
blob: 2c5d9d0e2e7b8c01565af8b566316ca3661aea5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests if the sources cache knows how to cache sources when prompted.
 */

const TAB_URL = EXAMPLE_URL + "doc_function-search.html";
const TOTAL_SOURCES = 4;

function test() {
  let options = {
    source: EXAMPLE_URL + "code_function-search-01.js",
    line: 1
  };
  initDebugger(TAB_URL, options).then(([aTab, aDebuggee, aPanel]) => {
    const gTab = aTab;
    const gDebuggee = aDebuggee;
    const gPanel = aPanel;
    const gDebugger = gPanel.panelWin;
    const gEditor = gDebugger.DebuggerView.editor;
    const gSources = gDebugger.DebuggerView.Sources;
    const gPrevLabelsCache = gDebugger.SourceUtils._labelsCache;
    const gPrevGroupsCache = gDebugger.SourceUtils._groupsCache;
    const getState = gDebugger.DebuggerController.getState;
    const queries = gDebugger.require("./content/queries");
    const actions = bindActionCreators(gPanel);

    function initialChecks() {
      ok(gEditor.getText().includes("First source!"),
         "Editor text contents appears to be correct.");
      is(gSources.selectedItem.attachment.label, "code_function-search-01.js",
         "The currently selected label in the sources container is correct.");
      ok(getSelectedSourceURL(gSources).includes("code_function-search-01.js"),
         "The currently selected value in the sources container appears to be correct.");

      is(gSources.itemCount, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " sources present in the sources list.");
      is(gSources.visibleItems.length, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " sources visible in the sources list.");
      is(gSources.attachments.length, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " attachments stored in the sources container model.");
      is(gSources.values.length, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " values stored in the sources container model.");

      info("Source labels: " + gSources.attachments.toSource());
      info("Source values: " + gSources.values.toSource());

      is(gSources.attachments[0].label, "code_function-search-01.js",
         "The first source label is correct.");
      ok(gSources.attachments[0].source.url.includes("code_function-search-01.js"),
         "The first source value appears to be correct.");

      is(gSources.attachments[1].label, "code_function-search-02.js",
         "The second source label is correct.");
      ok(gSources.attachments[1].source.url.includes("code_function-search-02.js"),
         "The second source value appears to be correct.");

      is(gSources.attachments[2].label, "code_function-search-03.js",
         "The third source label is correct.");
      ok(gSources.attachments[2].source.url.includes("code_function-search-03.js"),
         "The third source value appears to be correct.");

      is(gSources.attachments[3].label, "doc_function-search.html",
         "The third source label is correct.");
      ok(gSources.attachments[3].source.url.includes("doc_function-search.html"),
         "The third source value appears to be correct.");

      is(gDebugger.SourceUtils._labelsCache.size, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " labels cached.");
      is(gDebugger.SourceUtils._groupsCache.size, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " groups cached.");
    }

    function performReloadAndTestState() {
      gDebugger.gTarget.once("will-navigate", testStateBeforeReload);
      gDebugger.gTarget.once("navigate", testStateAfterReload);
      return reloadActiveTab(gPanel, gDebugger.EVENTS.SOURCE_SHOWN);
    }

    function testCacheIntegrity(cachedSources) {
      const contents = {
        [EXAMPLE_URL + "code_function-search-01.js"]: "First source!",
        [EXAMPLE_URL + "code_function-search-02.js"]: "Second source!",
        [EXAMPLE_URL + "code_function-search-03.js"]: "Third source!",
        [EXAMPLE_URL + "doc_function-search.html"]: "Peanut butter jelly time!"
      };

      const sourcesText = getState().sources.sourcesText;
      is(Object.keys(sourcesText).length, cachedSources.length,
         "The right number of sources is cached");

      cachedSources.forEach(sourceUrl => {
        const source = queries.getSourceByURL(getState(), EXAMPLE_URL + sourceUrl);
        const content = queries.getSourceText(getState(), source.actor);
        ok(content, "Source text is cached");
        ok(content.text.includes(contents[source.url]), "Source text is correct");
      });
    }

    function fetchAllSources() {
      const sources = queries.getSources(getState());
      return Promise.all(Object.keys(sources).map(k => {
        const source = sources[k];
        return actions.loadSourceText(source);
      }));
    }

    function testStateBeforeReload() {
      is(gSources.itemCount, 0,
         "There should be no sources present in the sources list during reload.");
      is(gDebugger.SourceUtils._labelsCache, gPrevLabelsCache,
         "The labels cache has been refreshed during reload and no new objects were created.");
      is(gDebugger.SourceUtils._groupsCache, gPrevGroupsCache,
         "The groups cache has been refreshed during reload and no new objects were created.");
      is(gDebugger.SourceUtils._labelsCache.size, 0,
         "There should be no labels cached during reload");
      is(gDebugger.SourceUtils._groupsCache.size, 0,
         "There should be no groups cached during reload");
    }

    function testStateAfterReload() {
      is(gSources.itemCount, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " sources present in the sources list.");
      is(gDebugger.SourceUtils._labelsCache.size, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " labels cached after reload.");
      is(gDebugger.SourceUtils._groupsCache.size, TOTAL_SOURCES,
         "There should be " + TOTAL_SOURCES + " groups cached after reload.");
    }

    Task.spawn(function* () {
      yield initialChecks();
      yield testCacheIntegrity(["code_function-search-01.js"]);
      yield fetchAllSources();
      yield testCacheIntegrity([
        "code_function-search-01.js",
        "code_function-search-02.js",
        "code_function-search-03.js",
        "doc_function-search.html"
      ]);
      yield performReloadAndTestState();
      closeDebuggerAndFinish(gPanel);
    });
  });
}