1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/rand_util.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "cc/playback/display_item_list.h"
13 #include "cc/playback/display_item_list_settings.h"
14 #include "ui/base/accelerators/accelerator.h"
15 #include "ui/base/clipboard/clipboard.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/compositor/compositor.h"
18 #include "ui/compositor/layer.h"
19 #include "ui/compositor/layer_animator.h"
20 #include "ui/compositor/paint_context.h"
21 #include "ui/compositor/test/draw_waiter_for_test.h"
22 #include "ui/events/event.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/events/keycodes/keyboard_codes.h"
25 #include "ui/gfx/canvas.h"
26 #include "ui/gfx/path.h"
27 #include "ui/gfx/transform.h"
28 #include "ui/strings/grit/ui_strings.h"
29 #include "ui/views/background.h"
30 #include "ui/views/controls/native/native_view_host.h"
31 #include "ui/views/controls/scroll_view.h"
32 #include "ui/views/controls/textfield/textfield.h"
33 #include "ui/views/focus/view_storage.h"
34 #include "ui/views/test/views_test_base.h"
35 #include "ui/views/view.h"
36 #include "ui/views/widget/native_widget.h"
37 #include "ui/views/widget/root_view.h"
38 #include "ui/views/window/dialog_client_view.h"
39 #include "ui/views/window/dialog_delegate.h"
41 using base::ASCIIToUTF16
;
45 // Returns true if |ancestor| is an ancestor of |layer|.
46 bool LayerIsAncestor(const ui::Layer
* ancestor
, const ui::Layer
* layer
) {
47 while (layer
&& layer
!= ancestor
)
48 layer
= layer
->parent();
49 return layer
== ancestor
;
52 // Convenience functions for walking a View tree.
53 const views::View
* FirstView(const views::View
* view
) {
54 const views::View
* v
= view
;
55 while (v
->has_children())
60 const views::View
* NextView(const views::View
* view
) {
61 const views::View
* v
= view
;
62 const views::View
* parent
= v
->parent();
65 int next
= parent
->GetIndexOf(v
) + 1;
66 if (next
!= parent
->child_count())
67 return FirstView(parent
->child_at(next
));
71 // Convenience functions for walking a Layer tree.
72 const ui::Layer
* FirstLayer(const ui::Layer
* layer
) {
73 const ui::Layer
* l
= layer
;
74 while (l
->children().size() > 0)
79 const ui::Layer
* NextLayer(const ui::Layer
* layer
) {
80 const ui::Layer
* parent
= layer
->parent();
83 const std::vector
<ui::Layer
*> children
= parent
->children();
85 for (index
= 0; index
< children
.size(); index
++) {
86 if (children
[index
] == layer
)
89 size_t next
= index
+ 1;
90 if (next
< children
.size())
91 return FirstLayer(children
[next
]);
95 // Given the root nodes of a View tree and a Layer tree, makes sure the two
97 bool ViewAndLayerTreeAreConsistent(const views::View
* view
,
98 const ui::Layer
* layer
) {
99 const views::View
* v
= FirstView(view
);
100 const ui::Layer
* l
= FirstLayer(layer
);
102 // Find the view with a layer.
103 while (v
&& !v
->layer())
109 // Check if the View tree and the Layer tree are in sync.
110 EXPECT_EQ(l
, v
->layer());
114 // Check if the visibility states of the View and the Layer are in sync.
115 EXPECT_EQ(l
->IsDrawn(), v
->IsDrawn());
116 if (v
->IsDrawn() != l
->IsDrawn()) {
117 for (const views::View
* vv
= v
; vv
; vv
= vv
->parent())
118 LOG(ERROR
) << "V: " << vv
<< " " << vv
->visible() << " "
119 << vv
->IsDrawn() << " " << vv
->layer();
120 for (const ui::Layer
* ll
= l
; ll
; ll
= ll
->parent())
121 LOG(ERROR
) << "L: " << ll
<< " " << ll
->IsDrawn();
125 // Check if the size of the View and the Layer are in sync.
126 EXPECT_EQ(l
->bounds(), v
->bounds());
127 if (v
->bounds() != l
->bounds())
130 if (v
== view
|| l
== layer
)
131 return v
== view
&& l
== layer
;
140 // Constructs a View tree with the specified depth.
141 void ConstructTree(views::View
* view
, int depth
) {
144 int count
= base::RandInt(1, 5);
145 for (int i
= 0; i
< count
; i
++) {
146 views::View
* v
= new views::View
;
147 view
->AddChildView(v
);
148 if (base::RandDouble() > 0.5)
149 v
->SetPaintToLayer(true);
150 if (base::RandDouble() < 0.2)
151 v
->SetVisible(false);
153 ConstructTree(v
, depth
- 1);
157 void ScrambleTree(views::View
* view
) {
158 int count
= view
->child_count();
161 for (int i
= 0; i
< count
; i
++) {
162 ScrambleTree(view
->child_at(i
));
166 int a
= base::RandInt(0, count
- 1);
167 int b
= base::RandInt(0, count
- 1);
169 views::View
* view_a
= view
->child_at(a
);
170 views::View
* view_b
= view
->child_at(b
);
171 view
->ReorderChildView(view_a
, b
);
172 view
->ReorderChildView(view_b
, a
);
175 if (!view
->layer() && base::RandDouble() < 0.1)
176 view
->SetPaintToLayer(true);
178 if (base::RandDouble() < 0.1)
179 view
->SetVisible(!view
->visible());
185 locale_
= l10n_util::GetApplicationLocale(std::string());
186 base::i18n::SetICUDefaultLocale("he");
188 ~ScopedRTL() { base::i18n::SetICUDefaultLocale(locale_
); }
198 typedef ViewsTestBase ViewTest
;
200 // A derived class for testing purpose.
201 class TestView
: public View
{
205 delete_on_pressed_(false),
208 can_process_events_within_subtree_(true) {}
209 ~TestView() override
{}
211 // Reset all test state
213 did_change_bounds_
= false;
214 last_mouse_event_type_
= 0;
215 location_
.SetPoint(0, 0);
216 received_mouse_enter_
= false;
217 received_mouse_exit_
= false;
219 accelerator_count_map_
.clear();
220 can_process_events_within_subtree_
= true;
223 // Exposed as public for testing.
225 views::View::Focus();
232 bool focusable() const { return View::focusable(); }
234 void set_can_process_events_within_subtree(bool can_process
) {
235 can_process_events_within_subtree_
= can_process
;
238 bool CanProcessEventsWithinSubtree() const override
{
239 return can_process_events_within_subtree_
;
242 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
243 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
244 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
245 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
246 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
247 void OnMouseExited(const ui::MouseEvent
& event
) override
;
249 void OnPaint(gfx::Canvas
* canvas
) override
;
250 void SchedulePaintInRect(const gfx::Rect
& rect
) override
;
251 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
253 void OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) override
;
256 bool did_change_bounds_
;
257 gfx::Rect new_bounds_
;
260 int last_mouse_event_type_
;
261 gfx::Point location_
;
262 bool received_mouse_enter_
;
263 bool received_mouse_exit_
;
264 bool delete_on_pressed_
;
267 std::vector
<gfx::Rect
> scheduled_paint_rects_
;
271 std::map
<ui::Accelerator
, int> accelerator_count_map_
;
274 const ui::NativeTheme
* native_theme_
;
276 // Value to return from CanProcessEventsWithinSubtree().
277 bool can_process_events_within_subtree_
;
280 ////////////////////////////////////////////////////////////////////////////////
282 ////////////////////////////////////////////////////////////////////////////////
284 void TestView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
285 did_change_bounds_
= true;
286 new_bounds_
= bounds();
289 TEST_F(ViewTest
, OnBoundsChanged
) {
292 gfx::Rect
prev_rect(0, 0, 200, 200);
293 gfx::Rect
new_rect(100, 100, 250, 250);
295 v
.SetBoundsRect(prev_rect
);
297 v
.SetBoundsRect(new_rect
);
299 EXPECT_TRUE(v
.did_change_bounds_
);
300 EXPECT_EQ(v
.new_bounds_
, new_rect
);
301 EXPECT_EQ(v
.bounds(), new_rect
);
304 ////////////////////////////////////////////////////////////////////////////////
306 ////////////////////////////////////////////////////////////////////////////////
308 bool TestView::OnMousePressed(const ui::MouseEvent
& event
) {
309 last_mouse_event_type_
= event
.type();
310 location_
.SetPoint(event
.x(), event
.y());
311 if (delete_on_pressed_
)
316 bool TestView::OnMouseDragged(const ui::MouseEvent
& event
) {
317 last_mouse_event_type_
= event
.type();
318 location_
.SetPoint(event
.x(), event
.y());
322 void TestView::OnMouseReleased(const ui::MouseEvent
& event
) {
323 last_mouse_event_type_
= event
.type();
324 location_
.SetPoint(event
.x(), event
.y());
327 void TestView::OnMouseEntered(const ui::MouseEvent
& event
) {
328 received_mouse_enter_
= true;
331 void TestView::OnMouseExited(const ui::MouseEvent
& event
) {
332 received_mouse_exit_
= true;
335 TEST_F(ViewTest
, MouseEvent
) {
336 TestView
* v1
= new TestView();
337 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
339 TestView
* v2
= new TestView();
340 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
342 scoped_ptr
<Widget
> widget(new Widget
);
343 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
344 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
345 params
.bounds
= gfx::Rect(50, 50, 650, 650);
346 widget
->Init(params
);
347 internal::RootView
* root
=
348 static_cast<internal::RootView
*>(widget
->GetRootView());
350 root
->AddChildView(v1
);
351 v1
->AddChildView(v2
);
356 gfx::Point
p1(110, 120);
357 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
, ui::EventTimeForNow(),
358 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
359 root
->OnMousePressed(pressed
);
360 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_PRESSED
);
361 EXPECT_EQ(v2
->location_
.x(), 10);
362 EXPECT_EQ(v2
->location_
.y(), 20);
363 // Make sure v1 did not receive the event
364 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
366 // Drag event out of bounds. Should still go to v2
369 gfx::Point
p2(50, 40);
370 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, p2
, p2
, ui::EventTimeForNow(),
371 ui::EF_LEFT_MOUSE_BUTTON
, 0);
372 root
->OnMouseDragged(dragged
);
373 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_DRAGGED
);
374 EXPECT_EQ(v2
->location_
.x(), -50);
375 EXPECT_EQ(v2
->location_
.y(), -60);
376 // Make sure v1 did not receive the event
377 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
379 // Releasted event out of bounds. Should still go to v2
382 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
383 ui::EventTimeForNow(), 0, 0);
384 root
->OnMouseDragged(released
);
385 EXPECT_EQ(v2
->last_mouse_event_type_
, ui::ET_MOUSE_RELEASED
);
386 EXPECT_EQ(v2
->location_
.x(), -100);
387 EXPECT_EQ(v2
->location_
.y(), -100);
388 // Make sure v1 did not receive the event
389 EXPECT_EQ(v1
->last_mouse_event_type_
, 0);
394 // Confirm that a view can be deleted as part of processing a mouse press.
395 TEST_F(ViewTest
, DeleteOnPressed
) {
396 TestView
* v1
= new TestView();
397 v1
->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
399 TestView
* v2
= new TestView();
400 v2
->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
405 scoped_ptr
<Widget
> widget(new Widget
);
406 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
407 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
408 params
.bounds
= gfx::Rect(50, 50, 650, 650);
409 widget
->Init(params
);
410 View
* root
= widget
->GetRootView();
412 root
->AddChildView(v1
);
413 v1
->AddChildView(v2
);
415 v2
->delete_on_pressed_
= true;
416 gfx::Point
point(110, 120);
417 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
418 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
419 ui::EF_LEFT_MOUSE_BUTTON
);
420 root
->OnMousePressed(pressed
);
421 EXPECT_EQ(0, v1
->child_count());
426 ////////////////////////////////////////////////////////////////////////////////
428 ////////////////////////////////////////////////////////////////////////////////
430 void TestView::OnPaint(gfx::Canvas
* canvas
) {
436 // Helper class to create a Widget with standard parameters that is closed when
437 // the helper class goes out of scope.
438 class ScopedTestPaintWidget
{
440 explicit ScopedTestPaintWidget(const Widget::InitParams
& params
)
441 : widget_(new Widget
) {
442 widget_
->Init(params
);
443 widget_
->GetRootView()->SetBounds(0, 0, 25, 26);
446 ~ScopedTestPaintWidget() {
450 Widget
* operator->() { return widget_
; }
455 DISALLOW_COPY_AND_ASSIGN(ScopedTestPaintWidget
);
460 TEST_F(ViewTest
, PaintEmptyView
) {
461 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
462 View
* root_view
= widget
->GetRootView();
465 TestView
* v1
= new TestView
;
466 v1
->SetBounds(10, 11, 0, 1);
467 root_view
->AddChildView(v1
);
469 // |v11| is a child of an empty |v1|.
470 TestView
* v11
= new TestView
;
471 v11
->SetBounds(3, 4, 6, 5);
472 v1
->AddChildView(v11
);
475 TestView
* v2
= new TestView
;
476 v2
->SetBounds(3, 4, 6, 5);
477 root_view
->AddChildView(v2
);
479 // Paint "everything".
480 gfx::Rect
first_paint(1, 1);
481 scoped_refptr
<cc::DisplayItemList
> list
=
482 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
483 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
485 // The empty view has nothing to paint so it doesn't try build a cache, nor do
486 // its children which would be clipped by its (empty) self.
487 EXPECT_FALSE(v1
->did_paint_
);
488 EXPECT_FALSE(v11
->did_paint_
);
489 EXPECT_TRUE(v2
->did_paint_
);
492 TEST_F(ViewTest
, PaintWithUnknownInvalidation
) {
493 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
494 View
* root_view
= widget
->GetRootView();
496 TestView
* v1
= new TestView
;
497 v1
->SetBounds(10, 11, 12, 13);
498 root_view
->AddChildView(v1
);
500 TestView
* v2
= new TestView
;
501 v2
->SetBounds(3, 4, 6, 5);
502 v1
->AddChildView(v2
);
504 // Paint everything once, since it has to build its cache. Then we can test
506 gfx::Rect
first_paint(1, 1);
507 scoped_refptr
<cc::DisplayItemList
> list
=
508 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
509 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
513 gfx::Rect
paint_area(1, 1);
514 gfx::Rect
root_area(root_view
->size());
515 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
517 // With a known invalidation, v1 and v2 are not painted.
518 EXPECT_FALSE(v1
->did_paint_
);
519 EXPECT_FALSE(v2
->did_paint_
);
520 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
521 EXPECT_FALSE(v1
->did_paint_
);
522 EXPECT_FALSE(v2
->did_paint_
);
524 // With unknown invalidation, v1 and v2 are painted.
526 ui::PaintContext(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
),
527 ui::PaintContext::CLONE_WITHOUT_INVALIDATION
));
528 EXPECT_TRUE(v1
->did_paint_
);
529 EXPECT_TRUE(v2
->did_paint_
);
532 TEST_F(ViewTest
, PaintContainsChildren
) {
533 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
534 View
* root_view
= widget
->GetRootView();
536 TestView
* v1
= new TestView
;
537 v1
->SetBounds(10, 11, 12, 13);
538 root_view
->AddChildView(v1
);
540 TestView
* v2
= new TestView
;
541 v2
->SetBounds(3, 4, 6, 5);
542 v1
->AddChildView(v2
);
544 // Paint everything once, since it has to build its cache. Then we can test
546 gfx::Rect
first_paint(1, 1);
547 scoped_refptr
<cc::DisplayItemList
> list
=
548 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
549 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
553 gfx::Rect
paint_area(25, 26);
554 gfx::Rect
root_area(root_view
->size());
555 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
557 EXPECT_FALSE(v1
->did_paint_
);
558 EXPECT_FALSE(v2
->did_paint_
);
559 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
560 EXPECT_TRUE(v1
->did_paint_
);
561 EXPECT_TRUE(v2
->did_paint_
);
564 TEST_F(ViewTest
, PaintContainsChildrenInRTL
) {
566 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
567 View
* root_view
= widget
->GetRootView();
569 TestView
* v1
= new TestView
;
570 v1
->SetBounds(10, 11, 12, 13);
571 root_view
->AddChildView(v1
);
573 TestView
* v2
= new TestView
;
574 v2
->SetBounds(3, 4, 6, 5);
575 v1
->AddChildView(v2
);
577 // Verify where the layers actually appear.
578 v1
->SetPaintToLayer(true);
579 // x: 25 - 10(x) - 12(width) = 3
580 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
581 v1
->SetPaintToLayer(false);
583 v2
->SetPaintToLayer(true);
584 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
585 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
586 v2
->SetPaintToLayer(false);
588 // Paint everything once, since it has to build its cache. Then we can test
590 gfx::Rect
first_paint(1, 1);
591 scoped_refptr
<cc::DisplayItemList
> list
=
592 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
593 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
597 gfx::Rect
paint_area(25, 26);
598 gfx::Rect
root_area(root_view
->size());
599 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
601 EXPECT_FALSE(v1
->did_paint_
);
602 EXPECT_FALSE(v2
->did_paint_
);
603 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
604 EXPECT_TRUE(v1
->did_paint_
);
605 EXPECT_TRUE(v2
->did_paint_
);
608 TEST_F(ViewTest
, PaintIntersectsChildren
) {
609 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
610 View
* root_view
= widget
->GetRootView();
612 TestView
* v1
= new TestView
;
613 v1
->SetBounds(10, 11, 12, 13);
614 root_view
->AddChildView(v1
);
616 TestView
* v2
= new TestView
;
617 v2
->SetBounds(3, 4, 6, 5);
618 v1
->AddChildView(v2
);
620 // Paint everything once, since it has to build its cache. Then we can test
622 gfx::Rect
first_paint(1, 1);
623 scoped_refptr
<cc::DisplayItemList
> list
=
624 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
625 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
629 gfx::Rect
paint_area(9, 10, 5, 6);
630 gfx::Rect
root_area(root_view
->size());
631 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
633 EXPECT_FALSE(v1
->did_paint_
);
634 EXPECT_FALSE(v2
->did_paint_
);
635 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
636 EXPECT_TRUE(v1
->did_paint_
);
637 EXPECT_TRUE(v2
->did_paint_
);
640 TEST_F(ViewTest
, PaintIntersectsChildrenInRTL
) {
642 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
643 View
* root_view
= widget
->GetRootView();
645 TestView
* v1
= new TestView
;
646 v1
->SetBounds(10, 11, 12, 13);
647 root_view
->AddChildView(v1
);
649 TestView
* v2
= new TestView
;
650 v2
->SetBounds(3, 4, 6, 5);
651 v1
->AddChildView(v2
);
653 // Verify where the layers actually appear.
654 v1
->SetPaintToLayer(true);
655 // x: 25 - 10(x) - 12(width) = 3
656 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
657 v1
->SetPaintToLayer(false);
659 v2
->SetPaintToLayer(true);
660 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
661 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
662 v2
->SetPaintToLayer(false);
664 // Paint everything once, since it has to build its cache. Then we can test
666 gfx::Rect
first_paint(1, 1);
667 scoped_refptr
<cc::DisplayItemList
> list
=
668 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
669 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
673 gfx::Rect
paint_area(2, 10, 5, 6);
674 gfx::Rect
root_area(root_view
->size());
675 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
677 EXPECT_FALSE(v1
->did_paint_
);
678 EXPECT_FALSE(v2
->did_paint_
);
679 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
680 EXPECT_TRUE(v1
->did_paint_
);
681 EXPECT_TRUE(v2
->did_paint_
);
684 TEST_F(ViewTest
, PaintIntersectsChildButNotGrandChild
) {
685 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
686 View
* root_view
= widget
->GetRootView();
688 TestView
* v1
= new TestView
;
689 v1
->SetBounds(10, 11, 12, 13);
690 root_view
->AddChildView(v1
);
692 TestView
* v2
= new TestView
;
693 v2
->SetBounds(3, 4, 6, 5);
694 v1
->AddChildView(v2
);
696 // Paint everything once, since it has to build its cache. Then we can test
698 gfx::Rect
first_paint(1, 1);
699 scoped_refptr
<cc::DisplayItemList
> list
=
700 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
701 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
705 gfx::Rect
paint_area(9, 10, 2, 3);
706 gfx::Rect
root_area(root_view
->size());
707 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
709 EXPECT_FALSE(v1
->did_paint_
);
710 EXPECT_FALSE(v2
->did_paint_
);
711 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
712 EXPECT_TRUE(v1
->did_paint_
);
713 EXPECT_FALSE(v2
->did_paint_
);
716 TEST_F(ViewTest
, PaintIntersectsChildButNotGrandChildInRTL
) {
718 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
719 View
* root_view
= widget
->GetRootView();
721 TestView
* v1
= new TestView
;
722 v1
->SetBounds(10, 11, 12, 13);
723 root_view
->AddChildView(v1
);
725 TestView
* v2
= new TestView
;
726 v2
->SetBounds(3, 4, 6, 5);
727 v1
->AddChildView(v2
);
729 // Verify where the layers actually appear.
730 v1
->SetPaintToLayer(true);
731 // x: 25 - 10(x) - 12(width) = 3
732 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
733 v1
->SetPaintToLayer(false);
735 v2
->SetPaintToLayer(true);
736 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
737 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
738 v2
->SetPaintToLayer(false);
740 // Paint everything once, since it has to build its cache. Then we can test
742 gfx::Rect
first_paint(1, 1);
743 scoped_refptr
<cc::DisplayItemList
> list
=
744 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
745 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
749 gfx::Rect
paint_area(2, 10, 2, 3);
750 gfx::Rect
root_area(root_view
->size());
751 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
753 EXPECT_FALSE(v1
->did_paint_
);
754 EXPECT_FALSE(v2
->did_paint_
);
755 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
756 EXPECT_TRUE(v1
->did_paint_
);
757 EXPECT_FALSE(v2
->did_paint_
);
760 TEST_F(ViewTest
, PaintIntersectsNoChildren
) {
761 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
762 View
* root_view
= widget
->GetRootView();
764 TestView
* v1
= new TestView
;
765 v1
->SetBounds(10, 11, 12, 13);
766 root_view
->AddChildView(v1
);
768 TestView
* v2
= new TestView
;
769 v2
->SetBounds(3, 4, 6, 5);
770 v1
->AddChildView(v2
);
772 // Paint everything once, since it has to build its cache. Then we can test
774 gfx::Rect
first_paint(1, 1);
775 scoped_refptr
<cc::DisplayItemList
> list
=
776 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
777 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
781 gfx::Rect
paint_area(9, 10, 2, 1);
782 gfx::Rect
root_area(root_view
->size());
783 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
785 EXPECT_FALSE(v1
->did_paint_
);
786 EXPECT_FALSE(v2
->did_paint_
);
787 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
788 EXPECT_FALSE(v1
->did_paint_
);
789 EXPECT_FALSE(v2
->did_paint_
);
792 TEST_F(ViewTest
, PaintIntersectsNoChildrenInRTL
) {
794 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
795 View
* root_view
= widget
->GetRootView();
797 TestView
* v1
= new TestView
;
798 v1
->SetBounds(10, 11, 12, 13);
799 root_view
->AddChildView(v1
);
801 TestView
* v2
= new TestView
;
802 v2
->SetBounds(3, 4, 6, 5);
803 v1
->AddChildView(v2
);
805 // Verify where the layers actually appear.
806 v1
->SetPaintToLayer(true);
807 // x: 25 - 10(x) - 12(width) = 3
808 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
809 v1
->SetPaintToLayer(false);
811 v2
->SetPaintToLayer(true);
812 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
813 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2
->layer()->bounds());
814 v2
->SetPaintToLayer(false);
816 // Paint everything once, since it has to build its cache. Then we can test
818 gfx::Rect
first_paint(1, 1);
819 scoped_refptr
<cc::DisplayItemList
> list
=
820 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
821 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
825 gfx::Rect
paint_area(2, 10, 2, 1);
826 gfx::Rect
root_area(root_view
->size());
827 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
829 EXPECT_FALSE(v1
->did_paint_
);
830 EXPECT_FALSE(v2
->did_paint_
);
831 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
832 EXPECT_FALSE(v1
->did_paint_
);
833 EXPECT_FALSE(v2
->did_paint_
);
836 TEST_F(ViewTest
, PaintIntersectsOneChild
) {
837 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
838 View
* root_view
= widget
->GetRootView();
840 TestView
* v1
= new TestView
;
841 v1
->SetBounds(10, 11, 12, 13);
842 root_view
->AddChildView(v1
);
844 TestView
* v2
= new TestView
;
845 v2
->SetBounds(3, 4, 6, 5);
846 root_view
->AddChildView(v2
);
848 // Paint everything once, since it has to build its cache. Then we can test
850 gfx::Rect
first_paint(1, 1);
851 scoped_refptr
<cc::DisplayItemList
> list
=
852 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
853 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
857 // Intersects with the second child only.
858 gfx::Rect
paint_area(3, 3, 1, 2);
859 gfx::Rect
root_area(root_view
->size());
860 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
862 EXPECT_FALSE(v1
->did_paint_
);
863 EXPECT_FALSE(v2
->did_paint_
);
864 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
865 EXPECT_FALSE(v1
->did_paint_
);
866 EXPECT_TRUE(v2
->did_paint_
);
868 // Intersects with the first child only.
869 paint_area
= gfx::Rect(20, 10, 1, 2);
873 EXPECT_FALSE(v1
->did_paint_
);
874 EXPECT_FALSE(v2
->did_paint_
);
875 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
876 EXPECT_TRUE(v1
->did_paint_
);
877 EXPECT_FALSE(v2
->did_paint_
);
880 TEST_F(ViewTest
, PaintIntersectsOneChildInRTL
) {
882 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
883 View
* root_view
= widget
->GetRootView();
885 TestView
* v1
= new TestView
;
886 v1
->SetBounds(10, 11, 12, 13);
887 root_view
->AddChildView(v1
);
889 TestView
* v2
= new TestView
;
890 v2
->SetBounds(3, 4, 6, 5);
891 root_view
->AddChildView(v2
);
893 // Verify where the layers actually appear.
894 v1
->SetPaintToLayer(true);
895 // x: 25 - 10(x) - 12(width) = 3
896 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1
->layer()->bounds());
897 v1
->SetPaintToLayer(false);
899 v2
->SetPaintToLayer(true);
900 // x: 25 - 3(x) - 6(width) = 16
901 EXPECT_EQ(gfx::Rect(16, 4, 6, 5), v2
->layer()->bounds());
902 v2
->SetPaintToLayer(false);
904 // Paint everything once, since it has to build its cache. Then we can test
906 gfx::Rect
first_paint(1, 1);
907 scoped_refptr
<cc::DisplayItemList
> list
=
908 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
909 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
913 // Intersects with the first child only.
914 gfx::Rect
paint_area(3, 10, 1, 2);
915 gfx::Rect
root_area(root_view
->size());
916 list
= cc::DisplayItemList::Create(root_area
, cc::DisplayItemListSettings());
918 EXPECT_FALSE(v1
->did_paint_
);
919 EXPECT_FALSE(v2
->did_paint_
);
920 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
921 EXPECT_TRUE(v1
->did_paint_
);
922 EXPECT_FALSE(v2
->did_paint_
);
924 // Intersects with the second child only.
925 paint_area
= gfx::Rect(21, 3, 1, 2);
929 EXPECT_FALSE(v1
->did_paint_
);
930 EXPECT_FALSE(v2
->did_paint_
);
931 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, root_area
, paint_area
));
932 EXPECT_FALSE(v1
->did_paint_
);
933 EXPECT_TRUE(v2
->did_paint_
);
936 TEST_F(ViewTest
, PaintInPromotedToLayer
) {
937 ScopedTestPaintWidget
widget(CreateParams(Widget::InitParams::TYPE_POPUP
));
938 View
* root_view
= widget
->GetRootView();
940 TestView
* v1
= new TestView
;
941 v1
->SetPaintToLayer(true);
942 v1
->SetBounds(10, 11, 12, 13);
943 root_view
->AddChildView(v1
);
945 TestView
* v2
= new TestView
;
946 v2
->SetBounds(3, 4, 6, 5);
947 v1
->AddChildView(v2
);
949 // Paint everything once, since it has to build its cache. Then we can test
951 gfx::Rect
first_paint(1, 1);
952 scoped_refptr
<cc::DisplayItemList
> list
=
953 cc::DisplayItemList::Create(first_paint
, cc::DisplayItemListSettings());
954 v1
->Paint(ui::PaintContext(list
.get(), 1.f
, first_paint
, first_paint
));
959 gfx::Rect
paint_area(25, 26);
960 gfx::Rect
view_area(root_view
->size());
961 scoped_refptr
<cc::DisplayItemList
> list
=
962 cc::DisplayItemList::Create(view_area
, cc::DisplayItemListSettings());
964 // The promoted views are not painted as they are separate paint roots.
965 root_view
->Paint(ui::PaintContext(list
.get(), 1.f
, view_area
, paint_area
));
966 EXPECT_FALSE(v1
->did_paint_
);
967 EXPECT_FALSE(v2
->did_paint_
);
971 gfx::Rect
paint_area(1, 1);
972 gfx::Rect
view_area(v1
->size());
973 scoped_refptr
<cc::DisplayItemList
> list
=
974 cc::DisplayItemList::Create(view_area
, cc::DisplayItemListSettings());
976 // The |v1| view is painted. If it used its offset incorrect, it would think
977 // its at (10,11) instead of at (0,0) since it is the paint root.
978 v1
->Paint(ui::PaintContext(list
.get(), 1.f
, view_area
, paint_area
));
979 EXPECT_TRUE(v1
->did_paint_
);
980 EXPECT_FALSE(v2
->did_paint_
);
986 gfx::Rect
paint_area(3, 3, 1, 2);
987 gfx::Rect
view_area(v1
->size());
988 scoped_refptr
<cc::DisplayItemList
> list
=
989 cc::DisplayItemList::Create(view_area
, cc::DisplayItemListSettings());
991 // The |v2| view is painted also. If it used its offset incorrect, it would
992 // think its at (13,15) instead of at (3,4) since |v1| is the paint root.
993 v1
->Paint(ui::PaintContext(list
.get(), 1.f
, view_area
, paint_area
));
994 EXPECT_TRUE(v1
->did_paint_
);
995 EXPECT_TRUE(v2
->did_paint_
);
999 void TestView::SchedulePaintInRect(const gfx::Rect
& rect
) {
1000 scheduled_paint_rects_
.push_back(rect
);
1001 View::SchedulePaintInRect(rect
);
1004 TEST_F(ViewTest
, RemoveNotification
) {
1005 ViewStorage
* vs
= ViewStorage::GetInstance();
1006 Widget
* widget
= new Widget
;
1007 widget
->Init(CreateParams(Widget::InitParams::TYPE_POPUP
));
1008 View
* root_view
= widget
->GetRootView();
1010 View
* v1
= new View
;
1011 int s1
= vs
->CreateStorageID();
1012 vs
->StoreView(s1
, v1
);
1013 root_view
->AddChildView(v1
);
1014 View
* v11
= new View
;
1015 int s11
= vs
->CreateStorageID();
1016 vs
->StoreView(s11
, v11
);
1017 v1
->AddChildView(v11
);
1018 View
* v111
= new View
;
1019 int s111
= vs
->CreateStorageID();
1020 vs
->StoreView(s111
, v111
);
1021 v11
->AddChildView(v111
);
1022 View
* v112
= new View
;
1023 int s112
= vs
->CreateStorageID();
1024 vs
->StoreView(s112
, v112
);
1025 v11
->AddChildView(v112
);
1026 View
* v113
= new View
;
1027 int s113
= vs
->CreateStorageID();
1028 vs
->StoreView(s113
, v113
);
1029 v11
->AddChildView(v113
);
1030 View
* v1131
= new View
;
1031 int s1131
= vs
->CreateStorageID();
1032 vs
->StoreView(s1131
, v1131
);
1033 v113
->AddChildView(v1131
);
1034 View
* v12
= new View
;
1035 int s12
= vs
->CreateStorageID();
1036 vs
->StoreView(s12
, v12
);
1037 v1
->AddChildView(v12
);
1039 View
* v2
= new View
;
1040 int s2
= vs
->CreateStorageID();
1041 vs
->StoreView(s2
, v2
);
1042 root_view
->AddChildView(v2
);
1043 View
* v21
= new View
;
1044 int s21
= vs
->CreateStorageID();
1045 vs
->StoreView(s21
, v21
);
1046 v2
->AddChildView(v21
);
1047 View
* v211
= new View
;
1048 int s211
= vs
->CreateStorageID();
1049 vs
->StoreView(s211
, v211
);
1050 v21
->AddChildView(v211
);
1052 size_t stored_views
= vs
->view_count();
1054 // Try removing a leaf view.
1055 v21
->RemoveChildView(v211
);
1056 EXPECT_EQ(stored_views
- 1, vs
->view_count());
1057 EXPECT_EQ(NULL
, vs
->RetrieveView(s211
));
1058 delete v211
; // We won't use this one anymore.
1060 // Now try removing a view with a hierarchy of depth 1.
1061 v11
->RemoveChildView(v113
);
1062 EXPECT_EQ(stored_views
- 3, vs
->view_count());
1063 EXPECT_EQ(NULL
, vs
->RetrieveView(s113
));
1064 EXPECT_EQ(NULL
, vs
->RetrieveView(s1131
));
1065 delete v113
; // We won't use this one anymore.
1067 // Now remove even more.
1068 root_view
->RemoveChildView(v1
);
1069 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
1070 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
1071 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
1072 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
1073 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
1075 // Put v1 back for more tests.
1076 root_view
->AddChildView(v1
);
1077 vs
->StoreView(s1
, v1
);
1079 // Synchronously closing the window deletes the view hierarchy, which should
1080 // remove all its views from ViewStorage.
1082 EXPECT_EQ(stored_views
- 10, vs
->view_count());
1083 EXPECT_EQ(NULL
, vs
->RetrieveView(s1
));
1084 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
1085 EXPECT_EQ(NULL
, vs
->RetrieveView(s11
));
1086 EXPECT_EQ(NULL
, vs
->RetrieveView(s12
));
1087 EXPECT_EQ(NULL
, vs
->RetrieveView(s21
));
1088 EXPECT_EQ(NULL
, vs
->RetrieveView(s111
));
1089 EXPECT_EQ(NULL
, vs
->RetrieveView(s112
));
1094 void RotateCounterclockwise(gfx::Transform
* transform
) {
1095 transform
->matrix().set3x3(0, -1, 0,
1100 void RotateClockwise(gfx::Transform
* transform
) {
1101 transform
->matrix().set3x3( 0, 1, 0,
1108 // Tests the correctness of the rect-based targeting algorithm implemented in
1109 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
1110 // of rect-based targeting.
1111 TEST_F(ViewTest
, GetEventHandlerForRect
) {
1112 Widget
* widget
= new Widget
;
1113 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1114 widget
->Init(params
);
1115 View
* root_view
= widget
->GetRootView();
1116 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1118 // Have this hierarchy of views (the coordinates here are all in
1119 // the root view's coordinate space):
1120 // v1 (0, 0, 100, 100)
1121 // v2 (150, 0, 250, 100)
1122 // v3 (0, 200, 150, 100)
1123 // v31 (10, 210, 80, 80)
1124 // v32 (110, 210, 30, 80)
1125 // v4 (300, 200, 100, 100)
1126 // v41 (310, 210, 80, 80)
1127 // v411 (370, 275, 10, 5)
1128 // v5 (450, 197, 30, 36)
1129 // v51 (450, 200, 30, 30)
1131 // The coordinates used for SetBounds are in parent coordinates.
1133 TestView
* v1
= new TestView
;
1134 v1
->SetBounds(0, 0, 100, 100);
1135 root_view
->AddChildView(v1
);
1137 TestView
* v2
= new TestView
;
1138 v2
->SetBounds(150, 0, 250, 100);
1139 root_view
->AddChildView(v2
);
1141 TestView
* v3
= new TestView
;
1142 v3
->SetBounds(0, 200, 150, 100);
1143 root_view
->AddChildView(v3
);
1145 TestView
* v4
= new TestView
;
1146 v4
->SetBounds(300, 200, 100, 100);
1147 root_view
->AddChildView(v4
);
1149 TestView
* v31
= new TestView
;
1150 v31
->SetBounds(10, 10, 80, 80);
1151 v3
->AddChildView(v31
);
1153 TestView
* v32
= new TestView
;
1154 v32
->SetBounds(110, 10, 30, 80);
1155 v3
->AddChildView(v32
);
1157 TestView
* v41
= new TestView
;
1158 v41
->SetBounds(10, 10, 80, 80);
1159 v4
->AddChildView(v41
);
1161 TestView
* v411
= new TestView
;
1162 v411
->SetBounds(60, 65, 10, 5);
1163 v41
->AddChildView(v411
);
1165 TestView
* v5
= new TestView
;
1166 v5
->SetBounds(450, 197, 30, 36);
1167 root_view
->AddChildView(v5
);
1169 TestView
* v51
= new TestView
;
1170 v51
->SetBounds(0, 3, 30, 30);
1171 v5
->AddChildView(v51
);
1173 // |touch_rect| does not intersect any descendant view of |root_view|.
1174 gfx::Rect
touch_rect(105, 105, 30, 45);
1175 View
* result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1176 EXPECT_EQ(root_view
, result_view
);
1179 // Covers |v1| by at least 60%.
1180 touch_rect
.SetRect(15, 15, 100, 100);
1181 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1182 EXPECT_EQ(v1
, result_view
);
1185 // Intersects |v1| but does not cover it by at least 60%. The center
1186 // of |touch_rect| is within |v1|.
1187 touch_rect
.SetRect(50, 50, 5, 10);
1188 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1189 EXPECT_EQ(v1
, result_view
);
1192 // Intersects |v1| but does not cover it by at least 60%. The center
1193 // of |touch_rect| is not within |v1|.
1194 touch_rect
.SetRect(95, 96, 21, 22);
1195 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1196 EXPECT_EQ(root_view
, result_view
);
1199 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
1200 touch_rect
.SetRect(95, 10, 300, 120);
1201 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1202 EXPECT_EQ(v2
, result_view
);
1205 // Covers both |v1| and |v2| by at least 60%, but the center point
1206 // of |touch_rect| is closer to the center point of |v2|.
1207 touch_rect
.SetRect(20, 20, 400, 100);
1208 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1209 EXPECT_EQ(v2
, result_view
);
1212 // Covers both |v1| and |v2| by at least 60%, but the center point
1213 // of |touch_rect| is closer to the center point of |v1|.
1214 touch_rect
.SetRect(-700, -15, 1050, 110);
1215 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1216 EXPECT_EQ(v1
, result_view
);
1219 // A mouse click within |v1| will target |v1|.
1220 touch_rect
.SetRect(15, 15, 1, 1);
1221 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1222 EXPECT_EQ(v1
, result_view
);
1225 // Intersects |v3| and |v31| by at least 60% and the center point
1226 // of |touch_rect| is closer to the center point of |v31|.
1227 touch_rect
.SetRect(0, 200, 110, 100);
1228 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1229 EXPECT_EQ(v31
, result_view
);
1232 // Intersects |v3| and |v31|, but neither by at least 60%. The
1233 // center point of |touch_rect| lies within |v31|.
1234 touch_rect
.SetRect(80, 280, 15, 15);
1235 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1236 EXPECT_EQ(v31
, result_view
);
1239 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1240 // center point of |touch_rect| is closest to the center point
1242 touch_rect
.SetRect(0, 200, 200, 100);
1243 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1244 EXPECT_EQ(v32
, result_view
);
1247 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1248 // |v31| and |v32| by at least 60%. The center point of
1249 // |touch_rect| is closest to the center point of |v32|.
1250 touch_rect
.SetRect(30, 225, 180, 115);
1251 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1252 EXPECT_EQ(v32
, result_view
);
1255 // A mouse click at the corner of |v3| will target |v3|.
1256 touch_rect
.SetRect(0, 200, 1, 1);
1257 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1258 EXPECT_EQ(v3
, result_view
);
1261 // A mouse click within |v32| will target |v32|.
1262 touch_rect
.SetRect(112, 211, 1, 1);
1263 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1264 EXPECT_EQ(v32
, result_view
);
1267 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1268 // The center point of |touch_rect| is equally close to
1269 // the center points of |v4| and |v41|.
1270 touch_rect
.SetRect(310, 210, 80, 80);
1271 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1272 EXPECT_EQ(v41
, result_view
);
1275 // Intersects all of |v4|, |v41|, and |v411| but only covers
1276 // |v411| by at least 60%.
1277 touch_rect
.SetRect(370, 275, 7, 5);
1278 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1279 EXPECT_EQ(v411
, result_view
);
1282 // Intersects |v4| and |v41| but covers neither by at least 60%.
1283 // The center point of |touch_rect| is equally close to the center
1284 // points of |v4| and |v41|.
1285 touch_rect
.SetRect(345, 245, 7, 7);
1286 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1287 EXPECT_EQ(v41
, result_view
);
1290 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1291 // them by at least 60%. The center point of |touch_rect| lies
1293 touch_rect
.SetRect(368, 272, 4, 6);
1294 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1295 EXPECT_EQ(v411
, result_view
);
1298 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1299 // them by at least 60%. The center point of |touch_rect| lies
1301 touch_rect
.SetRect(365, 270, 7, 7);
1302 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1303 EXPECT_EQ(v41
, result_view
);
1306 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1307 // them by at least 60%. The center point of |touch_rect| lies
1309 touch_rect
.SetRect(205, 275, 200, 2);
1310 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1311 EXPECT_EQ(v4
, result_view
);
1314 // Intersects all of |v4|, |v41|, and |v411| but only covers
1315 // |v41| by at least 60%.
1316 touch_rect
.SetRect(310, 210, 61, 66);
1317 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1318 EXPECT_EQ(v41
, result_view
);
1321 // A mouse click within |v411| will target |v411|.
1322 touch_rect
.SetRect(372, 275, 1, 1);
1323 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1324 EXPECT_EQ(v411
, result_view
);
1327 // A mouse click within |v41| will target |v41|.
1328 touch_rect
.SetRect(350, 215, 1, 1);
1329 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1330 EXPECT_EQ(v41
, result_view
);
1333 // Covers |v3|, |v4|, and all of their descendants by at
1334 // least 60%. The center point of |touch_rect| is closest
1335 // to the center point of |v32|.
1336 touch_rect
.SetRect(0, 200, 400, 100);
1337 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1338 EXPECT_EQ(v32
, result_view
);
1341 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1342 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1343 // The center point of |touch_rect| is closest to the center
1344 // point of |root_view|.
1345 touch_rect
.SetRect(110, 15, 375, 450);
1346 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1347 EXPECT_EQ(root_view
, result_view
);
1350 // Covers all views (except |v5| and |v51|) by at least 60%. The
1351 // center point of |touch_rect| is equally close to the center
1352 // points of |v2| and |v32|. One is not a descendant of the other,
1353 // so in this case the view selected is arbitrary (i.e.,
1354 // it depends only on the ordering of nodes in the views
1356 touch_rect
.SetRect(0, 0, 400, 300);
1357 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1358 EXPECT_EQ(v32
, result_view
);
1361 // Covers |v5| and |v51| by at least 60%, and the center point of
1362 // the touch is located within both views. Since both views share
1363 // the same center point, the child view should be selected.
1364 touch_rect
.SetRect(440, 190, 40, 40);
1365 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1366 EXPECT_EQ(v51
, result_view
);
1369 // Covers |v5| and |v51| by at least 60%, but the center point of
1370 // the touch is not located within either view. Since both views
1371 // share the same center point, the child view should be selected.
1372 touch_rect
.SetRect(455, 187, 60, 60);
1373 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1374 EXPECT_EQ(v51
, result_view
);
1377 // Covers neither |v5| nor |v51| by at least 60%, but the center
1378 // of the touch is located within |v51|.
1379 touch_rect
.SetRect(450, 197, 10, 10);
1380 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1381 EXPECT_EQ(v51
, result_view
);
1384 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1385 // The center point is located outside of both views.
1386 touch_rect
.SetRect(433, 180, 24, 24);
1387 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1388 EXPECT_EQ(root_view
, result_view
);
1391 // Only intersects |v5| but does not cover it by at least 60%. The
1392 // center point of the touch region is located within |v5|.
1393 touch_rect
.SetRect(449, 196, 3, 3);
1394 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1395 EXPECT_EQ(v5
, result_view
);
1398 // A mouse click within |v5| (but not |v51|) should target |v5|.
1399 touch_rect
.SetRect(462, 199, 1, 1);
1400 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1401 EXPECT_EQ(v5
, result_view
);
1404 // A mouse click |v5| and |v51| should target the child view.
1405 touch_rect
.SetRect(452, 226, 1, 1);
1406 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1407 EXPECT_EQ(v51
, result_view
);
1410 // A mouse click on the center of |v5| and |v51| should target
1412 touch_rect
.SetRect(465, 215, 1, 1);
1413 result_view
= root_view
->GetEventHandlerForRect(touch_rect
);
1414 EXPECT_EQ(v51
, result_view
);
1420 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1421 // as expected when different views in the view hierarchy return false
1422 // when CanProcessEventsWithinSubtree() is called.
1423 TEST_F(ViewTest
, CanProcessEventsWithinSubtree
) {
1424 Widget
* widget
= new Widget
;
1425 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1426 widget
->Init(params
);
1427 View
* root_view
= widget
->GetRootView();
1428 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1430 // Have this hierarchy of views (the coords here are in the coordinate
1431 // space of the root view):
1432 // v (0, 0, 100, 100)
1433 // - v_child (0, 0, 20, 30)
1434 // - v_grandchild (5, 5, 5, 15)
1436 TestView
* v
= new TestView
;
1437 v
->SetBounds(0, 0, 100, 100);
1438 root_view
->AddChildView(v
);
1439 v
->set_notify_enter_exit_on_child(true);
1441 TestView
* v_child
= new TestView
;
1442 v_child
->SetBounds(0, 0, 20, 30);
1443 v
->AddChildView(v_child
);
1445 TestView
* v_grandchild
= new TestView
;
1446 v_grandchild
->SetBounds(5, 5, 5, 15);
1447 v_child
->AddChildView(v_grandchild
);
1451 v_grandchild
->Reset();
1453 // Define rects and points within the views in the hierarchy.
1454 gfx::Rect
rect_in_v_grandchild(7, 7, 3, 3);
1455 gfx::Point
point_in_v_grandchild(rect_in_v_grandchild
.origin());
1456 gfx::Rect
rect_in_v_child(12, 3, 5, 5);
1457 gfx::Point
point_in_v_child(rect_in_v_child
.origin());
1458 gfx::Rect
rect_in_v(50, 50, 25, 30);
1459 gfx::Point
point_in_v(rect_in_v
.origin());
1461 // When all three views return true when CanProcessEventsWithinSubtree()
1462 // is called, targeting should behave as expected.
1464 View
* result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1465 EXPECT_EQ(v_grandchild
, result_view
);
1467 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1468 EXPECT_EQ(v_grandchild
, result_view
);
1471 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1472 EXPECT_EQ(v_child
, result_view
);
1474 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1475 EXPECT_EQ(v_child
, result_view
);
1478 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1479 EXPECT_EQ(v
, result_view
);
1481 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1482 EXPECT_EQ(v
, result_view
);
1485 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1486 // is called, then |v_grandchild| cannot be returned as a target.
1488 v_grandchild
->set_can_process_events_within_subtree(false);
1490 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1491 EXPECT_EQ(v_child
, result_view
);
1493 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1494 EXPECT_EQ(v_child
, result_view
);
1497 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1498 EXPECT_EQ(v_child
, result_view
);
1500 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1501 EXPECT_EQ(v_child
, result_view
);
1504 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1505 EXPECT_EQ(v
, result_view
);
1507 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1508 EXPECT_EQ(v
, result_view
);
1510 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1511 // is called, then NULL should be returned as a target if we call
1512 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
1513 // views tree. Note that the location must be in the coordinate space
1514 // of the root view (|v_grandchild| in this case), so use (1, 1).
1516 result_view
= v_grandchild
;
1517 result_view
= v_grandchild
->GetTooltipHandlerForPoint(gfx::Point(1, 1));
1518 EXPECT_EQ(NULL
, result_view
);
1521 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1522 // is called, then neither |v_child| nor |v_grandchild| can be returned
1523 // as a target (|v| should be returned as the target for each case).
1525 v_grandchild
->Reset();
1526 v_child
->set_can_process_events_within_subtree(false);
1528 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1529 EXPECT_EQ(v
, result_view
);
1531 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1532 EXPECT_EQ(v
, result_view
);
1535 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1536 EXPECT_EQ(v
, result_view
);
1538 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1539 EXPECT_EQ(v
, result_view
);
1542 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1543 EXPECT_EQ(v
, result_view
);
1545 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1546 EXPECT_EQ(v
, result_view
);
1549 // When |v| returns false when CanProcessEventsWithinSubtree()
1550 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1551 // as a target (|root_view| should be returned as the target for each case).
1554 v
->set_can_process_events_within_subtree(false);
1556 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_grandchild
);
1557 EXPECT_EQ(root_view
, result_view
);
1559 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_grandchild
);
1560 EXPECT_EQ(root_view
, result_view
);
1563 result_view
= root_view
->GetEventHandlerForRect(rect_in_v_child
);
1564 EXPECT_EQ(root_view
, result_view
);
1566 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v_child
);
1567 EXPECT_EQ(root_view
, result_view
);
1570 result_view
= root_view
->GetEventHandlerForRect(rect_in_v
);
1571 EXPECT_EQ(root_view
, result_view
);
1573 result_view
= root_view
->GetTooltipHandlerForPoint(point_in_v
);
1574 EXPECT_EQ(root_view
, result_view
);
1579 TEST_F(ViewTest
, NotifyEnterExitOnChild
) {
1580 Widget
* widget
= new Widget
;
1581 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1582 widget
->Init(params
);
1583 View
* root_view
= widget
->GetRootView();
1584 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1586 // Have this hierarchy of views (the coords here are in root coord):
1587 // v1 (0, 0, 100, 100)
1588 // - v11 (0, 0, 20, 30)
1589 // - v111 (5, 5, 5, 15)
1590 // - v12 (50, 10, 30, 90)
1591 // - v121 (60, 20, 10, 10)
1592 // v2 (105, 0, 100, 100)
1593 // - v21 (120, 10, 50, 20)
1595 TestView
* v1
= new TestView
;
1596 v1
->SetBounds(0, 0, 100, 100);
1597 root_view
->AddChildView(v1
);
1598 v1
->set_notify_enter_exit_on_child(true);
1600 TestView
* v11
= new TestView
;
1601 v11
->SetBounds(0, 0, 20, 30);
1602 v1
->AddChildView(v11
);
1604 TestView
* v111
= new TestView
;
1605 v111
->SetBounds(5, 5, 5, 15);
1606 v11
->AddChildView(v111
);
1608 TestView
* v12
= new TestView
;
1609 v12
->SetBounds(50, 10, 30, 90);
1610 v1
->AddChildView(v12
);
1612 TestView
* v121
= new TestView
;
1613 v121
->SetBounds(10, 10, 10, 10);
1614 v12
->AddChildView(v121
);
1616 TestView
* v2
= new TestView
;
1617 v2
->SetBounds(105, 0, 100, 100);
1618 root_view
->AddChildView(v2
);
1620 TestView
* v21
= new TestView
;
1621 v21
->SetBounds(15, 10, 50, 20);
1622 v2
->AddChildView(v21
);
1632 // Move the mouse in v111.
1633 gfx::Point
p1(6, 6);
1634 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, p1
, p1
, ui::EventTimeForNow(), 0, 0);
1635 root_view
->OnMouseMoved(move1
);
1636 EXPECT_TRUE(v111
->received_mouse_enter_
);
1637 EXPECT_FALSE(v11
->last_mouse_event_type_
);
1638 EXPECT_TRUE(v1
->received_mouse_enter_
);
1643 // Now, move into v121.
1644 gfx::Point
p2(65, 21);
1645 ui::MouseEvent
move2(ui::ET_MOUSE_MOVED
, p2
, p2
, ui::EventTimeForNow(), 0, 0);
1646 root_view
->OnMouseMoved(move2
);
1647 EXPECT_TRUE(v111
->received_mouse_exit_
);
1648 EXPECT_TRUE(v121
->received_mouse_enter_
);
1649 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1654 // Now, move into v11.
1655 gfx::Point
p3(1, 1);
1656 ui::MouseEvent
move3(ui::ET_MOUSE_MOVED
, p3
, p3
, ui::EventTimeForNow(), 0, 0);
1657 root_view
->OnMouseMoved(move3
);
1658 EXPECT_TRUE(v121
->received_mouse_exit_
);
1659 EXPECT_TRUE(v11
->received_mouse_enter_
);
1660 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1666 gfx::Point
p4(121, 15);
1667 ui::MouseEvent
move4(ui::ET_MOUSE_MOVED
, p4
, p4
, ui::EventTimeForNow(), 0, 0);
1668 root_view
->OnMouseMoved(move4
);
1669 EXPECT_TRUE(v21
->received_mouse_enter_
);
1670 EXPECT_FALSE(v2
->last_mouse_event_type_
);
1671 EXPECT_TRUE(v11
->received_mouse_exit_
);
1672 EXPECT_TRUE(v1
->received_mouse_exit_
);
1679 gfx::Point
p5(21, 0);
1680 ui::MouseEvent
move5(ui::ET_MOUSE_MOVED
, p5
, p5
, ui::EventTimeForNow(), 0, 0);
1681 root_view
->OnMouseMoved(move5
);
1682 EXPECT_TRUE(v21
->received_mouse_exit_
);
1683 EXPECT_TRUE(v1
->received_mouse_enter_
);
1688 // Now, move into v11.
1689 gfx::Point
p6(15, 15);
1690 ui::MouseEvent
mouse6(ui::ET_MOUSE_MOVED
, p6
, p6
, ui::EventTimeForNow(), 0,
1692 root_view
->OnMouseMoved(mouse6
);
1693 EXPECT_TRUE(v11
->received_mouse_enter_
);
1694 EXPECT_FALSE(v1
->last_mouse_event_type_
);
1699 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1700 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1701 // when the mouse leaves v11.
1702 gfx::Point
p7(21, 0);
1703 ui::MouseEvent
mouse7(ui::ET_MOUSE_MOVED
, p7
, p7
, ui::EventTimeForNow(), 0,
1705 root_view
->OnMouseMoved(mouse7
);
1706 EXPECT_TRUE(v11
->received_mouse_exit_
);
1707 EXPECT_FALSE(v1
->received_mouse_enter_
);
1712 TEST_F(ViewTest
, Textfield
) {
1713 const base::string16 kText
= ASCIIToUTF16(
1714 "Reality is that which, when you stop believing it, doesn't go away.");
1715 const base::string16 kExtraText
= ASCIIToUTF16("Pretty deep, Philip!");
1717 Widget
* widget
= new Widget
;
1718 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1719 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1720 widget
->Init(params
);
1721 View
* root_view
= widget
->GetRootView();
1723 Textfield
* textfield
= new Textfield();
1724 root_view
->AddChildView(textfield
);
1726 // Test setting, appending text.
1727 textfield
->SetText(kText
);
1728 EXPECT_EQ(kText
, textfield
->text());
1729 textfield
->AppendText(kExtraText
);
1730 EXPECT_EQ(kText
+ kExtraText
, textfield
->text());
1731 textfield
->SetText(base::string16());
1732 EXPECT_TRUE(textfield
->text().empty());
1734 // Test selection related methods.
1735 textfield
->SetText(kText
);
1736 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1737 textfield
->SelectAll(false);
1738 EXPECT_EQ(kText
, textfield
->text());
1739 textfield
->ClearSelection();
1740 EXPECT_TRUE(textfield
->GetSelectedText().empty());
1745 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1746 TEST_F(ViewTest
, TextfieldCutCopyPaste
) {
1747 const base::string16 kNormalText
= ASCIIToUTF16("Normal");
1748 const base::string16 kReadOnlyText
= ASCIIToUTF16("Read only");
1749 const base::string16 kPasswordText
=
1750 ASCIIToUTF16("Password! ** Secret stuff **");
1752 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1754 Widget
* widget
= new Widget
;
1755 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1756 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1757 widget
->Init(params
);
1758 View
* root_view
= widget
->GetRootView();
1760 Textfield
* normal
= new Textfield();
1761 Textfield
* read_only
= new Textfield();
1762 read_only
->SetReadOnly(true);
1763 Textfield
* password
= new Textfield();
1764 password
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
1766 root_view
->AddChildView(normal
);
1767 root_view
->AddChildView(read_only
);
1768 root_view
->AddChildView(password
);
1770 normal
->SetText(kNormalText
);
1771 read_only
->SetText(kReadOnlyText
);
1772 password
->SetText(kPasswordText
);
1778 normal
->SelectAll(false);
1779 normal
->ExecuteCommand(IDS_APP_CUT
);
1780 base::string16 result
;
1781 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1782 EXPECT_EQ(kNormalText
, result
);
1783 normal
->SetText(kNormalText
); // Let's revert to the original content.
1785 read_only
->SelectAll(false);
1786 read_only
->ExecuteCommand(IDS_APP_CUT
);
1788 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1789 // Cut should have failed, so the clipboard content should not have changed.
1790 EXPECT_EQ(kNormalText
, result
);
1792 password
->SelectAll(false);
1793 password
->ExecuteCommand(IDS_APP_CUT
);
1795 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1796 // Cut should have failed, so the clipboard content should not have changed.
1797 EXPECT_EQ(kNormalText
, result
);
1803 // Start with |read_only| to observe a change in clipboard text.
1804 read_only
->SelectAll(false);
1805 read_only
->ExecuteCommand(IDS_APP_COPY
);
1807 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1808 EXPECT_EQ(kReadOnlyText
, result
);
1810 normal
->SelectAll(false);
1811 normal
->ExecuteCommand(IDS_APP_COPY
);
1813 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1814 EXPECT_EQ(kNormalText
, result
);
1816 password
->SelectAll(false);
1817 password
->ExecuteCommand(IDS_APP_COPY
);
1819 clipboard
->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
1820 // Text cannot be copied from an obscured field; the clipboard won't change.
1821 EXPECT_EQ(kNormalText
, result
);
1827 // Attempting to paste kNormalText in a read-only text-field should fail.
1828 read_only
->SelectAll(false);
1829 read_only
->ExecuteCommand(IDS_APP_PASTE
);
1830 EXPECT_EQ(kReadOnlyText
, read_only
->text());
1832 password
->SelectAll(false);
1833 password
->ExecuteCommand(IDS_APP_PASTE
);
1834 EXPECT_EQ(kNormalText
, password
->text());
1836 // Copy from |read_only| to observe a change in the normal textfield text.
1837 read_only
->SelectAll(false);
1838 read_only
->ExecuteCommand(IDS_APP_COPY
);
1839 normal
->SelectAll(false);
1840 normal
->ExecuteCommand(IDS_APP_PASTE
);
1841 EXPECT_EQ(kReadOnlyText
, normal
->text());
1845 ////////////////////////////////////////////////////////////////////////////////
1847 ////////////////////////////////////////////////////////////////////////////////
1848 bool TestView::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
1849 accelerator_count_map_
[accelerator
]++;
1853 // TODO: these tests were initially commented out when getting aura to
1854 // run. Figure out if still valuable and either nuke or fix.
1856 TEST_F(ViewTest
, ActivateAccelerator
) {
1857 // Register a keyboard accelerator before the view is added to a window.
1858 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1859 TestView
* view
= new TestView();
1861 view
->AddAccelerator(return_accelerator
);
1862 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1864 // Create a window and add the view as its child.
1865 scoped_ptr
<Widget
> widget(new Widget
);
1866 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1867 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1868 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1869 widget
->Init(params
);
1870 View
* root
= widget
->GetRootView();
1871 root
->AddChildView(view
);
1874 // Get the focus manager.
1875 FocusManager
* focus_manager
= widget
->GetFocusManager();
1876 ASSERT_TRUE(focus_manager
);
1878 // Hit the return key and see if it takes effect.
1879 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1880 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1882 // Hit the escape key. Nothing should happen.
1883 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
1884 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1885 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1886 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 0);
1888 // Now register the escape key and hit it again.
1889 view
->AddAccelerator(escape_accelerator
);
1890 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1891 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1892 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1894 // Remove the return key accelerator.
1895 view
->RemoveAccelerator(return_accelerator
);
1896 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1897 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 1);
1898 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1900 // Add it again. Hit the return key and the escape key.
1901 view
->AddAccelerator(return_accelerator
);
1902 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1903 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1904 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 1);
1905 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1906 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1907 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1909 // Remove all the accelerators.
1910 view
->ResetAccelerators();
1911 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1912 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1913 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1914 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
1915 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 2);
1916 EXPECT_EQ(view
->accelerator_count_map_
[escape_accelerator
], 2);
1921 TEST_F(ViewTest
, HiddenViewWithAccelerator
) {
1922 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1923 TestView
* view
= new TestView();
1925 view
->AddAccelerator(return_accelerator
);
1926 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1928 scoped_ptr
<Widget
> widget(new Widget
);
1929 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1930 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1931 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1932 widget
->Init(params
);
1933 View
* root
= widget
->GetRootView();
1934 root
->AddChildView(view
);
1937 FocusManager
* focus_manager
= widget
->GetFocusManager();
1938 ASSERT_TRUE(focus_manager
);
1940 view
->SetVisible(false);
1941 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1943 view
->SetVisible(true);
1944 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1949 TEST_F(ViewTest
, ViewInHiddenWidgetWithAccelerator
) {
1950 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
1951 TestView
* view
= new TestView();
1953 view
->AddAccelerator(return_accelerator
);
1954 EXPECT_EQ(view
->accelerator_count_map_
[return_accelerator
], 0);
1956 scoped_ptr
<Widget
> widget(new Widget
);
1957 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
1958 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
1959 params
.bounds
= gfx::Rect(0, 0, 100, 100);
1960 widget
->Init(params
);
1961 View
* root
= widget
->GetRootView();
1962 root
->AddChildView(view
);
1964 FocusManager
* focus_manager
= widget
->GetFocusManager();
1965 ASSERT_TRUE(focus_manager
);
1967 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1968 EXPECT_EQ(0, view
->accelerator_count_map_
[return_accelerator
]);
1971 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
1972 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1975 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
1976 EXPECT_EQ(1, view
->accelerator_count_map_
[return_accelerator
]);
1981 ////////////////////////////////////////////////////////////////////////////////
1982 // Mouse-wheel message rerouting
1983 ////////////////////////////////////////////////////////////////////////////////
1984 class ScrollableTestView
: public View
{
1986 ScrollableTestView() { }
1988 virtual gfx::Size
GetPreferredSize() {
1989 return gfx::Size(100, 10000);
1992 virtual void Layout() {
1993 SizeToPreferredSize();
1997 class TestViewWithControls
: public View
{
1999 TestViewWithControls() {
2000 text_field_
= new Textfield();
2001 AddChildView(text_field_
);
2004 Textfield
* text_field_
;
2007 class SimpleWidgetDelegate
: public WidgetDelegate
{
2009 explicit SimpleWidgetDelegate(View
* contents
) : contents_(contents
) { }
2011 virtual void DeleteDelegate() { delete this; }
2013 virtual View
* GetContentsView() { return contents_
; }
2015 virtual Widget
* GetWidget() { return contents_
->GetWidget(); }
2016 virtual const Widget
* GetWidget() const { return contents_
->GetWidget(); }
2022 // Tests that the mouse-wheel messages are correctly rerouted to the window
2024 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
2026 // Note that this fails for a variety of reasons:
2027 // - focused view is apparently reset across window activations and never
2028 // properly restored
2029 // - this test depends on you not having any other window visible open under the
2030 // area that it opens the test windows. --beng
2031 TEST_F(ViewTest
, DISABLED_RerouteMouseWheelTest
) {
2032 TestViewWithControls
* view_with_controls
= new TestViewWithControls();
2033 Widget
* window1
= Widget::CreateWindowWithBounds(
2034 new SimpleWidgetDelegate(view_with_controls
),
2035 gfx::Rect(0, 0, 100, 100));
2037 ScrollView
* scroll_view
= new ScrollView();
2038 scroll_view
->SetContents(new ScrollableTestView());
2039 Widget
* window2
= Widget::CreateWindowWithBounds(
2040 new SimpleWidgetDelegate(scroll_view
),
2041 gfx::Rect(200, 200, 100, 100));
2043 EXPECT_EQ(0, scroll_view
->GetVisibleRect().y());
2045 // Make the window1 active, as this is what it would be in real-world.
2046 window1
->Activate();
2048 // Let's send a mouse-wheel message to the different controls and check that
2049 // it is rerouted to the window under the mouse (effectively scrolling the
2052 // First to the Window's HWND.
2053 ::SendMessage(view_with_controls
->GetWidget()->GetNativeView(),
2054 WM_MOUSEWHEEL
, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
2055 EXPECT_EQ(20, scroll_view
->GetVisibleRect().y());
2057 window1
->CloseNow();
2058 window2
->CloseNow();
2062 ////////////////////////////////////////////////////////////////////////////////
2063 // Native view hierachy
2064 ////////////////////////////////////////////////////////////////////////////////
2065 class ToplevelWidgetObserverView
: public View
{
2067 ToplevelWidgetObserverView() : toplevel_(NULL
) {
2069 ~ToplevelWidgetObserverView() override
{}
2072 void ViewHierarchyChanged(
2073 const ViewHierarchyChangedDetails
& details
) override
{
2074 if (details
.is_add
) {
2075 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
2080 void NativeViewHierarchyChanged() override
{
2081 toplevel_
= GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL
;
2084 Widget
* toplevel() { return toplevel_
; }
2089 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView
);
2092 // Test that a view can track the current top level widget by overriding
2093 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
2094 TEST_F(ViewTest
, NativeViewHierarchyChanged
) {
2095 scoped_ptr
<Widget
> toplevel1(new Widget
);
2096 Widget::InitParams toplevel1_params
=
2097 CreateParams(Widget::InitParams::TYPE_POPUP
);
2098 toplevel1_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2099 toplevel1
->Init(toplevel1_params
);
2101 scoped_ptr
<Widget
> toplevel2(new Widget
);
2102 Widget::InitParams toplevel2_params
=
2103 CreateParams(Widget::InitParams::TYPE_POPUP
);
2104 toplevel2_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2105 toplevel2
->Init(toplevel2_params
);
2107 Widget
* child
= new Widget
;
2108 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
2109 child_params
.parent
= toplevel1
->GetNativeView();
2110 child
->Init(child_params
);
2112 ToplevelWidgetObserverView
* observer_view
=
2113 new ToplevelWidgetObserverView();
2114 EXPECT_EQ(NULL
, observer_view
->toplevel());
2116 child
->SetContentsView(observer_view
);
2117 EXPECT_EQ(toplevel1
, observer_view
->toplevel());
2119 Widget::ReparentNativeView(child
->GetNativeView(),
2120 toplevel2
->GetNativeView());
2121 EXPECT_EQ(toplevel2
, observer_view
->toplevel());
2123 observer_view
->parent()->RemoveChildView(observer_view
);
2124 EXPECT_EQ(NULL
, observer_view
->toplevel());
2126 // Make |observer_view| |child|'s contents view again so that it gets deleted
2128 child
->SetContentsView(observer_view
);
2131 ////////////////////////////////////////////////////////////////////////////////
2133 ////////////////////////////////////////////////////////////////////////////////
2135 class TransformPaintView
: public TestView
{
2137 TransformPaintView() {}
2138 ~TransformPaintView() override
{}
2140 void ClearScheduledPaintRect() {
2141 scheduled_paint_rect_
= gfx::Rect();
2144 gfx::Rect
scheduled_paint_rect() const { return scheduled_paint_rect_
; }
2146 // Overridden from View:
2147 void SchedulePaintInRect(const gfx::Rect
& rect
) override
{
2148 gfx::Rect xrect
= ConvertRectToParent(rect
);
2149 scheduled_paint_rect_
.Union(xrect
);
2153 gfx::Rect scheduled_paint_rect_
;
2155 DISALLOW_COPY_AND_ASSIGN(TransformPaintView
);
2158 TEST_F(ViewTest
, TransformPaint
) {
2159 TransformPaintView
* v1
= new TransformPaintView();
2160 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2162 TestView
* v2
= new TestView();
2163 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2165 Widget
* widget
= new Widget
;
2166 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2167 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2168 widget
->Init(params
);
2170 View
* root
= widget
->GetRootView();
2172 root
->AddChildView(v1
);
2173 v1
->AddChildView(v2
);
2175 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2176 v1
->ClearScheduledPaintRect();
2177 v2
->SchedulePaint();
2179 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1
->scheduled_paint_rect());
2181 // Rotate |v1| counter-clockwise.
2182 gfx::Transform transform
;
2183 RotateCounterclockwise(&transform
);
2184 transform
.matrix().set(1, 3, 500.0);
2185 v1
->SetTransform(transform
);
2187 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2189 v1
->ClearScheduledPaintRect();
2190 v2
->SchedulePaint();
2192 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1
->scheduled_paint_rect());
2197 TEST_F(ViewTest
, TransformEvent
) {
2198 TestView
* v1
= new TestView();
2199 v1
->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2201 TestView
* v2
= new TestView();
2202 v2
->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2204 Widget
* widget
= new Widget
;
2205 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2206 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2207 widget
->Init(params
);
2208 View
* root
= widget
->GetRootView();
2210 root
->AddChildView(v1
);
2211 v1
->AddChildView(v2
);
2213 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2215 // Rotate |v1| counter-clockwise.
2216 gfx::Transform
transform(v1
->GetTransform());
2217 RotateCounterclockwise(&transform
);
2218 transform
.matrix().set(1, 3, 500.0);
2219 v1
->SetTransform(transform
);
2221 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2225 gfx::Point
p1(110, 210);
2226 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, p1
, p1
, ui::EventTimeForNow(),
2227 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2228 root
->OnMousePressed(pressed
);
2229 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2230 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2231 EXPECT_EQ(190, v2
->location_
.x());
2232 EXPECT_EQ(10, v2
->location_
.y());
2234 ui::MouseEvent
released(ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(),
2235 ui::EventTimeForNow(), 0, 0);
2236 root
->OnMouseReleased(released
);
2238 // Now rotate |v2| inside |v1| clockwise.
2239 transform
= v2
->GetTransform();
2240 RotateClockwise(&transform
);
2241 transform
.matrix().set(0, 3, 100.f
);
2242 v2
->SetTransform(transform
);
2244 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
2245 // (300, 400) in |root|.
2250 gfx::Point
point2(110, 320);
2251 ui::MouseEvent
p2(ui::ET_MOUSE_PRESSED
, point2
, point2
, ui::EventTimeForNow(),
2252 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2253 root
->OnMousePressed(p2
);
2254 EXPECT_EQ(0, v1
->last_mouse_event_type_
);
2255 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v2
->last_mouse_event_type_
);
2256 EXPECT_EQ(10, v2
->location_
.x());
2257 EXPECT_EQ(20, v2
->location_
.y());
2259 root
->OnMouseReleased(released
);
2261 v1
->SetTransform(gfx::Transform());
2262 v2
->SetTransform(gfx::Transform());
2264 TestView
* v3
= new TestView();
2265 v3
->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
2266 v2
->AddChildView(v3
);
2268 // Rotate |v3| clockwise with respect to |v2|.
2269 transform
= v1
->GetTransform();
2270 RotateClockwise(&transform
);
2271 transform
.matrix().set(0, 3, 30.f
);
2272 v3
->SetTransform(transform
);
2274 // Scale |v2| with respect to |v1| along both axis.
2275 transform
= v2
->GetTransform();
2276 transform
.matrix().set(0, 0, 0.8f
);
2277 transform
.matrix().set(1, 1, 0.5f
);
2278 v2
->SetTransform(transform
);
2280 // |v3| occupies (108, 105) to (132, 115) in |root|.
2286 gfx::Point
point(112, 110);
2287 ui::MouseEvent
p3(ui::ET_MOUSE_PRESSED
, point
, point
, ui::EventTimeForNow(),
2288 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2289 root
->OnMousePressed(p3
);
2291 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2292 EXPECT_EQ(10, v3
->location_
.x());
2293 EXPECT_EQ(25, v3
->location_
.y());
2295 root
->OnMouseReleased(released
);
2297 v1
->SetTransform(gfx::Transform());
2298 v2
->SetTransform(gfx::Transform());
2299 v3
->SetTransform(gfx::Transform());
2305 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
2306 transform
= v3
->GetTransform();
2307 RotateClockwise(&transform
);
2308 transform
.matrix().set(0, 3, 30.f
);
2309 // Rotation sets some scaling transformation. Using SetScale would overwrite
2310 // that and pollute the rotation. So combine the scaling with the existing
2312 gfx::Transform scale
;
2313 scale
.Scale(0.8f
, 0.5f
);
2314 transform
.ConcatTransform(scale
);
2315 v3
->SetTransform(transform
);
2317 // Translate |v2| with respect to |v1|.
2318 transform
= v2
->GetTransform();
2319 transform
.matrix().set(0, 3, 10.f
);
2320 transform
.matrix().set(1, 3, 10.f
);
2321 v2
->SetTransform(transform
);
2323 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2325 gfx::Point
point3(124, 125);
2326 ui::MouseEvent
p4(ui::ET_MOUSE_PRESSED
, point3
, point3
, ui::EventTimeForNow(),
2327 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
2328 root
->OnMousePressed(p4
);
2330 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, v3
->last_mouse_event_type_
);
2331 EXPECT_EQ(10, v3
->location_
.x());
2332 EXPECT_EQ(25, v3
->location_
.y());
2334 root
->OnMouseReleased(released
);
2339 TEST_F(ViewTest
, TransformVisibleBound
) {
2340 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2342 scoped_ptr
<Widget
> widget(new Widget
);
2343 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2344 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2345 params
.bounds
= viewport_bounds
;
2346 widget
->Init(params
);
2347 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2349 View
* viewport
= new View
;
2350 widget
->SetContentsView(viewport
);
2351 View
* contents
= new View
;
2352 viewport
->AddChildView(contents
);
2353 viewport
->SetBoundsRect(viewport_bounds
);
2354 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2356 View
* child
= new View
;
2357 contents
->AddChildView(child
);
2358 child
->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2359 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child
->GetVisibleBounds());
2361 // Rotate |child| counter-clockwise
2362 gfx::Transform transform
;
2363 RotateCounterclockwise(&transform
);
2364 transform
.matrix().set(1, 3, 50.f
);
2365 child
->SetTransform(transform
);
2366 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child
->GetVisibleBounds());
2371 ////////////////////////////////////////////////////////////////////////////////
2372 // OnVisibleBoundsChanged()
2374 class VisibleBoundsView
: public View
{
2376 VisibleBoundsView() : received_notification_(false) {}
2377 ~VisibleBoundsView() override
{}
2379 bool received_notification() const { return received_notification_
; }
2380 void set_received_notification(bool received
) {
2381 received_notification_
= received
;
2385 // Overridden from View:
2386 bool GetNeedsNotificationWhenVisibleBoundsChange() const override
{
2389 void OnVisibleBoundsChanged() override
{ received_notification_
= true; }
2391 bool received_notification_
;
2393 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView
);
2396 TEST_F(ViewTest
, OnVisibleBoundsChanged
) {
2397 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2399 scoped_ptr
<Widget
> widget(new Widget
);
2400 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2401 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2402 params
.bounds
= viewport_bounds
;
2403 widget
->Init(params
);
2404 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2406 View
* viewport
= new View
;
2407 widget
->SetContentsView(viewport
);
2408 View
* contents
= new View
;
2409 viewport
->AddChildView(contents
);
2410 viewport
->SetBoundsRect(viewport_bounds
);
2411 contents
->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2413 // Create a view that cares about visible bounds notifications, and position
2414 // it just outside the visible bounds of the viewport.
2415 VisibleBoundsView
* child
= new VisibleBoundsView
;
2416 contents
->AddChildView(child
);
2417 child
->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2419 // The child bound should be fully clipped.
2420 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2422 // Now scroll the contents, but not enough to make the child visible.
2423 contents
->SetY(contents
->y() - 1);
2425 // We should have received the notification since the visible bounds may have
2426 // changed (even though they didn't).
2427 EXPECT_TRUE(child
->received_notification());
2428 EXPECT_TRUE(child
->GetVisibleBounds().IsEmpty());
2429 child
->set_received_notification(false);
2431 // Now scroll the contents, this time by enough to make the child visible by
2433 contents
->SetY(contents
->y() - 10);
2434 EXPECT_TRUE(child
->received_notification());
2435 EXPECT_EQ(1, child
->GetVisibleBounds().height());
2436 child
->set_received_notification(false);
2441 TEST_F(ViewTest
, SetBoundsPaint
) {
2443 TestView
* child_view
= new TestView
;
2445 top_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2446 top_view
.scheduled_paint_rects_
.clear();
2447 child_view
->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2448 top_view
.AddChildView(child_view
);
2450 top_view
.scheduled_paint_rects_
.clear();
2451 child_view
->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2452 EXPECT_EQ(2U, top_view
.scheduled_paint_rects_
.size());
2454 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2455 gfx::Rect paint_rect
= top_view
.scheduled_paint_rects_
[0];
2456 paint_rect
.Union(top_view
.scheduled_paint_rects_
[1]);
2457 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect
);
2460 // Assertions around painting and focus gain/lost.
2461 TEST_F(ViewTest
, FocusBlurPaints
) {
2462 TestView parent_view
;
2463 TestView
* child_view1
= new TestView
; // Owned by |parent_view|.
2465 parent_view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2467 child_view1
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2468 parent_view
.AddChildView(child_view1
);
2470 parent_view
.scheduled_paint_rects_
.clear();
2471 child_view1
->scheduled_paint_rects_
.clear();
2473 // Focus change shouldn't trigger paints.
2474 child_view1
->DoFocus();
2476 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2477 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2479 child_view1
->DoBlur();
2480 EXPECT_TRUE(parent_view
.scheduled_paint_rects_
.empty());
2481 EXPECT_TRUE(child_view1
->scheduled_paint_rects_
.empty());
2484 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2485 TEST_F(ViewTest
, SetBoundsSameBoundsDoesntSchedulePaint
) {
2488 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2489 view
.InvalidateLayout();
2490 view
.scheduled_paint_rects_
.clear();
2491 view
.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2492 EXPECT_TRUE(view
.scheduled_paint_rects_
.empty());
2495 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2496 TEST_F(ViewTest
, AddAndRemoveSchedulePaints
) {
2497 gfx::Rect
viewport_bounds(0, 0, 100, 100);
2499 // We have to put the View hierarchy into a Widget or no paints will be
2501 scoped_ptr
<Widget
> widget(new Widget
);
2502 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2503 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2504 params
.bounds
= viewport_bounds
;
2505 widget
->Init(params
);
2506 widget
->GetRootView()->SetBoundsRect(viewport_bounds
);
2508 TestView
* parent_view
= new TestView
;
2509 widget
->SetContentsView(parent_view
);
2510 parent_view
->SetBoundsRect(viewport_bounds
);
2511 parent_view
->scheduled_paint_rects_
.clear();
2513 View
* child_view
= new View
;
2514 child_view
->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2515 parent_view
->AddChildView(child_view
);
2516 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2517 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2519 parent_view
->scheduled_paint_rects_
.clear();
2520 parent_view
->RemoveChildView(child_view
);
2521 scoped_ptr
<View
> child_deleter(child_view
);
2522 ASSERT_EQ(1U, parent_view
->scheduled_paint_rects_
.size());
2523 EXPECT_EQ(child_view
->bounds(), parent_view
->scheduled_paint_rects_
.front());
2528 // Tests conversion methods with a transform.
2529 TEST_F(ViewTest
, ConversionsWithTransform
) {
2532 // View hierarchy used to test scale transforms.
2533 TestView
* child
= new TestView
;
2534 TestView
* child_child
= new TestView
;
2536 // View used to test a rotation transform.
2537 TestView
* child_2
= new TestView
;
2539 top_view
.AddChildView(child
);
2540 child
->AddChildView(child_child
);
2542 top_view
.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2544 child
->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2545 gfx::Transform transform
;
2546 transform
.Scale(3.0, 4.0);
2547 child
->SetTransform(transform
);
2549 child_child
->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2550 transform
.MakeIdentity();
2551 transform
.Scale(5.0, 7.0);
2552 child_child
->SetTransform(transform
);
2554 top_view
.AddChildView(child_2
);
2555 child_2
->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2556 transform
.MakeIdentity();
2557 RotateClockwise(&transform
);
2558 child_2
->SetTransform(transform
);
2560 // Sanity check to make sure basic transforms act as expected.
2562 gfx::Transform transform
;
2563 transform
.Translate(110.0, -110.0);
2564 transform
.Scale(100.0, 55.0);
2565 transform
.Translate(1.0, 1.0);
2567 // convert to a 3x3 matrix.
2568 const SkMatrix
& matrix
= transform
.matrix();
2570 EXPECT_EQ(210, matrix
.getTranslateX());
2571 EXPECT_EQ(-55, matrix
.getTranslateY());
2572 EXPECT_EQ(100, matrix
.getScaleX());
2573 EXPECT_EQ(55, matrix
.getScaleY());
2574 EXPECT_EQ(0, matrix
.getSkewX());
2575 EXPECT_EQ(0, matrix
.getSkewY());
2579 gfx::Transform transform
;
2580 transform
.Translate(1.0, 1.0);
2582 t2
.Scale(100.0, 55.0);
2584 t3
.Translate(110.0, -110.0);
2585 transform
.ConcatTransform(t2
);
2586 transform
.ConcatTransform(t3
);
2588 // convert to a 3x3 matrix
2589 const SkMatrix
& matrix
= transform
.matrix();
2591 EXPECT_EQ(210, matrix
.getTranslateX());
2592 EXPECT_EQ(-55, matrix
.getTranslateY());
2593 EXPECT_EQ(100, matrix
.getScaleX());
2594 EXPECT_EQ(55, matrix
.getScaleY());
2595 EXPECT_EQ(0, matrix
.getSkewX());
2596 EXPECT_EQ(0, matrix
.getSkewY());
2599 // Conversions from child->top and top->child.
2601 gfx::Point
point(5, 5);
2602 View::ConvertPointToTarget(child
, &top_view
, &point
);
2603 EXPECT_EQ(22, point
.x());
2604 EXPECT_EQ(39, point
.y());
2606 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2607 View::ConvertRectToTarget(child
, &top_view
, &rect
);
2608 EXPECT_FLOAT_EQ(22.0f
, rect
.x());
2609 EXPECT_FLOAT_EQ(39.0f
, rect
.y());
2610 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2611 EXPECT_FLOAT_EQ(80.0f
, rect
.height());
2613 point
.SetPoint(22, 39);
2614 View::ConvertPointToTarget(&top_view
, child
, &point
);
2615 EXPECT_EQ(5, point
.x());
2616 EXPECT_EQ(5, point
.y());
2618 rect
.SetRect(22.0f
, 39.0f
, 30.0f
, 80.0f
);
2619 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2620 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2621 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2622 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2623 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2626 // Conversions from child_child->top and top->child_child.
2628 gfx::Point
point(5, 5);
2629 View::ConvertPointToTarget(child_child
, &top_view
, &point
);
2630 EXPECT_EQ(133, point
.x());
2631 EXPECT_EQ(211, point
.y());
2633 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2634 View::ConvertRectToTarget(child_child
, &top_view
, &rect
);
2635 EXPECT_FLOAT_EQ(133.0f
, rect
.x());
2636 EXPECT_FLOAT_EQ(211.0f
, rect
.y());
2637 EXPECT_FLOAT_EQ(150.0f
, rect
.width());
2638 EXPECT_FLOAT_EQ(560.0f
, rect
.height());
2640 point
.SetPoint(133, 211);
2641 View::ConvertPointToTarget(&top_view
, child_child
, &point
);
2642 EXPECT_EQ(5, point
.x());
2643 EXPECT_EQ(5, point
.y());
2645 rect
.SetRect(133.0f
, 211.0f
, 150.0f
, 560.0f
);
2646 View::ConvertRectToTarget(&top_view
, child_child
, &rect
);
2647 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2648 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2649 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2650 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2653 // Conversions from child_child->child and child->child_child
2655 gfx::Point
point(5, 5);
2656 View::ConvertPointToTarget(child_child
, child
, &point
);
2657 EXPECT_EQ(42, point
.x());
2658 EXPECT_EQ(48, point
.y());
2660 gfx::RectF
rect(5.0f
, 5.0f
, 10.0f
, 20.0f
);
2661 View::ConvertRectToTarget(child_child
, child
, &rect
);
2662 EXPECT_FLOAT_EQ(42.0f
, rect
.x());
2663 EXPECT_FLOAT_EQ(48.0f
, rect
.y());
2664 EXPECT_FLOAT_EQ(50.0f
, rect
.width());
2665 EXPECT_FLOAT_EQ(140.0f
, rect
.height());
2667 point
.SetPoint(42, 48);
2668 View::ConvertPointToTarget(child
, child_child
, &point
);
2669 EXPECT_EQ(5, point
.x());
2670 EXPECT_EQ(5, point
.y());
2672 rect
.SetRect(42.0f
, 48.0f
, 50.0f
, 140.0f
);
2673 View::ConvertRectToTarget(child
, child_child
, &rect
);
2674 EXPECT_FLOAT_EQ(5.0f
, rect
.x());
2675 EXPECT_FLOAT_EQ(5.0f
, rect
.y());
2676 EXPECT_FLOAT_EQ(10.0f
, rect
.width());
2677 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2680 // Conversions from top_view to child with a value that should be negative.
2681 // This ensures we don't round up with negative numbers.
2683 gfx::Point
point(6, 18);
2684 View::ConvertPointToTarget(&top_view
, child
, &point
);
2685 EXPECT_EQ(-1, point
.x());
2686 EXPECT_EQ(-1, point
.y());
2688 float error
= 0.01f
;
2689 gfx::RectF
rect(6.0f
, 18.0f
, 10.0f
, 39.0f
);
2690 View::ConvertRectToTarget(&top_view
, child
, &rect
);
2691 EXPECT_NEAR(-0.33f
, rect
.x(), error
);
2692 EXPECT_NEAR(-0.25f
, rect
.y(), error
);
2693 EXPECT_NEAR(3.33f
, rect
.width(), error
);
2694 EXPECT_NEAR(9.75f
, rect
.height(), error
);
2697 // Rect conversions from top_view->child_2 and child_2->top_view.
2699 gfx::RectF
rect(50.0f
, 55.0f
, 20.0f
, 30.0f
);
2700 View::ConvertRectToTarget(child_2
, &top_view
, &rect
);
2701 EXPECT_FLOAT_EQ(615.0f
, rect
.x());
2702 EXPECT_FLOAT_EQ(775.0f
, rect
.y());
2703 EXPECT_FLOAT_EQ(30.0f
, rect
.width());
2704 EXPECT_FLOAT_EQ(20.0f
, rect
.height());
2706 rect
.SetRect(615.0f
, 775.0f
, 30.0f
, 20.0f
);
2707 View::ConvertRectToTarget(&top_view
, child_2
, &rect
);
2708 EXPECT_FLOAT_EQ(50.0f
, rect
.x());
2709 EXPECT_FLOAT_EQ(55.0f
, rect
.y());
2710 EXPECT_FLOAT_EQ(20.0f
, rect
.width());
2711 EXPECT_FLOAT_EQ(30.0f
, rect
.height());
2715 // Tests conversion methods to and from screen coordinates.
2716 TEST_F(ViewTest
, ConversionsToFromScreen
) {
2717 scoped_ptr
<Widget
> widget(new Widget
);
2718 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2719 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2720 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2721 widget
->Init(params
);
2723 View
* child
= new View
;
2724 widget
->GetRootView()->AddChildView(child
);
2725 child
->SetBounds(10, 10, 100, 200);
2728 child
->SetTransform(t
);
2730 gfx::Point
point_in_screen(100, 90);
2731 gfx::Point
point_in_child(80, 60);
2733 gfx::Point point
= point_in_screen
;
2734 View::ConvertPointFromScreen(child
, &point
);
2735 EXPECT_EQ(point_in_child
.ToString(), point
.ToString());
2737 View::ConvertPointToScreen(child
, &point
);
2738 EXPECT_EQ(point_in_screen
.ToString(), point
.ToString());
2741 // Tests conversion methods for rectangles.
2742 TEST_F(ViewTest
, ConvertRectWithTransform
) {
2743 scoped_ptr
<Widget
> widget(new Widget
);
2744 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
2745 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
2746 params
.bounds
= gfx::Rect(50, 50, 650, 650);
2747 widget
->Init(params
);
2748 View
* root
= widget
->GetRootView();
2750 TestView
* v1
= new TestView
;
2751 TestView
* v2
= new TestView
;
2752 root
->AddChildView(v1
);
2753 v1
->AddChildView(v2
);
2755 v1
->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2756 v2
->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2758 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2759 gfx::Rect
rect(5, 5, 15, 40);
2760 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2
->ConvertRectToParent(rect
));
2761 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2
->ConvertRectToWidget(rect
));
2765 RotateCounterclockwise(&t2
);
2766 t2
.matrix().set(1, 3, 100.f
);
2767 v2
->SetTransform(t2
);
2769 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2770 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2771 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2
->ConvertRectToWidget(rect
));
2776 v1
->SetTransform(t1
);
2778 // The rectangle should remain the same for |v1|.
2779 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2
->ConvertRectToParent(rect
));
2781 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2782 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2783 v2
->ConvertRectToWidget(rect
).ToString());
2788 class ObserverView
: public View
{
2791 ~ObserverView() override
;
2793 void ResetTestState();
2795 bool has_add_details() const { return has_add_details_
; }
2796 bool has_remove_details() const { return has_remove_details_
; }
2798 const ViewHierarchyChangedDetails
& add_details() const {
2799 return add_details_
;
2802 const ViewHierarchyChangedDetails
& remove_details() const {
2803 return remove_details_
;
2808 void ViewHierarchyChanged(
2809 const ViewHierarchyChangedDetails
& details
) override
;
2811 bool has_add_details_
;
2812 bool has_remove_details_
;
2813 ViewHierarchyChangedDetails add_details_
;
2814 ViewHierarchyChangedDetails remove_details_
;
2816 DISALLOW_COPY_AND_ASSIGN(ObserverView
);
2819 ObserverView::ObserverView()
2820 : has_add_details_(false),
2821 has_remove_details_(false) {
2824 ObserverView::~ObserverView() {}
2826 void ObserverView::ResetTestState() {
2827 has_add_details_
= false;
2828 has_remove_details_
= false;
2829 add_details_
= ViewHierarchyChangedDetails();
2830 remove_details_
= ViewHierarchyChangedDetails();
2833 void ObserverView::ViewHierarchyChanged(
2834 const ViewHierarchyChangedDetails
& details
) {
2835 if (details
.is_add
) {
2836 has_add_details_
= true;
2837 add_details_
= details
;
2839 has_remove_details_
= true;
2840 remove_details_
= details
;
2844 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2845 // a child view is added or removed to all the views in the hierarchy (up and
2847 // The tree looks like this:
2851 // +-- v4 (starts here, then get reparented to v1)
2852 TEST_F(ViewTest
, ViewHierarchyChanged
) {
2855 ObserverView
* v3
= new ObserverView();
2857 // Add |v3| to |v2|.
2858 scoped_ptr
<ObserverView
> v2(new ObserverView());
2859 v2
->AddChildView(v3
);
2861 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2863 EXPECT_TRUE(v2
->has_add_details());
2864 EXPECT_FALSE(v2
->has_remove_details());
2865 EXPECT_EQ(v2
.get(), v2
->add_details().parent
);
2866 EXPECT_EQ(v3
, v2
->add_details().child
);
2867 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2869 EXPECT_TRUE(v3
->has_add_details());
2870 EXPECT_FALSE(v3
->has_remove_details());
2871 EXPECT_EQ(v2
.get(), v3
->add_details().parent
);
2872 EXPECT_EQ(v3
, v3
->add_details().child
);
2873 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2875 // Reset everything to the initial state.
2876 v2
->ResetTestState();
2877 v3
->ResetTestState();
2880 v1
.AddChildView(v2
.get());
2882 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2883 // Make sure all the views (v1, v2, v3) received _that_ information.
2884 EXPECT_TRUE(v1
.has_add_details());
2885 EXPECT_FALSE(v1
.has_remove_details());
2886 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2887 EXPECT_EQ(v2
.get(), v1
.add_details().child
);
2888 EXPECT_EQ(NULL
, v1
.add_details().move_view
);
2890 EXPECT_TRUE(v2
->has_add_details());
2891 EXPECT_FALSE(v2
->has_remove_details());
2892 EXPECT_EQ(&v1
, v2
->add_details().parent
);
2893 EXPECT_EQ(v2
.get(), v2
->add_details().child
);
2894 EXPECT_EQ(NULL
, v2
->add_details().move_view
);
2896 EXPECT_TRUE(v3
->has_add_details());
2897 EXPECT_FALSE(v3
->has_remove_details());
2898 EXPECT_EQ(&v1
, v3
->add_details().parent
);
2899 EXPECT_EQ(v2
.get(), v3
->add_details().child
);
2900 EXPECT_EQ(NULL
, v3
->add_details().move_view
);
2902 // Reset everything to the initial state.
2903 v1
.ResetTestState();
2904 v2
->ResetTestState();
2905 v3
->ResetTestState();
2907 // Remove |v2| from |v1|.
2908 v1
.RemoveChildView(v2
.get());
2910 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2911 // Make sure all the views (v1, v2, v3) received _that_ information.
2912 EXPECT_FALSE(v1
.has_add_details());
2913 EXPECT_TRUE(v1
.has_remove_details());
2914 EXPECT_EQ(&v1
, v1
.remove_details().parent
);
2915 EXPECT_EQ(v2
.get(), v1
.remove_details().child
);
2916 EXPECT_EQ(NULL
, v1
.remove_details().move_view
);
2918 EXPECT_FALSE(v2
->has_add_details());
2919 EXPECT_TRUE(v2
->has_remove_details());
2920 EXPECT_EQ(&v1
, v2
->remove_details().parent
);
2921 EXPECT_EQ(v2
.get(), v2
->remove_details().child
);
2922 EXPECT_EQ(NULL
, v2
->remove_details().move_view
);
2924 EXPECT_FALSE(v3
->has_add_details());
2925 EXPECT_TRUE(v3
->has_remove_details());
2926 EXPECT_EQ(&v1
, v3
->remove_details().parent
);
2927 EXPECT_EQ(v3
, v3
->remove_details().child
);
2928 EXPECT_EQ(NULL
, v3
->remove_details().move_view
);
2930 // Verifies notifications when reparenting a view.
2931 ObserverView
* v4
= new ObserverView();
2932 // Add |v4| to |v2|.
2933 v2
->AddChildView(v4
);
2935 // Reset everything to the initial state.
2936 v1
.ResetTestState();
2937 v2
->ResetTestState();
2938 v3
->ResetTestState();
2939 v4
->ResetTestState();
2941 // Reparent |v4| to |v1|.
2942 v1
.AddChildView(v4
);
2944 // Verifies that all views receive the correct information for all the child,
2945 // parent and move views.
2947 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2948 EXPECT_TRUE(v1
.has_add_details());
2949 EXPECT_FALSE(v1
.has_remove_details());
2950 EXPECT_EQ(&v1
, v1
.add_details().parent
);
2951 EXPECT_EQ(v4
, v1
.add_details().child
);
2952 EXPECT_EQ(v2
.get(), v1
.add_details().move_view
);
2954 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2956 EXPECT_FALSE(v2
->has_add_details());
2957 EXPECT_TRUE(v2
->has_remove_details());
2958 EXPECT_EQ(v2
.get(), v2
->remove_details().parent
);
2959 EXPECT_EQ(v4
, v2
->remove_details().child
);
2960 EXPECT_EQ(&v1
, v2
->remove_details().move_view
);
2962 // |v3| is not impacted by this operation, and hence receives no notification.
2963 EXPECT_FALSE(v3
->has_add_details());
2964 EXPECT_FALSE(v3
->has_remove_details());
2966 // |v4| is the reparented child, so it receives notifications for the remove
2967 // and then the add. |v2| is its old parent, |v1| is its new parent.
2968 EXPECT_TRUE(v4
->has_remove_details());
2969 EXPECT_TRUE(v4
->has_add_details());
2970 EXPECT_EQ(v2
.get(), v4
->remove_details().parent
);
2971 EXPECT_EQ(&v1
, v4
->add_details().parent
);
2972 EXPECT_EQ(v4
, v4
->add_details().child
);
2973 EXPECT_EQ(v4
, v4
->remove_details().child
);
2974 EXPECT_EQ(&v1
, v4
->remove_details().move_view
);
2975 EXPECT_EQ(v2
.get(), v4
->add_details().move_view
);
2978 // Verifies if the child views added under the root are all deleted when calling
2979 // RemoveAllChildViews.
2980 // The tree looks like this:
2989 TEST_F(ViewTest
, RemoveAllChildViews
) {
2992 View
* child1
= new View
;
2993 root
.AddChildView(child1
);
2995 for (int i
= 0; i
< 2; ++i
)
2996 root
.AddChildView(new View
);
2998 View
* foo
= new View
;
2999 child1
->AddChildView(foo
);
3001 // Add some nodes to |foo|.
3002 for (int i
= 0; i
< 3; ++i
)
3003 foo
->AddChildView(new View
);
3005 EXPECT_EQ(3, root
.child_count());
3006 EXPECT_EQ(1, child1
->child_count());
3007 EXPECT_EQ(3, foo
->child_count());
3009 // Now remove all child views from root.
3010 root
.RemoveAllChildViews(true);
3012 EXPECT_EQ(0, root
.child_count());
3013 EXPECT_FALSE(root
.has_children());
3016 TEST_F(ViewTest
, Contains
) {
3018 View
* v2
= new View
;
3019 View
* v3
= new View
;
3021 v1
.AddChildView(v2
);
3022 v2
->AddChildView(v3
);
3024 EXPECT_FALSE(v1
.Contains(NULL
));
3025 EXPECT_TRUE(v1
.Contains(&v1
));
3026 EXPECT_TRUE(v1
.Contains(v2
));
3027 EXPECT_TRUE(v1
.Contains(v3
));
3029 EXPECT_FALSE(v2
->Contains(NULL
));
3030 EXPECT_TRUE(v2
->Contains(v2
));
3031 EXPECT_FALSE(v2
->Contains(&v1
));
3032 EXPECT_TRUE(v2
->Contains(v3
));
3034 EXPECT_FALSE(v3
->Contains(NULL
));
3035 EXPECT_TRUE(v3
->Contains(v3
));
3036 EXPECT_FALSE(v3
->Contains(&v1
));
3037 EXPECT_FALSE(v3
->Contains(v2
));
3040 // Verifies if GetIndexOf() returns the correct index for the specified child
3042 // The tree looks like this:
3047 TEST_F(ViewTest
, GetIndexOf
) {
3050 View
* child1
= new View
;
3051 root
.AddChildView(child1
);
3053 View
* child2
= new View
;
3054 root
.AddChildView(child2
);
3056 View
* foo1
= new View
;
3057 child1
->AddChildView(foo1
);
3059 EXPECT_EQ(-1, root
.GetIndexOf(NULL
));
3060 EXPECT_EQ(-1, root
.GetIndexOf(&root
));
3061 EXPECT_EQ(0, root
.GetIndexOf(child1
));
3062 EXPECT_EQ(1, root
.GetIndexOf(child2
));
3063 EXPECT_EQ(-1, root
.GetIndexOf(foo1
));
3065 EXPECT_EQ(-1, child1
->GetIndexOf(NULL
));
3066 EXPECT_EQ(-1, child1
->GetIndexOf(&root
));
3067 EXPECT_EQ(-1, child1
->GetIndexOf(child1
));
3068 EXPECT_EQ(-1, child1
->GetIndexOf(child2
));
3069 EXPECT_EQ(0, child1
->GetIndexOf(foo1
));
3071 EXPECT_EQ(-1, child2
->GetIndexOf(NULL
));
3072 EXPECT_EQ(-1, child2
->GetIndexOf(&root
));
3073 EXPECT_EQ(-1, child2
->GetIndexOf(child2
));
3074 EXPECT_EQ(-1, child2
->GetIndexOf(child1
));
3075 EXPECT_EQ(-1, child2
->GetIndexOf(foo1
));
3078 // Verifies that the child views can be reordered correctly.
3079 TEST_F(ViewTest
, ReorderChildren
) {
3082 View
* child
= new View();
3083 root
.AddChildView(child
);
3085 View
* foo1
= new View();
3086 child
->AddChildView(foo1
);
3087 View
* foo2
= new View();
3088 child
->AddChildView(foo2
);
3089 View
* foo3
= new View();
3090 child
->AddChildView(foo3
);
3091 foo1
->SetFocusable(true);
3092 foo2
->SetFocusable(true);
3093 foo3
->SetFocusable(true);
3095 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
3096 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
3097 ASSERT_EQ(2, child
->GetIndexOf(foo3
));
3098 ASSERT_EQ(foo2
, foo1
->GetNextFocusableView());
3099 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
3100 ASSERT_EQ(NULL
, foo3
->GetNextFocusableView());
3102 // Move |foo2| at the end.
3103 child
->ReorderChildView(foo2
, -1);
3104 ASSERT_EQ(0, child
->GetIndexOf(foo1
));
3105 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
3106 ASSERT_EQ(2, child
->GetIndexOf(foo2
));
3107 ASSERT_EQ(foo3
, foo1
->GetNextFocusableView());
3108 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
3109 ASSERT_EQ(NULL
, foo2
->GetNextFocusableView());
3111 // Move |foo1| at the end.
3112 child
->ReorderChildView(foo1
, -1);
3113 ASSERT_EQ(0, child
->GetIndexOf(foo3
));
3114 ASSERT_EQ(1, child
->GetIndexOf(foo2
));
3115 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
3116 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
3117 ASSERT_EQ(foo2
, foo1
->GetPreviousFocusableView());
3118 ASSERT_EQ(foo2
, foo3
->GetNextFocusableView());
3119 ASSERT_EQ(foo1
, foo2
->GetNextFocusableView());
3121 // Move |foo2| to the front.
3122 child
->ReorderChildView(foo2
, 0);
3123 ASSERT_EQ(0, child
->GetIndexOf(foo2
));
3124 ASSERT_EQ(1, child
->GetIndexOf(foo3
));
3125 ASSERT_EQ(2, child
->GetIndexOf(foo1
));
3126 ASSERT_EQ(NULL
, foo1
->GetNextFocusableView());
3127 ASSERT_EQ(foo3
, foo1
->GetPreviousFocusableView());
3128 ASSERT_EQ(foo3
, foo2
->GetNextFocusableView());
3129 ASSERT_EQ(foo1
, foo3
->GetNextFocusableView());
3132 // Verifies that GetViewByID returns the correctly child view from the specified
3134 // The tree looks like this:
3139 TEST_F(ViewTest
, GetViewByID
) {
3141 const int kV1ID
= 1;
3145 const int kV2ID
= 2;
3149 const int kV3ID
= 3;
3153 const int kV4ID
= 4;
3156 const int kV5ID
= 5;
3158 v1
.AddChildView(&v2
);
3159 v2
.AddChildView(&v3
);
3160 v2
.AddChildView(&v4
);
3162 EXPECT_EQ(&v1
, v1
.GetViewByID(kV1ID
));
3163 EXPECT_EQ(&v2
, v1
.GetViewByID(kV2ID
));
3164 EXPECT_EQ(&v4
, v1
.GetViewByID(kV4ID
));
3166 EXPECT_EQ(NULL
, v1
.GetViewByID(kV5ID
)); // No V5 exists.
3167 EXPECT_EQ(NULL
, v2
.GetViewByID(kV1ID
)); // It can get only from child views.
3169 const int kGroup
= 1;
3170 v3
.SetGroup(kGroup
);
3171 v4
.SetGroup(kGroup
);
3174 v1
.GetViewsInGroup(kGroup
, &views
);
3175 EXPECT_EQ(2U, views
.size());
3177 View::Views::const_iterator
i(std::find(views
.begin(), views
.end(), &v3
));
3178 EXPECT_NE(views
.end(), i
);
3180 i
= std::find(views
.begin(), views
.end(), &v4
);
3181 EXPECT_NE(views
.end(), i
);
3184 TEST_F(ViewTest
, AddExistingChild
) {
3187 v1
.AddChildView(&v2
);
3188 v1
.AddChildView(&v3
);
3189 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3190 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3192 // Check that there's no change in order when adding at same index.
3193 v1
.AddChildViewAt(&v2
, 0);
3194 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3195 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3196 v1
.AddChildViewAt(&v3
, 1);
3197 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3198 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3200 // Add it at a different index and check for change in order.
3201 v1
.AddChildViewAt(&v2
, 1);
3202 EXPECT_EQ(1, v1
.GetIndexOf(&v2
));
3203 EXPECT_EQ(0, v1
.GetIndexOf(&v3
));
3204 v1
.AddChildViewAt(&v2
, 0);
3205 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3206 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3208 // Check that calling |AddChildView()| does not change the order.
3209 v1
.AddChildView(&v2
);
3210 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3211 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3212 v1
.AddChildView(&v3
);
3213 EXPECT_EQ(0, v1
.GetIndexOf(&v2
));
3214 EXPECT_EQ(1, v1
.GetIndexOf(&v3
));
3217 ////////////////////////////////////////////////////////////////////////////////
3219 ////////////////////////////////////////////////////////////////////////////////
3221 // A widget that always claims to be active, regardless of its real activation
3223 class ActiveWidget
: public Widget
{
3226 ~ActiveWidget() override
{}
3228 bool IsActive() const override
{ return true; }
3231 DISALLOW_COPY_AND_ASSIGN(ActiveWidget
);
3234 TEST_F(ViewTest
, AdvanceFocusIfNecessaryForUnfocusableView
) {
3235 // Create a widget with two views and give the first one focus.
3236 ActiveWidget widget
;
3237 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3238 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
3239 widget
.Init(params
);
3241 View
* view1
= new View();
3242 view1
->SetFocusable(true);
3243 widget
.GetRootView()->AddChildView(view1
);
3244 View
* view2
= new View();
3245 view2
->SetFocusable(true);
3246 widget
.GetRootView()->AddChildView(view2
);
3248 FocusManager
* focus_manager
= widget
.GetFocusManager();
3249 ASSERT_TRUE(focus_manager
);
3251 focus_manager
->SetFocusedView(view1
);
3252 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
3254 // Disable the focused view and check if the next view gets focused.
3255 view1
->SetEnabled(false);
3256 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
3258 // Re-enable and re-focus.
3259 view1
->SetEnabled(true);
3260 focus_manager
->SetFocusedView(view1
);
3261 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
3263 // Hide the focused view and check it the next view gets focused.
3264 view1
->SetVisible(false);
3265 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
3267 // Re-show and re-focus.
3268 view1
->SetVisible(true);
3269 focus_manager
->SetFocusedView(view1
);
3270 EXPECT_EQ(view1
, focus_manager
->GetFocusedView());
3272 // Set the focused view as not focusable and check if the next view gets
3274 view1
->SetFocusable(false);
3275 EXPECT_EQ(view2
, focus_manager
->GetFocusedView());
3278 ////////////////////////////////////////////////////////////////////////////////
3280 ////////////////////////////////////////////////////////////////////////////////
3284 // Test implementation of LayerAnimator.
3285 class TestLayerAnimator
: public ui::LayerAnimator
{
3287 TestLayerAnimator();
3289 const gfx::Rect
& last_bounds() const { return last_bounds_
; }
3292 void SetBounds(const gfx::Rect
& bounds
) override
;
3295 ~TestLayerAnimator() override
{}
3298 gfx::Rect last_bounds_
;
3300 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
3303 TestLayerAnimator::TestLayerAnimator()
3304 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
3307 void TestLayerAnimator::SetBounds(const gfx::Rect
& bounds
) {
3308 last_bounds_
= bounds
;
3313 class ViewLayerTest
: public ViewsTestBase
{
3315 ViewLayerTest() : widget_(NULL
) {}
3317 ~ViewLayerTest() override
{}
3319 // Returns the Layer used by the RootView.
3320 ui::Layer
* GetRootLayer() {
3321 return widget()->GetLayer();
3324 void SetUp() override
{
3326 widget_
= new Widget
;
3327 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
3328 params
.bounds
= gfx::Rect(50, 50, 200, 200);
3329 widget_
->Init(params
);
3331 widget_
->GetRootView()->SetBounds(0, 0, 200, 200);
3334 void TearDown() override
{
3335 widget_
->CloseNow();
3336 ViewsTestBase::TearDown();
3339 Widget
* widget() { return widget_
; }
3346 TEST_F(ViewLayerTest
, LayerToggling
) {
3347 // Because we lazily create textures the calls to DrawTree are necessary to
3348 // ensure we trigger creation of textures.
3349 ui::Layer
* root_layer
= widget()->GetLayer();
3350 View
* content_view
= new View
;
3351 widget()->SetContentsView(content_view
);
3353 // Create v1, give it a bounds and verify everything is set up correctly.
3354 View
* v1
= new View
;
3355 v1
->SetPaintToLayer(true);
3356 EXPECT_TRUE(v1
->layer() != NULL
);
3357 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3358 content_view
->AddChildView(v1
);
3359 ASSERT_TRUE(v1
->layer() != NULL
);
3360 EXPECT_EQ(root_layer
, v1
->layer()->parent());
3361 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1
->layer()->bounds());
3363 // Create v2 as a child of v1 and do basic assertion testing.
3364 View
* v2
= new View
;
3365 v1
->AddChildView(v2
);
3366 EXPECT_TRUE(v2
->layer() == NULL
);
3367 v2
->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
3368 v2
->SetPaintToLayer(true);
3369 ASSERT_TRUE(v2
->layer() != NULL
);
3370 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3371 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3373 // Turn off v1s layer. v2 should still have a layer but its parent should have
3375 v1
->SetPaintToLayer(false);
3376 EXPECT_TRUE(v1
->layer() == NULL
);
3377 EXPECT_TRUE(v2
->layer() != NULL
);
3378 EXPECT_EQ(root_layer
, v2
->layer()->parent());
3379 ASSERT_EQ(1u, root_layer
->children().size());
3380 EXPECT_EQ(root_layer
->children()[0], v2
->layer());
3381 // The bounds of the layer should have changed to be relative to the root view
3383 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2
->layer()->bounds());
3385 // Make v1 have a layer again and verify v2s layer is wired up correctly.
3386 gfx::Transform transform
;
3387 transform
.Scale(2.0, 2.0);
3388 v1
->SetTransform(transform
);
3389 EXPECT_TRUE(v1
->layer() != NULL
);
3390 EXPECT_TRUE(v2
->layer() != NULL
);
3391 EXPECT_EQ(root_layer
, v1
->layer()->parent());
3392 EXPECT_EQ(v1
->layer(), v2
->layer()->parent());
3393 ASSERT_EQ(1u, root_layer
->children().size());
3394 EXPECT_EQ(root_layer
->children()[0], v1
->layer());
3395 ASSERT_EQ(1u, v1
->layer()->children().size());
3396 EXPECT_EQ(v1
->layer()->children()[0], v2
->layer());
3397 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2
->layer()->bounds());
3400 // Verifies turning on a layer wires up children correctly.
3401 TEST_F(ViewLayerTest
, NestedLayerToggling
) {
3402 View
* content_view
= new View
;
3403 widget()->SetContentsView(content_view
);
3405 // Create v1, give it a bounds and verify everything is set up correctly.
3406 View
* v1
= new View
;
3407 content_view
->AddChildView(v1
);
3408 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3410 View
* v2
= new View
;
3411 v1
->AddChildView(v2
);
3413 View
* v3
= new View
;
3414 v3
->SetPaintToLayer(true);
3415 v2
->AddChildView(v3
);
3416 ASSERT_TRUE(v3
->layer() != NULL
);
3418 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
3420 v1
->SetPaintToLayer(true);
3421 EXPECT_EQ(v1
->layer(), v3
->layer()->parent());
3424 TEST_F(ViewLayerTest
, LayerAnimator
) {
3425 View
* content_view
= new View
;
3426 widget()->SetContentsView(content_view
);
3428 View
* v1
= new View
;
3429 content_view
->AddChildView(v1
);
3430 v1
->SetPaintToLayer(true);
3431 EXPECT_TRUE(v1
->layer() != NULL
);
3433 TestLayerAnimator
* animator
= new TestLayerAnimator();
3434 v1
->layer()->SetAnimator(animator
);
3436 gfx::Rect
bounds(1, 2, 3, 4);
3437 v1
->SetBoundsRect(bounds
);
3438 EXPECT_EQ(bounds
, animator
->last_bounds());
3439 // TestLayerAnimator doesn't update the layer.
3440 EXPECT_NE(bounds
, v1
->layer()->bounds());
3443 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3444 // doesn't have a layer change.
3445 TEST_F(ViewLayerTest
, BoundsChangeWithLayer
) {
3446 View
* content_view
= new View
;
3447 widget()->SetContentsView(content_view
);
3449 View
* v1
= new View
;
3450 content_view
->AddChildView(v1
);
3451 v1
->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3453 View
* v2
= new View
;
3454 v2
->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3455 v1
->AddChildView(v2
);
3456 v2
->SetPaintToLayer(true);
3457 ASSERT_TRUE(v2
->layer() != NULL
);
3458 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2
->layer()->bounds());
3460 v1
->SetPosition(gfx::Point(25, 36));
3461 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2
->layer()->bounds());
3463 v2
->SetPosition(gfx::Point(11, 12));
3464 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2
->layer()->bounds());
3466 // Bounds of the layer should change even if the view is not invisible.
3467 v1
->SetVisible(false);
3468 v1
->SetPosition(gfx::Point(20, 30));
3469 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2
->layer()->bounds());
3471 v2
->SetVisible(false);
3472 v2
->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3473 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2
->layer()->bounds());
3476 // Make sure layers are positioned correctly in RTL.
3477 TEST_F(ViewLayerTest
, BoundInRTL
) {
3480 View
* view
= new View
;
3481 widget()->SetContentsView(view
);
3483 int content_width
= view
->width();
3485 // |v1| is initially not attached to anything. So its layer will have the same
3486 // bounds as the view.
3487 View
* v1
= new View
;
3488 v1
->SetPaintToLayer(true);
3489 v1
->SetBounds(10, 10, 20, 10);
3490 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3491 v1
->layer()->bounds());
3493 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3495 view
->AddChildView(v1
);
3496 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3497 v1
->layer()->bounds());
3498 gfx::Rect l1bounds
= v1
->layer()->bounds();
3500 // Now attach a View to the widget first, then create a layer for it. Make
3501 // sure the bounds are correct.
3502 View
* v2
= new View
;
3503 v2
->SetBounds(50, 10, 30, 10);
3504 EXPECT_FALSE(v2
->layer());
3505 view
->AddChildView(v2
);
3506 v2
->SetPaintToLayer(true);
3507 EXPECT_EQ(gfx::Rect(content_width
- 80, 10, 30, 10),
3508 v2
->layer()->bounds());
3509 gfx::Rect l2bounds
= v2
->layer()->bounds();
3511 view
->SetPaintToLayer(true);
3512 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3513 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3515 // Move one of the views. Make sure the layer is positioned correctly
3517 v1
->SetBounds(v1
->x() - 5, v1
->y(), v1
->width(), v1
->height());
3518 l1bounds
.set_x(l1bounds
.x() + 5);
3519 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3521 view
->SetPaintToLayer(false);
3522 EXPECT_EQ(l1bounds
, v1
->layer()->bounds());
3523 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3525 // Move a view again.
3526 v2
->SetBounds(v2
->x() + 5, v2
->y(), v2
->width(), v2
->height());
3527 l2bounds
.set_x(l2bounds
.x() - 5);
3528 EXPECT_EQ(l2bounds
, v2
->layer()->bounds());
3531 // Make sure that resizing a parent in RTL correctly repositions its children.
3532 TEST_F(ViewLayerTest
, ResizeParentInRTL
) {
3535 View
* view
= new View
;
3536 widget()->SetContentsView(view
);
3538 int content_width
= view
->width();
3540 // Create a paints-to-layer view |v1|.
3541 View
* v1
= new View
;
3542 v1
->SetPaintToLayer(true);
3543 v1
->SetBounds(10, 10, 20, 10);
3544 view
->AddChildView(v1
);
3545 EXPECT_EQ(gfx::Rect(content_width
- 30, 10, 20, 10),
3546 v1
->layer()->bounds());
3548 // Attach a paints-to-layer child view to |v1|.
3549 View
* v2
= new View
;
3550 v2
->SetPaintToLayer(true);
3551 v2
->SetBounds(3, 5, 6, 4);
3552 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
3553 v2
->layer()->bounds());
3554 v1
->AddChildView(v2
);
3555 // Check that |v2| now has RTL-appropriate bounds.
3556 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3557 v2
->layer()->bounds());
3559 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
3560 View
* v3
= new View
;
3561 v3
->SetBounds(1, 1, 18, 8);
3562 View
* v4
= new View
;
3563 v4
->SetPaintToLayer(true);
3564 v4
->SetBounds(2, 4, 6, 4);
3565 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
3566 v4
->layer()->bounds());
3567 v3
->AddChildView(v4
);
3568 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
3569 v4
->layer()->bounds());
3570 v1
->AddChildView(v3
);
3571 // Check that |v4| now has RTL-appropriate bounds.
3572 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3573 v4
->layer()->bounds());
3575 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3576 // correctly to RTL-appropriate bounds.
3577 v1
->SetSize(gfx::Size(30, 10));
3578 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3579 v2
->layer()->bounds());
3580 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3581 v4
->layer()->bounds());
3583 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3584 // to RTL-appropriate bounds.
3585 v3
->SetBounds(2, 1, 12, 8);
3586 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3587 v4
->layer()->bounds());
3590 // Makes sure a transform persists after toggling the visibility.
3591 TEST_F(ViewLayerTest
, ToggleVisibilityWithTransform
) {
3592 View
* view
= new View
;
3593 gfx::Transform transform
;
3594 transform
.Scale(2.0, 2.0);
3595 view
->SetTransform(transform
);
3596 widget()->SetContentsView(view
);
3597 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3599 view
->SetVisible(false);
3600 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3602 view
->SetVisible(true);
3603 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3606 // Verifies a transform persists after removing/adding a view with a transform.
3607 TEST_F(ViewLayerTest
, ResetTransformOnLayerAfterAdd
) {
3608 View
* view
= new View
;
3609 gfx::Transform transform
;
3610 transform
.Scale(2.0, 2.0);
3611 view
->SetTransform(transform
);
3612 widget()->SetContentsView(view
);
3613 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3614 ASSERT_TRUE(view
->layer() != NULL
);
3615 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3617 View
* parent
= view
->parent();
3618 parent
->RemoveChildView(view
);
3619 parent
->AddChildView(view
);
3621 EXPECT_EQ(2.0f
, view
->GetTransform().matrix().get(0, 0));
3622 ASSERT_TRUE(view
->layer() != NULL
);
3623 EXPECT_EQ(2.0f
, view
->layer()->transform().matrix().get(0, 0));
3626 // Makes sure that layer visibility is correct after toggling View visibility.
3627 TEST_F(ViewLayerTest
, ToggleVisibilityWithLayer
) {
3628 View
* content_view
= new View
;
3629 widget()->SetContentsView(content_view
);
3631 // The view isn't attached to a widget or a parent view yet. But it should
3632 // still have a layer, but the layer should not be attached to the root
3634 View
* v1
= new View
;
3635 v1
->SetPaintToLayer(true);
3636 EXPECT_TRUE(v1
->layer());
3637 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3640 // Once the view is attached to a widget, its layer should be attached to the
3641 // root layer and visible.
3642 content_view
->AddChildView(v1
);
3643 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3645 EXPECT_TRUE(v1
->layer()->IsDrawn());
3647 v1
->SetVisible(false);
3648 EXPECT_FALSE(v1
->layer()->IsDrawn());
3650 v1
->SetVisible(true);
3651 EXPECT_TRUE(v1
->layer()->IsDrawn());
3654 EXPECT_FALSE(v1
->layer()->IsDrawn());
3657 EXPECT_TRUE(v1
->layer()->IsDrawn());
3660 // Tests that the layers in the subtree are orphaned after a View is removed
3662 TEST_F(ViewLayerTest
, OrphanLayerAfterViewRemove
) {
3663 View
* content_view
= new View
;
3664 widget()->SetContentsView(content_view
);
3666 View
* v1
= new View
;
3667 content_view
->AddChildView(v1
);
3669 View
* v2
= new View
;
3670 v1
->AddChildView(v2
);
3671 v2
->SetPaintToLayer(true);
3672 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3674 EXPECT_TRUE(v2
->layer()->IsDrawn());
3676 content_view
->RemoveChildView(v1
);
3678 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3682 content_view
->AddChildView(v2
);
3685 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3687 EXPECT_TRUE(v2
->layer()->IsDrawn());
3690 class PaintTrackingView
: public View
{
3692 PaintTrackingView() : painted_(false) {
3695 bool painted() const { return painted_
; }
3696 void set_painted(bool value
) { painted_
= value
; }
3698 void OnPaint(gfx::Canvas
* canvas
) override
{ painted_
= true; }
3703 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView
);
3706 // Makes sure child views with layers aren't painted when paint starts at an
3708 TEST_F(ViewLayerTest
, DontPaintChildrenWithLayers
) {
3709 PaintTrackingView
* content_view
= new PaintTrackingView
;
3710 widget()->SetContentsView(content_view
);
3711 content_view
->SetPaintToLayer(true);
3712 GetRootLayer()->GetCompositor()->ScheduleDraw();
3713 ui::DrawWaiterForTest::WaitForCompositingEnded(
3714 GetRootLayer()->GetCompositor());
3715 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3716 content_view
->set_painted(false);
3717 // content_view no longer has a dirty rect. Paint from the root and make sure
3718 // PaintTrackingView isn't painted.
3719 GetRootLayer()->GetCompositor()->ScheduleDraw();
3720 ui::DrawWaiterForTest::WaitForCompositingEnded(
3721 GetRootLayer()->GetCompositor());
3722 EXPECT_FALSE(content_view
->painted());
3724 // Make content_view have a dirty rect, paint the layers and make sure
3725 // PaintTrackingView is painted.
3726 content_view
->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3727 GetRootLayer()->GetCompositor()->ScheduleDraw();
3728 ui::DrawWaiterForTest::WaitForCompositingEnded(
3729 GetRootLayer()->GetCompositor());
3730 EXPECT_TRUE(content_view
->painted());
3733 // Tests that the visibility of child layers are updated correctly when a View's
3734 // visibility changes.
3735 TEST_F(ViewLayerTest
, VisibilityChildLayers
) {
3736 View
* v1
= new View
;
3737 v1
->SetPaintToLayer(true);
3738 widget()->SetContentsView(v1
);
3740 View
* v2
= new View
;
3741 v1
->AddChildView(v2
);
3743 View
* v3
= new View
;
3744 v2
->AddChildView(v3
);
3745 v3
->SetVisible(false);
3747 View
* v4
= new View
;
3748 v4
->SetPaintToLayer(true);
3749 v3
->AddChildView(v4
);
3751 EXPECT_TRUE(v1
->layer()->IsDrawn());
3752 EXPECT_FALSE(v4
->layer()->IsDrawn());
3754 v2
->SetVisible(false);
3755 EXPECT_TRUE(v1
->layer()->IsDrawn());
3756 EXPECT_FALSE(v4
->layer()->IsDrawn());
3758 v2
->SetVisible(true);
3759 EXPECT_TRUE(v1
->layer()->IsDrawn());
3760 EXPECT_FALSE(v4
->layer()->IsDrawn());
3762 v2
->SetVisible(false);
3763 EXPECT_TRUE(v1
->layer()->IsDrawn());
3764 EXPECT_FALSE(v4
->layer()->IsDrawn());
3765 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3767 v3
->SetVisible(true);
3768 EXPECT_TRUE(v1
->layer()->IsDrawn());
3769 EXPECT_FALSE(v4
->layer()->IsDrawn());
3770 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3772 // Reparent |v3| to |v1|.
3773 v1
->AddChildView(v3
);
3774 EXPECT_TRUE(v1
->layer()->IsDrawn());
3775 EXPECT_TRUE(v4
->layer()->IsDrawn());
3776 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1
, v1
->layer()));
3779 // This test creates a random View tree, and then randomly reorders child views,
3780 // reparents views etc. Unrelated changes can appear to break this test. So
3781 // marking this as FLAKY.
3782 TEST_F(ViewLayerTest
, DISABLED_ViewLayerTreesInSync
) {
3783 View
* content
= new View
;
3784 content
->SetPaintToLayer(true);
3785 widget()->SetContentsView(content
);
3788 ConstructTree(content
, 5);
3789 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3791 ScrambleTree(content
);
3792 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3794 ScrambleTree(content
);
3795 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3797 ScrambleTree(content
);
3798 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content
, content
->layer()));
3801 // Verifies when views are reordered the layer is also reordered. The widget is
3802 // providing the parent layer.
3803 TEST_F(ViewLayerTest
, ReorderUnderWidget
) {
3804 View
* content
= new View
;
3805 widget()->SetContentsView(content
);
3806 View
* c1
= new View
;
3807 c1
->SetPaintToLayer(true);
3808 content
->AddChildView(c1
);
3809 View
* c2
= new View
;
3810 c2
->SetPaintToLayer(true);
3811 content
->AddChildView(c2
);
3813 ui::Layer
* parent_layer
= c1
->layer()->parent();
3814 ASSERT_TRUE(parent_layer
);
3815 ASSERT_EQ(2u, parent_layer
->children().size());
3816 EXPECT_EQ(c1
->layer(), parent_layer
->children()[0]);
3817 EXPECT_EQ(c2
->layer(), parent_layer
->children()[1]);
3819 // Move c1 to the front. The layers should have moved too.
3820 content
->ReorderChildView(c1
, -1);
3821 EXPECT_EQ(c1
->layer(), parent_layer
->children()[1]);
3822 EXPECT_EQ(c2
->layer(), parent_layer
->children()[0]);
3825 // Verifies that the layer of a view can be acquired properly.
3826 TEST_F(ViewLayerTest
, AcquireLayer
) {
3827 View
* content
= new View
;
3828 widget()->SetContentsView(content
);
3829 scoped_ptr
<View
> c1(new View
);
3830 c1
->SetPaintToLayer(true);
3831 EXPECT_TRUE(c1
->layer());
3832 content
->AddChildView(c1
.get());
3834 scoped_ptr
<ui::Layer
> layer(c1
->AcquireLayer());
3835 EXPECT_EQ(layer
.get(), c1
->layer());
3837 scoped_ptr
<ui::Layer
> layer2(c1
->RecreateLayer());
3838 EXPECT_NE(c1
->layer(), layer2
.get());
3840 // Destroy view before destroying layer.
3844 // Verify the z-order of the layers as a result of calling RecreateLayer().
3845 TEST_F(ViewLayerTest
, RecreateLayerZOrder
) {
3846 scoped_ptr
<View
> v(new View());
3847 v
->SetPaintToLayer(true);
3849 View
* v1
= new View();
3850 v1
->SetPaintToLayer(true);
3851 v
->AddChildView(v1
);
3852 View
* v2
= new View();
3853 v2
->SetPaintToLayer(true);
3854 v
->AddChildView(v2
);
3856 // Test the initial z-order.
3857 const std::vector
<ui::Layer
*>& child_layers_pre
= v
->layer()->children();
3858 ASSERT_EQ(2u, child_layers_pre
.size());
3859 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3860 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3862 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3864 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3865 // for |v1| and |v2|.
3866 const std::vector
<ui::Layer
*>& child_layers_post
= v
->layer()->children();
3867 ASSERT_EQ(3u, child_layers_post
.size());
3868 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3869 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3870 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3873 // Verify the z-order of the layers as a result of calling RecreateLayer when
3874 // the widget is the parent with the layer.
3875 TEST_F(ViewLayerTest
, RecreateLayerZOrderWidgetParent
) {
3876 View
* v
= new View();
3877 widget()->SetContentsView(v
);
3879 View
* v1
= new View();
3880 v1
->SetPaintToLayer(true);
3881 v
->AddChildView(v1
);
3882 View
* v2
= new View();
3883 v2
->SetPaintToLayer(true);
3884 v
->AddChildView(v2
);
3886 ui::Layer
* root_layer
= GetRootLayer();
3888 // Test the initial z-order.
3889 const std::vector
<ui::Layer
*>& child_layers_pre
= root_layer
->children();
3890 ASSERT_EQ(2u, child_layers_pre
.size());
3891 EXPECT_EQ(v1
->layer(), child_layers_pre
[0]);
3892 EXPECT_EQ(v2
->layer(), child_layers_pre
[1]);
3894 scoped_ptr
<ui::Layer
> v1_old_layer(v1
->RecreateLayer());
3896 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3897 const std::vector
<ui::Layer
*>& child_layers_post
= root_layer
->children();
3898 ASSERT_EQ(3u, child_layers_post
.size());
3899 EXPECT_EQ(v1
->layer(), child_layers_post
[0]);
3900 EXPECT_EQ(v1_old_layer
, child_layers_post
[1]);
3901 EXPECT_EQ(v2
->layer(), child_layers_post
[2]);
3904 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3906 TEST_F(ViewLayerTest
, RecreateLayerMovesNonViewChildren
) {
3908 v
.SetPaintToLayer(true);
3910 child
.SetPaintToLayer(true);
3911 v
.AddChildView(&child
);
3912 ASSERT_TRUE(v
.layer() != NULL
);
3913 ASSERT_EQ(1u, v
.layer()->children().size());
3914 EXPECT_EQ(v
.layer()->children()[0], child
.layer());
3916 ui::Layer
layer(ui::LAYER_NOT_DRAWN
);
3917 v
.layer()->Add(&layer
);
3918 v
.layer()->StackAtBottom(&layer
);
3920 scoped_ptr
<ui::Layer
> old_layer(v
.RecreateLayer());
3922 // All children should be moved from old layer to new layer.
3923 ASSERT_TRUE(old_layer
.get() != NULL
);
3924 EXPECT_TRUE(old_layer
->children().empty());
3926 // And new layer should have the two children.
3927 ASSERT_TRUE(v
.layer() != NULL
);
3928 ASSERT_EQ(2u, v
.layer()->children().size());
3929 EXPECT_EQ(v
.layer()->children()[0], &layer
);
3930 EXPECT_EQ(v
.layer()->children()[1], child
.layer());
3935 std::string
ToString(const gfx::Vector2dF
& vector
) {
3936 return base::StringPrintf("%.2f %0.2f", vector
.x(), vector
.y());
3941 TEST_F(ViewLayerTest
, SnapLayerToPixel
) {
3942 View
* v1
= new View
;
3944 View
* v11
= new View
;
3945 v1
->AddChildView(v11
);
3947 widget()->SetContentsView(v1
);
3949 const gfx::Size
& size
= GetRootLayer()->GetCompositor()->size();
3950 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f
, size
);
3952 v11
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3953 v1
->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3954 v11
->SetPaintToLayer(true);
3956 EXPECT_EQ("0.40 0.40", ToString(v11
->layer()->subpixel_position_offset()));
3958 // Creating a layer in parent should update the child view's layer offset.
3959 v1
->SetPaintToLayer(true);
3960 EXPECT_EQ("-0.20 -0.20", ToString(v1
->layer()->subpixel_position_offset()));
3961 EXPECT_EQ("-0.20 -0.20", ToString(v11
->layer()->subpixel_position_offset()));
3963 // DSF change should get propagated and update offsets.
3964 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f
, size
);
3965 EXPECT_EQ("0.33 0.33", ToString(v1
->layer()->subpixel_position_offset()));
3966 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3968 // Deleting parent's layer should update the child view's layer's offset.
3969 v1
->SetPaintToLayer(false);
3970 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3972 // Setting parent view should update the child view's layer's offset.
3973 v1
->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3974 EXPECT_EQ("0.33 0.33", ToString(v11
->layer()->subpixel_position_offset()));
3976 // Setting integral DSF should reset the offset.
3977 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f
, size
);
3978 EXPECT_EQ("0.00 0.00", ToString(v11
->layer()->subpixel_position_offset()));
3981 TEST_F(ViewTest
, FocusableAssertions
) {
3982 // View subclasses may change insets based on whether they are focusable,
3983 // which effects the preferred size. To avoid preferred size changing around
3984 // these Views need to key off the last value set to SetFocusable(), not
3985 // whether the View is focusable right now. For this reason it's important
3986 // that focusable() return the last value passed to SetFocusable and not
3987 // whether the View is focusable right now.
3989 view
.SetFocusable(true);
3990 EXPECT_TRUE(view
.focusable());
3991 view
.SetEnabled(false);
3992 EXPECT_TRUE(view
.focusable());
3993 view
.SetFocusable(false);
3994 EXPECT_FALSE(view
.focusable());
3997 // Verifies when a view is deleted it is removed from ViewStorage.
3998 TEST_F(ViewTest
, UpdateViewStorageOnDelete
) {
3999 ViewStorage
* view_storage
= ViewStorage::GetInstance();
4000 const int storage_id
= view_storage
->CreateStorageID();
4003 view_storage
->StoreView(storage_id
, &view
);
4005 EXPECT_TRUE(view_storage
->RetrieveView(storage_id
) == NULL
);
4008 ////////////////////////////////////////////////////////////////////////////////
4010 ////////////////////////////////////////////////////////////////////////////////
4012 void TestView::OnNativeThemeChanged(const ui::NativeTheme
* native_theme
) {
4013 native_theme_
= native_theme
;
4016 TEST_F(ViewTest
, OnNativeThemeChanged
) {
4017 TestView
* test_view
= new TestView();
4018 EXPECT_FALSE(test_view
->native_theme_
);
4019 TestView
* test_view_child
= new TestView();
4020 EXPECT_FALSE(test_view_child
->native_theme_
);
4022 // Child view added before the widget hierarchy exists should get the
4023 // new native theme notification.
4024 test_view
->AddChildView(test_view_child
);
4026 scoped_ptr
<Widget
> widget(new Widget
);
4027 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
4028 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
4029 widget
->Init(params
);
4031 widget
->GetRootView()->AddChildView(test_view
);
4032 EXPECT_TRUE(test_view
->native_theme_
);
4033 EXPECT_EQ(widget
->GetNativeTheme(), test_view
->native_theme_
);
4034 EXPECT_TRUE(test_view_child
->native_theme_
);
4035 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child
->native_theme_
);
4037 // Child view added after the widget hierarchy exists should also get the
4039 TestView
* test_view_child_2
= new TestView();
4040 test_view
->AddChildView(test_view_child_2
);
4041 EXPECT_TRUE(test_view_child_2
->native_theme_
);
4042 EXPECT_EQ(widget
->GetNativeTheme(), test_view_child_2
->native_theme_
);
4047 } // namespace views