browser_inspector-iframe.js (2820B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const BYPASS_WALKERFRONT_CHILDREN_IFRAME_GUARD_PREF = 7 "devtools.testing.bypass-walker-children-iframe-guard"; 8 9 add_task(async function testIframe() { 10 info("Check that dedicated walker is used for retrieving iframe children"); 11 12 const TEST_URI = `https://example.com/document-builder.sjs?html=${encodeURIComponent(` 13 <h1>Test iframe</h1> 14 <iframe src="https://example.com/document-builder.sjs?html=Hello"></iframe> 15 `)}`; 16 17 const { walker } = await initInspectorFront(TEST_URI); 18 const iframeNodeFront = await walker.querySelector(walker.rootNode, "iframe"); 19 20 is( 21 iframeNodeFront.useChildTargetToFetchChildren, 22 true, 23 "useChildTargetToFetchChildren has expected value" 24 ); 25 is( 26 iframeNodeFront.numChildren, 27 1, 28 "numChildren is set to 1 (for the #document node)" 29 ); 30 31 const res = await walker.children(iframeNodeFront); 32 is( 33 res.nodes.length, 34 1, 35 "Retrieving the iframe children return an array with one element" 36 ); 37 const documentNodeFront = res.nodes[0]; 38 is( 39 documentNodeFront.nodeName, 40 "#document", 41 "The child is the #document element" 42 ); 43 Assert.notStrictEqual( 44 documentNodeFront.walkerFront, 45 walker, 46 "The child walker is different from the top level document one" 47 ); 48 is( 49 documentNodeFront.parentNode(), 50 iframeNodeFront, 51 "The child parent was set to the original iframe nodeFront" 52 ); 53 }); 54 55 add_task(async function testIframeBlockedByCSP() { 56 info("Check that iframe blocked by CSP don't have any children"); 57 58 const TEST_URI = `https://example.com/document-builder.sjs?html=${encodeURIComponent(` 59 <h1>Test CSP-blocked iframe</h1> 60 <iframe src="https://example.org/document-builder.sjs?html=Hello"></iframe> 61 `)}&headers=content-security-policy:default-src 'self'`; 62 63 const { walker } = await initInspectorFront(TEST_URI); 64 const iframeNodeFront = await walker.querySelector(walker.rootNode, "iframe"); 65 66 is( 67 iframeNodeFront.useChildTargetToFetchChildren, 68 false, 69 "useChildTargetToFetchChildren is false" 70 ); 71 is(iframeNodeFront.numChildren, 0, "numChildren is set to 0"); 72 73 info("Test calling WalkerFront#children with the safe guard removed"); 74 await pushPref(BYPASS_WALKERFRONT_CHILDREN_IFRAME_GUARD_PREF, true); 75 76 let res = await walker.children(iframeNodeFront); 77 is( 78 res.nodes.length, 79 0, 80 "Retrieving the iframe children return an empty array" 81 ); 82 83 info("Test calling WalkerFront#children again, but with the safe guard"); 84 Services.prefs.clearUserPref(BYPASS_WALKERFRONT_CHILDREN_IFRAME_GUARD_PREF); 85 res = await walker.children(iframeNodeFront); 86 is( 87 res.nodes.length, 88 0, 89 "Retrieving the iframe children return an empty array" 90 ); 91 });