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
|
// 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 <memory>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/animation/animation_test_helper.h"
#include "third_party/blink/renderer/core/animation/css_length_interpolation_type.h"
#include "third_party/blink/renderer/core/animation/css_number_interpolation_type.h"
#include "third_party/blink/renderer/core/animation/interpolable_value.h"
#include "third_party/blink/renderer/core/animation/interpolation_value.h"
#include "third_party/blink/renderer/core/animation/string_keyframe.h"
#include "third_party/blink/renderer/core/animation/transition_interpolation.h"
namespace blink {
class AnimationInterpolableValueTest : public testing::Test {
protected:
double InterpolateNumbers(int a, int b, double progress) {
// We require a property that maps to CSSNumberInterpolationType. 'z-index'
// suffices for this, and also means we can ignore the AnimatableValues for
// the compositor (as z-index isn't compositor-compatible).
PropertyHandle property_handle(GetCSSPropertyZIndex());
CSSNumberInterpolationType interpolation_type(property_handle);
InterpolationValue start(std::make_unique<InterpolableNumber>(a));
InterpolationValue end(std::make_unique<InterpolableNumber>(b));
TransitionInterpolation* i = TransitionInterpolation::Create(
property_handle, interpolation_type, std::move(start), std::move(end),
nullptr, nullptr);
i->Interpolate(0, progress);
std::unique_ptr<TypedInterpolationValue> interpolated_value =
i->GetInterpolatedValue();
EXPECT_TRUE(interpolated_value);
return ToInterpolableNumber(interpolated_value->GetInterpolableValue())
.Value();
}
void ScaleAndAdd(InterpolableValue& base,
double scale,
const InterpolableValue& add) {
base.ScaleAndAdd(scale, add);
}
std::unique_ptr<TypedInterpolationValue> InterpolateLists(
std::unique_ptr<InterpolableList> list_a,
std::unique_ptr<InterpolableList> list_b,
double progress) {
// We require a property that maps to CSSLengthInterpolationType. 'left'
// suffices for this, and also means we can ignore the AnimatableValues for
// the compositor (as left isn't compositor-compatible).
PropertyHandle property_handle(GetCSSPropertyLeft());
CSSLengthInterpolationType interpolation_type(property_handle);
InterpolationValue start(std::move(list_a));
InterpolationValue end(std::move(list_b));
TransitionInterpolation* i = TransitionInterpolation::Create(
property_handle, interpolation_type, std::move(start), std::move(end),
nullptr, nullptr);
i->Interpolate(0, progress);
return i->GetInterpolatedValue();
}
};
TEST_F(AnimationInterpolableValueTest, InterpolateNumbers) {
EXPECT_FLOAT_EQ(126, InterpolateNumbers(42, 0, -2));
EXPECT_FLOAT_EQ(42, InterpolateNumbers(42, 0, 0));
EXPECT_FLOAT_EQ(29.4f, InterpolateNumbers(42, 0, 0.3));
EXPECT_FLOAT_EQ(21, InterpolateNumbers(42, 0, 0.5));
EXPECT_FLOAT_EQ(0, InterpolateNumbers(42, 0, 1));
EXPECT_FLOAT_EQ(-21, InterpolateNumbers(42, 0, 1.5));
}
TEST_F(AnimationInterpolableValueTest, SimpleList) {
auto list_a = std::make_unique<InterpolableList>(3);
list_a->Set(0, std::make_unique<InterpolableNumber>(0));
list_a->Set(1, std::make_unique<InterpolableNumber>(42));
list_a->Set(2, std::make_unique<InterpolableNumber>(20.5));
auto list_b = std::make_unique<InterpolableList>(3);
list_b->Set(0, std::make_unique<InterpolableNumber>(100));
list_b->Set(1, std::make_unique<InterpolableNumber>(-200));
list_b->Set(2, std::make_unique<InterpolableNumber>(300));
std::unique_ptr<TypedInterpolationValue> interpolated_value =
InterpolateLists(std::move(list_a), std::move(list_b), 0.3);
const InterpolableList& out_list =
ToInterpolableList(interpolated_value->GetInterpolableValue());
EXPECT_FLOAT_EQ(30, ToInterpolableNumber(out_list.Get(0))->Value());
EXPECT_FLOAT_EQ(-30.6f, ToInterpolableNumber(out_list.Get(1))->Value());
EXPECT_FLOAT_EQ(104.35f, ToInterpolableNumber(out_list.Get(2))->Value());
}
TEST_F(AnimationInterpolableValueTest, NestedList) {
auto list_a = std::make_unique<InterpolableList>(3);
list_a->Set(0, std::make_unique<InterpolableNumber>(0));
auto sub_list_a = std::make_unique<InterpolableList>(1);
sub_list_a->Set(0, std::make_unique<InterpolableNumber>(100));
list_a->Set(1, std::move(sub_list_a));
list_a->Set(2, std::make_unique<InterpolableNumber>(0));
auto list_b = std::make_unique<InterpolableList>(3);
list_b->Set(0, std::make_unique<InterpolableNumber>(100));
auto sub_list_b = std::make_unique<InterpolableList>(1);
sub_list_b->Set(0, std::make_unique<InterpolableNumber>(50));
list_b->Set(1, std::move(sub_list_b));
list_b->Set(2, std::make_unique<InterpolableNumber>(1));
std::unique_ptr<TypedInterpolationValue> interpolated_value =
InterpolateLists(std::move(list_a), std::move(list_b), 0.5);
const InterpolableList& out_list =
ToInterpolableList(interpolated_value->GetInterpolableValue());
EXPECT_FLOAT_EQ(50, ToInterpolableNumber(out_list.Get(0))->Value());
EXPECT_FLOAT_EQ(
75, ToInterpolableNumber(ToInterpolableList(out_list.Get(1))->Get(0))
->Value());
EXPECT_FLOAT_EQ(0.5, ToInterpolableNumber(out_list.Get(2))->Value());
}
TEST_F(AnimationInterpolableValueTest, ScaleAndAddNumbers) {
std::unique_ptr<InterpolableNumber> base =
std::make_unique<InterpolableNumber>(10);
ScaleAndAdd(*base, 2, *std::make_unique<InterpolableNumber>(1));
EXPECT_FLOAT_EQ(21, base->Value());
base = std::make_unique<InterpolableNumber>(10);
ScaleAndAdd(*base, 0, *std::make_unique<InterpolableNumber>(5));
EXPECT_FLOAT_EQ(5, base->Value());
base = std::make_unique<InterpolableNumber>(10);
ScaleAndAdd(*base, -1, *std::make_unique<InterpolableNumber>(8));
EXPECT_FLOAT_EQ(-2, base->Value());
}
TEST_F(AnimationInterpolableValueTest, ScaleAndAddLists) {
auto base_list = std::make_unique<InterpolableList>(3);
base_list->Set(0, std::make_unique<InterpolableNumber>(5));
base_list->Set(1, std::make_unique<InterpolableNumber>(10));
base_list->Set(2, std::make_unique<InterpolableNumber>(15));
auto add_list = std::make_unique<InterpolableList>(3);
add_list->Set(0, std::make_unique<InterpolableNumber>(1));
add_list->Set(1, std::make_unique<InterpolableNumber>(2));
add_list->Set(2, std::make_unique<InterpolableNumber>(3));
ScaleAndAdd(*base_list, 2, *add_list);
EXPECT_FLOAT_EQ(11, ToInterpolableNumber(base_list->Get(0))->Value());
EXPECT_FLOAT_EQ(22, ToInterpolableNumber(base_list->Get(1))->Value());
EXPECT_FLOAT_EQ(33, ToInterpolableNumber(base_list->Get(2))->Value());
}
} // namespace blink
|