File: ui_element_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 (338 lines) | stat: -rw-r--r-- 11,296 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
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
// Copyright 2016 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/ui_element.h"

#include <utility>

#include "base/functional/bind.h"
#include "cc/animation/keyframe_model.h"
#include "chrome/browser/vr/databinding/binding.h"
#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/keyframed_animation_curve.h"
#include "ui/gfx/animation/keyframe/test/animation_utils.h"
#include "ui/gfx/geometry/test/geometry_util.h"

namespace vr {

TEST(UiElement, BoundsContainChildren) {
  auto parent = std::make_unique<UiElement>();
  parent->set_bounds_contain_children(true);
  parent->set_padding(0.1, 0.2);

  auto c1 = std::make_unique<UiElement>();
  c1->SetSize(3.0f, 3.0f);
  c1->SetTranslate(2.5f, 2.5f, 0.0f);
  auto* c1_ptr = c1.get();
  parent->AddChild(std::move(c1));

  parent->SizeAndLayOut();
  EXPECT_RECTF_NEAR(gfx::RectF(2.5f, 2.5f, 3.2f, 3.4f),
                    gfx::RectF(parent->local_origin(), parent->size()),
                    kEpsilon);
  EXPECT_EQ(parent->GetCenter().ToString(), c1_ptr->GetCenter().ToString());

  auto c2 = std::make_unique<UiElement>();
  c2->SetSize(4.0f, 4.0f);
  c2->SetTranslate(-3.0f, 0.0f, 0.0f);
  parent->AddChild(std::move(c2));

  parent->SizeAndLayOut();
  EXPECT_RECTF_NEAR(gfx::RectF(-0.5f, 1.0f, 9.2f, 6.4f),
                    gfx::RectF(parent->local_origin(), parent->size()),
                    kEpsilon);

  auto c3 = std::make_unique<UiElement>();
  c3->SetSize(2.0f, 2.0f);
  c3->SetTranslate(0.0f, -2.0f, 0.0f);
  parent->AddChild(std::move(c3));

  parent->SizeAndLayOut();
  EXPECT_RECTF_NEAR(gfx::RectF(-0.5f, 0.5f, 9.2f, 7.4f),
                    gfx::RectF(parent->local_origin(), parent->size()),
                    kEpsilon);

  auto c4 = std::make_unique<UiElement>();
  c4->SetSize(2.0f, 2.0f);
  c4->SetTranslate(20.0f, 20.0f, 0.0f);
  c4->SetVisible(false);
  parent->AddChild(std::move(c4));

  // We expect no change due to an invisible child.
  parent->SizeAndLayOut();
  EXPECT_RECTF_NEAR(gfx::RectF(-0.5f, 0.5f, 9.2f, 7.4f),
                    gfx::RectF(parent->local_origin(), parent->size()),
                    kEpsilon);

  auto grand_parent = std::make_unique<UiElement>();
  grand_parent->set_bounds_contain_children(true);
  grand_parent->set_padding(0.1, 0.2);
  grand_parent->AddChild(std::move(parent));

  auto anchored = std::make_unique<UiElement>();
  anchored->set_y_anchoring(BOTTOM);
  anchored->set_contributes_to_parent_bounds(false);

  auto* anchored_ptr = anchored.get();
  grand_parent->AddChild(std::move(anchored));

  grand_parent->SizeAndLayOut();
  EXPECT_RECTF_NEAR(
      gfx::RectF(-0.5f, 0.5f, 9.4f, 7.8f),
      gfx::RectF(grand_parent->local_origin(), grand_parent->size()), kEpsilon);

  gfx::Point3F p;
  p = anchored_ptr->LocalTransform().MapPoint(p);
  EXPECT_FLOAT_EQ(-3.9, p.y());
}

TEST(UiElement, ReplaceChild) {
  auto a = std::make_unique<UiElement>();
  auto b = std::make_unique<UiElement>();
  auto c = std::make_unique<UiElement>();
  auto d = std::make_unique<UiElement>();
  auto x = std::make_unique<UiElement>();

  auto* x_ptr = x.get();
  auto* c_ptr = c.get();

  a->AddChild(std::move(b));
  a->AddChild(std::move(c));
  a->AddChild(std::move(d));

  auto removed = a->ReplaceChild(c_ptr, std::move(x));

  EXPECT_EQ(c_ptr, removed.get());
  EXPECT_EQ(x_ptr, a->children()[1].get());
}

TEST(UiElement, IgnoringAsymmetricPadding) {
  // This test ensures that when we ignore asymmetric padding that we don't
  // accidentally shift the location of the parent; it should stay put.
  auto a = std::make_unique<UiElement>();
  a->set_bounds_contain_children(true);

  auto b = std::make_unique<UiElement>();
  b->set_bounds_contain_children(true);
  b->set_bounds_contain_padding(false);
  b->set_padding(0.0f, 5.0f, 0.0f, 0.0f);

  auto c = std::make_unique<UiElement>();
  c->set_bounds_contain_children(true);
  c->set_bounds_contain_padding(false);
  c->set_padding(0.0f, 2.0f, 0.0f, 0.0f);

  auto d = std::make_unique<UiElement>();
  d->SetSize(0.5f, 0.5f);

  c->AddChild(std::move(d));
  c->SizeAndLayOut();
  b->AddChild(std::move(c));
  b->SizeAndLayOut();
  a->AddChild(std::move(b));
  a->SizeAndLayOut();

  a->UpdateWorldSpaceTransform(false);

  gfx::Point3F p;
  p = a->world_space_transform().MapPoint(p);

  EXPECT_POINT3F_EQ(gfx::Point3F(), p);
}

TEST(UiElement, BoundsContainPaddingWithAnchoring) {
  // If an element's bounds do not contain padding, then padding should be
  // discounted when doing anchoring.
  auto parent = std::make_unique<UiElement>();
  parent->SetSize(1.0, 1.0);

  auto child = std::make_unique<UiElement>();
  child->SetSize(0.5, 0.5);
  child->set_padding(2.0, 2.0);
  child->set_bounds_contain_padding(false);
  child->set_bounds_contain_children(true);

  auto* child_ptr = child.get();

  parent->AddChild(std::move(child));

  struct {
    LayoutAlignment x_anchoring;
    LayoutAlignment y_anchoring;
    gfx::Point3F expected_position;
  } test_cases[] = {
      {LEFT, NONE, {-0.5, 0, 0}},
      {RIGHT, NONE, {0.5, 0, 0}},
      {NONE, TOP, {0, 0.5, 0}},
      {NONE, BOTTOM, {0, -0.5, 0}},
  };

  for (auto test_case : test_cases) {
    child_ptr->set_contributes_to_parent_bounds(false);
    child_ptr->set_x_anchoring(test_case.x_anchoring);
    child_ptr->set_y_anchoring(test_case.y_anchoring);
    parent->SizeAndLayOut();
    gfx::Point3F p;
    p = child_ptr->LocalTransform().MapPoint(p);
    EXPECT_POINT3F_EQ(test_case.expected_position, p);
  }
}

TEST(UiElement, BoundsContainPaddingWithCentering) {
  // If an element's bounds do not contain padding, then padding should be
  // discounted when doing centering.
  auto parent = std::make_unique<UiElement>();
  parent->SetSize(1.0, 1.0);

  auto child = std::make_unique<UiElement>();
  child->set_padding(2.0, 2.0);
  child->set_bounds_contain_padding(false);
  child->set_bounds_contain_children(true);

  auto grandchild = std::make_unique<UiElement>();
  grandchild->SetSize(0.5, 0.5);

  child->AddChild(std::move(grandchild));

  auto* child_ptr = child.get();

  parent->AddChild(std::move(child));

  struct {
    LayoutAlignment x_centering;
    LayoutAlignment y_centering;
    gfx::Point3F expected_position;
  } test_cases[] = {
      {LEFT, NONE, {0.25, 0, 0}},
      {RIGHT, NONE, {-0.25, 0, 0}},
      {NONE, TOP, {0, -0.25, 0}},
      {NONE, BOTTOM, {0, 0.25, 0}},
  };

  for (auto test_case : test_cases) {
    child_ptr->set_contributes_to_parent_bounds(false);
    child_ptr->set_x_centering(test_case.x_centering);
    child_ptr->set_y_centering(test_case.y_centering);
    parent->SizeAndLayOut();
    EXPECT_POINT3F_EQ(test_case.expected_position,
                      child_ptr->LocalTransform().MapPoint(gfx::Point3F()));
  }
}

TEST(UiElement, BoundsContainScaledChildren) {
  auto a = std::make_unique<UiElement>();
  a->SetSize(0.4, 0.3);

  auto b = std::make_unique<UiElement>();
  b->SetSize(0.2, 0.2);
  b->SetTranslate(0.6, 0.0, 0.0);
  b->SetScale(2.0, 3.0, 1.0);

  auto c = std::make_unique<UiElement>();
  c->set_bounds_contain_children(true);
  c->set_padding(0.05, 0.05);
  c->AddChild(std::move(a));
  c->AddChild(std::move(b));

  c->SizeAndLayOut();
  EXPECT_RECTF_NEAR(gfx::RectF(0.3f, 0.0f, 1.1f, 0.7f),
                    gfx::RectF(c->local_origin(), c->size()), kEpsilon);
}

TEST(UiElement, AnimateSize) {
  UiScene scene;
  scene.RunFirstFrameForTest();
  auto rect = std::make_unique<UiElement>();
  rect->SetSize(10, 100);
  rect->AddKeyframeModel(gfx::CreateSizeAnimation(
      rect.get(), 1, BOUNDS, gfx::SizeF(10, 100), gfx::SizeF(20, 200),
      gfx::MicrosecondsToDelta(10000)));
  UiElement* rect_ptr = rect.get();
  scene.AddUiElement(kRoot, std::move(rect));
  base::TimeTicks start_time = gfx::MicrosecondsToTicks(1);
  EXPECT_TRUE(scene.OnBeginFrame(start_time, kStartHeadPose));
  EXPECT_SIZEF_EQ(gfx::SizeF(10, 100), rect_ptr->size());
  EXPECT_TRUE(scene.OnBeginFrame(start_time + gfx::MicrosecondsToDelta(10000),
                                 kStartHeadPose));
  EXPECT_SIZEF_EQ(gfx::SizeF(20, 200), rect_ptr->size());
}

TEST(UiElement, AnimationAffectsInheritableTransform) {
  UiScene scene;
  scene.RunFirstFrameForTest();
  auto rect = std::make_unique<UiElement>();
  UiElement* rect_ptr = rect.get();
  scene.AddUiElement(kRoot, std::move(rect));

  gfx::TransformOperations from_operations;
  from_operations.AppendTranslate(10, 100, 1000);
  gfx::TransformOperations to_operations;
  to_operations.AppendTranslate(20, 200, 2000);
  rect_ptr->AddKeyframeModel(gfx::CreateTransformAnimation(
      rect_ptr, 2, TRANSFORM, from_operations, to_operations,
      gfx::MicrosecondsToDelta(10000)));

  base::TimeTicks start_time = gfx::MicrosecondsToTicks(1);
  EXPECT_TRUE(scene.OnBeginFrame(start_time, kStartHeadPose));
  EXPECT_POINT3F_EQ(gfx::Point3F(10, 100, 1000),
                    rect_ptr->LocalTransform().MapPoint(gfx::Point3F()));
  EXPECT_TRUE(scene.OnBeginFrame(start_time + gfx::MicrosecondsToDelta(10000),
                                 kStartHeadPose));
  EXPECT_POINT3F_EQ(gfx::Point3F(20, 200, 2000),
                    rect_ptr->LocalTransform().MapPoint(gfx::Point3F()));
}

TEST(UiElement, CoordinatedVisibilityTransitions) {
  UiScene scene;
  bool value = false;

  auto parent = std::make_unique<UiElement>();
  auto* parent_ptr = parent.get();
  parent->SetVisible(false);
  parent->SetTransitionedProperties({OPACITY});
  parent->AddBinding(std::make_unique<Binding<bool>>(
      VR_BIND_LAMBDA([](bool* value) { return *value; },
                     base::Unretained(&value)),
      VR_BIND_LAMBDA(
          [](UiElement* e, const bool& value) { e->SetVisible(value); },
          parent_ptr)));

  auto child = std::make_unique<UiElement>();
  auto* child_ptr = child.get();
  child->SetVisible(false);
  child->SetTransitionedProperties({OPACITY});
  child->AddBinding(std::make_unique<Binding<bool>>(
      VR_BIND_LAMBDA([](bool* value) { return *value; },
                     base::Unretained(&value)),
      VR_BIND_LAMBDA(
          [](UiElement* e, const bool& value) { e->SetVisible(value); },
          child_ptr)));

  parent->AddChild(std::move(child));
  scene.AddUiElement(kRoot, std::move(parent));

  scene.OnBeginFrame(gfx::MsToTicks(0), kStartHeadPose);

  value = true;

  scene.OnBeginFrame(gfx::MsToTicks(16), kStartHeadPose);

  // We should have started animating both, and they should both be at opacity
  // zero given that this is the first frame. This does not guarantee that
  // they've started animating together, however. Even if the animation was
  // unticked, we would still be at opacity zero. We must tick a second time to
  // reach a non-zero value.
  EXPECT_TRUE(parent_ptr->IsAnimatingProperty(OPACITY));
  EXPECT_TRUE(child_ptr->IsAnimatingProperty(OPACITY));
  EXPECT_EQ(child_ptr->opacity(), parent_ptr->opacity());

  scene.OnBeginFrame(gfx::MsToTicks(32), kStartHeadPose);
  EXPECT_EQ(child_ptr->opacity(), parent_ptr->opacity());
  EXPECT_LT(0.0f, child_ptr->opacity());
}

}  // namespace vr