browser_dbg-pretty-print-sourcemap.js (5578B)
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 // This tests pretty-printing for sourcemapped files. 6 7 "use strict"; 8 9 const httpServer = createTestHTTPServer(); 10 httpServer.registerContentType("html", "text/html"); 11 httpServer.registerContentType("js", "application/javascript"); 12 13 httpServer.registerPathHandler( 14 "/doc-prettyprint-sourcemap.html", 15 (request, response) => { 16 response.setStatusLine(request.httpVersion, 200, "OK"); 17 response.write(`<!DOCTYPE html> 18 <html> 19 <head> 20 <script type="text/javascript" src="/js1.min.js"></script> 21 <script type="text/javascript" src="/js2.min.js"></script> 22 </head> 23 <script> 24 const a = 3; 25 console.log(a); 26 </script> 27 </html> 28 `); 29 } 30 ); 31 32 function sourceHandler(request, response) { 33 response.setHeader("Content-Type", "text/javascript"); 34 console.log(request.path.substring(1)); 35 response.write(`function add(a,b,k){var result=a+b;return k(result)}function sub(a,b,k){var result=a-b;return k(result)}function mul(a,b,k){var result=a*b;return k(result)}function div(a,b,k){var result=a/b;return k(result)}function arithmetic(){add(4,4,function(a){sub(a,2,function(b){mul(b,3,function(c){div(c,2,function(d){console.log("arithmetic",d)})})})})}; 36 //# sourceMappingURL=${request.path.substring(1)}.map`); 37 } 38 39 httpServer.registerPathHandler("/js1.min.js", sourceHandler); 40 httpServer.registerPathHandler("/js2.min.js", sourceHandler); 41 42 // This returns no sourcemap 43 httpServer.registerPathHandler("/js1.min.js.map", (request, response) => { 44 response.setHeader("Content-Type", "application/javascript"); 45 response.setStatusLine(request.httpVersion, 404, "Not found"); 46 response.write(`console.log("http error")`); 47 }); 48 49 // This returns a sourcemap without any original source 50 httpServer.registerPathHandler("/js2.min.js.map", (request, response) => { 51 response.setHeader("Content-Type", "application/javascript"); 52 response.setStatusLine(request.httpVersion, 200, "OK"); 53 response.write( 54 `{"version":3,"sources":[],"names":["message","document","getElementById","logMessage","console","log","value","addEventListener","logMessageButton"],"mappings":"AAAA,GAAIA,SAAUC,SAASC,eAAe,UAEtC,SAASC,cACPC,QAAQC,IAAI,eAAiBL,QAAQM,OAGvCN,QAAQO,iBAAiB,QAAS,WAChCP,QAAQM,MAAQ,IACf,MAEH,IAAIE,kBAAmBP,SAASC,eAAe,cAE/CM,kBAAiBD,iBAAiB,QAAS,WACzCJ,cACC"}` 55 ); 56 }); 57 58 const BASE_URL = `http://localhost:${httpServer.identity.primaryPort}/`; 59 60 // Tests that the pretty-print icon is visible for source files with a sourceMappingURL 61 // but the linked sourcemap is not found or no original files exist. 62 add_task(async () => { 63 const dbg = await initDebuggerWithAbsoluteURL( 64 `${BASE_URL}doc-prettyprint-sourcemap.html`, 65 "doc-prettyprint-sourcemap.html", 66 "js1.min.js", 67 "js2.min.js" 68 ); 69 70 info(" - Test HTML source"); 71 const htmlSource = findSource(dbg, "doc-prettyprint-sourcemap.html"); 72 await selectSource(dbg, htmlSource); 73 assertPrettyPrintButton( 74 dbg, 75 DEBUGGER_L10N.getStr("sourceTabs.prettyPrint"), 76 false 77 ); 78 79 info(" - Test source with sourceMappingURL but sourcemap does not exist"); 80 const source1 = findSource(dbg, "js1.min.js"); 81 await selectSource(dbg, source1); 82 83 // The source may be reported as pretty printable while we are fetching the sourcemap, 84 // but once the sourcemap is reported to be failing, we no longer report to be pretty printable. 85 await waitFor( 86 () => !dbg.selectors.isSourceWithMap(source1.id), 87 "Wait for the selector to report the source to be source-map less" 88 ); 89 90 assertPrettyPrintButton( 91 dbg, 92 DEBUGGER_L10N.getStr("sourceTabs.prettyPrint"), 93 false 94 ); 95 96 info( 97 " - Test source with sourceMappingURL and sourcemap but no original files" 98 ); 99 const source2 = findSource(dbg, "js2.min.js"); 100 await selectSource(dbg, source2); 101 102 assertPrettyPrintButton( 103 dbg, 104 DEBUGGER_L10N.getStr("sourceTabs.prettyPrint"), 105 false 106 ); 107 108 info(" - Test an already pretty-printed source"); 109 info("Pretty print the script"); 110 clickElement(dbg, "prettyPrintButton"); 111 112 await waitForSelectedSource(dbg, "js2.min.js:formatted"); 113 assertPrettyPrintButton( 114 dbg, 115 DEBUGGER_L10N.getStr("sourceTabs.removePrettyPrint"), 116 false 117 ); 118 }); 119 120 add_task(async () => { 121 const dbg = await initDebugger( 122 "doc-sourcemaps2.html", 123 "main.min.js", 124 "main.js" 125 ); 126 127 info( 128 " - Test source with sourceMappingURL, sourcemap and original files exist" 129 ); 130 const source = findSource(dbg, "main.min.js"); 131 await selectSource(dbg, source); 132 133 assertPrettyPrintButton( 134 dbg, 135 DEBUGGER_L10N.getStr("sourceFooter.prettyPrint.hasSourceMapMessage"), 136 true 137 ); 138 139 info(" - Test an original source"); 140 const originalSource = findSource(dbg, "main.js"); 141 await selectSource(dbg, originalSource); 142 143 assertPrettyPrintButton( 144 dbg, 145 DEBUGGER_L10N.getStr("sourceFooter.prettyPrint.isOriginalMessage"), 146 true 147 ); 148 }); 149 150 function assertPrettyPrintButton(dbg, expectedTitle, shouldBeDisabled) { 151 const prettyPrintButton = findElement(dbg, "prettyPrintButton"); 152 ok(prettyPrintButton, "Pretty Print Button is visible"); 153 is( 154 prettyPrintButton.title, 155 expectedTitle, 156 "The pretty print button title should be correct" 157 ); 158 ok( 159 shouldBeDisabled ? prettyPrintButton.disabled : !prettyPrintButton.disabled, 160 `The pretty print button should be ${ 161 shouldBeDisabled ? "disabled" : "enabled" 162 }` 163 ); 164 }