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/utf_string_conversions.h"
11 #include "grit/ui_strings.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/base/accelerators/accelerator.h"
15 #include "ui/base/clipboard/clipboard.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/compositor/compositor.h"
18 #include "ui/compositor/layer.h"
19 #include "ui/compositor/layer_animator.h"
20 #include "ui/compositor/layer_tree_owner.h"
21 #include "ui/compositor/test/draw_waiter_for_test.h"
22 #include "ui/compositor/test/test_layers.h"
23 #include "ui/events/event.h"
24 #include "ui/events/gestures/gesture_recognizer.h"
25 #include "ui/events/keycodes/keyboard_codes.h"
26 #include "ui/gfx/canvas.h"
27 #include "ui/gfx/path.h"
28 #include "ui/gfx/transform.h"
29 #include "ui/views/background.h"
30 #include "ui/views/controls/native/native_view_host.h"
31 #include "ui/views/controls/scroll_view.h"
32 #include "ui/views/controls/textfield/textfield.h"
33 #include "ui/views/focus/view_storage.h"
34 #include "ui/views/test/views_test_base.h"
35 #include "ui/views/view.h"
36 #include "ui/views/view_constants_aura.h"
37 #include "ui/views/views_delegate.h"
38 #include "ui/views/widget/native_widget.h"
39 #include "ui/views/widget/root_view.h"
40 #include "ui/views/window/dialog_client_view.h"
41 #include "ui/views/window/dialog_delegate.h"
42 #include "ui/wm/core/window_util.h"
44 using base::ASCIIToUTF16
;
48 // Returns true if |ancestor| is an ancestor of |layer|.
49 bool LayerIsAncestor(const ui::Layer
* ancestor
, const ui::Layer
* layer
) {
50 while (layer
&& layer
!= ancestor
)
51 layer
= layer
->parent();
52 return layer
== ancestor
;
55 // Convenience functions for walking a View tree.
56 const views::View
* FirstView(const views::View
* view
) {
57 const views::View
* v
= view
;
58 while (v
->has_children())
63 const views::View
* NextView(const views::View
* view
) {
64 const views::View
* v
= view
;
65 const views::View
* parent
= v
->parent();
68 int next
= parent
->GetIndexOf(v
) + 1;
69 if (next
!= parent
->child_count())
70 return FirstView(parent
->child_at(next
));
74 // Convenience functions for walking a Layer tree.
75 const ui::Layer
* FirstLayer(const ui::Layer
* layer
) {
76 const ui::Layer
* l
= layer
;
77 while (l
->children().size() > 0)
82 const ui::Layer
* NextLayer(const ui::Layer
* layer
) {
83 const ui::Layer
* parent
= layer
->parent();
86 const std::vector
<ui::Layer
*> children
= parent
->children();
88 for (index
= 0; index
< children
.size(); index
++) {
89 if (children
[index
] == layer
)
92 size_t next
= index
+ 1;
93 if (next
< children
.size())
94 return FirstLayer(children
[next
]);
98 // Given the root nodes of a View tree and a Layer tree, makes sure the two
100 bool ViewAndLayerTreeAreConsistent(const views::View
* view
,
101 const ui::Layer
* layer
) {
102 const views::View
* v
= FirstView(view
);
103 const ui::Layer
* l
= FirstLayer(layer
);
105 // Find the view with a layer.
106 while (v
&& !v
->layer())
112 // Check if the View tree and the Layer tree are in sync.
113 EXPECT_EQ(l
, v
->layer());
117 // Check if the visibility states of the View and the Layer are in sync.
118 EXPECT_EQ(l
->IsDrawn(), v
->IsDrawn());
119 if (v
->IsDrawn() != l
->IsDrawn()) {
120 for (const views::View
* vv
= v
; vv
; vv
= vv
->parent())
121 LOG(ERROR
) << "V: " << vv
<< " " << vv
->visible() << " "
122 << vv
->IsDrawn() << " " << vv
->layer();
123 for (const ui::Layer
* ll
= l
; ll
; ll
= ll
->parent())
124 LOG(ERROR
) << "L: " << ll
<< " " << ll
->IsDrawn();
128 // Check if the size of the View and the Layer are in sync.
129 EXPECT_EQ(l
->bounds(), v
->bounds());
130 if (v
->bounds() != l
->bounds())
133 if (v
== view
|| l
== layer
)
134 return v
== view
&& l
== layer
;
143 // Constructs a View tree with the specified depth.
144 void ConstructTree(views::View
* view
, int depth
) {
147 int count
= base::RandInt(1, 5);
148 for (int i
= 0; i
< count
; i
++) {
149 views::View
* v
= new views::View
;
150 view
->AddChildView(v
);
151 if (base::RandDouble() > 0.5)
152 v
->SetPaintToLayer(true);
153 if (base::RandDouble() < 0.2)
154 v
->SetVisible(false);
156 ConstructTree(v
, depth
- 1);
160 void ScrambleTree(views::View
* view
) {
161 int count
= view
->child_count();
164 for (int i
= 0; i
< count
; i
++) {
165 ScrambleTree(view
->child_at(i
));
169 int a
= base::RandInt(0, count
- 1);
170 int b
= base::RandInt(0, count
- 1);
172 views::View
* view_a
= view
->child_at(a
);
173 views::View
* view_b
= view
->child_at(b
);
174 view
->ReorderChildView(view_a
, b
);
175 view
->ReorderChildView(view_b
, a
);
178 if (!view
->layer() && base::RandDouble() < 0.1)
179 view
->SetPaintToLayer(true);
181 if (base::RandDouble() < 0.1)
182 view
->SetVisible(!view
->visible());
185 // Convenience to make constructing a GestureEvent simpler.
186 class GestureEventForTest
: public ui::GestureEvent
{
188 GestureEventForTest(ui::EventType type
, int x
, int y
, int flags
)
189 : GestureEvent(type
, x
, y
, flags
, base::TimeDelta(),
190 ui::GestureEventDetails(type
, 0.0f
, 0.0f
), 0) {
194 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest
);
201 typedef ViewsTestBase ViewTest
;
203 // A derived class for testing purpose.
204 class TestView
: public View
{
208 delete_on_pressed_(false),
210 can_process_events_within_subtree_(true) {}
211 virtual ~TestView() {}
213 // Reset all test state
215 did_change_bounds_
= false;
216 last_mouse_event_type_
= 0;
217 location_
.SetPoint(0, 0);
218 received_mouse_enter_
= false;
219 received_mouse_exit_
= false;
220 last_gesture_event_type_
= 0;
221 last_gesture_event_was_handled_
= false;
222 last_clip_
.setEmpty();
223 accelerator_count_map_
.clear();
224 can_process_events_within_subtree_
= true;
227 // Exposed as public for testing.
229 views::View::Focus();
236 bool focusable() const { return View::focusable(); }
238 void set_can_process_events_within_subtree(bool can_process
) {
239 can_process_events_within_subtree_
= can_process
;
242 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE
{
243 return can_process_events_within_subtree_
;
246 virtual void OnBoundsChanged(const gfx::Rect
& previous_bounds
) OVERRIDE
;
247 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
248 virtual bool OnMouseDragged(const ui::MouseEvent
& event
) OVERRIDE
;
249 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
250 virtual void OnMouseEntered(const ui::MouseEvent
& event
) OVERRIDE
;
251 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
253 // Ignores GestureEvent by default.
254 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
;
256 virtual void Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) OVERRIDE
;
257 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
;
258 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
260 virtual void OnNativeThemeChanged(const ui::NativeTheme
* native_theme
)
264 bool did_change_bounds_
;
265 gfx::Rect new_bounds_
;
268 int last_mouse_event_type_
;
269 gfx::Point location_
;
270 bool received_mouse_enter_
;
271 bool received_mouse_exit_
;
272 bool delete_on_pressed_
;
275 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
278 int last_gesture_event_type_
;
279 bool last_gesture_event_was_handled_
;
285 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
288 const ui::NativeTheme
* native_theme_
;
290 // Value to return from CanProcessEventsWithinSubtree().
291 bool can_process_events_within_subtree_
;
294 // A view subclass that consumes all Gesture events for testing purposes.
295 class TestViewConsumeGesture
: public TestView
{
297 TestViewConsumeGesture() : TestView() {}
298 virtual ~TestViewConsumeGesture() {}
301 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
302 last_gesture_event_type_
= event
->type();
303 location_
.SetPoint(event
->x(), event
->y());
304 event
->StopPropagation();
308 DISALLOW_COPY_AND_ASSIGN(TestViewConsumeGesture
);
311 // A view subclass that ignores all Gesture events.
312 class TestViewIgnoreGesture
: public TestView
{
314 TestViewIgnoreGesture() : TestView() {}
315 virtual ~TestViewIgnoreGesture() {}
318 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
321 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreGesture
);
324 // A view subclass that ignores all scroll-gesture events, but consume all other
326 class TestViewIgnoreScrollGestures
: public TestViewConsumeGesture
{
328 TestViewIgnoreScrollGestures() {}
329 virtual ~TestViewIgnoreScrollGestures() {}
332 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
333 if (event
->IsScrollGestureEvent())
335 TestViewConsumeGesture::OnGestureEvent(event
);
338 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreScrollGestures
);
341 ////////////////////////////////////////////////////////////////////////////////
343 ////////////////////////////////////////////////////////////////////////////////
345 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
346 did_change_bounds_
= true;
347 new_bounds_
= bounds();
350 TEST_F(ViewTest
, OnBoundsChanged
) {
353 gfx::Rect
prev_rect(0, 0, 200, 200);
354 gfx::Rect
new_rect(100, 100, 250, 250);
356 v
.SetBoundsRect(prev_rect
);
358 v
.SetBoundsRect(new_rect
);
360 EXPECT_TRUE(v
.did_change_bounds_
);
361 EXPECT_EQ(v
.new_bounds_
, new_rect
);
362 EXPECT_EQ(v
.bounds(), new_rect
);
365 ////////////////////////////////////////////////////////////////////////////////
367 ////////////////////////////////////////////////////////////////////////////////
369 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
370 last_mouse_event_type_
= event
.type();
371 location_
.SetPoint(event
.x(), event
.y());
372 if (delete_on_pressed_
)
377 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
378 last_mouse_event_type_
= event
.type();
379 location_
.SetPoint(event
.x(), event
.y());
383 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
384 last_mouse_event_type_
= event
.type();
385 location_
.SetPoint(event
.x(), event
.y());
388 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
389 received_mouse_enter_
= true;
392 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
393 received_mouse_exit_
= true;
396 TEST_F(ViewTest
, MouseEvent
) {
397 TestView
* v1
= new TestView();
398 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
400 TestView
* v2
= new TestView();
401 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
403 scoped_ptr
<Widget
> widget(new Widget
);
404 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
405 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
406 params
.bounds
= gfx::Rect(50, 50, 650, 650);
407 widget
->Init(params
);
408 internal::RootView
* root
=
409 static_cast<internal::RootView
*>(widget
->GetRootView());
411 root
->AddChildView(v1
);
412 v1
->AddChildView(v2
);
417 gfx::Point
p1(110, 120);
418 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
419 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
420 root
->OnMousePressed(pressed
);
421 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
422 EXPECT_EQ(v2
->location_
.x(), 10);
423 EXPECT_EQ(v2
->location_
.y(), 20);
424 // Make sure v1 did not receive the event
425 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
427 // Drag event out of bounds. Should still go to v2
430 gfx::Point
p2(50, 40);
431 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
,
432 ui::EF_LEFT_MOUSE_BUTTON
, 0);
433 root
->OnMouseDragged(dragged
);
434 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
435 EXPECT_EQ(v2
->location_
.x(), -50);
436 EXPECT_EQ(v2
->location_
.y(), -60);
437 // Make sure v1 did not receive the event
438 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
440 // Releasted event out of bounds. Should still go to v2
443 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
445 root
->OnMouseDragged(released
);
446 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
447 EXPECT_EQ(v2
->location_
.x(), -100);
448 EXPECT_EQ(v2
->location_
.y(), -100);
449 // Make sure v1 did not receive the event
450 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
455 // Confirm that a view can be deleted as part of processing a mouse press.
456 TEST_F(ViewTest
, DeleteOnPressed
) {
457 TestView
* v1
= new TestView();
458 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
460 TestView
* v2
= new TestView();
461 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
466 scoped_ptr
<Widget
> widget(new Widget
);
467 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
468 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
469 params
.bounds
= gfx::Rect(50, 50, 650, 650);
470 widget
->Init(params
);
471 View
* root
= widget
->GetRootView();
473 root
->AddChildView(v1
);
474 v1
->AddChildView(v2
);
476 v2
->delete_on_pressed_
= true;
477 gfx::Point
point(110, 120);
478 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
479 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
480 root
->OnMousePressed(pressed
);
481 EXPECT_EQ(0, v1
->child_count());
486 ////////////////////////////////////////////////////////////////////////////////
488 ////////////////////////////////////////////////////////////////////////////////
490 void TestView::OnGestureEvent(ui::GestureEvent
* event
) {
493 TEST_F(ViewTest
, GestureEvent
) {
494 // Views hierarchy for non delivery of GestureEvent.
495 TestView
* v1
= new TestViewConsumeGesture();
496 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
498 TestView
* v2
= new TestViewConsumeGesture();
499 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
501 TestView
* v3
= new TestViewIgnoreGesture();
502 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
504 scoped_ptr
<Widget
> widget(new Widget());
505 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
506 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
507 params
.bounds
= gfx::Rect(50, 50, 650, 650);
508 widget
->Init(params
);
509 internal::RootView
* root
=
510 static_cast<internal::RootView
*>(widget
->GetRootView());
511 ui::EventDispatchDetails details
;
513 root
->AddChildView(v1
);
514 v1
->AddChildView(v2
);
515 v2
->AddChildView(v3
);
517 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
518 // reach |v2| because |v3| doesn't process any gesture events. However, since
519 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
527 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
528 details
= root
->OnEventFromSource(&g1
);
529 EXPECT_FALSE(details
.dispatcher_destroyed
);
530 EXPECT_FALSE(details
.target_destroyed
);
532 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
533 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
534 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
536 // Simulate an up so that RootView is no longer targetting |v3|.
537 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
538 details
= root
->OnEventFromSource(&g1_up
);
539 EXPECT_FALSE(details
.dispatcher_destroyed
);
540 EXPECT_FALSE(details
.target_destroyed
);
547 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
548 details
= root
->OnEventFromSource(&g2
);
549 EXPECT_FALSE(details
.dispatcher_destroyed
);
550 EXPECT_FALSE(details
.target_destroyed
);
552 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
553 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
554 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
556 // Send event |g1| again. Even though the coordinates target |v3| it should go
557 // to |v1| as that is the view the touch was initially down on.
558 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
559 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
560 details
= root
->OnEventFromSource(&g1
);
561 EXPECT_FALSE(details
.dispatcher_destroyed
);
562 EXPECT_FALSE(details
.target_destroyed
);
564 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
565 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
566 EXPECT_EQ("110,110", v1
->location_
.ToString());
571 TEST_F(ViewTest
, ScrollGestureEvent
) {
572 // Views hierarchy for non delivery of GestureEvent.
573 TestView
* v1
= new TestViewConsumeGesture();
574 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
576 TestView
* v2
= new TestViewIgnoreScrollGestures();
577 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
579 TestView
* v3
= new TestViewIgnoreGesture();
580 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
582 scoped_ptr
<Widget
> widget(new Widget());
583 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
584 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
585 params
.bounds
= gfx::Rect(50, 50, 650, 650);
586 widget
->Init(params
);
587 internal::RootView
* root
=
588 static_cast<internal::RootView
*>(widget
->GetRootView());
589 ui::EventDispatchDetails details
;
591 root
->AddChildView(v1
);
592 v1
->AddChildView(v2
);
593 v2
->AddChildView(v3
);
595 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
596 // reach |v2| because |v3| doesn't process any gesture events. However, since
597 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
605 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
606 details
= root
->OnEventFromSource(&g1
);
607 EXPECT_FALSE(details
.dispatcher_destroyed
);
608 EXPECT_FALSE(details
.target_destroyed
);
610 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
611 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
612 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
616 // Send scroll gestures on |v3|. The gesture should reach |v2|, however,
617 // since it does not process scroll-gesture events, these events should reach
619 GestureEventForTest
gscroll_begin(ui::ET_GESTURE_SCROLL_BEGIN
, 115, 115, 0);
620 details
= root
->OnEventFromSource(&gscroll_begin
);
621 EXPECT_FALSE(details
.dispatcher_destroyed
);
622 EXPECT_FALSE(details
.target_destroyed
);
624 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
625 EXPECT_EQ(ui::ET_GESTURE_SCROLL_BEGIN
, v1
->last_gesture_event_type_
);
628 // Send a second tap on |v1|. The event should reach |v2| since it is the
629 // default gesture handler, and not |v1| (even though it is the view under the
630 // point, and is the scroll event handler).
631 GestureEventForTest
second_tap(ui::ET_GESTURE_TAP
, 70, 70, 0);
632 details
= root
->OnEventFromSource(&second_tap
);
633 EXPECT_FALSE(details
.dispatcher_destroyed
);
634 EXPECT_FALSE(details
.target_destroyed
);
636 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
637 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
640 GestureEventForTest
gscroll_end(ui::ET_GESTURE_SCROLL_END
, 50, 50, 0);
641 details
= root
->OnEventFromSource(&gscroll_end
);
642 EXPECT_FALSE(details
.dispatcher_destroyed
);
643 EXPECT_FALSE(details
.target_destroyed
);
645 EXPECT_EQ(ui::ET_GESTURE_SCROLL_END
, v1
->last_gesture_event_type_
);
648 // Simulate an up so that RootView is no longer targetting |v3|.
649 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
650 details
= root
->OnEventFromSource(&g1_up
);
651 EXPECT_FALSE(details
.dispatcher_destroyed
);
652 EXPECT_FALSE(details
.target_destroyed
);
654 EXPECT_EQ(ui::ET_GESTURE_END
, v2
->last_gesture_event_type_
);
661 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
662 details
= root
->OnEventFromSource(&g2
);
663 EXPECT_FALSE(details
.dispatcher_destroyed
);
664 EXPECT_FALSE(details
.target_destroyed
);
666 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
667 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
668 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
670 // Send event |g1| again. Even though the coordinates target |v3| it should go
671 // to |v1| as that is the view the touch was initially down on.
672 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
673 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
674 details
= root
->OnEventFromSource(&g1
);
675 EXPECT_FALSE(details
.dispatcher_destroyed
);
676 EXPECT_FALSE(details
.target_destroyed
);
678 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
679 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
680 EXPECT_EQ("110,110", v1
->location_
.ToString());
685 ////////////////////////////////////////////////////////////////////////////////
687 ////////////////////////////////////////////////////////////////////////////////
689 void TestView::Paint(gfx::Canvas
* canvas
, const CullSet
& cull_set
) {
690 canvas
->sk_canvas()->getClipBounds(&last_clip_
);
693 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
694 scheduled_paint_rects_
.push_back(rect
);
695 View::SchedulePaintInRect(rect
);
698 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
699 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
700 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
701 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
702 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
705 TEST_F(ViewTest
, RemoveNotification
) {
706 ViewStorage
* vs
= ViewStorage::GetInstance();
707 Widget
* widget
= new Widget
;
708 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
709 View
* root_view
= widget
->GetRootView();
712 int s1
= vs
->CreateStorageID();
713 vs
->StoreView(s1
, v1
);
714 root_view
->AddChildView(v1
);
715 View
* v11
= new View
;
716 int s11
= vs
->CreateStorageID();
717 vs
->StoreView(s11
, v11
);
718 v1
->AddChildView(v11
);
719 View
* v111
= new View
;
720 int s111
= vs
->CreateStorageID();
721 vs
->StoreView(s111
, v111
);
722 v11
->AddChildView(v111
);
723 View
* v112
= new View
;
724 int s112
= vs
->CreateStorageID();
725 vs
->StoreView(s112
, v112
);
726 v11
->AddChildView(v112
);
727 View
* v113
= new View
;
728 int s113
= vs
->CreateStorageID();
729 vs
->StoreView(s113
, v113
);
730 v11
->AddChildView(v113
);
731 View
* v1131
= new View
;
732 int s1131
= vs
->CreateStorageID();
733 vs
->StoreView(s1131
, v1131
);
734 v113
->AddChildView(v1131
);
735 View
* v12
= new View
;
736 int s12
= vs
->CreateStorageID();
737 vs
->StoreView(s12
, v12
);
738 v1
->AddChildView(v12
);
741 int s2
= vs
->CreateStorageID();
742 vs
->StoreView(s2
, v2
);
743 root_view
->AddChildView(v2
);
744 View
* v21
= new View
;
745 int s21
= vs
->CreateStorageID();
746 vs
->StoreView(s21
, v21
);
747 v2
->AddChildView(v21
);
748 View
* v211
= new View
;
749 int s211
= vs
->CreateStorageID();
750 vs
->StoreView(s211
, v211
);
751 v21
->AddChildView(v211
);
753 size_t stored_views
= vs
->view_count();
755 // Try removing a leaf view.
756 v21
->RemoveChildView(v211
);
757 EXPECT_EQ(stored_views
- 1, vs
->view_count());
758 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
759 delete v211
; // We won't use this one anymore.
761 // Now try removing a view with a hierarchy of depth 1.
762 v11
->RemoveChildView(v113
);
763 EXPECT_EQ(stored_views
- 3, vs
->view_count());
764 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
765 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
766 delete v113
; // We won't use this one anymore.
768 // Now remove even more.
769 root_view
->RemoveChildView(v1
);
770 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
771 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
772 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
773 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
774 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
776 // Put v1 back for more tests.
777 root_view
->AddChildView(v1
);
778 vs
->StoreView(s1
, v1
);
780 // Synchronously closing the window deletes the view hierarchy, which should
781 // remove all its views from ViewStorage.
783 EXPECT_EQ(stored_views
- 10, vs
->view_count());
784 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
785 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
786 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
787 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
788 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
789 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
790 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
794 class HitTestView
: public View
{
796 explicit HitTestView(bool has_hittest_mask
)
797 : has_hittest_mask_(has_hittest_mask
) {
799 virtual ~HitTestView() {}
802 // Overridden from View:
803 virtual bool HasHitTestMask() const OVERRIDE
{
804 return has_hittest_mask_
;
806 virtual void GetHitTestMask(HitTestSource source
,
807 gfx::Path
* mask
) const OVERRIDE
{
808 DCHECK(has_hittest_mask_
);
811 SkScalar w
= SkIntToScalar(width());
812 SkScalar h
= SkIntToScalar(height());
814 // Create a triangular mask within the bounds of this View.
815 mask
->moveTo(w
/ 2, 0);
822 bool has_hittest_mask_
;
824 DISALLOW_COPY_AND_ASSIGN(HitTestView
);
827 gfx::Point
ConvertPointToView(View
* view
, const gfx::Point
& p
) {
829 View::ConvertPointToTarget(view
->GetWidget()->GetRootView(), view
, &tmp
);
833 gfx::Rect
ConvertRectToView(View
* view
, const gfx::Rect
& r
) {
835 tmp
.set_origin(ConvertPointToView(view
, r
.origin()));
839 void RotateCounterclockwise(gfx::Transform
* transform
) {
840 transform
->matrix().set3x3(0, -1, 0,
845 void RotateClockwise(gfx::Transform
* transform
) {
846 transform
->matrix().set3x3( 0, 1, 0,
853 TEST_F(ViewTest
, HitTestMasks
) {
854 Widget
* widget
= new Widget
;
855 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
856 widget
->Init(params
);
857 View
* root_view
= widget
->GetRootView();
858 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
860 gfx::Rect v1_bounds
= gfx::Rect(0, 0, 100, 100);
861 HitTestView
* v1
= new HitTestView(false);
862 v1
->SetBoundsRect(v1_bounds
);
863 root_view
->AddChildView(v1
);
865 gfx::Rect v2_bounds
= gfx::Rect(105, 0, 100, 100);
866 HitTestView
* v2
= new HitTestView(true);
867 v2
->SetBoundsRect(v2_bounds
);
868 root_view
->AddChildView(v2
);
870 gfx::Point v1_centerpoint
= v1_bounds
.CenterPoint();
871 gfx::Point v2_centerpoint
= v2_bounds
.CenterPoint();
872 gfx::Point v1_origin
= v1_bounds
.origin();
873 gfx::Point v2_origin
= v2_bounds
.origin();
875 gfx::Rect
r1(10, 10, 110, 15);
876 gfx::Rect
r2(106, 1, 98, 98);
877 gfx::Rect
r3(0, 0, 300, 300);
878 gfx::Rect
r4(115, 342, 200, 10);
881 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_centerpoint
)));
882 EXPECT_TRUE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_centerpoint
)));
884 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_origin
)));
885 EXPECT_FALSE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_origin
)));
888 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r1
)));
889 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r1
)));
891 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r2
)));
892 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r2
)));
894 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r3
)));
895 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r3
)));
897 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r4
)));
898 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r4
)));
900 // Test GetEventHandlerForPoint
901 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_centerpoint
));
902 EXPECT_EQ(v2
, root_view
->GetEventHandlerForPoint(v2_centerpoint
));
904 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_origin
));
905 EXPECT_EQ(root_view
, root_view
->GetEventHandlerForPoint(v2_origin
));
907 // Test GetTooltipHandlerForPoint
908 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_centerpoint
));
909 EXPECT_EQ(v2
, root_view
->GetTooltipHandlerForPoint(v2_centerpoint
));
911 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_origin
));
912 EXPECT_EQ(root_view
, root_view
->GetTooltipHandlerForPoint(v2_origin
));
914 EXPECT_FALSE(v1
->GetTooltipHandlerForPoint(v2_origin
));
919 // Tests the correctness of the rect-based targeting algorithm implemented in
920 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
921 // of rect-based targeting.
922 TEST_F(ViewTest
, GetEventHandlerForRect
) {
923 Widget
* widget
= new Widget
;
924 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
925 widget
->Init(params
);
926 View
* root_view
= widget
->GetRootView();
927 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
929 // Have this hierarchy of views (the coordinates here are all in
930 // the root view's coordinate space):
931 // v1 (0, 0, 100, 100)
932 // v2 (150, 0, 250, 100)
933 // v3 (0, 200, 150, 100)
934 // v31 (10, 210, 80, 80)
935 // v32 (110, 210, 30, 80)
936 // v4 (300, 200, 100, 100)
937 // v41 (310, 210, 80, 80)
938 // v411 (370, 275, 10, 5)
939 // v5 (450, 197, 30, 36)
940 // v51 (450, 200, 30, 30)
942 // The coordinates used for SetBounds are in parent coordinates.
944 TestView
* v1
= new TestView
;
945 v1
->SetBounds(0, 0, 100, 100);
946 root_view
->AddChildView(v1
);
948 TestView
* v2
= new TestView
;
949 v2
->SetBounds(150, 0, 250, 100);
950 root_view
->AddChildView(v2
);
952 TestView
* v3
= new TestView
;
953 v3
->SetBounds(0, 200, 150, 100);
954 root_view
->AddChildView(v3
);
956 TestView
* v4
= new TestView
;
957 v4
->SetBounds(300, 200, 100, 100);
958 root_view
->AddChildView(v4
);
960 TestView
* v31
= new TestView
;
961 v31
->SetBounds(10, 10, 80, 80);
962 v3
->AddChildView(v31
);
964 TestView
* v32
= new TestView
;
965 v32
->SetBounds(110, 10, 30, 80);
966 v3
->AddChildView(v32
);
968 TestView
* v41
= new TestView
;
969 v41
->SetBounds(10, 10, 80, 80);
970 v4
->AddChildView(v41
);
972 TestView
* v411
= new TestView
;
973 v411
->SetBounds(60, 65, 10, 5);
974 v41
->AddChildView(v411
);
976 TestView
* v5
= new TestView
;
977 v5
->SetBounds(450, 197, 30, 36);
978 root_view
->AddChildView(v5
);
980 TestView
* v51
= new TestView
;
981 v51
->SetBounds(0, 3, 30, 30);
982 v5
->AddChildView(v51
);
984 // |touch_rect| does not intersect any descendant view of |root_view|.
985 gfx::Rect
touch_rect(105, 105, 30, 45);
986 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
987 EXPECT_EQ(root_view
, result_view
);
990 // Covers |v1| by at least 60%.
991 touch_rect
.SetRect(15, 15, 100, 100);
992 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
993 EXPECT_EQ(v1
, result_view
);
996 // Intersects |v1| but does not cover it by at least 60%. The center
997 // of |touch_rect| is within |v1|.
998 touch_rect
.SetRect(50, 50, 5, 10);
999 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1000 EXPECT_EQ(v1
, result_view
);
1003 // Intersects |v1| but does not cover it by at least 60%. The center
1004 // of |touch_rect| is not within |v1|.
1005 touch_rect
.SetRect(95, 96, 21, 22);
1006 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1007 EXPECT_EQ(root_view
, result_view
);
1010 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
1011 touch_rect
.SetRect(95, 10, 300, 120);
1012 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1013 EXPECT_EQ(v2
, result_view
);
1016 // Covers both |v1| and |v2| by at least 60%, but the center point
1017 // of |touch_rect| is closer to the center point of |v2|.
1018 touch_rect
.SetRect(20, 20, 400, 100);
1019 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1020 EXPECT_EQ(v2
, result_view
);
1023 // Covers both |v1| and |v2| by at least 60%, but the center point
1024 // of |touch_rect| is closer to the center point of |v1|.
1025 touch_rect
.SetRect(-700, -15, 1050, 110);
1026 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1027 EXPECT_EQ(v1
, result_view
);
1030 // A mouse click within |v1| will target |v1|.
1031 touch_rect
.SetRect(15, 15, 1, 1);
1032 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1033 EXPECT_EQ(v1
, result_view
);
1036 // Intersects |v3| and |v31| by at least 60% and the center point
1037 // of |touch_rect| is closer to the center point of |v31|.
1038 touch_rect
.SetRect(0, 200, 110, 100);
1039 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1040 EXPECT_EQ(v31
, result_view
);
1043 // Intersects |v3| and |v31|, but neither by at least 60%. The
1044 // center point of |touch_rect| lies within |v31|.
1045 touch_rect
.SetRect(80, 280, 15, 15);
1046 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1047 EXPECT_EQ(v31
, result_view
);
1050 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1051 // center point of |touch_rect| is closest to the center point
1053 touch_rect
.SetRect(0, 200, 200, 100);
1054 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1055 EXPECT_EQ(v32
, result_view
);
1058 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1059 // |v31| and |v32| by at least 60%. The center point of
1060 // |touch_rect| is closest to the center point of |v32|.
1061 touch_rect
.SetRect(30, 225, 180, 115);
1062 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1063 EXPECT_EQ(v32
, result_view
);
1066 // A mouse click at the corner of |v3| will target |v3|.
1067 touch_rect
.SetRect(0, 200, 1, 1);
1068 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1069 EXPECT_EQ(v3
, result_view
);
1072 // A mouse click within |v32| will target |v32|.
1073 touch_rect
.SetRect(112, 211, 1, 1);
1074 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1075 EXPECT_EQ(v32
, result_view
);
1078 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1079 // The center point of |touch_rect| is equally close to
1080 // the center points of |v4| and |v41|.
1081 touch_rect
.SetRect(310, 210, 80, 80);
1082 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1083 EXPECT_EQ(v41
, result_view
);
1086 // Intersects all of |v4|, |v41|, and |v411| but only covers
1087 // |v411| by at least 60%.
1088 touch_rect
.SetRect(370, 275, 7, 5);
1089 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1090 EXPECT_EQ(v411
, result_view
);
1093 // Intersects |v4| and |v41| but covers neither by at least 60%.
1094 // The center point of |touch_rect| is equally close to the center
1095 // points of |v4| and |v41|.
1096 touch_rect
.SetRect(345, 245, 7, 7);
1097 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1098 EXPECT_EQ(v41
, result_view
);
1101 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1102 // them by at least 60%. The center point of |touch_rect| lies
1104 touch_rect
.SetRect(368, 272, 4, 6);
1105 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1106 EXPECT_EQ(v411
, result_view
);
1109 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1110 // them by at least 60%. The center point of |touch_rect| lies
1112 touch_rect
.SetRect(365, 270, 7, 7);
1113 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1114 EXPECT_EQ(v41
, result_view
);
1117 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1118 // them by at least 60%. The center point of |touch_rect| lies
1120 touch_rect
.SetRect(205, 275, 200, 2);
1121 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1122 EXPECT_EQ(v4
, result_view
);
1125 // Intersects all of |v4|, |v41|, and |v411| but only covers
1126 // |v41| by at least 60%.
1127 touch_rect
.SetRect(310, 210, 61, 66);
1128 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1129 EXPECT_EQ(v41
, result_view
);
1132 // A mouse click within |v411| will target |v411|.
1133 touch_rect
.SetRect(372, 275, 1, 1);
1134 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1135 EXPECT_EQ(v411
, result_view
);
1138 // A mouse click within |v41| will target |v41|.
1139 touch_rect
.SetRect(350, 215, 1, 1);
1140 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1141 EXPECT_EQ(v41
, result_view
);
1144 // Covers |v3|, |v4|, and all of their descendants by at
1145 // least 60%. The center point of |touch_rect| is closest
1146 // to the center point of |v32|.
1147 touch_rect
.SetRect(0, 200, 400, 100);
1148 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1149 EXPECT_EQ(v32
, result_view
);
1152 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1153 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1154 // The center point of |touch_rect| is closest to the center
1155 // point of |root_view|.
1156 touch_rect
.SetRect(110, 15, 375, 450);
1157 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1158 EXPECT_EQ(root_view
, result_view
);
1161 // Covers all views (except |v5| and |v51|) by at least 60%. The
1162 // center point of |touch_rect| is equally close to the center
1163 // points of |v2| and |v32|. One is not a descendant of the other,
1164 // so in this case the view selected is arbitrary (i.e.,
1165 // it depends only on the ordering of nodes in the views
1167 touch_rect
.SetRect(0, 0, 400, 300);
1168 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1169 EXPECT_EQ(v32
, result_view
);
1172 // Covers |v5| and |v51| by at least 60%, and the center point of
1173 // the touch is located within both views. Since both views share
1174 // the same center point, the child view should be selected.
1175 touch_rect
.SetRect(440, 190, 40, 40);
1176 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1177 EXPECT_EQ(v51
, result_view
);
1180 // Covers |v5| and |v51| by at least 60%, but the center point of
1181 // the touch is not located within either view. Since both views
1182 // share the same center point, the child view should be selected.
1183 touch_rect
.SetRect(455, 187, 60, 60);
1184 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1185 EXPECT_EQ(v51
, result_view
);
1188 // Covers neither |v5| nor |v51| by at least 60%, but the center
1189 // of the touch is located within |v51|.
1190 touch_rect
.SetRect(450, 197, 10, 10);
1191 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1192 EXPECT_EQ(v51
, result_view
);
1195 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1196 // The center point is located outside of both views.
1197 touch_rect
.SetRect(433, 180, 24, 24);
1198 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1199 EXPECT_EQ(root_view
, result_view
);
1202 // Only intersects |v5| but does not cover it by at least 60%. The
1203 // center point of the touch region is located within |v5|.
1204 touch_rect
.SetRect(449, 196, 3, 3);
1205 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1206 EXPECT_EQ(v5
, result_view
);
1209 // A mouse click within |v5| (but not |v51|) should target |v5|.
1210 touch_rect
.SetRect(462, 199, 1, 1);
1211 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1212 EXPECT_EQ(v5
, result_view
);
1215 // A mouse click |v5| and |v51| should target the child view.
1216 touch_rect
.SetRect(452, 226, 1, 1);
1217 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1218 EXPECT_EQ(v51
, result_view
);
1221 // A mouse click on the center of |v5| and |v51| should target
1223 touch_rect
.SetRect(465, 215, 1, 1);
1224 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1225 EXPECT_EQ(v51
, result_view
);
1231 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1232 // as expected when different views in the view hierarchy return false
1233 // when CanProcessEventsWithinSubtree() is called.
1234 TEST_F(ViewTest
, CanProcessEventsWithinSubtree
) {
1235 Widget
* widget
= new Widget
;
1236 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1237 widget
->Init(params
);
1238 View
* root_view
= widget
->GetRootView();
1239 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1241 // Have this hierarchy of views (the coords here are in the coordinate
1242 // space of the root view):
1243 // v (0, 0, 100, 100)
1244 // - v_child (0, 0, 20, 30)
1245 // - v_grandchild (5, 5, 5, 15)
1247 TestView
* v
= new TestView
;
1248 v
->SetBounds(0, 0, 100, 100);
1249 root_view
->AddChildView(v
);
1250 v
->set_notify_enter_exit_on_child(true);
1252 TestView
* v_child
= new TestView
;
1253 v_child
->SetBounds(0, 0, 20, 30);
1254 v
->AddChildView(v_child
);
1256 TestView
* v_grandchild
= new TestView
;
1257 v_grandchild
->SetBounds(5, 5, 5, 15);
1258 v_child
->AddChildView(v_grandchild
);
1262 v_grandchild
->Reset();
1264 // Define rects and points within the views in the hierarchy.
1265 gfx::Rect
rect_in_v_grandchild(7, 7, 3, 3);
1266 gfx::Point
point_in_v_grandchild(rect_in_v_grandchild
.origin());
1267 gfx::Rect
rect_in_v_child(12, 3, 5, 5);
1268 gfx::Point
point_in_v_child(rect_in_v_child
.origin());
1269 gfx::Rect
rect_in_v(50, 50, 25, 30);
1270 gfx::Point
point_in_v(rect_in_v
.origin());
1272 // When all three views return true when CanProcessEventsWithinSubtree()
1273 // is called, targeting should behave as expected.
1275 View
* result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1276 EXPECT_EQ(v_grandchild
, result_view
);
1278 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1279 EXPECT_EQ(v_grandchild
, result_view
);
1282 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1283 EXPECT_EQ(v_child
, result_view
);
1285 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1286 EXPECT_EQ(v_child
, result_view
);
1289 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1290 EXPECT_EQ(v
, result_view
);
1292 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1293 EXPECT_EQ(v
, result_view
);
1296 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1297 // is called, then |v_grandchild| cannot be returned as a target.
1299 v_grandchild
->set_can_process_events_within_subtree(false);
1301 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1302 EXPECT_EQ(v_child
, result_view
);
1304 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1305 EXPECT_EQ(v_child
, result_view
);
1308 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1309 EXPECT_EQ(v_child
, result_view
);
1311 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1312 EXPECT_EQ(v_child
, result_view
);
1315 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1316 EXPECT_EQ(v
, result_view
);
1318 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1319 EXPECT_EQ(v
, result_view
);
1322 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1323 // is called, then neither |v_child| nor |v_grandchild| can be returned
1324 // as a target (|v| should be returned as the target for each case).
1326 v_grandchild
->Reset();
1327 v_child
->set_can_process_events_within_subtree(false);
1329 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1330 EXPECT_EQ(v
, result_view
);
1332 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1333 EXPECT_EQ(v
, result_view
);
1336 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1337 EXPECT_EQ(v
, result_view
);
1339 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1340 EXPECT_EQ(v
, result_view
);
1343 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1344 EXPECT_EQ(v
, result_view
);
1346 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1347 EXPECT_EQ(v
, result_view
);
1350 // When |v| returns false when CanProcessEventsWithinSubtree()
1351 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1352 // as a target (|root_view| should be returned as the target for each case).
1355 v
->set_can_process_events_within_subtree(false);
1357 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1358 EXPECT_EQ(root_view
, result_view
);
1360 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1361 EXPECT_EQ(root_view
, result_view
);
1364 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1365 EXPECT_EQ(root_view
, result_view
);
1367 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1368 EXPECT_EQ(root_view
, result_view
);
1371 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1372 EXPECT_EQ(root_view
, result_view
);
1374 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1375 EXPECT_EQ(root_view
, result_view
);
1378 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1379 Widget
* widget
= new Widget
;
1380 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1381 widget
->Init(params
);
1382 View
* root_view
= widget
->GetRootView();
1383 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1385 // Have this hierarchy of views (the coords here are in root coord):
1386 // v1 (0, 0, 100, 100)
1387 // - v11 (0, 0, 20, 30)
1388 // - v111 (5, 5, 5, 15)
1389 // - v12 (50, 10, 30, 90)
1390 // - v121 (60, 20, 10, 10)
1391 // v2 (105, 0, 100, 100)
1392 // - v21 (120, 10, 50, 20)
1394 TestView
* v1
= new TestView
;
1395 v1
->SetBounds(0, 0, 100, 100);
1396 root_view
->AddChildView(v1
);
1397 v1
->set_notify_enter_exit_on_child(true);
1399 TestView
* v11
= new TestView
;
1400 v11
->SetBounds(0, 0, 20, 30);
1401 v1
->AddChildView(v11
);
1403 TestView
* v111
= new TestView
;
1404 v111
->SetBounds(5, 5, 5, 15);
1405 v11
->AddChildView(v111
);
1407 TestView
* v12
= new TestView
;
1408 v12
->SetBounds(50, 10, 30, 90);
1409 v1
->AddChildView(v12
);
1411 TestView
* v121
= new TestView
;
1412 v121
->SetBounds(10, 10, 10, 10);
1413 v12
->AddChildView(v121
);
1415 TestView
* v2
= new TestView
;
1416 v2
->SetBounds(105, 0, 100, 100);
1417 root_view
->AddChildView(v2
);
1419 TestView
* v21
= new TestView
;
1420 v21
->SetBounds(15, 10, 50, 20);
1421 v2
->AddChildView(v21
);
1431 // Move the mouse in v111.
1432 gfx::Point
p1(6, 6);
1433 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, 0, 0);
1434 root_view
->OnMouseMoved(move1
);
1435 EXPECT_TRUE(v111
->received_mouse_enter_
);
1436 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1437 EXPECT_TRUE(v1
->received_mouse_enter_
);
1442 // Now, move into v121.
1443 gfx::Point
p2(65, 21);
1444 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, 0, 0);
1445 root_view
->OnMouseMoved(move2
);
1446 EXPECT_TRUE(v111
->received_mouse_exit_
);
1447 EXPECT_TRUE(v121
->received_mouse_enter_
);
1448 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1453 // Now, move into v11.
1454 gfx::Point
p3(1, 1);
1455 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, 0, 0);
1456 root_view
->OnMouseMoved(move3
);
1457 EXPECT_TRUE(v121
->received_mouse_exit_
);
1458 EXPECT_TRUE(v11
->received_mouse_enter_
);
1459 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1465 gfx::Point
p4(121, 15);
1466 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, 0, 0);
1467 root_view
->OnMouseMoved(move4
);
1468 EXPECT_TRUE(v21
->received_mouse_enter_
);
1469 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1470 EXPECT_TRUE(v11
->received_mouse_exit_
);
1471 EXPECT_TRUE(v1
->received_mouse_exit_
);
1478 gfx::Point
p5(21, 0);
1479 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, 0, 0);
1480 root_view
->OnMouseMoved(move5
);
1481 EXPECT_TRUE(v21
->received_mouse_exit_
);
1482 EXPECT_TRUE(v1
->received_mouse_enter_
);
1487 // Now, move into v11.
1488 gfx::Point
p6(15, 15);
1489 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, 0, 0);
1490 root_view
->OnMouseMoved(mouse6
);
1491 EXPECT_TRUE(v11
->received_mouse_enter_
);
1492 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1497 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1498 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1499 // when the mouse leaves v11.
1500 gfx::Point
p7(21, 0);
1501 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, 0, 0);
1502 root_view
->OnMouseMoved(mouse7
);
1503 EXPECT_TRUE(v11
->received_mouse_exit_
);
1504 EXPECT_FALSE(v1
->received_mouse_enter_
);
1509 TEST_F(ViewTest
, Textfield
) {
1510 const base::string16 kText
= ASCIIToUTF16(
1511 "Reality is that which, when you stop believing it, doesn't go away.");
1512 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1513 const base::string16 kEmptyString
;
1515 Widget
* widget
= new Widget
;
1516 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1517 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1518 widget
->Init(params
);
1519 View
* root_view
= widget
->GetRootView();
1521 Textfield
* textfield
= new Textfield();
1522 root_view
->AddChildView(textfield
);
1524 // Test setting, appending text.
1525 textfield
->SetText(kText
);
1526 EXPECT_EQ(kText
, textfield
->text());
1527 textfield
->AppendText(kExtraText
);
1528 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1529 textfield
->SetText(base::string16());
1530 EXPECT_EQ(kEmptyString
, textfield
->text());
1532 // Test selection related methods.
1533 textfield
->SetText(kText
);
1534 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1535 textfield
->SelectAll(false);
1536 EXPECT_EQ(kText
, textfield
->text());
1537 textfield
->ClearSelection();
1538 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1543 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1544 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1545 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1546 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1547 const base::string16 kPasswordText
=
1548 ASCIIToUTF16("Password! ** Secret stuff **");
1550 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1552 Widget
* widget
= new Widget
;
1553 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1554 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1555 widget
->Init(params
);
1556 View
* root_view
= widget
->GetRootView();
1558 Textfield
* normal
= new Textfield();
1559 Textfield
* read_only
= new Textfield();
1560 read_only
->SetReadOnly(true);
1561 Textfield
* password
= new Textfield();
1562 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1564 root_view
->AddChildView(normal
);
1565 root_view
->AddChildView(read_only
);
1566 root_view
->AddChildView(password
);
1568 normal
->SetText(kNormalText
);
1569 read_only
->SetText(kReadOnlyText
);
1570 password
->SetText(kPasswordText
);
1576 normal
->SelectAll(false);
1577 normal
->ExecuteCommand(IDS_APP_CUT
);
1578 base::string16 result
;
1579 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1580 EXPECT_EQ(kNormalText
, result
);
1581 normal
->SetText(kNormalText
); // Let's revert to the original content.
1583 read_only
->SelectAll(false);
1584 read_only
->ExecuteCommand(IDS_APP_CUT
);
1586 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1587 // Cut should have failed, so the clipboard content should not have changed.
1588 EXPECT_EQ(kNormalText
, result
);
1590 password
->SelectAll(false);
1591 password
->ExecuteCommand(IDS_APP_CUT
);
1593 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1594 // Cut should have failed, so the clipboard content should not have changed.
1595 EXPECT_EQ(kNormalText
, result
);
1601 // Start with |read_only| to observe a change in clipboard text.
1602 read_only
->SelectAll(false);
1603 read_only
->ExecuteCommand(IDS_APP_COPY
);
1605 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1606 EXPECT_EQ(kReadOnlyText
, result
);
1608 normal
->SelectAll(false);
1609 normal
->ExecuteCommand(IDS_APP_COPY
);
1611 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1612 EXPECT_EQ(kNormalText
, result
);
1614 password
->SelectAll(false);
1615 password
->ExecuteCommand(IDS_APP_COPY
);
1617 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1618 // Text cannot be copied from an obscured field; the clipboard won't change.
1619 EXPECT_EQ(kNormalText
, result
);
1625 // Attempting to paste kNormalText in a read-only text-field should fail.
1626 read_only
->SelectAll(false);
1627 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1628 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1630 password
->SelectAll(false);
1631 password
->ExecuteCommand(IDS_APP_PASTE
);
1632 EXPECT_EQ(kNormalText
, password
->text());
1634 // Copy from |read_only| to observe a change in the normal textfield text.
1635 read_only
->SelectAll(false);
1636 read_only
->ExecuteCommand(IDS_APP_COPY
);
1637 normal
->SelectAll(false);
1638 normal
->ExecuteCommand(IDS_APP_PASTE
);
1639 EXPECT_EQ(kReadOnlyText
, normal
->text());
1643 ////////////////////////////////////////////////////////////////////////////////
1645 ////////////////////////////////////////////////////////////////////////////////
1646 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1647 accelerator_count_map_
[accelerator
]++;
1651 // TODO: these tests were initially commented out when getting aura to
1652 // run. Figure out if still valuable and either nuke or fix.
1654 TEST_F(ViewTest
, ActivateAccelerator
) {
1655 // Register a keyboard accelerator before the view is added to a window.
1656 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1657 TestView
* view
= new TestView();
1659 view
->AddAccelerator(return_accelerator
);
1660 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1662 // Create a window and add the view as its child.
1663 scoped_ptr
<Widget
> widget(new Widget
);
1664 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1665 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1666 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1667 widget
->Init(params
);
1668 View
* root
= widget
->GetRootView();
1669 root
->AddChildView(view
);
1672 // Get the focus manager.
1673 FocusManager
* focus_manager
= widget
->GetFocusManager();
1674 ASSERT_TRUE(focus_manager
);
1676 // Hit the return key and see if it takes effect.
1677 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1678 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1680 // Hit the escape key. Nothing should happen.
1681 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1682 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1683 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1684 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1686 // Now register the escape key and hit it again.
1687 view
->AddAccelerator(escape_accelerator
);
1688 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1689 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1690 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1692 // Remove the return key accelerator.
1693 view
->RemoveAccelerator(return_accelerator
);
1694 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1695 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1696 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1698 // Add it again. Hit the return key and the escape key.
1699 view
->AddAccelerator(return_accelerator
);
1700 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1701 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1702 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1703 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1704 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1705 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1707 // Remove all the accelerators.
1708 view
->ResetAccelerators();
1709 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1710 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1711 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1712 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1713 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1714 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1719 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1720 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1721 TestView
* view
= new TestView();
1723 view
->AddAccelerator(return_accelerator
);
1724 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1726 scoped_ptr
<Widget
> widget(new Widget
);
1727 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1728 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1729 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1730 widget
->Init(params
);
1731 View
* root
= widget
->GetRootView();
1732 root
->AddChildView(view
);
1735 FocusManager
* focus_manager
= widget
->GetFocusManager();
1736 ASSERT_TRUE(focus_manager
);
1738 view
->SetVisible(false);
1739 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1741 view
->SetVisible(true);
1742 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1747 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1748 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1749 TestView
* view
= new TestView();
1751 view
->AddAccelerator(return_accelerator
);
1752 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1754 scoped_ptr
<Widget
> widget(new Widget
);
1755 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1756 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1757 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1758 widget
->Init(params
);
1759 View
* root
= widget
->GetRootView();
1760 root
->AddChildView(view
);
1762 FocusManager
* focus_manager
= widget
->GetFocusManager();
1763 ASSERT_TRUE(focus_manager
);
1765 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1766 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1769 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1770 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1773 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1774 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1779 ////////////////////////////////////////////////////////////////////////////////
1780 // Mouse-wheel message rerouting
1781 ////////////////////////////////////////////////////////////////////////////////
1782 class ScrollableTestView
: public View
{
1784 ScrollableTestView() { }
1786 virtual gfx::Size
GetPreferredSize() {
1787 return gfx::Size(100, 10000);
1790 virtual void Layout() {
1791 SizeToPreferredSize();
1795 class TestViewWithControls
: public View
{
1797 TestViewWithControls() {
1798 text_field_
= new Textfield();
1799 AddChildView(text_field_
);
1802 Textfield
* text_field_
;
1805 class SimpleWidgetDelegate
: public WidgetDelegate
{
1807 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1809 virtual void DeleteDelegate() { delete this; }
1811 virtual View
* GetContentsView() { return contents_
; }
1813 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1814 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1820 // Tests that the mouse-wheel messages are correctly rerouted to the window
1822 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1824 // Note that this fails for a variety of reasons:
1825 // - focused view is apparently reset across window activations and never
1826 // properly restored
1827 // - this test depends on you not having any other window visible open under the
1828 // area that it opens the test windows. --beng
1829 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1830 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1831 Widget
* window1
= Widget::CreateWindowWithBounds(
1832 new SimpleWidgetDelegate(view_with_controls
),
1833 gfx::Rect(0, 0, 100, 100));
1835 ScrollView
* scroll_view
= new ScrollView();
1836 scroll_view
->SetContents(new ScrollableTestView());
1837 Widget
* window2
= Widget::CreateWindowWithBounds(
1838 new SimpleWidgetDelegate(scroll_view
),
1839 gfx::Rect(200, 200, 100, 100));
1841 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1843 // Make the window1 active, as this is what it would be in real-world.
1844 window1
->Activate();
1846 // Let's send a mouse-wheel message to the different controls and check that
1847 // it is rerouted to the window under the mouse (effectively scrolling the
1850 // First to the Window's HWND.
1851 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1852 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1853 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1855 window1
->CloseNow();
1856 window2
->CloseNow();
1860 ////////////////////////////////////////////////////////////////////////////////
1861 // Native view hierachy
1862 ////////////////////////////////////////////////////////////////////////////////
1863 class ToplevelWidgetObserverView
: public View
{
1865 ToplevelWidgetObserverView() : toplevel_(NULL
) {
1867 virtual ~ToplevelWidgetObserverView() {
1871 virtual void ViewHierarchyChanged(
1872 const ViewHierarchyChangedDetails
& details
) OVERRIDE
{
1873 if (details
.is_add
) {
1874 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1879 virtual void NativeViewHierarchyChanged() OVERRIDE
{
1880 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1883 Widget
* toplevel() { return toplevel_
; }
1888 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
1891 // Test that a view can track the current top level widget by overriding
1892 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1893 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
1894 scoped_ptr
<Widget
> toplevel1(new Widget
);
1895 Widget::InitParams toplevel1_params
=
1896 CreateParams(Widget::InitParams::TYPE_POPUP
);
1897 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1898 toplevel1
->Init(toplevel1_params
);
1900 scoped_ptr
<Widget
> toplevel2(new Widget
);
1901 Widget::InitParams toplevel2_params
=
1902 CreateParams(Widget::InitParams::TYPE_POPUP
);
1903 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1904 toplevel2
->Init(toplevel2_params
);
1906 Widget
* child
= new Widget
;
1907 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
1908 child_params
.parent
= toplevel1
->GetNativeView();
1909 child
->Init(child_params
);
1911 ToplevelWidgetObserverView
* observer_view
=
1912 new ToplevelWidgetObserverView();
1913 EXPECT_EQ(NULL
, observer_view
->toplevel());
1915 child
->SetContentsView(observer_view
);
1916 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
1918 Widget::ReparentNativeView(child
->GetNativeView(),
1919 toplevel2
->GetNativeView());
1920 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
1922 observer_view
->parent()->RemoveChildView(observer_view
);
1923 EXPECT_EQ(NULL
, observer_view
->toplevel());
1925 // Make |observer_view| |child|'s contents view again so that it gets deleted
1927 child
->SetContentsView(observer_view
);
1930 ////////////////////////////////////////////////////////////////////////////////
1932 ////////////////////////////////////////////////////////////////////////////////
1934 class TransformPaintView
: public TestView
{
1936 TransformPaintView() {}
1937 virtual ~TransformPaintView() {}
1939 void ClearScheduledPaintRect() {
1940 scheduled_paint_rect_
= gfx::Rect();
1943 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1945 // Overridden from View:
1946 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
{
1947 gfx::Rect xrect
= ConvertRectToParent(rect
);
1948 scheduled_paint_rect_
.Union(xrect
);
1952 gfx::Rect scheduled_paint_rect_
;
1954 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
1957 TEST_F(ViewTest
, TransformPaint
) {
1958 TransformPaintView
* v1
= new TransformPaintView();
1959 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1961 TestView
* v2
= new TestView();
1962 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1964 Widget
* widget
= new Widget
;
1965 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1966 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1967 widget
->Init(params
);
1969 View
* root
= widget
->GetRootView();
1971 root
->AddChildView(v1
);
1972 v1
->AddChildView(v2
);
1974 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1975 v1
->ClearScheduledPaintRect();
1976 v2
->SchedulePaint();
1978 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
1980 // Rotate |v1| counter-clockwise.
1981 gfx::Transform transform
;
1982 RotateCounterclockwise(&transform
);
1983 transform
.matrix().set(1, 3, 500.0);
1984 v1
->SetTransform(transform
);
1986 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1988 v1
->ClearScheduledPaintRect();
1989 v2
->SchedulePaint();
1991 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
1996 TEST_F(ViewTest
, TransformEvent
) {
1997 TestView
* v1
= new TestView();
1998 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2000 TestView
* v2
= new TestView();
2001 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2003 Widget
* widget
= new Widget
;
2004 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2005 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2006 widget
->Init(params
);
2007 View
* root
= widget
->GetRootView();
2009 root
->AddChildView(v1
);
2010 v1
->AddChildView(v2
);
2012 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2014 // Rotate |v1| counter-clockwise.
2015 gfx::Transform
transform(v1
->GetTransform());
2016 RotateCounterclockwise(&transform
);
2017 transform
.matrix().set(1, 3, 500.0);
2018 v1
->SetTransform(transform
);
2020 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2024 gfx::Point
p1(110, 210);
2025 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
2026 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2027 root
->OnMousePressed(pressed
);
2028 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2029 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2030 EXPECT_EQ(190, v2
->location_
.x());
2031 EXPECT_EQ(10, v2
->location_
.y());
2033 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
2035 root
->OnMouseReleased(released
);
2037 // Now rotate |v2| inside |v1| clockwise.
2038 transform
= v2
->GetTransform();
2039 RotateClockwise(&transform
);
2040 transform
.matrix().set(0, 3, 100.f
);
2041 v2
->SetTransform(transform
);
2043 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
2044 // (300, 400) in |root|.
2049 gfx::Point
point2(110, 320);
2050 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
,
2051 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2052 root
->OnMousePressed(p2
);
2053 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2054 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2055 EXPECT_EQ(10, v2
->location_
.x());
2056 EXPECT_EQ(20, v2
->location_
.y());
2058 root
->OnMouseReleased(released
);
2060 v1
->SetTransform(gfx::Transform());
2061 v2
->SetTransform(gfx::Transform());
2063 TestView
* v3
= new TestView();
2064 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
2065 v2
->AddChildView(v3
);
2067 // Rotate |v3| clockwise with respect to |v2|.
2068 transform
= v1
->GetTransform();
2069 RotateClockwise(&transform
);
2070 transform
.matrix().set(0, 3, 30.f
);
2071 v3
->SetTransform(transform
);
2073 // Scale |v2| with respect to |v1| along both axis.
2074 transform
= v2
->GetTransform();
2075 transform
.matrix().set(0, 0, 0.8f
);
2076 transform
.matrix().set(1, 1, 0.5f
);
2077 v2
->SetTransform(transform
);
2079 // |v3| occupies (108, 105) to (132, 115) in |root|.
2085 gfx::Point
point(112, 110);
2086 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
,
2087 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2088 root
->OnMousePressed(p3
);
2090 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2091 EXPECT_EQ(10, v3
->location_
.x());
2092 EXPECT_EQ(25, v3
->location_
.y());
2094 root
->OnMouseReleased(released
);
2096 v1
->SetTransform(gfx::Transform());
2097 v2
->SetTransform(gfx::Transform());
2098 v3
->SetTransform(gfx::Transform());
2104 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
2105 transform
= v3
->GetTransform();
2106 RotateClockwise(&transform
);
2107 transform
.matrix().set(0, 3, 30.f
);
2108 // Rotation sets some scaling transformation. Using SetScale would overwrite
2109 // that and pollute the rotation. So combine the scaling with the existing
2111 gfx::Transform scale
;
2112 scale
.Scale(0.8f
, 0.5f
);
2113 transform
.ConcatTransform(scale
);
2114 v3
->SetTransform(transform
);
2116 // Translate |v2| with respect to |v1|.
2117 transform
= v2
->GetTransform();
2118 transform
.matrix().set(0, 3, 10.f
);
2119 transform
.matrix().set(1, 3, 10.f
);
2120 v2
->SetTransform(transform
);
2122 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2124 gfx::Point
point3(124, 125);
2125 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
,
2126 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2127 root
->OnMousePressed(p4
);
2129 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2130 EXPECT_EQ(10, v3
->location_
.x());
2131 EXPECT_EQ(25, v3
->location_
.y());
2133 root
->OnMouseReleased(released
);
2138 TEST_F(ViewTest
, TransformVisibleBound
) {
2139 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2141 scoped_ptr
<Widget
> widget(new Widget
);
2142 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2143 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2144 params
.bounds
= viewport_bounds
;
2145 widget
->Init(params
);
2146 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2148 View
* viewport
= new View
;
2149 widget
->SetContentsView(viewport
);
2150 View
* contents
= new View
;
2151 viewport
->AddChildView(contents
);
2152 viewport
->SetBoundsRect(viewport_bounds
);
2153 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2155 View
* child
= new View
;
2156 contents
->AddChildView(child
);
2157 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2158 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
2160 // Rotate |child| counter-clockwise
2161 gfx::Transform transform
;
2162 RotateCounterclockwise(&transform
);
2163 transform
.matrix().set(1, 3, 50.f
);
2164 child
->SetTransform(transform
);
2165 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
2170 ////////////////////////////////////////////////////////////////////////////////
2171 // OnVisibleBoundsChanged()
2173 class VisibleBoundsView
: public View
{
2175 VisibleBoundsView() : received_notification_(false) {}
2176 virtual ~VisibleBoundsView() {}
2178 bool received_notification() const { return received_notification_
; }
2179 void set_received_notification(bool received
) {
2180 received_notification_
= received
;
2184 // Overridden from View:
2185 virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE
{
2188 virtual void OnVisibleBoundsChanged() OVERRIDE
{
2189 received_notification_
= true;
2192 bool received_notification_
;
2194 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2197 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2198 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2200 scoped_ptr
<Widget
> widget(new Widget
);
2201 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2202 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2203 params
.bounds
= viewport_bounds
;
2204 widget
->Init(params
);
2205 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2207 View
* viewport
= new View
;
2208 widget
->SetContentsView(viewport
);
2209 View
* contents
= new View
;
2210 viewport
->AddChildView(contents
);
2211 viewport
->SetBoundsRect(viewport_bounds
);
2212 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2214 // Create a view that cares about visible bounds notifications, and position
2215 // it just outside the visible bounds of the viewport.
2216 VisibleBoundsView
* child
= new VisibleBoundsView
;
2217 contents
->AddChildView(child
);
2218 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2220 // The child bound should be fully clipped.
2221 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2223 // Now scroll the contents, but not enough to make the child visible.
2224 contents
->SetY(contents
->y() - 1);
2226 // We should have received the notification since the visible bounds may have
2227 // changed (even though they didn't).
2228 EXPECT_TRUE(child
->received_notification());
2229 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2230 child
->set_received_notification(false);
2232 // Now scroll the contents, this time by enough to make the child visible by
2234 contents
->SetY(contents
->y() - 10);
2235 EXPECT_TRUE(child
->received_notification());
2236 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2237 child
->set_received_notification(false);
2242 TEST_F(ViewTest
, SetBoundsPaint
) {
2244 TestView
* child_view
= new TestView
;
2246 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2247 top_view
.scheduled_paint_rects_
.clear();
2248 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2249 top_view
.AddChildView(child_view
);
2251 top_view
.scheduled_paint_rects_
.clear();
2252 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2253 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2255 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2256 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2257 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2258 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2261 // Assertions around painting and focus gain/lost.
2262 TEST_F(ViewTest
, FocusBlurPaints
) {
2263 TestView parent_view
;
2264 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
2266 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2268 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2269 parent_view
.AddChildView(child_view1
);
2271 parent_view
.scheduled_paint_rects_
.clear();
2272 child_view1
->scheduled_paint_rects_
.clear();
2274 // Focus change shouldn't trigger paints.
2275 child_view1
->DoFocus();
2277 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2278 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2280 child_view1
->DoBlur();
2281 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2282 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2285 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2286 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
2289 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2290 view
.InvalidateLayout();
2291 view
.scheduled_paint_rects_
.clear();
2292 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2293 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
2296 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2297 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
2298 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2300 // We have to put the View hierarchy into a Widget or no paints will be
2302 scoped_ptr
<Widget
> widget(new Widget
);
2303 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2304 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2305 params
.bounds
= viewport_bounds
;
2306 widget
->Init(params
);
2307 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2309 TestView
* parent_view
= new TestView
;
2310 widget
->SetContentsView(parent_view
);
2311 parent_view
->SetBoundsRect(viewport_bounds
);
2312 parent_view
->scheduled_paint_rects_
.clear();
2314 View
* child_view
= new View
;
2315 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2316 parent_view
->AddChildView(child_view
);
2317 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2318 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2320 parent_view
->scheduled_paint_rects_
.clear();
2321 parent_view
->RemoveChildView(child_view
);
2322 scoped_ptr
<View
> child_deleter(child_view
);
2323 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2324 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2329 // Tests conversion methods with a transform.
2330 TEST_F(ViewTest
, ConversionsWithTransform
) {
2333 // View hierarchy used to test scale transforms.
2334 TestView
* child
= new TestView
;
2335 TestView
* child_child
= new TestView
;
2337 // View used to test a rotation transform.
2338 TestView
* child_2
= new TestView
;
2340 top_view
.AddChildView(child
);
2341 child
->AddChildView(child_child
);
2343 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2345 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2346 gfx::Transform transform
;
2347 transform
.Scale(3.0, 4.0);
2348 child
->SetTransform(transform
);
2350 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2351 transform
.MakeIdentity();
2352 transform
.Scale(5.0, 7.0);
2353 child_child
->SetTransform(transform
);
2355 top_view
.AddChildView(child_2
);
2356 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2357 transform
.MakeIdentity();
2358 RotateClockwise(&transform
);
2359 child_2
->SetTransform(transform
);
2361 // Sanity check to make sure basic transforms act as expected.
2363 gfx::Transform transform
;
2364 transform
.Translate(110.0, -110.0);
2365 transform
.Scale(100.0, 55.0);
2366 transform
.Translate(1.0, 1.0);
2368 // convert to a 3x3 matrix.
2369 const SkMatrix
& matrix
= transform
.matrix();
2371 EXPECT_EQ(210, matrix
.getTranslateX());
2372 EXPECT_EQ(-55, matrix
.getTranslateY());
2373 EXPECT_EQ(100, matrix
.getScaleX());
2374 EXPECT_EQ(55, matrix
.getScaleY());
2375 EXPECT_EQ(0, matrix
.getSkewX());
2376 EXPECT_EQ(0, matrix
.getSkewY());
2380 gfx::Transform transform
;
2381 transform
.Translate(1.0, 1.0);
2383 t2
.Scale(100.0, 55.0);
2385 t3
.Translate(110.0, -110.0);
2386 transform
.ConcatTransform(t2
);
2387 transform
.ConcatTransform(t3
);
2389 // convert to a 3x3 matrix
2390 const SkMatrix
& matrix
= transform
.matrix();
2392 EXPECT_EQ(210, matrix
.getTranslateX());
2393 EXPECT_EQ(-55, matrix
.getTranslateY());
2394 EXPECT_EQ(100, matrix
.getScaleX());
2395 EXPECT_EQ(55, matrix
.getScaleY());
2396 EXPECT_EQ(0, matrix
.getSkewX());
2397 EXPECT_EQ(0, matrix
.getSkewY());
2400 // Conversions from child->top and top->child.
2402 gfx::Point
point(5, 5);
2403 View::ConvertPointToTarget(child
, &top_view
, &point
);
2404 EXPECT_EQ(22, point
.x());
2405 EXPECT_EQ(39, point
.y());
2407 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2408 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2409 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2410 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2411 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2412 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2414 point
.SetPoint(22, 39);
2415 View::ConvertPointToTarget(&top_view
, child
, &point
);
2416 EXPECT_EQ(5, point
.x());
2417 EXPECT_EQ(5, point
.y());
2419 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2420 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2421 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2422 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2423 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2424 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2427 // Conversions from child_child->top and top->child_child.
2429 gfx::Point
point(5, 5);
2430 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2431 EXPECT_EQ(133, point
.x());
2432 EXPECT_EQ(211, point
.y());
2434 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2435 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2436 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2437 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2438 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2439 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2441 point
.SetPoint(133, 211);
2442 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2443 EXPECT_EQ(5, point
.x());
2444 EXPECT_EQ(5, point
.y());
2446 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2447 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2448 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2449 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2450 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2451 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2454 // Conversions from child_child->child and child->child_child
2456 gfx::Point
point(5, 5);
2457 View::ConvertPointToTarget(child_child
, child
, &point
);
2458 EXPECT_EQ(42, point
.x());
2459 EXPECT_EQ(48, point
.y());
2461 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2462 View::ConvertRectToTarget(child_child
, child
, &rect
);
2463 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2464 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2465 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2466 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2468 point
.SetPoint(42, 48);
2469 View::ConvertPointToTarget(child
, child_child
, &point
);
2470 EXPECT_EQ(5, point
.x());
2471 EXPECT_EQ(5, point
.y());
2473 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2474 View::ConvertRectToTarget(child
, child_child
, &rect
);
2475 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2476 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2477 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2478 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2481 // Conversions from top_view to child with a value that should be negative.
2482 // This ensures we don't round up with negative numbers.
2484 gfx::Point
point(6, 18);
2485 View::ConvertPointToTarget(&top_view
, child
, &point
);
2486 EXPECT_EQ(-1, point
.x());
2487 EXPECT_EQ(-1, point
.y());
2489 float error
= 0.01f
;
2490 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2491 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2492 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2493 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2494 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2495 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2498 // Rect conversions from top_view->child_2 and child_2->top_view.
2500 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2501 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2502 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2503 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2504 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2505 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2507 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2508 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2509 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2510 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2511 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2512 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2516 // Tests conversion methods to and from screen coordinates.
2517 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2518 scoped_ptr
<Widget
> widget(new Widget
);
2519 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2520 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2521 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2522 widget
->Init(params
);
2524 View
* child
= new View
;
2525 widget
->GetRootView()->AddChildView(child
);
2526 child
->SetBounds(10, 10, 100, 200);
2529 child
->SetTransform(t
);
2531 gfx::Point
point_in_screen(100, 90);
2532 gfx::Point
point_in_child(80,60);
2534 gfx::Point point
= point_in_screen
;
2535 View::ConvertPointFromScreen(child
, &point
);
2536 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2538 View::ConvertPointToScreen(child
, &point
);
2539 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2542 // Tests conversion methods for rectangles.
2543 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2544 scoped_ptr
<Widget
> widget(new Widget
);
2545 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2546 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2547 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2548 widget
->Init(params
);
2549 View
* root
= widget
->GetRootView();
2551 TestView
* v1
= new TestView
;
2552 TestView
* v2
= new TestView
;
2553 root
->AddChildView(v1
);
2554 v1
->AddChildView(v2
);
2556 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2557 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2559 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2560 gfx::Rect
rect(5, 5, 15, 40);
2561 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2562 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2566 RotateCounterclockwise(&t2
);
2567 t2
.matrix().set(1, 3, 100.f
);
2568 v2
->SetTransform(t2
);
2570 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2571 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2572 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2577 v1
->SetTransform(t1
);
2579 // The rectangle should remain the same for |v1|.
2580 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2582 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2583 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2584 v2
->ConvertRectToWidget(rect
).ToString());
2589 class ObserverView
: public View
{
2592 virtual ~ObserverView();
2594 void ResetTestState();
2596 bool has_add_details() const { return has_add_details_
; }
2597 bool has_remove_details() const { return has_remove_details_
; }
2599 const ViewHierarchyChangedDetails
& add_details() const {
2600 return add_details_
;
2603 const ViewHierarchyChangedDetails
& remove_details() const {
2604 return remove_details_
;
2609 virtual void ViewHierarchyChanged(
2610 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
2612 bool has_add_details_
;
2613 bool has_remove_details_
;
2614 ViewHierarchyChangedDetails add_details_
;
2615 ViewHierarchyChangedDetails remove_details_
;
2617 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2620 ObserverView::ObserverView()
2621 : has_add_details_(false),
2622 has_remove_details_(false) {
2625 ObserverView::~ObserverView() {}
2627 void ObserverView::ResetTestState() {
2628 has_add_details_
= false;
2629 has_remove_details_
= false;
2630 add_details_
= ViewHierarchyChangedDetails();
2631 remove_details_
= ViewHierarchyChangedDetails();
2634 void ObserverView::ViewHierarchyChanged(
2635 const ViewHierarchyChangedDetails
& details
) {
2636 if (details
.is_add
) {
2637 has_add_details_
= true;
2638 add_details_
= details
;
2640 has_remove_details_
= true;
2641 remove_details_
= details
;
2645 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2646 // a child view is added or removed to all the views in the hierarchy (up and
2648 // The tree looks like this:
2652 // +-- v4 (starts here, then get reparented to v1)
2653 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2656 ObserverView
* v3
= new ObserverView();
2658 // Add |v3| to |v2|.
2659 scoped_ptr
<ObserverView
> v2(new ObserverView());
2660 v2
->AddChildView(v3
);
2662 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2664 EXPECT_TRUE(v2
->has_add_details());
2665 EXPECT_FALSE(v2
->has_remove_details());
2666 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2667 EXPECT_EQ(v3
, v2
->add_details().child
);
2668 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2670 EXPECT_TRUE(v3
->has_add_details());
2671 EXPECT_FALSE(v3
->has_remove_details());
2672 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2673 EXPECT_EQ(v3
, v3
->add_details().child
);
2674 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2676 // Reset everything to the initial state.
2677 v2
->ResetTestState();
2678 v3
->ResetTestState();
2681 v1
.AddChildView(v2
.get());
2683 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2684 // Make sure all the views (v1, v2, v3) received _that_ information.
2685 EXPECT_TRUE(v1
.has_add_details());
2686 EXPECT_FALSE(v1
.has_remove_details());
2687 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2688 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2689 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2691 EXPECT_TRUE(v2
->has_add_details());
2692 EXPECT_FALSE(v2
->has_remove_details());
2693 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2694 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2695 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2697 EXPECT_TRUE(v3
->has_add_details());
2698 EXPECT_FALSE(v3
->has_remove_details());
2699 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2700 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2701 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2703 // Reset everything to the initial state.
2704 v1
.ResetTestState();
2705 v2
->ResetTestState();
2706 v3
->ResetTestState();
2708 // Remove |v2| from |v1|.
2709 v1
.RemoveChildView(v2
.get());
2711 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2712 // Make sure all the views (v1, v2, v3) received _that_ information.
2713 EXPECT_FALSE(v1
.has_add_details());
2714 EXPECT_TRUE(v1
.has_remove_details());
2715 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2716 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2717 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2719 EXPECT_FALSE(v2
->has_add_details());
2720 EXPECT_TRUE(v2
->has_remove_details());
2721 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2722 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2723 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2725 EXPECT_FALSE(v3
->has_add_details());
2726 EXPECT_TRUE(v3
->has_remove_details());
2727 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2728 EXPECT_EQ(v3
, v3
->remove_details().child
);
2729 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2731 // Verifies notifications when reparenting a view.
2732 ObserverView
* v4
= new ObserverView();
2733 // Add |v4| to |v2|.
2734 v2
->AddChildView(v4
);
2736 // Reset everything to the initial state.
2737 v1
.ResetTestState();
2738 v2
->ResetTestState();
2739 v3
->ResetTestState();
2740 v4
->ResetTestState();
2742 // Reparent |v4| to |v1|.
2743 v1
.AddChildView(v4
);
2745 // Verifies that all views receive the correct information for all the child,
2746 // parent and move views.
2748 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2749 EXPECT_TRUE(v1
.has_add_details());
2750 EXPECT_FALSE(v1
.has_remove_details());
2751 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2752 EXPECT_EQ(v4
, v1
.add_details().child
);
2753 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2755 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2757 EXPECT_FALSE(v2
->has_add_details());
2758 EXPECT_TRUE(v2
->has_remove_details());
2759 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2760 EXPECT_EQ(v4
, v2
->remove_details().child
);
2761 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2763 // |v3| is not impacted by this operation, and hence receives no notification.
2764 EXPECT_FALSE(v3
->has_add_details());
2765 EXPECT_FALSE(v3
->has_remove_details());
2767 // |v4| is the reparented child, so it receives notifications for the remove
2768 // and then the add. |v2| is its old parent, |v1| is its new parent.
2769 EXPECT_TRUE(v4
->has_remove_details());
2770 EXPECT_TRUE(v4
->has_add_details());
2771 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2772 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2773 EXPECT_EQ(v4
, v4
->add_details().child
);
2774 EXPECT_EQ(v4
, v4
->remove_details().child
);
2775 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2776 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2779 // Verifies if the child views added under the root are all deleted when calling
2780 // RemoveAllChildViews.
2781 // The tree looks like this:
2790 TEST_F(ViewTest
, RemoveAllChildViews
) {
2793 View
* child1
= new View
;
2794 root
.AddChildView(child1
);
2796 for (int i
= 0; i
< 2; ++i
)
2797 root
.AddChildView(new View
);
2799 View
* foo
= new View
;
2800 child1
->AddChildView(foo
);
2802 // Add some nodes to |foo|.
2803 for (int i
= 0; i
< 3; ++i
)
2804 foo
->AddChildView(new View
);
2806 EXPECT_EQ(3, root
.child_count());
2807 EXPECT_EQ(1, child1
->child_count());
2808 EXPECT_EQ(3, foo
->child_count());
2810 // Now remove all child views from root.
2811 root
.RemoveAllChildViews(true);
2813 EXPECT_EQ(0, root
.child_count());
2814 EXPECT_FALSE(root
.has_children());
2817 TEST_F(ViewTest
, Contains
) {
2819 View
* v2
= new View
;
2820 View
* v3
= new View
;
2822 v1
.AddChildView(v2
);
2823 v2
->AddChildView(v3
);
2825 EXPECT_FALSE(v1
.Contains(NULL
));
2826 EXPECT_TRUE(v1
.Contains(&v1
));
2827 EXPECT_TRUE(v1
.Contains(v2
));
2828 EXPECT_TRUE(v1
.Contains(v3
));
2830 EXPECT_FALSE(v2
->Contains(NULL
));
2831 EXPECT_TRUE(v2
->Contains(v2
));
2832 EXPECT_FALSE(v2
->Contains(&v1
));
2833 EXPECT_TRUE(v2
->Contains(v3
));
2835 EXPECT_FALSE(v3
->Contains(NULL
));
2836 EXPECT_TRUE(v3
->Contains(v3
));
2837 EXPECT_FALSE(v3
->Contains(&v1
));
2838 EXPECT_FALSE(v3
->Contains(v2
));
2841 // Verifies if GetIndexOf() returns the correct index for the specified child
2843 // The tree looks like this:
2848 TEST_F(ViewTest
, GetIndexOf
) {
2851 View
* child1
= new View
;
2852 root
.AddChildView(child1
);
2854 View
* child2
= new View
;
2855 root
.AddChildView(child2
);
2857 View
* foo1
= new View
;
2858 child1
->AddChildView(foo1
);
2860 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2861 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2862 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2863 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2864 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2866 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2867 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2868 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2869 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2870 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2872 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2873 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2874 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2875 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2876 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2879 // Verifies that the child views can be reordered correctly.
2880 TEST_F(ViewTest
, ReorderChildren
) {
2883 View
* child
= new View();
2884 root
.AddChildView(child
);
2886 View
* foo1
= new View();
2887 child
->AddChildView(foo1
);
2888 View
* foo2
= new View();
2889 child
->AddChildView(foo2
);
2890 View
* foo3
= new View();
2891 child
->AddChildView(foo3
);
2892 foo1
->SetFocusable(true);
2893 foo2
->SetFocusable(true);
2894 foo3
->SetFocusable(true);
2896 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2897 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2898 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2899 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2900 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2901 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2903 // Move |foo2| at the end.
2904 child
->ReorderChildView(foo2
, -1);
2905 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2906 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2907 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2908 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2909 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2910 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2912 // Move |foo1| at the end.
2913 child
->ReorderChildView(foo1
, -1);
2914 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2915 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2916 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2917 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2918 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2919 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2920 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2922 // Move |foo2| to the front.
2923 child
->ReorderChildView(foo2
, 0);
2924 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2925 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2926 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2927 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2928 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2929 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2930 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2933 // Verifies that GetViewByID returns the correctly child view from the specified
2935 // The tree looks like this:
2940 TEST_F(ViewTest
, GetViewByID
) {
2942 const int kV1ID
= 1;
2946 const int kV2ID
= 2;
2950 const int kV3ID
= 3;
2954 const int kV4ID
= 4;
2957 const int kV5ID
= 5;
2959 v1
.AddChildView(&v2
);
2960 v2
.AddChildView(&v3
);
2961 v2
.AddChildView(&v4
);
2963 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2964 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2965 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2967 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2968 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2970 const int kGroup
= 1;
2971 v3
.SetGroup(kGroup
);
2972 v4
.SetGroup(kGroup
);
2975 v1
.GetViewsInGroup(kGroup
, &views
);
2976 EXPECT_EQ(2U, views
.size());
2978 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2979 EXPECT_NE(views
.end(), i
);
2981 i
= std::find(views
.begin(), views
.end(), &v4
);
2982 EXPECT_NE(views
.end(), i
);
2985 TEST_F(ViewTest
, AddExistingChild
) {
2988 v1
.AddChildView(&v2
);
2989 v1
.AddChildView(&v3
);
2990 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2991 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2993 // Check that there's no change in order when adding at same index.
2994 v1
.AddChildViewAt(&v2
, 0);
2995 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2996 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2997 v1
.AddChildViewAt(&v3
, 1);
2998 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2999 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3001 // Add it at a different index and check for change in order.
3002 v1
.AddChildViewAt(&v2
, 1);
3003 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
3004 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
3005 v1
.AddChildViewAt(&v2
, 0);
3006 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3007 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3009 // Check that calling |AddChildView()| does not change the order.
3010 v1
.AddChildView(&v2
);
3011 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3012 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3013 v1
.AddChildView(&v3
);
3014 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3015 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3018 ////////////////////////////////////////////////////////////////////////////////
3020 ////////////////////////////////////////////////////////////////////////////////
3024 // Test implementation of LayerAnimator.
3025 class TestLayerAnimator
: public ui::LayerAnimator
{
3027 TestLayerAnimator();
3029 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
3032 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
3035 virtual ~TestLayerAnimator() { }
3038 gfx::Rect last_bounds_
;
3040 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
3043 TestLayerAnimator::TestLayerAnimator()
3044 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
3047 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
3048 last_bounds_
= bounds
;
3053 class ViewLayerTest
: public ViewsTestBase
{
3055 ViewLayerTest() : widget_(NULL
) {}
3057 virtual ~ViewLayerTest() {
3060 // Returns the Layer used by the RootView.
3061 ui::Layer
* GetRootLayer() {
3062 return widget()->GetLayer();
3065 virtual void SetUp() OVERRIDE
{
3067 widget_
= new Widget
;
3068 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3069 params
.bounds
= gfx::Rect(50, 50, 200, 200);
3070 widget_
->Init(params
);
3072 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
3075 virtual void TearDown() OVERRIDE
{
3076 widget_
->CloseNow();
3077 ViewsTestBase::TearDown();
3080 Widget
* widget() { return widget_
; }
3087 TEST_F(ViewLayerTest
, LayerToggling
) {
3088 // Because we lazily create textures the calls to DrawTree are necessary to
3089 // ensure we trigger creation of textures.
3090 ui::Layer
* root_layer
= widget()->GetLayer();
3091 View
* content_view
= new View
;
3092 widget()->SetContentsView(content_view
);
3094 // Create v1, give it a bounds and verify everything is set up correctly.
3095 View
* v1
= new View
;
3096 v1
->SetPaintToLayer(true);
3097 EXPECT_TRUE(v1
->layer() != NULL
);
3098 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3099 content_view
->AddChildView(v1
);
3100 ASSERT_TRUE(v1
->layer() != NULL
);
3101 EXPECT_EQ(root_layer
, v1
->layer()->parent());
3102 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
3104 // Create v2 as a child of v1 and do basic assertion testing.
3105 View
* v2
= new View
;
3106 v1
->AddChildView(v2
);
3107 EXPECT_TRUE(v2
->layer() == NULL
);
3108 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
3109 v2
->SetPaintToLayer(true);
3110 ASSERT_TRUE(v2
->layer() != NULL
);
3111 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3112 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3114 // Turn off v1s layer. v2 should still have a layer but its parent should have
3116 v1
->SetPaintToLayer(false);
3117 EXPECT_TRUE(v1
->layer() == NULL
);
3118 EXPECT_TRUE(v2
->layer() != NULL
);
3119 EXPECT_EQ(root_layer
, v2
->layer()->parent());
3120 ASSERT_EQ(1u, root_layer
->children().size());
3121 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
3122 // The bounds of the layer should have changed to be relative to the root view
3124 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
3126 // Make v1 have a layer again and verify v2s layer is wired up correctly.
3127 gfx::Transform transform
;
3128 transform
.Scale(2.0, 2.0);
3129 v1
->SetTransform(transform
);
3130 EXPECT_TRUE(v1
->layer() != NULL
);
3131 EXPECT_TRUE(v2
->layer() != NULL
);
3132 EXPECT_EQ(root_layer
, v1
->layer()->parent());
3133 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3134 ASSERT_EQ(1u, root_layer
->children().size());
3135 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
3136 ASSERT_EQ(1u, v1
->layer()->children().size());
3137 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
3138 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3141 // Verifies turning on a layer wires up children correctly.
3142 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
3143 View
* content_view
= new View
;
3144 widget()->SetContentsView(content_view
);
3146 // Create v1, give it a bounds and verify everything is set up correctly.
3147 View
* v1
= new View
;
3148 content_view
->AddChildView(v1
);
3149 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3151 View
* v2
= new View
;
3152 v1
->AddChildView(v2
);
3154 View
* v3
= new View
;
3155 v3
->SetPaintToLayer(true);
3156 v2
->AddChildView(v3
);
3157 ASSERT_TRUE(v3
->layer() != NULL
);
3159 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
3161 v1
->SetPaintToLayer(true);
3162 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
3165 TEST_F(ViewLayerTest
, LayerAnimator
) {
3166 View
* content_view
= new View
;
3167 widget()->SetContentsView(content_view
);
3169 View
* v1
= new View
;
3170 content_view
->AddChildView(v1
);
3171 v1
->SetPaintToLayer(true);
3172 EXPECT_TRUE(v1
->layer() != NULL
);
3174 TestLayerAnimator
* animator
= new TestLayerAnimator();
3175 v1
->layer()->SetAnimator(animator
);
3177 gfx::Rect
bounds(1, 2, 3, 4);
3178 v1
->SetBoundsRect(bounds
);
3179 EXPECT_EQ(bounds
, animator
->last_bounds());
3180 // TestLayerAnimator doesn't update the layer.
3181 EXPECT_NE(bounds
, v1
->layer()->bounds());
3184 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3185 // doesn't have a layer change.
3186 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
3187 View
* content_view
= new View
;
3188 widget()->SetContentsView(content_view
);
3190 View
* v1
= new View
;
3191 content_view
->AddChildView(v1
);
3192 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3194 View
* v2
= new View
;
3195 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3196 v1
->AddChildView(v2
);
3197 v2
->SetPaintToLayer(true);
3198 ASSERT_TRUE(v2
->layer() != NULL
);
3199 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
3201 v1
->SetPosition(gfx::Point(25, 36));
3202 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
3204 v2
->SetPosition(gfx::Point(11, 12));
3205 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
3207 // Bounds of the layer should change even if the view is not invisible.
3208 v1
->SetVisible(false);
3209 v1
->SetPosition(gfx::Point(20, 30));
3210 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
3212 v2
->SetVisible(false);
3213 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3214 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
3217 // Make sure layers are positioned correctly in RTL.
3218 TEST_F(ViewLayerTest
, BoundInRTL
) {
3219 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3220 base::i18n::SetICUDefaultLocale("he");
3222 View
* view
= new View
;
3223 widget()->SetContentsView(view
);
3225 int content_width
= view
->width();
3227 // |v1| is initially not attached to anything. So its layer will have the same
3228 // bounds as the view.
3229 View
* v1
= new View
;
3230 v1
->SetPaintToLayer(true);
3231 v1
->SetBounds(10, 10, 20, 10);
3232 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3233 v1
->layer()->bounds());
3235 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3237 view
->AddChildView(v1
);
3238 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3239 v1
->layer()->bounds());
3240 gfx::Rect l1bounds
= v1
->layer()->bounds();
3242 // Now attach a View to the widget first, then create a layer for it. Make
3243 // sure the bounds are correct.
3244 View
* v2
= new View
;
3245 v2
->SetBounds(50, 10, 30, 10);
3246 EXPECT_FALSE(v2
->layer());
3247 view
->AddChildView(v2
);
3248 v2
->SetPaintToLayer(true);
3249 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
3250 v2
->layer()->bounds());
3251 gfx::Rect l2bounds
= v2
->layer()->bounds();
3253 view
->SetPaintToLayer(true);
3254 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3255 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3257 // Move one of the views. Make sure the layer is positioned correctly
3259 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
3260 l1bounds
.set_x(l1bounds
.x() + 5);
3261 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3263 view
->SetPaintToLayer(false);
3264 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3265 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3267 // Move a view again.
3268 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
3269 l2bounds
.set_x(l2bounds
.x() - 5);
3270 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3273 base::i18n::SetICUDefaultLocale(locale
);
3276 // Makes sure a transform persists after toggling the visibility.
3277 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3278 View
* view
= new View
;
3279 gfx::Transform transform
;
3280 transform
.Scale(2.0, 2.0);
3281 view
->SetTransform(transform
);
3282 widget()->SetContentsView(view
);
3283 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3285 view
->SetVisible(false);
3286 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3288 view
->SetVisible(true);
3289 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3292 // Verifies a transform persists after removing/adding a view with a transform.
3293 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3294 View
* view
= new View
;
3295 gfx::Transform transform
;
3296 transform
.Scale(2.0, 2.0);
3297 view
->SetTransform(transform
);
3298 widget()->SetContentsView(view
);
3299 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3300 ASSERT_TRUE(view
->layer() != NULL
);
3301 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3303 View
* parent
= view
->parent();
3304 parent
->RemoveChildView(view
);
3305 parent
->AddChildView(view
);
3307 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3308 ASSERT_TRUE(view
->layer() != NULL
);
3309 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3312 // Makes sure that layer visibility is correct after toggling View visibility.
3313 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3314 View
* content_view
= new View
;
3315 widget()->SetContentsView(content_view
);
3317 // The view isn't attached to a widget or a parent view yet. But it should
3318 // still have a layer, but the layer should not be attached to the root
3320 View
* v1
= new View
;
3321 v1
->SetPaintToLayer(true);
3322 EXPECT_TRUE(v1
->layer());
3323 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3326 // Once the view is attached to a widget, its layer should be attached to the
3327 // root layer and visible.
3328 content_view
->AddChildView(v1
);
3329 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3331 EXPECT_TRUE(v1
->layer()->IsDrawn());
3333 v1
->SetVisible(false);
3334 EXPECT_FALSE(v1
->layer()->IsDrawn());
3336 v1
->SetVisible(true);
3337 EXPECT_TRUE(v1
->layer()->IsDrawn());
3340 EXPECT_FALSE(v1
->layer()->IsDrawn());
3343 EXPECT_TRUE(v1
->layer()->IsDrawn());
3346 // Tests that the layers in the subtree are orphaned after a View is removed
3348 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3349 View
* content_view
= new View
;
3350 widget()->SetContentsView(content_view
);
3352 View
* v1
= new View
;
3353 content_view
->AddChildView(v1
);
3355 View
* v2
= new View
;
3356 v1
->AddChildView(v2
);
3357 v2
->SetPaintToLayer(true);
3358 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3360 EXPECT_TRUE(v2
->layer()->IsDrawn());
3362 content_view
->RemoveChildView(v1
);
3364 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3368 content_view
->AddChildView(v2
);
3371 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3373 EXPECT_TRUE(v2
->layer()->IsDrawn());
3376 class PaintTrackingView
: public View
{
3378 PaintTrackingView() : painted_(false) {
3381 bool painted() const { return painted_
; }
3382 void set_painted(bool value
) { painted_
= value
; }
3384 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
3391 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3394 // Makes sure child views with layers aren't painted when paint starts at an
3396 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3397 PaintTrackingView
* content_view
= new PaintTrackingView
;
3398 widget()->SetContentsView(content_view
);
3399 content_view
->SetPaintToLayer(true);
3400 GetRootLayer()->GetCompositor()->ScheduleDraw();
3401 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3402 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3403 content_view
->set_painted(false);
3404 // content_view no longer has a dirty rect. Paint from the root and make sure
3405 // PaintTrackingView isn't painted.
3406 GetRootLayer()->GetCompositor()->ScheduleDraw();
3407 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3408 EXPECT_FALSE(content_view
->painted());
3410 // Make content_view have a dirty rect, paint the layers and make sure
3411 // PaintTrackingView is painted.
3412 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3413 GetRootLayer()->GetCompositor()->ScheduleDraw();
3414 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3415 EXPECT_TRUE(content_view
->painted());
3418 // Tests that the visibility of child layers are updated correctly when a View's
3419 // visibility changes.
3420 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3421 View
* v1
= new View
;
3422 v1
->SetPaintToLayer(true);
3423 widget()->SetContentsView(v1
);
3425 View
* v2
= new View
;
3426 v1
->AddChildView(v2
);
3428 View
* v3
= new View
;
3429 v2
->AddChildView(v3
);
3430 v3
->SetVisible(false);
3432 View
* v4
= new View
;
3433 v4
->SetPaintToLayer(true);
3434 v3
->AddChildView(v4
);
3436 EXPECT_TRUE(v1
->layer()->IsDrawn());
3437 EXPECT_FALSE(v4
->layer()->IsDrawn());
3439 v2
->SetVisible(false);
3440 EXPECT_TRUE(v1
->layer()->IsDrawn());
3441 EXPECT_FALSE(v4
->layer()->IsDrawn());
3443 v2
->SetVisible(true);
3444 EXPECT_TRUE(v1
->layer()->IsDrawn());
3445 EXPECT_FALSE(v4
->layer()->IsDrawn());
3447 v2
->SetVisible(false);
3448 EXPECT_TRUE(v1
->layer()->IsDrawn());
3449 EXPECT_FALSE(v4
->layer()->IsDrawn());
3450 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3452 v3
->SetVisible(true);
3453 EXPECT_TRUE(v1
->layer()->IsDrawn());
3454 EXPECT_FALSE(v4
->layer()->IsDrawn());
3455 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3457 // Reparent |v3| to |v1|.
3458 v1
->AddChildView(v3
);
3459 EXPECT_TRUE(v1
->layer()->IsDrawn());
3460 EXPECT_TRUE(v4
->layer()->IsDrawn());
3461 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3464 // This test creates a random View tree, and then randomly reorders child views,
3465 // reparents views etc. Unrelated changes can appear to break this test. So
3466 // marking this as FLAKY.
3467 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3468 View
* content
= new View
;
3469 content
->SetPaintToLayer(true);
3470 widget()->SetContentsView(content
);
3473 ConstructTree(content
, 5);
3474 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3476 ScrambleTree(content
);
3477 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3479 ScrambleTree(content
);
3480 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3482 ScrambleTree(content
);
3483 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3486 // Verifies when views are reordered the layer is also reordered. The widget is
3487 // providing the parent layer.
3488 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3489 View
* content
= new View
;
3490 widget()->SetContentsView(content
);
3491 View
* c1
= new View
;
3492 c1
->SetPaintToLayer(true);
3493 content
->AddChildView(c1
);
3494 View
* c2
= new View
;
3495 c2
->SetPaintToLayer(true);
3496 content
->AddChildView(c2
);
3498 ui::Layer
* parent_layer
= c1
->layer()->parent();
3499 ASSERT_TRUE(parent_layer
);
3500 ASSERT_EQ(2u, parent_layer
->children().size());
3501 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3502 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3504 // Move c1 to the front. The layers should have moved too.
3505 content
->ReorderChildView(c1
, -1);
3506 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3507 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3510 // Verifies that the layer of a view can be acquired properly.
3511 TEST_F(ViewLayerTest
, AcquireLayer
) {
3512 View
* content
= new View
;
3513 widget()->SetContentsView(content
);
3514 scoped_ptr
<View
> c1(new View
);
3515 c1
->SetPaintToLayer(true);
3516 EXPECT_TRUE(c1
->layer());
3517 content
->AddChildView(c1
.get());
3519 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3520 EXPECT_EQ(layer
.get(), c1
->layer());
3522 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3523 EXPECT_NE(c1
->layer(), layer2
.get());
3525 // Destroy view before destroying layer.
3529 // Verify the z-order of the layers as a result of calling RecreateLayer().
3530 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3531 scoped_ptr
<View
> v(new View());
3532 v
->SetPaintToLayer(true);
3534 View
* v1
= new View();
3535 v1
->SetPaintToLayer(true);
3536 v
->AddChildView(v1
);
3537 View
* v2
= new View();
3538 v2
->SetPaintToLayer(true);
3539 v
->AddChildView(v2
);
3541 // Test the initial z-order.
3542 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3543 ASSERT_EQ(2u, child_layers_pre
.size());
3544 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3545 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3547 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3549 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3550 // for |v1| and |v2|.
3551 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3552 ASSERT_EQ(3u, child_layers_post
.size());
3553 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3554 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3555 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3558 // Verify the z-order of the layers as a result of calling RecreateLayer when
3559 // the widget is the parent with the layer.
3560 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3561 View
* v
= new View();
3562 widget()->SetContentsView(v
);
3564 View
* v1
= new View();
3565 v1
->SetPaintToLayer(true);
3566 v
->AddChildView(v1
);
3567 View
* v2
= new View();
3568 v2
->SetPaintToLayer(true);
3569 v
->AddChildView(v2
);
3571 ui::Layer
* root_layer
= GetRootLayer();
3573 // Test the initial z-order.
3574 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3575 ASSERT_EQ(2u, child_layers_pre
.size());
3576 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3577 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3579 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3581 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3582 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3583 ASSERT_EQ(3u, child_layers_post
.size());
3584 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3585 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3586 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3589 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3591 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3593 v
.SetPaintToLayer(true);
3595 child
.SetPaintToLayer(true);
3596 v
.AddChildView(&child
);
3597 ASSERT_TRUE(v
.layer() != NULL
);
3598 ASSERT_EQ(1u, v
.layer()->children().size());
3599 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3601 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3602 v
.layer()->Add(&layer
);
3603 v
.layer()->StackAtBottom(&layer
);
3605 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3607 // All children should be moved from old layer to new layer.
3608 ASSERT_TRUE(old_layer
.get() != NULL
);
3609 EXPECT_TRUE(old_layer
->children().empty());
3611 // And new layer should have the two children.
3612 ASSERT_TRUE(v
.layer() != NULL
);
3613 ASSERT_EQ(2u, v
.layer()->children().size());
3614 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3615 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3618 class BoundsTreeTestView
: public View
{
3620 BoundsTreeTestView() {}
3622 virtual void PaintChildren(gfx::Canvas
* canvas
,
3623 const CullSet
& cull_set
) OVERRIDE
{
3624 // Save out a copy of the cull_set before calling the base implementation.
3625 last_cull_set_
.clear();
3626 if (cull_set
.cull_set_
) {
3627 for (base::hash_set
<intptr_t>::iterator it
= cull_set
.cull_set_
->begin();
3628 it
!= cull_set
.cull_set_
->end();
3630 last_cull_set_
.insert(reinterpret_cast<View
*>(*it
));
3633 View::PaintChildren(canvas
, cull_set
);
3636 std::set
<View
*> last_cull_set_
;
3639 TEST_F(ViewLayerTest
, BoundsTreePaintUpdatesCullSet
) {
3640 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3641 widget()->SetContentsView(test_view
);
3643 View
* v1
= new View();
3644 v1
->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3645 test_view
->AddChildView(v1
);
3647 View
* v2
= new View();
3648 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3649 v1
->AddChildView(v2
);
3651 // Schedule a full-view paint to get everyone's rectangles updated.
3652 test_view
->SchedulePaintInRect(test_view
->bounds());
3653 GetRootLayer()->GetCompositor()->ScheduleDraw();
3654 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3656 // Now we have test_view - v1 - v2. Damage to only test_view should only
3657 // return root_view and test_view.
3658 test_view
->SchedulePaintInRect(gfx::Rect(0, 0, 1, 1));
3659 GetRootLayer()->GetCompositor()->ScheduleDraw();
3660 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3661 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3662 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3663 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3665 // Damage to v1 only should only return root_view, test_view, and v1.
3666 test_view
->SchedulePaintInRect(gfx::Rect(11, 16, 1, 1));
3667 GetRootLayer()->GetCompositor()->ScheduleDraw();
3668 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3669 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3670 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3671 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3672 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3674 // A Damage rect inside v2 should get all 3 views back in the |last_cull_set_|
3675 // on call to TestView::Paint(), along with the widget root view.
3676 test_view
->SchedulePaintInRect(gfx::Rect(31, 49, 1, 1));
3677 GetRootLayer()->GetCompositor()->ScheduleDraw();
3678 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3679 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3680 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3681 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3682 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3683 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3686 TEST_F(ViewLayerTest
, BoundsTreeWithRTL
) {
3687 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3688 base::i18n::SetICUDefaultLocale("ar");
3690 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3691 widget()->SetContentsView(test_view
);
3693 // Add child views, which should be in RTL coordinate space of parent view.
3694 View
* v1
= new View
;
3695 v1
->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3696 test_view
->AddChildView(v1
);
3698 View
* v2
= new View
;
3699 v2
->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3700 v1
->AddChildView(v2
);
3702 // Schedule a full-view paint to get everyone's rectangles updated.
3703 test_view
->SchedulePaintInRect(test_view
->bounds());
3704 GetRootLayer()->GetCompositor()->ScheduleDraw();
3705 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3707 // Damage to the right side of the parent view should touch both child views.
3708 gfx::Rect
rtl_damage(test_view
->bounds().width() - 16, 18, 1, 1);
3709 test_view
->SchedulePaintInRect(rtl_damage
);
3710 GetRootLayer()->GetCompositor()->ScheduleDraw();
3711 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3712 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3713 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3714 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3715 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3716 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3718 // Damage to the left side of the parent view should only touch the
3720 gfx::Rect
ltr_damage(16, 18, 1, 1);
3721 test_view
->SchedulePaintInRect(ltr_damage
);
3722 GetRootLayer()->GetCompositor()->ScheduleDraw();
3723 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3724 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3725 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3726 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3729 base::i18n::SetICUDefaultLocale(locale
);
3732 TEST_F(ViewLayerTest
, BoundsTreeSetBoundsChangesCullSet
) {
3733 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3734 widget()->SetContentsView(test_view
);
3736 View
* v1
= new View
;
3737 v1
->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3738 test_view
->AddChildView(v1
);
3740 View
* v2
= new View
;
3741 v2
->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3742 v1
->AddChildView(v2
);
3744 // Schedule a full-view paint to get everyone's rectangles updated.
3745 test_view
->SchedulePaintInRect(test_view
->bounds());
3746 GetRootLayer()->GetCompositor()->ScheduleDraw();
3747 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3749 // Move v1 to a new origin out of the way of our next query.
3750 v1
->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3751 // The move will force a repaint.
3752 GetRootLayer()->GetCompositor()->ScheduleDraw();
3753 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3755 // Schedule a paint with damage rect where v1 used to be.
3756 test_view
->SchedulePaintInRect(gfx::Rect(5, 6, 10, 11));
3757 GetRootLayer()->GetCompositor()->ScheduleDraw();
3758 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3760 // Should only have picked up root_view and test_view.
3761 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3762 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3763 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3766 TEST_F(ViewLayerTest
, BoundsTreeLayerChangeMakesNewTree
) {
3767 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3768 widget()->SetContentsView(test_view
);
3770 View
* v1
= new View
;
3771 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3772 test_view
->AddChildView(v1
);
3774 View
* v2
= new View
;
3775 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3776 v1
->AddChildView(v2
);
3778 // Schedule a full-view paint to get everyone's rectangles updated.
3779 test_view
->SchedulePaintInRect(test_view
->bounds());
3780 GetRootLayer()->GetCompositor()->ScheduleDraw();
3781 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3783 // Set v1 to paint to its own layer, it should remove itself from the
3784 // test_view heiarchy and no longer intersect with damage rects in that cull
3786 v1
->SetPaintToLayer(true);
3788 // Schedule another full-view paint.
3789 test_view
->SchedulePaintInRect(test_view
->bounds());
3790 GetRootLayer()->GetCompositor()->ScheduleDraw();
3791 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3792 // v1 and v2 should no longer be present in the test_view cull_set.
3793 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3794 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3795 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3797 // Now set v1 back to not painting to a layer.
3798 v1
->SetPaintToLayer(false);
3799 // Schedule another full-view paint.
3800 test_view
->SchedulePaintInRect(test_view
->bounds());
3801 GetRootLayer()->GetCompositor()->ScheduleDraw();
3802 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3803 // We should be back to the full cull set including v1 and v2.
3804 EXPECT_EQ(4U, test_view
->last_cull_set_
.size());
3805 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3806 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3807 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3808 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3811 TEST_F(ViewLayerTest
, BoundsTreeRemoveChildRemovesBounds
) {
3812 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3813 widget()->SetContentsView(test_view
);
3815 View
* v1
= new View
;
3816 v1
->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3817 test_view
->AddChildView(v1
);
3819 View
* v2
= new View
;
3820 v2
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3821 v1
->AddChildView(v2
);
3823 // Schedule a full-view paint to get everyone's rectangles updated.
3824 test_view
->SchedulePaintInRect(test_view
->bounds());
3825 GetRootLayer()->GetCompositor()->ScheduleDraw();
3826 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3828 // Now remove v1 from the root view.
3829 test_view
->RemoveChildView(v1
);
3831 // Schedule another full-view paint.
3832 test_view
->SchedulePaintInRect(test_view
->bounds());
3833 GetRootLayer()->GetCompositor()->ScheduleDraw();
3834 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3835 // v1 and v2 should no longer be present in the test_view cull_set.
3836 EXPECT_EQ(2U, test_view
->last_cull_set_
.size());
3837 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v1
));
3838 EXPECT_EQ(0U, test_view
->last_cull_set_
.count(v2
));
3840 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3841 // deleted with that hierarchy.
3845 TEST_F(ViewLayerTest
, BoundsTreeMoveViewMovesBounds
) {
3846 BoundsTreeTestView
* test_view
= new BoundsTreeTestView
;
3847 widget()->SetContentsView(test_view
);
3849 // Build hierarchy v1 - v2 - v3.
3850 View
* v1
= new View
;
3851 v1
->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3852 test_view
->AddChildView(v1
);
3854 View
* v2
= new View
;
3855 v2
->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3856 v1
->AddChildView(v2
);
3858 View
* v3
= new View
;
3859 v3
->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3860 v2
->AddChildView(v3
);
3862 // Schedule a full-view paint and ensure all views are present in the cull.
3863 test_view
->SchedulePaintInRect(test_view
->bounds());
3864 GetRootLayer()->GetCompositor()->ScheduleDraw();
3865 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3866 EXPECT_EQ(5U, test_view
->last_cull_set_
.size());
3867 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3868 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3869 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3870 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v2
));
3871 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v3
));
3873 // Build an unrelated view hierarchy and move v2 in to it.
3874 scoped_ptr
<Widget
> test_widget(new Widget
);
3875 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3876 params
.bounds
= gfx::Rect(10, 10, 500, 500);
3877 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3878 test_widget
->Init(params
);
3879 test_widget
->Show();
3880 BoundsTreeTestView
* widget_view
= new BoundsTreeTestView
;
3881 test_widget
->SetContentsView(widget_view
);
3882 widget_view
->AddChildView(v2
);
3884 // Now schedule full-view paints in both widgets.
3885 test_view
->SchedulePaintInRect(test_view
->bounds());
3886 widget_view
->SchedulePaintInRect(widget_view
->bounds());
3887 GetRootLayer()->GetCompositor()->ScheduleDraw();
3888 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3890 // Only v1 should be present in the first cull set.
3891 EXPECT_EQ(3U, test_view
->last_cull_set_
.size());
3892 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(widget()->GetRootView()));
3893 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(test_view
));
3894 EXPECT_EQ(1U, test_view
->last_cull_set_
.count(v1
));
3896 // We should find v2 and v3 in the widget_view cull_set.
3897 EXPECT_EQ(4U, widget_view
->last_cull_set_
.size());
3898 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(test_widget
->GetRootView()));
3899 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(widget_view
));
3900 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v2
));
3901 EXPECT_EQ(1U, widget_view
->last_cull_set_
.count(v3
));
3904 TEST_F(ViewTest
, FocusableAssertions
) {
3905 // View subclasses may change insets based on whether they are focusable,
3906 // which effects the preferred size. To avoid preferred size changing around
3907 // these Views need to key off the last value set to SetFocusable(), not
3908 // whether the View is focusable right now. For this reason it's important
3909 // that focusable() return the last value passed to SetFocusable and not
3910 // whether the View is focusable right now.
3912 view
.SetFocusable(true);
3913 EXPECT_TRUE(view
.focusable());
3914 view
.SetEnabled(false);
3915 EXPECT_TRUE(view
.focusable());
3916 view
.SetFocusable(false);
3917 EXPECT_FALSE(view
.focusable());
3920 // Creates a widget of TYPE_CONTROL.
3921 // The caller takes ownership of the returned widget.
3922 Widget
* CreateControlWidget(aura::Window
* parent
, const gfx::Rect
& bounds
) {
3923 Widget::InitParams
params(Widget::InitParams::TYPE_CONTROL
);
3924 params
.parent
= parent
;
3925 params
.bounds
= bounds
;
3926 Widget
* widget
= new Widget();
3927 widget
->Init(params
);
3931 // Returns a view with a layer with the passed in |bounds| and |layer_name|.
3932 // The caller takes ownership of the returned view.
3933 View
* CreateViewWithLayer(const gfx::Rect
& bounds
,
3934 const char* layer_name
) {
3935 View
* view
= new View();
3936 view
->SetBoundsRect(bounds
);
3937 view
->SetPaintToLayer(true);
3938 view
->layer()->set_name(layer_name
);
3942 // Test that RecreateWindowLayers() recreates the layers for all child windows
3943 // and all child views and that the z-order of the recreated layers matches that
3944 // of the original layers.
3948 // +-- v2 (no layer)
3949 // +-- v3 (no layer)
3957 TEST_F(ViewTest
, RecreateLayers
) {
3958 Widget
* w1
= CreateControlWidget(GetContext(), gfx::Rect(0, 0, 100, 100));
3959 w1
->GetNativeView()->layer()->set_name("w1");
3961 View
* v2
= new View();
3962 v2
->SetBounds(0, 1, 100, 101);
3963 View
* v3
= new View();
3964 v3
->SetBounds(0, 2, 100, 102);
3965 View
* w2_host_view
= new View();
3967 View
* v1
= CreateViewWithLayer(gfx::Rect(0, 3, 100, 103), "v1");
3968 ui::Layer
* v1_layer
= v1
->layer();
3969 w1
->GetRootView()->AddChildView(v1
);
3970 w1
->GetRootView()->AddChildView(v2
);
3971 v2
->AddChildView(v3
);
3972 View
* v4
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v4");
3973 ui::Layer
* v4_layer
= v4
->layer();
3974 v2
->AddChildView(v4
);
3976 w1
->GetRootView()->AddChildView(w2_host_view
);
3977 View
* v7
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v7");
3978 ui::Layer
* v7_layer
= v7
->layer();
3979 w1
->GetRootView()->AddChildView(v7
);
3981 View
* v8
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v8");
3982 ui::Layer
* v8_layer
= v8
->layer();
3983 v7
->AddChildView(v8
);
3985 View
* v9
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v9");
3986 ui::Layer
* v9_layer
= v9
->layer();
3987 v7
->AddChildView(v9
);
3989 Widget
* w2
= CreateControlWidget(w1
->GetNativeView(),
3990 gfx::Rect(0, 5, 100, 105));
3991 w2
->GetNativeView()->layer()->set_name("w2");
3992 w2
->GetNativeView()->SetProperty(kHostViewKey
, w2_host_view
);
3994 View
* v5
= CreateViewWithLayer(gfx::Rect(0, 6, 100, 106), "v5");
3995 w2
->GetRootView()->AddChildView(v5
);
3996 View
* v6
= CreateViewWithLayer(gfx::Rect(0, 7, 100, 107), "v6");
3997 ui::Layer
* v6_layer
= v6
->layer();
3998 v5
->AddChildView(v6
);
4000 // Test the initial order of the layers.
4001 ui::Layer
* w1_layer
= w1
->GetNativeView()->layer();
4002 ASSERT_EQ("w1", w1_layer
->name());
4003 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer
));
4004 ui::Layer
* w2_layer
= w1_layer
->children()[2];
4005 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer
));
4006 ui::Layer
* v5_layer
= w2_layer
->children()[0];
4007 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer
));
4010 scoped_ptr
<ui::LayerTreeOwner
> cloned_owner(
4011 wm::RecreateLayers(w1
->GetNativeView()));
4012 EXPECT_EQ(w1_layer
, cloned_owner
->root());
4013 EXPECT_NE(w1_layer
, w1
->GetNativeView()->layer());
4015 // The old layers should still exist and have the same hierarchy.
4016 ASSERT_EQ("w1", w1_layer
->name());
4017 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer
));
4018 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer
));
4019 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer
));
4020 EXPECT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_layer
));
4022 ASSERT_EQ(4u, w1_layer
->children().size());
4023 EXPECT_EQ(v1_layer
, w1_layer
->children()[0]);
4024 EXPECT_EQ(v4_layer
, w1_layer
->children()[1]);
4025 EXPECT_EQ(w2_layer
, w1_layer
->children()[2]);
4026 EXPECT_EQ(v7_layer
, w1_layer
->children()[3]);
4028 ASSERT_EQ(1u, w2_layer
->children().size());
4029 EXPECT_EQ(v5_layer
, w2_layer
->children()[0]);
4031 ASSERT_EQ(1u, v5_layer
->children().size());
4032 EXPECT_EQ(v6_layer
, v5_layer
->children()[0]);
4034 ASSERT_EQ(0u, v6_layer
->children().size());
4036 EXPECT_EQ(2u, v7_layer
->children().size());
4037 EXPECT_EQ(v8_layer
, v7_layer
->children()[0]);
4038 EXPECT_EQ(v9_layer
, v7_layer
->children()[1]);
4040 // The cloned layers should have the same hierarchy as old.
4041 ui::Layer
* w1_new_layer
= w1
->GetNativeView()->layer();
4042 EXPECT_EQ("w1", w1_new_layer
->name());
4043 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_new_layer
));
4044 ui::Layer
* w2_new_layer
= w1_new_layer
->children()[2];
4045 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_new_layer
));
4046 ui::Layer
* v5_new_layer
= w2_new_layer
->children()[0];
4047 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_new_layer
));
4048 ui::Layer
* v7_new_layer
= w1_new_layer
->children()[3];
4049 ASSERT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_new_layer
));
4051 // The views and the widgets are destroyed when AuraTestHelper::TearDown()
4052 // destroys root_window().
4055 // Verifies when a view is deleted it is removed from ViewStorage.
4056 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
4057 ViewStorage
* view_storage
= ViewStorage::GetInstance();
4058 const int storage_id
= view_storage
->CreateStorageID();
4061 view_storage
->StoreView(storage_id
, &view
);
4063 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
4066 ////////////////////////////////////////////////////////////////////////////////
4068 ////////////////////////////////////////////////////////////////////////////////
4070 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
4071 native_theme_
= native_theme
;
4074 TEST_F(ViewTest
, OnNativeThemeChanged
) {
4075 TestView
* test_view
= new TestView();
4076 EXPECT_FALSE(test_view
->native_theme_
);
4077 TestView
* test_view_child
= new TestView();
4078 EXPECT_FALSE(test_view_child
->native_theme_
);
4080 // Child view added before the widget hierarchy exists should get the
4081 // new native theme notification.
4082 test_view
->AddChildView(test_view_child
);
4084 scoped_ptr
<Widget
> widget(new Widget
);
4085 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
4086 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
4087 widget
->Init(params
);
4089 widget
->GetRootView()->AddChildView(test_view
);
4090 EXPECT_TRUE(test_view
->native_theme_
);
4091 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
4092 EXPECT_TRUE(test_view_child
->native_theme_
);
4093 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
4095 // Child view added after the widget hierarchy exists should also get the
4097 TestView
* test_view_child_2
= new TestView();
4098 test_view
->AddChildView(test_view_child_2
);
4099 EXPECT_TRUE(test_view_child_2
->native_theme_
);
4100 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
4105 } // namespace views