summaryrefslogtreecommitdiff
path: root/devtools/server/tests/browser/browser_animation_refreshTransitions.js
blob: 088d55178b13d696277b00331a986d7b05b6ce70 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// When a transition finishes, no "removed" event is sent because it may still
// be used, but when it restarts again (transitions back), then a new
// AnimationPlayerFront should be sent, and the old one should be removed.

add_task(function* () {
  let {client, walker, animations} =
    yield initAnimationsFrontForUrl(MAIN_DOMAIN + "animation.html");

  info("Retrieve the test node");
  let node = yield walker.querySelector(walker.rootNode, ".all-transitions");

  info("Retrieve the animation players for the node");
  let players = yield animations.getAnimationPlayersForNode(node);
  is(players.length, 0, "The node has no animation players yet");

  info("Play a transition by adding the expand class, wait for mutations");
  let onMutations = expectMutationEvents(animations, 2);
  let cpow = content.document.querySelector(".all-transitions");
  cpow.classList.add("expand");
  let reportedMutations = yield onMutations;

  is(reportedMutations.length, 2, "2 mutation events were received");
  is(reportedMutations[0].type, "added", "The first event was 'added'");
  is(reportedMutations[1].type, "added", "The second event was 'added'");

  info("Wait for the transitions to be finished");
  yield waitForEnd(reportedMutations[0].player);
  yield waitForEnd(reportedMutations[1].player);

  info("Play the transition back by removing the class, wait for mutations");
  onMutations = expectMutationEvents(animations, 4);
  cpow.classList.remove("expand");
  reportedMutations = yield onMutations;

  is(reportedMutations.length, 4, "4 new mutation events were received");
  is(reportedMutations.filter(m => m.type === "removed").length, 2,
    "2 'removed' events were sent (for the old transitions)");
  is(reportedMutations.filter(m => m.type === "added").length, 2,
    "2 'added' events were sent (for the new transitions)");

  yield client.close();
  gBrowser.removeCurrentTab();
});

function expectMutationEvents(animationsFront, nbOfEvents) {
  return new Promise(resolve => {
    let reportedMutations = [];
    function onMutations(mutations) {
      reportedMutations = [...reportedMutations, ...mutations];
      info("Received " + reportedMutations.length + " mutation events, " +
           "expecting " + nbOfEvents);
      if (reportedMutations.length === nbOfEvents) {
        animationsFront.off("mutations", onMutations);
        resolve(reportedMutations);
      }
    }

    info("Start listening for mutation events from the AnimationsFront");
    animationsFront.on("mutations", onMutations);
  });
}

function* waitForEnd(animationFront) {
  let playState;
  while (playState !== "finished") {
    let state = yield animationFront.getCurrentState();
    playState = state.playState;
    info("Wait for transition " + animationFront.state.name +
         " to finish, playState=" + playState);
  }
}