1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
25#ifndef CompositorAnimationsTestHelper_h
26#define CompositorAnimationsTestHelper_h
27
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"
34
35#include <gmock/gmock.h>
36#include <gtest/gtest.h>
37
38
39namespace testing {
40
41template<typename T>
42PassOwnPtr<T> CloneToPassOwnPtr(T& o)
43{
44    return adoptPtr(new T(o));
45}
46
47} // namespace testing
48
49
50// Test helpers and mocks for Web* types
51// -----------------------------------------------------------------------
52namespace blink {
53
54// WebFloatKeyframe is a plain struct, so we just create an == operator
55// for it.
56inline bool operator==(const WebFloatKeyframe& a, const WebFloatKeyframe& b)
57{
58    return a.time == b.time && a.value == b.value;
59}
60
61inline void PrintTo(const WebFloatKeyframe& frame, ::std::ostream* os)
62{
63    *os << "WebFloatKeyframe@" << &frame << "(" << frame.time << ", " << frame.value << ")";
64}
65
66// -----------------------------------------------------------------------
67
68class WebCompositorAnimationMock : public WebCompositorAnimation {
69private:
70    WebCompositorAnimation::TargetProperty m_property;
71
72public:
73    // Target Property is set through the constructor.
74    WebCompositorAnimationMock(WebCompositorAnimation::TargetProperty p) : m_property(p) { }
75    virtual WebCompositorAnimation::TargetProperty targetProperty() const { return m_property; };
76
77    MOCK_METHOD0(id, int());
78
79    MOCK_CONST_METHOD0(iterations, double());
80    MOCK_METHOD1(setIterations, void(double));
81
82    MOCK_CONST_METHOD0(iterationStart, double());
83    MOCK_METHOD1(setIterationStart, void(double));
84
85    MOCK_CONST_METHOD0(startTime, double());
86    MOCK_METHOD1(setStartTime, void(double));
87
88    MOCK_CONST_METHOD0(timeOffset, double());
89    MOCK_METHOD1(setTimeOffset, void(double));
90
91    MOCK_CONST_METHOD0(direction, Direction());
92    MOCK_METHOD1(setDirection, void(Direction));
93
94    MOCK_CONST_METHOD0(playbackRate, double());
95    MOCK_METHOD1(setPlaybackRate, void(double));
96
97    MOCK_CONST_METHOD0(fillMode, FillMode());
98    MOCK_METHOD1(setFillMode, void(FillMode));
99
100    MOCK_METHOD0(delete_, void());
101    ~WebCompositorAnimationMock() { delete_(); }
102};
103
104template<typename CurveType, WebCompositorAnimationCurve::AnimationCurveType CurveId, typename KeyframeType>
105class WebCompositorAnimationCurveMock : public CurveType {
106public:
107    MOCK_METHOD1_T(add, void(const KeyframeType&));
108    MOCK_METHOD2_T(add, void(const KeyframeType&, WebCompositorAnimationCurve::TimingFunctionType));
109    MOCK_METHOD5_T(add, void(const KeyframeType&, double, double, double, double));
110
111    MOCK_CONST_METHOD1_T(getValue, float(double)); // Only on WebFloatAnimationCurve, but can't hurt to have here.
112
113    virtual WebCompositorAnimationCurve::AnimationCurveType type() const { return CurveId; };
114
115    MOCK_METHOD0(delete_, void());
116    ~WebCompositorAnimationCurveMock() { delete_(); }
117};
118
119typedef WebCompositorAnimationCurveMock<WebFloatAnimationCurve, WebCompositorAnimationCurve::AnimationCurveTypeFloat, WebFloatKeyframe> WebFloatAnimationCurveMock;
120
121} // namespace blink
122
123namespace blink {
124
125class AnimationCompositorAnimationsTestBase : public ::testing::Test {
126public:
127    AnimationCompositorAnimationsTestBase() : m_proxyPlatform(&m_mockCompositor) { };
128
129    class WebCompositorSupportMock : public WebCompositorSupport {
130    public:
131        MOCK_METHOD3(createAnimation, WebCompositorAnimation*(const WebCompositorAnimationCurve& curve, WebCompositorAnimation::TargetProperty target, int animationId));
132        MOCK_METHOD0(createFloatAnimationCurve, WebFloatAnimationCurve*());
133    };
134
135private:
136    class PlatformProxy : public Platform {
137    public:
138        PlatformProxy(WebCompositorSupportMock** compositor) : m_compositor(compositor) { }
139
140        virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t length) { ASSERT_NOT_REACHED(); }
141    private:
142        WebCompositorSupportMock** m_compositor;
143        virtual WebCompositorSupport* compositorSupport() OVERRIDE { return *m_compositor; }
144    };
145
146    WebCompositorSupportMock* m_mockCompositor;
147    PlatformProxy m_proxyPlatform;
148
149protected:
150    Platform* m_platform;
151
152    virtual void SetUp()
153    {
154        m_mockCompositor = 0;
155        m_platform = Platform::current();
156        Platform::initialize(&m_proxyPlatform);
157    }
158
159    virtual void TearDown()
160    {
161        Platform::initialize(m_platform);
162    }
163
164    void setCompositorForTesting(WebCompositorSupportMock& mock)
165    {
166        ASSERT(!m_mockCompositor);
167        m_mockCompositor = &mock;
168    }
169};
170
171}
172
173#endif
174