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/string_util.h"
10 #include "base/utf_string_conversions.h"
11 #include "grit/ui_strings.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "ui/base/accelerators/accelerator.h"
14 #include "ui/base/clipboard/clipboard.h"
15 #include "ui/base/events/event.h"
16 #include "ui/base/keycodes/keyboard_codes.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/models/simple_menu_model.h"
19 #include "ui/compositor/compositor.h"
20 #include "ui/compositor/layer.h"
21 #include "ui/compositor/layer_animator.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/path.h"
24 #include "ui/gfx/transform.h"
25 #include "ui/views/background.h"
26 #include "ui/views/controls/button/button_dropdown.h"
27 #include "ui/views/controls/button/checkbox.h"
28 #include "ui/views/controls/button/label_button.h"
29 #include "ui/views/controls/native/native_view_host.h"
30 #include "ui/views/controls/scroll_view.h"
31 #include "ui/views/controls/textfield/textfield.h"
32 #include "ui/views/focus/accelerator_handler.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/views_delegate.h"
37 #include "ui/views/widget/native_widget.h"
38 #include "ui/views/widget/root_view.h"
39 #include "ui/views/window/dialog_client_view.h"
40 #include "ui/views/window/dialog_delegate.h"
43 #include "ui/views/test/test_views_delegate.h"
46 #include "ui/aura/root_window.h"
47 #include "ui/base/gestures/gesture_recognizer.h"
54 // Returns true if |ancestor| is an ancestor of |layer|.
55 bool LayerIsAncestor(const ui::Layer
* ancestor
, const ui::Layer
* layer
) {
56 while (layer
&& layer
!= ancestor
)
57 layer
= layer
->parent();
58 return layer
== ancestor
;
61 // Convenience functions for walking a View tree.
62 const views::View
* FirstView(const views::View
* view
) {
63 const views::View
* v
= view
;
64 while (v
->has_children())
69 const views::View
* NextView(const views::View
* view
) {
70 const views::View
* v
= view
;
71 const views::View
* parent
= v
->parent();
74 int next
= parent
->GetIndexOf(v
) + 1;
75 if (next
!= parent
->child_count())
76 return FirstView(parent
->child_at(next
));
80 // Convenience functions for walking a Layer tree.
81 const ui::Layer
* FirstLayer(const ui::Layer
* layer
) {
82 const ui::Layer
* l
= layer
;
83 while (l
->children().size() > 0)
88 const ui::Layer
* NextLayer(const ui::Layer
* layer
) {
89 const ui::Layer
* parent
= layer
->parent();
92 const std::vector
<ui::Layer
*> children
= parent
->children();
94 for (index
= 0; index
< children
.size(); index
++) {
95 if (children
[index
] == layer
)
98 size_t next
= index
+ 1;
99 if (next
< children
.size())
100 return FirstLayer(children
[next
]);
104 // Given the root nodes of a View tree and a Layer tree, makes sure the two
105 // trees are in sync.
106 bool ViewAndLayerTreeAreConsistent(const views::View
* view
,
107 const ui::Layer
* layer
) {
108 const views::View
* v
= FirstView(view
);
109 const ui::Layer
* l
= FirstLayer(layer
);
111 // Find the view with a layer.
112 while (v
&& !v
->layer())
118 // Check if the View tree and the Layer tree are in sync.
119 EXPECT_EQ(l
, v
->layer());
123 // Check if the visibility states of the View and the Layer are in sync.
124 EXPECT_EQ(l
->IsDrawn(), v
->IsDrawn());
125 if (v
->IsDrawn() != l
->IsDrawn()) {
126 for (const views::View
* vv
= v
; vv
; vv
= vv
->parent())
127 LOG(ERROR
) << "V: " << vv
<< " " << vv
->visible() << " "
128 << vv
->IsDrawn() << " " << vv
->layer();
129 for (const ui::Layer
* ll
= l
; ll
; ll
= ll
->parent())
130 LOG(ERROR
) << "L: " << ll
<< " " << ll
->IsDrawn();
134 // Check if the size of the View and the Layer are in sync.
135 EXPECT_EQ(l
->bounds(), v
->bounds());
136 if (v
->bounds() != l
->bounds())
139 if (v
== view
|| l
== layer
)
140 return v
== view
&& l
== layer
;
149 // Constructs a View tree with the specified depth.
150 void ConstructTree(views::View
* view
, int depth
) {
153 int count
= base::RandInt(1, 5);
154 for (int i
= 0; i
< count
; i
++) {
155 views::View
* v
= new views::View
;
156 view
->AddChildView(v
);
157 if (base::RandDouble() > 0.5)
158 v
->SetPaintToLayer(true);
159 if (base::RandDouble() < 0.2)
160 v
->SetVisible(false);
162 ConstructTree(v
, depth
- 1);
166 void ScrambleTree(views::View
* view
) {
167 int count
= view
->child_count();
170 for (int i
= 0; i
< count
; i
++) {
171 ScrambleTree(view
->child_at(i
));
175 int a
= base::RandInt(0, count
- 1);
176 int b
= base::RandInt(0, count
- 1);
178 views::View
* view_a
= view
->child_at(a
);
179 views::View
* view_b
= view
->child_at(b
);
180 view
->ReorderChildView(view_a
, b
);
181 view
->ReorderChildView(view_b
, a
);
184 if (!view
->layer() && base::RandDouble() < 0.1)
185 view
->SetPaintToLayer(true);
187 if (base::RandDouble() < 0.1)
188 view
->SetVisible(!view
->visible());
191 // Convenience to make constructing a GestureEvent simpler.
192 class GestureEventForTest
: public ui::GestureEvent
{
194 GestureEventForTest(ui::EventType type
, int x
, int y
, int flags
)
195 : GestureEvent(type
, x
, y
, flags
, base::TimeDelta(),
196 ui::GestureEventDetails(type
, 0.0f
, 0.0f
), 0) {
200 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest
);
207 typedef ViewsTestBase ViewTest
;
209 // A derived class for testing purpose.
210 class TestView
: public View
{
212 TestView() : View(), delete_on_pressed_(false), in_touch_sequence_(false) {}
213 virtual ~TestView() {}
215 // Reset all test state
217 did_change_bounds_
= false;
218 last_mouse_event_type_
= 0;
219 location_
.SetPoint(0, 0);
220 received_mouse_enter_
= false;
221 received_mouse_exit_
= false;
222 last_touch_event_type_
= 0;
223 last_touch_event_was_handled_
= false;
224 last_gesture_event_type_
= 0;
225 last_gesture_event_was_handled_
= false;
226 last_clip_
.setEmpty();
227 accelerator_count_map_
.clear();
230 virtual void OnBoundsChanged(const gfx::Rect
& previous_bounds
) OVERRIDE
;
231 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
232 virtual bool OnMouseDragged(const ui::MouseEvent
& event
) OVERRIDE
;
233 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
234 virtual void OnMouseEntered(const ui::MouseEvent
& event
) OVERRIDE
;
235 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
237 virtual void OnTouchEvent(ui::TouchEvent
* event
) OVERRIDE
;
238 // Ignores GestureEvent by default.
239 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
;
241 virtual void Paint(gfx::Canvas
* canvas
) OVERRIDE
;
242 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
;
243 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
246 bool did_change_bounds_
;
247 gfx::Rect new_bounds_
;
250 int last_mouse_event_type_
;
251 gfx::Point location_
;
252 bool received_mouse_enter_
;
253 bool received_mouse_exit_
;
254 bool delete_on_pressed_
;
257 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
260 int last_gesture_event_type_
;
261 bool last_gesture_event_was_handled_
;
264 int last_touch_event_type_
;
265 bool last_touch_event_was_handled_
;
266 bool in_touch_sequence_
;
272 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
275 // A view subclass that ignores all touch events for testing purposes.
276 class TestViewIgnoreTouch
: public TestView
{
278 TestViewIgnoreTouch() : TestView() {}
279 virtual ~TestViewIgnoreTouch() {}
282 virtual void OnTouchEvent(ui::TouchEvent
* event
) OVERRIDE
;
285 // A view subclass that consumes all Gesture events for testing purposes.
286 class TestViewConsumeGesture
: public TestView
{
288 TestViewConsumeGesture() : TestView() {}
289 virtual ~TestViewConsumeGesture() {}
292 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
293 last_gesture_event_type_
= event
->type();
294 location_
.SetPoint(event
->x(), event
->y());
295 event
->StopPropagation();
299 DISALLOW_COPY_AND_ASSIGN(TestViewConsumeGesture
);
302 // A view subclass that ignores all Gesture events.
303 class TestViewIgnoreGesture
: public TestView
{
305 TestViewIgnoreGesture() : TestView() {}
306 virtual ~TestViewIgnoreGesture() {}
309 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
312 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreGesture
);
315 // A view subclass that ignores all scroll-gesture events, but consume all other
317 class TestViewIgnoreScrollGestures
: public TestViewConsumeGesture
{
319 TestViewIgnoreScrollGestures() {}
320 virtual ~TestViewIgnoreScrollGestures() {}
323 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
{
324 if (event
->IsScrollGestureEvent())
326 TestViewConsumeGesture::OnGestureEvent(event
);
329 DISALLOW_COPY_AND_ASSIGN(TestViewIgnoreScrollGestures
);
332 ////////////////////////////////////////////////////////////////////////////////
334 ////////////////////////////////////////////////////////////////////////////////
336 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
337 did_change_bounds_
= true;
338 new_bounds_
= bounds();
341 TEST_F(ViewTest
, OnBoundsChanged
) {
344 gfx::Rect
prev_rect(0, 0, 200, 200);
345 gfx::Rect
new_rect(100, 100, 250, 250);
347 v
.SetBoundsRect(prev_rect
);
349 v
.SetBoundsRect(new_rect
);
351 EXPECT_TRUE(v
.did_change_bounds_
);
352 EXPECT_EQ(v
.new_bounds_
, new_rect
);
353 EXPECT_EQ(v
.bounds(), new_rect
);
356 ////////////////////////////////////////////////////////////////////////////////
358 ////////////////////////////////////////////////////////////////////////////////
360 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
361 last_mouse_event_type_
= event
.type();
362 location_
.SetPoint(event
.x(), event
.y());
363 if (delete_on_pressed_
)
368 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
369 last_mouse_event_type_
= event
.type();
370 location_
.SetPoint(event
.x(), event
.y());
374 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
375 last_mouse_event_type_
= event
.type();
376 location_
.SetPoint(event
.x(), event
.y());
379 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
380 received_mouse_enter_
= true;
383 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
384 received_mouse_exit_
= true;
387 TEST_F(ViewTest
, MouseEvent
) {
388 TestView
* v1
= new TestView();
389 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
391 TestView
* v2
= new TestView();
392 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
394 scoped_ptr
<Widget
> widget(new Widget
);
395 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
396 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
397 params
.bounds
= gfx::Rect(50, 50, 650, 650);
398 widget
->Init(params
);
399 internal::RootView
* root
=
400 static_cast<internal::RootView
*>(widget
->GetRootView());
402 root
->AddChildView(v1
);
403 v1
->AddChildView(v2
);
408 gfx::Point
p1(110, 120);
409 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
410 ui::EF_LEFT_MOUSE_BUTTON
);
411 root
->OnMousePressed(pressed
);
412 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
413 EXPECT_EQ(v2
->location_
.x(), 10);
414 EXPECT_EQ(v2
->location_
.y(), 20);
415 // Make sure v1 did not receive the event
416 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
418 // Drag event out of bounds. Should still go to v2
421 gfx::Point
p2(50, 40);
422 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
,
423 ui::EF_LEFT_MOUSE_BUTTON
);
424 root
->OnMouseDragged(dragged
);
425 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
426 EXPECT_EQ(v2
->location_
.x(), -50);
427 EXPECT_EQ(v2
->location_
.y(), -60);
428 // Make sure v1 did not receive the event
429 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
431 // Releasted event out of bounds. Should still go to v2
434 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0);
435 root
->OnMouseDragged(released
);
436 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
437 EXPECT_EQ(v2
->location_
.x(), -100);
438 EXPECT_EQ(v2
->location_
.y(), -100);
439 // Make sure v1 did not receive the event
440 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
445 // Confirm that a view can be deleted as part of processing a mouse press.
446 TEST_F(ViewTest
, DeleteOnPressed
) {
447 TestView
* v1
= new TestView();
448 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
450 TestView
* v2
= new TestView();
451 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
456 scoped_ptr
<Widget
> widget(new Widget
);
457 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
458 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
459 params
.bounds
= gfx::Rect(50, 50, 650, 650);
460 widget
->Init(params
);
461 View
* root
= widget
->GetRootView();
463 root
->AddChildView(v1
);
464 v1
->AddChildView(v2
);
466 v2
->delete_on_pressed_
= true;
467 gfx::Point
point(110, 120);
468 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
469 ui::EF_LEFT_MOUSE_BUTTON
);
470 root
->OnMousePressed(pressed
);
471 EXPECT_EQ(0, v1
->child_count());
476 ////////////////////////////////////////////////////////////////////////////////
478 ////////////////////////////////////////////////////////////////////////////////
479 void TestView::OnTouchEvent(ui::TouchEvent
* event
) {
480 last_touch_event_type_
= event
->type();
481 location_
.SetPoint(event
->x(), event
->y());
482 if (!in_touch_sequence_
) {
483 if (event
->type() == ui::ET_TOUCH_PRESSED
) {
484 in_touch_sequence_
= true;
485 event
->StopPropagation();
489 if (event
->type() == ui::ET_TOUCH_RELEASED
) {
490 in_touch_sequence_
= false;
494 event
->StopPropagation();
498 if (last_touch_event_was_handled_
)
499 event
->StopPropagation();
502 void TestViewIgnoreTouch::OnTouchEvent(ui::TouchEvent
* event
) {
505 TEST_F(ViewTest
, TouchEvent
) {
506 TestView
* v1
= new TestView();
507 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
509 TestView
* v2
= new TestView();
510 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
512 TestView
* v3
= new TestViewIgnoreTouch();
513 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
515 scoped_ptr
<Widget
> widget(new Widget());
516 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
517 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
518 params
.bounds
= gfx::Rect(50, 50, 650, 650);
519 widget
->Init(params
);
520 internal::RootView
* root
=
521 static_cast<internal::RootView
*>(widget
->GetRootView());
523 root
->AddChildView(v1
);
524 v1
->AddChildView(v2
);
525 v2
->AddChildView(v3
);
527 // |v3| completely obscures |v2|, but all the touch events on |v3| should
528 // reach |v2| because |v3| doesn't process any touch events.
530 // Make sure if none of the views handle the touch event, the gesture manager
535 ui::TouchEvent
unhandled(ui::ET_TOUCH_MOVED
,
536 gfx::Point(400, 400),
538 0, /* first finger touch */
541 root
->DispatchTouchEvent(&unhandled
);
543 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
544 EXPECT_EQ(v2
->last_touch_event_type_
, 0);
546 // Test press, drag, release touch sequence.
550 ui::TouchEvent
pressed(ui::ET_TOUCH_PRESSED
,
551 gfx::Point(110, 120),
553 0, /* first finger touch */
556 v2
->last_touch_event_was_handled_
= true;
557 root
->DispatchTouchEvent(&pressed
);
559 EXPECT_EQ(v2
->last_touch_event_type_
, ui::ET_TOUCH_PRESSED
);
560 EXPECT_EQ(v2
->location_
.x(), 10);
561 EXPECT_EQ(v2
->location_
.y(), 20);
562 // Make sure v1 did not receive the event
563 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
565 // Drag event out of bounds. Should still go to v2
568 ui::TouchEvent
dragged(ui::ET_TOUCH_MOVED
,
571 0, /* first finger touch */
575 root
->DispatchTouchEvent(&dragged
);
576 EXPECT_EQ(v2
->last_touch_event_type_
, ui::ET_TOUCH_MOVED
);
577 EXPECT_EQ(v2
->location_
.x(), -50);
578 EXPECT_EQ(v2
->location_
.y(), -60);
579 // Make sure v1 did not receive the event
580 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
582 // Released event out of bounds. Should still go to v2
585 ui::TouchEvent
released(ui::ET_TOUCH_RELEASED
, gfx::Point(),
587 0, /* first finger */
590 v2
->last_touch_event_was_handled_
= true;
591 root
->DispatchTouchEvent(&released
);
592 EXPECT_EQ(v2
->last_touch_event_type_
, ui::ET_TOUCH_RELEASED
);
593 EXPECT_EQ(v2
->location_
.x(), -100);
594 EXPECT_EQ(v2
->location_
.y(), -100);
595 // Make sure v1 did not receive the event
596 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
601 void TestView::OnGestureEvent(ui::GestureEvent
* event
) {
604 TEST_F(ViewTest
, GestureEvent
) {
605 // Views hierarchy for non delivery of GestureEvent.
606 TestView
* v1
= new TestViewConsumeGesture();
607 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
609 TestView
* v2
= new TestViewConsumeGesture();
610 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
612 TestView
* v3
= new TestViewIgnoreGesture();
613 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
615 scoped_ptr
<Widget
> widget(new Widget());
616 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
617 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
618 params
.bounds
= gfx::Rect(50, 50, 650, 650);
619 widget
->Init(params
);
620 internal::RootView
* root
=
621 static_cast<internal::RootView
*>(widget
->GetRootView());
623 root
->AddChildView(v1
);
624 v1
->AddChildView(v2
);
625 v2
->AddChildView(v3
);
627 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
628 // reach |v2| because |v3| doesn't process any gesture events. However, since
629 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
637 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
638 root
->DispatchGestureEvent(&g1
);
639 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
640 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
641 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
643 // Simulate an up so that RootView is no longer targetting |v3|.
644 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
645 root
->DispatchGestureEvent(&g1_up
);
652 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
653 root
->DispatchGestureEvent(&g2
);
654 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
655 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
656 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
658 // Send event |g1| again. Even though the coordinates target |v3| it should go
659 // to |v1| as that is the view the touch was initially down on.
660 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
661 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
662 root
->DispatchGestureEvent(&g1
);
663 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
664 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
665 EXPECT_EQ("110,110", v1
->location_
.ToString());
670 TEST_F(ViewTest
, ScrollGestureEvent
) {
671 // Views hierarchy for non delivery of GestureEvent.
672 TestView
* v1
= new TestViewConsumeGesture();
673 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
675 TestView
* v2
= new TestViewIgnoreScrollGestures();
676 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
678 TestView
* v3
= new TestViewIgnoreGesture();
679 v3
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
681 scoped_ptr
<Widget
> widget(new Widget());
682 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
683 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
684 params
.bounds
= gfx::Rect(50, 50, 650, 650);
685 widget
->Init(params
);
686 internal::RootView
* root
=
687 static_cast<internal::RootView
*>(widget
->GetRootView());
689 root
->AddChildView(v1
);
690 v1
->AddChildView(v2
);
691 v2
->AddChildView(v3
);
693 // |v3| completely obscures |v2|, but all the gesture events on |v3| should
694 // reach |v2| because |v3| doesn't process any gesture events. However, since
695 // |v2| does process gesture events, gesture events on |v3| or |v2| should not
703 GestureEventForTest
g1(ui::ET_GESTURE_TAP
, 110, 110, 0);
704 root
->DispatchGestureEvent(&g1
);
705 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
706 EXPECT_EQ(gfx::Point(10, 10), v2
->location_
);
707 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
711 // Send scroll gestures on |v3|. The gesture should reach |v2|, however,
712 // since it does not process scroll-gesture events, these events should reach
714 GestureEventForTest
gscroll_begin(ui::ET_GESTURE_SCROLL_BEGIN
, 115, 115, 0);
715 root
->DispatchGestureEvent(&gscroll_begin
);
716 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
717 EXPECT_EQ(ui::ET_GESTURE_SCROLL_BEGIN
, v1
->last_gesture_event_type_
);
720 // Send a second tap on |v1|. The event should reach |v2| since it is the
721 // default gesture handler, and not |v1| (even though it is the view under the
722 // point, and is the scroll event handler).
723 GestureEventForTest
second_tap(ui::ET_GESTURE_TAP
, 70, 70, 0);
724 root
->DispatchGestureEvent(&second_tap
);
725 EXPECT_EQ(ui::ET_GESTURE_TAP
, v2
->last_gesture_event_type_
);
726 EXPECT_EQ(ui::ET_UNKNOWN
, v1
->last_gesture_event_type_
);
729 GestureEventForTest
gscroll_end(ui::ET_GESTURE_SCROLL_END
, 50, 50, 0);
730 root
->DispatchGestureEvent(&gscroll_end
);
731 EXPECT_EQ(ui::ET_GESTURE_SCROLL_END
, v1
->last_gesture_event_type_
);
734 // Simulate an up so that RootView is no longer targetting |v3|.
735 GestureEventForTest
g1_up(ui::ET_GESTURE_END
, 110, 110, 0);
736 root
->DispatchGestureEvent(&g1_up
);
737 EXPECT_EQ(ui::ET_GESTURE_END
, v2
->last_gesture_event_type_
);
744 GestureEventForTest
g2(ui::ET_GESTURE_TAP
, 80, 80, 0);
745 root
->DispatchGestureEvent(&g2
);
746 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
747 EXPECT_EQ(gfx::Point(80, 80), v1
->location_
);
748 EXPECT_EQ(ui::ET_UNKNOWN
, v2
->last_gesture_event_type_
);
750 // Send event |g1| again. Even though the coordinates target |v3| it should go
751 // to |v1| as that is the view the touch was initially down on.
752 v1
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
753 v3
->last_gesture_event_type_
= ui::ET_UNKNOWN
;
754 root
->DispatchGestureEvent(&g1
);
755 EXPECT_EQ(ui::ET_GESTURE_TAP
, v1
->last_gesture_event_type_
);
756 EXPECT_EQ(ui::ET_UNKNOWN
, v3
->last_gesture_event_type_
);
757 EXPECT_EQ("110,110", v1
->location_
.ToString());
762 ////////////////////////////////////////////////////////////////////////////////
764 ////////////////////////////////////////////////////////////////////////////////
766 void TestView::Paint(gfx::Canvas
* canvas
) {
767 canvas
->sk_canvas()->getClipBounds(&last_clip_
);
770 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
771 scheduled_paint_rects_
.push_back(rect
);
772 View::SchedulePaintInRect(rect
);
775 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
776 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
777 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
778 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
779 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
782 /* This test is disabled because it is flakey on some systems.
783 TEST_F(ViewTest, DISABLED_Painting) {
784 // Determine if InvalidateRect generates an empty paint rectangle.
785 EmptyWindow paint_window(CRect(50, 50, 650, 650));
786 paint_window.RedrawWindow(CRect(0, 0, 600, 600), NULL,
787 RDW_UPDATENOW | RDW_INVALIDATE | RDW_ALLCHILDREN);
788 bool empty_paint = paint_window.empty_paint();
790 NativeWidgetWin window;
791 window.set_delete_on_destroy(false);
792 window.set_window_style(WS_OVERLAPPEDWINDOW);
793 window.Init(NULL, gfx::Rect(50, 50, 650, 650), NULL);
794 View* root = window.GetRootView();
796 TestView* v1 = new TestView();
797 v1->SetBoundsRect(gfx::Rect(0, 0, 650, 650));
798 root->AddChildView(v1);
800 TestView* v2 = new TestView();
801 v2->SetBoundsRect(gfx::Rect(10, 10, 80, 80));
802 v1->AddChildView(v2);
804 TestView* v3 = new TestView();
805 v3->SetBoundsRect(gfx::Rect(10, 10, 60, 60));
806 v2->AddChildView(v3);
808 TestView* v4 = new TestView();
809 v4->SetBoundsRect(gfx::Rect(10, 200, 100, 100));
810 v1->AddChildView(v4);
812 // Make sure to paint current rects
813 PaintRootView(root, empty_paint);
820 v3->SchedulePaintInRect(gfx::Rect(10, 10, 10, 10));
821 PaintRootView(root, empty_paint);
825 tmp_rect.iset(10, 10, 20, 20);
826 CheckRect(v3->last_clip_, tmp_rect);
828 tmp_rect.iset(20, 20, 30, 30);
829 CheckRect(v2->last_clip_, tmp_rect);
831 tmp_rect.iset(30, 30, 40, 40);
832 CheckRect(v1->last_clip_, tmp_rect);
834 // Make sure v4 was not painted
836 CheckRect(v4->last_clip_, tmp_rect);
838 window.DestroyWindow();
842 TEST_F(ViewTest
, RemoveNotification
) {
843 ViewStorage
* vs
= ViewStorage::GetInstance();
844 Widget
* widget
= new Widget
;
845 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
846 View
* root_view
= widget
->GetRootView();
849 int s1
= vs
->CreateStorageID();
850 vs
->StoreView(s1
, v1
);
851 root_view
->AddChildView(v1
);
852 View
* v11
= new View
;
853 int s11
= vs
->CreateStorageID();
854 vs
->StoreView(s11
, v11
);
855 v1
->AddChildView(v11
);
856 View
* v111
= new View
;
857 int s111
= vs
->CreateStorageID();
858 vs
->StoreView(s111
, v111
);
859 v11
->AddChildView(v111
);
860 View
* v112
= new View
;
861 int s112
= vs
->CreateStorageID();
862 vs
->StoreView(s112
, v112
);
863 v11
->AddChildView(v112
);
864 View
* v113
= new View
;
865 int s113
= vs
->CreateStorageID();
866 vs
->StoreView(s113
, v113
);
867 v11
->AddChildView(v113
);
868 View
* v1131
= new View
;
869 int s1131
= vs
->CreateStorageID();
870 vs
->StoreView(s1131
, v1131
);
871 v113
->AddChildView(v1131
);
872 View
* v12
= new View
;
873 int s12
= vs
->CreateStorageID();
874 vs
->StoreView(s12
, v12
);
875 v1
->AddChildView(v12
);
878 int s2
= vs
->CreateStorageID();
879 vs
->StoreView(s2
, v2
);
880 root_view
->AddChildView(v2
);
881 View
* v21
= new View
;
882 int s21
= vs
->CreateStorageID();
883 vs
->StoreView(s21
, v21
);
884 v2
->AddChildView(v21
);
885 View
* v211
= new View
;
886 int s211
= vs
->CreateStorageID();
887 vs
->StoreView(s211
, v211
);
888 v21
->AddChildView(v211
);
890 size_t stored_views
= vs
->view_count();
892 // Try removing a leaf view.
893 v21
->RemoveChildView(v211
);
894 EXPECT_EQ(stored_views
- 1, vs
->view_count());
895 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
896 delete v211
; // We won't use this one anymore.
898 // Now try removing a view with a hierarchy of depth 1.
899 v11
->RemoveChildView(v113
);
900 EXPECT_EQ(stored_views
- 3, vs
->view_count());
901 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
902 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
903 delete v113
; // We won't use this one anymore.
905 // Now remove even more.
906 root_view
->RemoveChildView(v1
);
907 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
908 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
909 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
910 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
911 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
913 // Put v1 back for more tests.
914 root_view
->AddChildView(v1
);
915 vs
->StoreView(s1
, v1
);
917 // Synchronously closing the window deletes the view hierarchy, which should
918 // remove all its views from ViewStorage.
920 EXPECT_EQ(stored_views
- 10, vs
->view_count());
921 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
922 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
923 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
924 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
925 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
926 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
927 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
931 class HitTestView
: public View
{
933 explicit HitTestView(bool has_hittest_mask
)
934 : has_hittest_mask_(has_hittest_mask
) {
936 virtual ~HitTestView() {}
939 // Overridden from View:
940 virtual bool HasHitTestMask() const OVERRIDE
{
941 return has_hittest_mask_
;
943 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{
944 DCHECK(has_hittest_mask_
);
947 SkScalar w
= SkIntToScalar(width());
948 SkScalar h
= SkIntToScalar(height());
950 // Create a triangular mask within the bounds of this View.
951 mask
->moveTo(w
/ 2, 0);
958 bool has_hittest_mask_
;
960 DISALLOW_COPY_AND_ASSIGN(HitTestView
);
963 gfx::Point
ConvertPointToView(View
* view
, const gfx::Point
& p
) {
965 View::ConvertPointToTarget(view
->GetWidget()->GetRootView(), view
, &tmp
);
969 gfx::Rect
ConvertRectToView(View
* view
, const gfx::Rect
& r
) {
971 tmp
.set_origin(ConvertPointToView(view
, r
.origin()));
975 void RotateCounterclockwise(gfx::Transform
* transform
) {
976 transform
->matrix().set3x3(0, -1, 0,
981 void RotateClockwise(gfx::Transform
* transform
) {
982 transform
->matrix().set3x3( 0, 1, 0,
989 TEST_F(ViewTest
, HitTestMasks
) {
990 Widget
* widget
= new Widget
;
991 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
992 widget
->Init(params
);
993 View
* root_view
= widget
->GetRootView();
994 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
996 gfx::Rect v1_bounds
= gfx::Rect(0, 0, 100, 100);
997 HitTestView
* v1
= new HitTestView(false);
998 v1
->SetBoundsRect(v1_bounds
);
999 root_view
->AddChildView(v1
);
1001 gfx::Rect v2_bounds
= gfx::Rect(105, 0, 100, 100);
1002 HitTestView
* v2
= new HitTestView(true);
1003 v2
->SetBoundsRect(v2_bounds
);
1004 root_view
->AddChildView(v2
);
1006 gfx::Point v1_centerpoint
= v1_bounds
.CenterPoint();
1007 gfx::Point v2_centerpoint
= v2_bounds
.CenterPoint();
1008 gfx::Point v1_origin
= v1_bounds
.origin();
1009 gfx::Point v2_origin
= v2_bounds
.origin();
1011 gfx::Rect
r1(10, 10, 110, 15);
1012 gfx::Rect
r2(106, 1, 98, 98);
1013 gfx::Rect
r3(0, 0, 300, 300);
1014 gfx::Rect
r4(115, 342, 200, 10);
1016 // Test HitTestPoint
1017 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_centerpoint
)));
1018 EXPECT_TRUE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_centerpoint
)));
1020 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointToView(v1
, v1_origin
)));
1021 EXPECT_FALSE(v2
->HitTestPoint(ConvertPointToView(v2
, v2_origin
)));
1024 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r1
)));
1025 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r1
)));
1027 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r2
)));
1028 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r2
)));
1030 EXPECT_TRUE(v1
->HitTestRect(ConvertRectToView(v1
, r3
)));
1031 EXPECT_TRUE(v2
->HitTestRect(ConvertRectToView(v2
, r3
)));
1033 EXPECT_FALSE(v1
->HitTestRect(ConvertRectToView(v1
, r4
)));
1034 EXPECT_FALSE(v2
->HitTestRect(ConvertRectToView(v2
, r4
)));
1036 // Test GetEventHandlerForPoint
1037 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_centerpoint
));
1038 EXPECT_EQ(v2
, root_view
->GetEventHandlerForPoint(v2_centerpoint
));
1040 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_origin
));
1041 EXPECT_EQ(root_view
, root_view
->GetEventHandlerForPoint(v2_origin
));
1046 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1047 Widget
* widget
= new Widget
;
1048 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1049 widget
->Init(params
);
1050 View
* root_view
= widget
->GetRootView();
1051 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1053 // Have this hierarchy of views (the coords here are in root coord):
1054 // v1 (0, 0, 100, 100)
1055 // - v11 (0, 0, 20, 30)
1056 // - v111 (5, 5, 5, 15)
1057 // - v12 (50, 10, 30, 90)
1058 // - v121 (60, 20, 10, 10)
1059 // v2 (105, 0, 100, 100)
1060 // - v21 (120, 10, 50, 20)
1062 TestView
* v1
= new TestView
;
1063 v1
->SetBounds(0, 0, 100, 100);
1064 root_view
->AddChildView(v1
);
1065 v1
->set_notify_enter_exit_on_child(true);
1067 TestView
* v11
= new TestView
;
1068 v11
->SetBounds(0, 0, 20, 30);
1069 v1
->AddChildView(v11
);
1071 TestView
* v111
= new TestView
;
1072 v111
->SetBounds(5, 5, 5, 15);
1073 v11
->AddChildView(v111
);
1075 TestView
* v12
= new TestView
;
1076 v12
->SetBounds(50, 10, 30, 90);
1077 v1
->AddChildView(v12
);
1079 TestView
* v121
= new TestView
;
1080 v121
->SetBounds(10, 10, 10, 10);
1081 v12
->AddChildView(v121
);
1083 TestView
* v2
= new TestView
;
1084 v2
->SetBounds(105, 0, 100, 100);
1085 root_view
->AddChildView(v2
);
1087 TestView
* v21
= new TestView
;
1088 v21
->SetBounds(15, 10, 50, 20);
1089 v2
->AddChildView(v21
);
1099 // Move the mouse in v111.
1100 gfx::Point
p1(6, 6);
1101 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, 0);
1102 root_view
->OnMouseMoved(move1
);
1103 EXPECT_TRUE(v111
->received_mouse_enter_
);
1104 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1105 EXPECT_TRUE(v1
->received_mouse_enter_
);
1110 // Now, move into v121.
1111 gfx::Point
p2(65, 21);
1112 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, 0);
1113 root_view
->OnMouseMoved(move2
);
1114 EXPECT_TRUE(v111
->received_mouse_exit_
);
1115 EXPECT_TRUE(v121
->received_mouse_enter_
);
1116 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1121 // Now, move into v11.
1122 gfx::Point
p3(1, 1);
1123 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, 0);
1124 root_view
->OnMouseMoved(move3
);
1125 EXPECT_TRUE(v121
->received_mouse_exit_
);
1126 EXPECT_TRUE(v11
->received_mouse_enter_
);
1127 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1133 gfx::Point
p4(121, 15);
1134 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, 0);
1135 root_view
->OnMouseMoved(move4
);
1136 EXPECT_TRUE(v21
->received_mouse_enter_
);
1137 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1138 EXPECT_TRUE(v11
->received_mouse_exit_
);
1139 EXPECT_TRUE(v1
->received_mouse_exit_
);
1146 gfx::Point
p5(21, 0);
1147 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, 0);
1148 root_view
->OnMouseMoved(move5
);
1149 EXPECT_TRUE(v21
->received_mouse_exit_
);
1150 EXPECT_TRUE(v1
->received_mouse_enter_
);
1155 // Now, move into v11.
1156 gfx::Point
p6(15, 15);
1157 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, 0);
1158 root_view
->OnMouseMoved(mouse6
);
1159 EXPECT_TRUE(v11
->received_mouse_enter_
);
1160 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1165 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1166 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1167 // when the mouse leaves v11.
1168 gfx::Point
p7(21, 0);
1169 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, 0);
1170 root_view
->OnMouseMoved(mouse7
);
1171 EXPECT_TRUE(v11
->received_mouse_exit_
);
1172 EXPECT_FALSE(v1
->received_mouse_enter_
);
1177 TEST_F(ViewTest
, Textfield
) {
1178 const string16 kText
= ASCIIToUTF16("Reality is that which, when you stop "
1179 "believing it, doesn't go away.");
1180 const string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1181 const string16 kEmptyString
;
1183 Widget
* widget
= new Widget
;
1184 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1185 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1186 widget
->Init(params
);
1187 View
* root_view
= widget
->GetRootView();
1189 Textfield
* textfield
= new Textfield();
1190 root_view
->AddChildView(textfield
);
1192 // Test setting, appending text.
1193 textfield
->SetText(kText
);
1194 EXPECT_EQ(kText
, textfield
->text());
1195 textfield
->AppendText(kExtraText
);
1196 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1197 textfield
->SetText(string16());
1198 EXPECT_EQ(kEmptyString
, textfield
->text());
1200 // Test selection related methods.
1201 textfield
->SetText(kText
);
1202 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1203 textfield
->SelectAll(false);
1204 EXPECT_EQ(kText
, textfield
->text());
1205 textfield
->ClearSelection();
1206 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
1211 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1212 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1213 const string16 kNormalText
= ASCIIToUTF16("Normal");
1214 const string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1215 const string16 kPasswordText
= ASCIIToUTF16("Password! ** Secret stuff **");
1217 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1219 Widget
* widget
= new Widget
;
1220 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1221 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1222 widget
->Init(params
);
1223 View
* root_view
= widget
->GetRootView();
1225 Textfield
* normal
= new Textfield();
1226 Textfield
* read_only
= new Textfield();
1227 read_only
->SetReadOnly(true);
1228 Textfield
* password
= new Textfield(Textfield::STYLE_OBSCURED
);
1230 root_view
->AddChildView(normal
);
1231 root_view
->AddChildView(read_only
);
1232 root_view
->AddChildView(password
);
1234 normal
->SetText(kNormalText
);
1235 read_only
->SetText(kReadOnlyText
);
1236 password
->SetText(kPasswordText
);
1242 normal
->SelectAll(false);
1243 normal
->ExecuteCommand(IDS_APP_CUT
);
1245 clipboard
->ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
1246 EXPECT_EQ(kNormalText
, result
);
1247 normal
->SetText(kNormalText
); // Let's revert to the original content.
1249 read_only
->SelectAll(false);
1250 read_only
->ExecuteCommand(IDS_APP_CUT
);
1252 clipboard
->ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
1253 // Cut should have failed, so the clipboard content should not have changed.
1254 EXPECT_EQ(kNormalText
, result
);
1256 password
->SelectAll(false);
1257 password
->ExecuteCommand(IDS_APP_CUT
);
1259 clipboard
->ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
1260 // Cut should have failed, so the clipboard content should not have changed.
1261 EXPECT_EQ(kNormalText
, result
);
1267 // Start with |read_only| to observe a change in clipboard text.
1268 read_only
->SelectAll(false);
1269 read_only
->ExecuteCommand(IDS_APP_COPY
);
1271 clipboard
->ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
1272 EXPECT_EQ(kReadOnlyText
, result
);
1274 normal
->SelectAll(false);
1275 normal
->ExecuteCommand(IDS_APP_COPY
);
1277 clipboard
->ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
1278 EXPECT_EQ(kNormalText
, result
);
1280 password
->SelectAll(false);
1281 password
->ExecuteCommand(IDS_APP_COPY
);
1283 clipboard
->ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
1284 // Text cannot be copied from an obscured field; the clipboard won't change.
1285 EXPECT_EQ(kNormalText
, result
);
1291 // Attempting to paste kNormalText in a read-only text-field should fail.
1292 read_only
->SelectAll(false);
1293 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1294 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1296 password
->SelectAll(false);
1297 password
->ExecuteCommand(IDS_APP_PASTE
);
1298 EXPECT_EQ(kNormalText
, password
->text());
1300 // Copy from |read_only| to observe a change in the normal textfield text.
1301 read_only
->SelectAll(false);
1302 read_only
->ExecuteCommand(IDS_APP_COPY
);
1303 normal
->SelectAll(false);
1304 normal
->ExecuteCommand(IDS_APP_PASTE
);
1305 EXPECT_EQ(kReadOnlyText
, normal
->text());
1309 ////////////////////////////////////////////////////////////////////////////////
1311 ////////////////////////////////////////////////////////////////////////////////
1312 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1313 accelerator_count_map_
[accelerator
]++;
1317 #if defined(OS_WIN) && !defined(USE_AURA)
1318 TEST_F(ViewTest
, ActivateAccelerator
) {
1319 // Register a keyboard accelerator before the view is added to a window.
1320 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1321 TestView
* view
= new TestView();
1323 view
->AddAccelerator(return_accelerator
);
1324 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1326 // Create a window and add the view as its child.
1327 scoped_ptr
<Widget
> widget(new Widget
);
1328 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1329 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1330 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1331 widget
->Init(params
);
1332 View
* root
= widget
->GetRootView();
1333 root
->AddChildView(view
);
1336 // Get the focus manager.
1337 FocusManager
* focus_manager
= widget
->GetFocusManager();
1338 ASSERT_TRUE(focus_manager
);
1340 // Hit the return key and see if it takes effect.
1341 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1342 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1344 // Hit the escape key. Nothing should happen.
1345 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1346 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1347 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1348 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1350 // Now register the escape key and hit it again.
1351 view
->AddAccelerator(escape_accelerator
);
1352 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1353 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1354 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1356 // Remove the return key accelerator.
1357 view
->RemoveAccelerator(return_accelerator
);
1358 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1359 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1360 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1362 // Add it again. Hit the return key and the escape key.
1363 view
->AddAccelerator(return_accelerator
);
1364 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1365 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1366 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1367 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1368 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1369 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1371 // Remove all the accelerators.
1372 view
->ResetAccelerators();
1373 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1374 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1375 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1376 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1377 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1378 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1384 #if defined(OS_WIN) && !defined(USE_AURA)
1385 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1386 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1387 TestView
* view
= new TestView();
1389 view
->AddAccelerator(return_accelerator
);
1390 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1392 scoped_ptr
<Widget
> widget(new Widget
);
1393 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1394 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1395 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1396 widget
->Init(params
);
1397 View
* root
= widget
->GetRootView();
1398 root
->AddChildView(view
);
1401 FocusManager
* focus_manager
= widget
->GetFocusManager();
1402 ASSERT_TRUE(focus_manager
);
1404 view
->SetVisible(false);
1405 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1407 view
->SetVisible(true);
1408 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1414 #if defined(OS_WIN) && !defined(USE_AURA)
1415 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1416 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1417 TestView
* view
= new TestView();
1419 view
->AddAccelerator(return_accelerator
);
1420 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1422 scoped_ptr
<Widget
> widget(new Widget
);
1423 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1424 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1425 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1426 widget
->Init(params
);
1427 View
* root
= widget
->GetRootView();
1428 root
->AddChildView(view
);
1430 FocusManager
* focus_manager
= widget
->GetFocusManager();
1431 ASSERT_TRUE(focus_manager
);
1433 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1434 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1437 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1438 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1441 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1442 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1448 #if defined(OS_WIN) && !defined(USE_AURA)
1449 ////////////////////////////////////////////////////////////////////////////////
1450 // Mouse-wheel message rerouting
1451 ////////////////////////////////////////////////////////////////////////////////
1452 class ScrollableTestView
: public View
{
1454 ScrollableTestView() { }
1456 virtual gfx::Size
GetPreferredSize() {
1457 return gfx::Size(100, 10000);
1460 virtual void Layout() {
1461 SizeToPreferredSize();
1465 class TestViewWithControls
: public View
{
1467 TestViewWithControls() {
1468 text_field_
= new Textfield();
1469 AddChildView(text_field_
);
1472 Textfield
* text_field_
;
1475 class SimpleWidgetDelegate
: public WidgetDelegate
{
1477 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1479 virtual void DeleteDelegate() { delete this; }
1481 virtual View
* GetContentsView() { return contents_
; }
1483 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1484 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1490 // Tests that the mouse-wheel messages are correctly rerouted to the window
1492 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1494 // Note that this fails for a variety of reasons:
1495 // - focused view is apparently reset across window activations and never
1496 // properly restored
1497 // - this test depends on you not having any other window visible open under the
1498 // area that it opens the test windows. --beng
1499 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1500 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1501 Widget
* window1
= Widget::CreateWindowWithBounds(
1502 new SimpleWidgetDelegate(view_with_controls
),
1503 gfx::Rect(0, 0, 100, 100));
1505 ScrollView
* scroll_view
= new ScrollView();
1506 scroll_view
->SetContents(new ScrollableTestView());
1507 Widget
* window2
= Widget::CreateWindowWithBounds(
1508 new SimpleWidgetDelegate(scroll_view
),
1509 gfx::Rect(200, 200, 100, 100));
1511 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1513 // Make the window1 active, as this is what it would be in real-world.
1514 window1
->Activate();
1516 // Let's send a mouse-wheel message to the different controls and check that
1517 // it is rerouted to the window under the mouse (effectively scrolling the
1520 // First to the Window's HWND.
1521 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1522 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1523 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1525 // Then the text-field.
1526 ::SendMessage(view_with_controls
->text_field_
->GetTestingHandle(),
1527 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1528 EXPECT_EQ(80, scroll_view
->GetVisibleRect().y());
1530 // Ensure we don't scroll when the mouse is not over that window.
1531 ::SendMessage(view_with_controls
->text_field_
->GetTestingHandle(),
1532 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(50, 50));
1533 EXPECT_EQ(80, scroll_view
->GetVisibleRect().y());
1535 window1
->CloseNow();
1536 window2
->CloseNow();
1540 ////////////////////////////////////////////////////////////////////////////////
1541 // Dialogs' default button
1542 ////////////////////////////////////////////////////////////////////////////////
1544 class MockMenuModel
: public ui::MenuModel
{
1546 MOCK_CONST_METHOD0(HasIcons
, bool());
1547 MOCK_CONST_METHOD0(GetItemCount
, int());
1548 MOCK_CONST_METHOD1(GetTypeAt
, ItemType(int index
));
1549 MOCK_CONST_METHOD1(GetSeparatorTypeAt
, ui::MenuSeparatorType(int index
));
1550 MOCK_CONST_METHOD1(GetCommandIdAt
, int(int index
));
1551 MOCK_CONST_METHOD1(GetLabelAt
, string16(int index
));
1552 MOCK_CONST_METHOD1(IsItemDynamicAt
, bool(int index
));
1553 MOCK_CONST_METHOD1(GetLabelFontAt
, const gfx::Font
* (int index
));
1554 MOCK_CONST_METHOD2(GetAcceleratorAt
, bool(int index
,
1555 ui::Accelerator
* accelerator
));
1556 MOCK_CONST_METHOD1(IsItemCheckedAt
, bool(int index
));
1557 MOCK_CONST_METHOD1(GetGroupIdAt
, int(int index
));
1558 MOCK_METHOD2(GetIconAt
, bool(int index
, gfx::Image
* icon
));
1559 MOCK_CONST_METHOD1(GetButtonMenuItemAt
, ui::ButtonMenuItemModel
*(int index
));
1560 MOCK_CONST_METHOD1(IsEnabledAt
, bool(int index
));
1561 MOCK_CONST_METHOD1(IsVisibleAt
, bool(int index
));
1562 MOCK_CONST_METHOD1(GetSubmenuModelAt
, MenuModel
*(int index
));
1563 MOCK_METHOD1(HighlightChangedTo
, void(int index
));
1564 MOCK_METHOD1(ActivatedAt
, void(int index
));
1565 MOCK_METHOD2(ActivatedAt
, void(int index
, int disposition
));
1566 MOCK_METHOD0(MenuWillShow
, void());
1567 MOCK_METHOD0(MenuClosed
, void());
1568 MOCK_METHOD1(SetMenuModelDelegate
, void(ui::MenuModelDelegate
* delegate
));
1569 MOCK_CONST_METHOD0(GetMenuModelDelegate
, ui::MenuModelDelegate
*());
1570 MOCK_METHOD3(GetModelAndIndexForCommandId
, bool(int command_id
,
1571 MenuModel
** model
, int* index
));
1574 class TestDialog
: public DialogDelegate
, public ButtonListener
{
1576 explicit TestDialog(MockMenuModel
* mock_menu_model
)
1582 last_pressed_button_(NULL
),
1583 mock_menu_model_(mock_menu_model
),
1591 // Now we can close safely.
1595 // delegate has to be alive while shutting down.
1596 MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
1599 // DialogDelegate implementation:
1600 virtual int GetDefaultDialogButton() const OVERRIDE
{
1601 return ui::DIALOG_BUTTON_OK
;
1604 virtual View
* GetContentsView() OVERRIDE
{
1606 contents_
= new View
;
1607 button1_
= new LabelButton(this, ASCIIToUTF16("Button1"));
1608 button2_
= new LabelButton(this, ASCIIToUTF16("Button2"));
1609 checkbox_
= new Checkbox(ASCIIToUTF16("My checkbox"));
1610 button_drop_
= new ButtonDropDown(this, mock_menu_model_
);
1611 contents_
->AddChildView(button1_
);
1612 contents_
->AddChildView(button2_
);
1613 contents_
->AddChildView(checkbox_
);
1614 contents_
->AddChildView(button_drop_
);
1619 // Prevent the dialog from really closing (so we can click the OK/Cancel
1620 // buttons to our heart's content).
1621 virtual bool Cancel() OVERRIDE
{
1625 virtual bool Accept() OVERRIDE
{
1630 virtual Widget
* GetWidget() OVERRIDE
{
1633 virtual const Widget
* GetWidget() const OVERRIDE
{
1637 // ButtonListener implementation.
1638 virtual void ButtonPressed(Button
* sender
, const ui::Event
& event
) OVERRIDE
{
1639 last_pressed_button_
= sender
;
1642 void ResetStates() {
1645 last_pressed_button_
= NULL
;
1648 // Set up expectations for methods that are called when an (empty) menu is
1649 // shown from a drop down button.
1650 void ExpectShowDropMenu() {
1651 if (mock_menu_model_
) {
1652 EXPECT_CALL(*mock_menu_model_
, HasIcons());
1653 EXPECT_CALL(*mock_menu_model_
, GetItemCount());
1654 EXPECT_CALL(*mock_menu_model_
, MenuClosed());
1659 LabelButton
* button1_
;
1660 LabelButton
* button2_
;
1661 Checkbox
* checkbox_
;
1662 ButtonDropDown
* button_drop_
;
1663 Button
* last_pressed_button_
;
1664 MockMenuModel
* mock_menu_model_
;
1672 class DefaultButtonTest
: public ViewTest
{
1682 : focus_manager_(NULL
),
1686 cancel_button_(NULL
) {
1689 virtual void SetUp() OVERRIDE
{
1691 test_dialog_
= new TestDialog(NULL
);
1693 Widget
* window
= new Widget
;
1694 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
1695 params
.delegate
= test_dialog_
;
1696 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1697 window
->Init(params
);
1699 test_dialog_
->widget_
= window
;
1701 focus_manager_
= test_dialog_
->contents_
->GetFocusManager();
1702 ASSERT_TRUE(focus_manager_
!= NULL
);
1704 static_cast<DialogClientView
*>(window
->client_view());
1705 ok_button_
= client_view_
->ok_button();
1706 cancel_button_
= client_view_
->cancel_button();
1709 virtual void TearDown() OVERRIDE
{
1710 test_dialog_
->TearDown();
1711 ViewTest::TearDown();
1714 void SimulatePressingEnterAndCheckDefaultButton(ButtonID button_id
) {
1715 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_RETURN
, 0, false);
1716 focus_manager_
->OnKeyEvent(event
);
1717 switch (button_id
) {
1719 EXPECT_TRUE(test_dialog_
->oked_
);
1720 EXPECT_FALSE(test_dialog_
->canceled_
);
1721 EXPECT_FALSE(test_dialog_
->last_pressed_button_
);
1724 EXPECT_FALSE(test_dialog_
->oked_
);
1725 EXPECT_TRUE(test_dialog_
->canceled_
);
1726 EXPECT_FALSE(test_dialog_
->last_pressed_button_
);
1729 EXPECT_FALSE(test_dialog_
->oked_
);
1730 EXPECT_FALSE(test_dialog_
->canceled_
);
1731 EXPECT_TRUE(test_dialog_
->last_pressed_button_
==
1732 test_dialog_
->button1_
);
1735 EXPECT_FALSE(test_dialog_
->oked_
);
1736 EXPECT_FALSE(test_dialog_
->canceled_
);
1737 EXPECT_TRUE(test_dialog_
->last_pressed_button_
==
1738 test_dialog_
->button2_
);
1741 test_dialog_
->ResetStates();
1744 FocusManager
* focus_manager_
;
1745 TestDialog
* test_dialog_
;
1746 DialogClientView
* client_view_
;
1747 LabelButton
* ok_button_
;
1748 LabelButton
* cancel_button_
;
1751 TEST_F(DefaultButtonTest
, DialogDefaultButtonTest
) {
1752 // Window has just been shown, we expect the default button specified in the
1754 EXPECT_TRUE(ok_button_
->is_default());
1756 // Simulate pressing enter, that should trigger the OK button.
1757 SimulatePressingEnterAndCheckDefaultButton(OK
);
1759 // Simulate focusing another button, it should become the default button.
1760 client_view_
->OnWillChangeFocus(ok_button_
, test_dialog_
->button1_
);
1761 EXPECT_FALSE(ok_button_
->is_default());
1762 EXPECT_TRUE(test_dialog_
->button1_
->is_default());
1763 // Simulate pressing enter, that should trigger button1.
1764 SimulatePressingEnterAndCheckDefaultButton(BUTTON1
);
1766 // Now select something that is not a button, the OK should become the default
1768 client_view_
->OnWillChangeFocus(test_dialog_
->button1_
,
1769 test_dialog_
->checkbox_
);
1770 EXPECT_TRUE(ok_button_
->is_default());
1771 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1772 SimulatePressingEnterAndCheckDefaultButton(OK
);
1774 // Select yet another button.
1775 client_view_
->OnWillChangeFocus(test_dialog_
->checkbox_
,
1776 test_dialog_
->button2_
);
1777 EXPECT_FALSE(ok_button_
->is_default());
1778 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1779 EXPECT_TRUE(test_dialog_
->button2_
->is_default());
1780 SimulatePressingEnterAndCheckDefaultButton(BUTTON2
);
1783 client_view_
->OnWillChangeFocus(test_dialog_
->button2_
, NULL
);
1784 EXPECT_TRUE(ok_button_
->is_default());
1785 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1786 EXPECT_FALSE(test_dialog_
->button2_
->is_default());
1787 SimulatePressingEnterAndCheckDefaultButton(OK
);
1789 // Focus the cancel button.
1790 client_view_
->OnWillChangeFocus(NULL
, cancel_button_
);
1791 EXPECT_FALSE(ok_button_
->is_default());
1792 EXPECT_TRUE(cancel_button_
->is_default());
1793 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1794 EXPECT_FALSE(test_dialog_
->button2_
->is_default());
1795 SimulatePressingEnterAndCheckDefaultButton(CANCEL
);
1798 class ButtonDropDownTest
: public ViewTest
{
1800 ButtonDropDownTest()
1801 : test_dialog_(NULL
),
1802 button_as_view_(NULL
) {
1805 virtual void SetUp() OVERRIDE
{
1807 test_dialog_
= new TestDialog(new MockMenuModel());
1809 Widget
* window
= new Widget
;
1810 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
1811 params
.delegate
= test_dialog_
;
1812 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1813 window
->Init(params
);
1815 test_dialog_
->widget_
= window
;
1817 test_dialog_
->button_drop_
->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1818 // We have to cast the button back into a View in order to invoke it's
1819 // OnMouseReleased method.
1820 button_as_view_
= static_cast<View
*>(test_dialog_
->button_drop_
);
1823 virtual void TearDown() OVERRIDE
{
1824 test_dialog_
->TearDown();
1825 ViewTest::TearDown();
1828 TestDialog
* test_dialog_
;
1829 // This is owned by test_dialog_.
1830 View
* button_as_view_
;
1833 DISALLOW_COPY_AND_ASSIGN(ButtonDropDownTest
);
1836 // Ensure that regular clicks on the drop down button still work. (i.e. - the
1837 // click events are processed and the listener gets the click)
1838 TEST_F(ButtonDropDownTest
, RegularClickTest
) {
1839 gfx::Point
point(1, 1);
1840 ui::MouseEvent
press_event(ui::ET_MOUSE_PRESSED
, point
, point
,
1841 ui::EF_LEFT_MOUSE_BUTTON
);
1842 ui::MouseEvent
release_event(ui::ET_MOUSE_RELEASED
, point
, point
,
1843 ui::EF_LEFT_MOUSE_BUTTON
);
1844 button_as_view_
->OnMousePressed(press_event
);
1845 button_as_view_
->OnMouseReleased(release_event
);
1846 EXPECT_EQ(test_dialog_
->last_pressed_button_
, test_dialog_
->button_drop_
);
1849 ////////////////////////////////////////////////////////////////////////////////
1850 // Native view hierachy
1851 ////////////////////////////////////////////////////////////////////////////////
1852 class TestNativeViewHierarchy
: public View
{
1854 TestNativeViewHierarchy() {
1857 virtual void NativeViewHierarchyChanged(
1859 gfx::NativeView native_view
,
1860 internal::RootView
* root_view
) OVERRIDE
{
1861 NotificationInfo info
;
1862 info
.attached
= attached
;
1863 info
.native_view
= native_view
;
1864 info
.root_view
= root_view
;
1865 notifications_
.push_back(info
);
1867 struct NotificationInfo
{
1869 gfx::NativeView native_view
;
1870 internal::RootView
* root_view
;
1872 static const size_t kTotalViews
= 2;
1873 std::vector
<NotificationInfo
> notifications_
;
1876 class TestChangeNativeViewHierarchy
{
1878 explicit TestChangeNativeViewHierarchy(ViewTest
*view_test
) {
1879 view_test_
= view_test
;
1880 native_host_
= new NativeViewHost();
1882 Widget::InitParams params
=
1883 view_test
->CreateParams(Widget::InitParams::TYPE_POPUP
);
1884 params
.bounds
= gfx::Rect(0, 0, 500, 300);
1885 host_
->Init(params
);
1886 host_
->GetRootView()->AddChildView(native_host_
);
1887 for (size_t i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1888 windows_
[i
] = new Widget
;
1889 Widget::InitParams
params(Widget::InitParams::TYPE_CONTROL
);
1890 params
.parent
= host_
->GetNativeView();
1891 params
.bounds
= gfx::Rect(0, 0, 500, 300);
1892 windows_
[i
]->Init(params
);
1893 root_views_
[i
] = windows_
[i
]->GetRootView();
1894 test_views_
[i
] = new TestNativeViewHierarchy
;
1895 root_views_
[i
]->AddChildView(test_views_
[i
]);
1899 ~TestChangeNativeViewHierarchy() {
1900 for (size_t i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1901 windows_
[i
]->Close();
1904 // Will close and self-delete widgets - no need to manually delete them.
1905 view_test_
->RunPendingMessages();
1908 void CheckEnumeratingNativeWidgets() {
1909 if (!host_
->GetTopLevelWidget())
1911 Widget::Widgets widgets
;
1912 Widget::GetAllChildWidgets(host_
->GetNativeView(), &widgets
);
1913 EXPECT_EQ(TestNativeViewHierarchy::kTotalViews
+ 1, widgets
.size());
1914 // Unfortunately there is no guarantee the sequence of views here so always
1915 // go through all of them.
1916 for (Widget::Widgets::iterator i
= widgets
.begin();
1917 i
!= widgets
.end(); ++i
) {
1918 View
* root_view
= (*i
)->GetRootView();
1919 if (host_
->GetRootView() == root_view
)
1922 for (j
= 0; j
< TestNativeViewHierarchy::kTotalViews
; ++j
)
1923 if (root_views_
[j
] == root_view
)
1925 // EXPECT_LT/GT/GE() fails to compile with class-defined constants
1926 // with gcc, with error
1927 // "error: undefined reference to 'TestNativeViewHierarchy::kTotalViews'"
1928 // so I forced to use EXPECT_TRUE() instead.
1929 EXPECT_TRUE(TestNativeViewHierarchy::kTotalViews
> j
);
1933 void CheckChangingHierarhy() {
1935 for (i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1936 // TODO(georgey): use actual hierarchy changes to send notifications.
1937 static_cast<internal::RootView
*>(root_views_
[i
])->
1938 NotifyNativeViewHierarchyChanged(false, host_
->GetNativeView());
1939 static_cast<internal::RootView
*>(root_views_
[i
])->
1940 NotifyNativeViewHierarchyChanged(true, host_
->GetNativeView());
1942 for (i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1943 ASSERT_EQ(static_cast<size_t>(2), test_views_
[i
]->notifications_
.size());
1944 EXPECT_FALSE(test_views_
[i
]->notifications_
[0].attached
);
1945 EXPECT_EQ(host_
->GetNativeView(),
1946 test_views_
[i
]->notifications_
[0].native_view
);
1947 EXPECT_EQ(root_views_
[i
], test_views_
[i
]->notifications_
[0].root_view
);
1948 EXPECT_TRUE(test_views_
[i
]->notifications_
[1].attached
);
1949 EXPECT_EQ(host_
->GetNativeView(),
1950 test_views_
[i
]->notifications_
[1].native_view
);
1951 EXPECT_EQ(root_views_
[i
], test_views_
[i
]->notifications_
[1].root_view
);
1955 NativeViewHost
* native_host_
;
1957 Widget
* windows_
[TestNativeViewHierarchy::kTotalViews
];
1958 View
* root_views_
[TestNativeViewHierarchy::kTotalViews
];
1959 TestNativeViewHierarchy
* test_views_
[TestNativeViewHierarchy::kTotalViews
];
1960 ViewTest
* view_test_
;
1963 TEST_F(ViewTest
, ChangeNativeViewHierarchyFindRoots
) {
1964 // TODO(georgey): Fix the test for Linux
1966 TestChangeNativeViewHierarchy
test(this);
1967 test
.CheckEnumeratingNativeWidgets();
1971 TEST_F(ViewTest
, ChangeNativeViewHierarchyChangeHierarchy
) {
1972 // TODO(georgey): Fix the test for Linux
1974 TestChangeNativeViewHierarchy
test(this);
1975 test
.CheckChangingHierarhy();
1979 ////////////////////////////////////////////////////////////////////////////////
1981 ////////////////////////////////////////////////////////////////////////////////
1983 class TransformPaintView
: public TestView
{
1985 TransformPaintView() {}
1986 virtual ~TransformPaintView() {}
1988 void ClearScheduledPaintRect() {
1989 scheduled_paint_rect_
= gfx::Rect();
1992 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1994 // Overridden from View:
1995 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
{
1996 gfx::Rect xrect
= ConvertRectToParent(rect
);
1997 scheduled_paint_rect_
.Union(xrect
);
2001 gfx::Rect scheduled_paint_rect_
;
2003 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
2006 TEST_F(ViewTest
, TransformPaint
) {
2007 TransformPaintView
* v1
= new TransformPaintView();
2008 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2010 TestView
* v2
= new TestView();
2011 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2013 Widget
* widget
= new Widget
;
2014 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2015 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2016 widget
->Init(params
);
2018 View
* root
= widget
->GetRootView();
2020 root
->AddChildView(v1
);
2021 v1
->AddChildView(v2
);
2023 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2024 v1
->ClearScheduledPaintRect();
2025 v2
->SchedulePaint();
2027 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
2029 // Rotate |v1| counter-clockwise.
2030 gfx::Transform transform
;
2031 RotateCounterclockwise(&transform
);
2032 transform
.matrix().set(1, 3, 500.0);
2033 v1
->SetTransform(transform
);
2035 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2037 v1
->ClearScheduledPaintRect();
2038 v2
->SchedulePaint();
2040 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
2045 TEST_F(ViewTest
, TransformEvent
) {
2046 TestView
* v1
= new TestView();
2047 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2049 TestView
* v2
= new TestView();
2050 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2052 Widget
* widget
= new Widget
;
2053 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2054 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2055 widget
->Init(params
);
2056 View
* root
= widget
->GetRootView();
2058 root
->AddChildView(v1
);
2059 v1
->AddChildView(v2
);
2061 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2063 // Rotate |v1| counter-clockwise.
2064 gfx::Transform
transform(v1
->GetTransform());
2065 RotateCounterclockwise(&transform
);
2066 transform
.matrix().set(1, 3, 500.0);
2067 v1
->SetTransform(transform
);
2069 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2073 gfx::Point
p1(110, 210);
2074 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
,
2075 ui::EF_LEFT_MOUSE_BUTTON
);
2076 root
->OnMousePressed(pressed
);
2077 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2078 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2079 EXPECT_EQ(190, v2
->location_
.x());
2080 EXPECT_EQ(10, v2
->location_
.y());
2082 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), 0);
2083 root
->OnMouseReleased(released
);
2085 // Now rotate |v2| inside |v1| clockwise.
2086 transform
= v2
->GetTransform();
2087 RotateClockwise(&transform
);
2088 transform
.matrix().setDouble(0, 3, 100.0);
2089 v2
->SetTransform(transform
);
2091 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
2092 // (300, 400) in |root|.
2097 gfx::Point
point2(110, 320);
2098 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
,
2099 ui::EF_LEFT_MOUSE_BUTTON
);
2100 root
->OnMousePressed(p2
);
2101 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2102 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2103 EXPECT_EQ(10, v2
->location_
.x());
2104 EXPECT_EQ(20, v2
->location_
.y());
2106 root
->OnMouseReleased(released
);
2108 v1
->SetTransform(gfx::Transform());
2109 v2
->SetTransform(gfx::Transform());
2111 TestView
* v3
= new TestView();
2112 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
2113 v2
->AddChildView(v3
);
2115 // Rotate |v3| clockwise with respect to |v2|.
2116 transform
= v1
->GetTransform();
2117 RotateClockwise(&transform
);
2118 transform
.matrix().setDouble(0, 3, 30.0);
2119 v3
->SetTransform(transform
);
2121 // Scale |v2| with respect to |v1| along both axis.
2122 transform
= v2
->GetTransform();
2123 transform
.matrix().setDouble(0, 0, 0.8);
2124 transform
.matrix().setDouble(1, 1, 0.5);
2125 v2
->SetTransform(transform
);
2127 // |v3| occupies (108, 105) to (132, 115) in |root|.
2133 gfx::Point
point(112, 110);
2134 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
,
2135 ui::EF_LEFT_MOUSE_BUTTON
);
2136 root
->OnMousePressed(p3
);
2138 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2139 EXPECT_EQ(10, v3
->location_
.x());
2140 EXPECT_EQ(25, v3
->location_
.y());
2142 root
->OnMouseReleased(released
);
2144 v1
->SetTransform(gfx::Transform());
2145 v2
->SetTransform(gfx::Transform());
2146 v3
->SetTransform(gfx::Transform());
2152 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
2153 transform
= v3
->GetTransform();
2154 RotateClockwise(&transform
);
2155 transform
.matrix().setDouble(0, 3, 30.0);
2156 // Rotation sets some scaling transformation. Using SetScale would overwrite
2157 // that and pollute the rotation. So combine the scaling with the existing
2159 gfx::Transform scale
;
2160 scale
.Scale(0.8, 0.5);
2161 transform
.ConcatTransform(scale
);
2162 v3
->SetTransform(transform
);
2164 // Translate |v2| with respect to |v1|.
2165 transform
= v2
->GetTransform();
2166 transform
.matrix().setDouble(0, 3, 10.0);
2167 transform
.matrix().setDouble(1, 3, 10.0);
2168 v2
->SetTransform(transform
);
2170 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2172 gfx::Point
point3(124, 125);
2173 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
,
2174 ui::EF_LEFT_MOUSE_BUTTON
);
2175 root
->OnMousePressed(p4
);
2177 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2178 EXPECT_EQ(10, v3
->location_
.x());
2179 EXPECT_EQ(25, v3
->location_
.y());
2181 root
->OnMouseReleased(released
);
2186 TEST_F(ViewTest
, TransformVisibleBound
) {
2187 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2189 scoped_ptr
<Widget
> widget(new Widget
);
2190 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2191 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2192 params
.bounds
= viewport_bounds
;
2193 widget
->Init(params
);
2194 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2196 View
* viewport
= new View
;
2197 widget
->SetContentsView(viewport
);
2198 View
* contents
= new View
;
2199 viewport
->AddChildView(contents
);
2200 viewport
->SetBoundsRect(viewport_bounds
);
2201 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2203 View
* child
= new View
;
2204 contents
->AddChildView(child
);
2205 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2206 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
2208 // Rotate |child| counter-clockwise
2209 gfx::Transform transform
;
2210 RotateCounterclockwise(&transform
);
2211 transform
.matrix().setDouble(1, 3, 50.0);
2212 child
->SetTransform(transform
);
2213 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
2218 ////////////////////////////////////////////////////////////////////////////////
2219 // OnVisibleBoundsChanged()
2221 class VisibleBoundsView
: public View
{
2223 VisibleBoundsView() : received_notification_(false) {}
2224 virtual ~VisibleBoundsView() {}
2226 bool received_notification() const { return received_notification_
; }
2227 void set_received_notification(bool received
) {
2228 received_notification_
= received
;
2232 // Overridden from View:
2233 virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE
{
2236 virtual void OnVisibleBoundsChanged() OVERRIDE
{
2237 received_notification_
= true;
2240 bool received_notification_
;
2242 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2245 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2246 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2248 scoped_ptr
<Widget
> widget(new Widget
);
2249 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2250 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2251 params
.bounds
= viewport_bounds
;
2252 widget
->Init(params
);
2253 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2255 View
* viewport
= new View
;
2256 widget
->SetContentsView(viewport
);
2257 View
* contents
= new View
;
2258 viewport
->AddChildView(contents
);
2259 viewport
->SetBoundsRect(viewport_bounds
);
2260 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2262 // Create a view that cares about visible bounds notifications, and position
2263 // it just outside the visible bounds of the viewport.
2264 VisibleBoundsView
* child
= new VisibleBoundsView
;
2265 contents
->AddChildView(child
);
2266 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2268 // The child bound should be fully clipped.
2269 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2271 // Now scroll the contents, but not enough to make the child visible.
2272 contents
->SetY(contents
->y() - 1);
2274 // We should have received the notification since the visible bounds may have
2275 // changed (even though they didn't).
2276 EXPECT_TRUE(child
->received_notification());
2277 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2278 child
->set_received_notification(false);
2280 // Now scroll the contents, this time by enough to make the child visible by
2282 contents
->SetY(contents
->y() - 10);
2283 EXPECT_TRUE(child
->received_notification());
2284 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2285 child
->set_received_notification(false);
2290 ////////////////////////////////////////////////////////////////////////////////
2293 TEST_F(ViewTest
, SetBoundsPaint
) {
2295 TestView
* child_view
= new TestView
;
2297 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2298 top_view
.scheduled_paint_rects_
.clear();
2299 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2300 top_view
.AddChildView(child_view
);
2302 top_view
.scheduled_paint_rects_
.clear();
2303 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2304 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2306 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2307 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2308 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2309 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2312 // Tests conversion methods with a transform.
2313 TEST_F(ViewTest
, ConvertPointToViewWithTransform
) {
2315 TestView
* child
= new TestView
;
2316 TestView
* child_child
= new TestView
;
2318 top_view
.AddChildView(child
);
2319 child
->AddChildView(child_child
);
2321 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2323 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2324 gfx::Transform transform
;
2325 transform
.Scale(3.0, 4.0);
2326 child
->SetTransform(transform
);
2328 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2329 transform
.MakeIdentity();
2330 transform
.Scale(5.0, 7.0);
2331 child_child
->SetTransform(transform
);
2333 // Sanity check to make sure basic transforms act as expected.
2335 gfx::Transform transform
;
2336 transform
.Translate(110.0, -110.0);
2337 transform
.Scale(100.0, 55.0);
2338 transform
.Translate(1.0, 1.0);
2340 // convert to a 3x3 matrix.
2341 const SkMatrix
& matrix
= transform
.matrix();
2343 EXPECT_EQ(210, matrix
.getTranslateX());
2344 EXPECT_EQ(-55, matrix
.getTranslateY());
2345 EXPECT_EQ(100, matrix
.getScaleX());
2346 EXPECT_EQ(55, matrix
.getScaleY());
2347 EXPECT_EQ(0, matrix
.getSkewX());
2348 EXPECT_EQ(0, matrix
.getSkewY());
2352 gfx::Transform transform
;
2353 transform
.Translate(1.0, 1.0);
2355 t2
.Scale(100.0, 55.0);
2357 t3
.Translate(110.0, -110.0);
2358 transform
.ConcatTransform(t2
);
2359 transform
.ConcatTransform(t3
);
2361 // convert to a 3x3 matrix
2362 const SkMatrix
& matrix
= transform
.matrix();
2364 EXPECT_EQ(210, matrix
.getTranslateX());
2365 EXPECT_EQ(-55, matrix
.getTranslateY());
2366 EXPECT_EQ(100, matrix
.getScaleX());
2367 EXPECT_EQ(55, matrix
.getScaleY());
2368 EXPECT_EQ(0, matrix
.getSkewX());
2369 EXPECT_EQ(0, matrix
.getSkewY());
2372 // Conversions from child->top and top->child.
2374 gfx::Point
point(5, 5);
2375 View::ConvertPointToTarget(child
, &top_view
, &point
);
2376 EXPECT_EQ(22, point
.x());
2377 EXPECT_EQ(39, point
.y());
2379 point
.SetPoint(22, 39);
2380 View::ConvertPointToTarget(&top_view
, child
, &point
);
2381 EXPECT_EQ(5, point
.x());
2382 EXPECT_EQ(5, point
.y());
2385 // Conversions from child_child->top and top->child_child.
2387 gfx::Point
point(5, 5);
2388 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2389 EXPECT_EQ(133, point
.x());
2390 EXPECT_EQ(211, point
.y());
2392 point
.SetPoint(133, 211);
2393 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2394 EXPECT_EQ(5, point
.x());
2395 EXPECT_EQ(5, point
.y());
2398 // Conversions from child_child->child and child->child_child
2400 gfx::Point
point(5, 5);
2401 View::ConvertPointToTarget(child_child
, child
, &point
);
2402 EXPECT_EQ(42, point
.x());
2403 EXPECT_EQ(48, point
.y());
2405 point
.SetPoint(42, 48);
2406 View::ConvertPointToTarget(child
, child_child
, &point
);
2407 EXPECT_EQ(5, point
.x());
2408 EXPECT_EQ(5, point
.y());
2411 // Conversions from top_view to child with a value that should be negative.
2412 // This ensures we don't round up with negative numbers.
2414 gfx::Point
point(6, 18);
2415 View::ConvertPointToTarget(&top_view
, child
, &point
);
2416 EXPECT_EQ(-1, point
.x());
2417 EXPECT_EQ(-1, point
.y());
2421 // Tests conversion methods for rectangles.
2422 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2423 scoped_ptr
<Widget
> widget(new Widget
);
2424 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2425 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2426 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2427 widget
->Init(params
);
2428 View
* root
= widget
->GetRootView();
2430 TestView
* v1
= new TestView
;
2431 TestView
* v2
= new TestView
;
2432 root
->AddChildView(v1
);
2433 v1
->AddChildView(v2
);
2435 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2436 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2438 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2439 gfx::Rect
rect(5, 5, 15, 40);
2440 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2441 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2445 RotateCounterclockwise(&t2
);
2446 t2
.matrix().setDouble(1, 3, 100.0);
2447 v2
->SetTransform(t2
);
2449 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2450 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2451 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2456 v1
->SetTransform(t1
);
2458 // The rectangle should remain the same for |v1|.
2459 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2461 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2462 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2463 v2
->ConvertRectToWidget(rect
).ToString());
2468 class ObserverView
: public View
{
2471 virtual ~ObserverView();
2473 void ResetTestState();
2475 bool child_added() const { return child_added_
; }
2476 bool child_removed() const { return child_removed_
; }
2477 const View
* parent_view() const { return parent_view_
; }
2478 const View
* child_view() const { return child_view_
; }
2482 virtual void ViewHierarchyChanged(bool is_add
,
2484 View
* child
) OVERRIDE
;
2487 bool child_removed_
;
2491 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2494 ObserverView::ObserverView()
2495 : child_added_(false),
2496 child_removed_(false),
2501 ObserverView::~ObserverView() {}
2503 void ObserverView::ResetTestState() {
2504 child_added_
= false;
2505 child_removed_
= false;
2506 parent_view_
= NULL
;
2510 void ObserverView::ViewHierarchyChanged(bool is_add
,
2514 child_added_
= true;
2516 child_removed_
= true;
2518 parent_view_
= parent
;
2519 child_view_
= child
;
2522 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2523 // a child view is added or removed to all the views in the hierarchy (up and
2525 // The tree looks like this:
2529 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2532 ObserverView
* v3
= new ObserverView();
2534 // Add |v3| to |v2|.
2535 scoped_ptr
<ObserverView
> v2(new ObserverView());
2536 v2
->AddChildView(v3
);
2538 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2540 EXPECT_TRUE(v2
->child_added());
2541 EXPECT_FALSE(v2
->child_removed());
2542 EXPECT_EQ(v2
.get(), v2
->parent_view());
2543 EXPECT_EQ(v3
, v2
->child_view());
2545 EXPECT_TRUE(v3
->child_added());
2546 EXPECT_FALSE(v3
->child_removed());
2547 EXPECT_EQ(v2
.get(), v3
->parent_view());
2548 EXPECT_EQ(v3
, v3
->child_view());
2550 // Reset everything to the initial state.
2551 v2
->ResetTestState();
2552 v3
->ResetTestState();
2555 v1
.AddChildView(v2
.get());
2557 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2558 // Make sure all the views (v1, v2, v3) received _that_ information.
2559 EXPECT_TRUE(v1
.child_added());
2560 EXPECT_FALSE(v1
.child_removed());
2561 EXPECT_EQ(&v1
, v1
.parent_view());
2562 EXPECT_EQ(v2
.get(), v1
.child_view());
2564 EXPECT_TRUE(v2
->child_added());
2565 EXPECT_FALSE(v2
->child_removed());
2566 EXPECT_EQ(&v1
, v2
->parent_view());
2567 EXPECT_EQ(v2
.get(), v2
->child_view());
2569 EXPECT_TRUE(v3
->child_added());
2570 EXPECT_FALSE(v3
->child_removed());
2571 EXPECT_EQ(&v1
, v3
->parent_view());
2572 EXPECT_EQ(v2
.get(), v3
->child_view());
2574 // Reset everything to the initial state.
2575 v1
.ResetTestState();
2576 v2
->ResetTestState();
2577 v3
->ResetTestState();
2579 // Remove |v2| from |v1|.
2580 v1
.RemoveChildView(v2
.get());
2582 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2583 // Make sure all the views (v1, v2, v3) received _that_ information.
2584 EXPECT_FALSE(v1
.child_added());
2585 EXPECT_TRUE(v1
.child_removed());
2586 EXPECT_EQ(&v1
, v1
.parent_view());
2587 EXPECT_EQ(v2
.get(), v1
.child_view());
2589 EXPECT_FALSE(v2
->child_added());
2590 EXPECT_TRUE(v2
->child_removed());
2591 EXPECT_EQ(&v1
, v2
->parent_view());
2592 EXPECT_EQ(v2
.get(), v2
->child_view());
2594 EXPECT_FALSE(v3
->child_added());
2595 EXPECT_TRUE(v3
->child_removed());
2596 EXPECT_EQ(&v1
, v3
->parent_view());
2597 EXPECT_EQ(v3
, v3
->child_view());
2600 // Verifies if the child views added under the root are all deleted when calling
2601 // RemoveAllChildViews.
2602 // The tree looks like this:
2611 TEST_F(ViewTest
, RemoveAllChildViews
) {
2614 View
* child1
= new View
;
2615 root
.AddChildView(child1
);
2617 for (int i
= 0; i
< 2; ++i
)
2618 root
.AddChildView(new View
);
2620 View
* foo
= new View
;
2621 child1
->AddChildView(foo
);
2623 // Add some nodes to |foo|.
2624 for (int i
= 0; i
< 3; ++i
)
2625 foo
->AddChildView(new View
);
2627 EXPECT_EQ(3, root
.child_count());
2628 EXPECT_EQ(1, child1
->child_count());
2629 EXPECT_EQ(3, foo
->child_count());
2631 // Now remove all child views from root.
2632 root
.RemoveAllChildViews(true);
2634 EXPECT_EQ(0, root
.child_count());
2635 EXPECT_FALSE(root
.has_children());
2638 TEST_F(ViewTest
, Contains
) {
2640 View
* v2
= new View
;
2641 View
* v3
= new View
;
2643 v1
.AddChildView(v2
);
2644 v2
->AddChildView(v3
);
2646 EXPECT_FALSE(v1
.Contains(NULL
));
2647 EXPECT_TRUE(v1
.Contains(&v1
));
2648 EXPECT_TRUE(v1
.Contains(v2
));
2649 EXPECT_TRUE(v1
.Contains(v3
));
2651 EXPECT_FALSE(v2
->Contains(NULL
));
2652 EXPECT_TRUE(v2
->Contains(v2
));
2653 EXPECT_FALSE(v2
->Contains(&v1
));
2654 EXPECT_TRUE(v2
->Contains(v3
));
2656 EXPECT_FALSE(v3
->Contains(NULL
));
2657 EXPECT_TRUE(v3
->Contains(v3
));
2658 EXPECT_FALSE(v3
->Contains(&v1
));
2659 EXPECT_FALSE(v3
->Contains(v2
));
2662 // Verifies if GetIndexOf() returns the correct index for the specified child
2664 // The tree looks like this:
2669 TEST_F(ViewTest
, GetIndexOf
) {
2672 View
* child1
= new View
;
2673 root
.AddChildView(child1
);
2675 View
* child2
= new View
;
2676 root
.AddChildView(child2
);
2678 View
* foo1
= new View
;
2679 child1
->AddChildView(foo1
);
2681 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2682 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2683 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2684 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2685 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2687 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2688 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2689 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2690 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2691 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2693 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2694 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2695 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2696 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2697 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2700 // Verifies that the child views can be reordered correctly.
2701 TEST_F(ViewTest
, ReorderChildren
) {
2704 View
* child
= new View();
2705 root
.AddChildView(child
);
2707 View
* foo1
= new View();
2708 child
->AddChildView(foo1
);
2709 View
* foo2
= new View();
2710 child
->AddChildView(foo2
);
2711 View
* foo3
= new View();
2712 child
->AddChildView(foo3
);
2713 foo1
->set_focusable(true);
2714 foo2
->set_focusable(true);
2715 foo3
->set_focusable(true);
2717 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2718 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2719 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2720 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2721 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2722 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2724 // Move |foo2| at the end.
2725 child
->ReorderChildView(foo2
, -1);
2726 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2727 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2728 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2729 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2730 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2731 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2733 // Move |foo1| at the end.
2734 child
->ReorderChildView(foo1
, -1);
2735 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2736 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2737 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2738 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2739 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2740 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2741 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2743 // Move |foo2| to the front.
2744 child
->ReorderChildView(foo2
, 0);
2745 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2746 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2747 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2748 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2749 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2750 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2751 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2754 // Verifies that GetViewByID returns the correctly child view from the specified
2756 // The tree looks like this:
2761 TEST_F(ViewTest
, GetViewByID
) {
2763 const int kV1ID
= 1;
2767 const int kV2ID
= 2;
2771 const int kV3ID
= 3;
2775 const int kV4ID
= 4;
2778 const int kV5ID
= 5;
2780 v1
.AddChildView(&v2
);
2781 v2
.AddChildView(&v3
);
2782 v2
.AddChildView(&v4
);
2784 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2785 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2786 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2788 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2789 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2791 const int kGroup
= 1;
2792 v3
.SetGroup(kGroup
);
2793 v4
.SetGroup(kGroup
);
2796 v1
.GetViewsInGroup(kGroup
, &views
);
2797 EXPECT_EQ(2U, views
.size());
2799 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2800 EXPECT_NE(views
.end(), i
);
2802 i
= std::find(views
.begin(), views
.end(), &v4
);
2803 EXPECT_NE(views
.end(), i
);
2806 TEST_F(ViewTest
, AddExistingChild
) {
2809 v1
.AddChildView(&v2
);
2810 v1
.AddChildView(&v3
);
2811 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2812 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2814 // Check that there's no change in order when adding at same index.
2815 v1
.AddChildViewAt(&v2
, 0);
2816 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2817 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2818 v1
.AddChildViewAt(&v3
, 1);
2819 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2820 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2822 // Add it at a different index and check for change in order.
2823 v1
.AddChildViewAt(&v2
, 1);
2824 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
2825 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
2826 v1
.AddChildViewAt(&v2
, 0);
2827 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2828 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2830 // Check that calling |AddChildView()| does not change the order.
2831 v1
.AddChildView(&v2
);
2832 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2833 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2834 v1
.AddChildView(&v3
);
2835 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
2836 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
2839 ////////////////////////////////////////////////////////////////////////////////
2841 ////////////////////////////////////////////////////////////////////////////////
2843 #if defined(USE_AURA)
2847 // Test implementation of LayerAnimator.
2848 class TestLayerAnimator
: public ui::LayerAnimator
{
2850 TestLayerAnimator();
2852 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
2855 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
2858 virtual ~TestLayerAnimator() { }
2861 gfx::Rect last_bounds_
;
2863 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
2866 TestLayerAnimator::TestLayerAnimator()
2867 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2870 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
2871 last_bounds_
= bounds
;
2876 class ViewLayerTest
: public ViewsTestBase
{
2878 ViewLayerTest() : widget_(NULL
), old_use_acceleration_(false) {}
2880 virtual ~ViewLayerTest() {
2883 // Returns the Layer used by the RootView.
2884 ui::Layer
* GetRootLayer() {
2885 ui::Layer
* root_layer
= NULL
;
2886 widget()->CalculateOffsetToAncestorWithLayer(&root_layer
);
2890 virtual void SetUp() OVERRIDE
{
2892 old_use_acceleration_
= View::get_use_acceleration_when_possible();
2893 View::set_use_acceleration_when_possible(true);
2895 widget_
= new Widget
;
2896 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2897 params
.bounds
= gfx::Rect(50, 50, 200, 200);
2898 widget_
->Init(params
);
2900 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
2903 virtual void TearDown() OVERRIDE
{
2904 View::set_use_acceleration_when_possible(old_use_acceleration_
);
2905 widget_
->CloseNow();
2906 ViewsTestBase::TearDown();
2909 Widget
* widget() { return widget_
; }
2913 bool old_use_acceleration_
;
2917 TEST_F(ViewLayerTest
, LayerToggling
) {
2918 // Because we lazily create textures the calls to DrawTree are necessary to
2919 // ensure we trigger creation of textures.
2920 ui::Layer
* root_layer
= NULL
;
2921 widget()->CalculateOffsetToAncestorWithLayer(&root_layer
);
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
, RecreateLayer
) {
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());
3373 } // namespace views