summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webxr/xrSession_requestAnimationFrame_timestamp.https.html
blob: ee6bb49432069aba7647d269d0576ab3453660b3 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/webxr_util.js"></script>
<script src="resources/webxr_test_constants.js"></script>

<script>
const TEN_SECONDS = 10000; // 10k ms in ten seconds
const ONE_MINUTE = 60000; // 60k ms in one minute

let immersiveTestName = "XRFrame getViewerPose updates on the next frame for immersive";
let nonImmersiveTestName = "XRFrame getViewerPose updates on the next frame for non-immersive";

let fakeDeviceInitParams = TRACKED_IMMERSIVE_DEVICE;

let testFunction = function(session, fakeDeviceController, t) {
  return session.requestReferenceSpace('viewer')
    .then((referenceSpace) => new Promise((resolve, reject) => {
      let counter = 0;
      let windowFrameTime = 0;
      let frameTime = 0;
      let lastFrameTime = 0;

      let firstFrame = true;

      function onFrameFirst(time, xrFrame) {
        lastFrameTime = frameTime;
        frameTime = time;
        let now = performance.now();

        t.step( () => {
          if(firstFrame) {
            // This callback must be the first one called.
            assert_equals(counter, 0);
          } else {
            // If it's a second animation frame, the timestamp must be greater
            // than the timestamp on a previous frame.
            assert_greater_than(frameTime, lastFrameTime);
            // ... but not grater than 10 seconds.
            assert_approx_equals(frameTime, lastFrameTime, TEN_SECONDS);
          }

          // There's going to be some disparity between performance.now() and
          // the timestamp passed into the callback, but it shouldn't be huge.
          // If they're more than ten seconds apart something has gone horribly
          // wrong.
          assert_approx_equals(frameTime, now, TEN_SECONDS);
        });

        if (firstFrame) {
          // We also want this method to run for the second animation frame.
          session.requestAnimationFrame(onFrameFirst);
        } else {
          resolve();
        }

        firstFrame = false;
        counter++;
      }

      function onFrameSubsequent(time, xrFrame) {
        t.step( () => {
          // The timestamp passed to this callback should be exactly equal to
          // the one passed to the first callback in this set.
          assert_equals(time, frameTime);
        });

        counter++;
      }

      function onFrameLast(time, xrFrame) {
        t.step( () => {
          // Make sure all the previous callbacks fired as expected.
          assert_equals(counter, 11);
        });
      }

      session.requestAnimationFrame(onFrameFirst);
      // Queue up several callbacks
      for (let i = 0; i < 10; ++i) {
        session.requestAnimationFrame(onFrameSubsequent);
      }
      session.requestAnimationFrame(onFrameLast);

    }));
};

xr_session_promise_test(
  immersiveTestName, testFunction, fakeDeviceInitParams, 'immersive-vr');
xr_session_promise_test(
  nonImmersiveTestName, testFunction, fakeDeviceInitParams, 'inline');

</script>