Bug 1155927 - Make sure that browser_animation_actors_15.js waits for the transitions to end. r+a=test-only
authorPatrick Brosset <pbrosset@mozilla.com>
Thu, 02 Jul 2015 10:02:27 +0200
changeset 275247 0cdd7df90394e539c54f388c29a2032e4120c105
parent 275246 895816df56b0d9c62ca960e03786b32fcb0adaa7
child 275248 a788bdae916d6fe385245d33ecae2e429c7c6ca2
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1155927
milestone40.0
Bug 1155927 - Make sure that browser_animation_actors_15.js waits for the transitions to end. r+a=test-only
toolkit/devtools/server/tests/browser/browser_animation_actors_15.js
--- a/toolkit/devtools/server/tests/browser/browser_animation_actors_15.js
+++ b/toolkit/devtools/server/tests/browser/browser_animation_actors_15.js
@@ -7,65 +7,80 @@
 // 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.
 
 const {AnimationsFront} = require("devtools/server/actors/animation");
 const {InspectorFront} = require("devtools/server/actors/inspector");
 
 add_task(function*() {
-  let doc = yield addTab(MAIN_DOMAIN + "animation.html");
+  yield addTab(MAIN_DOMAIN + "animation.html");
 
   initDebuggerServer();
   let client = new DebuggerClient(DebuggerServer.connectPipe());
   let form = yield connectDebuggerClient(client);
   let inspector = InspectorFront(client, form);
   let walker = yield inspector.getWalker();
   let animations = AnimationsFront(client, form);
 
   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("Listen for new animations");
-  let reportedMutations = [];
-  function onMutations(mutations) {
-    reportedMutations = [...reportedMutations, ...mutations];
-  }
-  animations.on("mutations", onMutations);
-
-  info("Transition the node by adding the expand class");
+  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");
-  info("Wait for longer than the transition");
-  yield wait(500);
+  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'");
 
-  reportedMutations = [];
+  info("Wait for the transitions to be finished");
+  yield waitForEnd(reportedMutations[0].player);
+  yield waitForEnd(reportedMutations[1].player);
 
-  info("Transition back by removing the expand class");
+  info("Play the transition back by removing the class, wait for mutations");
+  onMutations = expectMutationEvents(animations, 4);
   cpow.classList.remove("expand");
-  info("Wait for longer than the transition");
-  yield wait(500);
+  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)");
 
-  animations.off("mutations", onMutations);
-
   yield closeDebuggerClient(client);
   gBrowser.removeCurrentTab();
 });
 
-function wait(ms) {
+function expectMutationEvents(animationsFront, nbOfEvents) {
   return new Promise(resolve => {
-    setTimeout(resolve, ms);
+    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);
+  }
+}