browser_animation_refreshTransitions.js (3295B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // When a transition finishes, no "removed" event is sent because it may still 7 // be used, but when it restarts again (transitions back), then a new 8 // AnimationPlayerFront should be sent, and the old one should be removed. 9 10 add_task(async function () { 11 const { target, walker, animations } = await initAnimationsFrontForUrl( 12 MAIN_DOMAIN + "animation.html" 13 ); 14 15 info("Retrieve the test node"); 16 const node = await walker.querySelector(walker.rootNode, ".all-transitions"); 17 18 info("Retrieve the animation players for the node"); 19 const players = await animations.getAnimationPlayersForNode(node); 20 is(players.length, 0, "The node has no animation players yet"); 21 22 info("Play a transition by adding the expand class, wait for mutations"); 23 let onMutations = expectMutationEvents(animations, 2); 24 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 25 const el = content.document.querySelector(".all-transitions"); 26 el.classList.add("expand"); 27 }); 28 let reportedMutations = await onMutations; 29 30 is(reportedMutations.length, 2, "2 mutation events were received"); 31 is(reportedMutations[0].type, "added", "The first event was 'added'"); 32 is(reportedMutations[1].type, "added", "The second event was 'added'"); 33 34 info("Wait for the transitions to be finished"); 35 await waitForEnd(reportedMutations[0].player); 36 await waitForEnd(reportedMutations[1].player); 37 38 info("Play the transition back by removing the class, wait for mutations"); 39 onMutations = expectMutationEvents(animations, 4); 40 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 41 const el = content.document.querySelector(".all-transitions"); 42 el.classList.remove("expand"); 43 }); 44 reportedMutations = await onMutations; 45 46 is(reportedMutations.length, 4, "4 new mutation events were received"); 47 is( 48 reportedMutations.filter(m => m.type === "removed").length, 49 2, 50 "2 'removed' events were sent (for the old transitions)" 51 ); 52 is( 53 reportedMutations.filter(m => m.type === "added").length, 54 2, 55 "2 'added' events were sent (for the new transitions)" 56 ); 57 58 await target.destroy(); 59 gBrowser.removeCurrentTab(); 60 }); 61 62 function expectMutationEvents(animationsFront, nbOfEvents) { 63 return new Promise(resolve => { 64 let reportedMutations = []; 65 function onMutations(mutations) { 66 reportedMutations = [...reportedMutations, ...mutations]; 67 info( 68 "Received " + 69 reportedMutations.length + 70 " mutation events, " + 71 "expecting " + 72 nbOfEvents 73 ); 74 if (reportedMutations.length === nbOfEvents) { 75 animationsFront.off("mutations", onMutations); 76 resolve(reportedMutations); 77 } 78 } 79 80 info("Start listening for mutation events from the AnimationsFront"); 81 animationsFront.on("mutations", onMutations); 82 }); 83 } 84 85 async function waitForEnd(animationFront) { 86 let playState; 87 while (playState !== "finished") { 88 const state = await animationFront.getCurrentState(); 89 playState = state.playState; 90 info( 91 "Wait for transition " + 92 animationFront.state.name + 93 " to finish, playState=" + 94 playState 95 ); 96 } 97 }