summaryrefslogtreecommitdiff
path: root/Source/WebKit/chromium/tests/WebTransformOperationsTest.cpp
blob: deaf7ad74f53291ef5b41aae109ed27180ef0d9e (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include <public/WebTransformOperations.h>

#include <public/WebTransformationMatrix.h>

#include "CCLayerTreeTestCommon.h"

#include <gtest/gtest.h>
#include <wtf/OwnPtr.h>
#include <wtf/PassOwnPtr.h>
#include <wtf/Vector.h>

using namespace WebKit;

TEST(WebTransformOperationTest, transformTypesAreUnique)
{
    Vector<OwnPtr<WebTransformOperations> > transforms;

    WebTransformOperations* toAdd = new WebTransformOperations();
    toAdd->appendTranslate(0, 0, 0);
    transforms.append(adoptPtr(toAdd));

    toAdd = new WebTransformOperations();
    toAdd->appendRotate(0, 0, 1, 0);
    transforms.append(adoptPtr(toAdd));

    toAdd = new WebTransformOperations();
    toAdd->appendScale(1, 1, 1);
    transforms.append(adoptPtr(toAdd));

    toAdd = new WebTransformOperations();
    toAdd->appendSkew(0, 0);
    transforms.append(adoptPtr(toAdd));

    toAdd = new WebTransformOperations();
    toAdd->appendPerspective(800);
    transforms.append(adoptPtr(toAdd));

    for (size_t i = 0; i < transforms.size(); ++i) {
        for (size_t j = 0; j < transforms.size(); ++j) {
            bool matchesType = transforms[i]->matchesTypes(*transforms[j]);
            EXPECT_TRUE((i == j && matchesType) || !matchesType);
        }
    }
}

TEST(WebTransformOperationTest, matchTypesSameLength)
{
    WebTransformOperations translates;
    translates.appendTranslate(0, 0, 0);
    translates.appendTranslate(0, 0, 0);
    translates.appendTranslate(0, 0, 0);

    WebTransformOperations skews;
    skews.appendSkew(0, 0);
    skews.appendSkew(0, 0);
    skews.appendSkew(0, 0);

    WebTransformOperations translates2;
    translates2.appendTranslate(0, 0, 0);
    translates2.appendTranslate(0, 0, 0);
    translates2.appendTranslate(0, 0, 0);

    WebTransformOperations translates3 = translates2;

    EXPECT_FALSE(translates.matchesTypes(skews));
    EXPECT_TRUE(translates.matchesTypes(translates2));
    EXPECT_TRUE(translates.matchesTypes(translates3));
}

TEST(WebTransformOperationTest, matchTypesDifferentLength)
{
    WebTransformOperations translates;
    translates.appendTranslate(0, 0, 0);
    translates.appendTranslate(0, 0, 0);
    translates.appendTranslate(0, 0, 0);

    WebTransformOperations skews;
    skews.appendSkew(0, 0);
    skews.appendSkew(0, 0);

    WebTransformOperations translates2;
    translates2.appendTranslate(0, 0, 0);
    translates2.appendTranslate(0, 0, 0);

    EXPECT_FALSE(translates.matchesTypes(skews));
    EXPECT_FALSE(translates.matchesTypes(translates2));
}

TEST(WebTransformOperationTest, applyTranslate)
{
    double x = 1;
    double y = 2;
    double z = 3;
    WebTransformOperations operations;
    operations.appendTranslate(x, y, z);
    WebTransformationMatrix expected;
    expected.translate3d(x, y, z);
    EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.apply());
}

TEST(WebTransformOperationTest, applyRotate)
{
    double x = 1;
    double y = 2;
    double z = 3;
    double degrees = 80;
    WebTransformOperations operations;
    operations.appendRotate(x, y, z, degrees);
    WebTransformationMatrix expected;
    expected.rotate3d(x, y, z, degrees);
    EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.apply());
}

TEST(WebTransformOperationTest, applyScale)
{
    double x = 1;
    double y = 2;
    double z = 3;
    WebTransformOperations operations;
    operations.appendScale(x, y, z);
    WebTransformationMatrix expected;
    expected.scale3d(x, y, z);
    EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.apply());
}

TEST(WebTransformOperationTest, applySkew)
{
    double x = 1;
    double y = 2;
    WebTransformOperations operations;
    operations.appendSkew(x, y);
    WebTransformationMatrix expected;
    expected.skewX(x);
    expected.skewY(y);
    EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.apply());
}

TEST(WebTransformOperationTest, applyPerspective)
{
    double depth = 800;
    WebTransformOperations operations;
    operations.appendPerspective(depth);
    WebTransformationMatrix expected;
    expected.applyPerspective(depth);
    EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operations.apply());
}

TEST(WebTransformOperationTest, applyMatrix)
{
    double dx = 1;
    double dy = 2;
    double dz = 3;
    WebTransformationMatrix expectedMatrix;
    expectedMatrix.translate3d(dx, dy, dz);
    WebTransformOperations matrixTransform;
    matrixTransform.appendMatrix(expectedMatrix);
    EXPECT_TRANSFORMATION_MATRIX_EQ(expectedMatrix, matrixTransform.apply());
}

