1 // Copyright (c) 2011 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 "testing/gmock/include/gmock/gmock.h"
12 #include "ui/base/clipboard/clipboard.h"
13 #include "ui/base/keycodes/keyboard_codes.h"
14 #include "ui/base/models/simple_menu_model.h"
15 #include "ui/base/models/accelerator.h"
16 #include "ui/gfx/canvas_skia.h"
17 #include "ui/gfx/compositor/compositor.h"
18 #include "ui/gfx/compositor/layer.h"
19 #include "ui/gfx/compositor/layer_animator.h"
20 #include "ui/gfx/compositor/test_compositor.h"
21 #include "ui/gfx/compositor/test_texture.h"
22 #include "ui/gfx/path.h"
23 #include "ui/gfx/transform.h"
24 #include "ui/views/window/dialog_delegate.h"
25 #include "views/background.h"
26 #include "views/controls/button/button_dropdown.h"
27 #include "views/controls/button/checkbox.h"
28 #include "views/controls/native/native_view_host.h"
29 #include "views/controls/scroll_view.h"
30 #include "views/controls/textfield/textfield.h"
31 #include "views/events/event.h"
32 #include "views/focus/accelerator_handler.h"
33 #include "views/focus/view_storage.h"
34 #include "views/test/views_test_base.h"
35 #include "views/touchui/gesture_manager.h"
36 #include "views/view.h"
37 #include "views/views_delegate.h"
38 #include "views/widget/native_widget.h"
39 #include "views/widget/root_view.h"
42 #include "views/test/test_views_delegate.h"
45 #include "ui/aura/desktop.h"
52 // Returns true if |ancestor| is an ancestor of |layer|.
53 bool LayerIsAncestor(const ui::Layer
* ancestor
, const ui::Layer
* layer
) {
54 while (layer
&& layer
!= ancestor
)
55 layer
= layer
->parent();
56 return layer
== ancestor
;
59 // Convenience functions for walking a View tree.
60 const views::View
* FirstView(const views::View
* view
) {
61 const views::View
* v
= view
;
62 while (v
->has_children())
67 const views::View
* NextView(const views::View
* view
) {
68 const views::View
* v
= view
;
69 const views::View
* parent
= v
->parent();
72 int next
= parent
->GetIndexOf(v
) + 1;
73 if (next
!= parent
->child_count())
74 return FirstView(parent
->child_at(next
));
78 // Convenience functions for walking a Layer tree.
79 const ui::Layer
* FirstLayer(const ui::Layer
* layer
) {
80 const ui::Layer
* l
= layer
;
81 while (l
->children().size() > 0)
86 const ui::Layer
* NextLayer(const ui::Layer
* layer
) {
87 const ui::Layer
* parent
= layer
->parent();
90 const std::vector
<ui::Layer
*> children
= parent
->children();
92 for (index
= 0; index
< children
.size(); index
++) {
93 if (children
[index
] == layer
)
96 size_t next
= index
+ 1;
97 if (next
< children
.size())
98 return FirstLayer(children
[next
]);
102 // Given the root nodes of a View tree and a Layer tree, makes sure the two
103 // trees are in sync.
104 bool ViewAndLayerTreeAreConsistent(const views::View
* view
,
105 const ui::Layer
* layer
) {
106 const views::View
* v
= FirstView(view
);
107 const ui::Layer
* l
= FirstLayer(layer
);
109 // Find the view with a layer.
110 while (v
&& !v
->layer())
116 // Check if the View tree and the Layer tree are in sync.
117 EXPECT_EQ(l
, v
->layer());
121 // Check if the visibility states of the View and the Layer are in sync.
122 EXPECT_EQ(l
->IsDrawn(), v
->IsVisibleInRootView());
123 if (v
->IsVisibleInRootView() != l
->IsDrawn()) {
124 for (const views::View
* vv
= v
; vv
; vv
= vv
->parent())
125 LOG(ERROR
) << "V: " << vv
<< " " << vv
->IsVisible() << " "
126 << vv
->IsVisibleInRootView() << " " << vv
->layer();
127 for (const ui::Layer
* ll
= l
; ll
; ll
= ll
->parent())
128 LOG(ERROR
) << "L: " << ll
<< " " << ll
->IsDrawn();
132 // Check if the size of the View and the Layer are in sync.
133 EXPECT_EQ(l
->bounds(), v
->bounds());
134 if (v
->bounds() != l
->bounds())
137 if (v
== view
|| l
== layer
)
138 return v
== view
&& l
== layer
;
147 // Constructs a View tree with the specified depth.
148 void ConstructTree(views::View
* view
, int depth
) {
151 int count
= base::RandInt(1, 5);
152 for (int i
= 0; i
< count
; i
++) {
153 views::View
* v
= new views::View
;
154 view
->AddChildView(v
);
155 if (base::RandDouble() > 0.5)
156 v
->SetPaintToLayer(true);
157 if (base::RandDouble() < 0.2)
158 v
->SetVisible(false);
160 ConstructTree(v
, depth
- 1);
164 void ScrambleTree(views::View
* view
) {
165 int count
= view
->child_count();
168 for (int i
= 0; i
< count
; i
++) {
169 ScrambleTree(view
->child_at(i
));
173 int a
= base::RandInt(0, count
- 1);
174 int b
= base::RandInt(0, count
- 1);
176 views::View
* view_a
= view
->child_at(a
);
177 views::View
* view_b
= view
->child_at(b
);
178 view
->ReorderChildView(view_a
, b
);
179 view
->ReorderChildView(view_b
, a
);
182 if (!view
->layer() && base::RandDouble() < 0.1)
183 view
->SetPaintToLayer(true);
185 if (base::RandDouble() < 0.1)
186 view
->SetVisible(!view
->IsVisible());
193 typedef ViewsTestBase ViewTest
;
195 // A derived class for testing purpose.
196 class TestView
: public View
{
198 TestView() : View(), in_touch_sequence_(false) {}
199 virtual ~TestView() {}
201 // Reset all test state
203 did_change_bounds_
= false;
204 last_mouse_event_type_
= 0;
205 location_
.SetPoint(0, 0);
206 last_touch_event_type_
= 0;
207 last_touch_event_was_handled_
= false;
208 last_clip_
.setEmpty();
209 accelerator_count_map_
.clear();
212 virtual void OnBoundsChanged(const gfx::Rect
& previous_bounds
) OVERRIDE
;
213 virtual bool OnMousePressed(const MouseEvent
& event
) OVERRIDE
;
214 virtual bool OnMouseDragged(const MouseEvent
& event
) OVERRIDE
;
215 virtual void OnMouseReleased(const MouseEvent
& event
) OVERRIDE
;
216 virtual ui::TouchStatus
OnTouchEvent(const TouchEvent
& event
) OVERRIDE
;
217 virtual void Paint(gfx::Canvas
* canvas
) OVERRIDE
;
218 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) OVERRIDE
;
219 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
222 bool did_change_bounds_
;
223 gfx::Rect new_bounds_
;
226 int last_mouse_event_type_
;
227 gfx::Point location_
;
230 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
233 int last_touch_event_type_
;
234 bool last_touch_event_was_handled_
;
235 bool in_touch_sequence_
;
241 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
244 // Mock instance of the GestureManager for testing.
245 class MockGestureManager
: public GestureManager
{
247 // Reset all test state.
249 last_touch_event_
= 0;
251 previously_handled_flag_
= false;
252 dispatched_synthetic_event_
= false;
255 bool ProcessTouchEventForGesture(const TouchEvent
& event
,
257 ui::TouchStatus status
);
258 MockGestureManager();
260 bool previously_handled_flag_
;
261 int last_touch_event_
;
263 bool dispatched_synthetic_event_
;
265 DISALLOW_COPY_AND_ASSIGN(MockGestureManager
);
268 // A view subclass that ignores all touch events for testing purposes.
269 class TestViewIgnoreTouch
: public TestView
{
271 TestViewIgnoreTouch() : TestView() {}
272 virtual ~TestViewIgnoreTouch() {}
275 virtual ui::TouchStatus
OnTouchEvent(const TouchEvent
& event
) OVERRIDE
;
278 ////////////////////////////////////////////////////////////////////////////////
280 ////////////////////////////////////////////////////////////////////////////////
282 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
283 did_change_bounds_
= true;
284 new_bounds_
= bounds();
287 TEST_F(ViewTest
, OnBoundsChanged
) {
290 gfx::Rect
prev_rect(0, 0, 200, 200);
291 gfx::Rect
new_rect(100, 100, 250, 250);
293 v
.SetBoundsRect(prev_rect
);
295 v
.SetBoundsRect(new_rect
);
297 EXPECT_EQ(v
.did_change_bounds_
, true);
298 EXPECT_EQ(v
.new_bounds_
, new_rect
);
299 EXPECT_EQ(v
.bounds(), new_rect
);
302 ////////////////////////////////////////////////////////////////////////////////
304 ////////////////////////////////////////////////////////////////////////////////
306 bool TestView::OnMousePressed(const MouseEvent
& event
) {
307 last_mouse_event_type_
= event
.type();
308 location_
.SetPoint(event
.x(), event
.y());
312 bool TestView::OnMouseDragged(const MouseEvent
& event
) {
313 last_mouse_event_type_
= event
.type();
314 location_
.SetPoint(event
.x(), event
.y());
318 void TestView::OnMouseReleased(const MouseEvent
& event
) {
319 last_mouse_event_type_
= event
.type();
320 location_
.SetPoint(event
.x(), event
.y());
323 TEST_F(ViewTest
, MouseEvent
) {
324 TestView
* v1
= new TestView();
325 v1
->SetBounds(0, 0, 300, 300);
327 TestView
* v2
= new TestView();
328 v2
->SetBounds(100, 100, 100, 100);
330 scoped_ptr
<Widget
> widget(new Widget
);
331 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
332 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
333 params
.bounds
= gfx::Rect(50, 50, 650, 650);
334 widget
->Init(params
);
335 View
* root
= widget
->GetRootView();
337 root
->AddChildView(v1
);
338 v1
->AddChildView(v2
);
343 MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
346 ui::EF_LEFT_BUTTON_DOWN
);
347 root
->OnMousePressed(pressed
);
348 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
349 EXPECT_EQ(v2
->location_
.x(), 10);
350 EXPECT_EQ(v2
->location_
.y(), 20);
351 // Make sure v1 did not receive the event
352 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
354 // Drag event out of bounds. Should still go to v2
357 MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
,
360 ui::EF_LEFT_BUTTON_DOWN
);
361 root
->OnMouseDragged(dragged
);
362 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
363 EXPECT_EQ(v2
->location_
.x(), -50);
364 EXPECT_EQ(v2
->location_
.y(), -60);
365 // Make sure v1 did not receive the event
366 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
368 // Releasted event out of bounds. Should still go to v2
371 MouseEvent
released(ui::ET_MOUSE_RELEASED
, 0, 0, 0);
372 root
->OnMouseDragged(released
);
373 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
374 EXPECT_EQ(v2
->location_
.x(), -100);
375 EXPECT_EQ(v2
->location_
.y(), -100);
376 // Make sure v1 did not receive the event
377 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
382 ////////////////////////////////////////////////////////////////////////////////
384 ////////////////////////////////////////////////////////////////////////////////
385 bool MockGestureManager::ProcessTouchEventForGesture(
386 const TouchEvent
& event
,
388 ui::TouchStatus status
) {
389 if (status
!= ui::TOUCH_STATUS_UNKNOWN
) {
390 dispatched_synthetic_event_
= false;
393 last_touch_event_
= event
.type();
395 previously_handled_flag_
= status
!= ui::TOUCH_STATUS_UNKNOWN
;
396 dispatched_synthetic_event_
= true;
400 MockGestureManager::MockGestureManager() {
403 ui::TouchStatus
TestView::OnTouchEvent(const TouchEvent
& event
) {
404 last_touch_event_type_
= event
.type();
405 location_
.SetPoint(event
.x(), event
.y());
406 if (!in_touch_sequence_
) {
407 if (event
.type() == ui::ET_TOUCH_PRESSED
) {
408 in_touch_sequence_
= true;
409 return ui::TOUCH_STATUS_START
;
412 if (event
.type() == ui::ET_TOUCH_RELEASED
) {
413 in_touch_sequence_
= false;
414 return ui::TOUCH_STATUS_END
;
416 return ui::TOUCH_STATUS_CONTINUE
;
418 return last_touch_event_was_handled_
? ui::TOUCH_STATUS_CONTINUE
:
419 ui::TOUCH_STATUS_UNKNOWN
;
422 ui::TouchStatus
TestViewIgnoreTouch::OnTouchEvent(const TouchEvent
& event
) {
423 return ui::TOUCH_STATUS_UNKNOWN
;
426 TEST_F(ViewTest
, TouchEvent
) {
427 MockGestureManager gm
;
429 TestView
* v1
= new TestView();
430 v1
->SetBounds(0, 0, 300, 300);
432 TestView
* v2
= new TestView();
433 v2
->SetBounds(100, 100, 100, 100);
435 TestView
* v3
= new TestViewIgnoreTouch();
436 v3
->SetBounds(0, 0, 100, 100);
438 scoped_ptr
<Widget
> widget(new Widget());
439 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
440 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
441 params
.bounds
= gfx::Rect(50, 50, 650, 650);
442 widget
->Init(params
);
443 View
* root
= widget
->GetRootView();
445 root
->AddChildView(v1
);
446 static_cast<internal::RootView
*>(root
)->SetGestureManagerForTesting(&gm
);
447 v1
->AddChildView(v2
);
448 v2
->AddChildView(v3
);
450 // |v3| completely obscures |v2|, but all the touch events on |v3| should
451 // reach |v2| because |v3| doesn't process any touch events.
453 // Make sure if none of the views handle the touch event, the gesture manager
459 TouchEvent
unhandled(ui::ET_TOUCH_MOVED
,
463 0, /* first finger touch */
465 root
->OnTouchEvent(unhandled
);
467 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
468 EXPECT_EQ(v2
->last_touch_event_type_
, 0);
470 EXPECT_EQ(gm
.previously_handled_flag_
, false);
471 EXPECT_EQ(gm
.last_touch_event_
, ui::ET_TOUCH_MOVED
);
472 EXPECT_EQ(gm
.last_view_
, root
);
473 EXPECT_EQ(gm
.dispatched_synthetic_event_
, true);
475 // Test press, drag, release touch sequence.
480 TouchEvent
pressed(ui::ET_TOUCH_PRESSED
,
484 0, /* first finger touch */
486 v2
->last_touch_event_was_handled_
= true;
487 root
->OnTouchEvent(pressed
);
489 EXPECT_EQ(v2
->last_touch_event_type_
, ui::ET_TOUCH_PRESSED
);
490 EXPECT_EQ(v2
->location_
.x(), 10);
491 EXPECT_EQ(v2
->location_
.y(), 20);
492 // Make sure v1 did not receive the event
493 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
495 // Since v2 handled the touch-event, the gesture manager should not handle it.
496 EXPECT_EQ(gm
.last_touch_event_
, 0);
497 EXPECT_EQ(NULL
, gm
.last_view_
);
498 EXPECT_EQ(gm
.previously_handled_flag_
, false);
500 // Drag event out of bounds. Should still go to v2
503 TouchEvent
dragged(ui::ET_TOUCH_MOVED
,
507 0, /* first finger touch */
510 root
->OnTouchEvent(dragged
);
511 EXPECT_EQ(v2
->last_touch_event_type_
, ui::ET_TOUCH_MOVED
);
512 EXPECT_EQ(v2
->location_
.x(), -50);
513 EXPECT_EQ(v2
->location_
.y(), -60);
514 // Make sure v1 did not receive the event
515 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
517 EXPECT_EQ(gm
.last_touch_event_
, 0);
518 EXPECT_EQ(NULL
, gm
.last_view_
);
519 EXPECT_EQ(gm
.previously_handled_flag_
, false);
521 // Released event out of bounds. Should still go to v2
524 TouchEvent
released(ui::ET_TOUCH_RELEASED
, 0, 0, 0, 0 /* first finger */,
526 v2
->last_touch_event_was_handled_
= true;
527 root
->OnTouchEvent(released
);
528 EXPECT_EQ(v2
->last_touch_event_type_
, ui::ET_TOUCH_RELEASED
);
529 EXPECT_EQ(v2
->location_
.x(), -100);
530 EXPECT_EQ(v2
->location_
.y(), -100);
531 // Make sure v1 did not receive the event
532 EXPECT_EQ(v1
->last_touch_event_type_
, 0);
534 EXPECT_EQ(gm
.last_touch_event_
, 0);
535 EXPECT_EQ(NULL
, gm
.last_view_
);
536 EXPECT_EQ(gm
.previously_handled_flag_
, false);
541 ////////////////////////////////////////////////////////////////////////////////
543 ////////////////////////////////////////////////////////////////////////////////
545 void TestView::Paint(gfx::Canvas
* canvas
) {
546 canvas
->GetSkCanvas()->getClipBounds(&last_clip_
);
549 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
550 scheduled_paint_rects_
.push_back(rect
);
551 View::SchedulePaintInRect(rect
);
554 void CheckRect(const SkRect
& check_rect
, const SkRect
& target_rect
) {
555 EXPECT_EQ(target_rect
.fLeft
, check_rect
.fLeft
);
556 EXPECT_EQ(target_rect
.fRight
, check_rect
.fRight
);
557 EXPECT_EQ(target_rect
.fTop
, check_rect
.fTop
);
558 EXPECT_EQ(target_rect
.fBottom
, check_rect
.fBottom
);
561 /* This test is disabled because it is flakey on some systems.
562 TEST_F(ViewTest, DISABLED_Painting) {
563 // Determine if InvalidateRect generates an empty paint rectangle.
564 EmptyWindow paint_window(CRect(50, 50, 650, 650));
565 paint_window.RedrawWindow(CRect(0, 0, 600, 600), NULL,
566 RDW_UPDATENOW | RDW_INVALIDATE | RDW_ALLCHILDREN);
567 bool empty_paint = paint_window.empty_paint();
569 NativeWidgetWin window;
570 window.set_delete_on_destroy(false);
571 window.set_window_style(WS_OVERLAPPEDWINDOW);
572 window.Init(NULL, gfx::Rect(50, 50, 650, 650), NULL);
573 View* root = window.GetRootView();
575 TestView* v1 = new TestView();
576 v1->SetBounds(0, 0, 650, 650);
577 root->AddChildView(v1);
579 TestView* v2 = new TestView();
580 v2->SetBounds(10, 10, 80, 80);
581 v1->AddChildView(v2);
583 TestView* v3 = new TestView();
584 v3->SetBounds(10, 10, 60, 60);
585 v2->AddChildView(v3);
587 TestView* v4 = new TestView();
588 v4->SetBounds(10, 200, 100, 100);
589 v1->AddChildView(v4);
591 // Make sure to paint current rects
592 PaintRootView(root, empty_paint);
599 v3->SchedulePaintInRect(gfx::Rect(10, 10, 10, 10));
600 PaintRootView(root, empty_paint);
604 tmp_rect.set(SkIntToScalar(10),
608 CheckRect(v3->last_clip_, tmp_rect);
610 tmp_rect.set(SkIntToScalar(20),
614 CheckRect(v2->last_clip_, tmp_rect);
616 tmp_rect.set(SkIntToScalar(30),
620 CheckRect(v1->last_clip_, tmp_rect);
622 // Make sure v4 was not painted
624 CheckRect(v4->last_clip_, tmp_rect);
626 window.DestroyWindow();
631 TEST_F(ViewTest
, RemoveNotification
) {
633 // TODO(beng): stopped working with widget hierarchy split,
634 // http://crbug.com/82364
635 TEST_F(ViewTest
, DISABLED_RemoveNotification
) {
637 ViewStorage
* vs
= ViewStorage::GetInstance();
638 Widget
* widget
= new Widget
;
639 widget
->Init(Widget::InitParams(Widget::InitParams::TYPE_POPUP
));
640 View
* root_view
= widget
->GetRootView();
643 int s1
= vs
->CreateStorageID();
644 vs
->StoreView(s1
, v1
);
645 root_view
->AddChildView(v1
);
646 View
* v11
= new View
;
647 int s11
= vs
->CreateStorageID();
648 vs
->StoreView(s11
, v11
);
649 v1
->AddChildView(v11
);
650 View
* v111
= new View
;
651 int s111
= vs
->CreateStorageID();
652 vs
->StoreView(s111
, v111
);
653 v11
->AddChildView(v111
);
654 View
* v112
= new View
;
655 int s112
= vs
->CreateStorageID();
656 vs
->StoreView(s112
, v112
);
657 v11
->AddChildView(v112
);
658 View
* v113
= new View
;
659 int s113
= vs
->CreateStorageID();
660 vs
->StoreView(s113
, v113
);
661 v11
->AddChildView(v113
);
662 View
* v1131
= new View
;
663 int s1131
= vs
->CreateStorageID();
664 vs
->StoreView(s1131
, v1131
);
665 v113
->AddChildView(v1131
);
666 View
* v12
= new View
;
667 int s12
= vs
->CreateStorageID();
668 vs
->StoreView(s12
, v12
);
669 v1
->AddChildView(v12
);
672 int s2
= vs
->CreateStorageID();
673 vs
->StoreView(s2
, v2
);
674 root_view
->AddChildView(v2
);
675 View
* v21
= new View
;
676 int s21
= vs
->CreateStorageID();
677 vs
->StoreView(s21
, v21
);
678 v2
->AddChildView(v21
);
679 View
* v211
= new View
;
680 int s211
= vs
->CreateStorageID();
681 vs
->StoreView(s211
, v211
);
682 v21
->AddChildView(v211
);
684 size_t stored_views
= vs
->view_count();
686 // Try removing a leaf view.
687 v21
->RemoveChildView(v211
);
688 EXPECT_EQ(stored_views
- 1, vs
->view_count());
689 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
690 delete v211
; // We won't use this one anymore.
692 // Now try removing a view with a hierarchy of depth 1.
693 v11
->RemoveChildView(v113
);
694 EXPECT_EQ(stored_views
- 3, vs
->view_count());
695 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
696 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
697 delete v113
; // We won't use this one anymore.
699 // Now remove even more.
700 root_view
->RemoveChildView(v1
);
701 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
702 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
703 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
704 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
705 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
707 // Put v1 back for more tests.
708 root_view
->AddChildView(v1
);
709 vs
->StoreView(s1
, v1
);
711 // Synchronously closing the window deletes the view hierarchy, which should
712 // remove all its views from ViewStorage.
714 EXPECT_EQ(stored_views
- 10, vs
->view_count());
715 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
716 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
717 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
718 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
719 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
720 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
721 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
725 class HitTestView
: public View
{
727 explicit HitTestView(bool has_hittest_mask
)
728 : has_hittest_mask_(has_hittest_mask
) {
730 virtual ~HitTestView() {}
733 // Overridden from View:
734 virtual bool HasHitTestMask() const {
735 return has_hittest_mask_
;
737 virtual void GetHitTestMask(gfx::Path
* mask
) const {
738 DCHECK(has_hittest_mask_
);
741 SkScalar w
= SkIntToScalar(width());
742 SkScalar h
= SkIntToScalar(height());
744 // Create a triangular mask within the bounds of this View.
745 mask
->moveTo(w
/ 2, 0);
752 bool has_hittest_mask_
;
754 DISALLOW_COPY_AND_ASSIGN(HitTestView
);
757 gfx::Point
ConvertPointToView(View
* view
, const gfx::Point
& p
) {
759 View::ConvertPointToView(view
->GetWidget()->GetRootView(), view
, &tmp
);
763 void RotateCounterclockwise(ui::Transform
& transform
) {
764 transform
.matrix().set3x3(0, -1, 0,
769 void RotateClockwise(ui::Transform
& transform
) {
770 transform
.matrix().set3x3( 0, 1, 0,
777 TEST_F(ViewTest
, HitTestMasks
) {
778 Widget
* widget
= new Widget
;
779 widget
->Init(Widget::InitParams(Widget::InitParams::TYPE_POPUP
));
780 View
* root_view
= widget
->GetRootView();
781 root_view
->SetBounds(0, 0, 500, 500);
783 gfx::Rect v1_bounds
= gfx::Rect(0, 0, 100, 100);
784 HitTestView
* v1
= new HitTestView(false);
785 v1
->SetBoundsRect(v1_bounds
);
786 root_view
->AddChildView(v1
);
788 gfx::Rect v2_bounds
= gfx::Rect(105, 0, 100, 100);
789 HitTestView
* v2
= new HitTestView(true);
790 v2
->SetBoundsRect(v2_bounds
);
791 root_view
->AddChildView(v2
);
793 gfx::Point v1_centerpoint
= v1_bounds
.CenterPoint();
794 gfx::Point v2_centerpoint
= v2_bounds
.CenterPoint();
795 gfx::Point v1_origin
= v1_bounds
.origin();
796 gfx::Point v2_origin
= v2_bounds
.origin();
799 EXPECT_TRUE(v1
->HitTest(ConvertPointToView(v1
, v1_centerpoint
)));
800 EXPECT_TRUE(v2
->HitTest(ConvertPointToView(v2
, v2_centerpoint
)));
802 EXPECT_TRUE(v1
->HitTest(ConvertPointToView(v1
, v1_origin
)));
803 EXPECT_FALSE(v2
->HitTest(ConvertPointToView(v2
, v2_origin
)));
805 // Test GetEventHandlerForPoint
806 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_centerpoint
));
807 EXPECT_EQ(v2
, root_view
->GetEventHandlerForPoint(v2_centerpoint
));
808 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_origin
));
809 EXPECT_EQ(root_view
, root_view
->GetEventHandlerForPoint(v2_origin
));
814 TEST_F(ViewTest
, Textfield
) {
815 const string16 kText
= ASCIIToUTF16("Reality is that which, when you stop "
816 "believing it, doesn't go away.");
817 const string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
818 const string16 kEmptyString
;
820 ui::Clipboard clipboard
;
822 Widget
* widget
= new Widget
;
823 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
824 params
.bounds
= gfx::Rect(0, 0, 100, 100);
825 widget
->Init(params
);
826 View
* root_view
= widget
->GetRootView();
828 Textfield
* textfield
= new Textfield();
829 root_view
->AddChildView(textfield
);
831 // Test setting, appending text.
832 textfield
->SetText(kText
);
833 EXPECT_EQ(kText
, textfield
->text());
834 textfield
->AppendText(kExtraText
);
835 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
836 textfield
->SetText(string16());
837 EXPECT_EQ(kEmptyString
, textfield
->text());
839 // Test selection related methods.
840 textfield
->SetText(kText
);
841 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
842 textfield
->SelectAll();
843 EXPECT_EQ(kText
, textfield
->text());
844 textfield
->ClearSelection();
845 EXPECT_EQ(kEmptyString
, textfield
->GetSelectedText());
850 #if defined(OS_WIN) && !defined(USE_AURA)
852 // Tests that the Textfield view respond appropiately to cut/copy/paste.
853 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
854 const string16 kNormalText
= ASCIIToUTF16("Normal");
855 const string16 kReadOnlyText
= ASCIIToUTF16("Read only");
856 const string16 kPasswordText
= ASCIIToUTF16("Password! ** Secret stuff **");
858 ui::Clipboard clipboard
;
860 Widget
* widget
= new Widget
;
861 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
862 params
.bounds
= gfx::Rect(0, 0, 100, 100);
863 widget
->Init(params
);
864 View
* root_view
= widget
->GetRootView();
866 Textfield
* normal
= new Textfield();
867 Textfield
* read_only
= new Textfield();
868 read_only
->SetReadOnly(true);
869 Textfield
* password
= new Textfield(Textfield::STYLE_PASSWORD
);
871 root_view
->AddChildView(normal
);
872 root_view
->AddChildView(read_only
);
873 root_view
->AddChildView(password
);
875 normal
->SetText(kNormalText
);
876 read_only
->SetText(kReadOnlyText
);
877 password
->SetText(kPasswordText
);
882 ASSERT_TRUE(normal
->GetTestingHandle());
884 ::SendMessage(normal
->GetTestingHandle(), WM_CUT
, 0, 0);
887 clipboard
.ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
888 EXPECT_EQ(kNormalText
, result
);
889 normal
->SetText(kNormalText
); // Let's revert to the original content.
891 ASSERT_TRUE(read_only
->GetTestingHandle());
892 read_only
->SelectAll();
893 ::SendMessage(read_only
->GetTestingHandle(), WM_CUT
, 0, 0);
895 clipboard
.ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
896 // Cut should have failed, so the clipboard content should not have changed.
897 EXPECT_EQ(kNormalText
, result
);
899 ASSERT_TRUE(password
->GetTestingHandle());
900 password
->SelectAll();
901 ::SendMessage(password
->GetTestingHandle(), WM_CUT
, 0, 0);
903 clipboard
.ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
904 // Cut should have failed, so the clipboard content should not have changed.
905 EXPECT_EQ(kNormalText
, result
);
911 // Let's start with read_only as the clipboard already contains the content
913 read_only
->SelectAll();
914 ::SendMessage(read_only
->GetTestingHandle(), WM_COPY
, 0, 0);
916 clipboard
.ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
917 EXPECT_EQ(kReadOnlyText
, result
);
920 ::SendMessage(normal
->GetTestingHandle(), WM_COPY
, 0, 0);
922 clipboard
.ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
923 EXPECT_EQ(kNormalText
, result
);
925 password
->SelectAll();
926 ::SendMessage(password
->GetTestingHandle(), WM_COPY
, 0, 0);
928 clipboard
.ReadText(ui::Clipboard::BUFFER_STANDARD
, &result
);
929 // We don't let you copy from a password field, clipboard should not have
931 EXPECT_EQ(kNormalText
, result
);
936 // Note that we use GetWindowText instead of Textfield::GetText below as the
937 // text in the Textfield class is synced to the text of the HWND on
938 // WM_KEYDOWN messages that we are not simulating here.
940 // Attempting to copy kNormalText in a read-only text-field should fail.
941 read_only
->SelectAll();
942 ::SendMessage(read_only
->GetTestingHandle(), WM_KEYDOWN
, 0, 0);
943 wchar_t buffer
[1024] = { 0 };
944 ::GetWindowText(read_only
->GetTestingHandle(), buffer
, 1024);
945 EXPECT_EQ(kReadOnlyText
, string16(buffer
));
947 password
->SelectAll();
948 ::SendMessage(password
->GetTestingHandle(), WM_PASTE
, 0, 0);
949 ::GetWindowText(password
->GetTestingHandle(), buffer
, 1024);
950 EXPECT_EQ(kNormalText
, string16(buffer
));
952 // Copy from read_only so the string we are pasting is not the same as the
954 read_only
->SelectAll();
955 ::SendMessage(read_only
->GetTestingHandle(), WM_COPY
, 0, 0);
957 ::SendMessage(normal
->GetTestingHandle(), WM_PASTE
, 0, 0);
958 ::GetWindowText(normal
->GetTestingHandle(), buffer
, 1024);
959 EXPECT_EQ(kReadOnlyText
, string16(buffer
));
964 ////////////////////////////////////////////////////////////////////////////////
966 ////////////////////////////////////////////////////////////////////////////////
967 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
968 accelerator_count_map_
[accelerator
]++;
972 #if defined(OS_WIN) && !defined(USE_AURA)
973 TEST_F(ViewTest
, ActivateAccelerator
) {
974 // Register a keyboard accelerator before the view is added to a window.
975 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, false, false, false);
976 TestView
* view
= new TestView();
978 view
->AddAccelerator(return_accelerator
);
979 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
981 // Create a window and add the view as its child.
982 scoped_ptr
<Widget
> widget(new Widget
);
983 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
984 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
985 params
.bounds
= gfx::Rect(0, 0, 100, 100);
986 widget
->Init(params
);
987 View
* root
= widget
->GetRootView();
988 root
->AddChildView(view
);
991 // Get the focus manager.
992 FocusManager
* focus_manager
= widget
->GetFocusManager();
993 ASSERT_TRUE(focus_manager
);
995 // Hit the return key and see if it takes effect.
996 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
997 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
999 // Hit the escape key. Nothing should happen.
1000 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, false, false, false);
1001 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1002 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1003 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1005 // Now register the escape key and hit it again.
1006 view
->AddAccelerator(escape_accelerator
);
1007 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1008 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1009 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1011 // Remove the return key accelerator.
1012 view
->RemoveAccelerator(return_accelerator
);
1013 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1014 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1015 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1017 // Add it again. Hit the return key and the escape key.
1018 view
->AddAccelerator(return_accelerator
);
1019 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1020 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1021 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1022 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1023 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1024 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1026 // Remove all the accelerators.
1027 view
->ResetAccelerators();
1028 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1029 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1030 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1031 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1032 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1033 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1039 #if defined(OS_WIN) && !defined(USE_AURA)
1040 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1041 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, false, false, false);
1042 TestView
* view
= new TestView();
1044 view
->AddAccelerator(return_accelerator
);
1045 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1047 scoped_ptr
<Widget
> widget(new Widget
);
1048 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
1049 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1050 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1051 widget
->Init(params
);
1052 View
* root
= widget
->GetRootView();
1053 root
->AddChildView(view
);
1056 FocusManager
* focus_manager
= widget
->GetFocusManager();
1057 ASSERT_TRUE(focus_manager
);
1059 view
->SetVisible(false);
1061 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
1063 view
->SetVisible(true);
1065 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
1071 #if defined(OS_WIN) && !defined(USE_AURA)
1072 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1073 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, false, false, false);
1074 TestView
* view
= new TestView();
1076 view
->AddAccelerator(return_accelerator
);
1077 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1079 scoped_ptr
<Widget
> widget(new Widget
);
1080 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
1081 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1082 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1083 widget
->Init(params
);
1084 View
* root
= widget
->GetRootView();
1085 root
->AddChildView(view
);
1087 FocusManager
* focus_manager
= widget
->GetFocusManager();
1088 ASSERT_TRUE(focus_manager
);
1091 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
1095 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
1099 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
1105 #if defined(OS_WIN) && !defined(USE_AURA)
1106 ////////////////////////////////////////////////////////////////////////////////
1107 // Mouse-wheel message rerouting
1108 ////////////////////////////////////////////////////////////////////////////////
1109 class ScrollableTestView
: public View
{
1111 ScrollableTestView() { }
1113 virtual gfx::Size
GetPreferredSize() {
1114 return gfx::Size(100, 10000);
1117 virtual void Layout() {
1118 SizeToPreferredSize();
1122 class TestViewWithControls
: public View
{
1124 TestViewWithControls() {
1125 text_field_
= new Textfield();
1126 AddChildView(text_field_
);
1129 Textfield
* text_field_
;
1132 class SimpleWidgetDelegate
: public WidgetDelegate
{
1134 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
1136 virtual void DeleteDelegate() { delete this; }
1138 virtual View
* GetContentsView() { return contents_
; }
1140 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
1141 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
1147 // Tests that the mouse-wheel messages are correctly rerouted to the window
1149 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1151 // Note that this fails for a variety of reasons:
1152 // - focused view is apparently reset across window activations and never
1153 // properly restored
1154 // - this test depends on you not having any other window visible open under the
1155 // area that it opens the test windows. --beng
1156 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
1157 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
1158 Widget
* window1
= Widget::CreateWindowWithBounds(
1159 new SimpleWidgetDelegate(view_with_controls
),
1160 gfx::Rect(0, 0, 100, 100));
1162 ScrollView
* scroll_view
= new ScrollView();
1163 scroll_view
->SetContents(new ScrollableTestView());
1164 Widget
* window2
= Widget::CreateWindowWithBounds(
1165 new SimpleWidgetDelegate(scroll_view
),
1166 gfx::Rect(200, 200, 100, 100));
1168 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
1170 // Make the window1 active, as this is what it would be in real-world.
1171 window1
->Activate();
1173 // Let's send a mouse-wheel message to the different controls and check that
1174 // it is rerouted to the window under the mouse (effectively scrolling the
1177 // First to the Window's HWND.
1178 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
1179 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1180 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
1182 // Then the text-field.
1183 ::SendMessage(view_with_controls
->text_field_
->GetTestingHandle(),
1184 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1185 EXPECT_EQ(80, scroll_view
->GetVisibleRect().y());
1187 // Ensure we don't scroll when the mouse is not over that window.
1188 ::SendMessage(view_with_controls
->text_field_
->GetTestingHandle(),
1189 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(50, 50));
1190 EXPECT_EQ(80, scroll_view
->GetVisibleRect().y());
1192 window1
->CloseNow();
1193 window2
->CloseNow();
1197 ////////////////////////////////////////////////////////////////////////////////
1198 // Dialogs' default button
1199 ////////////////////////////////////////////////////////////////////////////////
1201 class MockMenuModel
: public ui::MenuModel
{
1203 MOCK_CONST_METHOD0(HasIcons
, bool());
1204 MOCK_CONST_METHOD1(GetFirstItemIndex
, int(gfx::NativeMenu native_menu
));
1205 MOCK_CONST_METHOD0(GetItemCount
, int());
1206 MOCK_CONST_METHOD1(GetTypeAt
, ItemType(int index
));
1207 MOCK_CONST_METHOD1(GetCommandIdAt
, int(int index
));
1208 MOCK_CONST_METHOD1(GetLabelAt
, string16(int index
));
1209 MOCK_CONST_METHOD1(IsItemDynamicAt
, bool(int index
));
1210 MOCK_CONST_METHOD1(GetLabelFontAt
, const gfx::Font
* (int index
));
1211 MOCK_CONST_METHOD2(GetAcceleratorAt
, bool(int index
,
1212 ui::Accelerator
* accelerator
));
1213 MOCK_CONST_METHOD1(IsItemCheckedAt
, bool(int index
));
1214 MOCK_CONST_METHOD1(GetGroupIdAt
, int(int index
));
1215 MOCK_METHOD2(GetIconAt
, bool(int index
, SkBitmap
* icon
));
1216 MOCK_CONST_METHOD1(GetButtonMenuItemAt
, ui::ButtonMenuItemModel
*(int index
));
1217 MOCK_CONST_METHOD1(IsEnabledAt
, bool(int index
));
1218 MOCK_CONST_METHOD1(IsVisibleAt
, bool(int index
));
1219 MOCK_CONST_METHOD1(GetSubmenuModelAt
, MenuModel
*(int index
));
1220 MOCK_METHOD1(HighlightChangedTo
, void(int index
));
1221 MOCK_METHOD1(ActivatedAt
, void(int index
));
1222 MOCK_METHOD2(ActivatedAt
, void(int index
, int disposition
));
1223 MOCK_METHOD0(MenuWillShow
, void());
1224 MOCK_METHOD0(MenuClosed
, void());
1225 MOCK_METHOD1(SetMenuModelDelegate
, void(ui::MenuModelDelegate
* delegate
));
1226 MOCK_METHOD3(GetModelAndIndexForCommandId
, bool(int command_id
,
1227 MenuModel
** model
, int* index
));
1230 class TestDialog
: public DialogDelegate
, public ButtonListener
{
1232 explicit TestDialog(MockMenuModel
* mock_menu_model
)
1238 last_pressed_button_(NULL
),
1239 mock_menu_model_(mock_menu_model
),
1247 // Now we can close safely.
1251 // delegate has to be alive while shutting down.
1252 MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
1255 // DialogDelegate implementation:
1256 virtual int GetDefaultDialogButton() const OVERRIDE
{
1257 return ui::DIALOG_BUTTON_OK
;
1260 virtual View
* GetContentsView() OVERRIDE
{
1262 contents_
= new View
;
1263 button1_
= new NativeTextButton(this, ASCIIToUTF16("Button1"));
1264 button2_
= new NativeTextButton(this, ASCIIToUTF16("Button2"));
1265 checkbox_
= new Checkbox(ASCIIToUTF16("My checkbox"));
1266 button_drop_
= new ButtonDropDown(this, mock_menu_model_
);
1267 contents_
->AddChildView(button1_
);
1268 contents_
->AddChildView(button2_
);
1269 contents_
->AddChildView(checkbox_
);
1270 contents_
->AddChildView(button_drop_
);
1275 // Prevent the dialog from really closing (so we can click the OK/Cancel
1276 // buttons to our heart's content).
1277 virtual bool Cancel() OVERRIDE
{
1281 virtual bool Accept() OVERRIDE
{
1286 virtual Widget
* GetWidget() OVERRIDE
{
1289 virtual const Widget
* GetWidget() const OVERRIDE
{
1293 // ButtonListener implementation.
1294 virtual void ButtonPressed(Button
* sender
, const Event
& event
) OVERRIDE
{
1295 last_pressed_button_
= sender
;
1298 void ResetStates() {
1301 last_pressed_button_
= NULL
;
1304 // Set up expectations for methods that are called when an (empty) menu is
1305 // shown from a drop down button.
1306 void ExpectShowDropMenu() {
1307 if (mock_menu_model_
) {
1308 EXPECT_CALL(*mock_menu_model_
, HasIcons());
1309 EXPECT_CALL(*mock_menu_model_
, GetFirstItemIndex(_
));
1310 EXPECT_CALL(*mock_menu_model_
, GetItemCount());
1311 EXPECT_CALL(*mock_menu_model_
, MenuClosed());
1316 NativeTextButton
* button1_
;
1317 NativeTextButton
* button2_
;
1318 Checkbox
* checkbox_
;
1319 ButtonDropDown
* button_drop_
;
1320 Button
* last_pressed_button_
;
1321 MockMenuModel
* mock_menu_model_
;
1329 class DefaultButtonTest
: public ViewTest
{
1339 : focus_manager_(NULL
),
1343 cancel_button_(NULL
) {
1346 virtual void SetUp() OVERRIDE
{
1348 test_dialog_
= new TestDialog(NULL
);
1350 Widget::CreateWindowWithBounds(test_dialog_
, gfx::Rect(0, 0, 100, 100));
1351 test_dialog_
->widget_
= window
;
1353 focus_manager_
= test_dialog_
->contents_
->GetFocusManager();
1354 ASSERT_TRUE(focus_manager_
!= NULL
);
1356 static_cast<DialogClientView
*>(window
->client_view());
1357 ok_button_
= client_view_
->ok_button();
1358 cancel_button_
= client_view_
->cancel_button();
1361 virtual void TearDown() OVERRIDE
{
1362 test_dialog_
->TearDown();
1363 ViewTest::TearDown();
1366 void SimulatePressingEnterAndCheckDefaultButton(ButtonID button_id
) {
1367 KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_RETURN
, 0);
1368 focus_manager_
->OnKeyEvent(event
);
1369 switch (button_id
) {
1371 EXPECT_TRUE(test_dialog_
->oked_
);
1372 EXPECT_FALSE(test_dialog_
->canceled_
);
1373 EXPECT_FALSE(test_dialog_
->last_pressed_button_
);
1376 EXPECT_FALSE(test_dialog_
->oked_
);
1377 EXPECT_TRUE(test_dialog_
->canceled_
);
1378 EXPECT_FALSE(test_dialog_
->last_pressed_button_
);
1381 EXPECT_FALSE(test_dialog_
->oked_
);
1382 EXPECT_FALSE(test_dialog_
->canceled_
);
1383 EXPECT_TRUE(test_dialog_
->last_pressed_button_
==
1384 test_dialog_
->button1_
);
1387 EXPECT_FALSE(test_dialog_
->oked_
);
1388 EXPECT_FALSE(test_dialog_
->canceled_
);
1389 EXPECT_TRUE(test_dialog_
->last_pressed_button_
==
1390 test_dialog_
->button2_
);
1393 test_dialog_
->ResetStates();
1396 FocusManager
* focus_manager_
;
1397 TestDialog
* test_dialog_
;
1398 DialogClientView
* client_view_
;
1399 NativeTextButton
* ok_button_
;
1400 NativeTextButton
* cancel_button_
;
1403 TEST_F(DefaultButtonTest
, DialogDefaultButtonTest
) {
1404 // Window has just been shown, we expect the default button specified in the
1406 EXPECT_TRUE(ok_button_
->is_default());
1408 // Simulate pressing enter, that should trigger the OK button.
1409 SimulatePressingEnterAndCheckDefaultButton(OK
);
1411 // Simulate focusing another button, it should become the default button.
1412 client_view_
->OnWillChangeFocus(ok_button_
, test_dialog_
->button1_
);
1413 EXPECT_FALSE(ok_button_
->is_default());
1414 EXPECT_TRUE(test_dialog_
->button1_
->is_default());
1415 // Simulate pressing enter, that should trigger button1.
1416 SimulatePressingEnterAndCheckDefaultButton(BUTTON1
);
1418 // Now select something that is not a button, the OK should become the default
1420 client_view_
->OnWillChangeFocus(test_dialog_
->button1_
,
1421 test_dialog_
->checkbox_
);
1422 EXPECT_TRUE(ok_button_
->is_default());
1423 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1424 SimulatePressingEnterAndCheckDefaultButton(OK
);
1426 // Select yet another button.
1427 client_view_
->OnWillChangeFocus(test_dialog_
->checkbox_
,
1428 test_dialog_
->button2_
);
1429 EXPECT_FALSE(ok_button_
->is_default());
1430 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1431 EXPECT_TRUE(test_dialog_
->button2_
->is_default());
1432 SimulatePressingEnterAndCheckDefaultButton(BUTTON2
);
1435 client_view_
->OnWillChangeFocus(test_dialog_
->button2_
, NULL
);
1436 EXPECT_TRUE(ok_button_
->is_default());
1437 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1438 EXPECT_FALSE(test_dialog_
->button2_
->is_default());
1439 SimulatePressingEnterAndCheckDefaultButton(OK
);
1441 // Focus the cancel button.
1442 client_view_
->OnWillChangeFocus(NULL
, cancel_button_
);
1443 EXPECT_FALSE(ok_button_
->is_default());
1444 EXPECT_TRUE(cancel_button_
->is_default());
1445 EXPECT_FALSE(test_dialog_
->button1_
->is_default());
1446 EXPECT_FALSE(test_dialog_
->button2_
->is_default());
1447 SimulatePressingEnterAndCheckDefaultButton(CANCEL
);
1450 class ButtonDropDownTest
: public ViewTest
{
1452 ButtonDropDownTest()
1453 : test_dialog_(NULL
),
1454 button_as_view_(NULL
) {
1457 virtual void SetUp() OVERRIDE
{
1459 test_dialog_
= new TestDialog(&mock_menu_model_
);
1461 Widget::CreateWindowWithBounds(test_dialog_
, gfx::Rect(0, 0, 100, 100));
1462 test_dialog_
->widget_
= window
;
1464 test_dialog_
->button_drop_
->SetBounds(0, 0, 100, 100);
1465 // We have to cast the button back into a View in order to invoke it's
1466 // OnMouseReleased method.
1467 button_as_view_
= static_cast<View
*>(test_dialog_
->button_drop_
);
1470 virtual void TearDown() OVERRIDE
{
1471 test_dialog_
->TearDown();
1472 ViewTest::TearDown();
1475 TestDialog
* test_dialog_
;
1476 MockMenuModel mock_menu_model_
;
1477 // This is owned by test_dialog_.
1478 View
* button_as_view_
;
1481 DISALLOW_COPY_AND_ASSIGN(ButtonDropDownTest
);
1484 // Ensure that regular clicks on the drop down button still work. (i.e. - the
1485 // click events are processed and the listener gets the click)
1486 TEST_F(ButtonDropDownTest
, RegularClickTest
) {
1487 MouseEvent
press_event(ui::ET_MOUSE_PRESSED
, 1, 1, ui::EF_LEFT_BUTTON_DOWN
);
1488 MouseEvent
release_event(ui::ET_MOUSE_RELEASED
, 1, 1,
1489 ui::EF_LEFT_BUTTON_DOWN
);
1490 button_as_view_
->OnMousePressed(press_event
);
1491 button_as_view_
->OnMouseReleased(release_event
);
1492 EXPECT_EQ(test_dialog_
->last_pressed_button_
, test_dialog_
->button_drop_
);
1495 ////////////////////////////////////////////////////////////////////////////////
1496 // View hierarchy / Visibility changes
1497 ////////////////////////////////////////////////////////////////////////////////
1499 TEST_F(ViewTest, ChangeVisibility) {
1500 #if defined(OS_LINUX)
1501 // Make CRITICAL messages fatal
1502 // TODO(oshima): we probably should enable this for entire tests on linux.
1503 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
1505 scoped_ptr<Widget> window(CreateWidget());
1506 window->Init(NULL, gfx::Rect(0, 0, 500, 300));
1507 View* root_view = window->GetRootView();
1508 NativeTextButton* native = new NativeTextButton(NULL, ASCIIToUTF16("Native"));
1510 root_view->SetContentsView(native);
1511 native->SetVisible(true);
1513 root_view->RemoveChildView(native);
1514 native->SetVisible(false);
1515 // Change visibility to true with no widget.
1516 native->SetVisible(true);
1518 root_view->SetContentsView(native);
1519 native->SetVisible(true);
1523 ////////////////////////////////////////////////////////////////////////////////
1524 // Native view hierachy
1525 ////////////////////////////////////////////////////////////////////////////////
1526 class TestNativeViewHierarchy
: public View
{
1528 TestNativeViewHierarchy() {
1531 virtual void NativeViewHierarchyChanged(bool attached
,
1532 gfx::NativeView native_view
,
1533 internal::RootView
* root_view
) {
1534 NotificationInfo info
;
1535 info
.attached
= attached
;
1536 info
.native_view
= native_view
;
1537 info
.root_view
= root_view
;
1538 notifications_
.push_back(info
);
1540 struct NotificationInfo
{
1542 gfx::NativeView native_view
;
1543 internal::RootView
* root_view
;
1545 static const size_t kTotalViews
= 2;
1546 std::vector
<NotificationInfo
> notifications_
;
1549 class TestChangeNativeViewHierarchy
{
1551 explicit TestChangeNativeViewHierarchy(ViewTest
*view_test
) {
1552 view_test_
= view_test
;
1553 native_host_
= new NativeViewHost();
1555 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
1556 params
.bounds
= gfx::Rect(0, 0, 500, 300);
1557 host_
->Init(params
);
1558 host_
->GetRootView()->AddChildView(native_host_
);
1559 for (size_t i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1560 windows_
[i
] = new Widget
;
1561 Widget::InitParams
params(Widget::InitParams::TYPE_CONTROL
);
1562 params
.parent
= host_
->GetNativeView();
1563 params
.bounds
= gfx::Rect(0, 0, 500, 300);
1564 windows_
[i
]->Init(params
);
1565 root_views_
[i
] = windows_
[i
]->GetRootView();
1566 test_views_
[i
] = new TestNativeViewHierarchy
;
1567 root_views_
[i
]->AddChildView(test_views_
[i
]);
1571 ~TestChangeNativeViewHierarchy() {
1572 for (size_t i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1573 windows_
[i
]->Close();
1576 // Will close and self-delete widgets - no need to manually delete them.
1577 view_test_
->RunPendingMessages();
1580 void CheckEnumeratingNativeWidgets() {
1581 if (!host_
->GetTopLevelWidget())
1583 Widget::Widgets widgets
;
1584 Widget::GetAllChildWidgets(host_
->GetNativeView(), &widgets
);
1585 EXPECT_EQ(TestNativeViewHierarchy::kTotalViews
+ 1, widgets
.size());
1586 // Unfortunately there is no guarantee the sequence of views here so always
1587 // go through all of them.
1588 for (Widget::Widgets::iterator i
= widgets
.begin();
1589 i
!= widgets
.end(); ++i
) {
1590 View
* root_view
= (*i
)->GetRootView();
1591 if (host_
->GetRootView() == root_view
)
1594 for (j
= 0; j
< TestNativeViewHierarchy::kTotalViews
; ++j
)
1595 if (root_views_
[j
] == root_view
)
1597 // EXPECT_LT/GT/GE() fails to compile with class-defined constants
1598 // with gcc, with error
1599 // "error: undefined reference to 'TestNativeViewHierarchy::kTotalViews'"
1600 // so I forced to use EXPECT_TRUE() instead.
1601 EXPECT_TRUE(TestNativeViewHierarchy::kTotalViews
> j
);
1605 void CheckChangingHierarhy() {
1607 for (i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1608 // TODO(georgey): use actual hierarchy changes to send notifications.
1609 static_cast<internal::RootView
*>(root_views_
[i
])->
1610 NotifyNativeViewHierarchyChanged(false, host_
->GetNativeView());
1611 static_cast<internal::RootView
*>(root_views_
[i
])->
1612 NotifyNativeViewHierarchyChanged(true, host_
->GetNativeView());
1614 for (i
= 0; i
< TestNativeViewHierarchy::kTotalViews
; ++i
) {
1615 ASSERT_EQ(static_cast<size_t>(2), test_views_
[i
]->notifications_
.size());
1616 EXPECT_FALSE(test_views_
[i
]->notifications_
[0].attached
);
1617 EXPECT_EQ(host_
->GetNativeView(),
1618 test_views_
[i
]->notifications_
[0].native_view
);
1619 EXPECT_EQ(root_views_
[i
], test_views_
[i
]->notifications_
[0].root_view
);
1620 EXPECT_TRUE(test_views_
[i
]->notifications_
[1].attached
);
1621 EXPECT_EQ(host_
->GetNativeView(),
1622 test_views_
[i
]->notifications_
[1].native_view
);
1623 EXPECT_EQ(root_views_
[i
], test_views_
[i
]->notifications_
[1].root_view
);
1627 NativeViewHost
* native_host_
;
1629 Widget
* windows_
[TestNativeViewHierarchy::kTotalViews
];
1630 View
* root_views_
[TestNativeViewHierarchy::kTotalViews
];
1631 TestNativeViewHierarchy
* test_views_
[TestNativeViewHierarchy::kTotalViews
];
1632 ViewTest
* view_test_
;
1635 TEST_F(ViewTest
, ChangeNativeViewHierarchyFindRoots
) {
1636 // TODO(georgey): Fix the test for Linux
1638 TestChangeNativeViewHierarchy
test(this);
1639 test
.CheckEnumeratingNativeWidgets();
1643 TEST_F(ViewTest
, ChangeNativeViewHierarchyChangeHierarchy
) {
1644 // TODO(georgey): Fix the test for Linux
1646 TestChangeNativeViewHierarchy
test(this);
1647 test
.CheckChangingHierarhy();
1651 ////////////////////////////////////////////////////////////////////////////////
1653 ////////////////////////////////////////////////////////////////////////////////
1655 class TransformPaintView
: public TestView
{
1657 TransformPaintView() {}
1658 virtual ~TransformPaintView() {}
1660 void ClearScheduledPaintRect() {
1661 scheduled_paint_rect_
= gfx::Rect();
1664 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
1666 // Overridden from View:
1667 virtual void SchedulePaintInRect(const gfx::Rect
& rect
) {
1668 gfx::Rect xrect
= ConvertRectToParent(rect
);
1669 scheduled_paint_rect_
= scheduled_paint_rect_
.Union(xrect
);
1673 gfx::Rect scheduled_paint_rect_
;
1675 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
1678 TEST_F(ViewTest
, TransformPaint
) {
1679 TransformPaintView
* v1
= new TransformPaintView();
1680 v1
->SetBounds(0, 0, 500, 300);
1682 TestView
* v2
= new TestView();
1683 v2
->SetBounds(100, 100, 200, 100);
1685 Widget
* widget
= new Widget
;
1686 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
1687 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1688 widget
->Init(params
);
1690 View
* root
= widget
->GetRootView();
1692 root
->AddChildView(v1
);
1693 v1
->AddChildView(v2
);
1695 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1696 v1
->ClearScheduledPaintRect();
1697 v2
->SchedulePaint();
1699 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
1701 // Rotate |v1| counter-clockwise.
1702 ui::Transform transform
;
1703 RotateCounterclockwise(transform
);
1704 transform
.SetTranslateY(500.0f
);
1705 v1
->SetTransform(transform
);
1707 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1709 v1
->ClearScheduledPaintRect();
1710 v2
->SchedulePaint();
1712 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
1717 TEST_F(ViewTest
, TransformEvent
) {
1718 TestView
* v1
= new TestView();
1719 v1
->SetBounds(0, 0, 500, 300);
1721 TestView
* v2
= new TestView();
1722 v2
->SetBounds(100, 100, 200, 100);
1724 Widget
* widget
= new Widget
;
1725 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
1726 params
.bounds
= gfx::Rect(50, 50, 650, 650);
1727 widget
->Init(params
);
1728 View
* root
= widget
->GetRootView();
1730 root
->AddChildView(v1
);
1731 v1
->AddChildView(v2
);
1733 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1735 // Rotate |v1| counter-clockwise.
1736 ui::Transform
transform(v1
->GetTransform());
1737 RotateCounterclockwise(transform
);
1738 transform
.SetTranslateY(500.0f
);
1739 v1
->SetTransform(transform
);
1741 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1745 MouseEvent
pressed(ui::ET_MOUSE_PRESSED
,
1747 ui::EF_LEFT_BUTTON_DOWN
);
1748 root
->OnMousePressed(pressed
);
1749 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1750 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1751 EXPECT_EQ(190, v2
->location_
.x());
1752 EXPECT_EQ(10, v2
->location_
.y());
1754 MouseEvent
released(ui::ET_MOUSE_RELEASED
, 0, 0, 0);
1755 root
->OnMouseReleased(released
);
1757 // Now rotate |v2| inside |v1| clockwise.
1758 transform
= v2
->GetTransform();
1759 RotateClockwise(transform
);
1760 transform
.SetTranslateX(100.0f
);
1761 v2
->SetTransform(transform
);
1763 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1764 // (300, 400) in |root|.
1769 MouseEvent
p2(ui::ET_MOUSE_PRESSED
,
1771 ui::EF_LEFT_BUTTON_DOWN
);
1772 root
->OnMousePressed(p2
);
1773 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
1774 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
1775 EXPECT_EQ(10, v2
->location_
.x());
1776 EXPECT_EQ(20, v2
->location_
.y());
1778 root
->OnMouseReleased(released
);
1780 v1
->SetTransform(ui::Transform());
1781 v2
->SetTransform(ui::Transform());
1783 TestView
* v3
= new TestView();
1784 v3
->SetBounds(10, 10, 20, 30);
1785 v2
->AddChildView(v3
);
1787 // Rotate |v3| clockwise with respect to |v2|.
1788 transform
= v1
->GetTransform();
1789 RotateClockwise(transform
);
1790 transform
.SetTranslateX(30.0f
);
1791 v3
->SetTransform(transform
);
1793 // Scale |v2| with respect to |v1| along both axis.
1794 transform
= v2
->GetTransform();
1795 transform
.SetScale(0.8f
, 0.5f
);
1796 v2
->SetTransform(transform
);
1798 // |v3| occupies (108, 105) to (132, 115) in |root|.
1804 MouseEvent
p3(ui::ET_MOUSE_PRESSED
,
1806 ui::EF_LEFT_BUTTON_DOWN
);
1807 root
->OnMousePressed(p3
);
1809 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1810 EXPECT_EQ(10, v3
->location_
.x());
1811 EXPECT_EQ(25, v3
->location_
.y());
1813 root
->OnMouseReleased(released
);
1815 v1
->SetTransform(ui::Transform());
1816 v2
->SetTransform(ui::Transform());
1817 v3
->SetTransform(ui::Transform());
1823 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1824 transform
= v3
->GetTransform();
1825 RotateClockwise(transform
);
1826 transform
.SetTranslateX(30.0f
);
1827 // Rotation sets some scaling transformation. Using SetScale would overwrite
1828 // that and pollute the rotation. So combine the scaling with the existing
1830 transform
.ConcatScale(0.8f
, 0.5f
);
1831 v3
->SetTransform(transform
);
1833 // Translate |v2| with respect to |v1|.
1834 transform
= v2
->GetTransform();
1835 transform
.SetTranslate(10, 10);
1836 v2
->SetTransform(transform
);
1838 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1840 MouseEvent
p4(ui::ET_MOUSE_PRESSED
,
1842 ui::EF_LEFT_BUTTON_DOWN
);
1843 root
->OnMousePressed(p4
);
1845 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
1846 EXPECT_EQ(10, v3
->location_
.x());
1847 EXPECT_EQ(25, v3
->location_
.y());
1849 root
->OnMouseReleased(released
);
1854 TEST_F(ViewTest
, TransformVisibleBound
) {
1855 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1857 scoped_ptr
<Widget
> widget(new Widget
);
1858 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
1859 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1860 params
.bounds
= viewport_bounds
;
1861 widget
->Init(params
);
1862 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1864 View
* viewport
= new View
;
1865 widget
->SetContentsView(viewport
);
1866 View
* contents
= new View
;
1867 viewport
->AddChildView(contents
);
1868 viewport
->SetBoundsRect(viewport_bounds
);
1869 contents
->SetBounds(0, 0, 100, 200);
1871 View
* child
= new View
;
1872 contents
->AddChildView(child
);
1873 child
->SetBounds(10, 90, 50, 50);
1874 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
1876 // Rotate |child| counter-clockwise
1877 ui::Transform transform
;
1878 RotateCounterclockwise(transform
);
1879 transform
.SetTranslateY(50.0f
);
1880 child
->SetTransform(transform
);
1881 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
1886 ////////////////////////////////////////////////////////////////////////////////
1887 // OnVisibleBoundsChanged()
1889 class VisibleBoundsView
: public View
{
1891 VisibleBoundsView() : received_notification_(false) {}
1892 virtual ~VisibleBoundsView() {}
1894 bool received_notification() const { return received_notification_
; }
1895 void set_received_notification(bool received
) {
1896 received_notification_
= received
;
1900 // Overridden from View:
1901 virtual bool NeedsNotificationWhenVisibleBoundsChange() const {
1904 virtual void OnVisibleBoundsChanged() {
1905 received_notification_
= true;
1908 bool received_notification_
;
1910 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
1913 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
1914 gfx::Rect
viewport_bounds(0, 0, 100, 100);
1916 scoped_ptr
<Widget
> widget(new Widget
);
1917 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
1918 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1919 params
.bounds
= viewport_bounds
;
1920 widget
->Init(params
);
1921 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
1923 View
* viewport
= new View
;
1924 widget
->SetContentsView(viewport
);
1925 View
* contents
= new View
;
1926 viewport
->AddChildView(contents
);
1927 viewport
->SetBoundsRect(viewport_bounds
);
1928 contents
->SetBounds(0, 0, 100, 200);
1930 // Create a view that cares about visible bounds notifications, and position
1931 // it just outside the visible bounds of the viewport.
1932 VisibleBoundsView
* child
= new VisibleBoundsView
;
1933 contents
->AddChildView(child
);
1934 child
->SetBounds(10, 110, 50, 50);
1936 // The child bound should be fully clipped.
1937 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
1939 // Now scroll the contents, but not enough to make the child visible.
1940 contents
->SetY(contents
->y() - 1);
1942 // We should have received the notification since the visible bounds may have
1943 // changed (even though they didn't).
1944 EXPECT_TRUE(child
->received_notification());
1945 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
1946 child
->set_received_notification(false);
1948 // Now scroll the contents, this time by enough to make the child visible by
1950 contents
->SetY(contents
->y() - 10);
1951 EXPECT_TRUE(child
->received_notification());
1952 EXPECT_EQ(1, child
->GetVisibleBounds().height());
1953 child
->set_received_notification(false);
1958 ////////////////////////////////////////////////////////////////////////////////
1961 TEST_F(ViewTest
, SetBoundsPaint
) {
1963 TestView
* child_view
= new TestView
;
1965 top_view
.SetBounds(0, 0, 100, 100);
1966 top_view
.scheduled_paint_rects_
.clear();
1967 child_view
->SetBounds(10, 10, 20, 20);
1968 top_view
.AddChildView(child_view
);
1970 top_view
.scheduled_paint_rects_
.clear();
1971 child_view
->SetBounds(30, 30, 20, 20);
1972 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
1974 // There should be 2 rects, spanning from (10, 10) to (50, 50).
1975 gfx::Rect paint_rect
=
1976 top_view
.scheduled_paint_rects_
[0].Union(
1977 top_view
.scheduled_paint_rects_
[1]);
1978 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
1981 // Tests conversion methods with a transform.
1982 TEST_F(ViewTest
, ConvertPointToViewWithTransform
) {
1984 TestView
* child
= new TestView
;
1985 TestView
* child_child
= new TestView
;
1987 top_view
.AddChildView(child
);
1988 child
->AddChildView(child_child
);
1990 top_view
.SetBounds(0, 0, 1000, 1000);
1992 child
->SetBounds(7, 19, 500, 500);
1993 ui::Transform transform
;
1994 transform
.SetScale(3.0f
, 4.0f
);
1995 child
->SetTransform(transform
);
1997 child_child
->SetBounds(17, 13, 100, 100);
1998 transform
= ui::Transform();
1999 transform
.SetScale(5.0f
, 7.0f
);
2000 child_child
->SetTransform(transform
);
2002 // Sanity check to make sure basic transforms act as expected.
2004 ui::Transform transform
;
2005 transform
.ConcatTranslate(1, 1);
2006 transform
.ConcatScale(100, 55);
2007 transform
.ConcatTranslate(110, -110);
2009 // convert to a 3x3 matrix.
2010 const SkMatrix
& matrix
= transform
.matrix();
2012 EXPECT_EQ(210, matrix
.getTranslateX());
2013 EXPECT_EQ(-55, matrix
.getTranslateY());
2014 EXPECT_EQ(100, matrix
.getScaleX());
2015 EXPECT_EQ(55, matrix
.getScaleY());
2016 EXPECT_EQ(0, matrix
.getSkewX());
2017 EXPECT_EQ(0, matrix
.getSkewY());
2021 ui::Transform transform
;
2022 transform
.SetTranslate(1, 1);
2024 t2
.SetScale(100, 55);
2026 t3
.SetTranslate(110, -110);
2027 transform
.ConcatTransform(t2
);
2028 transform
.ConcatTransform(t3
);
2030 // convert to a 3x3 matrix
2031 const SkMatrix
& matrix
= transform
.matrix();
2033 EXPECT_EQ(210, matrix
.getTranslateX());
2034 EXPECT_EQ(-55, matrix
.getTranslateY());
2035 EXPECT_EQ(100, matrix
.getScaleX());
2036 EXPECT_EQ(55, matrix
.getScaleY());
2037 EXPECT_EQ(0, matrix
.getSkewX());
2038 EXPECT_EQ(0, matrix
.getSkewY());
2041 // Conversions from child->top and top->child.
2043 gfx::Point
point(5, 5);
2044 View::ConvertPointToView(child
, &top_view
, &point
);
2045 EXPECT_EQ(22, point
.x());
2046 EXPECT_EQ(39, point
.y());
2048 point
.SetPoint(22, 39);
2049 View::ConvertPointToView(&top_view
, child
, &point
);
2050 EXPECT_EQ(5, point
.x());
2051 EXPECT_EQ(5, point
.y());
2054 // Conversions from child_child->top and top->child_child.
2056 gfx::Point
point(5, 5);
2057 View::ConvertPointToView(child_child
, &top_view
, &point
);
2058 EXPECT_EQ(133, point
.x());
2059 EXPECT_EQ(211, point
.y());
2061 point
.SetPoint(133, 211);
2062 View::ConvertPointToView(&top_view
, child_child
, &point
);
2063 EXPECT_EQ(5, point
.x());
2064 EXPECT_EQ(5, point
.y());
2067 // Conversions from child_child->child and child->child_child
2069 gfx::Point
point(5, 5);
2070 View::ConvertPointToView(child_child
, child
, &point
);
2071 EXPECT_EQ(42, point
.x());
2072 EXPECT_EQ(48, point
.y());
2074 point
.SetPoint(42, 48);
2075 View::ConvertPointToView(child
, child_child
, &point
);
2076 EXPECT_EQ(5, point
.x());
2077 EXPECT_EQ(5, point
.y());
2080 // Conversions from top_view to child with a value that should be negative.
2081 // This ensures we don't round up with negative numbers.
2083 gfx::Point
point(6, 18);
2084 View::ConvertPointToView(&top_view
, child
, &point
);
2085 EXPECT_EQ(-1, point
.x());
2086 EXPECT_EQ(-1, point
.y());
2090 // Tests conversion methods for rectangles.
2091 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2092 scoped_ptr
<Widget
> widget(new Widget
);
2093 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
2094 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2095 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2096 widget
->Init(params
);
2097 View
* root
= widget
->GetRootView();
2099 TestView
* v1
= new TestView
;
2100 TestView
* v2
= new TestView
;
2101 root
->AddChildView(v1
);
2102 v1
->AddChildView(v2
);
2104 v1
->SetBounds(10, 10, 500, 500);
2105 v2
->SetBounds(20, 20, 100, 200);
2107 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2108 gfx::Rect
rect(5, 5, 15, 40);
2109 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2110 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2114 RotateCounterclockwise(t2
);
2115 t2
.SetTranslateY(100.0f
);
2116 v2
->SetTransform(t2
);
2118 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2119 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2120 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2124 t1
.SetScale(0.5, 0.5);
2125 v1
->SetTransform(t1
);
2127 // The rectangle should remain the same for |v1|.
2128 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2130 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2131 // There are some rounding of floating values here. These values may change if
2132 // floating operations are improved/changed.
2133 EXPECT_EQ(gfx::Rect(22, 60, 20, 7), v2
->ConvertRectToWidget(rect
));
2138 class ObserverView
: public View
{
2141 virtual ~ObserverView();
2143 void ResetTestState();
2145 bool child_added() const { return child_added_
; }
2146 bool child_removed() const { return child_removed_
; }
2147 const View
* parent_view() const { return parent_view_
; }
2148 const View
* child_view() const { return child_view_
; }
2152 virtual void ViewHierarchyChanged(bool is_add
,
2154 View
* child
) OVERRIDE
;
2157 bool child_removed_
;
2161 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2164 ObserverView::ObserverView()
2165 : child_added_(false),
2166 child_removed_(false),
2171 ObserverView::~ObserverView() {}
2173 void ObserverView::ResetTestState() {
2174 child_added_
= false;
2175 child_removed_
= false;
2176 parent_view_
= NULL
;
2180 void ObserverView::ViewHierarchyChanged(bool is_add
,
2184 child_added_
= true;
2186 child_removed_
= true;
2188 parent_view_
= parent
;
2189 child_view_
= child
;
2192 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2193 // a child view is added or removed to all the views in the hierarchy (up and
2195 // The tree looks like this:
2199 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2202 ObserverView
* v3
= new ObserverView();
2204 // Add |v3| to |v2|.
2205 scoped_ptr
<ObserverView
> v2(new ObserverView());
2206 v2
->AddChildView(v3
);
2208 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2210 EXPECT_TRUE(v2
->child_added());
2211 EXPECT_FALSE(v2
->child_removed());
2212 EXPECT_EQ(v2
.get(), v2
->parent_view());
2213 EXPECT_EQ(v3
, v2
->child_view());
2215 EXPECT_TRUE(v3
->child_added());
2216 EXPECT_FALSE(v3
->child_removed());
2217 EXPECT_EQ(v2
.get(), v3
->parent_view());
2218 EXPECT_EQ(v3
, v3
->child_view());
2220 // Reset everything to the initial state.
2221 v2
->ResetTestState();
2222 v3
->ResetTestState();
2225 v1
.AddChildView(v2
.get());
2227 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2228 // Make sure all the views (v1, v2, v3) received _that_ information.
2229 EXPECT_TRUE(v1
.child_added());
2230 EXPECT_FALSE(v1
.child_removed());
2231 EXPECT_EQ(&v1
, v1
.parent_view());
2232 EXPECT_EQ(v2
.get(), v1
.child_view());
2234 EXPECT_TRUE(v2
->child_added());
2235 EXPECT_FALSE(v2
->child_removed());
2236 EXPECT_EQ(&v1
, v2
->parent_view());
2237 EXPECT_EQ(v2
.get(), v2
->child_view());
2239 EXPECT_TRUE(v3
->child_added());
2240 EXPECT_FALSE(v3
->child_removed());
2241 EXPECT_EQ(&v1
, v3
->parent_view());
2242 EXPECT_EQ(v2
.get(), v3
->child_view());
2244 // Reset everything to the initial state.
2245 v1
.ResetTestState();
2246 v2
->ResetTestState();
2247 v3
->ResetTestState();
2249 // Remove |v2| from |v1|.
2250 v1
.RemoveChildView(v2
.get());
2252 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2253 // Make sure all the views (v1, v2, v3) received _that_ information.
2254 EXPECT_FALSE(v1
.child_added());
2255 EXPECT_TRUE(v1
.child_removed());
2256 EXPECT_EQ(&v1
, v1
.parent_view());
2257 EXPECT_EQ(v2
.get(), v1
.child_view());
2259 EXPECT_FALSE(v2
->child_added());
2260 EXPECT_TRUE(v2
->child_removed());
2261 EXPECT_EQ(&v1
, v2
->parent_view());
2262 EXPECT_EQ(v2
.get(), v2
->child_view());
2264 EXPECT_FALSE(v3
->child_added());
2265 EXPECT_TRUE(v3
->child_removed());
2266 EXPECT_EQ(&v1
, v3
->parent_view());
2267 EXPECT_EQ(v3
, v3
->child_view());
2270 // Verifies if the child views added under the root are all deleted when calling
2271 // RemoveAllChildViews.
2272 // The tree looks like this:
2281 TEST_F(ViewTest
, RemoveAllChildViews
) {
2284 View
* child1
= new View
;
2285 root
.AddChildView(child1
);
2287 for (int i
= 0; i
< 2; ++i
)
2288 root
.AddChildView(new View
);
2290 View
* foo
= new View
;
2291 child1
->AddChildView(foo
);
2293 // Add some nodes to |foo|.
2294 for (int i
= 0; i
< 3; ++i
)
2295 foo
->AddChildView(new View
);
2297 EXPECT_EQ(3, root
.child_count());
2298 EXPECT_EQ(1, child1
->child_count());
2299 EXPECT_EQ(3, foo
->child_count());
2301 // Now remove all child views from root.
2302 root
.RemoveAllChildViews(true);
2304 EXPECT_EQ(0, root
.child_count());
2305 EXPECT_FALSE(root
.has_children());
2308 TEST_F(ViewTest
, Contains
) {
2310 View
* v2
= new View
;
2311 View
* v3
= new View
;
2313 v1
.AddChildView(v2
);
2314 v2
->AddChildView(v3
);
2316 EXPECT_FALSE(v1
.Contains(NULL
));
2317 EXPECT_TRUE(v1
.Contains(&v1
));
2318 EXPECT_TRUE(v1
.Contains(v2
));
2319 EXPECT_TRUE(v1
.Contains(v3
));
2321 EXPECT_FALSE(v2
->Contains(NULL
));
2322 EXPECT_TRUE(v2
->Contains(v2
));
2323 EXPECT_FALSE(v2
->Contains(&v1
));
2324 EXPECT_TRUE(v2
->Contains(v3
));
2326 EXPECT_FALSE(v3
->Contains(NULL
));
2327 EXPECT_TRUE(v3
->Contains(v3
));
2328 EXPECT_FALSE(v3
->Contains(&v1
));
2329 EXPECT_FALSE(v3
->Contains(v2
));
2332 // Verifies if GetIndexOf() returns the correct index for the specified child
2334 // The tree looks like this:
2339 TEST_F(ViewTest
, GetIndexOf
) {
2342 View
* child1
= new View
;
2343 root
.AddChildView(child1
);
2345 View
* child2
= new View
;
2346 root
.AddChildView(child2
);
2348 View
* foo1
= new View
;
2349 child1
->AddChildView(foo1
);
2351 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
2352 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
2353 EXPECT_EQ(0, root
.GetIndexOf(child1
));
2354 EXPECT_EQ(1, root
.GetIndexOf(child2
));
2355 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
2357 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
2358 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
2359 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
2360 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
2361 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
2363 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
2364 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
2365 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
2366 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
2367 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
2370 // Verifies that the child views can be reordered correctly.
2371 TEST_F(ViewTest
, ReorderChildren
) {
2374 View
* child
= new View();
2375 root
.AddChildView(child
);
2377 View
* foo1
= new View();
2378 child
->AddChildView(foo1
);
2379 View
* foo2
= new View();
2380 child
->AddChildView(foo2
);
2381 View
* foo3
= new View();
2382 child
->AddChildView(foo3
);
2383 foo1
->set_focusable(true);
2384 foo2
->set_focusable(true);
2385 foo3
->set_focusable(true);
2387 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2388 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2389 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
2390 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
2391 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2392 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
2394 // Move |foo2| at the end.
2395 child
->ReorderChildView(foo2
, -1);
2396 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
2397 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2398 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
2399 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
2400 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2401 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
2403 // Move |foo1| at the end.
2404 child
->ReorderChildView(foo1
, -1);
2405 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
2406 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
2407 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2408 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2409 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
2410 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
2411 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
2413 // Move |foo2| to the front.
2414 child
->ReorderChildView(foo2
, 0);
2415 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
2416 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
2417 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
2418 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
2419 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
2420 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
2421 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
2424 // Verifies that GetViewByID returns the correctly child view from the specified
2426 // The tree looks like this:
2431 TEST_F(ViewTest
, GetViewByID
) {
2433 const int kV1ID
= 1;
2437 const int kV2ID
= 2;
2441 const int kV3ID
= 3;
2445 const int kV4ID
= 4;
2448 const int kV5ID
= 5;
2450 v1
.AddChildView(&v2
);
2451 v2
.AddChildView(&v3
);
2452 v2
.AddChildView(&v4
);
2454 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
2455 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
2456 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
2458 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
2459 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
2461 const int kGroup
= 1;
2462 v3
.SetGroup(kGroup
);
2463 v4
.SetGroup(kGroup
);
2466 v1
.GetViewsInGroup(kGroup
, &views
);
2467 EXPECT_EQ(2U, views
.size());
2469 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
2470 EXPECT_NE(views
.end(), i
);
2472 i
= std::find(views
.begin(), views
.end(), &v4
);
2473 EXPECT_NE(views
.end(), i
);
2476 ////////////////////////////////////////////////////////////////////////////////
2478 ////////////////////////////////////////////////////////////////////////////////
2480 #if defined(VIEWS_COMPOSITOR)
2484 // Test implementation of LayerAnimator.
2485 class TestLayerAnimator
: public ui::LayerAnimator
{
2487 TestLayerAnimator();
2489 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
2492 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
2495 gfx::Rect last_bounds_
;
2497 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
2500 TestLayerAnimator::TestLayerAnimator()
2501 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2504 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
2505 last_bounds_
= bounds
;
2510 class ViewLayerTest
: public ViewsTestBase
{
2512 ViewLayerTest() : widget_(NULL
), old_use_acceleration_(false) {}
2514 virtual ~ViewLayerTest() {
2517 // Returns the Layer used by the RootView.
2518 ui::Layer
* GetRootLayer() {
2519 #if defined(USE_AURA)
2520 ui::Layer
* root_layer
= NULL
;
2522 widget()->CalculateOffsetToAncestorWithLayer(&origin
, &root_layer
);
2525 return widget()->GetRootView()->layer();
2529 virtual void SetUp() OVERRIDE
{
2531 old_use_acceleration_
= View::get_use_acceleration_when_possible();
2532 View::set_use_acceleration_when_possible(true);
2534 ui::TestTexture::reset_live_count();
2536 widget_
= new Widget
;
2537 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
2538 params
.bounds
= gfx::Rect(50, 50, 200, 200);
2539 widget_
->Init(params
);
2541 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
2544 virtual void TearDown() OVERRIDE
{
2545 View::set_use_acceleration_when_possible(old_use_acceleration_
);
2546 widget_
->CloseNow();
2547 Widget::SetPureViews(false);
2548 ViewsTestBase::TearDown();
2551 Widget
* widget() { return widget_
; }
2555 bool old_use_acceleration_
;
2558 #if !defined(USE_AURA)
2559 // This test assumes a particular layer hierarchy that isn't valid for aura.
2560 // Ensures the RootView has a layer and its set up correctly.
2561 TEST_F(ViewLayerTest
, RootState
) {
2562 ui::Layer
* layer
= widget()->GetRootView()->layer();
2564 EXPECT_FALSE(layer
->parent());
2565 EXPECT_EQ(0u, layer
->children().size());
2566 EXPECT_FALSE(layer
->transform().HasChange());
2567 EXPECT_EQ(widget()->GetRootView()->bounds(), layer
->bounds());
2568 EXPECT_TRUE(layer
->compositor() != NULL
);
2571 // Verifies that the complete bounds of a texture are updated if the texture
2572 // needs to be refreshed and paint with a clip is invoked.
2573 // This test invokes OnNativeWidgetPaintAccelerated, which is not used by aura.
2574 TEST_F(ViewLayerTest
, PaintAll
) {
2575 View
* view
= widget()->GetRootView();
2576 ui::Layer
* layer
= GetRootLayer();
2577 view
->SetBounds(0, 0, 200, 200);
2578 widget()->OnNativeWidgetPaintAccelerated(gfx::Rect(0, 0, 1, 1));
2579 ASSERT_TRUE(layer
!= NULL
);
2580 const ui::TestTexture
* texture
=
2581 static_cast<const ui::TestTexture
*>(layer
->texture());
2582 ASSERT_TRUE(texture
!= NULL
);
2583 EXPECT_EQ(view
->GetLocalBounds(), texture
->bounds_of_last_paint());
2587 TEST_F(ViewLayerTest
, LayerToggling
) {
2588 // Because we lazily create textures the calls to DrawTree are necessary to
2589 // ensure we trigger creation of textures.
2590 #if defined(USE_AURA)
2591 ui::Layer
* root_layer
= NULL
;
2593 widget()->CalculateOffsetToAncestorWithLayer(&origin
, &root_layer
);
2595 ui::Layer
* root_layer
= widget()->GetRootView()->layer();
2597 View
* content_view
= new View
;
2598 widget()->SetContentsView(content_view
);
2600 #if !defined(USE_WEBKIT_COMPOSITOR)
2601 // TODO(piman): with the webkit compositor, we don't create Textures on
2602 // Layers. We're not supposed to be calling Layer::DrawTree. This test needs
2603 // refactoring to fully work in that case.
2604 root_layer
->DrawTree();
2605 ui::TestTexture::reset_live_count();
2608 // Create v1, give it a bounds and verify everything is set up correctly.
2609 View
* v1
= new View
;
2610 v1
->SetPaintToLayer(true);
2611 #if !defined(USE_WEBKIT_COMPOSITOR)
2612 root_layer
->DrawTree();
2613 EXPECT_EQ(0, ui::TestTexture::live_count());
2615 EXPECT_TRUE(v1
->layer() != NULL
);
2616 v1
->SetBounds(20, 30, 140, 150);
2617 content_view
->AddChildView(v1
);
2618 #if !defined(USE_WEBKIT_COMPOSITOR)
2619 root_layer
->DrawTree();
2620 EXPECT_EQ(1, ui::TestTexture::live_count());
2622 ASSERT_TRUE(v1
->layer() != NULL
);
2623 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2624 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
2626 // Create v2 as a child of v1 and do basic assertion testing.
2627 View
* v2
= new View
;
2628 v1
->AddChildView(v2
);
2629 EXPECT_TRUE(v2
->layer() == NULL
);
2630 v2
->SetBounds(10, 20, 30, 40);
2631 v2
->SetPaintToLayer(true);
2632 #if !defined(USE_WEBKIT_COMPOSITOR)
2633 root_layer
->DrawTree();
2634 EXPECT_EQ(2, ui::TestTexture::live_count());
2636 ASSERT_TRUE(v2
->layer() != NULL
);
2637 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2638 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2640 // Turn off v1s layer. v2 should still have a layer but its parent should have
2642 v1
->SetPaintToLayer(false);
2643 #if !defined(USE_WEBKIT_COMPOSITOR)
2644 root_layer
->DrawTree();
2645 EXPECT_EQ(1, ui::TestTexture::live_count());
2647 EXPECT_TRUE(v1
->layer() == NULL
);
2648 EXPECT_TRUE(v2
->layer() != NULL
);
2649 EXPECT_EQ(root_layer
, v2
->layer()->parent());
2650 ASSERT_EQ(1u, root_layer
->children().size());
2651 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
2652 // The bounds of the layer should have changed to be relative to the root view
2654 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
2656 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2657 ui::Transform transform
;
2658 transform
.SetScale(2.0f
, 2.0f
);
2659 v1
->SetTransform(transform
);
2660 #if !defined(USE_WEBKIT_COMPOSITOR)
2661 root_layer
->DrawTree();
2662 EXPECT_EQ(2, ui::TestTexture::live_count());
2664 EXPECT_TRUE(v1
->layer() != NULL
);
2665 EXPECT_TRUE(v2
->layer() != NULL
);
2666 EXPECT_EQ(root_layer
, v1
->layer()->parent());
2667 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
2668 ASSERT_EQ(1u, root_layer
->children().size());
2669 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
2670 ASSERT_EQ(1u, v1
->layer()->children().size());
2671 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
2672 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
2675 // Verifies turning on a layer wires up children correctly.
2676 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
2677 View
* content_view
= new View
;
2678 widget()->SetContentsView(content_view
);
2680 // Create v1, give it a bounds and verify everything is set up correctly.
2681 View
* v1
= new View
;
2682 content_view
->AddChildView(v1
);
2683 v1
->SetBounds(20, 30, 140, 150);
2685 View
* v2
= new View
;
2686 v1
->AddChildView(v2
);
2688 View
* v3
= new View
;
2689 v3
->SetPaintToLayer(true);
2690 v2
->AddChildView(v3
);
2691 ASSERT_TRUE(v3
->layer() != NULL
);
2693 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2695 v1
->SetPaintToLayer(true);
2696 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
2699 TEST_F(ViewLayerTest
, LayerAnimator
) {
2700 View
* content_view
= new View
;
2701 widget()->SetContentsView(content_view
);
2703 View
* v1
= new View
;
2704 content_view
->AddChildView(v1
);
2705 v1
->SetPaintToLayer(true);
2706 EXPECT_TRUE(v1
->layer() != NULL
);
2708 TestLayerAnimator
* animator
= new TestLayerAnimator();
2709 v1
->layer()->SetAnimator(animator
);
2711 gfx::Rect
bounds(1, 2, 3, 4);
2712 v1
->SetBoundsRect(bounds
);
2713 EXPECT_EQ(bounds
, animator
->last_bounds());
2714 // TestLayerAnimator doesn't update the layer.
2715 EXPECT_NE(bounds
, v1
->layer()->bounds());
2718 // Verifies the bounds of a layer are updated if the bounds of ancestor that
2719 // doesn't have a layer change.
2720 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
2721 View
* content_view
= new View
;
2722 widget()->SetContentsView(content_view
);
2724 View
* v1
= new View
;
2725 content_view
->AddChildView(v1
);
2726 v1
->SetBounds(20, 30, 140, 150);
2728 View
* v2
= new View
;
2729 v2
->SetBounds(10, 11, 40, 50);
2730 v1
->AddChildView(v2
);
2731 v2
->SetPaintToLayer(true);
2732 ASSERT_TRUE(v2
->layer() != NULL
);
2733 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
2735 v1
->SetPosition(gfx::Point(25, 36));
2736 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
2738 v2
->SetPosition(gfx::Point(11, 12));
2739 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
2741 // Bounds of the layer should change even if the view is not invisible.
2742 v1
->SetVisible(false);
2743 v1
->SetPosition(gfx::Point(20, 30));
2744 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
2746 v2
->SetVisible(false);
2747 v2
->SetBounds(10, 11, 20, 30);
2748 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
2751 // Makes sure a transform persists after toggling the visibility.
2752 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
2753 View
* view
= new View
;
2754 ui::Transform transform
;
2755 transform
.SetScale(2.0f
, 2.0f
);
2756 view
->SetTransform(transform
);
2757 widget()->SetContentsView(view
);
2758 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2760 view
->SetVisible(false);
2761 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2763 view
->SetVisible(true);
2764 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2767 // Verifies a transform persists after removing/adding a view with a transform.
2768 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
2769 View
* view
= new View
;
2770 ui::Transform transform
;
2771 transform
.SetScale(2.0f
, 2.0f
);
2772 view
->SetTransform(transform
);
2773 widget()->SetContentsView(view
);
2774 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2775 ASSERT_TRUE(view
->layer() != NULL
);
2776 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
2778 View
* parent
= view
->parent();
2779 parent
->RemoveChildView(view
);
2780 parent
->AddChildView(view
);
2782 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
2783 ASSERT_TRUE(view
->layer() != NULL
);
2784 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
2787 // Makes sure that layer visibility is correct after toggling View visibility.
2788 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
2789 View
* content_view
= new View
;
2790 widget()->SetContentsView(content_view
);
2792 // The view isn't attached to a widget or a parent view yet. But it should
2793 // still have a layer, but the layer should not be attached to the root
2795 View
* v1
= new View
;
2796 v1
->SetPaintToLayer(true);
2797 EXPECT_TRUE(v1
->layer());
2798 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2801 // Once the view is attached to a widget, its layer should be attached to the
2802 // root layer and visible.
2803 content_view
->AddChildView(v1
);
2804 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2806 EXPECT_TRUE(v1
->layer()->IsDrawn());
2808 v1
->SetVisible(false);
2809 EXPECT_FALSE(v1
->layer()->IsDrawn());
2811 v1
->SetVisible(true);
2812 EXPECT_TRUE(v1
->layer()->IsDrawn());
2815 EXPECT_FALSE(v1
->layer()->IsDrawn());
2818 EXPECT_TRUE(v1
->layer()->IsDrawn());
2821 // Test that a hole in a layer is correctly created regardless of whether
2822 // the opacity attribute is set before or after the layer is created.
2823 TEST_F(ViewLayerTest
, ToggleOpacityWithLayer
) {
2824 View
* content_view
= new View
;
2825 widget()->SetContentsView(content_view
);
2827 View
* parent_view
= new View
;
2828 content_view
->AddChildView(parent_view
);
2829 parent_view
->SetPaintToLayer(true);
2830 parent_view
->SetBounds(0, 0, 400, 400);
2832 View
* child_view
= new View
;
2833 child_view
->SetBounds(50, 50, 100, 100);
2834 parent_view
->AddChildView(child_view
);
2836 widget()->GetCompositor()->Draw(false);
2838 ASSERT_TRUE(child_view
->layer() == NULL
);
2839 child_view
->SetPaintToLayer(true);
2840 child_view
->SetFillsBoundsOpaquely(true);
2841 widget()->GetCompositor()->Draw(false);
2842 ASSERT_TRUE(child_view
->layer());
2844 gfx::Rect(50, 50, 100, 100), parent_view
->layer()->hole_rect());
2846 child_view
->SetFillsBoundsOpaquely(false);
2847 widget()->GetCompositor()->Draw(false);
2848 EXPECT_TRUE(parent_view
->layer()->hole_rect().IsEmpty());
2851 // Test that a hole in a layer always corresponds to the bounds of opaque
2853 TEST_F(ViewLayerTest
, MultipleOpaqueLayers
) {
2854 View
* content_view
= new View
;
2855 widget()->SetContentsView(content_view
);
2857 View
* parent_view
= new View
;
2858 parent_view
->SetPaintToLayer(true);
2859 parent_view
->SetBounds(0, 0, 400, 400);
2860 content_view
->AddChildView(parent_view
);
2862 View
* child_view1
= new View
;
2863 child_view1
->SetPaintToLayer(true);
2864 child_view1
->SetFillsBoundsOpaquely(true);
2865 child_view1
->SetBounds(50, 50, 100, 100);
2866 parent_view
->AddChildView(child_view1
);
2868 View
* child_view2
= new View
;
2869 child_view2
->SetPaintToLayer(true);
2870 child_view2
->SetFillsBoundsOpaquely(false);
2871 child_view2
->SetBounds(150, 150, 200, 200);
2872 parent_view
->AddChildView(child_view2
);
2874 widget()->GetCompositor()->Draw(false);
2876 // Only child_view1 is opaque
2878 gfx::Rect(50, 50, 100, 100), parent_view
->layer()->hole_rect());
2880 // Both child views are opaque
2881 child_view2
->SetFillsBoundsOpaquely(true);
2882 widget()->GetCompositor()->Draw(false);
2884 gfx::Rect(50, 50, 100, 100) == parent_view
->layer()->hole_rect() ||
2885 gfx::Rect(150, 150, 200, 200) == parent_view
->layer()->hole_rect());
2887 // Only child_view2 is opaque
2890 gfx::Rect(150, 150, 200, 200), parent_view
->layer()->hole_rect());
2893 // Makes sure that opacity of layer persists after toggling visibilty.
2894 TEST_F(ViewLayerTest
, ToggleVisibilityWithOpaqueLayer
) {
2895 View
* content_view
= new View
;
2896 widget()->SetContentsView(content_view
);
2898 View
* parent_view
= new View
;
2899 parent_view
->SetPaintToLayer(true);
2900 parent_view
->SetBounds(0, 0, 400, 400);
2901 content_view
->AddChildView(parent_view
);
2903 parent_view
->SetPaintToLayer(true);
2904 parent_view
->SetBounds(0, 0, 400, 400);
2906 View
* child_view
= new View
;
2907 child_view
->SetBounds(50, 50, 100, 100);
2908 child_view
->SetPaintToLayer(true);
2909 child_view
->SetFillsBoundsOpaquely(true);
2910 parent_view
->AddChildView(child_view
);
2911 widget()->GetCompositor()->Draw(false);
2913 gfx::Rect(50, 50, 100, 100), parent_view
->layer()->hole_rect());
2915 child_view
->SetVisible(false);
2916 widget()->GetCompositor()->Draw(false);
2917 EXPECT_TRUE(parent_view
->layer()->hole_rect().IsEmpty());
2919 child_view
->SetVisible(true);
2920 widget()->GetCompositor()->Draw(false);
2922 gfx::Rect(50, 50, 100, 100), parent_view
->layer()->hole_rect());
2925 // Tests that the layers in the subtree are orphaned after a View is removed
2927 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
2928 View
* content_view
= new View
;
2929 widget()->SetContentsView(content_view
);
2931 View
* v1
= new View
;
2932 content_view
->AddChildView(v1
);
2934 View
* v2
= new View
;
2935 v1
->AddChildView(v2
);
2936 v2
->SetPaintToLayer(true);
2937 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2939 EXPECT_TRUE(v2
->layer()->IsDrawn());
2941 content_view
->RemoveChildView(v1
);
2942 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2946 content_view
->AddChildView(v2
);
2947 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2949 EXPECT_TRUE(v2
->layer()->IsDrawn());
2952 // TODO(sky): reenable once focus issues are straightened out so that this
2954 TEST_F(ViewLayerTest
, DISABLED_NativeWidgetView
) {
2955 View
* content_view
= new View
;
2956 widget()->SetContentsView(content_view
);
2957 View
* view
= new View
;
2958 content_view
->AddChildView(view
);
2959 view
->SetBounds(10, 20, 300, 400);
2961 views_delegate().set_default_parent_view(view
);
2962 Widget::SetPureViews(true);
2963 scoped_ptr
<Widget
> child_widget(new Widget
);
2964 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
2965 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2966 params
.bounds
= gfx::Rect(1, 2, 100, 200);
2967 child_widget
->Init(params
);
2969 // NativeWidgetView should have been added to view.
2970 ASSERT_EQ(1, view
->child_count());
2971 View
* widget_view_host
= view
->child_at(0);
2972 ASSERT_TRUE(widget_view_host
->layer() != NULL
);
2973 EXPECT_EQ(gfx::Rect(11, 22, 100, 200), widget_view_host
->layer()->bounds());
2975 View
* widget_content_view
= new View
;
2976 child_widget
->SetContentsView(widget_content_view
);
2977 View
* child_view
= new View
;
2978 child_view
->SetPaintToLayer(true);
2979 child_view
->SetBounds(5, 6, 10, 11);
2980 widget_content_view
->AddChildView(child_view
);
2982 ASSERT_TRUE(child_view
->layer() != NULL
);
2983 EXPECT_EQ(gfx::Rect(5, 6, 10, 11), child_view
->layer()->bounds());
2985 widget_view_host
->SetPaintToLayer(false);
2986 EXPECT_TRUE(widget_view_host
->layer() == NULL
);
2988 ASSERT_TRUE(child_view
->layer() != NULL
);
2989 EXPECT_EQ(gfx::Rect(16, 28, 10, 11), child_view
->layer()->bounds());
2991 widget_view_host
->SetPaintToLayer(true);
2992 ASSERT_TRUE(widget_view_host
->layer() != NULL
);
2993 EXPECT_EQ(gfx::Rect(11, 22, 100, 200), widget_view_host
->layer()->bounds());
2994 ASSERT_TRUE(child_view
->layer() != NULL
);
2995 EXPECT_EQ(gfx::Rect(5, 6, 10, 11), child_view
->layer()->bounds());
2997 child_widget
->CloseNow();
3000 class PaintTrackingView
: public View
{
3002 PaintTrackingView() : painted_(false) {
3005 bool painted() const { return painted_
; }
3006 void set_painted(bool value
) { painted_
= value
; }
3008 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
3015 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3018 #if !defined(USE_WEBKIT_COMPOSITOR)
3019 // TODO(piman): this test relies on the way the non-webkit compositor works.
3020 // Layer::DrawTree should not be called with the webkit compositor. In the
3021 // WebKit case, it needs to go through the "real" compositor (not the test one)
3022 // to do the paints on the layer/views.
3024 // Makes sure child views with layers aren't painted when paint starts at an
3026 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3027 PaintTrackingView
* content_view
= new PaintTrackingView
;
3028 widget()->SetContentsView(content_view
);
3029 content_view
->SetPaintToLayer(true);
3030 GetRootLayer()->DrawTree();
3031 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3032 content_view
->set_painted(false);
3033 // content_view no longer has a dirty rect. Paint from the root and make sure
3034 // PaintTrackingView isn't painted.
3035 GetRootLayer()->DrawTree();
3036 EXPECT_FALSE(content_view
->painted());
3038 // Make content_view have a dirty rect, paint the layers and make sure
3039 // PaintTrackingView is painted.
3040 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3041 GetRootLayer()->DrawTree();
3042 EXPECT_TRUE(content_view
->painted());
3046 // Tests that the visibility of child layers are updated correctly when a View's
3047 // visibility changes.
3048 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3049 View
* v1
= new View
;
3050 v1
->SetPaintToLayer(true);
3051 widget()->SetContentsView(v1
);
3053 View
* v2
= new View
;
3054 v1
->AddChildView(v2
);
3056 View
* v3
= new View
;
3057 v2
->AddChildView(v3
);
3058 v3
->SetVisible(false);
3060 View
* v4
= new View
;
3061 v4
->SetPaintToLayer(true);
3062 v3
->AddChildView(v4
);
3064 EXPECT_TRUE(v1
->layer()->IsDrawn());
3065 EXPECT_FALSE(v4
->layer()->IsDrawn());
3067 v2
->SetVisible(false);
3068 EXPECT_TRUE(v1
->layer()->IsDrawn());
3069 EXPECT_FALSE(v4
->layer()->IsDrawn());
3071 v2
->SetVisible(true);
3072 EXPECT_TRUE(v1
->layer()->IsDrawn());
3073 EXPECT_FALSE(v4
->layer()->IsDrawn());
3075 v2
->SetVisible(false);
3076 EXPECT_TRUE(v1
->layer()->IsDrawn());
3077 EXPECT_FALSE(v4
->layer()->IsDrawn());
3078 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3080 v3
->SetVisible(true);
3081 EXPECT_TRUE(v1
->layer()->IsDrawn());
3082 EXPECT_FALSE(v4
->layer()->IsDrawn());
3083 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3085 // Reparent |v3| to |v1|.
3086 v1
->AddChildView(v3
);
3087 EXPECT_TRUE(v1
->layer()->IsDrawn());
3088 EXPECT_TRUE(v4
->layer()->IsDrawn());
3089 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3092 // This test creates a random View tree, and then randomly reorders child views,
3093 // reparents views etc. Unrelated changes can appear to break this test. So
3094 // marking this as FLAKY.
3095 TEST_F(ViewLayerTest
, FLAKY_ViewLayerTreesInSync
) {
3096 View
* content
= new View
;
3097 content
->SetPaintToLayer(true);
3098 widget()->SetContentsView(content
);
3101 ConstructTree(content
, 5);
3102 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3104 ScrambleTree(content
);
3105 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3107 ScrambleTree(content
);
3108 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3110 ScrambleTree(content
);
3111 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3114 #endif // VIEWS_COMPOSITOR
3116 } // namespace views