summaryrefslogtreecommitdiff
path: root/chromium/third_party/trace-viewer/src/ui/animation_controller_test.js
blob: 96e83631fd804ee626ab17d4919e3fc0dffc2e09 (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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

base.require('base.utils');
base.require('ui.animation_controller');

base.unittest.testSuite('ui.animation_controller', function() {
  function SimpleAnimation(options) {
    this.stopTime = options.stopTime;

    this.startCalled = false;
    this.didStopEarlyCalled = false;
    this.wasTakenOver = false;
    this.tickCount = 0;
  }

  SimpleAnimation.prototype = {
    __proto__: ui.Animation.prototype,

    canTakeOverFor: function(existingAnimation) {
      return false;
    },

    takeOverFor: function(existingAnimation, newStartTimestamp, target) {
      throw new Error('Not implemented');
    },

    start: function(timestamp, target) {
      this.startCalled = true;
    },

    didStopEarly: function(timestamp, target, willBeTakenOver) {
      this.didStopEarlyCalled = true;
      this.wasTakenOver = willBeTakenOver;
    },

    /**
     * @return {boolean} true if the animation is finished.
     */
    tick: function(timestamp, target) {
      this.tickCount++;
      return timestamp >= this.stopTime;
    }
  };

  test('cancel', function() {
    var target = {
      x: 0,
      cloneAnimationState: function() { return {x: this.x}; }
    };

    var controller = new ui.AnimationController();
    controller.target = target;

    var animation = new SimpleAnimation({stopTime: 100});
    controller.queueAnimation(animation);

    base.forcePendingRAFTasksToRun(0);
    assertEquals(1, animation.tickCount);
    controller.cancelActiveAnimation();
    assertFalse(controller.hasActiveAnimation);
    assertTrue(animation.didStopEarlyCalled);
  });

  test('simple', function() {
    var target = {
      x: 0,
      cloneAnimationState: function() { return {x: this.x}; }
    };

    var controller = new ui.AnimationController();
    controller.target = target;

    var animation = new SimpleAnimation({stopTime: 100});
    controller.queueAnimation(animation);

    base.forcePendingRAFTasksToRun(0);
    assertEquals(1, animation.tickCount);
    assertTrue(controller.hasActiveAnimation);

    base.forcePendingRAFTasksToRun(100);
    assertEquals(2, animation.tickCount);
    assertFalse(controller.hasActiveAnimation);
  });

  test('queueTwo', function() {
    // Clear all pending rafs so if something is lingering it will blow up here.
    base.forcePendingRAFTasksToRun(0);

    var target = {
      x: 0,
      cloneAnimationState: function() { return {x: this.x}; }
    };

    var controller = new ui.AnimationController();
    controller.target = target;

    var a1 = new SimpleAnimation({stopTime: 100});
    var a2 = new SimpleAnimation({stopTime: 100});
    controller.queueAnimation(a1, 0);
    assertTrue(a1.startCalled);
    controller.queueAnimation(a2, 50);
    assertTrue(a1.didStopEarlyCalled);
    assertTrue(a2.startCalled);

    base.forcePendingRAFTasksToRun(150);
    assertFalse(controller.hasActiveAnimation);
    assertTrue(a2.tickCount > 0);
  });

  /**
   * @constructor
   */
  function AnimationThatCanTakeOverForSimpleAnimation() {
    this.takeOverForAnimation = undefined;
  }

  AnimationThatCanTakeOverForSimpleAnimation.prototype = {
    __proto__: ui.Animation.prototype,


    canTakeOverFor: function(existingAnimation) {
      return existingAnimation instanceof SimpleAnimation;
    },

    takeOverFor: function(existingAnimation, newStartTimestamp, target) {
      this.takeOverForAnimation = existingAnimation;
    },

    start: function(timestamp, target) {
      this.startCalled = true;
    }
  };

  test('takeOver', function() {
    var target = {
      x: 0,
      cloneAnimationState: function() { return {x: this.x}; }
    };

    var controller = new ui.AnimationController();
    controller.target = target;

    var a1 = new SimpleAnimation({stopTime: 100});
    var a2 = new AnimationThatCanTakeOverForSimpleAnimation();
    controller.queueAnimation(a1, 0);
    assertTrue(a1.startCalled);
    assertEquals(0, a1.tickCount);
    controller.queueAnimation(a2, 10);
    assertTrue(a1.didStopEarlyCalled);
    assertTrue(a1.wasTakenOver);
    assertEquals(1, a1.tickCount);

    assertEquals(a2.takeOverForAnimation, a1);
    assertTrue(a2.startCalled);

    controller.cancelActiveAnimation();
    assertFalse(controller.hasActiveAnimation);
  });
});