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 "grit/ui_strings.h"
13 #include "ui/base/accelerators/accelerator.h"
14 #include "ui/base/clipboard/clipboard.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/compositor/compositor.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/layer_animator.h"
19 #include "ui/compositor/test/draw_waiter_for_test.h"
20 #include "ui/events/event.h"
21 #include "ui/events/gestures/gesture_recognizer.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/views/background.h"
27 #include "ui/views/controls/native/native_view_host.h"
28 #include "ui/views/controls/scroll_view.h"
29 #include "ui/views/controls/textfield/textfield.h"
30 #include "ui/views/focus/view_storage.h"
31 #include "ui/views/test/views_test_base.h"
32 #include "ui/views/view.h"
33 #include "ui/views/views_delegate.h"
34 #include "ui/views/widget/native_widget.h"
35 #include "ui/views/widget/root_view.h"
36 #include "ui/views/window/dialog_client_view.h"
37 #include "ui/views/window/dialog_delegate.h"
39 using base::ASCIIToUTF16
;
43 // Returns true if |ancestor| is an ancestor of |layer|.
44 bool LayerIsAncestor(const ui::Layer
* ancestor
, const ui::Layer
* layer
) {
45 while (layer
&& layer
!= ancestor
)
46 layer
= layer
->parent();
47 return layer
== ancestor
;
50 // Convenience functions for walking a View tree.
51 const views::View
* FirstView(const views::View
* view
) {
52 const views::View
* v
= view
;
53 while (v
->has_children())
58 const views::View
* NextView(const views::View
* view
) {
59 const views::View
* v
= view
;
60 const views::View
* parent
= v
->parent();
63 int next
= parent
->GetIndexOf(v
) + 1;
64 if (next
!= parent
->child_count())
65 return FirstView(parent
->child_at(next
));
69 // Convenience functions for walking a Layer tree.
70 const ui::Layer
* FirstLayer(const ui::Layer
* layer
) {
71 const ui::Layer
* l
= layer
;
72 while (l
->children().size() > 0)
77 const ui::Layer
* NextLayer(const ui::Layer
* layer
) {
78 const ui::Layer
* parent
= layer
->parent();
81 const std::vector
<ui::Layer
*> children
= parent
->children();
83 for (index
= 0; index
< children
.size(); index
++) {
84 if (children
[index
] == layer
)
87 size_t next
= index
+ 1;
88 if (next
< children
.size())
89 return FirstLayer(children
[next
]);
93 // Given the root nodes of a View tree and a Layer tree, makes sure the two
95 bool ViewAndLayerTreeAreConsistent(const views::View
* view
,
96 const ui::Layer
* layer
) {
97 const views::View
* v
= FirstView(view
);
98 const ui::Layer
* l
= FirstLayer(layer
);
100 // Find the view with a layer.
101 while (v
&& !v
->layer())
107 // Check if the View tree and the Layer tree are in sync.
108 EXPECT_EQ(l
, v
->layer());
112 // Check if the visibility states of the View and the Layer are in sync.
113 EXPECT_EQ(l
->IsDrawn(), v
->IsDrawn());
114 if (v
->IsDrawn() != l
->IsDrawn()) {
115 for (const views::View
* vv
= v
; vv
; vv
= vv
->parent())
116 LOG(ERROR
) << "V: " << vv
<< " " << vv
->visible() << " "
117 << vv
->IsDrawn() << " " << vv
->layer();
118 for (const ui::Layer
* ll
= l
; ll
; ll
= ll
->parent())
119 LOG(ERROR
) << "L: " << ll
<< " " << ll
->IsDrawn();
123 // Check if the size of the View and the Layer are in sync.
124 EXPECT_EQ(l
->bounds(), v
->bounds());
125 if (v
->bounds() != l
->bounds())
128 if (v
== view
|| l
== layer
)
129 return v
== view
&& l
== layer
;
138 // Constructs a View tree with the specified depth.
139 void ConstructTree(views::View
* view
, int depth
) {
142 int count
= base::RandInt(1, 5);
143 for (int i
= 0; i
< count
; i
++) {
144 views::View
* v
= new views::View
;
145 view
->AddChildView(v
);
146 if (base::RandDouble() > 0.5)
147 v
->SetPaintToLayer(true);
148 if (base::RandDouble() < 0.2)
149 v
->SetVisible(false);
151 ConstructTree(v
, depth
- 1);
155 void ScrambleTree(views::View
* view
) {
156 int count
= view
->child_count();
159 for (int i
= 0; i
< count
; i
++) {
160 ScrambleTree(view
->child_at(i
));
164 int a
= base::RandInt(0, count
- 1);
165 int b
= base::RandInt(0, count
- 1);
167 views::View
* view_a
= view
->child_at(a
);
168 views::View
* view_b
= view
->child_at(b
);
169 view
->ReorderChildView(view_a
, b
);
170 view
->ReorderChildView(view_b
, a
);
173 if (!view
->layer() && base::RandDouble() < 0.1)
174 view
->SetPaintToLayer(true);
176 if (base::RandDouble() < 0.1)
177 view
->SetVisible(!view
->visible());
180 // Convenience to make constructing a GestureEvent simpler.
181 class GestureEventForTest
: public ui::GestureEvent
{
183 GestureEventForTest(ui::EventType type
, int x
, int y
, int flags
)
184 : GestureEvent(x
, y
, flags
, base::TimeDelta(),
185 ui::GestureEventDetails(type
, 0.0f
, 0.0f
)) {
189 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest
);
196 typedef ViewsTestBase ViewTest
;
198 // A derived class for testing purpose.
199 class TestView
: public View
{
203 delete_on_pressed_(false),
205 can_process_events_within_subtree_(true) {}
206 virtual ~TestView() {}
208 // Reset all test state
210 did_change_bounds_
= false;
211 last_mouse_event_type_
= 0;
212 location_
.SetPoint(0, 0);
213 received_mouse_enter_
= false;
214 received_mouse_exit_
= false;
215 last_gesture_event_type_
= 0;
216 last_gesture_event_was_handled_
= false;
217 last_clip_
.setEmpty();
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 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE
{
238 return can_process_events_within_subtree_
;
241 virtual void OnBoundsChanged(const gfx::Rect
& previous_bounds
) OVERRIDE
;
242 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
243 virtual bool OnMouseDragged(const ui::MouseEvent
& event
) OVERRIDE
;
244 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
245 virtual void OnMouseEntered(const ui::MouseEvent
& event
) OVERRIDE
;
246 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
248 // Ignores GestureEvent by default.
249 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
;
251 virtual void Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) OVERRIDE
;
252 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
;
253 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
255 virtual void OnNativeThemeChanged(const ui::NativeTheme
* native_theme
)
259 bool did_change_bounds_
;
260 gfx::Rect new_bounds_
;
263 int last_mouse_event_type_
;
264 gfx::Point location_
;
265 bool received_mouse_enter_
;
266 bool received_mouse_exit_
;
267 bool delete_on_pressed_
;
270 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
273 int last_gesture_event_type_
;
274 bool last_gesture_event_was_handled_
;
280 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
283 const ui::NativeTheme
* native_theme_
;
285 // Value to return from CanProcessEventsWithinSubtree().
286 bool can_process_events_within_subtree_
;
289 // A view subclass that consumes all Gesture events for testing purposes.
290 class TestViewConsumeGesture
: public TestView
{
292 TestViewConsumeGesture() : TestView() {}
293 virtual ~TestViewConsumeGesture() {}
296 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
297 last_gesture_event_type_
= event
->type();
298 location_
.SetPoint(event
->x(), event
->y());
299 event
->StopPropagation();
303 DISALLOW_COPY_AND_ASSIGN(TestViewConsumeGesture
);
306 // A view subclass that ignores all Gesture events.
307 class TestViewIgnoreGesture
: public TestView
{
309 TestViewIgnoreGesture() : TestView() {}
310 virtual ~TestViewIgnoreGesture() {}
313 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
316 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreGesture
);
319 // A view subclass that ignores all scroll-gesture events, but consume all other
321 class TestViewIgnoreScrollGestures
: public TestViewConsumeGesture
{
323 TestViewIgnoreScrollGestures() {}
324 virtual ~TestViewIgnoreScrollGestures() {}
327 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
328 if (event
->IsScrollGestureEvent())
330 TestViewConsumeGesture::OnGestureEvent(event
);
333 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreScrollGestures
);
336 ////////////////////////////////////////////////////////////////////////////////
338 ////////////////////////////////////////////////////////////////////////////////
340 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
341 did_change_bounds_
= true;
342 new_bounds_
= bounds();
345 TEST_F(ViewTest
, OnBoundsChanged
) {
348 gfx::Rect
prev_rect(0, 0, 200, 200);
349 gfx::Rect
new_rect(100, 100, 250, 250);
351 v
.SetBoundsRect(prev_rect
);
353 v
.SetBoundsRect(new_rect
);
355 EXPECT_TRUE(v
.did_change_bounds_
);
356 EXPECT_EQ(v
.new_bounds_
, new_rect
);
357 EXPECT_EQ(v
.bounds(), new_rect
);
360 ////////////////////////////////////////////////////////////////////////////////
362 ////////////////////////////////////////////////////////////////////////////////
364 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
365 last_mouse_event_type_
= event
.type();
366 location_
.SetPoint(event
.x(), event
.y());
367 if (delete_on_pressed_
)
372 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
373 last_mouse_event_type_
= event
.type();
374 location_
.SetPoint(event
.x(), event
.y());
378 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
379 last_mouse_event_type_
= event
.type();
380 location_
.SetPoint(event
.x(), event
.y());
383 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
384 received_mouse_enter_
= true;
387 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
388 received_mouse_exit_
= true;
391 TEST_F(ViewTest
, MouseEvent
) {
392 TestView
* v1
= new TestView();
393 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
395 TestView
* v2
= new TestView();
396 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
398 scoped_ptr
<Widget
> widget(new Widget
);
399 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
400 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
401 params
.bounds
= gfx::Rect(50, 50, 650, 650);
402 widget
->Init(params
);
403 internal::RootView
* root
=
404 static_cast<internal::RootView
*>(widget
->GetRootView());
406 root
->AddChildView(v1
);
407 v1
->AddChildView(v2
);
412 gfx::Point
p1(110, 120);
413 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
414 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
415 root
->OnMousePressed(pressed
);
416 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
417 EXPECT_EQ(v2
->location_
.x(), 10);
418 EXPECT_EQ(v2
->location_
.y(), 20);
419 // Make sure v1 did not receive the event
420 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
422 // Drag event out of bounds. Should still go to v2
425 gfx::Point
p2(50, 40);
426 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
,
427 ui::EF_LEFT_MOUSE_BUTTON
, 0);
428 root
->OnMouseDragged(dragged
);
429 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
430 EXPECT_EQ(v2
->location_
.x(), -50);
431 EXPECT_EQ(v2
->location_
.y(), -60);
432 // Make sure v1 did not receive the event
433 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
435 // Releasted event out of bounds. Should still go to v2
438 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
440 root
->OnMouseDragged(released
);
441 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
442 EXPECT_EQ(v2
->location_
.x(), -100);
443 EXPECT_EQ(v2
->location_
.y(), -100);
444 // Make sure v1 did not receive the event
445 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
450 // Confirm that a view can be deleted as part of processing a mouse press.
451 TEST_F(ViewTest
, DeleteOnPressed
) {
452 TestView
* v1
= new TestView();
453 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
455 TestView
* v2
= new TestView();
456 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
461 scoped_ptr
<Widget
> widget(new Widget
);
462 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
463 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
464 params
.bounds
= gfx::Rect(50, 50, 650, 650);
465 widget
->Init(params
);
466 View
* root
= widget
->GetRootView();
468 root
->AddChildView(v1
);
469 v1
->AddChildView(v2
);
471 v2
->delete_on_pressed_
= true;
472 gfx::Point
point(110, 120);
473 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
474 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
475 root
->OnMousePressed(pressed
);
476 EXPECT_EQ(0, v1
->child_count());
481 ////////////////////////////////////////////////////////////////////////////////
483 ////////////////////////////////////////////////////////////////////////////////
485 void TestView::OnGestureEvent(ui::GestureEvent
* event
) {
488 TEST_F(ViewTest
, GestureEvent
) {
489 // Views hierarchy for non delivery of GestureEvent.
490 TestView
* v1
= new TestViewConsumeGesture();
491 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
493 TestView
* v2
= new TestViewConsumeGesture();
494 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
496 TestView
* v3
= new TestViewIgnoreGesture();
497 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
499 scoped_ptr
<Widget
> widget(new Widget());
500 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
501 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
502 params
.bounds
= gfx::Rect(50, 50, 650, 650);
503 widget
->Init(params
);
504 internal::RootView
* root
=
505 static_cast<internal::RootView
*>(widget
->GetRootView());
506 ui::EventDispatchDetails details
;
508 root
->AddChildView(v1
);
509 v1
->AddChildView(v2
);
510 v2
->AddChildView(v3
);
512 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
513 // reach |v2| because |v3| doesn't process any gesture events. However, since
514 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
522 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
523 details
= root
->OnEventFromSource(&g1
);
524 EXPECT_FALSE(details
.dispatcher_destroyed
);
525 EXPECT_FALSE(details
.target_destroyed
);
527 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
528 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
529 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
531 // Simulate an up so that RootView is no longer targetting |v3|.
532 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
533 details
= root
->OnEventFromSource(&g1_up
);
534 EXPECT_FALSE(details
.dispatcher_destroyed
);
535 EXPECT_FALSE(details
.target_destroyed
);
542 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
543 details
= root
->OnEventFromSource(&g2
);
544 EXPECT_FALSE(details
.dispatcher_destroyed
);
545 EXPECT_FALSE(details
.target_destroyed
);
547 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
548 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
549 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
551 // Send event |g1| again. Even though the coordinates target |v3| it should go
552 // to |v1| as that is the view the touch was initially down on.
553 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
554 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
555 details
= root
->OnEventFromSource(&g1
);
556 EXPECT_FALSE(details
.dispatcher_destroyed
);
557 EXPECT_FALSE(details
.target_destroyed
);
559 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
560 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
561 EXPECT_EQ("110,110", v1
->location_
.ToString());
566 TEST_F(ViewTest
, ScrollGestureEvent
) {
567 // Views hierarchy for non delivery of GestureEvent.
568 TestView
* v1
= new TestViewConsumeGesture();
569 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
571 TestView
* v2
= new TestViewIgnoreScrollGestures();
572 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
574 TestView
* v3
= new TestViewIgnoreGesture();
575 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
577 scoped_ptr
<Widget
> widget(new Widget());
578 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
579 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
580 params
.bounds
= gfx::Rect(50, 50, 650, 650);
581 widget
->Init(params
);
582 internal::RootView
* root
=
583 static_cast<internal::RootView
*>(widget
->GetRootView());
584 ui::EventDispatchDetails details
;
586 root
->AddChildView(v1
);
587 v1
->AddChildView(v2
);
588 v2
->AddChildView(v3
);
590 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
591 // reach |v2| because |v3| doesn't process any gesture events. However, since
592 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
600 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
601 details
= root
->OnEventFromSource(&g1
);
602 EXPECT_FALSE(details
.dispatcher_destroyed
);
603 EXPECT_FALSE(details
.target_destroyed
);
605 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
606 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
607 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
611 // Send scroll gestures on |v3|. The gesture should reach |v2|, however,
612 // since it does not process scroll-gesture events, these events should reach
614 GestureEventForTest
gscroll_begin(ui::ET_GESTURE_SCROLL_BEGIN
, 115, 115, 0);
615 details
= root
->OnEventFromSource(&gscroll_begin
);
616 EXPECT_FALSE(details
.dispatcher_destroyed
);
617 EXPECT_FALSE(details
.target_destroyed
);
619 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
620 EXPECT_EQ(ui::ET_GESTURE_SCROLL_BEGIN
, v1
->last_gesture_event_type_
);
623 // Send a second tap on |v1|. The event should reach |v2| since it is the
624 // default gesture handler, and not |v1| (even though it is the view under the
625 // point, and is the scroll event handler).
626 GestureEventForTest
second_tap(ui::ET_GESTURE_TAP
, 70, 70, 0);
627 details
= root
->OnEventFromSource(&second_tap
);
628 EXPECT_FALSE(details
.dispatcher_destroyed
);
629 EXPECT_FALSE(details
.target_destroyed
);
631 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
632 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
635 GestureEventForTest
gscroll_end(ui::ET_GESTURE_SCROLL_END
, 50, 50, 0);
636 details
= root
->OnEventFromSource(&gscroll_end
);
637 EXPECT_FALSE(details
.dispatcher_destroyed
);
638 EXPECT_FALSE(details
.target_destroyed
);
640 EXPECT_EQ(ui::ET_GESTURE_SCROLL_END
, v1
->last_gesture_event_type_
);
643 // Simulate an up so that RootView is no longer targetting |v3|.
644 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
645 details
= root
->OnEventFromSource(&g1_up
);
646 EXPECT_FALSE(details
.dispatcher_destroyed
);
647 EXPECT_FALSE(details
.target_destroyed
);
649 EXPECT_EQ(ui::ET_GESTURE_END
, v2
->last_gesture_event_type_
);
656 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
657 details
= root
->OnEventFromSource(&g2
);
658 EXPECT_FALSE(details
.dispatcher_destroyed
);
659 EXPECT_FALSE(details
.target_destroyed
);
661 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
662 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
663 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
665 // Send event |g1| again. Even though the coordinates target |v3| it should go
666 // to |v1| as that is the view the touch was initially down on.
667 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
668 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
669 details
= root
->OnEventFromSource(&g1
);
670 EXPECT_FALSE(details
.dispatcher_destroyed
);
671 EXPECT_FALSE(details
.target_destroyed
);
673 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
674 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
675 EXPECT_EQ("110,110", v1
->location_
.ToString());
680 ////////////////////////////////////////////////////////////////////////////////
682 ////////////////////////////////////////////////////////////////////////////////
684 void TestView::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
685 canvas
->sk_canvas()->getClipBounds(&last_clip_
);
688 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
689 scheduled_paint_rects_
.push_back(rect
);
690 View::SchedulePaintInRect(rect
);
693 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
694 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
695 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
696 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
697 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
700 TEST_F(ViewTest
, RemoveNotification
) {
701 ViewStorage
* vs
= ViewStorage::GetInstance();
702 Widget
* widget
= new Widget
;
703 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
704 View
* root_view
= widget
->GetRootView();
707 int s1
= vs
->CreateStorageID();
708 vs
->StoreView(s1
, v1
);
709 root_view
->AddChildView(v1
);
710 View
* v11
= new View
;
711 int s11
= vs
->CreateStorageID();
712 vs
->StoreView(s11
, v11
);
713 v1
->AddChildView(v11
);
714 View
* v111
= new View
;
715 int s111
= vs
->CreateStorageID();
716 vs
->StoreView(s111
, v111
);
717 v11
->AddChildView(v111
);
718 View
* v112
= new View
;
719 int s112
= vs
->CreateStorageID();
720 vs
->StoreView(s112
, v112
);
721 v11
->AddChildView(v112
);
722 View
* v113
= new View
;
723 int s113
= vs
->CreateStorageID();
724 vs
->StoreView(s113
, v113
);
725 v11
->AddChildView(v113
);
726 View
* v1131
= new View
;
727 int s1131
= vs
->CreateStorageID();
728 vs
->StoreView(s1131
, v1131
);
729 v113
->AddChildView(v1131
);
730 View
* v12
= new View
;
731 int s12
= vs
->CreateStorageID();
732 vs
->StoreView(s12
, v12
);
733 v1
->AddChildView(v12
);
736 int s2
= vs
->CreateStorageID();
737 vs
->StoreView(s2
, v2
);
738 root_view
->AddChildView(v2
);
739 View
* v21
= new View
;
740 int s21
= vs
->CreateStorageID();
741 vs
->StoreView(s21
, v21
);
742 v2
->AddChildView(v21
);
743 View
* v211
= new View
;
744 int s211
= vs
->CreateStorageID();
745 vs
->StoreView(s211
, v211
);
746 v21
->AddChildView(v211
);
748 size_t stored_views
= vs
->view_count();
750 // Try removing a leaf view.
751 v21
->RemoveChildView(v211
);
752 EXPECT_EQ(stored_views
- 1, vs
->view_count());
753 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
754 delete v211
; // We won't use this one anymore.
756 // Now try removing a view with a hierarchy of depth 1.
757 v11
->RemoveChildView(v113
);
758 EXPECT_EQ(stored_views
- 3, vs
->view_count());
759 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
760 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
761 delete v113
; // We won't use this one anymore.
763 // Now remove even more.
764 root_view
->RemoveChildView(v1
);
765 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
766 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
767 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
768 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
769 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
771 // Put v1 back for more tests.
772 root_view
->AddChildView(v1
);
773 vs
->StoreView(s1
, v1
);
775 // Synchronously closing the window deletes the view hierarchy, which should
776 // remove all its views from ViewStorage.
778 EXPECT_EQ(stored_views
- 10, vs
->view_count());
779 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
780 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
781 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
782 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
783 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
784 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
785 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
790 void RotateCounterclockwise(gfx::Transform
* transform
) {
791 transform
->matrix().set3x3(0, -1, 0,
796 void RotateClockwise(gfx::Transform
* transform
) {
797 transform
->matrix().set3x3( 0, 1, 0,
804 // Tests the correctness of the rect-based targeting algorithm implemented in
805 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
806 // of rect-based targeting.
807 TEST_F(ViewTest
, GetEventHandlerForRect
) {
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
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
814 // Have this hierarchy of views (the coordinates here are all in
815 // the root view's coordinate space):
816 // v1 (0, 0, 100, 100)
817 // v2 (150, 0, 250, 100)
818 // v3 (0, 200, 150, 100)
819 // v31 (10, 210, 80, 80)
820 // v32 (110, 210, 30, 80)
821 // v4 (300, 200, 100, 100)
822 // v41 (310, 210, 80, 80)
823 // v411 (370, 275, 10, 5)
824 // v5 (450, 197, 30, 36)
825 // v51 (450, 200, 30, 30)
827 // The coordinates used for SetBounds are in parent coordinates.
829 TestView
* v1
= new TestView
;
830 v1
->SetBounds(0, 0, 100, 100);
831 root_view
->AddChildView(v1
);
833 TestView
* v2
= new TestView
;
834 v2
->SetBounds(150, 0, 250, 100);
835 root_view
->AddChildView(v2
);
837 TestView
* v3
= new TestView
;
838 v3
->SetBounds(0, 200, 150, 100);
839 root_view
->AddChildView(v3
);
841 TestView
* v4
= new TestView
;
842 v4
->SetBounds(300, 200, 100, 100);
843 root_view
->AddChildView(v4
);
845 TestView
* v31
= new TestView
;
846 v31
->SetBounds(10, 10, 80, 80);
847 v3
->AddChildView(v31
);
849 TestView
* v32
= new TestView
;
850 v32
->SetBounds(110, 10, 30, 80);
851 v3
->AddChildView(v32
);
853 TestView
* v41
= new TestView
;
854 v41
->SetBounds(10, 10, 80, 80);
855 v4
->AddChildView(v41
);
857 TestView
* v411
= new TestView
;
858 v411
->SetBounds(60, 65, 10, 5);
859 v41
->AddChildView(v411
);
861 TestView
* v5
= new TestView
;
862 v5
->SetBounds(450, 197, 30, 36);
863 root_view
->AddChildView(v5
);
865 TestView
* v51
= new TestView
;
866 v51
->SetBounds(0, 3, 30, 30);
867 v5
->AddChildView(v51
);
869 // |touch_rect| does not intersect any descendant view of |root_view|.
870 gfx::Rect
touch_rect(105, 105, 30, 45);
871 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
872 EXPECT_EQ(root_view
, result_view
);
875 // Covers |v1| by at least 60%.
876 touch_rect
.SetRect(15, 15, 100, 100);
877 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
878 EXPECT_EQ(v1
, result_view
);
881 // Intersects |v1| but does not cover it by at least 60%. The center
882 // of |touch_rect| is within |v1|.
883 touch_rect
.SetRect(50, 50, 5, 10);
884 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
885 EXPECT_EQ(v1
, result_view
);
888 // Intersects |v1| but does not cover it by at least 60%. The center
889 // of |touch_rect| is not within |v1|.
890 touch_rect
.SetRect(95, 96, 21, 22);
891 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
892 EXPECT_EQ(root_view
, result_view
);
895 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
896 touch_rect
.SetRect(95, 10, 300, 120);
897 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
898 EXPECT_EQ(v2
, result_view
);
901 // Covers both |v1| and |v2| by at least 60%, but the center point
902 // of |touch_rect| is closer to the center point of |v2|.
903 touch_rect
.SetRect(20, 20, 400, 100);
904 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
905 EXPECT_EQ(v2
, result_view
);
908 // Covers both |v1| and |v2| by at least 60%, but the center point
909 // of |touch_rect| is closer to the center point of |v1|.
910 touch_rect
.SetRect(-700, -15, 1050, 110);
911 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
912 EXPECT_EQ(v1
, result_view
);
915 // A mouse click within |v1| will target |v1|.
916 touch_rect
.SetRect(15, 15, 1, 1);
917 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
918 EXPECT_EQ(v1
, result_view
);
921 // Intersects |v3| and |v31| by at least 60% and the center point
922 // of |touch_rect| is closer to the center point of |v31|.
923 touch_rect
.SetRect(0, 200, 110, 100);
924 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
925 EXPECT_EQ(v31
, result_view
);
928 // Intersects |v3| and |v31|, but neither by at least 60%. The
929 // center point of |touch_rect| lies within |v31|.
930 touch_rect
.SetRect(80, 280, 15, 15);
931 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
932 EXPECT_EQ(v31
, result_view
);
935 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
936 // center point of |touch_rect| is closest to the center point
938 touch_rect
.SetRect(0, 200, 200, 100);
939 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
940 EXPECT_EQ(v32
, result_view
);
943 // Intersects all of |v3|, |v31|, and |v32|, but only covers
944 // |v31| and |v32| by at least 60%. The center point of
945 // |touch_rect| is closest to the center point of |v32|.
946 touch_rect
.SetRect(30, 225, 180, 115);
947 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
948 EXPECT_EQ(v32
, result_view
);
951 // A mouse click at the corner of |v3| will target |v3|.
952 touch_rect
.SetRect(0, 200, 1, 1);
953 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
954 EXPECT_EQ(v3
, result_view
);
957 // A mouse click within |v32| will target |v32|.
958 touch_rect
.SetRect(112, 211, 1, 1);
959 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
960 EXPECT_EQ(v32
, result_view
);
963 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
964 // The center point of |touch_rect| is equally close to
965 // the center points of |v4| and |v41|.
966 touch_rect
.SetRect(310, 210, 80, 80);
967 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
968 EXPECT_EQ(v41
, result_view
);
971 // Intersects all of |v4|, |v41|, and |v411| but only covers
972 // |v411| by at least 60%.
973 touch_rect
.SetRect(370, 275, 7, 5);
974 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
975 EXPECT_EQ(v411
, result_view
);
978 // Intersects |v4| and |v41| but covers neither by at least 60%.
979 // The center point of |touch_rect| is equally close to the center
980 // points of |v4| and |v41|.
981 touch_rect
.SetRect(345, 245, 7, 7);
982 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
983 EXPECT_EQ(v41
, result_view
);
986 // Intersects all of |v4|, |v41|, and |v411| and covers none of
987 // them by at least 60%. The center point of |touch_rect| lies
989 touch_rect
.SetRect(368, 272, 4, 6);
990 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
991 EXPECT_EQ(v411
, result_view
);
994 // Intersects all of |v4|, |v41|, and |v411| and covers none of
995 // them by at least 60%. The center point of |touch_rect| lies
997 touch_rect
.SetRect(365, 270, 7, 7);
998 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
999 EXPECT_EQ(v41
, result_view
);
1002 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1003 // them by at least 60%. The center point of |touch_rect| lies
1005 touch_rect
.SetRect(205, 275, 200, 2);
1006 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1007 EXPECT_EQ(v4
, result_view
);
1010 // Intersects all of |v4|, |v41|, and |v411| but only covers
1011 // |v41| by at least 60%.
1012 touch_rect
.SetRect(310, 210, 61, 66);
1013 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1014 EXPECT_EQ(v41
, result_view
);
1017 // A mouse click within |v411| will target |v411|.
1018 touch_rect
.SetRect(372, 275, 1, 1);
1019 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1020 EXPECT_EQ(v411
, result_view
);
1023 // A mouse click within |v41| will target |v41|.
1024 touch_rect
.SetRect(350, 215, 1, 1);
1025 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1026 EXPECT_EQ(v41
, result_view
);
1029 // Covers |v3|, |v4|, and all of their descendants by at
1030 // least 60%. The center point of |touch_rect| is closest
1031 // to the center point of |v32|.
1032 touch_rect
.SetRect(0, 200, 400, 100);
1033 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1034 EXPECT_EQ(v32
, result_view
);
1037 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1038 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1039 // The center point of |touch_rect| is closest to the center
1040 // point of |root_view|.
1041 touch_rect
.SetRect(110, 15, 375, 450);
1042 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1043 EXPECT_EQ(root_view
, result_view
);
1046 // Covers all views (except |v5| and |v51|) by at least 60%. The
1047 // center point of |touch_rect| is equally close to the center
1048 // points of |v2| and |v32|. One is not a descendant of the other,
1049 // so in this case the view selected is arbitrary (i.e.,
1050 // it depends only on the ordering of nodes in the views
1052 touch_rect
.SetRect(0, 0, 400, 300);
1053 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1054 EXPECT_EQ(v32
, result_view
);
1057 // Covers |v5| and |v51| by at least 60%, and the center point of
1058 // the touch is located within both views. Since both views share
1059 // the same center point, the child view should be selected.
1060 touch_rect
.SetRect(440, 190, 40, 40);
1061 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1062 EXPECT_EQ(v51
, result_view
);
1065 // Covers |v5| and |v51| by at least 60%, but the center point of
1066 // the touch is not located within either view. Since both views
1067 // share the same center point, the child view should be selected.
1068 touch_rect
.SetRect(455, 187, 60, 60);
1069 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1070 EXPECT_EQ(v51
, result_view
);
1073 // Covers neither |v5| nor |v51| by at least 60%, but the center
1074 // of the touch is located within |v51|.
1075 touch_rect
.SetRect(450, 197, 10, 10);
1076 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1077 EXPECT_EQ(v51
, result_view
);
1080 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1081 // The center point is located outside of both views.
1082 touch_rect
.SetRect(433, 180, 24, 24);
1083 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1084 EXPECT_EQ(root_view
, result_view
);
1087 // Only intersects |v5| but does not cover it by at least 60%. The
1088 // center point of the touch region is located within |v5|.
1089 touch_rect
.SetRect(449, 196, 3, 3);
1090 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1091 EXPECT_EQ(v5
, result_view
);
1094 // A mouse click within |v5| (but not |v51|) should target |v5|.
1095 touch_rect
.SetRect(462, 199, 1, 1);
1096 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1097 EXPECT_EQ(v5
, result_view
);
1100 // A mouse click |v5| and |v51| should target the child view.
1101 touch_rect
.SetRect(452, 226, 1, 1);
1102 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1103 EXPECT_EQ(v51
, result_view
);
1106 // A mouse click on the center of |v5| and |v51| should target
1108 touch_rect
.SetRect(465, 215, 1, 1);
1109 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1110 EXPECT_EQ(v51
, result_view
);
1116 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1117 // as expected when different views in the view hierarchy return false
1118 // when CanProcessEventsWithinSubtree() is called.
1119 TEST_F(ViewTest
, CanProcessEventsWithinSubtree
) {
1120 Widget
* widget
= new Widget
;
1121 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1122 widget
->Init(params
);
1123 View
* root_view
= widget
->GetRootView();
1124 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1126 // Have this hierarchy of views (the coords here are in the coordinate
1127 // space of the root view):
1128 // v (0, 0, 100, 100)
1129 // - v_child (0, 0, 20, 30)
1130 // - v_grandchild (5, 5, 5, 15)
1132 TestView
* v
= new TestView
;
1133 v
->SetBounds(0, 0, 100, 100);
1134 root_view
->AddChildView(v
);
1135 v
->set_notify_enter_exit_on_child(true);
1137 TestView
* v_child
= new TestView
;
1138 v_child
->SetBounds(0, 0, 20, 30);
1139 v
->AddChildView(v_child
);
1141 TestView
* v_grandchild
= new TestView
;
1142 v_grandchild
->SetBounds(5, 5, 5, 15);
1143 v_child
->AddChildView(v_grandchild
);
1147 v_grandchild
->Reset();
1149 // Define rects and points within the views in the hierarchy.
1150 gfx::Rect
rect_in_v_grandchild(7, 7, 3, 3);
1151 gfx::Point
point_in_v_grandchild(rect_in_v_grandchild
.origin());
1152 gfx::Rect
rect_in_v_child(12, 3, 5, 5);
1153 gfx::Point
point_in_v_child(rect_in_v_child
.origin());
1154 gfx::Rect
rect_in_v(50, 50, 25, 30);
1155 gfx::Point
point_in_v(rect_in_v
.origin());
1157 // When all three views return true when CanProcessEventsWithinSubtree()
1158 // is called, targeting should behave as expected.
1160 View
* result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1161 EXPECT_EQ(v_grandchild
, result_view
);
1163 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1164 EXPECT_EQ(v_grandchild
, result_view
);
1167 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1168 EXPECT_EQ(v_child
, result_view
);
1170 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1171 EXPECT_EQ(v_child
, result_view
);
1174 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1175 EXPECT_EQ(v
, result_view
);
1177 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1178 EXPECT_EQ(v
, result_view
);
1181 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1182 // is called, then |v_grandchild| cannot be returned as a target.
1184 v_grandchild
->set_can_process_events_within_subtree(false);
1186 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1187 EXPECT_EQ(v_child
, result_view
);
1189 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1190 EXPECT_EQ(v_child
, result_view
);
1193 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1194 EXPECT_EQ(v_child
, result_view
);
1196 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1197 EXPECT_EQ(v_child
, result_view
);
1200 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1201 EXPECT_EQ(v
, result_view
);
1203 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1204 EXPECT_EQ(v
, result_view
);
1206 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1207 // is called, then NULL should be returned as a target if we call
1208 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
1209 // views tree. Note that the location must be in the coordinate space
1210 // of the root view (|v_grandchild| in this case), so use (1, 1).
1212 result_view
= v_grandchild
;
1213 result_view
= v_grandchild
->GetTooltipHandlerForPoint(gfx::Point(1, 1));
1214 EXPECT_EQ(NULL
, result_view
);
1217 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1218 // is called, then neither |v_child| nor |v_grandchild| can be returned
1219 // as a target (|v| should be returned as the target for each case).
1221 v_grandchild
->Reset();
1222 v_child
->set_can_process_events_within_subtree(false);
1224 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1225 EXPECT_EQ(v
, result_view
);
1227 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1228 EXPECT_EQ(v
, result_view
);
1231 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1232 EXPECT_EQ(v
, result_view
);
1234 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1235 EXPECT_EQ(v
, result_view
);
1238 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1239 EXPECT_EQ(v
, result_view
);
1241 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1242 EXPECT_EQ(v
, result_view
);
1245 // When |v| returns false when CanProcessEventsWithinSubtree()
1246 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1247 // as a target (|root_view| should be returned as the target for each case).
1250 v
->set_can_process_events_within_subtree(false);
1252 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1253 EXPECT_EQ(root_view
, result_view
);
1255 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1256 EXPECT_EQ(root_view
, result_view
);
1259 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1260 EXPECT_EQ(root_view
, result_view
);
1262 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1263 EXPECT_EQ(root_view
, result_view
);
1266 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1267 EXPECT_EQ(root_view
, result_view
);
1269 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1270 EXPECT_EQ(root_view
, result_view
);
1273 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1274 Widget
* widget
= new Widget
;
1275 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1276 widget
->Init(params
);
1277 View
* root_view
= widget
->GetRootView();
1278 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1280 // Have this hierarchy of views (the coords here are in root coord):
1281 // v1 (0, 0, 100, 100)
1282 // - v11 (0, 0, 20, 30)
1283 // - v111 (5, 5, 5, 15)
1284 // - v12 (50, 10, 30, 90)
1285 // - v121 (60, 20, 10, 10)
1286 // v2 (105, 0, 100, 100)
1287 // - v21 (120, 10, 50, 20)
1289 TestView
* v1
= new TestView
;
1290 v1
->SetBounds(0, 0, 100, 100);
1291 root_view
->AddChildView(v1
);
1292 v1
->set_notify_enter_exit_on_child(true);
1294 TestView
* v11
= new TestView
;
1295 v11
->SetBounds(0, 0, 20, 30);
1296 v1
->AddChildView(v11
);
1298 TestView
* v111
= new TestView
;
1299 v111
->SetBounds(5, 5, 5, 15);
1300 v11
->AddChildView(v111
);
1302 TestView
* v12
= new TestView
;
1303 v12
->SetBounds(50, 10, 30, 90);
1304 v1
->AddChildView(v12
);
1306 TestView
* v121
= new TestView
;
1307 v121
->SetBounds(10, 10, 10, 10);
1308 v12
->AddChildView(v121
);
1310 TestView
* v2
= new TestView
;
1311 v2
->SetBounds(105, 0, 100, 100);
1312 root_view
->AddChildView(v2
);
1314 TestView
* v21
= new TestView
;
1315 v21
->SetBounds(15, 10, 50, 20);
1316 v2
->AddChildView(v21
);
1326 // Move the mouse in v111.
1327 gfx::Point
p1(6, 6);
1328 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, 0, 0);
1329 root_view
->OnMouseMoved(move1
);
1330 EXPECT_TRUE(v111
->received_mouse_enter_
);
1331 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1332 EXPECT_TRUE(v1
->received_mouse_enter_
);
1337 // Now, move into v121.
1338 gfx::Point
p2(65, 21);
1339 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, 0, 0);
1340 root_view
->OnMouseMoved(move2
);
1341 EXPECT_TRUE(v111
->received_mouse_exit_
);
1342 EXPECT_TRUE(v121
->received_mouse_enter_
);
1343 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1348 // Now, move into v11.
1349 gfx::Point
p3(1, 1);
1350 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, 0, 0);
1351 root_view
->OnMouseMoved(move3
);
1352 EXPECT_TRUE(v121
->received_mouse_exit_
);
1353 EXPECT_TRUE(v11
->received_mouse_enter_
);
1354 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1360 gfx::Point
p4(121, 15);
1361 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, 0, 0);
1362 root_view
->OnMouseMoved(move4
);
1363 EXPECT_TRUE(v21
->received_mouse_enter_
);
1364 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1365 EXPECT_TRUE(v11
->received_mouse_exit_
);
1366 EXPECT_TRUE(v1
->received_mouse_exit_
);
1373 gfx::Point
p5(21, 0);
1374 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, 0, 0);
1375 root_view
->OnMouseMoved(move5
);
1376 EXPECT_TRUE(v21
->received_mouse_exit_
);
1377 EXPECT_TRUE(v1
->received_mouse_enter_
);
1382 // Now, move into v11.
1383 gfx::Point
p6(15, 15);
1384 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, 0, 0);
1385 root_view
->OnMouseMoved(mouse6
);
1386 EXPECT_TRUE(v11
->received_mouse_enter_
);
1387 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1392 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1393 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1394 // when the mouse leaves v11.
1395 gfx::Point
p7(21, 0);
1396 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, 0, 0);
1397 root_view
->OnMouseMoved(mouse7
);
1398 EXPECT_TRUE(v11
->received_mouse_exit_
);
1399 EXPECT_FALSE(v1
->received_mouse_enter_
);
1404 TEST_F(ViewTest
, Textfield
) {
1405 const base::string16 kText
= ASCIIToUTF16(
1406 "Reality is that which, when you stop believing it, doesn't go away.");
1407 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1409 Widget
* widget
= new Widget
;
1410 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1411 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1412 widget
->Init(params
);
1413 View
* root_view
= widget
->GetRootView();
1415 Textfield
* textfield
= new Textfield();
1416 root_view
->AddChildView(textfield
);
1418 // Test setting, appending text.
1419 textfield
->SetText(kText
);
1420 EXPECT_EQ(kText
, textfield
->text());
1421 textfield
->AppendText(kExtraText
);
1422 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1423 textfield
->SetText(base::string16());
1424 EXPECT_TRUE(textfield
->text().empty());
1426 // Test selection related methods.
1427 textfield
->SetText(kText
);
1428 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1429 textfield
->SelectAll(false);
1430 EXPECT_EQ(kText
, textfield
->text());
1431 textfield
->ClearSelection();
1432 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1437 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1438 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1439 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1440 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1441 const base::string16 kPasswordText
=
1442 ASCIIToUTF16("Password! ** Secret stuff **");
1444 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1446 Widget
* widget
= new Widget
;
1447 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1448 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1449 widget
->Init(params
);
1450 View
* root_view
= widget
->GetRootView();
1452 Textfield
* normal
= new Textfield();
1453 Textfield
* read_only
= new Textfield();
1454 read_only
->SetReadOnly(true);
1455 Textfield
* password
= new Textfield();
1456 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1458 root_view
->AddChildView(normal
);
1459 root_view
->AddChildView(read_only
);
1460 root_view
->AddChildView(password
);
1462 normal
->SetText(kNormalText
);
1463 read_only
->SetText(kReadOnlyText
);
1464 password
->SetText(kPasswordText
);
1470 normal
->SelectAll(false);
1471 normal
->ExecuteCommand(IDS_APP_CUT
);
1472 base::string16 result
;
1473 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1474 EXPECT_EQ(kNormalText
, result
);
1475 normal
->SetText(kNormalText
); // Let's revert to the original content.
1477 read_only
->SelectAll(false);
1478 read_only
->ExecuteCommand(IDS_APP_CUT
);
1480 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1481 // Cut should have failed, so the clipboard content should not have changed.
1482 EXPECT_EQ(kNormalText
, result
);
1484 password
->SelectAll(false);
1485 password
->ExecuteCommand(IDS_APP_CUT
);
1487 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1488 // Cut should have failed, so the clipboard content should not have changed.
1489 EXPECT_EQ(kNormalText
, result
);
1495 // Start with |read_only| to observe a change in clipboard text.
1496 read_only
->SelectAll(false);
1497 read_only
->ExecuteCommand(IDS_APP_COPY
);
1499 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1500 EXPECT_EQ(kReadOnlyText
, result
);
1502 normal
->SelectAll(false);
1503 normal
->ExecuteCommand(IDS_APP_COPY
);
1505 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1506 EXPECT_EQ(kNormalText
, result
);
1508 password
->SelectAll(false);
1509 password
->ExecuteCommand(IDS_APP_COPY
);
1511 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1512 // Text cannot be copied from an obscured field; the clipboard won't change.
1513 EXPECT_EQ(kNormalText
, result
);
1519 // Attempting to paste kNormalText in a read-only text-field should fail.
1520 read_only
->SelectAll(false);
1521 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1522 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1524 password
->SelectAll(false);
1525 password
->ExecuteCommand(IDS_APP_PASTE
);
1526 EXPECT_EQ(kNormalText
, password
->text());
1528 // Copy from |read_only| to observe a change in the normal textfield text.
1529 read_only
->SelectAll(false);
1530 read_only
->ExecuteCommand(IDS_APP_COPY
);
1531 normal
->SelectAll(false);
1532 normal
->ExecuteCommand(IDS_APP_PASTE
);
1533 EXPECT_EQ(kReadOnlyText
, normal
->text());
1537 ////////////////////////////////////////////////////////////////////////////////
1539 ////////////////////////////////////////////////////////////////////////////////
1540 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1541 accelerator_count_map_
[accelerator
]++;
1545 // TODO: these tests were initially commented out when getting aura to
1546 // run. Figure out if still valuable and either nuke or fix.
1548 TEST_F(ViewTest
, ActivateAccelerator
) {
1549 // Register a keyboard accelerator before the view is added to a window.
1550 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1551 TestView
* view
= new TestView();
1553 view
->AddAccelerator(return_accelerator
);
1554 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1556 // Create a window and add the view as its child.
1557 scoped_ptr
<Widget
> widget(new Widget
);
1558 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1559 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1560 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1561 widget
->Init(params
);
1562 View
* root
= widget
->GetRootView();
1563 root
->AddChildView(view
);
1566 // Get the focus manager.
1567 FocusManager
* focus_manager
= widget
->GetFocusManager();
1568 ASSERT_TRUE(focus_manager
);
1570 // Hit the return key and see if it takes effect.
1571 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1572 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1574 // Hit the escape key. Nothing should happen.
1575 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1576 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1577 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1578 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1580 // Now register the escape key and hit it again.
1581 view
->AddAccelerator(escape_accelerator
);
1582 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1583 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1584 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1586 // Remove the return key accelerator.
1587 view
->RemoveAccelerator(return_accelerator
);
1588 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1589 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1590 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1592 // Add it again. Hit the return key and the escape key.
1593 view
->AddAccelerator(return_accelerator
);
1594 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1595 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1596 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1597 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1598 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1599 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1601 // Remove all the accelerators.
1602 view
->ResetAccelerators();
1603 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1604 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1605 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1606 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1607 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1608 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1613 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1614 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1615 TestView
* view
= new TestView();
1617 view
->AddAccelerator(return_accelerator
);
1618 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1620 scoped_ptr
<Widget
> widget(new Widget
);
1621 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1622 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1623 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1624 widget
->Init(params
);
1625 View
* root
= widget
->GetRootView();
1626 root
->AddChildView(view
);
1629 FocusManager
* focus_manager
= widget
->GetFocusManager();
1630 ASSERT_TRUE(focus_manager
);
1632 view
->SetVisible(false);
1633 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1635 view
->SetVisible(true);
1636 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1641 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1642 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1643 TestView
* view
= new TestView();
1645 view
->AddAccelerator(return_accelerator
);
1646 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1648 scoped_ptr
<Widget
> widget(new Widget
);
1649 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1650 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1651 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1652 widget
->Init(params
);
1653 View
* root
= widget
->GetRootView();
1654 root
->AddChildView(view
);
1656 FocusManager
* focus_manager
= widget
->GetFocusManager();
1657 ASSERT_TRUE(focus_manager
);
1659 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1660 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1663 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1664 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1667 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1668 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1673 ////////////////////////////////////////////////////////////////////////////////
1674 // Mouse-wheel message rerouting
1675 ////////////////////////////////////////////////////////////////////////////////
1676 class ScrollableTestView
: public View
{
1678 ScrollableTestView() { }
1680 virtual gfx::Size
GetPreferredSize() {
1681 return gfx::Size(100, 10000);
1684 virtual void Layout() {
1685 SizeToPreferredSize();
1689 class TestViewWithControls
: public View
{
1691 TestViewWithControls() {
1692 text_field_
= new Textfield();
1693 AddChildView(text_field_
);
1696 Textfield
* text_field_
;
1699 class SimpleWidgetDelegate
: public WidgetDelegate
{
1701 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1703 virtual void DeleteDelegate() { delete this; }
1705 virtual View
* GetContentsView() { return contents_
; }
1707 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1708 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1714 // Tests that the mouse-wheel messages are correctly rerouted to the window
1716 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1718 // Note that this fails for a variety of reasons:
1719 // - focused view is apparently reset across window activations and never
1720 // properly restored
1721 // - this test depends on you not having any other window visible open under the
1722 // area that it opens the test windows. --beng
1723 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1724 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1725 Widget
* window1
= Widget::CreateWindowWithBounds(
1726 new SimpleWidgetDelegate(view_with_controls
),
1727 gfx::Rect(0, 0, 100, 100));
1729 ScrollView
* scroll_view
= new ScrollView();
1730 scroll_view
->SetContents(new ScrollableTestView());
1731 Widget
* window2
= Widget::CreateWindowWithBounds(
1732 new SimpleWidgetDelegate(scroll_view
),
1733 gfx::Rect(200, 200, 100, 100));
1735 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1737 // Make the window1 active, as this is what it would be in real-world.
1738 window1
->Activate();
1740 // Let's send a mouse-wheel message to the different controls and check that
1741 // it is rerouted to the window under the mouse (effectively scrolling the
1744 // First to the Window's HWND.
1745 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1746 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1747 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1749 window1
->CloseNow();
1750 window2
->CloseNow();
1754 ////////////////////////////////////////////////////////////////////////////////
1755 // Native view hierachy
1756 ////////////////////////////////////////////////////////////////////////////////
1757 class ToplevelWidgetObserverView
: public View
{
1759 ToplevelWidgetObserverView() : toplevel_(NULL
) {
1761 virtual ~ToplevelWidgetObserverView() {
1765 virtual void ViewHierarchyChanged(
1766 const ViewHierarchyChangedDetails
& details
) OVERRIDE
{
1767 if (details
.is_add
) {
1768 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1773 virtual void NativeViewHierarchyChanged() OVERRIDE
{
1774 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1777 Widget
* toplevel() { return toplevel_
; }
1782 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
1785 // Test that a view can track the current top level widget by overriding
1786 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1787 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
1788 scoped_ptr
<Widget
> toplevel1(new Widget
);
1789 Widget::InitParams toplevel1_params
=
1790 CreateParams(Widget::InitParams::TYPE_POPUP
);
1791 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1792 toplevel1
->Init(toplevel1_params
);
1794 scoped_ptr
<Widget
> toplevel2(new Widget
);
1795 Widget::InitParams toplevel2_params
=
1796 CreateParams(Widget::InitParams::TYPE_POPUP
);
1797 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1798 toplevel2
->Init(toplevel2_params
);
1800 Widget
* child
= new Widget
;
1801 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
1802 child_params
.parent
= toplevel1
->GetNativeView();
1803 child
->Init(child_params
);
1805 ToplevelWidgetObserverView
* observer_view
=
1806 new ToplevelWidgetObserverView();
1807 EXPECT_EQ(NULL
, observer_view
->toplevel());
1809 child
->SetContentsView(observer_view
);
1810 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
1812 Widget::ReparentNativeView(child
->GetNativeView(),
1813 toplevel2
->GetNativeView());
1814 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
1816 observer_view
->parent()->RemoveChildView(observer_view
);
1817 EXPECT_EQ(NULL
, observer_view
->toplevel());
1819 // Make |observer_view| |child|'s contents view again so that it gets deleted
1821 child
->SetContentsView(observer_view
);
1824 ////////////////////////////////////////////////////////////////////////////////
1826 ////////////////////////////////////////////////////////////////////////////////
1828 class TransformPaintView
: public TestView
{
1830 TransformPaintView() {}
1831 virtual ~TransformPaintView() {}
1833 void ClearScheduledPaintRect() {
1834 scheduled_paint_rect_
= gfx::Rect();
1837 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1839 // Overridden from View:
1840 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
{
1841 gfx::Rect xrect
= ConvertRectToParent(rect
);
1842 scheduled_paint_rect_
.Union(xrect
);
1846 gfx::Rect scheduled_paint_rect_
;
1848 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
1851 TEST_F(ViewTest
, TransformPaint
) {
1852 TransformPaintView
* v1
= new TransformPaintView();
1853 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1855 TestView
* v2
= new TestView();
1856 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1858 Widget
* widget
= new Widget
;
1859 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1860 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1861 widget
->Init(params
);
1863 View
* root
= widget
->GetRootView();
1865 root
->AddChildView(v1
);
1866 v1
->AddChildView(v2
);
1868 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1869 v1
->ClearScheduledPaintRect();
1870 v2
->SchedulePaint();
1872 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
1874 // Rotate |v1| counter-clockwise.
1875 gfx::Transform transform
;
1876 RotateCounterclockwise(&transform
);
1877 transform
.matrix().set(1, 3, 500.0);
1878 v1
->SetTransform(transform
);
1880 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1882 v1
->ClearScheduledPaintRect();
1883 v2
->SchedulePaint();
1885 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
1890 TEST_F(ViewTest
, TransformEvent
) {
1891 TestView
* v1
= new TestView();
1892 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1894 TestView
* v2
= new TestView();
1895 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1897 Widget
* widget
= new Widget
;
1898 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1899 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1900 widget
->Init(params
);
1901 View
* root
= widget
->GetRootView();
1903 root
->AddChildView(v1
);
1904 v1
->AddChildView(v2
);
1906 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1908 // Rotate |v1| counter-clockwise.
1909 gfx::Transform
transform(v1
->GetTransform());
1910 RotateCounterclockwise(&transform
);
1911 transform
.matrix().set(1, 3, 500.0);
1912 v1
->SetTransform(transform
);
1914 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1918 gfx::Point
p1(110, 210);
1919 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
1920 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1921 root
->OnMousePressed(pressed
);
1922 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1923 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1924 EXPECT_EQ(190, v2
->location_
.x());
1925 EXPECT_EQ(10, v2
->location_
.y());
1927 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
1929 root
->OnMouseReleased(released
);
1931 // Now rotate |v2| inside |v1| clockwise.
1932 transform
= v2
->GetTransform();
1933 RotateClockwise(&transform
);
1934 transform
.matrix().set(0, 3, 100.f
);
1935 v2
->SetTransform(transform
);
1937 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1938 // (300, 400) in |root|.
1943 gfx::Point
point2(110, 320);
1944 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
,
1945 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1946 root
->OnMousePressed(p2
);
1947 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1948 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1949 EXPECT_EQ(10, v2
->location_
.x());
1950 EXPECT_EQ(20, v2
->location_
.y());
1952 root
->OnMouseReleased(released
);
1954 v1
->SetTransform(gfx::Transform());
1955 v2
->SetTransform(gfx::Transform());
1957 TestView
* v3
= new TestView();
1958 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1959 v2
->AddChildView(v3
);
1961 // Rotate |v3| clockwise with respect to |v2|.
1962 transform
= v1
->GetTransform();
1963 RotateClockwise(&transform
);
1964 transform
.matrix().set(0, 3, 30.f
);
1965 v3
->SetTransform(transform
);
1967 // Scale |v2| with respect to |v1| along both axis.
1968 transform
= v2
->GetTransform();
1969 transform
.matrix().set(0, 0, 0.8f
);
1970 transform
.matrix().set(1, 1, 0.5f
);
1971 v2
->SetTransform(transform
);
1973 // |v3| occupies (108, 105) to (132, 115) in |root|.
1979 gfx::Point
point(112, 110);
1980 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
,
1981 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1982 root
->OnMousePressed(p3
);
1984 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1985 EXPECT_EQ(10, v3
->location_
.x());
1986 EXPECT_EQ(25, v3
->location_
.y());
1988 root
->OnMouseReleased(released
);
1990 v1
->SetTransform(gfx::Transform());
1991 v2
->SetTransform(gfx::Transform());
1992 v3
->SetTransform(gfx::Transform());
1998 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1999 transform
= v3
->GetTransform();
2000 RotateClockwise(&transform
);
2001 transform
.matrix().set(0, 3, 30.f
);
2002 // Rotation sets some scaling transformation. Using SetScale would overwrite
2003 // that and pollute the rotation. So combine the scaling with the existing
2005 gfx::Transform scale
;
2006 scale
.Scale(0.8f
, 0.5f
);
2007 transform
.ConcatTransform(scale
);
2008 v3
->SetTransform(transform
);
2010 // Translate |v2| with respect to |v1|.
2011 transform
= v2
->GetTransform();
2012 transform
.matrix().set(0, 3, 10.f
);
2013 transform
.matrix().set(1, 3, 10.f
);
2014 v2
->SetTransform(transform
);
2016 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2018 gfx::Point
point3(124, 125);
2019 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
,
2020 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2021 root
->OnMousePressed(p4
);
2023 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2024 EXPECT_EQ(10, v3
->location_
.x());
2025 EXPECT_EQ(25, v3
->location_
.y());
2027 root
->OnMouseReleased(released
);
2032 TEST_F(ViewTest
, TransformVisibleBound
) {
2033 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2035 scoped_ptr
<Widget
> widget(new Widget
);
2036 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2037 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2038 params
.bounds
= viewport_bounds
;
2039 widget
->Init(params
);
2040 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2042 View
* viewport
= new View
;
2043 widget
->SetContentsView(viewport
);
2044 View
* contents
= new View
;
2045 viewport
->AddChildView(contents
);
2046 viewport
->SetBoundsRect(viewport_bounds
);
2047 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2049 View
* child
= new View
;
2050 contents
->AddChildView(child
);
2051 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2052 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
2054 // Rotate |child| counter-clockwise
2055 gfx::Transform transform
;
2056 RotateCounterclockwise(&transform
);
2057 transform
.matrix().set(1, 3, 50.f
);
2058 child
->SetTransform(transform
);
2059 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
2064 ////////////////////////////////////////////////////////////////////////////////
2065 // OnVisibleBoundsChanged()
2067 class VisibleBoundsView
: public View
{
2069 VisibleBoundsView() : received_notification_(false) {}
2070 virtual ~VisibleBoundsView() {}
2072 bool received_notification() const { return received_notification_
; }
2073 void set_received_notification(bool received
) {
2074 received_notification_
= received
;
2078 // Overridden from View:
2079 virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE
{
2082 virtual void OnVisibleBoundsChanged() OVERRIDE
{
2083 received_notification_
= true;
2086 bool received_notification_
;
2088 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2091 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2092 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2094 scoped_ptr
<Widget
> widget(new Widget
);
2095 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2096 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2097 params
.bounds
= viewport_bounds
;
2098 widget
->Init(params
);
2099 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2101 View
* viewport
= new View
;
2102 widget
->SetContentsView(viewport
);
2103 View
* contents
= new View
;
2104 viewport
->AddChildView(contents
);
2105 viewport
->SetBoundsRect(viewport_bounds
);
2106 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2108 // Create a view that cares about visible bounds notifications, and position
2109 // it just outside the visible bounds of the viewport.
2110 VisibleBoundsView
* child
= new VisibleBoundsView
;
2111 contents
->AddChildView(child
);
2112 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2114 // The child bound should be fully clipped.
2115 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2117 // Now scroll the contents, but not enough to make the child visible.
2118 contents
->SetY(contents
->y() - 1);
2120 // We should have received the notification since the visible bounds may have
2121 // changed (even though they didn't).
2122 EXPECT_TRUE(child
->received_notification());
2123 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2124 child
->set_received_notification(false);
2126 // Now scroll the contents, this time by enough to make the child visible by
2128 contents
->SetY(contents
->y() - 10);
2129 EXPECT_TRUE(child
->received_notification());
2130 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2131 child
->set_received_notification(false);
2136 TEST_F(ViewTest
, SetBoundsPaint
) {
2138 TestView
* child_view
= new TestView
;
2140 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2141 top_view
.scheduled_paint_rects_
.clear();
2142 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2143 top_view
.AddChildView(child_view
);
2145 top_view
.scheduled_paint_rects_
.clear();
2146 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2147 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2149 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2150 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2151 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2152 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2155 // Assertions around painting and focus gain/lost.
2156 TEST_F(ViewTest
, FocusBlurPaints
) {
2157 TestView parent_view
;
2158 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
2160 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2162 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2163 parent_view
.AddChildView(child_view1
);
2165 parent_view
.scheduled_paint_rects_
.clear();
2166 child_view1
->scheduled_paint_rects_
.clear();
2168 // Focus change shouldn't trigger paints.
2169 child_view1
->DoFocus();
2171 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2172 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2174 child_view1
->DoBlur();
2175 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2176 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2179 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2180 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
2183 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2184 view
.InvalidateLayout();
2185 view
.scheduled_paint_rects_
.clear();
2186 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2187 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
2190 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2191 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
2192 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2194 // We have to put the View hierarchy into a Widget or no paints will be
2196 scoped_ptr
<Widget
> widget(new Widget
);
2197 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2198 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2199 params
.bounds
= viewport_bounds
;
2200 widget
->Init(params
);
2201 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2203 TestView
* parent_view
= new TestView
;
2204 widget
->SetContentsView(parent_view
);
2205 parent_view
->SetBoundsRect(viewport_bounds
);
2206 parent_view
->scheduled_paint_rects_
.clear();
2208 View
* child_view
= new View
;
2209 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2210 parent_view
->AddChildView(child_view
);
2211 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2212 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2214 parent_view
->scheduled_paint_rects_
.clear();
2215 parent_view
->RemoveChildView(child_view
);
2216 scoped_ptr
<View
> child_deleter(child_view
);
2217 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2218 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2223 // Tests conversion methods with a transform.
2224 TEST_F(ViewTest
, ConversionsWithTransform
) {
2227 // View hierarchy used to test scale transforms.
2228 TestView
* child
= new TestView
;
2229 TestView
* child_child
= new TestView
;
2231 // View used to test a rotation transform.
2232 TestView
* child_2
= new TestView
;
2234 top_view
.AddChildView(child
);
2235 child
->AddChildView(child_child
);
2237 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2239 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2240 gfx::Transform transform
;
2241 transform
.Scale(3.0, 4.0);
2242 child
->SetTransform(transform
);
2244 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2245 transform
.MakeIdentity();
2246 transform
.Scale(5.0, 7.0);
2247 child_child
->SetTransform(transform
);
2249 top_view
.AddChildView(child_2
);
2250 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2251 transform
.MakeIdentity();
2252 RotateClockwise(&transform
);
2253 child_2
->SetTransform(transform
);
2255 // Sanity check to make sure basic transforms act as expected.
2257 gfx::Transform transform
;
2258 transform
.Translate(110.0, -110.0);
2259 transform
.Scale(100.0, 55.0);
2260 transform
.Translate(1.0, 1.0);
2262 // convert to a 3x3 matrix.
2263 const SkMatrix
& matrix
= transform
.matrix();
2265 EXPECT_EQ(210, matrix
.getTranslateX());
2266 EXPECT_EQ(-55, matrix
.getTranslateY());
2267 EXPECT_EQ(100, matrix
.getScaleX());
2268 EXPECT_EQ(55, matrix
.getScaleY());
2269 EXPECT_EQ(0, matrix
.getSkewX());
2270 EXPECT_EQ(0, matrix
.getSkewY());
2274 gfx::Transform transform
;
2275 transform
.Translate(1.0, 1.0);
2277 t2
.Scale(100.0, 55.0);
2279 t3
.Translate(110.0, -110.0);
2280 transform
.ConcatTransform(t2
);
2281 transform
.ConcatTransform(t3
);
2283 // convert to a 3x3 matrix
2284 const SkMatrix
& matrix
= transform
.matrix();
2286 EXPECT_EQ(210, matrix
.getTranslateX());
2287 EXPECT_EQ(-55, matrix
.getTranslateY());
2288 EXPECT_EQ(100, matrix
.getScaleX());
2289 EXPECT_EQ(55, matrix
.getScaleY());
2290 EXPECT_EQ(0, matrix
.getSkewX());
2291 EXPECT_EQ(0, matrix
.getSkewY());
2294 // Conversions from child->top and top->child.
2296 gfx::Point
point(5, 5);
2297 View::ConvertPointToTarget(child
, &top_view
, &point
);
2298 EXPECT_EQ(22, point
.x());
2299 EXPECT_EQ(39, point
.y());
2301 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2302 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2303 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2304 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2305 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2306 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2308 point
.SetPoint(22, 39);
2309 View::ConvertPointToTarget(&top_view
, child
, &point
);
2310 EXPECT_EQ(5, point
.x());
2311 EXPECT_EQ(5, point
.y());
2313 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2314 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2315 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2316 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2317 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2318 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2321 // Conversions from child_child->top and top->child_child.
2323 gfx::Point
point(5, 5);
2324 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2325 EXPECT_EQ(133, point
.x());
2326 EXPECT_EQ(211, point
.y());
2328 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2329 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2330 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2331 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2332 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2333 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2335 point
.SetPoint(133, 211);
2336 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2337 EXPECT_EQ(5, point
.x());
2338 EXPECT_EQ(5, point
.y());
2340 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2341 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2342 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2343 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2344 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2345 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2348 // Conversions from child_child->child and child->child_child
2350 gfx::Point
point(5, 5);
2351 View::ConvertPointToTarget(child_child
, child
, &point
);
2352 EXPECT_EQ(42, point
.x());
2353 EXPECT_EQ(48, point
.y());
2355 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2356 View::ConvertRectToTarget(child_child
, child
, &rect
);
2357 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2358 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2359 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2360 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2362 point
.SetPoint(42, 48);
2363 View::ConvertPointToTarget(child
, child_child
, &point
);
2364 EXPECT_EQ(5, point
.x());
2365 EXPECT_EQ(5, point
.y());
2367 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2368 View::ConvertRectToTarget(child
, child_child
, &rect
);
2369 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2370 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2371 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2372 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2375 // Conversions from top_view to child with a value that should be negative.
2376 // This ensures we don't round up with negative numbers.
2378 gfx::Point
point(6, 18);
2379 View::ConvertPointToTarget(&top_view
, child
, &point
);
2380 EXPECT_EQ(-1, point
.x());
2381 EXPECT_EQ(-1, point
.y());
2383 float error
= 0.01f
;
2384 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2385 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2386 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2387 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2388 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2389 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2392 // Rect conversions from top_view->child_2 and child_2->top_view.
2394 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2395 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2396 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2397 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2398 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2399 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2401 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2402 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2403 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2404 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2405 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2406 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2410 // Tests conversion methods to and from screen coordinates.
2411 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2412 scoped_ptr
<Widget
> widget(new Widget
);
2413 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2414 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2415 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2416 widget
->Init(params
);
2418 View
* child
= new View
;
2419 widget
->GetRootView()->AddChildView(child
);
2420 child
->SetBounds(10, 10, 100, 200);
2423 child
->SetTransform(t
);
2425 gfx::Point
point_in_screen(100, 90);
2426 gfx::Point
point_in_child(80, 60);
2428 gfx::Point point
= point_in_screen
;
2429 View::ConvertPointFromScreen(child
, &point
);
2430 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2432 View::ConvertPointToScreen(child
, &point
);
2433 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2436 // Tests conversion methods for rectangles.
2437 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2438 scoped_ptr
<Widget
> widget(new Widget
);
2439 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2440 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2441 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2442 widget
->Init(params
);
2443 View
* root
= widget
->GetRootView();
2445 TestView
* v1
= new TestView
;
2446 TestView
* v2
= new TestView
;
2447 root
->AddChildView(v1
);
2448 v1
->AddChildView(v2
);
2450 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2451 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2453 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2454 gfx::Rect
rect(5, 5, 15, 40);
2455 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2456 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2460 RotateCounterclockwise(&t2
);
2461 t2
.matrix().set(1, 3, 100.f
);
2462 v2
->SetTransform(t2
);
2464 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2465 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2466 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2471 v1
->SetTransform(t1
);
2473 // The rectangle should remain the same for |v1|.
2474 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2476 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2477 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2478 v2
->ConvertRectToWidget(rect
).ToString());
2483 class ObserverView
: public View
{
2486 virtual ~ObserverView();
2488 void ResetTestState();
2490 bool has_add_details() const { return has_add_details_
; }
2491 bool has_remove_details() const { return has_remove_details_
; }
2493 const ViewHierarchyChangedDetails
& add_details() const {
2494 return add_details_
;
2497 const ViewHierarchyChangedDetails
& remove_details() const {
2498 return remove_details_
;
2503 virtual void ViewHierarchyChanged(
2504 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
2506 bool has_add_details_
;
2507 bool has_remove_details_
;
2508 ViewHierarchyChangedDetails add_details_
;
2509 ViewHierarchyChangedDetails remove_details_
;
2511 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2514 ObserverView::ObserverView()
2515 : has_add_details_(false),
2516 has_remove_details_(false) {
2519 ObserverView::~ObserverView() {}
2521 void ObserverView::ResetTestState() {
2522 has_add_details_
= false;
2523 has_remove_details_
= false;
2524 add_details_
= ViewHierarchyChangedDetails();
2525 remove_details_
= ViewHierarchyChangedDetails();
2528 void ObserverView::ViewHierarchyChanged(
2529 const ViewHierarchyChangedDetails
& details
) {
2530 if (details
.is_add
) {
2531 has_add_details_
= true;
2532 add_details_
= details
;
2534 has_remove_details_
= true;
2535 remove_details_
= details
;
2539 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2540 // a child view is added or removed to all the views in the hierarchy (up and
2542 // The tree looks like this:
2546 // +-- v4 (starts here, then get reparented to v1)
2547 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2550 ObserverView
* v3
= new ObserverView();
2552 // Add |v3| to |v2|.
2553 scoped_ptr
<ObserverView
> v2(new ObserverView());
2554 v2
->AddChildView(v3
);
2556 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2558 EXPECT_TRUE(v2
->has_add_details());
2559 EXPECT_FALSE(v2
->has_remove_details());
2560 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2561 EXPECT_EQ(v3
, v2
->add_details().child
);
2562 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2564 EXPECT_TRUE(v3
->has_add_details());
2565 EXPECT_FALSE(v3
->has_remove_details());
2566 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2567 EXPECT_EQ(v3
, v3
->add_details().child
);
2568 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2570 // Reset everything to the initial state.
2571 v2
->ResetTestState();
2572 v3
->ResetTestState();
2575 v1
.AddChildView(v2
.get());
2577 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2578 // Make sure all the views (v1, v2, v3) received _that_ information.
2579 EXPECT_TRUE(v1
.has_add_details());
2580 EXPECT_FALSE(v1
.has_remove_details());
2581 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2582 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2583 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2585 EXPECT_TRUE(v2
->has_add_details());
2586 EXPECT_FALSE(v2
->has_remove_details());
2587 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2588 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2589 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2591 EXPECT_TRUE(v3
->has_add_details());
2592 EXPECT_FALSE(v3
->has_remove_details());
2593 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2594 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2595 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2597 // Reset everything to the initial state.
2598 v1
.ResetTestState();
2599 v2
->ResetTestState();
2600 v3
->ResetTestState();
2602 // Remove |v2| from |v1|.
2603 v1
.RemoveChildView(v2
.get());
2605 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2606 // Make sure all the views (v1, v2, v3) received _that_ information.
2607 EXPECT_FALSE(v1
.has_add_details());
2608 EXPECT_TRUE(v1
.has_remove_details());
2609 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2610 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2611 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2613 EXPECT_FALSE(v2
->has_add_details());
2614 EXPECT_TRUE(v2
->has_remove_details());
2615 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2616 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2617 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2619 EXPECT_FALSE(v3
->has_add_details());
2620 EXPECT_TRUE(v3
->has_remove_details());
2621 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2622 EXPECT_EQ(v3
, v3
->remove_details().child
);
2623 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2625 // Verifies notifications when reparenting a view.
2626 ObserverView
* v4
= new ObserverView();
2627 // Add |v4| to |v2|.
2628 v2
->AddChildView(v4
);
2630 // Reset everything to the initial state.
2631 v1
.ResetTestState();
2632 v2
->ResetTestState();
2633 v3
->ResetTestState();
2634 v4
->ResetTestState();
2636 // Reparent |v4| to |v1|.
2637 v1
.AddChildView(v4
);
2639 // Verifies that all views receive the correct information for all the child,
2640 // parent and move views.
2642 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2643 EXPECT_TRUE(v1
.has_add_details());
2644 EXPECT_FALSE(v1
.has_remove_details());
2645 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2646 EXPECT_EQ(v4
, v1
.add_details().child
);
2647 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2649 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2651 EXPECT_FALSE(v2
->has_add_details());
2652 EXPECT_TRUE(v2
->has_remove_details());
2653 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2654 EXPECT_EQ(v4
, v2
->remove_details().child
);
2655 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2657 // |v3| is not impacted by this operation, and hence receives no notification.
2658 EXPECT_FALSE(v3
->has_add_details());
2659 EXPECT_FALSE(v3
->has_remove_details());
2661 // |v4| is the reparented child, so it receives notifications for the remove
2662 // and then the add. |v2| is its old parent, |v1| is its new parent.
2663 EXPECT_TRUE(v4
->has_remove_details());
2664 EXPECT_TRUE(v4
->has_add_details());
2665 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2666 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2667 EXPECT_EQ(v4
, v4
->add_details().child
);
2668 EXPECT_EQ(v4
, v4
->remove_details().child
);
2669 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2670 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2673 // Verifies if the child views added under the root are all deleted when calling
2674 // RemoveAllChildViews.
2675 // The tree looks like this:
2684 TEST_F(ViewTest
, RemoveAllChildViews
) {
2687 View
* child1
= new View
;
2688 root
.AddChildView(child1
);
2690 for (int i
= 0; i
< 2; ++i
)
2691 root
.AddChildView(new View
);
2693 View
* foo
= new View
;
2694 child1
->AddChildView(foo
);
2696 // Add some nodes to |foo|.
2697 for (int i
= 0; i
< 3; ++i
)
2698 foo
->AddChildView(new View
);
2700 EXPECT_EQ(3, root
.child_count());
2701 EXPECT_EQ(1, child1
->child_count());
2702 EXPECT_EQ(3, foo
->child_count());
2704 // Now remove all child views from root.
2705 root
.RemoveAllChildViews(true);
2707 EXPECT_EQ(0, root
.child_count());
2708 EXPECT_FALSE(root
.has_children());
2711 TEST_F(ViewTest
, Contains
) {
2713 View
* v2
= new View
;
2714 View
* v3
= new View
;
2716 v1
.AddChildView(v2
);
2717 v2
->AddChildView(v3
);
2719 EXPECT_FALSE(v1
.Contains(NULL
));
2720 EXPECT_TRUE(v1
.Contains(&v1
));
2721 EXPECT_TRUE(v1
.Contains(v2
));
2722 EXPECT_TRUE(v1
.Contains(v3
));
2724 EXPECT_FALSE(v2
->Contains(NULL
));
2725 EXPECT_TRUE(v2
->Contains(v2
));
2726 EXPECT_FALSE(v2
->Contains(&v1
));
2727 EXPECT_TRUE(v2
->Contains(v3
));
2729 EXPECT_FALSE(v3
->Contains(NULL
));
2730 EXPECT_TRUE(v3
->Contains(v3
));
2731 EXPECT_FALSE(v3
->Contains(&v1
));
2732 EXPECT_FALSE(v3
->Contains(v2
));
2735 // Verifies if GetIndexOf() returns the correct index for the specified child
2737 // The tree looks like this:
2742 TEST_F(ViewTest
, GetIndexOf
) {
2745 View
* child1
= new View
;
2746 root
.AddChildView(child1
);
2748 View
* child2
= new View
;
2749 root
.AddChildView(child2
);
2751 View
* foo1
= new View
;
2752 child1
->AddChildView(foo1
);
2754 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2755 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2756 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2757 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2758 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2760 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2761 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2762 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2763 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2764 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2766 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2767 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2768 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2769 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2770 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2773 // Verifies that the child views can be reordered correctly.
2774 TEST_F(ViewTest
, ReorderChildren
) {
2777 View
* child
= new View();
2778 root
.AddChildView(child
);
2780 View
* foo1
= new View();
2781 child
->AddChildView(foo1
);
2782 View
* foo2
= new View();
2783 child
->AddChildView(foo2
);
2784 View
* foo3
= new View();
2785 child
->AddChildView(foo3
);
2786 foo1
->SetFocusable(true);
2787 foo2
->SetFocusable(true);
2788 foo3
->SetFocusable(true);
2790 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2791 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2792 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2793 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2794 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2795 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2797 // Move |foo2| at the end.
2798 child
->ReorderChildView(foo2
, -1);
2799 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2800 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2801 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2802 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2803 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2804 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2806 // Move |foo1| at the end.
2807 child
->ReorderChildView(foo1
, -1);
2808 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2809 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2810 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2811 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2812 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2813 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2814 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2816 // Move |foo2| to the front.
2817 child
->ReorderChildView(foo2
, 0);
2818 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2819 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2820 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2821 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2822 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2823 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2824 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2827 // Verifies that GetViewByID returns the correctly child view from the specified
2829 // The tree looks like this:
2834 TEST_F(ViewTest
, GetViewByID
) {
2836 const int kV1ID
= 1;
2840 const int kV2ID
= 2;
2844 const int kV3ID
= 3;
2848 const int kV4ID
= 4;
2851 const int kV5ID
= 5;
2853 v1
.AddChildView(&v2
);
2854 v2
.AddChildView(&v3
);
2855 v2
.AddChildView(&v4
);
2857 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2858 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2859 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2861 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2862 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2864 const int kGroup
= 1;
2865 v3
.SetGroup(kGroup
);
2866 v4
.SetGroup(kGroup
);
2869 v1
.GetViewsInGroup(kGroup
, &views
);
2870 EXPECT_EQ(2U, views
.size());
2872 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2873 EXPECT_NE(views
.end(), i
);
2875 i
= std::find(views
.begin(), views
.end(), &v4
);
2876 EXPECT_NE(views
.end(), i
);
2879 TEST_F(ViewTest
, AddExistingChild
) {
2882 v1
.AddChildView(&v2
);
2883 v1
.AddChildView(&v3
);
2884 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2885 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2887 // Check that there's no change in order when adding at same index.
2888 v1
.AddChildViewAt(&v2
, 0);
2889 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2890 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2891 v1
.AddChildViewAt(&v3
, 1);
2892 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2893 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2895 // Add it at a different index and check for change in order.
2896 v1
.AddChildViewAt(&v2
, 1);
2897 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
2898 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
2899 v1
.AddChildViewAt(&v2
, 0);
2900 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2901 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2903 // Check that calling |AddChildView()| does not change the order.
2904 v1
.AddChildView(&v2
);
2905 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2906 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2907 v1
.AddChildView(&v3
);
2908 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2909 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2912 ////////////////////////////////////////////////////////////////////////////////
2914 ////////////////////////////////////////////////////////////////////////////////
2918 // Test implementation of LayerAnimator.
2919 class TestLayerAnimator
: public ui::LayerAnimator
{
2921 TestLayerAnimator();
2923 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
2926 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
2929 virtual ~TestLayerAnimator() { }
2932 gfx::Rect last_bounds_
;
2934 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
2937 TestLayerAnimator::TestLayerAnimator()
2938 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2941 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
2942 last_bounds_
= bounds
;
2947 class ViewLayerTest
: public ViewsTestBase
{
2949 ViewLayerTest() : widget_(NULL
) {}
2951 virtual ~ViewLayerTest() {
2954 // Returns the Layer used by the RootView.
2955 ui::Layer
* GetRootLayer() {
2956 return widget()->GetLayer();
2959 virtual void SetUp() OVERRIDE
{
2961 widget_
= new Widget
;
2962 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2963 params
.bounds
= gfx::Rect(50, 50, 200, 200);
2964 widget_
->Init(params
);
2966 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
2969 virtual void TearDown() OVERRIDE
{
2970 widget_
->CloseNow();
2971 ViewsTestBase::TearDown();
2974 Widget
* widget() { return widget_
; }
2981 TEST_F(ViewLayerTest
, LayerToggling
) {
2982 // Because we lazily create textures the calls to DrawTree are necessary to
2983 // ensure we trigger creation of textures.
2984 ui::Layer
* root_layer
= widget()->GetLayer();
2985 View
* content_view
= new View
;
2986 widget()->SetContentsView(content_view
);
2988 // Create v1, give it a bounds and verify everything is set up correctly.
2989 View
* v1
= new View
;
2990 v1
->SetPaintToLayer(true);
2991 EXPECT_TRUE(v1
->layer() != NULL
);
2992 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2993 content_view
->AddChildView(v1
);
2994 ASSERT_TRUE(v1
->layer() != NULL
);
2995 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2996 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
2998 // Create v2 as a child of v1 and do basic assertion testing.
2999 View
* v2
= new View
;
3000 v1
->AddChildView(v2
);
3001 EXPECT_TRUE(v2
->layer() == NULL
);
3002 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
3003 v2
->SetPaintToLayer(true);
3004 ASSERT_TRUE(v2
->layer() != NULL
);
3005 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3006 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3008 // Turn off v1s layer. v2 should still have a layer but its parent should have
3010 v1
->SetPaintToLayer(false);
3011 EXPECT_TRUE(v1
->layer() == NULL
);
3012 EXPECT_TRUE(v2
->layer() != NULL
);
3013 EXPECT_EQ(root_layer
, v2
->layer()->parent());
3014 ASSERT_EQ(1u, root_layer
->children().size());
3015 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
3016 // The bounds of the layer should have changed to be relative to the root view
3018 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
3020 // Make v1 have a layer again and verify v2s layer is wired up correctly.
3021 gfx::Transform transform
;
3022 transform
.Scale(2.0, 2.0);
3023 v1
->SetTransform(transform
);
3024 EXPECT_TRUE(v1
->layer() != NULL
);
3025 EXPECT_TRUE(v2
->layer() != NULL
);
3026 EXPECT_EQ(root_layer
, v1
->layer()->parent());
3027 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3028 ASSERT_EQ(1u, root_layer
->children().size());
3029 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
3030 ASSERT_EQ(1u, v1
->layer()->children().size());
3031 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
3032 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3035 // Verifies turning on a layer wires up children correctly.
3036 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
3037 View
* content_view
= new View
;
3038 widget()->SetContentsView(content_view
);
3040 // Create v1, give it a bounds and verify everything is set up correctly.
3041 View
* v1
= new View
;
3042 content_view
->AddChildView(v1
);
3043 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3045 View
* v2
= new View
;
3046 v1
->AddChildView(v2
);
3048 View
* v3
= new View
;
3049 v3
->SetPaintToLayer(true);
3050 v2
->AddChildView(v3
);
3051 ASSERT_TRUE(v3
->layer() != NULL
);
3053 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
3055 v1
->SetPaintToLayer(true);
3056 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
3059 TEST_F(ViewLayerTest
, LayerAnimator
) {
3060 View
* content_view
= new View
;
3061 widget()->SetContentsView(content_view
);
3063 View
* v1
= new View
;
3064 content_view
->AddChildView(v1
);
3065 v1
->SetPaintToLayer(true);
3066 EXPECT_TRUE(v1
->layer() != NULL
);
3068 TestLayerAnimator
* animator
= new TestLayerAnimator();
3069 v1
->layer()->SetAnimator(animator
);
3071 gfx::Rect
bounds(1, 2, 3, 4);
3072 v1
->SetBoundsRect(bounds
);
3073 EXPECT_EQ(bounds
, animator
->last_bounds());
3074 // TestLayerAnimator doesn't update the layer.
3075 EXPECT_NE(bounds
, v1
->layer()->bounds());
3078 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3079 // doesn't have a layer change.
3080 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
3081 View
* content_view
= new View
;
3082 widget()->SetContentsView(content_view
);
3084 View
* v1
= new View
;
3085 content_view
->AddChildView(v1
);
3086 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3088 View
* v2
= new View
;
3089 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3090 v1
->AddChildView(v2
);
3091 v2
->SetPaintToLayer(true);
3092 ASSERT_TRUE(v2
->layer() != NULL
);
3093 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
3095 v1
->SetPosition(gfx::Point(25, 36));
3096 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
3098 v2
->SetPosition(gfx::Point(11, 12));
3099 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
3101 // Bounds of the layer should change even if the view is not invisible.
3102 v1
->SetVisible(false);
3103 v1
->SetPosition(gfx::Point(20, 30));
3104 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
3106 v2
->SetVisible(false);
3107 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3108 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
3111 // Make sure layers are positioned correctly in RTL.
3112 TEST_F(ViewLayerTest
, BoundInRTL
) {
3113 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3114 base::i18n::SetICUDefaultLocale("he");
3116 View
* view
= new View
;
3117 widget()->SetContentsView(view
);
3119 int content_width
= view
->width();
3121 // |v1| is initially not attached to anything. So its layer will have the same
3122 // bounds as the view.
3123 View
* v1
= new View
;
3124 v1
->SetPaintToLayer(true);
3125 v1
->SetBounds(10, 10, 20, 10);
3126 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3127 v1
->layer()->bounds());
3129 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3131 view
->AddChildView(v1
);
3132 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3133 v1
->layer()->bounds());
3134 gfx::Rect l1bounds
= v1
->layer()->bounds();
3136 // Now attach a View to the widget first, then create a layer for it. Make
3137 // sure the bounds are correct.
3138 View
* v2
= new View
;
3139 v2
->SetBounds(50, 10, 30, 10);
3140 EXPECT_FALSE(v2
->layer());
3141 view
->AddChildView(v2
);
3142 v2
->SetPaintToLayer(true);
3143 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
3144 v2
->layer()->bounds());
3145 gfx::Rect l2bounds
= v2
->layer()->bounds();
3147 view
->SetPaintToLayer(true);
3148 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3149 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3151 // Move one of the views. Make sure the layer is positioned correctly
3153 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
3154 l1bounds
.set_x(l1bounds
.x() + 5);
3155 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3157 view
->SetPaintToLayer(false);
3158 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3159 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3161 // Move a view again.
3162 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
3163 l2bounds
.set_x(l2bounds
.x() - 5);
3164 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3167 base::i18n::SetICUDefaultLocale(locale
);
3170 // Makes sure a transform persists after toggling the visibility.
3171 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3172 View
* view
= new View
;
3173 gfx::Transform transform
;
3174 transform
.Scale(2.0, 2.0);
3175 view
->SetTransform(transform
);
3176 widget()->SetContentsView(view
);
3177 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3179 view
->SetVisible(false);
3180 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3182 view
->SetVisible(true);
3183 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3186 // Verifies a transform persists after removing/adding a view with a transform.
3187 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3188 View
* view
= new View
;
3189 gfx::Transform transform
;
3190 transform
.Scale(2.0, 2.0);
3191 view
->SetTransform(transform
);
3192 widget()->SetContentsView(view
);
3193 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3194 ASSERT_TRUE(view
->layer() != NULL
);
3195 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3197 View
* parent
= view
->parent();
3198 parent
->RemoveChildView(view
);
3199 parent
->AddChildView(view
);
3201 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3202 ASSERT_TRUE(view
->layer() != NULL
);
3203 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3206 // Makes sure that layer visibility is correct after toggling View visibility.
3207 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3208 View
* content_view
= new View
;
3209 widget()->SetContentsView(content_view
);
3211 // The view isn't attached to a widget or a parent view yet. But it should
3212 // still have a layer, but the layer should not be attached to the root
3214 View
* v1
= new View
;
3215 v1
->SetPaintToLayer(true);
3216 EXPECT_TRUE(v1
->layer());
3217 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3220 // Once the view is attached to a widget, its layer should be attached to the
3221 // root layer and visible.
3222 content_view
->AddChildView(v1
);
3223 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3225 EXPECT_TRUE(v1
->layer()->IsDrawn());
3227 v1
->SetVisible(false);
3228 EXPECT_FALSE(v1
->layer()->IsDrawn());
3230 v1
->SetVisible(true);
3231 EXPECT_TRUE(v1
->layer()->IsDrawn());
3234 EXPECT_FALSE(v1
->layer()->IsDrawn());
3237 EXPECT_TRUE(v1
->layer()->IsDrawn());
3240 // Tests that the layers in the subtree are orphaned after a View is removed
3242 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3243 View
* content_view
= new View
;
3244 widget()->SetContentsView(content_view
);
3246 View
* v1
= new View
;
3247 content_view
->AddChildView(v1
);
3249 View
* v2
= new View
;
3250 v1
->AddChildView(v2
);
3251 v2
->SetPaintToLayer(true);
3252 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3254 EXPECT_TRUE(v2
->layer()->IsDrawn());
3256 content_view
->RemoveChildView(v1
);
3258 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3262 content_view
->AddChildView(v2
);
3265 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3267 EXPECT_TRUE(v2
->layer()->IsDrawn());
3270 class PaintTrackingView
: public View
{
3272 PaintTrackingView() : painted_(false) {
3275 bool painted() const { return painted_
; }
3276 void set_painted(bool value
) { painted_
= value
; }
3278 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
3285 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3288 // Makes sure child views with layers aren't painted when paint starts at an
3290 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3291 PaintTrackingView
* content_view
= new PaintTrackingView
;
3292 widget()->SetContentsView(content_view
);
3293 content_view
->SetPaintToLayer(true);
3294 GetRootLayer()->GetCompositor()->ScheduleDraw();
3295 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3296 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3297 content_view
->set_painted(false);
3298 // content_view no longer has a dirty rect. Paint from the root and make sure
3299 // PaintTrackingView isn't painted.
3300 GetRootLayer()->GetCompositor()->ScheduleDraw();
3301 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3302 EXPECT_FALSE(content_view
->painted());
3304 // Make content_view have a dirty rect, paint the layers and make sure
3305 // PaintTrackingView is painted.
3306 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3307 GetRootLayer()->GetCompositor()->ScheduleDraw();
3308 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3309 EXPECT_TRUE(content_view
->painted());
3312 // Tests that the visibility of child layers are updated correctly when a View's
3313 // visibility changes.
3314 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3315 View
* v1
= new View
;
3316 v1
->SetPaintToLayer(true);
3317 widget()->SetContentsView(v1
);
3319 View
* v2
= new View
;
3320 v1
->AddChildView(v2
);
3322 View
* v3
= new View
;
3323 v2
->AddChildView(v3
);
3324 v3
->SetVisible(false);
3326 View
* v4
= new View
;
3327 v4
->SetPaintToLayer(true);
3328 v3
->AddChildView(v4
);
3330 EXPECT_TRUE(v1
->layer()->IsDrawn());
3331 EXPECT_FALSE(v4
->layer()->IsDrawn());
3333 v2
->SetVisible(false);
3334 EXPECT_TRUE(v1
->layer()->IsDrawn());
3335 EXPECT_FALSE(v4
->layer()->IsDrawn());
3337 v2
->SetVisible(true);
3338 EXPECT_TRUE(v1
->layer()->IsDrawn());
3339 EXPECT_FALSE(v4
->layer()->IsDrawn());
3341 v2
->SetVisible(false);
3342 EXPECT_TRUE(v1
->layer()->IsDrawn());
3343 EXPECT_FALSE(v4
->layer()->IsDrawn());
3344 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3346 v3
->SetVisible(true);
3347 EXPECT_TRUE(v1
->layer()->IsDrawn());
3348 EXPECT_FALSE(v4
->layer()->IsDrawn());
3349 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3351 // Reparent |v3| to |v1|.
3352 v1
->AddChildView(v3
);
3353 EXPECT_TRUE(v1
->layer()->IsDrawn());
3354 EXPECT_TRUE(v4
->layer()->IsDrawn());
3355 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3358 // This test creates a random View tree, and then randomly reorders child views,
3359 // reparents views etc. Unrelated changes can appear to break this test. So
3360 // marking this as FLAKY.
3361 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3362 View
* content
= new View
;
3363 content
->SetPaintToLayer(true);
3364 widget()->SetContentsView(content
);
3367 ConstructTree(content
, 5);
3368 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3370 ScrambleTree(content
);
3371 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3373 ScrambleTree(content
);
3374 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3376 ScrambleTree(content
);
3377 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3380 // Verifies when views are reordered the layer is also reordered. The widget is
3381 // providing the parent layer.
3382 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3383 View
* content
= new View
;
3384 widget()->SetContentsView(content
);
3385 View
* c1
= new View
;
3386 c1
->SetPaintToLayer(true);
3387 content
->AddChildView(c1
);
3388 View
* c2
= new View
;
3389 c2
->SetPaintToLayer(true);
3390 content
->AddChildView(c2
);
3392 ui::Layer
* parent_layer
= c1
->layer()->parent();
3393 ASSERT_TRUE(parent_layer
);
3394 ASSERT_EQ(2u, parent_layer
->children().size());
3395 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3396 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3398 // Move c1 to the front. The layers should have moved too.
3399 content
->ReorderChildView(c1
, -1);
3400 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3401 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3404 // Verifies that the layer of a view can be acquired properly.
3405 TEST_F(ViewLayerTest
, AcquireLayer
) {
3406 View
* content
= new View
;
3407 widget()->SetContentsView(content
);
3408 scoped_ptr
<View
> c1(new View
);
3409 c1
->SetPaintToLayer(true);
3410 EXPECT_TRUE(c1
->layer());
3411 content
->AddChildView(c1
.get());
3413 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3414 EXPECT_EQ(layer
.get(), c1
->layer());
3416 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3417 EXPECT_NE(c1
->layer(), layer2
.get());
3419 // Destroy view before destroying layer.
3423 // Verify the z-order of the layers as a result of calling RecreateLayer().
3424 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3425 scoped_ptr
<View
> v(new View());
3426 v
->SetPaintToLayer(true);
3428 View
* v1
= new View();
3429 v1
->SetPaintToLayer(true);
3430 v
->AddChildView(v1
);
3431 View
* v2
= new View();
3432 v2
->SetPaintToLayer(true);
3433 v
->AddChildView(v2
);
3435 // Test the initial z-order.
3436 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3437 ASSERT_EQ(2u, child_layers_pre
.size());
3438 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3439 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3441 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3443 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3444 // for |v1| and |v2|.
3445 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3446 ASSERT_EQ(3u, child_layers_post
.size());
3447 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3448 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3449 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3452 // Verify the z-order of the layers as a result of calling RecreateLayer when
3453 // the widget is the parent with the layer.
3454 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3455 View
* v
= new View();
3456 widget()->SetContentsView(v
);
3458 View
* v1
= new View();
3459 v1
->SetPaintToLayer(true);
3460 v
->AddChildView(v1
);
3461 View
* v2
= new View();
3462 v2
->SetPaintToLayer(true);
3463 v
->AddChildView(v2
);
3465 ui::Layer
* root_layer
= GetRootLayer();
3467 // Test the initial z-order.
3468 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3469 ASSERT_EQ(2u, child_layers_pre
.size());
3470 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3471 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3473 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3475 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3476 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3477 ASSERT_EQ(3u, child_layers_post
.size());
3478 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3479 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3480 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3483 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3485 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3487 v
.SetPaintToLayer(true);
3489 child
.SetPaintToLayer(true);
3490 v
.AddChildView(&child
);
3491 ASSERT_TRUE(v
.layer() != NULL
);
3492 ASSERT_EQ(1u, v
.layer()->children().size());
3493 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3495 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3496 v
.layer()->Add(&layer
);
3497 v
.layer()->StackAtBottom(&layer
);
3499 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3501 // All children should be moved from old layer to new layer.
3502 ASSERT_TRUE(old_layer
.get() != NULL
);
3503 EXPECT_TRUE(old_layer
->children().empty());
3505 // And new layer should have the two children.
3506 ASSERT_TRUE(v
.layer() != NULL
);
3507 ASSERT_EQ(2u, v
.layer()->children().size());
3508 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3509 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3512 class BoundsTreeTestView
: public View
{
3514 BoundsTreeTestView() {}
3516 virtual void PaintChildren(gfx::Canvas
* canvas
,
3517 const CullSet
& cull_set
) OVERRIDE
{
3518 // Save out a copy of the cull_set before calling the base implementation.
3519 last_cull_set_
.clear();
3520 if (cull_set
.cull_set_
) {
3521 for (base::hash_set
<intptr_t>::iterator it
= cull_set
.cull_set_
->begin();
3522 it
!= cull_set
.cull_set_
->end();
3524 last_cull_set_
.insert(reinterpret_cast<View
*>(*it
));
3527 View::PaintChildren(canvas
, cull_set
);
3530 std::set
<View
*> last_cull_set_
;
3533 TEST_F(ViewLayerTest
, BoundsTreePaintUpdatesCullSet
) {
3534 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3535 widget()->SetContentsView(test_view
);
3537 View
* v1
= new View();
3538 v1
->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3539 test_view
->AddChildView(v1
);
3541 View
* v2
= new View();
3542 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3543 v1
->AddChildView(v2
);
3545 // Schedule a full-view paint to get everyone's rectangles updated.
3546 test_view
->SchedulePaintInRect(test_view
->bounds());
3547 GetRootLayer()->GetCompositor()->ScheduleDraw();
3548 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3550 // Now we have test_view - v1 - v2. Damage to only test_view should only
3551 // return root_view and test_view.
3552 test_view
->SchedulePaintInRect(gfx::Rect(0, 0, 1, 1));
3553 GetRootLayer()->GetCompositor()->ScheduleDraw();
3554 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3555 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3556 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3557 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3559 // Damage to v1 only should only return root_view, test_view, and v1.
3560 test_view
->SchedulePaintInRect(gfx::Rect(11, 16, 1, 1));
3561 GetRootLayer()->GetCompositor()->ScheduleDraw();
3562 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3563 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3564 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3565 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3566 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3568 // A Damage rect inside v2 should get all 3 views back in the |last_cull_set_|
3569 // on call to TestView::Paint(), along with the widget root view.
3570 test_view
->SchedulePaintInRect(gfx::Rect(31, 49, 1, 1));
3571 GetRootLayer()->GetCompositor()->ScheduleDraw();
3572 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3573 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3574 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3575 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3576 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3577 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3580 TEST_F(ViewLayerTest
, BoundsTreeWithRTL
) {
3581 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3582 base::i18n::SetICUDefaultLocale("ar");
3584 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3585 widget()->SetContentsView(test_view
);
3587 // Add child views, which should be in RTL coordinate space of parent view.
3588 View
* v1
= new View
;
3589 v1
->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3590 test_view
->AddChildView(v1
);
3592 View
* v2
= new View
;
3593 v2
->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3594 v1
->AddChildView(v2
);
3596 // Schedule a full-view paint to get everyone's rectangles updated.
3597 test_view
->SchedulePaintInRect(test_view
->bounds());
3598 GetRootLayer()->GetCompositor()->ScheduleDraw();
3599 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3601 // Damage to the right side of the parent view should touch both child views.
3602 gfx::Rect
rtl_damage(test_view
->bounds().width() - 16, 18, 1, 1);
3603 test_view
->SchedulePaintInRect(rtl_damage
);
3604 GetRootLayer()->GetCompositor()->ScheduleDraw();
3605 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3606 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3607 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3608 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3609 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3610 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3612 // Damage to the left side of the parent view should only touch the
3614 gfx::Rect
ltr_damage(16, 18, 1, 1);
3615 test_view
->SchedulePaintInRect(ltr_damage
);
3616 GetRootLayer()->GetCompositor()->ScheduleDraw();
3617 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3618 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3619 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3620 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3623 base::i18n::SetICUDefaultLocale(locale
);
3626 TEST_F(ViewLayerTest
, BoundsTreeSetBoundsChangesCullSet
) {
3627 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3628 widget()->SetContentsView(test_view
);
3630 View
* v1
= new View
;
3631 v1
->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3632 test_view
->AddChildView(v1
);
3634 View
* v2
= new View
;
3635 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3636 v1
->AddChildView(v2
);
3638 // Schedule a full-view paint to get everyone's rectangles updated.
3639 test_view
->SchedulePaintInRect(test_view
->bounds());
3640 GetRootLayer()->GetCompositor()->ScheduleDraw();
3641 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3643 // Move v1 to a new origin out of the way of our next query.
3644 v1
->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3645 // The move will force a repaint.
3646 GetRootLayer()->GetCompositor()->ScheduleDraw();
3647 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3649 // Schedule a paint with damage rect where v1 used to be.
3650 test_view
->SchedulePaintInRect(gfx::Rect(5, 6, 10, 11));
3651 GetRootLayer()->GetCompositor()->ScheduleDraw();
3652 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3654 // Should only have picked up root_view and test_view.
3655 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3656 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3657 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3660 TEST_F(ViewLayerTest
, BoundsTreeLayerChangeMakesNewTree
) {
3661 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3662 widget()->SetContentsView(test_view
);
3664 View
* v1
= new View
;
3665 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3666 test_view
->AddChildView(v1
);
3668 View
* v2
= new View
;
3669 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3670 v1
->AddChildView(v2
);
3672 // Schedule a full-view paint to get everyone's rectangles updated.
3673 test_view
->SchedulePaintInRect(test_view
->bounds());
3674 GetRootLayer()->GetCompositor()->ScheduleDraw();
3675 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3677 // Set v1 to paint to its own layer, it should remove itself from the
3678 // test_view heiarchy and no longer intersect with damage rects in that cull
3680 v1
->SetPaintToLayer(true);
3682 // Schedule another full-view paint.
3683 test_view
->SchedulePaintInRect(test_view
->bounds());
3684 GetRootLayer()->GetCompositor()->ScheduleDraw();
3685 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3686 // v1 and v2 should no longer be present in the test_view cull_set.
3687 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3688 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3689 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3691 // Now set v1 back to not painting to a layer.
3692 v1
->SetPaintToLayer(false);
3693 // Schedule another full-view paint.
3694 test_view
->SchedulePaintInRect(test_view
->bounds());
3695 GetRootLayer()->GetCompositor()->ScheduleDraw();
3696 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3697 // We should be back to the full cull set including v1 and v2.
3698 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3699 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3700 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3701 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3702 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3705 TEST_F(ViewLayerTest
, BoundsTreeRemoveChildRemovesBounds
) {
3706 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3707 widget()->SetContentsView(test_view
);
3709 View
* v1
= new View
;
3710 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3711 test_view
->AddChildView(v1
);
3713 View
* v2
= new View
;
3714 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3715 v1
->AddChildView(v2
);
3717 // Schedule a full-view paint to get everyone's rectangles updated.
3718 test_view
->SchedulePaintInRect(test_view
->bounds());
3719 GetRootLayer()->GetCompositor()->ScheduleDraw();
3720 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3722 // Now remove v1 from the root view.
3723 test_view
->RemoveChildView(v1
);
3725 // Schedule another full-view paint.
3726 test_view
->SchedulePaintInRect(test_view
->bounds());
3727 GetRootLayer()->GetCompositor()->ScheduleDraw();
3728 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3729 // v1 and v2 should no longer be present in the test_view cull_set.
3730 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3731 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3732 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3734 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3735 // deleted with that hierarchy.
3739 TEST_F(ViewLayerTest
, BoundsTreeMoveViewMovesBounds
) {
3740 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3741 widget()->SetContentsView(test_view
);
3743 // Build hierarchy v1 - v2 - v3.
3744 View
* v1
= new View
;
3745 v1
->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3746 test_view
->AddChildView(v1
);
3748 View
* v2
= new View
;
3749 v2
->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3750 v1
->AddChildView(v2
);
3752 View
* v3
= new View
;
3753 v3
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3754 v2
->AddChildView(v3
);
3756 // Schedule a full-view paint and ensure all views are present in the cull.
3757 test_view
->SchedulePaintInRect(test_view
->bounds());
3758 GetRootLayer()->GetCompositor()->ScheduleDraw();
3759 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3760 EXPECT_EQ(5U, test_view
->last_cull_set_
.size());
3761 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3762 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3763 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3764 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3765 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v3
));
3767 // Build an unrelated view hierarchy and move v2 in to it.
3768 scoped_ptr
<Widget
> test_widget(new Widget
);
3769 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3770 params
.bounds
= gfx::Rect(10, 10, 500, 500);
3771 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3772 test_widget
->Init(params
);
3773 test_widget
->Show();
3774 BoundsTreeTestView
* widget_view
= new BoundsTreeTestView
;
3775 test_widget
->SetContentsView(widget_view
);
3776 widget_view
->AddChildView(v2
);
3778 // Now schedule full-view paints in both widgets.
3779 test_view
->SchedulePaintInRect(test_view
->bounds());
3780 widget_view
->SchedulePaintInRect(widget_view
->bounds());
3781 GetRootLayer()->GetCompositor()->ScheduleDraw();
3782 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3784 // Only v1 should be present in the first cull set.
3785 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3786 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3787 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3788 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3790 // We should find v2 and v3 in the widget_view cull_set.
3791 EXPECT_EQ(4U, widget_view
->last_cull_set_
.size());
3792 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(test_widget
->GetRootView()));
3793 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(widget_view
));
3794 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v2
));
3795 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v3
));
3800 std::string
ToString(const gfx::Vector2dF
& vector
) {
3801 return base::StringPrintf("%.2f %0.2f", vector
.x(), vector
.y());
3806 TEST_F(ViewLayerTest
, SnapLayerToPixel
) {
3807 View
* v1
= new View
;
3809 View
* v11
= new View
;
3810 v1
->AddChildView(v11
);
3812 widget()->SetContentsView(v1
);
3814 const gfx::Size
& size
= GetRootLayer()->GetCompositor()->size();
3815 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f
, size
);
3817 v11
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3818 v1
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3819 v11
->SetPaintToLayer(true);
3821 EXPECT_EQ("0.40 0.40", ToString(v11
->layer()->subpixel_position_offset()));
3823 // Creating a layer in parent should update the child view's layer offset.
3824 v1
->SetPaintToLayer(true);
3825 EXPECT_EQ("-0.20 -0.20", ToString(v1
->layer()->subpixel_position_offset()));
3826 EXPECT_EQ("-0.20 -0.20", ToString(v11
->layer()->subpixel_position_offset()));
3828 // DSF change should get propagated and update offsets.
3829 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f
, size
);
3830 EXPECT_EQ("0.33 0.33", ToString(v1
->layer()->subpixel_position_offset()));
3831 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3833 // Deleting parent's layer should update the child view's layer's offset.
3834 v1
->SetPaintToLayer(false);
3835 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3837 // Setting parent view should update the child view's layer's offset.
3838 v1
->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3839 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3841 // Setting integral DSF should reset the offset.
3842 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f
, size
);
3843 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3846 TEST_F(ViewTest
, FocusableAssertions
) {
3847 // View subclasses may change insets based on whether they are focusable,
3848 // which effects the preferred size. To avoid preferred size changing around
3849 // these Views need to key off the last value set to SetFocusable(), not
3850 // whether the View is focusable right now. For this reason it's important
3851 // that focusable() return the last value passed to SetFocusable and not
3852 // whether the View is focusable right now.
3854 view
.SetFocusable(true);
3855 EXPECT_TRUE(view
.focusable());
3856 view
.SetEnabled(false);
3857 EXPECT_TRUE(view
.focusable());
3858 view
.SetFocusable(false);
3859 EXPECT_FALSE(view
.focusable());
3862 // Verifies when a view is deleted it is removed from ViewStorage.
3863 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3864 ViewStorage
* view_storage
= ViewStorage::GetInstance();
3865 const int storage_id
= view_storage
->CreateStorageID();
3868 view_storage
->StoreView(storage_id
, &view
);
3870 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
3873 ////////////////////////////////////////////////////////////////////////////////
3875 ////////////////////////////////////////////////////////////////////////////////
3877 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
3878 native_theme_
= native_theme
;
3881 TEST_F(ViewTest
, OnNativeThemeChanged
) {
3882 TestView
* test_view
= new TestView();
3883 EXPECT_FALSE(test_view
->native_theme_
);
3884 TestView
* test_view_child
= new TestView();
3885 EXPECT_FALSE(test_view_child
->native_theme_
);
3887 // Child view added before the widget hierarchy exists should get the
3888 // new native theme notification.
3889 test_view
->AddChildView(test_view_child
);
3891 scoped_ptr
<Widget
> widget(new Widget
);
3892 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
3893 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3894 widget
->Init(params
);
3896 widget
->GetRootView()->AddChildView(test_view
);
3897 EXPECT_TRUE(test_view
->native_theme_
);
3898 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
3899 EXPECT_TRUE(test_view_child
->native_theme_
);
3900 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
3902 // Child view added after the widget hierarchy exists should also get the
3904 TestView
* test_view_child_2
= new TestView();
3905 test_view
->AddChildView(test_view_child_2
);
3906 EXPECT_TRUE(test_view_child_2
->native_theme_
);
3907 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
3912 } // namespace views