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
{
206 TestView() : View(), delete_on_pressed_(false), native_theme_(NULL
) {}
207 virtual ~TestView() {}
209 // Reset all test state
211 did_change_bounds_
= false;
212 last_mouse_event_type_
= 0;
213 location_
.SetPoint(0, 0);
214 received_mouse_enter_
= false;
215 received_mouse_exit_
= false;
216 last_gesture_event_type_
= 0;
217 last_gesture_event_was_handled_
= false;
218 last_clip_
.setEmpty();
219 accelerator_count_map_
.clear();
222 // Exposed as public for testing.
224 views::View::Focus();
231 bool focusable() const { return View::focusable(); }
233 virtual void OnBoundsChanged(const gfx::Rect
& previous_bounds
) OVERRIDE
;
234 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
235 virtual bool OnMouseDragged(const ui::MouseEvent
& event
) OVERRIDE
;
236 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
237 virtual void OnMouseEntered(const ui::MouseEvent
& event
) OVERRIDE
;
238 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
240 // Ignores GestureEvent by default.
241 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
;
243 virtual void Paint(gfx::Canvas
* canvas
) OVERRIDE
;
244 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
;
245 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
247 virtual void OnNativeThemeChanged(const ui::NativeTheme
* native_theme
)
251 bool did_change_bounds_
;
252 gfx::Rect new_bounds_
;
255 int last_mouse_event_type_
;
256 gfx::Point location_
;
257 bool received_mouse_enter_
;
258 bool received_mouse_exit_
;
259 bool delete_on_pressed_
;
262 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
265 int last_gesture_event_type_
;
266 bool last_gesture_event_was_handled_
;
272 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
275 const ui::NativeTheme
* native_theme_
;
278 // A view subclass that consumes all Gesture events for testing purposes.
279 class TestViewConsumeGesture
: public TestView
{
281 TestViewConsumeGesture() : TestView() {}
282 virtual ~TestViewConsumeGesture() {}
285 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
286 last_gesture_event_type_
= event
->type();
287 location_
.SetPoint(event
->x(), event
->y());
288 event
->StopPropagation();
292 DISALLOW_COPY_AND_ASSIGN(TestViewConsumeGesture
);
295 // A view subclass that ignores all Gesture events.
296 class TestViewIgnoreGesture
: public TestView
{
298 TestViewIgnoreGesture() : TestView() {}
299 virtual ~TestViewIgnoreGesture() {}
302 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
305 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreGesture
);
308 // A view subclass that ignores all scroll-gesture events, but consume all other
310 class TestViewIgnoreScrollGestures
: public TestViewConsumeGesture
{
312 TestViewIgnoreScrollGestures() {}
313 virtual ~TestViewIgnoreScrollGestures() {}
316 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
317 if (event
->IsScrollGestureEvent())
319 TestViewConsumeGesture::OnGestureEvent(event
);
322 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreScrollGestures
);
325 ////////////////////////////////////////////////////////////////////////////////
327 ////////////////////////////////////////////////////////////////////////////////
329 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
330 did_change_bounds_
= true;
331 new_bounds_
= bounds();
334 TEST_F(ViewTest
, OnBoundsChanged
) {
337 gfx::Rect
prev_rect(0, 0, 200, 200);
338 gfx::Rect
new_rect(100, 100, 250, 250);
340 v
.SetBoundsRect(prev_rect
);
342 v
.SetBoundsRect(new_rect
);
344 EXPECT_TRUE(v
.did_change_bounds_
);
345 EXPECT_EQ(v
.new_bounds_
, new_rect
);
346 EXPECT_EQ(v
.bounds(), new_rect
);
349 ////////////////////////////////////////////////////////////////////////////////
351 ////////////////////////////////////////////////////////////////////////////////
353 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
354 last_mouse_event_type_
= event
.type();
355 location_
.SetPoint(event
.x(), event
.y());
356 if (delete_on_pressed_
)
361 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
362 last_mouse_event_type_
= event
.type();
363 location_
.SetPoint(event
.x(), event
.y());
367 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
368 last_mouse_event_type_
= event
.type();
369 location_
.SetPoint(event
.x(), event
.y());
372 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
373 received_mouse_enter_
= true;
376 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
377 received_mouse_exit_
= true;
380 TEST_F(ViewTest
, MouseEvent
) {
381 TestView
* v1
= new TestView();
382 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
384 TestView
* v2
= new TestView();
385 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
387 scoped_ptr
<Widget
> widget(new Widget
);
388 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
389 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
390 params
.bounds
= gfx::Rect(50, 50, 650, 650);
391 widget
->Init(params
);
392 internal::RootView
* root
=
393 static_cast<internal::RootView
*>(widget
->GetRootView());
395 root
->AddChildView(v1
);
396 v1
->AddChildView(v2
);
401 gfx::Point
p1(110, 120);
402 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
403 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
404 root
->OnMousePressed(pressed
);
405 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
406 EXPECT_EQ(v2
->location_
.x(), 10);
407 EXPECT_EQ(v2
->location_
.y(), 20);
408 // Make sure v1 did not receive the event
409 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
411 // Drag event out of bounds. Should still go to v2
414 gfx::Point
p2(50, 40);
415 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
,
416 ui::EF_LEFT_MOUSE_BUTTON
, 0);
417 root
->OnMouseDragged(dragged
);
418 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
419 EXPECT_EQ(v2
->location_
.x(), -50);
420 EXPECT_EQ(v2
->location_
.y(), -60);
421 // Make sure v1 did not receive the event
422 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
424 // Releasted event out of bounds. Should still go to v2
427 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
429 root
->OnMouseDragged(released
);
430 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
431 EXPECT_EQ(v2
->location_
.x(), -100);
432 EXPECT_EQ(v2
->location_
.y(), -100);
433 // Make sure v1 did not receive the event
434 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
439 // Confirm that a view can be deleted as part of processing a mouse press.
440 TEST_F(ViewTest
, DeleteOnPressed
) {
441 TestView
* v1
= new TestView();
442 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
444 TestView
* v2
= new TestView();
445 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
450 scoped_ptr
<Widget
> widget(new Widget
);
451 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
452 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
453 params
.bounds
= gfx::Rect(50, 50, 650, 650);
454 widget
->Init(params
);
455 View
* root
= widget
->GetRootView();
457 root
->AddChildView(v1
);
458 v1
->AddChildView(v2
);
460 v2
->delete_on_pressed_
= true;
461 gfx::Point
point(110, 120);
462 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
463 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
464 root
->OnMousePressed(pressed
);
465 EXPECT_EQ(0, v1
->child_count());
470 ////////////////////////////////////////////////////////////////////////////////
472 ////////////////////////////////////////////////////////////////////////////////
474 void TestView::OnGestureEvent(ui::GestureEvent
* event
) {
477 TEST_F(ViewTest
, GestureEvent
) {
478 // Views hierarchy for non delivery of GestureEvent.
479 TestView
* v1
= new TestViewConsumeGesture();
480 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
482 TestView
* v2
= new TestViewConsumeGesture();
483 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
485 TestView
* v3
= new TestViewIgnoreGesture();
486 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
488 scoped_ptr
<Widget
> widget(new Widget());
489 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
490 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
491 params
.bounds
= gfx::Rect(50, 50, 650, 650);
492 widget
->Init(params
);
493 internal::RootView
* root
=
494 static_cast<internal::RootView
*>(widget
->GetRootView());
495 ui::EventDispatchDetails details
;
497 root
->AddChildView(v1
);
498 v1
->AddChildView(v2
);
499 v2
->AddChildView(v3
);
501 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
502 // reach |v2| because |v3| doesn't process any gesture events. However, since
503 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
511 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
512 details
= root
->OnEventFromSource(&g1
);
513 EXPECT_FALSE(details
.dispatcher_destroyed
);
514 EXPECT_FALSE(details
.target_destroyed
);
516 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
517 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
518 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
520 // Simulate an up so that RootView is no longer targetting |v3|.
521 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
522 details
= root
->OnEventFromSource(&g1_up
);
523 EXPECT_FALSE(details
.dispatcher_destroyed
);
524 EXPECT_FALSE(details
.target_destroyed
);
531 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
532 details
= root
->OnEventFromSource(&g2
);
533 EXPECT_FALSE(details
.dispatcher_destroyed
);
534 EXPECT_FALSE(details
.target_destroyed
);
536 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
537 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
538 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
540 // Send event |g1| again. Even though the coordinates target |v3| it should go
541 // to |v1| as that is the view the touch was initially down on.
542 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
543 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
544 details
= root
->OnEventFromSource(&g1
);
545 EXPECT_FALSE(details
.dispatcher_destroyed
);
546 EXPECT_FALSE(details
.target_destroyed
);
548 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
549 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
550 EXPECT_EQ("110,110", v1
->location_
.ToString());
555 TEST_F(ViewTest
, ScrollGestureEvent
) {
556 // Views hierarchy for non delivery of GestureEvent.
557 TestView
* v1
= new TestViewConsumeGesture();
558 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
560 TestView
* v2
= new TestViewIgnoreScrollGestures();
561 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
563 TestView
* v3
= new TestViewIgnoreGesture();
564 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
566 scoped_ptr
<Widget
> widget(new Widget());
567 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
568 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
569 params
.bounds
= gfx::Rect(50, 50, 650, 650);
570 widget
->Init(params
);
571 internal::RootView
* root
=
572 static_cast<internal::RootView
*>(widget
->GetRootView());
573 ui::EventDispatchDetails details
;
575 root
->AddChildView(v1
);
576 v1
->AddChildView(v2
);
577 v2
->AddChildView(v3
);
579 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
580 // reach |v2| because |v3| doesn't process any gesture events. However, since
581 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
589 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
590 details
= root
->OnEventFromSource(&g1
);
591 EXPECT_FALSE(details
.dispatcher_destroyed
);
592 EXPECT_FALSE(details
.target_destroyed
);
594 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
595 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
596 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
600 // Send scroll gestures on |v3|. The gesture should reach |v2|, however,
601 // since it does not process scroll-gesture events, these events should reach
603 GestureEventForTest
gscroll_begin(ui::ET_GESTURE_SCROLL_BEGIN
, 115, 115, 0);
604 details
= root
->OnEventFromSource(&gscroll_begin
);
605 EXPECT_FALSE(details
.dispatcher_destroyed
);
606 EXPECT_FALSE(details
.target_destroyed
);
608 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
609 EXPECT_EQ(ui::ET_GESTURE_SCROLL_BEGIN
, v1
->last_gesture_event_type_
);
612 // Send a second tap on |v1|. The event should reach |v2| since it is the
613 // default gesture handler, and not |v1| (even though it is the view under the
614 // point, and is the scroll event handler).
615 GestureEventForTest
second_tap(ui::ET_GESTURE_TAP
, 70, 70, 0);
616 details
= root
->OnEventFromSource(&second_tap
);
617 EXPECT_FALSE(details
.dispatcher_destroyed
);
618 EXPECT_FALSE(details
.target_destroyed
);
620 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
621 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
624 GestureEventForTest
gscroll_end(ui::ET_GESTURE_SCROLL_END
, 50, 50, 0);
625 details
= root
->OnEventFromSource(&gscroll_end
);
626 EXPECT_FALSE(details
.dispatcher_destroyed
);
627 EXPECT_FALSE(details
.target_destroyed
);
629 EXPECT_EQ(ui::ET_GESTURE_SCROLL_END
, v1
->last_gesture_event_type_
);
632 // Simulate an up so that RootView is no longer targetting |v3|.
633 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
634 details
= root
->OnEventFromSource(&g1_up
);
635 EXPECT_FALSE(details
.dispatcher_destroyed
);
636 EXPECT_FALSE(details
.target_destroyed
);
638 EXPECT_EQ(ui::ET_GESTURE_END
, v2
->last_gesture_event_type_
);
645 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
646 details
= root
->OnEventFromSource(&g2
);
647 EXPECT_FALSE(details
.dispatcher_destroyed
);
648 EXPECT_FALSE(details
.target_destroyed
);
650 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
651 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
652 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
654 // Send event |g1| again. Even though the coordinates target |v3| it should go
655 // to |v1| as that is the view the touch was initially down on.
656 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
657 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
658 details
= root
->OnEventFromSource(&g1
);
659 EXPECT_FALSE(details
.dispatcher_destroyed
);
660 EXPECT_FALSE(details
.target_destroyed
);
662 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
663 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
664 EXPECT_EQ("110,110", v1
->location_
.ToString());
669 ////////////////////////////////////////////////////////////////////////////////
671 ////////////////////////////////////////////////////////////////////////////////
673 void TestView::Paint(gfx::Canvas
* canvas
) {
674 canvas
->sk_canvas()->getClipBounds(&last_clip_
);
677 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
678 scheduled_paint_rects_
.push_back(rect
);
679 View::SchedulePaintInRect(rect
);
682 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
683 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
684 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
685 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
686 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
689 TEST_F(ViewTest
, RemoveNotification
) {
690 ViewStorage
* vs
= ViewStorage::GetInstance();
691 Widget
* widget
= new Widget
;
692 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
693 View
* root_view
= widget
->GetRootView();
696 int s1
= vs
->CreateStorageID();
697 vs
->StoreView(s1
, v1
);
698 root_view
->AddChildView(v1
);
699 View
* v11
= new View
;
700 int s11
= vs
->CreateStorageID();
701 vs
->StoreView(s11
, v11
);
702 v1
->AddChildView(v11
);
703 View
* v111
= new View
;
704 int s111
= vs
->CreateStorageID();
705 vs
->StoreView(s111
, v111
);
706 v11
->AddChildView(v111
);
707 View
* v112
= new View
;
708 int s112
= vs
->CreateStorageID();
709 vs
->StoreView(s112
, v112
);
710 v11
->AddChildView(v112
);
711 View
* v113
= new View
;
712 int s113
= vs
->CreateStorageID();
713 vs
->StoreView(s113
, v113
);
714 v11
->AddChildView(v113
);
715 View
* v1131
= new View
;
716 int s1131
= vs
->CreateStorageID();
717 vs
->StoreView(s1131
, v1131
);
718 v113
->AddChildView(v1131
);
719 View
* v12
= new View
;
720 int s12
= vs
->CreateStorageID();
721 vs
->StoreView(s12
, v12
);
722 v1
->AddChildView(v12
);
725 int s2
= vs
->CreateStorageID();
726 vs
->StoreView(s2
, v2
);
727 root_view
->AddChildView(v2
);
728 View
* v21
= new View
;
729 int s21
= vs
->CreateStorageID();
730 vs
->StoreView(s21
, v21
);
731 v2
->AddChildView(v21
);
732 View
* v211
= new View
;
733 int s211
= vs
->CreateStorageID();
734 vs
->StoreView(s211
, v211
);
735 v21
->AddChildView(v211
);
737 size_t stored_views
= vs
->view_count();
739 // Try removing a leaf view.
740 v21
->RemoveChildView(v211
);
741 EXPECT_EQ(stored_views
- 1, vs
->view_count());
742 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
743 delete v211
; // We won't use this one anymore.
745 // Now try removing a view with a hierarchy of depth 1.
746 v11
->RemoveChildView(v113
);
747 EXPECT_EQ(stored_views
- 3, vs
->view_count());
748 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
749 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
750 delete v113
; // We won't use this one anymore.
752 // Now remove even more.
753 root_view
->RemoveChildView(v1
);
754 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
755 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
756 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
757 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
758 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
760 // Put v1 back for more tests.
761 root_view
->AddChildView(v1
);
762 vs
->StoreView(s1
, v1
);
764 // Synchronously closing the window deletes the view hierarchy, which should
765 // remove all its views from ViewStorage.
767 EXPECT_EQ(stored_views
- 10, vs
->view_count());
768 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
769 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
770 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
771 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
772 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
773 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
774 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
778 class HitTestView
: public View
{
780 explicit HitTestView(bool has_hittest_mask
)
781 : has_hittest_mask_(has_hittest_mask
) {
783 virtual ~HitTestView() {}
786 // Overridden from View:
787 virtual bool HasHitTestMask() const OVERRIDE
{
788 return has_hittest_mask_
;
790 virtual void GetHitTestMask(HitTestSource source
,
791 gfx::Path
* mask
) const OVERRIDE
{
792 DCHECK(has_hittest_mask_
);
795 SkScalar w
= SkIntToScalar(width());
796 SkScalar h
= SkIntToScalar(height());
798 // Create a triangular mask within the bounds of this View.
799 mask
->moveTo(w
/ 2, 0);
806 bool has_hittest_mask_
;
808 DISALLOW_COPY_AND_ASSIGN(HitTestView
);
811 gfx::Point
ConvertPointToView(View
* view
, const gfx::Point
& p
) {
813 View::ConvertPointToTarget(view
->GetWidget()->GetRootView(), view
, &tmp
);
817 gfx::Rect
ConvertRectToView(View
* view
, const gfx::Rect
& r
) {
819 tmp
.set_origin(ConvertPointToView(view
, r
.origin()));
823 void RotateCounterclockwise(gfx::Transform
* transform
) {
824 transform
->matrix().set3x3(0, -1, 0,
829 void RotateClockwise(gfx::Transform
* transform
) {
830 transform
->matrix().set3x3( 0, 1, 0,
837 TEST_F(ViewTest
, HitTestMasks
) {
838 Widget
* widget
= new Widget
;
839 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
840 widget
->Init(params
);
841 View
* root_view
= widget
->GetRootView();
842 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
844 gfx::Rect v1_bounds
= gfx::Rect(0, 0, 100, 100);
845 HitTestView
* v1
= new HitTestView(false);
846 v1
->SetBoundsRect(v1_bounds
);
847 root_view
->AddChildView(v1
);
849 gfx::Rect v2_bounds
= gfx::Rect(105, 0, 100, 100);
850 HitTestView
* v2
= new HitTestView(true);
851 v2
->SetBoundsRect(v2_bounds
);
852 root_view
->AddChildView(v2
);
854 gfx::Point v1_centerpoint
= v1_bounds
.CenterPoint();
855 gfx::Point v2_centerpoint
= v2_bounds
.CenterPoint();
856 gfx::Point v1_origin
= v1_bounds
.origin();
857 gfx::Point v2_origin
= v2_bounds
.origin();
859 gfx::Rect
r1(10, 10, 110, 15);
860 gfx::Rect
r2(106, 1, 98, 98);
861 gfx::Rect
r3(0, 0, 300, 300);
862 gfx::Rect
r4(115, 342, 200, 10);
865 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_centerpoint
)));
866 EXPECT_TRUE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_centerpoint
)));
868 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_origin
)));
869 EXPECT_FALSE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_origin
)));
872 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r1
)));
873 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r1
)));
875 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r2
)));
876 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r2
)));
878 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r3
)));
879 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r3
)));
881 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r4
)));
882 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r4
)));
884 // Test GetEventHandlerForPoint
885 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_centerpoint
));
886 EXPECT_EQ(v2
, root_view
->GetEventHandlerForPoint(v2_centerpoint
));
888 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_origin
));
889 EXPECT_EQ(root_view
, root_view
->GetEventHandlerForPoint(v2_origin
));
891 // Test GetTooltipHandlerForPoint
892 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_centerpoint
));
893 EXPECT_EQ(v2
, root_view
->GetTooltipHandlerForPoint(v2_centerpoint
));
895 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_origin
));
896 EXPECT_EQ(root_view
, root_view
->GetTooltipHandlerForPoint(v2_origin
));
898 EXPECT_FALSE(v1
->GetTooltipHandlerForPoint(v2_origin
));
903 // Tests the correctness of the rect-based targeting algorithm implemented in
904 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
905 // of rect-based targeting.
906 TEST_F(ViewTest
, GetEventHandlerForRect
) {
907 Widget
* widget
= new Widget
;
908 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
909 widget
->Init(params
);
910 View
* root_view
= widget
->GetRootView();
911 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
913 // Have this hierarchy of views (the coordinates here are all in
914 // the root view's coordinate space):
915 // v1 (0, 0, 100, 100)
916 // v2 (150, 0, 250, 100)
917 // v3 (0, 200, 150, 100)
918 // v31 (10, 210, 80, 80)
919 // v32 (110, 210, 30, 80)
920 // v4 (300, 200, 100, 100)
921 // v41 (310, 210, 80, 80)
922 // v411 (370, 275, 10, 5)
923 // v5 (450, 197, 30, 36)
924 // v51 (450, 200, 30, 30)
926 // The coordinates used for SetBounds are in parent coordinates.
928 TestView
* v1
= new TestView
;
929 v1
->SetBounds(0, 0, 100, 100);
930 root_view
->AddChildView(v1
);
932 TestView
* v2
= new TestView
;
933 v2
->SetBounds(150, 0, 250, 100);
934 root_view
->AddChildView(v2
);
936 TestView
* v3
= new TestView
;
937 v3
->SetBounds(0, 200, 150, 100);
938 root_view
->AddChildView(v3
);
940 TestView
* v4
= new TestView
;
941 v4
->SetBounds(300, 200, 100, 100);
942 root_view
->AddChildView(v4
);
944 TestView
* v31
= new TestView
;
945 v31
->SetBounds(10, 10, 80, 80);
946 v3
->AddChildView(v31
);
948 TestView
* v32
= new TestView
;
949 v32
->SetBounds(110, 10, 30, 80);
950 v3
->AddChildView(v32
);
952 TestView
* v41
= new TestView
;
953 v41
->SetBounds(10, 10, 80, 80);
954 v4
->AddChildView(v41
);
956 TestView
* v411
= new TestView
;
957 v411
->SetBounds(60, 65, 10, 5);
958 v41
->AddChildView(v411
);
960 TestView
* v5
= new TestView
;
961 v5
->SetBounds(450, 197, 30, 36);
962 root_view
->AddChildView(v5
);
964 TestView
* v51
= new TestView
;
965 v51
->SetBounds(0, 3, 30, 30);
966 v5
->AddChildView(v51
);
968 // |touch_rect| does not intersect any descendant view of |root_view|.
969 gfx::Rect
touch_rect(105, 105, 30, 45);
970 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
971 EXPECT_EQ(root_view
, result_view
);
974 // Covers |v1| by at least 60%.
975 touch_rect
.SetRect(15, 15, 100, 100);
976 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
977 EXPECT_EQ(v1
, result_view
);
980 // Intersects |v1| but does not cover it by at least 60%. The center
981 // of |touch_rect| is within |v1|.
982 touch_rect
.SetRect(50, 50, 5, 10);
983 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
984 EXPECT_EQ(v1
, result_view
);
987 // Intersects |v1| but does not cover it by at least 60%. The center
988 // of |touch_rect| is not within |v1|.
989 touch_rect
.SetRect(95, 96, 21, 22);
990 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
991 EXPECT_EQ(root_view
, result_view
);
994 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
995 touch_rect
.SetRect(95, 10, 300, 120);
996 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
997 EXPECT_EQ(v2
, result_view
);
1000 // Covers both |v1| and |v2| by at least 60%, but the center point
1001 // of |touch_rect| is closer to the center point of |v2|.
1002 touch_rect
.SetRect(20, 20, 400, 100);
1003 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1004 EXPECT_EQ(v2
, result_view
);
1007 // Covers both |v1| and |v2| by at least 60%, but the center point
1008 // of |touch_rect| is closer to the center point of |v1|.
1009 touch_rect
.SetRect(-700, -15, 1050, 110);
1010 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1011 EXPECT_EQ(v1
, result_view
);
1014 // A mouse click within |v1| will target |v1|.
1015 touch_rect
.SetRect(15, 15, 1, 1);
1016 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1017 EXPECT_EQ(v1
, result_view
);
1020 // Intersects |v3| and |v31| by at least 60% and the center point
1021 // of |touch_rect| is closer to the center point of |v31|.
1022 touch_rect
.SetRect(0, 200, 110, 100);
1023 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1024 EXPECT_EQ(v31
, result_view
);
1027 // Intersects |v3| and |v31|, but neither by at least 60%. The
1028 // center point of |touch_rect| lies within |v31|.
1029 touch_rect
.SetRect(80, 280, 15, 15);
1030 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1031 EXPECT_EQ(v31
, result_view
);
1034 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1035 // center point of |touch_rect| is closest to the center point
1037 touch_rect
.SetRect(0, 200, 200, 100);
1038 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1039 EXPECT_EQ(v32
, result_view
);
1042 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1043 // |v31| and |v32| by at least 60%. The center point of
1044 // |touch_rect| is closest to the center point of |v32|.
1045 touch_rect
.SetRect(30, 225, 180, 115);
1046 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1047 EXPECT_EQ(v32
, result_view
);
1050 // A mouse click at the corner of |v3| will target |v3|.
1051 touch_rect
.SetRect(0, 200, 1, 1);
1052 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1053 EXPECT_EQ(v3
, result_view
);
1056 // A mouse click within |v32| will target |v32|.
1057 touch_rect
.SetRect(112, 211, 1, 1);
1058 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1059 EXPECT_EQ(v32
, result_view
);
1062 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1063 // The center point of |touch_rect| is equally close to
1064 // the center points of |v4| and |v41|.
1065 touch_rect
.SetRect(310, 210, 80, 80);
1066 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1067 EXPECT_EQ(v41
, result_view
);
1070 // Intersects all of |v4|, |v41|, and |v411| but only covers
1071 // |v411| by at least 60%.
1072 touch_rect
.SetRect(370, 275, 7, 5);
1073 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1074 EXPECT_EQ(v411
, result_view
);
1077 // Intersects |v4| and |v41| but covers neither by at least 60%.
1078 // The center point of |touch_rect| is equally close to the center
1079 // points of |v4| and |v41|.
1080 touch_rect
.SetRect(345, 245, 7, 7);
1081 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1082 EXPECT_EQ(v41
, result_view
);
1085 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1086 // them by at least 60%. The center point of |touch_rect| lies
1088 touch_rect
.SetRect(368, 272, 4, 6);
1089 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1090 EXPECT_EQ(v411
, result_view
);
1093 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1094 // them by at least 60%. The center point of |touch_rect| lies
1096 touch_rect
.SetRect(365, 270, 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(205, 275, 200, 2);
1105 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1106 EXPECT_EQ(v4
, result_view
);
1109 // Intersects all of |v4|, |v41|, and |v411| but only covers
1110 // |v41| by at least 60%.
1111 touch_rect
.SetRect(310, 210, 61, 66);
1112 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1113 EXPECT_EQ(v41
, result_view
);
1116 // A mouse click within |v411| will target |v411|.
1117 touch_rect
.SetRect(372, 275, 1, 1);
1118 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1119 EXPECT_EQ(v411
, result_view
);
1122 // A mouse click within |v41| will target |v41|.
1123 touch_rect
.SetRect(350, 215, 1, 1);
1124 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1125 EXPECT_EQ(v41
, result_view
);
1128 // Covers |v3|, |v4|, and all of their descendants by at
1129 // least 60%. The center point of |touch_rect| is closest
1130 // to the center point of |v32|.
1131 touch_rect
.SetRect(0, 200, 400, 100);
1132 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1133 EXPECT_EQ(v32
, result_view
);
1136 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1137 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1138 // The center point of |touch_rect| is closest to the center
1139 // point of |root_view|.
1140 touch_rect
.SetRect(110, 15, 375, 450);
1141 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1142 EXPECT_EQ(root_view
, result_view
);
1145 // Covers all views (except |v5| and |v51|) by at least 60%. The
1146 // center point of |touch_rect| is equally close to the center
1147 // points of |v2| and |v32|. One is not a descendant of the other,
1148 // so in this case the view selected is arbitrary (i.e.,
1149 // it depends only on the ordering of nodes in the views
1151 touch_rect
.SetRect(0, 0, 400, 300);
1152 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1153 EXPECT_EQ(v32
, result_view
);
1156 // Covers |v5| and |v51| by at least 60%, and the center point of
1157 // the touch is located within both views. Since both views share
1158 // the same center point, the child view should be selected.
1159 touch_rect
.SetRect(440, 190, 40, 40);
1160 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1161 EXPECT_EQ(v51
, result_view
);
1164 // Covers |v5| and |v51| by at least 60%, but the center point of
1165 // the touch is not located within either view. Since both views
1166 // share the same center point, the child view should be selected.
1167 touch_rect
.SetRect(455, 187, 60, 60);
1168 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1169 EXPECT_EQ(v51
, result_view
);
1172 // Covers neither |v5| nor |v51| by at least 60%, but the center
1173 // of the touch is located within |v51|.
1174 touch_rect
.SetRect(450, 197, 10, 10);
1175 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1176 EXPECT_EQ(v51
, result_view
);
1179 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1180 // The center point is located outside of both views.
1181 touch_rect
.SetRect(433, 180, 24, 24);
1182 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1183 EXPECT_EQ(root_view
, result_view
);
1186 // Only intersects |v5| but does not cover it by at least 60%. The
1187 // center point of the touch region is located within |v5|.
1188 touch_rect
.SetRect(449, 196, 3, 3);
1189 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1190 EXPECT_EQ(v5
, result_view
);
1193 // A mouse click within |v5| (but not |v51|) should target |v5|.
1194 touch_rect
.SetRect(462, 199, 1, 1);
1195 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1196 EXPECT_EQ(v5
, result_view
);
1199 // A mouse click |v5| and |v51| should target the child view.
1200 touch_rect
.SetRect(452, 226, 1, 1);
1201 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1202 EXPECT_EQ(v51
, result_view
);
1205 // A mouse click on the center of |v5| and |v51| should target
1207 touch_rect
.SetRect(465, 215, 1, 1);
1208 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1209 EXPECT_EQ(v51
, result_view
);
1215 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1216 Widget
* widget
= new Widget
;
1217 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1218 widget
->Init(params
);
1219 View
* root_view
= widget
->GetRootView();
1220 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1222 // Have this hierarchy of views (the coords here are in root coord):
1223 // v1 (0, 0, 100, 100)
1224 // - v11 (0, 0, 20, 30)
1225 // - v111 (5, 5, 5, 15)
1226 // - v12 (50, 10, 30, 90)
1227 // - v121 (60, 20, 10, 10)
1228 // v2 (105, 0, 100, 100)
1229 // - v21 (120, 10, 50, 20)
1231 TestView
* v1
= new TestView
;
1232 v1
->SetBounds(0, 0, 100, 100);
1233 root_view
->AddChildView(v1
);
1234 v1
->set_notify_enter_exit_on_child(true);
1236 TestView
* v11
= new TestView
;
1237 v11
->SetBounds(0, 0, 20, 30);
1238 v1
->AddChildView(v11
);
1240 TestView
* v111
= new TestView
;
1241 v111
->SetBounds(5, 5, 5, 15);
1242 v11
->AddChildView(v111
);
1244 TestView
* v12
= new TestView
;
1245 v12
->SetBounds(50, 10, 30, 90);
1246 v1
->AddChildView(v12
);
1248 TestView
* v121
= new TestView
;
1249 v121
->SetBounds(10, 10, 10, 10);
1250 v12
->AddChildView(v121
);
1252 TestView
* v2
= new TestView
;
1253 v2
->SetBounds(105, 0, 100, 100);
1254 root_view
->AddChildView(v2
);
1256 TestView
* v21
= new TestView
;
1257 v21
->SetBounds(15, 10, 50, 20);
1258 v2
->AddChildView(v21
);
1268 // Move the mouse in v111.
1269 gfx::Point
p1(6, 6);
1270 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, 0, 0);
1271 root_view
->OnMouseMoved(move1
);
1272 EXPECT_TRUE(v111
->received_mouse_enter_
);
1273 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1274 EXPECT_TRUE(v1
->received_mouse_enter_
);
1279 // Now, move into v121.
1280 gfx::Point
p2(65, 21);
1281 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, 0, 0);
1282 root_view
->OnMouseMoved(move2
);
1283 EXPECT_TRUE(v111
->received_mouse_exit_
);
1284 EXPECT_TRUE(v121
->received_mouse_enter_
);
1285 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1290 // Now, move into v11.
1291 gfx::Point
p3(1, 1);
1292 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, 0, 0);
1293 root_view
->OnMouseMoved(move3
);
1294 EXPECT_TRUE(v121
->received_mouse_exit_
);
1295 EXPECT_TRUE(v11
->received_mouse_enter_
);
1296 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1302 gfx::Point
p4(121, 15);
1303 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, 0, 0);
1304 root_view
->OnMouseMoved(move4
);
1305 EXPECT_TRUE(v21
->received_mouse_enter_
);
1306 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1307 EXPECT_TRUE(v11
->received_mouse_exit_
);
1308 EXPECT_TRUE(v1
->received_mouse_exit_
);
1315 gfx::Point
p5(21, 0);
1316 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, 0, 0);
1317 root_view
->OnMouseMoved(move5
);
1318 EXPECT_TRUE(v21
->received_mouse_exit_
);
1319 EXPECT_TRUE(v1
->received_mouse_enter_
);
1324 // Now, move into v11.
1325 gfx::Point
p6(15, 15);
1326 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, 0, 0);
1327 root_view
->OnMouseMoved(mouse6
);
1328 EXPECT_TRUE(v11
->received_mouse_enter_
);
1329 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1334 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1335 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1336 // when the mouse leaves v11.
1337 gfx::Point
p7(21, 0);
1338 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, 0, 0);
1339 root_view
->OnMouseMoved(mouse7
);
1340 EXPECT_TRUE(v11
->received_mouse_exit_
);
1341 EXPECT_FALSE(v1
->received_mouse_enter_
);
1346 TEST_F(ViewTest
, Textfield
) {
1347 const base::string16 kText
= ASCIIToUTF16(
1348 "Reality is that which, when you stop believing it, doesn't go away.");
1349 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1350 const base::string16 kEmptyString
;
1352 Widget
* widget
= new Widget
;
1353 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1354 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1355 widget
->Init(params
);
1356 View
* root_view
= widget
->GetRootView();
1358 Textfield
* textfield
= new Textfield();
1359 root_view
->AddChildView(textfield
);
1361 // Test setting, appending text.
1362 textfield
->SetText(kText
);
1363 EXPECT_EQ(kText
, textfield
->text());
1364 textfield
->AppendText(kExtraText
);
1365 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1366 textfield
->SetText(base::string16());
1367 EXPECT_EQ(kEmptyString
, textfield
->text());
1369 // Test selection related methods.
1370 textfield
->SetText(kText
);
1371 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1372 textfield
->SelectAll(false);
1373 EXPECT_EQ(kText
, textfield
->text());
1374 textfield
->ClearSelection();
1375 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1380 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1381 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1382 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1383 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1384 const base::string16 kPasswordText
=
1385 ASCIIToUTF16("Password! ** Secret stuff **");
1387 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1389 Widget
* widget
= new Widget
;
1390 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1391 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1392 widget
->Init(params
);
1393 View
* root_view
= widget
->GetRootView();
1395 Textfield
* normal
= new Textfield();
1396 Textfield
* read_only
= new Textfield();
1397 read_only
->SetReadOnly(true);
1398 Textfield
* password
= new Textfield();
1399 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1401 root_view
->AddChildView(normal
);
1402 root_view
->AddChildView(read_only
);
1403 root_view
->AddChildView(password
);
1405 normal
->SetText(kNormalText
);
1406 read_only
->SetText(kReadOnlyText
);
1407 password
->SetText(kPasswordText
);
1413 normal
->SelectAll(false);
1414 normal
->ExecuteCommand(IDS_APP_CUT
);
1415 base::string16 result
;
1416 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1417 EXPECT_EQ(kNormalText
, result
);
1418 normal
->SetText(kNormalText
); // Let's revert to the original content.
1420 read_only
->SelectAll(false);
1421 read_only
->ExecuteCommand(IDS_APP_CUT
);
1423 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1424 // Cut should have failed, so the clipboard content should not have changed.
1425 EXPECT_EQ(kNormalText
, result
);
1427 password
->SelectAll(false);
1428 password
->ExecuteCommand(IDS_APP_CUT
);
1430 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1431 // Cut should have failed, so the clipboard content should not have changed.
1432 EXPECT_EQ(kNormalText
, result
);
1438 // Start with |read_only| to observe a change in clipboard text.
1439 read_only
->SelectAll(false);
1440 read_only
->ExecuteCommand(IDS_APP_COPY
);
1442 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1443 EXPECT_EQ(kReadOnlyText
, result
);
1445 normal
->SelectAll(false);
1446 normal
->ExecuteCommand(IDS_APP_COPY
);
1448 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1449 EXPECT_EQ(kNormalText
, result
);
1451 password
->SelectAll(false);
1452 password
->ExecuteCommand(IDS_APP_COPY
);
1454 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1455 // Text cannot be copied from an obscured field; the clipboard won't change.
1456 EXPECT_EQ(kNormalText
, result
);
1462 // Attempting to paste kNormalText in a read-only text-field should fail.
1463 read_only
->SelectAll(false);
1464 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1465 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1467 password
->SelectAll(false);
1468 password
->ExecuteCommand(IDS_APP_PASTE
);
1469 EXPECT_EQ(kNormalText
, password
->text());
1471 // Copy from |read_only| to observe a change in the normal textfield text.
1472 read_only
->SelectAll(false);
1473 read_only
->ExecuteCommand(IDS_APP_COPY
);
1474 normal
->SelectAll(false);
1475 normal
->ExecuteCommand(IDS_APP_PASTE
);
1476 EXPECT_EQ(kReadOnlyText
, normal
->text());
1480 ////////////////////////////////////////////////////////////////////////////////
1482 ////////////////////////////////////////////////////////////////////////////////
1483 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1484 accelerator_count_map_
[accelerator
]++;
1488 // TODO: these tests were initially commented out when getting aura to
1489 // run. Figure out if still valuable and either nuke or fix.
1491 TEST_F(ViewTest
, ActivateAccelerator
) {
1492 // Register a keyboard accelerator before the view is added to a window.
1493 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1494 TestView
* view
= new TestView();
1496 view
->AddAccelerator(return_accelerator
);
1497 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1499 // Create a window and add the view as its child.
1500 scoped_ptr
<Widget
> widget(new Widget
);
1501 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1502 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1503 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1504 widget
->Init(params
);
1505 View
* root
= widget
->GetRootView();
1506 root
->AddChildView(view
);
1509 // Get the focus manager.
1510 FocusManager
* focus_manager
= widget
->GetFocusManager();
1511 ASSERT_TRUE(focus_manager
);
1513 // Hit the return key and see if it takes effect.
1514 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1515 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1517 // Hit the escape key. Nothing should happen.
1518 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1519 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1520 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1521 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1523 // Now register the escape key and hit it again.
1524 view
->AddAccelerator(escape_accelerator
);
1525 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1526 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1527 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1529 // Remove the return key accelerator.
1530 view
->RemoveAccelerator(return_accelerator
);
1531 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1532 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1533 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1535 // Add it again. Hit the return key and the escape key.
1536 view
->AddAccelerator(return_accelerator
);
1537 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1538 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1539 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1540 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1541 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1542 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1544 // Remove all the accelerators.
1545 view
->ResetAccelerators();
1546 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1547 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1548 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1549 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1550 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1551 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1556 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1557 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1558 TestView
* view
= new TestView();
1560 view
->AddAccelerator(return_accelerator
);
1561 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1563 scoped_ptr
<Widget
> widget(new Widget
);
1564 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1565 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1566 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1567 widget
->Init(params
);
1568 View
* root
= widget
->GetRootView();
1569 root
->AddChildView(view
);
1572 FocusManager
* focus_manager
= widget
->GetFocusManager();
1573 ASSERT_TRUE(focus_manager
);
1575 view
->SetVisible(false);
1576 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1578 view
->SetVisible(true);
1579 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1584 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1585 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1586 TestView
* view
= new TestView();
1588 view
->AddAccelerator(return_accelerator
);
1589 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1591 scoped_ptr
<Widget
> widget(new Widget
);
1592 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1593 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1594 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1595 widget
->Init(params
);
1596 View
* root
= widget
->GetRootView();
1597 root
->AddChildView(view
);
1599 FocusManager
* focus_manager
= widget
->GetFocusManager();
1600 ASSERT_TRUE(focus_manager
);
1602 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1603 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1606 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1607 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1610 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1611 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1616 ////////////////////////////////////////////////////////////////////////////////
1617 // Mouse-wheel message rerouting
1618 ////////////////////////////////////////////////////////////////////////////////
1619 class ScrollableTestView
: public View
{
1621 ScrollableTestView() { }
1623 virtual gfx::Size
GetPreferredSize() {
1624 return gfx::Size(100, 10000);
1627 virtual void Layout() {
1628 SizeToPreferredSize();
1632 class TestViewWithControls
: public View
{
1634 TestViewWithControls() {
1635 text_field_
= new Textfield();
1636 AddChildView(text_field_
);
1639 Textfield
* text_field_
;
1642 class SimpleWidgetDelegate
: public WidgetDelegate
{
1644 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1646 virtual void DeleteDelegate() { delete this; }
1648 virtual View
* GetContentsView() { return contents_
; }
1650 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1651 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1657 // Tests that the mouse-wheel messages are correctly rerouted to the window
1659 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1661 // Note that this fails for a variety of reasons:
1662 // - focused view is apparently reset across window activations and never
1663 // properly restored
1664 // - this test depends on you not having any other window visible open under the
1665 // area that it opens the test windows. --beng
1666 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1667 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1668 Widget
* window1
= Widget::CreateWindowWithBounds(
1669 new SimpleWidgetDelegate(view_with_controls
),
1670 gfx::Rect(0, 0, 100, 100));
1672 ScrollView
* scroll_view
= new ScrollView();
1673 scroll_view
->SetContents(new ScrollableTestView());
1674 Widget
* window2
= Widget::CreateWindowWithBounds(
1675 new SimpleWidgetDelegate(scroll_view
),
1676 gfx::Rect(200, 200, 100, 100));
1678 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1680 // Make the window1 active, as this is what it would be in real-world.
1681 window1
->Activate();
1683 // Let's send a mouse-wheel message to the different controls and check that
1684 // it is rerouted to the window under the mouse (effectively scrolling the
1687 // First to the Window's HWND.
1688 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1689 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1690 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1692 window1
->CloseNow();
1693 window2
->CloseNow();
1697 ////////////////////////////////////////////////////////////////////////////////
1698 // Native view hierachy
1699 ////////////////////////////////////////////////////////////////////////////////
1700 class ToplevelWidgetObserverView
: public View
{
1702 ToplevelWidgetObserverView() : toplevel_(NULL
) {
1704 virtual ~ToplevelWidgetObserverView() {
1708 virtual void ViewHierarchyChanged(
1709 const ViewHierarchyChangedDetails
& details
) OVERRIDE
{
1710 if (details
.is_add
) {
1711 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1716 virtual void NativeViewHierarchyChanged() OVERRIDE
{
1717 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1720 Widget
* toplevel() { return toplevel_
; }
1725 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
1728 // Test that a view can track the current top level widget by overriding
1729 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1730 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
1731 scoped_ptr
<Widget
> toplevel1(new Widget
);
1732 Widget::InitParams toplevel1_params
=
1733 CreateParams(Widget::InitParams::TYPE_POPUP
);
1734 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1735 toplevel1
->Init(toplevel1_params
);
1737 scoped_ptr
<Widget
> toplevel2(new Widget
);
1738 Widget::InitParams toplevel2_params
=
1739 CreateParams(Widget::InitParams::TYPE_POPUP
);
1740 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1741 toplevel2
->Init(toplevel2_params
);
1743 Widget
* child
= new Widget
;
1744 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
1745 child_params
.parent
= toplevel1
->GetNativeView();
1746 child
->Init(child_params
);
1748 ToplevelWidgetObserverView
* observer_view
=
1749 new ToplevelWidgetObserverView();
1750 EXPECT_EQ(NULL
, observer_view
->toplevel());
1752 child
->SetContentsView(observer_view
);
1753 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
1755 Widget::ReparentNativeView(child
->GetNativeView(),
1756 toplevel2
->GetNativeView());
1757 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
1759 observer_view
->parent()->RemoveChildView(observer_view
);
1760 EXPECT_EQ(NULL
, observer_view
->toplevel());
1762 // Make |observer_view| |child|'s contents view again so that it gets deleted
1764 child
->SetContentsView(observer_view
);
1767 ////////////////////////////////////////////////////////////////////////////////
1769 ////////////////////////////////////////////////////////////////////////////////
1771 class TransformPaintView
: public TestView
{
1773 TransformPaintView() {}
1774 virtual ~TransformPaintView() {}
1776 void ClearScheduledPaintRect() {
1777 scheduled_paint_rect_
= gfx::Rect();
1780 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1782 // Overridden from View:
1783 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
{
1784 gfx::Rect xrect
= ConvertRectToParent(rect
);
1785 scheduled_paint_rect_
.Union(xrect
);
1789 gfx::Rect scheduled_paint_rect_
;
1791 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
1794 TEST_F(ViewTest
, TransformPaint
) {
1795 TransformPaintView
* v1
= new TransformPaintView();
1796 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1798 TestView
* v2
= new TestView();
1799 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1801 Widget
* widget
= new Widget
;
1802 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1803 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1804 widget
->Init(params
);
1806 View
* root
= widget
->GetRootView();
1808 root
->AddChildView(v1
);
1809 v1
->AddChildView(v2
);
1811 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1812 v1
->ClearScheduledPaintRect();
1813 v2
->SchedulePaint();
1815 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
1817 // Rotate |v1| counter-clockwise.
1818 gfx::Transform transform
;
1819 RotateCounterclockwise(&transform
);
1820 transform
.matrix().set(1, 3, 500.0);
1821 v1
->SetTransform(transform
);
1823 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1825 v1
->ClearScheduledPaintRect();
1826 v2
->SchedulePaint();
1828 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
1833 TEST_F(ViewTest
, TransformEvent
) {
1834 TestView
* v1
= new TestView();
1835 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1837 TestView
* v2
= new TestView();
1838 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1840 Widget
* widget
= new Widget
;
1841 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1842 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1843 widget
->Init(params
);
1844 View
* root
= widget
->GetRootView();
1846 root
->AddChildView(v1
);
1847 v1
->AddChildView(v2
);
1849 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1851 // Rotate |v1| counter-clockwise.
1852 gfx::Transform
transform(v1
->GetTransform());
1853 RotateCounterclockwise(&transform
);
1854 transform
.matrix().set(1, 3, 500.0);
1855 v1
->SetTransform(transform
);
1857 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1861 gfx::Point
p1(110, 210);
1862 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
1863 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1864 root
->OnMousePressed(pressed
);
1865 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1866 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1867 EXPECT_EQ(190, v2
->location_
.x());
1868 EXPECT_EQ(10, v2
->location_
.y());
1870 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
1872 root
->OnMouseReleased(released
);
1874 // Now rotate |v2| inside |v1| clockwise.
1875 transform
= v2
->GetTransform();
1876 RotateClockwise(&transform
);
1877 transform
.matrix().set(0, 3, 100.f
);
1878 v2
->SetTransform(transform
);
1880 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1881 // (300, 400) in |root|.
1886 gfx::Point
point2(110, 320);
1887 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
,
1888 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1889 root
->OnMousePressed(p2
);
1890 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1891 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1892 EXPECT_EQ(10, v2
->location_
.x());
1893 EXPECT_EQ(20, v2
->location_
.y());
1895 root
->OnMouseReleased(released
);
1897 v1
->SetTransform(gfx::Transform());
1898 v2
->SetTransform(gfx::Transform());
1900 TestView
* v3
= new TestView();
1901 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1902 v2
->AddChildView(v3
);
1904 // Rotate |v3| clockwise with respect to |v2|.
1905 transform
= v1
->GetTransform();
1906 RotateClockwise(&transform
);
1907 transform
.matrix().set(0, 3, 30.f
);
1908 v3
->SetTransform(transform
);
1910 // Scale |v2| with respect to |v1| along both axis.
1911 transform
= v2
->GetTransform();
1912 transform
.matrix().set(0, 0, 0.8f
);
1913 transform
.matrix().set(1, 1, 0.5f
);
1914 v2
->SetTransform(transform
);
1916 // |v3| occupies (108, 105) to (132, 115) in |root|.
1922 gfx::Point
point(112, 110);
1923 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
,
1924 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1925 root
->OnMousePressed(p3
);
1927 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1928 EXPECT_EQ(10, v3
->location_
.x());
1929 EXPECT_EQ(25, v3
->location_
.y());
1931 root
->OnMouseReleased(released
);
1933 v1
->SetTransform(gfx::Transform());
1934 v2
->SetTransform(gfx::Transform());
1935 v3
->SetTransform(gfx::Transform());
1941 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1942 transform
= v3
->GetTransform();
1943 RotateClockwise(&transform
);
1944 transform
.matrix().set(0, 3, 30.f
);
1945 // Rotation sets some scaling transformation. Using SetScale would overwrite
1946 // that and pollute the rotation. So combine the scaling with the existing
1948 gfx::Transform scale
;
1949 scale
.Scale(0.8f
, 0.5f
);
1950 transform
.ConcatTransform(scale
);
1951 v3
->SetTransform(transform
);
1953 // Translate |v2| with respect to |v1|.
1954 transform
= v2
->GetTransform();
1955 transform
.matrix().set(0, 3, 10.f
);
1956 transform
.matrix().set(1, 3, 10.f
);
1957 v2
->SetTransform(transform
);
1959 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1961 gfx::Point
point3(124, 125);
1962 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
,
1963 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1964 root
->OnMousePressed(p4
);
1966 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1967 EXPECT_EQ(10, v3
->location_
.x());
1968 EXPECT_EQ(25, v3
->location_
.y());
1970 root
->OnMouseReleased(released
);
1975 TEST_F(ViewTest
, TransformVisibleBound
) {
1976 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1978 scoped_ptr
<Widget
> widget(new Widget
);
1979 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1980 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1981 params
.bounds
= viewport_bounds
;
1982 widget
->Init(params
);
1983 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1985 View
* viewport
= new View
;
1986 widget
->SetContentsView(viewport
);
1987 View
* contents
= new View
;
1988 viewport
->AddChildView(contents
);
1989 viewport
->SetBoundsRect(viewport_bounds
);
1990 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1992 View
* child
= new View
;
1993 contents
->AddChildView(child
);
1994 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
1995 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
1997 // Rotate |child| counter-clockwise
1998 gfx::Transform transform
;
1999 RotateCounterclockwise(&transform
);
2000 transform
.matrix().set(1, 3, 50.f
);
2001 child
->SetTransform(transform
);
2002 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
2007 ////////////////////////////////////////////////////////////////////////////////
2008 // OnVisibleBoundsChanged()
2010 class VisibleBoundsView
: public View
{
2012 VisibleBoundsView() : received_notification_(false) {}
2013 virtual ~VisibleBoundsView() {}
2015 bool received_notification() const { return received_notification_
; }
2016 void set_received_notification(bool received
) {
2017 received_notification_
= received
;
2021 // Overridden from View:
2022 virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE
{
2025 virtual void OnVisibleBoundsChanged() OVERRIDE
{
2026 received_notification_
= true;
2029 bool received_notification_
;
2031 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2034 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2035 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2037 scoped_ptr
<Widget
> widget(new Widget
);
2038 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2039 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2040 params
.bounds
= viewport_bounds
;
2041 widget
->Init(params
);
2042 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2044 View
* viewport
= new View
;
2045 widget
->SetContentsView(viewport
);
2046 View
* contents
= new View
;
2047 viewport
->AddChildView(contents
);
2048 viewport
->SetBoundsRect(viewport_bounds
);
2049 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2051 // Create a view that cares about visible bounds notifications, and position
2052 // it just outside the visible bounds of the viewport.
2053 VisibleBoundsView
* child
= new VisibleBoundsView
;
2054 contents
->AddChildView(child
);
2055 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2057 // The child bound should be fully clipped.
2058 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2060 // Now scroll the contents, but not enough to make the child visible.
2061 contents
->SetY(contents
->y() - 1);
2063 // We should have received the notification since the visible bounds may have
2064 // changed (even though they didn't).
2065 EXPECT_TRUE(child
->received_notification());
2066 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2067 child
->set_received_notification(false);
2069 // Now scroll the contents, this time by enough to make the child visible by
2071 contents
->SetY(contents
->y() - 10);
2072 EXPECT_TRUE(child
->received_notification());
2073 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2074 child
->set_received_notification(false);
2079 TEST_F(ViewTest
, SetBoundsPaint
) {
2081 TestView
* child_view
= new TestView
;
2083 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2084 top_view
.scheduled_paint_rects_
.clear();
2085 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2086 top_view
.AddChildView(child_view
);
2088 top_view
.scheduled_paint_rects_
.clear();
2089 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2090 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2092 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2093 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2094 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2095 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2098 // Assertions around painting and focus gain/lost.
2099 TEST_F(ViewTest
, FocusBlurPaints
) {
2100 TestView parent_view
;
2101 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
2103 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2105 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2106 parent_view
.AddChildView(child_view1
);
2108 parent_view
.scheduled_paint_rects_
.clear();
2109 child_view1
->scheduled_paint_rects_
.clear();
2111 // Focus change shouldn't trigger paints.
2112 child_view1
->DoFocus();
2114 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2115 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2117 child_view1
->DoBlur();
2118 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2119 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2122 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2123 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
2126 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2127 view
.InvalidateLayout();
2128 view
.scheduled_paint_rects_
.clear();
2129 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2130 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
2133 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2134 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
2135 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2137 // We have to put the View hierarchy into a Widget or no paints will be
2139 scoped_ptr
<Widget
> widget(new Widget
);
2140 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2141 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2142 params
.bounds
= viewport_bounds
;
2143 widget
->Init(params
);
2144 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2146 TestView
* parent_view
= new TestView
;
2147 widget
->SetContentsView(parent_view
);
2148 parent_view
->SetBoundsRect(viewport_bounds
);
2149 parent_view
->scheduled_paint_rects_
.clear();
2151 View
* child_view
= new View
;
2152 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2153 parent_view
->AddChildView(child_view
);
2154 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2155 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2157 parent_view
->scheduled_paint_rects_
.clear();
2158 parent_view
->RemoveChildView(child_view
);
2159 scoped_ptr
<View
> child_deleter(child_view
);
2160 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2161 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2166 // Tests conversion methods with a transform.
2167 TEST_F(ViewTest
, ConversionsWithTransform
) {
2170 // View hierarchy used to test scale transforms.
2171 TestView
* child
= new TestView
;
2172 TestView
* child_child
= new TestView
;
2174 // View used to test a rotation transform.
2175 TestView
* child_2
= new TestView
;
2177 top_view
.AddChildView(child
);
2178 child
->AddChildView(child_child
);
2180 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2182 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2183 gfx::Transform transform
;
2184 transform
.Scale(3.0, 4.0);
2185 child
->SetTransform(transform
);
2187 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2188 transform
.MakeIdentity();
2189 transform
.Scale(5.0, 7.0);
2190 child_child
->SetTransform(transform
);
2192 top_view
.AddChildView(child_2
);
2193 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2194 transform
.MakeIdentity();
2195 RotateClockwise(&transform
);
2196 child_2
->SetTransform(transform
);
2198 // Sanity check to make sure basic transforms act as expected.
2200 gfx::Transform transform
;
2201 transform
.Translate(110.0, -110.0);
2202 transform
.Scale(100.0, 55.0);
2203 transform
.Translate(1.0, 1.0);
2205 // convert to a 3x3 matrix.
2206 const SkMatrix
& matrix
= transform
.matrix();
2208 EXPECT_EQ(210, matrix
.getTranslateX());
2209 EXPECT_EQ(-55, matrix
.getTranslateY());
2210 EXPECT_EQ(100, matrix
.getScaleX());
2211 EXPECT_EQ(55, matrix
.getScaleY());
2212 EXPECT_EQ(0, matrix
.getSkewX());
2213 EXPECT_EQ(0, matrix
.getSkewY());
2217 gfx::Transform transform
;
2218 transform
.Translate(1.0, 1.0);
2220 t2
.Scale(100.0, 55.0);
2222 t3
.Translate(110.0, -110.0);
2223 transform
.ConcatTransform(t2
);
2224 transform
.ConcatTransform(t3
);
2226 // convert to a 3x3 matrix
2227 const SkMatrix
& matrix
= transform
.matrix();
2229 EXPECT_EQ(210, matrix
.getTranslateX());
2230 EXPECT_EQ(-55, matrix
.getTranslateY());
2231 EXPECT_EQ(100, matrix
.getScaleX());
2232 EXPECT_EQ(55, matrix
.getScaleY());
2233 EXPECT_EQ(0, matrix
.getSkewX());
2234 EXPECT_EQ(0, matrix
.getSkewY());
2237 // Conversions from child->top and top->child.
2239 gfx::Point
point(5, 5);
2240 View::ConvertPointToTarget(child
, &top_view
, &point
);
2241 EXPECT_EQ(22, point
.x());
2242 EXPECT_EQ(39, point
.y());
2244 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2245 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2246 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2247 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2248 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2249 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2251 point
.SetPoint(22, 39);
2252 View::ConvertPointToTarget(&top_view
, child
, &point
);
2253 EXPECT_EQ(5, point
.x());
2254 EXPECT_EQ(5, point
.y());
2256 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2257 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2258 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2259 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2260 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2261 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2264 // Conversions from child_child->top and top->child_child.
2266 gfx::Point
point(5, 5);
2267 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2268 EXPECT_EQ(133, point
.x());
2269 EXPECT_EQ(211, point
.y());
2271 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2272 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2273 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2274 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2275 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2276 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2278 point
.SetPoint(133, 211);
2279 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2280 EXPECT_EQ(5, point
.x());
2281 EXPECT_EQ(5, point
.y());
2283 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2284 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2285 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2286 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2287 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2288 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2291 // Conversions from child_child->child and child->child_child
2293 gfx::Point
point(5, 5);
2294 View::ConvertPointToTarget(child_child
, child
, &point
);
2295 EXPECT_EQ(42, point
.x());
2296 EXPECT_EQ(48, point
.y());
2298 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2299 View::ConvertRectToTarget(child_child
, child
, &rect
);
2300 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2301 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2302 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2303 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2305 point
.SetPoint(42, 48);
2306 View::ConvertPointToTarget(child
, child_child
, &point
);
2307 EXPECT_EQ(5, point
.x());
2308 EXPECT_EQ(5, point
.y());
2310 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2311 View::ConvertRectToTarget(child
, child_child
, &rect
);
2312 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2313 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2314 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2315 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2318 // Conversions from top_view to child with a value that should be negative.
2319 // This ensures we don't round up with negative numbers.
2321 gfx::Point
point(6, 18);
2322 View::ConvertPointToTarget(&top_view
, child
, &point
);
2323 EXPECT_EQ(-1, point
.x());
2324 EXPECT_EQ(-1, point
.y());
2326 float error
= 0.01f
;
2327 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2328 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2329 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2330 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2331 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2332 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2335 // Rect conversions from top_view->child_2 and child_2->top_view.
2337 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2338 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2339 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2340 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2341 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2342 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2344 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2345 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2346 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2347 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2348 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2349 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2353 // Tests conversion methods to and from screen coordinates.
2354 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2355 scoped_ptr
<Widget
> widget(new Widget
);
2356 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2357 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2358 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2359 widget
->Init(params
);
2361 View
* child
= new View
;
2362 widget
->GetRootView()->AddChildView(child
);
2363 child
->SetBounds(10, 10, 100, 200);
2366 child
->SetTransform(t
);
2368 gfx::Point
point_in_screen(100, 90);
2369 gfx::Point
point_in_child(80,60);
2371 gfx::Point point
= point_in_screen
;
2372 View::ConvertPointFromScreen(child
, &point
);
2373 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2375 View::ConvertPointToScreen(child
, &point
);
2376 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2379 // Tests conversion methods for rectangles.
2380 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2381 scoped_ptr
<Widget
> widget(new Widget
);
2382 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2383 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2384 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2385 widget
->Init(params
);
2386 View
* root
= widget
->GetRootView();
2388 TestView
* v1
= new TestView
;
2389 TestView
* v2
= new TestView
;
2390 root
->AddChildView(v1
);
2391 v1
->AddChildView(v2
);
2393 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2394 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2396 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2397 gfx::Rect
rect(5, 5, 15, 40);
2398 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2399 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2403 RotateCounterclockwise(&t2
);
2404 t2
.matrix().set(1, 3, 100.f
);
2405 v2
->SetTransform(t2
);
2407 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2408 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2409 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2414 v1
->SetTransform(t1
);
2416 // The rectangle should remain the same for |v1|.
2417 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2419 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2420 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2421 v2
->ConvertRectToWidget(rect
).ToString());
2426 class ObserverView
: public View
{
2429 virtual ~ObserverView();
2431 void ResetTestState();
2433 bool has_add_details() const { return has_add_details_
; }
2434 bool has_remove_details() const { return has_remove_details_
; }
2436 const ViewHierarchyChangedDetails
& add_details() const {
2437 return add_details_
;
2440 const ViewHierarchyChangedDetails
& remove_details() const {
2441 return remove_details_
;
2446 virtual void ViewHierarchyChanged(
2447 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
2449 bool has_add_details_
;
2450 bool has_remove_details_
;
2451 ViewHierarchyChangedDetails add_details_
;
2452 ViewHierarchyChangedDetails remove_details_
;
2454 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2457 ObserverView::ObserverView()
2458 : has_add_details_(false),
2459 has_remove_details_(false) {
2462 ObserverView::~ObserverView() {}
2464 void ObserverView::ResetTestState() {
2465 has_add_details_
= false;
2466 has_remove_details_
= false;
2467 add_details_
= ViewHierarchyChangedDetails();
2468 remove_details_
= ViewHierarchyChangedDetails();
2471 void ObserverView::ViewHierarchyChanged(
2472 const ViewHierarchyChangedDetails
& details
) {
2473 if (details
.is_add
) {
2474 has_add_details_
= true;
2475 add_details_
= details
;
2477 has_remove_details_
= true;
2478 remove_details_
= details
;
2482 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2483 // a child view is added or removed to all the views in the hierarchy (up and
2485 // The tree looks like this:
2489 // +-- v4 (starts here, then get reparented to v1)
2490 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2493 ObserverView
* v3
= new ObserverView();
2495 // Add |v3| to |v2|.
2496 scoped_ptr
<ObserverView
> v2(new ObserverView());
2497 v2
->AddChildView(v3
);
2499 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2501 EXPECT_TRUE(v2
->has_add_details());
2502 EXPECT_FALSE(v2
->has_remove_details());
2503 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2504 EXPECT_EQ(v3
, v2
->add_details().child
);
2505 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2507 EXPECT_TRUE(v3
->has_add_details());
2508 EXPECT_FALSE(v3
->has_remove_details());
2509 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2510 EXPECT_EQ(v3
, v3
->add_details().child
);
2511 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2513 // Reset everything to the initial state.
2514 v2
->ResetTestState();
2515 v3
->ResetTestState();
2518 v1
.AddChildView(v2
.get());
2520 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2521 // Make sure all the views (v1, v2, v3) received _that_ information.
2522 EXPECT_TRUE(v1
.has_add_details());
2523 EXPECT_FALSE(v1
.has_remove_details());
2524 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2525 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2526 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2528 EXPECT_TRUE(v2
->has_add_details());
2529 EXPECT_FALSE(v2
->has_remove_details());
2530 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2531 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2532 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2534 EXPECT_TRUE(v3
->has_add_details());
2535 EXPECT_FALSE(v3
->has_remove_details());
2536 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2537 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2538 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2540 // Reset everything to the initial state.
2541 v1
.ResetTestState();
2542 v2
->ResetTestState();
2543 v3
->ResetTestState();
2545 // Remove |v2| from |v1|.
2546 v1
.RemoveChildView(v2
.get());
2548 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2549 // Make sure all the views (v1, v2, v3) received _that_ information.
2550 EXPECT_FALSE(v1
.has_add_details());
2551 EXPECT_TRUE(v1
.has_remove_details());
2552 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2553 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2554 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2556 EXPECT_FALSE(v2
->has_add_details());
2557 EXPECT_TRUE(v2
->has_remove_details());
2558 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2559 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2560 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2562 EXPECT_FALSE(v3
->has_add_details());
2563 EXPECT_TRUE(v3
->has_remove_details());
2564 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2565 EXPECT_EQ(v3
, v3
->remove_details().child
);
2566 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2568 // Verifies notifications when reparenting a view.
2569 ObserverView
* v4
= new ObserverView();
2570 // Add |v4| to |v2|.
2571 v2
->AddChildView(v4
);
2573 // Reset everything to the initial state.
2574 v1
.ResetTestState();
2575 v2
->ResetTestState();
2576 v3
->ResetTestState();
2577 v4
->ResetTestState();
2579 // Reparent |v4| to |v1|.
2580 v1
.AddChildView(v4
);
2582 // Verifies that all views receive the correct information for all the child,
2583 // parent and move views.
2585 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2586 EXPECT_TRUE(v1
.has_add_details());
2587 EXPECT_FALSE(v1
.has_remove_details());
2588 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2589 EXPECT_EQ(v4
, v1
.add_details().child
);
2590 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2592 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2594 EXPECT_FALSE(v2
->has_add_details());
2595 EXPECT_TRUE(v2
->has_remove_details());
2596 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2597 EXPECT_EQ(v4
, v2
->remove_details().child
);
2598 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2600 // |v3| is not impacted by this operation, and hence receives no notification.
2601 EXPECT_FALSE(v3
->has_add_details());
2602 EXPECT_FALSE(v3
->has_remove_details());
2604 // |v4| is the reparented child, so it receives notifications for the remove
2605 // and then the add. |v2| is its old parent, |v1| is its new parent.
2606 EXPECT_TRUE(v4
->has_remove_details());
2607 EXPECT_TRUE(v4
->has_add_details());
2608 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2609 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2610 EXPECT_EQ(v4
, v4
->add_details().child
);
2611 EXPECT_EQ(v4
, v4
->remove_details().child
);
2612 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2613 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2616 // Verifies if the child views added under the root are all deleted when calling
2617 // RemoveAllChildViews.
2618 // The tree looks like this:
2627 TEST_F(ViewTest
, RemoveAllChildViews
) {
2630 View
* child1
= new View
;
2631 root
.AddChildView(child1
);
2633 for (int i
= 0; i
< 2; ++i
)
2634 root
.AddChildView(new View
);
2636 View
* foo
= new View
;
2637 child1
->AddChildView(foo
);
2639 // Add some nodes to |foo|.
2640 for (int i
= 0; i
< 3; ++i
)
2641 foo
->AddChildView(new View
);
2643 EXPECT_EQ(3, root
.child_count());
2644 EXPECT_EQ(1, child1
->child_count());
2645 EXPECT_EQ(3, foo
->child_count());
2647 // Now remove all child views from root.
2648 root
.RemoveAllChildViews(true);
2650 EXPECT_EQ(0, root
.child_count());
2651 EXPECT_FALSE(root
.has_children());
2654 TEST_F(ViewTest
, Contains
) {
2656 View
* v2
= new View
;
2657 View
* v3
= new View
;
2659 v1
.AddChildView(v2
);
2660 v2
->AddChildView(v3
);
2662 EXPECT_FALSE(v1
.Contains(NULL
));
2663 EXPECT_TRUE(v1
.Contains(&v1
));
2664 EXPECT_TRUE(v1
.Contains(v2
));
2665 EXPECT_TRUE(v1
.Contains(v3
));
2667 EXPECT_FALSE(v2
->Contains(NULL
));
2668 EXPECT_TRUE(v2
->Contains(v2
));
2669 EXPECT_FALSE(v2
->Contains(&v1
));
2670 EXPECT_TRUE(v2
->Contains(v3
));
2672 EXPECT_FALSE(v3
->Contains(NULL
));
2673 EXPECT_TRUE(v3
->Contains(v3
));
2674 EXPECT_FALSE(v3
->Contains(&v1
));
2675 EXPECT_FALSE(v3
->Contains(v2
));
2678 // Verifies if GetIndexOf() returns the correct index for the specified child
2680 // The tree looks like this:
2685 TEST_F(ViewTest
, GetIndexOf
) {
2688 View
* child1
= new View
;
2689 root
.AddChildView(child1
);
2691 View
* child2
= new View
;
2692 root
.AddChildView(child2
);
2694 View
* foo1
= new View
;
2695 child1
->AddChildView(foo1
);
2697 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2698 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2699 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2700 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2701 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2703 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2704 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2705 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2706 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2707 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2709 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2710 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2711 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2712 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2713 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2716 // Verifies that the child views can be reordered correctly.
2717 TEST_F(ViewTest
, ReorderChildren
) {
2720 View
* child
= new View();
2721 root
.AddChildView(child
);
2723 View
* foo1
= new View();
2724 child
->AddChildView(foo1
);
2725 View
* foo2
= new View();
2726 child
->AddChildView(foo2
);
2727 View
* foo3
= new View();
2728 child
->AddChildView(foo3
);
2729 foo1
->SetFocusable(true);
2730 foo2
->SetFocusable(true);
2731 foo3
->SetFocusable(true);
2733 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2734 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2735 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2736 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2737 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2738 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2740 // Move |foo2| at the end.
2741 child
->ReorderChildView(foo2
, -1);
2742 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2743 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2744 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2745 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2746 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2747 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2749 // Move |foo1| at the end.
2750 child
->ReorderChildView(foo1
, -1);
2751 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2752 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2753 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2754 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2755 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2756 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2757 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2759 // Move |foo2| to the front.
2760 child
->ReorderChildView(foo2
, 0);
2761 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2762 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2763 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2764 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2765 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2766 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2767 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2770 // Verifies that GetViewByID returns the correctly child view from the specified
2772 // The tree looks like this:
2777 TEST_F(ViewTest
, GetViewByID
) {
2779 const int kV1ID
= 1;
2783 const int kV2ID
= 2;
2787 const int kV3ID
= 3;
2791 const int kV4ID
= 4;
2794 const int kV5ID
= 5;
2796 v1
.AddChildView(&v2
);
2797 v2
.AddChildView(&v3
);
2798 v2
.AddChildView(&v4
);
2800 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2801 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2802 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2804 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2805 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2807 const int kGroup
= 1;
2808 v3
.SetGroup(kGroup
);
2809 v4
.SetGroup(kGroup
);
2812 v1
.GetViewsInGroup(kGroup
, &views
);
2813 EXPECT_EQ(2U, views
.size());
2815 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2816 EXPECT_NE(views
.end(), i
);
2818 i
= std::find(views
.begin(), views
.end(), &v4
);
2819 EXPECT_NE(views
.end(), i
);
2822 TEST_F(ViewTest
, AddExistingChild
) {
2825 v1
.AddChildView(&v2
);
2826 v1
.AddChildView(&v3
);
2827 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2828 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2830 // Check that there's no change in order when adding at same index.
2831 v1
.AddChildViewAt(&v2
, 0);
2832 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2833 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2834 v1
.AddChildViewAt(&v3
, 1);
2835 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2836 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2838 // Add it at a different index and check for change in order.
2839 v1
.AddChildViewAt(&v2
, 1);
2840 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
2841 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
2842 v1
.AddChildViewAt(&v2
, 0);
2843 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2844 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2846 // Check that calling |AddChildView()| does not change the order.
2847 v1
.AddChildView(&v2
);
2848 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2849 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2850 v1
.AddChildView(&v3
);
2851 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2852 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2855 ////////////////////////////////////////////////////////////////////////////////
2857 ////////////////////////////////////////////////////////////////////////////////
2861 // Test implementation of LayerAnimator.
2862 class TestLayerAnimator
: public ui::LayerAnimator
{
2864 TestLayerAnimator();
2866 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
2869 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
2872 virtual ~TestLayerAnimator() { }
2875 gfx::Rect last_bounds_
;
2877 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
2880 TestLayerAnimator::TestLayerAnimator()
2881 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2884 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
2885 last_bounds_
= bounds
;
2890 class ViewLayerTest
: public ViewsTestBase
{
2892 ViewLayerTest() : widget_(NULL
) {}
2894 virtual ~ViewLayerTest() {
2897 // Returns the Layer used by the RootView.
2898 ui::Layer
* GetRootLayer() {
2899 return widget()->GetLayer();
2902 virtual void SetUp() OVERRIDE
{
2904 widget_
= new Widget
;
2905 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2906 params
.bounds
= gfx::Rect(50, 50, 200, 200);
2907 widget_
->Init(params
);
2909 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
2912 virtual void TearDown() OVERRIDE
{
2913 widget_
->CloseNow();
2914 ViewsTestBase::TearDown();
2917 Widget
* widget() { return widget_
; }
2924 TEST_F(ViewLayerTest
, LayerToggling
) {
2925 // Because we lazily create textures the calls to DrawTree are necessary to
2926 // ensure we trigger creation of textures.
2927 ui::Layer
* root_layer
= widget()->GetLayer();
2928 View
* content_view
= new View
;
2929 widget()->SetContentsView(content_view
);
2931 // Create v1, give it a bounds and verify everything is set up correctly.
2932 View
* v1
= new View
;
2933 v1
->SetPaintToLayer(true);
2934 EXPECT_TRUE(v1
->layer() != NULL
);
2935 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2936 content_view
->AddChildView(v1
);
2937 ASSERT_TRUE(v1
->layer() != NULL
);
2938 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2939 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
2941 // Create v2 as a child of v1 and do basic assertion testing.
2942 View
* v2
= new View
;
2943 v1
->AddChildView(v2
);
2944 EXPECT_TRUE(v2
->layer() == NULL
);
2945 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
2946 v2
->SetPaintToLayer(true);
2947 ASSERT_TRUE(v2
->layer() != NULL
);
2948 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2949 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2951 // Turn off v1s layer. v2 should still have a layer but its parent should have
2953 v1
->SetPaintToLayer(false);
2954 EXPECT_TRUE(v1
->layer() == NULL
);
2955 EXPECT_TRUE(v2
->layer() != NULL
);
2956 EXPECT_EQ(root_layer
, v2
->layer()->parent());
2957 ASSERT_EQ(1u, root_layer
->children().size());
2958 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
2959 // The bounds of the layer should have changed to be relative to the root view
2961 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
2963 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2964 gfx::Transform transform
;
2965 transform
.Scale(2.0, 2.0);
2966 v1
->SetTransform(transform
);
2967 EXPECT_TRUE(v1
->layer() != NULL
);
2968 EXPECT_TRUE(v2
->layer() != NULL
);
2969 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2970 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2971 ASSERT_EQ(1u, root_layer
->children().size());
2972 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
2973 ASSERT_EQ(1u, v1
->layer()->children().size());
2974 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
2975 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2978 // Verifies turning on a layer wires up children correctly.
2979 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
2980 View
* content_view
= new View
;
2981 widget()->SetContentsView(content_view
);
2983 // Create v1, give it a bounds and verify everything is set up correctly.
2984 View
* v1
= new View
;
2985 content_view
->AddChildView(v1
);
2986 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2988 View
* v2
= new View
;
2989 v1
->AddChildView(v2
);
2991 View
* v3
= new View
;
2992 v3
->SetPaintToLayer(true);
2993 v2
->AddChildView(v3
);
2994 ASSERT_TRUE(v3
->layer() != NULL
);
2996 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2998 v1
->SetPaintToLayer(true);
2999 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
3002 TEST_F(ViewLayerTest
, LayerAnimator
) {
3003 View
* content_view
= new View
;
3004 widget()->SetContentsView(content_view
);
3006 View
* v1
= new View
;
3007 content_view
->AddChildView(v1
);
3008 v1
->SetPaintToLayer(true);
3009 EXPECT_TRUE(v1
->layer() != NULL
);
3011 TestLayerAnimator
* animator
= new TestLayerAnimator();
3012 v1
->layer()->SetAnimator(animator
);
3014 gfx::Rect
bounds(1, 2, 3, 4);
3015 v1
->SetBoundsRect(bounds
);
3016 EXPECT_EQ(bounds
, animator
->last_bounds());
3017 // TestLayerAnimator doesn't update the layer.
3018 EXPECT_NE(bounds
, v1
->layer()->bounds());
3021 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3022 // doesn't have a layer change.
3023 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
3024 View
* content_view
= new View
;
3025 widget()->SetContentsView(content_view
);
3027 View
* v1
= new View
;
3028 content_view
->AddChildView(v1
);
3029 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3031 View
* v2
= new View
;
3032 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3033 v1
->AddChildView(v2
);
3034 v2
->SetPaintToLayer(true);
3035 ASSERT_TRUE(v2
->layer() != NULL
);
3036 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
3038 v1
->SetPosition(gfx::Point(25, 36));
3039 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
3041 v2
->SetPosition(gfx::Point(11, 12));
3042 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
3044 // Bounds of the layer should change even if the view is not invisible.
3045 v1
->SetVisible(false);
3046 v1
->SetPosition(gfx::Point(20, 30));
3047 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
3049 v2
->SetVisible(false);
3050 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3051 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
3054 // Make sure layers are positioned correctly in RTL.
3055 TEST_F(ViewLayerTest
, BoundInRTL
) {
3056 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3057 base::i18n::SetICUDefaultLocale("he");
3059 View
* view
= new View
;
3060 widget()->SetContentsView(view
);
3062 int content_width
= view
->width();
3064 // |v1| is initially not attached to anything. So its layer will have the same
3065 // bounds as the view.
3066 View
* v1
= new View
;
3067 v1
->SetPaintToLayer(true);
3068 v1
->SetBounds(10, 10, 20, 10);
3069 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3070 v1
->layer()->bounds());
3072 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3074 view
->AddChildView(v1
);
3075 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3076 v1
->layer()->bounds());
3077 gfx::Rect l1bounds
= v1
->layer()->bounds();
3079 // Now attach a View to the widget first, then create a layer for it. Make
3080 // sure the bounds are correct.
3081 View
* v2
= new View
;
3082 v2
->SetBounds(50, 10, 30, 10);
3083 EXPECT_FALSE(v2
->layer());
3084 view
->AddChildView(v2
);
3085 v2
->SetPaintToLayer(true);
3086 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
3087 v2
->layer()->bounds());
3088 gfx::Rect l2bounds
= v2
->layer()->bounds();
3090 view
->SetPaintToLayer(true);
3091 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3092 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3094 // Move one of the views. Make sure the layer is positioned correctly
3096 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
3097 l1bounds
.set_x(l1bounds
.x() + 5);
3098 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3100 view
->SetPaintToLayer(false);
3101 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3102 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3104 // Move a view again.
3105 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
3106 l2bounds
.set_x(l2bounds
.x() - 5);
3107 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3110 base::i18n::SetICUDefaultLocale(locale
);
3113 // Makes sure a transform persists after toggling the visibility.
3114 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3115 View
* view
= new View
;
3116 gfx::Transform transform
;
3117 transform
.Scale(2.0, 2.0);
3118 view
->SetTransform(transform
);
3119 widget()->SetContentsView(view
);
3120 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3122 view
->SetVisible(false);
3123 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3125 view
->SetVisible(true);
3126 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3129 // Verifies a transform persists after removing/adding a view with a transform.
3130 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3131 View
* view
= new View
;
3132 gfx::Transform transform
;
3133 transform
.Scale(2.0, 2.0);
3134 view
->SetTransform(transform
);
3135 widget()->SetContentsView(view
);
3136 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3137 ASSERT_TRUE(view
->layer() != NULL
);
3138 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3140 View
* parent
= view
->parent();
3141 parent
->RemoveChildView(view
);
3142 parent
->AddChildView(view
);
3144 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3145 ASSERT_TRUE(view
->layer() != NULL
);
3146 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3149 // Makes sure that layer visibility is correct after toggling View visibility.
3150 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3151 View
* content_view
= new View
;
3152 widget()->SetContentsView(content_view
);
3154 // The view isn't attached to a widget or a parent view yet. But it should
3155 // still have a layer, but the layer should not be attached to the root
3157 View
* v1
= new View
;
3158 v1
->SetPaintToLayer(true);
3159 EXPECT_TRUE(v1
->layer());
3160 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3163 // Once the view is attached to a widget, its layer should be attached to the
3164 // root layer and visible.
3165 content_view
->AddChildView(v1
);
3166 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3168 EXPECT_TRUE(v1
->layer()->IsDrawn());
3170 v1
->SetVisible(false);
3171 EXPECT_FALSE(v1
->layer()->IsDrawn());
3173 v1
->SetVisible(true);
3174 EXPECT_TRUE(v1
->layer()->IsDrawn());
3177 EXPECT_FALSE(v1
->layer()->IsDrawn());
3180 EXPECT_TRUE(v1
->layer()->IsDrawn());
3183 // Tests that the layers in the subtree are orphaned after a View is removed
3185 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3186 View
* content_view
= new View
;
3187 widget()->SetContentsView(content_view
);
3189 View
* v1
= new View
;
3190 content_view
->AddChildView(v1
);
3192 View
* v2
= new View
;
3193 v1
->AddChildView(v2
);
3194 v2
->SetPaintToLayer(true);
3195 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3197 EXPECT_TRUE(v2
->layer()->IsDrawn());
3199 content_view
->RemoveChildView(v1
);
3201 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3205 content_view
->AddChildView(v2
);
3208 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3210 EXPECT_TRUE(v2
->layer()->IsDrawn());
3213 class PaintTrackingView
: public View
{
3215 PaintTrackingView() : painted_(false) {
3218 bool painted() const { return painted_
; }
3219 void set_painted(bool value
) { painted_
= value
; }
3221 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
3228 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3231 // Makes sure child views with layers aren't painted when paint starts at an
3233 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3234 PaintTrackingView
* content_view
= new PaintTrackingView
;
3235 widget()->SetContentsView(content_view
);
3236 content_view
->SetPaintToLayer(true);
3237 GetRootLayer()->GetCompositor()->ScheduleDraw();
3238 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3239 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3240 content_view
->set_painted(false);
3241 // content_view no longer has a dirty rect. Paint from the root and make sure
3242 // PaintTrackingView isn't painted.
3243 GetRootLayer()->GetCompositor()->ScheduleDraw();
3244 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3245 EXPECT_FALSE(content_view
->painted());
3247 // Make content_view have a dirty rect, paint the layers and make sure
3248 // PaintTrackingView is painted.
3249 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3250 GetRootLayer()->GetCompositor()->ScheduleDraw();
3251 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3252 EXPECT_TRUE(content_view
->painted());
3255 // Tests that the visibility of child layers are updated correctly when a View's
3256 // visibility changes.
3257 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3258 View
* v1
= new View
;
3259 v1
->SetPaintToLayer(true);
3260 widget()->SetContentsView(v1
);
3262 View
* v2
= new View
;
3263 v1
->AddChildView(v2
);
3265 View
* v3
= new View
;
3266 v2
->AddChildView(v3
);
3267 v3
->SetVisible(false);
3269 View
* v4
= new View
;
3270 v4
->SetPaintToLayer(true);
3271 v3
->AddChildView(v4
);
3273 EXPECT_TRUE(v1
->layer()->IsDrawn());
3274 EXPECT_FALSE(v4
->layer()->IsDrawn());
3276 v2
->SetVisible(false);
3277 EXPECT_TRUE(v1
->layer()->IsDrawn());
3278 EXPECT_FALSE(v4
->layer()->IsDrawn());
3280 v2
->SetVisible(true);
3281 EXPECT_TRUE(v1
->layer()->IsDrawn());
3282 EXPECT_FALSE(v4
->layer()->IsDrawn());
3284 v2
->SetVisible(false);
3285 EXPECT_TRUE(v1
->layer()->IsDrawn());
3286 EXPECT_FALSE(v4
->layer()->IsDrawn());
3287 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3289 v3
->SetVisible(true);
3290 EXPECT_TRUE(v1
->layer()->IsDrawn());
3291 EXPECT_FALSE(v4
->layer()->IsDrawn());
3292 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3294 // Reparent |v3| to |v1|.
3295 v1
->AddChildView(v3
);
3296 EXPECT_TRUE(v1
->layer()->IsDrawn());
3297 EXPECT_TRUE(v4
->layer()->IsDrawn());
3298 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3301 // This test creates a random View tree, and then randomly reorders child views,
3302 // reparents views etc. Unrelated changes can appear to break this test. So
3303 // marking this as FLAKY.
3304 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3305 View
* content
= new View
;
3306 content
->SetPaintToLayer(true);
3307 widget()->SetContentsView(content
);
3310 ConstructTree(content
, 5);
3311 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3313 ScrambleTree(content
);
3314 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3316 ScrambleTree(content
);
3317 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3319 ScrambleTree(content
);
3320 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3323 // Verifies when views are reordered the layer is also reordered. The widget is
3324 // providing the parent layer.
3325 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3326 View
* content
= new View
;
3327 widget()->SetContentsView(content
);
3328 View
* c1
= new View
;
3329 c1
->SetPaintToLayer(true);
3330 content
->AddChildView(c1
);
3331 View
* c2
= new View
;
3332 c2
->SetPaintToLayer(true);
3333 content
->AddChildView(c2
);
3335 ui::Layer
* parent_layer
= c1
->layer()->parent();
3336 ASSERT_TRUE(parent_layer
);
3337 ASSERT_EQ(2u, parent_layer
->children().size());
3338 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3339 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3341 // Move c1 to the front. The layers should have moved too.
3342 content
->ReorderChildView(c1
, -1);
3343 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3344 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3347 // Verifies that the layer of a view can be acquired properly.
3348 TEST_F(ViewLayerTest
, AcquireLayer
) {
3349 View
* content
= new View
;
3350 widget()->SetContentsView(content
);
3351 scoped_ptr
<View
> c1(new View
);
3352 c1
->SetPaintToLayer(true);
3353 EXPECT_TRUE(c1
->layer());
3354 content
->AddChildView(c1
.get());
3356 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3357 EXPECT_EQ(layer
.get(), c1
->layer());
3359 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3360 EXPECT_NE(c1
->layer(), layer2
.get());
3362 // Destroy view before destroying layer.
3366 // Verify that new layer scales content only if the old layer does.
3367 TEST_F(ViewLayerTest
, RecreateLayerScaling
) {
3368 scoped_ptr
<View
> v(new View());
3369 v
->SetPaintToLayer(true);
3370 // Set to non default value.
3371 v
->layer()->set_scale_content(false);
3372 scoped_ptr
<ui::Layer
> old_layer(v
->RecreateLayer());
3373 ui::Layer
* new_layer
= v
->layer();
3374 EXPECT_FALSE(new_layer
->scale_content());
3377 // Verify the z-order of the layers as a result of calling RecreateLayer().
3378 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3379 scoped_ptr
<View
> v(new View());
3380 v
->SetPaintToLayer(true);
3382 View
* v1
= new View();
3383 v1
->SetPaintToLayer(true);
3384 v
->AddChildView(v1
);
3385 View
* v2
= new View();
3386 v2
->SetPaintToLayer(true);
3387 v
->AddChildView(v2
);
3389 // Test the initial z-order.
3390 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3391 ASSERT_EQ(2u, child_layers_pre
.size());
3392 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3393 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3395 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3397 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3398 // for |v1| and |v2|.
3399 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3400 ASSERT_EQ(3u, child_layers_post
.size());
3401 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3402 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3403 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3406 // Verify the z-order of the layers as a result of calling RecreateLayer when
3407 // the widget is the parent with the layer.
3408 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3409 View
* v
= new View();
3410 widget()->SetContentsView(v
);
3412 View
* v1
= new View();
3413 v1
->SetPaintToLayer(true);
3414 v
->AddChildView(v1
);
3415 View
* v2
= new View();
3416 v2
->SetPaintToLayer(true);
3417 v
->AddChildView(v2
);
3419 ui::Layer
* root_layer
= GetRootLayer();
3421 // Test the initial z-order.
3422 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3423 ASSERT_EQ(2u, child_layers_pre
.size());
3424 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3425 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3427 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3429 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3430 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3431 ASSERT_EQ(3u, child_layers_post
.size());
3432 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3433 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3434 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3437 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3439 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3441 v
.SetPaintToLayer(true);
3443 child
.SetPaintToLayer(true);
3444 v
.AddChildView(&child
);
3445 ASSERT_TRUE(v
.layer() != NULL
);
3446 ASSERT_EQ(1u, v
.layer()->children().size());
3447 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3449 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3450 v
.layer()->Add(&layer
);
3451 v
.layer()->StackAtBottom(&layer
);
3453 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3455 // All children should be moved from old layer to new layer.
3456 ASSERT_TRUE(old_layer
.get() != NULL
);
3457 EXPECT_TRUE(old_layer
->children().empty());
3459 // And new layer should have the two children.
3460 ASSERT_TRUE(v
.layer() != NULL
);
3461 ASSERT_EQ(2u, v
.layer()->children().size());
3462 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3463 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3466 TEST_F(ViewTest
, FocusableAssertions
) {
3467 // View subclasses may change insets based on whether they are focusable,
3468 // which effects the preferred size. To avoid preferred size changing around
3469 // these Views need to key off the last value set to SetFocusable(), not
3470 // whether the View is focusable right now. For this reason it's important
3471 // that focusable() return the last value passed to SetFocusable and not
3472 // whether the View is focusable right now.
3474 view
.SetFocusable(true);
3475 EXPECT_TRUE(view
.focusable());
3476 view
.SetEnabled(false);
3477 EXPECT_TRUE(view
.focusable());
3478 view
.SetFocusable(false);
3479 EXPECT_FALSE(view
.focusable());
3482 // Creates a widget of TYPE_CONTROL.
3483 // The caller takes ownership of the returned widget.
3484 Widget
* CreateControlWidget(aura::Window
* parent
, const gfx::Rect
& bounds
) {
3485 Widget::InitParams
params(Widget::InitParams::TYPE_CONTROL
);
3486 params
.parent
= parent
;
3487 params
.bounds
= bounds
;
3488 Widget
* widget
= new Widget();
3489 widget
->Init(params
);
3493 // Returns a view with a layer with the passed in |bounds| and |layer_name|.
3494 // The caller takes ownership of the returned view.
3495 View
* CreateViewWithLayer(const gfx::Rect
& bounds
,
3496 const char* layer_name
) {
3497 View
* view
= new View();
3498 view
->SetBoundsRect(bounds
);
3499 view
->SetPaintToLayer(true);
3500 view
->layer()->set_name(layer_name
);
3504 // Test that RecreateWindowLayers() recreates the layers for all child windows
3505 // and all child views and that the z-order of the recreated layers matches that
3506 // of the original layers.
3510 // +-- v2 (no layer)
3511 // +-- v3 (no layer)
3519 TEST_F(ViewTest
, RecreateLayers
) {
3520 Widget
* w1
= CreateControlWidget(GetContext(), gfx::Rect(0, 0, 100, 100));
3521 w1
->GetNativeView()->layer()->set_name("w1");
3523 View
* v2
= new View();
3524 v2
->SetBounds(0, 1, 100, 101);
3525 View
* v3
= new View();
3526 v3
->SetBounds(0, 2, 100, 102);
3527 View
* w2_host_view
= new View();
3529 View
* v1
= CreateViewWithLayer(gfx::Rect(0, 3, 100, 103), "v1");
3530 ui::Layer
* v1_layer
= v1
->layer();
3531 w1
->GetRootView()->AddChildView(v1
);
3532 w1
->GetRootView()->AddChildView(v2
);
3533 v2
->AddChildView(v3
);
3534 View
* v4
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v4");
3535 ui::Layer
* v4_layer
= v4
->layer();
3536 v2
->AddChildView(v4
);
3538 w1
->GetRootView()->AddChildView(w2_host_view
);
3539 View
* v7
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v7");
3540 ui::Layer
* v7_layer
= v7
->layer();
3541 w1
->GetRootView()->AddChildView(v7
);
3543 View
* v8
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v8");
3544 ui::Layer
* v8_layer
= v8
->layer();
3545 v7
->AddChildView(v8
);
3547 View
* v9
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v9");
3548 ui::Layer
* v9_layer
= v9
->layer();
3549 v7
->AddChildView(v9
);
3551 Widget
* w2
= CreateControlWidget(w1
->GetNativeView(),
3552 gfx::Rect(0, 5, 100, 105));
3553 w2
->GetNativeView()->layer()->set_name("w2");
3554 w2
->GetNativeView()->SetProperty(kHostViewKey
, w2_host_view
);
3556 View
* v5
= CreateViewWithLayer(gfx::Rect(0, 6, 100, 106), "v5");
3557 w2
->GetRootView()->AddChildView(v5
);
3558 View
* v6
= CreateViewWithLayer(gfx::Rect(0, 7, 100, 107), "v6");
3559 ui::Layer
* v6_layer
= v6
->layer();
3560 v5
->AddChildView(v6
);
3562 // Test the initial order of the layers.
3563 ui::Layer
* w1_layer
= w1
->GetNativeView()->layer();
3564 ASSERT_EQ("w1", w1_layer
->name());
3565 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer
));
3566 ui::Layer
* w2_layer
= w1_layer
->children()[2];
3567 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer
));
3568 ui::Layer
* v5_layer
= w2_layer
->children()[0];
3569 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer
));
3572 scoped_ptr
<ui::LayerTreeOwner
> cloned_owner(
3573 wm::RecreateLayers(w1
->GetNativeView()));
3574 EXPECT_EQ(w1_layer
, cloned_owner
->root());
3575 EXPECT_NE(w1_layer
, w1
->GetNativeView()->layer());
3577 // The old layers should still exist and have the same hierarchy.
3578 ASSERT_EQ("w1", w1_layer
->name());
3579 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer
));
3580 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer
));
3581 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer
));
3582 EXPECT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_layer
));
3584 ASSERT_EQ(4u, w1_layer
->children().size());
3585 EXPECT_EQ(v1_layer
, w1_layer
->children()[0]);
3586 EXPECT_EQ(v4_layer
, w1_layer
->children()[1]);
3587 EXPECT_EQ(w2_layer
, w1_layer
->children()[2]);
3588 EXPECT_EQ(v7_layer
, w1_layer
->children()[3]);
3590 ASSERT_EQ(1u, w2_layer
->children().size());
3591 EXPECT_EQ(v5_layer
, w2_layer
->children()[0]);
3593 ASSERT_EQ(1u, v5_layer
->children().size());
3594 EXPECT_EQ(v6_layer
, v5_layer
->children()[0]);
3596 ASSERT_EQ(0u, v6_layer
->children().size());
3598 EXPECT_EQ(2u, v7_layer
->children().size());
3599 EXPECT_EQ(v8_layer
, v7_layer
->children()[0]);
3600 EXPECT_EQ(v9_layer
, v7_layer
->children()[1]);
3602 // The cloned layers should have the same hierarchy as old.
3603 ui::Layer
* w1_new_layer
= w1
->GetNativeView()->layer();
3604 EXPECT_EQ("w1", w1_new_layer
->name());
3605 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_new_layer
));
3606 ui::Layer
* w2_new_layer
= w1_new_layer
->children()[2];
3607 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_new_layer
));
3608 ui::Layer
* v5_new_layer
= w2_new_layer
->children()[0];
3609 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_new_layer
));
3610 ui::Layer
* v7_new_layer
= w1_new_layer
->children()[3];
3611 ASSERT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_new_layer
));
3613 // The views and the widgets are destroyed when AuraTestHelper::TearDown()
3614 // destroys root_window().
3617 // Verifies when a view is deleted it is removed from ViewStorage.
3618 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3619 ViewStorage
* view_storage
= ViewStorage::GetInstance();
3620 const int storage_id
= view_storage
->CreateStorageID();
3623 view_storage
->StoreView(storage_id
, &view
);
3625 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
3628 ////////////////////////////////////////////////////////////////////////////////
3630 ////////////////////////////////////////////////////////////////////////////////
3632 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
3633 native_theme_
= native_theme
;
3636 TEST_F(ViewTest
, OnNativeThemeChanged
) {
3637 TestView
* test_view
= new TestView();
3638 EXPECT_FALSE(test_view
->native_theme_
);
3639 TestView
* test_view_child
= new TestView();
3640 EXPECT_FALSE(test_view_child
->native_theme_
);
3642 // Child view added before the widget hierarchy exists should get the
3643 // new native theme notification.
3644 test_view
->AddChildView(test_view_child
);
3646 scoped_ptr
<Widget
> widget(new Widget
);
3647 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
3648 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3649 widget
->Init(params
);
3651 widget
->GetRootView()->AddChildView(test_view
);
3652 EXPECT_TRUE(test_view
->native_theme_
);
3653 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
3654 EXPECT_TRUE(test_view_child
->native_theme_
);
3655 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
3657 // Child view added after the widget hierarchy exists should also get the
3659 TestView
* test_view_child_2
= new TestView();
3660 test_view
->AddChildView(test_view_child_2
);
3661 EXPECT_TRUE(test_view_child_2
->native_theme_
);
3662 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
3667 } // namespace views