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/paint_context.h"
19 #include "ui/compositor/test/draw_waiter_for_test.h"
20 #include "ui/events/event.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/keycodes/keyboard_codes.h"
23 #include "ui/gfx/canvas.h"
24 #include "ui/gfx/path.h"
25 #include "ui/gfx/transform.h"
26 #include "ui/strings/grit/ui_strings.h"
27 #include "ui/views/background.h"
28 #include "ui/views/controls/native/native_view_host.h"
29 #include "ui/views/controls/scroll_view.h"
30 #include "ui/views/controls/textfield/textfield.h"
31 #include "ui/views/focus/view_storage.h"
32 #include "ui/views/test/views_test_base.h"
33 #include "ui/views/view.h"
34 #include "ui/views/views_delegate.h"
35 #include "ui/views/widget/native_widget.h"
36 #include "ui/views/widget/root_view.h"
37 #include "ui/views/window/dialog_client_view.h"
38 #include "ui/views/window/dialog_delegate.h"
40 using base::ASCIIToUTF16
;
44 // Returns true if |ancestor| is an ancestor of |layer|.
45 bool LayerIsAncestor(const ui::Layer
* ancestor
, const ui::Layer
* layer
) {
46 while (layer
&& layer
!= ancestor
)
47 layer
= layer
->parent();
48 return layer
== ancestor
;
51 // Convenience functions for walking a View tree.
52 const views::View
* FirstView(const views::View
* view
) {
53 const views::View
* v
= view
;
54 while (v
->has_children())
59 const views::View
* NextView(const views::View
* view
) {
60 const views::View
* v
= view
;
61 const views::View
* parent
= v
->parent();
64 int next
= parent
->GetIndexOf(v
) + 1;
65 if (next
!= parent
->child_count())
66 return FirstView(parent
->child_at(next
));
70 // Convenience functions for walking a Layer tree.
71 const ui::Layer
* FirstLayer(const ui::Layer
* layer
) {
72 const ui::Layer
* l
= layer
;
73 while (l
->children().size() > 0)
78 const ui::Layer
* NextLayer(const ui::Layer
* layer
) {
79 const ui::Layer
* parent
= layer
->parent();
82 const std::vector
<ui::Layer
*> children
= parent
->children();
84 for (index
= 0; index
< children
.size(); index
++) {
85 if (children
[index
] == layer
)
88 size_t next
= index
+ 1;
89 if (next
< children
.size())
90 return FirstLayer(children
[next
]);
94 // Given the root nodes of a View tree and a Layer tree, makes sure the two
96 bool ViewAndLayerTreeAreConsistent(const views::View
* view
,
97 const ui::Layer
* layer
) {
98 const views::View
* v
= FirstView(view
);
99 const ui::Layer
* l
= FirstLayer(layer
);
101 // Find the view with a layer.
102 while (v
&& !v
->layer())
108 // Check if the View tree and the Layer tree are in sync.
109 EXPECT_EQ(l
, v
->layer());
113 // Check if the visibility states of the View and the Layer are in sync.
114 EXPECT_EQ(l
->IsDrawn(), v
->IsDrawn());
115 if (v
->IsDrawn() != l
->IsDrawn()) {
116 for (const views::View
* vv
= v
; vv
; vv
= vv
->parent())
117 LOG(ERROR
) << "V: " << vv
<< " " << vv
->visible() << " "
118 << vv
->IsDrawn() << " " << vv
->layer();
119 for (const ui::Layer
* ll
= l
; ll
; ll
= ll
->parent())
120 LOG(ERROR
) << "L: " << ll
<< " " << ll
->IsDrawn();
124 // Check if the size of the View and the Layer are in sync.
125 EXPECT_EQ(l
->bounds(), v
->bounds());
126 if (v
->bounds() != l
->bounds())
129 if (v
== view
|| l
== layer
)
130 return v
== view
&& l
== layer
;
139 // Constructs a View tree with the specified depth.
140 void ConstructTree(views::View
* view
, int depth
) {
143 int count
= base::RandInt(1, 5);
144 for (int i
= 0; i
< count
; i
++) {
145 views::View
* v
= new views::View
;
146 view
->AddChildView(v
);
147 if (base::RandDouble() > 0.5)
148 v
->SetPaintToLayer(true);
149 if (base::RandDouble() < 0.2)
150 v
->SetVisible(false);
152 ConstructTree(v
, depth
- 1);
156 void ScrambleTree(views::View
* view
) {
157 int count
= view
->child_count();
160 for (int i
= 0; i
< count
; i
++) {
161 ScrambleTree(view
->child_at(i
));
165 int a
= base::RandInt(0, count
- 1);
166 int b
= base::RandInt(0, count
- 1);
168 views::View
* view_a
= view
->child_at(a
);
169 views::View
* view_b
= view
->child_at(b
);
170 view
->ReorderChildView(view_a
, b
);
171 view
->ReorderChildView(view_b
, a
);
174 if (!view
->layer() && base::RandDouble() < 0.1)
175 view
->SetPaintToLayer(true);
177 if (base::RandDouble() < 0.1)
178 view
->SetVisible(!view
->visible());
184 locale_
= l10n_util::GetApplicationLocale(std::string());
185 base::i18n::SetICUDefaultLocale("he");
187 ~ScopedRTL() { base::i18n::SetICUDefaultLocale(locale_
); }
197 typedef ViewsTestBase ViewTest
;
199 // A derived class for testing purpose.
200 class TestView
: public View
{
204 delete_on_pressed_(false),
207 can_process_events_within_subtree_(true) {}
208 ~TestView() override
{}
210 // Reset all test state
212 did_change_bounds_
= false;
213 last_mouse_event_type_
= 0;
214 location_
.SetPoint(0, 0);
215 received_mouse_enter_
= false;
216 received_mouse_exit_
= false;
218 accelerator_count_map_
.clear();
219 can_process_events_within_subtree_
= true;
222 // Exposed as public for testing.
224 views::View::Focus();
231 bool focusable() const { return View::focusable(); }
233 void set_can_process_events_within_subtree(bool can_process
) {
234 can_process_events_within_subtree_
= can_process
;
237 bool CanProcessEventsWithinSubtree() const override
{
238 return can_process_events_within_subtree_
;
241 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
242 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
243 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
244 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
245 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
246 void OnMouseExited(const ui::MouseEvent
& event
) override
;
248 void OnPaint(gfx::Canvas
* canvas
) override
;
249 void SchedulePaintInRect(const gfx::Rect
& rect
) override
;
250 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
252 void OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) override
;
255 bool did_change_bounds_
;
256 gfx::Rect new_bounds_
;
259 int last_mouse_event_type_
;
260 gfx::Point location_
;
261 bool received_mouse_enter_
;
262 bool received_mouse_exit_
;
263 bool delete_on_pressed_
;
266 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
270 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
273 const ui::NativeTheme
* native_theme_
;
275 // Value to return from CanProcessEventsWithinSubtree().
276 bool can_process_events_within_subtree_
;
279 ////////////////////////////////////////////////////////////////////////////////
281 ////////////////////////////////////////////////////////////////////////////////
283 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
284 did_change_bounds_
= true;
285 new_bounds_
= bounds();
288 TEST_F(ViewTest
, OnBoundsChanged
) {
291 gfx::Rect
prev_rect(0, 0, 200, 200);
292 gfx::Rect
new_rect(100, 100, 250, 250);
294 v
.SetBoundsRect(prev_rect
);
296 v
.SetBoundsRect(new_rect
);
298 EXPECT_TRUE(v
.did_change_bounds_
);
299 EXPECT_EQ(v
.new_bounds_
, new_rect
);
300 EXPECT_EQ(v
.bounds(), new_rect
);
303 ////////////////////////////////////////////////////////////////////////////////
305 ////////////////////////////////////////////////////////////////////////////////
307 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
308 last_mouse_event_type_
= event
.type();
309 location_
.SetPoint(event
.x(), event
.y());
310 if (delete_on_pressed_
)
315 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
316 last_mouse_event_type_
= event
.type();
317 location_
.SetPoint(event
.x(), event
.y());
321 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
322 last_mouse_event_type_
= event
.type();
323 location_
.SetPoint(event
.x(), event
.y());
326 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
327 received_mouse_enter_
= true;
330 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
331 received_mouse_exit_
= true;
334 TEST_F(ViewTest
, MouseEvent
) {
335 TestView
* v1
= new TestView();
336 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
338 TestView
* v2
= new TestView();
339 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
341 scoped_ptr
<Widget
> widget(new Widget
);
342 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
343 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
344 params
.bounds
= gfx::Rect(50, 50, 650, 650);
345 widget
->Init(params
);
346 internal::RootView
* root
=
347 static_cast<internal::RootView
*>(widget
->GetRootView());
349 root
->AddChildView(v1
);
350 v1
->AddChildView(v2
);
355 gfx::Point
p1(110, 120);
356 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
, ui::EventTimeForNow(),
357 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
358 root
->OnMousePressed(pressed
);
359 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
360 EXPECT_EQ(v2
->location_
.x(), 10);
361 EXPECT_EQ(v2
->location_
.y(), 20);
362 // Make sure v1 did not receive the event
363 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
365 // Drag event out of bounds. Should still go to v2
368 gfx::Point
p2(50, 40);
369 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
, ui::EventTimeForNow(),
370 ui::EF_LEFT_MOUSE_BUTTON
, 0);
371 root
->OnMouseDragged(dragged
);
372 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
373 EXPECT_EQ(v2
->location_
.x(), -50);
374 EXPECT_EQ(v2
->location_
.y(), -60);
375 // Make sure v1 did not receive the event
376 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
378 // Releasted event out of bounds. Should still go to v2
381 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
382 ui::EventTimeForNow(), 0, 0);
383 root
->OnMouseDragged(released
);
384 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
385 EXPECT_EQ(v2
->location_
.x(), -100);
386 EXPECT_EQ(v2
->location_
.y(), -100);
387 // Make sure v1 did not receive the event
388 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
393 // Confirm that a view can be deleted as part of processing a mouse press.
394 TEST_F(ViewTest
, DeleteOnPressed
) {
395 TestView
* v1
= new TestView();
396 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
398 TestView
* v2
= new TestView();
399 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
404 scoped_ptr
<Widget
> widget(new Widget
);
405 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
406 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
407 params
.bounds
= gfx::Rect(50, 50, 650, 650);
408 widget
->Init(params
);
409 View
* root
= widget
->GetRootView();
411 root
->AddChildView(v1
);
412 v1
->AddChildView(v2
);
414 v2
->delete_on_pressed_
= true;
415 gfx::Point
point(110, 120);
416 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
417 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
418 ui::EF_LEFT_MOUSE_BUTTON
);
419 root
->OnMousePressed(pressed
);
420 EXPECT_EQ(0, v1
->child_count());
425 ////////////////////////////////////////////////////////////////////////////////
427 ////////////////////////////////////////////////////////////////////////////////
429 void TestView::OnPaint(gfx::Canvas
* canvas
) {
433 TEST_F(ViewTest
, PaintWithUnknownInvalidation
) {
434 Widget
* widget
= new Widget
;
435 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
436 widget
->Init(params
);
437 View
* root_view
= widget
->GetRootView();
438 root_view
->SetBounds(0, 0, 25, 26);
440 TestView
* v1
= new TestView
;
441 v1
->SetBounds(10, 11, 12, 13);
442 root_view
->AddChildView(v1
);
444 TestView
* v2
= new TestView
;
445 v2
->SetBounds(3, 4, 6, 5);
446 v1
->AddChildView(v2
);
448 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
449 gfx::Rect
paint_area(1, 1);
451 EXPECT_FALSE(v1
->did_paint_
);
452 EXPECT_FALSE(v2
->did_paint_
);
453 root_view
->Paint(ui::PaintContext(&canvas
));
454 EXPECT_TRUE(v1
->did_paint_
);
455 EXPECT_TRUE(v2
->did_paint_
);
459 EXPECT_FALSE(v1
->did_paint_
);
460 EXPECT_FALSE(v2
->did_paint_
);
462 ui::PaintContext(ui::PaintContext(&canvas
, paint_area
),
463 ui::PaintContext::CLONE_WITHOUT_INVALIDATION
));
464 EXPECT_TRUE(v1
->did_paint_
);
465 EXPECT_TRUE(v2
->did_paint_
);
468 TEST_F(ViewTest
, PaintContainsChildren
) {
469 Widget
* widget
= new Widget
;
470 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
471 widget
->Init(params
);
472 View
* root_view
= widget
->GetRootView();
473 root_view
->SetBounds(0, 0, 25, 26);
475 TestView
* v1
= new TestView
;
476 v1
->SetBounds(10, 11, 12, 13);
477 root_view
->AddChildView(v1
);
479 TestView
* v2
= new TestView
;
480 v2
->SetBounds(3, 4, 6, 5);
481 v1
->AddChildView(v2
);
483 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
484 gfx::Rect
paint_area(25, 26);
486 EXPECT_FALSE(v1
->did_paint_
);
487 EXPECT_FALSE(v2
->did_paint_
);
488 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
489 EXPECT_TRUE(v1
->did_paint_
);
490 EXPECT_TRUE(v2
->did_paint_
);
493 TEST_F(ViewTest
, PaintContainsChildrenInRTL
) {
496 Widget
* widget
= new Widget
;
497 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
498 widget
->Init(params
);
499 View
* root_view
= widget
->GetRootView();
500 root_view
->SetBounds(0, 0, 25, 26);
502 TestView
* v1
= new TestView
;
503 v1
->SetBounds(10, 11, 12, 13);
504 root_view
->AddChildView(v1
);
506 TestView
* v2
= new TestView
;
507 v2
->SetBounds(3, 4, 6, 5);
508 v1
->AddChildView(v2
);
510 // Verify where the layers actually appear.
511 v1
->SetPaintToLayer(true);
512 // x: 25 - 10(x) - 12(width) = 3
513 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
514 v1
->SetPaintToLayer(false);
516 v2
->SetPaintToLayer(true);
517 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
518 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
519 v2
->SetPaintToLayer(false);
521 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
522 gfx::Rect
paint_area(25, 26);
524 EXPECT_FALSE(v1
->did_paint_
);
525 EXPECT_FALSE(v2
->did_paint_
);
526 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
527 EXPECT_TRUE(v1
->did_paint_
);
528 EXPECT_TRUE(v2
->did_paint_
);
531 TEST_F(ViewTest
, PaintIntersectsChildren
) {
532 Widget
* widget
= new Widget
;
533 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
534 widget
->Init(params
);
535 View
* root_view
= widget
->GetRootView();
536 root_view
->SetBounds(0, 0, 25, 26);
538 TestView
* v1
= new TestView
;
539 v1
->SetBounds(10, 11, 12, 13);
540 root_view
->AddChildView(v1
);
542 TestView
* v2
= new TestView
;
543 v2
->SetBounds(3, 4, 6, 5);
544 v1
->AddChildView(v2
);
546 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
547 gfx::Rect
paint_area(9, 10, 5, 6);
549 EXPECT_FALSE(v1
->did_paint_
);
550 EXPECT_FALSE(v2
->did_paint_
);
551 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
552 EXPECT_TRUE(v1
->did_paint_
);
553 EXPECT_TRUE(v2
->did_paint_
);
556 TEST_F(ViewTest
, PaintIntersectsChildrenInRTL
) {
559 Widget
* widget
= new Widget
;
560 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
561 widget
->Init(params
);
562 View
* root_view
= widget
->GetRootView();
563 root_view
->SetBounds(0, 0, 25, 26);
565 TestView
* v1
= new TestView
;
566 v1
->SetBounds(10, 11, 12, 13);
567 root_view
->AddChildView(v1
);
569 TestView
* v2
= new TestView
;
570 v2
->SetBounds(3, 4, 6, 5);
571 v1
->AddChildView(v2
);
573 // Verify where the layers actually appear.
574 v1
->SetPaintToLayer(true);
575 // x: 25 - 10(x) - 12(width) = 3
576 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
577 v1
->SetPaintToLayer(false);
579 v2
->SetPaintToLayer(true);
580 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
581 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
582 v2
->SetPaintToLayer(false);
584 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
585 gfx::Rect
paint_area(2, 10, 5, 6);
587 EXPECT_FALSE(v1
->did_paint_
);
588 EXPECT_FALSE(v2
->did_paint_
);
589 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
590 EXPECT_TRUE(v1
->did_paint_
);
591 EXPECT_TRUE(v2
->did_paint_
);
594 TEST_F(ViewTest
, PaintIntersectsChildButNotGrandChild
) {
595 Widget
* widget
= new Widget
;
596 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
597 widget
->Init(params
);
598 View
* root_view
= widget
->GetRootView();
599 root_view
->SetBounds(0, 0, 25, 26);
601 TestView
* v1
= new TestView
;
602 v1
->SetBounds(10, 11, 12, 13);
603 root_view
->AddChildView(v1
);
605 TestView
* v2
= new TestView
;
606 v2
->SetBounds(3, 4, 6, 5);
607 v1
->AddChildView(v2
);
609 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
610 gfx::Rect
paint_area(9, 10, 2, 3);
612 EXPECT_FALSE(v1
->did_paint_
);
613 EXPECT_FALSE(v2
->did_paint_
);
614 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
615 EXPECT_TRUE(v1
->did_paint_
);
616 EXPECT_FALSE(v2
->did_paint_
);
619 TEST_F(ViewTest
, PaintIntersectsChildButNotGrandChildInRTL
) {
622 Widget
* widget
= new Widget
;
623 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
624 widget
->Init(params
);
625 View
* root_view
= widget
->GetRootView();
626 root_view
->SetBounds(0, 0, 25, 26);
628 TestView
* v1
= new TestView
;
629 v1
->SetBounds(10, 11, 12, 13);
630 root_view
->AddChildView(v1
);
632 TestView
* v2
= new TestView
;
633 v2
->SetBounds(3, 4, 6, 5);
634 v1
->AddChildView(v2
);
636 // Verify where the layers actually appear.
637 v1
->SetPaintToLayer(true);
638 // x: 25 - 10(x) - 12(width) = 3
639 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
640 v1
->SetPaintToLayer(false);
642 v2
->SetPaintToLayer(true);
643 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
644 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
645 v2
->SetPaintToLayer(false);
647 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
648 gfx::Rect
paint_area(2, 10, 2, 3);
650 EXPECT_FALSE(v1
->did_paint_
);
651 EXPECT_FALSE(v2
->did_paint_
);
652 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
653 EXPECT_TRUE(v1
->did_paint_
);
654 EXPECT_FALSE(v2
->did_paint_
);
657 TEST_F(ViewTest
, PaintIntersectsNoChildren
) {
658 Widget
* widget
= new Widget
;
659 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
660 widget
->Init(params
);
661 View
* root_view
= widget
->GetRootView();
662 root_view
->SetBounds(0, 0, 25, 26);
664 TestView
* v1
= new TestView
;
665 v1
->SetBounds(10, 11, 12, 13);
666 root_view
->AddChildView(v1
);
668 TestView
* v2
= new TestView
;
669 v2
->SetBounds(3, 4, 6, 5);
670 v1
->AddChildView(v2
);
672 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
673 gfx::Rect
paint_area(9, 10, 2, 1);
675 EXPECT_FALSE(v1
->did_paint_
);
676 EXPECT_FALSE(v2
->did_paint_
);
677 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
678 EXPECT_FALSE(v1
->did_paint_
);
679 EXPECT_FALSE(v2
->did_paint_
);
682 TEST_F(ViewTest
, PaintIntersectsNoChildrenInRTL
) {
685 Widget
* widget
= new Widget
;
686 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
687 widget
->Init(params
);
688 View
* root_view
= widget
->GetRootView();
689 root_view
->SetBounds(0, 0, 25, 26);
691 TestView
* v1
= new TestView
;
692 v1
->SetBounds(10, 11, 12, 13);
693 root_view
->AddChildView(v1
);
695 TestView
* v2
= new TestView
;
696 v2
->SetBounds(3, 4, 6, 5);
697 v1
->AddChildView(v2
);
699 // Verify where the layers actually appear.
700 v1
->SetPaintToLayer(true);
701 // x: 25 - 10(x) - 12(width) = 3
702 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
703 v1
->SetPaintToLayer(false);
705 v2
->SetPaintToLayer(true);
706 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
707 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
708 v2
->SetPaintToLayer(false);
710 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
711 gfx::Rect
paint_area(2, 10, 2, 1);
713 EXPECT_FALSE(v1
->did_paint_
);
714 EXPECT_FALSE(v2
->did_paint_
);
715 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
716 EXPECT_FALSE(v1
->did_paint_
);
717 EXPECT_FALSE(v2
->did_paint_
);
720 TEST_F(ViewTest
, PaintIntersectsOneChild
) {
721 Widget
* widget
= new Widget
;
722 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
723 widget
->Init(params
);
724 View
* root_view
= widget
->GetRootView();
725 root_view
->SetBounds(0, 0, 25, 26);
727 TestView
* v1
= new TestView
;
728 v1
->SetBounds(10, 11, 12, 13);
729 root_view
->AddChildView(v1
);
731 TestView
* v2
= new TestView
;
732 v2
->SetBounds(3, 4, 6, 5);
733 root_view
->AddChildView(v2
);
735 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
736 // Intersects with the second child only.
737 gfx::Rect
paint_area(3, 3, 1, 2);
739 EXPECT_FALSE(v1
->did_paint_
);
740 EXPECT_FALSE(v2
->did_paint_
);
741 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
742 EXPECT_FALSE(v1
->did_paint_
);
743 EXPECT_TRUE(v2
->did_paint_
);
745 // Intersects with the first child only.
746 paint_area
= gfx::Rect(20, 10, 1, 2);
750 EXPECT_FALSE(v1
->did_paint_
);
751 EXPECT_FALSE(v2
->did_paint_
);
752 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
753 EXPECT_TRUE(v1
->did_paint_
);
754 EXPECT_FALSE(v2
->did_paint_
);
757 TEST_F(ViewTest
, PaintIntersectsOneChildInRTL
) {
760 Widget
* widget
= new Widget
;
761 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
762 widget
->Init(params
);
763 View
* root_view
= widget
->GetRootView();
764 root_view
->SetBounds(0, 0, 25, 26);
766 TestView
* v1
= new TestView
;
767 v1
->SetBounds(10, 11, 12, 13);
768 root_view
->AddChildView(v1
);
770 TestView
* v2
= new TestView
;
771 v2
->SetBounds(3, 4, 6, 5);
772 root_view
->AddChildView(v2
);
774 // Verify where the layers actually appear.
775 v1
->SetPaintToLayer(true);
776 // x: 25 - 10(x) - 12(width) = 3
777 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
778 v1
->SetPaintToLayer(false);
780 v2
->SetPaintToLayer(true);
781 // x: 25 - 3(x) - 6(width) = 16
782 EXPECT_EQ(gfx::Rect(16, 4, 6, 5), v2
->layer()->bounds());
783 v2
->SetPaintToLayer(false);
785 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
786 // Intersects with the first child only.
787 gfx::Rect
paint_area(3, 10, 1, 2);
789 EXPECT_FALSE(v1
->did_paint_
);
790 EXPECT_FALSE(v2
->did_paint_
);
791 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
792 EXPECT_TRUE(v1
->did_paint_
);
793 EXPECT_FALSE(v2
->did_paint_
);
795 // Intersects with the second child only.
796 paint_area
= gfx::Rect(21, 3, 1, 2);
800 EXPECT_FALSE(v1
->did_paint_
);
801 EXPECT_FALSE(v2
->did_paint_
);
802 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
803 EXPECT_FALSE(v1
->did_paint_
);
804 EXPECT_TRUE(v2
->did_paint_
);
807 TEST_F(ViewTest
, PaintInPromotedToLayer
) {
808 Widget
* widget
= new Widget
;
809 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
810 widget
->Init(params
);
811 View
* root_view
= widget
->GetRootView();
812 root_view
->SetBounds(0, 0, 25, 26);
814 TestView
* v1
= new TestView
;
815 v1
->SetPaintToLayer(true);
816 v1
->SetBounds(10, 11, 12, 13);
817 root_view
->AddChildView(v1
);
819 TestView
* v2
= new TestView
;
820 v2
->SetBounds(3, 4, 6, 5);
821 v1
->AddChildView(v2
);
823 EXPECT_FALSE(v1
->did_paint_
);
824 EXPECT_FALSE(v2
->did_paint_
);
827 gfx::Canvas
canvas(root_view
->size(), 1.f
, true);
828 gfx::Rect
paint_area(25, 26);
830 // The promoted views are not painted as they are separate paint roots.
831 root_view
->Paint(ui::PaintContext(&canvas
, paint_area
));
832 EXPECT_FALSE(v1
->did_paint_
);
833 EXPECT_FALSE(v2
->did_paint_
);
837 gfx::Canvas
canvas(v1
->size(), 1.f
, true);
838 gfx::Rect
paint_area(1, 1);
840 // The |v1| view is painted. If it used its offset incorrect, it would think
841 // its at (10,11) instead of at (0,0) since it is the paint root.
842 v1
->Paint(ui::PaintContext(&canvas
, paint_area
));
843 EXPECT_TRUE(v1
->did_paint_
);
844 EXPECT_FALSE(v2
->did_paint_
);
850 gfx::Canvas
canvas(v1
->size(), 1.f
, true);
851 gfx::Rect
paint_area(3, 3, 1, 2);
853 // The |v2| view is painted also. If it used its offset incorrect, it would
854 // think its at (13,15) instead of at (3,4) since |v1| is the paint root.
855 v1
->Paint(ui::PaintContext(&canvas
, paint_area
));
856 EXPECT_TRUE(v1
->did_paint_
);
857 EXPECT_TRUE(v2
->did_paint_
);
861 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
862 scheduled_paint_rects_
.push_back(rect
);
863 View::SchedulePaintInRect(rect
);
866 TEST_F(ViewTest
, RemoveNotification
) {
867 ViewStorage
* vs
= ViewStorage::GetInstance();
868 Widget
* widget
= new Widget
;
869 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
870 View
* root_view
= widget
->GetRootView();
873 int s1
= vs
->CreateStorageID();
874 vs
->StoreView(s1
, v1
);
875 root_view
->AddChildView(v1
);
876 View
* v11
= new View
;
877 int s11
= vs
->CreateStorageID();
878 vs
->StoreView(s11
, v11
);
879 v1
->AddChildView(v11
);
880 View
* v111
= new View
;
881 int s111
= vs
->CreateStorageID();
882 vs
->StoreView(s111
, v111
);
883 v11
->AddChildView(v111
);
884 View
* v112
= new View
;
885 int s112
= vs
->CreateStorageID();
886 vs
->StoreView(s112
, v112
);
887 v11
->AddChildView(v112
);
888 View
* v113
= new View
;
889 int s113
= vs
->CreateStorageID();
890 vs
->StoreView(s113
, v113
);
891 v11
->AddChildView(v113
);
892 View
* v1131
= new View
;
893 int s1131
= vs
->CreateStorageID();
894 vs
->StoreView(s1131
, v1131
);
895 v113
->AddChildView(v1131
);
896 View
* v12
= new View
;
897 int s12
= vs
->CreateStorageID();
898 vs
->StoreView(s12
, v12
);
899 v1
->AddChildView(v12
);
902 int s2
= vs
->CreateStorageID();
903 vs
->StoreView(s2
, v2
);
904 root_view
->AddChildView(v2
);
905 View
* v21
= new View
;
906 int s21
= vs
->CreateStorageID();
907 vs
->StoreView(s21
, v21
);
908 v2
->AddChildView(v21
);
909 View
* v211
= new View
;
910 int s211
= vs
->CreateStorageID();
911 vs
->StoreView(s211
, v211
);
912 v21
->AddChildView(v211
);
914 size_t stored_views
= vs
->view_count();
916 // Try removing a leaf view.
917 v21
->RemoveChildView(v211
);
918 EXPECT_EQ(stored_views
- 1, vs
->view_count());
919 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
920 delete v211
; // We won't use this one anymore.
922 // Now try removing a view with a hierarchy of depth 1.
923 v11
->RemoveChildView(v113
);
924 EXPECT_EQ(stored_views
- 3, vs
->view_count());
925 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
926 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
927 delete v113
; // We won't use this one anymore.
929 // Now remove even more.
930 root_view
->RemoveChildView(v1
);
931 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
932 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
933 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
934 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
935 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
937 // Put v1 back for more tests.
938 root_view
->AddChildView(v1
);
939 vs
->StoreView(s1
, v1
);
941 // Synchronously closing the window deletes the view hierarchy, which should
942 // remove all its views from ViewStorage.
944 EXPECT_EQ(stored_views
- 10, vs
->view_count());
945 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
946 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
947 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
948 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
949 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
950 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
951 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
956 void RotateCounterclockwise(gfx::Transform
* transform
) {
957 transform
->matrix().set3x3(0, -1, 0,
962 void RotateClockwise(gfx::Transform
* transform
) {
963 transform
->matrix().set3x3( 0, 1, 0,
970 // Tests the correctness of the rect-based targeting algorithm implemented in
971 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
972 // of rect-based targeting.
973 TEST_F(ViewTest
, GetEventHandlerForRect
) {
974 Widget
* widget
= new Widget
;
975 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
976 widget
->Init(params
);
977 View
* root_view
= widget
->GetRootView();
978 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
980 // Have this hierarchy of views (the coordinates here are all in
981 // the root view's coordinate space):
982 // v1 (0, 0, 100, 100)
983 // v2 (150, 0, 250, 100)
984 // v3 (0, 200, 150, 100)
985 // v31 (10, 210, 80, 80)
986 // v32 (110, 210, 30, 80)
987 // v4 (300, 200, 100, 100)
988 // v41 (310, 210, 80, 80)
989 // v411 (370, 275, 10, 5)
990 // v5 (450, 197, 30, 36)
991 // v51 (450, 200, 30, 30)
993 // The coordinates used for SetBounds are in parent coordinates.
995 TestView
* v1
= new TestView
;
996 v1
->SetBounds(0, 0, 100, 100);
997 root_view
->AddChildView(v1
);
999 TestView
* v2
= new TestView
;
1000 v2
->SetBounds(150, 0, 250, 100);
1001 root_view
->AddChildView(v2
);
1003 TestView
* v3
= new TestView
;
1004 v3
->SetBounds(0, 200, 150, 100);
1005 root_view
->AddChildView(v3
);
1007 TestView
* v4
= new TestView
;
1008 v4
->SetBounds(300, 200, 100, 100);
1009 root_view
->AddChildView(v4
);
1011 TestView
* v31
= new TestView
;
1012 v31
->SetBounds(10, 10, 80, 80);
1013 v3
->AddChildView(v31
);
1015 TestView
* v32
= new TestView
;
1016 v32
->SetBounds(110, 10, 30, 80);
1017 v3
->AddChildView(v32
);
1019 TestView
* v41
= new TestView
;
1020 v41
->SetBounds(10, 10, 80, 80);
1021 v4
->AddChildView(v41
);
1023 TestView
* v411
= new TestView
;
1024 v411
->SetBounds(60, 65, 10, 5);
1025 v41
->AddChildView(v411
);
1027 TestView
* v5
= new TestView
;
1028 v5
->SetBounds(450, 197, 30, 36);
1029 root_view
->AddChildView(v5
);
1031 TestView
* v51
= new TestView
;
1032 v51
->SetBounds(0, 3, 30, 30);
1033 v5
->AddChildView(v51
);
1035 // |touch_rect| does not intersect any descendant view of |root_view|.
1036 gfx::Rect
touch_rect(105, 105, 30, 45);
1037 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1038 EXPECT_EQ(root_view
, result_view
);
1041 // Covers |v1| by at least 60%.
1042 touch_rect
.SetRect(15, 15, 100, 100);
1043 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1044 EXPECT_EQ(v1
, result_view
);
1047 // Intersects |v1| but does not cover it by at least 60%. The center
1048 // of |touch_rect| is within |v1|.
1049 touch_rect
.SetRect(50, 50, 5, 10);
1050 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1051 EXPECT_EQ(v1
, result_view
);
1054 // Intersects |v1| but does not cover it by at least 60%. The center
1055 // of |touch_rect| is not within |v1|.
1056 touch_rect
.SetRect(95, 96, 21, 22);
1057 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1058 EXPECT_EQ(root_view
, result_view
);
1061 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
1062 touch_rect
.SetRect(95, 10, 300, 120);
1063 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1064 EXPECT_EQ(v2
, result_view
);
1067 // Covers both |v1| and |v2| by at least 60%, but the center point
1068 // of |touch_rect| is closer to the center point of |v2|.
1069 touch_rect
.SetRect(20, 20, 400, 100);
1070 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1071 EXPECT_EQ(v2
, result_view
);
1074 // Covers both |v1| and |v2| by at least 60%, but the center point
1075 // of |touch_rect| is closer to the center point of |v1|.
1076 touch_rect
.SetRect(-700, -15, 1050, 110);
1077 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1078 EXPECT_EQ(v1
, result_view
);
1081 // A mouse click within |v1| will target |v1|.
1082 touch_rect
.SetRect(15, 15, 1, 1);
1083 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1084 EXPECT_EQ(v1
, result_view
);
1087 // Intersects |v3| and |v31| by at least 60% and the center point
1088 // of |touch_rect| is closer to the center point of |v31|.
1089 touch_rect
.SetRect(0, 200, 110, 100);
1090 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1091 EXPECT_EQ(v31
, result_view
);
1094 // Intersects |v3| and |v31|, but neither by at least 60%. The
1095 // center point of |touch_rect| lies within |v31|.
1096 touch_rect
.SetRect(80, 280, 15, 15);
1097 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1098 EXPECT_EQ(v31
, result_view
);
1101 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1102 // center point of |touch_rect| is closest to the center point
1104 touch_rect
.SetRect(0, 200, 200, 100);
1105 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1106 EXPECT_EQ(v32
, result_view
);
1109 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1110 // |v31| and |v32| by at least 60%. The center point of
1111 // |touch_rect| is closest to the center point of |v32|.
1112 touch_rect
.SetRect(30, 225, 180, 115);
1113 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1114 EXPECT_EQ(v32
, result_view
);
1117 // A mouse click at the corner of |v3| will target |v3|.
1118 touch_rect
.SetRect(0, 200, 1, 1);
1119 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1120 EXPECT_EQ(v3
, result_view
);
1123 // A mouse click within |v32| will target |v32|.
1124 touch_rect
.SetRect(112, 211, 1, 1);
1125 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1126 EXPECT_EQ(v32
, result_view
);
1129 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1130 // The center point of |touch_rect| is equally close to
1131 // the center points of |v4| and |v41|.
1132 touch_rect
.SetRect(310, 210, 80, 80);
1133 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1134 EXPECT_EQ(v41
, result_view
);
1137 // Intersects all of |v4|, |v41|, and |v411| but only covers
1138 // |v411| by at least 60%.
1139 touch_rect
.SetRect(370, 275, 7, 5);
1140 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1141 EXPECT_EQ(v411
, result_view
);
1144 // Intersects |v4| and |v41| but covers neither by at least 60%.
1145 // The center point of |touch_rect| is equally close to the center
1146 // points of |v4| and |v41|.
1147 touch_rect
.SetRect(345, 245, 7, 7);
1148 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1149 EXPECT_EQ(v41
, result_view
);
1152 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1153 // them by at least 60%. The center point of |touch_rect| lies
1155 touch_rect
.SetRect(368, 272, 4, 6);
1156 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1157 EXPECT_EQ(v411
, result_view
);
1160 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1161 // them by at least 60%. The center point of |touch_rect| lies
1163 touch_rect
.SetRect(365, 270, 7, 7);
1164 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1165 EXPECT_EQ(v41
, result_view
);
1168 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1169 // them by at least 60%. The center point of |touch_rect| lies
1171 touch_rect
.SetRect(205, 275, 200, 2);
1172 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1173 EXPECT_EQ(v4
, result_view
);
1176 // Intersects all of |v4|, |v41|, and |v411| but only covers
1177 // |v41| by at least 60%.
1178 touch_rect
.SetRect(310, 210, 61, 66);
1179 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1180 EXPECT_EQ(v41
, result_view
);
1183 // A mouse click within |v411| will target |v411|.
1184 touch_rect
.SetRect(372, 275, 1, 1);
1185 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1186 EXPECT_EQ(v411
, result_view
);
1189 // A mouse click within |v41| will target |v41|.
1190 touch_rect
.SetRect(350, 215, 1, 1);
1191 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1192 EXPECT_EQ(v41
, result_view
);
1195 // Covers |v3|, |v4|, and all of their descendants by at
1196 // least 60%. The center point of |touch_rect| is closest
1197 // to the center point of |v32|.
1198 touch_rect
.SetRect(0, 200, 400, 100);
1199 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1200 EXPECT_EQ(v32
, result_view
);
1203 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1204 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1205 // The center point of |touch_rect| is closest to the center
1206 // point of |root_view|.
1207 touch_rect
.SetRect(110, 15, 375, 450);
1208 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1209 EXPECT_EQ(root_view
, result_view
);
1212 // Covers all views (except |v5| and |v51|) by at least 60%. The
1213 // center point of |touch_rect| is equally close to the center
1214 // points of |v2| and |v32|. One is not a descendant of the other,
1215 // so in this case the view selected is arbitrary (i.e.,
1216 // it depends only on the ordering of nodes in the views
1218 touch_rect
.SetRect(0, 0, 400, 300);
1219 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1220 EXPECT_EQ(v32
, result_view
);
1223 // Covers |v5| and |v51| by at least 60%, and the center point of
1224 // the touch is located within both views. Since both views share
1225 // the same center point, the child view should be selected.
1226 touch_rect
.SetRect(440, 190, 40, 40);
1227 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1228 EXPECT_EQ(v51
, result_view
);
1231 // Covers |v5| and |v51| by at least 60%, but the center point of
1232 // the touch is not located within either view. Since both views
1233 // share the same center point, the child view should be selected.
1234 touch_rect
.SetRect(455, 187, 60, 60);
1235 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1236 EXPECT_EQ(v51
, result_view
);
1239 // Covers neither |v5| nor |v51| by at least 60%, but the center
1240 // of the touch is located within |v51|.
1241 touch_rect
.SetRect(450, 197, 10, 10);
1242 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1243 EXPECT_EQ(v51
, result_view
);
1246 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1247 // The center point is located outside of both views.
1248 touch_rect
.SetRect(433, 180, 24, 24);
1249 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1250 EXPECT_EQ(root_view
, result_view
);
1253 // Only intersects |v5| but does not cover it by at least 60%. The
1254 // center point of the touch region is located within |v5|.
1255 touch_rect
.SetRect(449, 196, 3, 3);
1256 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1257 EXPECT_EQ(v5
, result_view
);
1260 // A mouse click within |v5| (but not |v51|) should target |v5|.
1261 touch_rect
.SetRect(462, 199, 1, 1);
1262 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1263 EXPECT_EQ(v5
, result_view
);
1266 // A mouse click |v5| and |v51| should target the child view.
1267 touch_rect
.SetRect(452, 226, 1, 1);
1268 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1269 EXPECT_EQ(v51
, result_view
);
1272 // A mouse click on the center of |v5| and |v51| should target
1274 touch_rect
.SetRect(465, 215, 1, 1);
1275 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1276 EXPECT_EQ(v51
, result_view
);
1282 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1283 // as expected when different views in the view hierarchy return false
1284 // when CanProcessEventsWithinSubtree() is called.
1285 TEST_F(ViewTest
, CanProcessEventsWithinSubtree
) {
1286 Widget
* widget
= new Widget
;
1287 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1288 widget
->Init(params
);
1289 View
* root_view
= widget
->GetRootView();
1290 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1292 // Have this hierarchy of views (the coords here are in the coordinate
1293 // space of the root view):
1294 // v (0, 0, 100, 100)
1295 // - v_child (0, 0, 20, 30)
1296 // - v_grandchild (5, 5, 5, 15)
1298 TestView
* v
= new TestView
;
1299 v
->SetBounds(0, 0, 100, 100);
1300 root_view
->AddChildView(v
);
1301 v
->set_notify_enter_exit_on_child(true);
1303 TestView
* v_child
= new TestView
;
1304 v_child
->SetBounds(0, 0, 20, 30);
1305 v
->AddChildView(v_child
);
1307 TestView
* v_grandchild
= new TestView
;
1308 v_grandchild
->SetBounds(5, 5, 5, 15);
1309 v_child
->AddChildView(v_grandchild
);
1313 v_grandchild
->Reset();
1315 // Define rects and points within the views in the hierarchy.
1316 gfx::Rect
rect_in_v_grandchild(7, 7, 3, 3);
1317 gfx::Point
point_in_v_grandchild(rect_in_v_grandchild
.origin());
1318 gfx::Rect
rect_in_v_child(12, 3, 5, 5);
1319 gfx::Point
point_in_v_child(rect_in_v_child
.origin());
1320 gfx::Rect
rect_in_v(50, 50, 25, 30);
1321 gfx::Point
point_in_v(rect_in_v
.origin());
1323 // When all three views return true when CanProcessEventsWithinSubtree()
1324 // is called, targeting should behave as expected.
1326 View
* result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1327 EXPECT_EQ(v_grandchild
, result_view
);
1329 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1330 EXPECT_EQ(v_grandchild
, result_view
);
1333 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1334 EXPECT_EQ(v_child
, result_view
);
1336 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1337 EXPECT_EQ(v_child
, result_view
);
1340 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1341 EXPECT_EQ(v
, result_view
);
1343 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1344 EXPECT_EQ(v
, result_view
);
1347 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1348 // is called, then |v_grandchild| cannot be returned as a target.
1350 v_grandchild
->set_can_process_events_within_subtree(false);
1352 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1353 EXPECT_EQ(v_child
, result_view
);
1355 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1356 EXPECT_EQ(v_child
, result_view
);
1359 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1360 EXPECT_EQ(v_child
, result_view
);
1362 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1363 EXPECT_EQ(v_child
, result_view
);
1366 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1367 EXPECT_EQ(v
, result_view
);
1369 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1370 EXPECT_EQ(v
, result_view
);
1372 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1373 // is called, then NULL should be returned as a target if we call
1374 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
1375 // views tree. Note that the location must be in the coordinate space
1376 // of the root view (|v_grandchild| in this case), so use (1, 1).
1378 result_view
= v_grandchild
;
1379 result_view
= v_grandchild
->GetTooltipHandlerForPoint(gfx::Point(1, 1));
1380 EXPECT_EQ(NULL
, result_view
);
1383 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1384 // is called, then neither |v_child| nor |v_grandchild| can be returned
1385 // as a target (|v| should be returned as the target for each case).
1387 v_grandchild
->Reset();
1388 v_child
->set_can_process_events_within_subtree(false);
1390 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1391 EXPECT_EQ(v
, result_view
);
1393 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1394 EXPECT_EQ(v
, result_view
);
1397 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1398 EXPECT_EQ(v
, result_view
);
1400 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1401 EXPECT_EQ(v
, result_view
);
1404 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1405 EXPECT_EQ(v
, result_view
);
1407 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1408 EXPECT_EQ(v
, result_view
);
1411 // When |v| returns false when CanProcessEventsWithinSubtree()
1412 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1413 // as a target (|root_view| should be returned as the target for each case).
1416 v
->set_can_process_events_within_subtree(false);
1418 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1419 EXPECT_EQ(root_view
, result_view
);
1421 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1422 EXPECT_EQ(root_view
, result_view
);
1425 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1426 EXPECT_EQ(root_view
, result_view
);
1428 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1429 EXPECT_EQ(root_view
, result_view
);
1432 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1433 EXPECT_EQ(root_view
, result_view
);
1435 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1436 EXPECT_EQ(root_view
, result_view
);
1439 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1440 Widget
* widget
= new Widget
;
1441 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1442 widget
->Init(params
);
1443 View
* root_view
= widget
->GetRootView();
1444 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1446 // Have this hierarchy of views (the coords here are in root coord):
1447 // v1 (0, 0, 100, 100)
1448 // - v11 (0, 0, 20, 30)
1449 // - v111 (5, 5, 5, 15)
1450 // - v12 (50, 10, 30, 90)
1451 // - v121 (60, 20, 10, 10)
1452 // v2 (105, 0, 100, 100)
1453 // - v21 (120, 10, 50, 20)
1455 TestView
* v1
= new TestView
;
1456 v1
->SetBounds(0, 0, 100, 100);
1457 root_view
->AddChildView(v1
);
1458 v1
->set_notify_enter_exit_on_child(true);
1460 TestView
* v11
= new TestView
;
1461 v11
->SetBounds(0, 0, 20, 30);
1462 v1
->AddChildView(v11
);
1464 TestView
* v111
= new TestView
;
1465 v111
->SetBounds(5, 5, 5, 15);
1466 v11
->AddChildView(v111
);
1468 TestView
* v12
= new TestView
;
1469 v12
->SetBounds(50, 10, 30, 90);
1470 v1
->AddChildView(v12
);
1472 TestView
* v121
= new TestView
;
1473 v121
->SetBounds(10, 10, 10, 10);
1474 v12
->AddChildView(v121
);
1476 TestView
* v2
= new TestView
;
1477 v2
->SetBounds(105, 0, 100, 100);
1478 root_view
->AddChildView(v2
);
1480 TestView
* v21
= new TestView
;
1481 v21
->SetBounds(15, 10, 50, 20);
1482 v2
->AddChildView(v21
);
1492 // Move the mouse in v111.
1493 gfx::Point
p1(6, 6);
1494 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, ui::EventTimeForNow(), 0, 0);
1495 root_view
->OnMouseMoved(move1
);
1496 EXPECT_TRUE(v111
->received_mouse_enter_
);
1497 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1498 EXPECT_TRUE(v1
->received_mouse_enter_
);
1503 // Now, move into v121.
1504 gfx::Point
p2(65, 21);
1505 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, ui::EventTimeForNow(), 0, 0);
1506 root_view
->OnMouseMoved(move2
);
1507 EXPECT_TRUE(v111
->received_mouse_exit_
);
1508 EXPECT_TRUE(v121
->received_mouse_enter_
);
1509 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1514 // Now, move into v11.
1515 gfx::Point
p3(1, 1);
1516 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, ui::EventTimeForNow(), 0, 0);
1517 root_view
->OnMouseMoved(move3
);
1518 EXPECT_TRUE(v121
->received_mouse_exit_
);
1519 EXPECT_TRUE(v11
->received_mouse_enter_
);
1520 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1526 gfx::Point
p4(121, 15);
1527 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, ui::EventTimeForNow(), 0, 0);
1528 root_view
->OnMouseMoved(move4
);
1529 EXPECT_TRUE(v21
->received_mouse_enter_
);
1530 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1531 EXPECT_TRUE(v11
->received_mouse_exit_
);
1532 EXPECT_TRUE(v1
->received_mouse_exit_
);
1539 gfx::Point
p5(21, 0);
1540 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, ui::EventTimeForNow(), 0, 0);
1541 root_view
->OnMouseMoved(move5
);
1542 EXPECT_TRUE(v21
->received_mouse_exit_
);
1543 EXPECT_TRUE(v1
->received_mouse_enter_
);
1548 // Now, move into v11.
1549 gfx::Point
p6(15, 15);
1550 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, ui::EventTimeForNow(), 0,
1552 root_view
->OnMouseMoved(mouse6
);
1553 EXPECT_TRUE(v11
->received_mouse_enter_
);
1554 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1559 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1560 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1561 // when the mouse leaves v11.
1562 gfx::Point
p7(21, 0);
1563 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, ui::EventTimeForNow(), 0,
1565 root_view
->OnMouseMoved(mouse7
);
1566 EXPECT_TRUE(v11
->received_mouse_exit_
);
1567 EXPECT_FALSE(v1
->received_mouse_enter_
);
1572 TEST_F(ViewTest
, Textfield
) {
1573 const base::string16 kText
= ASCIIToUTF16(
1574 "Reality is that which, when you stop believing it, doesn't go away.");
1575 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1577 Widget
* widget
= new Widget
;
1578 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1579 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1580 widget
->Init(params
);
1581 View
* root_view
= widget
->GetRootView();
1583 Textfield
* textfield
= new Textfield();
1584 root_view
->AddChildView(textfield
);
1586 // Test setting, appending text.
1587 textfield
->SetText(kText
);
1588 EXPECT_EQ(kText
, textfield
->text());
1589 textfield
->AppendText(kExtraText
);
1590 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1591 textfield
->SetText(base::string16());
1592 EXPECT_TRUE(textfield
->text().empty());
1594 // Test selection related methods.
1595 textfield
->SetText(kText
);
1596 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1597 textfield
->SelectAll(false);
1598 EXPECT_EQ(kText
, textfield
->text());
1599 textfield
->ClearSelection();
1600 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1605 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1606 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1607 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1608 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1609 const base::string16 kPasswordText
=
1610 ASCIIToUTF16("Password! ** Secret stuff **");
1612 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1614 Widget
* widget
= new Widget
;
1615 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1616 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1617 widget
->Init(params
);
1618 View
* root_view
= widget
->GetRootView();
1620 Textfield
* normal
= new Textfield();
1621 Textfield
* read_only
= new Textfield();
1622 read_only
->SetReadOnly(true);
1623 Textfield
* password
= new Textfield();
1624 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1626 root_view
->AddChildView(normal
);
1627 root_view
->AddChildView(read_only
);
1628 root_view
->AddChildView(password
);
1630 normal
->SetText(kNormalText
);
1631 read_only
->SetText(kReadOnlyText
);
1632 password
->SetText(kPasswordText
);
1638 normal
->SelectAll(false);
1639 normal
->ExecuteCommand(IDS_APP_CUT
);
1640 base::string16 result
;
1641 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1642 EXPECT_EQ(kNormalText
, result
);
1643 normal
->SetText(kNormalText
); // Let's revert to the original content.
1645 read_only
->SelectAll(false);
1646 read_only
->ExecuteCommand(IDS_APP_CUT
);
1648 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1649 // Cut should have failed, so the clipboard content should not have changed.
1650 EXPECT_EQ(kNormalText
, result
);
1652 password
->SelectAll(false);
1653 password
->ExecuteCommand(IDS_APP_CUT
);
1655 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1656 // Cut should have failed, so the clipboard content should not have changed.
1657 EXPECT_EQ(kNormalText
, result
);
1663 // Start with |read_only| to observe a change in clipboard text.
1664 read_only
->SelectAll(false);
1665 read_only
->ExecuteCommand(IDS_APP_COPY
);
1667 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1668 EXPECT_EQ(kReadOnlyText
, result
);
1670 normal
->SelectAll(false);
1671 normal
->ExecuteCommand(IDS_APP_COPY
);
1673 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1674 EXPECT_EQ(kNormalText
, result
);
1676 password
->SelectAll(false);
1677 password
->ExecuteCommand(IDS_APP_COPY
);
1679 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1680 // Text cannot be copied from an obscured field; the clipboard won't change.
1681 EXPECT_EQ(kNormalText
, result
);
1687 // Attempting to paste kNormalText in a read-only text-field should fail.
1688 read_only
->SelectAll(false);
1689 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1690 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1692 password
->SelectAll(false);
1693 password
->ExecuteCommand(IDS_APP_PASTE
);
1694 EXPECT_EQ(kNormalText
, password
->text());
1696 // Copy from |read_only| to observe a change in the normal textfield text.
1697 read_only
->SelectAll(false);
1698 read_only
->ExecuteCommand(IDS_APP_COPY
);
1699 normal
->SelectAll(false);
1700 normal
->ExecuteCommand(IDS_APP_PASTE
);
1701 EXPECT_EQ(kReadOnlyText
, normal
->text());
1705 ////////////////////////////////////////////////////////////////////////////////
1707 ////////////////////////////////////////////////////////////////////////////////
1708 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1709 accelerator_count_map_
[accelerator
]++;
1713 // TODO: these tests were initially commented out when getting aura to
1714 // run. Figure out if still valuable and either nuke or fix.
1716 TEST_F(ViewTest
, ActivateAccelerator
) {
1717 // Register a keyboard accelerator before the view is added to a window.
1718 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1719 TestView
* view
= new TestView();
1721 view
->AddAccelerator(return_accelerator
);
1722 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1724 // Create a window and add the view as its child.
1725 scoped_ptr
<Widget
> widget(new Widget
);
1726 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1727 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1728 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1729 widget
->Init(params
);
1730 View
* root
= widget
->GetRootView();
1731 root
->AddChildView(view
);
1734 // Get the focus manager.
1735 FocusManager
* focus_manager
= widget
->GetFocusManager();
1736 ASSERT_TRUE(focus_manager
);
1738 // Hit the return key and see if it takes effect.
1739 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1740 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1742 // Hit the escape key. Nothing should happen.
1743 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1744 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1745 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1746 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1748 // Now register the escape key and hit it again.
1749 view
->AddAccelerator(escape_accelerator
);
1750 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1751 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1752 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1754 // Remove the return key accelerator.
1755 view
->RemoveAccelerator(return_accelerator
);
1756 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1757 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1758 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1760 // Add it again. Hit the return key and the escape key.
1761 view
->AddAccelerator(return_accelerator
);
1762 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1763 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1764 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1765 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1766 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1767 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1769 // Remove all the accelerators.
1770 view
->ResetAccelerators();
1771 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1772 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1773 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1774 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1775 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1776 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1781 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1782 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1783 TestView
* view
= new TestView();
1785 view
->AddAccelerator(return_accelerator
);
1786 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1788 scoped_ptr
<Widget
> widget(new Widget
);
1789 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1790 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1791 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1792 widget
->Init(params
);
1793 View
* root
= widget
->GetRootView();
1794 root
->AddChildView(view
);
1797 FocusManager
* focus_manager
= widget
->GetFocusManager();
1798 ASSERT_TRUE(focus_manager
);
1800 view
->SetVisible(false);
1801 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1803 view
->SetVisible(true);
1804 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1809 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1810 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1811 TestView
* view
= new TestView();
1813 view
->AddAccelerator(return_accelerator
);
1814 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1816 scoped_ptr
<Widget
> widget(new Widget
);
1817 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1818 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1819 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1820 widget
->Init(params
);
1821 View
* root
= widget
->GetRootView();
1822 root
->AddChildView(view
);
1824 FocusManager
* focus_manager
= widget
->GetFocusManager();
1825 ASSERT_TRUE(focus_manager
);
1827 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1828 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1831 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1832 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1835 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1836 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1841 ////////////////////////////////////////////////////////////////////////////////
1842 // Mouse-wheel message rerouting
1843 ////////////////////////////////////////////////////////////////////////////////
1844 class ScrollableTestView
: public View
{
1846 ScrollableTestView() { }
1848 virtual gfx::Size
GetPreferredSize() {
1849 return gfx::Size(100, 10000);
1852 virtual void Layout() {
1853 SizeToPreferredSize();
1857 class TestViewWithControls
: public View
{
1859 TestViewWithControls() {
1860 text_field_
= new Textfield();
1861 AddChildView(text_field_
);
1864 Textfield
* text_field_
;
1867 class SimpleWidgetDelegate
: public WidgetDelegate
{
1869 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1871 virtual void DeleteDelegate() { delete this; }
1873 virtual View
* GetContentsView() { return contents_
; }
1875 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1876 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1882 // Tests that the mouse-wheel messages are correctly rerouted to the window
1884 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1886 // Note that this fails for a variety of reasons:
1887 // - focused view is apparently reset across window activations and never
1888 // properly restored
1889 // - this test depends on you not having any other window visible open under the
1890 // area that it opens the test windows. --beng
1891 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1892 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1893 Widget
* window1
= Widget::CreateWindowWithBounds(
1894 new SimpleWidgetDelegate(view_with_controls
),
1895 gfx::Rect(0, 0, 100, 100));
1897 ScrollView
* scroll_view
= new ScrollView();
1898 scroll_view
->SetContents(new ScrollableTestView());
1899 Widget
* window2
= Widget::CreateWindowWithBounds(
1900 new SimpleWidgetDelegate(scroll_view
),
1901 gfx::Rect(200, 200, 100, 100));
1903 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1905 // Make the window1 active, as this is what it would be in real-world.
1906 window1
->Activate();
1908 // Let's send a mouse-wheel message to the different controls and check that
1909 // it is rerouted to the window under the mouse (effectively scrolling the
1912 // First to the Window's HWND.
1913 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1914 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1915 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1917 window1
->CloseNow();
1918 window2
->CloseNow();
1922 ////////////////////////////////////////////////////////////////////////////////
1923 // Native view hierachy
1924 ////////////////////////////////////////////////////////////////////////////////
1925 class ToplevelWidgetObserverView
: public View
{
1927 ToplevelWidgetObserverView() : toplevel_(NULL
) {
1929 ~ToplevelWidgetObserverView() override
{}
1932 void ViewHierarchyChanged(
1933 const ViewHierarchyChangedDetails
& details
) override
{
1934 if (details
.is_add
) {
1935 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1940 void NativeViewHierarchyChanged() override
{
1941 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1944 Widget
* toplevel() { return toplevel_
; }
1949 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
1952 // Test that a view can track the current top level widget by overriding
1953 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1954 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
1955 scoped_ptr
<Widget
> toplevel1(new Widget
);
1956 Widget::InitParams toplevel1_params
=
1957 CreateParams(Widget::InitParams::TYPE_POPUP
);
1958 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1959 toplevel1
->Init(toplevel1_params
);
1961 scoped_ptr
<Widget
> toplevel2(new Widget
);
1962 Widget::InitParams toplevel2_params
=
1963 CreateParams(Widget::InitParams::TYPE_POPUP
);
1964 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1965 toplevel2
->Init(toplevel2_params
);
1967 Widget
* child
= new Widget
;
1968 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
1969 child_params
.parent
= toplevel1
->GetNativeView();
1970 child
->Init(child_params
);
1972 ToplevelWidgetObserverView
* observer_view
=
1973 new ToplevelWidgetObserverView();
1974 EXPECT_EQ(NULL
, observer_view
->toplevel());
1976 child
->SetContentsView(observer_view
);
1977 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
1979 Widget::ReparentNativeView(child
->GetNativeView(),
1980 toplevel2
->GetNativeView());
1981 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
1983 observer_view
->parent()->RemoveChildView(observer_view
);
1984 EXPECT_EQ(NULL
, observer_view
->toplevel());
1986 // Make |observer_view| |child|'s contents view again so that it gets deleted
1988 child
->SetContentsView(observer_view
);
1991 ////////////////////////////////////////////////////////////////////////////////
1993 ////////////////////////////////////////////////////////////////////////////////
1995 class TransformPaintView
: public TestView
{
1997 TransformPaintView() {}
1998 ~TransformPaintView() override
{}
2000 void ClearScheduledPaintRect() {
2001 scheduled_paint_rect_
= gfx::Rect();
2004 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
2006 // Overridden from View:
2007 void SchedulePaintInRect(const gfx::Rect
& rect
) override
{
2008 gfx::Rect xrect
= ConvertRectToParent(rect
);
2009 scheduled_paint_rect_
.Union(xrect
);
2013 gfx::Rect scheduled_paint_rect_
;
2015 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
2018 TEST_F(ViewTest
, TransformPaint
) {
2019 TransformPaintView
* v1
= new TransformPaintView();
2020 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2022 TestView
* v2
= new TestView();
2023 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2025 Widget
* widget
= new Widget
;
2026 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2027 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2028 widget
->Init(params
);
2030 View
* root
= widget
->GetRootView();
2032 root
->AddChildView(v1
);
2033 v1
->AddChildView(v2
);
2035 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2036 v1
->ClearScheduledPaintRect();
2037 v2
->SchedulePaint();
2039 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
2041 // Rotate |v1| counter-clockwise.
2042 gfx::Transform transform
;
2043 RotateCounterclockwise(&transform
);
2044 transform
.matrix().set(1, 3, 500.0);
2045 v1
->SetTransform(transform
);
2047 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2049 v1
->ClearScheduledPaintRect();
2050 v2
->SchedulePaint();
2052 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
2057 TEST_F(ViewTest
, TransformEvent
) {
2058 TestView
* v1
= new TestView();
2059 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2061 TestView
* v2
= new TestView();
2062 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2064 Widget
* widget
= new Widget
;
2065 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2066 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2067 widget
->Init(params
);
2068 View
* root
= widget
->GetRootView();
2070 root
->AddChildView(v1
);
2071 v1
->AddChildView(v2
);
2073 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2075 // Rotate |v1| counter-clockwise.
2076 gfx::Transform
transform(v1
->GetTransform());
2077 RotateCounterclockwise(&transform
);
2078 transform
.matrix().set(1, 3, 500.0);
2079 v1
->SetTransform(transform
);
2081 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2085 gfx::Point
p1(110, 210);
2086 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
, ui::EventTimeForNow(),
2087 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2088 root
->OnMousePressed(pressed
);
2089 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2090 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2091 EXPECT_EQ(190, v2
->location_
.x());
2092 EXPECT_EQ(10, v2
->location_
.y());
2094 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
2095 ui::EventTimeForNow(), 0, 0);
2096 root
->OnMouseReleased(released
);
2098 // Now rotate |v2| inside |v1| clockwise.
2099 transform
= v2
->GetTransform();
2100 RotateClockwise(&transform
);
2101 transform
.matrix().set(0, 3, 100.f
);
2102 v2
->SetTransform(transform
);
2104 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
2105 // (300, 400) in |root|.
2110 gfx::Point
point2(110, 320);
2111 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
, ui::EventTimeForNow(),
2112 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2113 root
->OnMousePressed(p2
);
2114 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2115 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2116 EXPECT_EQ(10, v2
->location_
.x());
2117 EXPECT_EQ(20, v2
->location_
.y());
2119 root
->OnMouseReleased(released
);
2121 v1
->SetTransform(gfx::Transform());
2122 v2
->SetTransform(gfx::Transform());
2124 TestView
* v3
= new TestView();
2125 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
2126 v2
->AddChildView(v3
);
2128 // Rotate |v3| clockwise with respect to |v2|.
2129 transform
= v1
->GetTransform();
2130 RotateClockwise(&transform
);
2131 transform
.matrix().set(0, 3, 30.f
);
2132 v3
->SetTransform(transform
);
2134 // Scale |v2| with respect to |v1| along both axis.
2135 transform
= v2
->GetTransform();
2136 transform
.matrix().set(0, 0, 0.8f
);
2137 transform
.matrix().set(1, 1, 0.5f
);
2138 v2
->SetTransform(transform
);
2140 // |v3| occupies (108, 105) to (132, 115) in |root|.
2146 gfx::Point
point(112, 110);
2147 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
, ui::EventTimeForNow(),
2148 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2149 root
->OnMousePressed(p3
);
2151 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2152 EXPECT_EQ(10, v3
->location_
.x());
2153 EXPECT_EQ(25, v3
->location_
.y());
2155 root
->OnMouseReleased(released
);
2157 v1
->SetTransform(gfx::Transform());
2158 v2
->SetTransform(gfx::Transform());
2159 v3
->SetTransform(gfx::Transform());
2165 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
2166 transform
= v3
->GetTransform();
2167 RotateClockwise(&transform
);
2168 transform
.matrix().set(0, 3, 30.f
);
2169 // Rotation sets some scaling transformation. Using SetScale would overwrite
2170 // that and pollute the rotation. So combine the scaling with the existing
2172 gfx::Transform scale
;
2173 scale
.Scale(0.8f
, 0.5f
);
2174 transform
.ConcatTransform(scale
);
2175 v3
->SetTransform(transform
);
2177 // Translate |v2| with respect to |v1|.
2178 transform
= v2
->GetTransform();
2179 transform
.matrix().set(0, 3, 10.f
);
2180 transform
.matrix().set(1, 3, 10.f
);
2181 v2
->SetTransform(transform
);
2183 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2185 gfx::Point
point3(124, 125);
2186 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
, ui::EventTimeForNow(),
2187 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2188 root
->OnMousePressed(p4
);
2190 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2191 EXPECT_EQ(10, v3
->location_
.x());
2192 EXPECT_EQ(25, v3
->location_
.y());
2194 root
->OnMouseReleased(released
);
2199 TEST_F(ViewTest
, TransformVisibleBound
) {
2200 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2202 scoped_ptr
<Widget
> widget(new Widget
);
2203 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2204 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2205 params
.bounds
= viewport_bounds
;
2206 widget
->Init(params
);
2207 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2209 View
* viewport
= new View
;
2210 widget
->SetContentsView(viewport
);
2211 View
* contents
= new View
;
2212 viewport
->AddChildView(contents
);
2213 viewport
->SetBoundsRect(viewport_bounds
);
2214 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2216 View
* child
= new View
;
2217 contents
->AddChildView(child
);
2218 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2219 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
2221 // Rotate |child| counter-clockwise
2222 gfx::Transform transform
;
2223 RotateCounterclockwise(&transform
);
2224 transform
.matrix().set(1, 3, 50.f
);
2225 child
->SetTransform(transform
);
2226 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
2231 ////////////////////////////////////////////////////////////////////////////////
2232 // OnVisibleBoundsChanged()
2234 class VisibleBoundsView
: public View
{
2236 VisibleBoundsView() : received_notification_(false) {}
2237 ~VisibleBoundsView() override
{}
2239 bool received_notification() const { return received_notification_
; }
2240 void set_received_notification(bool received
) {
2241 received_notification_
= received
;
2245 // Overridden from View:
2246 bool GetNeedsNotificationWhenVisibleBoundsChange() const override
{
2249 void OnVisibleBoundsChanged() override
{ received_notification_
= true; }
2251 bool received_notification_
;
2253 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2256 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2257 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2259 scoped_ptr
<Widget
> widget(new Widget
);
2260 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2261 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2262 params
.bounds
= viewport_bounds
;
2263 widget
->Init(params
);
2264 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2266 View
* viewport
= new View
;
2267 widget
->SetContentsView(viewport
);
2268 View
* contents
= new View
;
2269 viewport
->AddChildView(contents
);
2270 viewport
->SetBoundsRect(viewport_bounds
);
2271 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2273 // Create a view that cares about visible bounds notifications, and position
2274 // it just outside the visible bounds of the viewport.
2275 VisibleBoundsView
* child
= new VisibleBoundsView
;
2276 contents
->AddChildView(child
);
2277 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2279 // The child bound should be fully clipped.
2280 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2282 // Now scroll the contents, but not enough to make the child visible.
2283 contents
->SetY(contents
->y() - 1);
2285 // We should have received the notification since the visible bounds may have
2286 // changed (even though they didn't).
2287 EXPECT_TRUE(child
->received_notification());
2288 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2289 child
->set_received_notification(false);
2291 // Now scroll the contents, this time by enough to make the child visible by
2293 contents
->SetY(contents
->y() - 10);
2294 EXPECT_TRUE(child
->received_notification());
2295 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2296 child
->set_received_notification(false);
2301 TEST_F(ViewTest
, SetBoundsPaint
) {
2303 TestView
* child_view
= new TestView
;
2305 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2306 top_view
.scheduled_paint_rects_
.clear();
2307 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2308 top_view
.AddChildView(child_view
);
2310 top_view
.scheduled_paint_rects_
.clear();
2311 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2312 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2314 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2315 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2316 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2317 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2320 // Assertions around painting and focus gain/lost.
2321 TEST_F(ViewTest
, FocusBlurPaints
) {
2322 TestView parent_view
;
2323 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
2325 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2327 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2328 parent_view
.AddChildView(child_view1
);
2330 parent_view
.scheduled_paint_rects_
.clear();
2331 child_view1
->scheduled_paint_rects_
.clear();
2333 // Focus change shouldn't trigger paints.
2334 child_view1
->DoFocus();
2336 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2337 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2339 child_view1
->DoBlur();
2340 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2341 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2344 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2345 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
2348 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2349 view
.InvalidateLayout();
2350 view
.scheduled_paint_rects_
.clear();
2351 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2352 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
2355 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2356 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
2357 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2359 // We have to put the View hierarchy into a Widget or no paints will be
2361 scoped_ptr
<Widget
> widget(new Widget
);
2362 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2363 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2364 params
.bounds
= viewport_bounds
;
2365 widget
->Init(params
);
2366 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2368 TestView
* parent_view
= new TestView
;
2369 widget
->SetContentsView(parent_view
);
2370 parent_view
->SetBoundsRect(viewport_bounds
);
2371 parent_view
->scheduled_paint_rects_
.clear();
2373 View
* child_view
= new View
;
2374 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2375 parent_view
->AddChildView(child_view
);
2376 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2377 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2379 parent_view
->scheduled_paint_rects_
.clear();
2380 parent_view
->RemoveChildView(child_view
);
2381 scoped_ptr
<View
> child_deleter(child_view
);
2382 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2383 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2388 // Tests conversion methods with a transform.
2389 TEST_F(ViewTest
, ConversionsWithTransform
) {
2392 // View hierarchy used to test scale transforms.
2393 TestView
* child
= new TestView
;
2394 TestView
* child_child
= new TestView
;
2396 // View used to test a rotation transform.
2397 TestView
* child_2
= new TestView
;
2399 top_view
.AddChildView(child
);
2400 child
->AddChildView(child_child
);
2402 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2404 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2405 gfx::Transform transform
;
2406 transform
.Scale(3.0, 4.0);
2407 child
->SetTransform(transform
);
2409 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2410 transform
.MakeIdentity();
2411 transform
.Scale(5.0, 7.0);
2412 child_child
->SetTransform(transform
);
2414 top_view
.AddChildView(child_2
);
2415 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2416 transform
.MakeIdentity();
2417 RotateClockwise(&transform
);
2418 child_2
->SetTransform(transform
);
2420 // Sanity check to make sure basic transforms act as expected.
2422 gfx::Transform transform
;
2423 transform
.Translate(110.0, -110.0);
2424 transform
.Scale(100.0, 55.0);
2425 transform
.Translate(1.0, 1.0);
2427 // convert to a 3x3 matrix.
2428 const SkMatrix
& matrix
= transform
.matrix();
2430 EXPECT_EQ(210, matrix
.getTranslateX());
2431 EXPECT_EQ(-55, matrix
.getTranslateY());
2432 EXPECT_EQ(100, matrix
.getScaleX());
2433 EXPECT_EQ(55, matrix
.getScaleY());
2434 EXPECT_EQ(0, matrix
.getSkewX());
2435 EXPECT_EQ(0, matrix
.getSkewY());
2439 gfx::Transform transform
;
2440 transform
.Translate(1.0, 1.0);
2442 t2
.Scale(100.0, 55.0);
2444 t3
.Translate(110.0, -110.0);
2445 transform
.ConcatTransform(t2
);
2446 transform
.ConcatTransform(t3
);
2448 // convert to a 3x3 matrix
2449 const SkMatrix
& matrix
= transform
.matrix();
2451 EXPECT_EQ(210, matrix
.getTranslateX());
2452 EXPECT_EQ(-55, matrix
.getTranslateY());
2453 EXPECT_EQ(100, matrix
.getScaleX());
2454 EXPECT_EQ(55, matrix
.getScaleY());
2455 EXPECT_EQ(0, matrix
.getSkewX());
2456 EXPECT_EQ(0, matrix
.getSkewY());
2459 // Conversions from child->top and top->child.
2461 gfx::Point
point(5, 5);
2462 View::ConvertPointToTarget(child
, &top_view
, &point
);
2463 EXPECT_EQ(22, point
.x());
2464 EXPECT_EQ(39, point
.y());
2466 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2467 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2468 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2469 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2470 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2471 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2473 point
.SetPoint(22, 39);
2474 View::ConvertPointToTarget(&top_view
, child
, &point
);
2475 EXPECT_EQ(5, point
.x());
2476 EXPECT_EQ(5, point
.y());
2478 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2479 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2480 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2481 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2482 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2483 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2486 // Conversions from child_child->top and top->child_child.
2488 gfx::Point
point(5, 5);
2489 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2490 EXPECT_EQ(133, point
.x());
2491 EXPECT_EQ(211, point
.y());
2493 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2494 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2495 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2496 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2497 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2498 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2500 point
.SetPoint(133, 211);
2501 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2502 EXPECT_EQ(5, point
.x());
2503 EXPECT_EQ(5, point
.y());
2505 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2506 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2507 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2508 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2509 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2510 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2513 // Conversions from child_child->child and child->child_child
2515 gfx::Point
point(5, 5);
2516 View::ConvertPointToTarget(child_child
, child
, &point
);
2517 EXPECT_EQ(42, point
.x());
2518 EXPECT_EQ(48, point
.y());
2520 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2521 View::ConvertRectToTarget(child_child
, child
, &rect
);
2522 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2523 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2524 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2525 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2527 point
.SetPoint(42, 48);
2528 View::ConvertPointToTarget(child
, child_child
, &point
);
2529 EXPECT_EQ(5, point
.x());
2530 EXPECT_EQ(5, point
.y());
2532 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2533 View::ConvertRectToTarget(child
, child_child
, &rect
);
2534 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2535 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2536 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2537 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2540 // Conversions from top_view to child with a value that should be negative.
2541 // This ensures we don't round up with negative numbers.
2543 gfx::Point
point(6, 18);
2544 View::ConvertPointToTarget(&top_view
, child
, &point
);
2545 EXPECT_EQ(-1, point
.x());
2546 EXPECT_EQ(-1, point
.y());
2548 float error
= 0.01f
;
2549 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2550 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2551 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2552 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2553 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2554 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2557 // Rect conversions from top_view->child_2 and child_2->top_view.
2559 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2560 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2561 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2562 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2563 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2564 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2566 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2567 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2568 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2569 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2570 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2571 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2575 // Tests conversion methods to and from screen coordinates.
2576 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2577 scoped_ptr
<Widget
> widget(new Widget
);
2578 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2579 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2580 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2581 widget
->Init(params
);
2583 View
* child
= new View
;
2584 widget
->GetRootView()->AddChildView(child
);
2585 child
->SetBounds(10, 10, 100, 200);
2588 child
->SetTransform(t
);
2590 gfx::Point
point_in_screen(100, 90);
2591 gfx::Point
point_in_child(80, 60);
2593 gfx::Point point
= point_in_screen
;
2594 View::ConvertPointFromScreen(child
, &point
);
2595 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2597 View::ConvertPointToScreen(child
, &point
);
2598 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2601 // Tests conversion methods for rectangles.
2602 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2603 scoped_ptr
<Widget
> widget(new Widget
);
2604 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2605 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2606 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2607 widget
->Init(params
);
2608 View
* root
= widget
->GetRootView();
2610 TestView
* v1
= new TestView
;
2611 TestView
* v2
= new TestView
;
2612 root
->AddChildView(v1
);
2613 v1
->AddChildView(v2
);
2615 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2616 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2618 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2619 gfx::Rect
rect(5, 5, 15, 40);
2620 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2621 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2625 RotateCounterclockwise(&t2
);
2626 t2
.matrix().set(1, 3, 100.f
);
2627 v2
->SetTransform(t2
);
2629 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2630 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2631 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2636 v1
->SetTransform(t1
);
2638 // The rectangle should remain the same for |v1|.
2639 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2641 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2642 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2643 v2
->ConvertRectToWidget(rect
).ToString());
2648 class ObserverView
: public View
{
2651 ~ObserverView() override
;
2653 void ResetTestState();
2655 bool has_add_details() const { return has_add_details_
; }
2656 bool has_remove_details() const { return has_remove_details_
; }
2658 const ViewHierarchyChangedDetails
& add_details() const {
2659 return add_details_
;
2662 const ViewHierarchyChangedDetails
& remove_details() const {
2663 return remove_details_
;
2668 void ViewHierarchyChanged(
2669 const ViewHierarchyChangedDetails
& details
) override
;
2671 bool has_add_details_
;
2672 bool has_remove_details_
;
2673 ViewHierarchyChangedDetails add_details_
;
2674 ViewHierarchyChangedDetails remove_details_
;
2676 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2679 ObserverView::ObserverView()
2680 : has_add_details_(false),
2681 has_remove_details_(false) {
2684 ObserverView::~ObserverView() {}
2686 void ObserverView::ResetTestState() {
2687 has_add_details_
= false;
2688 has_remove_details_
= false;
2689 add_details_
= ViewHierarchyChangedDetails();
2690 remove_details_
= ViewHierarchyChangedDetails();
2693 void ObserverView::ViewHierarchyChanged(
2694 const ViewHierarchyChangedDetails
& details
) {
2695 if (details
.is_add
) {
2696 has_add_details_
= true;
2697 add_details_
= details
;
2699 has_remove_details_
= true;
2700 remove_details_
= details
;
2704 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2705 // a child view is added or removed to all the views in the hierarchy (up and
2707 // The tree looks like this:
2711 // +-- v4 (starts here, then get reparented to v1)
2712 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2715 ObserverView
* v3
= new ObserverView();
2717 // Add |v3| to |v2|.
2718 scoped_ptr
<ObserverView
> v2(new ObserverView());
2719 v2
->AddChildView(v3
);
2721 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2723 EXPECT_TRUE(v2
->has_add_details());
2724 EXPECT_FALSE(v2
->has_remove_details());
2725 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2726 EXPECT_EQ(v3
, v2
->add_details().child
);
2727 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2729 EXPECT_TRUE(v3
->has_add_details());
2730 EXPECT_FALSE(v3
->has_remove_details());
2731 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2732 EXPECT_EQ(v3
, v3
->add_details().child
);
2733 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2735 // Reset everything to the initial state.
2736 v2
->ResetTestState();
2737 v3
->ResetTestState();
2740 v1
.AddChildView(v2
.get());
2742 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2743 // Make sure all the views (v1, v2, v3) received _that_ information.
2744 EXPECT_TRUE(v1
.has_add_details());
2745 EXPECT_FALSE(v1
.has_remove_details());
2746 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2747 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2748 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2750 EXPECT_TRUE(v2
->has_add_details());
2751 EXPECT_FALSE(v2
->has_remove_details());
2752 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2753 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2754 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2756 EXPECT_TRUE(v3
->has_add_details());
2757 EXPECT_FALSE(v3
->has_remove_details());
2758 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2759 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2760 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2762 // Reset everything to the initial state.
2763 v1
.ResetTestState();
2764 v2
->ResetTestState();
2765 v3
->ResetTestState();
2767 // Remove |v2| from |v1|.
2768 v1
.RemoveChildView(v2
.get());
2770 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2771 // Make sure all the views (v1, v2, v3) received _that_ information.
2772 EXPECT_FALSE(v1
.has_add_details());
2773 EXPECT_TRUE(v1
.has_remove_details());
2774 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2775 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2776 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2778 EXPECT_FALSE(v2
->has_add_details());
2779 EXPECT_TRUE(v2
->has_remove_details());
2780 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2781 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2782 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2784 EXPECT_FALSE(v3
->has_add_details());
2785 EXPECT_TRUE(v3
->has_remove_details());
2786 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2787 EXPECT_EQ(v3
, v3
->remove_details().child
);
2788 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2790 // Verifies notifications when reparenting a view.
2791 ObserverView
* v4
= new ObserverView();
2792 // Add |v4| to |v2|.
2793 v2
->AddChildView(v4
);
2795 // Reset everything to the initial state.
2796 v1
.ResetTestState();
2797 v2
->ResetTestState();
2798 v3
->ResetTestState();
2799 v4
->ResetTestState();
2801 // Reparent |v4| to |v1|.
2802 v1
.AddChildView(v4
);
2804 // Verifies that all views receive the correct information for all the child,
2805 // parent and move views.
2807 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2808 EXPECT_TRUE(v1
.has_add_details());
2809 EXPECT_FALSE(v1
.has_remove_details());
2810 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2811 EXPECT_EQ(v4
, v1
.add_details().child
);
2812 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2814 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2816 EXPECT_FALSE(v2
->has_add_details());
2817 EXPECT_TRUE(v2
->has_remove_details());
2818 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2819 EXPECT_EQ(v4
, v2
->remove_details().child
);
2820 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2822 // |v3| is not impacted by this operation, and hence receives no notification.
2823 EXPECT_FALSE(v3
->has_add_details());
2824 EXPECT_FALSE(v3
->has_remove_details());
2826 // |v4| is the reparented child, so it receives notifications for the remove
2827 // and then the add. |v2| is its old parent, |v1| is its new parent.
2828 EXPECT_TRUE(v4
->has_remove_details());
2829 EXPECT_TRUE(v4
->has_add_details());
2830 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2831 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2832 EXPECT_EQ(v4
, v4
->add_details().child
);
2833 EXPECT_EQ(v4
, v4
->remove_details().child
);
2834 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2835 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2838 // Verifies if the child views added under the root are all deleted when calling
2839 // RemoveAllChildViews.
2840 // The tree looks like this:
2849 TEST_F(ViewTest
, RemoveAllChildViews
) {
2852 View
* child1
= new View
;
2853 root
.AddChildView(child1
);
2855 for (int i
= 0; i
< 2; ++i
)
2856 root
.AddChildView(new View
);
2858 View
* foo
= new View
;
2859 child1
->AddChildView(foo
);
2861 // Add some nodes to |foo|.
2862 for (int i
= 0; i
< 3; ++i
)
2863 foo
->AddChildView(new View
);
2865 EXPECT_EQ(3, root
.child_count());
2866 EXPECT_EQ(1, child1
->child_count());
2867 EXPECT_EQ(3, foo
->child_count());
2869 // Now remove all child views from root.
2870 root
.RemoveAllChildViews(true);
2872 EXPECT_EQ(0, root
.child_count());
2873 EXPECT_FALSE(root
.has_children());
2876 TEST_F(ViewTest
, Contains
) {
2878 View
* v2
= new View
;
2879 View
* v3
= new View
;
2881 v1
.AddChildView(v2
);
2882 v2
->AddChildView(v3
);
2884 EXPECT_FALSE(v1
.Contains(NULL
));
2885 EXPECT_TRUE(v1
.Contains(&v1
));
2886 EXPECT_TRUE(v1
.Contains(v2
));
2887 EXPECT_TRUE(v1
.Contains(v3
));
2889 EXPECT_FALSE(v2
->Contains(NULL
));
2890 EXPECT_TRUE(v2
->Contains(v2
));
2891 EXPECT_FALSE(v2
->Contains(&v1
));
2892 EXPECT_TRUE(v2
->Contains(v3
));
2894 EXPECT_FALSE(v3
->Contains(NULL
));
2895 EXPECT_TRUE(v3
->Contains(v3
));
2896 EXPECT_FALSE(v3
->Contains(&v1
));
2897 EXPECT_FALSE(v3
->Contains(v2
));
2900 // Verifies if GetIndexOf() returns the correct index for the specified child
2902 // The tree looks like this:
2907 TEST_F(ViewTest
, GetIndexOf
) {
2910 View
* child1
= new View
;
2911 root
.AddChildView(child1
);
2913 View
* child2
= new View
;
2914 root
.AddChildView(child2
);
2916 View
* foo1
= new View
;
2917 child1
->AddChildView(foo1
);
2919 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2920 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2921 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2922 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2923 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2925 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2926 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2927 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2928 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2929 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2931 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2932 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2933 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2934 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2935 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2938 // Verifies that the child views can be reordered correctly.
2939 TEST_F(ViewTest
, ReorderChildren
) {
2942 View
* child
= new View();
2943 root
.AddChildView(child
);
2945 View
* foo1
= new View();
2946 child
->AddChildView(foo1
);
2947 View
* foo2
= new View();
2948 child
->AddChildView(foo2
);
2949 View
* foo3
= new View();
2950 child
->AddChildView(foo3
);
2951 foo1
->SetFocusable(true);
2952 foo2
->SetFocusable(true);
2953 foo3
->SetFocusable(true);
2955 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2956 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2957 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2958 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2959 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2960 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2962 // Move |foo2| at the end.
2963 child
->ReorderChildView(foo2
, -1);
2964 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2965 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2966 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2967 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2968 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2969 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2971 // Move |foo1| at the end.
2972 child
->ReorderChildView(foo1
, -1);
2973 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2974 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2975 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2976 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2977 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2978 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2979 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2981 // Move |foo2| to the front.
2982 child
->ReorderChildView(foo2
, 0);
2983 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2984 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2985 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2986 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2987 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2988 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2989 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2992 // Verifies that GetViewByID returns the correctly child view from the specified
2994 // The tree looks like this:
2999 TEST_F(ViewTest
, GetViewByID
) {
3001 const int kV1ID
= 1;
3005 const int kV2ID
= 2;
3009 const int kV3ID
= 3;
3013 const int kV4ID
= 4;
3016 const int kV5ID
= 5;
3018 v1
.AddChildView(&v2
);
3019 v2
.AddChildView(&v3
);
3020 v2
.AddChildView(&v4
);
3022 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
3023 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
3024 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
3026 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
3027 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
3029 const int kGroup
= 1;
3030 v3
.SetGroup(kGroup
);
3031 v4
.SetGroup(kGroup
);
3034 v1
.GetViewsInGroup(kGroup
, &views
);
3035 EXPECT_EQ(2U, views
.size());
3037 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
3038 EXPECT_NE(views
.end(), i
);
3040 i
= std::find(views
.begin(), views
.end(), &v4
);
3041 EXPECT_NE(views
.end(), i
);
3044 TEST_F(ViewTest
, AddExistingChild
) {
3047 v1
.AddChildView(&v2
);
3048 v1
.AddChildView(&v3
);
3049 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3050 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3052 // Check that there's no change in order when adding at same index.
3053 v1
.AddChildViewAt(&v2
, 0);
3054 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3055 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3056 v1
.AddChildViewAt(&v3
, 1);
3057 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3058 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3060 // Add it at a different index and check for change in order.
3061 v1
.AddChildViewAt(&v2
, 1);
3062 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
3063 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
3064 v1
.AddChildViewAt(&v2
, 0);
3065 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3066 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3068 // Check that calling |AddChildView()| does not change the order.
3069 v1
.AddChildView(&v2
);
3070 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3071 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3072 v1
.AddChildView(&v3
);
3073 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3074 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3077 ////////////////////////////////////////////////////////////////////////////////
3079 ////////////////////////////////////////////////////////////////////////////////
3081 // A widget that always claims to be active, regardless of its real activation
3083 class ActiveWidget
: public Widget
{
3086 ~ActiveWidget() override
{}
3088 bool IsActive() const override
{ return true; }
3091 DISALLOW_COPY_AND_ASSIGN(ActiveWidget
);
3094 TEST_F(ViewTest
, AdvanceFocusIfNecessaryForUnfocusableView
) {
3095 // Create a widget with two views and give the first one focus.
3096 ActiveWidget widget
;
3097 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3098 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3099 widget
.Init(params
);
3101 View
* view1
= new View();
3102 view1
->SetFocusable(true);
3103 widget
.GetRootView()->AddChildView(view1
);
3104 View
* view2
= new View();
3105 view2
->SetFocusable(true);
3106 widget
.GetRootView()->AddChildView(view2
);
3108 FocusManager
* focus_manager
= widget
.GetFocusManager();
3109 ASSERT_TRUE(focus_manager
);
3111 focus_manager
->SetFocusedView(view1
);
3112 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
3114 // Disable the focused view and check if the next view gets focused.
3115 view1
->SetEnabled(false);
3116 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
3118 // Re-enable and re-focus.
3119 view1
->SetEnabled(true);
3120 focus_manager
->SetFocusedView(view1
);
3121 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
3123 // Hide the focused view and check it the next view gets focused.
3124 view1
->SetVisible(false);
3125 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
3127 // Re-show and re-focus.
3128 view1
->SetVisible(true);
3129 focus_manager
->SetFocusedView(view1
);
3130 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
3132 // Set the focused view as not focusable and check if the next view gets
3134 view1
->SetFocusable(false);
3135 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
3138 ////////////////////////////////////////////////////////////////////////////////
3140 ////////////////////////////////////////////////////////////////////////////////
3144 // Test implementation of LayerAnimator.
3145 class TestLayerAnimator
: public ui::LayerAnimator
{
3147 TestLayerAnimator();
3149 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
3152 void SetBounds(const gfx::Rect
& bounds
) override
;
3155 ~TestLayerAnimator() override
{}
3158 gfx::Rect last_bounds_
;
3160 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
3163 TestLayerAnimator::TestLayerAnimator()
3164 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
3167 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
3168 last_bounds_
= bounds
;
3173 class ViewLayerTest
: public ViewsTestBase
{
3175 ViewLayerTest() : widget_(NULL
) {}
3177 ~ViewLayerTest() override
{}
3179 // Returns the Layer used by the RootView.
3180 ui::Layer
* GetRootLayer() {
3181 return widget()->GetLayer();
3184 void SetUp() override
{
3186 widget_
= new Widget
;
3187 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3188 params
.bounds
= gfx::Rect(50, 50, 200, 200);
3189 widget_
->Init(params
);
3191 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
3194 void TearDown() override
{
3195 widget_
->CloseNow();
3196 ViewsTestBase::TearDown();
3199 Widget
* widget() { return widget_
; }
3206 TEST_F(ViewLayerTest
, LayerToggling
) {
3207 // Because we lazily create textures the calls to DrawTree are necessary to
3208 // ensure we trigger creation of textures.
3209 ui::Layer
* root_layer
= widget()->GetLayer();
3210 View
* content_view
= new View
;
3211 widget()->SetContentsView(content_view
);
3213 // Create v1, give it a bounds and verify everything is set up correctly.
3214 View
* v1
= new View
;
3215 v1
->SetPaintToLayer(true);
3216 EXPECT_TRUE(v1
->layer() != NULL
);
3217 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3218 content_view
->AddChildView(v1
);
3219 ASSERT_TRUE(v1
->layer() != NULL
);
3220 EXPECT_EQ(root_layer
, v1
->layer()->parent());
3221 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
3223 // Create v2 as a child of v1 and do basic assertion testing.
3224 View
* v2
= new View
;
3225 v1
->AddChildView(v2
);
3226 EXPECT_TRUE(v2
->layer() == NULL
);
3227 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
3228 v2
->SetPaintToLayer(true);
3229 ASSERT_TRUE(v2
->layer() != NULL
);
3230 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3231 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3233 // Turn off v1s layer. v2 should still have a layer but its parent should have
3235 v1
->SetPaintToLayer(false);
3236 EXPECT_TRUE(v1
->layer() == NULL
);
3237 EXPECT_TRUE(v2
->layer() != NULL
);
3238 EXPECT_EQ(root_layer
, v2
->layer()->parent());
3239 ASSERT_EQ(1u, root_layer
->children().size());
3240 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
3241 // The bounds of the layer should have changed to be relative to the root view
3243 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
3245 // Make v1 have a layer again and verify v2s layer is wired up correctly.
3246 gfx::Transform transform
;
3247 transform
.Scale(2.0, 2.0);
3248 v1
->SetTransform(transform
);
3249 EXPECT_TRUE(v1
->layer() != NULL
);
3250 EXPECT_TRUE(v2
->layer() != NULL
);
3251 EXPECT_EQ(root_layer
, v1
->layer()->parent());
3252 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3253 ASSERT_EQ(1u, root_layer
->children().size());
3254 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
3255 ASSERT_EQ(1u, v1
->layer()->children().size());
3256 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
3257 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3260 // Verifies turning on a layer wires up children correctly.
3261 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
3262 View
* content_view
= new View
;
3263 widget()->SetContentsView(content_view
);
3265 // Create v1, give it a bounds and verify everything is set up correctly.
3266 View
* v1
= new View
;
3267 content_view
->AddChildView(v1
);
3268 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3270 View
* v2
= new View
;
3271 v1
->AddChildView(v2
);
3273 View
* v3
= new View
;
3274 v3
->SetPaintToLayer(true);
3275 v2
->AddChildView(v3
);
3276 ASSERT_TRUE(v3
->layer() != NULL
);
3278 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
3280 v1
->SetPaintToLayer(true);
3281 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
3284 TEST_F(ViewLayerTest
, LayerAnimator
) {
3285 View
* content_view
= new View
;
3286 widget()->SetContentsView(content_view
);
3288 View
* v1
= new View
;
3289 content_view
->AddChildView(v1
);
3290 v1
->SetPaintToLayer(true);
3291 EXPECT_TRUE(v1
->layer() != NULL
);
3293 TestLayerAnimator
* animator
= new TestLayerAnimator();
3294 v1
->layer()->SetAnimator(animator
);
3296 gfx::Rect
bounds(1, 2, 3, 4);
3297 v1
->SetBoundsRect(bounds
);
3298 EXPECT_EQ(bounds
, animator
->last_bounds());
3299 // TestLayerAnimator doesn't update the layer.
3300 EXPECT_NE(bounds
, v1
->layer()->bounds());
3303 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3304 // doesn't have a layer change.
3305 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
3306 View
* content_view
= new View
;
3307 widget()->SetContentsView(content_view
);
3309 View
* v1
= new View
;
3310 content_view
->AddChildView(v1
);
3311 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3313 View
* v2
= new View
;
3314 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3315 v1
->AddChildView(v2
);
3316 v2
->SetPaintToLayer(true);
3317 ASSERT_TRUE(v2
->layer() != NULL
);
3318 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
3320 v1
->SetPosition(gfx::Point(25, 36));
3321 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
3323 v2
->SetPosition(gfx::Point(11, 12));
3324 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
3326 // Bounds of the layer should change even if the view is not invisible.
3327 v1
->SetVisible(false);
3328 v1
->SetPosition(gfx::Point(20, 30));
3329 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
3331 v2
->SetVisible(false);
3332 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3333 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
3336 // Make sure layers are positioned correctly in RTL.
3337 TEST_F(ViewLayerTest
, BoundInRTL
) {
3340 View
* view
= new View
;
3341 widget()->SetContentsView(view
);
3343 int content_width
= view
->width();
3345 // |v1| is initially not attached to anything. So its layer will have the same
3346 // bounds as the view.
3347 View
* v1
= new View
;
3348 v1
->SetPaintToLayer(true);
3349 v1
->SetBounds(10, 10, 20, 10);
3350 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3351 v1
->layer()->bounds());
3353 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3355 view
->AddChildView(v1
);
3356 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3357 v1
->layer()->bounds());
3358 gfx::Rect l1bounds
= v1
->layer()->bounds();
3360 // Now attach a View to the widget first, then create a layer for it. Make
3361 // sure the bounds are correct.
3362 View
* v2
= new View
;
3363 v2
->SetBounds(50, 10, 30, 10);
3364 EXPECT_FALSE(v2
->layer());
3365 view
->AddChildView(v2
);
3366 v2
->SetPaintToLayer(true);
3367 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
3368 v2
->layer()->bounds());
3369 gfx::Rect l2bounds
= v2
->layer()->bounds();
3371 view
->SetPaintToLayer(true);
3372 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3373 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3375 // Move one of the views. Make sure the layer is positioned correctly
3377 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
3378 l1bounds
.set_x(l1bounds
.x() + 5);
3379 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3381 view
->SetPaintToLayer(false);
3382 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3383 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3385 // Move a view again.
3386 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
3387 l2bounds
.set_x(l2bounds
.x() - 5);
3388 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3391 // Make sure that resizing a parent in RTL correctly repositions its children.
3392 TEST_F(ViewLayerTest
, ResizeParentInRTL
) {
3395 View
* view
= new View
;
3396 widget()->SetContentsView(view
);
3398 int content_width
= view
->width();
3400 // Create a paints-to-layer view |v1|.
3401 View
* v1
= new View
;
3402 v1
->SetPaintToLayer(true);
3403 v1
->SetBounds(10, 10, 20, 10);
3404 view
->AddChildView(v1
);
3405 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3406 v1
->layer()->bounds());
3408 // Attach a paints-to-layer child view to |v1|.
3409 View
* v2
= new View
;
3410 v2
->SetPaintToLayer(true);
3411 v2
->SetBounds(3, 5, 6, 4);
3412 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
3413 v2
->layer()->bounds());
3414 v1
->AddChildView(v2
);
3415 // Check that |v2| now has RTL-appropriate bounds.
3416 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3417 v2
->layer()->bounds());
3419 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
3420 View
* v3
= new View
;
3421 v3
->SetBounds(1, 1, 18, 8);
3422 View
* v4
= new View
;
3423 v4
->SetPaintToLayer(true);
3424 v4
->SetBounds(2, 4, 6, 4);
3425 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
3426 v4
->layer()->bounds());
3427 v3
->AddChildView(v4
);
3428 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
3429 v4
->layer()->bounds());
3430 v1
->AddChildView(v3
);
3431 // Check that |v4| now has RTL-appropriate bounds.
3432 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3433 v4
->layer()->bounds());
3435 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3436 // correctly to RTL-appropriate bounds.
3437 v1
->SetSize(gfx::Size(30, 10));
3438 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3439 v2
->layer()->bounds());
3440 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3441 v4
->layer()->bounds());
3443 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3444 // to RTL-appropriate bounds.
3445 v3
->SetBounds(2, 1, 12, 8);
3446 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3447 v4
->layer()->bounds());
3450 // Makes sure a transform persists after toggling the visibility.
3451 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3452 View
* view
= new View
;
3453 gfx::Transform transform
;
3454 transform
.Scale(2.0, 2.0);
3455 view
->SetTransform(transform
);
3456 widget()->SetContentsView(view
);
3457 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3459 view
->SetVisible(false);
3460 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3462 view
->SetVisible(true);
3463 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3466 // Verifies a transform persists after removing/adding a view with a transform.
3467 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3468 View
* view
= new View
;
3469 gfx::Transform transform
;
3470 transform
.Scale(2.0, 2.0);
3471 view
->SetTransform(transform
);
3472 widget()->SetContentsView(view
);
3473 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3474 ASSERT_TRUE(view
->layer() != NULL
);
3475 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3477 View
* parent
= view
->parent();
3478 parent
->RemoveChildView(view
);
3479 parent
->AddChildView(view
);
3481 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3482 ASSERT_TRUE(view
->layer() != NULL
);
3483 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3486 // Makes sure that layer visibility is correct after toggling View visibility.
3487 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3488 View
* content_view
= new View
;
3489 widget()->SetContentsView(content_view
);
3491 // The view isn't attached to a widget or a parent view yet. But it should
3492 // still have a layer, but the layer should not be attached to the root
3494 View
* v1
= new View
;
3495 v1
->SetPaintToLayer(true);
3496 EXPECT_TRUE(v1
->layer());
3497 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3500 // Once the view is attached to a widget, its layer should be attached to the
3501 // root layer and visible.
3502 content_view
->AddChildView(v1
);
3503 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3505 EXPECT_TRUE(v1
->layer()->IsDrawn());
3507 v1
->SetVisible(false);
3508 EXPECT_FALSE(v1
->layer()->IsDrawn());
3510 v1
->SetVisible(true);
3511 EXPECT_TRUE(v1
->layer()->IsDrawn());
3514 EXPECT_FALSE(v1
->layer()->IsDrawn());
3517 EXPECT_TRUE(v1
->layer()->IsDrawn());
3520 // Tests that the layers in the subtree are orphaned after a View is removed
3522 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3523 View
* content_view
= new View
;
3524 widget()->SetContentsView(content_view
);
3526 View
* v1
= new View
;
3527 content_view
->AddChildView(v1
);
3529 View
* v2
= new View
;
3530 v1
->AddChildView(v2
);
3531 v2
->SetPaintToLayer(true);
3532 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3534 EXPECT_TRUE(v2
->layer()->IsDrawn());
3536 content_view
->RemoveChildView(v1
);
3538 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3542 content_view
->AddChildView(v2
);
3545 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3547 EXPECT_TRUE(v2
->layer()->IsDrawn());
3550 class PaintTrackingView
: public View
{
3552 PaintTrackingView() : painted_(false) {
3555 bool painted() const { return painted_
; }
3556 void set_painted(bool value
) { painted_
= value
; }
3558 void OnPaint(gfx::Canvas
* canvas
) override
{ painted_
= true; }
3563 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3566 // Makes sure child views with layers aren't painted when paint starts at an
3568 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3569 PaintTrackingView
* content_view
= new PaintTrackingView
;
3570 widget()->SetContentsView(content_view
);
3571 content_view
->SetPaintToLayer(true);
3572 GetRootLayer()->GetCompositor()->ScheduleDraw();
3573 ui::DrawWaiterForTest::WaitForCompositingEnded(
3574 GetRootLayer()->GetCompositor());
3575 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3576 content_view
->set_painted(false);
3577 // content_view no longer has a dirty rect. Paint from the root and make sure
3578 // PaintTrackingView isn't painted.
3579 GetRootLayer()->GetCompositor()->ScheduleDraw();
3580 ui::DrawWaiterForTest::WaitForCompositingEnded(
3581 GetRootLayer()->GetCompositor());
3582 EXPECT_FALSE(content_view
->painted());
3584 // Make content_view have a dirty rect, paint the layers and make sure
3585 // PaintTrackingView is painted.
3586 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3587 GetRootLayer()->GetCompositor()->ScheduleDraw();
3588 ui::DrawWaiterForTest::WaitForCompositingEnded(
3589 GetRootLayer()->GetCompositor());
3590 EXPECT_TRUE(content_view
->painted());
3593 // Tests that the visibility of child layers are updated correctly when a View's
3594 // visibility changes.
3595 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3596 View
* v1
= new View
;
3597 v1
->SetPaintToLayer(true);
3598 widget()->SetContentsView(v1
);
3600 View
* v2
= new View
;
3601 v1
->AddChildView(v2
);
3603 View
* v3
= new View
;
3604 v2
->AddChildView(v3
);
3605 v3
->SetVisible(false);
3607 View
* v4
= new View
;
3608 v4
->SetPaintToLayer(true);
3609 v3
->AddChildView(v4
);
3611 EXPECT_TRUE(v1
->layer()->IsDrawn());
3612 EXPECT_FALSE(v4
->layer()->IsDrawn());
3614 v2
->SetVisible(false);
3615 EXPECT_TRUE(v1
->layer()->IsDrawn());
3616 EXPECT_FALSE(v4
->layer()->IsDrawn());
3618 v2
->SetVisible(true);
3619 EXPECT_TRUE(v1
->layer()->IsDrawn());
3620 EXPECT_FALSE(v4
->layer()->IsDrawn());
3622 v2
->SetVisible(false);
3623 EXPECT_TRUE(v1
->layer()->IsDrawn());
3624 EXPECT_FALSE(v4
->layer()->IsDrawn());
3625 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3627 v3
->SetVisible(true);
3628 EXPECT_TRUE(v1
->layer()->IsDrawn());
3629 EXPECT_FALSE(v4
->layer()->IsDrawn());
3630 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3632 // Reparent |v3| to |v1|.
3633 v1
->AddChildView(v3
);
3634 EXPECT_TRUE(v1
->layer()->IsDrawn());
3635 EXPECT_TRUE(v4
->layer()->IsDrawn());
3636 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3639 // This test creates a random View tree, and then randomly reorders child views,
3640 // reparents views etc. Unrelated changes can appear to break this test. So
3641 // marking this as FLAKY.
3642 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3643 View
* content
= new View
;
3644 content
->SetPaintToLayer(true);
3645 widget()->SetContentsView(content
);
3648 ConstructTree(content
, 5);
3649 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3651 ScrambleTree(content
);
3652 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3654 ScrambleTree(content
);
3655 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3657 ScrambleTree(content
);
3658 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3661 // Verifies when views are reordered the layer is also reordered. The widget is
3662 // providing the parent layer.
3663 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3664 View
* content
= new View
;
3665 widget()->SetContentsView(content
);
3666 View
* c1
= new View
;
3667 c1
->SetPaintToLayer(true);
3668 content
->AddChildView(c1
);
3669 View
* c2
= new View
;
3670 c2
->SetPaintToLayer(true);
3671 content
->AddChildView(c2
);
3673 ui::Layer
* parent_layer
= c1
->layer()->parent();
3674 ASSERT_TRUE(parent_layer
);
3675 ASSERT_EQ(2u, parent_layer
->children().size());
3676 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3677 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3679 // Move c1 to the front. The layers should have moved too.
3680 content
->ReorderChildView(c1
, -1);
3681 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3682 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3685 // Verifies that the layer of a view can be acquired properly.
3686 TEST_F(ViewLayerTest
, AcquireLayer
) {
3687 View
* content
= new View
;
3688 widget()->SetContentsView(content
);
3689 scoped_ptr
<View
> c1(new View
);
3690 c1
->SetPaintToLayer(true);
3691 EXPECT_TRUE(c1
->layer());
3692 content
->AddChildView(c1
.get());
3694 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3695 EXPECT_EQ(layer
.get(), c1
->layer());
3697 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3698 EXPECT_NE(c1
->layer(), layer2
.get());
3700 // Destroy view before destroying layer.
3704 // Verify the z-order of the layers as a result of calling RecreateLayer().
3705 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3706 scoped_ptr
<View
> v(new View());
3707 v
->SetPaintToLayer(true);
3709 View
* v1
= new View();
3710 v1
->SetPaintToLayer(true);
3711 v
->AddChildView(v1
);
3712 View
* v2
= new View();
3713 v2
->SetPaintToLayer(true);
3714 v
->AddChildView(v2
);
3716 // Test the initial z-order.
3717 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3718 ASSERT_EQ(2u, child_layers_pre
.size());
3719 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3720 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3722 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3724 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3725 // for |v1| and |v2|.
3726 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3727 ASSERT_EQ(3u, child_layers_post
.size());
3728 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3729 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3730 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3733 // Verify the z-order of the layers as a result of calling RecreateLayer when
3734 // the widget is the parent with the layer.
3735 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3736 View
* v
= new View();
3737 widget()->SetContentsView(v
);
3739 View
* v1
= new View();
3740 v1
->SetPaintToLayer(true);
3741 v
->AddChildView(v1
);
3742 View
* v2
= new View();
3743 v2
->SetPaintToLayer(true);
3744 v
->AddChildView(v2
);
3746 ui::Layer
* root_layer
= GetRootLayer();
3748 // Test the initial z-order.
3749 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3750 ASSERT_EQ(2u, child_layers_pre
.size());
3751 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3752 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3754 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3756 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3757 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3758 ASSERT_EQ(3u, child_layers_post
.size());
3759 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3760 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3761 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3764 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3766 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3768 v
.SetPaintToLayer(true);
3770 child
.SetPaintToLayer(true);
3771 v
.AddChildView(&child
);
3772 ASSERT_TRUE(v
.layer() != NULL
);
3773 ASSERT_EQ(1u, v
.layer()->children().size());
3774 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3776 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3777 v
.layer()->Add(&layer
);
3778 v
.layer()->StackAtBottom(&layer
);
3780 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3782 // All children should be moved from old layer to new layer.
3783 ASSERT_TRUE(old_layer
.get() != NULL
);
3784 EXPECT_TRUE(old_layer
->children().empty());
3786 // And new layer should have the two children.
3787 ASSERT_TRUE(v
.layer() != NULL
);
3788 ASSERT_EQ(2u, v
.layer()->children().size());
3789 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3790 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3795 std::string
ToString(const gfx::Vector2dF
& vector
) {
3796 return base::StringPrintf("%.2f %0.2f", vector
.x(), vector
.y());
3801 TEST_F(ViewLayerTest
, SnapLayerToPixel
) {
3802 View
* v1
= new View
;
3804 View
* v11
= new View
;
3805 v1
->AddChildView(v11
);
3807 widget()->SetContentsView(v1
);
3809 const gfx::Size
& size
= GetRootLayer()->GetCompositor()->size();
3810 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f
, size
);
3812 v11
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3813 v1
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3814 v11
->SetPaintToLayer(true);
3816 EXPECT_EQ("0.40 0.40", ToString(v11
->layer()->subpixel_position_offset()));
3818 // Creating a layer in parent should update the child view's layer offset.
3819 v1
->SetPaintToLayer(true);
3820 EXPECT_EQ("-0.20 -0.20", ToString(v1
->layer()->subpixel_position_offset()));
3821 EXPECT_EQ("-0.20 -0.20", ToString(v11
->layer()->subpixel_position_offset()));
3823 // DSF change should get propagated and update offsets.
3824 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f
, size
);
3825 EXPECT_EQ("0.33 0.33", ToString(v1
->layer()->subpixel_position_offset()));
3826 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3828 // Deleting parent's layer should update the child view's layer's offset.
3829 v1
->SetPaintToLayer(false);
3830 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3832 // Setting parent view should update the child view's layer's offset.
3833 v1
->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3834 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3836 // Setting integral DSF should reset the offset.
3837 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f
, size
);
3838 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3841 TEST_F(ViewTest
, FocusableAssertions
) {
3842 // View subclasses may change insets based on whether they are focusable,
3843 // which effects the preferred size. To avoid preferred size changing around
3844 // these Views need to key off the last value set to SetFocusable(), not
3845 // whether the View is focusable right now. For this reason it's important
3846 // that focusable() return the last value passed to SetFocusable and not
3847 // whether the View is focusable right now.
3849 view
.SetFocusable(true);
3850 EXPECT_TRUE(view
.focusable());
3851 view
.SetEnabled(false);
3852 EXPECT_TRUE(view
.focusable());
3853 view
.SetFocusable(false);
3854 EXPECT_FALSE(view
.focusable());
3857 // Verifies when a view is deleted it is removed from ViewStorage.
3858 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3859 ViewStorage
* view_storage
= ViewStorage::GetInstance();
3860 const int storage_id
= view_storage
->CreateStorageID();
3863 view_storage
->StoreView(storage_id
, &view
);
3865 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
3868 ////////////////////////////////////////////////////////////////////////////////
3870 ////////////////////////////////////////////////////////////////////////////////
3872 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
3873 native_theme_
= native_theme
;
3876 TEST_F(ViewTest
, OnNativeThemeChanged
) {
3877 TestView
* test_view
= new TestView();
3878 EXPECT_FALSE(test_view
->native_theme_
);
3879 TestView
* test_view_child
= new TestView();
3880 EXPECT_FALSE(test_view_child
->native_theme_
);
3882 // Child view added before the widget hierarchy exists should get the
3883 // new native theme notification.
3884 test_view
->AddChildView(test_view_child
);
3886 scoped_ptr
<Widget
> widget(new Widget
);
3887 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
3888 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3889 widget
->Init(params
);
3891 widget
->GetRootView()->AddChildView(test_view
);
3892 EXPECT_TRUE(test_view
->native_theme_
);
3893 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
3894 EXPECT_TRUE(test_view_child
->native_theme_
);
3895 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
3897 // Child view added after the widget hierarchy exists should also get the
3899 TestView
* test_view_child_2
= new TestView();
3900 test_view
->AddChildView(test_view_child_2
);
3901 EXPECT_TRUE(test_view_child_2
->native_theme_
);
3902 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
3907 } // namespace views