browser_async_flushes.js (2283B)
1 "use strict"; 2 3 const PATH = getRootDirectory(gTestPath).replace( 4 "chrome://mochitests/content/", 5 "http://example.com/" 6 ); 7 const URL = PATH + "file_async_flushes.html"; 8 9 add_task(async function test_flush() { 10 // Create new tab. 11 let tab = BrowserTestUtils.addTab(gBrowser, URL); 12 let browser = tab.linkedBrowser; 13 await promiseBrowserLoaded(browser); 14 15 // Flush to empty any queued update messages. 16 await TabStateFlusher.flush(browser); 17 18 // There should be one history entry. 19 let { entries } = JSON.parse(ss.getTabState(tab)); 20 is(entries.length, 1, "there is a single history entry"); 21 22 // Click the link to navigate, this will add second shistory entry. 23 await SpecialPowers.spawn(browser, [], async function () { 24 return new Promise(resolve => { 25 docShell.chromeEventHandler.addEventListener( 26 "hashchange", 27 () => resolve(), 28 { once: true, capture: true } 29 ); 30 31 // Click the link. 32 content.document.querySelector("a").click(); 33 }); 34 }); 35 36 // Flush to empty any queued update messages. 37 await TabStateFlusher.flush(browser); 38 39 // There should be two history entries now. 40 ({ entries } = JSON.parse(ss.getTabState(tab))); 41 is(entries.length, 2, "there are two shistory entries"); 42 43 // Cleanup. 44 gBrowser.removeTab(tab); 45 }); 46 47 add_task(async function test_remove() { 48 // Create new tab. 49 let tab = BrowserTestUtils.addTab(gBrowser, URL); 50 let browser = tab.linkedBrowser; 51 await promiseBrowserLoaded(browser); 52 53 // Flush to empty any queued update messages. 54 await TabStateFlusher.flush(browser); 55 56 // There should be one history entry. 57 let { entries } = JSON.parse(ss.getTabState(tab)); 58 is(entries.length, 1, "there is a single history entry"); 59 60 // Click the link to navigate. 61 await SpecialPowers.spawn(browser, [], async function () { 62 return new Promise(resolve => { 63 docShell.chromeEventHandler.addEventListener( 64 "hashchange", 65 () => resolve(), 66 { once: true, capture: true } 67 ); 68 69 // Click the link. 70 content.document.querySelector("a").click(); 71 }); 72 }); 73 74 // Request a flush and remove the tab. The flush should still complete. 75 await Promise.all([ 76 TabStateFlusher.flush(browser), 77 promiseRemoveTabAndSessionState(tab), 78 ]); 79 });