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
->OnMouseDragged(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 // Make sure that resizing a parent in RTL correctly repositions its children.
2959 TEST_F(ViewLayerTest
, ResizeParentInRTL
) {
2960 std::string locale
= l10n_util::GetApplicationLocale(std::string());
2961 base::i18n::SetICUDefaultLocale("he");
2963 View
* view
= new View
;
2964 widget()->SetContentsView(view
);
2966 int content_width
= view
->width();
2968 // Create a paints-to-layer view |v1|.
2969 View
* v1
= new View
;
2970 v1
->SetPaintToLayer(true);
2971 v1
->SetBounds(10, 10, 20, 10);
2972 view
->AddChildView(v1
);
2973 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
2974 v1
->layer()->bounds());
2976 // Attach a paints-to-layer child view to |v1|.
2977 View
* v2
= new View
;
2978 v2
->SetPaintToLayer(true);
2979 v2
->SetBounds(3, 5, 6, 4);
2980 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
2981 v2
->layer()->bounds());
2982 v1
->AddChildView(v2
);
2983 // Check that |v2| now has RTL-appropriate bounds.
2984 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
2985 v2
->layer()->bounds());
2987 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
2988 View
* v3
= new View
;
2989 v3
->SetBounds(1, 1, 18, 8);
2990 View
* v4
= new View
;
2991 v4
->SetPaintToLayer(true);
2992 v4
->SetBounds(2, 4, 6, 4);
2993 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
2994 v4
->layer()->bounds());
2995 v3
->AddChildView(v4
);
2996 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
2997 v4
->layer()->bounds());
2998 v1
->AddChildView(v3
);
2999 // Check that |v4| now has RTL-appropriate bounds.
3000 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3001 v4
->layer()->bounds());
3003 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3004 // correctly to RTL-appropriate bounds.
3005 v1
->SetSize(gfx::Size(30, 10));
3006 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3007 v2
->layer()->bounds());
3008 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3009 v4
->layer()->bounds());
3011 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3012 // to RTL-appropriate bounds.
3013 v3
->SetBounds(2, 1, 12, 8);
3014 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3015 v4
->layer()->bounds());
3018 base::i18n::SetICUDefaultLocale(locale
);
3021 // Makes sure a transform persists after toggling the visibility.
3022 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3023 View
* view
= new View
;
3024 gfx::Transform transform
;
3025 transform
.Scale(2.0, 2.0);
3026 view
->SetTransform(transform
);
3027 widget()->SetContentsView(view
);
3028 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3030 view
->SetVisible(false);
3031 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3033 view
->SetVisible(true);
3034 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3037 // Verifies a transform persists after removing/adding a view with a transform.
3038 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3039 View
* view
= new View
;
3040 gfx::Transform transform
;
3041 transform
.Scale(2.0, 2.0);
3042 view
->SetTransform(transform
);
3043 widget()->SetContentsView(view
);
3044 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3045 ASSERT_TRUE(view
->layer() != NULL
);
3046 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3048 View
* parent
= view
->parent();
3049 parent
->RemoveChildView(view
);
3050 parent
->AddChildView(view
);
3052 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3053 ASSERT_TRUE(view
->layer() != NULL
);
3054 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3057 // Makes sure that layer visibility is correct after toggling View visibility.
3058 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3059 View
* content_view
= new View
;
3060 widget()->SetContentsView(content_view
);
3062 // The view isn't attached to a widget or a parent view yet. But it should
3063 // still have a layer, but the layer should not be attached to the root
3065 View
* v1
= new View
;
3066 v1
->SetPaintToLayer(true);
3067 EXPECT_TRUE(v1
->layer());
3068 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3071 // Once the view is attached to a widget, its layer should be attached to the
3072 // root layer and visible.
3073 content_view
->AddChildView(v1
);
3074 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3076 EXPECT_TRUE(v1
->layer()->IsDrawn());
3078 v1
->SetVisible(false);
3079 EXPECT_FALSE(v1
->layer()->IsDrawn());
3081 v1
->SetVisible(true);
3082 EXPECT_TRUE(v1
->layer()->IsDrawn());
3085 EXPECT_FALSE(v1
->layer()->IsDrawn());
3088 EXPECT_TRUE(v1
->layer()->IsDrawn());
3091 // Tests that the layers in the subtree are orphaned after a View is removed
3093 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3094 View
* content_view
= new View
;
3095 widget()->SetContentsView(content_view
);
3097 View
* v1
= new View
;
3098 content_view
->AddChildView(v1
);
3100 View
* v2
= new View
;
3101 v1
->AddChildView(v2
);
3102 v2
->SetPaintToLayer(true);
3103 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3105 EXPECT_TRUE(v2
->layer()->IsDrawn());
3107 content_view
->RemoveChildView(v1
);
3109 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3113 content_view
->AddChildView(v2
);
3116 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3118 EXPECT_TRUE(v2
->layer()->IsDrawn());
3121 class PaintTrackingView
: public View
{
3123 PaintTrackingView() : painted_(false) {
3126 bool painted() const { return painted_
; }
3127 void set_painted(bool value
) { painted_
= value
; }
3129 void OnPaint(gfx::Canvas
* canvas
) override
{ painted_
= true; }
3134 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3137 // Makes sure child views with layers aren't painted when paint starts at an
3139 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3140 PaintTrackingView
* content_view
= new PaintTrackingView
;
3141 widget()->SetContentsView(content_view
);
3142 content_view
->SetPaintToLayer(true);
3143 GetRootLayer()->GetCompositor()->ScheduleDraw();
3144 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3145 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3146 content_view
->set_painted(false);
3147 // content_view no longer has a dirty rect. Paint from the root and make sure
3148 // PaintTrackingView isn't painted.
3149 GetRootLayer()->GetCompositor()->ScheduleDraw();
3150 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3151 EXPECT_FALSE(content_view
->painted());
3153 // Make content_view have a dirty rect, paint the layers and make sure
3154 // PaintTrackingView is painted.
3155 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3156 GetRootLayer()->GetCompositor()->ScheduleDraw();
3157 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3158 EXPECT_TRUE(content_view
->painted());
3161 // Tests that the visibility of child layers are updated correctly when a View's
3162 // visibility changes.
3163 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3164 View
* v1
= new View
;
3165 v1
->SetPaintToLayer(true);
3166 widget()->SetContentsView(v1
);
3168 View
* v2
= new View
;
3169 v1
->AddChildView(v2
);
3171 View
* v3
= new View
;
3172 v2
->AddChildView(v3
);
3173 v3
->SetVisible(false);
3175 View
* v4
= new View
;
3176 v4
->SetPaintToLayer(true);
3177 v3
->AddChildView(v4
);
3179 EXPECT_TRUE(v1
->layer()->IsDrawn());
3180 EXPECT_FALSE(v4
->layer()->IsDrawn());
3182 v2
->SetVisible(false);
3183 EXPECT_TRUE(v1
->layer()->IsDrawn());
3184 EXPECT_FALSE(v4
->layer()->IsDrawn());
3186 v2
->SetVisible(true);
3187 EXPECT_TRUE(v1
->layer()->IsDrawn());
3188 EXPECT_FALSE(v4
->layer()->IsDrawn());
3190 v2
->SetVisible(false);
3191 EXPECT_TRUE(v1
->layer()->IsDrawn());
3192 EXPECT_FALSE(v4
->layer()->IsDrawn());
3193 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3195 v3
->SetVisible(true);
3196 EXPECT_TRUE(v1
->layer()->IsDrawn());
3197 EXPECT_FALSE(v4
->layer()->IsDrawn());
3198 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3200 // Reparent |v3| to |v1|.
3201 v1
->AddChildView(v3
);
3202 EXPECT_TRUE(v1
->layer()->IsDrawn());
3203 EXPECT_TRUE(v4
->layer()->IsDrawn());
3204 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3207 // This test creates a random View tree, and then randomly reorders child views,
3208 // reparents views etc. Unrelated changes can appear to break this test. So
3209 // marking this as FLAKY.
3210 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3211 View
* content
= new View
;
3212 content
->SetPaintToLayer(true);
3213 widget()->SetContentsView(content
);
3216 ConstructTree(content
, 5);
3217 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3219 ScrambleTree(content
);
3220 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3222 ScrambleTree(content
);
3223 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3225 ScrambleTree(content
);
3226 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3229 // Verifies when views are reordered the layer is also reordered. The widget is
3230 // providing the parent layer.
3231 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3232 View
* content
= new View
;
3233 widget()->SetContentsView(content
);
3234 View
* c1
= new View
;
3235 c1
->SetPaintToLayer(true);
3236 content
->AddChildView(c1
);
3237 View
* c2
= new View
;
3238 c2
->SetPaintToLayer(true);
3239 content
->AddChildView(c2
);
3241 ui::Layer
* parent_layer
= c1
->layer()->parent();
3242 ASSERT_TRUE(parent_layer
);
3243 ASSERT_EQ(2u, parent_layer
->children().size());
3244 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3245 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3247 // Move c1 to the front. The layers should have moved too.
3248 content
->ReorderChildView(c1
, -1);
3249 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3250 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3253 // Verifies that the layer of a view can be acquired properly.
3254 TEST_F(ViewLayerTest
, AcquireLayer
) {
3255 View
* content
= new View
;
3256 widget()->SetContentsView(content
);
3257 scoped_ptr
<View
> c1(new View
);
3258 c1
->SetPaintToLayer(true);
3259 EXPECT_TRUE(c1
->layer());
3260 content
->AddChildView(c1
.get());
3262 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3263 EXPECT_EQ(layer
.get(), c1
->layer());
3265 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3266 EXPECT_NE(c1
->layer(), layer2
.get());
3268 // Destroy view before destroying layer.
3272 // Verify the z-order of the layers as a result of calling RecreateLayer().
3273 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3274 scoped_ptr
<View
> v(new View());
3275 v
->SetPaintToLayer(true);
3277 View
* v1
= new View();
3278 v1
->SetPaintToLayer(true);
3279 v
->AddChildView(v1
);
3280 View
* v2
= new View();
3281 v2
->SetPaintToLayer(true);
3282 v
->AddChildView(v2
);
3284 // Test the initial z-order.
3285 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3286 ASSERT_EQ(2u, child_layers_pre
.size());
3287 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3288 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3290 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3292 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3293 // for |v1| and |v2|.
3294 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3295 ASSERT_EQ(3u, child_layers_post
.size());
3296 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3297 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3298 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3301 // Verify the z-order of the layers as a result of calling RecreateLayer when
3302 // the widget is the parent with the layer.
3303 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3304 View
* v
= new View();
3305 widget()->SetContentsView(v
);
3307 View
* v1
= new View();
3308 v1
->SetPaintToLayer(true);
3309 v
->AddChildView(v1
);
3310 View
* v2
= new View();
3311 v2
->SetPaintToLayer(true);
3312 v
->AddChildView(v2
);
3314 ui::Layer
* root_layer
= GetRootLayer();
3316 // Test the initial z-order.
3317 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3318 ASSERT_EQ(2u, child_layers_pre
.size());
3319 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3320 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3322 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3324 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3325 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3326 ASSERT_EQ(3u, child_layers_post
.size());
3327 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3328 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3329 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3332 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3334 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3336 v
.SetPaintToLayer(true);
3338 child
.SetPaintToLayer(true);
3339 v
.AddChildView(&child
);
3340 ASSERT_TRUE(v
.layer() != NULL
);
3341 ASSERT_EQ(1u, v
.layer()->children().size());
3342 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3344 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3345 v
.layer()->Add(&layer
);
3346 v
.layer()->StackAtBottom(&layer
);
3348 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3350 // All children should be moved from old layer to new layer.
3351 ASSERT_TRUE(old_layer
.get() != NULL
);
3352 EXPECT_TRUE(old_layer
->children().empty());
3354 // And new layer should have the two children.
3355 ASSERT_TRUE(v
.layer() != NULL
);
3356 ASSERT_EQ(2u, v
.layer()->children().size());
3357 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3358 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3361 class BoundsTreeTestView
: public View
{
3363 BoundsTreeTestView() {}
3365 void PaintChildren(gfx::Canvas
* canvas
, const CullSet
& cull_set
) override
{
3366 // Save out a copy of the cull_set before calling the base implementation.
3367 last_cull_set_
.clear();
3368 if (cull_set
.cull_set_
) {
3369 for (base::hash_set
<intptr_t>::iterator it
= cull_set
.cull_set_
->begin();
3370 it
!= cull_set
.cull_set_
->end();
3372 last_cull_set_
.insert(reinterpret_cast<View
*>(*it
));
3375 View::PaintChildren(canvas
, cull_set
);
3378 std::set
<View
*> last_cull_set_
;
3381 TEST_F(ViewLayerTest
, BoundsTreePaintUpdatesCullSet
) {
3382 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3383 widget()->SetContentsView(test_view
);
3385 View
* v1
= new View();
3386 v1
->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3387 test_view
->AddChildView(v1
);
3389 View
* v2
= new View();
3390 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3391 v1
->AddChildView(v2
);
3393 // Schedule a full-view paint to get everyone's rectangles updated.
3394 test_view
->SchedulePaintInRect(test_view
->bounds());
3395 GetRootLayer()->GetCompositor()->ScheduleDraw();
3396 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3398 // Now we have test_view - v1 - v2. Damage to only test_view should only
3399 // return root_view and test_view.
3400 test_view
->SchedulePaintInRect(gfx::Rect(0, 0, 1, 1));
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
));
3407 // Damage to v1 only should only return root_view, test_view, and v1.
3408 test_view
->SchedulePaintInRect(gfx::Rect(11, 16, 1, 1));
3409 GetRootLayer()->GetCompositor()->ScheduleDraw();
3410 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3411 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3412 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3413 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3414 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3416 // A Damage rect inside v2 should get all 3 views back in the |last_cull_set_|
3417 // on call to TestView::Paint(), along with the widget root view.
3418 test_view
->SchedulePaintInRect(gfx::Rect(31, 49, 1, 1));
3419 GetRootLayer()->GetCompositor()->ScheduleDraw();
3420 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3421 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3422 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3423 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3424 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3425 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3428 TEST_F(ViewLayerTest
, BoundsTreeWithRTL
) {
3429 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3430 base::i18n::SetICUDefaultLocale("ar");
3432 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3433 widget()->SetContentsView(test_view
);
3435 // Add child views, which should be in RTL coordinate space of parent view.
3436 View
* v1
= new View
;
3437 v1
->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3438 test_view
->AddChildView(v1
);
3440 View
* v2
= new View
;
3441 v2
->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3442 v1
->AddChildView(v2
);
3444 // Schedule a full-view paint to get everyone's rectangles updated.
3445 test_view
->SchedulePaintInRect(test_view
->bounds());
3446 GetRootLayer()->GetCompositor()->ScheduleDraw();
3447 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3449 // Damage to the right side of the parent view should touch both child views.
3450 gfx::Rect
rtl_damage(test_view
->bounds().width() - 16, 18, 1, 1);
3451 test_view
->SchedulePaintInRect(rtl_damage
);
3452 GetRootLayer()->GetCompositor()->ScheduleDraw();
3453 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3454 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3455 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3456 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3457 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3458 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3460 // Damage to the left side of the parent view should only touch the
3462 gfx::Rect
ltr_damage(16, 18, 1, 1);
3463 test_view
->SchedulePaintInRect(ltr_damage
);
3464 GetRootLayer()->GetCompositor()->ScheduleDraw();
3465 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3466 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3467 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3468 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3471 base::i18n::SetICUDefaultLocale(locale
);
3474 TEST_F(ViewLayerTest
, BoundsTreeSetBoundsChangesCullSet
) {
3475 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3476 widget()->SetContentsView(test_view
);
3478 View
* v1
= new View
;
3479 v1
->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3480 test_view
->AddChildView(v1
);
3482 View
* v2
= new View
;
3483 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3484 v1
->AddChildView(v2
);
3486 // Schedule a full-view paint to get everyone's rectangles updated.
3487 test_view
->SchedulePaintInRect(test_view
->bounds());
3488 GetRootLayer()->GetCompositor()->ScheduleDraw();
3489 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3491 // Move v1 to a new origin out of the way of our next query.
3492 v1
->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3493 // The move will force a repaint.
3494 GetRootLayer()->GetCompositor()->ScheduleDraw();
3495 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3497 // Schedule a paint with damage rect where v1 used to be.
3498 test_view
->SchedulePaintInRect(gfx::Rect(5, 6, 10, 11));
3499 GetRootLayer()->GetCompositor()->ScheduleDraw();
3500 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3502 // Should only have picked up root_view and test_view.
3503 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3504 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3505 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3508 TEST_F(ViewLayerTest
, BoundsTreeLayerChangeMakesNewTree
) {
3509 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3510 widget()->SetContentsView(test_view
);
3512 View
* v1
= new View
;
3513 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3514 test_view
->AddChildView(v1
);
3516 View
* v2
= new View
;
3517 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3518 v1
->AddChildView(v2
);
3520 // Schedule a full-view paint to get everyone's rectangles updated.
3521 test_view
->SchedulePaintInRect(test_view
->bounds());
3522 GetRootLayer()->GetCompositor()->ScheduleDraw();
3523 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3525 // Set v1 to paint to its own layer, it should remove itself from the
3526 // test_view heiarchy and no longer intersect with damage rects in that cull
3528 v1
->SetPaintToLayer(true);
3530 // Schedule another full-view paint.
3531 test_view
->SchedulePaintInRect(test_view
->bounds());
3532 GetRootLayer()->GetCompositor()->ScheduleDraw();
3533 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3534 // v1 and v2 should no longer be present in the test_view cull_set.
3535 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3536 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3537 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3539 // Now set v1 back to not painting to a layer.
3540 v1
->SetPaintToLayer(false);
3541 // Schedule another full-view paint.
3542 test_view
->SchedulePaintInRect(test_view
->bounds());
3543 GetRootLayer()->GetCompositor()->ScheduleDraw();
3544 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3545 // We should be back to the full cull set including v1 and v2.
3546 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3547 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3548 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3549 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3550 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3553 TEST_F(ViewLayerTest
, BoundsTreeRemoveChildRemovesBounds
) {
3554 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3555 widget()->SetContentsView(test_view
);
3557 View
* v1
= new View
;
3558 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3559 test_view
->AddChildView(v1
);
3561 View
* v2
= new View
;
3562 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3563 v1
->AddChildView(v2
);
3565 // Schedule a full-view paint to get everyone's rectangles updated.
3566 test_view
->SchedulePaintInRect(test_view
->bounds());
3567 GetRootLayer()->GetCompositor()->ScheduleDraw();
3568 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3570 // Now remove v1 from the root view.
3571 test_view
->RemoveChildView(v1
);
3573 // Schedule another full-view paint.
3574 test_view
->SchedulePaintInRect(test_view
->bounds());
3575 GetRootLayer()->GetCompositor()->ScheduleDraw();
3576 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3577 // v1 and v2 should no longer be present in the test_view cull_set.
3578 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3579 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3580 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3582 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3583 // deleted with that hierarchy.
3587 TEST_F(ViewLayerTest
, BoundsTreeMoveViewMovesBounds
) {
3588 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3589 widget()->SetContentsView(test_view
);
3591 // Build hierarchy v1 - v2 - v3.
3592 View
* v1
= new View
;
3593 v1
->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3594 test_view
->AddChildView(v1
);
3596 View
* v2
= new View
;
3597 v2
->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3598 v1
->AddChildView(v2
);
3600 View
* v3
= new View
;
3601 v3
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3602 v2
->AddChildView(v3
);
3604 // Schedule a full-view paint and ensure all views are present in the cull.
3605 test_view
->SchedulePaintInRect(test_view
->bounds());
3606 GetRootLayer()->GetCompositor()->ScheduleDraw();
3607 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3608 EXPECT_EQ(5U, test_view
->last_cull_set_
.size());
3609 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3610 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3611 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3612 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3613 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v3
));
3615 // Build an unrelated view hierarchy and move v2 in to it.
3616 scoped_ptr
<Widget
> test_widget(new Widget
);
3617 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3618 params
.bounds
= gfx::Rect(10, 10, 500, 500);
3619 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3620 test_widget
->Init(params
);
3621 test_widget
->Show();
3622 BoundsTreeTestView
* widget_view
= new BoundsTreeTestView
;
3623 test_widget
->SetContentsView(widget_view
);
3624 widget_view
->AddChildView(v2
);
3626 // Now schedule full-view paints in both widgets.
3627 test_view
->SchedulePaintInRect(test_view
->bounds());
3628 widget_view
->SchedulePaintInRect(widget_view
->bounds());
3629 GetRootLayer()->GetCompositor()->ScheduleDraw();
3630 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3632 // Only v1 should be present in the first cull set.
3633 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3634 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3635 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3636 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3638 // We should find v2 and v3 in the widget_view cull_set.
3639 EXPECT_EQ(4U, widget_view
->last_cull_set_
.size());
3640 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(test_widget
->GetRootView()));
3641 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(widget_view
));
3642 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v2
));
3643 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v3
));
3648 std::string
ToString(const gfx::Vector2dF
& vector
) {
3649 return base::StringPrintf("%.2f %0.2f", vector
.x(), vector
.y());
3654 TEST_F(ViewLayerTest
, SnapLayerToPixel
) {
3655 View
* v1
= new View
;
3657 View
* v11
= new View
;
3658 v1
->AddChildView(v11
);
3660 widget()->SetContentsView(v1
);
3662 const gfx::Size
& size
= GetRootLayer()->GetCompositor()->size();
3663 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f
, size
);
3665 v11
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3666 v1
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3667 v11
->SetPaintToLayer(true);
3669 EXPECT_EQ("0.40 0.40", ToString(v11
->layer()->subpixel_position_offset()));
3671 // Creating a layer in parent should update the child view's layer offset.
3672 v1
->SetPaintToLayer(true);
3673 EXPECT_EQ("-0.20 -0.20", ToString(v1
->layer()->subpixel_position_offset()));
3674 EXPECT_EQ("-0.20 -0.20", ToString(v11
->layer()->subpixel_position_offset()));
3676 // DSF change should get propagated and update offsets.
3677 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f
, size
);
3678 EXPECT_EQ("0.33 0.33", ToString(v1
->layer()->subpixel_position_offset()));
3679 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3681 // Deleting parent's layer should update the child view's layer's offset.
3682 v1
->SetPaintToLayer(false);
3683 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3685 // Setting parent view should update the child view's layer's offset.
3686 v1
->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3687 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3689 // Setting integral DSF should reset the offset.
3690 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f
, size
);
3691 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3694 TEST_F(ViewTest
, FocusableAssertions
) {
3695 // View subclasses may change insets based on whether they are focusable,
3696 // which effects the preferred size. To avoid preferred size changing around
3697 // these Views need to key off the last value set to SetFocusable(), not
3698 // whether the View is focusable right now. For this reason it's important
3699 // that focusable() return the last value passed to SetFocusable and not
3700 // whether the View is focusable right now.
3702 view
.SetFocusable(true);
3703 EXPECT_TRUE(view
.focusable());
3704 view
.SetEnabled(false);
3705 EXPECT_TRUE(view
.focusable());
3706 view
.SetFocusable(false);
3707 EXPECT_FALSE(view
.focusable());
3710 // Verifies when a view is deleted it is removed from ViewStorage.
3711 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3712 ViewStorage
* view_storage
= ViewStorage::GetInstance();
3713 const int storage_id
= view_storage
->CreateStorageID();
3716 view_storage
->StoreView(storage_id
, &view
);
3718 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
3721 ////////////////////////////////////////////////////////////////////////////////
3723 ////////////////////////////////////////////////////////////////////////////////
3725 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
3726 native_theme_
= native_theme
;
3729 TEST_F(ViewTest
, OnNativeThemeChanged
) {
3730 TestView
* test_view
= new TestView();
3731 EXPECT_FALSE(test_view
->native_theme_
);
3732 TestView
* test_view_child
= new TestView();
3733 EXPECT_FALSE(test_view_child
->native_theme_
);
3735 // Child view added before the widget hierarchy exists should get the
3736 // new native theme notification.
3737 test_view
->AddChildView(test_view_child
);
3739 scoped_ptr
<Widget
> widget(new Widget
);
3740 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
3741 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3742 widget
->Init(params
);
3744 widget
->GetRootView()->AddChildView(test_view
);
3745 EXPECT_TRUE(test_view
->native_theme_
);
3746 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
3747 EXPECT_TRUE(test_view_child
->native_theme_
);
3748 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
3750 // Child view added after the widget hierarchy exists should also get the
3752 TestView
* test_view_child_2
= new TestView();
3753 test_view
->AddChildView(test_view_child_2
);
3754 EXPECT_TRUE(test_view_child_2
->native_theme_
);
3755 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
3760 } // namespace views