1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef ASH_TEST_TEST_SESSION_STATE_ANIMATOR_H_
6#define ASH_TEST_TEST_SESSION_STATE_ANIMATOR_H_
7
8#include <map>
9#include <vector>
10
11#include "ash/ash_export.h"
12#include "ash/wm/session_state_animator.h"
13#include "base/basictypes.h"
14#include "base/time/time.h"
15
16namespace ash {
17namespace test {
18
19// A SessionStateAnimator that offers control over the lifetime of active
20// animations.
21// NOTE: The TestSessionStateAnimator limits each
22// SessionStateAnimator::Container to a single active animation at any one time.
23// If a new animation is started on a container the existing one will be
24// aborted.
25class TestSessionStateAnimator : public SessionStateAnimator {
26 public:
27  TestSessionStateAnimator();
28  virtual ~TestSessionStateAnimator();
29
30  int last_animation_epoch() {
31    return last_animation_epoch_;
32  }
33
34  // Resets the current animation epoch back to 0 and aborts all currently
35  // active animations.
36  void ResetAnimationEpoch();
37
38  // Advances all contained animations by the specified |duration|.  Any
39  // animations that will have completed after |duration| will have its
40  // callback called.
41  void Advance(const base::TimeDelta& duration);
42
43  // Simulates running all of the contained animations to completion.  Each
44  // contained AnimationSequence will have OnAnimationCompleted called if
45  // |completed_successfully| is true and OnAnimationAborted called if false.
46  void CompleteAnimations(int animation_epoch, bool completed_successfully);
47
48  // Convenience method that calls CompleteAnimations with the last
49  // |animation_epoch|.  In effect this will complete all animations.
50  // See CompleteAnimations for more documenation on |completed_succesffully|.
51  void CompleteAllAnimations(bool completed_successfully);
52
53  // Returns true if there is an animation active with |type| for the given
54  // |container|.
55  bool IsContainerAnimated(SessionStateAnimator::Container container,
56                           SessionStateAnimator::AnimationType type) const;
57
58  // Returns true if there is an animation active with |type| for all the given
59  // containers specified by |container_mask|.
60  bool AreContainersAnimated(int container_mask,
61                             SessionStateAnimator::AnimationType type) const;
62
63  // Returns the number of active animations.
64  size_t GetAnimationCount() const;
65
66  // ash::SessionStateAnimator:
67  virtual void StartAnimation(int container_mask,
68                              AnimationType type,
69                              AnimationSpeed speed) OVERRIDE;
70  virtual void StartAnimationWithCallback(
71      int container_mask,
72      AnimationType type,
73      AnimationSpeed speed,
74      base::Closure callback) OVERRIDE;
75  virtual AnimationSequence* BeginAnimationSequence(
76      base::Closure callback) OVERRIDE;
77  virtual bool IsBackgroundHidden() const OVERRIDE;
78  virtual void ShowBackground() OVERRIDE;
79  virtual void HideBackground() OVERRIDE;
80
81 private:
82  class AnimationSequence;
83  friend class AnimationSequence;
84
85  // Data structure to track the currently active animations and their
86  // callbacks.
87  struct ActiveAnimation {
88    ActiveAnimation(
89        int animation_epoch,
90        base::TimeDelta duration,
91        SessionStateAnimator::Container container,
92        AnimationType type,
93        AnimationSpeed speed,
94        base::Closure success_callback,
95        base::Closure failed_callback);
96    virtual ~ActiveAnimation();
97
98    // The time epoch that this animation was scheduled.
99    int animation_epoch;
100
101    // The time remaining for this animation.
102    base::TimeDelta remaining_duration;
103
104    // The container which is being animated.
105    SessionStateAnimator::Container container;
106
107    // The animation type that is being done.
108    AnimationType type;
109
110    // The speed at which the animation is being done.
111    AnimationSpeed speed;
112
113    // The callback to be invoked upon a successful completion.
114    base::Closure success_callback;
115
116    // The callback to be invoked upon an unsuccessful completion.
117    base::Closure failed_callback;
118  };
119
120  typedef std::vector<ActiveAnimation> AnimationList;
121  typedef std::map<SessionStateAnimator::Container, AnimationList>
122      ActiveAnimationsMap;
123
124  // Starts an animation in the |animation_sequence| for each container
125  // specified by |container_mask| with the given |type| and |speed|.
126  virtual void StartAnimationInSequence(
127      int container_mask,
128      AnimationType type,
129      AnimationSpeed speed,
130      AnimationSequence* animation_sequence);
131
132  // Adds a single animation to the currently active animations.  If an
133  // animation is already active for the given |container| then it will be
134  // replaced by the new one.  The existing animation will be aborted by calling
135  // OnAnimationAborted.
136  void AddAnimation(SessionStateAnimator::Container container,
137                    AnimationType type,
138                    AnimationSpeed speed,
139                    base::Closure success_callback,
140                    base::Closure failed_callback);
141
142  // If an animation is currently active for the given |container| it will be
143  // aborted by invoking OnAnimationAborted and removed from the list of active
144  // animations.
145  void AbortAnimation(SessionStateAnimator::Container container);
146
147  // Used for easy iteration over all the containers.
148  static const SessionStateAnimator::Container kAllContainers[];
149
150  // A map of currently active animations.
151  ActiveAnimationsMap active_animations_;
152
153  // A time counter that tracks the last scheduled animation or animation
154  // sequence.
155  int last_animation_epoch_;
156
157  // Tracks whether the background is hidden or not.
158  bool is_background_hidden_;
159
160  DISALLOW_COPY_AND_ASSIGN(TestSessionStateAnimator);
161};
162
163}  // namespace test
164}  // namespace ash
165
166#endif  // ASH_TEST_TEST_SESSION_STATE_ANIMATOR_H_
167