MacViews: Use Mac's "Constrained Window Button" style for Button::STYLE_BUTTON LabelB...
[chromium-blink-merge.git] / ui / views / view_unittest.cc
blobc88a57cfa795af57987550f5283aaf804e4069dc
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 // Test that a view can track the current top level widget by overriding
2141 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
2142 TEST_F(ViewTest, NativeViewHierarchyChanged) {
2143 scoped_ptr<Widget> toplevel1(new Widget);
2144 Widget::InitParams toplevel1_params =
2145 CreateParams(Widget::InitParams::TYPE_POPUP);
2146 toplevel1_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2147 toplevel1->Init(toplevel1_params);
2149 scoped_ptr<Widget> toplevel2(new Widget);
2150 Widget::InitParams toplevel2_params =
2151 CreateParams(Widget::InitParams::TYPE_POPUP);
2152 toplevel2_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2153 toplevel2->Init(toplevel2_params);
2155 Widget* child = new Widget;
2156 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
2157 child_params.parent = toplevel1->GetNativeView();
2158 child->Init(child_params);
2160 ToplevelWidgetObserverView* observer_view =
2161 new ToplevelWidgetObserverView();
2162 EXPECT_EQ(NULL, observer_view->toplevel());
2164 child->SetContentsView(observer_view);
2165 EXPECT_EQ(toplevel1, observer_view->toplevel());
2167 Widget::ReparentNativeView(child->GetNativeView(),
2168 toplevel2->GetNativeView());
2169 EXPECT_EQ(toplevel2, observer_view->toplevel());
2171 observer_view->parent()->RemoveChildView(observer_view);
2172 EXPECT_EQ(NULL, observer_view->toplevel());
2174 // Make |observer_view| |child|'s contents view again so that it gets deleted
2175 // with the widget.
2176 child->SetContentsView(observer_view);
2179 ////////////////////////////////////////////////////////////////////////////////
2180 // Transformations
2181 ////////////////////////////////////////////////////////////////////////////////
2183 class TransformPaintView : public TestView {
2184 public:
2185 TransformPaintView() {}
2186 ~TransformPaintView() override {}
2188 void ClearScheduledPaintRect() {
2189 scheduled_paint_rect_ = gfx::Rect();
2192 gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
2194 // Overridden from View:
2195 void SchedulePaintInRect(const gfx::Rect& rect) override {
2196 gfx::Rect xrect = ConvertRectToParent(rect);
2197 scheduled_paint_rect_.Union(xrect);
2200 private:
2201 gfx::Rect scheduled_paint_rect_;
2203 DISALLOW_COPY_AND_ASSIGN(TransformPaintView);
2206 TEST_F(ViewTest, TransformPaint) {
2207 TransformPaintView* v1 = new TransformPaintView();
2208 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2210 TestView* v2 = new TestView();
2211 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2213 Widget* widget = new Widget;
2214 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2215 params.bounds = gfx::Rect(50, 50, 650, 650);
2216 widget->Init(params);
2217 widget->Show();
2218 View* root = widget->GetRootView();
2220 root->AddChildView(v1);
2221 v1->AddChildView(v2);
2223 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2224 v1->ClearScheduledPaintRect();
2225 v2->SchedulePaint();
2227 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
2229 // Rotate |v1| counter-clockwise.
2230 gfx::Transform transform;
2231 RotateCounterclockwise(&transform);
2232 transform.matrix().set(1, 3, 500.0);
2233 v1->SetTransform(transform);
2235 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2237 v1->ClearScheduledPaintRect();
2238 v2->SchedulePaint();
2240 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect());
2242 widget->CloseNow();
2245 TEST_F(ViewTest, TransformEvent) {
2246 TestView* v1 = new TestView();
2247 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2249 TestView* v2 = new TestView();
2250 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2252 Widget* widget = new Widget;
2253 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2254 params.bounds = gfx::Rect(50, 50, 650, 650);
2255 widget->Init(params);
2256 View* root = widget->GetRootView();
2258 root->AddChildView(v1);
2259 v1->AddChildView(v2);
2261 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2263 // Rotate |v1| counter-clockwise.
2264 gfx::Transform transform(v1->GetTransform());
2265 RotateCounterclockwise(&transform);
2266 transform.matrix().set(1, 3, 500.0);
2267 v1->SetTransform(transform);
2269 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2270 v1->Reset();
2271 v2->Reset();
2273 gfx::Point p1(110, 210);
2274 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
2275 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2276 root->OnMousePressed(pressed);
2277 EXPECT_EQ(0, v1->last_mouse_event_type_);
2278 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
2279 EXPECT_EQ(190, v2->location_.x());
2280 EXPECT_EQ(10, v2->location_.y());
2282 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
2283 ui::EventTimeForNow(), 0, 0);
2284 root->OnMouseReleased(released);
2286 // Now rotate |v2| inside |v1| clockwise.
2287 transform = v2->GetTransform();
2288 RotateClockwise(&transform);
2289 transform.matrix().set(0, 3, 100.f);
2290 v2->SetTransform(transform);
2292 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
2293 // (300, 400) in |root|.
2295 v1->Reset();
2296 v2->Reset();
2298 gfx::Point point2(110, 320);
2299 ui::MouseEvent p2(ui::ET_MOUSE_PRESSED, point2, point2, ui::EventTimeForNow(),
2300 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2301 root->OnMousePressed(p2);
2302 EXPECT_EQ(0, v1->last_mouse_event_type_);
2303 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
2304 EXPECT_EQ(10, v2->location_.x());
2305 EXPECT_EQ(20, v2->location_.y());
2307 root->OnMouseReleased(released);
2309 v1->SetTransform(gfx::Transform());
2310 v2->SetTransform(gfx::Transform());
2312 TestView* v3 = new TestView();
2313 v3->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
2314 v2->AddChildView(v3);
2316 // Rotate |v3| clockwise with respect to |v2|.
2317 transform = v1->GetTransform();
2318 RotateClockwise(&transform);
2319 transform.matrix().set(0, 3, 30.f);
2320 v3->SetTransform(transform);
2322 // Scale |v2| with respect to |v1| along both axis.
2323 transform = v2->GetTransform();
2324 transform.matrix().set(0, 0, 0.8f);
2325 transform.matrix().set(1, 1, 0.5f);
2326 v2->SetTransform(transform);
2328 // |v3| occupies (108, 105) to (132, 115) in |root|.
2330 v1->Reset();
2331 v2->Reset();
2332 v3->Reset();
2334 gfx::Point point(112, 110);
2335 ui::MouseEvent p3(ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
2336 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2337 root->OnMousePressed(p3);
2339 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
2340 EXPECT_EQ(10, v3->location_.x());
2341 EXPECT_EQ(25, v3->location_.y());
2343 root->OnMouseReleased(released);
2345 v1->SetTransform(gfx::Transform());
2346 v2->SetTransform(gfx::Transform());
2347 v3->SetTransform(gfx::Transform());
2349 v1->Reset();
2350 v2->Reset();
2351 v3->Reset();
2353 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
2354 transform = v3->GetTransform();
2355 RotateClockwise(&transform);
2356 transform.matrix().set(0, 3, 30.f);
2357 // Rotation sets some scaling transformation. Using SetScale would overwrite
2358 // that and pollute the rotation. So combine the scaling with the existing
2359 // transforamtion.
2360 gfx::Transform scale;
2361 scale.Scale(0.8f, 0.5f);
2362 transform.ConcatTransform(scale);
2363 v3->SetTransform(transform);
2365 // Translate |v2| with respect to |v1|.
2366 transform = v2->GetTransform();
2367 transform.matrix().set(0, 3, 10.f);
2368 transform.matrix().set(1, 3, 10.f);
2369 v2->SetTransform(transform);
2371 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2373 gfx::Point point3(124, 125);
2374 ui::MouseEvent p4(ui::ET_MOUSE_PRESSED, point3, point3, ui::EventTimeForNow(),
2375 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2376 root->OnMousePressed(p4);
2378 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
2379 EXPECT_EQ(10, v3->location_.x());
2380 EXPECT_EQ(25, v3->location_.y());
2382 root->OnMouseReleased(released);
2384 widget->CloseNow();
2387 TEST_F(ViewTest, TransformVisibleBound) {
2388 gfx::Rect viewport_bounds(0, 0, 100, 100);
2390 scoped_ptr<Widget> widget(new Widget);
2391 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2392 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2393 params.bounds = viewport_bounds;
2394 widget->Init(params);
2395 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2397 View* viewport = new View;
2398 widget->SetContentsView(viewport);
2399 View* contents = new View;
2400 viewport->AddChildView(contents);
2401 viewport->SetBoundsRect(viewport_bounds);
2402 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2404 View* child = new View;
2405 contents->AddChildView(child);
2406 child->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2407 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds());
2409 // Rotate |child| counter-clockwise
2410 gfx::Transform transform;
2411 RotateCounterclockwise(&transform);
2412 transform.matrix().set(1, 3, 50.f);
2413 child->SetTransform(transform);
2414 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds());
2416 widget->CloseNow();
2419 ////////////////////////////////////////////////////////////////////////////////
2420 // OnVisibleBoundsChanged()
2422 class VisibleBoundsView : public View {
2423 public:
2424 VisibleBoundsView() : received_notification_(false) {}
2425 ~VisibleBoundsView() override {}
2427 bool received_notification() const { return received_notification_; }
2428 void set_received_notification(bool received) {
2429 received_notification_ = received;
2432 private:
2433 // Overridden from View:
2434 bool GetNeedsNotificationWhenVisibleBoundsChange() const override {
2435 return true;
2437 void OnVisibleBoundsChanged() override { received_notification_ = true; }
2439 bool received_notification_;
2441 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView);
2444 TEST_F(ViewTest, OnVisibleBoundsChanged) {
2445 gfx::Rect viewport_bounds(0, 0, 100, 100);
2447 scoped_ptr<Widget> widget(new Widget);
2448 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2449 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2450 params.bounds = viewport_bounds;
2451 widget->Init(params);
2452 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2454 View* viewport = new View;
2455 widget->SetContentsView(viewport);
2456 View* contents = new View;
2457 viewport->AddChildView(contents);
2458 viewport->SetBoundsRect(viewport_bounds);
2459 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2461 // Create a view that cares about visible bounds notifications, and position
2462 // it just outside the visible bounds of the viewport.
2463 VisibleBoundsView* child = new VisibleBoundsView;
2464 contents->AddChildView(child);
2465 child->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2467 // The child bound should be fully clipped.
2468 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
2470 // Now scroll the contents, but not enough to make the child visible.
2471 contents->SetY(contents->y() - 1);
2473 // We should have received the notification since the visible bounds may have
2474 // changed (even though they didn't).
2475 EXPECT_TRUE(child->received_notification());
2476 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
2477 child->set_received_notification(false);
2479 // Now scroll the contents, this time by enough to make the child visible by
2480 // one pixel.
2481 contents->SetY(contents->y() - 10);
2482 EXPECT_TRUE(child->received_notification());
2483 EXPECT_EQ(1, child->GetVisibleBounds().height());
2484 child->set_received_notification(false);
2486 widget->CloseNow();
2489 TEST_F(ViewTest, SetBoundsPaint) {
2490 TestView top_view;
2491 TestView* child_view = new TestView;
2493 top_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2494 top_view.scheduled_paint_rects_.clear();
2495 child_view->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2496 top_view.AddChildView(child_view);
2498 top_view.scheduled_paint_rects_.clear();
2499 child_view->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2500 EXPECT_EQ(2U, top_view.scheduled_paint_rects_.size());
2502 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2503 gfx::Rect paint_rect = top_view.scheduled_paint_rects_[0];
2504 paint_rect.Union(top_view.scheduled_paint_rects_[1]);
2505 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect);
2508 // Assertions around painting and focus gain/lost.
2509 TEST_F(ViewTest, FocusBlurPaints) {
2510 TestView parent_view;
2511 TestView* child_view1 = new TestView; // Owned by |parent_view|.
2513 parent_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2515 child_view1->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2516 parent_view.AddChildView(child_view1);
2518 parent_view.scheduled_paint_rects_.clear();
2519 child_view1->scheduled_paint_rects_.clear();
2521 // Focus change shouldn't trigger paints.
2522 child_view1->DoFocus();
2524 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
2525 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
2527 child_view1->DoBlur();
2528 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
2529 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
2532 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2533 TEST_F(ViewTest, SetBoundsSameBoundsDoesntSchedulePaint) {
2534 TestView view;
2536 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2537 view.InvalidateLayout();
2538 view.scheduled_paint_rects_.clear();
2539 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2540 EXPECT_TRUE(view.scheduled_paint_rects_.empty());
2543 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2544 TEST_F(ViewTest, AddAndRemoveSchedulePaints) {
2545 gfx::Rect viewport_bounds(0, 0, 100, 100);
2547 // We have to put the View hierarchy into a Widget or no paints will be
2548 // scheduled.
2549 scoped_ptr<Widget> widget(new Widget);
2550 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2551 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2552 params.bounds = viewport_bounds;
2553 widget->Init(params);
2554 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2556 TestView* parent_view = new TestView;
2557 widget->SetContentsView(parent_view);
2558 parent_view->SetBoundsRect(viewport_bounds);
2559 parent_view->scheduled_paint_rects_.clear();
2561 View* child_view = new View;
2562 child_view->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2563 parent_view->AddChildView(child_view);
2564 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
2565 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
2567 parent_view->scheduled_paint_rects_.clear();
2568 parent_view->RemoveChildView(child_view);
2569 scoped_ptr<View> child_deleter(child_view);
2570 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
2571 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
2573 widget->CloseNow();
2576 // Tests conversion methods with a transform.
2577 TEST_F(ViewTest, ConversionsWithTransform) {
2578 TestView top_view;
2580 // View hierarchy used to test scale transforms.
2581 TestView* child = new TestView;
2582 TestView* child_child = new TestView;
2584 // View used to test a rotation transform.
2585 TestView* child_2 = new TestView;
2587 top_view.AddChildView(child);
2588 child->AddChildView(child_child);
2590 top_view.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2592 child->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2593 gfx::Transform transform;
2594 transform.Scale(3.0, 4.0);
2595 child->SetTransform(transform);
2597 child_child->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2598 transform.MakeIdentity();
2599 transform.Scale(5.0, 7.0);
2600 child_child->SetTransform(transform);
2602 top_view.AddChildView(child_2);
2603 child_2->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2604 transform.MakeIdentity();
2605 RotateClockwise(&transform);
2606 child_2->SetTransform(transform);
2608 // Sanity check to make sure basic transforms act as expected.
2610 gfx::Transform transform;
2611 transform.Translate(110.0, -110.0);
2612 transform.Scale(100.0, 55.0);
2613 transform.Translate(1.0, 1.0);
2615 // convert to a 3x3 matrix.
2616 const SkMatrix& matrix = transform.matrix();
2618 EXPECT_EQ(210, matrix.getTranslateX());
2619 EXPECT_EQ(-55, matrix.getTranslateY());
2620 EXPECT_EQ(100, matrix.getScaleX());
2621 EXPECT_EQ(55, matrix.getScaleY());
2622 EXPECT_EQ(0, matrix.getSkewX());
2623 EXPECT_EQ(0, matrix.getSkewY());
2627 gfx::Transform transform;
2628 transform.Translate(1.0, 1.0);
2629 gfx::Transform t2;
2630 t2.Scale(100.0, 55.0);
2631 gfx::Transform t3;
2632 t3.Translate(110.0, -110.0);
2633 transform.ConcatTransform(t2);
2634 transform.ConcatTransform(t3);
2636 // convert to a 3x3 matrix
2637 const SkMatrix& matrix = transform.matrix();
2639 EXPECT_EQ(210, matrix.getTranslateX());
2640 EXPECT_EQ(-55, matrix.getTranslateY());
2641 EXPECT_EQ(100, matrix.getScaleX());
2642 EXPECT_EQ(55, matrix.getScaleY());
2643 EXPECT_EQ(0, matrix.getSkewX());
2644 EXPECT_EQ(0, matrix.getSkewY());
2647 // Conversions from child->top and top->child.
2649 gfx::Point point(5, 5);
2650 View::ConvertPointToTarget(child, &top_view, &point);
2651 EXPECT_EQ(22, point.x());
2652 EXPECT_EQ(39, point.y());
2654 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2655 View::ConvertRectToTarget(child, &top_view, &rect);
2656 EXPECT_FLOAT_EQ(22.0f, rect.x());
2657 EXPECT_FLOAT_EQ(39.0f, rect.y());
2658 EXPECT_FLOAT_EQ(30.0f, rect.width());
2659 EXPECT_FLOAT_EQ(80.0f, rect.height());
2661 point.SetPoint(22, 39);
2662 View::ConvertPointToTarget(&top_view, child, &point);
2663 EXPECT_EQ(5, point.x());
2664 EXPECT_EQ(5, point.y());
2666 rect.SetRect(22.0f, 39.0f, 30.0f, 80.0f);
2667 View::ConvertRectToTarget(&top_view, child, &rect);
2668 EXPECT_FLOAT_EQ(5.0f, rect.x());
2669 EXPECT_FLOAT_EQ(5.0f, rect.y());
2670 EXPECT_FLOAT_EQ(10.0f, rect.width());
2671 EXPECT_FLOAT_EQ(20.0f, rect.height());
2674 // Conversions from child_child->top and top->child_child.
2676 gfx::Point point(5, 5);
2677 View::ConvertPointToTarget(child_child, &top_view, &point);
2678 EXPECT_EQ(133, point.x());
2679 EXPECT_EQ(211, point.y());
2681 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2682 View::ConvertRectToTarget(child_child, &top_view, &rect);
2683 EXPECT_FLOAT_EQ(133.0f, rect.x());
2684 EXPECT_FLOAT_EQ(211.0f, rect.y());
2685 EXPECT_FLOAT_EQ(150.0f, rect.width());
2686 EXPECT_FLOAT_EQ(560.0f, rect.height());
2688 point.SetPoint(133, 211);
2689 View::ConvertPointToTarget(&top_view, child_child, &point);
2690 EXPECT_EQ(5, point.x());
2691 EXPECT_EQ(5, point.y());
2693 rect.SetRect(133.0f, 211.0f, 150.0f, 560.0f);
2694 View::ConvertRectToTarget(&top_view, child_child, &rect);
2695 EXPECT_FLOAT_EQ(5.0f, rect.x());
2696 EXPECT_FLOAT_EQ(5.0f, rect.y());
2697 EXPECT_FLOAT_EQ(10.0f, rect.width());
2698 EXPECT_FLOAT_EQ(20.0f, rect.height());
2701 // Conversions from child_child->child and child->child_child
2703 gfx::Point point(5, 5);
2704 View::ConvertPointToTarget(child_child, child, &point);
2705 EXPECT_EQ(42, point.x());
2706 EXPECT_EQ(48, point.y());
2708 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2709 View::ConvertRectToTarget(child_child, child, &rect);
2710 EXPECT_FLOAT_EQ(42.0f, rect.x());
2711 EXPECT_FLOAT_EQ(48.0f, rect.y());
2712 EXPECT_FLOAT_EQ(50.0f, rect.width());
2713 EXPECT_FLOAT_EQ(140.0f, rect.height());
2715 point.SetPoint(42, 48);
2716 View::ConvertPointToTarget(child, child_child, &point);
2717 EXPECT_EQ(5, point.x());
2718 EXPECT_EQ(5, point.y());
2720 rect.SetRect(42.0f, 48.0f, 50.0f, 140.0f);
2721 View::ConvertRectToTarget(child, child_child, &rect);
2722 EXPECT_FLOAT_EQ(5.0f, rect.x());
2723 EXPECT_FLOAT_EQ(5.0f, rect.y());
2724 EXPECT_FLOAT_EQ(10.0f, rect.width());
2725 EXPECT_FLOAT_EQ(20.0f, rect.height());
2728 // Conversions from top_view to child with a value that should be negative.
2729 // This ensures we don't round up with negative numbers.
2731 gfx::Point point(6, 18);
2732 View::ConvertPointToTarget(&top_view, child, &point);
2733 EXPECT_EQ(-1, point.x());
2734 EXPECT_EQ(-1, point.y());
2736 float error = 0.01f;
2737 gfx::RectF rect(6.0f, 18.0f, 10.0f, 39.0f);
2738 View::ConvertRectToTarget(&top_view, child, &rect);
2739 EXPECT_NEAR(-0.33f, rect.x(), error);
2740 EXPECT_NEAR(-0.25f, rect.y(), error);
2741 EXPECT_NEAR(3.33f, rect.width(), error);
2742 EXPECT_NEAR(9.75f, rect.height(), error);
2745 // Rect conversions from top_view->child_2 and child_2->top_view.
2747 gfx::RectF rect(50.0f, 55.0f, 20.0f, 30.0f);
2748 View::ConvertRectToTarget(child_2, &top_view, &rect);
2749 EXPECT_FLOAT_EQ(615.0f, rect.x());
2750 EXPECT_FLOAT_EQ(775.0f, rect.y());
2751 EXPECT_FLOAT_EQ(30.0f, rect.width());
2752 EXPECT_FLOAT_EQ(20.0f, rect.height());
2754 rect.SetRect(615.0f, 775.0f, 30.0f, 20.0f);
2755 View::ConvertRectToTarget(&top_view, child_2, &rect);
2756 EXPECT_FLOAT_EQ(50.0f, rect.x());
2757 EXPECT_FLOAT_EQ(55.0f, rect.y());
2758 EXPECT_FLOAT_EQ(20.0f, rect.width());
2759 EXPECT_FLOAT_EQ(30.0f, rect.height());
2763 // Tests conversion methods to and from screen coordinates.
2764 TEST_F(ViewTest, ConversionsToFromScreen) {
2765 scoped_ptr<Widget> widget(new Widget);
2766 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2767 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2768 params.bounds = gfx::Rect(50, 50, 650, 650);
2769 widget->Init(params);
2771 View* child = new View;
2772 widget->GetRootView()->AddChildView(child);
2773 child->SetBounds(10, 10, 100, 200);
2774 gfx::Transform t;
2775 t.Scale(0.5, 0.5);
2776 child->SetTransform(t);
2778 gfx::Point point_in_screen(100, 90);
2779 gfx::Point point_in_child(80, 60);
2781 gfx::Point point = point_in_screen;
2782 View::ConvertPointFromScreen(child, &point);
2783 EXPECT_EQ(point_in_child.ToString(), point.ToString());
2785 View::ConvertPointToScreen(child, &point);
2786 EXPECT_EQ(point_in_screen.ToString(), point.ToString());
2789 // Tests conversion methods for rectangles.
2790 TEST_F(ViewTest, ConvertRectWithTransform) {
2791 scoped_ptr<Widget> widget(new Widget);
2792 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2793 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2794 params.bounds = gfx::Rect(50, 50, 650, 650);
2795 widget->Init(params);
2796 View* root = widget->GetRootView();
2798 TestView* v1 = new TestView;
2799 TestView* v2 = new TestView;
2800 root->AddChildView(v1);
2801 v1->AddChildView(v2);
2803 v1->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2804 v2->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2806 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2807 gfx::Rect rect(5, 5, 15, 40);
2808 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2->ConvertRectToParent(rect));
2809 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2->ConvertRectToWidget(rect));
2811 // Rotate |v2|
2812 gfx::Transform t2;
2813 RotateCounterclockwise(&t2);
2814 t2.matrix().set(1, 3, 100.f);
2815 v2->SetTransform(t2);
2817 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2818 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2819 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2->ConvertRectToWidget(rect));
2821 // Scale down |v1|
2822 gfx::Transform t1;
2823 t1.Scale(0.5, 0.5);
2824 v1->SetTransform(t1);
2826 // The rectangle should remain the same for |v1|.
2827 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2829 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2830 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2831 v2->ConvertRectToWidget(rect).ToString());
2833 widget->CloseNow();
2836 class ObserverView : public View {
2837 public:
2838 ObserverView();
2839 ~ObserverView() override;
2841 void ResetTestState();
2843 bool has_add_details() const { return has_add_details_; }
2844 bool has_remove_details() const { return has_remove_details_; }
2846 const ViewHierarchyChangedDetails& add_details() const {
2847 return add_details_;
2850 const ViewHierarchyChangedDetails& remove_details() const {
2851 return remove_details_;
2854 private:
2855 // View:
2856 void ViewHierarchyChanged(
2857 const ViewHierarchyChangedDetails& details) override;
2859 bool has_add_details_;
2860 bool has_remove_details_;
2861 ViewHierarchyChangedDetails add_details_;
2862 ViewHierarchyChangedDetails remove_details_;
2864 DISALLOW_COPY_AND_ASSIGN(ObserverView);
2867 ObserverView::ObserverView()
2868 : has_add_details_(false),
2869 has_remove_details_(false) {
2872 ObserverView::~ObserverView() {}
2874 void ObserverView::ResetTestState() {
2875 has_add_details_ = false;
2876 has_remove_details_ = false;
2877 add_details_ = ViewHierarchyChangedDetails();
2878 remove_details_ = ViewHierarchyChangedDetails();
2881 void ObserverView::ViewHierarchyChanged(
2882 const ViewHierarchyChangedDetails& details) {
2883 if (details.is_add) {
2884 has_add_details_ = true;
2885 add_details_ = details;
2886 } else {
2887 has_remove_details_ = true;
2888 remove_details_ = details;
2892 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2893 // a child view is added or removed to all the views in the hierarchy (up and
2894 // down).
2895 // The tree looks like this:
2896 // v1
2897 // +-- v2
2898 // +-- v3
2899 // +-- v4 (starts here, then get reparented to v1)
2900 TEST_F(ViewTest, ViewHierarchyChanged) {
2901 ObserverView v1;
2903 ObserverView* v3 = new ObserverView();
2905 // Add |v3| to |v2|.
2906 scoped_ptr<ObserverView> v2(new ObserverView());
2907 v2->AddChildView(v3);
2909 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2910 // notification.
2911 EXPECT_TRUE(v2->has_add_details());
2912 EXPECT_FALSE(v2->has_remove_details());
2913 EXPECT_EQ(v2.get(), v2->add_details().parent);
2914 EXPECT_EQ(v3, v2->add_details().child);
2915 EXPECT_EQ(NULL, v2->add_details().move_view);
2917 EXPECT_TRUE(v3->has_add_details());
2918 EXPECT_FALSE(v3->has_remove_details());
2919 EXPECT_EQ(v2.get(), v3->add_details().parent);
2920 EXPECT_EQ(v3, v3->add_details().child);
2921 EXPECT_EQ(NULL, v3->add_details().move_view);
2923 // Reset everything to the initial state.
2924 v2->ResetTestState();
2925 v3->ResetTestState();
2927 // Add |v2| to v1.
2928 v1.AddChildView(v2.get());
2930 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2931 // Make sure all the views (v1, v2, v3) received _that_ information.
2932 EXPECT_TRUE(v1.has_add_details());
2933 EXPECT_FALSE(v1.has_remove_details());
2934 EXPECT_EQ(&v1, v1.add_details().parent);
2935 EXPECT_EQ(v2.get(), v1.add_details().child);
2936 EXPECT_EQ(NULL, v1.add_details().move_view);
2938 EXPECT_TRUE(v2->has_add_details());
2939 EXPECT_FALSE(v2->has_remove_details());
2940 EXPECT_EQ(&v1, v2->add_details().parent);
2941 EXPECT_EQ(v2.get(), v2->add_details().child);
2942 EXPECT_EQ(NULL, v2->add_details().move_view);
2944 EXPECT_TRUE(v3->has_add_details());
2945 EXPECT_FALSE(v3->has_remove_details());
2946 EXPECT_EQ(&v1, v3->add_details().parent);
2947 EXPECT_EQ(v2.get(), v3->add_details().child);
2948 EXPECT_EQ(NULL, v3->add_details().move_view);
2950 // Reset everything to the initial state.
2951 v1.ResetTestState();
2952 v2->ResetTestState();
2953 v3->ResetTestState();
2955 // Remove |v2| from |v1|.
2956 v1.RemoveChildView(v2.get());
2958 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2959 // Make sure all the views (v1, v2, v3) received _that_ information.
2960 EXPECT_FALSE(v1.has_add_details());
2961 EXPECT_TRUE(v1.has_remove_details());
2962 EXPECT_EQ(&v1, v1.remove_details().parent);
2963 EXPECT_EQ(v2.get(), v1.remove_details().child);
2964 EXPECT_EQ(NULL, v1.remove_details().move_view);
2966 EXPECT_FALSE(v2->has_add_details());
2967 EXPECT_TRUE(v2->has_remove_details());
2968 EXPECT_EQ(&v1, v2->remove_details().parent);
2969 EXPECT_EQ(v2.get(), v2->remove_details().child);
2970 EXPECT_EQ(NULL, v2->remove_details().move_view);
2972 EXPECT_FALSE(v3->has_add_details());
2973 EXPECT_TRUE(v3->has_remove_details());
2974 EXPECT_EQ(&v1, v3->remove_details().parent);
2975 EXPECT_EQ(v3, v3->remove_details().child);
2976 EXPECT_EQ(NULL, v3->remove_details().move_view);
2978 // Verifies notifications when reparenting a view.
2979 ObserverView* v4 = new ObserverView();
2980 // Add |v4| to |v2|.
2981 v2->AddChildView(v4);
2983 // Reset everything to the initial state.
2984 v1.ResetTestState();
2985 v2->ResetTestState();
2986 v3->ResetTestState();
2987 v4->ResetTestState();
2989 // Reparent |v4| to |v1|.
2990 v1.AddChildView(v4);
2992 // Verifies that all views receive the correct information for all the child,
2993 // parent and move views.
2995 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2996 EXPECT_TRUE(v1.has_add_details());
2997 EXPECT_FALSE(v1.has_remove_details());
2998 EXPECT_EQ(&v1, v1.add_details().parent);
2999 EXPECT_EQ(v4, v1.add_details().child);
3000 EXPECT_EQ(v2.get(), v1.add_details().move_view);
3002 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
3003 // parent.
3004 EXPECT_FALSE(v2->has_add_details());
3005 EXPECT_TRUE(v2->has_remove_details());
3006 EXPECT_EQ(v2.get(), v2->remove_details().parent);
3007 EXPECT_EQ(v4, v2->remove_details().child);
3008 EXPECT_EQ(&v1, v2->remove_details().move_view);
3010 // |v3| is not impacted by this operation, and hence receives no notification.
3011 EXPECT_FALSE(v3->has_add_details());
3012 EXPECT_FALSE(v3->has_remove_details());
3014 // |v4| is the reparented child, so it receives notifications for the remove
3015 // and then the add. |v2| is its old parent, |v1| is its new parent.
3016 EXPECT_TRUE(v4->has_remove_details());
3017 EXPECT_TRUE(v4->has_add_details());
3018 EXPECT_EQ(v2.get(), v4->remove_details().parent);
3019 EXPECT_EQ(&v1, v4->add_details().parent);
3020 EXPECT_EQ(v4, v4->add_details().child);
3021 EXPECT_EQ(v4, v4->remove_details().child);
3022 EXPECT_EQ(&v1, v4->remove_details().move_view);
3023 EXPECT_EQ(v2.get(), v4->add_details().move_view);
3026 // Verifies if the child views added under the root are all deleted when calling
3027 // RemoveAllChildViews.
3028 // The tree looks like this:
3029 // root
3030 // +-- child1
3031 // +-- foo
3032 // +-- bar0
3033 // +-- bar1
3034 // +-- bar2
3035 // +-- child2
3036 // +-- child3
3037 TEST_F(ViewTest, RemoveAllChildViews) {
3038 View root;
3040 View* child1 = new View;
3041 root.AddChildView(child1);
3043 for (int i = 0; i < 2; ++i)
3044 root.AddChildView(new View);
3046 View* foo = new View;
3047 child1->AddChildView(foo);
3049 // Add some nodes to |foo|.
3050 for (int i = 0; i < 3; ++i)
3051 foo->AddChildView(new View);
3053 EXPECT_EQ(3, root.child_count());
3054 EXPECT_EQ(1, child1->child_count());
3055 EXPECT_EQ(3, foo->child_count());
3057 // Now remove all child views from root.
3058 root.RemoveAllChildViews(true);
3060 EXPECT_EQ(0, root.child_count());
3061 EXPECT_FALSE(root.has_children());
3064 TEST_F(ViewTest, Contains) {
3065 View v1;
3066 View* v2 = new View;
3067 View* v3 = new View;
3069 v1.AddChildView(v2);
3070 v2->AddChildView(v3);
3072 EXPECT_FALSE(v1.Contains(NULL));
3073 EXPECT_TRUE(v1.Contains(&v1));
3074 EXPECT_TRUE(v1.Contains(v2));
3075 EXPECT_TRUE(v1.Contains(v3));
3077 EXPECT_FALSE(v2->Contains(NULL));
3078 EXPECT_TRUE(v2->Contains(v2));
3079 EXPECT_FALSE(v2->Contains(&v1));
3080 EXPECT_TRUE(v2->Contains(v3));
3082 EXPECT_FALSE(v3->Contains(NULL));
3083 EXPECT_TRUE(v3->Contains(v3));
3084 EXPECT_FALSE(v3->Contains(&v1));
3085 EXPECT_FALSE(v3->Contains(v2));
3088 // Verifies if GetIndexOf() returns the correct index for the specified child
3089 // view.
3090 // The tree looks like this:
3091 // root
3092 // +-- child1
3093 // +-- foo1
3094 // +-- child2
3095 TEST_F(ViewTest, GetIndexOf) {
3096 View root;
3098 View* child1 = new View;
3099 root.AddChildView(child1);
3101 View* child2 = new View;
3102 root.AddChildView(child2);
3104 View* foo1 = new View;
3105 child1->AddChildView(foo1);
3107 EXPECT_EQ(-1, root.GetIndexOf(NULL));
3108 EXPECT_EQ(-1, root.GetIndexOf(&root));
3109 EXPECT_EQ(0, root.GetIndexOf(child1));
3110 EXPECT_EQ(1, root.GetIndexOf(child2));
3111 EXPECT_EQ(-1, root.GetIndexOf(foo1));
3113 EXPECT_EQ(-1, child1->GetIndexOf(NULL));
3114 EXPECT_EQ(-1, child1->GetIndexOf(&root));
3115 EXPECT_EQ(-1, child1->GetIndexOf(child1));
3116 EXPECT_EQ(-1, child1->GetIndexOf(child2));
3117 EXPECT_EQ(0, child1->GetIndexOf(foo1));
3119 EXPECT_EQ(-1, child2->GetIndexOf(NULL));
3120 EXPECT_EQ(-1, child2->GetIndexOf(&root));
3121 EXPECT_EQ(-1, child2->GetIndexOf(child2));
3122 EXPECT_EQ(-1, child2->GetIndexOf(child1));
3123 EXPECT_EQ(-1, child2->GetIndexOf(foo1));
3126 // Verifies that the child views can be reordered correctly.
3127 TEST_F(ViewTest, ReorderChildren) {
3128 View root;
3130 View* child = new View();
3131 root.AddChildView(child);
3133 View* foo1 = new View();
3134 child->AddChildView(foo1);
3135 View* foo2 = new View();
3136 child->AddChildView(foo2);
3137 View* foo3 = new View();
3138 child->AddChildView(foo3);
3139 foo1->SetFocusable(true);
3140 foo2->SetFocusable(true);
3141 foo3->SetFocusable(true);
3143 ASSERT_EQ(0, child->GetIndexOf(foo1));
3144 ASSERT_EQ(1, child->GetIndexOf(foo2));
3145 ASSERT_EQ(2, child->GetIndexOf(foo3));
3146 ASSERT_EQ(foo2, foo1->GetNextFocusableView());
3147 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
3148 ASSERT_EQ(NULL, foo3->GetNextFocusableView());
3150 // Move |foo2| at the end.
3151 child->ReorderChildView(foo2, -1);
3152 ASSERT_EQ(0, child->GetIndexOf(foo1));
3153 ASSERT_EQ(1, child->GetIndexOf(foo3));
3154 ASSERT_EQ(2, child->GetIndexOf(foo2));
3155 ASSERT_EQ(foo3, foo1->GetNextFocusableView());
3156 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
3157 ASSERT_EQ(NULL, foo2->GetNextFocusableView());
3159 // Move |foo1| at the end.
3160 child->ReorderChildView(foo1, -1);
3161 ASSERT_EQ(0, child->GetIndexOf(foo3));
3162 ASSERT_EQ(1, child->GetIndexOf(foo2));
3163 ASSERT_EQ(2, child->GetIndexOf(foo1));
3164 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
3165 ASSERT_EQ(foo2, foo1->GetPreviousFocusableView());
3166 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
3167 ASSERT_EQ(foo1, foo2->GetNextFocusableView());
3169 // Move |foo2| to the front.
3170 child->ReorderChildView(foo2, 0);
3171 ASSERT_EQ(0, child->GetIndexOf(foo2));
3172 ASSERT_EQ(1, child->GetIndexOf(foo3));
3173 ASSERT_EQ(2, child->GetIndexOf(foo1));
3174 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
3175 ASSERT_EQ(foo3, foo1->GetPreviousFocusableView());
3176 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
3177 ASSERT_EQ(foo1, foo3->GetNextFocusableView());
3180 // Verifies that GetViewByID returns the correctly child view from the specified
3181 // ID.
3182 // The tree looks like this:
3183 // v1
3184 // +-- v2
3185 // +-- v3
3186 // +-- v4
3187 TEST_F(ViewTest, GetViewByID) {
3188 View v1;
3189 const int kV1ID = 1;
3190 v1.set_id(kV1ID);
3192 View v2;
3193 const int kV2ID = 2;
3194 v2.set_id(kV2ID);
3196 View v3;
3197 const int kV3ID = 3;
3198 v3.set_id(kV3ID);
3200 View v4;
3201 const int kV4ID = 4;
3202 v4.set_id(kV4ID);
3204 const int kV5ID = 5;
3206 v1.AddChildView(&v2);
3207 v2.AddChildView(&v3);
3208 v2.AddChildView(&v4);
3210 EXPECT_EQ(&v1, v1.GetViewByID(kV1ID));
3211 EXPECT_EQ(&v2, v1.GetViewByID(kV2ID));
3212 EXPECT_EQ(&v4, v1.GetViewByID(kV4ID));
3214 EXPECT_EQ(NULL, v1.GetViewByID(kV5ID)); // No V5 exists.
3215 EXPECT_EQ(NULL, v2.GetViewByID(kV1ID)); // It can get only from child views.
3217 const int kGroup = 1;
3218 v3.SetGroup(kGroup);
3219 v4.SetGroup(kGroup);
3221 View::Views views;
3222 v1.GetViewsInGroup(kGroup, &views);
3223 EXPECT_EQ(2U, views.size());
3225 View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3));
3226 EXPECT_NE(views.end(), i);
3228 i = std::find(views.begin(), views.end(), &v4);
3229 EXPECT_NE(views.end(), i);
3232 TEST_F(ViewTest, AddExistingChild) {
3233 View v1, v2, v3;
3235 v1.AddChildView(&v2);
3236 v1.AddChildView(&v3);
3237 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3238 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3240 // Check that there's no change in order when adding at same index.
3241 v1.AddChildViewAt(&v2, 0);
3242 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3243 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3244 v1.AddChildViewAt(&v3, 1);
3245 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3246 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3248 // Add it at a different index and check for change in order.
3249 v1.AddChildViewAt(&v2, 1);
3250 EXPECT_EQ(1, v1.GetIndexOf(&v2));
3251 EXPECT_EQ(0, v1.GetIndexOf(&v3));
3252 v1.AddChildViewAt(&v2, 0);
3253 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3254 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3256 // Check that calling |AddChildView()| does not change the order.
3257 v1.AddChildView(&v2);
3258 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3259 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3260 v1.AddChildView(&v3);
3261 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3262 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3265 ////////////////////////////////////////////////////////////////////////////////
3266 // FocusManager
3267 ////////////////////////////////////////////////////////////////////////////////
3269 // A widget that always claims to be active, regardless of its real activation
3270 // status.
3271 class ActiveWidget : public Widget {
3272 public:
3273 ActiveWidget() {}
3274 ~ActiveWidget() override {}
3276 bool IsActive() const override { return true; }
3278 private:
3279 DISALLOW_COPY_AND_ASSIGN(ActiveWidget);
3282 TEST_F(ViewTest, AdvanceFocusIfNecessaryForUnfocusableView) {
3283 // Create a widget with two views and give the first one focus.
3284 ActiveWidget widget;
3285 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3286 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3287 widget.Init(params);
3289 View* view1 = new View();
3290 view1->SetFocusable(true);
3291 widget.GetRootView()->AddChildView(view1);
3292 View* view2 = new View();
3293 view2->SetFocusable(true);
3294 widget.GetRootView()->AddChildView(view2);
3296 FocusManager* focus_manager = widget.GetFocusManager();
3297 ASSERT_TRUE(focus_manager);
3299 focus_manager->SetFocusedView(view1);
3300 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3302 // Disable the focused view and check if the next view gets focused.
3303 view1->SetEnabled(false);
3304 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3306 // Re-enable and re-focus.
3307 view1->SetEnabled(true);
3308 focus_manager->SetFocusedView(view1);
3309 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3311 // Hide the focused view and check it the next view gets focused.
3312 view1->SetVisible(false);
3313 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3315 // Re-show and re-focus.
3316 view1->SetVisible(true);
3317 focus_manager->SetFocusedView(view1);
3318 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3320 // Set the focused view as not focusable and check if the next view gets
3321 // focused.
3322 view1->SetFocusable(false);
3323 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3326 ////////////////////////////////////////////////////////////////////////////////
3327 // Layers
3328 ////////////////////////////////////////////////////////////////////////////////
3330 namespace {
3332 // Test implementation of LayerAnimator.
3333 class TestLayerAnimator : public ui::LayerAnimator {
3334 public:
3335 TestLayerAnimator();
3337 const gfx::Rect& last_bounds() const { return last_bounds_; }
3339 // LayerAnimator.
3340 void SetBounds(const gfx::Rect& bounds) override;
3342 protected:
3343 ~TestLayerAnimator() override {}
3345 private:
3346 gfx::Rect last_bounds_;
3348 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
3351 TestLayerAnimator::TestLayerAnimator()
3352 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
3355 void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) {
3356 last_bounds_ = bounds;
3359 } // namespace
3361 class ViewLayerTest : public ViewsTestBase {
3362 public:
3363 ViewLayerTest() : widget_(NULL) {}
3365 ~ViewLayerTest() override {}
3367 // Returns the Layer used by the RootView.
3368 ui::Layer* GetRootLayer() {
3369 return widget()->GetLayer();
3372 void SetUp() override {
3373 ViewTest::SetUp();
3374 widget_ = new Widget;
3375 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3376 params.bounds = gfx::Rect(50, 50, 200, 200);
3377 widget_->Init(params);
3378 widget_->Show();
3379 widget_->GetRootView()->SetBounds(0, 0, 200, 200);
3382 void TearDown() override {
3383 widget_->CloseNow();
3384 ViewsTestBase::TearDown();
3387 Widget* widget() { return widget_; }
3389 private:
3390 Widget* widget_;
3394 TEST_F(ViewLayerTest, LayerToggling) {
3395 // Because we lazily create textures the calls to DrawTree are necessary to
3396 // ensure we trigger creation of textures.
3397 ui::Layer* root_layer = widget()->GetLayer();
3398 View* content_view = new View;
3399 widget()->SetContentsView(content_view);
3401 // Create v1, give it a bounds and verify everything is set up correctly.
3402 View* v1 = new View;
3403 v1->SetPaintToLayer(true);
3404 EXPECT_TRUE(v1->layer() != NULL);
3405 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3406 content_view->AddChildView(v1);
3407 ASSERT_TRUE(v1->layer() != NULL);
3408 EXPECT_EQ(root_layer, v1->layer()->parent());
3409 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds());
3411 // Create v2 as a child of v1 and do basic assertion testing.
3412 View* v2 = new View;
3413 v1->AddChildView(v2);
3414 EXPECT_TRUE(v2->layer() == NULL);
3415 v2->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
3416 v2->SetPaintToLayer(true);
3417 ASSERT_TRUE(v2->layer() != NULL);
3418 EXPECT_EQ(v1->layer(), v2->layer()->parent());
3419 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
3421 // Turn off v1s layer. v2 should still have a layer but its parent should have
3422 // changed.
3423 v1->SetPaintToLayer(false);
3424 EXPECT_TRUE(v1->layer() == NULL);
3425 EXPECT_TRUE(v2->layer() != NULL);
3426 EXPECT_EQ(root_layer, v2->layer()->parent());
3427 ASSERT_EQ(1u, root_layer->children().size());
3428 EXPECT_EQ(root_layer->children()[0], v2->layer());
3429 // The bounds of the layer should have changed to be relative to the root view
3430 // now.
3431 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds());
3433 // Make v1 have a layer again and verify v2s layer is wired up correctly.
3434 gfx::Transform transform;
3435 transform.Scale(2.0, 2.0);
3436 v1->SetTransform(transform);
3437 EXPECT_TRUE(v1->layer() != NULL);
3438 EXPECT_TRUE(v2->layer() != NULL);
3439 EXPECT_EQ(root_layer, v1->layer()->parent());
3440 EXPECT_EQ(v1->layer(), v2->layer()->parent());
3441 ASSERT_EQ(1u, root_layer->children().size());
3442 EXPECT_EQ(root_layer->children()[0], v1->layer());
3443 ASSERT_EQ(1u, v1->layer()->children().size());
3444 EXPECT_EQ(v1->layer()->children()[0], v2->layer());
3445 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
3448 // Verifies turning on a layer wires up children correctly.
3449 TEST_F(ViewLayerTest, NestedLayerToggling) {
3450 View* content_view = new View;
3451 widget()->SetContentsView(content_view);
3453 // Create v1, give it a bounds and verify everything is set up correctly.
3454 View* v1 = new View;
3455 content_view->AddChildView(v1);
3456 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3458 View* v2 = new View;
3459 v1->AddChildView(v2);
3461 View* v3 = new View;
3462 v3->SetPaintToLayer(true);
3463 v2->AddChildView(v3);
3464 ASSERT_TRUE(v3->layer() != NULL);
3466 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
3468 v1->SetPaintToLayer(true);
3469 EXPECT_EQ(v1->layer(), v3->layer()->parent());
3472 TEST_F(ViewLayerTest, LayerAnimator) {
3473 View* content_view = new View;
3474 widget()->SetContentsView(content_view);
3476 View* v1 = new View;
3477 content_view->AddChildView(v1);
3478 v1->SetPaintToLayer(true);
3479 EXPECT_TRUE(v1->layer() != NULL);
3481 TestLayerAnimator* animator = new TestLayerAnimator();
3482 v1->layer()->SetAnimator(animator);
3484 gfx::Rect bounds(1, 2, 3, 4);
3485 v1->SetBoundsRect(bounds);
3486 EXPECT_EQ(bounds, animator->last_bounds());
3487 // TestLayerAnimator doesn't update the layer.
3488 EXPECT_NE(bounds, v1->layer()->bounds());
3491 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3492 // doesn't have a layer change.
3493 TEST_F(ViewLayerTest, BoundsChangeWithLayer) {
3494 View* content_view = new View;
3495 widget()->SetContentsView(content_view);
3497 View* v1 = new View;
3498 content_view->AddChildView(v1);
3499 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3501 View* v2 = new View;
3502 v2->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3503 v1->AddChildView(v2);
3504 v2->SetPaintToLayer(true);
3505 ASSERT_TRUE(v2->layer() != NULL);
3506 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
3508 v1->SetPosition(gfx::Point(25, 36));
3509 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
3511 v2->SetPosition(gfx::Point(11, 12));
3512 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
3514 // Bounds of the layer should change even if the view is not invisible.
3515 v1->SetVisible(false);
3516 v1->SetPosition(gfx::Point(20, 30));
3517 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
3519 v2->SetVisible(false);
3520 v2->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3521 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
3524 // Make sure layers are positioned correctly in RTL.
3525 TEST_F(ViewLayerTest, BoundInRTL) {
3526 ScopedRTL rtl;
3528 View* view = new View;
3529 widget()->SetContentsView(view);
3531 int content_width = view->width();
3533 // |v1| is initially not attached to anything. So its layer will have the same
3534 // bounds as the view.
3535 View* v1 = new View;
3536 v1->SetPaintToLayer(true);
3537 v1->SetBounds(10, 10, 20, 10);
3538 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3539 v1->layer()->bounds());
3541 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3542 // bounds.
3543 view->AddChildView(v1);
3544 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
3545 v1->layer()->bounds());
3546 gfx::Rect l1bounds = v1->layer()->bounds();
3548 // Now attach a View to the widget first, then create a layer for it. Make
3549 // sure the bounds are correct.
3550 View* v2 = new View;
3551 v2->SetBounds(50, 10, 30, 10);
3552 EXPECT_FALSE(v2->layer());
3553 view->AddChildView(v2);
3554 v2->SetPaintToLayer(true);
3555 EXPECT_EQ(gfx::Rect(content_width - 80, 10, 30, 10),
3556 v2->layer()->bounds());
3557 gfx::Rect l2bounds = v2->layer()->bounds();
3559 view->SetPaintToLayer(true);
3560 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3561 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3563 // Move one of the views. Make sure the layer is positioned correctly
3564 // afterwards.
3565 v1->SetBounds(v1->x() - 5, v1->y(), v1->width(), v1->height());
3566 l1bounds.set_x(l1bounds.x() + 5);
3567 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3569 view->SetPaintToLayer(false);
3570 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3571 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3573 // Move a view again.
3574 v2->SetBounds(v2->x() + 5, v2->y(), v2->width(), v2->height());
3575 l2bounds.set_x(l2bounds.x() - 5);
3576 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3579 // Make sure that resizing a parent in RTL correctly repositions its children.
3580 TEST_F(ViewLayerTest, ResizeParentInRTL) {
3581 ScopedRTL rtl;
3583 View* view = new View;
3584 widget()->SetContentsView(view);
3586 int content_width = view->width();
3588 // Create a paints-to-layer view |v1|.
3589 View* v1 = new View;
3590 v1->SetPaintToLayer(true);
3591 v1->SetBounds(10, 10, 20, 10);
3592 view->AddChildView(v1);
3593 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
3594 v1->layer()->bounds());
3596 // Attach a paints-to-layer child view to |v1|.
3597 View* v2 = new View;
3598 v2->SetPaintToLayer(true);
3599 v2->SetBounds(3, 5, 6, 4);
3600 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
3601 v2->layer()->bounds());
3602 v1->AddChildView(v2);
3603 // Check that |v2| now has RTL-appropriate bounds.
3604 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3605 v2->layer()->bounds());
3607 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
3608 View* v3 = new View;
3609 v3->SetBounds(1, 1, 18, 8);
3610 View* v4 = new View;
3611 v4->SetPaintToLayer(true);
3612 v4->SetBounds(2, 4, 6, 4);
3613 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
3614 v4->layer()->bounds());
3615 v3->AddChildView(v4);
3616 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
3617 v4->layer()->bounds());
3618 v1->AddChildView(v3);
3619 // Check that |v4| now has RTL-appropriate bounds.
3620 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3621 v4->layer()->bounds());
3623 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3624 // correctly to RTL-appropriate bounds.
3625 v1->SetSize(gfx::Size(30, 10));
3626 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3627 v2->layer()->bounds());
3628 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3629 v4->layer()->bounds());
3631 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3632 // to RTL-appropriate bounds.
3633 v3->SetBounds(2, 1, 12, 8);
3634 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3635 v4->layer()->bounds());
3638 // Makes sure a transform persists after toggling the visibility.
3639 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
3640 View* view = new View;
3641 gfx::Transform transform;
3642 transform.Scale(2.0, 2.0);
3643 view->SetTransform(transform);
3644 widget()->SetContentsView(view);
3645 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3647 view->SetVisible(false);
3648 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3650 view->SetVisible(true);
3651 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3654 // Verifies a transform persists after removing/adding a view with a transform.
3655 TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) {
3656 View* view = new View;
3657 gfx::Transform transform;
3658 transform.Scale(2.0, 2.0);
3659 view->SetTransform(transform);
3660 widget()->SetContentsView(view);
3661 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3662 ASSERT_TRUE(view->layer() != NULL);
3663 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3665 View* parent = view->parent();
3666 parent->RemoveChildView(view);
3667 parent->AddChildView(view);
3669 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3670 ASSERT_TRUE(view->layer() != NULL);
3671 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3674 // Makes sure that layer visibility is correct after toggling View visibility.
3675 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
3676 View* content_view = new View;
3677 widget()->SetContentsView(content_view);
3679 // The view isn't attached to a widget or a parent view yet. But it should
3680 // still have a layer, but the layer should not be attached to the root
3681 // layer.
3682 View* v1 = new View;
3683 v1->SetPaintToLayer(true);
3684 EXPECT_TRUE(v1->layer());
3685 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3686 v1->layer()));
3688 // Once the view is attached to a widget, its layer should be attached to the
3689 // root layer and visible.
3690 content_view->AddChildView(v1);
3691 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3692 v1->layer()));
3693 EXPECT_TRUE(v1->layer()->IsDrawn());
3695 v1->SetVisible(false);
3696 EXPECT_FALSE(v1->layer()->IsDrawn());
3698 v1->SetVisible(true);
3699 EXPECT_TRUE(v1->layer()->IsDrawn());
3701 widget()->Hide();
3702 EXPECT_FALSE(v1->layer()->IsDrawn());
3704 widget()->Show();
3705 EXPECT_TRUE(v1->layer()->IsDrawn());
3708 // Tests that the layers in the subtree are orphaned after a View is removed
3709 // from the parent.
3710 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
3711 View* content_view = new View;
3712 widget()->SetContentsView(content_view);
3714 View* v1 = new View;
3715 content_view->AddChildView(v1);
3717 View* v2 = new View;
3718 v1->AddChildView(v2);
3719 v2->SetPaintToLayer(true);
3720 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3721 v2->layer()));
3722 EXPECT_TRUE(v2->layer()->IsDrawn());
3724 content_view->RemoveChildView(v1);
3726 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3727 v2->layer()));
3729 // Reparent |v2|.
3730 content_view->AddChildView(v2);
3731 delete v1;
3732 v1 = NULL;
3733 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3734 v2->layer()));
3735 EXPECT_TRUE(v2->layer()->IsDrawn());
3738 class PaintTrackingView : public View {
3739 public:
3740 PaintTrackingView() : painted_(false) {
3743 bool painted() const { return painted_; }
3744 void set_painted(bool value) { painted_ = value; }
3746 void OnPaint(gfx::Canvas* canvas) override { painted_ = true; }
3748 private:
3749 bool painted_;
3751 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView);
3754 // Makes sure child views with layers aren't painted when paint starts at an
3755 // ancestor.
3756 TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) {
3757 PaintTrackingView* content_view = new PaintTrackingView;
3758 widget()->SetContentsView(content_view);
3759 content_view->SetPaintToLayer(true);
3760 GetRootLayer()->GetCompositor()->ScheduleDraw();
3761 ui::DrawWaiterForTest::WaitForCompositingEnded(
3762 GetRootLayer()->GetCompositor());
3763 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3764 content_view->set_painted(false);
3765 // content_view no longer has a dirty rect. Paint from the root and make sure
3766 // PaintTrackingView isn't painted.
3767 GetRootLayer()->GetCompositor()->ScheduleDraw();
3768 ui::DrawWaiterForTest::WaitForCompositingEnded(
3769 GetRootLayer()->GetCompositor());
3770 EXPECT_FALSE(content_view->painted());
3772 // Make content_view have a dirty rect, paint the layers and make sure
3773 // PaintTrackingView is painted.
3774 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3775 GetRootLayer()->GetCompositor()->ScheduleDraw();
3776 ui::DrawWaiterForTest::WaitForCompositingEnded(
3777 GetRootLayer()->GetCompositor());
3778 EXPECT_TRUE(content_view->painted());
3781 // Tests that the visibility of child layers are updated correctly when a View's
3782 // visibility changes.
3783 TEST_F(ViewLayerTest, VisibilityChildLayers) {
3784 View* v1 = new View;
3785 v1->SetPaintToLayer(true);
3786 widget()->SetContentsView(v1);
3788 View* v2 = new View;
3789 v1->AddChildView(v2);
3791 View* v3 = new View;
3792 v2->AddChildView(v3);
3793 v3->SetVisible(false);
3795 View* v4 = new View;
3796 v4->SetPaintToLayer(true);
3797 v3->AddChildView(v4);
3799 EXPECT_TRUE(v1->layer()->IsDrawn());
3800 EXPECT_FALSE(v4->layer()->IsDrawn());
3802 v2->SetVisible(false);
3803 EXPECT_TRUE(v1->layer()->IsDrawn());
3804 EXPECT_FALSE(v4->layer()->IsDrawn());
3806 v2->SetVisible(true);
3807 EXPECT_TRUE(v1->layer()->IsDrawn());
3808 EXPECT_FALSE(v4->layer()->IsDrawn());
3810 v2->SetVisible(false);
3811 EXPECT_TRUE(v1->layer()->IsDrawn());
3812 EXPECT_FALSE(v4->layer()->IsDrawn());
3813 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3815 v3->SetVisible(true);
3816 EXPECT_TRUE(v1->layer()->IsDrawn());
3817 EXPECT_FALSE(v4->layer()->IsDrawn());
3818 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3820 // Reparent |v3| to |v1|.
3821 v1->AddChildView(v3);
3822 EXPECT_TRUE(v1->layer()->IsDrawn());
3823 EXPECT_TRUE(v4->layer()->IsDrawn());
3824 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3827 // This test creates a random View tree, and then randomly reorders child views,
3828 // reparents views etc. Unrelated changes can appear to break this test. So
3829 // marking this as FLAKY.
3830 TEST_F(ViewLayerTest, DISABLED_ViewLayerTreesInSync) {
3831 View* content = new View;
3832 content->SetPaintToLayer(true);
3833 widget()->SetContentsView(content);
3834 widget()->Show();
3836 ConstructTree(content, 5);
3837 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3839 ScrambleTree(content);
3840 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3842 ScrambleTree(content);
3843 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3845 ScrambleTree(content);
3846 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3849 // Verifies when views are reordered the layer is also reordered. The widget is
3850 // providing the parent layer.
3851 TEST_F(ViewLayerTest, ReorderUnderWidget) {
3852 View* content = new View;
3853 widget()->SetContentsView(content);
3854 View* c1 = new View;
3855 c1->SetPaintToLayer(true);
3856 content->AddChildView(c1);
3857 View* c2 = new View;
3858 c2->SetPaintToLayer(true);
3859 content->AddChildView(c2);
3861 ui::Layer* parent_layer = c1->layer()->parent();
3862 ASSERT_TRUE(parent_layer);
3863 ASSERT_EQ(2u, parent_layer->children().size());
3864 EXPECT_EQ(c1->layer(), parent_layer->children()[0]);
3865 EXPECT_EQ(c2->layer(), parent_layer->children()[1]);
3867 // Move c1 to the front. The layers should have moved too.
3868 content->ReorderChildView(c1, -1);
3869 EXPECT_EQ(c1->layer(), parent_layer->children()[1]);
3870 EXPECT_EQ(c2->layer(), parent_layer->children()[0]);
3873 // Verifies that the layer of a view can be acquired properly.
3874 TEST_F(ViewLayerTest, AcquireLayer) {
3875 View* content = new View;
3876 widget()->SetContentsView(content);
3877 scoped_ptr<View> c1(new View);
3878 c1->SetPaintToLayer(true);
3879 EXPECT_TRUE(c1->layer());
3880 content->AddChildView(c1.get());
3882 scoped_ptr<ui::Layer> layer(c1->AcquireLayer());
3883 EXPECT_EQ(layer.get(), c1->layer());
3885 scoped_ptr<ui::Layer> layer2(c1->RecreateLayer());
3886 EXPECT_NE(c1->layer(), layer2.get());
3888 // Destroy view before destroying layer.
3889 c1.reset();
3892 // Verify the z-order of the layers as a result of calling RecreateLayer().
3893 TEST_F(ViewLayerTest, RecreateLayerZOrder) {
3894 scoped_ptr<View> v(new View());
3895 v->SetPaintToLayer(true);
3897 View* v1 = new View();
3898 v1->SetPaintToLayer(true);
3899 v->AddChildView(v1);
3900 View* v2 = new View();
3901 v2->SetPaintToLayer(true);
3902 v->AddChildView(v2);
3904 // Test the initial z-order.
3905 const std::vector<ui::Layer*>& child_layers_pre = v->layer()->children();
3906 ASSERT_EQ(2u, child_layers_pre.size());
3907 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3908 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3910 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3912 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3913 // for |v1| and |v2|.
3914 const std::vector<ui::Layer*>& child_layers_post = v->layer()->children();
3915 ASSERT_EQ(3u, child_layers_post.size());
3916 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3917 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3918 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3921 // Verify the z-order of the layers as a result of calling RecreateLayer when
3922 // the widget is the parent with the layer.
3923 TEST_F(ViewLayerTest, RecreateLayerZOrderWidgetParent) {
3924 View* v = new View();
3925 widget()->SetContentsView(v);
3927 View* v1 = new View();
3928 v1->SetPaintToLayer(true);
3929 v->AddChildView(v1);
3930 View* v2 = new View();
3931 v2->SetPaintToLayer(true);
3932 v->AddChildView(v2);
3934 ui::Layer* root_layer = GetRootLayer();
3936 // Test the initial z-order.
3937 const std::vector<ui::Layer*>& child_layers_pre = root_layer->children();
3938 ASSERT_EQ(2u, child_layers_pre.size());
3939 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3940 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3942 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3944 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3945 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3946 ASSERT_EQ(3u, child_layers_post.size());
3947 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3948 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3949 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3952 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3953 // a View.
3954 TEST_F(ViewLayerTest, RecreateLayerMovesNonViewChildren) {
3955 View v;
3956 v.SetPaintToLayer(true);
3957 View child;
3958 child.SetPaintToLayer(true);
3959 v.AddChildView(&child);
3960 ASSERT_TRUE(v.layer() != NULL);
3961 ASSERT_EQ(1u, v.layer()->children().size());
3962 EXPECT_EQ(v.layer()->children()[0], child.layer());
3964 ui::Layer layer(ui::LAYER_NOT_DRAWN);
3965 v.layer()->Add(&layer);
3966 v.layer()->StackAtBottom(&layer);
3968 scoped_ptr<ui::Layer> old_layer(v.RecreateLayer());
3970 // All children should be moved from old layer to new layer.
3971 ASSERT_TRUE(old_layer.get() != NULL);
3972 EXPECT_TRUE(old_layer->children().empty());
3974 // And new layer should have the two children.
3975 ASSERT_TRUE(v.layer() != NULL);
3976 ASSERT_EQ(2u, v.layer()->children().size());
3977 EXPECT_EQ(v.layer()->children()[0], &layer);
3978 EXPECT_EQ(v.layer()->children()[1], child.layer());
3981 namespace {
3983 std::string ToString(const gfx::Vector2dF& vector) {
3984 return base::StringPrintf("%.2f %0.2f", vector.x(), vector.y());
3987 } // namespace
3989 TEST_F(ViewLayerTest, SnapLayerToPixel) {
3990 View* v1 = new View;
3992 View* v11 = new View;
3993 v1->AddChildView(v11);
3995 widget()->SetContentsView(v1);
3997 const gfx::Size& size = GetRootLayer()->GetCompositor()->size();
3998 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f, size);
4000 v11->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
4001 v1->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
4002 v11->SetPaintToLayer(true);
4004 EXPECT_EQ("0.40 0.40", ToString(v11->layer()->subpixel_position_offset()));
4006 // Creating a layer in parent should update the child view's layer offset.
4007 v1->SetPaintToLayer(true);
4008 EXPECT_EQ("-0.20 -0.20", ToString(v1->layer()->subpixel_position_offset()));
4009 EXPECT_EQ("-0.20 -0.20", ToString(v11->layer()->subpixel_position_offset()));
4011 // DSF change should get propagated and update offsets.
4012 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f, size);
4013 EXPECT_EQ("0.33 0.33", ToString(v1->layer()->subpixel_position_offset()));
4014 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
4016 // Deleting parent's layer should update the child view's layer's offset.
4017 v1->SetPaintToLayer(false);
4018 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
4020 // Setting parent view should update the child view's layer's offset.
4021 v1->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
4022 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
4024 // Setting integral DSF should reset the offset.
4025 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f, size);
4026 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
4029 TEST_F(ViewTest, FocusableAssertions) {
4030 // View subclasses may change insets based on whether they are focusable,
4031 // which effects the preferred size. To avoid preferred size changing around
4032 // these Views need to key off the last value set to SetFocusable(), not
4033 // whether the View is focusable right now. For this reason it's important
4034 // that focusable() return the last value passed to SetFocusable and not
4035 // whether the View is focusable right now.
4036 TestView view;
4037 view.SetFocusable(true);
4038 EXPECT_TRUE(view.focusable());
4039 view.SetEnabled(false);
4040 EXPECT_TRUE(view.focusable());
4041 view.SetFocusable(false);
4042 EXPECT_FALSE(view.focusable());
4045 // Verifies when a view is deleted it is removed from ViewStorage.
4046 TEST_F(ViewTest, UpdateViewStorageOnDelete) {
4047 ViewStorage* view_storage = ViewStorage::GetInstance();
4048 const int storage_id = view_storage->CreateStorageID();
4050 View view;
4051 view_storage->StoreView(storage_id, &view);
4053 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL);
4056 ////////////////////////////////////////////////////////////////////////////////
4057 // NativeTheme
4058 ////////////////////////////////////////////////////////////////////////////////
4060 void TestView::OnNativeThemeChanged(const ui::NativeTheme* native_theme) {
4061 native_theme_ = native_theme;
4064 TEST_F(ViewTest, OnNativeThemeChanged) {
4065 TestView* test_view = new TestView();
4066 EXPECT_FALSE(test_view->native_theme_);
4067 TestView* test_view_child = new TestView();
4068 EXPECT_FALSE(test_view_child->native_theme_);
4070 // Child view added before the widget hierarchy exists should get the
4071 // new native theme notification.
4072 test_view->AddChildView(test_view_child);
4074 scoped_ptr<Widget> widget(new Widget);
4075 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
4076 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
4077 widget->Init(params);
4079 widget->GetRootView()->AddChildView(test_view);
4080 EXPECT_TRUE(test_view->native_theme_);
4081 EXPECT_EQ(widget->GetNativeTheme(), test_view->native_theme_);
4082 EXPECT_TRUE(test_view_child->native_theme_);
4083 EXPECT_EQ(widget->GetNativeTheme(), test_view_child->native_theme_);
4085 // Child view added after the widget hierarchy exists should also get the
4086 // notification.
4087 TestView* test_view_child_2 = new TestView();
4088 test_view->AddChildView(test_view_child_2);
4089 EXPECT_TRUE(test_view_child_2->native_theme_);
4090 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_);
4092 widget->CloseNow();
4095 } // namespace views