ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / views / view_unittest.cc
blobcc15602d3eaa433056256b48855900f9f76f1c80
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 } // namespace
182 namespace views {
184 typedef ViewsTestBase ViewTest;
186 // A derived class for testing purpose.
187 class TestView : public View {
188 public:
189 TestView()
190 : View(),
191 delete_on_pressed_(false),
192 native_theme_(NULL),
193 can_process_events_within_subtree_(true) {}
194 ~TestView() override {}
196 // Reset all test state
197 void Reset() {
198 did_change_bounds_ = false;
199 last_mouse_event_type_ = 0;
200 location_.SetPoint(0, 0);
201 received_mouse_enter_ = false;
202 received_mouse_exit_ = false;
203 last_clip_.setEmpty();
204 accelerator_count_map_.clear();
205 can_process_events_within_subtree_ = true;
208 // Exposed as public for testing.
209 void DoFocus() {
210 views::View::Focus();
213 void DoBlur() {
214 views::View::Blur();
217 bool focusable() const { return View::focusable(); }
219 void set_can_process_events_within_subtree(bool can_process) {
220 can_process_events_within_subtree_ = can_process;
223 bool CanProcessEventsWithinSubtree() const override {
224 return can_process_events_within_subtree_;
227 void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
228 bool OnMousePressed(const ui::MouseEvent& event) override;
229 bool OnMouseDragged(const ui::MouseEvent& event) override;
230 void OnMouseReleased(const ui::MouseEvent& event) override;
231 void OnMouseEntered(const ui::MouseEvent& event) override;
232 void OnMouseExited(const ui::MouseEvent& event) override;
234 void Paint(gfx::Canvas* canvas, const CullSet& cull_set) override;
235 void SchedulePaintInRect(const gfx::Rect& rect) override;
236 bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
238 void OnNativeThemeChanged(const ui::NativeTheme* native_theme) override;
240 // OnBoundsChanged.
241 bool did_change_bounds_;
242 gfx::Rect new_bounds_;
244 // MouseEvent.
245 int last_mouse_event_type_;
246 gfx::Point location_;
247 bool received_mouse_enter_;
248 bool received_mouse_exit_;
249 bool delete_on_pressed_;
251 // Painting.
252 std::vector<gfx::Rect> scheduled_paint_rects_;
254 // Painting.
255 SkRect last_clip_;
257 // Accelerators.
258 std::map<ui::Accelerator, int> accelerator_count_map_;
260 // Native theme.
261 const ui::NativeTheme* native_theme_;
263 // Value to return from CanProcessEventsWithinSubtree().
264 bool can_process_events_within_subtree_;
267 ////////////////////////////////////////////////////////////////////////////////
268 // OnBoundsChanged
269 ////////////////////////////////////////////////////////////////////////////////
271 void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
272 did_change_bounds_ = true;
273 new_bounds_ = bounds();
276 TEST_F(ViewTest, OnBoundsChanged) {
277 TestView v;
279 gfx::Rect prev_rect(0, 0, 200, 200);
280 gfx::Rect new_rect(100, 100, 250, 250);
282 v.SetBoundsRect(prev_rect);
283 v.Reset();
284 v.SetBoundsRect(new_rect);
286 EXPECT_TRUE(v.did_change_bounds_);
287 EXPECT_EQ(v.new_bounds_, new_rect);
288 EXPECT_EQ(v.bounds(), new_rect);
291 ////////////////////////////////////////////////////////////////////////////////
292 // MouseEvent
293 ////////////////////////////////////////////////////////////////////////////////
295 bool TestView::OnMousePressed(const ui::MouseEvent& event) {
296 last_mouse_event_type_ = event.type();
297 location_.SetPoint(event.x(), event.y());
298 if (delete_on_pressed_)
299 delete this;
300 return true;
303 bool TestView::OnMouseDragged(const ui::MouseEvent& event) {
304 last_mouse_event_type_ = event.type();
305 location_.SetPoint(event.x(), event.y());
306 return true;
309 void TestView::OnMouseReleased(const ui::MouseEvent& event) {
310 last_mouse_event_type_ = event.type();
311 location_.SetPoint(event.x(), event.y());
314 void TestView::OnMouseEntered(const ui::MouseEvent& event) {
315 received_mouse_enter_ = true;
318 void TestView::OnMouseExited(const ui::MouseEvent& event) {
319 received_mouse_exit_ = true;
322 TEST_F(ViewTest, MouseEvent) {
323 TestView* v1 = new TestView();
324 v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
326 TestView* v2 = new TestView();
327 v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
329 scoped_ptr<Widget> widget(new Widget);
330 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
331 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
332 params.bounds = gfx::Rect(50, 50, 650, 650);
333 widget->Init(params);
334 internal::RootView* root =
335 static_cast<internal::RootView*>(widget->GetRootView());
337 root->AddChildView(v1);
338 v1->AddChildView(v2);
340 v1->Reset();
341 v2->Reset();
343 gfx::Point p1(110, 120);
344 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
345 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
346 root->OnMousePressed(pressed);
347 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_PRESSED);
348 EXPECT_EQ(v2->location_.x(), 10);
349 EXPECT_EQ(v2->location_.y(), 20);
350 // Make sure v1 did not receive the event
351 EXPECT_EQ(v1->last_mouse_event_type_, 0);
353 // Drag event out of bounds. Should still go to v2
354 v1->Reset();
355 v2->Reset();
356 gfx::Point p2(50, 40);
357 ui::MouseEvent dragged(ui::ET_MOUSE_DRAGGED, p2, p2, ui::EventTimeForNow(),
358 ui::EF_LEFT_MOUSE_BUTTON, 0);
359 root->OnMouseDragged(dragged);
360 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_DRAGGED);
361 EXPECT_EQ(v2->location_.x(), -50);
362 EXPECT_EQ(v2->location_.y(), -60);
363 // Make sure v1 did not receive the event
364 EXPECT_EQ(v1->last_mouse_event_type_, 0);
366 // Releasted event out of bounds. Should still go to v2
367 v1->Reset();
368 v2->Reset();
369 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
370 ui::EventTimeForNow(), 0, 0);
371 root->OnMouseDragged(released);
372 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_RELEASED);
373 EXPECT_EQ(v2->location_.x(), -100);
374 EXPECT_EQ(v2->location_.y(), -100);
375 // Make sure v1 did not receive the event
376 EXPECT_EQ(v1->last_mouse_event_type_, 0);
378 widget->CloseNow();
381 // Confirm that a view can be deleted as part of processing a mouse press.
382 TEST_F(ViewTest, DeleteOnPressed) {
383 TestView* v1 = new TestView();
384 v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
386 TestView* v2 = new TestView();
387 v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
389 v1->Reset();
390 v2->Reset();
392 scoped_ptr<Widget> widget(new Widget);
393 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
394 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
395 params.bounds = gfx::Rect(50, 50, 650, 650);
396 widget->Init(params);
397 View* root = widget->GetRootView();
399 root->AddChildView(v1);
400 v1->AddChildView(v2);
402 v2->delete_on_pressed_ = true;
403 gfx::Point point(110, 120);
404 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, point, point,
405 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
406 ui::EF_LEFT_MOUSE_BUTTON);
407 root->OnMousePressed(pressed);
408 EXPECT_EQ(0, v1->child_count());
410 widget->CloseNow();
413 ////////////////////////////////////////////////////////////////////////////////
414 // Painting
415 ////////////////////////////////////////////////////////////////////////////////
417 void TestView::Paint(gfx::Canvas* canvas, const CullSet& cull_set) {
418 canvas->sk_canvas()->getClipBounds(&last_clip_);
421 void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
422 scheduled_paint_rects_.push_back(rect);
423 View::SchedulePaintInRect(rect);
426 void CheckRect(const SkRect& check_rect, const SkRect& target_rect) {
427 EXPECT_EQ(target_rect.fLeft, check_rect.fLeft);
428 EXPECT_EQ(target_rect.fRight, check_rect.fRight);
429 EXPECT_EQ(target_rect.fTop, check_rect.fTop);
430 EXPECT_EQ(target_rect.fBottom, check_rect.fBottom);
433 TEST_F(ViewTest, RemoveNotification) {
434 ViewStorage* vs = ViewStorage::GetInstance();
435 Widget* widget = new Widget;
436 widget->Init(CreateParams(Widget::InitParams::TYPE_POPUP));
437 View* root_view = widget->GetRootView();
439 View* v1 = new View;
440 int s1 = vs->CreateStorageID();
441 vs->StoreView(s1, v1);
442 root_view->AddChildView(v1);
443 View* v11 = new View;
444 int s11 = vs->CreateStorageID();
445 vs->StoreView(s11, v11);
446 v1->AddChildView(v11);
447 View* v111 = new View;
448 int s111 = vs->CreateStorageID();
449 vs->StoreView(s111, v111);
450 v11->AddChildView(v111);
451 View* v112 = new View;
452 int s112 = vs->CreateStorageID();
453 vs->StoreView(s112, v112);
454 v11->AddChildView(v112);
455 View* v113 = new View;
456 int s113 = vs->CreateStorageID();
457 vs->StoreView(s113, v113);
458 v11->AddChildView(v113);
459 View* v1131 = new View;
460 int s1131 = vs->CreateStorageID();
461 vs->StoreView(s1131, v1131);
462 v113->AddChildView(v1131);
463 View* v12 = new View;
464 int s12 = vs->CreateStorageID();
465 vs->StoreView(s12, v12);
466 v1->AddChildView(v12);
468 View* v2 = new View;
469 int s2 = vs->CreateStorageID();
470 vs->StoreView(s2, v2);
471 root_view->AddChildView(v2);
472 View* v21 = new View;
473 int s21 = vs->CreateStorageID();
474 vs->StoreView(s21, v21);
475 v2->AddChildView(v21);
476 View* v211 = new View;
477 int s211 = vs->CreateStorageID();
478 vs->StoreView(s211, v211);
479 v21->AddChildView(v211);
481 size_t stored_views = vs->view_count();
483 // Try removing a leaf view.
484 v21->RemoveChildView(v211);
485 EXPECT_EQ(stored_views - 1, vs->view_count());
486 EXPECT_EQ(NULL, vs->RetrieveView(s211));
487 delete v211; // We won't use this one anymore.
489 // Now try removing a view with a hierarchy of depth 1.
490 v11->RemoveChildView(v113);
491 EXPECT_EQ(stored_views - 3, vs->view_count());
492 EXPECT_EQ(NULL, vs->RetrieveView(s113));
493 EXPECT_EQ(NULL, vs->RetrieveView(s1131));
494 delete v113; // We won't use this one anymore.
496 // Now remove even more.
497 root_view->RemoveChildView(v1);
498 EXPECT_EQ(NULL, vs->RetrieveView(s1));
499 EXPECT_EQ(NULL, vs->RetrieveView(s11));
500 EXPECT_EQ(NULL, vs->RetrieveView(s12));
501 EXPECT_EQ(NULL, vs->RetrieveView(s111));
502 EXPECT_EQ(NULL, vs->RetrieveView(s112));
504 // Put v1 back for more tests.
505 root_view->AddChildView(v1);
506 vs->StoreView(s1, v1);
508 // Synchronously closing the window deletes the view hierarchy, which should
509 // remove all its views from ViewStorage.
510 widget->CloseNow();
511 EXPECT_EQ(stored_views - 10, vs->view_count());
512 EXPECT_EQ(NULL, vs->RetrieveView(s1));
513 EXPECT_EQ(NULL, vs->RetrieveView(s12));
514 EXPECT_EQ(NULL, vs->RetrieveView(s11));
515 EXPECT_EQ(NULL, vs->RetrieveView(s12));
516 EXPECT_EQ(NULL, vs->RetrieveView(s21));
517 EXPECT_EQ(NULL, vs->RetrieveView(s111));
518 EXPECT_EQ(NULL, vs->RetrieveView(s112));
521 namespace {
523 void RotateCounterclockwise(gfx::Transform* transform) {
524 transform->matrix().set3x3(0, -1, 0,
525 1, 0, 0,
526 0, 0, 1);
529 void RotateClockwise(gfx::Transform* transform) {
530 transform->matrix().set3x3( 0, 1, 0,
531 -1, 0, 0,
532 0, 0, 1);
535 } // namespace
537 // Tests the correctness of the rect-based targeting algorithm implemented in
538 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
539 // of rect-based targeting.
540 TEST_F(ViewTest, GetEventHandlerForRect) {
541 Widget* widget = new Widget;
542 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
543 widget->Init(params);
544 View* root_view = widget->GetRootView();
545 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
547 // Have this hierarchy of views (the coordinates here are all in
548 // the root view's coordinate space):
549 // v1 (0, 0, 100, 100)
550 // v2 (150, 0, 250, 100)
551 // v3 (0, 200, 150, 100)
552 // v31 (10, 210, 80, 80)
553 // v32 (110, 210, 30, 80)
554 // v4 (300, 200, 100, 100)
555 // v41 (310, 210, 80, 80)
556 // v411 (370, 275, 10, 5)
557 // v5 (450, 197, 30, 36)
558 // v51 (450, 200, 30, 30)
560 // The coordinates used for SetBounds are in parent coordinates.
562 TestView* v1 = new TestView;
563 v1->SetBounds(0, 0, 100, 100);
564 root_view->AddChildView(v1);
566 TestView* v2 = new TestView;
567 v2->SetBounds(150, 0, 250, 100);
568 root_view->AddChildView(v2);
570 TestView* v3 = new TestView;
571 v3->SetBounds(0, 200, 150, 100);
572 root_view->AddChildView(v3);
574 TestView* v4 = new TestView;
575 v4->SetBounds(300, 200, 100, 100);
576 root_view->AddChildView(v4);
578 TestView* v31 = new TestView;
579 v31->SetBounds(10, 10, 80, 80);
580 v3->AddChildView(v31);
582 TestView* v32 = new TestView;
583 v32->SetBounds(110, 10, 30, 80);
584 v3->AddChildView(v32);
586 TestView* v41 = new TestView;
587 v41->SetBounds(10, 10, 80, 80);
588 v4->AddChildView(v41);
590 TestView* v411 = new TestView;
591 v411->SetBounds(60, 65, 10, 5);
592 v41->AddChildView(v411);
594 TestView* v5 = new TestView;
595 v5->SetBounds(450, 197, 30, 36);
596 root_view->AddChildView(v5);
598 TestView* v51 = new TestView;
599 v51->SetBounds(0, 3, 30, 30);
600 v5->AddChildView(v51);
602 // |touch_rect| does not intersect any descendant view of |root_view|.
603 gfx::Rect touch_rect(105, 105, 30, 45);
604 View* result_view = root_view->GetEventHandlerForRect(touch_rect);
605 EXPECT_EQ(root_view, result_view);
606 result_view = NULL;
608 // Covers |v1| by at least 60%.
609 touch_rect.SetRect(15, 15, 100, 100);
610 result_view = root_view->GetEventHandlerForRect(touch_rect);
611 EXPECT_EQ(v1, result_view);
612 result_view = NULL;
614 // Intersects |v1| but does not cover it by at least 60%. The center
615 // of |touch_rect| is within |v1|.
616 touch_rect.SetRect(50, 50, 5, 10);
617 result_view = root_view->GetEventHandlerForRect(touch_rect);
618 EXPECT_EQ(v1, result_view);
619 result_view = NULL;
621 // Intersects |v1| but does not cover it by at least 60%. The center
622 // of |touch_rect| is not within |v1|.
623 touch_rect.SetRect(95, 96, 21, 22);
624 result_view = root_view->GetEventHandlerForRect(touch_rect);
625 EXPECT_EQ(root_view, result_view);
626 result_view = NULL;
628 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
629 touch_rect.SetRect(95, 10, 300, 120);
630 result_view = root_view->GetEventHandlerForRect(touch_rect);
631 EXPECT_EQ(v2, result_view);
632 result_view = NULL;
634 // Covers both |v1| and |v2| by at least 60%, but the center point
635 // of |touch_rect| is closer to the center point of |v2|.
636 touch_rect.SetRect(20, 20, 400, 100);
637 result_view = root_view->GetEventHandlerForRect(touch_rect);
638 EXPECT_EQ(v2, result_view);
639 result_view = NULL;
641 // Covers both |v1| and |v2| by at least 60%, but the center point
642 // of |touch_rect| is closer to the center point of |v1|.
643 touch_rect.SetRect(-700, -15, 1050, 110);
644 result_view = root_view->GetEventHandlerForRect(touch_rect);
645 EXPECT_EQ(v1, result_view);
646 result_view = NULL;
648 // A mouse click within |v1| will target |v1|.
649 touch_rect.SetRect(15, 15, 1, 1);
650 result_view = root_view->GetEventHandlerForRect(touch_rect);
651 EXPECT_EQ(v1, result_view);
652 result_view = NULL;
654 // Intersects |v3| and |v31| by at least 60% and the center point
655 // of |touch_rect| is closer to the center point of |v31|.
656 touch_rect.SetRect(0, 200, 110, 100);
657 result_view = root_view->GetEventHandlerForRect(touch_rect);
658 EXPECT_EQ(v31, result_view);
659 result_view = NULL;
661 // Intersects |v3| and |v31|, but neither by at least 60%. The
662 // center point of |touch_rect| lies within |v31|.
663 touch_rect.SetRect(80, 280, 15, 15);
664 result_view = root_view->GetEventHandlerForRect(touch_rect);
665 EXPECT_EQ(v31, result_view);
666 result_view = NULL;
668 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
669 // center point of |touch_rect| is closest to the center point
670 // of |v32|.
671 touch_rect.SetRect(0, 200, 200, 100);
672 result_view = root_view->GetEventHandlerForRect(touch_rect);
673 EXPECT_EQ(v32, result_view);
674 result_view = NULL;
676 // Intersects all of |v3|, |v31|, and |v32|, but only covers
677 // |v31| and |v32| by at least 60%. The center point of
678 // |touch_rect| is closest to the center point of |v32|.
679 touch_rect.SetRect(30, 225, 180, 115);
680 result_view = root_view->GetEventHandlerForRect(touch_rect);
681 EXPECT_EQ(v32, result_view);
682 result_view = NULL;
684 // A mouse click at the corner of |v3| will target |v3|.
685 touch_rect.SetRect(0, 200, 1, 1);
686 result_view = root_view->GetEventHandlerForRect(touch_rect);
687 EXPECT_EQ(v3, result_view);
688 result_view = NULL;
690 // A mouse click within |v32| will target |v32|.
691 touch_rect.SetRect(112, 211, 1, 1);
692 result_view = root_view->GetEventHandlerForRect(touch_rect);
693 EXPECT_EQ(v32, result_view);
694 result_view = NULL;
696 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
697 // The center point of |touch_rect| is equally close to
698 // the center points of |v4| and |v41|.
699 touch_rect.SetRect(310, 210, 80, 80);
700 result_view = root_view->GetEventHandlerForRect(touch_rect);
701 EXPECT_EQ(v41, result_view);
702 result_view = NULL;
704 // Intersects all of |v4|, |v41|, and |v411| but only covers
705 // |v411| by at least 60%.
706 touch_rect.SetRect(370, 275, 7, 5);
707 result_view = root_view->GetEventHandlerForRect(touch_rect);
708 EXPECT_EQ(v411, result_view);
709 result_view = NULL;
711 // Intersects |v4| and |v41| but covers neither by at least 60%.
712 // The center point of |touch_rect| is equally close to the center
713 // points of |v4| and |v41|.
714 touch_rect.SetRect(345, 245, 7, 7);
715 result_view = root_view->GetEventHandlerForRect(touch_rect);
716 EXPECT_EQ(v41, result_view);
717 result_view = NULL;
719 // Intersects all of |v4|, |v41|, and |v411| and covers none of
720 // them by at least 60%. The center point of |touch_rect| lies
721 // within |v411|.
722 touch_rect.SetRect(368, 272, 4, 6);
723 result_view = root_view->GetEventHandlerForRect(touch_rect);
724 EXPECT_EQ(v411, result_view);
725 result_view = NULL;
727 // Intersects all of |v4|, |v41|, and |v411| and covers none of
728 // them by at least 60%. The center point of |touch_rect| lies
729 // within |v41|.
730 touch_rect.SetRect(365, 270, 7, 7);
731 result_view = root_view->GetEventHandlerForRect(touch_rect);
732 EXPECT_EQ(v41, result_view);
733 result_view = NULL;
735 // Intersects all of |v4|, |v41|, and |v411| and covers none of
736 // them by at least 60%. The center point of |touch_rect| lies
737 // within |v4|.
738 touch_rect.SetRect(205, 275, 200, 2);
739 result_view = root_view->GetEventHandlerForRect(touch_rect);
740 EXPECT_EQ(v4, result_view);
741 result_view = NULL;
743 // Intersects all of |v4|, |v41|, and |v411| but only covers
744 // |v41| by at least 60%.
745 touch_rect.SetRect(310, 210, 61, 66);
746 result_view = root_view->GetEventHandlerForRect(touch_rect);
747 EXPECT_EQ(v41, result_view);
748 result_view = NULL;
750 // A mouse click within |v411| will target |v411|.
751 touch_rect.SetRect(372, 275, 1, 1);
752 result_view = root_view->GetEventHandlerForRect(touch_rect);
753 EXPECT_EQ(v411, result_view);
754 result_view = NULL;
756 // A mouse click within |v41| will target |v41|.
757 touch_rect.SetRect(350, 215, 1, 1);
758 result_view = root_view->GetEventHandlerForRect(touch_rect);
759 EXPECT_EQ(v41, result_view);
760 result_view = NULL;
762 // Covers |v3|, |v4|, and all of their descendants by at
763 // least 60%. The center point of |touch_rect| is closest
764 // to the center point of |v32|.
765 touch_rect.SetRect(0, 200, 400, 100);
766 result_view = root_view->GetEventHandlerForRect(touch_rect);
767 EXPECT_EQ(v32, result_view);
768 result_view = NULL;
770 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
771 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
772 // The center point of |touch_rect| is closest to the center
773 // point of |root_view|.
774 touch_rect.SetRect(110, 15, 375, 450);
775 result_view = root_view->GetEventHandlerForRect(touch_rect);
776 EXPECT_EQ(root_view, result_view);
777 result_view = NULL;
779 // Covers all views (except |v5| and |v51|) by at least 60%. The
780 // center point of |touch_rect| is equally close to the center
781 // points of |v2| and |v32|. One is not a descendant of the other,
782 // so in this case the view selected is arbitrary (i.e.,
783 // it depends only on the ordering of nodes in the views
784 // hierarchy).
785 touch_rect.SetRect(0, 0, 400, 300);
786 result_view = root_view->GetEventHandlerForRect(touch_rect);
787 EXPECT_EQ(v32, result_view);
788 result_view = NULL;
790 // Covers |v5| and |v51| by at least 60%, and the center point of
791 // the touch is located within both views. Since both views share
792 // the same center point, the child view should be selected.
793 touch_rect.SetRect(440, 190, 40, 40);
794 result_view = root_view->GetEventHandlerForRect(touch_rect);
795 EXPECT_EQ(v51, result_view);
796 result_view = NULL;
798 // Covers |v5| and |v51| by at least 60%, but the center point of
799 // the touch is not located within either view. Since both views
800 // share the same center point, the child view should be selected.
801 touch_rect.SetRect(455, 187, 60, 60);
802 result_view = root_view->GetEventHandlerForRect(touch_rect);
803 EXPECT_EQ(v51, result_view);
804 result_view = NULL;
806 // Covers neither |v5| nor |v51| by at least 60%, but the center
807 // of the touch is located within |v51|.
808 touch_rect.SetRect(450, 197, 10, 10);
809 result_view = root_view->GetEventHandlerForRect(touch_rect);
810 EXPECT_EQ(v51, result_view);
811 result_view = NULL;
813 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
814 // The center point is located outside of both views.
815 touch_rect.SetRect(433, 180, 24, 24);
816 result_view = root_view->GetEventHandlerForRect(touch_rect);
817 EXPECT_EQ(root_view, result_view);
818 result_view = NULL;
820 // Only intersects |v5| but does not cover it by at least 60%. The
821 // center point of the touch region is located within |v5|.
822 touch_rect.SetRect(449, 196, 3, 3);
823 result_view = root_view->GetEventHandlerForRect(touch_rect);
824 EXPECT_EQ(v5, result_view);
825 result_view = NULL;
827 // A mouse click within |v5| (but not |v51|) should target |v5|.
828 touch_rect.SetRect(462, 199, 1, 1);
829 result_view = root_view->GetEventHandlerForRect(touch_rect);
830 EXPECT_EQ(v5, result_view);
831 result_view = NULL;
833 // A mouse click |v5| and |v51| should target the child view.
834 touch_rect.SetRect(452, 226, 1, 1);
835 result_view = root_view->GetEventHandlerForRect(touch_rect);
836 EXPECT_EQ(v51, result_view);
837 result_view = NULL;
839 // A mouse click on the center of |v5| and |v51| should target
840 // the child view.
841 touch_rect.SetRect(465, 215, 1, 1);
842 result_view = root_view->GetEventHandlerForRect(touch_rect);
843 EXPECT_EQ(v51, result_view);
844 result_view = NULL;
846 widget->CloseNow();
849 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
850 // as expected when different views in the view hierarchy return false
851 // when CanProcessEventsWithinSubtree() is called.
852 TEST_F(ViewTest, CanProcessEventsWithinSubtree) {
853 Widget* widget = new Widget;
854 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
855 widget->Init(params);
856 View* root_view = widget->GetRootView();
857 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
859 // Have this hierarchy of views (the coords here are in the coordinate
860 // space of the root view):
861 // v (0, 0, 100, 100)
862 // - v_child (0, 0, 20, 30)
863 // - v_grandchild (5, 5, 5, 15)
865 TestView* v = new TestView;
866 v->SetBounds(0, 0, 100, 100);
867 root_view->AddChildView(v);
868 v->set_notify_enter_exit_on_child(true);
870 TestView* v_child = new TestView;
871 v_child->SetBounds(0, 0, 20, 30);
872 v->AddChildView(v_child);
874 TestView* v_grandchild = new TestView;
875 v_grandchild->SetBounds(5, 5, 5, 15);
876 v_child->AddChildView(v_grandchild);
878 v->Reset();
879 v_child->Reset();
880 v_grandchild->Reset();
882 // Define rects and points within the views in the hierarchy.
883 gfx::Rect rect_in_v_grandchild(7, 7, 3, 3);
884 gfx::Point point_in_v_grandchild(rect_in_v_grandchild.origin());
885 gfx::Rect rect_in_v_child(12, 3, 5, 5);
886 gfx::Point point_in_v_child(rect_in_v_child.origin());
887 gfx::Rect rect_in_v(50, 50, 25, 30);
888 gfx::Point point_in_v(rect_in_v.origin());
890 // When all three views return true when CanProcessEventsWithinSubtree()
891 // is called, targeting should behave as expected.
893 View* result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
894 EXPECT_EQ(v_grandchild, result_view);
895 result_view = NULL;
896 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
897 EXPECT_EQ(v_grandchild, result_view);
898 result_view = NULL;
900 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
901 EXPECT_EQ(v_child, result_view);
902 result_view = NULL;
903 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
904 EXPECT_EQ(v_child, result_view);
905 result_view = NULL;
907 result_view = root_view->GetEventHandlerForRect(rect_in_v);
908 EXPECT_EQ(v, result_view);
909 result_view = NULL;
910 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
911 EXPECT_EQ(v, result_view);
912 result_view = NULL;
914 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
915 // is called, then |v_grandchild| cannot be returned as a target.
917 v_grandchild->set_can_process_events_within_subtree(false);
919 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
920 EXPECT_EQ(v_child, result_view);
921 result_view = NULL;
922 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
923 EXPECT_EQ(v_child, result_view);
924 result_view = NULL;
926 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
927 EXPECT_EQ(v_child, result_view);
928 result_view = NULL;
929 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
930 EXPECT_EQ(v_child, result_view);
931 result_view = NULL;
933 result_view = root_view->GetEventHandlerForRect(rect_in_v);
934 EXPECT_EQ(v, result_view);
935 result_view = NULL;
936 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
937 EXPECT_EQ(v, result_view);
939 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
940 // is called, then NULL should be returned as a target if we call
941 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
942 // views tree. Note that the location must be in the coordinate space
943 // of the root view (|v_grandchild| in this case), so use (1, 1).
945 result_view = v_grandchild;
946 result_view = v_grandchild->GetTooltipHandlerForPoint(gfx::Point(1, 1));
947 EXPECT_EQ(NULL, result_view);
948 result_view = NULL;
950 // When |v_child| returns false when CanProcessEventsWithinSubtree()
951 // is called, then neither |v_child| nor |v_grandchild| can be returned
952 // as a target (|v| should be returned as the target for each case).
954 v_grandchild->Reset();
955 v_child->set_can_process_events_within_subtree(false);
957 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
958 EXPECT_EQ(v, result_view);
959 result_view = NULL;
960 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
961 EXPECT_EQ(v, result_view);
962 result_view = NULL;
964 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
965 EXPECT_EQ(v, result_view);
966 result_view = NULL;
967 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
968 EXPECT_EQ(v, result_view);
969 result_view = NULL;
971 result_view = root_view->GetEventHandlerForRect(rect_in_v);
972 EXPECT_EQ(v, result_view);
973 result_view = NULL;
974 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
975 EXPECT_EQ(v, result_view);
976 result_view = NULL;
978 // When |v| returns false when CanProcessEventsWithinSubtree()
979 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
980 // as a target (|root_view| should be returned as the target for each case).
982 v_child->Reset();
983 v->set_can_process_events_within_subtree(false);
985 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
986 EXPECT_EQ(root_view, result_view);
987 result_view = NULL;
988 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
989 EXPECT_EQ(root_view, result_view);
990 result_view = NULL;
992 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
993 EXPECT_EQ(root_view, result_view);
994 result_view = NULL;
995 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
996 EXPECT_EQ(root_view, result_view);
997 result_view = NULL;
999 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1000 EXPECT_EQ(root_view, result_view);
1001 result_view = NULL;
1002 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1003 EXPECT_EQ(root_view, result_view);
1006 TEST_F(ViewTest, NotifyEnterExitOnChild) {
1007 Widget* widget = new Widget;
1008 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1009 widget->Init(params);
1010 View* root_view = widget->GetRootView();
1011 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1013 // Have this hierarchy of views (the coords here are in root coord):
1014 // v1 (0, 0, 100, 100)
1015 // - v11 (0, 0, 20, 30)
1016 // - v111 (5, 5, 5, 15)
1017 // - v12 (50, 10, 30, 90)
1018 // - v121 (60, 20, 10, 10)
1019 // v2 (105, 0, 100, 100)
1020 // - v21 (120, 10, 50, 20)
1022 TestView* v1 = new TestView;
1023 v1->SetBounds(0, 0, 100, 100);
1024 root_view->AddChildView(v1);
1025 v1->set_notify_enter_exit_on_child(true);
1027 TestView* v11 = new TestView;
1028 v11->SetBounds(0, 0, 20, 30);
1029 v1->AddChildView(v11);
1031 TestView* v111 = new TestView;
1032 v111->SetBounds(5, 5, 5, 15);
1033 v11->AddChildView(v111);
1035 TestView* v12 = new TestView;
1036 v12->SetBounds(50, 10, 30, 90);
1037 v1->AddChildView(v12);
1039 TestView* v121 = new TestView;
1040 v121->SetBounds(10, 10, 10, 10);
1041 v12->AddChildView(v121);
1043 TestView* v2 = new TestView;
1044 v2->SetBounds(105, 0, 100, 100);
1045 root_view->AddChildView(v2);
1047 TestView* v21 = new TestView;
1048 v21->SetBounds(15, 10, 50, 20);
1049 v2->AddChildView(v21);
1051 v1->Reset();
1052 v11->Reset();
1053 v111->Reset();
1054 v12->Reset();
1055 v121->Reset();
1056 v2->Reset();
1057 v21->Reset();
1059 // Move the mouse in v111.
1060 gfx::Point p1(6, 6);
1061 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, p1, p1, ui::EventTimeForNow(), 0, 0);
1062 root_view->OnMouseMoved(move1);
1063 EXPECT_TRUE(v111->received_mouse_enter_);
1064 EXPECT_FALSE(v11->last_mouse_event_type_);
1065 EXPECT_TRUE(v1->received_mouse_enter_);
1067 v111->Reset();
1068 v1->Reset();
1070 // Now, move into v121.
1071 gfx::Point p2(65, 21);
1072 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, p2, p2, ui::EventTimeForNow(), 0, 0);
1073 root_view->OnMouseMoved(move2);
1074 EXPECT_TRUE(v111->received_mouse_exit_);
1075 EXPECT_TRUE(v121->received_mouse_enter_);
1076 EXPECT_FALSE(v1->last_mouse_event_type_);
1078 v111->Reset();
1079 v121->Reset();
1081 // Now, move into v11.
1082 gfx::Point p3(1, 1);
1083 ui::MouseEvent move3(ui::ET_MOUSE_MOVED, p3, p3, ui::EventTimeForNow(), 0, 0);
1084 root_view->OnMouseMoved(move3);
1085 EXPECT_TRUE(v121->received_mouse_exit_);
1086 EXPECT_TRUE(v11->received_mouse_enter_);
1087 EXPECT_FALSE(v1->last_mouse_event_type_);
1089 v121->Reset();
1090 v11->Reset();
1092 // Move to v21.
1093 gfx::Point p4(121, 15);
1094 ui::MouseEvent move4(ui::ET_MOUSE_MOVED, p4, p4, ui::EventTimeForNow(), 0, 0);
1095 root_view->OnMouseMoved(move4);
1096 EXPECT_TRUE(v21->received_mouse_enter_);
1097 EXPECT_FALSE(v2->last_mouse_event_type_);
1098 EXPECT_TRUE(v11->received_mouse_exit_);
1099 EXPECT_TRUE(v1->received_mouse_exit_);
1101 v21->Reset();
1102 v11->Reset();
1103 v1->Reset();
1105 // Move to v1.
1106 gfx::Point p5(21, 0);
1107 ui::MouseEvent move5(ui::ET_MOUSE_MOVED, p5, p5, ui::EventTimeForNow(), 0, 0);
1108 root_view->OnMouseMoved(move5);
1109 EXPECT_TRUE(v21->received_mouse_exit_);
1110 EXPECT_TRUE(v1->received_mouse_enter_);
1112 v21->Reset();
1113 v1->Reset();
1115 // Now, move into v11.
1116 gfx::Point p6(15, 15);
1117 ui::MouseEvent mouse6(ui::ET_MOUSE_MOVED, p6, p6, ui::EventTimeForNow(), 0,
1119 root_view->OnMouseMoved(mouse6);
1120 EXPECT_TRUE(v11->received_mouse_enter_);
1121 EXPECT_FALSE(v1->last_mouse_event_type_);
1123 v11->Reset();
1124 v1->Reset();
1126 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1127 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1128 // when the mouse leaves v11.
1129 gfx::Point p7(21, 0);
1130 ui::MouseEvent mouse7(ui::ET_MOUSE_MOVED, p7, p7, ui::EventTimeForNow(), 0,
1132 root_view->OnMouseMoved(mouse7);
1133 EXPECT_TRUE(v11->received_mouse_exit_);
1134 EXPECT_FALSE(v1->received_mouse_enter_);
1136 widget->CloseNow();
1139 TEST_F(ViewTest, Textfield) {
1140 const base::string16 kText = ASCIIToUTF16(
1141 "Reality is that which, when you stop believing it, doesn't go away.");
1142 const base::string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!");
1144 Widget* widget = new Widget;
1145 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1146 params.bounds = gfx::Rect(0, 0, 100, 100);
1147 widget->Init(params);
1148 View* root_view = widget->GetRootView();
1150 Textfield* textfield = new Textfield();
1151 root_view->AddChildView(textfield);
1153 // Test setting, appending text.
1154 textfield->SetText(kText);
1155 EXPECT_EQ(kText, textfield->text());
1156 textfield->AppendText(kExtraText);
1157 EXPECT_EQ(kText + kExtraText, textfield->text());
1158 textfield->SetText(base::string16());
1159 EXPECT_TRUE(textfield->text().empty());
1161 // Test selection related methods.
1162 textfield->SetText(kText);
1163 EXPECT_TRUE(textfield->GetSelectedText().empty());
1164 textfield->SelectAll(false);
1165 EXPECT_EQ(kText, textfield->text());
1166 textfield->ClearSelection();
1167 EXPECT_TRUE(textfield->GetSelectedText().empty());
1169 widget->CloseNow();
1172 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1173 TEST_F(ViewTest, TextfieldCutCopyPaste) {
1174 const base::string16 kNormalText = ASCIIToUTF16("Normal");
1175 const base::string16 kReadOnlyText = ASCIIToUTF16("Read only");
1176 const base::string16 kPasswordText =
1177 ASCIIToUTF16("Password! ** Secret stuff **");
1179 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1181 Widget* widget = new Widget;
1182 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1183 params.bounds = gfx::Rect(0, 0, 100, 100);
1184 widget->Init(params);
1185 View* root_view = widget->GetRootView();
1187 Textfield* normal = new Textfield();
1188 Textfield* read_only = new Textfield();
1189 read_only->SetReadOnly(true);
1190 Textfield* password = new Textfield();
1191 password->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
1193 root_view->AddChildView(normal);
1194 root_view->AddChildView(read_only);
1195 root_view->AddChildView(password);
1197 normal->SetText(kNormalText);
1198 read_only->SetText(kReadOnlyText);
1199 password->SetText(kPasswordText);
1202 // Test cut.
1205 normal->SelectAll(false);
1206 normal->ExecuteCommand(IDS_APP_CUT);
1207 base::string16 result;
1208 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1209 EXPECT_EQ(kNormalText, result);
1210 normal->SetText(kNormalText); // Let's revert to the original content.
1212 read_only->SelectAll(false);
1213 read_only->ExecuteCommand(IDS_APP_CUT);
1214 result.clear();
1215 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1216 // Cut should have failed, so the clipboard content should not have changed.
1217 EXPECT_EQ(kNormalText, result);
1219 password->SelectAll(false);
1220 password->ExecuteCommand(IDS_APP_CUT);
1221 result.clear();
1222 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1223 // Cut should have failed, so the clipboard content should not have changed.
1224 EXPECT_EQ(kNormalText, result);
1227 // Test copy.
1230 // Start with |read_only| to observe a change in clipboard text.
1231 read_only->SelectAll(false);
1232 read_only->ExecuteCommand(IDS_APP_COPY);
1233 result.clear();
1234 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1235 EXPECT_EQ(kReadOnlyText, result);
1237 normal->SelectAll(false);
1238 normal->ExecuteCommand(IDS_APP_COPY);
1239 result.clear();
1240 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1241 EXPECT_EQ(kNormalText, result);
1243 password->SelectAll(false);
1244 password->ExecuteCommand(IDS_APP_COPY);
1245 result.clear();
1246 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1247 // Text cannot be copied from an obscured field; the clipboard won't change.
1248 EXPECT_EQ(kNormalText, result);
1251 // Test paste.
1254 // Attempting to paste kNormalText in a read-only text-field should fail.
1255 read_only->SelectAll(false);
1256 read_only->ExecuteCommand(IDS_APP_PASTE);
1257 EXPECT_EQ(kReadOnlyText, read_only->text());
1259 password->SelectAll(false);
1260 password->ExecuteCommand(IDS_APP_PASTE);
1261 EXPECT_EQ(kNormalText, password->text());
1263 // Copy from |read_only| to observe a change in the normal textfield text.
1264 read_only->SelectAll(false);
1265 read_only->ExecuteCommand(IDS_APP_COPY);
1266 normal->SelectAll(false);
1267 normal->ExecuteCommand(IDS_APP_PASTE);
1268 EXPECT_EQ(kReadOnlyText, normal->text());
1269 widget->CloseNow();
1272 ////////////////////////////////////////////////////////////////////////////////
1273 // Accelerators
1274 ////////////////////////////////////////////////////////////////////////////////
1275 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) {
1276 accelerator_count_map_[accelerator]++;
1277 return true;
1280 // TODO: these tests were initially commented out when getting aura to
1281 // run. Figure out if still valuable and either nuke or fix.
1282 #if 0
1283 TEST_F(ViewTest, ActivateAccelerator) {
1284 // Register a keyboard accelerator before the view is added to a window.
1285 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1286 TestView* view = new TestView();
1287 view->Reset();
1288 view->AddAccelerator(return_accelerator);
1289 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1291 // Create a window and add the view as its child.
1292 scoped_ptr<Widget> widget(new Widget);
1293 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1294 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1295 params.bounds = gfx::Rect(0, 0, 100, 100);
1296 widget->Init(params);
1297 View* root = widget->GetRootView();
1298 root->AddChildView(view);
1299 widget->Show();
1301 // Get the focus manager.
1302 FocusManager* focus_manager = widget->GetFocusManager();
1303 ASSERT_TRUE(focus_manager);
1305 // Hit the return key and see if it takes effect.
1306 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1307 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1309 // Hit the escape key. Nothing should happen.
1310 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE);
1311 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1312 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1313 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0);
1315 // Now register the escape key and hit it again.
1316 view->AddAccelerator(escape_accelerator);
1317 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1318 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1319 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1321 // Remove the return key accelerator.
1322 view->RemoveAccelerator(return_accelerator);
1323 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1324 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1325 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1327 // Add it again. Hit the return key and the escape key.
1328 view->AddAccelerator(return_accelerator);
1329 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1330 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1331 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1332 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1333 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1334 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1336 // Remove all the accelerators.
1337 view->ResetAccelerators();
1338 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1339 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1340 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1341 EXPECT_FALSE(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 widget->CloseNow();
1348 TEST_F(ViewTest, HiddenViewWithAccelerator) {
1349 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1350 TestView* view = new TestView();
1351 view->Reset();
1352 view->AddAccelerator(return_accelerator);
1353 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1355 scoped_ptr<Widget> widget(new Widget);
1356 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1357 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1358 params.bounds = gfx::Rect(0, 0, 100, 100);
1359 widget->Init(params);
1360 View* root = widget->GetRootView();
1361 root->AddChildView(view);
1362 widget->Show();
1364 FocusManager* focus_manager = widget->GetFocusManager();
1365 ASSERT_TRUE(focus_manager);
1367 view->SetVisible(false);
1368 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1370 view->SetVisible(true);
1371 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1373 widget->CloseNow();
1376 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) {
1377 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1378 TestView* view = new TestView();
1379 view->Reset();
1380 view->AddAccelerator(return_accelerator);
1381 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1383 scoped_ptr<Widget> widget(new Widget);
1384 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1385 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1386 params.bounds = gfx::Rect(0, 0, 100, 100);
1387 widget->Init(params);
1388 View* root = widget->GetRootView();
1389 root->AddChildView(view);
1391 FocusManager* focus_manager = widget->GetFocusManager();
1392 ASSERT_TRUE(focus_manager);
1394 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1395 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
1397 widget->Show();
1398 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1399 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1401 widget->Hide();
1402 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1403 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1405 widget->CloseNow();
1408 ////////////////////////////////////////////////////////////////////////////////
1409 // Mouse-wheel message rerouting
1410 ////////////////////////////////////////////////////////////////////////////////
1411 class ScrollableTestView : public View {
1412 public:
1413 ScrollableTestView() { }
1415 virtual gfx::Size GetPreferredSize() {
1416 return gfx::Size(100, 10000);
1419 virtual void Layout() {
1420 SizeToPreferredSize();
1424 class TestViewWithControls : public View {
1425 public:
1426 TestViewWithControls() {
1427 text_field_ = new Textfield();
1428 AddChildView(text_field_);
1431 Textfield* text_field_;
1434 class SimpleWidgetDelegate : public WidgetDelegate {
1435 public:
1436 explicit SimpleWidgetDelegate(View* contents) : contents_(contents) { }
1438 virtual void DeleteDelegate() { delete this; }
1440 virtual View* GetContentsView() { return contents_; }
1442 virtual Widget* GetWidget() { return contents_->GetWidget(); }
1443 virtual const Widget* GetWidget() const { return contents_->GetWidget(); }
1445 private:
1446 View* contents_;
1449 // Tests that the mouse-wheel messages are correctly rerouted to the window
1450 // under the mouse.
1451 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1452 // bot.
1453 // Note that this fails for a variety of reasons:
1454 // - focused view is apparently reset across window activations and never
1455 // properly restored
1456 // - this test depends on you not having any other window visible open under the
1457 // area that it opens the test windows. --beng
1458 TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) {
1459 TestViewWithControls* view_with_controls = new TestViewWithControls();
1460 Widget* window1 = Widget::CreateWindowWithBounds(
1461 new SimpleWidgetDelegate(view_with_controls),
1462 gfx::Rect(0, 0, 100, 100));
1463 window1->Show();
1464 ScrollView* scroll_view = new ScrollView();
1465 scroll_view->SetContents(new ScrollableTestView());
1466 Widget* window2 = Widget::CreateWindowWithBounds(
1467 new SimpleWidgetDelegate(scroll_view),
1468 gfx::Rect(200, 200, 100, 100));
1469 window2->Show();
1470 EXPECT_EQ(0, scroll_view->GetVisibleRect().y());
1472 // Make the window1 active, as this is what it would be in real-world.
1473 window1->Activate();
1475 // Let's send a mouse-wheel message to the different controls and check that
1476 // it is rerouted to the window under the mouse (effectively scrolling the
1477 // scroll-view).
1479 // First to the Window's HWND.
1480 ::SendMessage(view_with_controls->GetWidget()->GetNativeView(),
1481 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1482 EXPECT_EQ(20, scroll_view->GetVisibleRect().y());
1484 window1->CloseNow();
1485 window2->CloseNow();
1487 #endif // 0
1489 ////////////////////////////////////////////////////////////////////////////////
1490 // Native view hierachy
1491 ////////////////////////////////////////////////////////////////////////////////
1492 class ToplevelWidgetObserverView : public View {
1493 public:
1494 ToplevelWidgetObserverView() : toplevel_(NULL) {
1496 ~ToplevelWidgetObserverView() override {}
1498 // View overrides:
1499 void ViewHierarchyChanged(
1500 const ViewHierarchyChangedDetails& details) override {
1501 if (details.is_add) {
1502 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1503 } else {
1504 toplevel_ = NULL;
1507 void NativeViewHierarchyChanged() override {
1508 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1511 Widget* toplevel() { return toplevel_; }
1513 private:
1514 Widget* toplevel_;
1516 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView);
1519 // Test that a view can track the current top level widget by overriding
1520 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1521 TEST_F(ViewTest, NativeViewHierarchyChanged) {
1522 scoped_ptr<Widget> toplevel1(new Widget);
1523 Widget::InitParams toplevel1_params =
1524 CreateParams(Widget::InitParams::TYPE_POPUP);
1525 toplevel1_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1526 toplevel1->Init(toplevel1_params);
1528 scoped_ptr<Widget> toplevel2(new Widget);
1529 Widget::InitParams toplevel2_params =
1530 CreateParams(Widget::InitParams::TYPE_POPUP);
1531 toplevel2_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1532 toplevel2->Init(toplevel2_params);
1534 Widget* child = new Widget;
1535 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
1536 child_params.parent = toplevel1->GetNativeView();
1537 child->Init(child_params);
1539 ToplevelWidgetObserverView* observer_view =
1540 new ToplevelWidgetObserverView();
1541 EXPECT_EQ(NULL, observer_view->toplevel());
1543 child->SetContentsView(observer_view);
1544 EXPECT_EQ(toplevel1, observer_view->toplevel());
1546 Widget::ReparentNativeView(child->GetNativeView(),
1547 toplevel2->GetNativeView());
1548 EXPECT_EQ(toplevel2, observer_view->toplevel());
1550 observer_view->parent()->RemoveChildView(observer_view);
1551 EXPECT_EQ(NULL, observer_view->toplevel());
1553 // Make |observer_view| |child|'s contents view again so that it gets deleted
1554 // with the widget.
1555 child->SetContentsView(observer_view);
1558 ////////////////////////////////////////////////////////////////////////////////
1559 // Transformations
1560 ////////////////////////////////////////////////////////////////////////////////
1562 class TransformPaintView : public TestView {
1563 public:
1564 TransformPaintView() {}
1565 ~TransformPaintView() override {}
1567 void ClearScheduledPaintRect() {
1568 scheduled_paint_rect_ = gfx::Rect();
1571 gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
1573 // Overridden from View:
1574 void SchedulePaintInRect(const gfx::Rect& rect) override {
1575 gfx::Rect xrect = ConvertRectToParent(rect);
1576 scheduled_paint_rect_.Union(xrect);
1579 private:
1580 gfx::Rect scheduled_paint_rect_;
1582 DISALLOW_COPY_AND_ASSIGN(TransformPaintView);
1585 TEST_F(ViewTest, TransformPaint) {
1586 TransformPaintView* v1 = new TransformPaintView();
1587 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1589 TestView* v2 = new TestView();
1590 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1592 Widget* widget = new Widget;
1593 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1594 params.bounds = gfx::Rect(50, 50, 650, 650);
1595 widget->Init(params);
1596 widget->Show();
1597 View* root = widget->GetRootView();
1599 root->AddChildView(v1);
1600 v1->AddChildView(v2);
1602 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1603 v1->ClearScheduledPaintRect();
1604 v2->SchedulePaint();
1606 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
1608 // Rotate |v1| counter-clockwise.
1609 gfx::Transform transform;
1610 RotateCounterclockwise(&transform);
1611 transform.matrix().set(1, 3, 500.0);
1612 v1->SetTransform(transform);
1614 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1616 v1->ClearScheduledPaintRect();
1617 v2->SchedulePaint();
1619 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect());
1621 widget->CloseNow();
1624 TEST_F(ViewTest, TransformEvent) {
1625 TestView* v1 = new TestView();
1626 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1628 TestView* v2 = new TestView();
1629 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1631 Widget* widget = new Widget;
1632 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1633 params.bounds = gfx::Rect(50, 50, 650, 650);
1634 widget->Init(params);
1635 View* root = widget->GetRootView();
1637 root->AddChildView(v1);
1638 v1->AddChildView(v2);
1640 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1642 // Rotate |v1| counter-clockwise.
1643 gfx::Transform transform(v1->GetTransform());
1644 RotateCounterclockwise(&transform);
1645 transform.matrix().set(1, 3, 500.0);
1646 v1->SetTransform(transform);
1648 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1649 v1->Reset();
1650 v2->Reset();
1652 gfx::Point p1(110, 210);
1653 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
1654 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1655 root->OnMousePressed(pressed);
1656 EXPECT_EQ(0, v1->last_mouse_event_type_);
1657 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
1658 EXPECT_EQ(190, v2->location_.x());
1659 EXPECT_EQ(10, v2->location_.y());
1661 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
1662 ui::EventTimeForNow(), 0, 0);
1663 root->OnMouseReleased(released);
1665 // Now rotate |v2| inside |v1| clockwise.
1666 transform = v2->GetTransform();
1667 RotateClockwise(&transform);
1668 transform.matrix().set(0, 3, 100.f);
1669 v2->SetTransform(transform);
1671 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1672 // (300, 400) in |root|.
1674 v1->Reset();
1675 v2->Reset();
1677 gfx::Point point2(110, 320);
1678 ui::MouseEvent p2(ui::ET_MOUSE_PRESSED, point2, point2, ui::EventTimeForNow(),
1679 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1680 root->OnMousePressed(p2);
1681 EXPECT_EQ(0, v1->last_mouse_event_type_);
1682 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
1683 EXPECT_EQ(10, v2->location_.x());
1684 EXPECT_EQ(20, v2->location_.y());
1686 root->OnMouseReleased(released);
1688 v1->SetTransform(gfx::Transform());
1689 v2->SetTransform(gfx::Transform());
1691 TestView* v3 = new TestView();
1692 v3->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1693 v2->AddChildView(v3);
1695 // Rotate |v3| clockwise with respect to |v2|.
1696 transform = v1->GetTransform();
1697 RotateClockwise(&transform);
1698 transform.matrix().set(0, 3, 30.f);
1699 v3->SetTransform(transform);
1701 // Scale |v2| with respect to |v1| along both axis.
1702 transform = v2->GetTransform();
1703 transform.matrix().set(0, 0, 0.8f);
1704 transform.matrix().set(1, 1, 0.5f);
1705 v2->SetTransform(transform);
1707 // |v3| occupies (108, 105) to (132, 115) in |root|.
1709 v1->Reset();
1710 v2->Reset();
1711 v3->Reset();
1713 gfx::Point point(112, 110);
1714 ui::MouseEvent p3(ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
1715 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1716 root->OnMousePressed(p3);
1718 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
1719 EXPECT_EQ(10, v3->location_.x());
1720 EXPECT_EQ(25, v3->location_.y());
1722 root->OnMouseReleased(released);
1724 v1->SetTransform(gfx::Transform());
1725 v2->SetTransform(gfx::Transform());
1726 v3->SetTransform(gfx::Transform());
1728 v1->Reset();
1729 v2->Reset();
1730 v3->Reset();
1732 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1733 transform = v3->GetTransform();
1734 RotateClockwise(&transform);
1735 transform.matrix().set(0, 3, 30.f);
1736 // Rotation sets some scaling transformation. Using SetScale would overwrite
1737 // that and pollute the rotation. So combine the scaling with the existing
1738 // transforamtion.
1739 gfx::Transform scale;
1740 scale.Scale(0.8f, 0.5f);
1741 transform.ConcatTransform(scale);
1742 v3->SetTransform(transform);
1744 // Translate |v2| with respect to |v1|.
1745 transform = v2->GetTransform();
1746 transform.matrix().set(0, 3, 10.f);
1747 transform.matrix().set(1, 3, 10.f);
1748 v2->SetTransform(transform);
1750 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1752 gfx::Point point3(124, 125);
1753 ui::MouseEvent p4(ui::ET_MOUSE_PRESSED, point3, point3, ui::EventTimeForNow(),
1754 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1755 root->OnMousePressed(p4);
1757 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
1758 EXPECT_EQ(10, v3->location_.x());
1759 EXPECT_EQ(25, v3->location_.y());
1761 root->OnMouseReleased(released);
1763 widget->CloseNow();
1766 TEST_F(ViewTest, TransformVisibleBound) {
1767 gfx::Rect viewport_bounds(0, 0, 100, 100);
1769 scoped_ptr<Widget> widget(new Widget);
1770 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1771 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1772 params.bounds = viewport_bounds;
1773 widget->Init(params);
1774 widget->GetRootView()->SetBoundsRect(viewport_bounds);
1776 View* viewport = new View;
1777 widget->SetContentsView(viewport);
1778 View* contents = new View;
1779 viewport->AddChildView(contents);
1780 viewport->SetBoundsRect(viewport_bounds);
1781 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1783 View* child = new View;
1784 contents->AddChildView(child);
1785 child->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
1786 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds());
1788 // Rotate |child| counter-clockwise
1789 gfx::Transform transform;
1790 RotateCounterclockwise(&transform);
1791 transform.matrix().set(1, 3, 50.f);
1792 child->SetTransform(transform);
1793 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds());
1795 widget->CloseNow();
1798 ////////////////////////////////////////////////////////////////////////////////
1799 // OnVisibleBoundsChanged()
1801 class VisibleBoundsView : public View {
1802 public:
1803 VisibleBoundsView() : received_notification_(false) {}
1804 ~VisibleBoundsView() override {}
1806 bool received_notification() const { return received_notification_; }
1807 void set_received_notification(bool received) {
1808 received_notification_ = received;
1811 private:
1812 // Overridden from View:
1813 bool GetNeedsNotificationWhenVisibleBoundsChange() const override {
1814 return true;
1816 void OnVisibleBoundsChanged() override { received_notification_ = true; }
1818 bool received_notification_;
1820 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView);
1823 TEST_F(ViewTest, OnVisibleBoundsChanged) {
1824 gfx::Rect viewport_bounds(0, 0, 100, 100);
1826 scoped_ptr<Widget> widget(new Widget);
1827 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1828 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1829 params.bounds = viewport_bounds;
1830 widget->Init(params);
1831 widget->GetRootView()->SetBoundsRect(viewport_bounds);
1833 View* viewport = new View;
1834 widget->SetContentsView(viewport);
1835 View* contents = new View;
1836 viewport->AddChildView(contents);
1837 viewport->SetBoundsRect(viewport_bounds);
1838 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1840 // Create a view that cares about visible bounds notifications, and position
1841 // it just outside the visible bounds of the viewport.
1842 VisibleBoundsView* child = new VisibleBoundsView;
1843 contents->AddChildView(child);
1844 child->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
1846 // The child bound should be fully clipped.
1847 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
1849 // Now scroll the contents, but not enough to make the child visible.
1850 contents->SetY(contents->y() - 1);
1852 // We should have received the notification since the visible bounds may have
1853 // changed (even though they didn't).
1854 EXPECT_TRUE(child->received_notification());
1855 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
1856 child->set_received_notification(false);
1858 // Now scroll the contents, this time by enough to make the child visible by
1859 // one pixel.
1860 contents->SetY(contents->y() - 10);
1861 EXPECT_TRUE(child->received_notification());
1862 EXPECT_EQ(1, child->GetVisibleBounds().height());
1863 child->set_received_notification(false);
1865 widget->CloseNow();
1868 TEST_F(ViewTest, SetBoundsPaint) {
1869 TestView top_view;
1870 TestView* child_view = new TestView;
1872 top_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1873 top_view.scheduled_paint_rects_.clear();
1874 child_view->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
1875 top_view.AddChildView(child_view);
1877 top_view.scheduled_paint_rects_.clear();
1878 child_view->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
1879 EXPECT_EQ(2U, top_view.scheduled_paint_rects_.size());
1881 // There should be 2 rects, spanning from (10, 10) to (50, 50).
1882 gfx::Rect paint_rect = top_view.scheduled_paint_rects_[0];
1883 paint_rect.Union(top_view.scheduled_paint_rects_[1]);
1884 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect);
1887 // Assertions around painting and focus gain/lost.
1888 TEST_F(ViewTest, FocusBlurPaints) {
1889 TestView parent_view;
1890 TestView* child_view1 = new TestView; // Owned by |parent_view|.
1892 parent_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1894 child_view1->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
1895 parent_view.AddChildView(child_view1);
1897 parent_view.scheduled_paint_rects_.clear();
1898 child_view1->scheduled_paint_rects_.clear();
1900 // Focus change shouldn't trigger paints.
1901 child_view1->DoFocus();
1903 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
1904 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
1906 child_view1->DoBlur();
1907 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
1908 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
1911 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
1912 TEST_F(ViewTest, SetBoundsSameBoundsDoesntSchedulePaint) {
1913 TestView view;
1915 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1916 view.InvalidateLayout();
1917 view.scheduled_paint_rects_.clear();
1918 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
1919 EXPECT_TRUE(view.scheduled_paint_rects_.empty());
1922 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
1923 TEST_F(ViewTest, AddAndRemoveSchedulePaints) {
1924 gfx::Rect viewport_bounds(0, 0, 100, 100);
1926 // We have to put the View hierarchy into a Widget or no paints will be
1927 // scheduled.
1928 scoped_ptr<Widget> widget(new Widget);
1929 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1930 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1931 params.bounds = viewport_bounds;
1932 widget->Init(params);
1933 widget->GetRootView()->SetBoundsRect(viewport_bounds);
1935 TestView* parent_view = new TestView;
1936 widget->SetContentsView(parent_view);
1937 parent_view->SetBoundsRect(viewport_bounds);
1938 parent_view->scheduled_paint_rects_.clear();
1940 View* child_view = new View;
1941 child_view->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
1942 parent_view->AddChildView(child_view);
1943 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
1944 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
1946 parent_view->scheduled_paint_rects_.clear();
1947 parent_view->RemoveChildView(child_view);
1948 scoped_ptr<View> child_deleter(child_view);
1949 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
1950 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
1952 widget->CloseNow();
1955 // Tests conversion methods with a transform.
1956 TEST_F(ViewTest, ConversionsWithTransform) {
1957 TestView top_view;
1959 // View hierarchy used to test scale transforms.
1960 TestView* child = new TestView;
1961 TestView* child_child = new TestView;
1963 // View used to test a rotation transform.
1964 TestView* child_2 = new TestView;
1966 top_view.AddChildView(child);
1967 child->AddChildView(child_child);
1969 top_view.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
1971 child->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
1972 gfx::Transform transform;
1973 transform.Scale(3.0, 4.0);
1974 child->SetTransform(transform);
1976 child_child->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
1977 transform.MakeIdentity();
1978 transform.Scale(5.0, 7.0);
1979 child_child->SetTransform(transform);
1981 top_view.AddChildView(child_2);
1982 child_2->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
1983 transform.MakeIdentity();
1984 RotateClockwise(&transform);
1985 child_2->SetTransform(transform);
1987 // Sanity check to make sure basic transforms act as expected.
1989 gfx::Transform transform;
1990 transform.Translate(110.0, -110.0);
1991 transform.Scale(100.0, 55.0);
1992 transform.Translate(1.0, 1.0);
1994 // convert to a 3x3 matrix.
1995 const SkMatrix& matrix = transform.matrix();
1997 EXPECT_EQ(210, matrix.getTranslateX());
1998 EXPECT_EQ(-55, matrix.getTranslateY());
1999 EXPECT_EQ(100, matrix.getScaleX());
2000 EXPECT_EQ(55, matrix.getScaleY());
2001 EXPECT_EQ(0, matrix.getSkewX());
2002 EXPECT_EQ(0, matrix.getSkewY());
2006 gfx::Transform transform;
2007 transform.Translate(1.0, 1.0);
2008 gfx::Transform t2;
2009 t2.Scale(100.0, 55.0);
2010 gfx::Transform t3;
2011 t3.Translate(110.0, -110.0);
2012 transform.ConcatTransform(t2);
2013 transform.ConcatTransform(t3);
2015 // convert to a 3x3 matrix
2016 const SkMatrix& matrix = transform.matrix();
2018 EXPECT_EQ(210, matrix.getTranslateX());
2019 EXPECT_EQ(-55, matrix.getTranslateY());
2020 EXPECT_EQ(100, matrix.getScaleX());
2021 EXPECT_EQ(55, matrix.getScaleY());
2022 EXPECT_EQ(0, matrix.getSkewX());
2023 EXPECT_EQ(0, matrix.getSkewY());
2026 // Conversions from child->top and top->child.
2028 gfx::Point point(5, 5);
2029 View::ConvertPointToTarget(child, &top_view, &point);
2030 EXPECT_EQ(22, point.x());
2031 EXPECT_EQ(39, point.y());
2033 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2034 View::ConvertRectToTarget(child, &top_view, &rect);
2035 EXPECT_FLOAT_EQ(22.0f, rect.x());
2036 EXPECT_FLOAT_EQ(39.0f, rect.y());
2037 EXPECT_FLOAT_EQ(30.0f, rect.width());
2038 EXPECT_FLOAT_EQ(80.0f, rect.height());
2040 point.SetPoint(22, 39);
2041 View::ConvertPointToTarget(&top_view, child, &point);
2042 EXPECT_EQ(5, point.x());
2043 EXPECT_EQ(5, point.y());
2045 rect.SetRect(22.0f, 39.0f, 30.0f, 80.0f);
2046 View::ConvertRectToTarget(&top_view, child, &rect);
2047 EXPECT_FLOAT_EQ(5.0f, rect.x());
2048 EXPECT_FLOAT_EQ(5.0f, rect.y());
2049 EXPECT_FLOAT_EQ(10.0f, rect.width());
2050 EXPECT_FLOAT_EQ(20.0f, rect.height());
2053 // Conversions from child_child->top and top->child_child.
2055 gfx::Point point(5, 5);
2056 View::ConvertPointToTarget(child_child, &top_view, &point);
2057 EXPECT_EQ(133, point.x());
2058 EXPECT_EQ(211, point.y());
2060 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2061 View::ConvertRectToTarget(child_child, &top_view, &rect);
2062 EXPECT_FLOAT_EQ(133.0f, rect.x());
2063 EXPECT_FLOAT_EQ(211.0f, rect.y());
2064 EXPECT_FLOAT_EQ(150.0f, rect.width());
2065 EXPECT_FLOAT_EQ(560.0f, rect.height());
2067 point.SetPoint(133, 211);
2068 View::ConvertPointToTarget(&top_view, child_child, &point);
2069 EXPECT_EQ(5, point.x());
2070 EXPECT_EQ(5, point.y());
2072 rect.SetRect(133.0f, 211.0f, 150.0f, 560.0f);
2073 View::ConvertRectToTarget(&top_view, child_child, &rect);
2074 EXPECT_FLOAT_EQ(5.0f, rect.x());
2075 EXPECT_FLOAT_EQ(5.0f, rect.y());
2076 EXPECT_FLOAT_EQ(10.0f, rect.width());
2077 EXPECT_FLOAT_EQ(20.0f, rect.height());
2080 // Conversions from child_child->child and child->child_child
2082 gfx::Point point(5, 5);
2083 View::ConvertPointToTarget(child_child, child, &point);
2084 EXPECT_EQ(42, point.x());
2085 EXPECT_EQ(48, point.y());
2087 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2088 View::ConvertRectToTarget(child_child, child, &rect);
2089 EXPECT_FLOAT_EQ(42.0f, rect.x());
2090 EXPECT_FLOAT_EQ(48.0f, rect.y());
2091 EXPECT_FLOAT_EQ(50.0f, rect.width());
2092 EXPECT_FLOAT_EQ(140.0f, rect.height());
2094 point.SetPoint(42, 48);
2095 View::ConvertPointToTarget(child, child_child, &point);
2096 EXPECT_EQ(5, point.x());
2097 EXPECT_EQ(5, point.y());
2099 rect.SetRect(42.0f, 48.0f, 50.0f, 140.0f);
2100 View::ConvertRectToTarget(child, child_child, &rect);
2101 EXPECT_FLOAT_EQ(5.0f, rect.x());
2102 EXPECT_FLOAT_EQ(5.0f, rect.y());
2103 EXPECT_FLOAT_EQ(10.0f, rect.width());
2104 EXPECT_FLOAT_EQ(20.0f, rect.height());
2107 // Conversions from top_view to child with a value that should be negative.
2108 // This ensures we don't round up with negative numbers.
2110 gfx::Point point(6, 18);
2111 View::ConvertPointToTarget(&top_view, child, &point);
2112 EXPECT_EQ(-1, point.x());
2113 EXPECT_EQ(-1, point.y());
2115 float error = 0.01f;
2116 gfx::RectF rect(6.0f, 18.0f, 10.0f, 39.0f);
2117 View::ConvertRectToTarget(&top_view, child, &rect);
2118 EXPECT_NEAR(-0.33f, rect.x(), error);
2119 EXPECT_NEAR(-0.25f, rect.y(), error);
2120 EXPECT_NEAR(3.33f, rect.width(), error);
2121 EXPECT_NEAR(9.75f, rect.height(), error);
2124 // Rect conversions from top_view->child_2 and child_2->top_view.
2126 gfx::RectF rect(50.0f, 55.0f, 20.0f, 30.0f);
2127 View::ConvertRectToTarget(child_2, &top_view, &rect);
2128 EXPECT_FLOAT_EQ(615.0f, rect.x());
2129 EXPECT_FLOAT_EQ(775.0f, rect.y());
2130 EXPECT_FLOAT_EQ(30.0f, rect.width());
2131 EXPECT_FLOAT_EQ(20.0f, rect.height());
2133 rect.SetRect(615.0f, 775.0f, 30.0f, 20.0f);
2134 View::ConvertRectToTarget(&top_view, child_2, &rect);
2135 EXPECT_FLOAT_EQ(50.0f, rect.x());
2136 EXPECT_FLOAT_EQ(55.0f, rect.y());
2137 EXPECT_FLOAT_EQ(20.0f, rect.width());
2138 EXPECT_FLOAT_EQ(30.0f, rect.height());
2142 // Tests conversion methods to and from screen coordinates.
2143 TEST_F(ViewTest, ConversionsToFromScreen) {
2144 scoped_ptr<Widget> widget(new Widget);
2145 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2146 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2147 params.bounds = gfx::Rect(50, 50, 650, 650);
2148 widget->Init(params);
2150 View* child = new View;
2151 widget->GetRootView()->AddChildView(child);
2152 child->SetBounds(10, 10, 100, 200);
2153 gfx::Transform t;
2154 t.Scale(0.5, 0.5);
2155 child->SetTransform(t);
2157 gfx::Point point_in_screen(100, 90);
2158 gfx::Point point_in_child(80, 60);
2160 gfx::Point point = point_in_screen;
2161 View::ConvertPointFromScreen(child, &point);
2162 EXPECT_EQ(point_in_child.ToString(), point.ToString());
2164 View::ConvertPointToScreen(child, &point);
2165 EXPECT_EQ(point_in_screen.ToString(), point.ToString());
2168 // Tests conversion methods for rectangles.
2169 TEST_F(ViewTest, ConvertRectWithTransform) {
2170 scoped_ptr<Widget> widget(new Widget);
2171 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2172 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2173 params.bounds = gfx::Rect(50, 50, 650, 650);
2174 widget->Init(params);
2175 View* root = widget->GetRootView();
2177 TestView* v1 = new TestView;
2178 TestView* v2 = new TestView;
2179 root->AddChildView(v1);
2180 v1->AddChildView(v2);
2182 v1->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2183 v2->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2185 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2186 gfx::Rect rect(5, 5, 15, 40);
2187 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2->ConvertRectToParent(rect));
2188 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2->ConvertRectToWidget(rect));
2190 // Rotate |v2|
2191 gfx::Transform t2;
2192 RotateCounterclockwise(&t2);
2193 t2.matrix().set(1, 3, 100.f);
2194 v2->SetTransform(t2);
2196 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2197 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2198 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2->ConvertRectToWidget(rect));
2200 // Scale down |v1|
2201 gfx::Transform t1;
2202 t1.Scale(0.5, 0.5);
2203 v1->SetTransform(t1);
2205 // The rectangle should remain the same for |v1|.
2206 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2208 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2209 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2210 v2->ConvertRectToWidget(rect).ToString());
2212 widget->CloseNow();
2215 class ObserverView : public View {
2216 public:
2217 ObserverView();
2218 ~ObserverView() override;
2220 void ResetTestState();
2222 bool has_add_details() const { return has_add_details_; }
2223 bool has_remove_details() const { return has_remove_details_; }
2225 const ViewHierarchyChangedDetails& add_details() const {
2226 return add_details_;
2229 const ViewHierarchyChangedDetails& remove_details() const {
2230 return remove_details_;
2233 private:
2234 // View:
2235 void ViewHierarchyChanged(
2236 const ViewHierarchyChangedDetails& details) override;
2238 bool has_add_details_;
2239 bool has_remove_details_;
2240 ViewHierarchyChangedDetails add_details_;
2241 ViewHierarchyChangedDetails remove_details_;
2243 DISALLOW_COPY_AND_ASSIGN(ObserverView);
2246 ObserverView::ObserverView()
2247 : has_add_details_(false),
2248 has_remove_details_(false) {
2251 ObserverView::~ObserverView() {}
2253 void ObserverView::ResetTestState() {
2254 has_add_details_ = false;
2255 has_remove_details_ = false;
2256 add_details_ = ViewHierarchyChangedDetails();
2257 remove_details_ = ViewHierarchyChangedDetails();
2260 void ObserverView::ViewHierarchyChanged(
2261 const ViewHierarchyChangedDetails& details) {
2262 if (details.is_add) {
2263 has_add_details_ = true;
2264 add_details_ = details;
2265 } else {
2266 has_remove_details_ = true;
2267 remove_details_ = details;
2271 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2272 // a child view is added or removed to all the views in the hierarchy (up and
2273 // down).
2274 // The tree looks like this:
2275 // v1
2276 // +-- v2
2277 // +-- v3
2278 // +-- v4 (starts here, then get reparented to v1)
2279 TEST_F(ViewTest, ViewHierarchyChanged) {
2280 ObserverView v1;
2282 ObserverView* v3 = new ObserverView();
2284 // Add |v3| to |v2|.
2285 scoped_ptr<ObserverView> v2(new ObserverView());
2286 v2->AddChildView(v3);
2288 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2289 // notification.
2290 EXPECT_TRUE(v2->has_add_details());
2291 EXPECT_FALSE(v2->has_remove_details());
2292 EXPECT_EQ(v2.get(), v2->add_details().parent);
2293 EXPECT_EQ(v3, v2->add_details().child);
2294 EXPECT_EQ(NULL, v2->add_details().move_view);
2296 EXPECT_TRUE(v3->has_add_details());
2297 EXPECT_FALSE(v3->has_remove_details());
2298 EXPECT_EQ(v2.get(), v3->add_details().parent);
2299 EXPECT_EQ(v3, v3->add_details().child);
2300 EXPECT_EQ(NULL, v3->add_details().move_view);
2302 // Reset everything to the initial state.
2303 v2->ResetTestState();
2304 v3->ResetTestState();
2306 // Add |v2| to v1.
2307 v1.AddChildView(v2.get());
2309 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2310 // Make sure all the views (v1, v2, v3) received _that_ information.
2311 EXPECT_TRUE(v1.has_add_details());
2312 EXPECT_FALSE(v1.has_remove_details());
2313 EXPECT_EQ(&v1, v1.add_details().parent);
2314 EXPECT_EQ(v2.get(), v1.add_details().child);
2315 EXPECT_EQ(NULL, v1.add_details().move_view);
2317 EXPECT_TRUE(v2->has_add_details());
2318 EXPECT_FALSE(v2->has_remove_details());
2319 EXPECT_EQ(&v1, v2->add_details().parent);
2320 EXPECT_EQ(v2.get(), v2->add_details().child);
2321 EXPECT_EQ(NULL, v2->add_details().move_view);
2323 EXPECT_TRUE(v3->has_add_details());
2324 EXPECT_FALSE(v3->has_remove_details());
2325 EXPECT_EQ(&v1, v3->add_details().parent);
2326 EXPECT_EQ(v2.get(), v3->add_details().child);
2327 EXPECT_EQ(NULL, v3->add_details().move_view);
2329 // Reset everything to the initial state.
2330 v1.ResetTestState();
2331 v2->ResetTestState();
2332 v3->ResetTestState();
2334 // Remove |v2| from |v1|.
2335 v1.RemoveChildView(v2.get());
2337 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2338 // Make sure all the views (v1, v2, v3) received _that_ information.
2339 EXPECT_FALSE(v1.has_add_details());
2340 EXPECT_TRUE(v1.has_remove_details());
2341 EXPECT_EQ(&v1, v1.remove_details().parent);
2342 EXPECT_EQ(v2.get(), v1.remove_details().child);
2343 EXPECT_EQ(NULL, v1.remove_details().move_view);
2345 EXPECT_FALSE(v2->has_add_details());
2346 EXPECT_TRUE(v2->has_remove_details());
2347 EXPECT_EQ(&v1, v2->remove_details().parent);
2348 EXPECT_EQ(v2.get(), v2->remove_details().child);
2349 EXPECT_EQ(NULL, v2->remove_details().move_view);
2351 EXPECT_FALSE(v3->has_add_details());
2352 EXPECT_TRUE(v3->has_remove_details());
2353 EXPECT_EQ(&v1, v3->remove_details().parent);
2354 EXPECT_EQ(v3, v3->remove_details().child);
2355 EXPECT_EQ(NULL, v3->remove_details().move_view);
2357 // Verifies notifications when reparenting a view.
2358 ObserverView* v4 = new ObserverView();
2359 // Add |v4| to |v2|.
2360 v2->AddChildView(v4);
2362 // Reset everything to the initial state.
2363 v1.ResetTestState();
2364 v2->ResetTestState();
2365 v3->ResetTestState();
2366 v4->ResetTestState();
2368 // Reparent |v4| to |v1|.
2369 v1.AddChildView(v4);
2371 // Verifies that all views receive the correct information for all the child,
2372 // parent and move views.
2374 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2375 EXPECT_TRUE(v1.has_add_details());
2376 EXPECT_FALSE(v1.has_remove_details());
2377 EXPECT_EQ(&v1, v1.add_details().parent);
2378 EXPECT_EQ(v4, v1.add_details().child);
2379 EXPECT_EQ(v2.get(), v1.add_details().move_view);
2381 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2382 // parent.
2383 EXPECT_FALSE(v2->has_add_details());
2384 EXPECT_TRUE(v2->has_remove_details());
2385 EXPECT_EQ(v2.get(), v2->remove_details().parent);
2386 EXPECT_EQ(v4, v2->remove_details().child);
2387 EXPECT_EQ(&v1, v2->remove_details().move_view);
2389 // |v3| is not impacted by this operation, and hence receives no notification.
2390 EXPECT_FALSE(v3->has_add_details());
2391 EXPECT_FALSE(v3->has_remove_details());
2393 // |v4| is the reparented child, so it receives notifications for the remove
2394 // and then the add. |v2| is its old parent, |v1| is its new parent.
2395 EXPECT_TRUE(v4->has_remove_details());
2396 EXPECT_TRUE(v4->has_add_details());
2397 EXPECT_EQ(v2.get(), v4->remove_details().parent);
2398 EXPECT_EQ(&v1, v4->add_details().parent);
2399 EXPECT_EQ(v4, v4->add_details().child);
2400 EXPECT_EQ(v4, v4->remove_details().child);
2401 EXPECT_EQ(&v1, v4->remove_details().move_view);
2402 EXPECT_EQ(v2.get(), v4->add_details().move_view);
2405 // Verifies if the child views added under the root are all deleted when calling
2406 // RemoveAllChildViews.
2407 // The tree looks like this:
2408 // root
2409 // +-- child1
2410 // +-- foo
2411 // +-- bar0
2412 // +-- bar1
2413 // +-- bar2
2414 // +-- child2
2415 // +-- child3
2416 TEST_F(ViewTest, RemoveAllChildViews) {
2417 View root;
2419 View* child1 = new View;
2420 root.AddChildView(child1);
2422 for (int i = 0; i < 2; ++i)
2423 root.AddChildView(new View);
2425 View* foo = new View;
2426 child1->AddChildView(foo);
2428 // Add some nodes to |foo|.
2429 for (int i = 0; i < 3; ++i)
2430 foo->AddChildView(new View);
2432 EXPECT_EQ(3, root.child_count());
2433 EXPECT_EQ(1, child1->child_count());
2434 EXPECT_EQ(3, foo->child_count());
2436 // Now remove all child views from root.
2437 root.RemoveAllChildViews(true);
2439 EXPECT_EQ(0, root.child_count());
2440 EXPECT_FALSE(root.has_children());
2443 TEST_F(ViewTest, Contains) {
2444 View v1;
2445 View* v2 = new View;
2446 View* v3 = new View;
2448 v1.AddChildView(v2);
2449 v2->AddChildView(v3);
2451 EXPECT_FALSE(v1.Contains(NULL));
2452 EXPECT_TRUE(v1.Contains(&v1));
2453 EXPECT_TRUE(v1.Contains(v2));
2454 EXPECT_TRUE(v1.Contains(v3));
2456 EXPECT_FALSE(v2->Contains(NULL));
2457 EXPECT_TRUE(v2->Contains(v2));
2458 EXPECT_FALSE(v2->Contains(&v1));
2459 EXPECT_TRUE(v2->Contains(v3));
2461 EXPECT_FALSE(v3->Contains(NULL));
2462 EXPECT_TRUE(v3->Contains(v3));
2463 EXPECT_FALSE(v3->Contains(&v1));
2464 EXPECT_FALSE(v3->Contains(v2));
2467 // Verifies if GetIndexOf() returns the correct index for the specified child
2468 // view.
2469 // The tree looks like this:
2470 // root
2471 // +-- child1
2472 // +-- foo1
2473 // +-- child2
2474 TEST_F(ViewTest, GetIndexOf) {
2475 View root;
2477 View* child1 = new View;
2478 root.AddChildView(child1);
2480 View* child2 = new View;
2481 root.AddChildView(child2);
2483 View* foo1 = new View;
2484 child1->AddChildView(foo1);
2486 EXPECT_EQ(-1, root.GetIndexOf(NULL));
2487 EXPECT_EQ(-1, root.GetIndexOf(&root));
2488 EXPECT_EQ(0, root.GetIndexOf(child1));
2489 EXPECT_EQ(1, root.GetIndexOf(child2));
2490 EXPECT_EQ(-1, root.GetIndexOf(foo1));
2492 EXPECT_EQ(-1, child1->GetIndexOf(NULL));
2493 EXPECT_EQ(-1, child1->GetIndexOf(&root));
2494 EXPECT_EQ(-1, child1->GetIndexOf(child1));
2495 EXPECT_EQ(-1, child1->GetIndexOf(child2));
2496 EXPECT_EQ(0, child1->GetIndexOf(foo1));
2498 EXPECT_EQ(-1, child2->GetIndexOf(NULL));
2499 EXPECT_EQ(-1, child2->GetIndexOf(&root));
2500 EXPECT_EQ(-1, child2->GetIndexOf(child2));
2501 EXPECT_EQ(-1, child2->GetIndexOf(child1));
2502 EXPECT_EQ(-1, child2->GetIndexOf(foo1));
2505 // Verifies that the child views can be reordered correctly.
2506 TEST_F(ViewTest, ReorderChildren) {
2507 View root;
2509 View* child = new View();
2510 root.AddChildView(child);
2512 View* foo1 = new View();
2513 child->AddChildView(foo1);
2514 View* foo2 = new View();
2515 child->AddChildView(foo2);
2516 View* foo3 = new View();
2517 child->AddChildView(foo3);
2518 foo1->SetFocusable(true);
2519 foo2->SetFocusable(true);
2520 foo3->SetFocusable(true);
2522 ASSERT_EQ(0, child->GetIndexOf(foo1));
2523 ASSERT_EQ(1, child->GetIndexOf(foo2));
2524 ASSERT_EQ(2, child->GetIndexOf(foo3));
2525 ASSERT_EQ(foo2, foo1->GetNextFocusableView());
2526 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
2527 ASSERT_EQ(NULL, foo3->GetNextFocusableView());
2529 // Move |foo2| at the end.
2530 child->ReorderChildView(foo2, -1);
2531 ASSERT_EQ(0, child->GetIndexOf(foo1));
2532 ASSERT_EQ(1, child->GetIndexOf(foo3));
2533 ASSERT_EQ(2, child->GetIndexOf(foo2));
2534 ASSERT_EQ(foo3, foo1->GetNextFocusableView());
2535 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
2536 ASSERT_EQ(NULL, foo2->GetNextFocusableView());
2538 // Move |foo1| at the end.
2539 child->ReorderChildView(foo1, -1);
2540 ASSERT_EQ(0, child->GetIndexOf(foo3));
2541 ASSERT_EQ(1, child->GetIndexOf(foo2));
2542 ASSERT_EQ(2, child->GetIndexOf(foo1));
2543 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
2544 ASSERT_EQ(foo2, foo1->GetPreviousFocusableView());
2545 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
2546 ASSERT_EQ(foo1, foo2->GetNextFocusableView());
2548 // Move |foo2| to the front.
2549 child->ReorderChildView(foo2, 0);
2550 ASSERT_EQ(0, child->GetIndexOf(foo2));
2551 ASSERT_EQ(1, child->GetIndexOf(foo3));
2552 ASSERT_EQ(2, child->GetIndexOf(foo1));
2553 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
2554 ASSERT_EQ(foo3, foo1->GetPreviousFocusableView());
2555 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
2556 ASSERT_EQ(foo1, foo3->GetNextFocusableView());
2559 // Verifies that GetViewByID returns the correctly child view from the specified
2560 // ID.
2561 // The tree looks like this:
2562 // v1
2563 // +-- v2
2564 // +-- v3
2565 // +-- v4
2566 TEST_F(ViewTest, GetViewByID) {
2567 View v1;
2568 const int kV1ID = 1;
2569 v1.set_id(kV1ID);
2571 View v2;
2572 const int kV2ID = 2;
2573 v2.set_id(kV2ID);
2575 View v3;
2576 const int kV3ID = 3;
2577 v3.set_id(kV3ID);
2579 View v4;
2580 const int kV4ID = 4;
2581 v4.set_id(kV4ID);
2583 const int kV5ID = 5;
2585 v1.AddChildView(&v2);
2586 v2.AddChildView(&v3);
2587 v2.AddChildView(&v4);
2589 EXPECT_EQ(&v1, v1.GetViewByID(kV1ID));
2590 EXPECT_EQ(&v2, v1.GetViewByID(kV2ID));
2591 EXPECT_EQ(&v4, v1.GetViewByID(kV4ID));
2593 EXPECT_EQ(NULL, v1.GetViewByID(kV5ID)); // No V5 exists.
2594 EXPECT_EQ(NULL, v2.GetViewByID(kV1ID)); // It can get only from child views.
2596 const int kGroup = 1;
2597 v3.SetGroup(kGroup);
2598 v4.SetGroup(kGroup);
2600 View::Views views;
2601 v1.GetViewsInGroup(kGroup, &views);
2602 EXPECT_EQ(2U, views.size());
2604 View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3));
2605 EXPECT_NE(views.end(), i);
2607 i = std::find(views.begin(), views.end(), &v4);
2608 EXPECT_NE(views.end(), i);
2611 TEST_F(ViewTest, AddExistingChild) {
2612 View v1, v2, v3;
2614 v1.AddChildView(&v2);
2615 v1.AddChildView(&v3);
2616 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2617 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2619 // Check that there's no change in order when adding at same index.
2620 v1.AddChildViewAt(&v2, 0);
2621 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2622 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2623 v1.AddChildViewAt(&v3, 1);
2624 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2625 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2627 // Add it at a different index and check for change in order.
2628 v1.AddChildViewAt(&v2, 1);
2629 EXPECT_EQ(1, v1.GetIndexOf(&v2));
2630 EXPECT_EQ(0, v1.GetIndexOf(&v3));
2631 v1.AddChildViewAt(&v2, 0);
2632 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2633 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2635 // Check that calling |AddChildView()| does not change the order.
2636 v1.AddChildView(&v2);
2637 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2638 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2639 v1.AddChildView(&v3);
2640 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2641 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2644 ////////////////////////////////////////////////////////////////////////////////
2645 // FocusManager
2646 ////////////////////////////////////////////////////////////////////////////////
2648 // A widget that always claims to be active, regardless of its real activation
2649 // status.
2650 class ActiveWidget : public Widget {
2651 public:
2652 ActiveWidget() {}
2653 ~ActiveWidget() override {}
2655 bool IsActive() const override { return true; }
2657 private:
2658 DISALLOW_COPY_AND_ASSIGN(ActiveWidget);
2661 TEST_F(ViewTest, AdvanceFocusIfNecessaryForUnfocusableView) {
2662 // Create a widget with two views and give the first one focus.
2663 ActiveWidget widget;
2664 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2665 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2666 widget.Init(params);
2668 View* view1 = new View();
2669 view1->SetFocusable(true);
2670 widget.GetRootView()->AddChildView(view1);
2671 View* view2 = new View();
2672 view2->SetFocusable(true);
2673 widget.GetRootView()->AddChildView(view2);
2675 FocusManager* focus_manager = widget.GetFocusManager();
2676 ASSERT_TRUE(focus_manager);
2678 focus_manager->SetFocusedView(view1);
2679 EXPECT_EQ(view1, focus_manager->GetFocusedView());
2681 // Disable the focused view and check if the next view gets focused.
2682 view1->SetEnabled(false);
2683 EXPECT_EQ(view2, focus_manager->GetFocusedView());
2685 // Re-enable and re-focus.
2686 view1->SetEnabled(true);
2687 focus_manager->SetFocusedView(view1);
2688 EXPECT_EQ(view1, focus_manager->GetFocusedView());
2690 // Hide the focused view and check it the next view gets focused.
2691 view1->SetVisible(false);
2692 EXPECT_EQ(view2, focus_manager->GetFocusedView());
2694 // Re-show and re-focus.
2695 view1->SetVisible(true);
2696 focus_manager->SetFocusedView(view1);
2697 EXPECT_EQ(view1, focus_manager->GetFocusedView());
2699 // Set the focused view as not focusable and check if the next view gets
2700 // focused.
2701 view1->SetFocusable(false);
2702 EXPECT_EQ(view2, focus_manager->GetFocusedView());
2705 ////////////////////////////////////////////////////////////////////////////////
2706 // Layers
2707 ////////////////////////////////////////////////////////////////////////////////
2709 namespace {
2711 // Test implementation of LayerAnimator.
2712 class TestLayerAnimator : public ui::LayerAnimator {
2713 public:
2714 TestLayerAnimator();
2716 const gfx::Rect& last_bounds() const { return last_bounds_; }
2718 // LayerAnimator.
2719 void SetBounds(const gfx::Rect& bounds) override;
2721 protected:
2722 ~TestLayerAnimator() override {}
2724 private:
2725 gfx::Rect last_bounds_;
2727 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
2730 TestLayerAnimator::TestLayerAnimator()
2731 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2734 void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) {
2735 last_bounds_ = bounds;
2738 } // namespace
2740 class ViewLayerTest : public ViewsTestBase {
2741 public:
2742 ViewLayerTest() : widget_(NULL) {}
2744 ~ViewLayerTest() override {}
2746 // Returns the Layer used by the RootView.
2747 ui::Layer* GetRootLayer() {
2748 return widget()->GetLayer();
2751 void SetUp() override {
2752 ViewTest::SetUp();
2753 widget_ = new Widget;
2754 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2755 params.bounds = gfx::Rect(50, 50, 200, 200);
2756 widget_->Init(params);
2757 widget_->Show();
2758 widget_->GetRootView()->SetBounds(0, 0, 200, 200);
2761 void TearDown() override {
2762 widget_->CloseNow();
2763 ViewsTestBase::TearDown();
2766 Widget* widget() { return widget_; }
2768 private:
2769 Widget* widget_;
2773 TEST_F(ViewLayerTest, LayerToggling) {
2774 // Because we lazily create textures the calls to DrawTree are necessary to
2775 // ensure we trigger creation of textures.
2776 ui::Layer* root_layer = widget()->GetLayer();
2777 View* content_view = new View;
2778 widget()->SetContentsView(content_view);
2780 // Create v1, give it a bounds and verify everything is set up correctly.
2781 View* v1 = new View;
2782 v1->SetPaintToLayer(true);
2783 EXPECT_TRUE(v1->layer() != NULL);
2784 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2785 content_view->AddChildView(v1);
2786 ASSERT_TRUE(v1->layer() != NULL);
2787 EXPECT_EQ(root_layer, v1->layer()->parent());
2788 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds());
2790 // Create v2 as a child of v1 and do basic assertion testing.
2791 View* v2 = new View;
2792 v1->AddChildView(v2);
2793 EXPECT_TRUE(v2->layer() == NULL);
2794 v2->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
2795 v2->SetPaintToLayer(true);
2796 ASSERT_TRUE(v2->layer() != NULL);
2797 EXPECT_EQ(v1->layer(), v2->layer()->parent());
2798 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
2800 // Turn off v1s layer. v2 should still have a layer but its parent should have
2801 // changed.
2802 v1->SetPaintToLayer(false);
2803 EXPECT_TRUE(v1->layer() == NULL);
2804 EXPECT_TRUE(v2->layer() != NULL);
2805 EXPECT_EQ(root_layer, v2->layer()->parent());
2806 ASSERT_EQ(1u, root_layer->children().size());
2807 EXPECT_EQ(root_layer->children()[0], v2->layer());
2808 // The bounds of the layer should have changed to be relative to the root view
2809 // now.
2810 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds());
2812 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2813 gfx::Transform transform;
2814 transform.Scale(2.0, 2.0);
2815 v1->SetTransform(transform);
2816 EXPECT_TRUE(v1->layer() != NULL);
2817 EXPECT_TRUE(v2->layer() != NULL);
2818 EXPECT_EQ(root_layer, v1->layer()->parent());
2819 EXPECT_EQ(v1->layer(), v2->layer()->parent());
2820 ASSERT_EQ(1u, root_layer->children().size());
2821 EXPECT_EQ(root_layer->children()[0], v1->layer());
2822 ASSERT_EQ(1u, v1->layer()->children().size());
2823 EXPECT_EQ(v1->layer()->children()[0], v2->layer());
2824 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
2827 // Verifies turning on a layer wires up children correctly.
2828 TEST_F(ViewLayerTest, NestedLayerToggling) {
2829 View* content_view = new View;
2830 widget()->SetContentsView(content_view);
2832 // Create v1, give it a bounds and verify everything is set up correctly.
2833 View* v1 = new View;
2834 content_view->AddChildView(v1);
2835 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2837 View* v2 = new View;
2838 v1->AddChildView(v2);
2840 View* v3 = new View;
2841 v3->SetPaintToLayer(true);
2842 v2->AddChildView(v3);
2843 ASSERT_TRUE(v3->layer() != NULL);
2845 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2847 v1->SetPaintToLayer(true);
2848 EXPECT_EQ(v1->layer(), v3->layer()->parent());
2851 TEST_F(ViewLayerTest, LayerAnimator) {
2852 View* content_view = new View;
2853 widget()->SetContentsView(content_view);
2855 View* v1 = new View;
2856 content_view->AddChildView(v1);
2857 v1->SetPaintToLayer(true);
2858 EXPECT_TRUE(v1->layer() != NULL);
2860 TestLayerAnimator* animator = new TestLayerAnimator();
2861 v1->layer()->SetAnimator(animator);
2863 gfx::Rect bounds(1, 2, 3, 4);
2864 v1->SetBoundsRect(bounds);
2865 EXPECT_EQ(bounds, animator->last_bounds());
2866 // TestLayerAnimator doesn't update the layer.
2867 EXPECT_NE(bounds, v1->layer()->bounds());
2870 // Verifies the bounds of a layer are updated if the bounds of ancestor that
2871 // doesn't have a layer change.
2872 TEST_F(ViewLayerTest, BoundsChangeWithLayer) {
2873 View* content_view = new View;
2874 widget()->SetContentsView(content_view);
2876 View* v1 = new View;
2877 content_view->AddChildView(v1);
2878 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2880 View* v2 = new View;
2881 v2->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
2882 v1->AddChildView(v2);
2883 v2->SetPaintToLayer(true);
2884 ASSERT_TRUE(v2->layer() != NULL);
2885 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
2887 v1->SetPosition(gfx::Point(25, 36));
2888 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
2890 v2->SetPosition(gfx::Point(11, 12));
2891 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
2893 // Bounds of the layer should change even if the view is not invisible.
2894 v1->SetVisible(false);
2895 v1->SetPosition(gfx::Point(20, 30));
2896 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
2898 v2->SetVisible(false);
2899 v2->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
2900 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
2903 // Make sure layers are positioned correctly in RTL.
2904 TEST_F(ViewLayerTest, BoundInRTL) {
2905 std::string locale = l10n_util::GetApplicationLocale(std::string());
2906 base::i18n::SetICUDefaultLocale("he");
2908 View* view = new View;
2909 widget()->SetContentsView(view);
2911 int content_width = view->width();
2913 // |v1| is initially not attached to anything. So its layer will have the same
2914 // bounds as the view.
2915 View* v1 = new View;
2916 v1->SetPaintToLayer(true);
2917 v1->SetBounds(10, 10, 20, 10);
2918 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
2919 v1->layer()->bounds());
2921 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
2922 // bounds.
2923 view->AddChildView(v1);
2924 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
2925 v1->layer()->bounds());
2926 gfx::Rect l1bounds = v1->layer()->bounds();
2928 // Now attach a View to the widget first, then create a layer for it. Make
2929 // sure the bounds are correct.
2930 View* v2 = new View;
2931 v2->SetBounds(50, 10, 30, 10);
2932 EXPECT_FALSE(v2->layer());
2933 view->AddChildView(v2);
2934 v2->SetPaintToLayer(true);
2935 EXPECT_EQ(gfx::Rect(content_width - 80, 10, 30, 10),
2936 v2->layer()->bounds());
2937 gfx::Rect l2bounds = v2->layer()->bounds();
2939 view->SetPaintToLayer(true);
2940 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2941 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2943 // Move one of the views. Make sure the layer is positioned correctly
2944 // afterwards.
2945 v1->SetBounds(v1->x() - 5, v1->y(), v1->width(), v1->height());
2946 l1bounds.set_x(l1bounds.x() + 5);
2947 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2949 view->SetPaintToLayer(false);
2950 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2951 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2953 // Move a view again.
2954 v2->SetBounds(v2->x() + 5, v2->y(), v2->width(), v2->height());
2955 l2bounds.set_x(l2bounds.x() - 5);
2956 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2958 // Reset locale.
2959 base::i18n::SetICUDefaultLocale(locale);
2962 // Make sure that resizing a parent in RTL correctly repositions its children.
2963 TEST_F(ViewLayerTest, ResizeParentInRTL) {
2964 std::string locale = l10n_util::GetApplicationLocale(std::string());
2965 base::i18n::SetICUDefaultLocale("he");
2967 View* view = new View;
2968 widget()->SetContentsView(view);
2970 int content_width = view->width();
2972 // Create a paints-to-layer view |v1|.
2973 View* v1 = new View;
2974 v1->SetPaintToLayer(true);
2975 v1->SetBounds(10, 10, 20, 10);
2976 view->AddChildView(v1);
2977 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
2978 v1->layer()->bounds());
2980 // Attach a paints-to-layer child view to |v1|.
2981 View* v2 = new View;
2982 v2->SetPaintToLayer(true);
2983 v2->SetBounds(3, 5, 6, 4);
2984 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
2985 v2->layer()->bounds());
2986 v1->AddChildView(v2);
2987 // Check that |v2| now has RTL-appropriate bounds.
2988 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
2989 v2->layer()->bounds());
2991 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
2992 View* v3 = new View;
2993 v3->SetBounds(1, 1, 18, 8);
2994 View* v4 = new View;
2995 v4->SetPaintToLayer(true);
2996 v4->SetBounds(2, 4, 6, 4);
2997 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
2998 v4->layer()->bounds());
2999 v3->AddChildView(v4);
3000 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
3001 v4->layer()->bounds());
3002 v1->AddChildView(v3);
3003 // Check that |v4| now has RTL-appropriate bounds.
3004 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3005 v4->layer()->bounds());
3007 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3008 // correctly to RTL-appropriate bounds.
3009 v1->SetSize(gfx::Size(30, 10));
3010 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3011 v2->layer()->bounds());
3012 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3013 v4->layer()->bounds());
3015 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3016 // to RTL-appropriate bounds.
3017 v3->SetBounds(2, 1, 12, 8);
3018 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3019 v4->layer()->bounds());
3021 // Reset locale.
3022 base::i18n::SetICUDefaultLocale(locale);
3025 // Makes sure a transform persists after toggling the visibility.
3026 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
3027 View* view = new View;
3028 gfx::Transform transform;
3029 transform.Scale(2.0, 2.0);
3030 view->SetTransform(transform);
3031 widget()->SetContentsView(view);
3032 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3034 view->SetVisible(false);
3035 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3037 view->SetVisible(true);
3038 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3041 // Verifies a transform persists after removing/adding a view with a transform.
3042 TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) {
3043 View* view = new View;
3044 gfx::Transform transform;
3045 transform.Scale(2.0, 2.0);
3046 view->SetTransform(transform);
3047 widget()->SetContentsView(view);
3048 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3049 ASSERT_TRUE(view->layer() != NULL);
3050 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3052 View* parent = view->parent();
3053 parent->RemoveChildView(view);
3054 parent->AddChildView(view);
3056 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3057 ASSERT_TRUE(view->layer() != NULL);
3058 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3061 // Makes sure that layer visibility is correct after toggling View visibility.
3062 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
3063 View* content_view = new View;
3064 widget()->SetContentsView(content_view);
3066 // The view isn't attached to a widget or a parent view yet. But it should
3067 // still have a layer, but the layer should not be attached to the root
3068 // layer.
3069 View* v1 = new View;
3070 v1->SetPaintToLayer(true);
3071 EXPECT_TRUE(v1->layer());
3072 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3073 v1->layer()));
3075 // Once the view is attached to a widget, its layer should be attached to the
3076 // root layer and visible.
3077 content_view->AddChildView(v1);
3078 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3079 v1->layer()));
3080 EXPECT_TRUE(v1->layer()->IsDrawn());
3082 v1->SetVisible(false);
3083 EXPECT_FALSE(v1->layer()->IsDrawn());
3085 v1->SetVisible(true);
3086 EXPECT_TRUE(v1->layer()->IsDrawn());
3088 widget()->Hide();
3089 EXPECT_FALSE(v1->layer()->IsDrawn());
3091 widget()->Show();
3092 EXPECT_TRUE(v1->layer()->IsDrawn());
3095 // Tests that the layers in the subtree are orphaned after a View is removed
3096 // from the parent.
3097 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
3098 View* content_view = new View;
3099 widget()->SetContentsView(content_view);
3101 View* v1 = new View;
3102 content_view->AddChildView(v1);
3104 View* v2 = new View;
3105 v1->AddChildView(v2);
3106 v2->SetPaintToLayer(true);
3107 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3108 v2->layer()));
3109 EXPECT_TRUE(v2->layer()->IsDrawn());
3111 content_view->RemoveChildView(v1);
3113 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3114 v2->layer()));
3116 // Reparent |v2|.
3117 content_view->AddChildView(v2);
3118 delete v1;
3119 v1 = NULL;
3120 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3121 v2->layer()));
3122 EXPECT_TRUE(v2->layer()->IsDrawn());
3125 class PaintTrackingView : public View {
3126 public:
3127 PaintTrackingView() : painted_(false) {
3130 bool painted() const { return painted_; }
3131 void set_painted(bool value) { painted_ = value; }
3133 void OnPaint(gfx::Canvas* canvas) override { painted_ = true; }
3135 private:
3136 bool painted_;
3138 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView);
3141 // Makes sure child views with layers aren't painted when paint starts at an
3142 // ancestor.
3143 TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) {
3144 PaintTrackingView* content_view = new PaintTrackingView;
3145 widget()->SetContentsView(content_view);
3146 content_view->SetPaintToLayer(true);
3147 GetRootLayer()->GetCompositor()->ScheduleDraw();
3148 ui::DrawWaiterForTest::WaitForCompositingEnded(
3149 GetRootLayer()->GetCompositor());
3150 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3151 content_view->set_painted(false);
3152 // content_view no longer has a dirty rect. Paint from the root and make sure
3153 // PaintTrackingView isn't painted.
3154 GetRootLayer()->GetCompositor()->ScheduleDraw();
3155 ui::DrawWaiterForTest::WaitForCompositingEnded(
3156 GetRootLayer()->GetCompositor());
3157 EXPECT_FALSE(content_view->painted());
3159 // Make content_view have a dirty rect, paint the layers and make sure
3160 // PaintTrackingView is painted.
3161 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3162 GetRootLayer()->GetCompositor()->ScheduleDraw();
3163 ui::DrawWaiterForTest::WaitForCompositingEnded(
3164 GetRootLayer()->GetCompositor());
3165 EXPECT_TRUE(content_view->painted());
3168 // Tests that the visibility of child layers are updated correctly when a View's
3169 // visibility changes.
3170 TEST_F(ViewLayerTest, VisibilityChildLayers) {
3171 View* v1 = new View;
3172 v1->SetPaintToLayer(true);
3173 widget()->SetContentsView(v1);
3175 View* v2 = new View;
3176 v1->AddChildView(v2);
3178 View* v3 = new View;
3179 v2->AddChildView(v3);
3180 v3->SetVisible(false);
3182 View* v4 = new View;
3183 v4->SetPaintToLayer(true);
3184 v3->AddChildView(v4);
3186 EXPECT_TRUE(v1->layer()->IsDrawn());
3187 EXPECT_FALSE(v4->layer()->IsDrawn());
3189 v2->SetVisible(false);
3190 EXPECT_TRUE(v1->layer()->IsDrawn());
3191 EXPECT_FALSE(v4->layer()->IsDrawn());
3193 v2->SetVisible(true);
3194 EXPECT_TRUE(v1->layer()->IsDrawn());
3195 EXPECT_FALSE(v4->layer()->IsDrawn());
3197 v2->SetVisible(false);
3198 EXPECT_TRUE(v1->layer()->IsDrawn());
3199 EXPECT_FALSE(v4->layer()->IsDrawn());
3200 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3202 v3->SetVisible(true);
3203 EXPECT_TRUE(v1->layer()->IsDrawn());
3204 EXPECT_FALSE(v4->layer()->IsDrawn());
3205 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3207 // Reparent |v3| to |v1|.
3208 v1->AddChildView(v3);
3209 EXPECT_TRUE(v1->layer()->IsDrawn());
3210 EXPECT_TRUE(v4->layer()->IsDrawn());
3211 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3214 // This test creates a random View tree, and then randomly reorders child views,
3215 // reparents views etc. Unrelated changes can appear to break this test. So
3216 // marking this as FLAKY.
3217 TEST_F(ViewLayerTest, DISABLED_ViewLayerTreesInSync) {
3218 View* content = new View;
3219 content->SetPaintToLayer(true);
3220 widget()->SetContentsView(content);
3221 widget()->Show();
3223 ConstructTree(content, 5);
3224 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3226 ScrambleTree(content);
3227 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3229 ScrambleTree(content);
3230 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3232 ScrambleTree(content);
3233 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3236 // Verifies when views are reordered the layer is also reordered. The widget is
3237 // providing the parent layer.
3238 TEST_F(ViewLayerTest, ReorderUnderWidget) {
3239 View* content = new View;
3240 widget()->SetContentsView(content);
3241 View* c1 = new View;
3242 c1->SetPaintToLayer(true);
3243 content->AddChildView(c1);
3244 View* c2 = new View;
3245 c2->SetPaintToLayer(true);
3246 content->AddChildView(c2);
3248 ui::Layer* parent_layer = c1->layer()->parent();
3249 ASSERT_TRUE(parent_layer);
3250 ASSERT_EQ(2u, parent_layer->children().size());
3251 EXPECT_EQ(c1->layer(), parent_layer->children()[0]);
3252 EXPECT_EQ(c2->layer(), parent_layer->children()[1]);
3254 // Move c1 to the front. The layers should have moved too.
3255 content->ReorderChildView(c1, -1);
3256 EXPECT_EQ(c1->layer(), parent_layer->children()[1]);
3257 EXPECT_EQ(c2->layer(), parent_layer->children()[0]);
3260 // Verifies that the layer of a view can be acquired properly.
3261 TEST_F(ViewLayerTest, AcquireLayer) {
3262 View* content = new View;
3263 widget()->SetContentsView(content);
3264 scoped_ptr<View> c1(new View);
3265 c1->SetPaintToLayer(true);
3266 EXPECT_TRUE(c1->layer());
3267 content->AddChildView(c1.get());
3269 scoped_ptr<ui::Layer> layer(c1->AcquireLayer());
3270 EXPECT_EQ(layer.get(), c1->layer());
3272 scoped_ptr<ui::Layer> layer2(c1->RecreateLayer());
3273 EXPECT_NE(c1->layer(), layer2.get());
3275 // Destroy view before destroying layer.
3276 c1.reset();
3279 // Verify the z-order of the layers as a result of calling RecreateLayer().
3280 TEST_F(ViewLayerTest, RecreateLayerZOrder) {
3281 scoped_ptr<View> v(new View());
3282 v->SetPaintToLayer(true);
3284 View* v1 = new View();
3285 v1->SetPaintToLayer(true);
3286 v->AddChildView(v1);
3287 View* v2 = new View();
3288 v2->SetPaintToLayer(true);
3289 v->AddChildView(v2);
3291 // Test the initial z-order.
3292 const std::vector<ui::Layer*>& child_layers_pre = v->layer()->children();
3293 ASSERT_EQ(2u, child_layers_pre.size());
3294 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3295 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3297 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3299 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3300 // for |v1| and |v2|.
3301 const std::vector<ui::Layer*>& child_layers_post = v->layer()->children();
3302 ASSERT_EQ(3u, child_layers_post.size());
3303 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3304 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3305 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3308 // Verify the z-order of the layers as a result of calling RecreateLayer when
3309 // the widget is the parent with the layer.
3310 TEST_F(ViewLayerTest, RecreateLayerZOrderWidgetParent) {
3311 View* v = new View();
3312 widget()->SetContentsView(v);
3314 View* v1 = new View();
3315 v1->SetPaintToLayer(true);
3316 v->AddChildView(v1);
3317 View* v2 = new View();
3318 v2->SetPaintToLayer(true);
3319 v->AddChildView(v2);
3321 ui::Layer* root_layer = GetRootLayer();
3323 // Test the initial z-order.
3324 const std::vector<ui::Layer*>& child_layers_pre = root_layer->children();
3325 ASSERT_EQ(2u, child_layers_pre.size());
3326 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3327 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3329 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3331 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3332 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3333 ASSERT_EQ(3u, child_layers_post.size());
3334 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3335 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3336 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3339 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3340 // a View.
3341 TEST_F(ViewLayerTest, RecreateLayerMovesNonViewChildren) {
3342 View v;
3343 v.SetPaintToLayer(true);
3344 View child;
3345 child.SetPaintToLayer(true);
3346 v.AddChildView(&child);
3347 ASSERT_TRUE(v.layer() != NULL);
3348 ASSERT_EQ(1u, v.layer()->children().size());
3349 EXPECT_EQ(v.layer()->children()[0], child.layer());
3351 ui::Layer layer(ui::LAYER_NOT_DRAWN);
3352 v.layer()->Add(&layer);
3353 v.layer()->StackAtBottom(&layer);
3355 scoped_ptr<ui::Layer> old_layer(v.RecreateLayer());
3357 // All children should be moved from old layer to new layer.
3358 ASSERT_TRUE(old_layer.get() != NULL);
3359 EXPECT_TRUE(old_layer->children().empty());
3361 // And new layer should have the two children.
3362 ASSERT_TRUE(v.layer() != NULL);
3363 ASSERT_EQ(2u, v.layer()->children().size());
3364 EXPECT_EQ(v.layer()->children()[0], &layer);
3365 EXPECT_EQ(v.layer()->children()[1], child.layer());
3368 class BoundsTreeTestView : public View {
3369 public:
3370 BoundsTreeTestView() {}
3372 void PaintChildren(gfx::Canvas* canvas, const CullSet& cull_set) override {
3373 // Save out a copy of the cull_set before calling the base implementation.
3374 last_cull_set_.clear();
3375 if (cull_set.cull_set_) {
3376 for (base::hash_set<intptr_t>::iterator it = cull_set.cull_set_->begin();
3377 it != cull_set.cull_set_->end();
3378 ++it) {
3379 last_cull_set_.insert(reinterpret_cast<View*>(*it));
3382 View::PaintChildren(canvas, cull_set);
3385 std::set<View*> last_cull_set_;
3388 TEST_F(ViewLayerTest, BoundsTreePaintUpdatesCullSet) {
3389 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3390 widget()->SetContentsView(test_view);
3392 View* v1 = new View();
3393 v1->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3394 test_view->AddChildView(v1);
3396 View* v2 = new View();
3397 v2->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3398 v1->AddChildView(v2);
3400 // Schedule a full-view paint to get everyone's rectangles updated.
3401 test_view->SchedulePaintInRect(test_view->bounds());
3402 GetRootLayer()->GetCompositor()->ScheduleDraw();
3403 ui::DrawWaiterForTest::WaitForCompositingEnded(
3404 GetRootLayer()->GetCompositor());
3406 // Now we have test_view - v1 - v2. Damage to only test_view should only
3407 // return root_view and test_view.
3408 test_view->SchedulePaintInRect(gfx::Rect(0, 0, 1, 1));
3409 GetRootLayer()->GetCompositor()->ScheduleDraw();
3410 ui::DrawWaiterForTest::WaitForCompositingEnded(
3411 GetRootLayer()->GetCompositor());
3412 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3413 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3414 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3416 // Damage to v1 only should only return root_view, test_view, and v1.
3417 test_view->SchedulePaintInRect(gfx::Rect(11, 16, 1, 1));
3418 GetRootLayer()->GetCompositor()->ScheduleDraw();
3419 ui::DrawWaiterForTest::WaitForCompositingEnded(
3420 GetRootLayer()->GetCompositor());
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 test_view->SchedulePaintInRect(gfx::Rect(31, 49, 1, 1));
3429 GetRootLayer()->GetCompositor()->ScheduleDraw();
3430 ui::DrawWaiterForTest::WaitForCompositingEnded(
3431 GetRootLayer()->GetCompositor());
3432 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3433 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3434 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3435 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3436 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3439 TEST_F(ViewLayerTest, BoundsTreeWithRTL) {
3440 std::string locale = l10n_util::GetApplicationLocale(std::string());
3441 base::i18n::SetICUDefaultLocale("ar");
3443 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3444 widget()->SetContentsView(test_view);
3446 // Add child views, which should be in RTL coordinate space of parent view.
3447 View* v1 = new View;
3448 v1->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3449 test_view->AddChildView(v1);
3451 View* v2 = new View;
3452 v2->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3453 v1->AddChildView(v2);
3455 // Schedule a full-view paint to get everyone's rectangles updated.
3456 test_view->SchedulePaintInRect(test_view->bounds());
3457 GetRootLayer()->GetCompositor()->ScheduleDraw();
3458 ui::DrawWaiterForTest::WaitForCompositingEnded(
3459 GetRootLayer()->GetCompositor());
3461 // Damage to the right side of the parent view should touch both child views.
3462 gfx::Rect rtl_damage(test_view->bounds().width() - 16, 18, 1, 1);
3463 test_view->SchedulePaintInRect(rtl_damage);
3464 GetRootLayer()->GetCompositor()->ScheduleDraw();
3465 ui::DrawWaiterForTest::WaitForCompositingEnded(
3466 GetRootLayer()->GetCompositor());
3467 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3468 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3469 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3470 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3471 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3473 // Damage to the left side of the parent view should only touch the
3474 // container views.
3475 gfx::Rect ltr_damage(16, 18, 1, 1);
3476 test_view->SchedulePaintInRect(ltr_damage);
3477 GetRootLayer()->GetCompositor()->ScheduleDraw();
3478 ui::DrawWaiterForTest::WaitForCompositingEnded(
3479 GetRootLayer()->GetCompositor());
3480 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3481 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3482 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3484 // Reset locale.
3485 base::i18n::SetICUDefaultLocale(locale);
3488 TEST_F(ViewLayerTest, BoundsTreeSetBoundsChangesCullSet) {
3489 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3490 widget()->SetContentsView(test_view);
3492 View* v1 = new View;
3493 v1->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3494 test_view->AddChildView(v1);
3496 View* v2 = new View;
3497 v2->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3498 v1->AddChildView(v2);
3500 // Schedule a full-view paint to get everyone's rectangles updated.
3501 test_view->SchedulePaintInRect(test_view->bounds());
3502 GetRootLayer()->GetCompositor()->ScheduleDraw();
3503 ui::DrawWaiterForTest::WaitForCompositingEnded(
3504 GetRootLayer()->GetCompositor());
3506 // Move v1 to a new origin out of the way of our next query.
3507 v1->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3508 // The move will force a repaint.
3509 GetRootLayer()->GetCompositor()->ScheduleDraw();
3510 ui::DrawWaiterForTest::WaitForCompositingEnded(
3511 GetRootLayer()->GetCompositor());
3513 // Schedule a paint with damage rect where v1 used to be.
3514 test_view->SchedulePaintInRect(gfx::Rect(5, 6, 10, 11));
3515 GetRootLayer()->GetCompositor()->ScheduleDraw();
3516 ui::DrawWaiterForTest::WaitForCompositingEnded(
3517 GetRootLayer()->GetCompositor());
3519 // Should only have picked up root_view and test_view.
3520 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3521 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3522 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3525 TEST_F(ViewLayerTest, BoundsTreeLayerChangeMakesNewTree) {
3526 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3527 widget()->SetContentsView(test_view);
3529 View* v1 = new View;
3530 v1->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3531 test_view->AddChildView(v1);
3533 View* v2 = new View;
3534 v2->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3535 v1->AddChildView(v2);
3537 // Schedule a full-view paint to get everyone's rectangles updated.
3538 test_view->SchedulePaintInRect(test_view->bounds());
3539 GetRootLayer()->GetCompositor()->ScheduleDraw();
3540 ui::DrawWaiterForTest::WaitForCompositingEnded(
3541 GetRootLayer()->GetCompositor());
3543 // Set v1 to paint to its own layer, it should remove itself from the
3544 // test_view heiarchy and no longer intersect with damage rects in that cull
3545 // set.
3546 v1->SetPaintToLayer(true);
3548 // Schedule another full-view paint.
3549 test_view->SchedulePaintInRect(test_view->bounds());
3550 GetRootLayer()->GetCompositor()->ScheduleDraw();
3551 ui::DrawWaiterForTest::WaitForCompositingEnded(
3552 GetRootLayer()->GetCompositor());
3553 // v1 and v2 should no longer be present in the test_view cull_set.
3554 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3555 EXPECT_EQ(0U, test_view->last_cull_set_.count(v1));
3556 EXPECT_EQ(0U, test_view->last_cull_set_.count(v2));
3558 // Now set v1 back to not painting to a layer.
3559 v1->SetPaintToLayer(false);
3560 // Schedule another full-view paint.
3561 test_view->SchedulePaintInRect(test_view->bounds());
3562 GetRootLayer()->GetCompositor()->ScheduleDraw();
3563 ui::DrawWaiterForTest::WaitForCompositingEnded(
3564 GetRootLayer()->GetCompositor());
3565 // We should be back to the full cull set including v1 and v2.
3566 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3567 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3568 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3569 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3570 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3573 TEST_F(ViewLayerTest, BoundsTreeRemoveChildRemovesBounds) {
3574 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3575 widget()->SetContentsView(test_view);
3577 View* v1 = new View;
3578 v1->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3579 test_view->AddChildView(v1);
3581 View* v2 = new View;
3582 v2->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3583 v1->AddChildView(v2);
3585 // Schedule a full-view paint to get everyone's rectangles updated.
3586 test_view->SchedulePaintInRect(test_view->bounds());
3587 GetRootLayer()->GetCompositor()->ScheduleDraw();
3588 ui::DrawWaiterForTest::WaitForCompositingEnded(
3589 GetRootLayer()->GetCompositor());
3591 // Now remove v1 from the root view.
3592 test_view->RemoveChildView(v1);
3594 // Schedule another full-view paint.
3595 test_view->SchedulePaintInRect(test_view->bounds());
3596 GetRootLayer()->GetCompositor()->ScheduleDraw();
3597 ui::DrawWaiterForTest::WaitForCompositingEnded(
3598 GetRootLayer()->GetCompositor());
3599 // v1 and v2 should no longer be present in the test_view cull_set.
3600 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3601 EXPECT_EQ(0U, test_view->last_cull_set_.count(v1));
3602 EXPECT_EQ(0U, test_view->last_cull_set_.count(v2));
3604 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3605 // deleted with that hierarchy.
3606 delete v1;
3609 TEST_F(ViewLayerTest, BoundsTreeMoveViewMovesBounds) {
3610 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3611 widget()->SetContentsView(test_view);
3613 // Build hierarchy v1 - v2 - v3.
3614 View* v1 = new View;
3615 v1->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3616 test_view->AddChildView(v1);
3618 View* v2 = new View;
3619 v2->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3620 v1->AddChildView(v2);
3622 View* v3 = new View;
3623 v3->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3624 v2->AddChildView(v3);
3626 // Schedule a full-view paint and ensure all views are present in the cull.
3627 test_view->SchedulePaintInRect(test_view->bounds());
3628 GetRootLayer()->GetCompositor()->ScheduleDraw();
3629 ui::DrawWaiterForTest::WaitForCompositingEnded(
3630 GetRootLayer()->GetCompositor());
3631 EXPECT_EQ(5U, test_view->last_cull_set_.size());
3632 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3633 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3634 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3635 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3636 EXPECT_EQ(1U, test_view->last_cull_set_.count(v3));
3638 // Build an unrelated view hierarchy and move v2 in to it.
3639 scoped_ptr<Widget> test_widget(new Widget);
3640 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3641 params.bounds = gfx::Rect(10, 10, 500, 500);
3642 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3643 test_widget->Init(params);
3644 test_widget->Show();
3645 BoundsTreeTestView* widget_view = new BoundsTreeTestView;
3646 test_widget->SetContentsView(widget_view);
3647 widget_view->AddChildView(v2);
3649 // Now schedule full-view paints in both widgets.
3650 test_view->SchedulePaintInRect(test_view->bounds());
3651 widget_view->SchedulePaintInRect(widget_view->bounds());
3652 GetRootLayer()->GetCompositor()->ScheduleDraw();
3653 ui::DrawWaiterForTest::WaitForCompositingEnded(
3654 GetRootLayer()->GetCompositor());
3656 // Only v1 should be present in the first cull set.
3657 EXPECT_EQ(3U, test_view->last_cull_set_.size());
3658 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3659 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3660 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3662 // We should find v2 and v3 in the widget_view cull_set.
3663 EXPECT_EQ(4U, widget_view->last_cull_set_.size());
3664 EXPECT_EQ(1U, widget_view->last_cull_set_.count(test_widget->GetRootView()));
3665 EXPECT_EQ(1U, widget_view->last_cull_set_.count(widget_view));
3666 EXPECT_EQ(1U, widget_view->last_cull_set_.count(v2));
3667 EXPECT_EQ(1U, widget_view->last_cull_set_.count(v3));
3670 namespace {
3672 std::string ToString(const gfx::Vector2dF& vector) {
3673 return base::StringPrintf("%.2f %0.2f", vector.x(), vector.y());
3676 } // namespace
3678 TEST_F(ViewLayerTest, SnapLayerToPixel) {
3679 View* v1 = new View;
3681 View* v11 = new View;
3682 v1->AddChildView(v11);
3684 widget()->SetContentsView(v1);
3686 const gfx::Size& size = GetRootLayer()->GetCompositor()->size();
3687 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f, size);
3689 v11->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3690 v1->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3691 v11->SetPaintToLayer(true);
3693 EXPECT_EQ("0.40 0.40", ToString(v11->layer()->subpixel_position_offset()));
3695 // Creating a layer in parent should update the child view's layer offset.
3696 v1->SetPaintToLayer(true);
3697 EXPECT_EQ("-0.20 -0.20", ToString(v1->layer()->subpixel_position_offset()));
3698 EXPECT_EQ("-0.20 -0.20", ToString(v11->layer()->subpixel_position_offset()));
3700 // DSF change should get propagated and update offsets.
3701 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f, size);
3702 EXPECT_EQ("0.33 0.33", ToString(v1->layer()->subpixel_position_offset()));
3703 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3705 // Deleting parent's layer should update the child view's layer's offset.
3706 v1->SetPaintToLayer(false);
3707 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3709 // Setting parent view should update the child view's layer's offset.
3710 v1->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3711 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3713 // Setting integral DSF should reset the offset.
3714 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f, size);
3715 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3718 TEST_F(ViewTest, FocusableAssertions) {
3719 // View subclasses may change insets based on whether they are focusable,
3720 // which effects the preferred size. To avoid preferred size changing around
3721 // these Views need to key off the last value set to SetFocusable(), not
3722 // whether the View is focusable right now. For this reason it's important
3723 // that focusable() return the last value passed to SetFocusable and not
3724 // whether the View is focusable right now.
3725 TestView view;
3726 view.SetFocusable(true);
3727 EXPECT_TRUE(view.focusable());
3728 view.SetEnabled(false);
3729 EXPECT_TRUE(view.focusable());
3730 view.SetFocusable(false);
3731 EXPECT_FALSE(view.focusable());
3734 // Verifies when a view is deleted it is removed from ViewStorage.
3735 TEST_F(ViewTest, UpdateViewStorageOnDelete) {
3736 ViewStorage* view_storage = ViewStorage::GetInstance();
3737 const int storage_id = view_storage->CreateStorageID();
3739 View view;
3740 view_storage->StoreView(storage_id, &view);
3742 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL);
3745 ////////////////////////////////////////////////////////////////////////////////
3746 // NativeTheme
3747 ////////////////////////////////////////////////////////////////////////////////
3749 void TestView::OnNativeThemeChanged(const ui::NativeTheme* native_theme) {
3750 native_theme_ = native_theme;
3753 TEST_F(ViewTest, OnNativeThemeChanged) {
3754 TestView* test_view = new TestView();
3755 EXPECT_FALSE(test_view->native_theme_);
3756 TestView* test_view_child = new TestView();
3757 EXPECT_FALSE(test_view_child->native_theme_);
3759 // Child view added before the widget hierarchy exists should get the
3760 // new native theme notification.
3761 test_view->AddChildView(test_view_child);
3763 scoped_ptr<Widget> widget(new Widget);
3764 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
3765 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3766 widget->Init(params);
3768 widget->GetRootView()->AddChildView(test_view);
3769 EXPECT_TRUE(test_view->native_theme_);
3770 EXPECT_EQ(widget->GetNativeTheme(), test_view->native_theme_);
3771 EXPECT_TRUE(test_view_child->native_theme_);
3772 EXPECT_EQ(widget->GetNativeTheme(), test_view_child->native_theme_);
3774 // Child view added after the widget hierarchy exists should also get the
3775 // notification.
3776 TestView* test_view_child_2 = new TestView();
3777 test_view->AddChildView(test_view_child_2);
3778 EXPECT_TRUE(test_view_child_2->native_theme_);
3779 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_);
3781 widget->CloseNow();
3784 } // namespace views