diff options
author | Matt A. Tobin <email@mattatobin.com> | 2018-02-09 11:10:00 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2018-02-09 11:10:00 -0500 |
commit | f164d9124708b50789dbb6959e1de96cc5697c48 (patch) | |
tree | 6dffd12e08c5383130df0252fb69cd6d6330794f /toolkit/components/webextensions/test/mochitest/test_ext_storage_tab.html | |
parent | 30de4018913f0cdaea19d1dd12ecd8209e2ed08e (diff) | |
download | UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.gz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.lz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.xz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.zip |
Rename Toolkit's webextensions component directory to better reflect what it is.
Diffstat (limited to 'toolkit/components/webextensions/test/mochitest/test_ext_storage_tab.html')
-rw-r--r-- | toolkit/components/webextensions/test/mochitest/test_ext_storage_tab.html | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/toolkit/components/webextensions/test/mochitest/test_ext_storage_tab.html b/toolkit/components/webextensions/test/mochitest/test_ext_storage_tab.html new file mode 100644 index 000000000..32d8e6af0 --- /dev/null +++ b/toolkit/components/webextensions/test/mochitest/test_ext_storage_tab.html @@ -0,0 +1,118 @@ +<!DOCTYPE html> +<html> +<head> + <title>WebExtension test</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script> + <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script> + <script type="text/javascript" src="head.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> + +<script type="text/javascript"> +"use strict"; + +add_task(function* test_multiple_pages() { + async function background() { + let tabReady = new Promise(resolve => { + browser.runtime.onMessage.addListener(function listener(msg) { + browser.test.log("onMessage " + msg); + if (msg == "tab-ready") { + browser.runtime.onMessage.removeListener(listener); + resolve(); + } + }); + }); + + let tabId; + let tabRemoved = new Promise(resolve => { + browser.tabs.onRemoved.addListener(function listener(removedId) { + if (removedId == tabId) { + browser.tabs.onRemoved.removeListener(listener); + + // Delay long enough to be sure the inner window has been nuked. + setTimeout(resolve, 0); + } + }); + }); + + try { + let storage = browser.storage.local; + + browser.test.log("create"); + let tab = await browser.tabs.create({url: "tab.html"}); + tabId = tab.id; + + await tabReady; + + let result = await storage.get("key"); + browser.test.assertEq(undefined, result.key, "Key should be undefined"); + + await browser.runtime.sendMessage("tab-set-key"); + + result = await storage.get("key"); + browser.test.assertEq(JSON.stringify({foo: {bar: "baz"}}), + JSON.stringify(result.key), + "Key should be set to the value from the tab"); + + browser.test.log("Remove tab"); + + await Promise.all([ + browser.tabs.remove(tabId), + tabRemoved, + ]); + + result = await storage.get("key"); + browser.test.assertEq(JSON.stringify({foo: {bar: "baz"}}), + JSON.stringify(result.key), + "Key should still be set to the value from the tab"); + + browser.test.notifyPass("storage-multiple"); + } catch (e) { + browser.test.fail(`Error: ${e} :: ${e.stack}`); + browser.test.notifyFail("storage-multiple"); + } + } + + function tab() { + browser.test.log("tab"); + browser.runtime.onMessage.addListener(msg => { + if (msg == "tab-set-key") { + return browser.storage.local.set({key: {foo: {bar: "baz"}}}); + } + }); + + browser.runtime.sendMessage("tab-ready"); + } + + let extension = ExtensionTestUtils.loadExtension({ + background, + + files: { + "tab.html": `<!DOCTYPE html> + <html> + <head> + <meta charset="utf-8"> + <script src="tab.js"><\/script> + </head> + </html>`, + + "tab.js": tab, + }, + + manifest: { + permissions: ["storage"], + }, + }); + + yield extension.startup(); + + yield extension.awaitFinish("storage-multiple"); + yield extension.unload(); +}); + +</script> + +</body> +</html> |