summaryrefslogtreecommitdiffstats
path: root/browser/components/resistfingerprinting/test/browser/browser_performanceAPI.js
blob: b6e2aaf6ee31f9d8160a68e01a78a8e0d8a3d2e4 (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
// ================================================================================================
// ================================================================================================
add_task(async function runRFPandRTPTests() {
  // RFP = ResistFingerprinting / RTP = ReduceTimePrecision
  let runTests = async function (data) {
    let timerlist = data.list;
    let labelType = data.resistFingerprinting
      ? "ResistFingerprinting"
      : "ReduceTimePrecision";
    let expectedPrecision = data.precision;
    // eslint beleives that isrounded is available in this scope, but if you
    // remove the assignment, you will see it is not
    // eslint-disable-next-line
    let isRounded = eval(data.isRoundedFunc);

    ok(
      isRounded(content.performance.timeOrigin, expectedPrecision),
      `For ${labelType}, performance.timeOrigin is not correctly rounded: ` +
        content.performance.timeOrigin
    );

    // Check that whether the performance timing API is correctly spoofed.
    for (let time of timerlist) {
      if (
        data.resistFingerprinting &&
        (time == "domainLookupStart" || time == "domainLookupEnd")
      ) {
        is(
          content.performance.timing[time],
          content.performance.timing.fetchStart,
          `For resistFingerprinting, the timing(${time}) is not correctly spoofed.`
        );
      } else {
        ok(
          isRounded(content.performance.timing[time], expectedPrecision),
          `For ${labelType}(` +
            expectedPrecision +
            `), the timing(${time}) is not correctly rounded: ` +
            content.performance.timing[time]
        );
      }
    }

    // Try to add some entries.
    content.performance.mark("Test");
    content.performance.mark("Test-End");
    content.performance.measure("Test-Measure", "Test", "Test-End");

    // Check the entries for performance.getEntries/getEntriesByType/getEntriesByName.
    await new Promise(resolve => {
      const paintObserver = new content.PerformanceObserver(() => {
        resolve();
      });
      paintObserver.observe({ type: "paint", buffered: true });
    });

    is(
      content.performance.getEntries().length,
      5,
      `For ${labelType}, there should be 4 entries for performance.getEntries()`
      // PerformancePaintTiming, PerformanceNavigationTiming, PerformanceMark, PerformanceMark, PerformanceMeasure
    );
    for (var i = 0; i < 5; i++) {
      let startTime = content.performance.getEntries()[i].startTime;
      let duration = content.performance.getEntries()[i].duration;
      ok(
        isRounded(startTime, expectedPrecision),
        `For ${labelType}(` +
          expectedPrecision +
          "), performance.getEntries(" +
          i +
          ").startTime is not rounded: " +
          startTime
      );
      ok(
        isRounded(duration, expectedPrecision),
        `For ${labelType}(` +
          expectedPrecision +
          "), performance.getEntries(" +
          i +
          ").duration is not rounded: " +
          duration
      );
    }
    is(
      content.performance.getEntriesByType("mark").length,
      2,
      `For ${labelType}, there should be 2 entries for performance.getEntriesByType()`
    );
    is(
      content.performance.getEntriesByName("Test", "mark").length,
      1,
      `For ${labelType}, there should be 1 entry for performance.getEntriesByName()`
    );
    content.performance.clearMarks();
    content.performance.clearMeasures();
    content.performance.clearResourceTimings();

    // Get all LCP candidates
    let lcpEntryList = await new Promise(resolve => {
      const paintObserver = new content.PerformanceObserver(entryList => {
        resolve(entryList);
      });
      paintObserver.observe({
        type: "largest-contentful-paint",
        buffered: true,
      });
    });

    for (const entry of lcpEntryList.getEntries()) {
      ok(
        isRounded(entry.startTime, expectedPrecision),
        `For ${labelType}(` +
          expectedPrecision +
          "), LargestContentfulPaint.startTime is not rounded: " +
          entry.startTime
      );

      ok(
        isRounded(entry.renderTime, expectedPrecision),
        `For ${labelType}(` +
          expectedPrecision +
          "), LargestContentfulPaint.renderTime is not rounded: " +
          entry.renderTime
      );

      ok(
        isRounded(entry.loadTime, expectedPrecision),
        `For ${labelType}(` +
          expectedPrecision +
          "), LargestContentfulPaint.loadTime is not rounded: " +
          entry.loadTime
      );
    }
  };

  await setupPerformanceAPISpoofAndDisableTest(
    true,
    true,
    false,
    200,
    runTests
  );
  await setupPerformanceAPISpoofAndDisableTest(
    true,
    true,
    false,
    100,
    runTests
  );
  await setupPerformanceAPISpoofAndDisableTest(
    true,
    false,
    false,
    13,
    runTests
  );
  await setupPerformanceAPISpoofAndDisableTest(
    true,
    false,
    false,
    0.13,
    runTests
  );
  await setupPerformanceAPISpoofAndDisableTest(true, true, true, 100, runTests);
  await setupPerformanceAPISpoofAndDisableTest(true, false, true, 13, runTests);
  await setupPerformanceAPISpoofAndDisableTest(
    true,
    false,
    true,
    0.13,
    runTests
  );

  await setupPerformanceAPISpoofAndDisableTest(
    false,
    true,
    false,
    100,
    runTests
  );
  await setupPerformanceAPISpoofAndDisableTest(
    false,
    true,
    false,
    13,
    runTests
  );
  await setupPerformanceAPISpoofAndDisableTest(
    false,
    true,
    false,
    0.13,
    runTests
  );
  await setupPerformanceAPISpoofAndDisableTest(
    false,
    true,
    true,
    0.005,
    runTests
  );
});