summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/WebKit/Source/core/animation/EffectInputTest.cpp
blob: d8e174ce2a032b486814dd89f86dad205ea8fd26 (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
// Copyright 2014 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.

#include "config.h"
#include "core/animation/EffectInput.h"

#include "bindings/v8/Dictionary.h"
#include "core/animation/AnimationTestHelper.h"
#include "core/animation/KeyframeEffectModel.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include <gtest/gtest.h>
#include <v8.h>

using namespace WebCore;

namespace {

class AnimationEffectInputTest : public ::testing::Test {
protected:
    AnimationEffectInputTest()
        : document(Document::create())
        , element(document->createElement("foo", ASSERT_NO_EXCEPTION))
        , m_isolate(v8::Isolate::GetCurrent())
        , m_scope(m_isolate)
    {
    }

    RefPtrWillBePersistent<Document> document;
    RefPtrWillBePersistent<Element> element;
    TrackExceptionState exceptionState;
    v8::Isolate* m_isolate;

private:
    V8TestingScope m_scope;
};

TEST_F(AnimationEffectInputTest, SortedOffsets)
{
    Vector<Dictionary> jsKeyframes;
    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);

    setV8ObjectPropertyAsString(keyframe1, "width", "100px");
    setV8ObjectPropertyAsString(keyframe1, "offset", "0");
    setV8ObjectPropertyAsString(keyframe2, "width", "0px");
    setV8ObjectPropertyAsString(keyframe2, "offset", "1");

    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
    jsKeyframes.append(Dictionary(keyframe2, m_isolate));

    RefPtrWillBeRawPtr<AnimationEffect> animationEffect = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
    EXPECT_FALSE(exceptionState.hadException());
    const KeyframeEffectModelBase& keyframeEffect = *toKeyframeEffectModelBase(animationEffect.get());
    EXPECT_EQ(1.0, keyframeEffect.getFrames()[1]->offset());
}

TEST_F(AnimationEffectInputTest, UnsortedOffsets)
{
    Vector<Dictionary> jsKeyframes;
    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);

    setV8ObjectPropertyAsString(keyframe1, "width", "0px");
    setV8ObjectPropertyAsString(keyframe1, "offset", "1");
    setV8ObjectPropertyAsString(keyframe2, "width", "100px");
    setV8ObjectPropertyAsString(keyframe2, "offset", "0");

    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
    jsKeyframes.append(Dictionary(keyframe2, m_isolate));

    RefPtrWillBeRawPtr<AnimationEffect> animationEffect = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
    EXPECT_FALSE(exceptionState.hadException());
    const KeyframeEffectModelBase& keyframeEffect = *toKeyframeEffectModelBase(animationEffect.get());
    EXPECT_EQ(1.0, keyframeEffect.getFrames()[1]->offset());
}

TEST_F(AnimationEffectInputTest, LooslySorted)
{
    Vector<Dictionary> jsKeyframes;
    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);
    v8::Handle<v8::Object> keyframe3 = v8::Object::New(m_isolate);

    setV8ObjectPropertyAsString(keyframe1, "width", "100px");
    setV8ObjectPropertyAsString(keyframe1, "offset", "0");
    setV8ObjectPropertyAsString(keyframe2, "width", "200px");
    setV8ObjectPropertyAsString(keyframe3, "width", "0px");
    setV8ObjectPropertyAsString(keyframe3, "offset", "1");

    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
    jsKeyframes.append(Dictionary(keyframe2, m_isolate));
    jsKeyframes.append(Dictionary(keyframe3, m_isolate));

    RefPtrWillBeRawPtr<AnimationEffect> animationEffect = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
    EXPECT_FALSE(exceptionState.hadException());
    const KeyframeEffectModelBase& keyframeEffect = *toKeyframeEffectModelBase(animationEffect.get());
    EXPECT_EQ(1, keyframeEffect.getFrames()[2]->offset());
}

TEST_F(AnimationEffectInputTest, Invalid)
{
    // Not loosely sorted by offset, and there exists a keyframe with null offset.
    Vector<Dictionary> jsKeyframes;
    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);
    v8::Handle<v8::Object> keyframe3 = v8::Object::New(m_isolate);

    setV8ObjectPropertyAsString(keyframe1, "width", "0px");
    setV8ObjectPropertyAsString(keyframe1, "offset", "1");
    setV8ObjectPropertyAsString(keyframe2, "width", "200px");
    setV8ObjectPropertyAsString(keyframe3, "width", "100px");
    setV8ObjectPropertyAsString(keyframe3, "offset", "0");

    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
    jsKeyframes.append(Dictionary(keyframe2, m_isolate));
    jsKeyframes.append(Dictionary(keyframe3, m_isolate));

    RefPtrWillBeRawPtr<AnimationEffect> animationEffect ALLOW_UNUSED = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
    EXPECT_TRUE(exceptionState.hadException());
    EXPECT_EQ(InvalidModificationError, exceptionState.code());
}

}