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) {}
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
;
248 bool did_change_bounds_
;
249 gfx::Rect new_bounds_
;
252 int last_mouse_event_type_
;
253 gfx::Point location_
;
254 bool received_mouse_enter_
;
255 bool received_mouse_exit_
;
256 bool delete_on_pressed_
;
259 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
262 int last_gesture_event_type_
;
263 bool last_gesture_event_was_handled_
;
269 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
272 // A view subclass that consumes all Gesture events for testing purposes.
273 class TestViewConsumeGesture
: public TestView
{
275 TestViewConsumeGesture() : TestView() {}
276 virtual ~TestViewConsumeGesture() {}
279 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
280 last_gesture_event_type_
= event
->type();
281 location_
.SetPoint(event
->x(), event
->y());
282 event
->StopPropagation();
286 DISALLOW_COPY_AND_ASSIGN(TestViewConsumeGesture
);
289 // A view subclass that ignores all Gesture events.
290 class TestViewIgnoreGesture
: public TestView
{
292 TestViewIgnoreGesture() : TestView() {}
293 virtual ~TestViewIgnoreGesture() {}
296 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
299 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreGesture
);
302 // A view subclass that ignores all scroll-gesture events, but consume all other
304 class TestViewIgnoreScrollGestures
: public TestViewConsumeGesture
{
306 TestViewIgnoreScrollGestures() {}
307 virtual ~TestViewIgnoreScrollGestures() {}
310 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
311 if (event
->IsScrollGestureEvent())
313 TestViewConsumeGesture::OnGestureEvent(event
);
316 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreScrollGestures
);
319 ////////////////////////////////////////////////////////////////////////////////
321 ////////////////////////////////////////////////////////////////////////////////
323 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
324 did_change_bounds_
= true;
325 new_bounds_
= bounds();
328 TEST_F(ViewTest
, OnBoundsChanged
) {
331 gfx::Rect
prev_rect(0, 0, 200, 200);
332 gfx::Rect
new_rect(100, 100, 250, 250);
334 v
.SetBoundsRect(prev_rect
);
336 v
.SetBoundsRect(new_rect
);
338 EXPECT_TRUE(v
.did_change_bounds_
);
339 EXPECT_EQ(v
.new_bounds_
, new_rect
);
340 EXPECT_EQ(v
.bounds(), new_rect
);
343 ////////////////////////////////////////////////////////////////////////////////
345 ////////////////////////////////////////////////////////////////////////////////
347 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
348 last_mouse_event_type_
= event
.type();
349 location_
.SetPoint(event
.x(), event
.y());
350 if (delete_on_pressed_
)
355 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
356 last_mouse_event_type_
= event
.type();
357 location_
.SetPoint(event
.x(), event
.y());
361 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
362 last_mouse_event_type_
= event
.type();
363 location_
.SetPoint(event
.x(), event
.y());
366 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
367 received_mouse_enter_
= true;
370 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
371 received_mouse_exit_
= true;
374 TEST_F(ViewTest
, MouseEvent
) {
375 TestView
* v1
= new TestView();
376 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
378 TestView
* v2
= new TestView();
379 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
381 scoped_ptr
<Widget
> widget(new Widget
);
382 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
383 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
384 params
.bounds
= gfx::Rect(50, 50, 650, 650);
385 widget
->Init(params
);
386 internal::RootView
* root
=
387 static_cast<internal::RootView
*>(widget
->GetRootView());
389 root
->AddChildView(v1
);
390 v1
->AddChildView(v2
);
395 gfx::Point
p1(110, 120);
396 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
397 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
398 root
->OnMousePressed(pressed
);
399 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
400 EXPECT_EQ(v2
->location_
.x(), 10);
401 EXPECT_EQ(v2
->location_
.y(), 20);
402 // Make sure v1 did not receive the event
403 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
405 // Drag event out of bounds. Should still go to v2
408 gfx::Point
p2(50, 40);
409 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
,
410 ui::EF_LEFT_MOUSE_BUTTON
, 0);
411 root
->OnMouseDragged(dragged
);
412 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
413 EXPECT_EQ(v2
->location_
.x(), -50);
414 EXPECT_EQ(v2
->location_
.y(), -60);
415 // Make sure v1 did not receive the event
416 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
418 // Releasted event out of bounds. Should still go to v2
421 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
423 root
->OnMouseDragged(released
);
424 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
425 EXPECT_EQ(v2
->location_
.x(), -100);
426 EXPECT_EQ(v2
->location_
.y(), -100);
427 // Make sure v1 did not receive the event
428 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
433 // Confirm that a view can be deleted as part of processing a mouse press.
434 TEST_F(ViewTest
, DeleteOnPressed
) {
435 TestView
* v1
= new TestView();
436 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
438 TestView
* v2
= new TestView();
439 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
444 scoped_ptr
<Widget
> widget(new Widget
);
445 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
446 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
447 params
.bounds
= gfx::Rect(50, 50, 650, 650);
448 widget
->Init(params
);
449 View
* root
= widget
->GetRootView();
451 root
->AddChildView(v1
);
452 v1
->AddChildView(v2
);
454 v2
->delete_on_pressed_
= true;
455 gfx::Point
point(110, 120);
456 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
457 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
458 root
->OnMousePressed(pressed
);
459 EXPECT_EQ(0, v1
->child_count());
464 ////////////////////////////////////////////////////////////////////////////////
466 ////////////////////////////////////////////////////////////////////////////////
468 void TestView::OnGestureEvent(ui::GestureEvent
* event
) {
471 TEST_F(ViewTest
, GestureEvent
) {
472 // Views hierarchy for non delivery of GestureEvent.
473 TestView
* v1
= new TestViewConsumeGesture();
474 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
476 TestView
* v2
= new TestViewConsumeGesture();
477 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
479 TestView
* v3
= new TestViewIgnoreGesture();
480 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
482 scoped_ptr
<Widget
> widget(new Widget());
483 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
484 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
485 params
.bounds
= gfx::Rect(50, 50, 650, 650);
486 widget
->Init(params
);
487 internal::RootView
* root
=
488 static_cast<internal::RootView
*>(widget
->GetRootView());
489 ui::EventDispatchDetails details
;
491 root
->AddChildView(v1
);
492 v1
->AddChildView(v2
);
493 v2
->AddChildView(v3
);
495 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
496 // reach |v2| because |v3| doesn't process any gesture events. However, since
497 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
505 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
506 details
= root
->OnEventFromSource(&g1
);
507 EXPECT_FALSE(details
.dispatcher_destroyed
);
508 EXPECT_FALSE(details
.target_destroyed
);
510 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
511 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
512 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
514 // Simulate an up so that RootView is no longer targetting |v3|.
515 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
516 details
= root
->OnEventFromSource(&g1_up
);
517 EXPECT_FALSE(details
.dispatcher_destroyed
);
518 EXPECT_FALSE(details
.target_destroyed
);
525 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
526 details
= root
->OnEventFromSource(&g2
);
527 EXPECT_FALSE(details
.dispatcher_destroyed
);
528 EXPECT_FALSE(details
.target_destroyed
);
530 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
531 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
532 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
534 // Send event |g1| again. Even though the coordinates target |v3| it should go
535 // to |v1| as that is the view the touch was initially down on.
536 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
537 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
538 details
= root
->OnEventFromSource(&g1
);
539 EXPECT_FALSE(details
.dispatcher_destroyed
);
540 EXPECT_FALSE(details
.target_destroyed
);
542 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
543 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
544 EXPECT_EQ("110,110", v1
->location_
.ToString());
549 TEST_F(ViewTest
, ScrollGestureEvent
) {
550 // Views hierarchy for non delivery of GestureEvent.
551 TestView
* v1
= new TestViewConsumeGesture();
552 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
554 TestView
* v2
= new TestViewIgnoreScrollGestures();
555 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
557 TestView
* v3
= new TestViewIgnoreGesture();
558 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
560 scoped_ptr
<Widget
> widget(new Widget());
561 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
562 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
563 params
.bounds
= gfx::Rect(50, 50, 650, 650);
564 widget
->Init(params
);
565 internal::RootView
* root
=
566 static_cast<internal::RootView
*>(widget
->GetRootView());
567 ui::EventDispatchDetails details
;
569 root
->AddChildView(v1
);
570 v1
->AddChildView(v2
);
571 v2
->AddChildView(v3
);
573 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
574 // reach |v2| because |v3| doesn't process any gesture events. However, since
575 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
583 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
584 details
= root
->OnEventFromSource(&g1
);
585 EXPECT_FALSE(details
.dispatcher_destroyed
);
586 EXPECT_FALSE(details
.target_destroyed
);
588 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
589 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
590 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
594 // Send scroll gestures on |v3|. The gesture should reach |v2|, however,
595 // since it does not process scroll-gesture events, these events should reach
597 GestureEventForTest
gscroll_begin(ui::ET_GESTURE_SCROLL_BEGIN
, 115, 115, 0);
598 details
= root
->OnEventFromSource(&gscroll_begin
);
599 EXPECT_FALSE(details
.dispatcher_destroyed
);
600 EXPECT_FALSE(details
.target_destroyed
);
602 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
603 EXPECT_EQ(ui::ET_GESTURE_SCROLL_BEGIN
, v1
->last_gesture_event_type_
);
606 // Send a second tap on |v1|. The event should reach |v2| since it is the
607 // default gesture handler, and not |v1| (even though it is the view under the
608 // point, and is the scroll event handler).
609 GestureEventForTest
second_tap(ui::ET_GESTURE_TAP
, 70, 70, 0);
610 details
= root
->OnEventFromSource(&second_tap
);
611 EXPECT_FALSE(details
.dispatcher_destroyed
);
612 EXPECT_FALSE(details
.target_destroyed
);
614 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
615 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
618 GestureEventForTest
gscroll_end(ui::ET_GESTURE_SCROLL_END
, 50, 50, 0);
619 details
= root
->OnEventFromSource(&gscroll_end
);
620 EXPECT_FALSE(details
.dispatcher_destroyed
);
621 EXPECT_FALSE(details
.target_destroyed
);
623 EXPECT_EQ(ui::ET_GESTURE_SCROLL_END
, v1
->last_gesture_event_type_
);
626 // Simulate an up so that RootView is no longer targetting |v3|.
627 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
628 details
= root
->OnEventFromSource(&g1_up
);
629 EXPECT_FALSE(details
.dispatcher_destroyed
);
630 EXPECT_FALSE(details
.target_destroyed
);
632 EXPECT_EQ(ui::ET_GESTURE_END
, v2
->last_gesture_event_type_
);
639 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
640 details
= root
->OnEventFromSource(&g2
);
641 EXPECT_FALSE(details
.dispatcher_destroyed
);
642 EXPECT_FALSE(details
.target_destroyed
);
644 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
645 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
646 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
648 // Send event |g1| again. Even though the coordinates target |v3| it should go
649 // to |v1| as that is the view the touch was initially down on.
650 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
651 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
652 details
= root
->OnEventFromSource(&g1
);
653 EXPECT_FALSE(details
.dispatcher_destroyed
);
654 EXPECT_FALSE(details
.target_destroyed
);
656 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
657 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
658 EXPECT_EQ("110,110", v1
->location_
.ToString());
663 ////////////////////////////////////////////////////////////////////////////////
665 ////////////////////////////////////////////////////////////////////////////////
667 void TestView::Paint(gfx::Canvas
* canvas
) {
668 canvas
->sk_canvas()->getClipBounds(&last_clip_
);
671 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
672 scheduled_paint_rects_
.push_back(rect
);
673 View::SchedulePaintInRect(rect
);
676 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
677 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
678 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
679 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
680 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
683 TEST_F(ViewTest
, RemoveNotification
) {
684 ViewStorage
* vs
= ViewStorage::GetInstance();
685 Widget
* widget
= new Widget
;
686 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
687 View
* root_view
= widget
->GetRootView();
690 int s1
= vs
->CreateStorageID();
691 vs
->StoreView(s1
, v1
);
692 root_view
->AddChildView(v1
);
693 View
* v11
= new View
;
694 int s11
= vs
->CreateStorageID();
695 vs
->StoreView(s11
, v11
);
696 v1
->AddChildView(v11
);
697 View
* v111
= new View
;
698 int s111
= vs
->CreateStorageID();
699 vs
->StoreView(s111
, v111
);
700 v11
->AddChildView(v111
);
701 View
* v112
= new View
;
702 int s112
= vs
->CreateStorageID();
703 vs
->StoreView(s112
, v112
);
704 v11
->AddChildView(v112
);
705 View
* v113
= new View
;
706 int s113
= vs
->CreateStorageID();
707 vs
->StoreView(s113
, v113
);
708 v11
->AddChildView(v113
);
709 View
* v1131
= new View
;
710 int s1131
= vs
->CreateStorageID();
711 vs
->StoreView(s1131
, v1131
);
712 v113
->AddChildView(v1131
);
713 View
* v12
= new View
;
714 int s12
= vs
->CreateStorageID();
715 vs
->StoreView(s12
, v12
);
716 v1
->AddChildView(v12
);
719 int s2
= vs
->CreateStorageID();
720 vs
->StoreView(s2
, v2
);
721 root_view
->AddChildView(v2
);
722 View
* v21
= new View
;
723 int s21
= vs
->CreateStorageID();
724 vs
->StoreView(s21
, v21
);
725 v2
->AddChildView(v21
);
726 View
* v211
= new View
;
727 int s211
= vs
->CreateStorageID();
728 vs
->StoreView(s211
, v211
);
729 v21
->AddChildView(v211
);
731 size_t stored_views
= vs
->view_count();
733 // Try removing a leaf view.
734 v21
->RemoveChildView(v211
);
735 EXPECT_EQ(stored_views
- 1, vs
->view_count());
736 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
737 delete v211
; // We won't use this one anymore.
739 // Now try removing a view with a hierarchy of depth 1.
740 v11
->RemoveChildView(v113
);
741 EXPECT_EQ(stored_views
- 3, vs
->view_count());
742 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
743 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
744 delete v113
; // We won't use this one anymore.
746 // Now remove even more.
747 root_view
->RemoveChildView(v1
);
748 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
749 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
750 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
751 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
752 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
754 // Put v1 back for more tests.
755 root_view
->AddChildView(v1
);
756 vs
->StoreView(s1
, v1
);
758 // Synchronously closing the window deletes the view hierarchy, which should
759 // remove all its views from ViewStorage.
761 EXPECT_EQ(stored_views
- 10, vs
->view_count());
762 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
763 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
764 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
765 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
766 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
767 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
768 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
772 class HitTestView
: public View
{
774 explicit HitTestView(bool has_hittest_mask
)
775 : has_hittest_mask_(has_hittest_mask
) {
777 virtual ~HitTestView() {}
780 // Overridden from View:
781 virtual bool HasHitTestMask() const OVERRIDE
{
782 return has_hittest_mask_
;
784 virtual void GetHitTestMask(HitTestSource source
,
785 gfx::Path
* mask
) const OVERRIDE
{
786 DCHECK(has_hittest_mask_
);
789 SkScalar w
= SkIntToScalar(width());
790 SkScalar h
= SkIntToScalar(height());
792 // Create a triangular mask within the bounds of this View.
793 mask
->moveTo(w
/ 2, 0);
800 bool has_hittest_mask_
;
802 DISALLOW_COPY_AND_ASSIGN(HitTestView
);
805 gfx::Point
ConvertPointToView(View
* view
, const gfx::Point
& p
) {
807 View::ConvertPointToTarget(view
->GetWidget()->GetRootView(), view
, &tmp
);
811 gfx::Rect
ConvertRectToView(View
* view
, const gfx::Rect
& r
) {
813 tmp
.set_origin(ConvertPointToView(view
, r
.origin()));
817 void RotateCounterclockwise(gfx::Transform
* transform
) {
818 transform
->matrix().set3x3(0, -1, 0,
823 void RotateClockwise(gfx::Transform
* transform
) {
824 transform
->matrix().set3x3( 0, 1, 0,
831 TEST_F(ViewTest
, HitTestMasks
) {
832 Widget
* widget
= new Widget
;
833 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
834 widget
->Init(params
);
835 View
* root_view
= widget
->GetRootView();
836 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
838 gfx::Rect v1_bounds
= gfx::Rect(0, 0, 100, 100);
839 HitTestView
* v1
= new HitTestView(false);
840 v1
->SetBoundsRect(v1_bounds
);
841 root_view
->AddChildView(v1
);
843 gfx::Rect v2_bounds
= gfx::Rect(105, 0, 100, 100);
844 HitTestView
* v2
= new HitTestView(true);
845 v2
->SetBoundsRect(v2_bounds
);
846 root_view
->AddChildView(v2
);
848 gfx::Point v1_centerpoint
= v1_bounds
.CenterPoint();
849 gfx::Point v2_centerpoint
= v2_bounds
.CenterPoint();
850 gfx::Point v1_origin
= v1_bounds
.origin();
851 gfx::Point v2_origin
= v2_bounds
.origin();
853 gfx::Rect
r1(10, 10, 110, 15);
854 gfx::Rect
r2(106, 1, 98, 98);
855 gfx::Rect
r3(0, 0, 300, 300);
856 gfx::Rect
r4(115, 342, 200, 10);
859 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_centerpoint
)));
860 EXPECT_TRUE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_centerpoint
)));
862 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_origin
)));
863 EXPECT_FALSE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_origin
)));
866 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r1
)));
867 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r1
)));
869 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r2
)));
870 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r2
)));
872 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r3
)));
873 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r3
)));
875 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r4
)));
876 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r4
)));
878 // Test GetEventHandlerForPoint
879 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_centerpoint
));
880 EXPECT_EQ(v2
, root_view
->GetEventHandlerForPoint(v2_centerpoint
));
882 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_origin
));
883 EXPECT_EQ(root_view
, root_view
->GetEventHandlerForPoint(v2_origin
));
885 // Test GetTooltipHandlerForPoint
886 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_centerpoint
));
887 EXPECT_EQ(v2
, root_view
->GetTooltipHandlerForPoint(v2_centerpoint
));
889 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_origin
));
890 EXPECT_EQ(root_view
, root_view
->GetTooltipHandlerForPoint(v2_origin
));
892 EXPECT_FALSE(v1
->GetTooltipHandlerForPoint(v2_origin
));
897 // Tests the correctness of the rect-based targeting algorithm implemented in
898 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
899 // of rect-based targeting.
900 TEST_F(ViewTest
, GetEventHandlerForRect
) {
901 Widget
* widget
= new Widget
;
902 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
903 widget
->Init(params
);
904 View
* root_view
= widget
->GetRootView();
905 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
907 // Have this hierarchy of views (the coordinates here are all in
908 // the root view's coordinate space):
909 // v1 (0, 0, 100, 100)
910 // v2 (150, 0, 250, 100)
911 // v3 (0, 200, 150, 100)
912 // v31 (10, 210, 80, 80)
913 // v32 (110, 210, 30, 80)
914 // v4 (300, 200, 100, 100)
915 // v41 (310, 210, 80, 80)
916 // v411 (370, 275, 10, 5)
917 // v5 (450, 197, 30, 36)
918 // v51 (450, 200, 30, 30)
920 // The coordinates used for SetBounds are in parent coordinates.
922 TestView
* v1
= new TestView
;
923 v1
->SetBounds(0, 0, 100, 100);
924 root_view
->AddChildView(v1
);
926 TestView
* v2
= new TestView
;
927 v2
->SetBounds(150, 0, 250, 100);
928 root_view
->AddChildView(v2
);
930 TestView
* v3
= new TestView
;
931 v3
->SetBounds(0, 200, 150, 100);
932 root_view
->AddChildView(v3
);
934 TestView
* v4
= new TestView
;
935 v4
->SetBounds(300, 200, 100, 100);
936 root_view
->AddChildView(v4
);
938 TestView
* v31
= new TestView
;
939 v31
->SetBounds(10, 10, 80, 80);
940 v3
->AddChildView(v31
);
942 TestView
* v32
= new TestView
;
943 v32
->SetBounds(110, 10, 30, 80);
944 v3
->AddChildView(v32
);
946 TestView
* v41
= new TestView
;
947 v41
->SetBounds(10, 10, 80, 80);
948 v4
->AddChildView(v41
);
950 TestView
* v411
= new TestView
;
951 v411
->SetBounds(60, 65, 10, 5);
952 v41
->AddChildView(v411
);
954 TestView
* v5
= new TestView
;
955 v5
->SetBounds(450, 197, 30, 36);
956 root_view
->AddChildView(v5
);
958 TestView
* v51
= new TestView
;
959 v51
->SetBounds(0, 3, 30, 30);
960 v5
->AddChildView(v51
);
962 // |touch_rect| does not intersect any descendant view of |root_view|.
963 gfx::Rect
touch_rect(105, 105, 30, 45);
964 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
965 EXPECT_EQ(root_view
, result_view
);
968 // Covers |v1| by at least 60%.
969 touch_rect
.SetRect(15, 15, 100, 100);
970 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
971 EXPECT_EQ(v1
, result_view
);
974 // Intersects |v1| but does not cover it by at least 60%. The center
975 // of |touch_rect| is within |v1|.
976 touch_rect
.SetRect(50, 50, 5, 10);
977 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
978 EXPECT_EQ(v1
, result_view
);
981 // Intersects |v1| but does not cover it by at least 60%. The center
982 // of |touch_rect| is not within |v1|.
983 touch_rect
.SetRect(95, 96, 21, 22);
984 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
985 EXPECT_EQ(root_view
, result_view
);
988 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
989 touch_rect
.SetRect(95, 10, 300, 120);
990 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
991 EXPECT_EQ(v2
, result_view
);
994 // Covers both |v1| and |v2| by at least 60%, but the center point
995 // of |touch_rect| is closer to the center point of |v2|.
996 touch_rect
.SetRect(20, 20, 400, 100);
997 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
998 EXPECT_EQ(v2
, result_view
);
1001 // Covers both |v1| and |v2| by at least 60%, but the center point
1002 // of |touch_rect| is closer to the center point of |v1|.
1003 touch_rect
.SetRect(-700, -15, 1050, 110);
1004 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1005 EXPECT_EQ(v1
, result_view
);
1008 // A mouse click within |v1| will target |v1|.
1009 touch_rect
.SetRect(15, 15, 1, 1);
1010 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1011 EXPECT_EQ(v1
, result_view
);
1014 // Intersects |v3| and |v31| by at least 60% and the center point
1015 // of |touch_rect| is closer to the center point of |v31|.
1016 touch_rect
.SetRect(0, 200, 110, 100);
1017 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1018 EXPECT_EQ(v31
, result_view
);
1021 // Intersects |v3| and |v31|, but neither by at least 60%. The
1022 // center point of |touch_rect| lies within |v31|.
1023 touch_rect
.SetRect(80, 280, 15, 15);
1024 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1025 EXPECT_EQ(v31
, result_view
);
1028 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1029 // center point of |touch_rect| is closest to the center point
1031 touch_rect
.SetRect(0, 200, 200, 100);
1032 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1033 EXPECT_EQ(v32
, result_view
);
1036 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1037 // |v31| and |v32| by at least 60%. The center point of
1038 // |touch_rect| is closest to the center point of |v32|.
1039 touch_rect
.SetRect(30, 225, 180, 115);
1040 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1041 EXPECT_EQ(v32
, result_view
);
1044 // A mouse click at the corner of |v3| will target |v3|.
1045 touch_rect
.SetRect(0, 200, 1, 1);
1046 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1047 EXPECT_EQ(v3
, result_view
);
1050 // A mouse click within |v32| will target |v32|.
1051 touch_rect
.SetRect(112, 211, 1, 1);
1052 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1053 EXPECT_EQ(v32
, result_view
);
1056 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1057 // The center point of |touch_rect| is equally close to
1058 // the center points of |v4| and |v41|.
1059 touch_rect
.SetRect(310, 210, 80, 80);
1060 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1061 EXPECT_EQ(v41
, result_view
);
1064 // Intersects all of |v4|, |v41|, and |v411| but only covers
1065 // |v411| by at least 60%.
1066 touch_rect
.SetRect(370, 275, 7, 5);
1067 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1068 EXPECT_EQ(v411
, result_view
);
1071 // Intersects |v4| and |v41| but covers neither by at least 60%.
1072 // The center point of |touch_rect| is equally close to the center
1073 // points of |v4| and |v41|.
1074 touch_rect
.SetRect(345, 245, 7, 7);
1075 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1076 EXPECT_EQ(v41
, result_view
);
1079 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1080 // them by at least 60%. The center point of |touch_rect| lies
1082 touch_rect
.SetRect(368, 272, 4, 6);
1083 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1084 EXPECT_EQ(v411
, result_view
);
1087 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1088 // them by at least 60%. The center point of |touch_rect| lies
1090 touch_rect
.SetRect(365, 270, 7, 7);
1091 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1092 EXPECT_EQ(v41
, result_view
);
1095 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1096 // them by at least 60%. The center point of |touch_rect| lies
1098 touch_rect
.SetRect(205, 275, 200, 2);
1099 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1100 EXPECT_EQ(v4
, result_view
);
1103 // Intersects all of |v4|, |v41|, and |v411| but only covers
1104 // |v41| by at least 60%.
1105 touch_rect
.SetRect(310, 210, 61, 66);
1106 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1107 EXPECT_EQ(v41
, result_view
);
1110 // A mouse click within |v411| will target |v411|.
1111 touch_rect
.SetRect(372, 275, 1, 1);
1112 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1113 EXPECT_EQ(v411
, result_view
);
1116 // A mouse click within |v41| will target |v41|.
1117 touch_rect
.SetRect(350, 215, 1, 1);
1118 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1119 EXPECT_EQ(v41
, result_view
);
1122 // Covers |v3|, |v4|, and all of their descendants by at
1123 // least 60%. The center point of |touch_rect| is closest
1124 // to the center point of |v32|.
1125 touch_rect
.SetRect(0, 200, 400, 100);
1126 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1127 EXPECT_EQ(v32
, result_view
);
1130 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1131 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1132 // The center point of |touch_rect| is closest to the center
1133 // point of |root_view|.
1134 touch_rect
.SetRect(110, 15, 375, 450);
1135 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1136 EXPECT_EQ(root_view
, result_view
);
1139 // Covers all views (except |v5| and |v51|) by at least 60%. The
1140 // center point of |touch_rect| is equally close to the center
1141 // points of |v2| and |v32|. One is not a descendant of the other,
1142 // so in this case the view selected is arbitrary (i.e.,
1143 // it depends only on the ordering of nodes in the views
1145 touch_rect
.SetRect(0, 0, 400, 300);
1146 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1147 EXPECT_EQ(v32
, result_view
);
1150 // Covers |v5| and |v51| by at least 60%, and the center point of
1151 // the touch is located within both views. Since both views share
1152 // the same center point, the child view should be selected.
1153 touch_rect
.SetRect(440, 190, 40, 40);
1154 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1155 EXPECT_EQ(v51
, result_view
);
1158 // Covers |v5| and |v51| by at least 60%, but the center point of
1159 // the touch is not located within either view. Since both views
1160 // share the same center point, the child view should be selected.
1161 touch_rect
.SetRect(455, 187, 60, 60);
1162 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1163 EXPECT_EQ(v51
, result_view
);
1166 // Covers neither |v5| nor |v51| by at least 60%, but the center
1167 // of the touch is located within |v51|.
1168 touch_rect
.SetRect(450, 197, 10, 10);
1169 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1170 EXPECT_EQ(v51
, result_view
);
1173 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1174 // The center point is located outside of both views.
1175 touch_rect
.SetRect(433, 180, 24, 24);
1176 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1177 EXPECT_EQ(root_view
, result_view
);
1180 // Only intersects |v5| but does not cover it by at least 60%. The
1181 // center point of the touch region is located within |v5|.
1182 touch_rect
.SetRect(449, 196, 3, 3);
1183 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1184 EXPECT_EQ(v5
, result_view
);
1187 // A mouse click within |v5| (but not |v51|) should target |v5|.
1188 touch_rect
.SetRect(462, 199, 1, 1);
1189 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1190 EXPECT_EQ(v5
, result_view
);
1193 // A mouse click |v5| and |v51| should target the child view.
1194 touch_rect
.SetRect(452, 226, 1, 1);
1195 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1196 EXPECT_EQ(v51
, result_view
);
1199 // A mouse click on the center of |v5| and |v51| should target
1201 touch_rect
.SetRect(465, 215, 1, 1);
1202 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1203 EXPECT_EQ(v51
, result_view
);
1209 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1210 Widget
* widget
= new Widget
;
1211 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1212 widget
->Init(params
);
1213 View
* root_view
= widget
->GetRootView();
1214 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1216 // Have this hierarchy of views (the coords here are in root coord):
1217 // v1 (0, 0, 100, 100)
1218 // - v11 (0, 0, 20, 30)
1219 // - v111 (5, 5, 5, 15)
1220 // - v12 (50, 10, 30, 90)
1221 // - v121 (60, 20, 10, 10)
1222 // v2 (105, 0, 100, 100)
1223 // - v21 (120, 10, 50, 20)
1225 TestView
* v1
= new TestView
;
1226 v1
->SetBounds(0, 0, 100, 100);
1227 root_view
->AddChildView(v1
);
1228 v1
->set_notify_enter_exit_on_child(true);
1230 TestView
* v11
= new TestView
;
1231 v11
->SetBounds(0, 0, 20, 30);
1232 v1
->AddChildView(v11
);
1234 TestView
* v111
= new TestView
;
1235 v111
->SetBounds(5, 5, 5, 15);
1236 v11
->AddChildView(v111
);
1238 TestView
* v12
= new TestView
;
1239 v12
->SetBounds(50, 10, 30, 90);
1240 v1
->AddChildView(v12
);
1242 TestView
* v121
= new TestView
;
1243 v121
->SetBounds(10, 10, 10, 10);
1244 v12
->AddChildView(v121
);
1246 TestView
* v2
= new TestView
;
1247 v2
->SetBounds(105, 0, 100, 100);
1248 root_view
->AddChildView(v2
);
1250 TestView
* v21
= new TestView
;
1251 v21
->SetBounds(15, 10, 50, 20);
1252 v2
->AddChildView(v21
);
1262 // Move the mouse in v111.
1263 gfx::Point
p1(6, 6);
1264 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, 0, 0);
1265 root_view
->OnMouseMoved(move1
);
1266 EXPECT_TRUE(v111
->received_mouse_enter_
);
1267 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1268 EXPECT_TRUE(v1
->received_mouse_enter_
);
1273 // Now, move into v121.
1274 gfx::Point
p2(65, 21);
1275 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, 0, 0);
1276 root_view
->OnMouseMoved(move2
);
1277 EXPECT_TRUE(v111
->received_mouse_exit_
);
1278 EXPECT_TRUE(v121
->received_mouse_enter_
);
1279 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1284 // Now, move into v11.
1285 gfx::Point
p3(1, 1);
1286 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, 0, 0);
1287 root_view
->OnMouseMoved(move3
);
1288 EXPECT_TRUE(v121
->received_mouse_exit_
);
1289 EXPECT_TRUE(v11
->received_mouse_enter_
);
1290 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1296 gfx::Point
p4(121, 15);
1297 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, 0, 0);
1298 root_view
->OnMouseMoved(move4
);
1299 EXPECT_TRUE(v21
->received_mouse_enter_
);
1300 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1301 EXPECT_TRUE(v11
->received_mouse_exit_
);
1302 EXPECT_TRUE(v1
->received_mouse_exit_
);
1309 gfx::Point
p5(21, 0);
1310 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, 0, 0);
1311 root_view
->OnMouseMoved(move5
);
1312 EXPECT_TRUE(v21
->received_mouse_exit_
);
1313 EXPECT_TRUE(v1
->received_mouse_enter_
);
1318 // Now, move into v11.
1319 gfx::Point
p6(15, 15);
1320 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, 0, 0);
1321 root_view
->OnMouseMoved(mouse6
);
1322 EXPECT_TRUE(v11
->received_mouse_enter_
);
1323 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1328 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1329 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1330 // when the mouse leaves v11.
1331 gfx::Point
p7(21, 0);
1332 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, 0, 0);
1333 root_view
->OnMouseMoved(mouse7
);
1334 EXPECT_TRUE(v11
->received_mouse_exit_
);
1335 EXPECT_FALSE(v1
->received_mouse_enter_
);
1340 TEST_F(ViewTest
, Textfield
) {
1341 const base::string16 kText
= ASCIIToUTF16(
1342 "Reality is that which, when you stop believing it, doesn't go away.");
1343 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1344 const base::string16 kEmptyString
;
1346 Widget
* widget
= new Widget
;
1347 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1348 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1349 widget
->Init(params
);
1350 View
* root_view
= widget
->GetRootView();
1352 Textfield
* textfield
= new Textfield();
1353 root_view
->AddChildView(textfield
);
1355 // Test setting, appending text.
1356 textfield
->SetText(kText
);
1357 EXPECT_EQ(kText
, textfield
->text());
1358 textfield
->AppendText(kExtraText
);
1359 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1360 textfield
->SetText(base::string16());
1361 EXPECT_EQ(kEmptyString
, textfield
->text());
1363 // Test selection related methods.
1364 textfield
->SetText(kText
);
1365 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1366 textfield
->SelectAll(false);
1367 EXPECT_EQ(kText
, textfield
->text());
1368 textfield
->ClearSelection();
1369 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1374 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1375 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1376 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1377 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1378 const base::string16 kPasswordText
=
1379 ASCIIToUTF16("Password! ** Secret stuff **");
1381 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1383 Widget
* widget
= new Widget
;
1384 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1385 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1386 widget
->Init(params
);
1387 View
* root_view
= widget
->GetRootView();
1389 Textfield
* normal
= new Textfield();
1390 Textfield
* read_only
= new Textfield();
1391 read_only
->SetReadOnly(true);
1392 Textfield
* password
= new Textfield();
1393 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1395 root_view
->AddChildView(normal
);
1396 root_view
->AddChildView(read_only
);
1397 root_view
->AddChildView(password
);
1399 normal
->SetText(kNormalText
);
1400 read_only
->SetText(kReadOnlyText
);
1401 password
->SetText(kPasswordText
);
1407 normal
->SelectAll(false);
1408 normal
->ExecuteCommand(IDS_APP_CUT
);
1409 base::string16 result
;
1410 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1411 EXPECT_EQ(kNormalText
, result
);
1412 normal
->SetText(kNormalText
); // Let's revert to the original content.
1414 read_only
->SelectAll(false);
1415 read_only
->ExecuteCommand(IDS_APP_CUT
);
1417 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1418 // Cut should have failed, so the clipboard content should not have changed.
1419 EXPECT_EQ(kNormalText
, result
);
1421 password
->SelectAll(false);
1422 password
->ExecuteCommand(IDS_APP_CUT
);
1424 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1425 // Cut should have failed, so the clipboard content should not have changed.
1426 EXPECT_EQ(kNormalText
, result
);
1432 // Start with |read_only| to observe a change in clipboard text.
1433 read_only
->SelectAll(false);
1434 read_only
->ExecuteCommand(IDS_APP_COPY
);
1436 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1437 EXPECT_EQ(kReadOnlyText
, result
);
1439 normal
->SelectAll(false);
1440 normal
->ExecuteCommand(IDS_APP_COPY
);
1442 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1443 EXPECT_EQ(kNormalText
, result
);
1445 password
->SelectAll(false);
1446 password
->ExecuteCommand(IDS_APP_COPY
);
1448 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1449 // Text cannot be copied from an obscured field; the clipboard won't change.
1450 EXPECT_EQ(kNormalText
, result
);
1456 // Attempting to paste kNormalText in a read-only text-field should fail.
1457 read_only
->SelectAll(false);
1458 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1459 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1461 password
->SelectAll(false);
1462 password
->ExecuteCommand(IDS_APP_PASTE
);
1463 EXPECT_EQ(kNormalText
, password
->text());
1465 // Copy from |read_only| to observe a change in the normal textfield text.
1466 read_only
->SelectAll(false);
1467 read_only
->ExecuteCommand(IDS_APP_COPY
);
1468 normal
->SelectAll(false);
1469 normal
->ExecuteCommand(IDS_APP_PASTE
);
1470 EXPECT_EQ(kReadOnlyText
, normal
->text());
1474 ////////////////////////////////////////////////////////////////////////////////
1476 ////////////////////////////////////////////////////////////////////////////////
1477 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1478 accelerator_count_map_
[accelerator
]++;
1482 // TODO: these tests were initially commented out when getting aura to
1483 // run. Figure out if still valuable and either nuke or fix.
1485 TEST_F(ViewTest
, ActivateAccelerator
) {
1486 // Register a keyboard accelerator before the view is added to a window.
1487 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1488 TestView
* view
= new TestView();
1490 view
->AddAccelerator(return_accelerator
);
1491 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1493 // Create a window and add the view as its child.
1494 scoped_ptr
<Widget
> widget(new Widget
);
1495 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1496 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1497 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1498 widget
->Init(params
);
1499 View
* root
= widget
->GetRootView();
1500 root
->AddChildView(view
);
1503 // Get the focus manager.
1504 FocusManager
* focus_manager
= widget
->GetFocusManager();
1505 ASSERT_TRUE(focus_manager
);
1507 // Hit the return key and see if it takes effect.
1508 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1509 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1511 // Hit the escape key. Nothing should happen.
1512 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1513 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1514 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1515 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1517 // Now register the escape key and hit it again.
1518 view
->AddAccelerator(escape_accelerator
);
1519 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1520 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1521 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1523 // Remove the return key accelerator.
1524 view
->RemoveAccelerator(return_accelerator
);
1525 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1526 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1527 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1529 // Add it again. Hit the return key and the escape key.
1530 view
->AddAccelerator(return_accelerator
);
1531 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1532 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1533 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1534 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1535 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1536 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1538 // Remove all the accelerators.
1539 view
->ResetAccelerators();
1540 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1541 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1542 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1543 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1544 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1545 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1550 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1551 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1552 TestView
* view
= new TestView();
1554 view
->AddAccelerator(return_accelerator
);
1555 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1557 scoped_ptr
<Widget
> widget(new Widget
);
1558 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1559 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1560 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1561 widget
->Init(params
);
1562 View
* root
= widget
->GetRootView();
1563 root
->AddChildView(view
);
1566 FocusManager
* focus_manager
= widget
->GetFocusManager();
1567 ASSERT_TRUE(focus_manager
);
1569 view
->SetVisible(false);
1570 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1572 view
->SetVisible(true);
1573 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1578 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1579 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1580 TestView
* view
= new TestView();
1582 view
->AddAccelerator(return_accelerator
);
1583 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1585 scoped_ptr
<Widget
> widget(new Widget
);
1586 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1587 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1588 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1589 widget
->Init(params
);
1590 View
* root
= widget
->GetRootView();
1591 root
->AddChildView(view
);
1593 FocusManager
* focus_manager
= widget
->GetFocusManager();
1594 ASSERT_TRUE(focus_manager
);
1596 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1597 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1600 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1601 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1604 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1605 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1610 ////////////////////////////////////////////////////////////////////////////////
1611 // Mouse-wheel message rerouting
1612 ////////////////////////////////////////////////////////////////////////////////
1613 class ScrollableTestView
: public View
{
1615 ScrollableTestView() { }
1617 virtual gfx::Size
GetPreferredSize() {
1618 return gfx::Size(100, 10000);
1621 virtual void Layout() {
1622 SizeToPreferredSize();
1626 class TestViewWithControls
: public View
{
1628 TestViewWithControls() {
1629 text_field_
= new Textfield();
1630 AddChildView(text_field_
);
1633 Textfield
* text_field_
;
1636 class SimpleWidgetDelegate
: public WidgetDelegate
{
1638 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1640 virtual void DeleteDelegate() { delete this; }
1642 virtual View
* GetContentsView() { return contents_
; }
1644 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1645 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1651 // Tests that the mouse-wheel messages are correctly rerouted to the window
1653 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1655 // Note that this fails for a variety of reasons:
1656 // - focused view is apparently reset across window activations and never
1657 // properly restored
1658 // - this test depends on you not having any other window visible open under the
1659 // area that it opens the test windows. --beng
1660 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1661 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1662 Widget
* window1
= Widget::CreateWindowWithBounds(
1663 new SimpleWidgetDelegate(view_with_controls
),
1664 gfx::Rect(0, 0, 100, 100));
1666 ScrollView
* scroll_view
= new ScrollView();
1667 scroll_view
->SetContents(new ScrollableTestView());
1668 Widget
* window2
= Widget::CreateWindowWithBounds(
1669 new SimpleWidgetDelegate(scroll_view
),
1670 gfx::Rect(200, 200, 100, 100));
1672 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1674 // Make the window1 active, as this is what it would be in real-world.
1675 window1
->Activate();
1677 // Let's send a mouse-wheel message to the different controls and check that
1678 // it is rerouted to the window under the mouse (effectively scrolling the
1681 // First to the Window's HWND.
1682 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1683 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1684 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1686 window1
->CloseNow();
1687 window2
->CloseNow();
1691 ////////////////////////////////////////////////////////////////////////////////
1692 // Native view hierachy
1693 ////////////////////////////////////////////////////////////////////////////////
1694 class ToplevelWidgetObserverView
: public View
{
1696 ToplevelWidgetObserverView() : toplevel_(NULL
) {
1698 virtual ~ToplevelWidgetObserverView() {
1702 virtual void ViewHierarchyChanged(
1703 const ViewHierarchyChangedDetails
& details
) OVERRIDE
{
1704 if (details
.is_add
) {
1705 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1710 virtual void NativeViewHierarchyChanged() OVERRIDE
{
1711 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
1714 Widget
* toplevel() { return toplevel_
; }
1719 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
1722 // Test that a view can track the current top level widget by overriding
1723 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1724 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
1725 scoped_ptr
<Widget
> toplevel1(new Widget
);
1726 Widget::InitParams toplevel1_params
=
1727 CreateParams(Widget::InitParams::TYPE_POPUP
);
1728 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1729 toplevel1
->Init(toplevel1_params
);
1731 scoped_ptr
<Widget
> toplevel2(new Widget
);
1732 Widget::InitParams toplevel2_params
=
1733 CreateParams(Widget::InitParams::TYPE_POPUP
);
1734 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1735 toplevel2
->Init(toplevel2_params
);
1737 Widget
* child
= new Widget
;
1738 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
1739 child_params
.parent
= toplevel1
->GetNativeView();
1740 child
->Init(child_params
);
1742 ToplevelWidgetObserverView
* observer_view
=
1743 new ToplevelWidgetObserverView();
1744 EXPECT_EQ(NULL
, observer_view
->toplevel());
1746 child
->SetContentsView(observer_view
);
1747 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
1749 Widget::ReparentNativeView(child
->GetNativeView(),
1750 toplevel2
->GetNativeView());
1751 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
1753 observer_view
->parent()->RemoveChildView(observer_view
);
1754 EXPECT_EQ(NULL
, observer_view
->toplevel());
1756 // Make |observer_view| |child|'s contents view again so that it gets deleted
1758 child
->SetContentsView(observer_view
);
1761 ////////////////////////////////////////////////////////////////////////////////
1763 ////////////////////////////////////////////////////////////////////////////////
1765 class TransformPaintView
: public TestView
{
1767 TransformPaintView() {}
1768 virtual ~TransformPaintView() {}
1770 void ClearScheduledPaintRect() {
1771 scheduled_paint_rect_
= gfx::Rect();
1774 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1776 // Overridden from View:
1777 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
{
1778 gfx::Rect xrect
= ConvertRectToParent(rect
);
1779 scheduled_paint_rect_
.Union(xrect
);
1783 gfx::Rect scheduled_paint_rect_
;
1785 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
1788 TEST_F(ViewTest
, TransformPaint
) {
1789 TransformPaintView
* v1
= new TransformPaintView();
1790 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1792 TestView
* v2
= new TestView();
1793 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1795 Widget
* widget
= new Widget
;
1796 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1797 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1798 widget
->Init(params
);
1800 View
* root
= widget
->GetRootView();
1802 root
->AddChildView(v1
);
1803 v1
->AddChildView(v2
);
1805 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1806 v1
->ClearScheduledPaintRect();
1807 v2
->SchedulePaint();
1809 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
1811 // Rotate |v1| counter-clockwise.
1812 gfx::Transform transform
;
1813 RotateCounterclockwise(&transform
);
1814 transform
.matrix().set(1, 3, 500.0);
1815 v1
->SetTransform(transform
);
1817 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1819 v1
->ClearScheduledPaintRect();
1820 v2
->SchedulePaint();
1822 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
1827 TEST_F(ViewTest
, TransformEvent
) {
1828 TestView
* v1
= new TestView();
1829 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1831 TestView
* v2
= new TestView();
1832 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1834 Widget
* widget
= new Widget
;
1835 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1836 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1837 widget
->Init(params
);
1838 View
* root
= widget
->GetRootView();
1840 root
->AddChildView(v1
);
1841 v1
->AddChildView(v2
);
1843 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1845 // Rotate |v1| counter-clockwise.
1846 gfx::Transform
transform(v1
->GetTransform());
1847 RotateCounterclockwise(&transform
);
1848 transform
.matrix().set(1, 3, 500.0);
1849 v1
->SetTransform(transform
);
1851 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1855 gfx::Point
p1(110, 210);
1856 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
1857 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1858 root
->OnMousePressed(pressed
);
1859 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1860 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1861 EXPECT_EQ(190, v2
->location_
.x());
1862 EXPECT_EQ(10, v2
->location_
.y());
1864 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0,
1866 root
->OnMouseReleased(released
);
1868 // Now rotate |v2| inside |v1| clockwise.
1869 transform
= v2
->GetTransform();
1870 RotateClockwise(&transform
);
1871 transform
.matrix().set(0, 3, 100.f
);
1872 v2
->SetTransform(transform
);
1874 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1875 // (300, 400) in |root|.
1880 gfx::Point
point2(110, 320);
1881 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
,
1882 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1883 root
->OnMousePressed(p2
);
1884 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1885 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1886 EXPECT_EQ(10, v2
->location_
.x());
1887 EXPECT_EQ(20, v2
->location_
.y());
1889 root
->OnMouseReleased(released
);
1891 v1
->SetTransform(gfx::Transform());
1892 v2
->SetTransform(gfx::Transform());
1894 TestView
* v3
= new TestView();
1895 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1896 v2
->AddChildView(v3
);
1898 // Rotate |v3| clockwise with respect to |v2|.
1899 transform
= v1
->GetTransform();
1900 RotateClockwise(&transform
);
1901 transform
.matrix().set(0, 3, 30.f
);
1902 v3
->SetTransform(transform
);
1904 // Scale |v2| with respect to |v1| along both axis.
1905 transform
= v2
->GetTransform();
1906 transform
.matrix().set(0, 0, 0.8f
);
1907 transform
.matrix().set(1, 1, 0.5f
);
1908 v2
->SetTransform(transform
);
1910 // |v3| occupies (108, 105) to (132, 115) in |root|.
1916 gfx::Point
point(112, 110);
1917 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
,
1918 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1919 root
->OnMousePressed(p3
);
1921 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1922 EXPECT_EQ(10, v3
->location_
.x());
1923 EXPECT_EQ(25, v3
->location_
.y());
1925 root
->OnMouseReleased(released
);
1927 v1
->SetTransform(gfx::Transform());
1928 v2
->SetTransform(gfx::Transform());
1929 v3
->SetTransform(gfx::Transform());
1935 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1936 transform
= v3
->GetTransform();
1937 RotateClockwise(&transform
);
1938 transform
.matrix().set(0, 3, 30.f
);
1939 // Rotation sets some scaling transformation. Using SetScale would overwrite
1940 // that and pollute the rotation. So combine the scaling with the existing
1942 gfx::Transform scale
;
1943 scale
.Scale(0.8f
, 0.5f
);
1944 transform
.ConcatTransform(scale
);
1945 v3
->SetTransform(transform
);
1947 // Translate |v2| with respect to |v1|.
1948 transform
= v2
->GetTransform();
1949 transform
.matrix().set(0, 3, 10.f
);
1950 transform
.matrix().set(1, 3, 10.f
);
1951 v2
->SetTransform(transform
);
1953 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1955 gfx::Point
point3(124, 125);
1956 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
,
1957 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
1958 root
->OnMousePressed(p4
);
1960 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1961 EXPECT_EQ(10, v3
->location_
.x());
1962 EXPECT_EQ(25, v3
->location_
.y());
1964 root
->OnMouseReleased(released
);
1969 TEST_F(ViewTest
, TransformVisibleBound
) {
1970 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1972 scoped_ptr
<Widget
> widget(new Widget
);
1973 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1974 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1975 params
.bounds
= viewport_bounds
;
1976 widget
->Init(params
);
1977 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1979 View
* viewport
= new View
;
1980 widget
->SetContentsView(viewport
);
1981 View
* contents
= new View
;
1982 viewport
->AddChildView(contents
);
1983 viewport
->SetBoundsRect(viewport_bounds
);
1984 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1986 View
* child
= new View
;
1987 contents
->AddChildView(child
);
1988 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
1989 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
1991 // Rotate |child| counter-clockwise
1992 gfx::Transform transform
;
1993 RotateCounterclockwise(&transform
);
1994 transform
.matrix().set(1, 3, 50.f
);
1995 child
->SetTransform(transform
);
1996 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
2001 ////////////////////////////////////////////////////////////////////////////////
2002 // OnVisibleBoundsChanged()
2004 class VisibleBoundsView
: public View
{
2006 VisibleBoundsView() : received_notification_(false) {}
2007 virtual ~VisibleBoundsView() {}
2009 bool received_notification() const { return received_notification_
; }
2010 void set_received_notification(bool received
) {
2011 received_notification_
= received
;
2015 // Overridden from View:
2016 virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE
{
2019 virtual void OnVisibleBoundsChanged() OVERRIDE
{
2020 received_notification_
= true;
2023 bool received_notification_
;
2025 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2028 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2029 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2031 scoped_ptr
<Widget
> widget(new Widget
);
2032 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2033 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2034 params
.bounds
= viewport_bounds
;
2035 widget
->Init(params
);
2036 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2038 View
* viewport
= new View
;
2039 widget
->SetContentsView(viewport
);
2040 View
* contents
= new View
;
2041 viewport
->AddChildView(contents
);
2042 viewport
->SetBoundsRect(viewport_bounds
);
2043 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2045 // Create a view that cares about visible bounds notifications, and position
2046 // it just outside the visible bounds of the viewport.
2047 VisibleBoundsView
* child
= new VisibleBoundsView
;
2048 contents
->AddChildView(child
);
2049 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2051 // The child bound should be fully clipped.
2052 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2054 // Now scroll the contents, but not enough to make the child visible.
2055 contents
->SetY(contents
->y() - 1);
2057 // We should have received the notification since the visible bounds may have
2058 // changed (even though they didn't).
2059 EXPECT_TRUE(child
->received_notification());
2060 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2061 child
->set_received_notification(false);
2063 // Now scroll the contents, this time by enough to make the child visible by
2065 contents
->SetY(contents
->y() - 10);
2066 EXPECT_TRUE(child
->received_notification());
2067 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2068 child
->set_received_notification(false);
2073 TEST_F(ViewTest
, SetBoundsPaint
) {
2075 TestView
* child_view
= new TestView
;
2077 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2078 top_view
.scheduled_paint_rects_
.clear();
2079 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2080 top_view
.AddChildView(child_view
);
2082 top_view
.scheduled_paint_rects_
.clear();
2083 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2084 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2086 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2087 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2088 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2089 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2092 // Assertions around painting and focus gain/lost.
2093 TEST_F(ViewTest
, FocusBlurPaints
) {
2094 TestView parent_view
;
2095 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
2097 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2099 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2100 parent_view
.AddChildView(child_view1
);
2102 parent_view
.scheduled_paint_rects_
.clear();
2103 child_view1
->scheduled_paint_rects_
.clear();
2105 // Focus change shouldn't trigger paints.
2106 child_view1
->DoFocus();
2108 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2109 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2111 child_view1
->DoBlur();
2112 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2113 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2116 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2117 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
2120 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2121 view
.InvalidateLayout();
2122 view
.scheduled_paint_rects_
.clear();
2123 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2124 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
2127 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2128 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
2129 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2131 // We have to put the View hierarchy into a Widget or no paints will be
2133 scoped_ptr
<Widget
> widget(new Widget
);
2134 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2135 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2136 params
.bounds
= viewport_bounds
;
2137 widget
->Init(params
);
2138 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2140 TestView
* parent_view
= new TestView
;
2141 widget
->SetContentsView(parent_view
);
2142 parent_view
->SetBoundsRect(viewport_bounds
);
2143 parent_view
->scheduled_paint_rects_
.clear();
2145 View
* child_view
= new View
;
2146 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2147 parent_view
->AddChildView(child_view
);
2148 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2149 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2151 parent_view
->scheduled_paint_rects_
.clear();
2152 parent_view
->RemoveChildView(child_view
);
2153 scoped_ptr
<View
> child_deleter(child_view
);
2154 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2155 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2160 // Tests conversion methods with a transform.
2161 TEST_F(ViewTest
, ConversionsWithTransform
) {
2164 // View hierarchy used to test scale transforms.
2165 TestView
* child
= new TestView
;
2166 TestView
* child_child
= new TestView
;
2168 // View used to test a rotation transform.
2169 TestView
* child_2
= new TestView
;
2171 top_view
.AddChildView(child
);
2172 child
->AddChildView(child_child
);
2174 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2176 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2177 gfx::Transform transform
;
2178 transform
.Scale(3.0, 4.0);
2179 child
->SetTransform(transform
);
2181 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2182 transform
.MakeIdentity();
2183 transform
.Scale(5.0, 7.0);
2184 child_child
->SetTransform(transform
);
2186 top_view
.AddChildView(child_2
);
2187 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2188 transform
.MakeIdentity();
2189 RotateClockwise(&transform
);
2190 child_2
->SetTransform(transform
);
2192 // Sanity check to make sure basic transforms act as expected.
2194 gfx::Transform transform
;
2195 transform
.Translate(110.0, -110.0);
2196 transform
.Scale(100.0, 55.0);
2197 transform
.Translate(1.0, 1.0);
2199 // convert to a 3x3 matrix.
2200 const SkMatrix
& matrix
= transform
.matrix();
2202 EXPECT_EQ(210, matrix
.getTranslateX());
2203 EXPECT_EQ(-55, matrix
.getTranslateY());
2204 EXPECT_EQ(100, matrix
.getScaleX());
2205 EXPECT_EQ(55, matrix
.getScaleY());
2206 EXPECT_EQ(0, matrix
.getSkewX());
2207 EXPECT_EQ(0, matrix
.getSkewY());
2211 gfx::Transform transform
;
2212 transform
.Translate(1.0, 1.0);
2214 t2
.Scale(100.0, 55.0);
2216 t3
.Translate(110.0, -110.0);
2217 transform
.ConcatTransform(t2
);
2218 transform
.ConcatTransform(t3
);
2220 // convert to a 3x3 matrix
2221 const SkMatrix
& matrix
= transform
.matrix();
2223 EXPECT_EQ(210, matrix
.getTranslateX());
2224 EXPECT_EQ(-55, matrix
.getTranslateY());
2225 EXPECT_EQ(100, matrix
.getScaleX());
2226 EXPECT_EQ(55, matrix
.getScaleY());
2227 EXPECT_EQ(0, matrix
.getSkewX());
2228 EXPECT_EQ(0, matrix
.getSkewY());
2231 // Conversions from child->top and top->child.
2233 gfx::Point
point(5, 5);
2234 View::ConvertPointToTarget(child
, &top_view
, &point
);
2235 EXPECT_EQ(22, point
.x());
2236 EXPECT_EQ(39, point
.y());
2238 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2239 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2240 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2241 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2242 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2243 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2245 point
.SetPoint(22, 39);
2246 View::ConvertPointToTarget(&top_view
, child
, &point
);
2247 EXPECT_EQ(5, point
.x());
2248 EXPECT_EQ(5, point
.y());
2250 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2251 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2252 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2253 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2254 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2255 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2258 // Conversions from child_child->top and top->child_child.
2260 gfx::Point
point(5, 5);
2261 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2262 EXPECT_EQ(133, point
.x());
2263 EXPECT_EQ(211, point
.y());
2265 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2266 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2267 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2268 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2269 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2270 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2272 point
.SetPoint(133, 211);
2273 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2274 EXPECT_EQ(5, point
.x());
2275 EXPECT_EQ(5, point
.y());
2277 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2278 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2279 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2280 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2281 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2282 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2285 // Conversions from child_child->child and child->child_child
2287 gfx::Point
point(5, 5);
2288 View::ConvertPointToTarget(child_child
, child
, &point
);
2289 EXPECT_EQ(42, point
.x());
2290 EXPECT_EQ(48, point
.y());
2292 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2293 View::ConvertRectToTarget(child_child
, child
, &rect
);
2294 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2295 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2296 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2297 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2299 point
.SetPoint(42, 48);
2300 View::ConvertPointToTarget(child
, child_child
, &point
);
2301 EXPECT_EQ(5, point
.x());
2302 EXPECT_EQ(5, point
.y());
2304 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2305 View::ConvertRectToTarget(child
, child_child
, &rect
);
2306 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2307 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2308 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2309 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2312 // Conversions from top_view to child with a value that should be negative.
2313 // This ensures we don't round up with negative numbers.
2315 gfx::Point
point(6, 18);
2316 View::ConvertPointToTarget(&top_view
, child
, &point
);
2317 EXPECT_EQ(-1, point
.x());
2318 EXPECT_EQ(-1, point
.y());
2320 float error
= 0.01f
;
2321 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2322 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2323 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2324 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2325 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2326 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2329 // Rect conversions from top_view->child_2 and child_2->top_view.
2331 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2332 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2333 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2334 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2335 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2336 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2338 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2339 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2340 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2341 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2342 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2343 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2347 // Tests conversion methods to and from screen coordinates.
2348 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2349 scoped_ptr
<Widget
> widget(new Widget
);
2350 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2351 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2352 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2353 widget
->Init(params
);
2355 View
* child
= new View
;
2356 widget
->GetRootView()->AddChildView(child
);
2357 child
->SetBounds(10, 10, 100, 200);
2360 child
->SetTransform(t
);
2362 gfx::Point
point_in_screen(100, 90);
2363 gfx::Point
point_in_child(80,60);
2365 gfx::Point point
= point_in_screen
;
2366 View::ConvertPointFromScreen(child
, &point
);
2367 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2369 View::ConvertPointToScreen(child
, &point
);
2370 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2373 // Tests conversion methods for rectangles.
2374 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2375 scoped_ptr
<Widget
> widget(new Widget
);
2376 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2377 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2378 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2379 widget
->Init(params
);
2380 View
* root
= widget
->GetRootView();
2382 TestView
* v1
= new TestView
;
2383 TestView
* v2
= new TestView
;
2384 root
->AddChildView(v1
);
2385 v1
->AddChildView(v2
);
2387 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2388 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2390 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2391 gfx::Rect
rect(5, 5, 15, 40);
2392 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2393 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2397 RotateCounterclockwise(&t2
);
2398 t2
.matrix().set(1, 3, 100.f
);
2399 v2
->SetTransform(t2
);
2401 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2402 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2403 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2408 v1
->SetTransform(t1
);
2410 // The rectangle should remain the same for |v1|.
2411 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2413 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2414 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2415 v2
->ConvertRectToWidget(rect
).ToString());
2420 class ObserverView
: public View
{
2423 virtual ~ObserverView();
2425 void ResetTestState();
2427 bool has_add_details() const { return has_add_details_
; }
2428 bool has_remove_details() const { return has_remove_details_
; }
2430 const ViewHierarchyChangedDetails
& add_details() const {
2431 return add_details_
;
2434 const ViewHierarchyChangedDetails
& remove_details() const {
2435 return remove_details_
;
2440 virtual void ViewHierarchyChanged(
2441 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
2443 bool has_add_details_
;
2444 bool has_remove_details_
;
2445 ViewHierarchyChangedDetails add_details_
;
2446 ViewHierarchyChangedDetails remove_details_
;
2448 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2451 ObserverView::ObserverView()
2452 : has_add_details_(false),
2453 has_remove_details_(false) {
2456 ObserverView::~ObserverView() {}
2458 void ObserverView::ResetTestState() {
2459 has_add_details_
= false;
2460 has_remove_details_
= false;
2461 add_details_
= ViewHierarchyChangedDetails();
2462 remove_details_
= ViewHierarchyChangedDetails();
2465 void ObserverView::ViewHierarchyChanged(
2466 const ViewHierarchyChangedDetails
& details
) {
2467 if (details
.is_add
) {
2468 has_add_details_
= true;
2469 add_details_
= details
;
2471 has_remove_details_
= true;
2472 remove_details_
= details
;
2476 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2477 // a child view is added or removed to all the views in the hierarchy (up and
2479 // The tree looks like this:
2483 // +-- v4 (starts here, then get reparented to v1)
2484 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2487 ObserverView
* v3
= new ObserverView();
2489 // Add |v3| to |v2|.
2490 scoped_ptr
<ObserverView
> v2(new ObserverView());
2491 v2
->AddChildView(v3
);
2493 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2495 EXPECT_TRUE(v2
->has_add_details());
2496 EXPECT_FALSE(v2
->has_remove_details());
2497 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2498 EXPECT_EQ(v3
, v2
->add_details().child
);
2499 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2501 EXPECT_TRUE(v3
->has_add_details());
2502 EXPECT_FALSE(v3
->has_remove_details());
2503 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2504 EXPECT_EQ(v3
, v3
->add_details().child
);
2505 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2507 // Reset everything to the initial state.
2508 v2
->ResetTestState();
2509 v3
->ResetTestState();
2512 v1
.AddChildView(v2
.get());
2514 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2515 // Make sure all the views (v1, v2, v3) received _that_ information.
2516 EXPECT_TRUE(v1
.has_add_details());
2517 EXPECT_FALSE(v1
.has_remove_details());
2518 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2519 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2520 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2522 EXPECT_TRUE(v2
->has_add_details());
2523 EXPECT_FALSE(v2
->has_remove_details());
2524 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2525 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2526 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2528 EXPECT_TRUE(v3
->has_add_details());
2529 EXPECT_FALSE(v3
->has_remove_details());
2530 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2531 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2532 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2534 // Reset everything to the initial state.
2535 v1
.ResetTestState();
2536 v2
->ResetTestState();
2537 v3
->ResetTestState();
2539 // Remove |v2| from |v1|.
2540 v1
.RemoveChildView(v2
.get());
2542 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2543 // Make sure all the views (v1, v2, v3) received _that_ information.
2544 EXPECT_FALSE(v1
.has_add_details());
2545 EXPECT_TRUE(v1
.has_remove_details());
2546 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2547 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2548 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2550 EXPECT_FALSE(v2
->has_add_details());
2551 EXPECT_TRUE(v2
->has_remove_details());
2552 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2553 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2554 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2556 EXPECT_FALSE(v3
->has_add_details());
2557 EXPECT_TRUE(v3
->has_remove_details());
2558 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2559 EXPECT_EQ(v3
, v3
->remove_details().child
);
2560 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2562 // Verifies notifications when reparenting a view.
2563 ObserverView
* v4
= new ObserverView();
2564 // Add |v4| to |v2|.
2565 v2
->AddChildView(v4
);
2567 // Reset everything to the initial state.
2568 v1
.ResetTestState();
2569 v2
->ResetTestState();
2570 v3
->ResetTestState();
2571 v4
->ResetTestState();
2573 // Reparent |v4| to |v1|.
2574 v1
.AddChildView(v4
);
2576 // Verifies that all views receive the correct information for all the child,
2577 // parent and move views.
2579 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2580 EXPECT_TRUE(v1
.has_add_details());
2581 EXPECT_FALSE(v1
.has_remove_details());
2582 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2583 EXPECT_EQ(v4
, v1
.add_details().child
);
2584 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2586 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2588 EXPECT_FALSE(v2
->has_add_details());
2589 EXPECT_TRUE(v2
->has_remove_details());
2590 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2591 EXPECT_EQ(v4
, v2
->remove_details().child
);
2592 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2594 // |v3| is not impacted by this operation, and hence receives no notification.
2595 EXPECT_FALSE(v3
->has_add_details());
2596 EXPECT_FALSE(v3
->has_remove_details());
2598 // |v4| is the reparented child, so it receives notifications for the remove
2599 // and then the add. |v2| is its old parent, |v1| is its new parent.
2600 EXPECT_TRUE(v4
->has_remove_details());
2601 EXPECT_TRUE(v4
->has_add_details());
2602 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2603 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2604 EXPECT_EQ(v4
, v4
->add_details().child
);
2605 EXPECT_EQ(v4
, v4
->remove_details().child
);
2606 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2607 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2610 // Verifies if the child views added under the root are all deleted when calling
2611 // RemoveAllChildViews.
2612 // The tree looks like this:
2621 TEST_F(ViewTest
, RemoveAllChildViews
) {
2624 View
* child1
= new View
;
2625 root
.AddChildView(child1
);
2627 for (int i
= 0; i
< 2; ++i
)
2628 root
.AddChildView(new View
);
2630 View
* foo
= new View
;
2631 child1
->AddChildView(foo
);
2633 // Add some nodes to |foo|.
2634 for (int i
= 0; i
< 3; ++i
)
2635 foo
->AddChildView(new View
);
2637 EXPECT_EQ(3, root
.child_count());
2638 EXPECT_EQ(1, child1
->child_count());
2639 EXPECT_EQ(3, foo
->child_count());
2641 // Now remove all child views from root.
2642 root
.RemoveAllChildViews(true);
2644 EXPECT_EQ(0, root
.child_count());
2645 EXPECT_FALSE(root
.has_children());
2648 TEST_F(ViewTest
, Contains
) {
2650 View
* v2
= new View
;
2651 View
* v3
= new View
;
2653 v1
.AddChildView(v2
);
2654 v2
->AddChildView(v3
);
2656 EXPECT_FALSE(v1
.Contains(NULL
));
2657 EXPECT_TRUE(v1
.Contains(&v1
));
2658 EXPECT_TRUE(v1
.Contains(v2
));
2659 EXPECT_TRUE(v1
.Contains(v3
));
2661 EXPECT_FALSE(v2
->Contains(NULL
));
2662 EXPECT_TRUE(v2
->Contains(v2
));
2663 EXPECT_FALSE(v2
->Contains(&v1
));
2664 EXPECT_TRUE(v2
->Contains(v3
));
2666 EXPECT_FALSE(v3
->Contains(NULL
));
2667 EXPECT_TRUE(v3
->Contains(v3
));
2668 EXPECT_FALSE(v3
->Contains(&v1
));
2669 EXPECT_FALSE(v3
->Contains(v2
));
2672 // Verifies if GetIndexOf() returns the correct index for the specified child
2674 // The tree looks like this:
2679 TEST_F(ViewTest
, GetIndexOf
) {
2682 View
* child1
= new View
;
2683 root
.AddChildView(child1
);
2685 View
* child2
= new View
;
2686 root
.AddChildView(child2
);
2688 View
* foo1
= new View
;
2689 child1
->AddChildView(foo1
);
2691 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2692 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2693 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2694 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2695 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2697 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2698 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2699 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2700 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2701 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2703 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2704 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2705 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2706 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2707 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2710 // Verifies that the child views can be reordered correctly.
2711 TEST_F(ViewTest
, ReorderChildren
) {
2714 View
* child
= new View();
2715 root
.AddChildView(child
);
2717 View
* foo1
= new View();
2718 child
->AddChildView(foo1
);
2719 View
* foo2
= new View();
2720 child
->AddChildView(foo2
);
2721 View
* foo3
= new View();
2722 child
->AddChildView(foo3
);
2723 foo1
->SetFocusable(true);
2724 foo2
->SetFocusable(true);
2725 foo3
->SetFocusable(true);
2727 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2728 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2729 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2730 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2731 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2732 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2734 // Move |foo2| at the end.
2735 child
->ReorderChildView(foo2
, -1);
2736 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2737 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2738 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2739 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2740 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2741 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2743 // Move |foo1| at the end.
2744 child
->ReorderChildView(foo1
, -1);
2745 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2746 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2747 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2748 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2749 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2750 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2751 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2753 // Move |foo2| to the front.
2754 child
->ReorderChildView(foo2
, 0);
2755 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2756 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2757 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2758 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2759 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2760 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2761 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2764 // Verifies that GetViewByID returns the correctly child view from the specified
2766 // The tree looks like this:
2771 TEST_F(ViewTest
, GetViewByID
) {
2773 const int kV1ID
= 1;
2777 const int kV2ID
= 2;
2781 const int kV3ID
= 3;
2785 const int kV4ID
= 4;
2788 const int kV5ID
= 5;
2790 v1
.AddChildView(&v2
);
2791 v2
.AddChildView(&v3
);
2792 v2
.AddChildView(&v4
);
2794 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2795 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2796 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2798 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2799 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2801 const int kGroup
= 1;
2802 v3
.SetGroup(kGroup
);
2803 v4
.SetGroup(kGroup
);
2806 v1
.GetViewsInGroup(kGroup
, &views
);
2807 EXPECT_EQ(2U, views
.size());
2809 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2810 EXPECT_NE(views
.end(), i
);
2812 i
= std::find(views
.begin(), views
.end(), &v4
);
2813 EXPECT_NE(views
.end(), i
);
2816 TEST_F(ViewTest
, AddExistingChild
) {
2819 v1
.AddChildView(&v2
);
2820 v1
.AddChildView(&v3
);
2821 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2822 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2824 // Check that there's no change in order when adding at same index.
2825 v1
.AddChildViewAt(&v2
, 0);
2826 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2827 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2828 v1
.AddChildViewAt(&v3
, 1);
2829 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2830 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2832 // Add it at a different index and check for change in order.
2833 v1
.AddChildViewAt(&v2
, 1);
2834 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
2835 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
2836 v1
.AddChildViewAt(&v2
, 0);
2837 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2838 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2840 // Check that calling |AddChildView()| does not change the order.
2841 v1
.AddChildView(&v2
);
2842 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2843 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2844 v1
.AddChildView(&v3
);
2845 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2846 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2849 ////////////////////////////////////////////////////////////////////////////////
2851 ////////////////////////////////////////////////////////////////////////////////
2855 // Test implementation of LayerAnimator.
2856 class TestLayerAnimator
: public ui::LayerAnimator
{
2858 TestLayerAnimator();
2860 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
2863 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
2866 virtual ~TestLayerAnimator() { }
2869 gfx::Rect last_bounds_
;
2871 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
2874 TestLayerAnimator::TestLayerAnimator()
2875 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2878 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
2879 last_bounds_
= bounds
;
2884 class ViewLayerTest
: public ViewsTestBase
{
2886 ViewLayerTest() : widget_(NULL
) {}
2888 virtual ~ViewLayerTest() {
2891 // Returns the Layer used by the RootView.
2892 ui::Layer
* GetRootLayer() {
2893 return widget()->GetLayer();
2896 virtual void SetUp() OVERRIDE
{
2898 widget_
= new Widget
;
2899 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2900 params
.bounds
= gfx::Rect(50, 50, 200, 200);
2901 widget_
->Init(params
);
2903 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
2906 virtual void TearDown() OVERRIDE
{
2907 widget_
->CloseNow();
2908 ViewsTestBase::TearDown();
2911 Widget
* widget() { return widget_
; }
2918 TEST_F(ViewLayerTest
, LayerToggling
) {
2919 // Because we lazily create textures the calls to DrawTree are necessary to
2920 // ensure we trigger creation of textures.
2921 ui::Layer
* root_layer
= widget()->GetLayer();
2922 View
* content_view
= new View
;
2923 widget()->SetContentsView(content_view
);
2925 // Create v1, give it a bounds and verify everything is set up correctly.
2926 View
* v1
= new View
;
2927 v1
->SetPaintToLayer(true);
2928 EXPECT_TRUE(v1
->layer() != NULL
);
2929 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2930 content_view
->AddChildView(v1
);
2931 ASSERT_TRUE(v1
->layer() != NULL
);
2932 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2933 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
2935 // Create v2 as a child of v1 and do basic assertion testing.
2936 View
* v2
= new View
;
2937 v1
->AddChildView(v2
);
2938 EXPECT_TRUE(v2
->layer() == NULL
);
2939 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
2940 v2
->SetPaintToLayer(true);
2941 ASSERT_TRUE(v2
->layer() != NULL
);
2942 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2943 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2945 // Turn off v1s layer. v2 should still have a layer but its parent should have
2947 v1
->SetPaintToLayer(false);
2948 EXPECT_TRUE(v1
->layer() == NULL
);
2949 EXPECT_TRUE(v2
->layer() != NULL
);
2950 EXPECT_EQ(root_layer
, v2
->layer()->parent());
2951 ASSERT_EQ(1u, root_layer
->children().size());
2952 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
2953 // The bounds of the layer should have changed to be relative to the root view
2955 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
2957 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2958 gfx::Transform transform
;
2959 transform
.Scale(2.0, 2.0);
2960 v1
->SetTransform(transform
);
2961 EXPECT_TRUE(v1
->layer() != NULL
);
2962 EXPECT_TRUE(v2
->layer() != NULL
);
2963 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2964 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2965 ASSERT_EQ(1u, root_layer
->children().size());
2966 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
2967 ASSERT_EQ(1u, v1
->layer()->children().size());
2968 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
2969 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2972 // Verifies turning on a layer wires up children correctly.
2973 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
2974 View
* content_view
= new View
;
2975 widget()->SetContentsView(content_view
);
2977 // Create v1, give it a bounds and verify everything is set up correctly.
2978 View
* v1
= new View
;
2979 content_view
->AddChildView(v1
);
2980 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2982 View
* v2
= new View
;
2983 v1
->AddChildView(v2
);
2985 View
* v3
= new View
;
2986 v3
->SetPaintToLayer(true);
2987 v2
->AddChildView(v3
);
2988 ASSERT_TRUE(v3
->layer() != NULL
);
2990 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2992 v1
->SetPaintToLayer(true);
2993 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
2996 TEST_F(ViewLayerTest
, LayerAnimator
) {
2997 View
* content_view
= new View
;
2998 widget()->SetContentsView(content_view
);
3000 View
* v1
= new View
;
3001 content_view
->AddChildView(v1
);
3002 v1
->SetPaintToLayer(true);
3003 EXPECT_TRUE(v1
->layer() != NULL
);
3005 TestLayerAnimator
* animator
= new TestLayerAnimator();
3006 v1
->layer()->SetAnimator(animator
);
3008 gfx::Rect
bounds(1, 2, 3, 4);
3009 v1
->SetBoundsRect(bounds
);
3010 EXPECT_EQ(bounds
, animator
->last_bounds());
3011 // TestLayerAnimator doesn't update the layer.
3012 EXPECT_NE(bounds
, v1
->layer()->bounds());
3015 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3016 // doesn't have a layer change.
3017 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
3018 View
* content_view
= new View
;
3019 widget()->SetContentsView(content_view
);
3021 View
* v1
= new View
;
3022 content_view
->AddChildView(v1
);
3023 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3025 View
* v2
= new View
;
3026 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3027 v1
->AddChildView(v2
);
3028 v2
->SetPaintToLayer(true);
3029 ASSERT_TRUE(v2
->layer() != NULL
);
3030 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
3032 v1
->SetPosition(gfx::Point(25, 36));
3033 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
3035 v2
->SetPosition(gfx::Point(11, 12));
3036 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
3038 // Bounds of the layer should change even if the view is not invisible.
3039 v1
->SetVisible(false);
3040 v1
->SetPosition(gfx::Point(20, 30));
3041 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
3043 v2
->SetVisible(false);
3044 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3045 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
3048 // Make sure layers are positioned correctly in RTL.
3049 TEST_F(ViewLayerTest
, BoundInRTL
) {
3050 std::string locale
= l10n_util::GetApplicationLocale(std::string());
3051 base::i18n::SetICUDefaultLocale("he");
3053 View
* view
= new View
;
3054 widget()->SetContentsView(view
);
3056 int content_width
= view
->width();
3058 // |v1| is initially not attached to anything. So its layer will have the same
3059 // bounds as the view.
3060 View
* v1
= new View
;
3061 v1
->SetPaintToLayer(true);
3062 v1
->SetBounds(10, 10, 20, 10);
3063 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3064 v1
->layer()->bounds());
3066 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3068 view
->AddChildView(v1
);
3069 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3070 v1
->layer()->bounds());
3071 gfx::Rect l1bounds
= v1
->layer()->bounds();
3073 // Now attach a View to the widget first, then create a layer for it. Make
3074 // sure the bounds are correct.
3075 View
* v2
= new View
;
3076 v2
->SetBounds(50, 10, 30, 10);
3077 EXPECT_FALSE(v2
->layer());
3078 view
->AddChildView(v2
);
3079 v2
->SetPaintToLayer(true);
3080 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
3081 v2
->layer()->bounds());
3082 gfx::Rect l2bounds
= v2
->layer()->bounds();
3084 view
->SetPaintToLayer(true);
3085 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3086 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3088 // Move one of the views. Make sure the layer is positioned correctly
3090 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
3091 l1bounds
.set_x(l1bounds
.x() + 5);
3092 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3094 view
->SetPaintToLayer(false);
3095 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3096 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3098 // Move a view again.
3099 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
3100 l2bounds
.set_x(l2bounds
.x() - 5);
3101 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3104 base::i18n::SetICUDefaultLocale(locale
);
3107 // Makes sure a transform persists after toggling the visibility.
3108 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3109 View
* view
= new View
;
3110 gfx::Transform transform
;
3111 transform
.Scale(2.0, 2.0);
3112 view
->SetTransform(transform
);
3113 widget()->SetContentsView(view
);
3114 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3116 view
->SetVisible(false);
3117 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3119 view
->SetVisible(true);
3120 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3123 // Verifies a transform persists after removing/adding a view with a transform.
3124 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3125 View
* view
= new View
;
3126 gfx::Transform transform
;
3127 transform
.Scale(2.0, 2.0);
3128 view
->SetTransform(transform
);
3129 widget()->SetContentsView(view
);
3130 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3131 ASSERT_TRUE(view
->layer() != NULL
);
3132 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3134 View
* parent
= view
->parent();
3135 parent
->RemoveChildView(view
);
3136 parent
->AddChildView(view
);
3138 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3139 ASSERT_TRUE(view
->layer() != NULL
);
3140 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3143 // Makes sure that layer visibility is correct after toggling View visibility.
3144 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3145 View
* content_view
= new View
;
3146 widget()->SetContentsView(content_view
);
3148 // The view isn't attached to a widget or a parent view yet. But it should
3149 // still have a layer, but the layer should not be attached to the root
3151 View
* v1
= new View
;
3152 v1
->SetPaintToLayer(true);
3153 EXPECT_TRUE(v1
->layer());
3154 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3157 // Once the view is attached to a widget, its layer should be attached to the
3158 // root layer and visible.
3159 content_view
->AddChildView(v1
);
3160 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3162 EXPECT_TRUE(v1
->layer()->IsDrawn());
3164 v1
->SetVisible(false);
3165 EXPECT_FALSE(v1
->layer()->IsDrawn());
3167 v1
->SetVisible(true);
3168 EXPECT_TRUE(v1
->layer()->IsDrawn());
3171 EXPECT_FALSE(v1
->layer()->IsDrawn());
3174 EXPECT_TRUE(v1
->layer()->IsDrawn());
3177 // Tests that the layers in the subtree are orphaned after a View is removed
3179 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3180 View
* content_view
= new View
;
3181 widget()->SetContentsView(content_view
);
3183 View
* v1
= new View
;
3184 content_view
->AddChildView(v1
);
3186 View
* v2
= new View
;
3187 v1
->AddChildView(v2
);
3188 v2
->SetPaintToLayer(true);
3189 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3191 EXPECT_TRUE(v2
->layer()->IsDrawn());
3193 content_view
->RemoveChildView(v1
);
3195 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3199 content_view
->AddChildView(v2
);
3202 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3204 EXPECT_TRUE(v2
->layer()->IsDrawn());
3207 class PaintTrackingView
: public View
{
3209 PaintTrackingView() : painted_(false) {
3212 bool painted() const { return painted_
; }
3213 void set_painted(bool value
) { painted_
= value
; }
3215 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
3222 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3225 // Makes sure child views with layers aren't painted when paint starts at an
3227 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3228 PaintTrackingView
* content_view
= new PaintTrackingView
;
3229 widget()->SetContentsView(content_view
);
3230 content_view
->SetPaintToLayer(true);
3231 GetRootLayer()->GetCompositor()->ScheduleDraw();
3232 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3233 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3234 content_view
->set_painted(false);
3235 // content_view no longer has a dirty rect. Paint from the root and make sure
3236 // PaintTrackingView isn't painted.
3237 GetRootLayer()->GetCompositor()->ScheduleDraw();
3238 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3239 EXPECT_FALSE(content_view
->painted());
3241 // Make content_view have a dirty rect, paint the layers and make sure
3242 // PaintTrackingView is painted.
3243 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3244 GetRootLayer()->GetCompositor()->ScheduleDraw();
3245 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3246 EXPECT_TRUE(content_view
->painted());
3249 // Tests that the visibility of child layers are updated correctly when a View's
3250 // visibility changes.
3251 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3252 View
* v1
= new View
;
3253 v1
->SetPaintToLayer(true);
3254 widget()->SetContentsView(v1
);
3256 View
* v2
= new View
;
3257 v1
->AddChildView(v2
);
3259 View
* v3
= new View
;
3260 v2
->AddChildView(v3
);
3261 v3
->SetVisible(false);
3263 View
* v4
= new View
;
3264 v4
->SetPaintToLayer(true);
3265 v3
->AddChildView(v4
);
3267 EXPECT_TRUE(v1
->layer()->IsDrawn());
3268 EXPECT_FALSE(v4
->layer()->IsDrawn());
3270 v2
->SetVisible(false);
3271 EXPECT_TRUE(v1
->layer()->IsDrawn());
3272 EXPECT_FALSE(v4
->layer()->IsDrawn());
3274 v2
->SetVisible(true);
3275 EXPECT_TRUE(v1
->layer()->IsDrawn());
3276 EXPECT_FALSE(v4
->layer()->IsDrawn());
3278 v2
->SetVisible(false);
3279 EXPECT_TRUE(v1
->layer()->IsDrawn());
3280 EXPECT_FALSE(v4
->layer()->IsDrawn());
3281 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3283 v3
->SetVisible(true);
3284 EXPECT_TRUE(v1
->layer()->IsDrawn());
3285 EXPECT_FALSE(v4
->layer()->IsDrawn());
3286 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3288 // Reparent |v3| to |v1|.
3289 v1
->AddChildView(v3
);
3290 EXPECT_TRUE(v1
->layer()->IsDrawn());
3291 EXPECT_TRUE(v4
->layer()->IsDrawn());
3292 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3295 // This test creates a random View tree, and then randomly reorders child views,
3296 // reparents views etc. Unrelated changes can appear to break this test. So
3297 // marking this as FLAKY.
3298 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3299 View
* content
= new View
;
3300 content
->SetPaintToLayer(true);
3301 widget()->SetContentsView(content
);
3304 ConstructTree(content
, 5);
3305 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3307 ScrambleTree(content
);
3308 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3310 ScrambleTree(content
);
3311 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3313 ScrambleTree(content
);
3314 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3317 // Verifies when views are reordered the layer is also reordered. The widget is
3318 // providing the parent layer.
3319 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3320 View
* content
= new View
;
3321 widget()->SetContentsView(content
);
3322 View
* c1
= new View
;
3323 c1
->SetPaintToLayer(true);
3324 content
->AddChildView(c1
);
3325 View
* c2
= new View
;
3326 c2
->SetPaintToLayer(true);
3327 content
->AddChildView(c2
);
3329 ui::Layer
* parent_layer
= c1
->layer()->parent();
3330 ASSERT_TRUE(parent_layer
);
3331 ASSERT_EQ(2u, parent_layer
->children().size());
3332 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3333 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3335 // Move c1 to the front. The layers should have moved too.
3336 content
->ReorderChildView(c1
, -1);
3337 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3338 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3341 // Verifies that the layer of a view can be acquired properly.
3342 TEST_F(ViewLayerTest
, AcquireLayer
) {
3343 View
* content
= new View
;
3344 widget()->SetContentsView(content
);
3345 scoped_ptr
<View
> c1(new View
);
3346 c1
->SetPaintToLayer(true);
3347 EXPECT_TRUE(c1
->layer());
3348 content
->AddChildView(c1
.get());
3350 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3351 EXPECT_EQ(layer
.get(), c1
->layer());
3353 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3354 EXPECT_NE(c1
->layer(), layer2
.get());
3356 // Destroy view before destroying layer.
3360 // Verify that new layer scales content only if the old layer does.
3361 TEST_F(ViewLayerTest
, RecreateLayerScaling
) {
3362 scoped_ptr
<View
> v(new View());
3363 v
->SetPaintToLayer(true);
3364 // Set to non default value.
3365 v
->layer()->set_scale_content(false);
3366 scoped_ptr
<ui::Layer
> old_layer(v
->RecreateLayer());
3367 ui::Layer
* new_layer
= v
->layer();
3368 EXPECT_FALSE(new_layer
->scale_content());
3371 // Verify the z-order of the layers as a result of calling RecreateLayer().
3372 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3373 scoped_ptr
<View
> v(new View());
3374 v
->SetPaintToLayer(true);
3376 View
* v1
= new View();
3377 v1
->SetPaintToLayer(true);
3378 v
->AddChildView(v1
);
3379 View
* v2
= new View();
3380 v2
->SetPaintToLayer(true);
3381 v
->AddChildView(v2
);
3383 // Test the initial z-order.
3384 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3385 ASSERT_EQ(2u, child_layers_pre
.size());
3386 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3387 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3389 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3391 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3392 // for |v1| and |v2|.
3393 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3394 ASSERT_EQ(3u, child_layers_post
.size());
3395 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3396 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3397 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3400 // Verify the z-order of the layers as a result of calling RecreateLayer when
3401 // the widget is the parent with the layer.
3402 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3403 View
* v
= new View();
3404 widget()->SetContentsView(v
);
3406 View
* v1
= new View();
3407 v1
->SetPaintToLayer(true);
3408 v
->AddChildView(v1
);
3409 View
* v2
= new View();
3410 v2
->SetPaintToLayer(true);
3411 v
->AddChildView(v2
);
3413 ui::Layer
* root_layer
= GetRootLayer();
3415 // Test the initial z-order.
3416 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3417 ASSERT_EQ(2u, child_layers_pre
.size());
3418 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3419 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3421 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3423 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3424 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3425 ASSERT_EQ(3u, child_layers_post
.size());
3426 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3427 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3428 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3431 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3433 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3435 v
.SetPaintToLayer(true);
3437 child
.SetPaintToLayer(true);
3438 v
.AddChildView(&child
);
3439 ASSERT_TRUE(v
.layer() != NULL
);
3440 ASSERT_EQ(1u, v
.layer()->children().size());
3441 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3443 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3444 v
.layer()->Add(&layer
);
3445 v
.layer()->StackAtBottom(&layer
);
3447 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3449 // All children should be moved from old layer to new layer.
3450 ASSERT_TRUE(old_layer
.get() != NULL
);
3451 EXPECT_TRUE(old_layer
->children().empty());
3453 // And new layer should have the two children.
3454 ASSERT_TRUE(v
.layer() != NULL
);
3455 ASSERT_EQ(2u, v
.layer()->children().size());
3456 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3457 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3460 TEST_F(ViewTest
, FocusableAssertions
) {
3461 // View subclasses may change insets based on whether they are focusable,
3462 // which effects the preferred size. To avoid preferred size changing around
3463 // these Views need to key off the last value set to SetFocusable(), not
3464 // whether the View is focusable right now. For this reason it's important
3465 // that focusable() return the last value passed to SetFocusable and not
3466 // whether the View is focusable right now.
3468 view
.SetFocusable(true);
3469 EXPECT_TRUE(view
.focusable());
3470 view
.SetEnabled(false);
3471 EXPECT_TRUE(view
.focusable());
3472 view
.SetFocusable(false);
3473 EXPECT_FALSE(view
.focusable());
3476 // Creates a widget of TYPE_CONTROL.
3477 // The caller takes ownership of the returned widget.
3478 Widget
* CreateControlWidget(aura::Window
* parent
, const gfx::Rect
& bounds
) {
3479 Widget::InitParams
params(Widget::InitParams::TYPE_CONTROL
);
3480 params
.parent
= parent
;
3481 params
.bounds
= bounds
;
3482 Widget
* widget
= new Widget();
3483 widget
->Init(params
);
3487 // Returns a view with a layer with the passed in |bounds| and |layer_name|.
3488 // The caller takes ownership of the returned view.
3489 View
* CreateViewWithLayer(const gfx::Rect
& bounds
,
3490 const char* layer_name
) {
3491 View
* view
= new View();
3492 view
->SetBoundsRect(bounds
);
3493 view
->SetPaintToLayer(true);
3494 view
->layer()->set_name(layer_name
);
3498 // Test that RecreateWindowLayers() recreates the layers for all child windows
3499 // and all child views and that the z-order of the recreated layers matches that
3500 // of the original layers.
3504 // +-- v2 (no layer)
3505 // +-- v3 (no layer)
3513 TEST_F(ViewTest
, RecreateLayers
) {
3514 Widget
* w1
= CreateControlWidget(GetContext(), gfx::Rect(0, 0, 100, 100));
3515 w1
->GetNativeView()->layer()->set_name("w1");
3517 View
* v2
= new View();
3518 v2
->SetBounds(0, 1, 100, 101);
3519 View
* v3
= new View();
3520 v3
->SetBounds(0, 2, 100, 102);
3521 View
* w2_host_view
= new View();
3523 View
* v1
= CreateViewWithLayer(gfx::Rect(0, 3, 100, 103), "v1");
3524 ui::Layer
* v1_layer
= v1
->layer();
3525 w1
->GetRootView()->AddChildView(v1
);
3526 w1
->GetRootView()->AddChildView(v2
);
3527 v2
->AddChildView(v3
);
3528 View
* v4
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v4");
3529 ui::Layer
* v4_layer
= v4
->layer();
3530 v2
->AddChildView(v4
);
3532 w1
->GetRootView()->AddChildView(w2_host_view
);
3533 View
* v7
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v7");
3534 ui::Layer
* v7_layer
= v7
->layer();
3535 w1
->GetRootView()->AddChildView(v7
);
3537 View
* v8
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v8");
3538 ui::Layer
* v8_layer
= v8
->layer();
3539 v7
->AddChildView(v8
);
3541 View
* v9
= CreateViewWithLayer(gfx::Rect(0, 4, 100, 104), "v9");
3542 ui::Layer
* v9_layer
= v9
->layer();
3543 v7
->AddChildView(v9
);
3545 Widget
* w2
= CreateControlWidget(w1
->GetNativeView(),
3546 gfx::Rect(0, 5, 100, 105));
3547 w2
->GetNativeView()->layer()->set_name("w2");
3548 w2
->GetNativeView()->SetProperty(kHostViewKey
, w2_host_view
);
3550 View
* v5
= CreateViewWithLayer(gfx::Rect(0, 6, 100, 106), "v5");
3551 w2
->GetRootView()->AddChildView(v5
);
3552 View
* v6
= CreateViewWithLayer(gfx::Rect(0, 7, 100, 107), "v6");
3553 ui::Layer
* v6_layer
= v6
->layer();
3554 v5
->AddChildView(v6
);
3556 // Test the initial order of the layers.
3557 ui::Layer
* w1_layer
= w1
->GetNativeView()->layer();
3558 ASSERT_EQ("w1", w1_layer
->name());
3559 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer
));
3560 ui::Layer
* w2_layer
= w1_layer
->children()[2];
3561 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer
));
3562 ui::Layer
* v5_layer
= w2_layer
->children()[0];
3563 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer
));
3566 scoped_ptr
<ui::LayerTreeOwner
> cloned_owner(
3567 wm::RecreateLayers(w1
->GetNativeView()));
3568 EXPECT_EQ(w1_layer
, cloned_owner
->root());
3569 EXPECT_NE(w1_layer
, w1
->GetNativeView()->layer());
3571 // The old layers should still exist and have the same hierarchy.
3572 ASSERT_EQ("w1", w1_layer
->name());
3573 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_layer
));
3574 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_layer
));
3575 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_layer
));
3576 EXPECT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_layer
));
3578 ASSERT_EQ(4u, w1_layer
->children().size());
3579 EXPECT_EQ(v1_layer
, w1_layer
->children()[0]);
3580 EXPECT_EQ(v4_layer
, w1_layer
->children()[1]);
3581 EXPECT_EQ(w2_layer
, w1_layer
->children()[2]);
3582 EXPECT_EQ(v7_layer
, w1_layer
->children()[3]);
3584 ASSERT_EQ(1u, w2_layer
->children().size());
3585 EXPECT_EQ(v5_layer
, w2_layer
->children()[0]);
3587 ASSERT_EQ(1u, v5_layer
->children().size());
3588 EXPECT_EQ(v6_layer
, v5_layer
->children()[0]);
3590 ASSERT_EQ(0u, v6_layer
->children().size());
3592 EXPECT_EQ(2u, v7_layer
->children().size());
3593 EXPECT_EQ(v8_layer
, v7_layer
->children()[0]);
3594 EXPECT_EQ(v9_layer
, v7_layer
->children()[1]);
3596 // The cloned layers should have the same hierarchy as old.
3597 ui::Layer
* w1_new_layer
= w1
->GetNativeView()->layer();
3598 EXPECT_EQ("w1", w1_new_layer
->name());
3599 ASSERT_EQ("v1 v4 w2 v7", ui::test::ChildLayerNamesAsString(*w1_new_layer
));
3600 ui::Layer
* w2_new_layer
= w1_new_layer
->children()[2];
3601 ASSERT_EQ("v5", ui::test::ChildLayerNamesAsString(*w2_new_layer
));
3602 ui::Layer
* v5_new_layer
= w2_new_layer
->children()[0];
3603 ASSERT_EQ("v6", ui::test::ChildLayerNamesAsString(*v5_new_layer
));
3604 ui::Layer
* v7_new_layer
= w1_new_layer
->children()[3];
3605 ASSERT_EQ("v8 v9", ui::test::ChildLayerNamesAsString(*v7_new_layer
));
3607 // The views and the widgets are destroyed when AuraTestHelper::TearDown()
3608 // destroys root_window().
3611 // Verifies when a view is deleted it is removed from ViewStorage.
3612 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3613 ViewStorage
* view_storage
= ViewStorage::GetInstance();
3614 const int storage_id
= view_storage
->CreateStorageID();
3617 view_storage
->StoreView(storage_id
, &view
);
3619 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
3622 } // namespace views