summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_performance_counters.js
blob: 1a14ff6eb6b1e8356ee271d0c72e4221ca25a12a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const { ExtensionParent } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionParent.sys.mjs"
);

const ENABLE_COUNTER_PREF =
  "extensions.webextensions.enablePerformanceCounters";
const TIMING_MAX_AGE = "extensions.webextensions.performanceCountersMaxAge";

let { ParentAPIManager } = ExtensionParent;

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms)); // eslint-disable-line mozilla/no-arbitrary-setTimeout
}

async function retrieveSpecificCounter(apiName, expectedCount) {
  let currentCount = 0;
  let data;
  while (currentCount < expectedCount) {
    data = await ParentAPIManager.retrievePerformanceCounters();
    for (let [console, counters] of data) {
      for (let [api, counter] of counters) {
        if (api == apiName) {
          currentCount += counter.calls;
        }
      }
    }
    await sleep(100);
  }
  return data;
}

async function test_counter() {
  async function background() {
    // creating a bookmark is done in the parent
    let folder = await browser.bookmarks.create({ title: "Folder" });
    await browser.bookmarks.create({
      title: "Bookmark",
      url: "http://example.com",
      parentId: folder.id,
    });

    // getURL() is done in the child, let do three
    browser.runtime.getURL("beasts/frog.html");
    browser.runtime.getURL("beasts/frog2.html");
    browser.runtime.getURL("beasts/frog3.html");
    browser.test.sendMessage("done");
  }

  let extensionData = {
    background,
    manifest: {
      permissions: ["bookmarks"],
    },
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);
  await extension.startup();
  await extension.awaitMessage("done");

  let counters = await retrieveSpecificCounter("getURL", 3);
  await extension.unload();

  // check that the bookmarks.create API was tracked
  let counter = counters.get(extension.id).get("bookmarks.create");
  ok(counter.calls > 0);
  ok(counter.duration > 0);

  // check that the getURL API was tracked
  counter = counters.get(extension.id).get("getURL");
  ok(counter.calls > 0);
  ok(counter.duration > 0);
}

add_task(function test_performance_counter() {
  return runWithPrefs(
    [
      [ENABLE_COUNTER_PREF, true],
      [TIMING_MAX_AGE, 1],
    ],
    test_counter
  );
});