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
, ScrollGestureEvent
) {
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 TestViewIgnoreScrollGestures();
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_
);
533 // Send scroll gestures on |v3|. The gesture should reach |v2|, however,
534 // since it does not process scroll-gesture events, these events should reach
536 GestureEventForTest
gscroll_begin(ui::ET_GESTURE_SCROLL_BEGIN
, 115, 115, 0);
537 details
= root
->OnEventFromSource(&gscroll_begin
);
538 EXPECT_FALSE(details
.dispatcher_destroyed
);
539 EXPECT_FALSE(details
.target_destroyed
);
541 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
542 EXPECT_EQ(ui::ET_GESTURE_SCROLL_BEGIN
, v1
->last_gesture_event_type_
);
545 // Send a second tap on |v1|. The event should reach |v2| since it is the
546 // default gesture handler, and not |v1| (even though it is the view under the
547 // point, and is the scroll event handler).
548 GestureEventForTest
second_tap(ui::ET_GESTURE_TAP
, 70, 70, 0);
549 details
= root
->OnEventFromSource(&second_tap
);
550 EXPECT_FALSE(details
.dispatcher_destroyed
);
551 EXPECT_FALSE(details
.target_destroyed
);
553 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
554 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
557 GestureEventForTest
gscroll_end(ui::ET_GESTURE_SCROLL_END
, 50, 50, 0);
558 details
= root
->OnEventFromSource(&gscroll_end
);
559 EXPECT_FALSE(details
.dispatcher_destroyed
);
560 EXPECT_FALSE(details
.target_destroyed
);
562 EXPECT_EQ(ui::ET_GESTURE_SCROLL_END
, v1
->last_gesture_event_type_
);
565 // Simulate an up so that RootView is no longer targetting |v3|.
566 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
567 details
= root
->OnEventFromSource(&g1_up
);
568 EXPECT_FALSE(details
.dispatcher_destroyed
);
569 EXPECT_FALSE(details
.target_destroyed
);
571 EXPECT_EQ(ui::ET_GESTURE_END
, v2
->last_gesture_event_type_
);
578 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
579 details
= root
->OnEventFromSource(&g2
);
580 EXPECT_FALSE(details
.dispatcher_destroyed
);
581 EXPECT_FALSE(details
.target_destroyed
);
583 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
584 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
585 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
587 // Send event |g1| again. Even though the coordinates target |v3| it should go
588 // to |v1| as that is the view the touch was initially down on.
589 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
590 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
591 details
= root
->OnEventFromSource(&g1
);
592 EXPECT_FALSE(details
.dispatcher_destroyed
);
593 EXPECT_FALSE(details
.target_destroyed
);
595 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
596 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
597 EXPECT_EQ("110,110", v1
->location_
.ToString());
602 ////////////////////////////////////////////////////////////////////////////////
604 ////////////////////////////////////////////////////////////////////////////////
606 void TestView::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
607 canvas
->sk_canvas()->getClipBounds(&last_clip_
);
610 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
611 scheduled_paint_rects_
.push_back(rect
);
612 View::SchedulePaintInRect(rect
);
615 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
616 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
617 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
618 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
619 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
622 TEST_F(ViewTest
, RemoveNotification
) {
623 ViewStorage
* vs
= ViewStorage::GetInstance();
624 Widget
* widget
= new Widget
;
625 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
626 View
* root_view
= widget
->GetRootView();
629 int s1
= vs
->CreateStorageID();
630 vs
->StoreView(s1
, v1
);
631 root_view
->AddChildView(v1
);
632 View
* v11
= new View
;
633 int s11
= vs
->CreateStorageID();
634 vs
->StoreView(s11
, v11
);
635 v1
->AddChildView(v11
);
636 View
* v111
= new View
;
637 int s111
= vs
->CreateStorageID();
638 vs
->StoreView(s111
, v111
);
639 v11
->AddChildView(v111
);
640 View
* v112
= new View
;
641 int s112
= vs
->CreateStorageID();
642 vs
->StoreView(s112
, v112
);
643 v11
->AddChildView(v112
);
644 View
* v113
= new View
;
645 int s113
= vs
->CreateStorageID();
646 vs
->StoreView(s113
, v113
);
647 v11
->AddChildView(v113
);
648 View
* v1131
= new View
;
649 int s1131
= vs
->CreateStorageID();
650 vs
->StoreView(s1131
, v1131
);
651 v113
->AddChildView(v1131
);
652 View
* v12
= new View
;
653 int s12
= vs
->CreateStorageID();
654 vs
->StoreView(s12
, v12
);
655 v1
->AddChildView(v12
);
658 int s2
= vs
->CreateStorageID();
659 vs
->StoreView(s2
, v2
);
660 root_view
->AddChildView(v2
);
661 View
* v21
= new View
;
662 int s21
= vs
->CreateStorageID();
663 vs
->StoreView(s21
, v21
);
664 v2
->AddChildView(v21
);
665 View
* v211
= new View
;
666 int s211
= vs
->CreateStorageID();
667 vs
->StoreView(s211
, v211
);
668 v21
->AddChildView(v211
);
670 size_t stored_views
= vs
->view_count();
672 // Try removing a leaf view.
673 v21
->RemoveChildView(v211
);
674 EXPECT_EQ(stored_views
- 1, vs
->view_count());
675 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
676 delete v211
; // We won't use this one anymore.
678 // Now try removing a view with a hierarchy of depth 1.
679 v11
->RemoveChildView(v113
);
680 EXPECT_EQ(stored_views
- 3, vs
->view_count());
681 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
682 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
683 delete v113
; // We won't use this one anymore.
685 // Now remove even more.
686 root_view
->RemoveChildView(v1
);
687 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
688 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
689 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
690 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
691 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
693 // Put v1 back for more tests.
694 root_view
->AddChildView(v1
);
695 vs
->StoreView(s1
, v1
);
697 // Synchronously closing the window deletes the view hierarchy, which should
698 // remove all its views from ViewStorage.
700 EXPECT_EQ(stored_views
- 10, vs
->view_count());
701 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
702 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
703 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
704 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
705 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
706 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
707 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
712 void RotateCounterclockwise(gfx::Transform
* transform
) {
713 transform
->matrix().set3x3(0, -1, 0,
718 void RotateClockwise(gfx::Transform
* transform
) {
719 transform
->matrix().set3x3( 0, 1, 0,
726 // Tests the correctness of the rect-based targeting algorithm implemented in
727 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
728 // of rect-based targeting.
729 TEST_F(ViewTest
, GetEventHandlerForRect
) {
730 Widget
* widget
= new Widget
;
731 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
732 widget
->Init(params
);
733 View
* root_view
= widget
->GetRootView();
734 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
736 // Have this hierarchy of views (the coordinates here are all in
737 // the root view's coordinate space):
738 // v1 (0, 0, 100, 100)
739 // v2 (150, 0, 250, 100)
740 // v3 (0, 200, 150, 100)
741 // v31 (10, 210, 80, 80)
742 // v32 (110, 210, 30, 80)
743 // v4 (300, 200, 100, 100)
744 // v41 (310, 210, 80, 80)
745 // v411 (370, 275, 10, 5)
746 // v5 (450, 197, 30, 36)
747 // v51 (450, 200, 30, 30)
749 // The coordinates used for SetBounds are in parent coordinates.
751 TestView
* v1
= new TestView
;
752 v1
->SetBounds(0, 0, 100, 100);
753 root_view
->AddChildView(v1
);
755 TestView
* v2
= new TestView
;
756 v2
->SetBounds(150, 0, 250, 100);
757 root_view
->AddChildView(v2
);
759 TestView
* v3
= new TestView
;
760 v3
->SetBounds(0, 200, 150, 100);
761 root_view
->AddChildView(v3
);
763 TestView
* v4
= new TestView
;
764 v4
->SetBounds(300, 200, 100, 100);
765 root_view
->AddChildView(v4
);
767 TestView
* v31
= new TestView
;
768 v31
->SetBounds(10, 10, 80, 80);
769 v3
->AddChildView(v31
);
771 TestView
* v32
= new TestView
;
772 v32
->SetBounds(110, 10, 30, 80);
773 v3
->AddChildView(v32
);
775 TestView
* v41
= new TestView
;
776 v41
->SetBounds(10, 10, 80, 80);
777 v4
->AddChildView(v41
);
779 TestView
* v411
= new TestView
;
780 v411
->SetBounds(60, 65, 10, 5);
781 v41
->AddChildView(v411
);
783 TestView
* v5
= new TestView
;
784 v5
->SetBounds(450, 197, 30, 36);
785 root_view
->AddChildView(v5
);
787 TestView
* v51
= new TestView
;
788 v51
->SetBounds(0, 3, 30, 30);
789 v5
->AddChildView(v51
);
791 // |touch_rect| does not intersect any descendant view of |root_view|.
792 gfx::Rect
touch_rect(105, 105, 30, 45);
793 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
794 EXPECT_EQ(root_view
, result_view
);
797 // Covers |v1| by at least 60%.
798 touch_rect
.SetRect(15, 15, 100, 100);
799 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
800 EXPECT_EQ(v1
, result_view
);
803 // Intersects |v1| but does not cover it by at least 60%. The center
804 // of |touch_rect| is within |v1|.
805 touch_rect
.SetRect(50, 50, 5, 10);
806 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
807 EXPECT_EQ(v1
, result_view
);
810 // Intersects |v1| but does not cover it by at least 60%. The center
811 // of |touch_rect| is not within |v1|.
812 touch_rect
.SetRect(95, 96, 21, 22);
813 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
814 EXPECT_EQ(root_view
, result_view
);
817 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
818 touch_rect
.SetRect(95, 10, 300, 120);
819 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
820 EXPECT_EQ(v2
, result_view
);
823 // Covers both |v1| and |v2| by at least 60%, but the center point
824 // of |touch_rect| is closer to the center point of |v2|.
825 touch_rect
.SetRect(20, 20, 400, 100);
826 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
827 EXPECT_EQ(v2
, result_view
);
830 // Covers both |v1| and |v2| by at least 60%, but the center point
831 // of |touch_rect| is closer to the center point of |v1|.
832 touch_rect
.SetRect(-700, -15, 1050, 110);
833 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
834 EXPECT_EQ(v1
, result_view
);
837 // A mouse click within |v1| will target |v1|.
838 touch_rect
.SetRect(15, 15, 1, 1);
839 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
840 EXPECT_EQ(v1
, result_view
);
843 // Intersects |v3| and |v31| by at least 60% and the center point
844 // of |touch_rect| is closer to the center point of |v31|.
845 touch_rect
.SetRect(0, 200, 110, 100);
846 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
847 EXPECT_EQ(v31
, result_view
);
850 // Intersects |v3| and |v31|, but neither by at least 60%. The
851 // center point of |touch_rect| lies within |v31|.
852 touch_rect
.SetRect(80, 280, 15, 15);
853 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
854 EXPECT_EQ(v31
, result_view
);
857 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
858 // center point of |touch_rect| is closest to the center point
860 touch_rect
.SetRect(0, 200, 200, 100);
861 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
862 EXPECT_EQ(v32
, result_view
);
865 // Intersects all of |v3|, |v31|, and |v32|, but only covers
866 // |v31| and |v32| by at least 60%. The center point of
867 // |touch_rect| is closest to the center point of |v32|.
868 touch_rect
.SetRect(30, 225, 180, 115);
869 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
870 EXPECT_EQ(v32
, result_view
);
873 // A mouse click at the corner of |v3| will target |v3|.
874 touch_rect
.SetRect(0, 200, 1, 1);
875 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
876 EXPECT_EQ(v3
, result_view
);
879 // A mouse click within |v32| will target |v32|.
880 touch_rect
.SetRect(112, 211, 1, 1);
881 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
882 EXPECT_EQ(v32
, result_view
);
885 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
886 // The center point of |touch_rect| is equally close to
887 // the center points of |v4| and |v41|.
888 touch_rect
.SetRect(310, 210, 80, 80);
889 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
890 EXPECT_EQ(v41
, result_view
);
893 // Intersects all of |v4|, |v41|, and |v411| but only covers
894 // |v411| by at least 60%.
895 touch_rect
.SetRect(370, 275, 7, 5);
896 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
897 EXPECT_EQ(v411
, result_view
);
900 // Intersects |v4| and |v41| but covers neither by at least 60%.
901 // The center point of |touch_rect| is equally close to the center
902 // points of |v4| and |v41|.
903 touch_rect
.SetRect(345, 245, 7, 7);
904 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
905 EXPECT_EQ(v41
, result_view
);
908 // Intersects all of |v4|, |v41|, and |v411| and covers none of
909 // them by at least 60%. The center point of |touch_rect| lies
911 touch_rect
.SetRect(368, 272, 4, 6);
912 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
913 EXPECT_EQ(v411
, result_view
);
916 // Intersects all of |v4|, |v41|, and |v411| and covers none of
917 // them by at least 60%. The center point of |touch_rect| lies
919 touch_rect
.SetRect(365, 270, 7, 7);
920 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
921 EXPECT_EQ(v41
, result_view
);
924 // Intersects all of |v4|, |v41|, and |v411| and covers none of
925 // them by at least 60%. The center point of |touch_rect| lies
927 touch_rect
.SetRect(205, 275, 200, 2);
928 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
929 EXPECT_EQ(v4
, result_view
);
932 // Intersects all of |v4|, |v41|, and |v411| but only covers
933 // |v41| by at least 60%.
934 touch_rect
.SetRect(310, 210, 61, 66);
935 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
936 EXPECT_EQ(v41
, result_view
);
939 // A mouse click within |v411| will target |v411|.
940 touch_rect
.SetRect(372, 275, 1, 1);
941 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
942 EXPECT_EQ(v411
, result_view
);
945 // A mouse click within |v41| will target |v41|.
946 touch_rect
.SetRect(350, 215, 1, 1);
947 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
948 EXPECT_EQ(v41
, result_view
);
951 // Covers |v3|, |v4|, and all of their descendants by at
952 // least 60%. The center point of |touch_rect| is closest
953 // to the center point of |v32|.
954 touch_rect
.SetRect(0, 200, 400, 100);
955 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
956 EXPECT_EQ(v32
, result_view
);
959 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
960 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
961 // The center point of |touch_rect| is closest to the center
962 // point of |root_view|.
963 touch_rect
.SetRect(110, 15, 375, 450);
964 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
965 EXPECT_EQ(root_view
, result_view
);
968 // Covers all views (except |v5| and |v51|) by at least 60%. The
969 // center point of |touch_rect| is equally close to the center
970 // points of |v2| and |v32|. One is not a descendant of the other,
971 // so in this case the view selected is arbitrary (i.e.,
972 // it depends only on the ordering of nodes in the views
974 touch_rect
.SetRect(0, 0, 400, 300);
975 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
976 EXPECT_EQ(v32
, result_view
);
979 // Covers |v5| and |v51| by at least 60%, and the center point of
980 // the touch is located within both views. Since both views share
981 // the same center point, the child view should be selected.
982 touch_rect
.SetRect(440, 190, 40, 40);
983 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
984 EXPECT_EQ(v51
, result_view
);
987 // Covers |v5| and |v51| by at least 60%, but the center point of
988 // the touch is not located within either view. Since both views
989 // share the same center point, the child view should be selected.
990 touch_rect
.SetRect(455, 187, 60, 60);
991 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
992 EXPECT_EQ(v51
, result_view
);
995 // Covers neither |v5| nor |v51| by at least 60%, but the center
996 // of the touch is located within |v51|.
997 touch_rect
.SetRect(450, 197, 10, 10);
998 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
999 EXPECT_EQ(v51
, result_view
);
1002 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1003 // The center point is located outside of both views.
1004 touch_rect
.SetRect(433, 180, 24, 24);
1005 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1006 EXPECT_EQ(root_view
, result_view
);
1009 // Only intersects |v5| but does not cover it by at least 60%. The
1010 // center point of the touch region is located within |v5|.
1011 touch_rect
.SetRect(449, 196, 3, 3);
1012 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1013 EXPECT_EQ(v5
, result_view
);
1016 // A mouse click within |v5| (but not |v51|) should target |v5|.
1017 touch_rect
.SetRect(462, 199, 1, 1);
1018 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1019 EXPECT_EQ(v5
, result_view
);
1022 // A mouse click |v5| and |v51| should target the child view.
1023 touch_rect
.SetRect(452, 226, 1, 1);
1024 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1025 EXPECT_EQ(v51
, result_view
);
1028 // A mouse click on the center of |v5| and |v51| should target
1030 touch_rect
.SetRect(465, 215, 1, 1);
1031 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1032 EXPECT_EQ(v51
, result_view
);
1038 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1039 // as expected when different views in the view hierarchy return false
1040 // when CanProcessEventsWithinSubtree() is called.
1041 TEST_F(ViewTest
, CanProcessEventsWithinSubtree
) {
1042 Widget
* widget
= new Widget
;
1043 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1044 widget
->Init(params
);
1045 View
* root_view
= widget
->GetRootView();
1046 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1048 // Have this hierarchy of views (the coords here are in the coordinate
1049 // space of the root view):
1050 // v (0, 0, 100, 100)
1051 // - v_child (0, 0, 20, 30)
1052 // - v_grandchild (5, 5, 5, 15)
1054 TestView
* v
= new TestView
;
1055 v
->SetBounds(0, 0, 100, 100);
1056 root_view
->AddChildView(v
);
1057 v
->set_notify_enter_exit_on_child(true);
1059 TestView
* v_child
= new TestView
;
1060 v_child
->SetBounds(0, 0, 20, 30);
1061 v
->AddChildView(v_child
);
1063 TestView
* v_grandchild
= new TestView
;
1064 v_grandchild
->SetBounds(5, 5, 5, 15);
1065 v_child
->AddChildView(v_grandchild
);
1069 v_grandchild
->Reset();
1071 // Define rects and points within the views in the hierarchy.
1072 gfx::Rect
rect_in_v_grandchild(7, 7, 3, 3);
1073 gfx::Point
point_in_v_grandchild(rect_in_v_grandchild
.origin());
1074 gfx::Rect
rect_in_v_child(12, 3, 5, 5);
1075 gfx::Point
point_in_v_child(rect_in_v_child
.origin());
1076 gfx::Rect
rect_in_v(50, 50, 25, 30);
1077 gfx::Point
point_in_v(rect_in_v
.origin());
1079 // When all three views return true when CanProcessEventsWithinSubtree()
1080 // is called, targeting should behave as expected.
1082 View
* result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1083 EXPECT_EQ(v_grandchild
, result_view
);
1085 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1086 EXPECT_EQ(v_grandchild
, result_view
);
1089 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1090 EXPECT_EQ(v_child
, result_view
);
1092 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1093 EXPECT_EQ(v_child
, result_view
);
1096 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1097 EXPECT_EQ(v
, result_view
);
1099 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1100 EXPECT_EQ(v
, result_view
);
1103 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1104 // is called, then |v_grandchild| cannot be returned as a target.
1106 v_grandchild
->set_can_process_events_within_subtree(false);
1108 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1109 EXPECT_EQ(v_child
, result_view
);
1111 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1112 EXPECT_EQ(v_child
, result_view
);
1115 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1116 EXPECT_EQ(v_child
, result_view
);
1118 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1119 EXPECT_EQ(v_child
, result_view
);
1122 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1123 EXPECT_EQ(v
, result_view
);
1125 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1126 EXPECT_EQ(v
, result_view
);
1128 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1129 // is called, then NULL should be returned as a target if we call
1130 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
1131 // views tree. Note that the location must be in the coordinate space
1132 // of the root view (|v_grandchild| in this case), so use (1, 1).
1134 result_view
= v_grandchild
;
1135 result_view
= v_grandchild
->GetTooltipHandlerForPoint(gfx::Point(1, 1));
1136 EXPECT_EQ(NULL
, result_view
);
1139 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1140 // is called, then neither |v_child| nor |v_grandchild| can be returned
1141 // as a target (|v| should be returned as the target for each case).
1143 v_grandchild
->Reset();
1144 v_child
->set_can_process_events_within_subtree(false);
1146 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1147 EXPECT_EQ(v
, result_view
);
1149 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1150 EXPECT_EQ(v
, result_view
);
1153 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1154 EXPECT_EQ(v
, result_view
);
1156 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1157 EXPECT_EQ(v
, result_view
);
1160 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1161 EXPECT_EQ(v
, result_view
);
1163 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1164 EXPECT_EQ(v
, result_view
);
1167 // When |v| returns false when CanProcessEventsWithinSubtree()
1168 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1169 // as a target (|root_view| should be returned as the target for each case).
1172 v
->set_can_process_events_within_subtree(false);
1174 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1175 EXPECT_EQ(root_view
, result_view
);
1177 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1178 EXPECT_EQ(root_view
, result_view
);
1181 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1182 EXPECT_EQ(root_view
, result_view
);
1184 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1185 EXPECT_EQ(root_view
, result_view
);
1188 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1189 EXPECT_EQ(root_view
, result_view
);
1191 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1192 EXPECT_EQ(root_view
, result_view
);
1195 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1196 Widget
* widget
= new Widget
;
1197 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1198 widget
->Init(params
);
1199 View
* root_view
= widget
->GetRootView();
1200 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1202 // Have this hierarchy of views (the coords here are in root coord):
1203 // v1 (0, 0, 100, 100)
1204 // - v11 (0, 0, 20, 30)
1205 // - v111 (5, 5, 5, 15)
1206 // - v12 (50, 10, 30, 90)
1207 // - v121 (60, 20, 10, 10)
1208 // v2 (105, 0, 100, 100)
1209 // - v21 (120, 10, 50, 20)
1211 TestView
* v1
= new TestView
;
1212 v1
->SetBounds(0, 0, 100, 100);
1213 root_view
->AddChildView(v1
);
1214 v1
->set_notify_enter_exit_on_child(true);
1216 TestView
* v11
= new TestView
;
1217 v11
->SetBounds(0, 0, 20, 30);
1218 v1
->AddChildView(v11
);
1220 TestView
* v111
= new TestView
;
1221 v111
->SetBounds(5, 5, 5, 15);
1222 v11
->AddChildView(v111
);
1224 TestView
* v12
= new TestView
;
1225 v12
->SetBounds(50, 10, 30, 90);
1226 v1
->AddChildView(v12
);
1228 TestView
* v121
= new TestView
;
1229 v121
->SetBounds(10, 10, 10, 10);
1230 v12
->AddChildView(v121
);
1232 TestView
* v2
= new TestView
;
1233 v2
->SetBounds(105, 0, 100, 100);
1234 root_view
->AddChildView(v2
);
1236 TestView
* v21
= new TestView
;
1237 v21
->SetBounds(15, 10, 50, 20);
1238 v2
->AddChildView(v21
);
1248 // Move the mouse in v111.
1249 gfx::Point
p1(6, 6);
1250 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, 0, 0);
1251 root_view
->OnMouseMoved(move1
);
1252 EXPECT_TRUE(v111
->received_mouse_enter_
);
1253 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1254 EXPECT_TRUE(v1
->received_mouse_enter_
);
1259 // Now, move into v121.
1260 gfx::Point
p2(65, 21);
1261 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, 0, 0);
1262 root_view
->OnMouseMoved(move2
);
1263 EXPECT_TRUE(v111
->received_mouse_exit_
);
1264 EXPECT_TRUE(v121
->received_mouse_enter_
);
1265 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1270 // Now, move into v11.
1271 gfx::Point
p3(1, 1);
1272 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, 0, 0);
1273 root_view
->OnMouseMoved(move3
);
1274 EXPECT_TRUE(v121
->received_mouse_exit_
);
1275 EXPECT_TRUE(v11
->received_mouse_enter_
);
1276 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1282 gfx::Point
p4(121, 15);
1283 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, 0, 0);
1284 root_view
->OnMouseMoved(move4
);
1285 EXPECT_TRUE(v21
->received_mouse_enter_
);
1286 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1287 EXPECT_TRUE(v11
->received_mouse_exit_
);
1288 EXPECT_TRUE(v1
->received_mouse_exit_
);
1295 gfx::Point
p5(21, 0);
1296 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, 0, 0);
1297 root_view
->OnMouseMoved(move5
);
1298 EXPECT_TRUE(v21
->received_mouse_exit_
);
1299 EXPECT_TRUE(v1
->received_mouse_enter_
);
1304 // Now, move into v11.
1305 gfx::Point
p6(15, 15);
1306 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, 0, 0);
1307 root_view
->OnMouseMoved(mouse6
);
1308 EXPECT_TRUE(v11
->received_mouse_enter_
);
1309 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1314 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1315 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1316 // when the mouse leaves v11.
1317 gfx::Point
p7(21, 0);
1318 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, 0, 0);
1319 root_view
->OnMouseMoved(mouse7
);
1320 EXPECT_TRUE(v11
->received_mouse_exit_
);
1321 EXPECT_FALSE(v1
->received_mouse_enter_
);
1326 TEST_F(ViewTest
, Textfield
) {
1327 const base::string16 kText
= ASCIIToUTF16(
1328 "Reality is that which, when you stop believing it, doesn't go away.");
1329 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1331 Widget
* widget
= new Widget
;
1332 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1333 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1334 widget
->Init(params
);
1335 View
* root_view
= widget
->GetRootView();
1337 Textfield
* textfield
= new Textfield();
1338 root_view
->AddChildView(textfield
);
1340 // Test setting, appending text.
1341 textfield
->SetText(kText
);
1342 EXPECT_EQ(kText
, textfield
->text());
1343 textfield
->AppendText(kExtraText
);
1344 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1345 textfield
->SetText(base::string16());
1346 EXPECT_TRUE(textfield
->text().empty());
1348 // Test selection related methods.
1349 textfield
->SetText(kText
);
1350 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1351 textfield
->SelectAll(false);
1352 EXPECT_EQ(kText
, textfield
->text());
1353 textfield
->ClearSelection();
1354 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1359 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1360 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1361 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1362 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1363 const base::string16 kPasswordText
=
1364 ASCIIToUTF16("Password! ** Secret stuff **");
1366 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1368 Widget
* widget
= new Widget
;
1369 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1370 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1371 widget
->Init(params
);
1372 View
* root_view
= widget
->GetRootView();
1374 Textfield
* normal
= new Textfield();
1375 Textfield
* read_only
= new Textfield();
1376 read_only
->SetReadOnly(true);
1377 Textfield
* password
= new Textfield();
1378 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1380 root_view
->AddChildView(normal
);
1381 root_view
->AddChildView(read_only
);
1382 root_view
->AddChildView(password
);
1384 normal
->SetText(kNormalText
);
1385 read_only
->SetText(kReadOnlyText
);
1386 password
->SetText(kPasswordText
);
1392 normal
->SelectAll(false);
1393 normal
->ExecuteCommand(IDS_APP_CUT
);
1394 base::string16 result
;
1395 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1396 EXPECT_EQ(kNormalText
, result
);
1397 normal
->SetText(kNormalText
); // Let's revert to the original content.
1399 read_only
->SelectAll(false);
1400 read_only
->ExecuteCommand(IDS_APP_CUT
);
1402 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1403 // Cut should have failed, so the clipboard content should not have changed.
1404 EXPECT_EQ(kNormalText
, result
);
1406 password
->SelectAll(false);
1407 password
->ExecuteCommand(IDS_APP_CUT
);
1409 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1410 // Cut should have failed, so the clipboard content should not have changed.
1411 EXPECT_EQ(kNormalText
, result
);
1417 // Start with |read_only| to observe a change in clipboard text.
1418 read_only
->SelectAll(false);
1419 read_only
->ExecuteCommand(IDS_APP_COPY
);
1421 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1422 EXPECT_EQ(kReadOnlyText
, result
);
1424 normal
->SelectAll(false);
1425 normal
->ExecuteCommand(IDS_APP_COPY
);
1427 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1428 EXPECT_EQ(kNormalText
, result
);
1430 password
->SelectAll(false);
1431 password
->ExecuteCommand(IDS_APP_COPY
);
1433 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1434 // Text cannot be copied from an obscured field; the clipboard won't change.
1435 EXPECT_EQ(kNormalText
, result
);
1441 // Attempting to paste kNormalText in a read-only text-field should fail.
1442 read_only
->SelectAll(false);
1443 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1444 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1446 password
->SelectAll(false);
1447 password
->ExecuteCommand(IDS_APP_PASTE
);
1448 EXPECT_EQ(kNormalText
, password
->text());
1450 // Copy from |read_only| to observe a change in the normal textfield text.
1451 read_only
->SelectAll(false);
1452 read_only
->ExecuteCommand(IDS_APP_COPY
);
1453 normal
->SelectAll(false);
1454 normal
->ExecuteCommand(IDS_APP_PASTE
);
1455 EXPECT_EQ(kReadOnlyText
, normal
->text());
1459 ////////////////////////////////////////////////////////////////////////////////
1461 ////////////////////////////////////////////////////////////////////////////////
1462 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1463 accelerator_count_map_
[accelerator
]++;
1467 // TODO: these tests were initially commented out when getting aura to
1468 // run. Figure out if still valuable and either nuke or fix.
1470 TEST_F(ViewTest
, ActivateAccelerator
) {
1471 // Register a keyboard accelerator before the view is added to a window.
1472 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1473 TestView
* view
= new TestView();
1475 view
->AddAccelerator(return_accelerator
);
1476 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1478 // Create a window and add the view as its child.
1479 scoped_ptr
<Widget
> widget(new Widget
);
1480 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1481 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1482 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1483 widget
->Init(params
);
1484 View
* root
= widget
->GetRootView();
1485 root
->AddChildView(view
);
1488 // Get the focus manager.
1489 FocusManager
* focus_manager
= widget
->GetFocusManager();
1490 ASSERT_TRUE(focus_manager
);
1492 // Hit the return key and see if it takes effect.
1493 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1494 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1496 // Hit the escape key. Nothing should happen.
1497 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1498 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1499 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1500 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1502 // Now register the escape key and hit it again.
1503 view
->AddAccelerator(escape_accelerator
);
1504 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1505 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1506 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1508 // Remove the return key accelerator.
1509 view
->RemoveAccelerator(return_accelerator
);
1510 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1511 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1512 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1514 // Add it again. Hit the return key and the escape key.
1515 view
->AddAccelerator(return_accelerator
);
1516 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1517 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1518 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1519 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1520 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1521 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1523 // Remove all the accelerators.
1524 view
->ResetAccelerators();
1525 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1526 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1527 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1528 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1529 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1530 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1535 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1536 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1537 TestView
* view
= new TestView();
1539 view
->AddAccelerator(return_accelerator
);
1540 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1542 scoped_ptr
<Widget
> widget(new Widget
);
1543 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1544 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1545 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1546 widget
->Init(params
);
1547 View
* root
= widget
->GetRootView();
1548 root
->AddChildView(view
);
1551 FocusManager
* focus_manager
= widget
->GetFocusManager();
1552 ASSERT_TRUE(focus_manager
);
1554 view
->SetVisible(false);
1555 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1557 view
->SetVisible(true);
1558 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1563 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1564 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1565 TestView
* view
= new TestView();
1567 view
->AddAccelerator(return_accelerator
);
1568 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1570 scoped_ptr
<Widget
> widget(new Widget
);
1571 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1572 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1573 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1574 widget
->Init(params
);
1575 View
* root
= widget
->GetRootView();
1576 root
->AddChildView(view
);
1578 FocusManager
* focus_manager
= widget
->GetFocusManager();
1579 ASSERT_TRUE(focus_manager
);
1581 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1582 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1585 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1586 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1589 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1590 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1595 ////////////////////////////////////////////////////////////////////////////////
1596 // Mouse-wheel message rerouting
1597 ////////////////////////////////////////////////////////////////////////////////
1598 class ScrollableTestView
: public View
{
1600 ScrollableTestView() { }
1602 virtual gfx::Size
GetPreferredSize() {
1603 return gfx::Size(100, 10000);
1606 virtual void Layout() {
1607 SizeToPreferredSize();
1611 class TestViewWithControls
: public View
{
1613 TestViewWithControls() {
1614 text_field_
= new Textfield();
1615 AddChildView(text_field_
);
1618 Textfield
* text_field_
;
1621 class SimpleWidgetDelegate
: public WidgetDelegate
{
1623 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1625 virtual void DeleteDelegate() { delete this; }
1627 virtual View
* GetContentsView() { return contents_
; }
1629 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1630 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1636 // Tests that the mouse-wheel messages are correctly rerouted to the window
1638 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1640 // Note that this fails for a variety of reasons:
1641 // - focused view is apparently reset across window activations and never
1642 // properly restored
1643 // - this test depends on you not having any other window visible open under the
1644 // area that it opens the test windows. --beng
1645 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1646 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1647 Widget
* window1
= Widget::CreateWindowWithBounds(
1648 new SimpleWidgetDelegate(view_with_controls
),
1649 gfx::Rect(0, 0, 100, 100));
1651 ScrollView
* scroll_view
= new ScrollView();
1652 scroll_view
->SetContents(new ScrollableTestView());
1653 Widget
* window2
= Widget::CreateWindowWithBounds(
1654 new SimpleWidgetDelegate(scroll_view
),
1655 gfx::Rect(200, 200, 100, 100));
1657 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1659 // Make the window1 active, as this is what it would be in real-world.
1660 window1
->Activate();
1662 // Let's send a mouse-wheel message to the different controls and check that
1663 // it is rerouted to the window under the mouse (effectively scrolling the
1666 // First to the Window's HWND.
1667 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1668 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1669 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1671 window1
->CloseNow();
1672 window2
->CloseNow();
1676 ////////////////////////////////////////////////////////////////////////////////
1677 // Native view hierachy
1678 ////////////////////////////////////////////////////////////////////////////////
1679 class ToplevelWidgetObserverView
: public View
{
1681 ToplevelWidgetObserverView() : toplevel_(NULL
) {
1683 virtual ~ToplevelWidgetObserverView() {
1687 virtual void ViewHierarchyChanged(
1688 const ViewHierarchyChangedDetails
& details
) OVERRIDE
{
1689 if (details
.is_add
) {
1690 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1695 virtual void NativeViewHierarchyChanged() OVERRIDE
{
1696 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1699 Widget
* toplevel() { return toplevel_
; }
1704 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
1707 // Test that a view can track the current top level widget by overriding
1708 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1709 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
1710 scoped_ptr
<Widget
> toplevel1(new Widget
);
1711 Widget::InitParams toplevel1_params
=
1712 CreateParams(Widget::InitParams::TYPE_POPUP
);
1713 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1714 toplevel1
->Init(toplevel1_params
);
1716 scoped_ptr
<Widget
> toplevel2(new Widget
);
1717 Widget::InitParams toplevel2_params
=
1718 CreateParams(Widget::InitParams::TYPE_POPUP
);
1719 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1720 toplevel2
->Init(toplevel2_params
);
1722 Widget
* child
= new Widget
;
1723 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
1724 child_params
.parent
= toplevel1
->GetNativeView();
1725 child
->Init(child_params
);
1727 ToplevelWidgetObserverView
* observer_view
=
1728 new ToplevelWidgetObserverView();
1729 EXPECT_EQ(NULL
, observer_view
->toplevel());
1731 child
->SetContentsView(observer_view
);
1732 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
1734 Widget::ReparentNativeView(child
->GetNativeView(),
1735 toplevel2
->GetNativeView());
1736 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
1738 observer_view
->parent()->RemoveChildView(observer_view
);
1739 EXPECT_EQ(NULL
, observer_view
->toplevel());
1741 // Make |observer_view| |child|'s contents view again so that it gets deleted
1743 child
->SetContentsView(observer_view
);
1746 ////////////////////////////////////////////////////////////////////////////////
1748 ////////////////////////////////////////////////////////////////////////////////
1750 class TransformPaintView
: public TestView
{
1752 TransformPaintView() {}
1753 virtual ~TransformPaintView() {}
1755 void ClearScheduledPaintRect() {
1756 scheduled_paint_rect_
= gfx::Rect();
1759 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1761 // Overridden from View:
1762 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
{
1763 gfx::Rect xrect
= ConvertRectToParent(rect
);
1764 scheduled_paint_rect_
.Union(xrect
);
1768 gfx::Rect scheduled_paint_rect_
;
1770 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
1773 TEST_F(ViewTest
, TransformPaint
) {
1774 TransformPaintView
* v1
= new TransformPaintView();
1775 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1777 TestView
* v2
= new TestView();
1778 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1780 Widget
* widget
= new Widget
;
1781 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1782 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1783 widget
->Init(params
);
1785 View
* root
= widget
->GetRootView();
1787 root
->AddChildView(v1
);
1788 v1
->AddChildView(v2
);
1790 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1791 v1
->ClearScheduledPaintRect();
1792 v2
->SchedulePaint();
1794 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
1796 // Rotate |v1| counter-clockwise.
1797 gfx::Transform transform
;
1798 RotateCounterclockwise(&transform
);
1799 transform
.matrix().set(1, 3, 500.0);
1800 v1
->SetTransform(transform
);
1802 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1804 v1
->ClearScheduledPaintRect();
1805 v2
->SchedulePaint();
1807 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
1812 TEST_F(ViewTest
, TransformEvent
) {
1813 TestView
* v1
= new TestView();
1814 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1816 TestView
* v2
= new TestView();
1817 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1819 Widget
* widget
= new Widget
;
1820 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1821 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1822 widget
->Init(params
);
1823 View
* root
= widget
->GetRootView();
1825 root
->AddChildView(v1
);
1826 v1
->AddChildView(v2
);
1828 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1830 // Rotate |v1| counter-clockwise.
1831 gfx::Transform
transform(v1
->GetTransform());
1832 RotateCounterclockwise(&transform
);
1833 transform
.matrix().set(1, 3, 500.0);
1834 v1
->SetTransform(transform
);
1836 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1840 gfx::Point
p1(110, 210);
1841 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
1842 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1843 root
->OnMousePressed(pressed
);
1844 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1845 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1846 EXPECT_EQ(190, v2
->location_
.x());
1847 EXPECT_EQ(10, v2
->location_
.y());
1849 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
1851 root
->OnMouseReleased(released
);
1853 // Now rotate |v2| inside |v1| clockwise.
1854 transform
= v2
->GetTransform();
1855 RotateClockwise(&transform
);
1856 transform
.matrix().set(0, 3, 100.f
);
1857 v2
->SetTransform(transform
);
1859 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1860 // (300, 400) in |root|.
1865 gfx::Point
point2(110, 320);
1866 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
,
1867 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1868 root
->OnMousePressed(p2
);
1869 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1870 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1871 EXPECT_EQ(10, v2
->location_
.x());
1872 EXPECT_EQ(20, v2
->location_
.y());
1874 root
->OnMouseReleased(released
);
1876 v1
->SetTransform(gfx::Transform());
1877 v2
->SetTransform(gfx::Transform());
1879 TestView
* v3
= new TestView();
1880 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1881 v2
->AddChildView(v3
);
1883 // Rotate |v3| clockwise with respect to |v2|.
1884 transform
= v1
->GetTransform();
1885 RotateClockwise(&transform
);
1886 transform
.matrix().set(0, 3, 30.f
);
1887 v3
->SetTransform(transform
);
1889 // Scale |v2| with respect to |v1| along both axis.
1890 transform
= v2
->GetTransform();
1891 transform
.matrix().set(0, 0, 0.8f
);
1892 transform
.matrix().set(1, 1, 0.5f
);
1893 v2
->SetTransform(transform
);
1895 // |v3| occupies (108, 105) to (132, 115) in |root|.
1901 gfx::Point
point(112, 110);
1902 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
,
1903 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1904 root
->OnMousePressed(p3
);
1906 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1907 EXPECT_EQ(10, v3
->location_
.x());
1908 EXPECT_EQ(25, v3
->location_
.y());
1910 root
->OnMouseReleased(released
);
1912 v1
->SetTransform(gfx::Transform());
1913 v2
->SetTransform(gfx::Transform());
1914 v3
->SetTransform(gfx::Transform());
1920 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1921 transform
= v3
->GetTransform();
1922 RotateClockwise(&transform
);
1923 transform
.matrix().set(0, 3, 30.f
);
1924 // Rotation sets some scaling transformation. Using SetScale would overwrite
1925 // that and pollute the rotation. So combine the scaling with the existing
1927 gfx::Transform scale
;
1928 scale
.Scale(0.8f
, 0.5f
);
1929 transform
.ConcatTransform(scale
);
1930 v3
->SetTransform(transform
);
1932 // Translate |v2| with respect to |v1|.
1933 transform
= v2
->GetTransform();
1934 transform
.matrix().set(0, 3, 10.f
);
1935 transform
.matrix().set(1, 3, 10.f
);
1936 v2
->SetTransform(transform
);
1938 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1940 gfx::Point
point3(124, 125);
1941 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
,
1942 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1943 root
->OnMousePressed(p4
);
1945 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1946 EXPECT_EQ(10, v3
->location_
.x());
1947 EXPECT_EQ(25, v3
->location_
.y());
1949 root
->OnMouseReleased(released
);
1954 TEST_F(ViewTest
, TransformVisibleBound
) {
1955 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1957 scoped_ptr
<Widget
> widget(new Widget
);
1958 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1959 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1960 params
.bounds
= viewport_bounds
;
1961 widget
->Init(params
);
1962 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1964 View
* viewport
= new View
;
1965 widget
->SetContentsView(viewport
);
1966 View
* contents
= new View
;
1967 viewport
->AddChildView(contents
);
1968 viewport
->SetBoundsRect(viewport_bounds
);
1969 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1971 View
* child
= new View
;
1972 contents
->AddChildView(child
);
1973 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
1974 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
1976 // Rotate |child| counter-clockwise
1977 gfx::Transform transform
;
1978 RotateCounterclockwise(&transform
);
1979 transform
.matrix().set(1, 3, 50.f
);
1980 child
->SetTransform(transform
);
1981 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
1986 ////////////////////////////////////////////////////////////////////////////////
1987 // OnVisibleBoundsChanged()
1989 class VisibleBoundsView
: public View
{
1991 VisibleBoundsView() : received_notification_(false) {}
1992 virtual ~VisibleBoundsView() {}
1994 bool received_notification() const { return received_notification_
; }
1995 void set_received_notification(bool received
) {
1996 received_notification_
= received
;
2000 // Overridden from View:
2001 virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE
{
2004 virtual void OnVisibleBoundsChanged() OVERRIDE
{
2005 received_notification_
= true;
2008 bool received_notification_
;
2010 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2013 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2014 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2016 scoped_ptr
<Widget
> widget(new Widget
);
2017 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2018 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2019 params
.bounds
= viewport_bounds
;
2020 widget
->Init(params
);
2021 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2023 View
* viewport
= new View
;
2024 widget
->SetContentsView(viewport
);
2025 View
* contents
= new View
;
2026 viewport
->AddChildView(contents
);
2027 viewport
->SetBoundsRect(viewport_bounds
);
2028 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2030 // Create a view that cares about visible bounds notifications, and position
2031 // it just outside the visible bounds of the viewport.
2032 VisibleBoundsView
* child
= new VisibleBoundsView
;
2033 contents
->AddChildView(child
);
2034 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2036 // The child bound should be fully clipped.
2037 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2039 // Now scroll the contents, but not enough to make the child visible.
2040 contents
->SetY(contents
->y() - 1);
2042 // We should have received the notification since the visible bounds may have
2043 // changed (even though they didn't).
2044 EXPECT_TRUE(child
->received_notification());
2045 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2046 child
->set_received_notification(false);
2048 // Now scroll the contents, this time by enough to make the child visible by
2050 contents
->SetY(contents
->y() - 10);
2051 EXPECT_TRUE(child
->received_notification());
2052 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2053 child
->set_received_notification(false);
2058 TEST_F(ViewTest
, SetBoundsPaint
) {
2060 TestView
* child_view
= new TestView
;
2062 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2063 top_view
.scheduled_paint_rects_
.clear();
2064 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2065 top_view
.AddChildView(child_view
);
2067 top_view
.scheduled_paint_rects_
.clear();
2068 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2069 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2071 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2072 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2073 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2074 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2077 // Assertions around painting and focus gain/lost.
2078 TEST_F(ViewTest
, FocusBlurPaints
) {
2079 TestView parent_view
;
2080 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
2082 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2084 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2085 parent_view
.AddChildView(child_view1
);
2087 parent_view
.scheduled_paint_rects_
.clear();
2088 child_view1
->scheduled_paint_rects_
.clear();
2090 // Focus change shouldn't trigger paints.
2091 child_view1
->DoFocus();
2093 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2094 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2096 child_view1
->DoBlur();
2097 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2098 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2101 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2102 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
2105 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2106 view
.InvalidateLayout();
2107 view
.scheduled_paint_rects_
.clear();
2108 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2109 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
2112 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2113 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
2114 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2116 // We have to put the View hierarchy into a Widget or no paints will be
2118 scoped_ptr
<Widget
> widget(new Widget
);
2119 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2120 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2121 params
.bounds
= viewport_bounds
;
2122 widget
->Init(params
);
2123 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2125 TestView
* parent_view
= new TestView
;
2126 widget
->SetContentsView(parent_view
);
2127 parent_view
->SetBoundsRect(viewport_bounds
);
2128 parent_view
->scheduled_paint_rects_
.clear();
2130 View
* child_view
= new View
;
2131 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2132 parent_view
->AddChildView(child_view
);
2133 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2134 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2136 parent_view
->scheduled_paint_rects_
.clear();
2137 parent_view
->RemoveChildView(child_view
);
2138 scoped_ptr
<View
> child_deleter(child_view
);
2139 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2140 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2145 // Tests conversion methods with a transform.
2146 TEST_F(ViewTest
, ConversionsWithTransform
) {
2149 // View hierarchy used to test scale transforms.
2150 TestView
* child
= new TestView
;
2151 TestView
* child_child
= new TestView
;
2153 // View used to test a rotation transform.
2154 TestView
* child_2
= new TestView
;
2156 top_view
.AddChildView(child
);
2157 child
->AddChildView(child_child
);
2159 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2161 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2162 gfx::Transform transform
;
2163 transform
.Scale(3.0, 4.0);
2164 child
->SetTransform(transform
);
2166 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2167 transform
.MakeIdentity();
2168 transform
.Scale(5.0, 7.0);
2169 child_child
->SetTransform(transform
);
2171 top_view
.AddChildView(child_2
);
2172 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2173 transform
.MakeIdentity();
2174 RotateClockwise(&transform
);
2175 child_2
->SetTransform(transform
);
2177 // Sanity check to make sure basic transforms act as expected.
2179 gfx::Transform transform
;
2180 transform
.Translate(110.0, -110.0);
2181 transform
.Scale(100.0, 55.0);
2182 transform
.Translate(1.0, 1.0);
2184 // convert to a 3x3 matrix.
2185 const SkMatrix
& matrix
= transform
.matrix();
2187 EXPECT_EQ(210, matrix
.getTranslateX());
2188 EXPECT_EQ(-55, matrix
.getTranslateY());
2189 EXPECT_EQ(100, matrix
.getScaleX());
2190 EXPECT_EQ(55, matrix
.getScaleY());
2191 EXPECT_EQ(0, matrix
.getSkewX());
2192 EXPECT_EQ(0, matrix
.getSkewY());
2196 gfx::Transform transform
;
2197 transform
.Translate(1.0, 1.0);
2199 t2
.Scale(100.0, 55.0);
2201 t3
.Translate(110.0, -110.0);
2202 transform
.ConcatTransform(t2
);
2203 transform
.ConcatTransform(t3
);
2205 // convert to a 3x3 matrix
2206 const SkMatrix
& matrix
= transform
.matrix();
2208 EXPECT_EQ(210, matrix
.getTranslateX());
2209 EXPECT_EQ(-55, matrix
.getTranslateY());
2210 EXPECT_EQ(100, matrix
.getScaleX());
2211 EXPECT_EQ(55, matrix
.getScaleY());
2212 EXPECT_EQ(0, matrix
.getSkewX());
2213 EXPECT_EQ(0, matrix
.getSkewY());
2216 // Conversions from child->top and top->child.
2218 gfx::Point
point(5, 5);
2219 View::ConvertPointToTarget(child
, &top_view
, &point
);
2220 EXPECT_EQ(22, point
.x());
2221 EXPECT_EQ(39, point
.y());
2223 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2224 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2225 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2226 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2227 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2228 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2230 point
.SetPoint(22, 39);
2231 View::ConvertPointToTarget(&top_view
, child
, &point
);
2232 EXPECT_EQ(5, point
.x());
2233 EXPECT_EQ(5, point
.y());
2235 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2236 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2237 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2238 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2239 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2240 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2243 // Conversions from child_child->top and top->child_child.
2245 gfx::Point
point(5, 5);
2246 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2247 EXPECT_EQ(133, point
.x());
2248 EXPECT_EQ(211, point
.y());
2250 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2251 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2252 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2253 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2254 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2255 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2257 point
.SetPoint(133, 211);
2258 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2259 EXPECT_EQ(5, point
.x());
2260 EXPECT_EQ(5, point
.y());
2262 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2263 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2264 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2265 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2266 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2267 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2270 // Conversions from child_child->child and child->child_child
2272 gfx::Point
point(5, 5);
2273 View::ConvertPointToTarget(child_child
, child
, &point
);
2274 EXPECT_EQ(42, point
.x());
2275 EXPECT_EQ(48, point
.y());
2277 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2278 View::ConvertRectToTarget(child_child
, child
, &rect
);
2279 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2280 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2281 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2282 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2284 point
.SetPoint(42, 48);
2285 View::ConvertPointToTarget(child
, child_child
, &point
);
2286 EXPECT_EQ(5, point
.x());
2287 EXPECT_EQ(5, point
.y());
2289 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2290 View::ConvertRectToTarget(child
, child_child
, &rect
);
2291 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2292 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2293 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2294 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2297 // Conversions from top_view to child with a value that should be negative.
2298 // This ensures we don't round up with negative numbers.
2300 gfx::Point
point(6, 18);
2301 View::ConvertPointToTarget(&top_view
, child
, &point
);
2302 EXPECT_EQ(-1, point
.x());
2303 EXPECT_EQ(-1, point
.y());
2305 float error
= 0.01f
;
2306 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2307 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2308 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2309 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2310 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2311 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2314 // Rect conversions from top_view->child_2 and child_2->top_view.
2316 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2317 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2318 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2319 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2320 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2321 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2323 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2324 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2325 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2326 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2327 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2328 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2332 // Tests conversion methods to and from screen coordinates.
2333 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2334 scoped_ptr
<Widget
> widget(new Widget
);
2335 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2336 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2337 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2338 widget
->Init(params
);
2340 View
* child
= new View
;
2341 widget
->GetRootView()->AddChildView(child
);
2342 child
->SetBounds(10, 10, 100, 200);
2345 child
->SetTransform(t
);
2347 gfx::Point
point_in_screen(100, 90);
2348 gfx::Point
point_in_child(80, 60);
2350 gfx::Point point
= point_in_screen
;
2351 View::ConvertPointFromScreen(child
, &point
);
2352 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2354 View::ConvertPointToScreen(child
, &point
);
2355 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2358 // Tests conversion methods for rectangles.
2359 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2360 scoped_ptr
<Widget
> widget(new Widget
);
2361 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2362 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2363 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2364 widget
->Init(params
);
2365 View
* root
= widget
->GetRootView();
2367 TestView
* v1
= new TestView
;
2368 TestView
* v2
= new TestView
;
2369 root
->AddChildView(v1
);
2370 v1
->AddChildView(v2
);
2372 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2373 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2375 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2376 gfx::Rect
rect(5, 5, 15, 40);
2377 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2378 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2382 RotateCounterclockwise(&t2
);
2383 t2
.matrix().set(1, 3, 100.f
);
2384 v2
->SetTransform(t2
);
2386 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2387 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2388 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2393 v1
->SetTransform(t1
);
2395 // The rectangle should remain the same for |v1|.
2396 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2398 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2399 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2400 v2
->ConvertRectToWidget(rect
).ToString());
2405 class ObserverView
: public View
{
2408 virtual ~ObserverView();
2410 void ResetTestState();
2412 bool has_add_details() const { return has_add_details_
; }
2413 bool has_remove_details() const { return has_remove_details_
; }
2415 const ViewHierarchyChangedDetails
& add_details() const {
2416 return add_details_
;
2419 const ViewHierarchyChangedDetails
& remove_details() const {
2420 return remove_details_
;
2425 virtual void ViewHierarchyChanged(
2426 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
2428 bool has_add_details_
;
2429 bool has_remove_details_
;
2430 ViewHierarchyChangedDetails add_details_
;
2431 ViewHierarchyChangedDetails remove_details_
;
2433 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2436 ObserverView::ObserverView()
2437 : has_add_details_(false),
2438 has_remove_details_(false) {
2441 ObserverView::~ObserverView() {}
2443 void ObserverView::ResetTestState() {
2444 has_add_details_
= false;
2445 has_remove_details_
= false;
2446 add_details_
= ViewHierarchyChangedDetails();
2447 remove_details_
= ViewHierarchyChangedDetails();
2450 void ObserverView::ViewHierarchyChanged(
2451 const ViewHierarchyChangedDetails
& details
) {
2452 if (details
.is_add
) {
2453 has_add_details_
= true;
2454 add_details_
= details
;
2456 has_remove_details_
= true;
2457 remove_details_
= details
;
2461 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2462 // a child view is added or removed to all the views in the hierarchy (up and
2464 // The tree looks like this:
2468 // +-- v4 (starts here, then get reparented to v1)
2469 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2472 ObserverView
* v3
= new ObserverView();
2474 // Add |v3| to |v2|.
2475 scoped_ptr
<ObserverView
> v2(new ObserverView());
2476 v2
->AddChildView(v3
);
2478 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2480 EXPECT_TRUE(v2
->has_add_details());
2481 EXPECT_FALSE(v2
->has_remove_details());
2482 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2483 EXPECT_EQ(v3
, v2
->add_details().child
);
2484 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2486 EXPECT_TRUE(v3
->has_add_details());
2487 EXPECT_FALSE(v3
->has_remove_details());
2488 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2489 EXPECT_EQ(v3
, v3
->add_details().child
);
2490 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2492 // Reset everything to the initial state.
2493 v2
->ResetTestState();
2494 v3
->ResetTestState();
2497 v1
.AddChildView(v2
.get());
2499 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2500 // Make sure all the views (v1, v2, v3) received _that_ information.
2501 EXPECT_TRUE(v1
.has_add_details());
2502 EXPECT_FALSE(v1
.has_remove_details());
2503 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2504 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2505 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2507 EXPECT_TRUE(v2
->has_add_details());
2508 EXPECT_FALSE(v2
->has_remove_details());
2509 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2510 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2511 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2513 EXPECT_TRUE(v3
->has_add_details());
2514 EXPECT_FALSE(v3
->has_remove_details());
2515 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2516 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2517 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2519 // Reset everything to the initial state.
2520 v1
.ResetTestState();
2521 v2
->ResetTestState();
2522 v3
->ResetTestState();
2524 // Remove |v2| from |v1|.
2525 v1
.RemoveChildView(v2
.get());
2527 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2528 // Make sure all the views (v1, v2, v3) received _that_ information.
2529 EXPECT_FALSE(v1
.has_add_details());
2530 EXPECT_TRUE(v1
.has_remove_details());
2531 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2532 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2533 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2535 EXPECT_FALSE(v2
->has_add_details());
2536 EXPECT_TRUE(v2
->has_remove_details());
2537 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2538 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2539 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2541 EXPECT_FALSE(v3
->has_add_details());
2542 EXPECT_TRUE(v3
->has_remove_details());
2543 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2544 EXPECT_EQ(v3
, v3
->remove_details().child
);
2545 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2547 // Verifies notifications when reparenting a view.
2548 ObserverView
* v4
= new ObserverView();
2549 // Add |v4| to |v2|.
2550 v2
->AddChildView(v4
);
2552 // Reset everything to the initial state.
2553 v1
.ResetTestState();
2554 v2
->ResetTestState();
2555 v3
->ResetTestState();
2556 v4
->ResetTestState();
2558 // Reparent |v4| to |v1|.
2559 v1
.AddChildView(v4
);
2561 // Verifies that all views receive the correct information for all the child,
2562 // parent and move views.
2564 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2565 EXPECT_TRUE(v1
.has_add_details());
2566 EXPECT_FALSE(v1
.has_remove_details());
2567 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2568 EXPECT_EQ(v4
, v1
.add_details().child
);
2569 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2571 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2573 EXPECT_FALSE(v2
->has_add_details());
2574 EXPECT_TRUE(v2
->has_remove_details());
2575 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2576 EXPECT_EQ(v4
, v2
->remove_details().child
);
2577 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2579 // |v3| is not impacted by this operation, and hence receives no notification.
2580 EXPECT_FALSE(v3
->has_add_details());
2581 EXPECT_FALSE(v3
->has_remove_details());
2583 // |v4| is the reparented child, so it receives notifications for the remove
2584 // and then the add. |v2| is its old parent, |v1| is its new parent.
2585 EXPECT_TRUE(v4
->has_remove_details());
2586 EXPECT_TRUE(v4
->has_add_details());
2587 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2588 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2589 EXPECT_EQ(v4
, v4
->add_details().child
);
2590 EXPECT_EQ(v4
, v4
->remove_details().child
);
2591 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2592 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2595 // Verifies if the child views added under the root are all deleted when calling
2596 // RemoveAllChildViews.
2597 // The tree looks like this:
2606 TEST_F(ViewTest
, RemoveAllChildViews
) {
2609 View
* child1
= new View
;
2610 root
.AddChildView(child1
);
2612 for (int i
= 0; i
< 2; ++i
)
2613 root
.AddChildView(new View
);
2615 View
* foo
= new View
;
2616 child1
->AddChildView(foo
);
2618 // Add some nodes to |foo|.
2619 for (int i
= 0; i
< 3; ++i
)
2620 foo
->AddChildView(new View
);
2622 EXPECT_EQ(3, root
.child_count());
2623 EXPECT_EQ(1, child1
->child_count());
2624 EXPECT_EQ(3, foo
->child_count());
2626 // Now remove all child views from root.
2627 root
.RemoveAllChildViews(true);
2629 EXPECT_EQ(0, root
.child_count());
2630 EXPECT_FALSE(root
.has_children());
2633 TEST_F(ViewTest
, Contains
) {
2635 View
* v2
= new View
;
2636 View
* v3
= new View
;
2638 v1
.AddChildView(v2
);
2639 v2
->AddChildView(v3
);
2641 EXPECT_FALSE(v1
.Contains(NULL
));
2642 EXPECT_TRUE(v1
.Contains(&v1
));
2643 EXPECT_TRUE(v1
.Contains(v2
));
2644 EXPECT_TRUE(v1
.Contains(v3
));
2646 EXPECT_FALSE(v2
->Contains(NULL
));
2647 EXPECT_TRUE(v2
->Contains(v2
));
2648 EXPECT_FALSE(v2
->Contains(&v1
));
2649 EXPECT_TRUE(v2
->Contains(v3
));
2651 EXPECT_FALSE(v3
->Contains(NULL
));
2652 EXPECT_TRUE(v3
->Contains(v3
));
2653 EXPECT_FALSE(v3
->Contains(&v1
));
2654 EXPECT_FALSE(v3
->Contains(v2
));
2657 // Verifies if GetIndexOf() returns the correct index for the specified child
2659 // The tree looks like this:
2664 TEST_F(ViewTest
, GetIndexOf
) {
2667 View
* child1
= new View
;
2668 root
.AddChildView(child1
);
2670 View
* child2
= new View
;
2671 root
.AddChildView(child2
);
2673 View
* foo1
= new View
;
2674 child1
->AddChildView(foo1
);
2676 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2677 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2678 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2679 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2680 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2682 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2683 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2684 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2685 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2686 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2688 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2689 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2690 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2691 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2692 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2695 // Verifies that the child views can be reordered correctly.
2696 TEST_F(ViewTest
, ReorderChildren
) {
2699 View
* child
= new View();
2700 root
.AddChildView(child
);
2702 View
* foo1
= new View();
2703 child
->AddChildView(foo1
);
2704 View
* foo2
= new View();
2705 child
->AddChildView(foo2
);
2706 View
* foo3
= new View();
2707 child
->AddChildView(foo3
);
2708 foo1
->SetFocusable(true);
2709 foo2
->SetFocusable(true);
2710 foo3
->SetFocusable(true);
2712 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2713 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2714 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2715 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2716 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2717 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2719 // Move |foo2| at the end.
2720 child
->ReorderChildView(foo2
, -1);
2721 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2722 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2723 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2724 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2725 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2726 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2728 // Move |foo1| at the end.
2729 child
->ReorderChildView(foo1
, -1);
2730 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2731 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2732 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2733 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2734 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2735 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2736 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2738 // Move |foo2| to the front.
2739 child
->ReorderChildView(foo2
, 0);
2740 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2741 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2742 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2743 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2744 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2745 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2746 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2749 // Verifies that GetViewByID returns the correctly child view from the specified
2751 // The tree looks like this:
2756 TEST_F(ViewTest
, GetViewByID
) {
2758 const int kV1ID
= 1;
2762 const int kV2ID
= 2;
2766 const int kV3ID
= 3;
2770 const int kV4ID
= 4;
2773 const int kV5ID
= 5;
2775 v1
.AddChildView(&v2
);
2776 v2
.AddChildView(&v3
);
2777 v2
.AddChildView(&v4
);
2779 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2780 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2781 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2783 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2784 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2786 const int kGroup
= 1;
2787 v3
.SetGroup(kGroup
);
2788 v4
.SetGroup(kGroup
);
2791 v1
.GetViewsInGroup(kGroup
, &views
);
2792 EXPECT_EQ(2U, views
.size());
2794 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2795 EXPECT_NE(views
.end(), i
);
2797 i
= std::find(views
.begin(), views
.end(), &v4
);
2798 EXPECT_NE(views
.end(), i
);
2801 TEST_F(ViewTest
, AddExistingChild
) {
2804 v1
.AddChildView(&v2
);
2805 v1
.AddChildView(&v3
);
2806 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2807 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2809 // Check that there's no change in order when adding at same index.
2810 v1
.AddChildViewAt(&v2
, 0);
2811 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2812 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2813 v1
.AddChildViewAt(&v3
, 1);
2814 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2815 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2817 // Add it at a different index and check for change in order.
2818 v1
.AddChildViewAt(&v2
, 1);
2819 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
2820 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
2821 v1
.AddChildViewAt(&v2
, 0);
2822 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2823 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2825 // Check that calling |AddChildView()| does not change the order.
2826 v1
.AddChildView(&v2
);
2827 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2828 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2829 v1
.AddChildView(&v3
);
2830 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2831 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2834 ////////////////////////////////////////////////////////////////////////////////
2836 ////////////////////////////////////////////////////////////////////////////////
2840 // Test implementation of LayerAnimator.
2841 class TestLayerAnimator
: public ui::LayerAnimator
{
2843 TestLayerAnimator();
2845 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
2848 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
2851 virtual ~TestLayerAnimator() { }
2854 gfx::Rect last_bounds_
;
2856 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
2859 TestLayerAnimator::TestLayerAnimator()
2860 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2863 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
2864 last_bounds_
= bounds
;
2869 class ViewLayerTest
: public ViewsTestBase
{
2871 ViewLayerTest() : widget_(NULL
) {}
2873 virtual ~ViewLayerTest() {
2876 // Returns the Layer used by the RootView.
2877 ui::Layer
* GetRootLayer() {
2878 return widget()->GetLayer();
2881 virtual void SetUp() OVERRIDE
{
2883 widget_
= new Widget
;
2884 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2885 params
.bounds
= gfx::Rect(50, 50, 200, 200);
2886 widget_
->Init(params
);
2888 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
2891 virtual void TearDown() OVERRIDE
{
2892 widget_
->CloseNow();
2893 ViewsTestBase::TearDown();
2896 Widget
* widget() { return widget_
; }
2903 TEST_F(ViewLayerTest
, LayerToggling
) {
2904 // Because we lazily create textures the calls to DrawTree are necessary to
2905 // ensure we trigger creation of textures.
2906 ui::Layer
* root_layer
= widget()->GetLayer();
2907 View
* content_view
= new View
;
2908 widget()->SetContentsView(content_view
);
2910 // Create v1, give it a bounds and verify everything is set up correctly.
2911 View
* v1
= new View
;
2912 v1
->SetPaintToLayer(true);
2913 EXPECT_TRUE(v1
->layer() != NULL
);
2914 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2915 content_view
->AddChildView(v1
);
2916 ASSERT_TRUE(v1
->layer() != NULL
);
2917 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2918 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
2920 // Create v2 as a child of v1 and do basic assertion testing.
2921 View
* v2
= new View
;
2922 v1
->AddChildView(v2
);
2923 EXPECT_TRUE(v2
->layer() == NULL
);
2924 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
2925 v2
->SetPaintToLayer(true);
2926 ASSERT_TRUE(v2
->layer() != NULL
);
2927 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2928 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2930 // Turn off v1s layer. v2 should still have a layer but its parent should have
2932 v1
->SetPaintToLayer(false);
2933 EXPECT_TRUE(v1
->layer() == NULL
);
2934 EXPECT_TRUE(v2
->layer() != NULL
);
2935 EXPECT_EQ(root_layer
, v2
->layer()->parent());
2936 ASSERT_EQ(1u, root_layer
->children().size());
2937 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
2938 // The bounds of the layer should have changed to be relative to the root view
2940 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
2942 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2943 gfx::Transform transform
;
2944 transform
.Scale(2.0, 2.0);
2945 v1
->SetTransform(transform
);
2946 EXPECT_TRUE(v1
->layer() != NULL
);
2947 EXPECT_TRUE(v2
->layer() != NULL
);
2948 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2949 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2950 ASSERT_EQ(1u, root_layer
->children().size());
2951 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
2952 ASSERT_EQ(1u, v1
->layer()->children().size());
2953 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
2954 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2957 // Verifies turning on a layer wires up children correctly.
2958 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
2959 View
* content_view
= new View
;
2960 widget()->SetContentsView(content_view
);
2962 // Create v1, give it a bounds and verify everything is set up correctly.
2963 View
* v1
= new View
;
2964 content_view
->AddChildView(v1
);
2965 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2967 View
* v2
= new View
;
2968 v1
->AddChildView(v2
);
2970 View
* v3
= new View
;
2971 v3
->SetPaintToLayer(true);
2972 v2
->AddChildView(v3
);
2973 ASSERT_TRUE(v3
->layer() != NULL
);
2975 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2977 v1
->SetPaintToLayer(true);
2978 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
2981 TEST_F(ViewLayerTest
, LayerAnimator
) {
2982 View
* content_view
= new View
;
2983 widget()->SetContentsView(content_view
);
2985 View
* v1
= new View
;
2986 content_view
->AddChildView(v1
);
2987 v1
->SetPaintToLayer(true);
2988 EXPECT_TRUE(v1
->layer() != NULL
);
2990 TestLayerAnimator
* animator
= new TestLayerAnimator();
2991 v1
->layer()->SetAnimator(animator
);
2993 gfx::Rect
bounds(1, 2, 3, 4);
2994 v1
->SetBoundsRect(bounds
);
2995 EXPECT_EQ(bounds
, animator
->last_bounds());
2996 // TestLayerAnimator doesn't update the layer.
2997 EXPECT_NE(bounds
, v1
->layer()->bounds());
3000 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3001 // doesn't have a layer change.
3002 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
3003 View
* content_view
= new View
;
3004 widget()->SetContentsView(content_view
);
3006 View
* v1
= new View
;
3007 content_view
->AddChildView(v1
);
3008 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3010 View
* v2
= new View
;
3011 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3012 v1
->AddChildView(v2
);
3013 v2
->SetPaintToLayer(true);
3014 ASSERT_TRUE(v2
->layer() != NULL
);
3015 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
3017 v1
->SetPosition(gfx::Point(25, 36));
3018 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
3020 v2
->SetPosition(gfx::Point(11, 12));
3021 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
3023 // Bounds of the layer should change even if the view is not invisible.
3024 v1
->SetVisible(false);
3025 v1
->SetPosition(gfx::Point(20, 30));
3026 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
3028 v2
->SetVisible(false);
3029 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3030 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
3033 // Make sure layers are positioned correctly in RTL.
3034 TEST_F(ViewLayerTest
, BoundInRTL
) {
3035 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3036 base::i18n::SetICUDefaultLocale("he");
3038 View
* view
= new View
;
3039 widget()->SetContentsView(view
);
3041 int content_width
= view
->width();
3043 // |v1| is initially not attached to anything. So its layer will have the same
3044 // bounds as the view.
3045 View
* v1
= new View
;
3046 v1
->SetPaintToLayer(true);
3047 v1
->SetBounds(10, 10, 20, 10);
3048 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3049 v1
->layer()->bounds());
3051 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3053 view
->AddChildView(v1
);
3054 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3055 v1
->layer()->bounds());
3056 gfx::Rect l1bounds
= v1
->layer()->bounds();
3058 // Now attach a View to the widget first, then create a layer for it. Make
3059 // sure the bounds are correct.
3060 View
* v2
= new View
;
3061 v2
->SetBounds(50, 10, 30, 10);
3062 EXPECT_FALSE(v2
->layer());
3063 view
->AddChildView(v2
);
3064 v2
->SetPaintToLayer(true);
3065 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
3066 v2
->layer()->bounds());
3067 gfx::Rect l2bounds
= v2
->layer()->bounds();
3069 view
->SetPaintToLayer(true);
3070 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3071 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3073 // Move one of the views. Make sure the layer is positioned correctly
3075 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
3076 l1bounds
.set_x(l1bounds
.x() + 5);
3077 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3079 view
->SetPaintToLayer(false);
3080 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3081 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3083 // Move a view again.
3084 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
3085 l2bounds
.set_x(l2bounds
.x() - 5);
3086 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3089 base::i18n::SetICUDefaultLocale(locale
);
3092 // Makes sure a transform persists after toggling the visibility.
3093 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3094 View
* view
= new View
;
3095 gfx::Transform transform
;
3096 transform
.Scale(2.0, 2.0);
3097 view
->SetTransform(transform
);
3098 widget()->SetContentsView(view
);
3099 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3101 view
->SetVisible(false);
3102 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3104 view
->SetVisible(true);
3105 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3108 // Verifies a transform persists after removing/adding a view with a transform.
3109 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3110 View
* view
= new View
;
3111 gfx::Transform transform
;
3112 transform
.Scale(2.0, 2.0);
3113 view
->SetTransform(transform
);
3114 widget()->SetContentsView(view
);
3115 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3116 ASSERT_TRUE(view
->layer() != NULL
);
3117 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3119 View
* parent
= view
->parent();
3120 parent
->RemoveChildView(view
);
3121 parent
->AddChildView(view
);
3123 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3124 ASSERT_TRUE(view
->layer() != NULL
);
3125 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3128 // Makes sure that layer visibility is correct after toggling View visibility.
3129 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3130 View
* content_view
= new View
;
3131 widget()->SetContentsView(content_view
);
3133 // The view isn't attached to a widget or a parent view yet. But it should
3134 // still have a layer, but the layer should not be attached to the root
3136 View
* v1
= new View
;
3137 v1
->SetPaintToLayer(true);
3138 EXPECT_TRUE(v1
->layer());
3139 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3142 // Once the view is attached to a widget, its layer should be attached to the
3143 // root layer and visible.
3144 content_view
->AddChildView(v1
);
3145 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3147 EXPECT_TRUE(v1
->layer()->IsDrawn());
3149 v1
->SetVisible(false);
3150 EXPECT_FALSE(v1
->layer()->IsDrawn());
3152 v1
->SetVisible(true);
3153 EXPECT_TRUE(v1
->layer()->IsDrawn());
3156 EXPECT_FALSE(v1
->layer()->IsDrawn());
3159 EXPECT_TRUE(v1
->layer()->IsDrawn());
3162 // Tests that the layers in the subtree are orphaned after a View is removed
3164 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3165 View
* content_view
= new View
;
3166 widget()->SetContentsView(content_view
);
3168 View
* v1
= new View
;
3169 content_view
->AddChildView(v1
);
3171 View
* v2
= new View
;
3172 v1
->AddChildView(v2
);
3173 v2
->SetPaintToLayer(true);
3174 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3176 EXPECT_TRUE(v2
->layer()->IsDrawn());
3178 content_view
->RemoveChildView(v1
);
3180 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3184 content_view
->AddChildView(v2
);
3187 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3189 EXPECT_TRUE(v2
->layer()->IsDrawn());
3192 class PaintTrackingView
: public View
{
3194 PaintTrackingView() : painted_(false) {
3197 bool painted() const { return painted_
; }
3198 void set_painted(bool value
) { painted_
= value
; }
3200 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
3207 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3210 // Makes sure child views with layers aren't painted when paint starts at an
3212 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3213 PaintTrackingView
* content_view
= new PaintTrackingView
;
3214 widget()->SetContentsView(content_view
);
3215 content_view
->SetPaintToLayer(true);
3216 GetRootLayer()->GetCompositor()->ScheduleDraw();
3217 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3218 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3219 content_view
->set_painted(false);
3220 // content_view no longer has a dirty rect. Paint from the root and make sure
3221 // PaintTrackingView isn't painted.
3222 GetRootLayer()->GetCompositor()->ScheduleDraw();
3223 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3224 EXPECT_FALSE(content_view
->painted());
3226 // Make content_view have a dirty rect, paint the layers and make sure
3227 // PaintTrackingView is painted.
3228 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3229 GetRootLayer()->GetCompositor()->ScheduleDraw();
3230 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3231 EXPECT_TRUE(content_view
->painted());
3234 // Tests that the visibility of child layers are updated correctly when a View's
3235 // visibility changes.
3236 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3237 View
* v1
= new View
;
3238 v1
->SetPaintToLayer(true);
3239 widget()->SetContentsView(v1
);
3241 View
* v2
= new View
;
3242 v1
->AddChildView(v2
);
3244 View
* v3
= new View
;
3245 v2
->AddChildView(v3
);
3246 v3
->SetVisible(false);
3248 View
* v4
= new View
;
3249 v4
->SetPaintToLayer(true);
3250 v3
->AddChildView(v4
);
3252 EXPECT_TRUE(v1
->layer()->IsDrawn());
3253 EXPECT_FALSE(v4
->layer()->IsDrawn());
3255 v2
->SetVisible(false);
3256 EXPECT_TRUE(v1
->layer()->IsDrawn());
3257 EXPECT_FALSE(v4
->layer()->IsDrawn());
3259 v2
->SetVisible(true);
3260 EXPECT_TRUE(v1
->layer()->IsDrawn());
3261 EXPECT_FALSE(v4
->layer()->IsDrawn());
3263 v2
->SetVisible(false);
3264 EXPECT_TRUE(v1
->layer()->IsDrawn());
3265 EXPECT_FALSE(v4
->layer()->IsDrawn());
3266 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3268 v3
->SetVisible(true);
3269 EXPECT_TRUE(v1
->layer()->IsDrawn());
3270 EXPECT_FALSE(v4
->layer()->IsDrawn());
3271 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3273 // Reparent |v3| to |v1|.
3274 v1
->AddChildView(v3
);
3275 EXPECT_TRUE(v1
->layer()->IsDrawn());
3276 EXPECT_TRUE(v4
->layer()->IsDrawn());
3277 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3280 // This test creates a random View tree, and then randomly reorders child views,
3281 // reparents views etc. Unrelated changes can appear to break this test. So
3282 // marking this as FLAKY.
3283 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3284 View
* content
= new View
;
3285 content
->SetPaintToLayer(true);
3286 widget()->SetContentsView(content
);
3289 ConstructTree(content
, 5);
3290 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3292 ScrambleTree(content
);
3293 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3295 ScrambleTree(content
);
3296 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3298 ScrambleTree(content
);
3299 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3302 // Verifies when views are reordered the layer is also reordered. The widget is
3303 // providing the parent layer.
3304 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3305 View
* content
= new View
;
3306 widget()->SetContentsView(content
);
3307 View
* c1
= new View
;
3308 c1
->SetPaintToLayer(true);
3309 content
->AddChildView(c1
);
3310 View
* c2
= new View
;
3311 c2
->SetPaintToLayer(true);
3312 content
->AddChildView(c2
);
3314 ui::Layer
* parent_layer
= c1
->layer()->parent();
3315 ASSERT_TRUE(parent_layer
);
3316 ASSERT_EQ(2u, parent_layer
->children().size());
3317 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3318 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3320 // Move c1 to the front. The layers should have moved too.
3321 content
->ReorderChildView(c1
, -1);
3322 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3323 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3326 // Verifies that the layer of a view can be acquired properly.
3327 TEST_F(ViewLayerTest
, AcquireLayer
) {
3328 View
* content
= new View
;
3329 widget()->SetContentsView(content
);
3330 scoped_ptr
<View
> c1(new View
);
3331 c1
->SetPaintToLayer(true);
3332 EXPECT_TRUE(c1
->layer());
3333 content
->AddChildView(c1
.get());
3335 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3336 EXPECT_EQ(layer
.get(), c1
->layer());
3338 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3339 EXPECT_NE(c1
->layer(), layer2
.get());
3341 // Destroy view before destroying layer.
3345 // Verify the z-order of the layers as a result of calling RecreateLayer().
3346 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3347 scoped_ptr
<View
> v(new View());
3348 v
->SetPaintToLayer(true);
3350 View
* v1
= new View();
3351 v1
->SetPaintToLayer(true);
3352 v
->AddChildView(v1
);
3353 View
* v2
= new View();
3354 v2
->SetPaintToLayer(true);
3355 v
->AddChildView(v2
);
3357 // Test the initial z-order.
3358 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3359 ASSERT_EQ(2u, child_layers_pre
.size());
3360 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3361 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3363 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3365 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3366 // for |v1| and |v2|.
3367 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3368 ASSERT_EQ(3u, child_layers_post
.size());
3369 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3370 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3371 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3374 // Verify the z-order of the layers as a result of calling RecreateLayer when
3375 // the widget is the parent with the layer.
3376 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3377 View
* v
= new View();
3378 widget()->SetContentsView(v
);
3380 View
* v1
= new View();
3381 v1
->SetPaintToLayer(true);
3382 v
->AddChildView(v1
);
3383 View
* v2
= new View();
3384 v2
->SetPaintToLayer(true);
3385 v
->AddChildView(v2
);
3387 ui::Layer
* root_layer
= GetRootLayer();
3389 // Test the initial z-order.
3390 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3391 ASSERT_EQ(2u, child_layers_pre
.size());
3392 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3393 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3395 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3397 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3398 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3399 ASSERT_EQ(3u, child_layers_post
.size());
3400 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3401 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3402 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3405 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3407 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3409 v
.SetPaintToLayer(true);
3411 child
.SetPaintToLayer(true);
3412 v
.AddChildView(&child
);
3413 ASSERT_TRUE(v
.layer() != NULL
);
3414 ASSERT_EQ(1u, v
.layer()->children().size());
3415 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3417 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3418 v
.layer()->Add(&layer
);
3419 v
.layer()->StackAtBottom(&layer
);
3421 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3423 // All children should be moved from old layer to new layer.
3424 ASSERT_TRUE(old_layer
.get() != NULL
);
3425 EXPECT_TRUE(old_layer
->children().empty());
3427 // And new layer should have the two children.
3428 ASSERT_TRUE(v
.layer() != NULL
);
3429 ASSERT_EQ(2u, v
.layer()->children().size());
3430 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3431 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3434 class BoundsTreeTestView
: public View
{
3436 BoundsTreeTestView() {}
3438 virtual void PaintChildren(gfx::Canvas
* canvas
,
3439 const CullSet
& cull_set
) OVERRIDE
{
3440 // Save out a copy of the cull_set before calling the base implementation.
3441 last_cull_set_
.clear();
3442 if (cull_set
.cull_set_
) {
3443 for (base::hash_set
<intptr_t>::iterator it
= cull_set
.cull_set_
->begin();
3444 it
!= cull_set
.cull_set_
->end();
3446 last_cull_set_
.insert(reinterpret_cast<View
*>(*it
));
3449 View::PaintChildren(canvas
, cull_set
);
3452 std::set
<View
*> last_cull_set_
;
3455 TEST_F(ViewLayerTest
, BoundsTreePaintUpdatesCullSet
) {
3456 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3457 widget()->SetContentsView(test_view
);
3459 View
* v1
= new View();
3460 v1
->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3461 test_view
->AddChildView(v1
);
3463 View
* v2
= new View();
3464 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3465 v1
->AddChildView(v2
);
3467 // Schedule a full-view paint to get everyone's rectangles updated.
3468 test_view
->SchedulePaintInRect(test_view
->bounds());
3469 GetRootLayer()->GetCompositor()->ScheduleDraw();
3470 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3472 // Now we have test_view - v1 - v2. Damage to only test_view should only
3473 // return root_view and test_view.
3474 test_view
->SchedulePaintInRect(gfx::Rect(0, 0, 1, 1));
3475 GetRootLayer()->GetCompositor()->ScheduleDraw();
3476 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3477 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3478 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3479 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3481 // Damage to v1 only should only return root_view, test_view, and v1.
3482 test_view
->SchedulePaintInRect(gfx::Rect(11, 16, 1, 1));
3483 GetRootLayer()->GetCompositor()->ScheduleDraw();
3484 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3485 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3486 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3487 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3488 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3490 // A Damage rect inside v2 should get all 3 views back in the |last_cull_set_|
3491 // on call to TestView::Paint(), along with the widget root view.
3492 test_view
->SchedulePaintInRect(gfx::Rect(31, 49, 1, 1));
3493 GetRootLayer()->GetCompositor()->ScheduleDraw();
3494 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3495 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3496 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3497 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3498 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3499 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3502 TEST_F(ViewLayerTest
, BoundsTreeWithRTL
) {
3503 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3504 base::i18n::SetICUDefaultLocale("ar");
3506 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3507 widget()->SetContentsView(test_view
);
3509 // Add child views, which should be in RTL coordinate space of parent view.
3510 View
* v1
= new View
;
3511 v1
->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3512 test_view
->AddChildView(v1
);
3514 View
* v2
= new View
;
3515 v2
->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3516 v1
->AddChildView(v2
);
3518 // Schedule a full-view paint to get everyone's rectangles updated.
3519 test_view
->SchedulePaintInRect(test_view
->bounds());
3520 GetRootLayer()->GetCompositor()->ScheduleDraw();
3521 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3523 // Damage to the right side of the parent view should touch both child views.
3524 gfx::Rect
rtl_damage(test_view
->bounds().width() - 16, 18, 1, 1);
3525 test_view
->SchedulePaintInRect(rtl_damage
);
3526 GetRootLayer()->GetCompositor()->ScheduleDraw();
3527 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3528 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3529 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3530 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3531 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3532 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3534 // Damage to the left side of the parent view should only touch the
3536 gfx::Rect
ltr_damage(16, 18, 1, 1);
3537 test_view
->SchedulePaintInRect(ltr_damage
);
3538 GetRootLayer()->GetCompositor()->ScheduleDraw();
3539 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3540 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3541 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3542 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3545 base::i18n::SetICUDefaultLocale(locale
);
3548 TEST_F(ViewLayerTest
, BoundsTreeSetBoundsChangesCullSet
) {
3549 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3550 widget()->SetContentsView(test_view
);
3552 View
* v1
= new View
;
3553 v1
->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3554 test_view
->AddChildView(v1
);
3556 View
* v2
= new View
;
3557 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3558 v1
->AddChildView(v2
);
3560 // Schedule a full-view paint to get everyone's rectangles updated.
3561 test_view
->SchedulePaintInRect(test_view
->bounds());
3562 GetRootLayer()->GetCompositor()->ScheduleDraw();
3563 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3565 // Move v1 to a new origin out of the way of our next query.
3566 v1
->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3567 // The move will force a repaint.
3568 GetRootLayer()->GetCompositor()->ScheduleDraw();
3569 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3571 // Schedule a paint with damage rect where v1 used to be.
3572 test_view
->SchedulePaintInRect(gfx::Rect(5, 6, 10, 11));
3573 GetRootLayer()->GetCompositor()->ScheduleDraw();
3574 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3576 // Should only have picked up root_view and test_view.
3577 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3578 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3579 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3582 TEST_F(ViewLayerTest
, BoundsTreeLayerChangeMakesNewTree
) {
3583 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3584 widget()->SetContentsView(test_view
);
3586 View
* v1
= new View
;
3587 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3588 test_view
->AddChildView(v1
);
3590 View
* v2
= new View
;
3591 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3592 v1
->AddChildView(v2
);
3594 // Schedule a full-view paint to get everyone's rectangles updated.
3595 test_view
->SchedulePaintInRect(test_view
->bounds());
3596 GetRootLayer()->GetCompositor()->ScheduleDraw();
3597 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3599 // Set v1 to paint to its own layer, it should remove itself from the
3600 // test_view heiarchy and no longer intersect with damage rects in that cull
3602 v1
->SetPaintToLayer(true);
3604 // Schedule another full-view paint.
3605 test_view
->SchedulePaintInRect(test_view
->bounds());
3606 GetRootLayer()->GetCompositor()->ScheduleDraw();
3607 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3608 // v1 and v2 should no longer be present in the test_view cull_set.
3609 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3610 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3611 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3613 // Now set v1 back to not painting to a layer.
3614 v1
->SetPaintToLayer(false);
3615 // Schedule another full-view paint.
3616 test_view
->SchedulePaintInRect(test_view
->bounds());
3617 GetRootLayer()->GetCompositor()->ScheduleDraw();
3618 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3619 // We should be back to the full cull set including v1 and v2.
3620 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3621 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3622 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3623 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3624 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3627 TEST_F(ViewLayerTest
, BoundsTreeRemoveChildRemovesBounds
) {
3628 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3629 widget()->SetContentsView(test_view
);
3631 View
* v1
= new View
;
3632 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3633 test_view
->AddChildView(v1
);
3635 View
* v2
= new View
;
3636 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3637 v1
->AddChildView(v2
);
3639 // Schedule a full-view paint to get everyone's rectangles updated.
3640 test_view
->SchedulePaintInRect(test_view
->bounds());
3641 GetRootLayer()->GetCompositor()->ScheduleDraw();
3642 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3644 // Now remove v1 from the root view.
3645 test_view
->RemoveChildView(v1
);
3647 // Schedule another full-view paint.
3648 test_view
->SchedulePaintInRect(test_view
->bounds());
3649 GetRootLayer()->GetCompositor()->ScheduleDraw();
3650 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3651 // v1 and v2 should no longer be present in the test_view cull_set.
3652 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3653 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3654 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3656 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3657 // deleted with that hierarchy.
3661 TEST_F(ViewLayerTest
, BoundsTreeMoveViewMovesBounds
) {
3662 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3663 widget()->SetContentsView(test_view
);
3665 // Build hierarchy v1 - v2 - v3.
3666 View
* v1
= new View
;
3667 v1
->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3668 test_view
->AddChildView(v1
);
3670 View
* v2
= new View
;
3671 v2
->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3672 v1
->AddChildView(v2
);
3674 View
* v3
= new View
;
3675 v3
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3676 v2
->AddChildView(v3
);
3678 // Schedule a full-view paint and ensure all views are present in the cull.
3679 test_view
->SchedulePaintInRect(test_view
->bounds());
3680 GetRootLayer()->GetCompositor()->ScheduleDraw();
3681 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3682 EXPECT_EQ(5U, test_view
->last_cull_set_
.size());
3683 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3684 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3685 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3686 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3687 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v3
));
3689 // Build an unrelated view hierarchy and move v2 in to it.
3690 scoped_ptr
<Widget
> test_widget(new Widget
);
3691 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3692 params
.bounds
= gfx::Rect(10, 10, 500, 500);
3693 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3694 test_widget
->Init(params
);
3695 test_widget
->Show();
3696 BoundsTreeTestView
* widget_view
= new BoundsTreeTestView
;
3697 test_widget
->SetContentsView(widget_view
);
3698 widget_view
->AddChildView(v2
);
3700 // Now schedule full-view paints in both widgets.
3701 test_view
->SchedulePaintInRect(test_view
->bounds());
3702 widget_view
->SchedulePaintInRect(widget_view
->bounds());
3703 GetRootLayer()->GetCompositor()->ScheduleDraw();
3704 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3706 // Only v1 should be present in the first cull set.
3707 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3708 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3709 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3710 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3712 // We should find v2 and v3 in the widget_view cull_set.
3713 EXPECT_EQ(4U, widget_view
->last_cull_set_
.size());
3714 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(test_widget
->GetRootView()));
3715 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(widget_view
));
3716 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v2
));
3717 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v3
));
3722 std::string
ToString(const gfx::Vector2dF
& vector
) {
3723 return base::StringPrintf("%.2f %0.2f", vector
.x(), vector
.y());
3728 TEST_F(ViewLayerTest
, SnapLayerToPixel
) {
3729 View
* v1
= new View
;
3731 View
* v11
= new View
;
3732 v1
->AddChildView(v11
);
3734 widget()->SetContentsView(v1
);
3736 const gfx::Size
& size
= GetRootLayer()->GetCompositor()->size();
3737 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f
, size
);
3739 v11
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3740 v1
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3741 v11
->SetPaintToLayer(true);
3743 EXPECT_EQ("0.40 0.40", ToString(v11
->layer()->subpixel_position_offset()));
3745 // Creating a layer in parent should update the child view's layer offset.
3746 v1
->SetPaintToLayer(true);
3747 EXPECT_EQ("-0.20 -0.20", ToString(v1
->layer()->subpixel_position_offset()));
3748 EXPECT_EQ("-0.20 -0.20", ToString(v11
->layer()->subpixel_position_offset()));
3750 // DSF change should get propagated and update offsets.
3751 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f
, size
);
3752 EXPECT_EQ("0.33 0.33", ToString(v1
->layer()->subpixel_position_offset()));
3753 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3755 // Deleting parent's layer should update the child view's layer's offset.
3756 v1
->SetPaintToLayer(false);
3757 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3759 // Setting parent view should update the child view's layer's offset.
3760 v1
->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3761 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3763 // Setting integral DSF should reset the offset.
3764 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f
, size
);
3765 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3768 TEST_F(ViewTest
, FocusableAssertions
) {
3769 // View subclasses may change insets based on whether they are focusable,
3770 // which effects the preferred size. To avoid preferred size changing around
3771 // these Views need to key off the last value set to SetFocusable(), not
3772 // whether the View is focusable right now. For this reason it's important
3773 // that focusable() return the last value passed to SetFocusable and not
3774 // whether the View is focusable right now.
3776 view
.SetFocusable(true);
3777 EXPECT_TRUE(view
.focusable());
3778 view
.SetEnabled(false);
3779 EXPECT_TRUE(view
.focusable());
3780 view
.SetFocusable(false);
3781 EXPECT_FALSE(view
.focusable());
3784 // Verifies when a view is deleted it is removed from ViewStorage.
3785 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3786 ViewStorage
* view_storage
= ViewStorage::GetInstance();
3787 const int storage_id
= view_storage
->CreateStorageID();
3790 view_storage
->StoreView(storage_id
, &view
);
3792 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
3795 ////////////////////////////////////////////////////////////////////////////////
3797 ////////////////////////////////////////////////////////////////////////////////
3799 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
3800 native_theme_
= native_theme
;
3803 TEST_F(ViewTest
, OnNativeThemeChanged
) {
3804 TestView
* test_view
= new TestView();
3805 EXPECT_FALSE(test_view
->native_theme_
);
3806 TestView
* test_view_child
= new TestView();
3807 EXPECT_FALSE(test_view_child
->native_theme_
);
3809 // Child view added before the widget hierarchy exists should get the
3810 // new native theme notification.
3811 test_view
->AddChildView(test_view_child
);
3813 scoped_ptr
<Widget
> widget(new Widget
);
3814 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
3815 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3816 widget
->Init(params
);
3818 widget
->GetRootView()->AddChildView(test_view
);
3819 EXPECT_TRUE(test_view
->native_theme_
);
3820 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
3821 EXPECT_TRUE(test_view_child
->native_theme_
);
3822 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
3824 // Child view added after the widget hierarchy exists should also get the
3826 TestView
* test_view_child_2
= new TestView();
3827 test_view
->AddChildView(test_view_child_2
);
3828 EXPECT_TRUE(test_view_child_2
->native_theme_
);
3829 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
3834 } // namespace views