browser_frametree.js (4239B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const URL = HTTPROOT + "browser_frametree_sample.html"; 7 const URL_FRAMESET = HTTPROOT + "browser_frametree_sample_frameset.html"; 8 const URL_IFRAMES = HTTPROOT + "browser_frametree_sample_iframes.html"; 9 10 /** 11 * Check that we correctly enumerate non-dynamic child frames. 12 */ 13 add_task(async function test_frametree() { 14 // Add an empty tab for a start. 15 let tab = BrowserTestUtils.addTab(gBrowser, URL); 16 let browser = tab.linkedBrowser; 17 await promiseBrowserLoaded(browser); 18 19 // The page is a single frame with no children. 20 is(await countNonDynamicFrames(browser), 0, "no child frames"); 21 22 // Navigate to a frameset. 23 BrowserTestUtils.startLoadingURIString(browser, URL_FRAMESET); 24 await promiseBrowserLoaded(browser); 25 26 // The frameset has two frames. 27 is(await countNonDynamicFrames(browser), 2, "two non-dynamic child frames"); 28 29 // Go back in history. 30 let pageShowPromise = BrowserTestUtils.waitForContentEvent( 31 browser, 32 "pageshow", 33 true 34 ); 35 browser.goBack(); 36 await pageShowPromise; 37 38 // We're at page one again. 39 is(await countNonDynamicFrames(browser), 0, "no child frames"); 40 41 // Append a dynamic frame. 42 await SpecialPowers.spawn(browser, [URL], async ([url]) => { 43 let frame = content.document.createElement("iframe"); 44 frame.setAttribute("src", url); 45 content.document.body.appendChild(frame); 46 await ContentTaskUtils.waitForEvent(frame, "load"); 47 }); 48 49 // The dynamic frame should be ignored. 50 is( 51 await countNonDynamicFrames(browser), 52 0, 53 "we still have a single root frame" 54 ); 55 56 // Cleanup. 57 BrowserTestUtils.removeTab(tab); 58 }); 59 60 /** 61 * Check that we correctly enumerate non-dynamic child frames. 62 */ 63 add_task(async function test_frametree_dynamic() { 64 // Add an empty tab for a start. 65 let tab = BrowserTestUtils.addTab(gBrowser, URL_IFRAMES); 66 let browser = tab.linkedBrowser; 67 await promiseBrowserLoaded(browser); 68 69 // The page has two iframes. 70 is(await countNonDynamicFrames(browser), 2, "two non-dynamic child frames"); 71 is(await enumerateIndexes(browser), "0,1", "correct indexes 0 and 1"); 72 73 // Insert a dynamic frame. 74 await SpecialPowers.spawn(browser, [URL], async ([url]) => { 75 let frame = content.document.createElement("iframe"); 76 frame.setAttribute("src", url); 77 content.document.body.insertBefore( 78 frame, 79 content.document.getElementsByTagName("iframe")[1] 80 ); 81 await ContentTaskUtils.waitForEvent(frame, "load"); 82 }); 83 84 // The page still has two iframes. 85 is(await countNonDynamicFrames(browser), 2, "two non-dynamic child frames"); 86 is(await enumerateIndexes(browser), "0,1", "correct indexes 0 and 1"); 87 88 // Append a dynamic frame. 89 await SpecialPowers.spawn(browser, [URL], async ([url]) => { 90 let frame = content.document.createElement("iframe"); 91 frame.setAttribute("src", url); 92 content.document.body.appendChild(frame); 93 await ContentTaskUtils.waitForEvent(frame, "load"); 94 }); 95 96 // The page still has two iframes. 97 is(await countNonDynamicFrames(browser), 2, "two non-dynamic child frames"); 98 is(await enumerateIndexes(browser), "0,1", "correct indexes 0 and 1"); 99 100 // Remopve a non-dynamic iframe. 101 await SpecialPowers.spawn(browser, [URL], async () => { 102 // Remove the first iframe, which should be a non-dynamic iframe. 103 content.document.body.removeChild( 104 content.document.getElementsByTagName("iframe")[0] 105 ); 106 }); 107 108 is(await countNonDynamicFrames(browser), 1, "one non-dynamic child frame"); 109 is(await enumerateIndexes(browser), "1", "correct index 1"); 110 111 // Cleanup. 112 BrowserTestUtils.removeTab(tab); 113 }); 114 115 async function countNonDynamicFrames(browser) { 116 return SpecialPowers.spawn(browser, [], async () => { 117 let count = 0; 118 content.SessionStoreUtils.forEachNonDynamicChildFrame( 119 content, 120 () => count++ 121 ); 122 return count; 123 }); 124 } 125 126 async function enumerateIndexes(browser) { 127 return SpecialPowers.spawn(browser, [], async () => { 128 let indexes = []; 129 content.SessionStoreUtils.forEachNonDynamicChildFrame(content, (frame, i) => 130 indexes.push(i) 131 ); 132 return indexes.join(","); 133 }); 134 }