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.
6 #include "core/animation/EffectInput.h"
8 #include "bindings/core/v8/Dictionary.h"
9 #include "bindings/core/v8/V8BindingForTesting.h"
10 #include "core/animation/AnimationTestHelper.h"
11 #include "core/animation/KeyframeEffectModel.h"
12 #include "core/dom/Document.h"
13 #include "core/dom/Element.h"
14 #include "core/dom/ExceptionCode.h"
15 #include "core/testing/DummyPageHolder.h"
16 #include <gtest/gtest.h>
21 class AnimationEffectInputTest
: public ::testing::Test
{
23 AnimationEffectInputTest()
24 : pageHolder(DummyPageHolder::create())
25 , element(pageHolder
->document().createElement("foo", ASSERT_NO_EXCEPTION
))
26 , m_isolate(v8::Isolate::GetCurrent())
29 pageHolder
->document().documentElement()->appendChild(element
.get());
32 OwnPtr
<DummyPageHolder
> pageHolder
;
33 RefPtrWillBePersistent
<Element
> element
;
34 TrackExceptionState exceptionState
;
35 v8::Isolate
* m_isolate
;
38 V8TestingScope m_scope
;
41 TEST_F(AnimationEffectInputTest
, SortedOffsets
)
43 Vector
<Dictionary
> jsKeyframes
;
44 v8::Local
<v8::Object
> keyframe1
= v8::Object::New(m_isolate
);
45 v8::Local
<v8::Object
> keyframe2
= v8::Object::New(m_isolate
);
47 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "width", "100px");
48 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "offset", "0");
49 setV8ObjectPropertyAsString(m_isolate
, keyframe2
, "width", "0px");
50 setV8ObjectPropertyAsString(m_isolate
, keyframe2
, "offset", "1");
52 jsKeyframes
.append(Dictionary(keyframe1
, m_isolate
, exceptionState
));
53 jsKeyframes
.append(Dictionary(keyframe2
, m_isolate
, exceptionState
));
55 EffectModel
* animationEffect
= EffectInput::convert(element
.get(), jsKeyframes
, exceptionState
);
56 EXPECT_FALSE(exceptionState
.hadException());
57 const KeyframeEffectModelBase
& keyframeEffect
= *toKeyframeEffectModelBase(animationEffect
);
58 EXPECT_EQ(1.0, keyframeEffect
.getFrames()[1]->offset());
61 TEST_F(AnimationEffectInputTest
, UnsortedOffsets
)
63 Vector
<Dictionary
> jsKeyframes
;
64 v8::Local
<v8::Object
> keyframe1
= v8::Object::New(m_isolate
);
65 v8::Local
<v8::Object
> keyframe2
= v8::Object::New(m_isolate
);
67 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "width", "0px");
68 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "offset", "1");
69 setV8ObjectPropertyAsString(m_isolate
, keyframe2
, "width", "100px");
70 setV8ObjectPropertyAsString(m_isolate
, keyframe2
, "offset", "0");
72 jsKeyframes
.append(Dictionary(keyframe1
, m_isolate
, exceptionState
));
73 jsKeyframes
.append(Dictionary(keyframe2
, m_isolate
, exceptionState
));
75 EffectInput::convert(element
.get(), jsKeyframes
, exceptionState
);
76 EXPECT_TRUE(exceptionState
.hadException());
77 EXPECT_EQ(InvalidModificationError
, exceptionState
.code());
80 TEST_F(AnimationEffectInputTest
, LooslySorted
)
82 Vector
<Dictionary
> jsKeyframes
;
83 v8::Local
<v8::Object
> keyframe1
= v8::Object::New(m_isolate
);
84 v8::Local
<v8::Object
> keyframe2
= v8::Object::New(m_isolate
);
85 v8::Local
<v8::Object
> keyframe3
= v8::Object::New(m_isolate
);
87 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "width", "100px");
88 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "offset", "0");
89 setV8ObjectPropertyAsString(m_isolate
, keyframe2
, "width", "200px");
90 setV8ObjectPropertyAsString(m_isolate
, keyframe3
, "width", "0px");
91 setV8ObjectPropertyAsString(m_isolate
, keyframe3
, "offset", "1");
93 jsKeyframes
.append(Dictionary(keyframe1
, m_isolate
, exceptionState
));
94 jsKeyframes
.append(Dictionary(keyframe2
, m_isolate
, exceptionState
));
95 jsKeyframes
.append(Dictionary(keyframe3
, m_isolate
, exceptionState
));
97 EffectModel
* animationEffect
= EffectInput::convert(element
.get(), jsKeyframes
, exceptionState
);
98 EXPECT_FALSE(exceptionState
.hadException());
99 const KeyframeEffectModelBase
& keyframeEffect
= *toKeyframeEffectModelBase(animationEffect
);
100 EXPECT_EQ(1, keyframeEffect
.getFrames()[2]->offset());
103 TEST_F(AnimationEffectInputTest
, OutOfOrderWithNullOffsets
)
105 Vector
<Dictionary
> jsKeyframes
;
106 v8::Local
<v8::Object
> keyframe1
= v8::Object::New(m_isolate
);
107 v8::Local
<v8::Object
> keyframe2
= v8::Object::New(m_isolate
);
108 v8::Local
<v8::Object
> keyframe3
= v8::Object::New(m_isolate
);
109 v8::Local
<v8::Object
> keyframe4
= v8::Object::New(m_isolate
);
111 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "height", "100px");
112 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "offset", "0.5");
113 setV8ObjectPropertyAsString(m_isolate
, keyframe2
, "height", "150px");
114 setV8ObjectPropertyAsString(m_isolate
, keyframe3
, "height", "200px");
115 setV8ObjectPropertyAsString(m_isolate
, keyframe3
, "offset", "0");
116 setV8ObjectPropertyAsString(m_isolate
, keyframe4
, "height", "300px");
117 setV8ObjectPropertyAsString(m_isolate
, keyframe4
, "offset", "1");
119 jsKeyframes
.append(Dictionary(keyframe1
, m_isolate
, exceptionState
));
120 jsKeyframes
.append(Dictionary(keyframe2
, m_isolate
, exceptionState
));
121 jsKeyframes
.append(Dictionary(keyframe3
, m_isolate
, exceptionState
));
122 jsKeyframes
.append(Dictionary(keyframe4
, m_isolate
, exceptionState
));
124 EffectInput::convert(element
.get(), jsKeyframes
, exceptionState
);
125 EXPECT_TRUE(exceptionState
.hadException());
128 TEST_F(AnimationEffectInputTest
, Invalid
)
130 // Not loosely sorted by offset, and there exists a keyframe with null offset.
131 Vector
<Dictionary
> jsKeyframes
;
132 v8::Local
<v8::Object
> keyframe1
= v8::Object::New(m_isolate
);
133 v8::Local
<v8::Object
> keyframe2
= v8::Object::New(m_isolate
);
134 v8::Local
<v8::Object
> keyframe3
= v8::Object::New(m_isolate
);
136 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "width", "0px");
137 setV8ObjectPropertyAsString(m_isolate
, keyframe1
, "offset", "1");
138 setV8ObjectPropertyAsString(m_isolate
, keyframe2
, "width", "200px");
139 setV8ObjectPropertyAsString(m_isolate
, keyframe3
, "width", "100px");
140 setV8ObjectPropertyAsString(m_isolate
, keyframe3
, "offset", "0");
142 jsKeyframes
.append(Dictionary(keyframe1
, m_isolate
, exceptionState
));
143 jsKeyframes
.append(Dictionary(keyframe2
, m_isolate
, exceptionState
));
144 jsKeyframes
.append(Dictionary(keyframe3
, m_isolate
, exceptionState
));
146 EffectInput::convert(element
.get(), jsKeyframes
, exceptionState
);
147 EXPECT_TRUE(exceptionState
.hadException());
148 EXPECT_EQ(InvalidModificationError
, exceptionState
.code());