summaryrefslogtreecommitdiff
path: root/dom/performance/tests/test_performance_observer.js
blob: 9716570e29190b28f2af2a006f1e5b26d858e84a (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
setup({ explicit_done: true });

test(t => {
  assert_throws({name: "TypeError"}, function() {
    new PerformanceObserver();
  }, "PerformanceObserver constructor should throw TypeError if no argument is specified.");

  assert_throws({name: "TypeError"}, function() {
    new PerformanceObserver({});
  }, "PerformanceObserver constructor should throw TypeError if the argument is not a function.");
}, "Test that PerformanceObserver constructor throws exception");

test(t => {
  var observer = new PerformanceObserver(() => {
  });

  assert_throws({name: "TypeError"}, function() {
    observer.observe();
  }, "observe() should throw TypeError exception if no option specified.");

  assert_throws({name: "TypeError"}, function() {
    observer.observe({ unsupportedAttribute: "unsupported" });
  }, "obsrve() should throw TypeError exception if the option has no 'entryTypes' attribute.");

  assert_throws({name: "TypeError"}, function() {
    observer.observe({ entryTypes: [] });
  }, "obsrve() should throw TypeError exception if 'entryTypes' attribute is an empty sequence.");

  assert_throws({name: "TypeError"}, function() {
    observer.observe({ entryTypes: null });
  }, "obsrve() should throw TypeError exception if 'entryTypes' attribute is null.");

  assert_throws({name: "TypeError"}, function() {
    observer.observe({ entryTypes: ["invalid"]});
  }, "obsrve() should throw TypeError exception if 'entryTypes' attribute value is invalid.");
}, "Test that PerformanceObserver.observe throws exception");

function promiseObserve(test, options) {
  return new Promise(resolve => {
    performance.clearMarks();
    performance.clearMeasures();

    var observer = new PerformanceObserver(list => resolve(list));
    observer.observe(options);
    test.add_cleanup(() => observer.disconnect());
  });
}

promise_test(t => {
  var promise = promiseObserve(t, {entryTypes: ['mark', 'measure']});

  performance.mark("test-start");
  performance.mark("test-end");
  performance.measure("test-measure", "test-start", "test-end");

  return promise.then(list => {
    assert_equals(list.getEntries().length, 3,
      "There should be three observed entries.");

    var markEntries = list.getEntries().filter(entry => {
      return entry.entryType == "mark";
    });
    assert_array_equals(markEntries, performance.getEntriesByType("mark"),
      "Observed 'mark' entries should equal to entries obtained by getEntriesByType.");

    var measureEntries = list.getEntries().filter(entry => {
      return entry.entryType == "measure";
    });
    assert_array_equals(measureEntries, performance.getEntriesByType("measure"),
      "Observed 'measure' entries should equal to entries obtained by getEntriesByType.");
  });
}, "Test for user-timing with PerformanceObserver");

promise_test(t => {
  var promise = new Promise((resolve, reject) => {
    performance.clearMarks();
    performance.clearMeasures();

    var observer = new PerformanceObserver(list => reject(list));
    observer.observe({entryTypes: ['mark', 'measure']});
    observer.disconnect();
    t.step_timeout(resolve, 100);
  });

  performance.mark("test-start");
  performance.mark("test-end");
  performance.measure("test-measure", "test-start", "test-end");

  return promise.then(() => {
    assert_equals(performance.getEntriesByType("mark").length, 2);
    assert_equals(performance.getEntriesByType("measure").length, 1);
  }, list => {
    assert_unreached("Observer callback should never be called.");
  });

}, "Nothing should be notified after disconnecting observer");

promise_test(t => {
  var promise = promiseObserve(t, {entryTypes: ['mark']});

  performance.mark("test");

  return promise.then(list => {
    assert_array_equals(list.getEntries({"entryType": "mark"}),
                        performance.getEntriesByType("mark"),
                        "getEntries with entryType filter should return correct results.");

    assert_array_equals(list.getEntries({"name": "test"}),
                        performance.getEntriesByName("test"),
                        "getEntries with name filter should return correct results.");

    assert_array_equals(list.getEntries({"name": "test",
                                                      "entryType": "mark"}),
                        performance.getEntriesByName("test"),
                        "getEntries with name and entryType filter should return correct results.");

    assert_array_equals(list.getEntries({"name": "invalid"}),
                        [],
                        "getEntries with non-existent name filter should return an empty array.");

    assert_array_equals(list.getEntries({"name": "test",
                                                      "entryType": "measure"}),
                        [],
                        "getEntries with name filter and non-existent entryType should return an empty array.");

    assert_array_equals(list.getEntries({"name": "invalid",
                                                      "entryType": "mark"}),
                        [],
                        "getEntries with non-existent name and entryType filter should return an empty array.");

    assert_array_equals(list.getEntries({initiatorType: "xmlhttprequest"}),
                        [],
                        "getEntries with initiatorType filter should return an empty array.");
  });
}, "Test for PerformanceObserverEntryList.getEntries");

promise_test(t => {
  var promise = promiseObserve(t, {entryTypes: ['mark', 'measure']});

  performance.mark("test");
  performance.measure("test-measure", "test", "test");

  return promise.then(list => {
    assert_array_equals(list.getEntriesByType("mark"),
                        performance.getEntriesByType("mark"));
    assert_array_equals(list.getEntriesByType("measure"),
                        performance.getEntriesByType("measure"));
  });
}, "Test for PerformanceObserverEntryList.getEntriesByType");

promise_test(t => {
  var promise = promiseObserve(t, {entryTypes: ['mark', 'measure']});

  performance.mark("test");
  performance.measure("test-measure", "test", "test");

  return promise.then(list => {
    assert_array_equals(list.getEntriesByName("test"),
                        performance.getEntriesByName("test"));
    assert_array_equals(list.getEntriesByName("test-measure"),
                        performance.getEntriesByName("test-measure"));
  });
}, "Test for PerformanceObserverEntryList.getEntriesByName");

promise_test(t => {
  var promise = new Promise(resolve => {
    performance.clearMarks();
    performance.clearMeasures();

    var observer = new PerformanceObserver(list => resolve(list));
    observer.observe({entryTypes: ['mark', 'measure']});
    observer.observe({entryTypes: ['mark', 'measure']});
    t.add_cleanup(() => observer.disconnect());
  });

  performance.mark("test-start");
  performance.mark("test-end");
  performance.measure("test-measure", "test-start", "test-end");

  return promise.then(list => {
    assert_equals(list.getEntries().length, 3,
                  "Observed user timing entries should have only three entries.");
  });
}, "Test that invoking observe method twice affects nothing");

promise_test(t => {
  var promise = new Promise(resolve => {
    performance.clearMarks();
    performance.clearMeasures();

    var observer = new PerformanceObserver(list => resolve(list));
    observer.observe({entryTypes: ['mark', 'measure']});
    observer.observe({entryTypes: ['mark']});
    t.add_cleanup(() => observer.disconnect());
  });

  performance.mark("test-start");
  performance.mark("test-end");
  performance.measure("test-measure", "test-start", "test-end");

  return promise.then(list => {
    assert_equals(list.getEntries().length, 2,
                  "Observed user timing entries should have only two entries.");
  });
}, "Test that observing filter is replaced by a new filter");

promise_test(t => {
  var promise = new Promise(resolve => {
    performance.clearMarks();
    performance.clearMeasures();

    var observer = new PerformanceObserver(list => resolve(list));
    observer.observe({entryTypes: ['mark']});
    observer.observe({entryTypes: ['measure']});
    t.add_cleanup(() => observer.disconnect());
  });

  performance.mark("test-start");
  performance.mark("test-end");
  performance.measure("test-measure", "test-start", "test-end");

  return promise.then(list => {
    assert_equals(list.getEntries().length, 1,
                  "Observed user timing entries should have only 1 entries.");
  });
}, "Test that observing filter is replaced by a new filter");