summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/unit/test_animation_name.js
blob: 4cd708fc4d899ce6c595f94126efe446cdbaf891 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test that AnimationPlayerActor.getName returns the right name depending on
// the type of an animation and the various properties available on it.

const { AnimationPlayerActor } = require("devtools/server/actors/animation");

function run_test() {
  // Mock a window with just the properties the AnimationPlayerActor uses.
  let window = {
    MutationObserver: function () {
      this.observe = () => {};
    },
    Animation: function () {
      this.effect = {target: getMockNode()};
    },
    CSSAnimation: function () {
      this.effect = {target: getMockNode()};
    },
    CSSTransition: function () {
      this.effect = {target: getMockNode()};
    }
  };

  window.CSSAnimation.prototype = Object.create(window.Animation.prototype);
  window.CSSTransition.prototype = Object.create(window.Animation.prototype);

  // Helper to get a mock DOM node.
  function getMockNode() {
    return {
      ownerDocument: {
        defaultView: window
      }
    };
  }

  // Objects in this array should contain the following properties:
  // - desc {String} For logging
  // - animation {Object} An animation object instantiated from one of the mock
  //   window animation constructors.
  // - props {Objet} Properties of this object will be added to the animation
  //   object.
  // - expectedName {String} The expected name returned by
  //   AnimationPlayerActor.getName.
  const TEST_DATA = [{
    desc: "Animation with an id",
    animation: new window.Animation(),
    props: { id: "animation-id" },
    expectedName: "animation-id"
  }, {
    desc: "Animation without an id",
    animation: new window.Animation(),
    props: {},
    expectedName: ""
  }, {
    desc: "CSSTransition with an id",
    animation: new window.CSSTransition(),
    props: { id: "transition-with-id", transitionProperty: "width" },
    expectedName: "transition-with-id"
  }, {
    desc: "CSSAnimation with an id",
    animation: new window.CSSAnimation(),
    props: { id: "animation-with-id", animationName: "move" },
    expectedName: "animation-with-id"
  }, {
    desc: "CSSTransition without an id",
    animation: new window.CSSTransition(),
    props: { transitionProperty: "width" },
    expectedName: "width"
  }, {
    desc: "CSSAnimation without an id",
    animation: new window.CSSAnimation(),
    props: { animationName: "move" },
    expectedName: "move"
  }];

  for (let { desc, animation, props, expectedName } of TEST_DATA) {
    do_print(desc);
    for (let key in props) {
      animation[key] = props[key];
    }
    let actor = AnimationPlayerActor({}, animation);
    do_check_eq(actor.getName(), expectedName);
  }
}