TEST(WebTransformOperationTest, applyOrder)
{
    double sx = 2;
    double sy = 4;
    double sz = 8;

    double dx = 1;
    double dy = 2;
    double dz = 3;

    WebTransformOperations operations;
    operations.appendScale(sx, sy, sz);
    operations.appendTranslate(dx, dy, dz);

    WebTransformationMatrix expectedScaleMatrix;
    expectedScaleMatrix.scale3d(sx, sy, sz);

    WebTransformationMatrix expectedTranslateMatrix;
    expectedTranslateMatrix.translate3d(dx, dy, dz);

    WebTransformationMatrix expectedCombinedMatrix = expectedScaleMatrix;
    expectedCombinedMatrix.multiply(expectedTranslateMatrix);

    EXPECT_TRANSFORMATION_MATRIX_EQ(expectedCombinedMatrix, operations.apply());
}

TEST(WebTransformOperationTest, blendOrder)
{
    double sx1 = 2;
    double sy1 = 4;
    double sz1 = 8;

    double dx1 = 1;
    double dy1 = 2;
    double dz1 = 3;

    double sx2 = 4;
    double sy2 = 8;
    double sz2 = 16;

    double dx2 = 10;
    double dy2 = 20;
    double dz2 = 30;

    WebTransformOperations operationsFrom;
    operationsFrom.appendScale(sx1, sy1, sz1);
    operationsFrom.appendTranslate(dx1, dy1, dz1);

    WebTransformOperations operationsTo;
    operationsTo.appendScale(sx2, sy2, sz2);
    operationsTo.appendTranslate(dx2, dy2, dz2);

    WebTransformationMatrix scaleFrom;
    scaleFrom.scale3d(sx1, sy1, sz1);
    WebTransformationMatrix translateFrom;
    translateFrom.translate3d(dx1, dy1, dz1);

    WebTransformationMatrix scaleTo;
    scaleTo.scale3d(sx2, sy2, sz2);
    WebTransformationMatrix translateTo;
    translateTo.translate3d(dx2, dy2, dz2);

    double progress = 0.25;

    WebTransformationMatrix blendedScale = scaleTo;
    blendedScale.blend(scaleFrom, progress);

    WebTransformationMatrix blendedTranslate = translateTo;
    blendedTranslate.blend(translateFrom, progress);

    WebTransformationMatrix expected = blendedScale;
    expected.multiply(blendedTranslate);

    EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operationsTo.blend(operationsFrom, progress));
}

static void checkProgress(double progress,
                          const WebTransformationMatrix& fromMatrix,
                          const WebTransformationMatrix& toMatrix,
                          const WebTransformOperations& fromTransform,
                          const WebTransformOperations& toTransform)
{
    WebTransformationMatrix expectedMatrix = toMatrix;
    expectedMatrix.blend(fromMatrix, progress);
    EXPECT_TRANSFORMATION_MATRIX_EQ(expectedMatrix, toTransform.blend(fromTransform, progress));
}

TEST(WebTransformOperationTest, blendProgress)
{
    double sx = 2;
    double sy = 4;
    double sz = 8;
    WebTransformOperations operationsFrom;
    operationsFrom.appendScale(sx, sy, sz);

    WebTransformationMatrix matrixFrom;
    matrixFrom.scale3d(sx, sy, sz);

    sx = 4;
    sy = 8;
    sz = 16;
    WebTransformOperations operationsTo;
    operationsTo.appendScale(sx, sy, sz);

    WebTransformationMatrix matrixTo;
    matrixTo.scale3d(sx, sy, sz);

    checkProgress(-1, matrixFrom, matrixTo, operationsFrom, operationsTo);
    checkProgress(0, matrixFrom, matrixTo, operationsFrom, operationsTo);
    checkProgress(0.25, matrixFrom, matrixTo, operationsFrom, operationsTo);
    checkProgress(0.5, matrixFrom, matrixTo, operationsFrom, operationsTo);
    checkProgress(1, matrixFrom, matrixTo, operationsFrom, operationsTo);
    checkProgress(2, matrixFrom, matrixTo, operationsFrom, operationsTo);
}

TEST(WebTransformOperationTest, blendWhenTypesDoNotMatch)
{
    double sx1 = 2;
    double sy1 = 4;
    double sz1 = 8;

    double dx1 = 1;
    double dy1 = 2;
    double dz1 = 3;

    double sx2 = 4;
    double sy2 = 8;
    double sz2 = 16;

    double dx2 = 10;
    double dy2 = 20;
    double dz2 = 30;

    WebTransformOperations operationsFrom;
    operationsFrom.appendScale(sx1, sy1, sz1);
    operationsFrom.appendTranslate(dx1, dy1, dz1);

    WebTransformOperations operationsTo;
    operationsTo.appendTranslate(dx2, dy2, dz2);
    operationsTo.appendScale(sx2, sy2, sz2);

    WebTransformationMatrix from;
    from.scale3d(sx1, sy1, sz1);
    from.translate3d(dx1, dy1, dz1);

    WebTransformationMatrix to;
    to.translate3d(dx2, dy2, dz2);
    to.scale3d(sx2, sy2, sz2);

    double progress = 0.25;

    WebTransformationMatrix expected = to;
    expected.blend(from, progress);

    EXPECT_TRANSFORMATION_MATRIX_EQ(expected, operationsTo.blend(operationsFrom, progress));
}