1f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu// Copyright 2014 The Chromium Authors. All rights reserved.
2f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu// Use of this source code is governed by a BSD-style license that can be
3f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu// found in the LICENSE file.
4f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
5f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "config.h"
6f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "core/animation/EffectInput.h"
7f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
8f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "bindings/v8/Dictionary.h"
9f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "core/animation/AnimationTestHelper.h"
10f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "core/animation/KeyframeEffectModel.h"
11f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "core/dom/Document.h"
12f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "core/dom/Element.h"
13f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include <gtest/gtest.h>
14f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <v8.h>
15f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
16f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuusing namespace WebCore;
17f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
18f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liunamespace {
19f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
20f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuclass AnimationEffectInputTest : public ::testing::Test {
21f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuprotected:
22f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    AnimationEffectInputTest()
23f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        : document(Document::create())
24f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        , element(document->createElement("foo", ASSERT_NO_EXCEPTION))
25f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        , m_isolate(v8::Isolate::GetCurrent())
26f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        , m_scope(m_isolate)
27f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    {
28f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    }
29f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
30f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    RefPtrWillBePersistent<Document> document;
31d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    RefPtrWillBePersistent<Element> element;
32f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    TrackExceptionState exceptionState;
33f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Isolate* m_isolate;
34f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
35f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuprivate:
36f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    V8TestingScope m_scope;
37f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu};
38f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
39f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuTEST_F(AnimationEffectInputTest, SortedOffsets)
40f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu{
41f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    Vector<Dictionary> jsKeyframes;
42f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
43f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);
44f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
45f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "width", "100px");
46f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "offset", "0");
47f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe2, "width", "0px");
48f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe2, "offset", "1");
49f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
50f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
51f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe2, m_isolate));
52f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
5376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    RefPtrWillBeRawPtr<AnimationEffect> animationEffect = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
54f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_FALSE(exceptionState.hadException());
55f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    const KeyframeEffectModelBase& keyframeEffect = *toKeyframeEffectModelBase(animationEffect.get());
56f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_EQ(1.0, keyframeEffect.getFrames()[1]->offset());
57f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
58f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
59f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuTEST_F(AnimationEffectInputTest, UnsortedOffsets)
60f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu{
61f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    Vector<Dictionary> jsKeyframes;
62f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
63f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);
64f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
65f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "width", "0px");
66f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "offset", "1");
67f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe2, "width", "100px");
68f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe2, "offset", "0");
69f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
70f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
71f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe2, m_isolate));
72f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
7376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    RefPtrWillBeRawPtr<AnimationEffect> animationEffect = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
74f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_FALSE(exceptionState.hadException());
75f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    const KeyframeEffectModelBase& keyframeEffect = *toKeyframeEffectModelBase(animationEffect.get());
76f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_EQ(1.0, keyframeEffect.getFrames()[1]->offset());
77f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
78f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
79f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuTEST_F(AnimationEffectInputTest, LooslySorted)
80f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu{
81f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    Vector<Dictionary> jsKeyframes;
82f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
83f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);
84f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe3 = v8::Object::New(m_isolate);
85f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
86f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "width", "100px");
87f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "offset", "0");
88f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe2, "width", "200px");
89f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe3, "width", "0px");
90f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe3, "offset", "1");
91f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
92f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
93f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe2, m_isolate));
94f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe3, m_isolate));
95f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
9676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    RefPtrWillBeRawPtr<AnimationEffect> animationEffect = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
97f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_FALSE(exceptionState.hadException());
98f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    const KeyframeEffectModelBase& keyframeEffect = *toKeyframeEffectModelBase(animationEffect.get());
99f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_EQ(1, keyframeEffect.getFrames()[2]->offset());
100f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
101f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
102f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuTEST_F(AnimationEffectInputTest, Invalid)
103f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu{
104f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    // Not loosely sorted by offset, and there exists a keyframe with null offset.
105f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    Vector<Dictionary> jsKeyframes;
106f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe1 = v8::Object::New(m_isolate);
107f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe2 = v8::Object::New(m_isolate);
108f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    v8::Handle<v8::Object> keyframe3 = v8::Object::New(m_isolate);
109f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
110f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "width", "0px");
111f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe1, "offset", "1");
112f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe2, "width", "200px");
113f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe3, "width", "100px");
114f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    setV8ObjectPropertyAsString(keyframe3, "offset", "0");
115f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
116f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe1, m_isolate));
117f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe2, m_isolate));
118f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    jsKeyframes.append(Dictionary(keyframe3, m_isolate));
119f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
12076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    RefPtrWillBeRawPtr<AnimationEffect> animationEffect ALLOW_UNUSED = EffectInput::convert(element.get(), jsKeyframes, exceptionState);
121f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_TRUE(exceptionState.hadException());
122f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    EXPECT_EQ(InvalidModificationError, exceptionState.code());
123f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
124f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
125f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
126