summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/core/animation/effect_input_test.cc
blob: 942371e5ee3ca6e31f0bcc579c7bb3a1e3e75cfd (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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 "third_party/blink/renderer/core/animation/effect_input.h"

#include <memory>

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_object_builder.h"
#include "third_party/blink/renderer/core/animation/animation_test_helpers.h"
#include "third_party/blink/renderer/core/animation/keyframe_effect_model.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/testing/dummy_page_holder.h"
#include "v8/include/v8.h"

namespace blink {

Element* AppendElement(Document& document) {
  Element* element = document.CreateElementForBinding("foo");
  document.documentElement()->AppendChild(element);
  return element;
}

TEST(AnimationEffectInputTest, SortedOffsets) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();

  HeapVector<ScriptValue> blink_keyframes = {V8ObjectBuilder(script_state)
                                                 .AddString("width", "100px")
                                                 .AddString("offset", "0")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("width", "0px")
                                                 .AddString("offset", "1")
                                                 .GetScriptValue()};

  ScriptValue js_keyframes(
      scope.GetIsolate(),
      ToV8(blink_keyframes, scope.GetContext()->Global(), scope.GetIsolate()));

  Element* element = AppendElement(scope.GetDocument());
  KeyframeEffectModelBase* effect = EffectInput::Convert(
      element, js_keyframes, EffectModel::kCompositeReplace,
      scope.GetScriptState(), scope.GetExceptionState());
  EXPECT_FALSE(scope.GetExceptionState().HadException());
  EXPECT_EQ(1.0, effect->GetFrames()[1]->CheckedOffset());
}

TEST(AnimationEffectInputTest, UnsortedOffsets) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();

  HeapVector<ScriptValue> blink_keyframes = {V8ObjectBuilder(script_state)
                                                 .AddString("width", "0px")
                                                 .AddString("offset", "1")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("width", "100px")
                                                 .AddString("offset", "0")
                                                 .GetScriptValue()};

  ScriptValue js_keyframes(
      scope.GetIsolate(),
      ToV8(blink_keyframes, scope.GetContext()->Global(), scope.GetIsolate()));

  Element* element = AppendElement(scope.GetDocument());
  EffectInput::Convert(element, js_keyframes, EffectModel::kCompositeReplace,
                       scope.GetScriptState(), scope.GetExceptionState());
  EXPECT_TRUE(scope.GetExceptionState().HadException());
  EXPECT_EQ(ESErrorType::kTypeError,
            scope.GetExceptionState().CodeAs<ESErrorType>());
}

TEST(AnimationEffectInputTest, LooslySorted) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();

  HeapVector<ScriptValue> blink_keyframes = {V8ObjectBuilder(script_state)
                                                 .AddString("width", "100px")
                                                 .AddString("offset", "0")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("width", "200px")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("width", "0px")
                                                 .AddString("offset", "1")
                                                 .GetScriptValue()};

  ScriptValue js_keyframes(
      scope.GetIsolate(),
      ToV8(blink_keyframes, scope.GetContext()->Global(), scope.GetIsolate()));

  Element* element = AppendElement(scope.GetDocument());
  KeyframeEffectModelBase* effect = EffectInput::Convert(
      element, js_keyframes, EffectModel::kCompositeReplace,
      scope.GetScriptState(), scope.GetExceptionState());
  EXPECT_FALSE(scope.GetExceptionState().HadException());
  EXPECT_EQ(1, effect->GetFrames()[2]->CheckedOffset());
}

TEST(AnimationEffectInputTest, OutOfOrderWithNullOffsets) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();

  HeapVector<ScriptValue> blink_keyframes = {V8ObjectBuilder(script_state)
                                                 .AddString("height", "100px")
                                                 .AddString("offset", "0.5")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("height", "150px")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("height", "200px")
                                                 .AddString("offset", "0")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("height", "300px")
                                                 .AddString("offset", "1")
                                                 .GetScriptValue()};

  ScriptValue js_keyframes(
      scope.GetIsolate(),
      ToV8(blink_keyframes, scope.GetContext()->Global(), scope.GetIsolate()));

  Element* element = AppendElement(scope.GetDocument());
  EffectInput::Convert(element, js_keyframes, EffectModel::kCompositeReplace,
                       scope.GetScriptState(), scope.GetExceptionState());
  EXPECT_TRUE(scope.GetExceptionState().HadException());
}

TEST(AnimationEffectInputTest, Invalid) {
  V8TestingScope scope;
  ScriptState* script_state = scope.GetScriptState();

  // Not loosely sorted by offset, and there exists a keyframe with null offset.
  HeapVector<ScriptValue> blink_keyframes = {V8ObjectBuilder(script_state)
                                                 .AddString("width", "0px")
                                                 .AddString("offset", "1")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("width", "200px")
                                                 .GetScriptValue(),
                                             V8ObjectBuilder(script_state)
                                                 .AddString("width", "200px")
                                                 .AddString("offset", "0")
                                                 .GetScriptValue()};

  ScriptValue js_keyframes(
      scope.GetIsolate(),
      ToV8(blink_keyframes, scope.GetContext()->Global(), scope.GetIsolate()));

  Element* element = AppendElement(scope.GetDocument());
  EffectInput::Convert(element, js_keyframes, EffectModel::kCompositeReplace,
                       scope.GetScriptState(), scope.GetExceptionState());
  EXPECT_TRUE(scope.GetExceptionState().HadException());
  EXPECT_EQ(ESErrorType::kTypeError,
            scope.GetExceptionState().CodeAs<ESErrorType>());
}

}  // namespace blink