summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-contain/content-visibility/animation-display-lock.html
blob: 279ffd002ed21a90b2525978e9c0b328375b69a1 (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
<!DOCTYPE html>
<meta charset=utf8>
<title>Test getComputedStyle on a CSS animation in a display locked subtree</title>
<link rel="help" href="https://drafts.csswg.org/css-contain-2/">
<script src="/web-animations/testcommon.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
  #container {
    content-visibility: visible;
    contain: style layout paint;
    contain-intrinsic-size: 0 100px;
  }
  @keyframes fade {
    from { opacity: 1; }
    to { opacity: 0;  }
  }
  #target {
    background: 'green';
    height: 100px;
    width: 100px;
  }
  .animate {
    animation: fade 1s linear 2 alternate;
  }
  .transition {
    transition: opacity 1s linear;
  }
</style>
<body>
  <div id="container"></div>
</body>
<script>
"use strict";

function reset() {
  const container = document.getElementById('container');
  const target = document.getElementById('target');
  container.style = '';
  container.removeChild(target);
}

function createAnimatingElement(test, name) {
  const container = document.getElementById('container');
  const target = document.createElement('div');
  container.appendChild(target);
  target.id = 'target';
  target.className = name;
  test.add_cleanup(() => {
    reset();
  });
  return target;
}

function waitForEvent(element, eventName) {
  return new Promise(resolve => element.addEventListener(eventName, resolve, { once: true }));
}

promise_test(async t => {
  const container = document.getElementById('container');
  const target = createAnimatingElement(t, 'animate');
  let animationIterationEvent = false;
  target.addEventListener('animationiteration', () => {
    animationIterationEvent = true;
  });
  const animation = target.getAnimations()[0];
  await animation.ready;
  await waitForAnimationFrames(1);
  container.style.contentVisibility = 'hidden';
  animation.currentTime = 1500;
  assert_approx_equals(
      parseFloat(getComputedStyle(target).opacity), 0.5, 1e-6,
      'Computed style is updated even when the animation is running in a ' +
      'display locked subtree');
  await waitForAnimationFrames(2);
  assert_false(animationIterationEvent,
               'Animation events do not fire while the animation is ' +
               'running in a display locked subtree');
  container.style.contentVisibility = 'visible';
  await waitForEvent(target, 'animationiteration');
  assert_true(animationIterationEvent,
              'The animationiteration event fires once the animation is ' +
              'no longer display locked');
}, 'Animation events do not fire for a CSS animation running in a display ' +
   'locked subtree');

promise_test(async t => {
  const container = document.getElementById('container');
  const target = createAnimatingElement(t, 'animate');
  const animation = target.getAnimations()[0];
  await animation.ready;
  let finishedWhileDisplayLocked = false;
  animation.finished.then(() => {
    finishedWhileDisplayLocked =
        getComputedStyle(container).contentVisibility == 'hidden';
  });
  await waitForAnimationFrames(1);
  container.style.contentVisibility = 'hidden';
  // Advance to just shy of the effect end.
  animation.currentTime = 1999;
  assert_approx_equals(
      parseFloat(getComputedStyle(target).opacity), 0.999, 1e-6,
                'Computed style is updated even when the animation is ' +
                'running in a display locked subtree');
  // Advancing frames should not resolve the finished promise.
  await waitForAnimationFrames(3);
  container.style.contentVisibility = 'visible';
  // Now we can resolve the finished promise.
  await animation.finished;
  assert_equals(finishedWhileDisplayLocked, false);
}, 'The finished promise does not resolve due to the normal passage of time  ' +
   'for a CSS animation in a display locked subtree');

promise_test(async t => {
  const container = document.getElementById('container');
  await waitForAnimationFrames(1);
  const target = createAnimatingElement(t, 'transition');
  await waitForAnimationFrames(1);
  target.style.opacity = 0;
  const animation = target.getAnimations()[0];
  await animation.ready;
  let finishedWhileDisplayLocked = false;
  animation.finished.then(() => {
    finishedWhileDisplayLocked =
        getComputedStyle(container).contentVisibility == 'hidden';
  });
  await waitForAnimationFrames(1);
  container.style.contentVisibility = 'hidden';
  // Advance to just shy of the effect end.
  animation.currentTime = 999;
  assert_approx_equals(
      parseFloat(getComputedStyle(target).opacity), 0.001, 1e-6,
                'Computed style is updated even when the animation is ' +
                'running in a display locked subtree');
  // Advancing frames should not resolve the finished promise.
  await waitForAnimationFrames(3);
  container.style.contentVisibility = 'visible';
  // Now we can resolve the finished promise.
  await animation.finished;
  assert_equals(finishedWhileDisplayLocked, false);
}, 'The finished promise does not resolve due to the normal passage of time  ' +
   'for a CSS transition in a display locked subtree');

promise_test(async t => {
  const container = document.getElementById('container');
  const target = createAnimatingElement(t, 'animate');
  const animation = target.getAnimations()[0];
  target.className = '';
  container.style.contentVisibility = 'hidden';
  assert_equals(target.getAnimations().length, 0);

  // Though originally a CSS animation, it is no longer associated with
  // CSS rules and no longer has an owning element. It now behaves like a
  // programmatic web animation. Animation playback events (but not CSS
  // animation events) should be dispatched and promises resolved despite
  // being in a display locked subtree.

  let cssAnimationEndEvent = false;
  target.addEventListener('animationend', () => {
    cssAnimationEndEvent = true;
  });

  let animationFinishEvent = false;
  animation.addEventListener('finish', () => {
    animationFinishEvent = true;
  });

  let animationFinished = false;
  animation.finished.then(() => {
    animationFinished = true;
  });

  animation.play();
  assert_equals(target.getAnimations().length, 1);

  animation.currentTime = 1999;
  await animation.ready;
  await waitForAnimationFrames(2);

  assert_true(animationFinishEvent,
              'Animation event not blocked on display locked subtree if ' +
              'no owning element');
  assert_true(animationFinished,
              'Finished promise not blocked on display locked subtrtee if ' +
              'no owning element');
  assert_false(cssAnimationEndEvent,
              'CSS animation events should not be dispatched if there is no ' +
              'owning element');
}, 'Events and promises are handled normally for animations without an ' +
   'owning element');

</script>