blob: 257dd2806d38e9ff4158d5e6d48a9cc60c40f011 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef TEST_UTILS_H
17#define TEST_UTILS_H
18
19#include <Matrix.h>
20#include <Snapshot.h>
21#include <RenderNode.h>
22
23#include <memory>
24
25namespace android {
26namespace uirenderer {
27
28#define EXPECT_MATRIX_APPROX_EQ(a, b) \
29 EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b))
30
31class TestUtils {
32public:
33 static bool matricesAreApproxEqual(const Matrix4& a, const Matrix4& b) {
34 for (int i = 0; i < 16; i++) {
35 if (!MathUtils::areEqual(a[i], b[i])) {
36 return false;
37 }
38 }
39 return true;
40 }
41
42 static std::unique_ptr<Snapshot> makeSnapshot(const Matrix4& transform, const Rect& clip) {
43 std::unique_ptr<Snapshot> snapshot(new Snapshot());
44 snapshot->clip(clip.left, clip.top, clip.right, clip.bottom, SkRegion::kReplace_Op);
45 *(snapshot->transform) = transform;
46 return snapshot;
47 }
48
49 template<class CanvasType>
50 static std::unique_ptr<DisplayListData> createDLD(int width, int height,
51 std::function<void(CanvasType& canvas)> canvasCallback) {
52 CanvasType canvas(width, height);
53 canvasCallback(canvas);
54 return std::unique_ptr<DisplayListData>(canvas.finishRecording());
55 }
56
57 template<class CanvasType>
58 static sp<RenderNode> createNode(int left, int top, int right, int bottom,
59 std::function<void(CanvasType& canvas)> canvasCallback) {
60 sp<RenderNode> node = new RenderNode();
61 node->mutateStagingProperties().setLeftTopRightBottom(left, top, right, bottom);
62 node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
63
64 CanvasType canvas(
65 node->stagingProperties().getWidth(), node->stagingProperties().getHeight());
66 canvasCallback(canvas);
67 node->setStagingDisplayList(canvas.finishRecording());
68 return node;
69 }
70
71 static void syncNodePropertiesAndDisplayList(sp<RenderNode>& node) {
72 node->syncProperties();
73 node->syncDisplayList();
74 }
75}; // class TestUtils
76
77} /* namespace uirenderer */
78} /* namespace android */
79
80#endif /* TEST_UTILS_H */