1 // Copyright (c) 2012 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.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/rand_util.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/base/accelerators/accelerator.h"
13 #include "ui/base/clipboard/clipboard.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/compositor/compositor.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/compositor/layer_animator.h"
18 #include "ui/compositor/test/draw_waiter_for_test.h"
19 #include "ui/events/event.h"
20 #include "ui/events/keycodes/keyboard_codes.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/path.h"
23 #include "ui/gfx/transform.h"
24 #include "ui/strings/grit/ui_strings.h"
25 #include "ui/views/background.h"
26 #include "ui/views/controls/native/native_view_host.h"
27 #include "ui/views/controls/scroll_view.h"
28 #include "ui/views/controls/textfield/textfield.h"
29 #include "ui/views/focus/view_storage.h"
30 #include "ui/views/test/views_test_base.h"
31 #include "ui/views/view.h"
32 #include "ui/views/views_delegate.h"
33 #include "ui/views/widget/native_widget.h"
34 #include "ui/views/widget/root_view.h"
35 #include "ui/views/window/dialog_client_view.h"
36 #include "ui/views/window/dialog_delegate.h"
38 using base::ASCIIToUTF16
;
42 // Returns true if |ancestor| is an ancestor of |layer|.
43 bool LayerIsAncestor(const ui::Layer
* ancestor
, const ui::Layer
* layer
) {
44 while (layer
&& layer
!= ancestor
)
45 layer
= layer
->parent();
46 return layer
== ancestor
;
49 // Convenience functions for walking a View tree.
50 const views::View
* FirstView(const views::View
* view
) {
51 const views::View
* v
= view
;
52 while (v
->has_children())
57 const views::View
* NextView(const views::View
* view
) {
58 const views::View
* v
= view
;
59 const views::View
* parent
= v
->parent();
62 int next
= parent
->GetIndexOf(v
) + 1;
63 if (next
!= parent
->child_count())
64 return FirstView(parent
->child_at(next
));
68 // Convenience functions for walking a Layer tree.
69 const ui::Layer
* FirstLayer(const ui::Layer
* layer
) {
70 const ui::Layer
* l
= layer
;
71 while (l
->children().size() > 0)
76 const ui::Layer
* NextLayer(const ui::Layer
* layer
) {
77 const ui::Layer
* parent
= layer
->parent();
80 const std::vector
<ui::Layer
*> children
= parent
->children();
82 for (index
= 0; index
< children
.size(); index
++) {
83 if (children
[index
] == layer
)
86 size_t next
= index
+ 1;
87 if (next
< children
.size())
88 return FirstLayer(children
[next
]);
92 // Given the root nodes of a View tree and a Layer tree, makes sure the two
94 bool ViewAndLayerTreeAreConsistent(const views::View
* view
,
95 const ui::Layer
* layer
) {
96 const views::View
* v
= FirstView(view
);
97 const ui::Layer
* l
= FirstLayer(layer
);
99 // Find the view with a layer.
100 while (v
&& !v
->layer())
106 // Check if the View tree and the Layer tree are in sync.
107 EXPECT_EQ(l
, v
->layer());
111 // Check if the visibility states of the View and the Layer are in sync.
112 EXPECT_EQ(l
->IsDrawn(), v
->IsDrawn());
113 if (v
->IsDrawn() != l
->IsDrawn()) {
114 for (const views::View
* vv
= v
; vv
; vv
= vv
->parent())
115 LOG(ERROR
) << "V: " << vv
<< " " << vv
->visible() << " "
116 << vv
->IsDrawn() << " " << vv
->layer();
117 for (const ui::Layer
* ll
= l
; ll
; ll
= ll
->parent())
118 LOG(ERROR
) << "L: " << ll
<< " " << ll
->IsDrawn();
122 // Check if the size of the View and the Layer are in sync.
123 EXPECT_EQ(l
->bounds(), v
->bounds());
124 if (v
->bounds() != l
->bounds())
127 if (v
== view
|| l
== layer
)
128 return v
== view
&& l
== layer
;
137 // Constructs a View tree with the specified depth.
138 void ConstructTree(views::View
* view
, int depth
) {
141 int count
= base::RandInt(1, 5);
142 for (int i
= 0; i
< count
; i
++) {
143 views::View
* v
= new views::View
;
144 view
->AddChildView(v
);
145 if (base::RandDouble() > 0.5)
146 v
->SetPaintToLayer(true);
147 if (base::RandDouble() < 0.2)
148 v
->SetVisible(false);
150 ConstructTree(v
, depth
- 1);
154 void ScrambleTree(views::View
* view
) {
155 int count
= view
->child_count();
158 for (int i
= 0; i
< count
; i
++) {
159 ScrambleTree(view
->child_at(i
));
163 int a
= base::RandInt(0, count
- 1);
164 int b
= base::RandInt(0, count
- 1);
166 views::View
* view_a
= view
->child_at(a
);
167 views::View
* view_b
= view
->child_at(b
);
168 view
->ReorderChildView(view_a
, b
);
169 view
->ReorderChildView(view_b
, a
);
172 if (!view
->layer() && base::RandDouble() < 0.1)
173 view
->SetPaintToLayer(true);
175 if (base::RandDouble() < 0.1)
176 view
->SetVisible(!view
->visible());
183 typedef ViewsTestBase ViewTest
;
185 // A derived class for testing purpose.
186 class TestView
: public View
{
190 delete_on_pressed_(false),
192 can_process_events_within_subtree_(true) {}
193 ~TestView() override
{}
195 // Reset all test state
197 did_change_bounds_
= false;
198 last_mouse_event_type_
= 0;
199 location_
.SetPoint(0, 0);
200 received_mouse_enter_
= false;
201 received_mouse_exit_
= false;
202 last_clip_
.setEmpty();
203 accelerator_count_map_
.clear();
204 can_process_events_within_subtree_
= true;
207 // Exposed as public for testing.
209 views::View::Focus();
216 bool focusable() const { return View::focusable(); }
218 void set_can_process_events_within_subtree(bool can_process
) {
219 can_process_events_within_subtree_
= can_process
;
222 bool CanProcessEventsWithinSubtree() const override
{
223 return can_process_events_within_subtree_
;
226 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
227 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
228 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
229 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
230 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
231 void OnMouseExited(const ui::MouseEvent
& event
) override
;
233 void Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) override
;
234 void SchedulePaintInRect(const gfx::Rect
& rect
) override
;
235 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
237 void OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) override
;
240 bool did_change_bounds_
;
241 gfx::Rect new_bounds_
;
244 int last_mouse_event_type_
;
245 gfx::Point location_
;
246 bool received_mouse_enter_
;
247 bool received_mouse_exit_
;
248 bool delete_on_pressed_
;
251 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
257 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
260 const ui::NativeTheme
* native_theme_
;
262 // Value to return from CanProcessEventsWithinSubtree().
263 bool can_process_events_within_subtree_
;
266 ////////////////////////////////////////////////////////////////////////////////
268 ////////////////////////////////////////////////////////////////////////////////
270 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
271 did_change_bounds_
= true;
272 new_bounds_
= bounds();
275 TEST_F(ViewTest
, OnBoundsChanged
) {
278 gfx::Rect
prev_rect(0, 0, 200, 200);
279 gfx::Rect
new_rect(100, 100, 250, 250);
281 v
.SetBoundsRect(prev_rect
);
283 v
.SetBoundsRect(new_rect
);
285 EXPECT_TRUE(v
.did_change_bounds_
);
286 EXPECT_EQ(v
.new_bounds_
, new_rect
);
287 EXPECT_EQ(v
.bounds(), new_rect
);
290 ////////////////////////////////////////////////////////////////////////////////
292 ////////////////////////////////////////////////////////////////////////////////
294 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
295 last_mouse_event_type_
= event
.type();
296 location_
.SetPoint(event
.x(), event
.y());
297 if (delete_on_pressed_
)
302 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
303 last_mouse_event_type_
= event
.type();
304 location_
.SetPoint(event
.x(), event
.y());
308 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
309 last_mouse_event_type_
= event
.type();
310 location_
.SetPoint(event
.x(), event
.y());
313 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
314 received_mouse_enter_
= true;
317 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
318 received_mouse_exit_
= true;
321 TEST_F(ViewTest
, MouseEvent
) {
322 TestView
* v1
= new TestView();
323 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
325 TestView
* v2
= new TestView();
326 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
328 scoped_ptr
<Widget
> widget(new Widget
);
329 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
330 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
331 params
.bounds
= gfx::Rect(50, 50, 650, 650);
332 widget
->Init(params
);
333 internal::RootView
* root
=
334 static_cast<internal::RootView
*>(widget
->GetRootView());
336 root
->AddChildView(v1
);
337 v1
->AddChildView(v2
);
342 gfx::Point
p1(110, 120);
343 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
344 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
345 root
->OnMousePressed(pressed
);
346 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
347 EXPECT_EQ(v2
->location_
.x(), 10);
348 EXPECT_EQ(v2
->location_
.y(), 20);
349 // Make sure v1 did not receive the event
350 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
352 // Drag event out of bounds. Should still go to v2
355 gfx::Point
p2(50, 40);
356 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
,
357 ui::EF_LEFT_MOUSE_BUTTON
, 0);
358 root
->OnMouseDragged(dragged
);
359 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
360 EXPECT_EQ(v2
->location_
.x(), -50);
361 EXPECT_EQ(v2
->location_
.y(), -60);
362 // Make sure v1 did not receive the event
363 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
365 // Releasted event out of bounds. Should still go to v2
368 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
370 root
->OnMouseReleased(released
);
371 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
372 EXPECT_EQ(v2
->location_
.x(), -100);
373 EXPECT_EQ(v2
->location_
.y(), -100);
374 // Make sure v1 did not receive the event
375 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
380 // Confirm that a view can be deleted as part of processing a mouse press.
381 TEST_F(ViewTest
, DeleteOnPressed
) {
382 TestView
* v1
= new TestView();
383 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
385 TestView
* v2
= new TestView();
386 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
391 scoped_ptr
<Widget
> widget(new Widget
);
392 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
393 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
394 params
.bounds
= gfx::Rect(50, 50, 650, 650);
395 widget
->Init(params
);
396 View
* root
= widget
->GetRootView();
398 root
->AddChildView(v1
);
399 v1
->AddChildView(v2
);
401 v2
->delete_on_pressed_
= true;
402 gfx::Point
point(110, 120);
403 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
404 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
405 root
->OnMousePressed(pressed
);
406 EXPECT_EQ(0, v1
->child_count());
411 ////////////////////////////////////////////////////////////////////////////////
413 ////////////////////////////////////////////////////////////////////////////////
415 void TestView::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
416 canvas
->sk_canvas()->getClipBounds(&last_clip_
);
419 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
420 scheduled_paint_rects_
.push_back(rect
);
421 View::SchedulePaintInRect(rect
);
424 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
425 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
426 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
427 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
428 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
431 TEST_F(ViewTest
, RemoveNotification
) {
432 ViewStorage
* vs
= ViewStorage::GetInstance();
433 Widget
* widget
= new Widget
;
434 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
435 View
* root_view
= widget
->GetRootView();
438 int s1
= vs
->CreateStorageID();
439 vs
->StoreView(s1
, v1
);
440 root_view
->AddChildView(v1
);
441 View
* v11
= new View
;
442 int s11
= vs
->CreateStorageID();
443 vs
->StoreView(s11
, v11
);
444 v1
->AddChildView(v11
);
445 View
* v111
= new View
;
446 int s111
= vs
->CreateStorageID();
447 vs
->StoreView(s111
, v111
);
448 v11
->AddChildView(v111
);
449 View
* v112
= new View
;
450 int s112
= vs
->CreateStorageID();
451 vs
->StoreView(s112
, v112
);
452 v11
->AddChildView(v112
);
453 View
* v113
= new View
;
454 int s113
= vs
->CreateStorageID();
455 vs
->StoreView(s113
, v113
);
456 v11
->AddChildView(v113
);
457 View
* v1131
= new View
;
458 int s1131
= vs
->CreateStorageID();
459 vs
->StoreView(s1131
, v1131
);
460 v113
->AddChildView(v1131
);
461 View
* v12
= new View
;
462 int s12
= vs
->CreateStorageID();
463 vs
->StoreView(s12
, v12
);
464 v1
->AddChildView(v12
);
467 int s2
= vs
->CreateStorageID();
468 vs
->StoreView(s2
, v2
);
469 root_view
->AddChildView(v2
);
470 View
* v21
= new View
;
471 int s21
= vs
->CreateStorageID();
472 vs
->StoreView(s21
, v21
);
473 v2
->AddChildView(v21
);
474 View
* v211
= new View
;
475 int s211
= vs
->CreateStorageID();
476 vs
->StoreView(s211
, v211
);
477 v21
->AddChildView(v211
);
479 size_t stored_views
= vs
->view_count();
481 // Try removing a leaf view.
482 v21
->RemoveChildView(v211
);
483 EXPECT_EQ(stored_views
- 1, vs
->view_count());
484 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
485 delete v211
; // We won't use this one anymore.
487 // Now try removing a view with a hierarchy of depth 1.
488 v11
->RemoveChildView(v113
);
489 EXPECT_EQ(stored_views
- 3, vs
->view_count());
490 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
491 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
492 delete v113
; // We won't use this one anymore.
494 // Now remove even more.
495 root_view
->RemoveChildView(v1
);
496 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
497 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
498 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
499 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
500 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
502 // Put v1 back for more tests.
503 root_view
->AddChildView(v1
);
504 vs
->StoreView(s1
, v1
);
506 // Synchronously closing the window deletes the view hierarchy, which should
507 // remove all its views from ViewStorage.
509 EXPECT_EQ(stored_views
- 10, vs
->view_count());
510 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
511 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
512 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
513 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
514 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
515 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
516 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
521 void RotateCounterclockwise(gfx::Transform
* transform
) {
522 transform
->matrix().set3x3(0, -1, 0,
527 void RotateClockwise(gfx::Transform
* transform
) {
528 transform
->matrix().set3x3( 0, 1, 0,
535 // Tests the correctness of the rect-based targeting algorithm implemented in
536 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
537 // of rect-based targeting.
538 TEST_F(ViewTest
, GetEventHandlerForRect
) {
539 Widget
* widget
= new Widget
;
540 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
541 widget
->Init(params
);
542 View
* root_view
= widget
->GetRootView();
543 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
545 // Have this hierarchy of views (the coordinates here are all in
546 // the root view's coordinate space):
547 // v1 (0, 0, 100, 100)
548 // v2 (150, 0, 250, 100)
549 // v3 (0, 200, 150, 100)
550 // v31 (10, 210, 80, 80)
551 // v32 (110, 210, 30, 80)
552 // v4 (300, 200, 100, 100)
553 // v41 (310, 210, 80, 80)
554 // v411 (370, 275, 10, 5)
555 // v5 (450, 197, 30, 36)
556 // v51 (450, 200, 30, 30)
558 // The coordinates used for SetBounds are in parent coordinates.
560 TestView
* v1
= new TestView
;
561 v1
->SetBounds(0, 0, 100, 100);
562 root_view
->AddChildView(v1
);
564 TestView
* v2
= new TestView
;
565 v2
->SetBounds(150, 0, 250, 100);
566 root_view
->AddChildView(v2
);
568 TestView
* v3
= new TestView
;
569 v3
->SetBounds(0, 200, 150, 100);
570 root_view
->AddChildView(v3
);
572 TestView
* v4
= new TestView
;
573 v4
->SetBounds(300, 200, 100, 100);
574 root_view
->AddChildView(v4
);
576 TestView
* v31
= new TestView
;
577 v31
->SetBounds(10, 10, 80, 80);
578 v3
->AddChildView(v31
);
580 TestView
* v32
= new TestView
;
581 v32
->SetBounds(110, 10, 30, 80);
582 v3
->AddChildView(v32
);
584 TestView
* v41
= new TestView
;
585 v41
->SetBounds(10, 10, 80, 80);
586 v4
->AddChildView(v41
);
588 TestView
* v411
= new TestView
;
589 v411
->SetBounds(60, 65, 10, 5);
590 v41
->AddChildView(v411
);
592 TestView
* v5
= new TestView
;
593 v5
->SetBounds(450, 197, 30, 36);
594 root_view
->AddChildView(v5
);
596 TestView
* v51
= new TestView
;
597 v51
->SetBounds(0, 3, 30, 30);
598 v5
->AddChildView(v51
);
600 // |touch_rect| does not intersect any descendant view of |root_view|.
601 gfx::Rect
touch_rect(105, 105, 30, 45);
602 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
603 EXPECT_EQ(root_view
, result_view
);
606 // Covers |v1| by at least 60%.
607 touch_rect
.SetRect(15, 15, 100, 100);
608 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
609 EXPECT_EQ(v1
, result_view
);
612 // Intersects |v1| but does not cover it by at least 60%. The center
613 // of |touch_rect| is within |v1|.
614 touch_rect
.SetRect(50, 50, 5, 10);
615 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
616 EXPECT_EQ(v1
, result_view
);
619 // Intersects |v1| but does not cover it by at least 60%. The center
620 // of |touch_rect| is not within |v1|.
621 touch_rect
.SetRect(95, 96, 21, 22);
622 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
623 EXPECT_EQ(root_view
, result_view
);
626 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
627 touch_rect
.SetRect(95, 10, 300, 120);
628 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
629 EXPECT_EQ(v2
, result_view
);
632 // Covers both |v1| and |v2| by at least 60%, but the center point
633 // of |touch_rect| is closer to the center point of |v2|.
634 touch_rect
.SetRect(20, 20, 400, 100);
635 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
636 EXPECT_EQ(v2
, result_view
);
639 // Covers both |v1| and |v2| by at least 60%, but the center point
640 // of |touch_rect| is closer to the center point of |v1|.
641 touch_rect
.SetRect(-700, -15, 1050, 110);
642 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
643 EXPECT_EQ(v1
, result_view
);
646 // A mouse click within |v1| will target |v1|.
647 touch_rect
.SetRect(15, 15, 1, 1);
648 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
649 EXPECT_EQ(v1
, result_view
);
652 // Intersects |v3| and |v31| by at least 60% and the center point
653 // of |touch_rect| is closer to the center point of |v31|.
654 touch_rect
.SetRect(0, 200, 110, 100);
655 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
656 EXPECT_EQ(v31
, result_view
);
659 // Intersects |v3| and |v31|, but neither by at least 60%. The
660 // center point of |touch_rect| lies within |v31|.
661 touch_rect
.SetRect(80, 280, 15, 15);
662 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
663 EXPECT_EQ(v31
, result_view
);
666 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
667 // center point of |touch_rect| is closest to the center point
669 touch_rect
.SetRect(0, 200, 200, 100);
670 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
671 EXPECT_EQ(v32
, result_view
);
674 // Intersects all of |v3|, |v31|, and |v32|, but only covers
675 // |v31| and |v32| by at least 60%. The center point of
676 // |touch_rect| is closest to the center point of |v32|.
677 touch_rect
.SetRect(30, 225, 180, 115);
678 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
679 EXPECT_EQ(v32
, result_view
);
682 // A mouse click at the corner of |v3| will target |v3|.
683 touch_rect
.SetRect(0, 200, 1, 1);
684 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
685 EXPECT_EQ(v3
, result_view
);
688 // A mouse click within |v32| will target |v32|.
689 touch_rect
.SetRect(112, 211, 1, 1);
690 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
691 EXPECT_EQ(v32
, result_view
);
694 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
695 // The center point of |touch_rect| is equally close to
696 // the center points of |v4| and |v41|.
697 touch_rect
.SetRect(310, 210, 80, 80);
698 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
699 EXPECT_EQ(v41
, result_view
);
702 // Intersects all of |v4|, |v41|, and |v411| but only covers
703 // |v411| by at least 60%.
704 touch_rect
.SetRect(370, 275, 7, 5);
705 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
706 EXPECT_EQ(v411
, result_view
);
709 // Intersects |v4| and |v41| but covers neither by at least 60%.
710 // The center point of |touch_rect| is equally close to the center
711 // points of |v4| and |v41|.
712 touch_rect
.SetRect(345, 245, 7, 7);
713 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
714 EXPECT_EQ(v41
, result_view
);
717 // Intersects all of |v4|, |v41|, and |v411| and covers none of
718 // them by at least 60%. The center point of |touch_rect| lies
720 touch_rect
.SetRect(368, 272, 4, 6);
721 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
722 EXPECT_EQ(v411
, result_view
);
725 // Intersects all of |v4|, |v41|, and |v411| and covers none of
726 // them by at least 60%. The center point of |touch_rect| lies
728 touch_rect
.SetRect(365, 270, 7, 7);
729 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
730 EXPECT_EQ(v41
, result_view
);
733 // Intersects all of |v4|, |v41|, and |v411| and covers none of
734 // them by at least 60%. The center point of |touch_rect| lies
736 touch_rect
.SetRect(205, 275, 200, 2);
737 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
738 EXPECT_EQ(v4
, result_view
);
741 // Intersects all of |v4|, |v41|, and |v411| but only covers
742 // |v41| by at least 60%.
743 touch_rect
.SetRect(310, 210, 61, 66);
744 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
745 EXPECT_EQ(v41
, result_view
);
748 // A mouse click within |v411| will target |v411|.
749 touch_rect
.SetRect(372, 275, 1, 1);
750 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
751 EXPECT_EQ(v411
, result_view
);
754 // A mouse click within |v41| will target |v41|.
755 touch_rect
.SetRect(350, 215, 1, 1);
756 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
757 EXPECT_EQ(v41
, result_view
);
760 // Covers |v3|, |v4|, and all of their descendants by at
761 // least 60%. The center point of |touch_rect| is closest
762 // to the center point of |v32|.
763 touch_rect
.SetRect(0, 200, 400, 100);
764 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
765 EXPECT_EQ(v32
, result_view
);
768 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
769 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
770 // The center point of |touch_rect| is closest to the center
771 // point of |root_view|.
772 touch_rect
.SetRect(110, 15, 375, 450);
773 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
774 EXPECT_EQ(root_view
, result_view
);
777 // Covers all views (except |v5| and |v51|) by at least 60%. The
778 // center point of |touch_rect| is equally close to the center
779 // points of |v2| and |v32|. One is not a descendant of the other,
780 // so in this case the view selected is arbitrary (i.e.,
781 // it depends only on the ordering of nodes in the views
783 touch_rect
.SetRect(0, 0, 400, 300);
784 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
785 EXPECT_EQ(v32
, result_view
);
788 // Covers |v5| and |v51| by at least 60%, and the center point of
789 // the touch is located within both views. Since both views share
790 // the same center point, the child view should be selected.
791 touch_rect
.SetRect(440, 190, 40, 40);
792 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
793 EXPECT_EQ(v51
, result_view
);
796 // Covers |v5| and |v51| by at least 60%, but the center point of
797 // the touch is not located within either view. Since both views
798 // share the same center point, the child view should be selected.
799 touch_rect
.SetRect(455, 187, 60, 60);
800 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
801 EXPECT_EQ(v51
, result_view
);
804 // Covers neither |v5| nor |v51| by at least 60%, but the center
805 // of the touch is located within |v51|.
806 touch_rect
.SetRect(450, 197, 10, 10);
807 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
808 EXPECT_EQ(v51
, result_view
);
811 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
812 // The center point is located outside of both views.
813 touch_rect
.SetRect(433, 180, 24, 24);
814 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
815 EXPECT_EQ(root_view
, result_view
);
818 // Only intersects |v5| but does not cover it by at least 60%. The
819 // center point of the touch region is located within |v5|.
820 touch_rect
.SetRect(449, 196, 3, 3);
821 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
822 EXPECT_EQ(v5
, result_view
);
825 // A mouse click within |v5| (but not |v51|) should target |v5|.
826 touch_rect
.SetRect(462, 199, 1, 1);
827 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
828 EXPECT_EQ(v5
, result_view
);
831 // A mouse click |v5| and |v51| should target the child view.
832 touch_rect
.SetRect(452, 226, 1, 1);
833 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
834 EXPECT_EQ(v51
, result_view
);
837 // A mouse click on the center of |v5| and |v51| should target
839 touch_rect
.SetRect(465, 215, 1, 1);
840 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
841 EXPECT_EQ(v51
, result_view
);
847 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
848 // as expected when different views in the view hierarchy return false
849 // when CanProcessEventsWithinSubtree() is called.
850 TEST_F(ViewTest
, CanProcessEventsWithinSubtree
) {
851 Widget
* widget
= new Widget
;
852 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
853 widget
->Init(params
);
854 View
* root_view
= widget
->GetRootView();
855 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
857 // Have this hierarchy of views (the coords here are in the coordinate
858 // space of the root view):
859 // v (0, 0, 100, 100)
860 // - v_child (0, 0, 20, 30)
861 // - v_grandchild (5, 5, 5, 15)
863 TestView
* v
= new TestView
;
864 v
->SetBounds(0, 0, 100, 100);
865 root_view
->AddChildView(v
);
866 v
->set_notify_enter_exit_on_child(true);
868 TestView
* v_child
= new TestView
;
869 v_child
->SetBounds(0, 0, 20, 30);
870 v
->AddChildView(v_child
);
872 TestView
* v_grandchild
= new TestView
;
873 v_grandchild
->SetBounds(5, 5, 5, 15);
874 v_child
->AddChildView(v_grandchild
);
878 v_grandchild
->Reset();
880 // Define rects and points within the views in the hierarchy.
881 gfx::Rect
rect_in_v_grandchild(7, 7, 3, 3);
882 gfx::Point
point_in_v_grandchild(rect_in_v_grandchild
.origin());
883 gfx::Rect
rect_in_v_child(12, 3, 5, 5);
884 gfx::Point
point_in_v_child(rect_in_v_child
.origin());
885 gfx::Rect
rect_in_v(50, 50, 25, 30);
886 gfx::Point
point_in_v(rect_in_v
.origin());
888 // When all three views return true when CanProcessEventsWithinSubtree()
889 // is called, targeting should behave as expected.
891 View
* result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
892 EXPECT_EQ(v_grandchild
, result_view
);
894 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
895 EXPECT_EQ(v_grandchild
, result_view
);
898 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
899 EXPECT_EQ(v_child
, result_view
);
901 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
902 EXPECT_EQ(v_child
, result_view
);
905 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
906 EXPECT_EQ(v
, result_view
);
908 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
909 EXPECT_EQ(v
, result_view
);
912 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
913 // is called, then |v_grandchild| cannot be returned as a target.
915 v_grandchild
->set_can_process_events_within_subtree(false);
917 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
918 EXPECT_EQ(v_child
, result_view
);
920 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
921 EXPECT_EQ(v_child
, result_view
);
924 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
925 EXPECT_EQ(v_child
, result_view
);
927 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
928 EXPECT_EQ(v_child
, result_view
);
931 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
932 EXPECT_EQ(v
, result_view
);
934 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
935 EXPECT_EQ(v
, result_view
);
937 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
938 // is called, then NULL should be returned as a target if we call
939 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
940 // views tree. Note that the location must be in the coordinate space
941 // of the root view (|v_grandchild| in this case), so use (1, 1).
943 result_view
= v_grandchild
;
944 result_view
= v_grandchild
->GetTooltipHandlerForPoint(gfx::Point(1, 1));
945 EXPECT_EQ(NULL
, result_view
);
948 // When |v_child| returns false when CanProcessEventsWithinSubtree()
949 // is called, then neither |v_child| nor |v_grandchild| can be returned
950 // as a target (|v| should be returned as the target for each case).
952 v_grandchild
->Reset();
953 v_child
->set_can_process_events_within_subtree(false);
955 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
956 EXPECT_EQ(v
, result_view
);
958 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
959 EXPECT_EQ(v
, result_view
);
962 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
963 EXPECT_EQ(v
, result_view
);
965 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
966 EXPECT_EQ(v
, result_view
);
969 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
970 EXPECT_EQ(v
, result_view
);
972 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
973 EXPECT_EQ(v
, result_view
);
976 // When |v| returns false when CanProcessEventsWithinSubtree()
977 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
978 // as a target (|root_view| should be returned as the target for each case).
981 v
->set_can_process_events_within_subtree(false);
983 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
984 EXPECT_EQ(root_view
, result_view
);
986 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
987 EXPECT_EQ(root_view
, result_view
);
990 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
991 EXPECT_EQ(root_view
, result_view
);
993 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
994 EXPECT_EQ(root_view
, result_view
);
997 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
998 EXPECT_EQ(root_view
, result_view
);
1000 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1001 EXPECT_EQ(root_view
, result_view
);
1004 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1005 Widget
* widget
= new Widget
;
1006 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1007 widget
->Init(params
);
1008 View
* root_view
= widget
->GetRootView();
1009 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1011 // Have this hierarchy of views (the coords here are in root coord):
1012 // v1 (0, 0, 100, 100)
1013 // - v11 (0, 0, 20, 30)
1014 // - v111 (5, 5, 5, 15)
1015 // - v12 (50, 10, 30, 90)
1016 // - v121 (60, 20, 10, 10)
1017 // v2 (105, 0, 100, 100)
1018 // - v21 (120, 10, 50, 20)
1020 TestView
* v1
= new TestView
;
1021 v1
->SetBounds(0, 0, 100, 100);
1022 root_view
->AddChildView(v1
);
1023 v1
->set_notify_enter_exit_on_child(true);
1025 TestView
* v11
= new TestView
;
1026 v11
->SetBounds(0, 0, 20, 30);
1027 v1
->AddChildView(v11
);
1029 TestView
* v111
= new TestView
;
1030 v111
->SetBounds(5, 5, 5, 15);
1031 v11
->AddChildView(v111
);
1033 TestView
* v12
= new TestView
;
1034 v12
->SetBounds(50, 10, 30, 90);
1035 v1
->AddChildView(v12
);
1037 TestView
* v121
= new TestView
;
1038 v121
->SetBounds(10, 10, 10, 10);
1039 v12
->AddChildView(v121
);
1041 TestView
* v2
= new TestView
;
1042 v2
->SetBounds(105, 0, 100, 100);
1043 root_view
->AddChildView(v2
);
1045 TestView
* v21
= new TestView
;
1046 v21
->SetBounds(15, 10, 50, 20);
1047 v2
->AddChildView(v21
);
1057 // Move the mouse in v111.
1058 gfx::Point
p1(6, 6);
1059 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, 0, 0);
1060 root_view
->OnMouseMoved(move1
);
1061 EXPECT_TRUE(v111
->received_mouse_enter_
);
1062 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1063 EXPECT_TRUE(v1
->received_mouse_enter_
);
1068 // Now, move into v121.
1069 gfx::Point
p2(65, 21);
1070 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, 0, 0);
1071 root_view
->OnMouseMoved(move2
);
1072 EXPECT_TRUE(v111
->received_mouse_exit_
);
1073 EXPECT_TRUE(v121
->received_mouse_enter_
);
1074 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1079 // Now, move into v11.
1080 gfx::Point
p3(1, 1);
1081 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, 0, 0);
1082 root_view
->OnMouseMoved(move3
);
1083 EXPECT_TRUE(v121
->received_mouse_exit_
);
1084 EXPECT_TRUE(v11
->received_mouse_enter_
);
1085 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1091 gfx::Point
p4(121, 15);
1092 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, 0, 0);
1093 root_view
->OnMouseMoved(move4
);
1094 EXPECT_TRUE(v21
->received_mouse_enter_
);
1095 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1096 EXPECT_TRUE(v11
->received_mouse_exit_
);
1097 EXPECT_TRUE(v1
->received_mouse_exit_
);
1104 gfx::Point
p5(21, 0);
1105 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, 0, 0);
1106 root_view
->OnMouseMoved(move5
);
1107 EXPECT_TRUE(v21
->received_mouse_exit_
);
1108 EXPECT_TRUE(v1
->received_mouse_enter_
);
1113 // Now, move into v11.
1114 gfx::Point
p6(15, 15);
1115 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, 0, 0);
1116 root_view
->OnMouseMoved(mouse6
);
1117 EXPECT_TRUE(v11
->received_mouse_enter_
);
1118 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1123 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1124 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1125 // when the mouse leaves v11.
1126 gfx::Point
p7(21, 0);
1127 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, 0, 0);
1128 root_view
->OnMouseMoved(mouse7
);
1129 EXPECT_TRUE(v11
->received_mouse_exit_
);
1130 EXPECT_FALSE(v1
->received_mouse_enter_
);
1135 TEST_F(ViewTest
, Textfield
) {
1136 const base::string16 kText
= ASCIIToUTF16(
1137 "Reality is that which, when you stop believing it, doesn't go away.");
1138 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1140 Widget
* widget
= new Widget
;
1141 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1142 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1143 widget
->Init(params
);
1144 View
* root_view
= widget
->GetRootView();
1146 Textfield
* textfield
= new Textfield();
1147 root_view
->AddChildView(textfield
);
1149 // Test setting, appending text.
1150 textfield
->SetText(kText
);
1151 EXPECT_EQ(kText
, textfield
->text());
1152 textfield
->AppendText(kExtraText
);
1153 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1154 textfield
->SetText(base::string16());
1155 EXPECT_TRUE(textfield
->text().empty());
1157 // Test selection related methods.
1158 textfield
->SetText(kText
);
1159 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1160 textfield
->SelectAll(false);
1161 EXPECT_EQ(kText
, textfield
->text());
1162 textfield
->ClearSelection();
1163 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1168 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1169 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1170 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1171 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1172 const base::string16 kPasswordText
=
1173 ASCIIToUTF16("Password! ** Secret stuff **");
1175 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1177 Widget
* widget
= new Widget
;
1178 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1179 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1180 widget
->Init(params
);
1181 View
* root_view
= widget
->GetRootView();
1183 Textfield
* normal
= new Textfield();
1184 Textfield
* read_only
= new Textfield();
1185 read_only
->SetReadOnly(true);
1186 Textfield
* password
= new Textfield();
1187 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1189 root_view
->AddChildView(normal
);
1190 root_view
->AddChildView(read_only
);
1191 root_view
->AddChildView(password
);
1193 normal
->SetText(kNormalText
);
1194 read_only
->SetText(kReadOnlyText
);
1195 password
->SetText(kPasswordText
);
1201 normal
->SelectAll(false);
1202 normal
->ExecuteCommand(IDS_APP_CUT
);
1203 base::string16 result
;
1204 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1205 EXPECT_EQ(kNormalText
, result
);
1206 normal
->SetText(kNormalText
); // Let's revert to the original content.
1208 read_only
->SelectAll(false);
1209 read_only
->ExecuteCommand(IDS_APP_CUT
);
1211 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1212 // Cut should have failed, so the clipboard content should not have changed.
1213 EXPECT_EQ(kNormalText
, result
);
1215 password
->SelectAll(false);
1216 password
->ExecuteCommand(IDS_APP_CUT
);
1218 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1219 // Cut should have failed, so the clipboard content should not have changed.
1220 EXPECT_EQ(kNormalText
, result
);
1226 // Start with |read_only| to observe a change in clipboard text.
1227 read_only
->SelectAll(false);
1228 read_only
->ExecuteCommand(IDS_APP_COPY
);
1230 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1231 EXPECT_EQ(kReadOnlyText
, result
);
1233 normal
->SelectAll(false);
1234 normal
->ExecuteCommand(IDS_APP_COPY
);
1236 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1237 EXPECT_EQ(kNormalText
, result
);
1239 password
->SelectAll(false);
1240 password
->ExecuteCommand(IDS_APP_COPY
);
1242 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1243 // Text cannot be copied from an obscured field; the clipboard won't change.
1244 EXPECT_EQ(kNormalText
, result
);
1250 // Attempting to paste kNormalText in a read-only text-field should fail.
1251 read_only
->SelectAll(false);
1252 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1253 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1255 password
->SelectAll(false);
1256 password
->ExecuteCommand(IDS_APP_PASTE
);
1257 EXPECT_EQ(kNormalText
, password
->text());
1259 // Copy from |read_only| to observe a change in the normal textfield text.
1260 read_only
->SelectAll(false);
1261 read_only
->ExecuteCommand(IDS_APP_COPY
);
1262 normal
->SelectAll(false);
1263 normal
->ExecuteCommand(IDS_APP_PASTE
);
1264 EXPECT_EQ(kReadOnlyText
, normal
->text());
1268 ////////////////////////////////////////////////////////////////////////////////
1270 ////////////////////////////////////////////////////////////////////////////////
1271 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1272 accelerator_count_map_
[accelerator
]++;
1276 // TODO: these tests were initially commented out when getting aura to
1277 // run. Figure out if still valuable and either nuke or fix.
1279 TEST_F(ViewTest
, ActivateAccelerator
) {
1280 // Register a keyboard accelerator before the view is added to a window.
1281 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1282 TestView
* view
= new TestView();
1284 view
->AddAccelerator(return_accelerator
);
1285 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1287 // Create a window and add the view as its child.
1288 scoped_ptr
<Widget
> widget(new Widget
);
1289 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1290 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1291 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1292 widget
->Init(params
);
1293 View
* root
= widget
->GetRootView();
1294 root
->AddChildView(view
);
1297 // Get the focus manager.
1298 FocusManager
* focus_manager
= widget
->GetFocusManager();
1299 ASSERT_TRUE(focus_manager
);
1301 // Hit the return key and see if it takes effect.
1302 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1303 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1305 // Hit the escape key. Nothing should happen.
1306 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1307 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1308 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1309 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1311 // Now register the escape key and hit it again.
1312 view
->AddAccelerator(escape_accelerator
);
1313 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1314 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1315 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1317 // Remove the return key accelerator.
1318 view
->RemoveAccelerator(return_accelerator
);
1319 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1320 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1321 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1323 // Add it again. Hit the return key and the escape key.
1324 view
->AddAccelerator(return_accelerator
);
1325 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1326 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1327 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1328 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1329 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1330 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1332 // Remove all the accelerators.
1333 view
->ResetAccelerators();
1334 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1335 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1336 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1337 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1338 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1339 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1344 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1345 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1346 TestView
* view
= new TestView();
1348 view
->AddAccelerator(return_accelerator
);
1349 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1351 scoped_ptr
<Widget
> widget(new Widget
);
1352 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1353 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1354 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1355 widget
->Init(params
);
1356 View
* root
= widget
->GetRootView();
1357 root
->AddChildView(view
);
1360 FocusManager
* focus_manager
= widget
->GetFocusManager();
1361 ASSERT_TRUE(focus_manager
);
1363 view
->SetVisible(false);
1364 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1366 view
->SetVisible(true);
1367 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1372 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1373 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1374 TestView
* view
= new TestView();
1376 view
->AddAccelerator(return_accelerator
);
1377 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1379 scoped_ptr
<Widget
> widget(new Widget
);
1380 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1381 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1382 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1383 widget
->Init(params
);
1384 View
* root
= widget
->GetRootView();
1385 root
->AddChildView(view
);
1387 FocusManager
* focus_manager
= widget
->GetFocusManager();
1388 ASSERT_TRUE(focus_manager
);
1390 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1391 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1394 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1395 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1398 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1399 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1404 ////////////////////////////////////////////////////////////////////////////////
1405 // Mouse-wheel message rerouting
1406 ////////////////////////////////////////////////////////////////////////////////
1407 class ScrollableTestView
: public View
{
1409 ScrollableTestView() { }
1411 virtual gfx::Size
GetPreferredSize() {
1412 return gfx::Size(100, 10000);
1415 virtual void Layout() {
1416 SizeToPreferredSize();
1420 class TestViewWithControls
: public View
{
1422 TestViewWithControls() {
1423 text_field_
= new Textfield();
1424 AddChildView(text_field_
);
1427 Textfield
* text_field_
;
1430 class SimpleWidgetDelegate
: public WidgetDelegate
{
1432 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1434 virtual void DeleteDelegate() { delete this; }
1436 virtual View
* GetContentsView() { return contents_
; }
1438 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1439 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1445 // Tests that the mouse-wheel messages are correctly rerouted to the window
1447 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1449 // Note that this fails for a variety of reasons:
1450 // - focused view is apparently reset across window activations and never
1451 // properly restored
1452 // - this test depends on you not having any other window visible open under the
1453 // area that it opens the test windows. --beng
1454 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1455 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1456 Widget
* window1
= Widget::CreateWindowWithBounds(
1457 new SimpleWidgetDelegate(view_with_controls
),
1458 gfx::Rect(0, 0, 100, 100));
1460 ScrollView
* scroll_view
= new ScrollView();
1461 scroll_view
->SetContents(new ScrollableTestView());
1462 Widget
* window2
= Widget::CreateWindowWithBounds(
1463 new SimpleWidgetDelegate(scroll_view
),
1464 gfx::Rect(200, 200, 100, 100));
1466 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1468 // Make the window1 active, as this is what it would be in real-world.
1469 window1
->Activate();
1471 // Let's send a mouse-wheel message to the different controls and check that
1472 // it is rerouted to the window under the mouse (effectively scrolling the
1475 // First to the Window's HWND.
1476 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1477 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1478 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1480 window1
->CloseNow();
1481 window2
->CloseNow();
1485 ////////////////////////////////////////////////////////////////////////////////
1486 // Native view hierachy
1487 ////////////////////////////////////////////////////////////////////////////////
1488 class ToplevelWidgetObserverView
: public View
{
1490 ToplevelWidgetObserverView() : toplevel_(NULL
) {
1492 ~ToplevelWidgetObserverView() override
{}
1495 void ViewHierarchyChanged(
1496 const ViewHierarchyChangedDetails
& details
) override
{
1497 if (details
.is_add
) {
1498 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1503 void NativeViewHierarchyChanged() override
{
1504 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1507 Widget
* toplevel() { return toplevel_
; }
1512 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
1515 // Test that a view can track the current top level widget by overriding
1516 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1517 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
1518 scoped_ptr
<Widget
> toplevel1(new Widget
);
1519 Widget::InitParams toplevel1_params
=
1520 CreateParams(Widget::InitParams::TYPE_POPUP
);
1521 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1522 toplevel1
->Init(toplevel1_params
);
1524 scoped_ptr
<Widget
> toplevel2(new Widget
);
1525 Widget::InitParams toplevel2_params
=
1526 CreateParams(Widget::InitParams::TYPE_POPUP
);
1527 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1528 toplevel2
->Init(toplevel2_params
);
1530 Widget
* child
= new Widget
;
1531 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
1532 child_params
.parent
= toplevel1
->GetNativeView();
1533 child
->Init(child_params
);
1535 ToplevelWidgetObserverView
* observer_view
=
1536 new ToplevelWidgetObserverView();
1537 EXPECT_EQ(NULL
, observer_view
->toplevel());
1539 child
->SetContentsView(observer_view
);
1540 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
1542 Widget::ReparentNativeView(child
->GetNativeView(),
1543 toplevel2
->GetNativeView());
1544 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
1546 observer_view
->parent()->RemoveChildView(observer_view
);
1547 EXPECT_EQ(NULL
, observer_view
->toplevel());
1549 // Make |observer_view| |child|'s contents view again so that it gets deleted
1551 child
->SetContentsView(observer_view
);
1554 ////////////////////////////////////////////////////////////////////////////////
1556 ////////////////////////////////////////////////////////////////////////////////
1558 class TransformPaintView
: public TestView
{
1560 TransformPaintView() {}
1561 ~TransformPaintView() override
{}
1563 void ClearScheduledPaintRect() {
1564 scheduled_paint_rect_
= gfx::Rect();
1567 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1569 // Overridden from View:
1570 void SchedulePaintInRect(const gfx::Rect
& rect
) override
{
1571 gfx::Rect xrect
= ConvertRectToParent(rect
);
1572 scheduled_paint_rect_
.Union(xrect
);
1576 gfx::Rect scheduled_paint_rect_
;
1578 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
1581 TEST_F(ViewTest
, TransformPaint
) {
1582 TransformPaintView
* v1
= new TransformPaintView();
1583 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1585 TestView
* v2
= new TestView();
1586 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1588 Widget
* widget
= new Widget
;
1589 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1590 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1591 widget
->Init(params
);
1593 View
* root
= widget
->GetRootView();
1595 root
->AddChildView(v1
);
1596 v1
->AddChildView(v2
);
1598 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1599 v1
->ClearScheduledPaintRect();
1600 v2
->SchedulePaint();
1602 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
1604 // Rotate |v1| counter-clockwise.
1605 gfx::Transform transform
;
1606 RotateCounterclockwise(&transform
);
1607 transform
.matrix().set(1, 3, 500.0);
1608 v1
->SetTransform(transform
);
1610 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1612 v1
->ClearScheduledPaintRect();
1613 v2
->SchedulePaint();
1615 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
1620 TEST_F(ViewTest
, TransformEvent
) {
1621 TestView
* v1
= new TestView();
1622 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1624 TestView
* v2
= new TestView();
1625 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1627 Widget
* widget
= new Widget
;
1628 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1629 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1630 widget
->Init(params
);
1631 View
* root
= widget
->GetRootView();
1633 root
->AddChildView(v1
);
1634 v1
->AddChildView(v2
);
1636 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1638 // Rotate |v1| counter-clockwise.
1639 gfx::Transform
transform(v1
->GetTransform());
1640 RotateCounterclockwise(&transform
);
1641 transform
.matrix().set(1, 3, 500.0);
1642 v1
->SetTransform(transform
);
1644 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1648 gfx::Point
p1(110, 210);
1649 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
1650 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1651 root
->OnMousePressed(pressed
);
1652 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1653 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1654 EXPECT_EQ(190, v2
->location_
.x());
1655 EXPECT_EQ(10, v2
->location_
.y());
1657 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
1659 root
->OnMouseReleased(released
);
1661 // Now rotate |v2| inside |v1| clockwise.
1662 transform
= v2
->GetTransform();
1663 RotateClockwise(&transform
);
1664 transform
.matrix().set(0, 3, 100.f
);
1665 v2
->SetTransform(transform
);
1667 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1668 // (300, 400) in |root|.
1673 gfx::Point
point2(110, 320);
1674 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
,
1675 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1676 root
->OnMousePressed(p2
);
1677 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1678 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1679 EXPECT_EQ(10, v2
->location_
.x());
1680 EXPECT_EQ(20, v2
->location_
.y());
1682 root
->OnMouseReleased(released
);
1684 v1
->SetTransform(gfx::Transform());
1685 v2
->SetTransform(gfx::Transform());
1687 TestView
* v3
= new TestView();
1688 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1689 v2
->AddChildView(v3
);
1691 // Rotate |v3| clockwise with respect to |v2|.
1692 transform
= v1
->GetTransform();
1693 RotateClockwise(&transform
);
1694 transform
.matrix().set(0, 3, 30.f
);
1695 v3
->SetTransform(transform
);
1697 // Scale |v2| with respect to |v1| along both axis.
1698 transform
= v2
->GetTransform();
1699 transform
.matrix().set(0, 0, 0.8f
);
1700 transform
.matrix().set(1, 1, 0.5f
);
1701 v2
->SetTransform(transform
);
1703 // |v3| occupies (108, 105) to (132, 115) in |root|.
1709 gfx::Point
point(112, 110);
1710 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
,
1711 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1712 root
->OnMousePressed(p3
);
1714 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1715 EXPECT_EQ(10, v3
->location_
.x());
1716 EXPECT_EQ(25, v3
->location_
.y());
1718 root
->OnMouseReleased(released
);
1720 v1
->SetTransform(gfx::Transform());
1721 v2
->SetTransform(gfx::Transform());
1722 v3
->SetTransform(gfx::Transform());
1728 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1729 transform
= v3
->GetTransform();
1730 RotateClockwise(&transform
);
1731 transform
.matrix().set(0, 3, 30.f
);
1732 // Rotation sets some scaling transformation. Using SetScale would overwrite
1733 // that and pollute the rotation. So combine the scaling with the existing
1735 gfx::Transform scale
;
1736 scale
.Scale(0.8f
, 0.5f
);
1737 transform
.ConcatTransform(scale
);
1738 v3
->SetTransform(transform
);
1740 // Translate |v2| with respect to |v1|.
1741 transform
= v2
->GetTransform();
1742 transform
.matrix().set(0, 3, 10.f
);
1743 transform
.matrix().set(1, 3, 10.f
);
1744 v2
->SetTransform(transform
);
1746 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1748 gfx::Point
point3(124, 125);
1749 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
,
1750 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1751 root
->OnMousePressed(p4
);
1753 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1754 EXPECT_EQ(10, v3
->location_
.x());
1755 EXPECT_EQ(25, v3
->location_
.y());
1757 root
->OnMouseReleased(released
);
1762 TEST_F(ViewTest
, TransformVisibleBound
) {
1763 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1765 scoped_ptr
<Widget
> widget(new Widget
);
1766 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1767 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1768 params
.bounds
= viewport_bounds
;
1769 widget
->Init(params
);
1770 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1772 View
* viewport
= new View
;
1773 widget
->SetContentsView(viewport
);
1774 View
* contents
= new View
;
1775 viewport
->AddChildView(contents
);
1776 viewport
->SetBoundsRect(viewport_bounds
);
1777 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1779 View
* child
= new View
;
1780 contents
->AddChildView(child
);
1781 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
1782 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
1784 // Rotate |child| counter-clockwise
1785 gfx::Transform transform
;
1786 RotateCounterclockwise(&transform
);
1787 transform
.matrix().set(1, 3, 50.f
);
1788 child
->SetTransform(transform
);
1789 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
1794 ////////////////////////////////////////////////////////////////////////////////
1795 // OnVisibleBoundsChanged()
1797 class VisibleBoundsView
: public View
{
1799 VisibleBoundsView() : received_notification_(false) {}
1800 ~VisibleBoundsView() override
{}
1802 bool received_notification() const { return received_notification_
; }
1803 void set_received_notification(bool received
) {
1804 received_notification_
= received
;
1808 // Overridden from View:
1809 bool GetNeedsNotificationWhenVisibleBoundsChange() const override
{
1812 void OnVisibleBoundsChanged() override
{ received_notification_
= true; }
1814 bool received_notification_
;
1816 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
1819 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
1820 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1822 scoped_ptr
<Widget
> widget(new Widget
);
1823 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1824 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1825 params
.bounds
= viewport_bounds
;
1826 widget
->Init(params
);
1827 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1829 View
* viewport
= new View
;
1830 widget
->SetContentsView(viewport
);
1831 View
* contents
= new View
;
1832 viewport
->AddChildView(contents
);
1833 viewport
->SetBoundsRect(viewport_bounds
);
1834 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1836 // Create a view that cares about visible bounds notifications, and position
1837 // it just outside the visible bounds of the viewport.
1838 VisibleBoundsView
* child
= new VisibleBoundsView
;
1839 contents
->AddChildView(child
);
1840 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
1842 // The child bound should be fully clipped.
1843 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
1845 // Now scroll the contents, but not enough to make the child visible.
1846 contents
->SetY(contents
->y() - 1);
1848 // We should have received the notification since the visible bounds may have
1849 // changed (even though they didn't).
1850 EXPECT_TRUE(child
->received_notification());
1851 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
1852 child
->set_received_notification(false);
1854 // Now scroll the contents, this time by enough to make the child visible by
1856 contents
->SetY(contents
->y() - 10);
1857 EXPECT_TRUE(child
->received_notification());
1858 EXPECT_EQ(1, child
->GetVisibleBounds().height());
1859 child
->set_received_notification(false);
1864 TEST_F(ViewTest
, SetBoundsPaint
) {
1866 TestView
* child_view
= new TestView
;
1868 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1869 top_view
.scheduled_paint_rects_
.clear();
1870 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
1871 top_view
.AddChildView(child_view
);
1873 top_view
.scheduled_paint_rects_
.clear();
1874 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
1875 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
1877 // There should be 2 rects, spanning from (10, 10) to (50, 50).
1878 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
1879 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
1880 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
1883 // Assertions around painting and focus gain/lost.
1884 TEST_F(ViewTest
, FocusBlurPaints
) {
1885 TestView parent_view
;
1886 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
1888 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1890 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
1891 parent_view
.AddChildView(child_view1
);
1893 parent_view
.scheduled_paint_rects_
.clear();
1894 child_view1
->scheduled_paint_rects_
.clear();
1896 // Focus change shouldn't trigger paints.
1897 child_view1
->DoFocus();
1899 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
1900 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
1902 child_view1
->DoBlur();
1903 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
1904 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
1907 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
1908 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
1911 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1912 view
.InvalidateLayout();
1913 view
.scheduled_paint_rects_
.clear();
1914 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1915 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
1918 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
1919 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
1920 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1922 // We have to put the View hierarchy into a Widget or no paints will be
1924 scoped_ptr
<Widget
> widget(new Widget
);
1925 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1926 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1927 params
.bounds
= viewport_bounds
;
1928 widget
->Init(params
);
1929 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1931 TestView
* parent_view
= new TestView
;
1932 widget
->SetContentsView(parent_view
);
1933 parent_view
->SetBoundsRect(viewport_bounds
);
1934 parent_view
->scheduled_paint_rects_
.clear();
1936 View
* child_view
= new View
;
1937 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
1938 parent_view
->AddChildView(child_view
);
1939 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
1940 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
1942 parent_view
->scheduled_paint_rects_
.clear();
1943 parent_view
->RemoveChildView(child_view
);
1944 scoped_ptr
<View
> child_deleter(child_view
);
1945 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
1946 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
1951 // Tests conversion methods with a transform.
1952 TEST_F(ViewTest
, ConversionsWithTransform
) {
1955 // View hierarchy used to test scale transforms.
1956 TestView
* child
= new TestView
;
1957 TestView
* child_child
= new TestView
;
1959 // View used to test a rotation transform.
1960 TestView
* child_2
= new TestView
;
1962 top_view
.AddChildView(child
);
1963 child
->AddChildView(child_child
);
1965 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
1967 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
1968 gfx::Transform transform
;
1969 transform
.Scale(3.0, 4.0);
1970 child
->SetTransform(transform
);
1972 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
1973 transform
.MakeIdentity();
1974 transform
.Scale(5.0, 7.0);
1975 child_child
->SetTransform(transform
);
1977 top_view
.AddChildView(child_2
);
1978 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
1979 transform
.MakeIdentity();
1980 RotateClockwise(&transform
);
1981 child_2
->SetTransform(transform
);
1983 // Sanity check to make sure basic transforms act as expected.
1985 gfx::Transform transform
;
1986 transform
.Translate(110.0, -110.0);
1987 transform
.Scale(100.0, 55.0);
1988 transform
.Translate(1.0, 1.0);
1990 // convert to a 3x3 matrix.
1991 const SkMatrix
& matrix
= transform
.matrix();
1993 EXPECT_EQ(210, matrix
.getTranslateX());
1994 EXPECT_EQ(-55, matrix
.getTranslateY());
1995 EXPECT_EQ(100, matrix
.getScaleX());
1996 EXPECT_EQ(55, matrix
.getScaleY());
1997 EXPECT_EQ(0, matrix
.getSkewX());
1998 EXPECT_EQ(0, matrix
.getSkewY());
2002 gfx::Transform transform
;
2003 transform
.Translate(1.0, 1.0);
2005 t2
.Scale(100.0, 55.0);
2007 t3
.Translate(110.0, -110.0);
2008 transform
.ConcatTransform(t2
);
2009 transform
.ConcatTransform(t3
);
2011 // convert to a 3x3 matrix
2012 const SkMatrix
& matrix
= transform
.matrix();
2014 EXPECT_EQ(210, matrix
.getTranslateX());
2015 EXPECT_EQ(-55, matrix
.getTranslateY());
2016 EXPECT_EQ(100, matrix
.getScaleX());
2017 EXPECT_EQ(55, matrix
.getScaleY());
2018 EXPECT_EQ(0, matrix
.getSkewX());
2019 EXPECT_EQ(0, matrix
.getSkewY());
2022 // Conversions from child->top and top->child.
2024 gfx::Point
point(5, 5);
2025 View::ConvertPointToTarget(child
, &top_view
, &point
);
2026 EXPECT_EQ(22, point
.x());
2027 EXPECT_EQ(39, point
.y());
2029 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2030 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2031 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2032 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2033 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2034 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2036 point
.SetPoint(22, 39);
2037 View::ConvertPointToTarget(&top_view
, child
, &point
);
2038 EXPECT_EQ(5, point
.x());
2039 EXPECT_EQ(5, point
.y());
2041 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2042 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2043 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2044 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2045 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2046 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2049 // Conversions from child_child->top and top->child_child.
2051 gfx::Point
point(5, 5);
2052 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2053 EXPECT_EQ(133, point
.x());
2054 EXPECT_EQ(211, point
.y());
2056 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2057 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2058 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2059 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2060 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2061 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2063 point
.SetPoint(133, 211);
2064 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2065 EXPECT_EQ(5, point
.x());
2066 EXPECT_EQ(5, point
.y());
2068 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2069 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2070 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2071 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2072 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2073 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2076 // Conversions from child_child->child and child->child_child
2078 gfx::Point
point(5, 5);
2079 View::ConvertPointToTarget(child_child
, child
, &point
);
2080 EXPECT_EQ(42, point
.x());
2081 EXPECT_EQ(48, point
.y());
2083 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2084 View::ConvertRectToTarget(child_child
, child
, &rect
);
2085 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2086 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2087 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2088 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2090 point
.SetPoint(42, 48);
2091 View::ConvertPointToTarget(child
, child_child
, &point
);
2092 EXPECT_EQ(5, point
.x());
2093 EXPECT_EQ(5, point
.y());
2095 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2096 View::ConvertRectToTarget(child
, child_child
, &rect
);
2097 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2098 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2099 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2100 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2103 // Conversions from top_view to child with a value that should be negative.
2104 // This ensures we don't round up with negative numbers.
2106 gfx::Point
point(6, 18);
2107 View::ConvertPointToTarget(&top_view
, child
, &point
);
2108 EXPECT_EQ(-1, point
.x());
2109 EXPECT_EQ(-1, point
.y());
2111 float error
= 0.01f
;
2112 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2113 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2114 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2115 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2116 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2117 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2120 // Rect conversions from top_view->child_2 and child_2->top_view.
2122 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2123 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2124 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2125 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2126 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2127 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2129 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2130 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2131 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2132 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2133 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2134 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2138 // Tests conversion methods to and from screen coordinates.
2139 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2140 scoped_ptr
<Widget
> widget(new Widget
);
2141 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2142 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2143 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2144 widget
->Init(params
);
2146 View
* child
= new View
;
2147 widget
->GetRootView()->AddChildView(child
);
2148 child
->SetBounds(10, 10, 100, 200);
2151 child
->SetTransform(t
);
2153 gfx::Point
point_in_screen(100, 90);
2154 gfx::Point
point_in_child(80, 60);
2156 gfx::Point point
= point_in_screen
;
2157 View::ConvertPointFromScreen(child
, &point
);
2158 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2160 View::ConvertPointToScreen(child
, &point
);
2161 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2164 // Tests conversion methods for rectangles.
2165 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2166 scoped_ptr
<Widget
> widget(new Widget
);
2167 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2168 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2169 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2170 widget
->Init(params
);
2171 View
* root
= widget
->GetRootView();
2173 TestView
* v1
= new TestView
;
2174 TestView
* v2
= new TestView
;
2175 root
->AddChildView(v1
);
2176 v1
->AddChildView(v2
);
2178 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2179 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2181 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2182 gfx::Rect
rect(5, 5, 15, 40);
2183 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2184 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2188 RotateCounterclockwise(&t2
);
2189 t2
.matrix().set(1, 3, 100.f
);
2190 v2
->SetTransform(t2
);
2192 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2193 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2194 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2199 v1
->SetTransform(t1
);
2201 // The rectangle should remain the same for |v1|.
2202 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2204 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2205 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2206 v2
->ConvertRectToWidget(rect
).ToString());
2211 class ObserverView
: public View
{
2214 ~ObserverView() override
;
2216 void ResetTestState();
2218 bool has_add_details() const { return has_add_details_
; }
2219 bool has_remove_details() const { return has_remove_details_
; }
2221 const ViewHierarchyChangedDetails
& add_details() const {
2222 return add_details_
;
2225 const ViewHierarchyChangedDetails
& remove_details() const {
2226 return remove_details_
;
2231 void ViewHierarchyChanged(
2232 const ViewHierarchyChangedDetails
& details
) override
;
2234 bool has_add_details_
;
2235 bool has_remove_details_
;
2236 ViewHierarchyChangedDetails add_details_
;
2237 ViewHierarchyChangedDetails remove_details_
;
2239 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2242 ObserverView::ObserverView()
2243 : has_add_details_(false),
2244 has_remove_details_(false) {
2247 ObserverView::~ObserverView() {}
2249 void ObserverView::ResetTestState() {
2250 has_add_details_
= false;
2251 has_remove_details_
= false;
2252 add_details_
= ViewHierarchyChangedDetails();
2253 remove_details_
= ViewHierarchyChangedDetails();
2256 void ObserverView::ViewHierarchyChanged(
2257 const ViewHierarchyChangedDetails
& details
) {
2258 if (details
.is_add
) {
2259 has_add_details_
= true;
2260 add_details_
= details
;
2262 has_remove_details_
= true;
2263 remove_details_
= details
;
2267 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2268 // a child view is added or removed to all the views in the hierarchy (up and
2270 // The tree looks like this:
2274 // +-- v4 (starts here, then get reparented to v1)
2275 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2278 ObserverView
* v3
= new ObserverView();
2280 // Add |v3| to |v2|.
2281 scoped_ptr
<ObserverView
> v2(new ObserverView());
2282 v2
->AddChildView(v3
);
2284 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2286 EXPECT_TRUE(v2
->has_add_details());
2287 EXPECT_FALSE(v2
->has_remove_details());
2288 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2289 EXPECT_EQ(v3
, v2
->add_details().child
);
2290 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2292 EXPECT_TRUE(v3
->has_add_details());
2293 EXPECT_FALSE(v3
->has_remove_details());
2294 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2295 EXPECT_EQ(v3
, v3
->add_details().child
);
2296 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2298 // Reset everything to the initial state.
2299 v2
->ResetTestState();
2300 v3
->ResetTestState();
2303 v1
.AddChildView(v2
.get());
2305 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2306 // Make sure all the views (v1, v2, v3) received _that_ information.
2307 EXPECT_TRUE(v1
.has_add_details());
2308 EXPECT_FALSE(v1
.has_remove_details());
2309 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2310 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2311 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2313 EXPECT_TRUE(v2
->has_add_details());
2314 EXPECT_FALSE(v2
->has_remove_details());
2315 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2316 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2317 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2319 EXPECT_TRUE(v3
->has_add_details());
2320 EXPECT_FALSE(v3
->has_remove_details());
2321 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2322 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2323 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2325 // Reset everything to the initial state.
2326 v1
.ResetTestState();
2327 v2
->ResetTestState();
2328 v3
->ResetTestState();
2330 // Remove |v2| from |v1|.
2331 v1
.RemoveChildView(v2
.get());
2333 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2334 // Make sure all the views (v1, v2, v3) received _that_ information.
2335 EXPECT_FALSE(v1
.has_add_details());
2336 EXPECT_TRUE(v1
.has_remove_details());
2337 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2338 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2339 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2341 EXPECT_FALSE(v2
->has_add_details());
2342 EXPECT_TRUE(v2
->has_remove_details());
2343 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2344 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2345 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2347 EXPECT_FALSE(v3
->has_add_details());
2348 EXPECT_TRUE(v3
->has_remove_details());
2349 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2350 EXPECT_EQ(v3
, v3
->remove_details().child
);
2351 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2353 // Verifies notifications when reparenting a view.
2354 ObserverView
* v4
= new ObserverView();
2355 // Add |v4| to |v2|.
2356 v2
->AddChildView(v4
);
2358 // Reset everything to the initial state.
2359 v1
.ResetTestState();
2360 v2
->ResetTestState();
2361 v3
->ResetTestState();
2362 v4
->ResetTestState();
2364 // Reparent |v4| to |v1|.
2365 v1
.AddChildView(v4
);
2367 // Verifies that all views receive the correct information for all the child,
2368 // parent and move views.
2370 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2371 EXPECT_TRUE(v1
.has_add_details());
2372 EXPECT_FALSE(v1
.has_remove_details());
2373 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2374 EXPECT_EQ(v4
, v1
.add_details().child
);
2375 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2377 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2379 EXPECT_FALSE(v2
->has_add_details());
2380 EXPECT_TRUE(v2
->has_remove_details());
2381 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2382 EXPECT_EQ(v4
, v2
->remove_details().child
);
2383 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2385 // |v3| is not impacted by this operation, and hence receives no notification.
2386 EXPECT_FALSE(v3
->has_add_details());
2387 EXPECT_FALSE(v3
->has_remove_details());
2389 // |v4| is the reparented child, so it receives notifications for the remove
2390 // and then the add. |v2| is its old parent, |v1| is its new parent.
2391 EXPECT_TRUE(v4
->has_remove_details());
2392 EXPECT_TRUE(v4
->has_add_details());
2393 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2394 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2395 EXPECT_EQ(v4
, v4
->add_details().child
);
2396 EXPECT_EQ(v4
, v4
->remove_details().child
);
2397 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2398 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2401 // Verifies if the child views added under the root are all deleted when calling
2402 // RemoveAllChildViews.
2403 // The tree looks like this:
2412 TEST_F(ViewTest
, RemoveAllChildViews
) {
2415 View
* child1
= new View
;
2416 root
.AddChildView(child1
);
2418 for (int i
= 0; i
< 2; ++i
)
2419 root
.AddChildView(new View
);
2421 View
* foo
= new View
;
2422 child1
->AddChildView(foo
);
2424 // Add some nodes to |foo|.
2425 for (int i
= 0; i
< 3; ++i
)
2426 foo
->AddChildView(new View
);
2428 EXPECT_EQ(3, root
.child_count());
2429 EXPECT_EQ(1, child1
->child_count());
2430 EXPECT_EQ(3, foo
->child_count());
2432 // Now remove all child views from root.
2433 root
.RemoveAllChildViews(true);
2435 EXPECT_EQ(0, root
.child_count());
2436 EXPECT_FALSE(root
.has_children());
2439 TEST_F(ViewTest
, Contains
) {
2441 View
* v2
= new View
;
2442 View
* v3
= new View
;
2444 v1
.AddChildView(v2
);
2445 v2
->AddChildView(v3
);
2447 EXPECT_FALSE(v1
.Contains(NULL
));
2448 EXPECT_TRUE(v1
.Contains(&v1
));
2449 EXPECT_TRUE(v1
.Contains(v2
));
2450 EXPECT_TRUE(v1
.Contains(v3
));
2452 EXPECT_FALSE(v2
->Contains(NULL
));
2453 EXPECT_TRUE(v2
->Contains(v2
));
2454 EXPECT_FALSE(v2
->Contains(&v1
));
2455 EXPECT_TRUE(v2
->Contains(v3
));
2457 EXPECT_FALSE(v3
->Contains(NULL
));
2458 EXPECT_TRUE(v3
->Contains(v3
));
2459 EXPECT_FALSE(v3
->Contains(&v1
));
2460 EXPECT_FALSE(v3
->Contains(v2
));
2463 // Verifies if GetIndexOf() returns the correct index for the specified child
2465 // The tree looks like this:
2470 TEST_F(ViewTest
, GetIndexOf
) {
2473 View
* child1
= new View
;
2474 root
.AddChildView(child1
);
2476 View
* child2
= new View
;
2477 root
.AddChildView(child2
);
2479 View
* foo1
= new View
;
2480 child1
->AddChildView(foo1
);
2482 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2483 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2484 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2485 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2486 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2488 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2489 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2490 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2491 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2492 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2494 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2495 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2496 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2497 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2498 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2501 // Verifies that the child views can be reordered correctly.
2502 TEST_F(ViewTest
, ReorderChildren
) {
2505 View
* child
= new View();
2506 root
.AddChildView(child
);
2508 View
* foo1
= new View();
2509 child
->AddChildView(foo1
);
2510 View
* foo2
= new View();
2511 child
->AddChildView(foo2
);
2512 View
* foo3
= new View();
2513 child
->AddChildView(foo3
);
2514 foo1
->SetFocusable(true);
2515 foo2
->SetFocusable(true);
2516 foo3
->SetFocusable(true);
2518 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2519 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2520 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2521 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2522 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2523 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2525 // Move |foo2| at the end.
2526 child
->ReorderChildView(foo2
, -1);
2527 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2528 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2529 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2530 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2531 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2532 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2534 // Move |foo1| at the end.
2535 child
->ReorderChildView(foo1
, -1);
2536 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2537 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2538 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2539 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2540 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2541 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2542 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2544 // Move |foo2| to the front.
2545 child
->ReorderChildView(foo2
, 0);
2546 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2547 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2548 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2549 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2550 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2551 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2552 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2555 // Verifies that GetViewByID returns the correctly child view from the specified
2557 // The tree looks like this:
2562 TEST_F(ViewTest
, GetViewByID
) {
2564 const int kV1ID
= 1;
2568 const int kV2ID
= 2;
2572 const int kV3ID
= 3;
2576 const int kV4ID
= 4;
2579 const int kV5ID
= 5;
2581 v1
.AddChildView(&v2
);
2582 v2
.AddChildView(&v3
);
2583 v2
.AddChildView(&v4
);
2585 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2586 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2587 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2589 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2590 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2592 const int kGroup
= 1;
2593 v3
.SetGroup(kGroup
);
2594 v4
.SetGroup(kGroup
);
2597 v1
.GetViewsInGroup(kGroup
, &views
);
2598 EXPECT_EQ(2U, views
.size());
2600 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2601 EXPECT_NE(views
.end(), i
);
2603 i
= std::find(views
.begin(), views
.end(), &v4
);
2604 EXPECT_NE(views
.end(), i
);
2607 TEST_F(ViewTest
, AddExistingChild
) {
2610 v1
.AddChildView(&v2
);
2611 v1
.AddChildView(&v3
);
2612 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2613 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2615 // Check that there's no change in order when adding at same index.
2616 v1
.AddChildViewAt(&v2
, 0);
2617 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2618 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2619 v1
.AddChildViewAt(&v3
, 1);
2620 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2621 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2623 // Add it at a different index and check for change in order.
2624 v1
.AddChildViewAt(&v2
, 1);
2625 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
2626 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
2627 v1
.AddChildViewAt(&v2
, 0);
2628 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2629 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2631 // Check that calling |AddChildView()| does not change the order.
2632 v1
.AddChildView(&v2
);
2633 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2634 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2635 v1
.AddChildView(&v3
);
2636 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2637 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2640 ////////////////////////////////////////////////////////////////////////////////
2642 ////////////////////////////////////////////////////////////////////////////////
2644 // A widget that always claims to be active, regardless of its real activation
2646 class ActiveWidget
: public Widget
{
2649 ~ActiveWidget() override
{}
2651 bool IsActive() const override
{ return true; }
2654 DISALLOW_COPY_AND_ASSIGN(ActiveWidget
);
2657 TEST_F(ViewTest
, AdvanceFocusIfNecessaryForUnfocusableView
) {
2658 // Create a widget with two views and give the first one focus.
2659 ActiveWidget widget
;
2660 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2661 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2662 widget
.Init(params
);
2664 View
* view1
= new View();
2665 view1
->SetFocusable(true);
2666 widget
.GetRootView()->AddChildView(view1
);
2667 View
* view2
= new View();
2668 view2
->SetFocusable(true);
2669 widget
.GetRootView()->AddChildView(view2
);
2671 FocusManager
* focus_manager
= widget
.GetFocusManager();
2672 ASSERT_TRUE(focus_manager
);
2674 focus_manager
->SetFocusedView(view1
);
2675 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
2677 // Disable the focused view and check if the next view gets focused.
2678 view1
->SetEnabled(false);
2679 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
2681 // Re-enable and re-focus.
2682 view1
->SetEnabled(true);
2683 focus_manager
->SetFocusedView(view1
);
2684 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
2686 // Hide the focused view and check it the next view gets focused.
2687 view1
->SetVisible(false);
2688 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
2690 // Re-show and re-focus.
2691 view1
->SetVisible(true);
2692 focus_manager
->SetFocusedView(view1
);
2693 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
2695 // Set the focused view as not focusable and check if the next view gets
2697 view1
->SetFocusable(false);
2698 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
2701 ////////////////////////////////////////////////////////////////////////////////
2703 ////////////////////////////////////////////////////////////////////////////////
2707 // Test implementation of LayerAnimator.
2708 class TestLayerAnimator
: public ui::LayerAnimator
{
2710 TestLayerAnimator();
2712 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
2715 void SetBounds(const gfx::Rect
& bounds
) override
;
2718 ~TestLayerAnimator() override
{}
2721 gfx::Rect last_bounds_
;
2723 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
2726 TestLayerAnimator::TestLayerAnimator()
2727 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2730 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
2731 last_bounds_
= bounds
;
2736 class ViewLayerTest
: public ViewsTestBase
{
2738 ViewLayerTest() : widget_(NULL
) {}
2740 ~ViewLayerTest() override
{}
2742 // Returns the Layer used by the RootView.
2743 ui::Layer
* GetRootLayer() {
2744 return widget()->GetLayer();
2747 void SetUp() override
{
2749 widget_
= new Widget
;
2750 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2751 params
.bounds
= gfx::Rect(50, 50, 200, 200);
2752 widget_
->Init(params
);
2754 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
2757 void TearDown() override
{
2758 widget_
->CloseNow();
2759 ViewsTestBase::TearDown();
2762 Widget
* widget() { return widget_
; }
2769 TEST_F(ViewLayerTest
, LayerToggling
) {
2770 // Because we lazily create textures the calls to DrawTree are necessary to
2771 // ensure we trigger creation of textures.
2772 ui::Layer
* root_layer
= widget()->GetLayer();
2773 View
* content_view
= new View
;
2774 widget()->SetContentsView(content_view
);
2776 // Create v1, give it a bounds and verify everything is set up correctly.
2777 View
* v1
= new View
;
2778 v1
->SetPaintToLayer(true);
2779 EXPECT_TRUE(v1
->layer() != NULL
);
2780 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2781 content_view
->AddChildView(v1
);
2782 ASSERT_TRUE(v1
->layer() != NULL
);
2783 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2784 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
2786 // Create v2 as a child of v1 and do basic assertion testing.
2787 View
* v2
= new View
;
2788 v1
->AddChildView(v2
);
2789 EXPECT_TRUE(v2
->layer() == NULL
);
2790 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
2791 v2
->SetPaintToLayer(true);
2792 ASSERT_TRUE(v2
->layer() != NULL
);
2793 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2794 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2796 // Turn off v1s layer. v2 should still have a layer but its parent should have
2798 v1
->SetPaintToLayer(false);
2799 EXPECT_TRUE(v1
->layer() == NULL
);
2800 EXPECT_TRUE(v2
->layer() != NULL
);
2801 EXPECT_EQ(root_layer
, v2
->layer()->parent());
2802 ASSERT_EQ(1u, root_layer
->children().size());
2803 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
2804 // The bounds of the layer should have changed to be relative to the root view
2806 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
2808 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2809 gfx::Transform transform
;
2810 transform
.Scale(2.0, 2.0);
2811 v1
->SetTransform(transform
);
2812 EXPECT_TRUE(v1
->layer() != NULL
);
2813 EXPECT_TRUE(v2
->layer() != NULL
);
2814 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2815 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2816 ASSERT_EQ(1u, root_layer
->children().size());
2817 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
2818 ASSERT_EQ(1u, v1
->layer()->children().size());
2819 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
2820 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2823 // Verifies turning on a layer wires up children correctly.
2824 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
2825 View
* content_view
= new View
;
2826 widget()->SetContentsView(content_view
);
2828 // Create v1, give it a bounds and verify everything is set up correctly.
2829 View
* v1
= new View
;
2830 content_view
->AddChildView(v1
);
2831 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2833 View
* v2
= new View
;
2834 v1
->AddChildView(v2
);
2836 View
* v3
= new View
;
2837 v3
->SetPaintToLayer(true);
2838 v2
->AddChildView(v3
);
2839 ASSERT_TRUE(v3
->layer() != NULL
);
2841 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2843 v1
->SetPaintToLayer(true);
2844 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
2847 TEST_F(ViewLayerTest
, LayerAnimator
) {
2848 View
* content_view
= new View
;
2849 widget()->SetContentsView(content_view
);
2851 View
* v1
= new View
;
2852 content_view
->AddChildView(v1
);
2853 v1
->SetPaintToLayer(true);
2854 EXPECT_TRUE(v1
->layer() != NULL
);
2856 TestLayerAnimator
* animator
= new TestLayerAnimator();
2857 v1
->layer()->SetAnimator(animator
);
2859 gfx::Rect
bounds(1, 2, 3, 4);
2860 v1
->SetBoundsRect(bounds
);
2861 EXPECT_EQ(bounds
, animator
->last_bounds());
2862 // TestLayerAnimator doesn't update the layer.
2863 EXPECT_NE(bounds
, v1
->layer()->bounds());
2866 // Verifies the bounds of a layer are updated if the bounds of ancestor that
2867 // doesn't have a layer change.
2868 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
2869 View
* content_view
= new View
;
2870 widget()->SetContentsView(content_view
);
2872 View
* v1
= new View
;
2873 content_view
->AddChildView(v1
);
2874 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2876 View
* v2
= new View
;
2877 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
2878 v1
->AddChildView(v2
);
2879 v2
->SetPaintToLayer(true);
2880 ASSERT_TRUE(v2
->layer() != NULL
);
2881 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
2883 v1
->SetPosition(gfx::Point(25, 36));
2884 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
2886 v2
->SetPosition(gfx::Point(11, 12));
2887 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
2889 // Bounds of the layer should change even if the view is not invisible.
2890 v1
->SetVisible(false);
2891 v1
->SetPosition(gfx::Point(20, 30));
2892 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
2894 v2
->SetVisible(false);
2895 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
2896 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
2899 // Make sure layers are positioned correctly in RTL.
2900 TEST_F(ViewLayerTest
, BoundInRTL
) {
2901 std::string locale
= l10n_util::GetApplicationLocale(std::string());
2902 base::i18n::SetICUDefaultLocale("he");
2904 View
* view
= new View
;
2905 widget()->SetContentsView(view
);
2907 int content_width
= view
->width();
2909 // |v1| is initially not attached to anything. So its layer will have the same
2910 // bounds as the view.
2911 View
* v1
= new View
;
2912 v1
->SetPaintToLayer(true);
2913 v1
->SetBounds(10, 10, 20, 10);
2914 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
2915 v1
->layer()->bounds());
2917 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
2919 view
->AddChildView(v1
);
2920 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
2921 v1
->layer()->bounds());
2922 gfx::Rect l1bounds
= v1
->layer()->bounds();
2924 // Now attach a View to the widget first, then create a layer for it. Make
2925 // sure the bounds are correct.
2926 View
* v2
= new View
;
2927 v2
->SetBounds(50, 10, 30, 10);
2928 EXPECT_FALSE(v2
->layer());
2929 view
->AddChildView(v2
);
2930 v2
->SetPaintToLayer(true);
2931 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
2932 v2
->layer()->bounds());
2933 gfx::Rect l2bounds
= v2
->layer()->bounds();
2935 view
->SetPaintToLayer(true);
2936 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
2937 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
2939 // Move one of the views. Make sure the layer is positioned correctly
2941 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
2942 l1bounds
.set_x(l1bounds
.x() + 5);
2943 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
2945 view
->SetPaintToLayer(false);
2946 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
2947 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
2949 // Move a view again.
2950 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
2951 l2bounds
.set_x(l2bounds
.x() - 5);
2952 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
2955 base::i18n::SetICUDefaultLocale(locale
);
2958 // Makes sure a transform persists after toggling the visibility.
2959 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
2960 View
* view
= new View
;
2961 gfx::Transform transform
;
2962 transform
.Scale(2.0, 2.0);
2963 view
->SetTransform(transform
);
2964 widget()->SetContentsView(view
);
2965 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2967 view
->SetVisible(false);
2968 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2970 view
->SetVisible(true);
2971 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2974 // Verifies a transform persists after removing/adding a view with a transform.
2975 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
2976 View
* view
= new View
;
2977 gfx::Transform transform
;
2978 transform
.Scale(2.0, 2.0);
2979 view
->SetTransform(transform
);
2980 widget()->SetContentsView(view
);
2981 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2982 ASSERT_TRUE(view
->layer() != NULL
);
2983 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
2985 View
* parent
= view
->parent();
2986 parent
->RemoveChildView(view
);
2987 parent
->AddChildView(view
);
2989 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2990 ASSERT_TRUE(view
->layer() != NULL
);
2991 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
2994 // Makes sure that layer visibility is correct after toggling View visibility.
2995 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
2996 View
* content_view
= new View
;
2997 widget()->SetContentsView(content_view
);
2999 // The view isn't attached to a widget or a parent view yet. But it should
3000 // still have a layer, but the layer should not be attached to the root
3002 View
* v1
= new View
;
3003 v1
->SetPaintToLayer(true);
3004 EXPECT_TRUE(v1
->layer());
3005 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3008 // Once the view is attached to a widget, its layer should be attached to the
3009 // root layer and visible.
3010 content_view
->AddChildView(v1
);
3011 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3013 EXPECT_TRUE(v1
->layer()->IsDrawn());
3015 v1
->SetVisible(false);
3016 EXPECT_FALSE(v1
->layer()->IsDrawn());
3018 v1
->SetVisible(true);
3019 EXPECT_TRUE(v1
->layer()->IsDrawn());
3022 EXPECT_FALSE(v1
->layer()->IsDrawn());
3025 EXPECT_TRUE(v1
->layer()->IsDrawn());
3028 // Tests that the layers in the subtree are orphaned after a View is removed
3030 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3031 View
* content_view
= new View
;
3032 widget()->SetContentsView(content_view
);
3034 View
* v1
= new View
;
3035 content_view
->AddChildView(v1
);
3037 View
* v2
= new View
;
3038 v1
->AddChildView(v2
);
3039 v2
->SetPaintToLayer(true);
3040 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3042 EXPECT_TRUE(v2
->layer()->IsDrawn());
3044 content_view
->RemoveChildView(v1
);
3046 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3050 content_view
->AddChildView(v2
);
3053 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3055 EXPECT_TRUE(v2
->layer()->IsDrawn());
3058 class PaintTrackingView
: public View
{
3060 PaintTrackingView() : painted_(false) {
3063 bool painted() const { return painted_
; }
3064 void set_painted(bool value
) { painted_
= value
; }
3066 void OnPaint(gfx::Canvas
* canvas
) override
{ painted_
= true; }
3071 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3074 // Makes sure child views with layers aren't painted when paint starts at an
3076 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3077 PaintTrackingView
* content_view
= new PaintTrackingView
;
3078 widget()->SetContentsView(content_view
);
3079 content_view
->SetPaintToLayer(true);
3080 GetRootLayer()->GetCompositor()->ScheduleDraw();
3081 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3082 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3083 content_view
->set_painted(false);
3084 // content_view no longer has a dirty rect. Paint from the root and make sure
3085 // PaintTrackingView isn't painted.
3086 GetRootLayer()->GetCompositor()->ScheduleDraw();
3087 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3088 EXPECT_FALSE(content_view
->painted());
3090 // Make content_view have a dirty rect, paint the layers and make sure
3091 // PaintTrackingView is painted.
3092 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3093 GetRootLayer()->GetCompositor()->ScheduleDraw();
3094 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3095 EXPECT_TRUE(content_view
->painted());
3098 // Tests that the visibility of child layers are updated correctly when a View's
3099 // visibility changes.
3100 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3101 View
* v1
= new View
;
3102 v1
->SetPaintToLayer(true);
3103 widget()->SetContentsView(v1
);
3105 View
* v2
= new View
;
3106 v1
->AddChildView(v2
);
3108 View
* v3
= new View
;
3109 v2
->AddChildView(v3
);
3110 v3
->SetVisible(false);
3112 View
* v4
= new View
;
3113 v4
->SetPaintToLayer(true);
3114 v3
->AddChildView(v4
);
3116 EXPECT_TRUE(v1
->layer()->IsDrawn());
3117 EXPECT_FALSE(v4
->layer()->IsDrawn());
3119 v2
->SetVisible(false);
3120 EXPECT_TRUE(v1
->layer()->IsDrawn());
3121 EXPECT_FALSE(v4
->layer()->IsDrawn());
3123 v2
->SetVisible(true);
3124 EXPECT_TRUE(v1
->layer()->IsDrawn());
3125 EXPECT_FALSE(v4
->layer()->IsDrawn());
3127 v2
->SetVisible(false);
3128 EXPECT_TRUE(v1
->layer()->IsDrawn());
3129 EXPECT_FALSE(v4
->layer()->IsDrawn());
3130 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3132 v3
->SetVisible(true);
3133 EXPECT_TRUE(v1
->layer()->IsDrawn());
3134 EXPECT_FALSE(v4
->layer()->IsDrawn());
3135 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3137 // Reparent |v3| to |v1|.
3138 v1
->AddChildView(v3
);
3139 EXPECT_TRUE(v1
->layer()->IsDrawn());
3140 EXPECT_TRUE(v4
->layer()->IsDrawn());
3141 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3144 // This test creates a random View tree, and then randomly reorders child views,
3145 // reparents views etc. Unrelated changes can appear to break this test. So
3146 // marking this as FLAKY.
3147 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3148 View
* content
= new View
;
3149 content
->SetPaintToLayer(true);
3150 widget()->SetContentsView(content
);
3153 ConstructTree(content
, 5);
3154 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3156 ScrambleTree(content
);
3157 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3159 ScrambleTree(content
);
3160 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3162 ScrambleTree(content
);
3163 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3166 // Verifies when views are reordered the layer is also reordered. The widget is
3167 // providing the parent layer.
3168 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3169 View
* content
= new View
;
3170 widget()->SetContentsView(content
);
3171 View
* c1
= new View
;
3172 c1
->SetPaintToLayer(true);
3173 content
->AddChildView(c1
);
3174 View
* c2
= new View
;
3175 c2
->SetPaintToLayer(true);
3176 content
->AddChildView(c2
);
3178 ui::Layer
* parent_layer
= c1
->layer()->parent();
3179 ASSERT_TRUE(parent_layer
);
3180 ASSERT_EQ(2u, parent_layer
->children().size());
3181 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3182 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3184 // Move c1 to the front. The layers should have moved too.
3185 content
->ReorderChildView(c1
, -1);
3186 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3187 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3190 // Verifies that the layer of a view can be acquired properly.
3191 TEST_F(ViewLayerTest
, AcquireLayer
) {
3192 View
* content
= new View
;
3193 widget()->SetContentsView(content
);
3194 scoped_ptr
<View
> c1(new View
);
3195 c1
->SetPaintToLayer(true);
3196 EXPECT_TRUE(c1
->layer());
3197 content
->AddChildView(c1
.get());
3199 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3200 EXPECT_EQ(layer
.get(), c1
->layer());
3202 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3203 EXPECT_NE(c1
->layer(), layer2
.get());
3205 // Destroy view before destroying layer.
3209 // Verify the z-order of the layers as a result of calling RecreateLayer().
3210 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3211 scoped_ptr
<View
> v(new View());
3212 v
->SetPaintToLayer(true);
3214 View
* v1
= new View();
3215 v1
->SetPaintToLayer(true);
3216 v
->AddChildView(v1
);
3217 View
* v2
= new View();
3218 v2
->SetPaintToLayer(true);
3219 v
->AddChildView(v2
);
3221 // Test the initial z-order.
3222 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3223 ASSERT_EQ(2u, child_layers_pre
.size());
3224 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3225 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3227 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3229 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3230 // for |v1| and |v2|.
3231 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3232 ASSERT_EQ(3u, child_layers_post
.size());
3233 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3234 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3235 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3238 // Verify the z-order of the layers as a result of calling RecreateLayer when
3239 // the widget is the parent with the layer.
3240 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3241 View
* v
= new View();
3242 widget()->SetContentsView(v
);
3244 View
* v1
= new View();
3245 v1
->SetPaintToLayer(true);
3246 v
->AddChildView(v1
);
3247 View
* v2
= new View();
3248 v2
->SetPaintToLayer(true);
3249 v
->AddChildView(v2
);
3251 ui::Layer
* root_layer
= GetRootLayer();
3253 // Test the initial z-order.
3254 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3255 ASSERT_EQ(2u, child_layers_pre
.size());
3256 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3257 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3259 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3261 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3262 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3263 ASSERT_EQ(3u, child_layers_post
.size());
3264 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3265 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3266 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3269 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3271 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3273 v
.SetPaintToLayer(true);
3275 child
.SetPaintToLayer(true);
3276 v
.AddChildView(&child
);
3277 ASSERT_TRUE(v
.layer() != NULL
);
3278 ASSERT_EQ(1u, v
.layer()->children().size());
3279 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3281 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3282 v
.layer()->Add(&layer
);
3283 v
.layer()->StackAtBottom(&layer
);
3285 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3287 // All children should be moved from old layer to new layer.
3288 ASSERT_TRUE(old_layer
.get() != NULL
);
3289 EXPECT_TRUE(old_layer
->children().empty());
3291 // And new layer should have the two children.
3292 ASSERT_TRUE(v
.layer() != NULL
);
3293 ASSERT_EQ(2u, v
.layer()->children().size());
3294 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3295 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3298 class BoundsTreeTestView
: public View
{
3300 BoundsTreeTestView() {}
3302 void PaintChildren(gfx::Canvas
* canvas
, const CullSet
& cull_set
) override
{
3303 // Save out a copy of the cull_set before calling the base implementation.
3304 last_cull_set_
.clear();
3305 if (cull_set
.cull_set_
) {
3306 for (base::hash_set
<intptr_t>::iterator it
= cull_set
.cull_set_
->begin();
3307 it
!= cull_set
.cull_set_
->end();
3309 last_cull_set_
.insert(reinterpret_cast<View
*>(*it
));
3312 View::PaintChildren(canvas
, cull_set
);
3315 std::set
<View
*> last_cull_set_
;
3318 TEST_F(ViewLayerTest
, BoundsTreePaintUpdatesCullSet
) {
3319 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3320 widget()->SetContentsView(test_view
);
3322 View
* v1
= new View();
3323 v1
->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3324 test_view
->AddChildView(v1
);
3326 View
* v2
= new View();
3327 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3328 v1
->AddChildView(v2
);
3330 // Schedule a full-view paint to get everyone's rectangles updated.
3331 test_view
->SchedulePaintInRect(test_view
->bounds());
3332 GetRootLayer()->GetCompositor()->ScheduleDraw();
3333 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3335 // Now we have test_view - v1 - v2. Damage to only test_view should only
3336 // return root_view and test_view.
3337 test_view
->SchedulePaintInRect(gfx::Rect(0, 0, 1, 1));
3338 GetRootLayer()->GetCompositor()->ScheduleDraw();
3339 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3340 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3341 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3342 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3344 // Damage to v1 only should only return root_view, test_view, and v1.
3345 test_view
->SchedulePaintInRect(gfx::Rect(11, 16, 1, 1));
3346 GetRootLayer()->GetCompositor()->ScheduleDraw();
3347 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3348 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3349 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3350 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3351 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3353 // A Damage rect inside v2 should get all 3 views back in the |last_cull_set_|
3354 // on call to TestView::Paint(), along with the widget root view.
3355 test_view
->SchedulePaintInRect(gfx::Rect(31, 49, 1, 1));
3356 GetRootLayer()->GetCompositor()->ScheduleDraw();
3357 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3358 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3359 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3360 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3361 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3362 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3365 TEST_F(ViewLayerTest
, BoundsTreeWithRTL
) {
3366 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3367 base::i18n::SetICUDefaultLocale("ar");
3369 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3370 widget()->SetContentsView(test_view
);
3372 // Add child views, which should be in RTL coordinate space of parent view.
3373 View
* v1
= new View
;
3374 v1
->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3375 test_view
->AddChildView(v1
);
3377 View
* v2
= new View
;
3378 v2
->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3379 v1
->AddChildView(v2
);
3381 // Schedule a full-view paint to get everyone's rectangles updated.
3382 test_view
->SchedulePaintInRect(test_view
->bounds());
3383 GetRootLayer()->GetCompositor()->ScheduleDraw();
3384 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3386 // Damage to the right side of the parent view should touch both child views.
3387 gfx::Rect
rtl_damage(test_view
->bounds().width() - 16, 18, 1, 1);
3388 test_view
->SchedulePaintInRect(rtl_damage
);
3389 GetRootLayer()->GetCompositor()->ScheduleDraw();
3390 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3391 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3392 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3393 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3394 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3395 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3397 // Damage to the left side of the parent view should only touch the
3399 gfx::Rect
ltr_damage(16, 18, 1, 1);
3400 test_view
->SchedulePaintInRect(ltr_damage
);
3401 GetRootLayer()->GetCompositor()->ScheduleDraw();
3402 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3403 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3404 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3405 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3408 base::i18n::SetICUDefaultLocale(locale
);
3411 TEST_F(ViewLayerTest
, BoundsTreeSetBoundsChangesCullSet
) {
3412 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3413 widget()->SetContentsView(test_view
);
3415 View
* v1
= new View
;
3416 v1
->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3417 test_view
->AddChildView(v1
);
3419 View
* v2
= new View
;
3420 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3421 v1
->AddChildView(v2
);
3423 // Schedule a full-view paint to get everyone's rectangles updated.
3424 test_view
->SchedulePaintInRect(test_view
->bounds());
3425 GetRootLayer()->GetCompositor()->ScheduleDraw();
3426 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3428 // Move v1 to a new origin out of the way of our next query.
3429 v1
->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3430 // The move will force a repaint.
3431 GetRootLayer()->GetCompositor()->ScheduleDraw();
3432 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3434 // Schedule a paint with damage rect where v1 used to be.
3435 test_view
->SchedulePaintInRect(gfx::Rect(5, 6, 10, 11));
3436 GetRootLayer()->GetCompositor()->ScheduleDraw();
3437 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3439 // Should only have picked up root_view and test_view.
3440 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3441 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3442 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3445 TEST_F(ViewLayerTest
, BoundsTreeLayerChangeMakesNewTree
) {
3446 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3447 widget()->SetContentsView(test_view
);
3449 View
* v1
= new View
;
3450 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3451 test_view
->AddChildView(v1
);
3453 View
* v2
= new View
;
3454 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3455 v1
->AddChildView(v2
);
3457 // Schedule a full-view paint to get everyone's rectangles updated.
3458 test_view
->SchedulePaintInRect(test_view
->bounds());
3459 GetRootLayer()->GetCompositor()->ScheduleDraw();
3460 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3462 // Set v1 to paint to its own layer, it should remove itself from the
3463 // test_view heiarchy and no longer intersect with damage rects in that cull
3465 v1
->SetPaintToLayer(true);
3467 // Schedule another full-view paint.
3468 test_view
->SchedulePaintInRect(test_view
->bounds());
3469 GetRootLayer()->GetCompositor()->ScheduleDraw();
3470 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3471 // v1 and v2 should no longer be present in the test_view cull_set.
3472 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3473 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3474 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3476 // Now set v1 back to not painting to a layer.
3477 v1
->SetPaintToLayer(false);
3478 // Schedule another full-view paint.
3479 test_view
->SchedulePaintInRect(test_view
->bounds());
3480 GetRootLayer()->GetCompositor()->ScheduleDraw();
3481 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3482 // We should be back to the full cull set including v1 and v2.
3483 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3484 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3485 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3486 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3487 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3490 TEST_F(ViewLayerTest
, BoundsTreeRemoveChildRemovesBounds
) {
3491 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3492 widget()->SetContentsView(test_view
);
3494 View
* v1
= new View
;
3495 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3496 test_view
->AddChildView(v1
);
3498 View
* v2
= new View
;
3499 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3500 v1
->AddChildView(v2
);
3502 // Schedule a full-view paint to get everyone's rectangles updated.
3503 test_view
->SchedulePaintInRect(test_view
->bounds());
3504 GetRootLayer()->GetCompositor()->ScheduleDraw();
3505 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3507 // Now remove v1 from the root view.
3508 test_view
->RemoveChildView(v1
);
3510 // Schedule another full-view paint.
3511 test_view
->SchedulePaintInRect(test_view
->bounds());
3512 GetRootLayer()->GetCompositor()->ScheduleDraw();
3513 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3514 // v1 and v2 should no longer be present in the test_view cull_set.
3515 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3516 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3517 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3519 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3520 // deleted with that hierarchy.
3524 TEST_F(ViewLayerTest
, BoundsTreeMoveViewMovesBounds
) {
3525 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3526 widget()->SetContentsView(test_view
);
3528 // Build hierarchy v1 - v2 - v3.
3529 View
* v1
= new View
;
3530 v1
->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3531 test_view
->AddChildView(v1
);
3533 View
* v2
= new View
;
3534 v2
->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3535 v1
->AddChildView(v2
);
3537 View
* v3
= new View
;
3538 v3
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3539 v2
->AddChildView(v3
);
3541 // Schedule a full-view paint and ensure all views are present in the cull.
3542 test_view
->SchedulePaintInRect(test_view
->bounds());
3543 GetRootLayer()->GetCompositor()->ScheduleDraw();
3544 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3545 EXPECT_EQ(5U, test_view
->last_cull_set_
.size());
3546 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3547 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3548 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3549 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3550 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v3
));
3552 // Build an unrelated view hierarchy and move v2 in to it.
3553 scoped_ptr
<Widget
> test_widget(new Widget
);
3554 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3555 params
.bounds
= gfx::Rect(10, 10, 500, 500);
3556 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3557 test_widget
->Init(params
);
3558 test_widget
->Show();
3559 BoundsTreeTestView
* widget_view
= new BoundsTreeTestView
;
3560 test_widget
->SetContentsView(widget_view
);
3561 widget_view
->AddChildView(v2
);
3563 // Now schedule full-view paints in both widgets.
3564 test_view
->SchedulePaintInRect(test_view
->bounds());
3565 widget_view
->SchedulePaintInRect(widget_view
->bounds());
3566 GetRootLayer()->GetCompositor()->ScheduleDraw();
3567 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3569 // Only v1 should be present in the first cull set.
3570 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3571 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3572 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3573 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3575 // We should find v2 and v3 in the widget_view cull_set.
3576 EXPECT_EQ(4U, widget_view
->last_cull_set_
.size());
3577 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(test_widget
->GetRootView()));
3578 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(widget_view
));
3579 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v2
));
3580 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v3
));
3585 std::string
ToString(const gfx::Vector2dF
& vector
) {
3586 return base::StringPrintf("%.2f %0.2f", vector
.x(), vector
.y());
3591 TEST_F(ViewLayerTest
, SnapLayerToPixel
) {
3592 View
* v1
= new View
;
3594 View
* v11
= new View
;
3595 v1
->AddChildView(v11
);
3597 widget()->SetContentsView(v1
);
3599 const gfx::Size
& size
= GetRootLayer()->GetCompositor()->size();
3600 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f
, size
);
3602 v11
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3603 v1
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3604 v11
->SetPaintToLayer(true);
3606 EXPECT_EQ("0.40 0.40", ToString(v11
->layer()->subpixel_position_offset()));
3608 // Creating a layer in parent should update the child view's layer offset.
3609 v1
->SetPaintToLayer(true);
3610 EXPECT_EQ("-0.20 -0.20", ToString(v1
->layer()->subpixel_position_offset()));
3611 EXPECT_EQ("-0.20 -0.20", ToString(v11
->layer()->subpixel_position_offset()));
3613 // DSF change should get propagated and update offsets.
3614 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f
, size
);
3615 EXPECT_EQ("0.33 0.33", ToString(v1
->layer()->subpixel_position_offset()));
3616 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3618 // Deleting parent's layer should update the child view's layer's offset.
3619 v1
->SetPaintToLayer(false);
3620 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3622 // Setting parent view should update the child view's layer's offset.
3623 v1
->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3624 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3626 // Setting integral DSF should reset the offset.
3627 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f
, size
);
3628 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3631 TEST_F(ViewTest
, FocusableAssertions
) {
3632 // View subclasses may change insets based on whether they are focusable,
3633 // which effects the preferred size. To avoid preferred size changing around
3634 // these Views need to key off the last value set to SetFocusable(), not
3635 // whether the View is focusable right now. For this reason it's important
3636 // that focusable() return the last value passed to SetFocusable and not
3637 // whether the View is focusable right now.
3639 view
.SetFocusable(true);
3640 EXPECT_TRUE(view
.focusable());
3641 view
.SetEnabled(false);
3642 EXPECT_TRUE(view
.focusable());
3643 view
.SetFocusable(false);
3644 EXPECT_FALSE(view
.focusable());
3647 // Verifies when a view is deleted it is removed from ViewStorage.
3648 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3649 ViewStorage
* view_storage
= ViewStorage::GetInstance();
3650 const int storage_id
= view_storage
->CreateStorageID();
3653 view_storage
->StoreView(storage_id
, &view
);
3655 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
3658 ////////////////////////////////////////////////////////////////////////////////
3660 ////////////////////////////////////////////////////////////////////////////////
3662 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
3663 native_theme_
= native_theme
;
3666 TEST_F(ViewTest
, OnNativeThemeChanged
) {
3667 TestView
* test_view
= new TestView();
3668 EXPECT_FALSE(test_view
->native_theme_
);
3669 TestView
* test_view_child
= new TestView();
3670 EXPECT_FALSE(test_view_child
->native_theme_
);
3672 // Child view added before the widget hierarchy exists should get the
3673 // new native theme notification.
3674 test_view
->AddChildView(test_view_child
);
3676 scoped_ptr
<Widget
> widget(new Widget
);
3677 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
3678 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3679 widget
->Init(params
);
3681 widget
->GetRootView()->AddChildView(test_view
);
3682 EXPECT_TRUE(test_view
->native_theme_
);
3683 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
3684 EXPECT_TRUE(test_view_child
->native_theme_
);
3685 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
3687 // Child view added after the widget hierarchy exists should also get the
3689 TestView
* test_view_child_2
= new TestView();
3690 test_view
->AddChildView(test_view_child_2
);
3691 EXPECT_TRUE(test_view_child_2
->native_theme_
);
3692 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
3697 } // namespace views