File: linear_layout_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (210 lines) | stat: -rw-r--r-- 7,024 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/vr/elements/linear_layout.h"

#include <memory>

#include "chrome/browser/vr/test/animation_utils.h"
#include "chrome/browser/vr/test/constants.h"
#include "chrome/browser/vr/ui_scene.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/animation/keyframe/test/animation_utils.h"

namespace vr {

namespace {

// Helper class supplying convenient layout-related accessors.
class TestElement : public UiElement {
 public:
  float x() const { return world_space_transform().To2dTranslation().x(); }
  float y() const { return world_space_transform().To2dTranslation().y(); }
  float local_x() const { return LocalTransform().To2dTranslation().x(); }
  float local_y() const { return LocalTransform().To2dTranslation().y(); }
};

}  // namespace

TEST(LinearLayout, HorizontalVerticalLayout) {
  LinearLayout layout(LinearLayout::kRight);
  layout.set_margin(10);
  auto element = std::make_unique<UiElement>();
  UiElement* rect_a = element.get();
  rect_a->SetSize(10, 10);
  layout.AddChild(std::move(element));

  // One element should require no position adjustment at all.
  layout.SizeAndLayOut();
  EXPECT_TRUE(rect_a->LocalTransform().IsIdentity());

  // Two elements should be centered and separated by the margin.
  element = std::make_unique<UiElement>();
  UiElement* rect_b = element.get();
  rect_b->SetSize(10, 10);
  rect_b->SetScale(2.0f, 2.0f, 0.0f);
  layout.AddChild(std::move(element));
  layout.SizeAndLayOut();

  gfx::Point3F position_a;
  position_a = rect_a->LocalTransform().MapPoint(position_a);

  gfx::Point3F position_b;
  position_b = rect_b->LocalTransform().MapPoint(position_b);

  EXPECT_FLOAT_EQ(-15.0f, position_a.x());
  EXPECT_FLOAT_EQ(0.0f, position_a.y());
  EXPECT_FLOAT_EQ(0.0f, position_a.z());

  EXPECT_FLOAT_EQ(10.0f, position_b.x());
  EXPECT_FLOAT_EQ(0.0f, position_b.y());
  EXPECT_FLOAT_EQ(0.0f, position_b.z());

  rect_a->set_requires_layout(false);
  layout.SizeAndLayOut();

  EXPECT_FLOAT_EQ(20.0f, layout.size().width());
}

TEST(LinearLayout, Alignment) {
  LinearLayout layout(LinearLayout::kRight);
  layout.set_margin(10);
  auto element = std::make_unique<UiElement>();
  UiElement* rect_a = element.get();
  rect_a->SetSize(1, 1);
  layout.AddChild(std::move(element));
  element = std::make_unique<UiElement>();
  UiElement* rect_b = element.get();

  rect_b->SetSize(10, 10);
  rect_b->SetScale(2.0f, 2.0f, 0.0f);
  layout.AddChild(std::move(element));

  gfx::Point3F position_a;
  rect_a->set_y_anchoring(TOP);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(9.5f, position_a.y());
  position_a = gfx::Point3F();
  rect_a->set_y_anchoring(BOTTOM);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(-9.5f, position_a.y());

  layout.set_direction(LinearLayout::kLeft);
  position_a = gfx::Point3F();
  rect_a->set_y_anchoring(TOP);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(9.5f, position_a.y());
  position_a = gfx::Point3F();
  rect_a->set_y_anchoring(BOTTOM);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(-9.5f, position_a.y());

  layout.set_direction(LinearLayout::kDown);
  position_a = gfx::Point3F();
  rect_a->set_x_anchoring(LEFT);
  rect_a->set_y_anchoring(NONE);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(-9.5f, position_a.x());
  position_a = gfx::Point3F();
  rect_a->set_x_anchoring(RIGHT);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(9.5f, position_a.x());

  layout.set_direction(LinearLayout::kUp);
  position_a = gfx::Point3F();
  rect_a->set_x_anchoring(LEFT);
  rect_a->set_y_anchoring(NONE);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(-9.5f, position_a.x());
  position_a = gfx::Point3F();
  rect_a->set_x_anchoring(RIGHT);
  layout.SizeAndLayOut();
  position_a = rect_a->LocalTransform().MapPoint(position_a);
  EXPECT_FLOAT_EQ(9.5f, position_a.x());
}

TEST(LinearLayout, Orientations) {
  LinearLayout layout(LinearLayout::kUp);

  TestElement* rect;
  for (int i = 0; i < 2; i++) {
    auto element = std::make_unique<TestElement>();
    rect = element.get();
    element->SetSize(10, 10);
    layout.AddChild(std::move(element));
  }

  layout.set_direction(LinearLayout::kUp);
  layout.SizeAndLayOut();
  EXPECT_FLOAT_EQ(0.0f, rect->local_x());
  EXPECT_FLOAT_EQ(5.0f, rect->local_y());

  layout.set_direction(LinearLayout::kDown);
  layout.SizeAndLayOut();
  EXPECT_FLOAT_EQ(0.0f, rect->local_x());
  EXPECT_FLOAT_EQ(-5.0f, rect->local_y());

  layout.set_direction(LinearLayout::kLeft);
  layout.SizeAndLayOut();
  EXPECT_FLOAT_EQ(-5.0f, rect->local_x());
  EXPECT_FLOAT_EQ(0.0f, rect->local_y());

  layout.set_direction(LinearLayout::kRight);
  layout.SizeAndLayOut();
  EXPECT_FLOAT_EQ(5.0f, rect->local_x());
  EXPECT_FLOAT_EQ(0.0f, rect->local_y());
}

TEST(LinearLayout, NestedLayouts) {
  // Build a tree of elements, including nested layouts:
  //   parent_layout
  //     child_layout
  //       rect_a
  //       rect_b
  //     rect_c
  auto parent_layout = std::make_unique<LinearLayout>(LinearLayout::kDown);
  UiElement* p_parent_layout = parent_layout.get();
  auto child_layout = std::make_unique<LinearLayout>(LinearLayout::kDown);
  UiElement* p_child_layout = child_layout.get();
  auto rect_a = std::make_unique<TestElement>();
  TestElement* p_rect_a = rect_a.get();
  rect_a->SetSize(10, 10);
  child_layout->AddChild(std::move(rect_a));
  auto rect_b = std::make_unique<TestElement>();
  TestElement* p_rect_b = rect_b.get();
  rect_b->SetSize(10, 10);
  child_layout->AddChild(std::move(rect_b));
  auto rect_c = std::make_unique<TestElement>();
  TestElement* p_rect_c = rect_c.get();
  rect_c->SetSize(999, 10);
  parent_layout->AddChild(std::move(child_layout));
  parent_layout->AddChild(std::move(rect_c));

  auto scene = std::make_unique<UiScene>();
  scene->AddUiElement(kRoot, std::move(parent_layout));
  scene->OnBeginFrame(gfx::MicrosecondsToTicks(1), kStartHeadPose);

  // Ensure that layouts expand to include the cumulative size of children.
  EXPECT_FLOAT_EQ(p_parent_layout->size().width(), 999.f);
  EXPECT_FLOAT_EQ(p_parent_layout->size().height(), 30.f);
  EXPECT_FLOAT_EQ(p_child_layout->size().width(), 10.f);
  EXPECT_FLOAT_EQ(p_child_layout->size().height(), 20.f);

  // Ensure that children are at correct positions.
  EXPECT_FLOAT_EQ(p_rect_a->x(), 0);
  EXPECT_FLOAT_EQ(p_rect_a->y(), 10);
  EXPECT_FLOAT_EQ(p_rect_b->x(), 0);
  EXPECT_FLOAT_EQ(p_rect_b->y(), 0);
  EXPECT_FLOAT_EQ(p_rect_c->x(), 0);
  EXPECT_FLOAT_EQ(p_rect_c->y(), -10);
}

}  // namespace vr