Home | History | Annotate | Download | only in animation
      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 
     39 namespace testing {
     40 
     41 template<typename T>
     42 PassOwnPtr<T> CloneToPassOwnPtr(T& o)
     43 {
     44     return adoptPtr(new T(o));
     45 }
     46 
     47 } // namespace testing
     48 
     49 
     50 // Test helpers and mocks for blink::Web* types
     51 // -----------------------------------------------------------------------
     52 namespace blink {
     53 
     54 // blink::WebFloatKeyframe is a plain struct, so we just create an == operator
     55 // for it.
     56 inline bool operator==(const WebFloatKeyframe& a, const WebFloatKeyframe& b)
     57 {
     58     return a.time == b.time && a.value == b.value;
     59 }
     60 
     61 inline void PrintTo(const WebFloatKeyframe& frame, ::std::ostream* os)
     62 {
     63     *os << "WebFloatKeyframe@" << &frame << "(" << frame.time << ", " << frame.value << ")";
     64 }
     65 
     66 // -----------------------------------------------------------------------
     67 
     68 class WebAnimationMock : public blink::WebAnimation {
     69 private:
     70     blink::WebAnimation::TargetProperty m_property;
     71 
     72 public:
     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; };
     76 
     77     MOCK_METHOD0(id, int());
     78 
     79     MOCK_CONST_METHOD0(iterations, int());
     80     MOCK_METHOD1(setIterations, void(int));
     81 
     82     MOCK_CONST_METHOD0(startTime, double());
     83     MOCK_METHOD1(setStartTime, void(double));
     84 
     85     MOCK_CONST_METHOD0(timeOffset, double());
     86     MOCK_METHOD1(setTimeOffset, void(double));
     87 
     88     MOCK_CONST_METHOD0(alternatesDirection, bool());
     89     MOCK_METHOD1(setAlternatesDirection, void(bool));
     90 
     91     MOCK_METHOD0(delete_, void());
     92     ~WebAnimationMock() { delete_(); }
     93 };
     94 
     95 template<typename CurveType, blink::WebAnimationCurve::AnimationCurveType CurveId, typename KeyframeType>
     96 class WebAnimationCurveMock : public CurveType {
     97 public:
     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));
    101 
    102     MOCK_CONST_METHOD1_T(getValue, float(double)); // Only on WebFloatAnimationCurve, but can't hurt to have here.
    103 
    104     virtual blink::WebAnimationCurve::AnimationCurveType type() const { return CurveId; };
    105 
    106     MOCK_METHOD0(delete_, void());
    107     ~WebAnimationCurveMock() { delete_(); }
    108 };
    109 
    110 typedef WebAnimationCurveMock<blink::WebFloatAnimationCurve, blink::WebAnimationCurve::AnimationCurveTypeFloat, blink::WebFloatKeyframe> WebFloatAnimationCurveMock;
    111 
    112 } // namespace blink
    113 
    114 namespace WebCore {
    115 
    116 class AnimationCompositorAnimationsTestBase : public ::testing::Test {
    117 public:
    118     AnimationCompositorAnimationsTestBase() : m_proxyPlatform(&m_mockCompositor) { };
    119 
    120     class WebCompositorSupportMock : public blink::WebCompositorSupport {
    121     public:
    122         MOCK_METHOD3(createAnimation, blink::WebAnimation*(const blink::WebAnimationCurve& curve, blink::WebAnimation::TargetProperty target, int animationId));
    123         MOCK_METHOD0(createFloatAnimationCurve, blink::WebFloatAnimationCurve*());
    124     };
    125 
    126 private:
    127     class PlatformProxy : public blink::Platform {
    128     public:
    129         PlatformProxy(WebCompositorSupportMock** compositor) : m_compositor(compositor) { }
    130 
    131         virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t length) { ASSERT_NOT_REACHED(); }
    132     private:
    133         WebCompositorSupportMock** m_compositor;
    134         blink::WebCompositorSupport* compositorSupport() OVERRIDE { return *m_compositor; }
    135     };
    136 
    137     WebCompositorSupportMock* m_mockCompositor;
    138     PlatformProxy m_proxyPlatform;
    139 
    140 protected:
    141     blink::Platform* m_platform;
    142 
    143     virtual void SetUp()
    144     {
    145         m_mockCompositor = 0;
    146         m_platform = blink::Platform::current();
    147         blink::Platform::initialize(&m_proxyPlatform);
    148     }
    149 
    150     virtual void TearDown()
    151     {
    152         blink::Platform::initialize(m_platform);
    153     }
    154 
    155     void setCompositorForTesting(WebCompositorSupportMock& mock)
    156     {
    157         ASSERT(!m_mockCompositor);
    158         m_mockCompositor = &mock;
    159     }
    160 };
    161 
    162 }
    163 
    164 #endif
    165