Roll src/third_party/WebKit b944946:201fd41 (svn 192400:192407)
[chromium-blink-merge.git] / ui / views / view_unittest.cc
blob707d9f2f34816bc25054a740be1a7b6a14ffb2d3
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 "ui/base/accelerators/accelerator.h"
13 #include "ui/base/clipboard/clipboard.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/compositor/compositor.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/compositor/layer_animator.h"
18 #include "ui/compositor/test/draw_waiter_for_test.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_utils.h"
21 #include "ui/events/keycodes/keyboard_codes.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/path.h"
24 #include "ui/gfx/transform.h"
25 #include "ui/strings/grit/ui_strings.h"
26 #include "ui/views/background.h"
27 #include "ui/views/controls/native/native_view_host.h"
28 #include "ui/views/controls/scroll_view.h"
29 #include "ui/views/controls/textfield/textfield.h"
30 #include "ui/views/focus/view_storage.h"
31 #include "ui/views/test/views_test_base.h"
32 #include "ui/views/view.h"
33 #include "ui/views/views_delegate.h"
34 #include "ui/views/widget/native_widget.h"
35 #include "ui/views/widget/root_view.h"
36 #include "ui/views/window/dialog_client_view.h"
37 #include "ui/views/window/dialog_delegate.h"
39 using base::ASCIIToUTF16;
41 namespace {
43 // Returns true if |ancestor| is an ancestor of |layer|.
44 bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) {
45 while (layer && layer != ancestor)
46 layer = layer->parent();
47 return layer == ancestor;
50 // Convenience functions for walking a View tree.
51 const views::View* FirstView(const views::View* view) {
52 const views::View* v = view;
53 while (v->has_children())
54 v = v->child_at(0);
55 return v;
58 const views::View* NextView(const views::View* view) {
59 const views::View* v = view;
60 const views::View* parent = v->parent();
61 if (!parent)
62 return NULL;
63 int next = parent->GetIndexOf(v) + 1;
64 if (next != parent->child_count())
65 return FirstView(parent->child_at(next));
66 return parent;
69 // Convenience functions for walking a Layer tree.
70 const ui::Layer* FirstLayer(const ui::Layer* layer) {
71 const ui::Layer* l = layer;
72 while (l->children().size() > 0)
73 l = l->children()[0];
74 return l;
77 const ui::Layer* NextLayer(const ui::Layer* layer) {
78 const ui::Layer* parent = layer->parent();
79 if (!parent)
80 return NULL;
81 const std::vector<ui::Layer*> children = parent->children();
82 size_t index;
83 for (index = 0; index < children.size(); index++) {
84 if (children[index] == layer)
85 break;
87 size_t next = index + 1;
88 if (next < children.size())
89 return FirstLayer(children[next]);
90 return parent;
93 // Given the root nodes of a View tree and a Layer tree, makes sure the two
94 // trees are in sync.
95 bool ViewAndLayerTreeAreConsistent(const views::View* view,
96 const ui::Layer* layer) {
97 const views::View* v = FirstView(view);
98 const ui::Layer* l = FirstLayer(layer);
99 while (v && l) {
100 // Find the view with a layer.
101 while (v && !v->layer())
102 v = NextView(v);
103 EXPECT_TRUE(v);
104 if (!v)
105 return false;
107 // Check if the View tree and the Layer tree are in sync.
108 EXPECT_EQ(l, v->layer());
109 if (v->layer() != l)
110 return false;
112 // Check if the visibility states of the View and the Layer are in sync.
113 EXPECT_EQ(l->IsDrawn(), v->IsDrawn());
114 if (v->IsDrawn() != l->IsDrawn()) {
115 for (const views::View* vv = v; vv; vv = vv->parent())
116 LOG(ERROR) << "V: " << vv << " " << vv->visible() << " "
117 << vv->IsDrawn() << " " << vv->layer();
118 for (const ui::Layer* ll = l; ll; ll = ll->parent())
119 LOG(ERROR) << "L: " << ll << " " << ll->IsDrawn();
120 return false;
123 // Check if the size of the View and the Layer are in sync.
124 EXPECT_EQ(l->bounds(), v->bounds());
125 if (v->bounds() != l->bounds())
126 return false;
128 if (v == view || l == layer)
129 return v == view && l == layer;
131 v = NextView(v);
132 l = NextLayer(l);
135 return false;
138 // Constructs a View tree with the specified depth.
139 void ConstructTree(views::View* view, int depth) {
140 if (depth == 0)
141 return;
142 int count = base::RandInt(1, 5);
143 for (int i = 0; i < count; i++) {
144 views::View* v = new views::View;
145 view->AddChildView(v);
146 if (base::RandDouble() > 0.5)
147 v->SetPaintToLayer(true);
148 if (base::RandDouble() < 0.2)
149 v->SetVisible(false);
151 ConstructTree(v, depth - 1);
155 void ScrambleTree(views::View* view) {
156 int count = view->child_count();
157 if (count == 0)
158 return;
159 for (int i = 0; i < count; i++) {
160 ScrambleTree(view->child_at(i));
163 if (count > 1) {
164 int a = base::RandInt(0, count - 1);
165 int b = base::RandInt(0, count - 1);
167 views::View* view_a = view->child_at(a);
168 views::View* view_b = view->child_at(b);
169 view->ReorderChildView(view_a, b);
170 view->ReorderChildView(view_b, a);
173 if (!view->layer() && base::RandDouble() < 0.1)
174 view->SetPaintToLayer(true);
176 if (base::RandDouble() < 0.1)
177 view->SetVisible(!view->visible());
180 void PaintWidgetInRect(views::Widget* widget, const gfx::Rect& rect) {
181 const float image_scale = 1.f;
182 const bool is_opaque = true;
183 gfx::Canvas canvas(widget->GetRootView()->bounds().size(), image_scale,
184 is_opaque);
185 canvas.ClipRect(rect);
186 widget->GetRootView()->Paint(&canvas, views::CullSet());
189 } // namespace
191 namespace views {
193 typedef ViewsTestBase ViewTest;
195 // A derived class for testing purpose.
196 class TestView : public View {
197 public:
198 TestView()
199 : View(),
200 delete_on_pressed_(false),
201 native_theme_(NULL),
202 can_process_events_within_subtree_(true) {}
203 ~TestView() override {}
205 // Reset all test state
206 void Reset() {
207 did_change_bounds_ = false;
208 last_mouse_event_type_ = 0;
209 location_.SetPoint(0, 0);
210 received_mouse_enter_ = false;
211 received_mouse_exit_ = false;
212 last_clip_.setEmpty();
213 accelerator_count_map_.clear();
214 can_process_events_within_subtree_ = true;
217 // Exposed as public for testing.
218 void DoFocus() {
219 views::View::Focus();
222 void DoBlur() {
223 views::View::Blur();
226 bool focusable() const { return View::focusable(); }
228 void set_can_process_events_within_subtree(bool can_process) {
229 can_process_events_within_subtree_ = can_process;
232 bool CanProcessEventsWithinSubtree() const override {
233 return can_process_events_within_subtree_;
236 void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
237 bool OnMousePressed(const ui::MouseEvent& event) override;
238 bool OnMouseDragged(const ui::MouseEvent& event) override;
239 void OnMouseReleased(const ui::MouseEvent& event) override;
240 void OnMouseEntered(const ui::MouseEvent& event) override;
241 void OnMouseExited(const ui::MouseEvent& event) override;
243 void Paint(gfx::Canvas* canvas, const CullSet& cull_set) override;
244 void SchedulePaintInRect(const gfx::Rect& rect) override;
245 bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
247 void OnNativeThemeChanged(const ui::NativeTheme* native_theme) override;
249 // OnBoundsChanged.
250 bool did_change_bounds_;
251 gfx::Rect new_bounds_;
253 // MouseEvent.
254 int last_mouse_event_type_;
255 gfx::Point location_;
256 bool received_mouse_enter_;
257 bool received_mouse_exit_;
258 bool delete_on_pressed_;
260 // Painting.
261 std::vector<gfx::Rect> scheduled_paint_rects_;
263 // Painting.
264 SkRect last_clip_;
266 // Accelerators.
267 std::map<ui::Accelerator, int> accelerator_count_map_;
269 // Native theme.
270 const ui::NativeTheme* native_theme_;
272 // Value to return from CanProcessEventsWithinSubtree().
273 bool can_process_events_within_subtree_;
276 ////////////////////////////////////////////////////////////////////////////////
277 // OnBoundsChanged
278 ////////////////////////////////////////////////////////////////////////////////
280 void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
281 did_change_bounds_ = true;
282 new_bounds_ = bounds();
285 TEST_F(ViewTest, OnBoundsChanged) {
286 TestView v;
288 gfx::Rect prev_rect(0, 0, 200, 200);
289 gfx::Rect new_rect(100, 100, 250, 250);
291 v.SetBoundsRect(prev_rect);
292 v.Reset();
293 v.SetBoundsRect(new_rect);
295 EXPECT_TRUE(v.did_change_bounds_);
296 EXPECT_EQ(v.new_bounds_, new_rect);
297 EXPECT_EQ(v.bounds(), new_rect);
300 ////////////////////////////////////////////////////////////////////////////////
301 // MouseEvent
302 ////////////////////////////////////////////////////////////////////////////////
304 bool TestView::OnMousePressed(const ui::MouseEvent& event) {
305 last_mouse_event_type_ = event.type();
306 location_.SetPoint(event.x(), event.y());
307 if (delete_on_pressed_)
308 delete this;
309 return true;
312 bool TestView::OnMouseDragged(const ui::MouseEvent& event) {
313 last_mouse_event_type_ = event.type();
314 location_.SetPoint(event.x(), event.y());
315 return true;
318 void TestView::OnMouseReleased(const ui::MouseEvent& event) {
319 last_mouse_event_type_ = event.type();
320 location_.SetPoint(event.x(), event.y());
323 void TestView::OnMouseEntered(const ui::MouseEvent& event) {
324 received_mouse_enter_ = true;
327 void TestView::OnMouseExited(const ui::MouseEvent& event) {
328 received_mouse_exit_ = true;
331 TEST_F(ViewTest, MouseEvent) {
332 TestView* v1 = new TestView();
333 v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
335 TestView* v2 = new TestView();
336 v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
338 scoped_ptr<Widget> widget(new Widget);
339 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
340 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
341 params.bounds = gfx::Rect(50, 50, 650, 650);
342 widget->Init(params);
343 internal::RootView* root =
344 static_cast<internal::RootView*>(widget->GetRootView());
346 root->AddChildView(v1);
347 v1->AddChildView(v2);
349 v1->Reset();
350 v2->Reset();
352 gfx::Point p1(110, 120);
353 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
354 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
355 root->OnMousePressed(pressed);
356 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_PRESSED);
357 EXPECT_EQ(v2->location_.x(), 10);
358 EXPECT_EQ(v2->location_.y(), 20);
359 // Make sure v1 did not receive the event
360 EXPECT_EQ(v1->last_mouse_event_type_, 0);
362 // Drag event out of bounds. Should still go to v2
363 v1->Reset();
364 v2->Reset();
365 gfx::Point p2(50, 40);
366 ui::MouseEvent dragged(ui::ET_MOUSE_DRAGGED, p2, p2, ui::EventTimeForNow(),
367 ui::EF_LEFT_MOUSE_BUTTON, 0);
368 root->OnMouseDragged(dragged);
369 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_DRAGGED);
370 EXPECT_EQ(v2->location_.x(), -50);
371 EXPECT_EQ(v2->location_.y(), -60);
372 // Make sure v1 did not receive the event
373 EXPECT_EQ(v1->last_mouse_event_type_, 0);
375 // Releasted event out of bounds. Should still go to v2
376 v1->Reset();
377 v2->Reset();
378 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
379 ui::EventTimeForNow(), 0, 0);
380 root->OnMouseDragged(released);
381 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_RELEASED);
382 EXPECT_EQ(v2->location_.x(), -100);
383 EXPECT_EQ(v2->location_.y(), -100);
384 // Make sure v1 did not receive the event
385 EXPECT_EQ(v1->last_mouse_event_type_, 0);
387 widget->CloseNow();
390 // Confirm that a view can be deleted as part of processing a mouse press.
391 TEST_F(ViewTest, DeleteOnPressed) {
392 TestView* v1 = new TestView();
393 v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
395 TestView* v2 = new TestView();
396 v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
398 v1->Reset();
399 v2->Reset();
401 scoped_ptr<Widget> widget(new Widget);
402 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
403 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
404 params.bounds = gfx::Rect(50, 50, 650, 650);
405 widget->Init(params);
406 View* root = widget->GetRootView();
408 root->AddChildView(v1);
409 v1->AddChildView(v2);
411 v2->delete_on_pressed_ = true;
412 gfx::Point point(110, 120);
413 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, point, point,
414 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
415 ui::EF_LEFT_MOUSE_BUTTON);
416 root->OnMousePressed(pressed);
417 EXPECT_EQ(0, v1->child_count());
419 widget->CloseNow();
422 ////////////////////////////////////////////////////////////////////////////////
423 // Painting
424 ////////////////////////////////////////////////////////////////////////////////
426 void TestView::Paint(gfx::Canvas* canvas, const CullSet& cull_set) {
427 canvas->sk_canvas()->getClipBounds(&last_clip_);
430 void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
431 scheduled_paint_rects_.push_back(rect);
432 View::SchedulePaintInRect(rect);
435 void CheckRect(const SkRect& check_rect, const SkRect& target_rect) {
436 EXPECT_EQ(target_rect.fLeft, check_rect.fLeft);
437 EXPECT_EQ(target_rect.fRight, check_rect.fRight);
438 EXPECT_EQ(target_rect.fTop, check_rect.fTop);
439 EXPECT_EQ(target_rect.fBottom, check_rect.fBottom);
442 TEST_F(ViewTest, RemoveNotification) {
443 ViewStorage* vs = ViewStorage::GetInstance();
444 Widget* widget = new Widget;
445 widget->Init(CreateParams(Widget::InitParams::TYPE_POPUP));
446 View* root_view = widget->GetRootView();
448 View* v1 = new View;
449 int s1 = vs->CreateStorageID();
450 vs->StoreView(s1, v1);
451 root_view->AddChildView(v1);
452 View* v11 = new View;
453 int s11 = vs->CreateStorageID();
454 vs->StoreView(s11, v11);
455 v1->AddChildView(v11);
456 View* v111 = new View;
457 int s111 = vs->CreateStorageID();
458 vs->StoreView(s111, v111);
459 v11->AddChildView(v111);
460 View* v112 = new View;
461 int s112 = vs->CreateStorageID();
462 vs->StoreView(s112, v112);
463 v11->AddChildView(v112);
464 View* v113 = new View;
465 int s113 = vs->CreateStorageID();
466 vs->StoreView(s113, v113);
467 v11->AddChildView(v113);
468 View* v1131 = new View;
469 int s1131 = vs->CreateStorageID();
470 vs->StoreView(s1131, v1131);
471 v113->AddChildView(v1131);
472 View* v12 = new View;
473 int s12 = vs->CreateStorageID();
474 vs->StoreView(s12, v12);
475 v1->AddChildView(v12);
477 View* v2 = new View;
478 int s2 = vs->CreateStorageID();
479 vs->StoreView(s2, v2);
480 root_view->AddChildView(v2);
481 View* v21 = new View;
482 int s21 = vs->CreateStorageID();
483 vs->StoreView(s21, v21);
484 v2->AddChildView(v21);
485 View* v211 = new View;
486 int s211 = vs->CreateStorageID();
487 vs->StoreView(s211, v211);
488 v21->AddChildView(v211);
490 size_t stored_views = vs->view_count();
492 // Try removing a leaf view.
493 v21->RemoveChildView(v211);
494 EXPECT_EQ(stored_views - 1, vs->view_count());
495 EXPECT_EQ(NULL, vs->RetrieveView(s211));
496 delete v211; // We won't use this one anymore.
498 // Now try removing a view with a hierarchy of depth 1.
499 v11->RemoveChildView(v113);
500 EXPECT_EQ(stored_views - 3, vs->view_count());
501 EXPECT_EQ(NULL, vs->RetrieveView(s113));
502 EXPECT_EQ(NULL, vs->RetrieveView(s1131));
503 delete v113; // We won't use this one anymore.
505 // Now remove even more.
506 root_view->RemoveChildView(v1);
507 EXPECT_EQ(NULL, vs->RetrieveView(s1));
508 EXPECT_EQ(NULL, vs->RetrieveView(s11));
509 EXPECT_EQ(NULL, vs->RetrieveView(s12));
510 EXPECT_EQ(NULL, vs->RetrieveView(s111));
511 EXPECT_EQ(NULL, vs->RetrieveView(s112));
513 // Put v1 back for more tests.
514 root_view->AddChildView(v1);
515 vs->StoreView(s1, v1);
517 // Synchronously closing the window deletes the view hierarchy, which should
518 // remove all its views from ViewStorage.
519 widget->CloseNow();
520 EXPECT_EQ(stored_views - 10, vs->view_count());
521 EXPECT_EQ(NULL, vs->RetrieveView(s1));
522 EXPECT_EQ(NULL, vs->RetrieveView(s12));
523 EXPECT_EQ(NULL, vs->RetrieveView(s11));
524 EXPECT_EQ(NULL, vs->RetrieveView(s12));
525 EXPECT_EQ(NULL, vs->RetrieveView(s21));
526 EXPECT_EQ(NULL, vs->RetrieveView(s111));
527 EXPECT_EQ(NULL, vs->RetrieveView(s112));
530 namespace {
532 void RotateCounterclockwise(gfx::Transform* transform) {
533 transform->matrix().set3x3(0, -1, 0,
534 1, 0, 0,
535 0, 0, 1);
538 void RotateClockwise(gfx::Transform* transform) {
539 transform->matrix().set3x3( 0, 1, 0,
540 -1, 0, 0,
541 0, 0, 1);
544 } // namespace
546 // Tests the correctness of the rect-based targeting algorithm implemented in
547 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
548 // of rect-based targeting.
549 TEST_F(ViewTest, GetEventHandlerForRect) {
550 Widget* widget = new Widget;
551 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
552 widget->Init(params);
553 View* root_view = widget->GetRootView();
554 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
556 // Have this hierarchy of views (the coordinates here are all in
557 // the root view's coordinate space):
558 // v1 (0, 0, 100, 100)
559 // v2 (150, 0, 250, 100)
560 // v3 (0, 200, 150, 100)
561 // v31 (10, 210, 80, 80)
562 // v32 (110, 210, 30, 80)
563 // v4 (300, 200, 100, 100)
564 // v41 (310, 210, 80, 80)
565 // v411 (370, 275, 10, 5)
566 // v5 (450, 197, 30, 36)
567 // v51 (450, 200, 30, 30)
569 // The coordinates used for SetBounds are in parent coordinates.
571 TestView* v1 = new TestView;
572 v1->SetBounds(0, 0, 100, 100);
573 root_view->AddChildView(v1);
575 TestView* v2 = new TestView;
576 v2->SetBounds(150, 0, 250, 100);
577 root_view->AddChildView(v2);
579 TestView* v3 = new TestView;
580 v3->SetBounds(0, 200, 150, 100);
581 root_view->AddChildView(v3);
583 TestView* v4 = new TestView;
584 v4->SetBounds(300, 200, 100, 100);
585 root_view->AddChildView(v4);
587 TestView* v31 = new TestView;
588 v31->SetBounds(10, 10, 80, 80);
589 v3->AddChildView(v31);
591 TestView* v32 = new TestView;
592 v32->SetBounds(110, 10, 30, 80);
593 v3->AddChildView(v32);
595 TestView* v41 = new TestView;
596 v41->SetBounds(10, 10, 80, 80);
597 v4->AddChildView(v41);
599 TestView* v411 = new TestView;
600 v411->SetBounds(60, 65, 10, 5);
601 v41->AddChildView(v411);
603 TestView* v5 = new TestView;
604 v5->SetBounds(450, 197, 30, 36);
605 root_view->AddChildView(v5);
607 TestView* v51 = new TestView;
608 v51->SetBounds(0, 3, 30, 30);
609 v5->AddChildView(v51);
611 // |touch_rect| does not intersect any descendant view of |root_view|.
612 gfx::Rect touch_rect(105, 105, 30, 45);
613 View* result_view = root_view->GetEventHandlerForRect(touch_rect);
614 EXPECT_EQ(root_view, result_view);
615 result_view = NULL;
617 // Covers |v1| by at least 60%.
618 touch_rect.SetRect(15, 15, 100, 100);
619 result_view = root_view->GetEventHandlerForRect(touch_rect);
620 EXPECT_EQ(v1, result_view);
621 result_view = NULL;
623 // Intersects |v1| but does not cover it by at least 60%. The center
624 // of |touch_rect| is within |v1|.
625 touch_rect.SetRect(50, 50, 5, 10);
626 result_view = root_view->GetEventHandlerForRect(touch_rect);
627 EXPECT_EQ(v1, result_view);
628 result_view = NULL;
630 // Intersects |v1| but does not cover it by at least 60%. The center
631 // of |touch_rect| is not within |v1|.
632 touch_rect.SetRect(95, 96, 21, 22);
633 result_view = root_view->GetEventHandlerForRect(touch_rect);
634 EXPECT_EQ(root_view, result_view);
635 result_view = NULL;
637 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
638 touch_rect.SetRect(95, 10, 300, 120);
639 result_view = root_view->GetEventHandlerForRect(touch_rect);
640 EXPECT_EQ(v2, result_view);
641 result_view = NULL;
643 // Covers both |v1| and |v2| by at least 60%, but the center point
644 // of |touch_rect| is closer to the center point of |v2|.
645 touch_rect.SetRect(20, 20, 400, 100);
646 result_view = root_view->GetEventHandlerForRect(touch_rect);
647 EXPECT_EQ(v2, result_view);
648 result_view = NULL;
650 // Covers both |v1| and |v2| by at least 60%, but the center point
651 // of |touch_rect| is closer to the center point of |v1|.
652 touch_rect.SetRect(-700, -15, 1050, 110);
653 result_view = root_view->GetEventHandlerForRect(touch_rect);
654 EXPECT_EQ(v1, result_view);
655 result_view = NULL;
657 // A mouse click within |v1| will target |v1|.
658 touch_rect.SetRect(15, 15, 1, 1);
659 result_view = root_view->GetEventHandlerForRect(touch_rect);
660 EXPECT_EQ(v1, result_view);
661 result_view = NULL;
663 // Intersects |v3| and |v31| by at least 60% and the center point
664 // of |touch_rect| is closer to the center point of |v31|.
665 touch_rect.SetRect(0, 200, 110, 100);
666 result_view = root_view->GetEventHandlerForRect(touch_rect);
667 EXPECT_EQ(v31, result_view);
668 result_view = NULL;
670 // Intersects |v3| and |v31|, but neither by at least 60%. The
671 // center point of |touch_rect| lies within |v31|.
672 touch_rect.SetRect(80, 280, 15, 15);
673 result_view = root_view->GetEventHandlerForRect(touch_rect);
674 EXPECT_EQ(v31, result_view);
675 result_view = NULL;
677 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
678 // center point of |touch_rect| is closest to the center point
679 // of |v32|.
680 touch_rect.SetRect(0, 200, 200, 100);
681 result_view = root_view->GetEventHandlerForRect(touch_rect);
682 EXPECT_EQ(v32, result_view);
683 result_view = NULL;
685 // Intersects all of |v3|, |v31|, and |v32|, but only covers
686 // |v31| and |v32| by at least 60%. The center point of
687 // |touch_rect| is closest to the center point of |v32|.
688 touch_rect.SetRect(30, 225, 180, 115);
689 result_view = root_view->GetEventHandlerForRect(touch_rect);
690 EXPECT_EQ(v32, result_view);
691 result_view = NULL;
693 // A mouse click at the corner of |v3| will target |v3|.
694 touch_rect.SetRect(0, 200, 1, 1);
695 result_view = root_view->GetEventHandlerForRect(touch_rect);
696 EXPECT_EQ(v3, result_view);
697 result_view = NULL;
699 // A mouse click within |v32| will target |v32|.
700 touch_rect.SetRect(112, 211, 1, 1);
701 result_view = root_view->GetEventHandlerForRect(touch_rect);
702 EXPECT_EQ(v32, result_view);
703 result_view = NULL;
705 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
706 // The center point of |touch_rect| is equally close to
707 // the center points of |v4| and |v41|.
708 touch_rect.SetRect(310, 210, 80, 80);
709 result_view = root_view->GetEventHandlerForRect(touch_rect);
710 EXPECT_EQ(v41, result_view);
711 result_view = NULL;
713 // Intersects all of |v4|, |v41|, and |v411| but only covers
714 // |v411| by at least 60%.
715 touch_rect.SetRect(370, 275, 7, 5);
716 result_view = root_view->GetEventHandlerForRect(touch_rect);
717 EXPECT_EQ(v411, result_view);
718 result_view = NULL;
720 // Intersects |v4| and |v41| but covers neither by at least 60%.
721 // The center point of |touch_rect| is equally close to the center
722 // points of |v4| and |v41|.
723 touch_rect.SetRect(345, 245, 7, 7);
724 result_view = root_view->GetEventHandlerForRect(touch_rect);
725 EXPECT_EQ(v41, result_view);
726 result_view = NULL;
728 // Intersects all of |v4|, |v41|, and |v411| and covers none of
729 // them by at least 60%. The center point of |touch_rect| lies
730 // within |v411|.
731 touch_rect.SetRect(368, 272, 4, 6);
732 result_view = root_view->GetEventHandlerForRect(touch_rect);
733 EXPECT_EQ(v411, result_view);
734 result_view = NULL;
736 // Intersects all of |v4|, |v41|, and |v411| and covers none of
737 // them by at least 60%. The center point of |touch_rect| lies
738 // within |v41|.
739 touch_rect.SetRect(365, 270, 7, 7);
740 result_view = root_view->GetEventHandlerForRect(touch_rect);
741 EXPECT_EQ(v41, result_view);
742 result_view = NULL;
744 // Intersects all of |v4|, |v41|, and |v411| and covers none of
745 // them by at least 60%. The center point of |touch_rect| lies
746 // within |v4|.
747 touch_rect.SetRect(205, 275, 200, 2);
748 result_view = root_view->GetEventHandlerForRect(touch_rect);
749 EXPECT_EQ(v4, result_view);
750 result_view = NULL;
752 // Intersects all of |v4|, |v41|, and |v411| but only covers
753 // |v41| by at least 60%.
754 touch_rect.SetRect(310, 210, 61, 66);
755 result_view = root_view->GetEventHandlerForRect(touch_rect);
756 EXPECT_EQ(v41, result_view);
757 result_view = NULL;
759 // A mouse click within |v411| will target |v411|.
760 touch_rect.SetRect(372, 275, 1, 1);
761 result_view = root_view->GetEventHandlerForRect(touch_rect);
762 EXPECT_EQ(v411, result_view);
763 result_view = NULL;
765 // A mouse click within |v41| will target |v41|.
766 touch_rect.SetRect(350, 215, 1, 1);
767 result_view = root_view->GetEventHandlerForRect(touch_rect);
768 EXPECT_EQ(v41, result_view);
769 result_view = NULL;
771 // Covers |v3|, |v4|, and all of their descendants by at
772 // least 60%. The center point of |touch_rect| is closest
773 // to the center point of |v32|.
774 touch_rect.SetRect(0, 200, 400, 100);
775 result_view = root_view->GetEventHandlerForRect(touch_rect);
776 EXPECT_EQ(v32, result_view);
777 result_view = NULL;
779 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
780 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
781 // The center point of |touch_rect| is closest to the center
782 // point of |root_view|.
783 touch_rect.SetRect(110, 15, 375, 450);
784 result_view = root_view->GetEventHandlerForRect(touch_rect);
785 EXPECT_EQ(root_view, result_view);
786 result_view = NULL;
788 // Covers all views (except |v5| and |v51|) by at least 60%. The
789 // center point of |touch_rect| is equally close to the center
790 // points of |v2| and |v32|. One is not a descendant of the other,
791 // so in this case the view selected is arbitrary (i.e.,
792 // it depends only on the ordering of nodes in the views
793 // hierarchy).
794 touch_rect.SetRect(0, 0, 400, 300);
795 result_view = root_view->GetEventHandlerForRect(touch_rect);
796 EXPECT_EQ(v32, result_view);
797 result_view = NULL;
799 // Covers |v5| and |v51| by at least 60%, and the center point of
800 // the touch is located within both views. Since both views share
801 // the same center point, the child view should be selected.
802 touch_rect.SetRect(440, 190, 40, 40);
803 result_view = root_view->GetEventHandlerForRect(touch_rect);
804 EXPECT_EQ(v51, result_view);
805 result_view = NULL;
807 // Covers |v5| and |v51| by at least 60%, but the center point of
808 // the touch is not located within either view. Since both views
809 // share the same center point, the child view should be selected.
810 touch_rect.SetRect(455, 187, 60, 60);
811 result_view = root_view->GetEventHandlerForRect(touch_rect);
812 EXPECT_EQ(v51, result_view);
813 result_view = NULL;
815 // Covers neither |v5| nor |v51| by at least 60%, but the center
816 // of the touch is located within |v51|.
817 touch_rect.SetRect(450, 197, 10, 10);
818 result_view = root_view->GetEventHandlerForRect(touch_rect);
819 EXPECT_EQ(v51, result_view);
820 result_view = NULL;
822 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
823 // The center point is located outside of both views.
824 touch_rect.SetRect(433, 180, 24, 24);
825 result_view = root_view->GetEventHandlerForRect(touch_rect);
826 EXPECT_EQ(root_view, result_view);
827 result_view = NULL;
829 // Only intersects |v5| but does not cover it by at least 60%. The
830 // center point of the touch region is located within |v5|.
831 touch_rect.SetRect(449, 196, 3, 3);
832 result_view = root_view->GetEventHandlerForRect(touch_rect);
833 EXPECT_EQ(v5, result_view);
834 result_view = NULL;
836 // A mouse click within |v5| (but not |v51|) should target |v5|.
837 touch_rect.SetRect(462, 199, 1, 1);
838 result_view = root_view->GetEventHandlerForRect(touch_rect);
839 EXPECT_EQ(v5, result_view);
840 result_view = NULL;
842 // A mouse click |v5| and |v51| should target the child view.
843 touch_rect.SetRect(452, 226, 1, 1);
844 result_view = root_view->GetEventHandlerForRect(touch_rect);
845 EXPECT_EQ(v51, result_view);
846 result_view = NULL;
848 // A mouse click on the center of |v5| and |v51| should target
849 // the child view.
850 touch_rect.SetRect(465, 215, 1, 1);
851 result_view = root_view->GetEventHandlerForRect(touch_rect);
852 EXPECT_EQ(v51, result_view);
853 result_view = NULL;
855 widget->CloseNow();
858 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
859 // as expected when different views in the view hierarchy return false
860 // when CanProcessEventsWithinSubtree() is called.
861 TEST_F(ViewTest, CanProcessEventsWithinSubtree) {
862 Widget* widget = new Widget;
863 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
864 widget->Init(params);
865 View* root_view = widget->GetRootView();
866 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
868 // Have this hierarchy of views (the coords here are in the coordinate
869 // space of the root view):
870 // v (0, 0, 100, 100)
871 // - v_child (0, 0, 20, 30)
872 // - v_grandchild (5, 5, 5, 15)
874 TestView* v = new TestView;
875 v->SetBounds(0, 0, 100, 100);
876 root_view->AddChildView(v);
877 v->set_notify_enter_exit_on_child(true);
879 TestView* v_child = new TestView;
880 v_child->SetBounds(0, 0, 20, 30);
881 v->AddChildView(v_child);
883 TestView* v_grandchild = new TestView;
884 v_grandchild->SetBounds(5, 5, 5, 15);
885 v_child->AddChildView(v_grandchild);
887 v->Reset();
888 v_child->Reset();
889 v_grandchild->Reset();
891 // Define rects and points within the views in the hierarchy.
892 gfx::Rect rect_in_v_grandchild(7, 7, 3, 3);
893 gfx::Point point_in_v_grandchild(rect_in_v_grandchild.origin());
894 gfx::Rect rect_in_v_child(12, 3, 5, 5);
895 gfx::Point point_in_v_child(rect_in_v_child.origin());
896 gfx::Rect rect_in_v(50, 50, 25, 30);
897 gfx::Point point_in_v(rect_in_v.origin());
899 // When all three views return true when CanProcessEventsWithinSubtree()
900 // is called, targeting should behave as expected.
902 View* result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
903 EXPECT_EQ(v_grandchild, result_view);
904 result_view = NULL;
905 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
906 EXPECT_EQ(v_grandchild, result_view);
907 result_view = NULL;
909 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
910 EXPECT_EQ(v_child, result_view);
911 result_view = NULL;
912 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
913 EXPECT_EQ(v_child, result_view);
914 result_view = NULL;
916 result_view = root_view->GetEventHandlerForRect(rect_in_v);
917 EXPECT_EQ(v, result_view);
918 result_view = NULL;
919 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
920 EXPECT_EQ(v, result_view);
921 result_view = NULL;
923 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
924 // is called, then |v_grandchild| cannot be returned as a target.
926 v_grandchild->set_can_process_events_within_subtree(false);
928 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
929 EXPECT_EQ(v_child, result_view);
930 result_view = NULL;
931 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
932 EXPECT_EQ(v_child, result_view);
933 result_view = NULL;
935 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
936 EXPECT_EQ(v_child, result_view);
937 result_view = NULL;
938 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
939 EXPECT_EQ(v_child, result_view);
940 result_view = NULL;
942 result_view = root_view->GetEventHandlerForRect(rect_in_v);
943 EXPECT_EQ(v, result_view);
944 result_view = NULL;
945 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
946 EXPECT_EQ(v, result_view);
948 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
949 // is called, then NULL should be returned as a target if we call
950 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
951 // views tree. Note that the location must be in the coordinate space
952 // of the root view (|v_grandchild| in this case), so use (1, 1).
954 result_view = v_grandchild;
955 result_view = v_grandchild->GetTooltipHandlerForPoint(gfx::Point(1, 1));
956 EXPECT_EQ(NULL, result_view);
957 result_view = NULL;
959 // When |v_child| returns false when CanProcessEventsWithinSubtree()
960 // is called, then neither |v_child| nor |v_grandchild| can be returned
961 // as a target (|v| should be returned as the target for each case).
963 v_grandchild->Reset();
964 v_child->set_can_process_events_within_subtree(false);
966 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
967 EXPECT_EQ(v, result_view);
968 result_view = NULL;
969 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
970 EXPECT_EQ(v, result_view);
971 result_view = NULL;
973 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
974 EXPECT_EQ(v, result_view);
975 result_view = NULL;
976 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
977 EXPECT_EQ(v, result_view);
978 result_view = NULL;
980 result_view = root_view->GetEventHandlerForRect(rect_in_v);
981 EXPECT_EQ(v, result_view);
982 result_view = NULL;
983 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
984 EXPECT_EQ(v, result_view);
985 result_view = NULL;
987 // When |v| returns false when CanProcessEventsWithinSubtree()
988 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
989 // as a target (|root_view| should be returned as the target for each case).
991 v_child->Reset();
992 v->set_can_process_events_within_subtree(false);
994 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
995 EXPECT_EQ(root_view, result_view);
996 result_view = NULL;
997 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
998 EXPECT_EQ(root_view, result_view);
999 result_view = NULL;
1001 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1002 EXPECT_EQ(root_view, result_view);
1003 result_view = NULL;
1004 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1005 EXPECT_EQ(root_view, result_view);
1006 result_view = NULL;
1008 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1009 EXPECT_EQ(root_view, result_view);
1010 result_view = NULL;
1011 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1012 EXPECT_EQ(root_view, result_view);
1015 TEST_F(ViewTest, NotifyEnterExitOnChild) {
1016 Widget* widget = new Widget;
1017 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1018 widget->Init(params);
1019 View* root_view = widget->GetRootView();
1020 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1022 // Have this hierarchy of views (the coords here are in root coord):
1023 // v1 (0, 0, 100, 100)
1024 // - v11 (0, 0, 20, 30)
1025 // - v111 (5, 5, 5, 15)
1026 // - v12 (50, 10, 30, 90)
1027 // - v121 (60, 20, 10, 10)
1028 // v2 (105, 0, 100, 100)
1029 // - v21 (120, 10, 50, 20)
1031 TestView* v1 = new TestView;
1032 v1->SetBounds(0, 0, 100, 100);
1033 root_view->AddChildView(v1);
1034 v1->set_notify_enter_exit_on_child(true);
1036 TestView* v11 = new TestView;
1037 v11->SetBounds(0, 0, 20, 30);
1038 v1->AddChildView(v11);
1040 TestView* v111 = new TestView;
1041 v111->SetBounds(5, 5, 5, 15);
1042 v11->AddChildView(v111);
1044 TestView* v12 = new TestView;
1045 v12->SetBounds(50, 10, 30, 90);
1046 v1->AddChildView(v12);
1048 TestView* v121 = new TestView;
1049 v121->SetBounds(10, 10, 10, 10);
1050 v12->AddChildView(v121);
1052 TestView* v2 = new TestView;
1053 v2->SetBounds(105, 0, 100, 100);
1054 root_view->AddChildView(v2);
1056 TestView* v21 = new TestView;
1057 v21->SetBounds(15, 10, 50, 20);
1058 v2->AddChildView(v21);
1060 v1->Reset();
1061 v11->Reset();
1062 v111->Reset();
1063 v12->Reset();
1064 v121->Reset();
1065 v2->Reset();
1066 v21->Reset();
1068 // Move the mouse in v111.
1069 gfx::Point p1(6, 6);
1070 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, p1, p1, ui::EventTimeForNow(), 0, 0);
1071 root_view->OnMouseMoved(move1);
1072 EXPECT_TRUE(v111->received_mouse_enter_);
1073 EXPECT_FALSE(v11->last_mouse_event_type_);
1074 EXPECT_TRUE(v1->received_mouse_enter_);
1076 v111->Reset();
1077 v1->Reset();
1079 // Now, move into v121.
1080 gfx::Point p2(65, 21);
1081 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, p2, p2, ui::EventTimeForNow(), 0, 0);
1082 root_view->OnMouseMoved(move2);
1083 EXPECT_TRUE(v111->received_mouse_exit_);
1084 EXPECT_TRUE(v121->received_mouse_enter_);
1085 EXPECT_FALSE(v1->last_mouse_event_type_);
1087 v111->Reset();
1088 v121->Reset();
1090 // Now, move into v11.
1091 gfx::Point p3(1, 1);
1092 ui::MouseEvent move3(ui::ET_MOUSE_MOVED, p3, p3, ui::EventTimeForNow(), 0, 0);
1093 root_view->OnMouseMoved(move3);
1094 EXPECT_TRUE(v121->received_mouse_exit_);
1095 EXPECT_TRUE(v11->received_mouse_enter_);
1096 EXPECT_FALSE(v1->last_mouse_event_type_);
1098 v121->Reset();
1099 v11->Reset();
1101 // Move to v21.
1102 gfx::Point p4(121, 15);
1103 ui::MouseEvent move4(ui::ET_MOUSE_MOVED, p4, p4, ui::EventTimeForNow(), 0, 0);
1104 root_view->OnMouseMoved(move4);
1105 EXPECT_TRUE(v21->received_mouse_enter_);
1106 EXPECT_FALSE(v2->last_mouse_event_type_);
1107 EXPECT_TRUE(v11->received_mouse_exit_);
1108 EXPECT_TRUE(v1->received_mouse_exit_);
1110 v21->Reset();
1111 v11->Reset();
1112 v1->Reset();
1114 // Move to v1.
1115 gfx::Point p5(21, 0);
1116 ui::MouseEvent move5(ui::ET_MOUSE_MOVED, p5, p5, ui::EventTimeForNow(), 0, 0);
1117 root_view->OnMouseMoved(move5);
1118 EXPECT_TRUE(v21->received_mouse_exit_);
1119 EXPECT_TRUE(v1->received_mouse_enter_);
1121 v21->Reset();
1122 v1->Reset();
1124 // Now, move into v11.
1125 gfx::Point p6(15, 15);
1126 ui::MouseEvent mouse6(ui::ET_MOUSE_MOVED, p6, p6, ui::EventTimeForNow(), 0,
1128 root_view->OnMouseMoved(mouse6);
1129 EXPECT_TRUE(v11->received_mouse_enter_);
1130 EXPECT_FALSE(v1->last_mouse_event_type_);
1132 v11->Reset();
1133 v1->Reset();
1135 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1136 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1137 // when the mouse leaves v11.
1138 gfx::Point p7(21, 0);
1139 ui::MouseEvent mouse7(ui::ET_MOUSE_MOVED, p7, p7, ui::EventTimeForNow(), 0,
1141 root_view->OnMouseMoved(mouse7);
1142 EXPECT_TRUE(v11->received_mouse_exit_);
1143 EXPECT_FALSE(v1->received_mouse_enter_);
1145 widget->CloseNow();
1148 TEST_F(ViewTest, Textfield) {
1149 const base::string16 kText = ASCIIToUTF16(
1150 "Reality is that which, when you stop believing it, doesn't go away.");
1151 const base::string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!");
1153 Widget* widget = new Widget;
1154 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1155 params.bounds = gfx::Rect(0, 0, 100, 100);
1156 widget->Init(params);
1157 View* root_view = widget->GetRootView();
1159 Textfield* textfield = new Textfield();
1160 root_view->AddChildView(textfield);
1162 // Test setting, appending text.
1163 textfield->SetText(kText);
1164 EXPECT_EQ(kText, textfield->text());
1165 textfield->AppendText(kExtraText);
1166 EXPECT_EQ(kText + kExtraText, textfield->text());
1167 textfield->SetText(base::string16());
1168 EXPECT_TRUE(textfield->text().empty());
1170 // Test selection related methods.
1171 textfield->SetText(kText);
1172 EXPECT_TRUE(textfield->GetSelectedText().empty());
1173 textfield->SelectAll(false);
1174 EXPECT_EQ(kText, textfield->text());
1175 textfield->ClearSelection();
1176 EXPECT_TRUE(textfield->GetSelectedText().empty());
1178 widget->CloseNow();
1181 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1182 TEST_F(ViewTest, TextfieldCutCopyPaste) {
1183 const base::string16 kNormalText = ASCIIToUTF16("Normal");
1184 const base::string16 kReadOnlyText = ASCIIToUTF16("Read only");
1185 const base::string16 kPasswordText =
1186 ASCIIToUTF16("Password! ** Secret stuff **");
1188 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1190 Widget* widget = new Widget;
1191 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1192 params.bounds = gfx::Rect(0, 0, 100, 100);
1193 widget->Init(params);
1194 View* root_view = widget->GetRootView();
1196 Textfield* normal = new Textfield();
1197 Textfield* read_only = new Textfield();
1198 read_only->SetReadOnly(true);
1199 Textfield* password = new Textfield();
1200 password->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
1202 root_view->AddChildView(normal);
1203 root_view->AddChildView(read_only);
1204 root_view->AddChildView(password);
1206 normal->SetText(kNormalText);
1207 read_only->SetText(kReadOnlyText);
1208 password->SetText(kPasswordText);
1211 // Test cut.
1214 normal->SelectAll(false);
1215 normal->ExecuteCommand(IDS_APP_CUT);
1216 base::string16 result;
1217 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1218 EXPECT_EQ(kNormalText, result);
1219 normal->SetText(kNormalText); // Let's revert to the original content.
1221 read_only->SelectAll(false);
1222 read_only->ExecuteCommand(IDS_APP_CUT);
1223 result.clear();
1224 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1225 // Cut should have failed, so the clipboard content should not have changed.
1226 EXPECT_EQ(kNormalText, result);
1228 password->SelectAll(false);
1229 password->ExecuteCommand(IDS_APP_CUT);
1230 result.clear();
1231 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1232 // Cut should have failed, so the clipboard content should not have changed.
1233 EXPECT_EQ(kNormalText, result);
1236 // Test copy.
1239 // Start with |read_only| to observe a change in clipboard text.
1240 read_only->SelectAll(false);
1241 read_only->ExecuteCommand(IDS_APP_COPY);
1242 result.clear();
1243 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1244 EXPECT_EQ(kReadOnlyText, result);
1246 normal->SelectAll(false);
1247 normal->ExecuteCommand(IDS_APP_COPY);
1248 result.clear();
1249 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1250 EXPECT_EQ(kNormalText, result);
1252 password->SelectAll(false);
1253 password->ExecuteCommand(IDS_APP_COPY);
1254 result.clear();
1255 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1256 // Text cannot be copied from an obscured field; the clipboard won't change.
1257 EXPECT_EQ(kNormalText, result);
1260 // Test paste.
1263 // Attempting to paste kNormalText in a read-only text-field should fail.
1264 read_only->SelectAll(false);
1265 read_only->ExecuteCommand(IDS_APP_PASTE);
1266 EXPECT_EQ(kReadOnlyText, read_only->text());
1268 password->SelectAll(false);
1269 password->ExecuteCommand(IDS_APP_PASTE);
1270 EXPECT_EQ(kNormalText, password->text());
1272 // Copy from |read_only| to observe a change in the normal textfield text.
1273 read_only->SelectAll(false);
1274 read_only->ExecuteCommand(IDS_APP_COPY);
1275 normal->SelectAll(false);
1276 normal->ExecuteCommand(IDS_APP_PASTE);
1277 EXPECT_EQ(kReadOnlyText, normal->text());
1278 widget->CloseNow();
1281 ////////////////////////////////////////////////////////////////////////////////
1282 // Accelerators
1283 ////////////////////////////////////////////////////////////////////////////////
1284 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) {
1285 accelerator_count_map_[accelerator]++;
1286 return true;
1289 // TODO: these tests were initially commented out when getting aura to
1290 // run. Figure out if still valuable and either nuke or fix.
1291 #if 0
1292 TEST_F(ViewTest, ActivateAccelerator) {
1293 // Register a keyboard accelerator before the view is added to a window.
1294 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1295 TestView* view = new TestView();
1296 view->Reset();
1297 view->AddAccelerator(return_accelerator);
1298 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1300 // Create a window and add the view as its child.
1301 scoped_ptr<Widget> widget(new Widget);
1302 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1303 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1304 params.bounds = gfx::Rect(0, 0, 100, 100);
1305 widget->Init(params);
1306 View* root = widget->GetRootView();
1307 root->AddChildView(view);
1308 widget->Show();
1310 // Get the focus manager.
1311 FocusManager* focus_manager = widget->GetFocusManager();
1312 ASSERT_TRUE(focus_manager);
1314 // Hit the return key and see if it takes effect.
1315 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1316 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1318 // Hit the escape key. Nothing should happen.
1319 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE);
1320 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1321 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1322 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0);
1324 // Now register the escape key and hit it again.
1325 view->AddAccelerator(escape_accelerator);
1326 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1327 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1328 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1330 // Remove the return key accelerator.
1331 view->RemoveAccelerator(return_accelerator);
1332 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1333 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1334 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1336 // Add it again. Hit the return key and the escape key.
1337 view->AddAccelerator(return_accelerator);
1338 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1339 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1340 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1341 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1342 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1343 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1345 // Remove all the accelerators.
1346 view->ResetAccelerators();
1347 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1348 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1349 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1350 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1351 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1352 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1354 widget->CloseNow();
1357 TEST_F(ViewTest, HiddenViewWithAccelerator) {
1358 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1359 TestView* view = new TestView();
1360 view->Reset();
1361 view->AddAccelerator(return_accelerator);
1362 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1364 scoped_ptr<Widget> widget(new Widget);
1365 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1366 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1367 params.bounds = gfx::Rect(0, 0, 100, 100);
1368 widget->Init(params);
1369 View* root = widget->GetRootView();
1370 root->AddChildView(view);
1371 widget->Show();
1373 FocusManager* focus_manager = widget->GetFocusManager();
1374 ASSERT_TRUE(focus_manager);
1376 view->SetVisible(false);
1377 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1379 view->SetVisible(true);
1380 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1382 widget->CloseNow();
1385 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) {
1386 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1387 TestView* view = new TestView();
1388 view->Reset();
1389 view->AddAccelerator(return_accelerator);
1390 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1392 scoped_ptr<Widget> widget(new Widget);
1393 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1394 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1395 params.bounds = gfx::Rect(0, 0, 100, 100);
1396 widget->Init(params);
1397 View* root = widget->GetRootView();
1398 root->AddChildView(view);
1400 FocusManager* focus_manager = widget->GetFocusManager();
1401 ASSERT_TRUE(focus_manager);
1403 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1404 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
1406 widget->Show();
1407 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1408 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1410 widget->Hide();
1411 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1412 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1414 widget->CloseNow();
1417 ////////////////////////////////////////////////////////////////////////////////
1418 // Mouse-wheel message rerouting
1419 ////////////////////////////////////////////////////////////////////////////////
1420 class ScrollableTestView : public View {
1421 public:
1422 ScrollableTestView() { }
1424 virtual gfx::Size GetPreferredSize() {
1425 return gfx::Size(100, 10000);
1428 virtual void Layout() {
1429 SizeToPreferredSize();
1433 class TestViewWithControls : public View {
1434 public:
1435 TestViewWithControls() {
1436 text_field_ = new Textfield();
1437 AddChildView(text_field_);
1440 Textfield* text_field_;
1443 class SimpleWidgetDelegate : public WidgetDelegate {
1444 public:
1445 explicit SimpleWidgetDelegate(View* contents) : contents_(contents) { }
1447 virtual void DeleteDelegate() { delete this; }
1449 virtual View* GetContentsView() { return contents_; }
1451 virtual Widget* GetWidget() { return contents_->GetWidget(); }
1452 virtual const Widget* GetWidget() const { return contents_->GetWidget(); }
1454 private:
1455 View* contents_;
1458 // Tests that the mouse-wheel messages are correctly rerouted to the window
1459 // under the mouse.
1460 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1461 // bot.
1462 // Note that this fails for a variety of reasons:
1463 // - focused view is apparently reset across window activations and never
1464 // properly restored
1465 // - this test depends on you not having any other window visible open under the
1466 // area that it opens the test windows. --beng
1467 TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) {
1468 TestViewWithControls* view_with_controls = new TestViewWithControls();
1469 Widget* window1 = Widget::CreateWindowWithBounds(
1470 new SimpleWidgetDelegate(view_with_controls),
1471 gfx::Rect(0, 0, 100, 100));
1472 window1->Show();
1473 ScrollView* scroll_view = new ScrollView();
1474 scroll_view->SetContents(new ScrollableTestView());
1475 Widget* window2 = Widget::CreateWindowWithBounds(
1476 new SimpleWidgetDelegate(scroll_view),
1477 gfx::Rect(200, 200, 100, 100));
1478 window2->Show();
1479 EXPECT_EQ(0, scroll_view->GetVisibleRect().y());
1481 // Make the window1 active, as this is what it would be in real-world.
1482 window1->Activate();
1484 // Let's send a mouse-wheel message to the different controls and check that
1485 // it is rerouted to the window under the mouse (effectively scrolling the
1486 // scroll-view).
1488 // First to the Window's HWND.
1489 ::SendMessage(view_with_controls->GetWidget()->GetNativeView(),
1490 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1491 EXPECT_EQ(20, scroll_view->GetVisibleRect().y());
1493 window1->CloseNow();
1494 window2->CloseNow();
1496 #endif // 0
1498 ////////////////////////////////////////////////////////////////////////////////
1499 // Native view hierachy
1500 ////////////////////////////////////////////////////////////////////////////////
1501 class ToplevelWidgetObserverView : public View {
1502 public:
1503 ToplevelWidgetObserverView() : toplevel_(NULL) {
1505 ~ToplevelWidgetObserverView() override {}
1507 // View overrides:
1508 void ViewHierarchyChanged(
1509 const ViewHierarchyChangedDetails& details) override {
1510 if (details.is_add) {
1511 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1512 } else {
1513 toplevel_ = NULL;
1516 void NativeViewHierarchyChanged() override {
1517 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1520 Widget* toplevel() { return toplevel_; }
1522 private:
1523 Widget* toplevel_;
1525 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView);
1528 // Test that a view can track the current top level widget by overriding
1529 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1530 TEST_F(ViewTest, NativeViewHierarchyChanged) {
1531 scoped_ptr<Widget> toplevel1(new Widget);
1532 Widget::InitParams toplevel1_params =
1533 CreateParams(Widget::InitParams::TYPE_POPUP);
1534 toplevel1_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1535 toplevel1->Init(toplevel1_params);
1537 scoped_ptr<Widget> toplevel2(new Widget);
1538 Widget::InitParams toplevel2_params =
1539 CreateParams(Widget::InitParams::TYPE_POPUP);
1540 toplevel2_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1541 toplevel2->Init(toplevel2_params);
1543 Widget* child = new Widget;
1544 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
1545 child_params.parent = toplevel1->GetNativeView();
1546 child->Init(child_params);
1548 ToplevelWidgetObserverView* observer_view =
1549 new ToplevelWidgetObserverView();
1550 EXPECT_EQ(NULL, observer_view->toplevel());
1552 child->SetContentsView(observer_view);
1553 EXPECT_EQ(toplevel1, observer_view->toplevel());
1555 Widget::ReparentNativeView(child->GetNativeView(),
1556 toplevel2->GetNativeView());
1557 EXPECT_EQ(toplevel2, observer_view->toplevel());
1559 observer_view->parent()->RemoveChildView(observer_view);
1560 EXPECT_EQ(NULL, observer_view->toplevel());
1562 // Make |observer_view| |child|'s contents view again so that it gets deleted
1563 // with the widget.
1564 child->SetContentsView(observer_view);
1567 ////////////////////////////////////////////////////////////////////////////////
1568 // Transformations
1569 ////////////////////////////////////////////////////////////////////////////////
1571 class TransformPaintView : public TestView {
1572 public:
1573 TransformPaintView() {}
1574 ~TransformPaintView() override {}
1576 void ClearScheduledPaintRect() {
1577 scheduled_paint_rect_ = gfx::Rect();
1580 gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
1582 // Overridden from View:
1583 void SchedulePaintInRect(const gfx::Rect& rect) override {
1584 gfx::Rect xrect = ConvertRectToParent(rect);
1585 scheduled_paint_rect_.Union(xrect);
1588 private:
1589 gfx::Rect scheduled_paint_rect_;
1591 DISALLOW_COPY_AND_ASSIGN(TransformPaintView);
1594 TEST_F(ViewTest, TransformPaint) {
1595 TransformPaintView* v1 = new TransformPaintView();
1596 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1598 TestView* v2 = new TestView();
1599 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1601 Widget* widget = new Widget;
1602 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1603 params.bounds = gfx::Rect(50, 50, 650, 650);
1604 widget->Init(params);
1605 widget->Show();
1606 View* root = widget->GetRootView();
1608 root->AddChildView(v1);
1609 v1->AddChildView(v2);
1611 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1612 v1->ClearScheduledPaintRect();
1613 v2->SchedulePaint();
1615 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
1617 // Rotate |v1| counter-clockwise.
1618 gfx::Transform transform;
1619 RotateCounterclockwise(&transform);
1620 transform.matrix().set(1, 3, 500.0);
1621 v1->SetTransform(transform);
1623 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1625 v1->ClearScheduledPaintRect();
1626 v2->SchedulePaint();
1628 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect());
1630 widget->CloseNow();
1633 TEST_F(ViewTest, TransformEvent) {
1634 TestView* v1 = new TestView();
1635 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1637 TestView* v2 = new TestView();
1638 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1640 Widget* widget = new Widget;
1641 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1642 params.bounds = gfx::Rect(50, 50, 650, 650);
1643 widget->Init(params);
1644 View* root = widget->GetRootView();
1646 root->AddChildView(v1);
1647 v1->AddChildView(v2);
1649 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1651 // Rotate |v1| counter-clockwise.
1652 gfx::Transform transform(v1->GetTransform());
1653 RotateCounterclockwise(&transform);
1654 transform.matrix().set(1, 3, 500.0);
1655 v1->SetTransform(transform);
1657 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1658 v1->Reset();
1659 v2->Reset();
1661 gfx::Point p1(110, 210);
1662 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
1663 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1664 root->OnMousePressed(pressed);
1665 EXPECT_EQ(0, v1->last_mouse_event_type_);
1666 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
1667 EXPECT_EQ(190, v2->location_.x());
1668 EXPECT_EQ(10, v2->location_.y());
1670 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
1671 ui::EventTimeForNow(), 0, 0);
1672 root->OnMouseReleased(released);
1674 // Now rotate |v2| inside |v1| clockwise.
1675 transform = v2->GetTransform();
1676 RotateClockwise(&transform);
1677 transform.matrix().set(0, 3, 100.f);
1678 v2->SetTransform(transform);
1680 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1681 // (300, 400) in |root|.
1683 v1->Reset();
1684 v2->Reset();
1686 gfx::Point point2(110, 320);
1687 ui::MouseEvent p2(ui::ET_MOUSE_PRESSED, point2, point2, ui::EventTimeForNow(),
1688 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1689 root->OnMousePressed(p2);
1690 EXPECT_EQ(0, v1->last_mouse_event_type_);
1691 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
1692 EXPECT_EQ(10, v2->location_.x());
1693 EXPECT_EQ(20, v2->location_.y());
1695 root->OnMouseReleased(released);
1697 v1->SetTransform(gfx::Transform());
1698 v2->SetTransform(gfx::Transform());
1700 TestView* v3 = new TestView();
1701 v3->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1702 v2->AddChildView(v3);
1704 // Rotate |v3| clockwise with respect to |v2|.
1705 transform = v1->GetTransform();
1706 RotateClockwise(&transform);
1707 transform.matrix().set(0, 3, 30.f);
1708 v3->SetTransform(transform);
1710 // Scale |v2| with respect to |v1| along both axis.
1711 transform = v2->GetTransform();
1712 transform.matrix().set(0, 0, 0.8f);
1713 transform.matrix().set(1, 1, 0.5f);
1714 v2->SetTransform(transform);
1716 // |v3| occupies (108, 105) to (132, 115) in |root|.
1718 v1->Reset();
1719 v2->Reset();
1720 v3->Reset();
1722 gfx::Point point(112, 110);
1723 ui::MouseEvent p3(ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
1724 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1725 root->OnMousePressed(p3);
1727 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
1728 EXPECT_EQ(10, v3->location_.x());
1729 EXPECT_EQ(25, v3->location_.y());
1731 root->OnMouseReleased(released);
1733 v1->SetTransform(gfx::Transform());
1734 v2->SetTransform(gfx::Transform());
1735 v3->SetTransform(gfx::Transform());
1737 v1->Reset();
1738 v2->Reset();
1739 v3->Reset();
1741 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1742 transform = v3->GetTransform();
1743 RotateClockwise(&transform);
1744 transform.matrix().set(0, 3, 30.f);
1745 // Rotation sets some scaling transformation. Using SetScale would overwrite
1746 // that and pollute the rotation. So combine the scaling with the existing
1747 // transforamtion.
1748 gfx::Transform scale;
1749 scale.Scale(0.8f, 0.5f);
1750 transform.ConcatTransform(scale);
1751 v3->SetTransform(transform);
1753 // Translate |v2| with respect to |v1|.
1754 transform = v2->GetTransform();
1755 transform.matrix().set(0, 3, 10.f);
1756 transform.matrix().set(1, 3, 10.f);
1757 v2->SetTransform(transform);
1759 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1761 gfx::Point point3(124, 125);
1762 ui::MouseEvent p4(ui::ET_MOUSE_PRESSED, point3, point3, ui::EventTimeForNow(),
1763 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1764 root->OnMousePressed(p4);
1766 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
1767 EXPECT_EQ(10, v3->location_.x());
1768 EXPECT_EQ(25, v3->location_.y());
1770 root->OnMouseReleased(released);
1772 widget->CloseNow();
1775 TEST_F(ViewTest, TransformVisibleBound) {
1776 gfx::Rect viewport_bounds(0, 0, 100, 100);
1778 scoped_ptr<Widget> widget(new Widget);
1779 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1780 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1781 params.bounds = viewport_bounds;
1782 widget->Init(params);
1783 widget->GetRootView()->SetBoundsRect(viewport_bounds);
1785 View* viewport = new View;
1786 widget->SetContentsView(viewport);
1787 View* contents = new View;
1788 viewport->AddChildView(contents);
1789 viewport->SetBoundsRect(viewport_bounds);
1790 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1792 View* child = new View;
1793 contents->AddChildView(child);
1794 child->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
1795 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds());
1797 // Rotate |child| counter-clockwise
1798 gfx::Transform transform;
1799 RotateCounterclockwise(&transform);
1800 transform.matrix().set(1, 3, 50.f);
1801 child->SetTransform(transform);
1802 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds());
1804 widget->CloseNow();
1807 ////////////////////////////////////////////////////////////////////////////////
1808 // OnVisibleBoundsChanged()
1810 class VisibleBoundsView : public View {
1811 public:
1812 VisibleBoundsView() : received_notification_(false) {}
1813 ~VisibleBoundsView() override {}
1815 bool received_notification() const { return received_notification_; }
1816 void set_received_notification(bool received) {
1817 received_notification_ = received;
1820 private:
1821 // Overridden from View:
1822 bool GetNeedsNotificationWhenVisibleBoundsChange() const override {
1823 return true;
1825 void OnVisibleBoundsChanged() override { received_notification_ = true; }
1827 bool received_notification_;
1829 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView);
1832 TEST_F(ViewTest, OnVisibleBoundsChanged) {
1833 gfx::Rect viewport_bounds(0, 0, 100, 100);
1835 scoped_ptr<Widget> widget(new Widget);
1836 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1837 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1838 params.bounds = viewport_bounds;
1839 widget->Init(params);
1840 widget->GetRootView()->SetBoundsRect(viewport_bounds);
1842 View* viewport = new View;
1843 widget->SetContentsView(viewport);
1844 View* contents = new View;
1845 viewport->AddChildView(contents);
1846 viewport->SetBoundsRect(viewport_bounds);
1847 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1849 // Create a view that cares about visible bounds notifications, and position
1850 // it just outside the visible bounds of the viewport.
1851 VisibleBoundsView* child = new VisibleBoundsView;
1852 contents->AddChildView(child);
1853 child->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
1855 // The child bound should be fully clipped.
1856 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
1858 // Now scroll the contents, but not enough to make the child visible.
1859 contents->SetY(contents->y() - 1);
1861 // We should have received the notification since the visible bounds may have
1862 // changed (even though they didn't).
1863 EXPECT_TRUE(child->received_notification());
1864 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
1865 child->set_received_notification(false);
1867 // Now scroll the contents, this time by enough to make the child visible by
1868 // one pixel.
1869 contents->SetY(contents->y() - 10);
1870 EXPECT_TRUE(child->received_notification());
1871 EXPECT_EQ(1, child->GetVisibleBounds().height());
1872 child->set_received_notification(false);
1874 widget->CloseNow();
1877 TEST_F(ViewTest, SetBoundsPaint) {
1878 TestView top_view;
1879 TestView* child_view = new TestView;
1881 top_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1882 top_view.scheduled_paint_rects_.clear();
1883 child_view->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
1884 top_view.AddChildView(child_view);
1886 top_view.scheduled_paint_rects_.clear();
1887 child_view->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
1888 EXPECT_EQ(2U, top_view.scheduled_paint_rects_.size());
1890 // There should be 2 rects, spanning from (10, 10) to (50, 50).
1891 gfx::Rect paint_rect = top_view.scheduled_paint_rects_[0];
1892 paint_rect.Union(top_view.scheduled_paint_rects_[1]);
1893 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect);
1896 // Assertions around painting and focus gain/lost.
1897 TEST_F(ViewTest, FocusBlurPaints) {
1898 TestView parent_view;
1899 TestView* child_view1 = new TestView; // Owned by |parent_view|.
1901 parent_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1903 child_view1->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
1904 parent_view.AddChildView(child_view1);
1906 parent_view.scheduled_paint_rects_.clear();
1907 child_view1->scheduled_paint_rects_.clear();
1909 // Focus change shouldn't trigger paints.
1910 child_view1->DoFocus();
1912 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
1913 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
1915 child_view1->DoBlur();
1916 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
1917 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
1920 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
1921 TEST_F(ViewTest, SetBoundsSameBoundsDoesntSchedulePaint) {
1922 TestView view;
1924 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1925 view.InvalidateLayout();
1926 view.scheduled_paint_rects_.clear();
1927 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1928 EXPECT_TRUE(view.scheduled_paint_rects_.empty());
1931 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
1932 TEST_F(ViewTest, AddAndRemoveSchedulePaints) {
1933 gfx::Rect viewport_bounds(0, 0, 100, 100);
1935 // We have to put the View hierarchy into a Widget or no paints will be
1936 // scheduled.
1937 scoped_ptr<Widget> widget(new Widget);
1938 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1939 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1940 params.bounds = viewport_bounds;
1941 widget->Init(params);
1942 widget->GetRootView()->SetBoundsRect(viewport_bounds);
1944 TestView* parent_view = new TestView;
1945 widget->SetContentsView(parent_view);
1946 parent_view->SetBoundsRect(viewport_bounds);
1947 parent_view->scheduled_paint_rects_.clear();
1949 View* child_view = new View;
1950 child_view->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
1951 parent_view->AddChildView(child_view);
1952 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
1953 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
1955 parent_view->scheduled_paint_rects_.clear();
1956 parent_view->RemoveChildView(child_view);
1957 scoped_ptr<View> child_deleter(child_view);
1958 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
1959 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
1961 widget->CloseNow();
1964 // Tests conversion methods with a transform.
1965 TEST_F(ViewTest, ConversionsWithTransform) {
1966 TestView top_view;
1968 // View hierarchy used to test scale transforms.
1969 TestView* child = new TestView;
1970 TestView* child_child = new TestView;
1972 // View used to test a rotation transform.
1973 TestView* child_2 = new TestView;
1975 top_view.AddChildView(child);
1976 child->AddChildView(child_child);
1978 top_view.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
1980 child->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
1981 gfx::Transform transform;
1982 transform.Scale(3.0, 4.0);
1983 child->SetTransform(transform);
1985 child_child->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
1986 transform.MakeIdentity();
1987 transform.Scale(5.0, 7.0);
1988 child_child->SetTransform(transform);
1990 top_view.AddChildView(child_2);
1991 child_2->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
1992 transform.MakeIdentity();
1993 RotateClockwise(&transform);
1994 child_2->SetTransform(transform);
1996 // Sanity check to make sure basic transforms act as expected.
1998 gfx::Transform transform;
1999 transform.Translate(110.0, -110.0);
2000 transform.Scale(100.0, 55.0);
2001 transform.Translate(1.0, 1.0);
2003 // convert to a 3x3 matrix.
2004 const SkMatrix& matrix = transform.matrix();
2006 EXPECT_EQ(210, matrix.getTranslateX());
2007 EXPECT_EQ(-55, matrix.getTranslateY());
2008 EXPECT_EQ(100, matrix.getScaleX());
2009 EXPECT_EQ(55, matrix.getScaleY());
2010 EXPECT_EQ(0, matrix.getSkewX());
2011 EXPECT_EQ(0, matrix.getSkewY());
2015 gfx::Transform transform;
2016 transform.Translate(1.0, 1.0);
2017 gfx::Transform t2;
2018 t2.Scale(100.0, 55.0);
2019 gfx::Transform t3;
2020 t3.Translate(110.0, -110.0);
2021 transform.ConcatTransform(t2);
2022 transform.ConcatTransform(t3);
2024 // convert to a 3x3 matrix
2025 const SkMatrix& matrix = transform.matrix();
2027 EXPECT_EQ(210, matrix.getTranslateX());
2028 EXPECT_EQ(-55, matrix.getTranslateY());
2029 EXPECT_EQ(100, matrix.getScaleX());
2030 EXPECT_EQ(55, matrix.getScaleY());
2031 EXPECT_EQ(0, matrix.getSkewX());
2032 EXPECT_EQ(0, matrix.getSkewY());
2035 // Conversions from child->top and top->child.
2037 gfx::Point point(5, 5);
2038 View::ConvertPointToTarget(child, &top_view, &point);
2039 EXPECT_EQ(22, point.x());
2040 EXPECT_EQ(39, point.y());
2042 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2043 View::ConvertRectToTarget(child, &top_view, &rect);
2044 EXPECT_FLOAT_EQ(22.0f, rect.x());
2045 EXPECT_FLOAT_EQ(39.0f, rect.y());
2046 EXPECT_FLOAT_EQ(30.0f, rect.width());
2047 EXPECT_FLOAT_EQ(80.0f, rect.height());
2049 point.SetPoint(22, 39);
2050 View::ConvertPointToTarget(&top_view, child, &point);
2051 EXPECT_EQ(5, point.x());
2052 EXPECT_EQ(5, point.y());
2054 rect.SetRect(22.0f, 39.0f, 30.0f, 80.0f);
2055 View::ConvertRectToTarget(&top_view, child, &rect);
2056 EXPECT_FLOAT_EQ(5.0f, rect.x());
2057 EXPECT_FLOAT_EQ(5.0f, rect.y());
2058 EXPECT_FLOAT_EQ(10.0f, rect.width());
2059 EXPECT_FLOAT_EQ(20.0f, rect.height());
2062 // Conversions from child_child->top and top->child_child.
2064 gfx::Point point(5, 5);
2065 View::ConvertPointToTarget(child_child, &top_view, &point);
2066 EXPECT_EQ(133, point.x());
2067 EXPECT_EQ(211, point.y());
2069 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2070 View::ConvertRectToTarget(child_child, &top_view, &rect);
2071 EXPECT_FLOAT_EQ(133.0f, rect.x());
2072 EXPECT_FLOAT_EQ(211.0f, rect.y());
2073 EXPECT_FLOAT_EQ(150.0f, rect.width());
2074 EXPECT_FLOAT_EQ(560.0f, rect.height());
2076 point.SetPoint(133, 211);
2077 View::ConvertPointToTarget(&top_view, child_child, &point);
2078 EXPECT_EQ(5, point.x());
2079 EXPECT_EQ(5, point.y());
2081 rect.SetRect(133.0f, 211.0f, 150.0f, 560.0f);
2082 View::ConvertRectToTarget(&top_view, child_child, &rect);
2083 EXPECT_FLOAT_EQ(5.0f, rect.x());
2084 EXPECT_FLOAT_EQ(5.0f, rect.y());
2085 EXPECT_FLOAT_EQ(10.0f, rect.width());
2086 EXPECT_FLOAT_EQ(20.0f, rect.height());
2089 // Conversions from child_child->child and child->child_child
2091 gfx::Point point(5, 5);
2092 View::ConvertPointToTarget(child_child, child, &point);
2093 EXPECT_EQ(42, point.x());
2094 EXPECT_EQ(48, point.y());
2096 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2097 View::ConvertRectToTarget(child_child, child, &rect);
2098 EXPECT_FLOAT_EQ(42.0f, rect.x());
2099 EXPECT_FLOAT_EQ(48.0f, rect.y());
2100 EXPECT_FLOAT_EQ(50.0f, rect.width());
2101 EXPECT_FLOAT_EQ(140.0f, rect.height());
2103 point.SetPoint(42, 48);
2104 View::ConvertPointToTarget(child, child_child, &point);
2105 EXPECT_EQ(5, point.x());
2106 EXPECT_EQ(5, point.y());
2108 rect.SetRect(42.0f, 48.0f, 50.0f, 140.0f);
2109 View::ConvertRectToTarget(child, child_child, &rect);
2110 EXPECT_FLOAT_EQ(5.0f, rect.x());
2111 EXPECT_FLOAT_EQ(5.0f, rect.y());
2112 EXPECT_FLOAT_EQ(10.0f, rect.width());
2113 EXPECT_FLOAT_EQ(20.0f, rect.height());
2116 // Conversions from top_view to child with a value that should be negative.
2117 // This ensures we don't round up with negative numbers.
2119 gfx::Point point(6, 18);
2120 View::ConvertPointToTarget(&top_view, child, &point);
2121 EXPECT_EQ(-1, point.x());
2122 EXPECT_EQ(-1, point.y());
2124 float error = 0.01f;
2125 gfx::RectF rect(6.0f, 18.0f, 10.0f, 39.0f);
2126 View::ConvertRectToTarget(&top_view, child, &rect);
2127 EXPECT_NEAR(-0.33f, rect.x(), error);
2128 EXPECT_NEAR(-0.25f, rect.y(), error);
2129 EXPECT_NEAR(3.33f, rect.width(), error);
2130 EXPECT_NEAR(9.75f, rect.height(), error);
2133 // Rect conversions from top_view->child_2 and child_2->top_view.
2135 gfx::RectF rect(50.0f, 55.0f, 20.0f, 30.0f);
2136 View::ConvertRectToTarget(child_2, &top_view, &rect);
2137 EXPECT_FLOAT_EQ(615.0f, rect.x());
2138 EXPECT_FLOAT_EQ(775.0f, rect.y());
2139 EXPECT_FLOAT_EQ(30.0f, rect.width());
2140 EXPECT_FLOAT_EQ(20.0f, rect.height());
2142 rect.SetRect(615.0f, 775.0f, 30.0f, 20.0f);
2143 View::ConvertRectToTarget(&top_view, child_2, &rect);
2144 EXPECT_FLOAT_EQ(50.0f, rect.x());
2145 EXPECT_FLOAT_EQ(55.0f, rect.y());
2146 EXPECT_FLOAT_EQ(20.0f, rect.width());
2147 EXPECT_FLOAT_EQ(30.0f, rect.height());
2151 // Tests conversion methods to and from screen coordinates.
2152 TEST_F(ViewTest, ConversionsToFromScreen) {
2153 scoped_ptr<Widget> widget(new Widget);
2154 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2155 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2156 params.bounds = gfx::Rect(50, 50, 650, 650);
2157 widget->Init(params);
2159 View* child = new View;
2160 widget->GetRootView()->AddChildView(child);
2161 child->SetBounds(10, 10, 100, 200);
2162 gfx::Transform t;
2163 t.Scale(0.5, 0.5);
2164 child->SetTransform(t);
2166 gfx::Point point_in_screen(100, 90);
2167 gfx::Point point_in_child(80, 60);
2169 gfx::Point point = point_in_screen;
2170 View::ConvertPointFromScreen(child, &point);
2171 EXPECT_EQ(point_in_child.ToString(), point.ToString());
2173 View::ConvertPointToScreen(child, &point);
2174 EXPECT_EQ(point_in_screen.ToString(), point.ToString());
2177 // Tests conversion methods for rectangles.
2178 TEST_F(ViewTest, ConvertRectWithTransform) {
2179 scoped_ptr<Widget> widget(new Widget);
2180 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2181 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2182 params.bounds = gfx::Rect(50, 50, 650, 650);
2183 widget->Init(params);
2184 View* root = widget->GetRootView();
2186 TestView* v1 = new TestView;
2187 TestView* v2 = new TestView;
2188 root->AddChildView(v1);
2189 v1->AddChildView(v2);
2191 v1->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2192 v2->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2194 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2195 gfx::Rect rect(5, 5, 15, 40);
2196 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2->ConvertRectToParent(rect));
2197 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2->ConvertRectToWidget(rect));
2199 // Rotate |v2|
2200 gfx::Transform t2;
2201 RotateCounterclockwise(&t2);
2202 t2.matrix().set(1, 3, 100.f);
2203 v2->SetTransform(t2);
2205 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2206 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2207 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2->ConvertRectToWidget(rect));
2209 // Scale down |v1|
2210 gfx::Transform t1;
2211 t1.Scale(0.5, 0.5);
2212 v1->SetTransform(t1);
2214 // The rectangle should remain the same for |v1|.
2215 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2217 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2218 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2219 v2->ConvertRectToWidget(rect).ToString());
2221 widget->CloseNow();
2224 class ObserverView : public View {
2225 public:
2226 ObserverView();
2227 ~ObserverView() override;
2229 void ResetTestState();
2231 bool has_add_details() const { return has_add_details_; }
2232 bool has_remove_details() const { return has_remove_details_; }
2234 const ViewHierarchyChangedDetails& add_details() const {
2235 return add_details_;
2238 const ViewHierarchyChangedDetails& remove_details() const {
2239 return remove_details_;
2242 private:
2243 // View:
2244 void ViewHierarchyChanged(
2245 const ViewHierarchyChangedDetails& details) override;
2247 bool has_add_details_;
2248 bool has_remove_details_;
2249 ViewHierarchyChangedDetails add_details_;
2250 ViewHierarchyChangedDetails remove_details_;
2252 DISALLOW_COPY_AND_ASSIGN(ObserverView);
2255 ObserverView::ObserverView()
2256 : has_add_details_(false),
2257 has_remove_details_(false) {
2260 ObserverView::~ObserverView() {}
2262 void ObserverView::ResetTestState() {
2263 has_add_details_ = false;
2264 has_remove_details_ = false;
2265 add_details_ = ViewHierarchyChangedDetails();
2266 remove_details_ = ViewHierarchyChangedDetails();
2269 void ObserverView::ViewHierarchyChanged(
2270 const ViewHierarchyChangedDetails& details) {
2271 if (details.is_add) {
2272 has_add_details_ = true;
2273 add_details_ = details;
2274 } else {
2275 has_remove_details_ = true;
2276 remove_details_ = details;
2280 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2281 // a child view is added or removed to all the views in the hierarchy (up and
2282 // down).
2283 // The tree looks like this:
2284 // v1
2285 // +-- v2
2286 // +-- v3
2287 // +-- v4 (starts here, then get reparented to v1)
2288 TEST_F(ViewTest, ViewHierarchyChanged) {
2289 ObserverView v1;
2291 ObserverView* v3 = new ObserverView();
2293 // Add |v3| to |v2|.
2294 scoped_ptr<ObserverView> v2(new ObserverView());
2295 v2->AddChildView(v3);
2297 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2298 // notification.
2299 EXPECT_TRUE(v2->has_add_details());
2300 EXPECT_FALSE(v2->has_remove_details());
2301 EXPECT_EQ(v2.get(), v2->add_details().parent);
2302 EXPECT_EQ(v3, v2->add_details().child);
2303 EXPECT_EQ(NULL, v2->add_details().move_view);
2305 EXPECT_TRUE(v3->has_add_details());
2306 EXPECT_FALSE(v3->has_remove_details());
2307 EXPECT_EQ(v2.get(), v3->add_details().parent);
2308 EXPECT_EQ(v3, v3->add_details().child);
2309 EXPECT_EQ(NULL, v3->add_details().move_view);
2311 // Reset everything to the initial state.
2312 v2->ResetTestState();
2313 v3->ResetTestState();
2315 // Add |v2| to v1.
2316 v1.AddChildView(v2.get());
2318 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2319 // Make sure all the views (v1, v2, v3) received _that_ information.
2320 EXPECT_TRUE(v1.has_add_details());
2321 EXPECT_FALSE(v1.has_remove_details());
2322 EXPECT_EQ(&v1, v1.add_details().parent);
2323 EXPECT_EQ(v2.get(), v1.add_details().child);
2324 EXPECT_EQ(NULL, v1.add_details().move_view);
2326 EXPECT_TRUE(v2->has_add_details());
2327 EXPECT_FALSE(v2->has_remove_details());
2328 EXPECT_EQ(&v1, v2->add_details().parent);
2329 EXPECT_EQ(v2.get(), v2->add_details().child);
2330 EXPECT_EQ(NULL, v2->add_details().move_view);
2332 EXPECT_TRUE(v3->has_add_details());
2333 EXPECT_FALSE(v3->has_remove_details());
2334 EXPECT_EQ(&v1, v3->add_details().parent);
2335 EXPECT_EQ(v2.get(), v3->add_details().child);
2336 EXPECT_EQ(NULL, v3->add_details().move_view);
2338 // Reset everything to the initial state.
2339 v1.ResetTestState();
2340 v2->ResetTestState();
2341 v3->ResetTestState();
2343 // Remove |v2| from |v1|.
2344 v1.RemoveChildView(v2.get());
2346 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2347 // Make sure all the views (v1, v2, v3) received _that_ information.
2348 EXPECT_FALSE(v1.has_add_details());
2349 EXPECT_TRUE(v1.has_remove_details());
2350 EXPECT_EQ(&v1, v1.remove_details().parent);
2351 EXPECT_EQ(v2.get(), v1.remove_details().child);
2352 EXPECT_EQ(NULL, v1.remove_details().move_view);
2354 EXPECT_FALSE(v2->has_add_details());
2355 EXPECT_TRUE(v2->has_remove_details());
2356 EXPECT_EQ(&v1, v2->remove_details().parent);
2357 EXPECT_EQ(v2.get(), v2->remove_details().child);
2358 EXPECT_EQ(NULL, v2->remove_details().move_view);
2360 EXPECT_FALSE(v3->has_add_details());
2361 EXPECT_TRUE(v3->has_remove_details());
2362 EXPECT_EQ(&v1, v3->remove_details().parent);
2363 EXPECT_EQ(v3, v3->remove_details().child);
2364 EXPECT_EQ(NULL, v3->remove_details().move_view);
2366 // Verifies notifications when reparenting a view.
2367 ObserverView* v4 = new ObserverView();
2368 // Add |v4| to |v2|.
2369 v2->AddChildView(v4);
2371 // Reset everything to the initial state.
2372 v1.ResetTestState();
2373 v2->ResetTestState();
2374 v3->ResetTestState();
2375 v4->ResetTestState();
2377 // Reparent |v4| to |v1|.
2378 v1.AddChildView(v4);
2380 // Verifies that all views receive the correct information for all the child,
2381 // parent and move views.
2383 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2384 EXPECT_TRUE(v1.has_add_details());
2385 EXPECT_FALSE(v1.has_remove_details());
2386 EXPECT_EQ(&v1, v1.add_details().parent);
2387 EXPECT_EQ(v4, v1.add_details().child);
2388 EXPECT_EQ(v2.get(), v1.add_details().move_view);
2390 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2391 // parent.
2392 EXPECT_FALSE(v2->has_add_details());
2393 EXPECT_TRUE(v2->has_remove_details());
2394 EXPECT_EQ(v2.get(), v2->remove_details().parent);
2395 EXPECT_EQ(v4, v2->remove_details().child);
2396 EXPECT_EQ(&v1, v2->remove_details().move_view);
2398 // |v3| is not impacted by this operation, and hence receives no notification.
2399 EXPECT_FALSE(v3->has_add_details());
2400 EXPECT_FALSE(v3->has_remove_details());
2402 // |v4| is the reparented child, so it receives notifications for the remove
2403 // and then the add. |v2| is its old parent, |v1| is its new parent.
2404 EXPECT_TRUE(v4->has_remove_details());
2405 EXPECT_TRUE(v4->has_add_details());
2406 EXPECT_EQ(v2.get(), v4->remove_details().parent);
2407 EXPECT_EQ(&v1, v4->add_details().parent);
2408 EXPECT_EQ(v4, v4->add_details().child);
2409 EXPECT_EQ(v4, v4->remove_details().child);
2410 EXPECT_EQ(&v1, v4->remove_details().move_view);
2411 EXPECT_EQ(v2.get(), v4->add_details().move_view);
2414 // Verifies if the child views added under the root are all deleted when calling
2415 // RemoveAllChildViews.
2416 // The tree looks like this:
2417 // root
2418 // +-- child1
2419 // +-- foo
2420 // +-- bar0
2421 // +-- bar1
2422 // +-- bar2
2423 // +-- child2
2424 // +-- child3
2425 TEST_F(ViewTest, RemoveAllChildViews) {
2426 View root;
2428 View* child1 = new View;
2429 root.AddChildView(child1);
2431 for (int i = 0; i < 2; ++i)
2432 root.AddChildView(new View);
2434 View* foo = new View;
2435 child1->AddChildView(foo);
2437 // Add some nodes to |foo|.
2438 for (int i = 0; i < 3; ++i)
2439 foo->AddChildView(new View);
2441 EXPECT_EQ(3, root.child_count());
2442 EXPECT_EQ(1, child1->child_count());
2443 EXPECT_EQ(3, foo->child_count());
2445 // Now remove all child views from root.
2446 root.RemoveAllChildViews(true);
2448 EXPECT_EQ(0, root.child_count());
2449 EXPECT_FALSE(root.has_children());
2452 TEST_F(ViewTest, Contains) {
2453 View v1;
2454 View* v2 = new View;
2455 View* v3 = new View;
2457 v1.AddChildView(v2);
2458 v2->AddChildView(v3);
2460 EXPECT_FALSE(v1.Contains(NULL));
2461 EXPECT_TRUE(v1.Contains(&v1));
2462 EXPECT_TRUE(v1.Contains(v2));
2463 EXPECT_TRUE(v1.Contains(v3));
2465 EXPECT_FALSE(v2->Contains(NULL));
2466 EXPECT_TRUE(v2->Contains(v2));
2467 EXPECT_FALSE(v2->Contains(&v1));
2468 EXPECT_TRUE(v2->Contains(v3));
2470 EXPECT_FALSE(v3->Contains(NULL));
2471 EXPECT_TRUE(v3->Contains(v3));
2472 EXPECT_FALSE(v3->Contains(&v1));
2473 EXPECT_FALSE(v3->Contains(v2));
2476 // Verifies if GetIndexOf() returns the correct index for the specified child
2477 // view.
2478 // The tree looks like this:
2479 // root
2480 // +-- child1
2481 // +-- foo1
2482 // +-- child2
2483 TEST_F(ViewTest, GetIndexOf) {
2484 View root;
2486 View* child1 = new View;
2487 root.AddChildView(child1);
2489 View* child2 = new View;
2490 root.AddChildView(child2);
2492 View* foo1 = new View;
2493 child1->AddChildView(foo1);
2495 EXPECT_EQ(-1, root.GetIndexOf(NULL));
2496 EXPECT_EQ(-1, root.GetIndexOf(&root));
2497 EXPECT_EQ(0, root.GetIndexOf(child1));
2498 EXPECT_EQ(1, root.GetIndexOf(child2));
2499 EXPECT_EQ(-1, root.GetIndexOf(foo1));
2501 EXPECT_EQ(-1, child1->GetIndexOf(NULL));
2502 EXPECT_EQ(-1, child1->GetIndexOf(&root));
2503 EXPECT_EQ(-1, child1->GetIndexOf(child1));
2504 EXPECT_EQ(-1, child1->GetIndexOf(child2));
2505 EXPECT_EQ(0, child1->GetIndexOf(foo1));
2507 EXPECT_EQ(-1, child2->GetIndexOf(NULL));
2508 EXPECT_EQ(-1, child2->GetIndexOf(&root));
2509 EXPECT_EQ(-1, child2->GetIndexOf(child2));
2510 EXPECT_EQ(-1, child2->GetIndexOf(child1));
2511 EXPECT_EQ(-1, child2->GetIndexOf(foo1));
2514 // Verifies that the child views can be reordered correctly.
2515 TEST_F(ViewTest, ReorderChildren) {
2516 View root;
2518 View* child = new View();
2519 root.AddChildView(child);
2521 View* foo1 = new View();
2522 child->AddChildView(foo1);
2523 View* foo2 = new View();
2524 child->AddChildView(foo2);
2525 View* foo3 = new View();
2526 child->AddChildView(foo3);
2527 foo1->SetFocusable(true);
2528 foo2->SetFocusable(true);
2529 foo3->SetFocusable(true);
2531 ASSERT_EQ(0, child->GetIndexOf(foo1));
2532 ASSERT_EQ(1, child->GetIndexOf(foo2));
2533 ASSERT_EQ(2, child->GetIndexOf(foo3));
2534 ASSERT_EQ(foo2, foo1->GetNextFocusableView());
2535 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
2536 ASSERT_EQ(NULL, foo3->GetNextFocusableView());
2538 // Move |foo2| at the end.
2539 child->ReorderChildView(foo2, -1);
2540 ASSERT_EQ(0, child->GetIndexOf(foo1));
2541 ASSERT_EQ(1, child->GetIndexOf(foo3));
2542 ASSERT_EQ(2, child->GetIndexOf(foo2));
2543 ASSERT_EQ(foo3, foo1->GetNextFocusableView());
2544 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
2545 ASSERT_EQ(NULL, foo2->GetNextFocusableView());
2547 // Move |foo1| at the end.
2548 child->ReorderChildView(foo1, -1);
2549 ASSERT_EQ(0, child->GetIndexOf(foo3));
2550 ASSERT_EQ(1, child->GetIndexOf(foo2));
2551 ASSERT_EQ(2, child->GetIndexOf(foo1));
2552 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
2553 ASSERT_EQ(foo2, foo1->GetPreviousFocusableView());
2554 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
2555 ASSERT_EQ(foo1, foo2->GetNextFocusableView());
2557 // Move |foo2| to the front.
2558 child->ReorderChildView(foo2, 0);
2559 ASSERT_EQ(0, child->GetIndexOf(foo2));
2560 ASSERT_EQ(1, child->GetIndexOf(foo3));
2561 ASSERT_EQ(2, child->GetIndexOf(foo1));
2562 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
2563 ASSERT_EQ(foo3, foo1->GetPreviousFocusableView());
2564 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
2565 ASSERT_EQ(foo1, foo3->GetNextFocusableView());
2568 // Verifies that GetViewByID returns the correctly child view from the specified
2569 // ID.
2570 // The tree looks like this:
2571 // v1
2572 // +-- v2
2573 // +-- v3
2574 // +-- v4
2575 TEST_F(ViewTest, GetViewByID) {
2576 View v1;
2577 const int kV1ID = 1;
2578 v1.set_id(kV1ID);
2580 View v2;
2581 const int kV2ID = 2;
2582 v2.set_id(kV2ID);
2584 View v3;
2585 const int kV3ID = 3;
2586 v3.set_id(kV3ID);
2588 View v4;
2589 const int kV4ID = 4;
2590 v4.set_id(kV4ID);
2592 const int kV5ID = 5;
2594 v1.AddChildView(&v2);
2595 v2.AddChildView(&v3);
2596 v2.AddChildView(&v4);
2598 EXPECT_EQ(&v1, v1.GetViewByID(kV1ID));
2599 EXPECT_EQ(&v2, v1.GetViewByID(kV2ID));
2600 EXPECT_EQ(&v4, v1.GetViewByID(kV4ID));
2602 EXPECT_EQ(NULL, v1.GetViewByID(kV5ID)); // No V5 exists.
2603 EXPECT_EQ(NULL, v2.GetViewByID(kV1ID)); // It can get only from child views.
2605 const int kGroup = 1;
2606 v3.SetGroup(kGroup);
2607 v4.SetGroup(kGroup);
2609 View::Views views;
2610 v1.GetViewsInGroup(kGroup, &views);
2611 EXPECT_EQ(2U, views.size());
2613 View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3));
2614 EXPECT_NE(views.end(), i);
2616 i = std::find(views.begin(), views.end(), &v4);
2617 EXPECT_NE(views.end(), i);
2620 TEST_F(ViewTest, AddExistingChild) {
2621 View v1, v2, v3;
2623 v1.AddChildView(&v2);
2624 v1.AddChildView(&v3);
2625 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2626 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2628 // Check that there's no change in order when adding at same index.
2629 v1.AddChildViewAt(&v2, 0);
2630 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2631 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2632 v1.AddChildViewAt(&v3, 1);
2633 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2634 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2636 // Add it at a different index and check for change in order.
2637 v1.AddChildViewAt(&v2, 1);
2638 EXPECT_EQ(1, v1.GetIndexOf(&v2));
2639 EXPECT_EQ(0, v1.GetIndexOf(&v3));
2640 v1.AddChildViewAt(&v2, 0);
2641 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2642 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2644 // Check that calling |AddChildView()| does not change the order.
2645 v1.AddChildView(&v2);
2646 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2647 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2648 v1.AddChildView(&v3);
2649 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2650 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2653 ////////////////////////////////////////////////////////////////////////////////
2654 // FocusManager
2655 ////////////////////////////////////////////////////////////////////////////////
2657 // A widget that always claims to be active, regardless of its real activation
2658 // status.
2659 class ActiveWidget : public Widget {
2660 public:
2661 ActiveWidget() {}
2662 ~ActiveWidget() override {}
2664 bool IsActive() const override { return true; }
2666 private:
2667 DISALLOW_COPY_AND_ASSIGN(ActiveWidget);
2670 TEST_F(ViewTest, AdvanceFocusIfNecessaryForUnfocusableView) {
2671 // Create a widget with two views and give the first one focus.
2672 ActiveWidget widget;
2673 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2674 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2675 widget.Init(params);
2677 View* view1 = new View();
2678 view1->SetFocusable(true);
2679 widget.GetRootView()->AddChildView(view1);
2680 View* view2 = new View();
2681 view2->SetFocusable(true);
2682 widget.GetRootView()->AddChildView(view2);
2684 FocusManager* focus_manager = widget.GetFocusManager();
2685 ASSERT_TRUE(focus_manager);
2687 focus_manager->SetFocusedView(view1);
2688 EXPECT_EQ(view1, focus_manager->GetFocusedView());
2690 // Disable the focused view and check if the next view gets focused.
2691 view1->SetEnabled(false);
2692 EXPECT_EQ(view2, focus_manager->GetFocusedView());
2694 // Re-enable and re-focus.
2695 view1->SetEnabled(true);
2696 focus_manager->SetFocusedView(view1);
2697 EXPECT_EQ(view1, focus_manager->GetFocusedView());
2699 // Hide the focused view and check it the next view gets focused.
2700 view1->SetVisible(false);
2701 EXPECT_EQ(view2, focus_manager->GetFocusedView());
2703 // Re-show and re-focus.
2704 view1->SetVisible(true);
2705 focus_manager->SetFocusedView(view1);
2706 EXPECT_EQ(view1, focus_manager->GetFocusedView());
2708 // Set the focused view as not focusable and check if the next view gets
2709 // focused.
2710 view1->SetFocusable(false);
2711 EXPECT_EQ(view2, focus_manager->GetFocusedView());
2714 ////////////////////////////////////////////////////////////////////////////////
2715 // Layers
2716 ////////////////////////////////////////////////////////////////////////////////
2718 namespace {
2720 // Test implementation of LayerAnimator.
2721 class TestLayerAnimator : public ui::LayerAnimator {
2722 public:
2723 TestLayerAnimator();
2725 const gfx::Rect& last_bounds() const { return last_bounds_; }
2727 // LayerAnimator.
2728 void SetBounds(const gfx::Rect& bounds) override;
2730 protected:
2731 ~TestLayerAnimator() override {}
2733 private:
2734 gfx::Rect last_bounds_;
2736 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
2739 TestLayerAnimator::TestLayerAnimator()
2740 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2743 void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) {
2744 last_bounds_ = bounds;
2747 } // namespace
2749 class ViewLayerTest : public ViewsTestBase {
2750 public:
2751 ViewLayerTest() : widget_(NULL) {}
2753 ~ViewLayerTest() override {}
2755 // Returns the Layer used by the RootView.
2756 ui::Layer* GetRootLayer() {
2757 return widget()->GetLayer();
2760 void SetUp() override {
2761 ViewTest::SetUp();
2762 widget_ = new Widget;
2763 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2764 params.bounds = gfx::Rect(50, 50, 200, 200);
2765 widget_->Init(params);
2766 widget_->Show();
2767 widget_->GetRootView()->SetBounds(0, 0, 200, 200);
2770 void TearDown() override {
2771 widget_->CloseNow();
2772 ViewsTestBase::TearDown();
2775 Widget* widget() { return widget_; }
2777 private:
2778 Widget* widget_;
2782 TEST_F(ViewLayerTest, LayerToggling) {
2783 // Because we lazily create textures the calls to DrawTree are necessary to
2784 // ensure we trigger creation of textures.
2785 ui::Layer* root_layer = widget()->GetLayer();
2786 View* content_view = new View;
2787 widget()->SetContentsView(content_view);
2789 // Create v1, give it a bounds and verify everything is set up correctly.
2790 View* v1 = new View;
2791 v1->SetPaintToLayer(true);
2792 EXPECT_TRUE(v1->layer() != NULL);
2793 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2794 content_view->AddChildView(v1);
2795 ASSERT_TRUE(v1->layer() != NULL);
2796 EXPECT_EQ(root_layer, v1->layer()->parent());
2797 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds());
2799 // Create v2 as a child of v1 and do basic assertion testing.
2800 View* v2 = new View;
2801 v1->AddChildView(v2);
2802 EXPECT_TRUE(v2->layer() == NULL);
2803 v2->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
2804 v2->SetPaintToLayer(true);
2805 ASSERT_TRUE(v2->layer() != NULL);
2806 EXPECT_EQ(v1->layer(), v2->layer()->parent());
2807 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
2809 // Turn off v1s layer. v2 should still have a layer but its parent should have
2810 // changed.
2811 v1->SetPaintToLayer(false);
2812 EXPECT_TRUE(v1->layer() == NULL);
2813 EXPECT_TRUE(v2->layer() != NULL);
2814 EXPECT_EQ(root_layer, v2->layer()->parent());
2815 ASSERT_EQ(1u, root_layer->children().size());
2816 EXPECT_EQ(root_layer->children()[0], v2->layer());
2817 // The bounds of the layer should have changed to be relative to the root view
2818 // now.
2819 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds());
2821 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2822 gfx::Transform transform;
2823 transform.Scale(2.0, 2.0);
2824 v1->SetTransform(transform);
2825 EXPECT_TRUE(v1->layer() != NULL);
2826 EXPECT_TRUE(v2->layer() != NULL);
2827 EXPECT_EQ(root_layer, v1->layer()->parent());
2828 EXPECT_EQ(v1->layer(), v2->layer()->parent());
2829 ASSERT_EQ(1u, root_layer->children().size());
2830 EXPECT_EQ(root_layer->children()[0], v1->layer());
2831 ASSERT_EQ(1u, v1->layer()->children().size());
2832 EXPECT_EQ(v1->layer()->children()[0], v2->layer());
2833 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
2836 // Verifies turning on a layer wires up children correctly.
2837 TEST_F(ViewLayerTest, NestedLayerToggling) {
2838 View* content_view = new View;
2839 widget()->SetContentsView(content_view);
2841 // Create v1, give it a bounds and verify everything is set up correctly.
2842 View* v1 = new View;
2843 content_view->AddChildView(v1);
2844 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2846 View* v2 = new View;
2847 v1->AddChildView(v2);
2849 View* v3 = new View;
2850 v3->SetPaintToLayer(true);
2851 v2->AddChildView(v3);
2852 ASSERT_TRUE(v3->layer() != NULL);
2854 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2856 v1->SetPaintToLayer(true);
2857 EXPECT_EQ(v1->layer(), v3->layer()->parent());
2860 TEST_F(ViewLayerTest, LayerAnimator) {
2861 View* content_view = new View;
2862 widget()->SetContentsView(content_view);
2864 View* v1 = new View;
2865 content_view->AddChildView(v1);
2866 v1->SetPaintToLayer(true);
2867 EXPECT_TRUE(v1->layer() != NULL);
2869 TestLayerAnimator* animator = new TestLayerAnimator();
2870 v1->layer()->SetAnimator(animator);
2872 gfx::Rect bounds(1, 2, 3, 4);
2873 v1->SetBoundsRect(bounds);
2874 EXPECT_EQ(bounds, animator->last_bounds());
2875 // TestLayerAnimator doesn't update the layer.
2876 EXPECT_NE(bounds, v1->layer()->bounds());
2879 // Verifies the bounds of a layer are updated if the bounds of ancestor that
2880 // doesn't have a layer change.
2881 TEST_F(ViewLayerTest, BoundsChangeWithLayer) {
2882 View* content_view = new View;
2883 widget()->SetContentsView(content_view);
2885 View* v1 = new View;
2886 content_view->AddChildView(v1);
2887 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2889 View* v2 = new View;
2890 v2->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
2891 v1->AddChildView(v2);
2892 v2->SetPaintToLayer(true);
2893 ASSERT_TRUE(v2->layer() != NULL);
2894 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
2896 v1->SetPosition(gfx::Point(25, 36));
2897 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
2899 v2->SetPosition(gfx::Point(11, 12));
2900 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
2902 // Bounds of the layer should change even if the view is not invisible.
2903 v1->SetVisible(false);
2904 v1->SetPosition(gfx::Point(20, 30));
2905 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
2907 v2->SetVisible(false);
2908 v2->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
2909 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
2912 // Make sure layers are positioned correctly in RTL.
2913 TEST_F(ViewLayerTest, BoundInRTL) {
2914 std::string locale = l10n_util::GetApplicationLocale(std::string());
2915 base::i18n::SetICUDefaultLocale("he");
2917 View* view = new View;
2918 widget()->SetContentsView(view);
2920 int content_width = view->width();
2922 // |v1| is initially not attached to anything. So its layer will have the same
2923 // bounds as the view.
2924 View* v1 = new View;
2925 v1->SetPaintToLayer(true);
2926 v1->SetBounds(10, 10, 20, 10);
2927 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
2928 v1->layer()->bounds());
2930 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
2931 // bounds.
2932 view->AddChildView(v1);
2933 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
2934 v1->layer()->bounds());
2935 gfx::Rect l1bounds = v1->layer()->bounds();
2937 // Now attach a View to the widget first, then create a layer for it. Make
2938 // sure the bounds are correct.
2939 View* v2 = new View;
2940 v2->SetBounds(50, 10, 30, 10);
2941 EXPECT_FALSE(v2->layer());
2942 view->AddChildView(v2);
2943 v2->SetPaintToLayer(true);
2944 EXPECT_EQ(gfx::Rect(content_width - 80, 10, 30, 10),
2945 v2->layer()->bounds());
2946 gfx::Rect l2bounds = v2->layer()->bounds();
2948 view->SetPaintToLayer(true);
2949 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2950 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2952 // Move one of the views. Make sure the layer is positioned correctly
2953 // afterwards.
2954 v1->SetBounds(v1->x() - 5, v1->y(), v1->width(), v1->height());
2955 l1bounds.set_x(l1bounds.x() + 5);
2956 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2958 view->SetPaintToLayer(false);
2959 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2960 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2962 // Move a view again.
2963 v2->SetBounds(v2->x() + 5, v2->y(), v2->width(), v2->height());
2964 l2bounds.set_x(l2bounds.x() - 5);
2965 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2967 // Reset locale.
2968 base::i18n::SetICUDefaultLocale(locale);
2971 // Make sure that resizing a parent in RTL correctly repositions its children.
2972 TEST_F(ViewLayerTest, ResizeParentInRTL) {
2973 std::string locale = l10n_util::GetApplicationLocale(std::string());
2974 base::i18n::SetICUDefaultLocale("he");
2976 View* view = new View;
2977 widget()->SetContentsView(view);
2979 int content_width = view->width();
2981 // Create a paints-to-layer view |v1|.
2982 View* v1 = new View;
2983 v1->SetPaintToLayer(true);
2984 v1->SetBounds(10, 10, 20, 10);
2985 view->AddChildView(v1);
2986 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
2987 v1->layer()->bounds());
2989 // Attach a paints-to-layer child view to |v1|.
2990 View* v2 = new View;
2991 v2->SetPaintToLayer(true);
2992 v2->SetBounds(3, 5, 6, 4);
2993 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
2994 v2->layer()->bounds());
2995 v1->AddChildView(v2);
2996 // Check that |v2| now has RTL-appropriate bounds.
2997 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
2998 v2->layer()->bounds());
3000 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
3001 View* v3 = new View;
3002 v3->SetBounds(1, 1, 18, 8);
3003 View* v4 = new View;
3004 v4->SetPaintToLayer(true);
3005 v4->SetBounds(2, 4, 6, 4);
3006 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
3007 v4->layer()->bounds());
3008 v3->AddChildView(v4);
3009 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
3010 v4->layer()->bounds());
3011 v1->AddChildView(v3);
3012 // Check that |v4| now has RTL-appropriate bounds.
3013 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3014 v4->layer()->bounds());
3016 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3017 // correctly to RTL-appropriate bounds.
3018 v1->SetSize(gfx::Size(30, 10));
3019 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3020 v2->layer()->bounds());
3021 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3022 v4->layer()->bounds());
3024 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3025 // to RTL-appropriate bounds.
3026 v3->SetBounds(2, 1, 12, 8);
3027 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3028 v4->layer()->bounds());
3030 // Reset locale.
3031 base::i18n::SetICUDefaultLocale(locale);
3034 // Makes sure a transform persists after toggling the visibility.
3035 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
3036 View* view = new View;
3037 gfx::Transform transform;
3038 transform.Scale(2.0, 2.0);
3039 view->SetTransform(transform);
3040 widget()->SetContentsView(view);
3041 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3043 view->SetVisible(false);
3044 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3046 view->SetVisible(true);
3047 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3050 // Verifies a transform persists after removing/adding a view with a transform.
3051 TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) {
3052 View* view = new View;
3053 gfx::Transform transform;
3054 transform.Scale(2.0, 2.0);
3055 view->SetTransform(transform);
3056 widget()->SetContentsView(view);
3057 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3058 ASSERT_TRUE(view->layer() != NULL);
3059 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3061 View* parent = view->parent();
3062 parent->RemoveChildView(view);
3063 parent->AddChildView(view);
3065 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3066 ASSERT_TRUE(view->layer() != NULL);
3067 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3070 // Makes sure that layer visibility is correct after toggling View visibility.
3071 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
3072 View* content_view = new View;
3073 widget()->SetContentsView(content_view);
3075 // The view isn't attached to a widget or a parent view yet. But it should
3076 // still have a layer, but the layer should not be attached to the root
3077 // layer.
3078 View* v1 = new View;
3079 v1->SetPaintToLayer(true);
3080 EXPECT_TRUE(v1->layer());
3081 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3082 v1->layer()));
3084 // Once the view is attached to a widget, its layer should be attached to the
3085 // root layer and visible.
3086 content_view->AddChildView(v1);
3087 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3088 v1->layer()));
3089 EXPECT_TRUE(v1->layer()->IsDrawn());
3091 v1->SetVisible(false);
3092 EXPECT_FALSE(v1->layer()->IsDrawn());
3094 v1->SetVisible(true);
3095 EXPECT_TRUE(v1->layer()->IsDrawn());
3097 widget()->Hide();
3098 EXPECT_FALSE(v1->layer()->IsDrawn());
3100 widget()->Show();
3101 EXPECT_TRUE(v1->layer()->IsDrawn());
3104 // Tests that the layers in the subtree are orphaned after a View is removed
3105 // from the parent.
3106 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
3107 View* content_view = new View;
3108 widget()->SetContentsView(content_view);
3110 View* v1 = new View;
3111 content_view->AddChildView(v1);
3113 View* v2 = new View;
3114 v1->AddChildView(v2);
3115 v2->SetPaintToLayer(true);
3116 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3117 v2->layer()));
3118 EXPECT_TRUE(v2->layer()->IsDrawn());
3120 content_view->RemoveChildView(v1);
3122 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3123 v2->layer()));
3125 // Reparent |v2|.
3126 content_view->AddChildView(v2);
3127 delete v1;
3128 v1 = NULL;
3129 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3130 v2->layer()));
3131 EXPECT_TRUE(v2->layer()->IsDrawn());
3134 class PaintTrackingView : public View {
3135 public:
3136 PaintTrackingView() : painted_(false) {
3139 bool painted() const { return painted_; }
3140 void set_painted(bool value) { painted_ = value; }
3142 void OnPaint(gfx::Canvas* canvas) override { painted_ = true; }
3144 private:
3145 bool painted_;
3147 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView);
3150 // Makes sure child views with layers aren't painted when paint starts at an
3151 // ancestor.
3152 TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) {
3153 PaintTrackingView* content_view = new PaintTrackingView;
3154 widget()->SetContentsView(content_view);
3155 content_view->SetPaintToLayer(true);
3156 GetRootLayer()->GetCompositor()->ScheduleDraw();
3157 ui::DrawWaiterForTest::WaitForCompositingEnded(
3158 GetRootLayer()->GetCompositor());
3159 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3160 content_view->set_painted(false);
3161 // content_view no longer has a dirty rect. Paint from the root and make sure
3162 // PaintTrackingView isn't painted.
3163 GetRootLayer()->GetCompositor()->ScheduleDraw();
3164 ui::DrawWaiterForTest::WaitForCompositingEnded(
3165 GetRootLayer()->GetCompositor());
3166 EXPECT_FALSE(content_view->painted());
3168 // Make content_view have a dirty rect, paint the layers and make sure
3169 // PaintTrackingView is painted.
3170 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3171 GetRootLayer()->GetCompositor()->ScheduleDraw();
3172 ui::DrawWaiterForTest::WaitForCompositingEnded(
3173 GetRootLayer()->GetCompositor());
3174 EXPECT_TRUE(content_view->painted());
3177 // Tests that the visibility of child layers are updated correctly when a View's
3178 // visibility changes.
3179 TEST_F(ViewLayerTest, VisibilityChildLayers) {
3180 View* v1 = new View;
3181 v1->SetPaintToLayer(true);
3182 widget()->SetContentsView(v1);
3184 View* v2 = new View;
3185 v1->AddChildView(v2);
3187 View* v3 = new View;
3188 v2->AddChildView(v3);
3189 v3->SetVisible(false);
3191 View* v4 = new View;
3192 v4->SetPaintToLayer(true);
3193 v3->AddChildView(v4);
3195 EXPECT_TRUE(v1->layer()->IsDrawn());
3196 EXPECT_FALSE(v4->layer()->IsDrawn());
3198 v2->SetVisible(false);
3199 EXPECT_TRUE(v1->layer()->IsDrawn());
3200 EXPECT_FALSE(v4->layer()->IsDrawn());
3202 v2->SetVisible(true);
3203 EXPECT_TRUE(v1->layer()->IsDrawn());
3204 EXPECT_FALSE(v4->layer()->IsDrawn());
3206 v2->SetVisible(false);
3207 EXPECT_TRUE(v1->layer()->IsDrawn());
3208 EXPECT_FALSE(v4->layer()->IsDrawn());
3209 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3211 v3->SetVisible(true);
3212 EXPECT_TRUE(v1->layer()->IsDrawn());
3213 EXPECT_FALSE(v4->layer()->IsDrawn());
3214 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3216 // Reparent |v3| to |v1|.
3217 v1->AddChildView(v3);
3218 EXPECT_TRUE(v1->layer()->IsDrawn());
3219 EXPECT_TRUE(v4->layer()->IsDrawn());
3220 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3223 // This test creates a random View tree, and then randomly reorders child views,
3224 // reparents views etc. Unrelated changes can appear to break this test. So
3225 // marking this as FLAKY.
3226 TEST_F(ViewLayerTest, DISABLED_ViewLayerTreesInSync) {
3227 View* content = new View;
3228 content->SetPaintToLayer(true);
3229 widget()->SetContentsView(content);
3230 widget()->Show();
3232 ConstructTree(content, 5);
3233 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3235 ScrambleTree(content);
3236 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3238 ScrambleTree(content);
3239 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3241 ScrambleTree(content);
3242 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3245 // Verifies when views are reordered the layer is also reordered. The widget is
3246 // providing the parent layer.
3247 TEST_F(ViewLayerTest, ReorderUnderWidget) {
3248 View* content = new View;
3249 widget()->SetContentsView(content);
3250 View* c1 = new View;
3251 c1->SetPaintToLayer(true);
3252 content->AddChildView(c1);
3253 View* c2 = new View;
3254 c2->SetPaintToLayer(true);
3255 content->AddChildView(c2);
3257 ui::Layer* parent_layer = c1->layer()->parent();
3258 ASSERT_TRUE(parent_layer);
3259 ASSERT_EQ(2u, parent_layer->children().size());
3260 EXPECT_EQ(c1->layer(), parent_layer->children()[0]);
3261 EXPECT_EQ(c2->layer(), parent_layer->children()[1]);
3263 // Move c1 to the front. The layers should have moved too.
3264 content->ReorderChildView(c1, -1);
3265 EXPECT_EQ(c1->layer(), parent_layer->children()[1]);
3266 EXPECT_EQ(c2->layer(), parent_layer->children()[0]);
3269 // Verifies that the layer of a view can be acquired properly.
3270 TEST_F(ViewLayerTest, AcquireLayer) {
3271 View* content = new View;
3272 widget()->SetContentsView(content);
3273 scoped_ptr<View> c1(new View);
3274 c1->SetPaintToLayer(true);
3275 EXPECT_TRUE(c1->layer());
3276 content->AddChildView(c1.get());
3278 scoped_ptr<ui::Layer> layer(c1->AcquireLayer());
3279 EXPECT_EQ(layer.get(), c1->layer());
3281 scoped_ptr<ui::Layer> layer2(c1->RecreateLayer());
3282 EXPECT_NE(c1->layer(), layer2.get());
3284 // Destroy view before destroying layer.
3285 c1.reset();
3288 // Verify the z-order of the layers as a result of calling RecreateLayer().
3289 TEST_F(ViewLayerTest, RecreateLayerZOrder) {
3290 scoped_ptr<View> v(new View());
3291 v->SetPaintToLayer(true);
3293 View* v1 = new View();
3294 v1->SetPaintToLayer(true);
3295 v->AddChildView(v1);
3296 View* v2 = new View();
3297 v2->SetPaintToLayer(true);
3298 v->AddChildView(v2);
3300 // Test the initial z-order.
3301 const std::vector<ui::Layer*>& child_layers_pre = v->layer()->children();
3302 ASSERT_EQ(2u, child_layers_pre.size());
3303 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3304 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3306 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3308 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3309 // for |v1| and |v2|.
3310 const std::vector<ui::Layer*>& child_layers_post = v->layer()->children();
3311 ASSERT_EQ(3u, child_layers_post.size());
3312 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3313 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3314 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3317 // Verify the z-order of the layers as a result of calling RecreateLayer when
3318 // the widget is the parent with the layer.
3319 TEST_F(ViewLayerTest, RecreateLayerZOrderWidgetParent) {
3320 View* v = new View();
3321 widget()->SetContentsView(v);
3323 View* v1 = new View();
3324 v1->SetPaintToLayer(true);
3325 v->AddChildView(v1);
3326 View* v2 = new View();
3327 v2->SetPaintToLayer(true);
3328 v->AddChildView(v2);
3330 ui::Layer* root_layer = GetRootLayer();
3332 // Test the initial z-order.
3333 const std::vector<ui::Layer*>& child_layers_pre = root_layer->children();
3334 ASSERT_EQ(2u, child_layers_pre.size());
3335 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3336 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3338 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3340 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3341 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3342 ASSERT_EQ(3u, child_layers_post.size());
3343 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3344 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3345 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3348 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3349 // a View.
3350 TEST_F(ViewLayerTest, RecreateLayerMovesNonViewChildren) {
3351 View v;
3352 v.SetPaintToLayer(true);
3353 View child;
3354 child.SetPaintToLayer(true);
3355 v.AddChildView(&child);
3356 ASSERT_TRUE(v.layer() != NULL);
3357 ASSERT_EQ(1u, v.layer()->children().size());
3358 EXPECT_EQ(v.layer()->children()[0], child.layer());
3360 ui::Layer layer(ui::LAYER_NOT_DRAWN);
3361 v.layer()->Add(&layer);
3362 v.layer()->StackAtBottom(&layer);
3364 scoped_ptr<ui::Layer> old_layer(v.RecreateLayer());
3366 // All children should be moved from old layer to new layer.
3367 ASSERT_TRUE(old_layer.get() != NULL);
3368 EXPECT_TRUE(old_layer->children().empty());
3370 // And new layer should have the two children.
3371 ASSERT_TRUE(v.layer() != NULL);
3372 ASSERT_EQ(2u, v.layer()->children().size());
3373 EXPECT_EQ(v.layer()->children()[0], &layer);
3374 EXPECT_EQ(v.layer()->children()[1], child.layer());
3377 class BoundsTreeTestView : public View {
3378 public:
3379 BoundsTreeTestView() {}
3381 void PaintChildren(gfx::Canvas* canvas, const CullSet& cull_set) override {
3382 // Save out a copy of the cull_set before calling the base implementation.
3383 last_cull_set_.clear();
3384 if (cull_set.cull_set_) {
3385 for (base::hash_set<intptr_t>::iterator it = cull_set.cull_set_->begin();
3386 it != cull_set.cull_set_->end();
3387 ++it) {
3388 last_cull_set_.insert(reinterpret_cast<View*>(*it));
3391 View::PaintChildren(canvas, cull_set);
3394 std::set<View*> last_cull_set_;
3397 TEST_F(ViewLayerTest, BoundsTreePaintUpdatesCullSet) {
3398 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3399 widget()->SetContentsView(test_view);
3401 View* v1 = new View();
3402 v1->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3403 test_view->AddChildView(v1);
3405 View* v2 = new View();
3406 v2->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3407 v1->AddChildView(v2);
3409 // Schedule a full-view paint to get everyone's rectangles updated.
3410 PaintWidgetInRect(widget(), test_view->bounds());
3412 // Now we have test_view - v1 - v2. Damage to only test_view should only
3413 // return root_view and test_view.
3414 PaintWidgetInRect(widget(), gfx::Rect(0, 0, 1, 1));
3415 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3416 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3417 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3419 // Damage to v1 only should only return root_view, test_view, and v1.
3420 PaintWidgetInRect(widget(), gfx::Rect(11, 16, 1, 1));
3421 EXPECT_EQ(3U, test_view->last_cull_set_.size());
3422 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3423 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3424 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3426 // A Damage rect inside v2 should get all 3 views back in the |last_cull_set_|
3427 // on call to TestView::Paint(), along with the widget root view.
3428 PaintWidgetInRect(widget(), gfx::Rect(31, 49, 1, 1));
3429 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3430 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3431 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3432 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3433 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3436 TEST_F(ViewLayerTest, BoundsTreeWithRTL) {
3437 std::string locale = l10n_util::GetApplicationLocale(std::string());
3438 base::i18n::SetICUDefaultLocale("ar");
3440 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3441 widget()->SetContentsView(test_view);
3443 // Add child views, which should be in RTL coordinate space of parent view.
3444 View* v1 = new View;
3445 v1->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3446 test_view->AddChildView(v1);
3448 View* v2 = new View;
3449 v2->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3450 v1->AddChildView(v2);
3452 // Schedule a full-view paint to get everyone's rectangles updated.
3453 PaintWidgetInRect(widget(), test_view->bounds());
3455 // Damage to the right side of the parent view should touch both child views.
3456 gfx::Rect rtl_damage(test_view->bounds().width() - 16, 18, 1, 1);
3457 PaintWidgetInRect(widget(), rtl_damage);
3458 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3459 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3460 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3461 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3462 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3464 // Damage to the left side of the parent view should only touch the
3465 // container views.
3466 gfx::Rect ltr_damage(16, 18, 1, 1);
3467 PaintWidgetInRect(widget(), ltr_damage);
3468 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3469 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3470 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3472 // Reset locale.
3473 base::i18n::SetICUDefaultLocale(locale);
3476 TEST_F(ViewLayerTest, BoundsTreeSetBoundsChangesCullSet) {
3477 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3478 widget()->SetContentsView(test_view);
3480 View* v1 = new View;
3481 v1->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3482 test_view->AddChildView(v1);
3484 View* v2 = new View;
3485 v2->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3486 v1->AddChildView(v2);
3488 // Schedule a full-view paint to get everyone's rectangles updated.
3489 PaintWidgetInRect(widget(), test_view->bounds());
3491 // Move v1 to a new origin out of the way of our next query.
3492 v1->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3493 // The move will force a repaint.
3494 PaintWidgetInRect(widget(), test_view->bounds());
3496 // Schedule a paint with damage rect where v1 used to be.
3497 PaintWidgetInRect(widget(), gfx::Rect(5, 6, 10, 11));
3499 // Should only have picked up root_view and test_view.
3500 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3501 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3502 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3505 TEST_F(ViewLayerTest, BoundsTreeLayerChangeMakesNewTree) {
3506 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3507 widget()->SetContentsView(test_view);
3509 View* v1 = new View;
3510 v1->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3511 test_view->AddChildView(v1);
3513 View* v2 = new View;
3514 v2->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3515 v1->AddChildView(v2);
3517 // Schedule a full-view paint to get everyone's rectangles updated.
3518 PaintWidgetInRect(widget(), test_view->bounds());
3520 // Set v1 to paint to its own layer, it should remove itself from the
3521 // test_view heiarchy and no longer intersect with damage rects in that cull
3522 // set.
3523 v1->SetPaintToLayer(true);
3525 // Schedule another full-view paint.
3526 PaintWidgetInRect(widget(), test_view->bounds());
3527 // v1 and v2 should no longer be present in the test_view cull_set.
3528 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3529 EXPECT_EQ(0U, test_view->last_cull_set_.count(v1));
3530 EXPECT_EQ(0U, test_view->last_cull_set_.count(v2));
3532 // Now set v1 back to not painting to a layer.
3533 v1->SetPaintToLayer(false);
3534 // Schedule another full-view paint.
3535 PaintWidgetInRect(widget(), test_view->bounds());
3536 // We should be back to the full cull set including v1 and v2.
3537 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3538 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3539 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3540 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3541 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3544 TEST_F(ViewLayerTest, BoundsTreeRemoveChildRemovesBounds) {
3545 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3546 widget()->SetContentsView(test_view);
3548 View* v1 = new View;
3549 v1->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3550 test_view->AddChildView(v1);
3552 View* v2 = new View;
3553 v2->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3554 v1->AddChildView(v2);
3556 // Schedule a full-view paint to get everyone's rectangles updated.
3557 PaintWidgetInRect(widget(), test_view->bounds());
3559 // Now remove v1 from the root view.
3560 test_view->RemoveChildView(v1);
3562 // Schedule another full-view paint.
3563 PaintWidgetInRect(widget(), test_view->bounds());
3564 // v1 and v2 should no longer be present in the test_view cull_set.
3565 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3566 EXPECT_EQ(0U, test_view->last_cull_set_.count(v1));
3567 EXPECT_EQ(0U, test_view->last_cull_set_.count(v2));
3569 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3570 // deleted with that hierarchy.
3571 delete v1;
3574 TEST_F(ViewLayerTest, BoundsTreeMoveViewMovesBounds) {
3575 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3576 widget()->SetContentsView(test_view);
3578 // Build hierarchy v1 - v2 - v3.
3579 View* v1 = new View;
3580 v1->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3581 test_view->AddChildView(v1);
3583 View* v2 = new View;
3584 v2->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3585 v1->AddChildView(v2);
3587 View* v3 = new View;
3588 v3->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3589 v2->AddChildView(v3);
3591 // Schedule a full-view paint and ensure all views are present in the cull.
3592 PaintWidgetInRect(widget(), test_view->bounds());
3593 EXPECT_EQ(5U, test_view->last_cull_set_.size());
3594 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3595 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3596 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3597 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3598 EXPECT_EQ(1U, test_view->last_cull_set_.count(v3));
3600 // Build an unrelated view hierarchy and move v2 in to it.
3601 scoped_ptr<Widget> test_widget(new Widget);
3602 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3603 params.bounds = gfx::Rect(10, 10, 500, 500);
3604 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3605 test_widget->Init(params);
3606 test_widget->Show();
3607 BoundsTreeTestView* widget_view = new BoundsTreeTestView;
3608 test_widget->SetContentsView(widget_view);
3609 widget_view->AddChildView(v2);
3611 // Now schedule full-view paints in both widgets.
3612 PaintWidgetInRect(widget(), test_view->bounds());
3613 PaintWidgetInRect(test_widget.get(), widget_view->bounds());
3615 // Only v1 should be present in the first cull set.
3616 EXPECT_EQ(3U, test_view->last_cull_set_.size());
3617 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3618 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3619 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3621 // We should find v2 and v3 in the widget_view cull_set.
3622 EXPECT_EQ(4U, widget_view->last_cull_set_.size());
3623 EXPECT_EQ(1U, widget_view->last_cull_set_.count(test_widget->GetRootView()));
3624 EXPECT_EQ(1U, widget_view->last_cull_set_.count(widget_view));
3625 EXPECT_EQ(1U, widget_view->last_cull_set_.count(v2));
3626 EXPECT_EQ(1U, widget_view->last_cull_set_.count(v3));
3629 namespace {
3631 std::string ToString(const gfx::Vector2dF& vector) {
3632 return base::StringPrintf("%.2f %0.2f", vector.x(), vector.y());
3635 } // namespace
3637 TEST_F(ViewLayerTest, SnapLayerToPixel) {
3638 View* v1 = new View;
3640 View* v11 = new View;
3641 v1->AddChildView(v11);
3643 widget()->SetContentsView(v1);
3645 const gfx::Size& size = GetRootLayer()->GetCompositor()->size();
3646 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f, size);
3648 v11->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3649 v1->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3650 v11->SetPaintToLayer(true);
3652 EXPECT_EQ("0.40 0.40", ToString(v11->layer()->subpixel_position_offset()));
3654 // Creating a layer in parent should update the child view's layer offset.
3655 v1->SetPaintToLayer(true);
3656 EXPECT_EQ("-0.20 -0.20", ToString(v1->layer()->subpixel_position_offset()));
3657 EXPECT_EQ("-0.20 -0.20", ToString(v11->layer()->subpixel_position_offset()));
3659 // DSF change should get propagated and update offsets.
3660 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f, size);
3661 EXPECT_EQ("0.33 0.33", ToString(v1->layer()->subpixel_position_offset()));
3662 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3664 // Deleting parent's layer should update the child view's layer's offset.
3665 v1->SetPaintToLayer(false);
3666 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3668 // Setting parent view should update the child view's layer's offset.
3669 v1->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3670 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3672 // Setting integral DSF should reset the offset.
3673 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f, size);
3674 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3677 TEST_F(ViewTest, FocusableAssertions) {
3678 // View subclasses may change insets based on whether they are focusable,
3679 // which effects the preferred size. To avoid preferred size changing around
3680 // these Views need to key off the last value set to SetFocusable(), not
3681 // whether the View is focusable right now. For this reason it's important
3682 // that focusable() return the last value passed to SetFocusable and not
3683 // whether the View is focusable right now.
3684 TestView view;
3685 view.SetFocusable(true);
3686 EXPECT_TRUE(view.focusable());
3687 view.SetEnabled(false);
3688 EXPECT_TRUE(view.focusable());
3689 view.SetFocusable(false);
3690 EXPECT_FALSE(view.focusable());
3693 // Verifies when a view is deleted it is removed from ViewStorage.
3694 TEST_F(ViewTest, UpdateViewStorageOnDelete) {
3695 ViewStorage* view_storage = ViewStorage::GetInstance();
3696 const int storage_id = view_storage->CreateStorageID();
3698 View view;
3699 view_storage->StoreView(storage_id, &view);
3701 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL);
3704 ////////////////////////////////////////////////////////////////////////////////
3705 // NativeTheme
3706 ////////////////////////////////////////////////////////////////////////////////
3708 void TestView::OnNativeThemeChanged(const ui::NativeTheme* native_theme) {
3709 native_theme_ = native_theme;
3712 TEST_F(ViewTest, OnNativeThemeChanged) {
3713 TestView* test_view = new TestView();
3714 EXPECT_FALSE(test_view->native_theme_);
3715 TestView* test_view_child = new TestView();
3716 EXPECT_FALSE(test_view_child->native_theme_);
3718 // Child view added before the widget hierarchy exists should get the
3719 // new native theme notification.
3720 test_view->AddChildView(test_view_child);
3722 scoped_ptr<Widget> widget(new Widget);
3723 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
3724 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3725 widget->Init(params);
3727 widget->GetRootView()->AddChildView(test_view);
3728 EXPECT_TRUE(test_view->native_theme_);
3729 EXPECT_EQ(widget->GetNativeTheme(), test_view->native_theme_);
3730 EXPECT_TRUE(test_view_child->native_theme_);
3731 EXPECT_EQ(widget->GetNativeTheme(), test_view_child->native_theme_);
3733 // Child view added after the widget hierarchy exists should also get the
3734 // notification.
3735 TestView* test_view_child_2 = new TestView();
3736 test_view->AddChildView(test_view_child_2);
3737 EXPECT_TRUE(test_view_child_2->native_theme_);
3738 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_);
3740 widget->CloseNow();
3743 } // namespace views