blob: 19b39284340eca776fb8c3917f3ac0310d2ea092 [file] [log] [blame]
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001/*
2 * Copyright 2020 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#include <compositionengine/ProjectionSpace.h>
17#include <gtest/gtest.h>
18
19namespace android::compositionengine {
20namespace {
21
22// Returns a rectangular strip along the side of the given rect pointed by
23// rotation. E.g. if rotation is ROTATION_0, the srip will be along the top
24// side, if it is ROTATION_90 the stip will be along the right wall.
25// One of the dimensions of the strip will be 0 and the other one will match
26// the length of the corresponding side.
27// The strip will be contained inside the given rect.
28Rect getSideStrip(const Rect& rect, ui::Rotation rotation) {
29 int width, height;
30 if (rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270) {
31 width = 0;
32 height = rect.height();
33 } else {
34 width = rect.width();
35 height = 0;
36 }
37
38 if (rotation == ui::ROTATION_0 || rotation == ui::ROTATION_270) {
39 return Rect(rect.left, rect.top, rect.left + width, rect.top + height);
40 }
41
42 if (rotation == ui::ROTATION_90) {
43 return Rect(rect.right, rect.top, rect.right + width, rect.top + height);
44 }
45
46 if (rotation == ui::ROTATION_180) {
47 return Rect(rect.left, rect.bottom, rect.left + width, rect.bottom + height);
48 }
49
50 return Rect::INVALID_RECT;
51}
52} // namespace
53
54TEST(ProjectionSpaceTest, getTransformToSelfIsIdentity) {
55 ProjectionSpace space;
Angel Aguayob084e0c2021-08-04 23:27:28 +000056 space.setContent(Rect(100, 200));
57 space.setBounds(ui::Size(100, 200));
Marin Shalamanov68933fb2020-09-10 17:58:12 +020058
59 const ui::Transform identity;
60 for (int rotation = 0; rotation <= 3; rotation++) {
Angel Aguayob084e0c2021-08-04 23:27:28 +000061 space.setOrientation(ui::Rotation(rotation));
Marin Shalamanov68933fb2020-09-10 17:58:12 +020062 EXPECT_EQ(space.getTransform(space), identity);
63 }
64}
65
66TEST(ProjectionSpaceTest, getTransformWhenTranslationIsNeeded) {
67 ProjectionSpace source;
Angel Aguayob084e0c2021-08-04 23:27:28 +000068 source.setContent(Rect(10, 10, 20, 20));
69 source.setBounds(ui::Size(100, 200));
Marin Shalamanov68933fb2020-09-10 17:58:12 +020070
71 ProjectionSpace dest;
Angel Aguayob084e0c2021-08-04 23:27:28 +000072 dest.setContent(Rect(10, 20, 30, 20));
73 dest.setBounds(source.getBounds());
Marin Shalamanov68933fb2020-09-10 17:58:12 +020074
75 const auto transform = source.getTransform(dest);
Angel Aguayob084e0c2021-08-04 23:27:28 +000076 EXPECT_EQ(transform.transform(source.getContent()), dest.getContent());
Marin Shalamanov68933fb2020-09-10 17:58:12 +020077}
78
79TEST(ProjectionSpaceTest, getTransformWhenScaleIsNeeded) {
80 ProjectionSpace source;
Angel Aguayob084e0c2021-08-04 23:27:28 +000081 source.setContent(Rect(0, 0, 20, 20));
82 source.setBounds(ui::Size(100, 200));
Marin Shalamanov68933fb2020-09-10 17:58:12 +020083
84 ProjectionSpace dest;
Angel Aguayob084e0c2021-08-04 23:27:28 +000085 dest.setContent(Rect(0, 0, 40, 30));
86 dest.setBounds(source.getBounds());
Marin Shalamanov68933fb2020-09-10 17:58:12 +020087
88 const auto transform = source.getTransform(dest);
Angel Aguayob084e0c2021-08-04 23:27:28 +000089 EXPECT_EQ(transform.transform(source.getContent()), dest.getContent());
Marin Shalamanov68933fb2020-09-10 17:58:12 +020090}
91
92TEST(ProjectionSpaceTest, getSideStripTest) {
93 const Rect rect(10, 20, 40, 100);
94 EXPECT_EQ(getSideStrip(rect, ui::ROTATION_0), Rect(10, 20, 40, 20));
95 EXPECT_EQ(getSideStrip(rect, ui::ROTATION_90), Rect(40, 20, 40, 100));
96 EXPECT_EQ(getSideStrip(rect, ui::ROTATION_180), Rect(10, 100, 40, 100));
97 EXPECT_EQ(getSideStrip(rect, ui::ROTATION_270), Rect(10, 20, 10, 100));
98}
99
100void testTransform(const ProjectionSpace& source, const ProjectionSpace& dest) {
101 const auto transform = source.getTransform(dest);
Angel Aguayob084e0c2021-08-04 23:27:28 +0000102 EXPECT_EQ(transform.transform(source.getContent()), dest.getContent())
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200103 << "Source content doesn't map to dest content when projecting " << to_string(source)
104 << " onto " << to_string(dest);
105
106 // We take a strip at the top (according to the orientation) of each
107 // content rect and verify that transform maps between them. This way we
108 // verify that the transform is rotating properly.
109 // In the following example the strip is marked with asterisks:
110 //
111 // ******* +-------*
112 // | | | *
113 // | | | *
114 // +-----+ +-------*
115 // source(ROTATION_0) dest (ROTATION_90)
Angel Aguayob084e0c2021-08-04 23:27:28 +0000116 const auto sourceStrip = getSideStrip(source.getContent(), source.getOrientation());
117 const auto destStrip = getSideStrip(dest.getContent(), dest.getOrientation());
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200118 ASSERT_NE(sourceStrip, Rect::INVALID_RECT);
119 ASSERT_NE(destStrip, Rect::INVALID_RECT);
120 const auto mappedStrip = transform.transform(sourceStrip);
121 EXPECT_EQ(mappedStrip, destStrip)
122 << to_string(sourceStrip) << " maps to " << to_string(mappedStrip) << " instead of "
123 << to_string(destStrip) << " when projecting " << to_string(source) << " onto "
124 << to_string(dest);
125}
126
127TEST(ProjectionSpaceTest, getTransformWithOrienations) {
128 ProjectionSpace source;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000129 source.setBounds(ui::Size(666, 776));
130 source.setContent(Rect(40, 50, 234, 343));
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200131 ProjectionSpace dest;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000132 dest.setBounds(ui::Size(862, 546));
133 dest.setContent(Rect(43, 52, 432, 213));
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200134
135 for (int sourceRot = 0; sourceRot <= 3; sourceRot++) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000136 source.setOrientation(ui::Rotation(sourceRot));
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200137 for (int destRot = 0; destRot <= 3; destRot++) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000138 dest.setOrientation(ui::Rotation(destRot));
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200139 testTransform(source, dest);
140 }
141 }
142}
143
144} // namespace android::compositionengine