OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/ui_scene.h" | 5 #include "chrome/browser/android/vr_shell/ui_scene.h" |
6 | 6 |
7 #define _USE_MATH_DEFINES // For M_PI in MSVC. | 7 #define _USE_MATH_DEFINES // For M_PI in MSVC. |
8 #include <cmath> | 8 #include <cmath> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 #define EXPECT_VEC3F_NEAR(a, b) \ | 22 #define EXPECT_VEC3F_NEAR(a, b) \ |
23 EXPECT_NEAR(a.x, b.x, TOLERANCE); \ | 23 EXPECT_NEAR(a.x, b.x, TOLERANCE); \ |
24 EXPECT_NEAR(a.y, b.y, TOLERANCE); \ | 24 EXPECT_NEAR(a.y, b.y, TOLERANCE); \ |
25 EXPECT_NEAR(a.z, b.z, TOLERANCE); | 25 EXPECT_NEAR(a.z, b.z, TOLERANCE); |
26 | 26 |
27 namespace vr_shell { | 27 namespace vr_shell { |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
| 31 base::TimeTicks usToTicks(uint64_t us) { |
| 32 return base::TimeTicks::FromInternalValue(us); |
| 33 } |
| 34 |
| 35 base::TimeDelta usToDelta(uint64_t us) { |
| 36 return base::TimeDelta::FromInternalValue(us); |
| 37 } |
| 38 |
31 void addElement(UiScene* scene, int id) { | 39 void addElement(UiScene* scene, int id) { |
32 auto element = base::MakeUnique<ContentRectangle>(); | 40 auto element = base::MakeUnique<ContentRectangle>(); |
33 element->id = id; | 41 element->id = id; |
34 scene->AddUiElement(std::move(element)); | 42 scene->AddUiElement(std::move(element)); |
35 } | 43 } |
36 | 44 |
37 void addAnimation(UiScene* scene, | 45 void addAnimation(UiScene* scene, |
38 int element_id, | 46 int element_id, |
39 int animation_id, | 47 int animation_id, |
40 Animation::Property property) { | 48 Animation::Property property) { |
41 std::unique_ptr<easing::Easing> easing = base::MakeUnique<easing::Linear>(); | 49 std::unique_ptr<easing::Easing> easing = base::MakeUnique<easing::Linear>(); |
42 std::vector<float> from = {}; | 50 std::vector<float> from = {}; |
43 std::vector<float> to = {1, 1, 1, 1}; | 51 std::vector<float> to = {1, 1, 1, 1}; |
44 auto animation = base::MakeUnique<Animation>( | 52 auto animation = |
45 animation_id, property, std::move(easing), from, to, 0, 1); | 53 base::MakeUnique<Animation>(animation_id, property, std::move(easing), |
| 54 from, to, usToTicks(0), usToDelta(1)); |
46 scene->AddAnimation(element_id, std::move(animation)); | 55 scene->AddAnimation(element_id, std::move(animation)); |
47 } | 56 } |
48 | 57 |
49 } // namespace | 58 } // namespace |
50 | 59 |
51 TEST(UiScene, AddRemoveElements) { | 60 TEST(UiScene, AddRemoveElements) { |
52 UiScene scene; | 61 UiScene scene; |
53 | 62 |
54 EXPECT_EQ(scene.GetUiElements().size(), 0u); | 63 EXPECT_EQ(scene.GetUiElements().size(), 0u); |
55 addElement(&scene, 0); | 64 addElement(&scene, 0); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 element->size = {1, 1, 1}; | 128 element->size = {1, 1, 1}; |
120 element->scale = {2, 2, 1}; | 129 element->scale = {2, 2, 1}; |
121 element->rotation = {0, 0, 1, M_PI / 2}; | 130 element->rotation = {0, 0, 1, M_PI / 2}; |
122 element->translation = {3, 0, 0}; | 131 element->translation = {3, 0, 0}; |
123 scene.AddUiElement(std::move(element)); | 132 scene.AddUiElement(std::move(element)); |
124 const ContentRectangle* child = scene.GetUiElementById(1); | 133 const ContentRectangle* child = scene.GetUiElementById(1); |
125 | 134 |
126 const gvr::Vec3f origin({0, 0, 0}); | 135 const gvr::Vec3f origin({0, 0, 0}); |
127 const gvr::Vec3f point({1, 0, 0}); | 136 const gvr::Vec3f point({1, 0, 0}); |
128 | 137 |
129 scene.UpdateTransforms(0); | 138 scene.UpdateTransforms(usToTicks(0)); |
130 auto new_origin = MatrixVectorMul(child->TransformMatrix(), origin); | 139 auto new_origin = MatrixVectorMul(child->TransformMatrix(), origin); |
131 auto new_point = MatrixVectorMul(child->TransformMatrix(), point); | 140 auto new_point = MatrixVectorMul(child->TransformMatrix(), point); |
132 EXPECT_VEC3F_NEAR(gvr::Vec3f({6, 10, 0}), new_origin); | 141 EXPECT_VEC3F_NEAR(gvr::Vec3f({6, 10, 0}), new_origin); |
133 EXPECT_VEC3F_NEAR(gvr::Vec3f({0, 10, 0}), new_point); | 142 EXPECT_VEC3F_NEAR(gvr::Vec3f({0, 10, 0}), new_point); |
134 } | 143 } |
135 | 144 |
136 TEST(UiScene, Opacity) { | 145 TEST(UiScene, Opacity) { |
137 UiScene scene; | 146 UiScene scene; |
138 | 147 |
139 auto element = base::MakeUnique<ContentRectangle>(); | 148 auto element = base::MakeUnique<ContentRectangle>(); |
140 element->id = 0; | 149 element->id = 0; |
141 element->opacity = 0.5; | 150 element->opacity = 0.5; |
142 scene.AddUiElement(std::move(element)); | 151 scene.AddUiElement(std::move(element)); |
143 | 152 |
144 element = base::MakeUnique<ContentRectangle>(); | 153 element = base::MakeUnique<ContentRectangle>(); |
145 element->id = 1; | 154 element->id = 1; |
146 element->parent_id = 0; | 155 element->parent_id = 0; |
147 element->opacity = 0.5; | 156 element->opacity = 0.5; |
148 scene.AddUiElement(std::move(element)); | 157 scene.AddUiElement(std::move(element)); |
149 | 158 |
150 scene.UpdateTransforms(0); | 159 scene.UpdateTransforms(usToTicks(0)); |
151 EXPECT_EQ(scene.GetUiElementById(0)->computed_opacity, 0.5f); | 160 EXPECT_EQ(scene.GetUiElementById(0)->computed_opacity, 0.5f); |
152 EXPECT_EQ(scene.GetUiElementById(1)->computed_opacity, 0.25f); | 161 EXPECT_EQ(scene.GetUiElementById(1)->computed_opacity, 0.25f); |
153 } | 162 } |
154 | 163 |
155 TEST(UiScene, LockToFov) { | 164 TEST(UiScene, LockToFov) { |
156 UiScene scene; | 165 UiScene scene; |
157 | 166 |
158 auto element = base::MakeUnique<ContentRectangle>(); | 167 auto element = base::MakeUnique<ContentRectangle>(); |
159 element->id = 0; | 168 element->id = 0; |
160 element->lock_to_fov = true; | 169 element->lock_to_fov = true; |
161 scene.AddUiElement(std::move(element)); | 170 scene.AddUiElement(std::move(element)); |
162 | 171 |
163 element = base::MakeUnique<ContentRectangle>(); | 172 element = base::MakeUnique<ContentRectangle>(); |
164 element->id = 1; | 173 element->id = 1; |
165 element->parent_id = 0; | 174 element->parent_id = 0; |
166 element->lock_to_fov = false; | 175 element->lock_to_fov = false; |
167 scene.AddUiElement(std::move(element)); | 176 scene.AddUiElement(std::move(element)); |
168 | 177 |
169 scene.UpdateTransforms(0); | 178 scene.UpdateTransforms(usToTicks(0)); |
170 EXPECT_EQ(scene.GetUiElementById(0)->computed_lock_to_fov, true); | 179 EXPECT_EQ(scene.GetUiElementById(0)->computed_lock_to_fov, true); |
171 EXPECT_EQ(scene.GetUiElementById(1)->computed_lock_to_fov, true); | 180 EXPECT_EQ(scene.GetUiElementById(1)->computed_lock_to_fov, true); |
172 } | 181 } |
173 | 182 |
174 typedef struct { | 183 typedef struct { |
175 XAnchoring x_anchoring; | 184 XAnchoring x_anchoring; |
176 YAnchoring y_anchoring; | 185 YAnchoring y_anchoring; |
177 float expected_x; | 186 float expected_x; |
178 float expected_y; | 187 float expected_y; |
179 } AnchoringTestCase; | 188 } AnchoringTestCase; |
(...skipping 11 matching lines...) Expand all Loading... |
191 scene.AddUiElement(std::move(element)); | 200 scene.AddUiElement(std::move(element)); |
192 | 201 |
193 // Add a child to the parent, with anchoring. | 202 // Add a child to the parent, with anchoring. |
194 element = base::MakeUnique<ContentRectangle>(); | 203 element = base::MakeUnique<ContentRectangle>(); |
195 element->id = 1; | 204 element->id = 1; |
196 element->parent_id = 0; | 205 element->parent_id = 0; |
197 element->x_anchoring = GetParam().x_anchoring; | 206 element->x_anchoring = GetParam().x_anchoring; |
198 element->y_anchoring = GetParam().y_anchoring; | 207 element->y_anchoring = GetParam().y_anchoring; |
199 scene.AddUiElement(std::move(element)); | 208 scene.AddUiElement(std::move(element)); |
200 | 209 |
201 scene.UpdateTransforms(0); | 210 scene.UpdateTransforms(usToTicks(0)); |
202 const ContentRectangle* child = scene.GetUiElementById(1); | 211 const ContentRectangle* child = scene.GetUiElementById(1); |
203 EXPECT_NEAR(child->GetCenter().x, GetParam().expected_x, TOLERANCE); | 212 EXPECT_NEAR(child->GetCenter().x, GetParam().expected_x, TOLERANCE); |
204 EXPECT_NEAR(child->GetCenter().y, GetParam().expected_y, TOLERANCE); | 213 EXPECT_NEAR(child->GetCenter().y, GetParam().expected_y, TOLERANCE); |
205 scene.RemoveUiElement(1); | 214 scene.RemoveUiElement(1); |
206 } | 215 } |
207 | 216 |
208 const std::vector<AnchoringTestCase> anchoring_test_cases = { | 217 const std::vector<AnchoringTestCase> anchoring_test_cases = { |
209 {XAnchoring::XNONE, YAnchoring::YNONE, 0, 0}, | 218 {XAnchoring::XNONE, YAnchoring::YNONE, 0, 0}, |
210 {XAnchoring::XLEFT, YAnchoring::YNONE, -2, 0}, | 219 {XAnchoring::XLEFT, YAnchoring::YNONE, -2, 0}, |
211 {XAnchoring::XRIGHT, YAnchoring::YNONE, 2, 0}, | 220 {XAnchoring::XRIGHT, YAnchoring::YNONE, 2, 0}, |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 to->SetInteger("a", 203); | 422 to->SetInteger("a", 203); |
414 dict.Set("to", std::move(to)); | 423 dict.Set("to", std::move(to)); |
415 | 424 |
416 auto from = base::MakeUnique<base::DictionaryValue>(); | 425 auto from = base::MakeUnique<base::DictionaryValue>(); |
417 from->SetInteger("x", 300); | 426 from->SetInteger("x", 300); |
418 from->SetInteger("y", 301); | 427 from->SetInteger("y", 301); |
419 from->SetInteger("z", 302); | 428 from->SetInteger("z", 302); |
420 from->SetInteger("a", 303); | 429 from->SetInteger("a", 303); |
421 dict.Set("from", std::move(from)); | 430 dict.Set("from", std::move(from)); |
422 | 431 |
423 scene.AddAnimationFromDict(dict, 10000000); | 432 scene.AddAnimationFromDict(dict, usToTicks(10000000)); |
424 const auto* element = scene.GetUiElementById(0); | 433 const auto* element = scene.GetUiElementById(0); |
425 const auto* animation = element->animations[0].get(); | 434 const auto* animation = element->animations[0].get(); |
426 EXPECT_NE(animation, nullptr); | 435 EXPECT_NE(animation, nullptr); |
427 | 436 |
428 EXPECT_EQ(animation->id, 10); | 437 EXPECT_EQ(animation->id, 10); |
429 | 438 |
430 EXPECT_FLOAT_EQ(animation->to[0], 200); | 439 EXPECT_FLOAT_EQ(200, animation->to[0]); |
431 EXPECT_FLOAT_EQ(animation->to[1], 201); | 440 EXPECT_FLOAT_EQ(201, animation->to[1]); |
432 EXPECT_FLOAT_EQ(animation->to[2], 202); | 441 EXPECT_FLOAT_EQ(202, animation->to[2]); |
433 EXPECT_FLOAT_EQ(animation->to[3], 203); | 442 EXPECT_FLOAT_EQ(203, animation->to[3]); |
434 | 443 |
435 EXPECT_FLOAT_EQ(animation->from[0], 300); | 444 EXPECT_FLOAT_EQ(300, animation->from[0]); |
436 EXPECT_FLOAT_EQ(animation->from[1], 301); | 445 EXPECT_FLOAT_EQ(301, animation->from[1]); |
437 EXPECT_FLOAT_EQ(animation->from[2], 302); | 446 EXPECT_FLOAT_EQ(302, animation->from[2]); |
438 EXPECT_FLOAT_EQ(animation->from[3], 303); | 447 EXPECT_FLOAT_EQ(303, animation->from[3]); |
439 | 448 |
440 EXPECT_EQ(animation->start, 22345000); | 449 EXPECT_EQ(usToTicks(22345000), animation->start); |
441 EXPECT_EQ(animation->duration, 54321000); | 450 EXPECT_EQ(usToDelta(54321000), animation->duration); |
442 } | 451 } |
443 | 452 |
444 } // namespace vr_shell | 453 } // namespace vr_shell |
OLD | NEW |