test_ext_tabs_update_url.html (3350B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Tabs update Test</title> 6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script> 8 <script type="text/javascript" src="head.js"></script> 9 <link rel="stylesheet" href="/tests/SimpleTest/test.css"/> 10 </head> 11 <body> 12 13 <script type="text/javascript"> 14 "use strict"; 15 16 async function testTabsUpdateURL(existentTabURL, tabsUpdateURL, isErrorExpected) { 17 const extension = ExtensionTestUtils.loadExtension({ 18 manifest: { 19 "permissions": ["tabs"], 20 }, 21 22 files: { 23 "tab.html": ` 24 <!DOCTYPE html> 25 <html> 26 <head> 27 <meta charset="utf-8"> 28 </head> 29 <body> 30 <h1>tab page</h1> 31 </body> 32 </html> 33 `.trim(), 34 }, 35 background: function() { 36 browser.test.sendMessage("ready", browser.runtime.getURL("tab.html")); 37 38 browser.test.onMessage.addListener(async (msg, tabsUpdateURL, isErrorExpected) => { 39 const tabs = await browser.tabs.query({lastFocusedWindow: true}); 40 41 try { 42 const tab = await browser.tabs.update(tabs[0].id, {url: tabsUpdateURL}); 43 44 browser.test.assertFalse(isErrorExpected, `tabs.update with URL ${tabsUpdateURL} should be rejected`); 45 browser.test.assertTrue(tab, "on success the tab should be defined"); 46 } catch (error) { 47 browser.test.assertTrue(isErrorExpected, `tabs.update with URL ${tabsUpdateURL} should not be rejected`); 48 browser.test.assertTrue(/^Illegal URL/.test(error.message), 49 "tabs.update should be rejected with the expected error message"); 50 } 51 52 browser.test.sendMessage("done"); 53 }); 54 }, 55 }); 56 57 await extension.startup(); 58 59 const mozExtTabURL = await extension.awaitMessage("ready"); 60 61 if (tabsUpdateURL == "self") { 62 tabsUpdateURL = mozExtTabURL; 63 } 64 65 info(`tab.update URL "${tabsUpdateURL}" on tab with URL "${existentTabURL}"`); 66 67 const tab1 = window.open(existentTabURL); 68 69 extension.sendMessage("start", tabsUpdateURL, isErrorExpected); 70 await extension.awaitMessage("done"); 71 72 tab1.close(); 73 await extension.unload(); 74 } 75 76 add_task(async function() { 77 info("Start testing tabs.update on javascript URLs"); 78 79 const dataURLPage = `data:text/html, 80 <!DOCTYPE html> 81 <html> 82 <head> 83 <meta charset="utf-8"> 84 </head> 85 <body> 86 <h1>data url page</h1> 87 </body> 88 </html>`; 89 90 const checkList = [ 91 { 92 tabsUpdateURL: "http://example.net", 93 isErrorExpected: false, 94 }, 95 { 96 tabsUpdateURL: "self", 97 isErrorExpected: false, 98 }, 99 { 100 tabsUpdateURL: "about:addons", 101 isErrorExpected: true, 102 }, 103 { 104 tabsUpdateURL: "javascript:console.log('tabs.update execute javascript')", 105 isErrorExpected: true, 106 }, 107 { 108 tabsUpdateURL: dataURLPage, 109 isErrorExpected: true, 110 }, 111 ]; 112 113 const testCases = checkList 114 .map((check) => Object.assign({}, check, {existentTabURL: "about:blank"})); 115 116 for (const {existentTabURL, tabsUpdateURL, isErrorExpected} of testCases) { 117 await testTabsUpdateURL(existentTabURL, tabsUpdateURL, isErrorExpected); 118 } 119 120 info("done"); 121 }); 122 </script> 123 124 </body> 125 </html>