browser_dbg-content-script-sources.js (4490B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 // Tests that the content scripts are listed in the source tree. 6 7 "use strict"; 8 9 add_task(async function () { 10 await pushPref("devtools.debugger.show-content-scripts", false); 11 12 const extension = await installAndStartContentScriptExtension(); 13 14 const otherExtension = ExtensionTestUtils.loadExtension({ 15 manifest: { 16 name: "Other extension", 17 content_scripts: [ 18 { 19 js: ["other_content_script.js"], 20 matches: ["https://example.com/*"], 21 run_at: "document_idle", 22 }, 23 ], 24 }, 25 files: { 26 "other_content_script.js": "// This one does nothing", 27 }, 28 }); 29 await otherExtension.startup(); 30 31 let dbg = await initDebugger("doc-content-script-sources.html"); 32 ok( 33 !sourceExists(dbg, "content_script.js"), 34 "The extension content script isn't reported to the frontend and isn't in the reducer" 35 ); 36 37 info("But the content script isn't visible by default"); 38 await waitForSourcesInSourceTree(dbg, [ 39 "doc-content-script-sources.html", 40 "doc-strict.html", 41 ]); 42 43 info("Enable the content script setting"); 44 await toggleSourcesTreeSettingsMenuItem(dbg, { 45 className: ".debugger-settings-menu-item-show-content-scripts", 46 isChecked: false, 47 }); 48 49 info("The extension content script should now be visible in the source tree"); 50 await waitForSourcesInSourceTree(dbg, [ 51 "doc-content-script-sources.html", 52 "doc-strict.html", 53 "content_script.js", 54 "other_content_script.js", 55 ]); 56 is(dbg.selectors.getSourceCount(), 4, "There are only three sources"); 57 58 await waitForSources(dbg, "content_script.js"); 59 await selectSource(dbg, "content_script.js"); 60 await closeTab(dbg, "content_script.js"); 61 62 const sourceTreeThreadLabels = [ 63 ...findAllElements(dbg, "sourceTreeThreads"), 64 ].map(el => { 65 return el.textContent; 66 }); 67 // Verify that the content script is below the target of the frame it was executed against 68 Assert.deepEqual( 69 sourceTreeThreadLabels, 70 [ 71 "Main Thread", 72 "Other extension", 73 "Test content script extension", 74 "Debugger test page", 75 ], 76 "The threads are correctly ordered" 77 ); 78 const threadPanelLabels = [...findAllElements(dbg, "threadsPaneItems")].map( 79 el => { 80 return el.textContent; 81 } 82 ); 83 // Verify that we get the exact same ordering in the threads panel 84 Assert.deepEqual( 85 threadPanelLabels, 86 sourceTreeThreadLabels, 87 "The threads are correctly ordered in the threads panel" 88 ); 89 90 // Destroy the toolbox and repeat the test in a new toolbox 91 // and ensures that the content script is still listed. 92 await dbg.toolbox.destroy(); 93 94 const toolbox = await openToolboxForTab(gBrowser.selectedTab, "jsdebugger"); 95 dbg = createDebuggerContext(toolbox); 96 await waitForSources(dbg, "content_script.js"); 97 await selectSource(dbg, "content_script.js"); 98 99 await addBreakpoint(dbg, "content_script.js", 2); 100 101 for (let i = 1; i < 3; i++) { 102 info(`Reloading tab (${i} time)`); 103 gBrowser.reloadTab(gBrowser.selectedTab); 104 await waitForPaused(dbg); 105 await waitForSelectedSource(dbg, "content_script.js"); 106 await waitFor( 107 () => findElementWithSelector(dbg, ".sources-list .focused"), 108 "Source is focused" 109 ); 110 await assertPausedAtSourceAndLine( 111 dbg, 112 findSource(dbg, "content_script.js").id, 113 2 114 ); 115 116 const pausedThread = dbg.selectors.getCurrentThread(); 117 await stepIn(dbg); 118 is( 119 dbg.selectors.getCurrentThread(), 120 pausedThread, 121 "We step in the same thread" 122 ); 123 await assertPausedAtSourceAndLine( 124 dbg, 125 findSource(dbg, "content_script.js").id, 126 7 127 ); 128 129 await resume(dbg); 130 } 131 132 await resume(dbg); 133 assertNotPaused(dbg); 134 135 await closeTab(dbg, "content_script.js"); 136 137 await waitFor( 138 () => dbg.selectors.getAllThreads().length == 3, 139 "Ensure that we only get the main thread and the two content scripts thread" 140 ); 141 await waitFor( 142 () => dbg.selectors.getSourceCount() == 4, 143 "There are only three sources" 144 ); 145 146 await extension.unload(); 147 await otherExtension.unload(); 148 await waitFor( 149 () => dbg.selectors.getAllThreads().length == 2, 150 "After unloading the add-on, the content script thread is removed, but there is still two html documents" 151 ); 152 });