1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/v2/public/view.h"
7 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/compositor/layer.h"
10 #include "ui/compositor/layer_type.h"
14 // View ------------------------------------------------------------------------
16 typedef testing::Test ViewTest
;
18 TEST_F(ViewTest
, AddChild
) {
22 EXPECT_EQ(1U, v1
.children().size());
25 TEST_F(ViewTest
, RemoveChild
) {
29 EXPECT_EQ(1U, v1
.children().size());
31 EXPECT_EQ(0U, v1
.children().size());
34 TEST_F(ViewTest
, Reparent
) {
39 EXPECT_EQ(1U, v1
.children().size());
41 EXPECT_EQ(1U, v2
.children().size());
42 EXPECT_EQ(0U, v1
.children().size());
45 TEST_F(ViewTest
, Contains
) {
51 EXPECT_TRUE(v1
.Contains(v11
));
53 // Indirect descendant.
54 View
* v111
= new View
;
56 EXPECT_TRUE(v1
.Contains(v111
));
59 TEST_F(ViewTest
, Stacking
) {
68 // Order: v11, v12, v13
69 EXPECT_EQ(3U, v1
.children().size());
70 EXPECT_EQ(v11
, v1
.children().front());
71 EXPECT_EQ(v13
, v1
.children().back());
74 // Resulting order: v12, v13, v11
75 v1
.StackChildAtTop(v11
);
76 EXPECT_EQ(v12
, v1
.children().front());
77 EXPECT_EQ(v11
, v1
.children().back());
80 // Resulting order: v11, v12, v13
81 v1
.StackChildAtBottom(v11
);
82 EXPECT_EQ(v11
, v1
.children().front());
83 EXPECT_EQ(v13
, v1
.children().back());
85 // Move v11 above v12.
86 // Resulting order: v12. v11, v13
87 v1
.StackChildAbove(v11
, v12
);
88 EXPECT_EQ(v12
, v1
.children().front());
89 EXPECT_EQ(v13
, v1
.children().back());
91 // Move v11 below v12.
92 // Resulting order: v11, v12, v13
93 v1
.StackChildBelow(v11
, v12
);
94 EXPECT_EQ(v11
, v1
.children().front());
95 EXPECT_EQ(v13
, v1
.children().back());
98 TEST_F(ViewTest
, Layer
) {
100 v1
.CreateLayer(ui::LAYER_NOT_DRAWN
);
101 EXPECT_TRUE(v1
.HasLayer());
103 EXPECT_FALSE(v1
.HasLayer());
105 v1
.CreateLayer(ui::LAYER_NOT_DRAWN
);
106 scoped_ptr
<ui::Layer
>(v1
.AcquireLayer());
107 // Acquiring the layer transfers ownership to the scoped_ptr above, so this
108 // test passes if it doesn't crash.
111 // ViewObserver ----------------------------------------------------------------
113 typedef testing::Test ViewObserverTest
;
115 bool TreeChangeParamsMatch(const ViewObserver::TreeChangeParams
& lhs
,
116 const ViewObserver::TreeChangeParams
& rhs
) {
117 return lhs
.target
== rhs
.target
&& lhs
.old_parent
== rhs
.old_parent
&&
118 lhs
.new_parent
== rhs
.new_parent
&& lhs
.receiver
== rhs
.receiver
&&
119 lhs
.phase
== rhs
.phase
;
122 class TreeChangeObserver
: public ViewObserver
{
124 explicit TreeChangeObserver(View
* observee
) : observee_(observee
) {
125 observee_
->AddObserver(this);
127 virtual ~TreeChangeObserver() {
128 observee_
->RemoveObserver(this);
132 received_params_
.clear();
135 const std::vector
<TreeChangeParams
>& received_params() {
136 return received_params_
;
140 // Overridden from ViewObserver:
141 virtual void OnViewTreeChange(const TreeChangeParams
& params
) OVERRIDE
{
142 received_params_
.push_back(params
);
146 std::vector
<TreeChangeParams
> received_params_
;
148 DISALLOW_COPY_AND_ASSIGN(TreeChangeObserver
);
151 // Adds/Removes v11 to v1.
152 TEST_F(ViewObserverTest
, TreeChange_SimpleAddRemove
) {
154 TreeChangeObserver
o1(&v1
);
155 EXPECT_TRUE(o1
.received_params().empty());
158 v11
.set_owned_by_parent(false);
159 TreeChangeObserver
o11(&v11
);
160 EXPECT_TRUE(o11
.received_params().empty());
166 EXPECT_EQ(1U, o1
.received_params().size());
167 ViewObserver::TreeChangeParams p1
;
170 p1
.old_parent
= NULL
;
172 p1
.phase
= ViewObserver::DISPOSITION_CHANGED
;
173 EXPECT_TRUE(TreeChangeParamsMatch(p1
, o1
.received_params().back()));
175 EXPECT_EQ(2U, o11
.received_params().size());
176 ViewObserver::TreeChangeParams p11
= p1
;
178 p11
.phase
= ViewObserver::DISPOSITION_CHANGING
;
179 EXPECT_TRUE(TreeChangeParamsMatch(p11
, o11
.received_params().front()));
180 p11
.phase
= ViewObserver::DISPOSITION_CHANGED
;
181 EXPECT_TRUE(TreeChangeParamsMatch(p11
, o11
.received_params().back()));
185 EXPECT_TRUE(o1
.received_params().empty());
186 EXPECT_TRUE(o11
.received_params().empty());
190 v1
.RemoveChild(&v11
);
192 EXPECT_EQ(1U, o1
.received_params().size());
196 p1
.new_parent
= NULL
;
197 p1
.phase
= ViewObserver::DISPOSITION_CHANGING
;
198 EXPECT_TRUE(TreeChangeParamsMatch(p1
, o1
.received_params().back()));
200 EXPECT_EQ(2U, o11
.received_params().size());
203 EXPECT_TRUE(TreeChangeParamsMatch(p11
, o11
.received_params().front()));
204 p11
.phase
= ViewObserver::DISPOSITION_CHANGED
;
205 EXPECT_TRUE(TreeChangeParamsMatch(p11
, o11
.received_params().back()));
208 // Creates these two trees:
214 // Then adds/removes v111 from v11.
215 TEST_F(ViewObserverTest
, TreeChange_NestedAddRemove
) {
216 View v1
, v11
, v111
, v1111
, v1112
;
219 v11
.set_owned_by_parent(false);
222 // Tree to be attached.
223 v111
.set_owned_by_parent(false);
224 v1111
.set_owned_by_parent(false);
225 v111
.AddChild(&v1111
);
226 v1112
.set_owned_by_parent(false);
227 v111
.AddChild(&v1112
);
229 TreeChangeObserver
o1(&v1
), o11(&v11
), o111(&v111
), o1111(&v1111
),
231 ViewObserver::TreeChangeParams p1
, p11
, p111
, p1111
, p1112
;
237 EXPECT_EQ(1U, o1
.received_params().size());
240 p1
.old_parent
= NULL
;
241 p1
.new_parent
= &v11
;
242 p1
.phase
= ViewObserver::DISPOSITION_CHANGED
;
243 EXPECT_TRUE(TreeChangeParamsMatch(p1
, o1
.received_params().back()));
245 EXPECT_EQ(1U, o11
.received_params().size());
248 EXPECT_TRUE(TreeChangeParamsMatch(p11
, o11
.received_params().back()));
250 EXPECT_EQ(2U, o111
.received_params().size());
252 p111
.receiver
= &v111
;
253 p111
.phase
= ViewObserver::DISPOSITION_CHANGING
;
254 EXPECT_TRUE(TreeChangeParamsMatch(p111
, o111
.received_params().front()));
255 p111
.phase
= ViewObserver::DISPOSITION_CHANGED
;
256 EXPECT_TRUE(TreeChangeParamsMatch(p111
, o111
.received_params().back()));
258 EXPECT_EQ(2U, o1111
.received_params().size());
260 p1111
.receiver
= &v1111
;
261 p1111
.phase
= ViewObserver::DISPOSITION_CHANGING
;
262 EXPECT_TRUE(TreeChangeParamsMatch(p1111
, o1111
.received_params().front()));
263 p1111
.phase
= ViewObserver::DISPOSITION_CHANGED
;
264 EXPECT_TRUE(TreeChangeParamsMatch(p1111
, o1111
.received_params().back()));
266 EXPECT_EQ(2U, o1112
.received_params().size());
268 p1112
.receiver
= &v1112
;
269 p1112
.phase
= ViewObserver::DISPOSITION_CHANGING
;
270 EXPECT_TRUE(TreeChangeParamsMatch(p1112
, o1112
.received_params().front()));
271 p1112
.phase
= ViewObserver::DISPOSITION_CHANGED
;
272 EXPECT_TRUE(TreeChangeParamsMatch(p1112
, o1112
.received_params().back()));
280 EXPECT_TRUE(o1
.received_params().empty());
281 EXPECT_TRUE(o11
.received_params().empty());
282 EXPECT_TRUE(o111
.received_params().empty());
283 EXPECT_TRUE(o1111
.received_params().empty());
284 EXPECT_TRUE(o1112
.received_params().empty());
286 v11
.RemoveChild(&v111
);
288 EXPECT_EQ(1U, o1
.received_params().size());
291 p1
.old_parent
= &v11
;
292 p1
.new_parent
= NULL
;
293 p1
.phase
= ViewObserver::DISPOSITION_CHANGING
;
294 EXPECT_TRUE(TreeChangeParamsMatch(p1
, o1
.received_params().back()));
296 EXPECT_EQ(1U, o11
.received_params().size());
299 EXPECT_TRUE(TreeChangeParamsMatch(p11
, o11
.received_params().back()));
301 EXPECT_EQ(2U, o111
.received_params().size());
303 p111
.receiver
= &v111
;
304 p111
.phase
= ViewObserver::DISPOSITION_CHANGING
;
305 EXPECT_TRUE(TreeChangeParamsMatch(p111
, o111
.received_params().front()));
306 p111
.phase
= ViewObserver::DISPOSITION_CHANGED
;
307 EXPECT_TRUE(TreeChangeParamsMatch(p111
, o111
.received_params().back()));
309 EXPECT_EQ(2U, o1111
.received_params().size());
311 p1111
.receiver
= &v1111
;
312 p1111
.phase
= ViewObserver::DISPOSITION_CHANGING
;
313 EXPECT_TRUE(TreeChangeParamsMatch(p1111
, o1111
.received_params().front()));
314 p1111
.phase
= ViewObserver::DISPOSITION_CHANGED
;
315 EXPECT_TRUE(TreeChangeParamsMatch(p1111
, o1111
.received_params().back()));
317 EXPECT_EQ(2U, o1112
.received_params().size());
319 p1112
.receiver
= &v1112
;
320 p1112
.phase
= ViewObserver::DISPOSITION_CHANGING
;
321 EXPECT_TRUE(TreeChangeParamsMatch(p1112
, o1112
.received_params().front()));
322 p1112
.phase
= ViewObserver::DISPOSITION_CHANGED
;
323 EXPECT_TRUE(TreeChangeParamsMatch(p1112
, o1112
.received_params().back()));
326 TEST_F(ViewObserverTest
, TreeChange_Reparent
) {
327 View v1
, v11
, v12
, v111
;
328 v11
.set_owned_by_parent(false);
329 v111
.set_owned_by_parent(false);
330 v12
.set_owned_by_parent(false);
335 TreeChangeObserver
o1(&v1
), o11(&v11
), o12(&v12
), o111(&v111
);
340 // v1 (root) should see both changing and changed notifications.
341 EXPECT_EQ(2U, o1
.received_params().size());
342 ViewObserver::TreeChangeParams p1
;
345 p1
.old_parent
= &v11
;
346 p1
.new_parent
= &v12
;
347 p1
.phase
= ViewObserver::DISPOSITION_CHANGING
;
348 EXPECT_TRUE(TreeChangeParamsMatch(p1
, o1
.received_params().front()));
349 p1
.phase
= ViewObserver::DISPOSITION_CHANGED
;
350 EXPECT_TRUE(TreeChangeParamsMatch(p1
, o1
.received_params().back()));
352 // v11 should see changing notifications.
353 EXPECT_EQ(1U, o11
.received_params().size());
354 ViewObserver::TreeChangeParams p11
;
357 p11
.phase
= ViewObserver::DISPOSITION_CHANGING
;
358 EXPECT_TRUE(TreeChangeParamsMatch(p11
, o11
.received_params().back()));
360 // v12 should see changed notifications.
361 EXPECT_EQ(1U, o12
.received_params().size());
362 ViewObserver::TreeChangeParams p12
;
365 p12
.phase
= ViewObserver::DISPOSITION_CHANGED
;
366 EXPECT_TRUE(TreeChangeParamsMatch(p12
, o12
.received_params().back()));
368 // v111 should see both changing and changed notifications.
369 EXPECT_EQ(2U, o111
.received_params().size());
370 ViewObserver::TreeChangeParams p111
;
372 p111
.receiver
= &v111
;
373 p111
.phase
= ViewObserver::DISPOSITION_CHANGING
;
374 EXPECT_TRUE(TreeChangeParamsMatch(p111
, o111
.received_params().front()));
375 p111
.phase
= ViewObserver::DISPOSITION_CHANGED
;
376 EXPECT_TRUE(TreeChangeParamsMatch(p111
, o111
.received_params().back()));
379 class VisibilityObserver
: public ViewObserver
{
381 typedef std::pair
<ViewObserver::DispositionChangePhase
, bool> LogEntry
;
382 typedef std::vector
<LogEntry
> LogEntries
;
384 VisibilityObserver(View
* view
) : view_(view
) {
385 view_
->AddObserver(this);
387 virtual ~VisibilityObserver() {
388 view_
->RemoveObserver(this);
391 const LogEntries
& log_entries() const { return log_entries_
; }
394 // Overridden from ViewObserver:
395 virtual void OnViewVisibilityChange(
397 ViewObserver::DispositionChangePhase phase
) OVERRIDE
{
398 DCHECK_EQ(view_
, view
);
399 log_entries_
.push_back(std::make_pair(phase
, view
->visible()));
403 LogEntries log_entries_
;
405 DISALLOW_COPY_AND_ASSIGN(VisibilityObserver
);
408 TEST_F(ViewObserverTest
, VisibilityChange
) {
409 View v1
; // Starts out visible.
411 VisibilityObserver
o1(&v1
);
413 v1
.SetVisible(false);
415 EXPECT_EQ(2U, o1
.log_entries().size());
416 EXPECT_EQ(ViewObserver::DISPOSITION_CHANGING
, o1
.log_entries().front().first
);
417 EXPECT_EQ(o1
.log_entries().front().second
, true);
418 EXPECT_EQ(ViewObserver::DISPOSITION_CHANGED
, o1
.log_entries().back().first
);
419 EXPECT_EQ(o1
.log_entries().back().second
, false);
422 class BoundsObserver
: public ViewObserver
{
424 typedef std::pair
<gfx::Rect
, gfx::Rect
> BoundsChange
;
425 typedef std::vector
<BoundsChange
> BoundsChanges
;
427 explicit BoundsObserver(View
* view
) : view_(view
) {
428 view_
->AddObserver(this);
430 virtual ~BoundsObserver() {
431 view_
->RemoveObserver(this);
434 const BoundsChanges
& bounds_changes() const { return bounds_changes_
; }
437 virtual void OnViewBoundsChanged(View
* view
,
438 const gfx::Rect
& old_bounds
,
439 const gfx::Rect
& new_bounds
) OVERRIDE
{
440 DCHECK_EQ(view_
, view
);
441 bounds_changes_
.push_back(std::make_pair(old_bounds
, new_bounds
));
445 BoundsChanges bounds_changes_
;
447 DISALLOW_COPY_AND_ASSIGN(BoundsObserver
);
450 TEST_F(ViewObserverTest
, BoundsChanged
) {
452 BoundsObserver
o1(&v1
);
454 gfx::Rect
new_bounds(0, 0, 10, 10);
456 v1
.SetBounds(new_bounds
);
457 EXPECT_EQ(1U, o1
.bounds_changes().size());
458 EXPECT_EQ(gfx::Rect(), o1
.bounds_changes().front().first
);
459 EXPECT_EQ(new_bounds
, o1
.bounds_changes().front().second
);