summaryrefslogtreecommitdiffstats
path: root/toolkit/components/perfmonitoring/tests/browser/browser_webpagePerformanceAlerts.js
blob: eb908c8dbf63a394f4489abdc4a1e846553506eb (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";

/**
 * Tests for PerformanceWatcher watching slow web pages.
 */

 /**
  * Simulate a slow webpage.
  */
function WebpageBurner() {
  CPUBurner.call(this, "http://example.com/browser/toolkit/components/perfmonitoring/tests/browser/browser_compartments.html?test=" + Math.random(), 300000);
}
WebpageBurner.prototype = Object.create(CPUBurner.prototype);
WebpageBurner.prototype.promiseBurnContentCPU = function() {
  return promiseContentResponse(this._browser, "test-performance-watcher:burn-content-cpu", {});
};

function WebpageListener(windowId, accept) {
  info(`Creating WebpageListener for ${windowId}`);
  AlertListener.call(this, accept, {
    register: () => PerformanceWatcher.addPerformanceListener({windowId}, this.listener),
    unregister: () => PerformanceWatcher.removePerformanceListener({windowId}, this.listener)
  });
}
WebpageListener.prototype = Object.create(AlertListener.prototype);

add_task(function* init() {
  // Get rid of buffering.
  let service = Cc["@mozilla.org/toolkit/performance-stats-service;1"].getService(
    Ci.nsIPerformanceStatsService);
  let oldDelay = service.jankAlertBufferingDelay;

  service.jankAlertBufferingDelay = 0 /* ms */;
  registerCleanupFunction(() => {
    info("Cleanup");
    service.jankAlertBufferingDelay = oldDelay;
  });
});

add_task(function* test_open_window_then_watch_it() {
  let burner = new WebpageBurner();
  yield burner.promiseInitialized;
  yield burner.promiseBurnContentCPU();

  info(`Check that burning CPU triggers the real listener, but not the fake listener`);
  let realListener = new WebpageListener(burner.windowId, (group, details) => {
    info(`test: realListener for ${burner.tab.linkedBrowser.outerWindowID}: ${group}, ${details}\n`);
    Assert.equal(group.windowId, burner.windowId, "We should not receive data meant for another group");
    return details;
  }); // This listener should be triggered.

  info(`Creating fake burner`);
  let otherTab = gBrowser.addTab();
  yield BrowserTestUtils.browserLoaded(otherTab.linkedBrowser);
  info(`Check that burning CPU triggers the real listener, but not the fake listener`);
  let fakeListener = new WebpageListener(otherTab.linkedBrowser.outerWindowID, group => group.windowId == burner.windowId); // This listener should never be triggered.
  let universalListener = new WebpageListener(0, alerts =>
    alerts.find(alert => alert.source.windowId == burner.windowId)
  );

  // Waiting a little – listeners are buffered.
  yield new Promise(resolve => setTimeout(resolve, 100));

  yield burner.run("promiseBurnContentCPU", 20, realListener);
  Assert.ok(realListener.triggered, `1. The real listener was triggered`);
  Assert.ok(universalListener.triggered, `1. The universal listener was triggered`);
  Assert.ok(!fakeListener.triggered, `1. The fake listener was not triggered`);

  if (realListener.result) {
    Assert.ok(realListener.result.highestJank >= 300, `1. jank is at least 300ms (${realListener.result.highestJank}ms)`);
  }

  info(`Attempting to remove a performance listener incorrectly, check that this does not hurt our real listener`);
  Assert.throws(() => PerformanceWatcher.removePerformanceListener({addonId: addon.addonId}, () => {}));
  Assert.throws(() => PerformanceWatcher.removePerformanceListener({addonId: addon.addonId + "-unbound-id-" + Math.random()}, realListener.listener));

  // Waiting a little – listeners are buffered.
  yield new Promise(resolve => setTimeout(resolve, 100));
  yield burner.run("promiseBurnContentCPU", 20, realListener);
  // Waiting a little – listeners are buffered.
  yield new Promise(resolve => setTimeout(resolve, 100));

  Assert.ok(realListener.triggered, `2. The real listener was triggered`);
  Assert.ok(universalListener.triggered, `2. The universal listener was triggered`);
  Assert.ok(!fakeListener.triggered, `2. The fake listener was not triggered`);
  if (realListener.result) {
    Assert.ok(realListener.result.highestJank >= 300, `2. jank is at least 300ms (${realListener.jank}ms)`);
  }

  info(`Attempting to remove correctly, check if the listener is still triggered`);
  // Waiting a little – listeners are buffered.
  yield new Promise(resolve => setTimeout(resolve, 100));
  realListener.unregister();

  // Waiting a little – listeners are buffered.
  yield new Promise(resolve => setTimeout(resolve, 100));
  yield burner.run("promiseBurnContentCPU", 3, realListener);
  Assert.ok(!realListener.triggered, `3. After being unregistered, the real listener was not triggered`);
  Assert.ok(universalListener.triggered, `3. The universal listener is still triggered`);

  universalListener.unregister();

  // Waiting a little – listeners are buffered.
  yield new Promise(resolve => setTimeout(resolve, 100));
  yield burner.run("promiseBurnContentCPU", 3, realListener);
  Assert.ok(!universalListener.triggered, `4. After being unregistered, the universal listener is not triggered`);

  fakeListener.unregister();
  burner.dispose();
  gBrowser.removeTab(otherTab);
});