summaryrefslogtreecommitdiffstats
path: root/browser/components/resistfingerprinting/test/browser/browser_cross_origin_isolated_animation_api.js
blob: f55a4929840c67bef8d7fbd05f6eaa41d22ee7ca (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
/**
 * Bug 1621677 - A test for making sure getting the correct (higher) precision
 *   when it's cross-origin-isolated on animation APIs.
 */

// ================================================================================================
// ================================================================================================
// This test case is mostly copy-and-paste from the test case for window in
// test_animation_api.html. The main difference is this test case
// verifies animation has more precsion when it's in cross-origin-isolated and
// cross-origin-isolated doesn't affect RFP.
add_task(async function runRTPTestAnimation() {
  let runTests = async function (data) {
    function waitForCondition(aCond, aCallback, aErrorMsg) {
      var tries = 0;
      var interval = content.setInterval(() => {
        if (tries >= 30) {
          ok(false, aErrorMsg);
          moveOn();
          return;
        }
        var conditionPassed;
        try {
          conditionPassed = aCond();
        } catch (e) {
          ok(false, `${e}\n${e.stack}`);
          conditionPassed = false;
        }
        if (conditionPassed) {
          moveOn();
        }
        tries++;
      }, 100);
      var moveOn = () => {
        content.clearInterval(interval);
        aCallback();
      };
    }

    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);

    const testDiv = content.document.getElementById("testDiv");
    const animation = testDiv.animate({ opacity: [0, 1] }, 100000);
    animation.play();

    let done;
    let promise = new Promise(resolve => {
      done = resolve;
    });

    waitForCondition(
      () => animation.currentTime > 100,
      () => {
        // We have disabled Time Precision Reduction for CSS Animations, so we
        // expect those tests to fail.
        // If we are testing that preference, we accept either rounded or not
        // rounded values as A-OK.
        var maybeAcceptEverything = function (value) {
          if (
            data.options.reduceTimerPrecision &&
            !data.options.resistFingerprinting
          ) {
            return true;
          }

          return value;
        };

        ok(
          maybeAcceptEverything(
            isRounded(animation.startTime, expectedPrecision)
          ),
          `Animation.startTime with precision ${expectedPrecision} is not ` +
            `rounded: ${animation.startTime}`
        );
        ok(
          maybeAcceptEverything(
            isRounded(animation.currentTime, expectedPrecision)
          ),
          `Animation.currentTime with precision ${expectedPrecision} is ` +
            `not rounded: ${animation.currentTime}`
        );
        ok(
          maybeAcceptEverything(
            isRounded(animation.timeline.currentTime, expectedPrecision)
          ),
          `Animation.timeline.currentTime with precision ` +
            `${expectedPrecision} is not rounded: ` +
            `${animation.timeline.currentTime}`
        );
        if (content.document.timeline) {
          ok(
            maybeAcceptEverything(
              isRounded(
                content.document.timeline.currentTime,
                expectedPrecision
              )
            ),
            `Document.timeline.currentTime with precision ` +
              `${expectedPrecision} is not rounded: ` +
              `${content.document.timeline.currentTime}`
          );
        }
        done();
      },
      "animation failed to start"
    );

    await promise;
  };

  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
    },
    100,
    runTests
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      crossOriginIsolated: true,
    },
    50,
    runTests
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      crossOriginIsolated: true,
    },
    0.1,
    runTests
  );
  await setupAndRunCrossOriginIsolatedTest(
    {
      resistFingerprinting: true,
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
    },
    0.013,
    runTests
  );

  await setupAndRunCrossOriginIsolatedTest(
    {
      reduceTimerPrecision: true,
      crossOriginIsolated: true,
    },
    0.005,
    runTests
  );
});