2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #ifndef CompositorAnimationsTestHelper_h
26 #define CompositorAnimationsTestHelper_h
28 #include "core/animation/CompositorAnimations.h"
29 #include "public/platform/Platform.h"
30 #include "public/platform/WebCompositorSupport.h"
31 #include "public/platform/WebFloatAnimationCurve.h"
32 #include "public/platform/WebFloatKeyframe.h"
33 #include "wtf/PassOwnPtr.h"
35 #include <gmock/gmock.h>
36 #include <gtest/gtest.h>
42 PassOwnPtr<T> CloneToPassOwnPtr(T& o)
44 return adoptPtr(new T(o));
47 } // namespace testing
50 // Test helpers and mocks for blink::Web* types
51 // -----------------------------------------------------------------------
54 // blink::WebFloatKeyframe is a plain struct, so we just create an == operator
56 inline bool operator==(const WebFloatKeyframe& a, const WebFloatKeyframe& b)
58 return a.time == b.time && a.value == b.value;
61 inline void PrintTo(const WebFloatKeyframe& frame, ::std::ostream* os)
63 *os << "WebFloatKeyframe@" << &frame << "(" << frame.time << ", " << frame.value << ")";
66 // -----------------------------------------------------------------------
68 class WebAnimationMock : public blink::WebAnimation {
70 blink::WebAnimation::TargetProperty m_property;
73 // Target Property is set through the constructor.
74 WebAnimationMock(blink::WebAnimation::TargetProperty p) : m_property(p) { }
75 virtual blink::WebAnimation::TargetProperty targetProperty() const { return m_property; };
77 MOCK_METHOD0(id, int());
79 MOCK_CONST_METHOD0(iterations, int());
80 MOCK_METHOD1(setIterations, void(int));
82 MOCK_CONST_METHOD0(startTime, double());
83 MOCK_METHOD1(setStartTime, void(double));
85 MOCK_CONST_METHOD0(timeOffset, double());
86 MOCK_METHOD1(setTimeOffset, void(double));
88 MOCK_CONST_METHOD0(alternatesDirection, bool());
89 MOCK_METHOD1(setAlternatesDirection, void(bool));
91 MOCK_METHOD0(delete_, void());
92 ~WebAnimationMock() { delete_(); }
95 template<typename CurveType, blink::WebAnimationCurve::AnimationCurveType CurveId, typename KeyframeType>
96 class WebAnimationCurveMock : public CurveType {
98 MOCK_METHOD1_T(add, void(const KeyframeType&));
99 MOCK_METHOD2_T(add, void(const KeyframeType&, blink::WebAnimationCurve::TimingFunctionType));
100 MOCK_METHOD5_T(add, void(const KeyframeType&, double, double, double, double));
102 MOCK_CONST_METHOD1_T(getValue, float(double)); // Only on WebFloatAnimationCurve, but can't hurt to have here.
104 virtual blink::WebAnimationCurve::AnimationCurveType type() const { return CurveId; };
106 MOCK_METHOD0(delete_, void());
107 ~WebAnimationCurveMock() { delete_(); }
110 typedef WebAnimationCurveMock<blink::WebFloatAnimationCurve, blink::WebAnimationCurve::AnimationCurveTypeFloat, blink::WebFloatKeyframe> WebFloatAnimationCurveMock;
116 class AnimationCompositorAnimationsTestBase : public ::testing::Test {
118 AnimationCompositorAnimationsTestBase() : m_proxyPlatform(&m_mockCompositor) { };
120 class WebCompositorSupportMock : public blink::WebCompositorSupport {
122 MOCK_METHOD3(createAnimation, blink::WebAnimation*(const blink::WebAnimationCurve& curve, blink::WebAnimation::TargetProperty target, int animationId));
123 MOCK_METHOD0(createFloatAnimationCurve, blink::WebFloatAnimationCurve*());
127 class PlatformProxy : public blink::Platform {
129 PlatformProxy(WebCompositorSupportMock** compositor) : m_compositor(compositor) { }
131 virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t length) { ASSERT_NOT_REACHED(); }
133 WebCompositorSupportMock** m_compositor;
134 virtual blink::WebCompositorSupport* compositorSupport() OVERRIDE { return *m_compositor; }
137 WebCompositorSupportMock* m_mockCompositor;
138 PlatformProxy m_proxyPlatform;
141 blink::Platform* m_platform;
145 m_mockCompositor = 0;
146 m_platform = blink::Platform::current();
147 blink::Platform::initialize(&m_proxyPlatform);
150 virtual void TearDown()
152 blink::Platform::initialize(m_platform);
155 void setCompositorForTesting(WebCompositorSupportMock& mock)
157 ASSERT(!m_mockCompositor);
158 m_mockCompositor = &mock;