1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/debugger/test/browser_dbg_sources-cache.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * Tests if the sources cache knows how to cache sources when prompted. 1.9 + */ 1.10 + 1.11 +const TAB_URL = EXAMPLE_URL + "doc_function-search.html"; 1.12 +const TOTAL_SOURCES = 4; 1.13 + 1.14 +let gTab, gDebuggee, gPanel, gDebugger; 1.15 +let gEditor, gSources, gControllerSources; 1.16 +let gPrevLabelsCache, gPrevGroupsCache; 1.17 + 1.18 +function test() { 1.19 + initDebugger(TAB_URL).then(([aTab, aDebuggee, aPanel]) => { 1.20 + gTab = aTab; 1.21 + gDebuggee = aDebuggee; 1.22 + gPanel = aPanel; 1.23 + gDebugger = gPanel.panelWin; 1.24 + gEditor = gDebugger.DebuggerView.editor; 1.25 + gSources = gDebugger.DebuggerView.Sources; 1.26 + gControllerSources = gDebugger.DebuggerController.SourceScripts; 1.27 + gPrevLabelsCache = gDebugger.SourceUtils._labelsCache; 1.28 + gPrevGroupsCache = gDebugger.SourceUtils._groupsCache; 1.29 + 1.30 + waitForSourceShown(gPanel, "-01.js") 1.31 + .then(initialChecks) 1.32 + .then(getTextForSourcesAndCheckIntegrity) 1.33 + .then(performReloadAndTestState) 1.34 + .then(() => closeDebuggerAndFinish(gPanel)) 1.35 + .then(null, aError => { 1.36 + ok(false, "Got an error: " + aError.message + "\n" + aError.stack); 1.37 + }); 1.38 + }); 1.39 +} 1.40 + 1.41 +function initialChecks() { 1.42 + ok(gEditor.getText().contains("First source!"), 1.43 + "Editor text contents appears to be correct."); 1.44 + is(gSources.selectedItem.attachment.label, "code_function-search-01.js", 1.45 + "The currently selected label in the sources container is correct."); 1.46 + ok(gSources.selectedValue.contains("code_function-search-01.js"), 1.47 + "The currently selected value in the sources container appears to be correct."); 1.48 + 1.49 + is(gSources.itemCount, TOTAL_SOURCES, 1.50 + "There should be " + TOTAL_SOURCES + " sources present in the sources list."); 1.51 + is(gSources.visibleItems.length, TOTAL_SOURCES, 1.52 + "There should be " + TOTAL_SOURCES + " sources visible in the sources list."); 1.53 + is(gSources.attachments.length, TOTAL_SOURCES, 1.54 + "There should be " + TOTAL_SOURCES + " attachments stored in the sources container model.") 1.55 + is(gSources.values.length, TOTAL_SOURCES, 1.56 + "There should be " + TOTAL_SOURCES + " values stored in the sources container model.") 1.57 + 1.58 + info("Source labels: " + gSources.attachments.toSource()); 1.59 + info("Source values: " + gSources.values.toSource()); 1.60 + 1.61 + is(gSources.attachments[0].label, "code_function-search-01.js", 1.62 + "The first source label is correct."); 1.63 + ok(gSources.values[0].contains("code_function-search-01.js"), 1.64 + "The first source value appears to be correct."); 1.65 + 1.66 + is(gSources.attachments[1].label, "code_function-search-02.js", 1.67 + "The second source label is correct."); 1.68 + ok(gSources.values[1].contains("code_function-search-02.js"), 1.69 + "The second source value appears to be correct."); 1.70 + 1.71 + is(gSources.attachments[2].label, "code_function-search-03.js", 1.72 + "The third source label is correct."); 1.73 + ok(gSources.values[2].contains("code_function-search-03.js"), 1.74 + "The third source value appears to be correct."); 1.75 + 1.76 + is(gSources.attachments[3].label, "doc_function-search.html", 1.77 + "The third source label is correct."); 1.78 + ok(gSources.values[3].contains("doc_function-search.html"), 1.79 + "The third source value appears to be correct."); 1.80 + 1.81 + is(gDebugger.SourceUtils._labelsCache.size, TOTAL_SOURCES, 1.82 + "There should be " + TOTAL_SOURCES + " labels cached."); 1.83 + is(gDebugger.SourceUtils._groupsCache.size, TOTAL_SOURCES, 1.84 + "There should be " + TOTAL_SOURCES + " groups cached."); 1.85 +} 1.86 + 1.87 +function getTextForSourcesAndCheckIntegrity() { 1.88 + return gControllerSources.getTextForSources(gSources.values).then(testCacheIntegrity); 1.89 +} 1.90 + 1.91 +function performReloadAndTestState() { 1.92 + gDebugger.gTarget.once("will-navigate", testStateBeforeReload); 1.93 + gDebugger.gTarget.once("navigate", testStateAfterReload); 1.94 + return reloadActiveTab(gPanel, gDebugger.EVENTS.SOURCE_SHOWN); 1.95 +} 1.96 + 1.97 +function testCacheIntegrity(aSources) { 1.98 + for (let [url, contents] of aSources) { 1.99 + // Sources of a debugee don't always finish fetching consecutively. D'uh. 1.100 + let index = gSources.values.indexOf(url); 1.101 + 1.102 + ok(index >= 0 && index <= TOTAL_SOURCES, 1.103 + "Found a source url cached correctly (" + index + ")."); 1.104 + ok(contents.contains( 1.105 + ["First source!", "Second source!", "Third source!", "Peanut butter jelly time!"][index]), 1.106 + "Found a source's text contents cached correctly (" + index + ")."); 1.107 + 1.108 + info("Cached source url at " + index + ": " + url); 1.109 + info("Cached source text at " + index + ": " + contents); 1.110 + } 1.111 +} 1.112 + 1.113 +function testStateBeforeReload() { 1.114 + is(gSources.itemCount, 0, 1.115 + "There should be no sources present in the sources list during reload."); 1.116 + is(gDebugger.SourceUtils._labelsCache, gPrevLabelsCache, 1.117 + "The labels cache has been refreshed during reload and no new objects were created."); 1.118 + is(gDebugger.SourceUtils._groupsCache, gPrevGroupsCache, 1.119 + "The groups cache has been refreshed during reload and no new objects were created."); 1.120 + is(gDebugger.SourceUtils._labelsCache.size, 0, 1.121 + "There should be no labels cached during reload"); 1.122 + is(gDebugger.SourceUtils._groupsCache.size, 0, 1.123 + "There should be no groups cached during reload"); 1.124 +} 1.125 + 1.126 +function testStateAfterReload() { 1.127 + is(gSources.itemCount, TOTAL_SOURCES, 1.128 + "There should be " + TOTAL_SOURCES + " sources present in the sources list."); 1.129 + is(gDebugger.SourceUtils._labelsCache.size, TOTAL_SOURCES, 1.130 + "There should be " + TOTAL_SOURCES + " labels cached after reload."); 1.131 + is(gDebugger.SourceUtils._groupsCache.size, TOTAL_SOURCES, 1.132 + "There should be " + TOTAL_SOURCES + " groups cached after reload."); 1.133 +} 1.134 + 1.135 +registerCleanupFunction(function() { 1.136 + gTab = null; 1.137 + gDebuggee = null; 1.138 + gPanel = null; 1.139 + gDebugger = null; 1.140 + gEditor = null; 1.141 + gSources = null; 1.142 + gControllerSources = null; 1.143 + gPrevLabelsCache = null; 1.144 + gPrevGroupsCache = null; 1.145 +});