Bug 1441336 - Test addon permissions for PerformanceTiming properties draft
authorTomislav Jovanovic <tomica@gmail.com>
Sat, 07 Apr 2018 02:38:51 +0200
changeset 780659 0287128c8af8a1ce8f4a79fb03b6f28decf974f5
parent 780658 f5a218cdaa7f40f6a64f1235e15c8a5f088a97a5
push id106067
push userbmo:tomica@gmail.com
push dateWed, 11 Apr 2018 18:55:08 +0000
bugs1441336
milestone61.0a1
Bug 1441336 - Test addon permissions for PerformanceTiming properties MozReview-Commit-ID: LlySv9AinVC
toolkit/components/extensions/test/xpcshell/test_ext_contentscript_perf_observers.js
toolkit/components/extensions/test/xpcshell/xpcshell-remote.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_perf_observers.js
@@ -0,0 +1,67 @@
+"use strict";
+
+const server = createHttpServer({hosts: ["a.example.com", "b.example.com", "c.example.com"]});
+server.registerDirectory("/data/", do_get_file("data"));
+
+add_task(async function test_perf_observers_cors() {
+  const extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: [
+        "http://b.example.com/",
+      ],
+      content_scripts: [{
+        matches: ["http://a.example.com/file_sample.html"],
+        js: ["cs.js"],
+      }],
+    },
+    files: {
+      "cs.js"() {
+        let obs = new window.PerformanceObserver(list => {
+          list.getEntries().forEach(e => {
+            browser.test.sendMessage("observed", {
+              url: e.name,
+              time: e.connectEnd,
+              size: e.encodedBodySize,
+            });
+          });
+        });
+        obs.observe({entryTypes: ["resource"]});
+
+        let b = document.createElement("link");
+        b.rel = "stylesheet";
+
+        // Simulate page including a cross-origin resource from b.example.com.
+        b.wrappedJSObject.href = "http://b.example.com/file_download.txt";
+        document.head.appendChild(b);
+
+        let c = document.createElement("link");
+        c.rel = "stylesheet";
+
+        // Simulate page including a cross-origin resource from c.example.com.
+        c.wrappedJSObject.href = "http://c.example.com/file_download.txt";
+        document.head.appendChild(c);
+      },
+    },
+  });
+
+  let page = await ExtensionTestUtils.loadContentPage("http://a.example.com/file_sample.html");
+  await extension.startup();
+
+  let b = await extension.awaitMessage("observed");
+  let c = await extension.awaitMessage("observed");
+
+  if (b.url.startsWith("http://c.")) {
+    [c, b] = [b, c];
+  }
+
+  ok(b.url.startsWith("http://b."), "Observed resource from b.example.com");
+  ok(b.time > 0, "connectionEnd available from b.example.com");
+  equal(b.size, 428, "encodedBodySize available from b.example.com");
+
+  ok(c.url.startsWith("http://c."), "Observed resource from c.example.com");
+  equal(c.time, 0, "connectionEnd == 0 from c.example.com");
+  equal(c.size, 0, "encodedBodySize == 0 from c.example.com");
+
+  await extension.unload();
+  await page.close();
+});
--- a/toolkit/components/extensions/test/xpcshell/xpcshell-remote.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell-remote.ini
@@ -6,8 +6,11 @@ skip-if = appname == "thunderbird" || os
 dupe-manifest =
 support-files =
   data/**
   xpcshell-content.ini
 tags = webextensions remote-webextensions
 
 [include:xpcshell-common.ini]
 [include:xpcshell-content.ini]
+
+[test_ext_contentscript_perf_observers.js]
+# Inexplicably, PerformanceObserver used in the test doesn't fire in non-e10s mode.