Roll src/third_party/WebKit 88cc93e:f6ae725 (svn 200125:200141)
[chromium-blink-merge.git] / ui / views / view_unittest.cc
blob38683cc303a93c5be3645c0793ad4adea9bf0993
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.
5 #include <map>
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;
43 namespace {
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())
56 v = v->child_at(0);
57 return v;
60 const views::View* NextView(const views::View* view) {
61 const views::View* v = view;
62 const views::View* parent = v->parent();
63 if (!parent)
64 return NULL;
65 int next = parent->GetIndexOf(v) + 1;
66 if (next != parent->child_count())
67 return FirstView(parent->child_at(next));
68 return parent;
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)
75 l = l->children()[0];
76 return l;
79 const ui::Layer* NextLayer(const ui::Layer* layer) {
80 const ui::Layer* parent = layer->parent();
81 if (!parent)
82 return NULL;
83 const std::vector<ui::Layer*> children = parent->children();
84 size_t index;
85 for (index = 0; index < children.size(); index++) {
86 if (children[index] == layer)
87 break;
89 size_t next = index + 1;
90 if (next < children.size())
91 return FirstLayer(children[next]);
92 return parent;
95 // Given the root nodes of a View tree and a Layer tree, makes sure the two
96 // trees are in sync.
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);
101 while (v && l) {
102 // Find the view with a layer.
103 while (v && !v->layer())
104 v = NextView(v);
105 EXPECT_TRUE(v);
106 if (!v)
107 return false;
109 // Check if the View tree and the Layer tree are in sync.
110 EXPECT_EQ(l, v->layer());
111 if (v->layer() != l)
112 return false;
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();
122 return false;
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())
128 return false;
130 if (v == view || l == layer)
131 return v == view && l == layer;
133 v = NextView(v);
134 l = NextLayer(l);
137 return false;
140 // Constructs a View tree with the specified depth.
141 void ConstructTree(views::View* view, int depth) {
142 if (depth == 0)
143 return;
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();
159 if (count == 0)
160 return;
161 for (int i = 0; i < count; i++) {
162 ScrambleTree(view->child_at(i));
165 if (count > 1) {
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());
182 class ScopedRTL {
183 public:
184 ScopedRTL() {
185 locale_ = l10n_util::GetApplicationLocale(std::string());
186 base::i18n::SetICUDefaultLocale("he");
188 ~ScopedRTL() { base::i18n::SetICUDefaultLocale(locale_); }
190 private:
191 std::string locale_;
194 } // namespace
196 namespace views {
198 typedef ViewsTestBase ViewTest;
200 // A derived class for testing purpose.
201 class TestView : public View {
202 public:
203 TestView()
204 : View(),
205 delete_on_pressed_(false),
206 did_paint_(false),
207 native_theme_(NULL),
208 can_process_events_within_subtree_(true) {}
209 ~TestView() override {}
211 // Reset all test state
212 void Reset() {
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;
218 did_paint_ = false;
219 accelerator_count_map_.clear();
220 can_process_events_within_subtree_ = true;
223 // Exposed as public for testing.
224 void DoFocus() {
225 views::View::Focus();
228 void DoBlur() {
229 views::View::Blur();
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;
255 // OnBoundsChanged.
256 bool did_change_bounds_;
257 gfx::Rect new_bounds_;
259 // MouseEvent.
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_;
266 // Painting.
267 std::vector<gfx::Rect> scheduled_paint_rects_;
268 bool did_paint_;
270 // Accelerators.
271 std::map<ui::Accelerator, int> accelerator_count_map_;
273 // Native theme.
274 const ui::NativeTheme* native_theme_;
276 // Value to return from CanProcessEventsWithinSubtree().
277 bool can_process_events_within_subtree_;
280 ////////////////////////////////////////////////////////////////////////////////
281 // OnBoundsChanged
282 ////////////////////////////////////////////////////////////////////////////////
284 void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
285 did_change_bounds_ = true;
286 new_bounds_ = bounds();
289 TEST_F(ViewTest, OnBoundsChanged) {
290 TestView v;
292 gfx::Rect prev_rect(0, 0, 200, 200);
293 gfx::Rect new_rect(100, 100, 250, 250);
295 v.SetBoundsRect(prev_rect);
296 v.Reset();
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 ////////////////////////////////////////////////////////////////////////////////
305 // MouseEvent
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_)
312 delete this;
313 return true;
316 bool TestView::OnMouseDragged(const ui::MouseEvent& event) {
317 last_mouse_event_type_ = event.type();
318 location_.SetPoint(event.x(), event.y());
319 return true;
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);
353 v1->Reset();
354 v2->Reset();
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
367 v1->Reset();
368 v2->Reset();
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
380 v1->Reset();
381 v2->Reset();
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);
391 widget->CloseNow();
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));
402 v1->Reset();
403 v2->Reset();
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());
423 widget->CloseNow();
426 ////////////////////////////////////////////////////////////////////////////////
427 // Painting
428 ////////////////////////////////////////////////////////////////////////////////
430 void TestView::OnPaint(gfx::Canvas* canvas) {
431 did_paint_ = true;
434 namespace {
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 {
439 public:
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() {
447 widget_->CloseNow();
450 Widget* operator->() { return widget_; }
452 private:
453 Widget* widget_;
455 DISALLOW_COPY_AND_ASSIGN(ScopedTestPaintWidget);
458 } // namespace
460 TEST_F(ViewTest, PaintEmptyView) {
461 ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
462 View* root_view = widget->GetRootView();
464 // |v1| is empty.
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);
474 // |v2| is not.
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));
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
505 // invalidation.
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));
510 v1->Reset();
511 v2->Reset();
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, paint_area));
521 EXPECT_FALSE(v1->did_paint_);
522 EXPECT_FALSE(v2->did_paint_);
524 // With unknown invalidation, v1 and v2 are painted.
525 root_view->Paint(
526 ui::PaintContext(ui::PaintContext(list.get(), 1.f, 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
545 // invalidation.
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));
550 v1->Reset();
551 v2->Reset();
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, paint_area));
560 EXPECT_TRUE(v1->did_paint_);
561 EXPECT_TRUE(v2->did_paint_);
564 TEST_F(ViewTest, PaintContainsChildrenInRTL) {
565 ScopedRTL rtl;
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
589 // invalidation.
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));
594 v1->Reset();
595 v2->Reset();
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, 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
621 // invalidation.
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));
626 v1->Reset();
627 v2->Reset();
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, paint_area));
636 EXPECT_TRUE(v1->did_paint_);
637 EXPECT_TRUE(v2->did_paint_);
640 TEST_F(ViewTest, PaintIntersectsChildrenInRTL) {
641 ScopedRTL rtl;
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
665 // invalidation.
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));
670 v1->Reset();
671 v2->Reset();
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, 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
697 // invalidation.
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));
702 v1->Reset();
703 v2->Reset();
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, paint_area));
712 EXPECT_TRUE(v1->did_paint_);
713 EXPECT_FALSE(v2->did_paint_);
716 TEST_F(ViewTest, PaintIntersectsChildButNotGrandChildInRTL) {
717 ScopedRTL rtl;
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
741 // invalidation.
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));
746 v1->Reset();
747 v2->Reset();
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, 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
773 // invalidation.
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));
778 v1->Reset();
779 v2->Reset();
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, paint_area));
788 EXPECT_FALSE(v1->did_paint_);
789 EXPECT_FALSE(v2->did_paint_);
792 TEST_F(ViewTest, PaintIntersectsNoChildrenInRTL) {
793 ScopedRTL rtl;
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
817 // invalidation.
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));
822 v1->Reset();
823 v2->Reset();
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, 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
849 // invalidation.
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));
854 v1->Reset();
855 v2->Reset();
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, 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);
871 v1->Reset();
872 v2->Reset();
873 EXPECT_FALSE(v1->did_paint_);
874 EXPECT_FALSE(v2->did_paint_);
875 root_view->Paint(ui::PaintContext(list.get(), 1.f, paint_area));
876 EXPECT_TRUE(v1->did_paint_);
877 EXPECT_FALSE(v2->did_paint_);
880 TEST_F(ViewTest, PaintIntersectsOneChildInRTL) {
881 ScopedRTL rtl;
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
905 // invalidation.
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));
910 v1->Reset();
911 v2->Reset();
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, 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);
927 v1->Reset();
928 v2->Reset();
929 EXPECT_FALSE(v1->did_paint_);
930 EXPECT_FALSE(v2->did_paint_);
931 root_view->Paint(ui::PaintContext(list.get(), 1.f, 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
950 // invalidation.
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));
955 v1->Reset();
956 v2->Reset();
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, 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, paint_area));
979 EXPECT_TRUE(v1->did_paint_);
980 EXPECT_FALSE(v2->did_paint_);
983 v1->Reset();
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, paint_area));
994 EXPECT_TRUE(v1->did_paint_);
995 EXPECT_TRUE(v2->did_paint_);
999 // A derived class for testing paint.
1000 class TestPaintView : public TestView {
1001 public:
1002 TestPaintView() : TestView(), canvas_bounds_(gfx::Rect()) {}
1003 ~TestPaintView() override {}
1005 void OnPaint(gfx::Canvas* canvas) override {
1006 did_paint_ = true;
1007 // Get the bounds from the canvas this view paints to.
1008 EXPECT_TRUE(canvas->GetClipBounds(&canvas_bounds_));
1011 gfx::Rect canvas_bounds() const { return canvas_bounds_; }
1013 private:
1014 gfx::Rect canvas_bounds_;
1017 TEST_F(ViewTest, PaintLocalBounds) {
1018 ScopedTestPaintWidget widget(CreateParams(Widget::InitParams::TYPE_POPUP));
1019 View* root_view = widget->GetRootView();
1020 // Make |root_view|'s bounds larger so |v1|'s visible bounds is not clipped by
1021 // |root_view|.
1022 root_view->SetBounds(0, 0, 200, 200);
1024 TestPaintView* v1 = new TestPaintView;
1025 v1->SetPaintToLayer(true);
1027 // Set bounds for |v1| such that it has an offset to its parent and only part
1028 // of it is visible. The visible bounds does not intersect with |root_view|'s
1029 // bounds.
1030 v1->SetBounds(0, -1000, 100, 1100);
1031 root_view->AddChildView(v1);
1032 EXPECT_EQ(gfx::Rect(0, 0, 100, 1100), v1->GetLocalBounds());
1033 EXPECT_EQ(gfx::Rect(0, 1000, 100, 100), v1->GetVisibleBounds());
1035 scoped_refptr<cc::DisplayItemList> list =
1036 cc::DisplayItemList::Create(gfx::Rect(), cc::DisplayItemListSettings());
1037 ui::PaintContext context(list.get(), 1.f, gfx::Rect());
1039 v1->Paint(context);
1040 EXPECT_TRUE(v1->did_paint_);
1042 // Check that the canvas produced by |v1| for paint contains all of |v1|'s
1043 // visible bounds.
1044 EXPECT_TRUE(v1->canvas_bounds().Contains(v1->GetVisibleBounds()));
1047 void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
1048 scheduled_paint_rects_.push_back(rect);
1049 View::SchedulePaintInRect(rect);
1052 TEST_F(ViewTest, RemoveNotification) {
1053 ViewStorage* vs = ViewStorage::GetInstance();
1054 Widget* widget = new Widget;
1055 widget->Init(CreateParams(Widget::InitParams::TYPE_POPUP));
1056 View* root_view = widget->GetRootView();
1058 View* v1 = new View;
1059 int s1 = vs->CreateStorageID();
1060 vs->StoreView(s1, v1);
1061 root_view->AddChildView(v1);
1062 View* v11 = new View;
1063 int s11 = vs->CreateStorageID();
1064 vs->StoreView(s11, v11);
1065 v1->AddChildView(v11);
1066 View* v111 = new View;
1067 int s111 = vs->CreateStorageID();
1068 vs->StoreView(s111, v111);
1069 v11->AddChildView(v111);
1070 View* v112 = new View;
1071 int s112 = vs->CreateStorageID();
1072 vs->StoreView(s112, v112);
1073 v11->AddChildView(v112);
1074 View* v113 = new View;
1075 int s113 = vs->CreateStorageID();
1076 vs->StoreView(s113, v113);
1077 v11->AddChildView(v113);
1078 View* v1131 = new View;
1079 int s1131 = vs->CreateStorageID();
1080 vs->StoreView(s1131, v1131);
1081 v113->AddChildView(v1131);
1082 View* v12 = new View;
1083 int s12 = vs->CreateStorageID();
1084 vs->StoreView(s12, v12);
1085 v1->AddChildView(v12);
1087 View* v2 = new View;
1088 int s2 = vs->CreateStorageID();
1089 vs->StoreView(s2, v2);
1090 root_view->AddChildView(v2);
1091 View* v21 = new View;
1092 int s21 = vs->CreateStorageID();
1093 vs->StoreView(s21, v21);
1094 v2->AddChildView(v21);
1095 View* v211 = new View;
1096 int s211 = vs->CreateStorageID();
1097 vs->StoreView(s211, v211);
1098 v21->AddChildView(v211);
1100 size_t stored_views = vs->view_count();
1102 // Try removing a leaf view.
1103 v21->RemoveChildView(v211);
1104 EXPECT_EQ(stored_views - 1, vs->view_count());
1105 EXPECT_EQ(NULL, vs->RetrieveView(s211));
1106 delete v211; // We won't use this one anymore.
1108 // Now try removing a view with a hierarchy of depth 1.
1109 v11->RemoveChildView(v113);
1110 EXPECT_EQ(stored_views - 3, vs->view_count());
1111 EXPECT_EQ(NULL, vs->RetrieveView(s113));
1112 EXPECT_EQ(NULL, vs->RetrieveView(s1131));
1113 delete v113; // We won't use this one anymore.
1115 // Now remove even more.
1116 root_view->RemoveChildView(v1);
1117 EXPECT_EQ(NULL, vs->RetrieveView(s1));
1118 EXPECT_EQ(NULL, vs->RetrieveView(s11));
1119 EXPECT_EQ(NULL, vs->RetrieveView(s12));
1120 EXPECT_EQ(NULL, vs->RetrieveView(s111));
1121 EXPECT_EQ(NULL, vs->RetrieveView(s112));
1123 // Put v1 back for more tests.
1124 root_view->AddChildView(v1);
1125 vs->StoreView(s1, v1);
1127 // Synchronously closing the window deletes the view hierarchy, which should
1128 // remove all its views from ViewStorage.
1129 widget->CloseNow();
1130 EXPECT_EQ(stored_views - 10, vs->view_count());
1131 EXPECT_EQ(NULL, vs->RetrieveView(s1));
1132 EXPECT_EQ(NULL, vs->RetrieveView(s12));
1133 EXPECT_EQ(NULL, vs->RetrieveView(s11));
1134 EXPECT_EQ(NULL, vs->RetrieveView(s12));
1135 EXPECT_EQ(NULL, vs->RetrieveView(s21));
1136 EXPECT_EQ(NULL, vs->RetrieveView(s111));
1137 EXPECT_EQ(NULL, vs->RetrieveView(s112));
1140 namespace {
1142 void RotateCounterclockwise(gfx::Transform* transform) {
1143 transform->matrix().set3x3(0, -1, 0,
1144 1, 0, 0,
1145 0, 0, 1);
1148 void RotateClockwise(gfx::Transform* transform) {
1149 transform->matrix().set3x3( 0, 1, 0,
1150 -1, 0, 0,
1151 0, 0, 1);
1154 } // namespace
1156 // Tests the correctness of the rect-based targeting algorithm implemented in
1157 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
1158 // of rect-based targeting.
1159 TEST_F(ViewTest, GetEventHandlerForRect) {
1160 Widget* widget = new Widget;
1161 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1162 widget->Init(params);
1163 View* root_view = widget->GetRootView();
1164 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1166 // Have this hierarchy of views (the coordinates here are all in
1167 // the root view's coordinate space):
1168 // v1 (0, 0, 100, 100)
1169 // v2 (150, 0, 250, 100)
1170 // v3 (0, 200, 150, 100)
1171 // v31 (10, 210, 80, 80)
1172 // v32 (110, 210, 30, 80)
1173 // v4 (300, 200, 100, 100)
1174 // v41 (310, 210, 80, 80)
1175 // v411 (370, 275, 10, 5)
1176 // v5 (450, 197, 30, 36)
1177 // v51 (450, 200, 30, 30)
1179 // The coordinates used for SetBounds are in parent coordinates.
1181 TestView* v1 = new TestView;
1182 v1->SetBounds(0, 0, 100, 100);
1183 root_view->AddChildView(v1);
1185 TestView* v2 = new TestView;
1186 v2->SetBounds(150, 0, 250, 100);
1187 root_view->AddChildView(v2);
1189 TestView* v3 = new TestView;
1190 v3->SetBounds(0, 200, 150, 100);
1191 root_view->AddChildView(v3);
1193 TestView* v4 = new TestView;
1194 v4->SetBounds(300, 200, 100, 100);
1195 root_view->AddChildView(v4);
1197 TestView* v31 = new TestView;
1198 v31->SetBounds(10, 10, 80, 80);
1199 v3->AddChildView(v31);
1201 TestView* v32 = new TestView;
1202 v32->SetBounds(110, 10, 30, 80);
1203 v3->AddChildView(v32);
1205 TestView* v41 = new TestView;
1206 v41->SetBounds(10, 10, 80, 80);
1207 v4->AddChildView(v41);
1209 TestView* v411 = new TestView;
1210 v411->SetBounds(60, 65, 10, 5);
1211 v41->AddChildView(v411);
1213 TestView* v5 = new TestView;
1214 v5->SetBounds(450, 197, 30, 36);
1215 root_view->AddChildView(v5);
1217 TestView* v51 = new TestView;
1218 v51->SetBounds(0, 3, 30, 30);
1219 v5->AddChildView(v51);
1221 // |touch_rect| does not intersect any descendant view of |root_view|.
1222 gfx::Rect touch_rect(105, 105, 30, 45);
1223 View* result_view = root_view->GetEventHandlerForRect(touch_rect);
1224 EXPECT_EQ(root_view, result_view);
1225 result_view = NULL;
1227 // Covers |v1| by at least 60%.
1228 touch_rect.SetRect(15, 15, 100, 100);
1229 result_view = root_view->GetEventHandlerForRect(touch_rect);
1230 EXPECT_EQ(v1, result_view);
1231 result_view = NULL;
1233 // Intersects |v1| but does not cover it by at least 60%. The center
1234 // of |touch_rect| is within |v1|.
1235 touch_rect.SetRect(50, 50, 5, 10);
1236 result_view = root_view->GetEventHandlerForRect(touch_rect);
1237 EXPECT_EQ(v1, result_view);
1238 result_view = NULL;
1240 // Intersects |v1| but does not cover it by at least 60%. The center
1241 // of |touch_rect| is not within |v1|.
1242 touch_rect.SetRect(95, 96, 21, 22);
1243 result_view = root_view->GetEventHandlerForRect(touch_rect);
1244 EXPECT_EQ(root_view, result_view);
1245 result_view = NULL;
1247 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
1248 touch_rect.SetRect(95, 10, 300, 120);
1249 result_view = root_view->GetEventHandlerForRect(touch_rect);
1250 EXPECT_EQ(v2, result_view);
1251 result_view = NULL;
1253 // Covers both |v1| and |v2| by at least 60%, but the center point
1254 // of |touch_rect| is closer to the center point of |v2|.
1255 touch_rect.SetRect(20, 20, 400, 100);
1256 result_view = root_view->GetEventHandlerForRect(touch_rect);
1257 EXPECT_EQ(v2, result_view);
1258 result_view = NULL;
1260 // Covers both |v1| and |v2| by at least 60%, but the center point
1261 // of |touch_rect| is closer to the center point of |v1|.
1262 touch_rect.SetRect(-700, -15, 1050, 110);
1263 result_view = root_view->GetEventHandlerForRect(touch_rect);
1264 EXPECT_EQ(v1, result_view);
1265 result_view = NULL;
1267 // A mouse click within |v1| will target |v1|.
1268 touch_rect.SetRect(15, 15, 1, 1);
1269 result_view = root_view->GetEventHandlerForRect(touch_rect);
1270 EXPECT_EQ(v1, result_view);
1271 result_view = NULL;
1273 // Intersects |v3| and |v31| by at least 60% and the center point
1274 // of |touch_rect| is closer to the center point of |v31|.
1275 touch_rect.SetRect(0, 200, 110, 100);
1276 result_view = root_view->GetEventHandlerForRect(touch_rect);
1277 EXPECT_EQ(v31, result_view);
1278 result_view = NULL;
1280 // Intersects |v3| and |v31|, but neither by at least 60%. The
1281 // center point of |touch_rect| lies within |v31|.
1282 touch_rect.SetRect(80, 280, 15, 15);
1283 result_view = root_view->GetEventHandlerForRect(touch_rect);
1284 EXPECT_EQ(v31, result_view);
1285 result_view = NULL;
1287 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1288 // center point of |touch_rect| is closest to the center point
1289 // of |v32|.
1290 touch_rect.SetRect(0, 200, 200, 100);
1291 result_view = root_view->GetEventHandlerForRect(touch_rect);
1292 EXPECT_EQ(v32, result_view);
1293 result_view = NULL;
1295 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1296 // |v31| and |v32| by at least 60%. The center point of
1297 // |touch_rect| is closest to the center point of |v32|.
1298 touch_rect.SetRect(30, 225, 180, 115);
1299 result_view = root_view->GetEventHandlerForRect(touch_rect);
1300 EXPECT_EQ(v32, result_view);
1301 result_view = NULL;
1303 // A mouse click at the corner of |v3| will target |v3|.
1304 touch_rect.SetRect(0, 200, 1, 1);
1305 result_view = root_view->GetEventHandlerForRect(touch_rect);
1306 EXPECT_EQ(v3, result_view);
1307 result_view = NULL;
1309 // A mouse click within |v32| will target |v32|.
1310 touch_rect.SetRect(112, 211, 1, 1);
1311 result_view = root_view->GetEventHandlerForRect(touch_rect);
1312 EXPECT_EQ(v32, result_view);
1313 result_view = NULL;
1315 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1316 // The center point of |touch_rect| is equally close to
1317 // the center points of |v4| and |v41|.
1318 touch_rect.SetRect(310, 210, 80, 80);
1319 result_view = root_view->GetEventHandlerForRect(touch_rect);
1320 EXPECT_EQ(v41, result_view);
1321 result_view = NULL;
1323 // Intersects all of |v4|, |v41|, and |v411| but only covers
1324 // |v411| by at least 60%.
1325 touch_rect.SetRect(370, 275, 7, 5);
1326 result_view = root_view->GetEventHandlerForRect(touch_rect);
1327 EXPECT_EQ(v411, result_view);
1328 result_view = NULL;
1330 // Intersects |v4| and |v41| but covers neither by at least 60%.
1331 // The center point of |touch_rect| is equally close to the center
1332 // points of |v4| and |v41|.
1333 touch_rect.SetRect(345, 245, 7, 7);
1334 result_view = root_view->GetEventHandlerForRect(touch_rect);
1335 EXPECT_EQ(v41, result_view);
1336 result_view = NULL;
1338 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1339 // them by at least 60%. The center point of |touch_rect| lies
1340 // within |v411|.
1341 touch_rect.SetRect(368, 272, 4, 6);
1342 result_view = root_view->GetEventHandlerForRect(touch_rect);
1343 EXPECT_EQ(v411, result_view);
1344 result_view = NULL;
1346 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1347 // them by at least 60%. The center point of |touch_rect| lies
1348 // within |v41|.
1349 touch_rect.SetRect(365, 270, 7, 7);
1350 result_view = root_view->GetEventHandlerForRect(touch_rect);
1351 EXPECT_EQ(v41, result_view);
1352 result_view = NULL;
1354 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1355 // them by at least 60%. The center point of |touch_rect| lies
1356 // within |v4|.
1357 touch_rect.SetRect(205, 275, 200, 2);
1358 result_view = root_view->GetEventHandlerForRect(touch_rect);
1359 EXPECT_EQ(v4, result_view);
1360 result_view = NULL;
1362 // Intersects all of |v4|, |v41|, and |v411| but only covers
1363 // |v41| by at least 60%.
1364 touch_rect.SetRect(310, 210, 61, 66);
1365 result_view = root_view->GetEventHandlerForRect(touch_rect);
1366 EXPECT_EQ(v41, result_view);
1367 result_view = NULL;
1369 // A mouse click within |v411| will target |v411|.
1370 touch_rect.SetRect(372, 275, 1, 1);
1371 result_view = root_view->GetEventHandlerForRect(touch_rect);
1372 EXPECT_EQ(v411, result_view);
1373 result_view = NULL;
1375 // A mouse click within |v41| will target |v41|.
1376 touch_rect.SetRect(350, 215, 1, 1);
1377 result_view = root_view->GetEventHandlerForRect(touch_rect);
1378 EXPECT_EQ(v41, result_view);
1379 result_view = NULL;
1381 // Covers |v3|, |v4|, and all of their descendants by at
1382 // least 60%. The center point of |touch_rect| is closest
1383 // to the center point of |v32|.
1384 touch_rect.SetRect(0, 200, 400, 100);
1385 result_view = root_view->GetEventHandlerForRect(touch_rect);
1386 EXPECT_EQ(v32, result_view);
1387 result_view = NULL;
1389 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1390 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1391 // The center point of |touch_rect| is closest to the center
1392 // point of |root_view|.
1393 touch_rect.SetRect(110, 15, 375, 450);
1394 result_view = root_view->GetEventHandlerForRect(touch_rect);
1395 EXPECT_EQ(root_view, result_view);
1396 result_view = NULL;
1398 // Covers all views (except |v5| and |v51|) by at least 60%. The
1399 // center point of |touch_rect| is equally close to the center
1400 // points of |v2| and |v32|. One is not a descendant of the other,
1401 // so in this case the view selected is arbitrary (i.e.,
1402 // it depends only on the ordering of nodes in the views
1403 // hierarchy).
1404 touch_rect.SetRect(0, 0, 400, 300);
1405 result_view = root_view->GetEventHandlerForRect(touch_rect);
1406 EXPECT_EQ(v32, result_view);
1407 result_view = NULL;
1409 // Covers |v5| and |v51| by at least 60%, and the center point of
1410 // the touch is located within both views. Since both views share
1411 // the same center point, the child view should be selected.
1412 touch_rect.SetRect(440, 190, 40, 40);
1413 result_view = root_view->GetEventHandlerForRect(touch_rect);
1414 EXPECT_EQ(v51, result_view);
1415 result_view = NULL;
1417 // Covers |v5| and |v51| by at least 60%, but the center point of
1418 // the touch is not located within either view. Since both views
1419 // share the same center point, the child view should be selected.
1420 touch_rect.SetRect(455, 187, 60, 60);
1421 result_view = root_view->GetEventHandlerForRect(touch_rect);
1422 EXPECT_EQ(v51, result_view);
1423 result_view = NULL;
1425 // Covers neither |v5| nor |v51| by at least 60%, but the center
1426 // of the touch is located within |v51|.
1427 touch_rect.SetRect(450, 197, 10, 10);
1428 result_view = root_view->GetEventHandlerForRect(touch_rect);
1429 EXPECT_EQ(v51, result_view);
1430 result_view = NULL;
1432 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1433 // The center point is located outside of both views.
1434 touch_rect.SetRect(433, 180, 24, 24);
1435 result_view = root_view->GetEventHandlerForRect(touch_rect);
1436 EXPECT_EQ(root_view, result_view);
1437 result_view = NULL;
1439 // Only intersects |v5| but does not cover it by at least 60%. The
1440 // center point of the touch region is located within |v5|.
1441 touch_rect.SetRect(449, 196, 3, 3);
1442 result_view = root_view->GetEventHandlerForRect(touch_rect);
1443 EXPECT_EQ(v5, result_view);
1444 result_view = NULL;
1446 // A mouse click within |v5| (but not |v51|) should target |v5|.
1447 touch_rect.SetRect(462, 199, 1, 1);
1448 result_view = root_view->GetEventHandlerForRect(touch_rect);
1449 EXPECT_EQ(v5, result_view);
1450 result_view = NULL;
1452 // A mouse click |v5| and |v51| should target the child view.
1453 touch_rect.SetRect(452, 226, 1, 1);
1454 result_view = root_view->GetEventHandlerForRect(touch_rect);
1455 EXPECT_EQ(v51, result_view);
1456 result_view = NULL;
1458 // A mouse click on the center of |v5| and |v51| should target
1459 // the child view.
1460 touch_rect.SetRect(465, 215, 1, 1);
1461 result_view = root_view->GetEventHandlerForRect(touch_rect);
1462 EXPECT_EQ(v51, result_view);
1463 result_view = NULL;
1465 widget->CloseNow();
1468 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1469 // as expected when different views in the view hierarchy return false
1470 // when CanProcessEventsWithinSubtree() is called.
1471 TEST_F(ViewTest, CanProcessEventsWithinSubtree) {
1472 Widget* widget = new Widget;
1473 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1474 widget->Init(params);
1475 View* root_view = widget->GetRootView();
1476 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1478 // Have this hierarchy of views (the coords here are in the coordinate
1479 // space of the root view):
1480 // v (0, 0, 100, 100)
1481 // - v_child (0, 0, 20, 30)
1482 // - v_grandchild (5, 5, 5, 15)
1484 TestView* v = new TestView;
1485 v->SetBounds(0, 0, 100, 100);
1486 root_view->AddChildView(v);
1487 v->set_notify_enter_exit_on_child(true);
1489 TestView* v_child = new TestView;
1490 v_child->SetBounds(0, 0, 20, 30);
1491 v->AddChildView(v_child);
1493 TestView* v_grandchild = new TestView;
1494 v_grandchild->SetBounds(5, 5, 5, 15);
1495 v_child->AddChildView(v_grandchild);
1497 v->Reset();
1498 v_child->Reset();
1499 v_grandchild->Reset();
1501 // Define rects and points within the views in the hierarchy.
1502 gfx::Rect rect_in_v_grandchild(7, 7, 3, 3);
1503 gfx::Point point_in_v_grandchild(rect_in_v_grandchild.origin());
1504 gfx::Rect rect_in_v_child(12, 3, 5, 5);
1505 gfx::Point point_in_v_child(rect_in_v_child.origin());
1506 gfx::Rect rect_in_v(50, 50, 25, 30);
1507 gfx::Point point_in_v(rect_in_v.origin());
1509 // When all three views return true when CanProcessEventsWithinSubtree()
1510 // is called, targeting should behave as expected.
1512 View* result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1513 EXPECT_EQ(v_grandchild, result_view);
1514 result_view = NULL;
1515 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1516 EXPECT_EQ(v_grandchild, result_view);
1517 result_view = NULL;
1519 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1520 EXPECT_EQ(v_child, result_view);
1521 result_view = NULL;
1522 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1523 EXPECT_EQ(v_child, result_view);
1524 result_view = NULL;
1526 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1527 EXPECT_EQ(v, result_view);
1528 result_view = NULL;
1529 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1530 EXPECT_EQ(v, result_view);
1531 result_view = NULL;
1533 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1534 // is called, then |v_grandchild| cannot be returned as a target.
1536 v_grandchild->set_can_process_events_within_subtree(false);
1538 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1539 EXPECT_EQ(v_child, result_view);
1540 result_view = NULL;
1541 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1542 EXPECT_EQ(v_child, result_view);
1543 result_view = NULL;
1545 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1546 EXPECT_EQ(v_child, result_view);
1547 result_view = NULL;
1548 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1549 EXPECT_EQ(v_child, result_view);
1550 result_view = NULL;
1552 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1553 EXPECT_EQ(v, result_view);
1554 result_view = NULL;
1555 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1556 EXPECT_EQ(v, result_view);
1558 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1559 // is called, then NULL should be returned as a target if we call
1560 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
1561 // views tree. Note that the location must be in the coordinate space
1562 // of the root view (|v_grandchild| in this case), so use (1, 1).
1564 result_view = v_grandchild;
1565 result_view = v_grandchild->GetTooltipHandlerForPoint(gfx::Point(1, 1));
1566 EXPECT_EQ(NULL, result_view);
1567 result_view = NULL;
1569 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1570 // is called, then neither |v_child| nor |v_grandchild| can be returned
1571 // as a target (|v| should be returned as the target for each case).
1573 v_grandchild->Reset();
1574 v_child->set_can_process_events_within_subtree(false);
1576 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1577 EXPECT_EQ(v, result_view);
1578 result_view = NULL;
1579 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1580 EXPECT_EQ(v, result_view);
1581 result_view = NULL;
1583 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1584 EXPECT_EQ(v, result_view);
1585 result_view = NULL;
1586 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1587 EXPECT_EQ(v, result_view);
1588 result_view = NULL;
1590 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1591 EXPECT_EQ(v, result_view);
1592 result_view = NULL;
1593 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1594 EXPECT_EQ(v, result_view);
1595 result_view = NULL;
1597 // When |v| returns false when CanProcessEventsWithinSubtree()
1598 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1599 // as a target (|root_view| should be returned as the target for each case).
1601 v_child->Reset();
1602 v->set_can_process_events_within_subtree(false);
1604 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1605 EXPECT_EQ(root_view, result_view);
1606 result_view = NULL;
1607 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1608 EXPECT_EQ(root_view, result_view);
1609 result_view = NULL;
1611 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1612 EXPECT_EQ(root_view, result_view);
1613 result_view = NULL;
1614 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1615 EXPECT_EQ(root_view, result_view);
1616 result_view = NULL;
1618 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1619 EXPECT_EQ(root_view, result_view);
1620 result_view = NULL;
1621 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1622 EXPECT_EQ(root_view, result_view);
1624 widget->CloseNow();
1627 TEST_F(ViewTest, NotifyEnterExitOnChild) {
1628 Widget* widget = new Widget;
1629 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1630 widget->Init(params);
1631 View* root_view = widget->GetRootView();
1632 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1634 // Have this hierarchy of views (the coords here are in root coord):
1635 // v1 (0, 0, 100, 100)
1636 // - v11 (0, 0, 20, 30)
1637 // - v111 (5, 5, 5, 15)
1638 // - v12 (50, 10, 30, 90)
1639 // - v121 (60, 20, 10, 10)
1640 // v2 (105, 0, 100, 100)
1641 // - v21 (120, 10, 50, 20)
1643 TestView* v1 = new TestView;
1644 v1->SetBounds(0, 0, 100, 100);
1645 root_view->AddChildView(v1);
1646 v1->set_notify_enter_exit_on_child(true);
1648 TestView* v11 = new TestView;
1649 v11->SetBounds(0, 0, 20, 30);
1650 v1->AddChildView(v11);
1652 TestView* v111 = new TestView;
1653 v111->SetBounds(5, 5, 5, 15);
1654 v11->AddChildView(v111);
1656 TestView* v12 = new TestView;
1657 v12->SetBounds(50, 10, 30, 90);
1658 v1->AddChildView(v12);
1660 TestView* v121 = new TestView;
1661 v121->SetBounds(10, 10, 10, 10);
1662 v12->AddChildView(v121);
1664 TestView* v2 = new TestView;
1665 v2->SetBounds(105, 0, 100, 100);
1666 root_view->AddChildView(v2);
1668 TestView* v21 = new TestView;
1669 v21->SetBounds(15, 10, 50, 20);
1670 v2->AddChildView(v21);
1672 v1->Reset();
1673 v11->Reset();
1674 v111->Reset();
1675 v12->Reset();
1676 v121->Reset();
1677 v2->Reset();
1678 v21->Reset();
1680 // Move the mouse in v111.
1681 gfx::Point p1(6, 6);
1682 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, p1, p1, ui::EventTimeForNow(), 0, 0);
1683 root_view->OnMouseMoved(move1);
1684 EXPECT_TRUE(v111->received_mouse_enter_);
1685 EXPECT_FALSE(v11->last_mouse_event_type_);
1686 EXPECT_TRUE(v1->received_mouse_enter_);
1688 v111->Reset();
1689 v1->Reset();
1691 // Now, move into v121.
1692 gfx::Point p2(65, 21);
1693 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, p2, p2, ui::EventTimeForNow(), 0, 0);
1694 root_view->OnMouseMoved(move2);
1695 EXPECT_TRUE(v111->received_mouse_exit_);
1696 EXPECT_TRUE(v121->received_mouse_enter_);
1697 EXPECT_FALSE(v1->last_mouse_event_type_);
1699 v111->Reset();
1700 v121->Reset();
1702 // Now, move into v11.
1703 gfx::Point p3(1, 1);
1704 ui::MouseEvent move3(ui::ET_MOUSE_MOVED, p3, p3, ui::EventTimeForNow(), 0, 0);
1705 root_view->OnMouseMoved(move3);
1706 EXPECT_TRUE(v121->received_mouse_exit_);
1707 EXPECT_TRUE(v11->received_mouse_enter_);
1708 EXPECT_FALSE(v1->last_mouse_event_type_);
1710 v121->Reset();
1711 v11->Reset();
1713 // Move to v21.
1714 gfx::Point p4(121, 15);
1715 ui::MouseEvent move4(ui::ET_MOUSE_MOVED, p4, p4, ui::EventTimeForNow(), 0, 0);
1716 root_view->OnMouseMoved(move4);
1717 EXPECT_TRUE(v21->received_mouse_enter_);
1718 EXPECT_FALSE(v2->last_mouse_event_type_);
1719 EXPECT_TRUE(v11->received_mouse_exit_);
1720 EXPECT_TRUE(v1->received_mouse_exit_);
1722 v21->Reset();
1723 v11->Reset();
1724 v1->Reset();
1726 // Move to v1.
1727 gfx::Point p5(21, 0);
1728 ui::MouseEvent move5(ui::ET_MOUSE_MOVED, p5, p5, ui::EventTimeForNow(), 0, 0);
1729 root_view->OnMouseMoved(move5);
1730 EXPECT_TRUE(v21->received_mouse_exit_);
1731 EXPECT_TRUE(v1->received_mouse_enter_);
1733 v21->Reset();
1734 v1->Reset();
1736 // Now, move into v11.
1737 gfx::Point p6(15, 15);
1738 ui::MouseEvent mouse6(ui::ET_MOUSE_MOVED, p6, p6, ui::EventTimeForNow(), 0,
1740 root_view->OnMouseMoved(mouse6);
1741 EXPECT_TRUE(v11->received_mouse_enter_);
1742 EXPECT_FALSE(v1->last_mouse_event_type_);
1744 v11->Reset();
1745 v1->Reset();
1747 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1748 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1749 // when the mouse leaves v11.
1750 gfx::Point p7(21, 0);
1751 ui::MouseEvent mouse7(ui::ET_MOUSE_MOVED, p7, p7, ui::EventTimeForNow(), 0,
1753 root_view->OnMouseMoved(mouse7);
1754 EXPECT_TRUE(v11->received_mouse_exit_);
1755 EXPECT_FALSE(v1->received_mouse_enter_);
1757 widget->CloseNow();
1760 TEST_F(ViewTest, Textfield) {
1761 const base::string16 kText = ASCIIToUTF16(
1762 "Reality is that which, when you stop believing it, doesn't go away.");
1763 const base::string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!");
1765 Widget* widget = new Widget;
1766 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1767 params.bounds = gfx::Rect(0, 0, 100, 100);
1768 widget->Init(params);
1769 View* root_view = widget->GetRootView();
1771 Textfield* textfield = new Textfield();
1772 root_view->AddChildView(textfield);
1774 // Test setting, appending text.
1775 textfield->SetText(kText);
1776 EXPECT_EQ(kText, textfield->text());
1777 textfield->AppendText(kExtraText);
1778 EXPECT_EQ(kText + kExtraText, textfield->text());
1779 textfield->SetText(base::string16());
1780 EXPECT_TRUE(textfield->text().empty());
1782 // Test selection related methods.
1783 textfield->SetText(kText);
1784 EXPECT_TRUE(textfield->GetSelectedText().empty());
1785 textfield->SelectAll(false);
1786 EXPECT_EQ(kText, textfield->text());
1787 textfield->ClearSelection();
1788 EXPECT_TRUE(textfield->GetSelectedText().empty());
1790 widget->CloseNow();
1793 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1794 TEST_F(ViewTest, TextfieldCutCopyPaste) {
1795 const base::string16 kNormalText = ASCIIToUTF16("Normal");
1796 const base::string16 kReadOnlyText = ASCIIToUTF16("Read only");
1797 const base::string16 kPasswordText =
1798 ASCIIToUTF16("Password! ** Secret stuff **");
1800 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1802 Widget* widget = new Widget;
1803 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1804 params.bounds = gfx::Rect(0, 0, 100, 100);
1805 widget->Init(params);
1806 View* root_view = widget->GetRootView();
1808 Textfield* normal = new Textfield();
1809 Textfield* read_only = new Textfield();
1810 read_only->SetReadOnly(true);
1811 Textfield* password = new Textfield();
1812 password->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
1814 root_view->AddChildView(normal);
1815 root_view->AddChildView(read_only);
1816 root_view->AddChildView(password);
1818 normal->SetText(kNormalText);
1819 read_only->SetText(kReadOnlyText);
1820 password->SetText(kPasswordText);
1823 // Test cut.
1826 normal->SelectAll(false);
1827 normal->ExecuteCommand(IDS_APP_CUT);
1828 base::string16 result;
1829 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1830 EXPECT_EQ(kNormalText, result);
1831 normal->SetText(kNormalText); // Let's revert to the original content.
1833 read_only->SelectAll(false);
1834 read_only->ExecuteCommand(IDS_APP_CUT);
1835 result.clear();
1836 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1837 // Cut should have failed, so the clipboard content should not have changed.
1838 EXPECT_EQ(kNormalText, result);
1840 password->SelectAll(false);
1841 password->ExecuteCommand(IDS_APP_CUT);
1842 result.clear();
1843 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1844 // Cut should have failed, so the clipboard content should not have changed.
1845 EXPECT_EQ(kNormalText, result);
1848 // Test copy.
1851 // Start with |read_only| to observe a change in clipboard text.
1852 read_only->SelectAll(false);
1853 read_only->ExecuteCommand(IDS_APP_COPY);
1854 result.clear();
1855 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1856 EXPECT_EQ(kReadOnlyText, result);
1858 normal->SelectAll(false);
1859 normal->ExecuteCommand(IDS_APP_COPY);
1860 result.clear();
1861 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1862 EXPECT_EQ(kNormalText, result);
1864 password->SelectAll(false);
1865 password->ExecuteCommand(IDS_APP_COPY);
1866 result.clear();
1867 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1868 // Text cannot be copied from an obscured field; the clipboard won't change.
1869 EXPECT_EQ(kNormalText, result);
1872 // Test paste.
1875 // Attempting to paste kNormalText in a read-only text-field should fail.
1876 read_only->SelectAll(false);
1877 read_only->ExecuteCommand(IDS_APP_PASTE);
1878 EXPECT_EQ(kReadOnlyText, read_only->text());
1880 password->SelectAll(false);
1881 password->ExecuteCommand(IDS_APP_PASTE);
1882 EXPECT_EQ(kNormalText, password->text());
1884 // Copy from |read_only| to observe a change in the normal textfield text.
1885 read_only->SelectAll(false);
1886 read_only->ExecuteCommand(IDS_APP_COPY);
1887 normal->SelectAll(false);
1888 normal->ExecuteCommand(IDS_APP_PASTE);
1889 EXPECT_EQ(kReadOnlyText, normal->text());
1890 widget->CloseNow();
1893 ////////////////////////////////////////////////////////////////////////////////
1894 // Accelerators
1895 ////////////////////////////////////////////////////////////////////////////////
1896 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) {
1897 accelerator_count_map_[accelerator]++;
1898 return true;
1901 // TODO: these tests were initially commented out when getting aura to
1902 // run. Figure out if still valuable and either nuke or fix.
1903 #if 0
1904 TEST_F(ViewTest, ActivateAccelerator) {
1905 // Register a keyboard accelerator before the view is added to a window.
1906 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1907 TestView* view = new TestView();
1908 view->Reset();
1909 view->AddAccelerator(return_accelerator);
1910 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1912 // Create a window and add the view as its child.
1913 scoped_ptr<Widget> widget(new Widget);
1914 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1915 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1916 params.bounds = gfx::Rect(0, 0, 100, 100);
1917 widget->Init(params);
1918 View* root = widget->GetRootView();
1919 root->AddChildView(view);
1920 widget->Show();
1922 // Get the focus manager.
1923 FocusManager* focus_manager = widget->GetFocusManager();
1924 ASSERT_TRUE(focus_manager);
1926 // Hit the return key and see if it takes effect.
1927 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1928 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1930 // Hit the escape key. Nothing should happen.
1931 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE);
1932 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1933 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1934 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0);
1936 // Now register the escape key and hit it again.
1937 view->AddAccelerator(escape_accelerator);
1938 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1939 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1940 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1942 // Remove the return key accelerator.
1943 view->RemoveAccelerator(return_accelerator);
1944 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1945 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1946 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1948 // Add it again. Hit the return key and the escape key.
1949 view->AddAccelerator(return_accelerator);
1950 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1951 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1952 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1953 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1954 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1955 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1957 // Remove all the accelerators.
1958 view->ResetAccelerators();
1959 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1960 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1961 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1962 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1963 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1964 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1966 widget->CloseNow();
1969 TEST_F(ViewTest, HiddenViewWithAccelerator) {
1970 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1971 TestView* view = new TestView();
1972 view->Reset();
1973 view->AddAccelerator(return_accelerator);
1974 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1976 scoped_ptr<Widget> widget(new Widget);
1977 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1978 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1979 params.bounds = gfx::Rect(0, 0, 100, 100);
1980 widget->Init(params);
1981 View* root = widget->GetRootView();
1982 root->AddChildView(view);
1983 widget->Show();
1985 FocusManager* focus_manager = widget->GetFocusManager();
1986 ASSERT_TRUE(focus_manager);
1988 view->SetVisible(false);
1989 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1991 view->SetVisible(true);
1992 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1994 widget->CloseNow();
1997 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) {
1998 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1999 TestView* view = new TestView();
2000 view->Reset();
2001 view->AddAccelerator(return_accelerator);
2002 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
2004 scoped_ptr<Widget> widget(new Widget);
2005 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2006 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2007 params.bounds = gfx::Rect(0, 0, 100, 100);
2008 widget->Init(params);
2009 View* root = widget->GetRootView();
2010 root->AddChildView(view);
2012 FocusManager* focus_manager = widget->GetFocusManager();
2013 ASSERT_TRUE(focus_manager);
2015 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
2016 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
2018 widget->Show();
2019 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
2020 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
2022 widget->Hide();
2023 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
2024 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
2026 widget->CloseNow();
2029 ////////////////////////////////////////////////////////////////////////////////
2030 // Mouse-wheel message rerouting
2031 ////////////////////////////////////////////////////////////////////////////////
2032 class ScrollableTestView : public View {
2033 public:
2034 ScrollableTestView() { }
2036 virtual gfx::Size GetPreferredSize() {
2037 return gfx::Size(100, 10000);
2040 virtual void Layout() {
2041 SizeToPreferredSize();
2045 class TestViewWithControls : public View {
2046 public:
2047 TestViewWithControls() {
2048 text_field_ = new Textfield();
2049 AddChildView(text_field_);
2052 Textfield* text_field_;
2055 class SimpleWidgetDelegate : public WidgetDelegate {
2056 public:
2057 explicit SimpleWidgetDelegate(View* contents) : contents_(contents) { }
2059 virtual void DeleteDelegate() { delete this; }
2061 virtual View* GetContentsView() { return contents_; }
2063 virtual Widget* GetWidget() { return contents_->GetWidget(); }
2064 virtual const Widget* GetWidget() const { return contents_->GetWidget(); }
2066 private:
2067 View* contents_;
2070 // Tests that the mouse-wheel messages are correctly rerouted to the window
2071 // under the mouse.
2072 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
2073 // bot.
2074 // Note that this fails for a variety of reasons:
2075 // - focused view is apparently reset across window activations and never
2076 // properly restored
2077 // - this test depends on you not having any other window visible open under the
2078 // area that it opens the test windows. --beng
2079 TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) {
2080 TestViewWithControls* view_with_controls = new TestViewWithControls();
2081 Widget* window1 = Widget::CreateWindowWithBounds(
2082 new SimpleWidgetDelegate(view_with_controls),
2083 gfx::Rect(0, 0, 100, 100));
2084 window1->Show();
2085 ScrollView* scroll_view = new ScrollView();
2086 scroll_view->SetContents(new ScrollableTestView());
2087 Widget* window2 = Widget::CreateWindowWithBounds(
2088 new SimpleWidgetDelegate(scroll_view),
2089 gfx::Rect(200, 200, 100, 100));
2090 window2->Show();
2091 EXPECT_EQ(0, scroll_view->GetVisibleRect().y());
2093 // Make the window1 active, as this is what it would be in real-world.
2094 window1->Activate();
2096 // Let's send a mouse-wheel message to the different controls and check that
2097 // it is rerouted to the window under the mouse (effectively scrolling the
2098 // scroll-view).
2100 // First to the Window's HWND.
2101 ::SendMessage(view_with_controls->GetWidget()->GetNativeView(),
2102 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
2103 EXPECT_EQ(20, scroll_view->GetVisibleRect().y());
2105 window1->CloseNow();
2106 window2->CloseNow();
2108 #endif // 0
2110 ////////////////////////////////////////////////////////////////////////////////
2111 // Native view hierachy
2112 ////////////////////////////////////////////////////////////////////////////////
2113 class ToplevelWidgetObserverView : public View {
2114 public:
2115 ToplevelWidgetObserverView() : toplevel_(NULL) {
2117 ~ToplevelWidgetObserverView() override {}
2119 // View overrides:
2120 void ViewHierarchyChanged(
2121 const ViewHierarchyChangedDetails& details) override {
2122 if (details.is_add) {
2123 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
2124 } else {
2125 toplevel_ = NULL;
2128 void NativeViewHierarchyChanged() override {
2129 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
2132 Widget* toplevel() { return toplevel_; }
2134 private:
2135 Widget* toplevel_;
2137 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView);
2140 // No ReparentNativeView on Mac. See http://crbug.com/514920.
2141 #if defined(OS_MACOSX) && !defined(USE_AURA)
2142 #define MAYBE_NativeViewHierarchyChanged DISABLED_NativeViewHierarchyChanged
2143 #else
2144 #define MAYBE_NativeViewHierarchyChanged NativeViewHierarchyChanged
2145 #endif
2147 // Test that a view can track the current top level widget by overriding
2148 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
2149 TEST_F(ViewTest, MAYBE_NativeViewHierarchyChanged) {
2150 scoped_ptr<Widget> toplevel1(new Widget);
2151 Widget::InitParams toplevel1_params =
2152 CreateParams(Widget::InitParams::TYPE_POPUP);
2153 toplevel1_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2154 toplevel1->Init(toplevel1_params);
2156 scoped_ptr<Widget> toplevel2(new Widget);
2157 Widget::InitParams toplevel2_params =
2158 CreateParams(Widget::InitParams::TYPE_POPUP);
2159 toplevel2_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2160 toplevel2->Init(toplevel2_params);
2162 Widget* child = new Widget;
2163 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
2164 child_params.parent = toplevel1->GetNativeView();
2165 child->Init(child_params);
2167 ToplevelWidgetObserverView* observer_view =
2168 new ToplevelWidgetObserverView();
2169 EXPECT_EQ(NULL, observer_view->toplevel());
2171 child->SetContentsView(observer_view);
2172 EXPECT_EQ(toplevel1, observer_view->toplevel());
2174 Widget::ReparentNativeView(child->GetNativeView(),
2175 toplevel2->GetNativeView());
2176 EXPECT_EQ(toplevel2, observer_view->toplevel());
2178 observer_view->parent()->RemoveChildView(observer_view);
2179 EXPECT_EQ(NULL, observer_view->toplevel());
2181 // Make |observer_view| |child|'s contents view again so that it gets deleted
2182 // with the widget.
2183 child->SetContentsView(observer_view);
2186 ////////////////////////////////////////////////////////////////////////////////
2187 // Transformations
2188 ////////////////////////////////////////////////////////////////////////////////
2190 class TransformPaintView : public TestView {
2191 public:
2192 TransformPaintView() {}
2193 ~TransformPaintView() override {}
2195 void ClearScheduledPaintRect() {
2196 scheduled_paint_rect_ = gfx::Rect();
2199 gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
2201 // Overridden from View:
2202 void SchedulePaintInRect(const gfx::Rect& rect) override {
2203 gfx::Rect xrect = ConvertRectToParent(rect);
2204 scheduled_paint_rect_.Union(xrect);
2207 private:
2208 gfx::Rect scheduled_paint_rect_;
2210 DISALLOW_COPY_AND_ASSIGN(TransformPaintView);
2213 TEST_F(ViewTest, TransformPaint) {
2214 TransformPaintView* v1 = new TransformPaintView();
2215 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2217 TestView* v2 = new TestView();
2218 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2220 Widget* widget = new Widget;
2221 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2222 params.bounds = gfx::Rect(50, 50, 650, 650);
2223 widget->Init(params);
2224 widget->Show();
2225 View* root = widget->GetRootView();
2227 root->AddChildView(v1);
2228 v1->AddChildView(v2);
2230 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2231 v1->ClearScheduledPaintRect();
2232 v2->SchedulePaint();
2234 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
2236 // Rotate |v1| counter-clockwise.
2237 gfx::Transform transform;
2238 RotateCounterclockwise(&transform);
2239 transform.matrix().set(1, 3, 500.0);
2240 v1->SetTransform(transform);
2242 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2244 v1->ClearScheduledPaintRect();
2245 v2->SchedulePaint();
2247 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect());
2249 widget->CloseNow();
2252 TEST_F(ViewTest, TransformEvent) {
2253 TestView* v1 = new TestView();
2254 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2256 TestView* v2 = new TestView();
2257 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2259 Widget* widget = new Widget;
2260 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2261 params.bounds = gfx::Rect(50, 50, 650, 650);
2262 widget->Init(params);
2263 View* root = widget->GetRootView();
2265 root->AddChildView(v1);
2266 v1->AddChildView(v2);
2268 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2270 // Rotate |v1| counter-clockwise.
2271 gfx::Transform transform(v1->GetTransform());
2272 RotateCounterclockwise(&transform);
2273 transform.matrix().set(1, 3, 500.0);
2274 v1->SetTransform(transform);
2276 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2277 v1->Reset();
2278 v2->Reset();
2280 gfx::Point p1(110, 210);
2281 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
2282 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2283 root->OnMousePressed(pressed);
2284 EXPECT_EQ(0, v1->last_mouse_event_type_);
2285 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
2286 EXPECT_EQ(190, v2->location_.x());
2287 EXPECT_EQ(10, v2->location_.y());
2289 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
2290 ui::EventTimeForNow(), 0, 0);
2291 root->OnMouseReleased(released);
2293 // Now rotate |v2| inside |v1| clockwise.
2294 transform = v2->GetTransform();
2295 RotateClockwise(&transform);
2296 transform.matrix().set(0, 3, 100.f);
2297 v2->SetTransform(transform);
2299 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
2300 // (300, 400) in |root|.
2302 v1->Reset();
2303 v2->Reset();
2305 gfx::Point point2(110, 320);
2306 ui::MouseEvent p2(ui::ET_MOUSE_PRESSED, point2, point2, ui::EventTimeForNow(),
2307 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2308 root->OnMousePressed(p2);
2309 EXPECT_EQ(0, v1->last_mouse_event_type_);
2310 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
2311 EXPECT_EQ(10, v2->location_.x());
2312 EXPECT_EQ(20, v2->location_.y());
2314 root->OnMouseReleased(released);
2316 v1->SetTransform(gfx::Transform());
2317 v2->SetTransform(gfx::Transform());
2319 TestView* v3 = new TestView();
2320 v3->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
2321 v2->AddChildView(v3);
2323 // Rotate |v3| clockwise with respect to |v2|.
2324 transform = v1->GetTransform();
2325 RotateClockwise(&transform);
2326 transform.matrix().set(0, 3, 30.f);
2327 v3->SetTransform(transform);
2329 // Scale |v2| with respect to |v1| along both axis.
2330 transform = v2->GetTransform();
2331 transform.matrix().set(0, 0, 0.8f);
2332 transform.matrix().set(1, 1, 0.5f);
2333 v2->SetTransform(transform);
2335 // |v3| occupies (108, 105) to (132, 115) in |root|.
2337 v1->Reset();
2338 v2->Reset();
2339 v3->Reset();
2341 gfx::Point point(112, 110);
2342 ui::MouseEvent p3(ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
2343 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2344 root->OnMousePressed(p3);
2346 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
2347 EXPECT_EQ(10, v3->location_.x());
2348 EXPECT_EQ(25, v3->location_.y());
2350 root->OnMouseReleased(released);
2352 v1->SetTransform(gfx::Transform());
2353 v2->SetTransform(gfx::Transform());
2354 v3->SetTransform(gfx::Transform());
2356 v1->Reset();
2357 v2->Reset();
2358 v3->Reset();
2360 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
2361 transform = v3->GetTransform();
2362 RotateClockwise(&transform);
2363 transform.matrix().set(0, 3, 30.f);
2364 // Rotation sets some scaling transformation. Using SetScale would overwrite
2365 // that and pollute the rotation. So combine the scaling with the existing
2366 // transforamtion.
2367 gfx::Transform scale;
2368 scale.Scale(0.8f, 0.5f);
2369 transform.ConcatTransform(scale);
2370 v3->SetTransform(transform);
2372 // Translate |v2| with respect to |v1|.
2373 transform = v2->GetTransform();
2374 transform.matrix().set(0, 3, 10.f);
2375 transform.matrix().set(1, 3, 10.f);
2376 v2->SetTransform(transform);
2378 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2380 gfx::Point point3(124, 125);
2381 ui::MouseEvent p4(ui::ET_MOUSE_PRESSED, point3, point3, ui::EventTimeForNow(),
2382 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2383 root->OnMousePressed(p4);
2385 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
2386 EXPECT_EQ(10, v3->location_.x());
2387 EXPECT_EQ(25, v3->location_.y());
2389 root->OnMouseReleased(released);
2391 widget->CloseNow();
2394 TEST_F(ViewTest, TransformVisibleBound) {
2395 gfx::Rect viewport_bounds(0, 0, 100, 100);
2397 scoped_ptr<Widget> widget(new Widget);
2398 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2399 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2400 params.bounds = viewport_bounds;
2401 widget->Init(params);
2402 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2404 View* viewport = new View;
2405 widget->SetContentsView(viewport);
2406 View* contents = new View;
2407 viewport->AddChildView(contents);
2408 viewport->SetBoundsRect(viewport_bounds);
2409 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2411 View* child = new View;
2412 contents->AddChildView(child);
2413 child->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2414 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds());
2416 // Rotate |child| counter-clockwise
2417 gfx::Transform transform;
2418 RotateCounterclockwise(&transform);
2419 transform.matrix().set(1, 3, 50.f);
2420 child->SetTransform(transform);
2421 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds());
2423 widget->CloseNow();
2426 ////////////////////////////////////////////////////////////////////////////////
2427 // OnVisibleBoundsChanged()
2429 class VisibleBoundsView : public View {
2430 public:
2431 VisibleBoundsView() : received_notification_(false) {}
2432 ~VisibleBoundsView() override {}
2434 bool received_notification() const { return received_notification_; }
2435 void set_received_notification(bool received) {
2436 received_notification_ = received;
2439 private:
2440 // Overridden from View:
2441 bool GetNeedsNotificationWhenVisibleBoundsChange() const override {
2442 return true;
2444 void OnVisibleBoundsChanged() override { received_notification_ = true; }
2446 bool received_notification_;
2448 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView);
2451 TEST_F(ViewTest, OnVisibleBoundsChanged) {
2452 gfx::Rect viewport_bounds(0, 0, 100, 100);
2454 scoped_ptr<Widget> widget(new Widget);
2455 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2456 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2457 params.bounds = viewport_bounds;
2458 widget->Init(params);
2459 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2461 View* viewport = new View;
2462 widget->SetContentsView(viewport);
2463 View* contents = new View;
2464 viewport->AddChildView(contents);
2465 viewport->SetBoundsRect(viewport_bounds);
2466 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2468 // Create a view that cares about visible bounds notifications, and position
2469 // it just outside the visible bounds of the viewport.
2470 VisibleBoundsView* child = new VisibleBoundsView;
2471 contents->AddChildView(child);
2472 child->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2474 // The child bound should be fully clipped.
2475 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
2477 // Now scroll the contents, but not enough to make the child visible.
2478 contents->SetY(contents->y() - 1);
2480 // We should have received the notification since the visible bounds may have
2481 // changed (even though they didn't).
2482 EXPECT_TRUE(child->received_notification());
2483 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
2484 child->set_received_notification(false);
2486 // Now scroll the contents, this time by enough to make the child visible by
2487 // one pixel.
2488 contents->SetY(contents->y() - 10);
2489 EXPECT_TRUE(child->received_notification());
2490 EXPECT_EQ(1, child->GetVisibleBounds().height());
2491 child->set_received_notification(false);
2493 widget->CloseNow();
2496 TEST_F(ViewTest, SetBoundsPaint) {
2497 TestView top_view;
2498 TestView* child_view = new TestView;
2500 top_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2501 top_view.scheduled_paint_rects_.clear();
2502 child_view->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2503 top_view.AddChildView(child_view);
2505 top_view.scheduled_paint_rects_.clear();
2506 child_view->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2507 EXPECT_EQ(2U, top_view.scheduled_paint_rects_.size());
2509 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2510 gfx::Rect paint_rect = top_view.scheduled_paint_rects_[0];
2511 paint_rect.Union(top_view.scheduled_paint_rects_[1]);
2512 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect);
2515 // Assertions around painting and focus gain/lost.
2516 TEST_F(ViewTest, FocusBlurPaints) {
2517 TestView parent_view;
2518 TestView* child_view1 = new TestView; // Owned by |parent_view|.
2520 parent_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2522 child_view1->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2523 parent_view.AddChildView(child_view1);
2525 parent_view.scheduled_paint_rects_.clear();
2526 child_view1->scheduled_paint_rects_.clear();
2528 // Focus change shouldn't trigger paints.
2529 child_view1->DoFocus();
2531 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
2532 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
2534 child_view1->DoBlur();
2535 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
2536 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
2539 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2540 TEST_F(ViewTest, SetBoundsSameBoundsDoesntSchedulePaint) {
2541 TestView view;
2543 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2544 view.InvalidateLayout();
2545 view.scheduled_paint_rects_.clear();
2546 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2547 EXPECT_TRUE(view.scheduled_paint_rects_.empty());
2550 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2551 TEST_F(ViewTest, AddAndRemoveSchedulePaints) {
2552 gfx::Rect viewport_bounds(0, 0, 100, 100);
2554 // We have to put the View hierarchy into a Widget or no paints will be
2555 // scheduled.
2556 scoped_ptr<Widget> widget(new Widget);
2557 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2558 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2559 params.bounds = viewport_bounds;
2560 widget->Init(params);
2561 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2563 TestView* parent_view = new TestView;
2564 widget->SetContentsView(parent_view);
2565 parent_view->SetBoundsRect(viewport_bounds);
2566 parent_view->scheduled_paint_rects_.clear();
2568 View* child_view = new View;
2569 child_view->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2570 parent_view->AddChildView(child_view);
2571 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
2572 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
2574 parent_view->scheduled_paint_rects_.clear();
2575 parent_view->RemoveChildView(child_view);
2576 scoped_ptr<View> child_deleter(child_view);
2577 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
2578 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
2580 widget->CloseNow();
2583 // Tests conversion methods with a transform.
2584 TEST_F(ViewTest, ConversionsWithTransform) {
2585 TestView top_view;
2587 // View hierarchy used to test scale transforms.
2588 TestView* child = new TestView;
2589 TestView* child_child = new TestView;
2591 // View used to test a rotation transform.
2592 TestView* child_2 = new TestView;
2594 top_view.AddChildView(child);
2595 child->AddChildView(child_child);
2597 top_view.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2599 child->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2600 gfx::Transform transform;
2601 transform.Scale(3.0, 4.0);
2602 child->SetTransform(transform);
2604 child_child->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2605 transform.MakeIdentity();
2606 transform.Scale(5.0, 7.0);
2607 child_child->SetTransform(transform);
2609 top_view.AddChildView(child_2);
2610 child_2->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2611 transform.MakeIdentity();
2612 RotateClockwise(&transform);
2613 child_2->SetTransform(transform);
2615 // Sanity check to make sure basic transforms act as expected.
2617 gfx::Transform transform;
2618 transform.Translate(110.0, -110.0);
2619 transform.Scale(100.0, 55.0);
2620 transform.Translate(1.0, 1.0);
2622 // convert to a 3x3 matrix.
2623 const SkMatrix& matrix = transform.matrix();
2625 EXPECT_EQ(210, matrix.getTranslateX());
2626 EXPECT_EQ(-55, matrix.getTranslateY());
2627 EXPECT_EQ(100, matrix.getScaleX());
2628 EXPECT_EQ(55, matrix.getScaleY());
2629 EXPECT_EQ(0, matrix.getSkewX());
2630 EXPECT_EQ(0, matrix.getSkewY());
2634 gfx::Transform transform;
2635 transform.Translate(1.0, 1.0);
2636 gfx::Transform t2;
2637 t2.Scale(100.0, 55.0);
2638 gfx::Transform t3;
2639 t3.Translate(110.0, -110.0);
2640 transform.ConcatTransform(t2);
2641 transform.ConcatTransform(t3);
2643 // convert to a 3x3 matrix
2644 const SkMatrix& matrix = transform.matrix();
2646 EXPECT_EQ(210, matrix.getTranslateX());
2647 EXPECT_EQ(-55, matrix.getTranslateY());
2648 EXPECT_EQ(100, matrix.getScaleX());
2649 EXPECT_EQ(55, matrix.getScaleY());
2650 EXPECT_EQ(0, matrix.getSkewX());
2651 EXPECT_EQ(0, matrix.getSkewY());
2654 // Conversions from child->top and top->child.
2656 gfx::Point point(5, 5);
2657 View::ConvertPointToTarget(child, &top_view, &point);
2658 EXPECT_EQ(22, point.x());
2659 EXPECT_EQ(39, point.y());
2661 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2662 View::ConvertRectToTarget(child, &top_view, &rect);
2663 EXPECT_FLOAT_EQ(22.0f, rect.x());
2664 EXPECT_FLOAT_EQ(39.0f, rect.y());
2665 EXPECT_FLOAT_EQ(30.0f, rect.width());
2666 EXPECT_FLOAT_EQ(80.0f, rect.height());
2668 point.SetPoint(22, 39);
2669 View::ConvertPointToTarget(&top_view, child, &point);
2670 EXPECT_EQ(5, point.x());
2671 EXPECT_EQ(5, point.y());
2673 rect.SetRect(22.0f, 39.0f, 30.0f, 80.0f);
2674 View::ConvertRectToTarget(&top_view, child, &rect);
2675 EXPECT_FLOAT_EQ(5.0f, rect.x());
2676 EXPECT_FLOAT_EQ(5.0f, rect.y());
2677 EXPECT_FLOAT_EQ(10.0f, rect.width());
2678 EXPECT_FLOAT_EQ(20.0f, rect.height());
2681 // Conversions from child_child->top and top->child_child.
2683 gfx::Point point(5, 5);
2684 View::ConvertPointToTarget(child_child, &top_view, &point);
2685 EXPECT_EQ(133, point.x());
2686 EXPECT_EQ(211, point.y());
2688 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2689 View::ConvertRectToTarget(child_child, &top_view, &rect);
2690 EXPECT_FLOAT_EQ(133.0f, rect.x());
2691 EXPECT_FLOAT_EQ(211.0f, rect.y());
2692 EXPECT_FLOAT_EQ(150.0f, rect.width());
2693 EXPECT_FLOAT_EQ(560.0f, rect.height());
2695 point.SetPoint(133, 211);
2696 View::ConvertPointToTarget(&top_view, child_child, &point);
2697 EXPECT_EQ(5, point.x());
2698 EXPECT_EQ(5, point.y());
2700 rect.SetRect(133.0f, 211.0f, 150.0f, 560.0f);
2701 View::ConvertRectToTarget(&top_view, child_child, &rect);
2702 EXPECT_FLOAT_EQ(5.0f, rect.x());
2703 EXPECT_FLOAT_EQ(5.0f, rect.y());
2704 EXPECT_FLOAT_EQ(10.0f, rect.width());
2705 EXPECT_FLOAT_EQ(20.0f, rect.height());
2708 // Conversions from child_child->child and child->child_child
2710 gfx::Point point(5, 5);
2711 View::ConvertPointToTarget(child_child, child, &point);
2712 EXPECT_EQ(42, point.x());
2713 EXPECT_EQ(48, point.y());
2715 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2716 View::ConvertRectToTarget(child_child, child, &rect);
2717 EXPECT_FLOAT_EQ(42.0f, rect.x());
2718 EXPECT_FLOAT_EQ(48.0f, rect.y());
2719 EXPECT_FLOAT_EQ(50.0f, rect.width());
2720 EXPECT_FLOAT_EQ(140.0f, rect.height());
2722 point.SetPoint(42, 48);
2723 View::ConvertPointToTarget(child, child_child, &point);
2724 EXPECT_EQ(5, point.x());
2725 EXPECT_EQ(5, point.y());
2727 rect.SetRect(42.0f, 48.0f, 50.0f, 140.0f);
2728 View::ConvertRectToTarget(child, child_child, &rect);
2729 EXPECT_FLOAT_EQ(5.0f, rect.x());
2730 EXPECT_FLOAT_EQ(5.0f, rect.y());
2731 EXPECT_FLOAT_EQ(10.0f, rect.width());
2732 EXPECT_FLOAT_EQ(20.0f, rect.height());
2735 // Conversions from top_view to child with a value that should be negative.
2736 // This ensures we don't round up with negative numbers.
2738 gfx::Point point(6, 18);
2739 View::ConvertPointToTarget(&top_view, child, &point);
2740 EXPECT_EQ(-1, point.x());
2741 EXPECT_EQ(-1, point.y());
2743 float error = 0.01f;
2744 gfx::RectF rect(6.0f, 18.0f, 10.0f, 39.0f);
2745 View::ConvertRectToTarget(&top_view, child, &rect);
2746 EXPECT_NEAR(-0.33f, rect.x(), error);
2747 EXPECT_NEAR(-0.25f, rect.y(), error);
2748 EXPECT_NEAR(3.33f, rect.width(), error);
2749 EXPECT_NEAR(9.75f, rect.height(), error);
2752 // Rect conversions from top_view->child_2 and child_2->top_view.
2754 gfx::RectF rect(50.0f, 55.0f, 20.0f, 30.0f);
2755 View::ConvertRectToTarget(child_2, &top_view, &rect);
2756 EXPECT_FLOAT_EQ(615.0f, rect.x());
2757 EXPECT_FLOAT_EQ(775.0f, rect.y());
2758 EXPECT_FLOAT_EQ(30.0f, rect.width());
2759 EXPECT_FLOAT_EQ(20.0f, rect.height());
2761 rect.SetRect(615.0f, 775.0f, 30.0f, 20.0f);
2762 View::ConvertRectToTarget(&top_view, child_2, &rect);
2763 EXPECT_FLOAT_EQ(50.0f, rect.x());
2764 EXPECT_FLOAT_EQ(55.0f, rect.y());
2765 EXPECT_FLOAT_EQ(20.0f, rect.width());
2766 EXPECT_FLOAT_EQ(30.0f, rect.height());
2770 // Tests conversion methods to and from screen coordinates.
2771 TEST_F(ViewTest, ConversionsToFromScreen) {
2772 scoped_ptr<Widget> widget(new Widget);
2773 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2774 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2775 params.bounds = gfx::Rect(50, 50, 650, 650);
2776 widget->Init(params);
2778 View* child = new View;
2779 widget->GetRootView()->AddChildView(child);
2780 child->SetBounds(10, 10, 100, 200);
2781 gfx::Transform t;
2782 t.Scale(0.5, 0.5);
2783 child->SetTransform(t);
2785 gfx::Point point_in_screen(100, 90);
2786 gfx::Point point_in_child(80, 60);
2788 gfx::Point point = point_in_screen;
2789 View::ConvertPointFromScreen(child, &point);
2790 EXPECT_EQ(point_in_child.ToString(), point.ToString());
2792 View::ConvertPointToScreen(child, &point);
2793 EXPECT_EQ(point_in_screen.ToString(), point.ToString());
2796 // Tests conversion methods for rectangles.
2797 TEST_F(ViewTest, ConvertRectWithTransform) {
2798 scoped_ptr<Widget> widget(new Widget);
2799 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2800 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2801 params.bounds = gfx::Rect(50, 50, 650, 650);
2802 widget->Init(params);
2803 View* root = widget->GetRootView();
2805 TestView* v1 = new TestView;
2806 TestView* v2 = new TestView;
2807 root->AddChildView(v1);
2808 v1->AddChildView(v2);
2810 v1->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2811 v2->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2813 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2814 gfx::Rect rect(5, 5, 15, 40);
2815 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2->ConvertRectToParent(rect));
2816 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2->ConvertRectToWidget(rect));
2818 // Rotate |v2|
2819 gfx::Transform t2;
2820 RotateCounterclockwise(&t2);
2821 t2.matrix().set(1, 3, 100.f);
2822 v2->SetTransform(t2);
2824 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2825 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2826 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2->ConvertRectToWidget(rect));
2828 // Scale down |v1|
2829 gfx::Transform t1;
2830 t1.Scale(0.5, 0.5);
2831 v1->SetTransform(t1);
2833 // The rectangle should remain the same for |v1|.
2834 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2836 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2837 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2838 v2->ConvertRectToWidget(rect).ToString());
2840 widget->CloseNow();
2843 class ObserverView : public View {
2844 public:
2845 ObserverView();
2846 ~ObserverView() override;
2848 void ResetTestState();
2850 bool has_add_details() const { return has_add_details_; }
2851 bool has_remove_details() const { return has_remove_details_; }
2853 const ViewHierarchyChangedDetails& add_details() const {
2854 return add_details_;
2857 const ViewHierarchyChangedDetails& remove_details() const {
2858 return remove_details_;
2861 private:
2862 // View:
2863 void ViewHierarchyChanged(
2864 const ViewHierarchyChangedDetails& details) override;
2866 bool has_add_details_;
2867 bool has_remove_details_;
2868 ViewHierarchyChangedDetails add_details_;
2869 ViewHierarchyChangedDetails remove_details_;
2871 DISALLOW_COPY_AND_ASSIGN(ObserverView);
2874 ObserverView::ObserverView()
2875 : has_add_details_(false),
2876 has_remove_details_(false) {
2879 ObserverView::~ObserverView() {}
2881 void ObserverView::ResetTestState() {
2882 has_add_details_ = false;
2883 has_remove_details_ = false;
2884 add_details_ = ViewHierarchyChangedDetails();
2885 remove_details_ = ViewHierarchyChangedDetails();
2888 void ObserverView::ViewHierarchyChanged(
2889 const ViewHierarchyChangedDetails& details) {
2890 if (details.is_add) {
2891 has_add_details_ = true;
2892 add_details_ = details;
2893 } else {
2894 has_remove_details_ = true;
2895 remove_details_ = details;
2899 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2900 // a child view is added or removed to all the views in the hierarchy (up and
2901 // down).
2902 // The tree looks like this:
2903 // v1
2904 // +-- v2
2905 // +-- v3
2906 // +-- v4 (starts here, then get reparented to v1)
2907 TEST_F(ViewTest, ViewHierarchyChanged) {
2908 ObserverView v1;
2910 ObserverView* v3 = new ObserverView();
2912 // Add |v3| to |v2|.
2913 scoped_ptr<ObserverView> v2(new ObserverView());
2914 v2->AddChildView(v3);
2916 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2917 // notification.
2918 EXPECT_TRUE(v2->has_add_details());
2919 EXPECT_FALSE(v2->has_remove_details());
2920 EXPECT_EQ(v2.get(), v2->add_details().parent);
2921 EXPECT_EQ(v3, v2->add_details().child);
2922 EXPECT_EQ(NULL, v2->add_details().move_view);
2924 EXPECT_TRUE(v3->has_add_details());
2925 EXPECT_FALSE(v3->has_remove_details());
2926 EXPECT_EQ(v2.get(), v3->add_details().parent);
2927 EXPECT_EQ(v3, v3->add_details().child);
2928 EXPECT_EQ(NULL, v3->add_details().move_view);
2930 // Reset everything to the initial state.
2931 v2->ResetTestState();
2932 v3->ResetTestState();
2934 // Add |v2| to v1.
2935 v1.AddChildView(v2.get());
2937 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2938 // Make sure all the views (v1, v2, v3) received _that_ information.
2939 EXPECT_TRUE(v1.has_add_details());
2940 EXPECT_FALSE(v1.has_remove_details());
2941 EXPECT_EQ(&v1, v1.add_details().parent);
2942 EXPECT_EQ(v2.get(), v1.add_details().child);
2943 EXPECT_EQ(NULL, v1.add_details().move_view);
2945 EXPECT_TRUE(v2->has_add_details());
2946 EXPECT_FALSE(v2->has_remove_details());
2947 EXPECT_EQ(&v1, v2->add_details().parent);
2948 EXPECT_EQ(v2.get(), v2->add_details().child);
2949 EXPECT_EQ(NULL, v2->add_details().move_view);
2951 EXPECT_TRUE(v3->has_add_details());
2952 EXPECT_FALSE(v3->has_remove_details());
2953 EXPECT_EQ(&v1, v3->add_details().parent);
2954 EXPECT_EQ(v2.get(), v3->add_details().child);
2955 EXPECT_EQ(NULL, v3->add_details().move_view);
2957 // Reset everything to the initial state.
2958 v1.ResetTestState();
2959 v2->ResetTestState();
2960 v3->ResetTestState();
2962 // Remove |v2| from |v1|.
2963 v1.RemoveChildView(v2.get());
2965 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2966 // Make sure all the views (v1, v2, v3) received _that_ information.
2967 EXPECT_FALSE(v1.has_add_details());
2968 EXPECT_TRUE(v1.has_remove_details());
2969 EXPECT_EQ(&v1, v1.remove_details().parent);
2970 EXPECT_EQ(v2.get(), v1.remove_details().child);
2971 EXPECT_EQ(NULL, v1.remove_details().move_view);
2973 EXPECT_FALSE(v2->has_add_details());
2974 EXPECT_TRUE(v2->has_remove_details());
2975 EXPECT_EQ(&v1, v2->remove_details().parent);
2976 EXPECT_EQ(v2.get(), v2->remove_details().child);
2977 EXPECT_EQ(NULL, v2->remove_details().move_view);
2979 EXPECT_FALSE(v3->has_add_details());
2980 EXPECT_TRUE(v3->has_remove_details());
2981 EXPECT_EQ(&v1, v3->remove_details().parent);
2982 EXPECT_EQ(v3, v3->remove_details().child);
2983 EXPECT_EQ(NULL, v3->remove_details().move_view);
2985 // Verifies notifications when reparenting a view.
2986 ObserverView* v4 = new ObserverView();
2987 // Add |v4| to |v2|.
2988 v2->AddChildView(v4);
2990 // Reset everything to the initial state.
2991 v1.ResetTestState();
2992 v2->ResetTestState();
2993 v3->ResetTestState();
2994 v4->ResetTestState();
2996 // Reparent |v4| to |v1|.
2997 v1.AddChildView(v4);
2999 // Verifies that all views receive the correct information for all the child,
3000 // parent and move views.
3002 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
3003 EXPECT_TRUE(v1.has_add_details());
3004 EXPECT_FALSE(v1.has_remove_details());
3005 EXPECT_EQ(&v1, v1.add_details().parent);
3006 EXPECT_EQ(v4, v1.add_details().child);
3007 EXPECT_EQ(v2.get(), v1.add_details().move_view);
3009 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
3010 // parent.
3011 EXPECT_FALSE(v2->has_add_details());
3012 EXPECT_TRUE(v2->has_remove_details());
3013 EXPECT_EQ(v2.get(), v2->remove_details().parent);
3014 EXPECT_EQ(v4, v2->remove_details().child);
3015 EXPECT_EQ(&v1, v2->remove_details().move_view);
3017 // |v3| is not impacted by this operation, and hence receives no notification.
3018 EXPECT_FALSE(v3->has_add_details());
3019 EXPECT_FALSE(v3->has_remove_details());
3021 // |v4| is the reparented child, so it receives notifications for the remove
3022 // and then the add. |v2| is its old parent, |v1| is its new parent.
3023 EXPECT_TRUE(v4->has_remove_details());
3024 EXPECT_TRUE(v4->has_add_details());
3025 EXPECT_EQ(v2.get(), v4->remove_details().parent);
3026 EXPECT_EQ(&v1, v4->add_details().parent);
3027 EXPECT_EQ(v4, v4->add_details().child);
3028 EXPECT_EQ(v4, v4->remove_details().child);
3029 EXPECT_EQ(&v1, v4->remove_details().move_view);
3030 EXPECT_EQ(v2.get(), v4->add_details().move_view);
3033 // Verifies if the child views added under the root are all deleted when calling
3034 // RemoveAllChildViews.
3035 // The tree looks like this:
3036 // root
3037 // +-- child1
3038 // +-- foo
3039 // +-- bar0
3040 // +-- bar1
3041 // +-- bar2
3042 // +-- child2
3043 // +-- child3
3044 TEST_F(ViewTest, RemoveAllChildViews) {
3045 View root;
3047 View* child1 = new View;
3048 root.AddChildView(child1);
3050 for (int i = 0; i < 2; ++i)
3051 root.AddChildView(new View);
3053 View* foo = new View;
3054 child1->AddChildView(foo);
3056 // Add some nodes to |foo|.
3057 for (int i = 0; i < 3; ++i)
3058 foo->AddChildView(new View);
3060 EXPECT_EQ(3, root.child_count());
3061 EXPECT_EQ(1, child1->child_count());
3062 EXPECT_EQ(3, foo->child_count());
3064 // Now remove all child views from root.
3065 root.RemoveAllChildViews(true);
3067 EXPECT_EQ(0, root.child_count());
3068 EXPECT_FALSE(root.has_children());
3071 TEST_F(ViewTest, Contains) {
3072 View v1;
3073 View* v2 = new View;
3074 View* v3 = new View;
3076 v1.AddChildView(v2);
3077 v2->AddChildView(v3);
3079 EXPECT_FALSE(v1.Contains(NULL));
3080 EXPECT_TRUE(v1.Contains(&v1));
3081 EXPECT_TRUE(v1.Contains(v2));
3082 EXPECT_TRUE(v1.Contains(v3));
3084 EXPECT_FALSE(v2->Contains(NULL));
3085 EXPECT_TRUE(v2->Contains(v2));
3086 EXPECT_FALSE(v2->Contains(&v1));
3087 EXPECT_TRUE(v2->Contains(v3));
3089 EXPECT_FALSE(v3->Contains(NULL));
3090 EXPECT_TRUE(v3->Contains(v3));
3091 EXPECT_FALSE(v3->Contains(&v1));
3092 EXPECT_FALSE(v3->Contains(v2));
3095 // Verifies if GetIndexOf() returns the correct index for the specified child
3096 // view.
3097 // The tree looks like this:
3098 // root
3099 // +-- child1
3100 // +-- foo1
3101 // +-- child2
3102 TEST_F(ViewTest, GetIndexOf) {
3103 View root;
3105 View* child1 = new View;
3106 root.AddChildView(child1);
3108 View* child2 = new View;
3109 root.AddChildView(child2);
3111 View* foo1 = new View;
3112 child1->AddChildView(foo1);
3114 EXPECT_EQ(-1, root.GetIndexOf(NULL));
3115 EXPECT_EQ(-1, root.GetIndexOf(&root));
3116 EXPECT_EQ(0, root.GetIndexOf(child1));
3117 EXPECT_EQ(1, root.GetIndexOf(child2));
3118 EXPECT_EQ(-1, root.GetIndexOf(foo1));
3120 EXPECT_EQ(-1, child1->GetIndexOf(NULL));
3121 EXPECT_EQ(-1, child1->GetIndexOf(&root));
3122 EXPECT_EQ(-1, child1->GetIndexOf(child1));
3123 EXPECT_EQ(-1, child1->GetIndexOf(child2));
3124 EXPECT_EQ(0, child1->GetIndexOf(foo1));
3126 EXPECT_EQ(-1, child2->GetIndexOf(NULL));
3127 EXPECT_EQ(-1, child2->GetIndexOf(&root));
3128 EXPECT_EQ(-1, child2->GetIndexOf(child2));
3129 EXPECT_EQ(-1, child2->GetIndexOf(child1));
3130 EXPECT_EQ(-1, child2->GetIndexOf(foo1));
3133 // Verifies that the child views can be reordered correctly.
3134 TEST_F(ViewTest, ReorderChildren) {
3135 View root;
3137 View* child = new View();
3138 root.AddChildView(child);
3140 View* foo1 = new View();
3141 child->AddChildView(foo1);
3142 View* foo2 = new View();
3143 child->AddChildView(foo2);
3144 View* foo3 = new View();
3145 child->AddChildView(foo3);
3146 foo1->SetFocusable(true);
3147 foo2->SetFocusable(true);
3148 foo3->SetFocusable(true);
3150 ASSERT_EQ(0, child->GetIndexOf(foo1));
3151 ASSERT_EQ(1, child->GetIndexOf(foo2));
3152 ASSERT_EQ(2, child->GetIndexOf(foo3));
3153 ASSERT_EQ(foo2, foo1->GetNextFocusableView());
3154 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
3155 ASSERT_EQ(NULL, foo3->GetNextFocusableView());
3157 // Move |foo2| at the end.
3158 child->ReorderChildView(foo2, -1);
3159 ASSERT_EQ(0, child->GetIndexOf(foo1));
3160 ASSERT_EQ(1, child->GetIndexOf(foo3));
3161 ASSERT_EQ(2, child->GetIndexOf(foo2));
3162 ASSERT_EQ(foo3, foo1->GetNextFocusableView());
3163 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
3164 ASSERT_EQ(NULL, foo2->GetNextFocusableView());
3166 // Move |foo1| at the end.
3167 child->ReorderChildView(foo1, -1);
3168 ASSERT_EQ(0, child->GetIndexOf(foo3));
3169 ASSERT_EQ(1, child->GetIndexOf(foo2));
3170 ASSERT_EQ(2, child->GetIndexOf(foo1));
3171 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
3172 ASSERT_EQ(foo2, foo1->GetPreviousFocusableView());
3173 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
3174 ASSERT_EQ(foo1, foo2->GetNextFocusableView());
3176 // Move |foo2| to the front.
3177 child->ReorderChildView(foo2, 0);
3178 ASSERT_EQ(0, child->GetIndexOf(foo2));
3179 ASSERT_EQ(1, child->GetIndexOf(foo3));
3180 ASSERT_EQ(2, child->GetIndexOf(foo1));
3181 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
3182 ASSERT_EQ(foo3, foo1->GetPreviousFocusableView());
3183 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
3184 ASSERT_EQ(foo1, foo3->GetNextFocusableView());
3187 // Verifies that GetViewByID returns the correctly child view from the specified
3188 // ID.
3189 // The tree looks like this:
3190 // v1
3191 // +-- v2
3192 // +-- v3
3193 // +-- v4
3194 TEST_F(ViewTest, GetViewByID) {
3195 View v1;
3196 const int kV1ID = 1;
3197 v1.set_id(kV1ID);
3199 View v2;
3200 const int kV2ID = 2;
3201 v2.set_id(kV2ID);
3203 View v3;
3204 const int kV3ID = 3;
3205 v3.set_id(kV3ID);
3207 View v4;
3208 const int kV4ID = 4;
3209 v4.set_id(kV4ID);
3211 const int kV5ID = 5;
3213 v1.AddChildView(&v2);
3214 v2.AddChildView(&v3);
3215 v2.AddChildView(&v4);
3217 EXPECT_EQ(&v1, v1.GetViewByID(kV1ID));
3218 EXPECT_EQ(&v2, v1.GetViewByID(kV2ID));
3219 EXPECT_EQ(&v4, v1.GetViewByID(kV4ID));
3221 EXPECT_EQ(NULL, v1.GetViewByID(kV5ID)); // No V5 exists.
3222 EXPECT_EQ(NULL, v2.GetViewByID(kV1ID)); // It can get only from child views.
3224 const int kGroup = 1;
3225 v3.SetGroup(kGroup);
3226 v4.SetGroup(kGroup);
3228 View::Views views;
3229 v1.GetViewsInGroup(kGroup, &views);
3230 EXPECT_EQ(2U, views.size());
3232 View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3));
3233 EXPECT_NE(views.end(), i);
3235 i = std::find(views.begin(), views.end(), &v4);
3236 EXPECT_NE(views.end(), i);
3239 TEST_F(ViewTest, AddExistingChild) {
3240 View v1, v2, v3;
3242 v1.AddChildView(&v2);
3243 v1.AddChildView(&v3);
3244 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3245 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3247 // Check that there's no change in order when adding at same index.
3248 v1.AddChildViewAt(&v2, 0);
3249 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3250 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3251 v1.AddChildViewAt(&v3, 1);
3252 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3253 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3255 // Add it at a different index and check for change in order.
3256 v1.AddChildViewAt(&v2, 1);
3257 EXPECT_EQ(1, v1.GetIndexOf(&v2));
3258 EXPECT_EQ(0, v1.GetIndexOf(&v3));
3259 v1.AddChildViewAt(&v2, 0);
3260 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3261 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3263 // Check that calling |AddChildView()| does not change the order.
3264 v1.AddChildView(&v2);
3265 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3266 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3267 v1.AddChildView(&v3);
3268 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3269 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3272 ////////////////////////////////////////////////////////////////////////////////
3273 // FocusManager
3274 ////////////////////////////////////////////////////////////////////////////////
3276 // A widget that always claims to be active, regardless of its real activation
3277 // status.
3278 class ActiveWidget : public Widget {
3279 public:
3280 ActiveWidget() {}
3281 ~ActiveWidget() override {}
3283 bool IsActive() const override { return true; }
3285 private:
3286 DISALLOW_COPY_AND_ASSIGN(ActiveWidget);
3289 TEST_F(ViewTest, AdvanceFocusIfNecessaryForUnfocusableView) {
3290 // Create a widget with two views and give the first one focus.
3291 ActiveWidget widget;
3292 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3293 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3294 widget.Init(params);
3296 View* view1 = new View();
3297 view1->SetFocusable(true);
3298 widget.GetRootView()->AddChildView(view1);
3299 View* view2 = new View();
3300 view2->SetFocusable(true);
3301 widget.GetRootView()->AddChildView(view2);
3303 FocusManager* focus_manager = widget.GetFocusManager();
3304 ASSERT_TRUE(focus_manager);
3306 focus_manager->SetFocusedView(view1);
3307 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3309 // Disable the focused view and check if the next view gets focused.
3310 view1->SetEnabled(false);
3311 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3313 // Re-enable and re-focus.
3314 view1->SetEnabled(true);
3315 focus_manager->SetFocusedView(view1);
3316 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3318 // Hide the focused view and check it the next view gets focused.
3319 view1->SetVisible(false);
3320 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3322 // Re-show and re-focus.
3323 view1->SetVisible(true);
3324 focus_manager->SetFocusedView(view1);
3325 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3327 // Set the focused view as not focusable and check if the next view gets
3328 // focused.
3329 view1->SetFocusable(false);
3330 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3333 ////////////////////////////////////////////////////////////////////////////////
3334 // Layers
3335 ////////////////////////////////////////////////////////////////////////////////
3337 namespace {
3339 // Test implementation of LayerAnimator.
3340 class TestLayerAnimator : public ui::LayerAnimator {
3341 public:
3342 TestLayerAnimator();
3344 const gfx::Rect& last_bounds() const { return last_bounds_; }
3346 // LayerAnimator.
3347 void SetBounds(const gfx::Rect& bounds) override;
3349 protected:
3350 ~TestLayerAnimator() override {}
3352 private:
3353 gfx::Rect last_bounds_;
3355 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
3358 TestLayerAnimator::TestLayerAnimator()
3359 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
3362 void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) {
3363 last_bounds_ = bounds;
3366 } // namespace
3368 class ViewLayerTest : public ViewsTestBase {
3369 public:
3370 ViewLayerTest() : widget_(NULL) {}
3372 ~ViewLayerTest() override {}
3374 // Returns the Layer used by the RootView.
3375 ui::Layer* GetRootLayer() {
3376 return widget()->GetLayer();
3379 void SetUp() override {
3380 ViewTest::SetUp();
3381 widget_ = new Widget;
3382 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3383 params.bounds = gfx::Rect(50, 50, 200, 200);
3384 widget_->Init(params);
3385 widget_->Show();
3386 widget_->GetRootView()->SetBounds(0, 0, 200, 200);
3389 void TearDown() override {
3390 widget_->CloseNow();
3391 ViewsTestBase::TearDown();
3394 Widget* widget() { return widget_; }
3396 private:
3397 Widget* widget_;
3401 TEST_F(ViewLayerTest, LayerToggling) {
3402 // Because we lazily create textures the calls to DrawTree are necessary to
3403 // ensure we trigger creation of textures.
3404 ui::Layer* root_layer = widget()->GetLayer();
3405 View* content_view = new View;
3406 widget()->SetContentsView(content_view);
3408 // Create v1, give it a bounds and verify everything is set up correctly.
3409 View* v1 = new View;
3410 v1->SetPaintToLayer(true);
3411 EXPECT_TRUE(v1->layer() != NULL);
3412 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3413 content_view->AddChildView(v1);
3414 ASSERT_TRUE(v1->layer() != NULL);
3415 EXPECT_EQ(root_layer, v1->layer()->parent());
3416 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds());
3418 // Create v2 as a child of v1 and do basic assertion testing.
3419 View* v2 = new View;
3420 v1->AddChildView(v2);
3421 EXPECT_TRUE(v2->layer() == NULL);
3422 v2->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
3423 v2->SetPaintToLayer(true);
3424 ASSERT_TRUE(v2->layer() != NULL);
3425 EXPECT_EQ(v1->layer(), v2->layer()->parent());
3426 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
3428 // Turn off v1s layer. v2 should still have a layer but its parent should have
3429 // changed.
3430 v1->SetPaintToLayer(false);
3431 EXPECT_TRUE(v1->layer() == NULL);
3432 EXPECT_TRUE(v2->layer() != NULL);
3433 EXPECT_EQ(root_layer, v2->layer()->parent());
3434 ASSERT_EQ(1u, root_layer->children().size());
3435 EXPECT_EQ(root_layer->children()[0], v2->layer());
3436 // The bounds of the layer should have changed to be relative to the root view
3437 // now.
3438 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds());
3440 // Make v1 have a layer again and verify v2s layer is wired up correctly.
3441 gfx::Transform transform;
3442 transform.Scale(2.0, 2.0);
3443 v1->SetTransform(transform);
3444 EXPECT_TRUE(v1->layer() != NULL);
3445 EXPECT_TRUE(v2->layer() != NULL);
3446 EXPECT_EQ(root_layer, v1->layer()->parent());
3447 EXPECT_EQ(v1->layer(), v2->layer()->parent());
3448 ASSERT_EQ(1u, root_layer->children().size());
3449 EXPECT_EQ(root_layer->children()[0], v1->layer());
3450 ASSERT_EQ(1u, v1->layer()->children().size());
3451 EXPECT_EQ(v1->layer()->children()[0], v2->layer());
3452 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
3455 // Verifies turning on a layer wires up children correctly.
3456 TEST_F(ViewLayerTest, NestedLayerToggling) {
3457 View* content_view = new View;
3458 widget()->SetContentsView(content_view);
3460 // Create v1, give it a bounds and verify everything is set up correctly.
3461 View* v1 = new View;
3462 content_view->AddChildView(v1);
3463 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3465 View* v2 = new View;
3466 v1->AddChildView(v2);
3468 View* v3 = new View;
3469 v3->SetPaintToLayer(true);
3470 v2->AddChildView(v3);
3471 ASSERT_TRUE(v3->layer() != NULL);
3473 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
3475 v1->SetPaintToLayer(true);
3476 EXPECT_EQ(v1->layer(), v3->layer()->parent());
3479 TEST_F(ViewLayerTest, LayerAnimator) {
3480 View* content_view = new View;
3481 widget()->SetContentsView(content_view);
3483 View* v1 = new View;
3484 content_view->AddChildView(v1);
3485 v1->SetPaintToLayer(true);
3486 EXPECT_TRUE(v1->layer() != NULL);
3488 TestLayerAnimator* animator = new TestLayerAnimator();
3489 v1->layer()->SetAnimator(animator);
3491 gfx::Rect bounds(1, 2, 3, 4);
3492 v1->SetBoundsRect(bounds);
3493 EXPECT_EQ(bounds, animator->last_bounds());
3494 // TestLayerAnimator doesn't update the layer.
3495 EXPECT_NE(bounds, v1->layer()->bounds());
3498 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3499 // doesn't have a layer change.
3500 TEST_F(ViewLayerTest, BoundsChangeWithLayer) {
3501 View* content_view = new View;
3502 widget()->SetContentsView(content_view);
3504 View* v1 = new View;
3505 content_view->AddChildView(v1);
3506 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3508 View* v2 = new View;
3509 v2->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3510 v1->AddChildView(v2);
3511 v2->SetPaintToLayer(true);
3512 ASSERT_TRUE(v2->layer() != NULL);
3513 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
3515 v1->SetPosition(gfx::Point(25, 36));
3516 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
3518 v2->SetPosition(gfx::Point(11, 12));
3519 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
3521 // Bounds of the layer should change even if the view is not invisible.
3522 v1->SetVisible(false);
3523 v1->SetPosition(gfx::Point(20, 30));
3524 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
3526 v2->SetVisible(false);
3527 v2->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3528 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
3531 // Make sure layers are positioned correctly in RTL.
3532 TEST_F(ViewLayerTest, BoundInRTL) {
3533 ScopedRTL rtl;
3535 View* view = new View;
3536 widget()->SetContentsView(view);
3538 int content_width = view->width();
3540 // |v1| is initially not attached to anything. So its layer will have the same
3541 // bounds as the view.
3542 View* v1 = new View;
3543 v1->SetPaintToLayer(true);
3544 v1->SetBounds(10, 10, 20, 10);
3545 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3546 v1->layer()->bounds());
3548 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3549 // bounds.
3550 view->AddChildView(v1);
3551 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
3552 v1->layer()->bounds());
3553 gfx::Rect l1bounds = v1->layer()->bounds();
3555 // Now attach a View to the widget first, then create a layer for it. Make
3556 // sure the bounds are correct.
3557 View* v2 = new View;
3558 v2->SetBounds(50, 10, 30, 10);
3559 EXPECT_FALSE(v2->layer());
3560 view->AddChildView(v2);
3561 v2->SetPaintToLayer(true);
3562 EXPECT_EQ(gfx::Rect(content_width - 80, 10, 30, 10),
3563 v2->layer()->bounds());
3564 gfx::Rect l2bounds = v2->layer()->bounds();
3566 view->SetPaintToLayer(true);
3567 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3568 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3570 // Move one of the views. Make sure the layer is positioned correctly
3571 // afterwards.
3572 v1->SetBounds(v1->x() - 5, v1->y(), v1->width(), v1->height());
3573 l1bounds.set_x(l1bounds.x() + 5);
3574 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3576 view->SetPaintToLayer(false);
3577 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3578 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3580 // Move a view again.
3581 v2->SetBounds(v2->x() + 5, v2->y(), v2->width(), v2->height());
3582 l2bounds.set_x(l2bounds.x() - 5);
3583 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3586 // Make sure that resizing a parent in RTL correctly repositions its children.
3587 TEST_F(ViewLayerTest, ResizeParentInRTL) {
3588 ScopedRTL rtl;
3590 View* view = new View;
3591 widget()->SetContentsView(view);
3593 int content_width = view->width();
3595 // Create a paints-to-layer view |v1|.
3596 View* v1 = new View;
3597 v1->SetPaintToLayer(true);
3598 v1->SetBounds(10, 10, 20, 10);
3599 view->AddChildView(v1);
3600 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
3601 v1->layer()->bounds());
3603 // Attach a paints-to-layer child view to |v1|.
3604 View* v2 = new View;
3605 v2->SetPaintToLayer(true);
3606 v2->SetBounds(3, 5, 6, 4);
3607 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
3608 v2->layer()->bounds());
3609 v1->AddChildView(v2);
3610 // Check that |v2| now has RTL-appropriate bounds.
3611 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3612 v2->layer()->bounds());
3614 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
3615 View* v3 = new View;
3616 v3->SetBounds(1, 1, 18, 8);
3617 View* v4 = new View;
3618 v4->SetPaintToLayer(true);
3619 v4->SetBounds(2, 4, 6, 4);
3620 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
3621 v4->layer()->bounds());
3622 v3->AddChildView(v4);
3623 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
3624 v4->layer()->bounds());
3625 v1->AddChildView(v3);
3626 // Check that |v4| now has RTL-appropriate bounds.
3627 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3628 v4->layer()->bounds());
3630 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3631 // correctly to RTL-appropriate bounds.
3632 v1->SetSize(gfx::Size(30, 10));
3633 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3634 v2->layer()->bounds());
3635 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3636 v4->layer()->bounds());
3638 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3639 // to RTL-appropriate bounds.
3640 v3->SetBounds(2, 1, 12, 8);
3641 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3642 v4->layer()->bounds());
3645 // Makes sure a transform persists after toggling the visibility.
3646 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
3647 View* view = new View;
3648 gfx::Transform transform;
3649 transform.Scale(2.0, 2.0);
3650 view->SetTransform(transform);
3651 widget()->SetContentsView(view);
3652 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3654 view->SetVisible(false);
3655 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3657 view->SetVisible(true);
3658 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3661 // Verifies a transform persists after removing/adding a view with a transform.
3662 TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) {
3663 View* view = new View;
3664 gfx::Transform transform;
3665 transform.Scale(2.0, 2.0);
3666 view->SetTransform(transform);
3667 widget()->SetContentsView(view);
3668 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3669 ASSERT_TRUE(view->layer() != NULL);
3670 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3672 View* parent = view->parent();
3673 parent->RemoveChildView(view);
3674 parent->AddChildView(view);
3676 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3677 ASSERT_TRUE(view->layer() != NULL);
3678 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3681 // Makes sure that layer visibility is correct after toggling View visibility.
3682 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
3683 View* content_view = new View;
3684 widget()->SetContentsView(content_view);
3686 // The view isn't attached to a widget or a parent view yet. But it should
3687 // still have a layer, but the layer should not be attached to the root
3688 // layer.
3689 View* v1 = new View;
3690 v1->SetPaintToLayer(true);
3691 EXPECT_TRUE(v1->layer());
3692 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3693 v1->layer()));
3695 // Once the view is attached to a widget, its layer should be attached to the
3696 // root layer and visible.
3697 content_view->AddChildView(v1);
3698 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3699 v1->layer()));
3700 EXPECT_TRUE(v1->layer()->IsDrawn());
3702 v1->SetVisible(false);
3703 EXPECT_FALSE(v1->layer()->IsDrawn());
3705 v1->SetVisible(true);
3706 EXPECT_TRUE(v1->layer()->IsDrawn());
3708 widget()->Hide();
3709 EXPECT_FALSE(v1->layer()->IsDrawn());
3711 widget()->Show();
3712 EXPECT_TRUE(v1->layer()->IsDrawn());
3715 // Tests that the layers in the subtree are orphaned after a View is removed
3716 // from the parent.
3717 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
3718 View* content_view = new View;
3719 widget()->SetContentsView(content_view);
3721 View* v1 = new View;
3722 content_view->AddChildView(v1);
3724 View* v2 = new View;
3725 v1->AddChildView(v2);
3726 v2->SetPaintToLayer(true);
3727 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3728 v2->layer()));
3729 EXPECT_TRUE(v2->layer()->IsDrawn());
3731 content_view->RemoveChildView(v1);
3733 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3734 v2->layer()));
3736 // Reparent |v2|.
3737 content_view->AddChildView(v2);
3738 delete v1;
3739 v1 = NULL;
3740 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3741 v2->layer()));
3742 EXPECT_TRUE(v2->layer()->IsDrawn());
3745 class PaintTrackingView : public View {
3746 public:
3747 PaintTrackingView() : painted_(false) {
3750 bool painted() const { return painted_; }
3751 void set_painted(bool value) { painted_ = value; }
3753 void OnPaint(gfx::Canvas* canvas) override { painted_ = true; }
3755 private:
3756 bool painted_;
3758 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView);
3761 // Makes sure child views with layers aren't painted when paint starts at an
3762 // ancestor.
3763 TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) {
3764 PaintTrackingView* content_view = new PaintTrackingView;
3765 widget()->SetContentsView(content_view);
3766 content_view->SetPaintToLayer(true);
3767 GetRootLayer()->GetCompositor()->ScheduleDraw();
3768 ui::DrawWaiterForTest::WaitForCompositingEnded(
3769 GetRootLayer()->GetCompositor());
3770 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3771 content_view->set_painted(false);
3772 // content_view no longer has a dirty rect. Paint from the root and make sure
3773 // PaintTrackingView isn't painted.
3774 GetRootLayer()->GetCompositor()->ScheduleDraw();
3775 ui::DrawWaiterForTest::WaitForCompositingEnded(
3776 GetRootLayer()->GetCompositor());
3777 EXPECT_FALSE(content_view->painted());
3779 // Make content_view have a dirty rect, paint the layers and make sure
3780 // PaintTrackingView is painted.
3781 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3782 GetRootLayer()->GetCompositor()->ScheduleDraw();
3783 ui::DrawWaiterForTest::WaitForCompositingEnded(
3784 GetRootLayer()->GetCompositor());
3785 EXPECT_TRUE(content_view->painted());
3788 // Tests that the visibility of child layers are updated correctly when a View's
3789 // visibility changes.
3790 TEST_F(ViewLayerTest, VisibilityChildLayers) {
3791 View* v1 = new View;
3792 v1->SetPaintToLayer(true);
3793 widget()->SetContentsView(v1);
3795 View* v2 = new View;
3796 v1->AddChildView(v2);
3798 View* v3 = new View;
3799 v2->AddChildView(v3);
3800 v3->SetVisible(false);
3802 View* v4 = new View;
3803 v4->SetPaintToLayer(true);
3804 v3->AddChildView(v4);
3806 EXPECT_TRUE(v1->layer()->IsDrawn());
3807 EXPECT_FALSE(v4->layer()->IsDrawn());
3809 v2->SetVisible(false);
3810 EXPECT_TRUE(v1->layer()->IsDrawn());
3811 EXPECT_FALSE(v4->layer()->IsDrawn());
3813 v2->SetVisible(true);
3814 EXPECT_TRUE(v1->layer()->IsDrawn());
3815 EXPECT_FALSE(v4->layer()->IsDrawn());
3817 v2->SetVisible(false);
3818 EXPECT_TRUE(v1->layer()->IsDrawn());
3819 EXPECT_FALSE(v4->layer()->IsDrawn());
3820 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3822 v3->SetVisible(true);
3823 EXPECT_TRUE(v1->layer()->IsDrawn());
3824 EXPECT_FALSE(v4->layer()->IsDrawn());
3825 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3827 // Reparent |v3| to |v1|.
3828 v1->AddChildView(v3);
3829 EXPECT_TRUE(v1->layer()->IsDrawn());
3830 EXPECT_TRUE(v4->layer()->IsDrawn());
3831 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3834 // This test creates a random View tree, and then randomly reorders child views,
3835 // reparents views etc. Unrelated changes can appear to break this test. So
3836 // marking this as FLAKY.
3837 TEST_F(ViewLayerTest, DISABLED_ViewLayerTreesInSync) {
3838 View* content = new View;
3839 content->SetPaintToLayer(true);
3840 widget()->SetContentsView(content);
3841 widget()->Show();
3843 ConstructTree(content, 5);
3844 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3846 ScrambleTree(content);
3847 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3849 ScrambleTree(content);
3850 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3852 ScrambleTree(content);
3853 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3856 // Verifies when views are reordered the layer is also reordered. The widget is
3857 // providing the parent layer.
3858 TEST_F(ViewLayerTest, ReorderUnderWidget) {
3859 View* content = new View;
3860 widget()->SetContentsView(content);
3861 View* c1 = new View;
3862 c1->SetPaintToLayer(true);
3863 content->AddChildView(c1);
3864 View* c2 = new View;
3865 c2->SetPaintToLayer(true);
3866 content->AddChildView(c2);
3868 ui::Layer* parent_layer = c1->layer()->parent();
3869 ASSERT_TRUE(parent_layer);
3870 ASSERT_EQ(2u, parent_layer->children().size());
3871 EXPECT_EQ(c1->layer(), parent_layer->children()[0]);
3872 EXPECT_EQ(c2->layer(), parent_layer->children()[1]);
3874 // Move c1 to the front. The layers should have moved too.
3875 content->ReorderChildView(c1, -1);
3876 EXPECT_EQ(c1->layer(), parent_layer->children()[1]);
3877 EXPECT_EQ(c2->layer(), parent_layer->children()[0]);
3880 // Verifies that the layer of a view can be acquired properly.
3881 TEST_F(ViewLayerTest, AcquireLayer) {
3882 View* content = new View;
3883 widget()->SetContentsView(content);
3884 scoped_ptr<View> c1(new View);
3885 c1->SetPaintToLayer(true);
3886 EXPECT_TRUE(c1->layer());
3887 content->AddChildView(c1.get());
3889 scoped_ptr<ui::Layer> layer(c1->AcquireLayer());
3890 EXPECT_EQ(layer.get(), c1->layer());
3892 scoped_ptr<ui::Layer> layer2(c1->RecreateLayer());
3893 EXPECT_NE(c1->layer(), layer2.get());
3895 // Destroy view before destroying layer.
3896 c1.reset();
3899 // Verify the z-order of the layers as a result of calling RecreateLayer().
3900 TEST_F(ViewLayerTest, RecreateLayerZOrder) {
3901 scoped_ptr<View> v(new View());
3902 v->SetPaintToLayer(true);
3904 View* v1 = new View();
3905 v1->SetPaintToLayer(true);
3906 v->AddChildView(v1);
3907 View* v2 = new View();
3908 v2->SetPaintToLayer(true);
3909 v->AddChildView(v2);
3911 // Test the initial z-order.
3912 const std::vector<ui::Layer*>& child_layers_pre = v->layer()->children();
3913 ASSERT_EQ(2u, child_layers_pre.size());
3914 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3915 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3917 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3919 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3920 // for |v1| and |v2|.
3921 const std::vector<ui::Layer*>& child_layers_post = v->layer()->children();
3922 ASSERT_EQ(3u, child_layers_post.size());
3923 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3924 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3925 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3928 // Verify the z-order of the layers as a result of calling RecreateLayer when
3929 // the widget is the parent with the layer.
3930 TEST_F(ViewLayerTest, RecreateLayerZOrderWidgetParent) {
3931 View* v = new View();
3932 widget()->SetContentsView(v);
3934 View* v1 = new View();
3935 v1->SetPaintToLayer(true);
3936 v->AddChildView(v1);
3937 View* v2 = new View();
3938 v2->SetPaintToLayer(true);
3939 v->AddChildView(v2);
3941 ui::Layer* root_layer = GetRootLayer();
3943 // Test the initial z-order.
3944 const std::vector<ui::Layer*>& child_layers_pre = root_layer->children();
3945 ASSERT_EQ(2u, child_layers_pre.size());
3946 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3947 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3949 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3951 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3952 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3953 ASSERT_EQ(3u, child_layers_post.size());
3954 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3955 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3956 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3959 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3960 // a View.
3961 TEST_F(ViewLayerTest, RecreateLayerMovesNonViewChildren) {
3962 View v;
3963 v.SetPaintToLayer(true);
3964 View child;
3965 child.SetPaintToLayer(true);
3966 v.AddChildView(&child);
3967 ASSERT_TRUE(v.layer() != NULL);
3968 ASSERT_EQ(1u, v.layer()->children().size());
3969 EXPECT_EQ(v.layer()->children()[0], child.layer());
3971 ui::Layer layer(ui::LAYER_NOT_DRAWN);
3972 v.layer()->Add(&layer);
3973 v.layer()->StackAtBottom(&layer);
3975 scoped_ptr<ui::Layer> old_layer(v.RecreateLayer());
3977 // All children should be moved from old layer to new layer.
3978 ASSERT_TRUE(old_layer.get() != NULL);
3979 EXPECT_TRUE(old_layer->children().empty());
3981 // And new layer should have the two children.
3982 ASSERT_TRUE(v.layer() != NULL);
3983 ASSERT_EQ(2u, v.layer()->children().size());
3984 EXPECT_EQ(v.layer()->children()[0], &layer);
3985 EXPECT_EQ(v.layer()->children()[1], child.layer());
3988 namespace {
3990 std::string ToString(const gfx::Vector2dF& vector) {
3991 return base::StringPrintf("%.2f %0.2f", vector.x(), vector.y());
3994 } // namespace
3996 TEST_F(ViewLayerTest, SnapLayerToPixel) {
3997 View* v1 = new View;
3999 View* v11 = new View;
4000 v1->AddChildView(v11);
4002 widget()->SetContentsView(v1);
4004 const gfx::Size& size = GetRootLayer()->GetCompositor()->size();
4005 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f, size);
4007 v11->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
4008 v1->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
4009 v11->SetPaintToLayer(true);
4011 EXPECT_EQ("0.40 0.40", ToString(v11->layer()->subpixel_position_offset()));
4013 // Creating a layer in parent should update the child view's layer offset.
4014 v1->SetPaintToLayer(true);
4015 EXPECT_EQ("-0.20 -0.20", ToString(v1->layer()->subpixel_position_offset()));
4016 EXPECT_EQ("-0.20 -0.20", ToString(v11->layer()->subpixel_position_offset()));
4018 // DSF change should get propagated and update offsets.
4019 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f, size);
4020 EXPECT_EQ("0.33 0.33", ToString(v1->layer()->subpixel_position_offset()));
4021 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
4023 // Deleting parent's layer should update the child view's layer's offset.
4024 v1->SetPaintToLayer(false);
4025 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
4027 // Setting parent view should update the child view's layer's offset.
4028 v1->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
4029 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
4031 // Setting integral DSF should reset the offset.
4032 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f, size);
4033 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
4036 TEST_F(ViewTest, FocusableAssertions) {
4037 // View subclasses may change insets based on whether they are focusable,
4038 // which effects the preferred size. To avoid preferred size changing around
4039 // these Views need to key off the last value set to SetFocusable(), not
4040 // whether the View is focusable right now. For this reason it's important
4041 // that focusable() return the last value passed to SetFocusable and not
4042 // whether the View is focusable right now.
4043 TestView view;
4044 view.SetFocusable(true);
4045 EXPECT_TRUE(view.focusable());
4046 view.SetEnabled(false);
4047 EXPECT_TRUE(view.focusable());
4048 view.SetFocusable(false);
4049 EXPECT_FALSE(view.focusable());
4052 // Verifies when a view is deleted it is removed from ViewStorage.
4053 TEST_F(ViewTest, UpdateViewStorageOnDelete) {
4054 ViewStorage* view_storage = ViewStorage::GetInstance();
4055 const int storage_id = view_storage->CreateStorageID();
4057 View view;
4058 view_storage->StoreView(storage_id, &view);
4060 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL);
4063 ////////////////////////////////////////////////////////////////////////////////
4064 // NativeTheme
4065 ////////////////////////////////////////////////////////////////////////////////
4067 void TestView::OnNativeThemeChanged(const ui::NativeTheme* native_theme) {
4068 native_theme_ = native_theme;
4071 TEST_F(ViewTest, OnNativeThemeChanged) {
4072 TestView* test_view = new TestView();
4073 EXPECT_FALSE(test_view->native_theme_);
4074 TestView* test_view_child = new TestView();
4075 EXPECT_FALSE(test_view_child->native_theme_);
4077 // Child view added before the widget hierarchy exists should get the
4078 // new native theme notification.
4079 test_view->AddChildView(test_view_child);
4081 scoped_ptr<Widget> widget(new Widget);
4082 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
4083 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
4084 widget->Init(params);
4086 widget->GetRootView()->AddChildView(test_view);
4087 EXPECT_TRUE(test_view->native_theme_);
4088 EXPECT_EQ(widget->GetNativeTheme(), test_view->native_theme_);
4089 EXPECT_TRUE(test_view_child->native_theme_);
4090 EXPECT_EQ(widget->GetNativeTheme(), test_view_child->native_theme_);
4092 // Child view added after the widget hierarchy exists should also get the
4093 // notification.
4094 TestView* test_view_child_2 = new TestView();
4095 test_view->AddChildView(test_view_child_2);
4096 EXPECT_TRUE(test_view_child_2->native_theme_);
4097 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_);
4099 widget->CloseNow();
4102 } // namespace views