Revert 287747 "Make SingleThreadProxy a SchedulerClient"
[chromium-blink-merge.git] / ui / views / view_unittest.cc
blob544e9997450b7643b940e0d388a0c42600f241ec
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 "grit/ui_strings.h"
13 #include "ui/base/accelerators/accelerator.h"
14 #include "ui/base/clipboard/clipboard.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/compositor/compositor.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/layer_animator.h"
19 #include "ui/compositor/test/draw_waiter_for_test.h"
20 #include "ui/events/event.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/views/background.h"
26 #include "ui/views/controls/native/native_view_host.h"
27 #include "ui/views/controls/scroll_view.h"
28 #include "ui/views/controls/textfield/textfield.h"
29 #include "ui/views/focus/view_storage.h"
30 #include "ui/views/test/views_test_base.h"
31 #include "ui/views/view.h"
32 #include "ui/views/views_delegate.h"
33 #include "ui/views/widget/native_widget.h"
34 #include "ui/views/widget/root_view.h"
35 #include "ui/views/window/dialog_client_view.h"
36 #include "ui/views/window/dialog_delegate.h"
38 using base::ASCIIToUTF16;
40 namespace {
42 // Returns true if |ancestor| is an ancestor of |layer|.
43 bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) {
44 while (layer && layer != ancestor)
45 layer = layer->parent();
46 return layer == ancestor;
49 // Convenience functions for walking a View tree.
50 const views::View* FirstView(const views::View* view) {
51 const views::View* v = view;
52 while (v->has_children())
53 v = v->child_at(0);
54 return v;
57 const views::View* NextView(const views::View* view) {
58 const views::View* v = view;
59 const views::View* parent = v->parent();
60 if (!parent)
61 return NULL;
62 int next = parent->GetIndexOf(v) + 1;
63 if (next != parent->child_count())
64 return FirstView(parent->child_at(next));
65 return parent;
68 // Convenience functions for walking a Layer tree.
69 const ui::Layer* FirstLayer(const ui::Layer* layer) {
70 const ui::Layer* l = layer;
71 while (l->children().size() > 0)
72 l = l->children()[0];
73 return l;
76 const ui::Layer* NextLayer(const ui::Layer* layer) {
77 const ui::Layer* parent = layer->parent();
78 if (!parent)
79 return NULL;
80 const std::vector<ui::Layer*> children = parent->children();
81 size_t index;
82 for (index = 0; index < children.size(); index++) {
83 if (children[index] == layer)
84 break;
86 size_t next = index + 1;
87 if (next < children.size())
88 return FirstLayer(children[next]);
89 return parent;
92 // Given the root nodes of a View tree and a Layer tree, makes sure the two
93 // trees are in sync.
94 bool ViewAndLayerTreeAreConsistent(const views::View* view,
95 const ui::Layer* layer) {
96 const views::View* v = FirstView(view);
97 const ui::Layer* l = FirstLayer(layer);
98 while (v && l) {
99 // Find the view with a layer.
100 while (v && !v->layer())
101 v = NextView(v);
102 EXPECT_TRUE(v);
103 if (!v)
104 return false;
106 // Check if the View tree and the Layer tree are in sync.
107 EXPECT_EQ(l, v->layer());
108 if (v->layer() != l)
109 return false;
111 // Check if the visibility states of the View and the Layer are in sync.
112 EXPECT_EQ(l->IsDrawn(), v->IsDrawn());
113 if (v->IsDrawn() != l->IsDrawn()) {
114 for (const views::View* vv = v; vv; vv = vv->parent())
115 LOG(ERROR) << "V: " << vv << " " << vv->visible() << " "
116 << vv->IsDrawn() << " " << vv->layer();
117 for (const ui::Layer* ll = l; ll; ll = ll->parent())
118 LOG(ERROR) << "L: " << ll << " " << ll->IsDrawn();
119 return false;
122 // Check if the size of the View and the Layer are in sync.
123 EXPECT_EQ(l->bounds(), v->bounds());
124 if (v->bounds() != l->bounds())
125 return false;
127 if (v == view || l == layer)
128 return v == view && l == layer;
130 v = NextView(v);
131 l = NextLayer(l);
134 return false;
137 // Constructs a View tree with the specified depth.
138 void ConstructTree(views::View* view, int depth) {
139 if (depth == 0)
140 return;
141 int count = base::RandInt(1, 5);
142 for (int i = 0; i < count; i++) {
143 views::View* v = new views::View;
144 view->AddChildView(v);
145 if (base::RandDouble() > 0.5)
146 v->SetPaintToLayer(true);
147 if (base::RandDouble() < 0.2)
148 v->SetVisible(false);
150 ConstructTree(v, depth - 1);
154 void ScrambleTree(views::View* view) {
155 int count = view->child_count();
156 if (count == 0)
157 return;
158 for (int i = 0; i < count; i++) {
159 ScrambleTree(view->child_at(i));
162 if (count > 1) {
163 int a = base::RandInt(0, count - 1);
164 int b = base::RandInt(0, count - 1);
166 views::View* view_a = view->child_at(a);
167 views::View* view_b = view->child_at(b);
168 view->ReorderChildView(view_a, b);
169 view->ReorderChildView(view_b, a);
172 if (!view->layer() && base::RandDouble() < 0.1)
173 view->SetPaintToLayer(true);
175 if (base::RandDouble() < 0.1)
176 view->SetVisible(!view->visible());
179 } // namespace
181 namespace views {
183 typedef ViewsTestBase ViewTest;
185 // A derived class for testing purpose.
186 class TestView : public View {
187 public:
188 TestView()
189 : View(),
190 delete_on_pressed_(false),
191 native_theme_(NULL),
192 can_process_events_within_subtree_(true) {}
193 virtual ~TestView() {}
195 // Reset all test state
196 void Reset() {
197 did_change_bounds_ = false;
198 last_mouse_event_type_ = 0;
199 location_.SetPoint(0, 0);
200 received_mouse_enter_ = false;
201 received_mouse_exit_ = false;
202 last_clip_.setEmpty();
203 accelerator_count_map_.clear();
204 can_process_events_within_subtree_ = true;
207 // Exposed as public for testing.
208 void DoFocus() {
209 views::View::Focus();
212 void DoBlur() {
213 views::View::Blur();
216 bool focusable() const { return View::focusable(); }
218 void set_can_process_events_within_subtree(bool can_process) {
219 can_process_events_within_subtree_ = can_process;
222 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE {
223 return can_process_events_within_subtree_;
226 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
227 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
228 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
229 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
230 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
231 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
233 virtual void Paint(gfx::Canvas* canvas, const CullSet& cull_set) OVERRIDE;
234 virtual void SchedulePaintInRect(const gfx::Rect& rect) OVERRIDE;
235 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
237 virtual void OnNativeThemeChanged(const ui::NativeTheme* native_theme)
238 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,
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,
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(), 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::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
406 root->OnMousePressed(pressed);
407 EXPECT_EQ(0, v1->child_count());
409 widget->CloseNow();
412 ////////////////////////////////////////////////////////////////////////////////
413 // Painting
414 ////////////////////////////////////////////////////////////////////////////////
416 void TestView::Paint(gfx::Canvas* canvas, const CullSet& cull_set) {
417 canvas->sk_canvas()->getClipBounds(&last_clip_);
420 void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
421 scheduled_paint_rects_.push_back(rect);
422 View::SchedulePaintInRect(rect);
425 void CheckRect(const SkRect& check_rect, const SkRect& target_rect) {
426 EXPECT_EQ(target_rect.fLeft, check_rect.fLeft);
427 EXPECT_EQ(target_rect.fRight, check_rect.fRight);
428 EXPECT_EQ(target_rect.fTop, check_rect.fTop);
429 EXPECT_EQ(target_rect.fBottom, check_rect.fBottom);
432 TEST_F(ViewTest, RemoveNotification) {
433 ViewStorage* vs = ViewStorage::GetInstance();
434 Widget* widget = new Widget;
435 widget->Init(CreateParams(Widget::InitParams::TYPE_POPUP));
436 View* root_view = widget->GetRootView();
438 View* v1 = new View;
439 int s1 = vs->CreateStorageID();
440 vs->StoreView(s1, v1);
441 root_view->AddChildView(v1);
442 View* v11 = new View;
443 int s11 = vs->CreateStorageID();
444 vs->StoreView(s11, v11);
445 v1->AddChildView(v11);
446 View* v111 = new View;
447 int s111 = vs->CreateStorageID();
448 vs->StoreView(s111, v111);
449 v11->AddChildView(v111);
450 View* v112 = new View;
451 int s112 = vs->CreateStorageID();
452 vs->StoreView(s112, v112);
453 v11->AddChildView(v112);
454 View* v113 = new View;
455 int s113 = vs->CreateStorageID();
456 vs->StoreView(s113, v113);
457 v11->AddChildView(v113);
458 View* v1131 = new View;
459 int s1131 = vs->CreateStorageID();
460 vs->StoreView(s1131, v1131);
461 v113->AddChildView(v1131);
462 View* v12 = new View;
463 int s12 = vs->CreateStorageID();
464 vs->StoreView(s12, v12);
465 v1->AddChildView(v12);
467 View* v2 = new View;
468 int s2 = vs->CreateStorageID();
469 vs->StoreView(s2, v2);
470 root_view->AddChildView(v2);
471 View* v21 = new View;
472 int s21 = vs->CreateStorageID();
473 vs->StoreView(s21, v21);
474 v2->AddChildView(v21);
475 View* v211 = new View;
476 int s211 = vs->CreateStorageID();
477 vs->StoreView(s211, v211);
478 v21->AddChildView(v211);
480 size_t stored_views = vs->view_count();
482 // Try removing a leaf view.
483 v21->RemoveChildView(v211);
484 EXPECT_EQ(stored_views - 1, vs->view_count());
485 EXPECT_EQ(NULL, vs->RetrieveView(s211));
486 delete v211; // We won't use this one anymore.
488 // Now try removing a view with a hierarchy of depth 1.
489 v11->RemoveChildView(v113);
490 EXPECT_EQ(stored_views - 3, vs->view_count());
491 EXPECT_EQ(NULL, vs->RetrieveView(s113));
492 EXPECT_EQ(NULL, vs->RetrieveView(s1131));
493 delete v113; // We won't use this one anymore.
495 // Now remove even more.
496 root_view->RemoveChildView(v1);
497 EXPECT_EQ(NULL, vs->RetrieveView(s1));
498 EXPECT_EQ(NULL, vs->RetrieveView(s11));
499 EXPECT_EQ(NULL, vs->RetrieveView(s12));
500 EXPECT_EQ(NULL, vs->RetrieveView(s111));
501 EXPECT_EQ(NULL, vs->RetrieveView(s112));
503 // Put v1 back for more tests.
504 root_view->AddChildView(v1);
505 vs->StoreView(s1, v1);
507 // Synchronously closing the window deletes the view hierarchy, which should
508 // remove all its views from ViewStorage.
509 widget->CloseNow();
510 EXPECT_EQ(stored_views - 10, vs->view_count());
511 EXPECT_EQ(NULL, vs->RetrieveView(s1));
512 EXPECT_EQ(NULL, vs->RetrieveView(s12));
513 EXPECT_EQ(NULL, vs->RetrieveView(s11));
514 EXPECT_EQ(NULL, vs->RetrieveView(s12));
515 EXPECT_EQ(NULL, vs->RetrieveView(s21));
516 EXPECT_EQ(NULL, vs->RetrieveView(s111));
517 EXPECT_EQ(NULL, vs->RetrieveView(s112));
520 namespace {
522 void RotateCounterclockwise(gfx::Transform* transform) {
523 transform->matrix().set3x3(0, -1, 0,
524 1, 0, 0,
525 0, 0, 1);
528 void RotateClockwise(gfx::Transform* transform) {
529 transform->matrix().set3x3( 0, 1, 0,
530 -1, 0, 0,
531 0, 0, 1);
534 } // namespace
536 // Tests the correctness of the rect-based targeting algorithm implemented in
537 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
538 // of rect-based targeting.
539 TEST_F(ViewTest, GetEventHandlerForRect) {
540 Widget* widget = new Widget;
541 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
542 widget->Init(params);
543 View* root_view = widget->GetRootView();
544 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
546 // Have this hierarchy of views (the coordinates here are all in
547 // the root view's coordinate space):
548 // v1 (0, 0, 100, 100)
549 // v2 (150, 0, 250, 100)
550 // v3 (0, 200, 150, 100)
551 // v31 (10, 210, 80, 80)
552 // v32 (110, 210, 30, 80)
553 // v4 (300, 200, 100, 100)
554 // v41 (310, 210, 80, 80)
555 // v411 (370, 275, 10, 5)
556 // v5 (450, 197, 30, 36)
557 // v51 (450, 200, 30, 30)
559 // The coordinates used for SetBounds are in parent coordinates.
561 TestView* v1 = new TestView;
562 v1->SetBounds(0, 0, 100, 100);
563 root_view->AddChildView(v1);
565 TestView* v2 = new TestView;
566 v2->SetBounds(150, 0, 250, 100);
567 root_view->AddChildView(v2);
569 TestView* v3 = new TestView;
570 v3->SetBounds(0, 200, 150, 100);
571 root_view->AddChildView(v3);
573 TestView* v4 = new TestView;
574 v4->SetBounds(300, 200, 100, 100);
575 root_view->AddChildView(v4);
577 TestView* v31 = new TestView;
578 v31->SetBounds(10, 10, 80, 80);
579 v3->AddChildView(v31);
581 TestView* v32 = new TestView;
582 v32->SetBounds(110, 10, 30, 80);
583 v3->AddChildView(v32);
585 TestView* v41 = new TestView;
586 v41->SetBounds(10, 10, 80, 80);
587 v4->AddChildView(v41);
589 TestView* v411 = new TestView;
590 v411->SetBounds(60, 65, 10, 5);
591 v41->AddChildView(v411);
593 TestView* v5 = new TestView;
594 v5->SetBounds(450, 197, 30, 36);
595 root_view->AddChildView(v5);
597 TestView* v51 = new TestView;
598 v51->SetBounds(0, 3, 30, 30);
599 v5->AddChildView(v51);
601 // |touch_rect| does not intersect any descendant view of |root_view|.
602 gfx::Rect touch_rect(105, 105, 30, 45);
603 View* result_view = root_view->GetEventHandlerForRect(touch_rect);
604 EXPECT_EQ(root_view, result_view);
605 result_view = NULL;
607 // Covers |v1| by at least 60%.
608 touch_rect.SetRect(15, 15, 100, 100);
609 result_view = root_view->GetEventHandlerForRect(touch_rect);
610 EXPECT_EQ(v1, result_view);
611 result_view = NULL;
613 // Intersects |v1| but does not cover it by at least 60%. The center
614 // of |touch_rect| is within |v1|.
615 touch_rect.SetRect(50, 50, 5, 10);
616 result_view = root_view->GetEventHandlerForRect(touch_rect);
617 EXPECT_EQ(v1, result_view);
618 result_view = NULL;
620 // Intersects |v1| but does not cover it by at least 60%. The center
621 // of |touch_rect| is not within |v1|.
622 touch_rect.SetRect(95, 96, 21, 22);
623 result_view = root_view->GetEventHandlerForRect(touch_rect);
624 EXPECT_EQ(root_view, result_view);
625 result_view = NULL;
627 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
628 touch_rect.SetRect(95, 10, 300, 120);
629 result_view = root_view->GetEventHandlerForRect(touch_rect);
630 EXPECT_EQ(v2, result_view);
631 result_view = NULL;
633 // Covers both |v1| and |v2| by at least 60%, but the center point
634 // of |touch_rect| is closer to the center point of |v2|.
635 touch_rect.SetRect(20, 20, 400, 100);
636 result_view = root_view->GetEventHandlerForRect(touch_rect);
637 EXPECT_EQ(v2, result_view);
638 result_view = NULL;
640 // Covers both |v1| and |v2| by at least 60%, but the center point
641 // of |touch_rect| is closer to the center point of |v1|.
642 touch_rect.SetRect(-700, -15, 1050, 110);
643 result_view = root_view->GetEventHandlerForRect(touch_rect);
644 EXPECT_EQ(v1, result_view);
645 result_view = NULL;
647 // A mouse click within |v1| will target |v1|.
648 touch_rect.SetRect(15, 15, 1, 1);
649 result_view = root_view->GetEventHandlerForRect(touch_rect);
650 EXPECT_EQ(v1, result_view);
651 result_view = NULL;
653 // Intersects |v3| and |v31| by at least 60% and the center point
654 // of |touch_rect| is closer to the center point of |v31|.
655 touch_rect.SetRect(0, 200, 110, 100);
656 result_view = root_view->GetEventHandlerForRect(touch_rect);
657 EXPECT_EQ(v31, result_view);
658 result_view = NULL;
660 // Intersects |v3| and |v31|, but neither by at least 60%. The
661 // center point of |touch_rect| lies within |v31|.
662 touch_rect.SetRect(80, 280, 15, 15);
663 result_view = root_view->GetEventHandlerForRect(touch_rect);
664 EXPECT_EQ(v31, result_view);
665 result_view = NULL;
667 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
668 // center point of |touch_rect| is closest to the center point
669 // of |v32|.
670 touch_rect.SetRect(0, 200, 200, 100);
671 result_view = root_view->GetEventHandlerForRect(touch_rect);
672 EXPECT_EQ(v32, result_view);
673 result_view = NULL;
675 // Intersects all of |v3|, |v31|, and |v32|, but only covers
676 // |v31| and |v32| by at least 60%. The center point of
677 // |touch_rect| is closest to the center point of |v32|.
678 touch_rect.SetRect(30, 225, 180, 115);
679 result_view = root_view->GetEventHandlerForRect(touch_rect);
680 EXPECT_EQ(v32, result_view);
681 result_view = NULL;
683 // A mouse click at the corner of |v3| will target |v3|.
684 touch_rect.SetRect(0, 200, 1, 1);
685 result_view = root_view->GetEventHandlerForRect(touch_rect);
686 EXPECT_EQ(v3, result_view);
687 result_view = NULL;
689 // A mouse click within |v32| will target |v32|.
690 touch_rect.SetRect(112, 211, 1, 1);
691 result_view = root_view->GetEventHandlerForRect(touch_rect);
692 EXPECT_EQ(v32, result_view);
693 result_view = NULL;
695 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
696 // The center point of |touch_rect| is equally close to
697 // the center points of |v4| and |v41|.
698 touch_rect.SetRect(310, 210, 80, 80);
699 result_view = root_view->GetEventHandlerForRect(touch_rect);
700 EXPECT_EQ(v41, result_view);
701 result_view = NULL;
703 // Intersects all of |v4|, |v41|, and |v411| but only covers
704 // |v411| by at least 60%.
705 touch_rect.SetRect(370, 275, 7, 5);
706 result_view = root_view->GetEventHandlerForRect(touch_rect);
707 EXPECT_EQ(v411, result_view);
708 result_view = NULL;
710 // Intersects |v4| and |v41| but covers neither by at least 60%.
711 // The center point of |touch_rect| is equally close to the center
712 // points of |v4| and |v41|.
713 touch_rect.SetRect(345, 245, 7, 7);
714 result_view = root_view->GetEventHandlerForRect(touch_rect);
715 EXPECT_EQ(v41, result_view);
716 result_view = NULL;
718 // Intersects all of |v4|, |v41|, and |v411| and covers none of
719 // them by at least 60%. The center point of |touch_rect| lies
720 // within |v411|.
721 touch_rect.SetRect(368, 272, 4, 6);
722 result_view = root_view->GetEventHandlerForRect(touch_rect);
723 EXPECT_EQ(v411, result_view);
724 result_view = NULL;
726 // Intersects all of |v4|, |v41|, and |v411| and covers none of
727 // them by at least 60%. The center point of |touch_rect| lies
728 // within |v41|.
729 touch_rect.SetRect(365, 270, 7, 7);
730 result_view = root_view->GetEventHandlerForRect(touch_rect);
731 EXPECT_EQ(v41, result_view);
732 result_view = NULL;
734 // Intersects all of |v4|, |v41|, and |v411| and covers none of
735 // them by at least 60%. The center point of |touch_rect| lies
736 // within |v4|.
737 touch_rect.SetRect(205, 275, 200, 2);
738 result_view = root_view->GetEventHandlerForRect(touch_rect);
739 EXPECT_EQ(v4, result_view);
740 result_view = NULL;
742 // Intersects all of |v4|, |v41|, and |v411| but only covers
743 // |v41| by at least 60%.
744 touch_rect.SetRect(310, 210, 61, 66);
745 result_view = root_view->GetEventHandlerForRect(touch_rect);
746 EXPECT_EQ(v41, result_view);
747 result_view = NULL;
749 // A mouse click within |v411| will target |v411|.
750 touch_rect.SetRect(372, 275, 1, 1);
751 result_view = root_view->GetEventHandlerForRect(touch_rect);
752 EXPECT_EQ(v411, result_view);
753 result_view = NULL;
755 // A mouse click within |v41| will target |v41|.
756 touch_rect.SetRect(350, 215, 1, 1);
757 result_view = root_view->GetEventHandlerForRect(touch_rect);
758 EXPECT_EQ(v41, result_view);
759 result_view = NULL;
761 // Covers |v3|, |v4|, and all of their descendants by at
762 // least 60%. The center point of |touch_rect| is closest
763 // to the center point of |v32|.
764 touch_rect.SetRect(0, 200, 400, 100);
765 result_view = root_view->GetEventHandlerForRect(touch_rect);
766 EXPECT_EQ(v32, result_view);
767 result_view = NULL;
769 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
770 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
771 // The center point of |touch_rect| is closest to the center
772 // point of |root_view|.
773 touch_rect.SetRect(110, 15, 375, 450);
774 result_view = root_view->GetEventHandlerForRect(touch_rect);
775 EXPECT_EQ(root_view, result_view);
776 result_view = NULL;
778 // Covers all views (except |v5| and |v51|) by at least 60%. The
779 // center point of |touch_rect| is equally close to the center
780 // points of |v2| and |v32|. One is not a descendant of the other,
781 // so in this case the view selected is arbitrary (i.e.,
782 // it depends only on the ordering of nodes in the views
783 // hierarchy).
784 touch_rect.SetRect(0, 0, 400, 300);
785 result_view = root_view->GetEventHandlerForRect(touch_rect);
786 EXPECT_EQ(v32, result_view);
787 result_view = NULL;
789 // Covers |v5| and |v51| by at least 60%, and the center point of
790 // the touch is located within both views. Since both views share
791 // the same center point, the child view should be selected.
792 touch_rect.SetRect(440, 190, 40, 40);
793 result_view = root_view->GetEventHandlerForRect(touch_rect);
794 EXPECT_EQ(v51, result_view);
795 result_view = NULL;
797 // Covers |v5| and |v51| by at least 60%, but the center point of
798 // the touch is not located within either view. Since both views
799 // share the same center point, the child view should be selected.
800 touch_rect.SetRect(455, 187, 60, 60);
801 result_view = root_view->GetEventHandlerForRect(touch_rect);
802 EXPECT_EQ(v51, result_view);
803 result_view = NULL;
805 // Covers neither |v5| nor |v51| by at least 60%, but the center
806 // of the touch is located within |v51|.
807 touch_rect.SetRect(450, 197, 10, 10);
808 result_view = root_view->GetEventHandlerForRect(touch_rect);
809 EXPECT_EQ(v51, result_view);
810 result_view = NULL;
812 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
813 // The center point is located outside of both views.
814 touch_rect.SetRect(433, 180, 24, 24);
815 result_view = root_view->GetEventHandlerForRect(touch_rect);
816 EXPECT_EQ(root_view, result_view);
817 result_view = NULL;
819 // Only intersects |v5| but does not cover it by at least 60%. The
820 // center point of the touch region is located within |v5|.
821 touch_rect.SetRect(449, 196, 3, 3);
822 result_view = root_view->GetEventHandlerForRect(touch_rect);
823 EXPECT_EQ(v5, result_view);
824 result_view = NULL;
826 // A mouse click within |v5| (but not |v51|) should target |v5|.
827 touch_rect.SetRect(462, 199, 1, 1);
828 result_view = root_view->GetEventHandlerForRect(touch_rect);
829 EXPECT_EQ(v5, result_view);
830 result_view = NULL;
832 // A mouse click |v5| and |v51| should target the child view.
833 touch_rect.SetRect(452, 226, 1, 1);
834 result_view = root_view->GetEventHandlerForRect(touch_rect);
835 EXPECT_EQ(v51, result_view);
836 result_view = NULL;
838 // A mouse click on the center of |v5| and |v51| should target
839 // the child view.
840 touch_rect.SetRect(465, 215, 1, 1);
841 result_view = root_view->GetEventHandlerForRect(touch_rect);
842 EXPECT_EQ(v51, result_view);
843 result_view = NULL;
845 widget->CloseNow();
848 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
849 // as expected when different views in the view hierarchy return false
850 // when CanProcessEventsWithinSubtree() is called.
851 TEST_F(ViewTest, CanProcessEventsWithinSubtree) {
852 Widget* widget = new Widget;
853 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
854 widget->Init(params);
855 View* root_view = widget->GetRootView();
856 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
858 // Have this hierarchy of views (the coords here are in the coordinate
859 // space of the root view):
860 // v (0, 0, 100, 100)
861 // - v_child (0, 0, 20, 30)
862 // - v_grandchild (5, 5, 5, 15)
864 TestView* v = new TestView;
865 v->SetBounds(0, 0, 100, 100);
866 root_view->AddChildView(v);
867 v->set_notify_enter_exit_on_child(true);
869 TestView* v_child = new TestView;
870 v_child->SetBounds(0, 0, 20, 30);
871 v->AddChildView(v_child);
873 TestView* v_grandchild = new TestView;
874 v_grandchild->SetBounds(5, 5, 5, 15);
875 v_child->AddChildView(v_grandchild);
877 v->Reset();
878 v_child->Reset();
879 v_grandchild->Reset();
881 // Define rects and points within the views in the hierarchy.
882 gfx::Rect rect_in_v_grandchild(7, 7, 3, 3);
883 gfx::Point point_in_v_grandchild(rect_in_v_grandchild.origin());
884 gfx::Rect rect_in_v_child(12, 3, 5, 5);
885 gfx::Point point_in_v_child(rect_in_v_child.origin());
886 gfx::Rect rect_in_v(50, 50, 25, 30);
887 gfx::Point point_in_v(rect_in_v.origin());
889 // When all three views return true when CanProcessEventsWithinSubtree()
890 // is called, targeting should behave as expected.
892 View* result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
893 EXPECT_EQ(v_grandchild, result_view);
894 result_view = NULL;
895 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
896 EXPECT_EQ(v_grandchild, result_view);
897 result_view = NULL;
899 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
900 EXPECT_EQ(v_child, result_view);
901 result_view = NULL;
902 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
903 EXPECT_EQ(v_child, result_view);
904 result_view = NULL;
906 result_view = root_view->GetEventHandlerForRect(rect_in_v);
907 EXPECT_EQ(v, result_view);
908 result_view = NULL;
909 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
910 EXPECT_EQ(v, result_view);
911 result_view = NULL;
913 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
914 // is called, then |v_grandchild| cannot be returned as a target.
916 v_grandchild->set_can_process_events_within_subtree(false);
918 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
919 EXPECT_EQ(v_child, result_view);
920 result_view = NULL;
921 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
922 EXPECT_EQ(v_child, result_view);
923 result_view = NULL;
925 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
926 EXPECT_EQ(v_child, result_view);
927 result_view = NULL;
928 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
929 EXPECT_EQ(v_child, result_view);
930 result_view = NULL;
932 result_view = root_view->GetEventHandlerForRect(rect_in_v);
933 EXPECT_EQ(v, result_view);
934 result_view = NULL;
935 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
936 EXPECT_EQ(v, result_view);
938 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
939 // is called, then NULL should be returned as a target if we call
940 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
941 // views tree. Note that the location must be in the coordinate space
942 // of the root view (|v_grandchild| in this case), so use (1, 1).
944 result_view = v_grandchild;
945 result_view = v_grandchild->GetTooltipHandlerForPoint(gfx::Point(1, 1));
946 EXPECT_EQ(NULL, result_view);
947 result_view = NULL;
949 // When |v_child| returns false when CanProcessEventsWithinSubtree()
950 // is called, then neither |v_child| nor |v_grandchild| can be returned
951 // as a target (|v| should be returned as the target for each case).
953 v_grandchild->Reset();
954 v_child->set_can_process_events_within_subtree(false);
956 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
957 EXPECT_EQ(v, result_view);
958 result_view = NULL;
959 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
960 EXPECT_EQ(v, result_view);
961 result_view = NULL;
963 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
964 EXPECT_EQ(v, result_view);
965 result_view = NULL;
966 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
967 EXPECT_EQ(v, result_view);
968 result_view = NULL;
970 result_view = root_view->GetEventHandlerForRect(rect_in_v);
971 EXPECT_EQ(v, result_view);
972 result_view = NULL;
973 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
974 EXPECT_EQ(v, result_view);
975 result_view = NULL;
977 // When |v| returns false when CanProcessEventsWithinSubtree()
978 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
979 // as a target (|root_view| should be returned as the target for each case).
981 v_child->Reset();
982 v->set_can_process_events_within_subtree(false);
984 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
985 EXPECT_EQ(root_view, result_view);
986 result_view = NULL;
987 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
988 EXPECT_EQ(root_view, result_view);
989 result_view = NULL;
991 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
992 EXPECT_EQ(root_view, result_view);
993 result_view = NULL;
994 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
995 EXPECT_EQ(root_view, result_view);
996 result_view = NULL;
998 result_view = root_view->GetEventHandlerForRect(rect_in_v);
999 EXPECT_EQ(root_view, result_view);
1000 result_view = NULL;
1001 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1002 EXPECT_EQ(root_view, result_view);
1005 TEST_F(ViewTest, NotifyEnterExitOnChild) {
1006 Widget* widget = new Widget;
1007 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1008 widget->Init(params);
1009 View* root_view = widget->GetRootView();
1010 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1012 // Have this hierarchy of views (the coords here are in root coord):
1013 // v1 (0, 0, 100, 100)
1014 // - v11 (0, 0, 20, 30)
1015 // - v111 (5, 5, 5, 15)
1016 // - v12 (50, 10, 30, 90)
1017 // - v121 (60, 20, 10, 10)
1018 // v2 (105, 0, 100, 100)
1019 // - v21 (120, 10, 50, 20)
1021 TestView* v1 = new TestView;
1022 v1->SetBounds(0, 0, 100, 100);
1023 root_view->AddChildView(v1);
1024 v1->set_notify_enter_exit_on_child(true);
1026 TestView* v11 = new TestView;
1027 v11->SetBounds(0, 0, 20, 30);
1028 v1->AddChildView(v11);
1030 TestView* v111 = new TestView;
1031 v111->SetBounds(5, 5, 5, 15);
1032 v11->AddChildView(v111);
1034 TestView* v12 = new TestView;
1035 v12->SetBounds(50, 10, 30, 90);
1036 v1->AddChildView(v12);
1038 TestView* v121 = new TestView;
1039 v121->SetBounds(10, 10, 10, 10);
1040 v12->AddChildView(v121);
1042 TestView* v2 = new TestView;
1043 v2->SetBounds(105, 0, 100, 100);
1044 root_view->AddChildView(v2);
1046 TestView* v21 = new TestView;
1047 v21->SetBounds(15, 10, 50, 20);
1048 v2->AddChildView(v21);
1050 v1->Reset();
1051 v11->Reset();
1052 v111->Reset();
1053 v12->Reset();
1054 v121->Reset();
1055 v2->Reset();
1056 v21->Reset();
1058 // Move the mouse in v111.
1059 gfx::Point p1(6, 6);
1060 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, p1, p1, 0, 0);
1061 root_view->OnMouseMoved(move1);
1062 EXPECT_TRUE(v111->received_mouse_enter_);
1063 EXPECT_FALSE(v11->last_mouse_event_type_);
1064 EXPECT_TRUE(v1->received_mouse_enter_);
1066 v111->Reset();
1067 v1->Reset();
1069 // Now, move into v121.
1070 gfx::Point p2(65, 21);
1071 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, p2, p2, 0, 0);
1072 root_view->OnMouseMoved(move2);
1073 EXPECT_TRUE(v111->received_mouse_exit_);
1074 EXPECT_TRUE(v121->received_mouse_enter_);
1075 EXPECT_FALSE(v1->last_mouse_event_type_);
1077 v111->Reset();
1078 v121->Reset();
1080 // Now, move into v11.
1081 gfx::Point p3(1, 1);
1082 ui::MouseEvent move3(ui::ET_MOUSE_MOVED, p3, p3, 0, 0);
1083 root_view->OnMouseMoved(move3);
1084 EXPECT_TRUE(v121->received_mouse_exit_);
1085 EXPECT_TRUE(v11->received_mouse_enter_);
1086 EXPECT_FALSE(v1->last_mouse_event_type_);
1088 v121->Reset();
1089 v11->Reset();
1091 // Move to v21.
1092 gfx::Point p4(121, 15);
1093 ui::MouseEvent move4(ui::ET_MOUSE_MOVED, p4, p4, 0, 0);
1094 root_view->OnMouseMoved(move4);
1095 EXPECT_TRUE(v21->received_mouse_enter_);
1096 EXPECT_FALSE(v2->last_mouse_event_type_);
1097 EXPECT_TRUE(v11->received_mouse_exit_);
1098 EXPECT_TRUE(v1->received_mouse_exit_);
1100 v21->Reset();
1101 v11->Reset();
1102 v1->Reset();
1104 // Move to v1.
1105 gfx::Point p5(21, 0);
1106 ui::MouseEvent move5(ui::ET_MOUSE_MOVED, p5, p5, 0, 0);
1107 root_view->OnMouseMoved(move5);
1108 EXPECT_TRUE(v21->received_mouse_exit_);
1109 EXPECT_TRUE(v1->received_mouse_enter_);
1111 v21->Reset();
1112 v1->Reset();
1114 // Now, move into v11.
1115 gfx::Point p6(15, 15);
1116 ui::MouseEvent mouse6(ui::ET_MOUSE_MOVED, p6, p6, 0, 0);
1117 root_view->OnMouseMoved(mouse6);
1118 EXPECT_TRUE(v11->received_mouse_enter_);
1119 EXPECT_FALSE(v1->last_mouse_event_type_);
1121 v11->Reset();
1122 v1->Reset();
1124 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1125 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1126 // when the mouse leaves v11.
1127 gfx::Point p7(21, 0);
1128 ui::MouseEvent mouse7(ui::ET_MOUSE_MOVED, p7, p7, 0, 0);
1129 root_view->OnMouseMoved(mouse7);
1130 EXPECT_TRUE(v11->received_mouse_exit_);
1131 EXPECT_FALSE(v1->received_mouse_enter_);
1133 widget->CloseNow();
1136 TEST_F(ViewTest, Textfield) {
1137 const base::string16 kText = ASCIIToUTF16(
1138 "Reality is that which, when you stop believing it, doesn't go away.");
1139 const base::string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!");
1141 Widget* widget = new Widget;
1142 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1143 params.bounds = gfx::Rect(0, 0, 100, 100);
1144 widget->Init(params);
1145 View* root_view = widget->GetRootView();
1147 Textfield* textfield = new Textfield();
1148 root_view->AddChildView(textfield);
1150 // Test setting, appending text.
1151 textfield->SetText(kText);
1152 EXPECT_EQ(kText, textfield->text());
1153 textfield->AppendText(kExtraText);
1154 EXPECT_EQ(kText + kExtraText, textfield->text());
1155 textfield->SetText(base::string16());
1156 EXPECT_TRUE(textfield->text().empty());
1158 // Test selection related methods.
1159 textfield->SetText(kText);
1160 EXPECT_TRUE(textfield->GetSelectedText().empty());
1161 textfield->SelectAll(false);
1162 EXPECT_EQ(kText, textfield->text());
1163 textfield->ClearSelection();
1164 EXPECT_TRUE(textfield->GetSelectedText().empty());
1166 widget->CloseNow();
1169 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1170 TEST_F(ViewTest, TextfieldCutCopyPaste) {
1171 const base::string16 kNormalText = ASCIIToUTF16("Normal");
1172 const base::string16 kReadOnlyText = ASCIIToUTF16("Read only");
1173 const base::string16 kPasswordText =
1174 ASCIIToUTF16("Password! ** Secret stuff **");
1176 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1178 Widget* widget = new Widget;
1179 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1180 params.bounds = gfx::Rect(0, 0, 100, 100);
1181 widget->Init(params);
1182 View* root_view = widget->GetRootView();
1184 Textfield* normal = new Textfield();
1185 Textfield* read_only = new Textfield();
1186 read_only->SetReadOnly(true);
1187 Textfield* password = new Textfield();
1188 password->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
1190 root_view->AddChildView(normal);
1191 root_view->AddChildView(read_only);
1192 root_view->AddChildView(password);
1194 normal->SetText(kNormalText);
1195 read_only->SetText(kReadOnlyText);
1196 password->SetText(kPasswordText);
1199 // Test cut.
1202 normal->SelectAll(false);
1203 normal->ExecuteCommand(IDS_APP_CUT);
1204 base::string16 result;
1205 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1206 EXPECT_EQ(kNormalText, result);
1207 normal->SetText(kNormalText); // Let's revert to the original content.
1209 read_only->SelectAll(false);
1210 read_only->ExecuteCommand(IDS_APP_CUT);
1211 result.clear();
1212 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1213 // Cut should have failed, so the clipboard content should not have changed.
1214 EXPECT_EQ(kNormalText, result);
1216 password->SelectAll(false);
1217 password->ExecuteCommand(IDS_APP_CUT);
1218 result.clear();
1219 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1220 // Cut should have failed, so the clipboard content should not have changed.
1221 EXPECT_EQ(kNormalText, result);
1224 // Test copy.
1227 // Start with |read_only| to observe a change in clipboard text.
1228 read_only->SelectAll(false);
1229 read_only->ExecuteCommand(IDS_APP_COPY);
1230 result.clear();
1231 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1232 EXPECT_EQ(kReadOnlyText, result);
1234 normal->SelectAll(false);
1235 normal->ExecuteCommand(IDS_APP_COPY);
1236 result.clear();
1237 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1238 EXPECT_EQ(kNormalText, result);
1240 password->SelectAll(false);
1241 password->ExecuteCommand(IDS_APP_COPY);
1242 result.clear();
1243 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1244 // Text cannot be copied from an obscured field; the clipboard won't change.
1245 EXPECT_EQ(kNormalText, result);
1248 // Test paste.
1251 // Attempting to paste kNormalText in a read-only text-field should fail.
1252 read_only->SelectAll(false);
1253 read_only->ExecuteCommand(IDS_APP_PASTE);
1254 EXPECT_EQ(kReadOnlyText, read_only->text());
1256 password->SelectAll(false);
1257 password->ExecuteCommand(IDS_APP_PASTE);
1258 EXPECT_EQ(kNormalText, password->text());
1260 // Copy from |read_only| to observe a change in the normal textfield text.
1261 read_only->SelectAll(false);
1262 read_only->ExecuteCommand(IDS_APP_COPY);
1263 normal->SelectAll(false);
1264 normal->ExecuteCommand(IDS_APP_PASTE);
1265 EXPECT_EQ(kReadOnlyText, normal->text());
1266 widget->CloseNow();
1269 ////////////////////////////////////////////////////////////////////////////////
1270 // Accelerators
1271 ////////////////////////////////////////////////////////////////////////////////
1272 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) {
1273 accelerator_count_map_[accelerator]++;
1274 return true;
1277 // TODO: these tests were initially commented out when getting aura to
1278 // run. Figure out if still valuable and either nuke or fix.
1279 #if 0
1280 TEST_F(ViewTest, ActivateAccelerator) {
1281 // Register a keyboard accelerator before the view is added to a window.
1282 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1283 TestView* view = new TestView();
1284 view->Reset();
1285 view->AddAccelerator(return_accelerator);
1286 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1288 // Create a window and add the view as its child.
1289 scoped_ptr<Widget> widget(new Widget);
1290 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1291 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1292 params.bounds = gfx::Rect(0, 0, 100, 100);
1293 widget->Init(params);
1294 View* root = widget->GetRootView();
1295 root->AddChildView(view);
1296 widget->Show();
1298 // Get the focus manager.
1299 FocusManager* focus_manager = widget->GetFocusManager();
1300 ASSERT_TRUE(focus_manager);
1302 // Hit the return key and see if it takes effect.
1303 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1304 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1306 // Hit the escape key. Nothing should happen.
1307 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE);
1308 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1309 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1310 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0);
1312 // Now register the escape key and hit it again.
1313 view->AddAccelerator(escape_accelerator);
1314 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1315 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1316 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1318 // Remove the return key accelerator.
1319 view->RemoveAccelerator(return_accelerator);
1320 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1321 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1322 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1324 // Add it again. Hit the return key and the escape key.
1325 view->AddAccelerator(return_accelerator);
1326 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1327 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1328 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1329 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1330 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1331 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1333 // Remove all the accelerators.
1334 view->ResetAccelerators();
1335 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1336 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1337 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1338 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1339 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1340 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1342 widget->CloseNow();
1345 TEST_F(ViewTest, HiddenViewWithAccelerator) {
1346 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1347 TestView* view = new TestView();
1348 view->Reset();
1349 view->AddAccelerator(return_accelerator);
1350 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1352 scoped_ptr<Widget> widget(new Widget);
1353 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1354 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1355 params.bounds = gfx::Rect(0, 0, 100, 100);
1356 widget->Init(params);
1357 View* root = widget->GetRootView();
1358 root->AddChildView(view);
1359 widget->Show();
1361 FocusManager* focus_manager = widget->GetFocusManager();
1362 ASSERT_TRUE(focus_manager);
1364 view->SetVisible(false);
1365 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1367 view->SetVisible(true);
1368 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1370 widget->CloseNow();
1373 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) {
1374 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1375 TestView* view = new TestView();
1376 view->Reset();
1377 view->AddAccelerator(return_accelerator);
1378 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1380 scoped_ptr<Widget> widget(new Widget);
1381 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1382 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1383 params.bounds = gfx::Rect(0, 0, 100, 100);
1384 widget->Init(params);
1385 View* root = widget->GetRootView();
1386 root->AddChildView(view);
1388 FocusManager* focus_manager = widget->GetFocusManager();
1389 ASSERT_TRUE(focus_manager);
1391 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1392 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
1394 widget->Show();
1395 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1396 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1398 widget->Hide();
1399 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1400 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1402 widget->CloseNow();
1405 ////////////////////////////////////////////////////////////////////////////////
1406 // Mouse-wheel message rerouting
1407 ////////////////////////////////////////////////////////////////////////////////
1408 class ScrollableTestView : public View {
1409 public:
1410 ScrollableTestView() { }
1412 virtual gfx::Size GetPreferredSize() {
1413 return gfx::Size(100, 10000);
1416 virtual void Layout() {
1417 SizeToPreferredSize();
1421 class TestViewWithControls : public View {
1422 public:
1423 TestViewWithControls() {
1424 text_field_ = new Textfield();
1425 AddChildView(text_field_);
1428 Textfield* text_field_;
1431 class SimpleWidgetDelegate : public WidgetDelegate {
1432 public:
1433 explicit SimpleWidgetDelegate(View* contents) : contents_(contents) { }
1435 virtual void DeleteDelegate() { delete this; }
1437 virtual View* GetContentsView() { return contents_; }
1439 virtual Widget* GetWidget() { return contents_->GetWidget(); }
1440 virtual const Widget* GetWidget() const { return contents_->GetWidget(); }
1442 private:
1443 View* contents_;
1446 // Tests that the mouse-wheel messages are correctly rerouted to the window
1447 // under the mouse.
1448 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1449 // bot.
1450 // Note that this fails for a variety of reasons:
1451 // - focused view is apparently reset across window activations and never
1452 // properly restored
1453 // - this test depends on you not having any other window visible open under the
1454 // area that it opens the test windows. --beng
1455 TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) {
1456 TestViewWithControls* view_with_controls = new TestViewWithControls();
1457 Widget* window1 = Widget::CreateWindowWithBounds(
1458 new SimpleWidgetDelegate(view_with_controls),
1459 gfx::Rect(0, 0, 100, 100));
1460 window1->Show();
1461 ScrollView* scroll_view = new ScrollView();
1462 scroll_view->SetContents(new ScrollableTestView());
1463 Widget* window2 = Widget::CreateWindowWithBounds(
1464 new SimpleWidgetDelegate(scroll_view),
1465 gfx::Rect(200, 200, 100, 100));
1466 window2->Show();
1467 EXPECT_EQ(0, scroll_view->GetVisibleRect().y());
1469 // Make the window1 active, as this is what it would be in real-world.
1470 window1->Activate();
1472 // Let's send a mouse-wheel message to the different controls and check that
1473 // it is rerouted to the window under the mouse (effectively scrolling the
1474 // scroll-view).
1476 // First to the Window's HWND.
1477 ::SendMessage(view_with_controls->GetWidget()->GetNativeView(),
1478 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1479 EXPECT_EQ(20, scroll_view->GetVisibleRect().y());
1481 window1->CloseNow();
1482 window2->CloseNow();
1484 #endif // 0
1486 ////////////////////////////////////////////////////////////////////////////////
1487 // Native view hierachy
1488 ////////////////////////////////////////////////////////////////////////////////
1489 class ToplevelWidgetObserverView : public View {
1490 public:
1491 ToplevelWidgetObserverView() : toplevel_(NULL) {
1493 virtual ~ToplevelWidgetObserverView() {
1496 // View overrides:
1497 virtual void ViewHierarchyChanged(
1498 const ViewHierarchyChangedDetails& details) OVERRIDE {
1499 if (details.is_add) {
1500 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1501 } else {
1502 toplevel_ = NULL;
1505 virtual void NativeViewHierarchyChanged() OVERRIDE {
1506 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1509 Widget* toplevel() { return toplevel_; }
1511 private:
1512 Widget* toplevel_;
1514 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView);
1517 // Test that a view can track the current top level widget by overriding
1518 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1519 TEST_F(ViewTest, NativeViewHierarchyChanged) {
1520 scoped_ptr<Widget> toplevel1(new Widget);
1521 Widget::InitParams toplevel1_params =
1522 CreateParams(Widget::InitParams::TYPE_POPUP);
1523 toplevel1_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1524 toplevel1->Init(toplevel1_params);
1526 scoped_ptr<Widget> toplevel2(new Widget);
1527 Widget::InitParams toplevel2_params =
1528 CreateParams(Widget::InitParams::TYPE_POPUP);
1529 toplevel2_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1530 toplevel2->Init(toplevel2_params);
1532 Widget* child = new Widget;
1533 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
1534 child_params.parent = toplevel1->GetNativeView();
1535 child->Init(child_params);
1537 ToplevelWidgetObserverView* observer_view =
1538 new ToplevelWidgetObserverView();
1539 EXPECT_EQ(NULL, observer_view->toplevel());
1541 child->SetContentsView(observer_view);
1542 EXPECT_EQ(toplevel1, observer_view->toplevel());
1544 Widget::ReparentNativeView(child->GetNativeView(),
1545 toplevel2->GetNativeView());
1546 EXPECT_EQ(toplevel2, observer_view->toplevel());
1548 observer_view->parent()->RemoveChildView(observer_view);
1549 EXPECT_EQ(NULL, observer_view->toplevel());
1551 // Make |observer_view| |child|'s contents view again so that it gets deleted
1552 // with the widget.
1553 child->SetContentsView(observer_view);
1556 ////////////////////////////////////////////////////////////////////////////////
1557 // Transformations
1558 ////////////////////////////////////////////////////////////////////////////////
1560 class TransformPaintView : public TestView {
1561 public:
1562 TransformPaintView() {}
1563 virtual ~TransformPaintView() {}
1565 void ClearScheduledPaintRect() {
1566 scheduled_paint_rect_ = gfx::Rect();
1569 gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
1571 // Overridden from View:
1572 virtual void SchedulePaintInRect(const gfx::Rect& rect) OVERRIDE {
1573 gfx::Rect xrect = ConvertRectToParent(rect);
1574 scheduled_paint_rect_.Union(xrect);
1577 private:
1578 gfx::Rect scheduled_paint_rect_;
1580 DISALLOW_COPY_AND_ASSIGN(TransformPaintView);
1583 TEST_F(ViewTest, TransformPaint) {
1584 TransformPaintView* v1 = new TransformPaintView();
1585 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1587 TestView* v2 = new TestView();
1588 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1590 Widget* widget = new Widget;
1591 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1592 params.bounds = gfx::Rect(50, 50, 650, 650);
1593 widget->Init(params);
1594 widget->Show();
1595 View* root = widget->GetRootView();
1597 root->AddChildView(v1);
1598 v1->AddChildView(v2);
1600 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1601 v1->ClearScheduledPaintRect();
1602 v2->SchedulePaint();
1604 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
1606 // Rotate |v1| counter-clockwise.
1607 gfx::Transform transform;
1608 RotateCounterclockwise(&transform);
1609 transform.matrix().set(1, 3, 500.0);
1610 v1->SetTransform(transform);
1612 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1614 v1->ClearScheduledPaintRect();
1615 v2->SchedulePaint();
1617 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect());
1619 widget->CloseNow();
1622 TEST_F(ViewTest, TransformEvent) {
1623 TestView* v1 = new TestView();
1624 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
1626 TestView* v2 = new TestView();
1627 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
1629 Widget* widget = new Widget;
1630 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1631 params.bounds = gfx::Rect(50, 50, 650, 650);
1632 widget->Init(params);
1633 View* root = widget->GetRootView();
1635 root->AddChildView(v1);
1636 v1->AddChildView(v2);
1638 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
1640 // Rotate |v1| counter-clockwise.
1641 gfx::Transform transform(v1->GetTransform());
1642 RotateCounterclockwise(&transform);
1643 transform.matrix().set(1, 3, 500.0);
1644 v1->SetTransform(transform);
1646 // |v2| now occupies (100, 200) to (200, 400) in |root|.
1647 v1->Reset();
1648 v2->Reset();
1650 gfx::Point p1(110, 210);
1651 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1,
1652 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1653 root->OnMousePressed(pressed);
1654 EXPECT_EQ(0, v1->last_mouse_event_type_);
1655 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
1656 EXPECT_EQ(190, v2->location_.x());
1657 EXPECT_EQ(10, v2->location_.y());
1659 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), 0,
1661 root->OnMouseReleased(released);
1663 // Now rotate |v2| inside |v1| clockwise.
1664 transform = v2->GetTransform();
1665 RotateClockwise(&transform);
1666 transform.matrix().set(0, 3, 100.f);
1667 v2->SetTransform(transform);
1669 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
1670 // (300, 400) in |root|.
1672 v1->Reset();
1673 v2->Reset();
1675 gfx::Point point2(110, 320);
1676 ui::MouseEvent p2(ui::ET_MOUSE_PRESSED, point2, point2,
1677 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1678 root->OnMousePressed(p2);
1679 EXPECT_EQ(0, v1->last_mouse_event_type_);
1680 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
1681 EXPECT_EQ(10, v2->location_.x());
1682 EXPECT_EQ(20, v2->location_.y());
1684 root->OnMouseReleased(released);
1686 v1->SetTransform(gfx::Transform());
1687 v2->SetTransform(gfx::Transform());
1689 TestView* v3 = new TestView();
1690 v3->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
1691 v2->AddChildView(v3);
1693 // Rotate |v3| clockwise with respect to |v2|.
1694 transform = v1->GetTransform();
1695 RotateClockwise(&transform);
1696 transform.matrix().set(0, 3, 30.f);
1697 v3->SetTransform(transform);
1699 // Scale |v2| with respect to |v1| along both axis.
1700 transform = v2->GetTransform();
1701 transform.matrix().set(0, 0, 0.8f);
1702 transform.matrix().set(1, 1, 0.5f);
1703 v2->SetTransform(transform);
1705 // |v3| occupies (108, 105) to (132, 115) in |root|.
1707 v1->Reset();
1708 v2->Reset();
1709 v3->Reset();
1711 gfx::Point point(112, 110);
1712 ui::MouseEvent p3(ui::ET_MOUSE_PRESSED, point, point,
1713 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1714 root->OnMousePressed(p3);
1716 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
1717 EXPECT_EQ(10, v3->location_.x());
1718 EXPECT_EQ(25, v3->location_.y());
1720 root->OnMouseReleased(released);
1722 v1->SetTransform(gfx::Transform());
1723 v2->SetTransform(gfx::Transform());
1724 v3->SetTransform(gfx::Transform());
1726 v1->Reset();
1727 v2->Reset();
1728 v3->Reset();
1730 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
1731 transform = v3->GetTransform();
1732 RotateClockwise(&transform);
1733 transform.matrix().set(0, 3, 30.f);
1734 // Rotation sets some scaling transformation. Using SetScale would overwrite
1735 // that and pollute the rotation. So combine the scaling with the existing
1736 // transforamtion.
1737 gfx::Transform scale;
1738 scale.Scale(0.8f, 0.5f);
1739 transform.ConcatTransform(scale);
1740 v3->SetTransform(transform);
1742 // Translate |v2| with respect to |v1|.
1743 transform = v2->GetTransform();
1744 transform.matrix().set(0, 3, 10.f);
1745 transform.matrix().set(1, 3, 10.f);
1746 v2->SetTransform(transform);
1748 // |v3| now occupies (120, 120) to (144, 130) in |root|.
1750 gfx::Point point3(124, 125);
1751 ui::MouseEvent p4(ui::ET_MOUSE_PRESSED, point3, point3,
1752 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
1753 root->OnMousePressed(p4);
1755 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
1756 EXPECT_EQ(10, v3->location_.x());
1757 EXPECT_EQ(25, v3->location_.y());
1759 root->OnMouseReleased(released);
1761 widget->CloseNow();
1764 TEST_F(ViewTest, TransformVisibleBound) {
1765 gfx::Rect viewport_bounds(0, 0, 100, 100);
1767 scoped_ptr<Widget> widget(new Widget);
1768 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1769 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1770 params.bounds = viewport_bounds;
1771 widget->Init(params);
1772 widget->GetRootView()->SetBoundsRect(viewport_bounds);
1774 View* viewport = new View;
1775 widget->SetContentsView(viewport);
1776 View* contents = new View;
1777 viewport->AddChildView(contents);
1778 viewport->SetBoundsRect(viewport_bounds);
1779 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
1781 View* child = new View;
1782 contents->AddChildView(child);
1783 child->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
1784 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds());
1786 // Rotate |child| counter-clockwise
1787 gfx::Transform transform;
1788 RotateCounterclockwise(&transform);
1789 transform.matrix().set(1, 3, 50.f);
1790 child->SetTransform(transform);
1791 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds());
1793 widget->CloseNow();
1796 ////////////////////////////////////////////////////////////////////////////////
1797 // OnVisibleBoundsChanged()
1799 class VisibleBoundsView : public View {
1800 public:
1801 VisibleBoundsView() : received_notification_(false) {}
1802 virtual ~VisibleBoundsView() {}
1804 bool received_notification() const { return received_notification_; }
1805 void set_received_notification(bool received) {
1806 received_notification_ = received;
1809 private:
1810 // Overridden from View:
1811 virtual bool NeedsNotificationWhenVisibleBoundsChange() const OVERRIDE {
1812 return true;
1814 virtual void OnVisibleBoundsChanged() OVERRIDE {
1815 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 virtual ~ObserverView();
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 virtual 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 // Layers
2646 ////////////////////////////////////////////////////////////////////////////////
2648 namespace {
2650 // Test implementation of LayerAnimator.
2651 class TestLayerAnimator : public ui::LayerAnimator {
2652 public:
2653 TestLayerAnimator();
2655 const gfx::Rect& last_bounds() const { return last_bounds_; }
2657 // LayerAnimator.
2658 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
2660 protected:
2661 virtual ~TestLayerAnimator() { }
2663 private:
2664 gfx::Rect last_bounds_;
2666 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
2669 TestLayerAnimator::TestLayerAnimator()
2670 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
2673 void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) {
2674 last_bounds_ = bounds;
2677 } // namespace
2679 class ViewLayerTest : public ViewsTestBase {
2680 public:
2681 ViewLayerTest() : widget_(NULL) {}
2683 virtual ~ViewLayerTest() {
2686 // Returns the Layer used by the RootView.
2687 ui::Layer* GetRootLayer() {
2688 return widget()->GetLayer();
2691 virtual void SetUp() OVERRIDE {
2692 ViewTest::SetUp();
2693 widget_ = new Widget;
2694 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2695 params.bounds = gfx::Rect(50, 50, 200, 200);
2696 widget_->Init(params);
2697 widget_->Show();
2698 widget_->GetRootView()->SetBounds(0, 0, 200, 200);
2701 virtual void TearDown() OVERRIDE {
2702 widget_->CloseNow();
2703 ViewsTestBase::TearDown();
2706 Widget* widget() { return widget_; }
2708 private:
2709 Widget* widget_;
2713 TEST_F(ViewLayerTest, LayerToggling) {
2714 // Because we lazily create textures the calls to DrawTree are necessary to
2715 // ensure we trigger creation of textures.
2716 ui::Layer* root_layer = widget()->GetLayer();
2717 View* content_view = new View;
2718 widget()->SetContentsView(content_view);
2720 // Create v1, give it a bounds and verify everything is set up correctly.
2721 View* v1 = new View;
2722 v1->SetPaintToLayer(true);
2723 EXPECT_TRUE(v1->layer() != NULL);
2724 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2725 content_view->AddChildView(v1);
2726 ASSERT_TRUE(v1->layer() != NULL);
2727 EXPECT_EQ(root_layer, v1->layer()->parent());
2728 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds());
2730 // Create v2 as a child of v1 and do basic assertion testing.
2731 View* v2 = new View;
2732 v1->AddChildView(v2);
2733 EXPECT_TRUE(v2->layer() == NULL);
2734 v2->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
2735 v2->SetPaintToLayer(true);
2736 ASSERT_TRUE(v2->layer() != NULL);
2737 EXPECT_EQ(v1->layer(), v2->layer()->parent());
2738 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
2740 // Turn off v1s layer. v2 should still have a layer but its parent should have
2741 // changed.
2742 v1->SetPaintToLayer(false);
2743 EXPECT_TRUE(v1->layer() == NULL);
2744 EXPECT_TRUE(v2->layer() != NULL);
2745 EXPECT_EQ(root_layer, v2->layer()->parent());
2746 ASSERT_EQ(1u, root_layer->children().size());
2747 EXPECT_EQ(root_layer->children()[0], v2->layer());
2748 // The bounds of the layer should have changed to be relative to the root view
2749 // now.
2750 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds());
2752 // Make v1 have a layer again and verify v2s layer is wired up correctly.
2753 gfx::Transform transform;
2754 transform.Scale(2.0, 2.0);
2755 v1->SetTransform(transform);
2756 EXPECT_TRUE(v1->layer() != NULL);
2757 EXPECT_TRUE(v2->layer() != NULL);
2758 EXPECT_EQ(root_layer, v1->layer()->parent());
2759 EXPECT_EQ(v1->layer(), v2->layer()->parent());
2760 ASSERT_EQ(1u, root_layer->children().size());
2761 EXPECT_EQ(root_layer->children()[0], v1->layer());
2762 ASSERT_EQ(1u, v1->layer()->children().size());
2763 EXPECT_EQ(v1->layer()->children()[0], v2->layer());
2764 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
2767 // Verifies turning on a layer wires up children correctly.
2768 TEST_F(ViewLayerTest, NestedLayerToggling) {
2769 View* content_view = new View;
2770 widget()->SetContentsView(content_view);
2772 // Create v1, give it a bounds and verify everything is set up correctly.
2773 View* v1 = new View;
2774 content_view->AddChildView(v1);
2775 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2777 View* v2 = new View;
2778 v1->AddChildView(v2);
2780 View* v3 = new View;
2781 v3->SetPaintToLayer(true);
2782 v2->AddChildView(v3);
2783 ASSERT_TRUE(v3->layer() != NULL);
2785 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
2787 v1->SetPaintToLayer(true);
2788 EXPECT_EQ(v1->layer(), v3->layer()->parent());
2791 TEST_F(ViewLayerTest, LayerAnimator) {
2792 View* content_view = new View;
2793 widget()->SetContentsView(content_view);
2795 View* v1 = new View;
2796 content_view->AddChildView(v1);
2797 v1->SetPaintToLayer(true);
2798 EXPECT_TRUE(v1->layer() != NULL);
2800 TestLayerAnimator* animator = new TestLayerAnimator();
2801 v1->layer()->SetAnimator(animator);
2803 gfx::Rect bounds(1, 2, 3, 4);
2804 v1->SetBoundsRect(bounds);
2805 EXPECT_EQ(bounds, animator->last_bounds());
2806 // TestLayerAnimator doesn't update the layer.
2807 EXPECT_NE(bounds, v1->layer()->bounds());
2810 // Verifies the bounds of a layer are updated if the bounds of ancestor that
2811 // doesn't have a layer change.
2812 TEST_F(ViewLayerTest, BoundsChangeWithLayer) {
2813 View* content_view = new View;
2814 widget()->SetContentsView(content_view);
2816 View* v1 = new View;
2817 content_view->AddChildView(v1);
2818 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
2820 View* v2 = new View;
2821 v2->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
2822 v1->AddChildView(v2);
2823 v2->SetPaintToLayer(true);
2824 ASSERT_TRUE(v2->layer() != NULL);
2825 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
2827 v1->SetPosition(gfx::Point(25, 36));
2828 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
2830 v2->SetPosition(gfx::Point(11, 12));
2831 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
2833 // Bounds of the layer should change even if the view is not invisible.
2834 v1->SetVisible(false);
2835 v1->SetPosition(gfx::Point(20, 30));
2836 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
2838 v2->SetVisible(false);
2839 v2->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
2840 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
2843 // Make sure layers are positioned correctly in RTL.
2844 TEST_F(ViewLayerTest, BoundInRTL) {
2845 std::string locale = l10n_util::GetApplicationLocale(std::string());
2846 base::i18n::SetICUDefaultLocale("he");
2848 View* view = new View;
2849 widget()->SetContentsView(view);
2851 int content_width = view->width();
2853 // |v1| is initially not attached to anything. So its layer will have the same
2854 // bounds as the view.
2855 View* v1 = new View;
2856 v1->SetPaintToLayer(true);
2857 v1->SetBounds(10, 10, 20, 10);
2858 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
2859 v1->layer()->bounds());
2861 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
2862 // bounds.
2863 view->AddChildView(v1);
2864 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
2865 v1->layer()->bounds());
2866 gfx::Rect l1bounds = v1->layer()->bounds();
2868 // Now attach a View to the widget first, then create a layer for it. Make
2869 // sure the bounds are correct.
2870 View* v2 = new View;
2871 v2->SetBounds(50, 10, 30, 10);
2872 EXPECT_FALSE(v2->layer());
2873 view->AddChildView(v2);
2874 v2->SetPaintToLayer(true);
2875 EXPECT_EQ(gfx::Rect(content_width - 80, 10, 30, 10),
2876 v2->layer()->bounds());
2877 gfx::Rect l2bounds = v2->layer()->bounds();
2879 view->SetPaintToLayer(true);
2880 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2881 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2883 // Move one of the views. Make sure the layer is positioned correctly
2884 // afterwards.
2885 v1->SetBounds(v1->x() - 5, v1->y(), v1->width(), v1->height());
2886 l1bounds.set_x(l1bounds.x() + 5);
2887 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2889 view->SetPaintToLayer(false);
2890 EXPECT_EQ(l1bounds, v1->layer()->bounds());
2891 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2893 // Move a view again.
2894 v2->SetBounds(v2->x() + 5, v2->y(), v2->width(), v2->height());
2895 l2bounds.set_x(l2bounds.x() - 5);
2896 EXPECT_EQ(l2bounds, v2->layer()->bounds());
2898 // Reset locale.
2899 base::i18n::SetICUDefaultLocale(locale);
2902 // Makes sure a transform persists after toggling the visibility.
2903 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
2904 View* view = new View;
2905 gfx::Transform transform;
2906 transform.Scale(2.0, 2.0);
2907 view->SetTransform(transform);
2908 widget()->SetContentsView(view);
2909 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
2911 view->SetVisible(false);
2912 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
2914 view->SetVisible(true);
2915 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
2918 // Verifies a transform persists after removing/adding a view with a transform.
2919 TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) {
2920 View* view = new View;
2921 gfx::Transform transform;
2922 transform.Scale(2.0, 2.0);
2923 view->SetTransform(transform);
2924 widget()->SetContentsView(view);
2925 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
2926 ASSERT_TRUE(view->layer() != NULL);
2927 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
2929 View* parent = view->parent();
2930 parent->RemoveChildView(view);
2931 parent->AddChildView(view);
2933 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
2934 ASSERT_TRUE(view->layer() != NULL);
2935 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
2938 // Makes sure that layer visibility is correct after toggling View visibility.
2939 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
2940 View* content_view = new View;
2941 widget()->SetContentsView(content_view);
2943 // The view isn't attached to a widget or a parent view yet. But it should
2944 // still have a layer, but the layer should not be attached to the root
2945 // layer.
2946 View* v1 = new View;
2947 v1->SetPaintToLayer(true);
2948 EXPECT_TRUE(v1->layer());
2949 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2950 v1->layer()));
2952 // Once the view is attached to a widget, its layer should be attached to the
2953 // root layer and visible.
2954 content_view->AddChildView(v1);
2955 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2956 v1->layer()));
2957 EXPECT_TRUE(v1->layer()->IsDrawn());
2959 v1->SetVisible(false);
2960 EXPECT_FALSE(v1->layer()->IsDrawn());
2962 v1->SetVisible(true);
2963 EXPECT_TRUE(v1->layer()->IsDrawn());
2965 widget()->Hide();
2966 EXPECT_FALSE(v1->layer()->IsDrawn());
2968 widget()->Show();
2969 EXPECT_TRUE(v1->layer()->IsDrawn());
2972 // Tests that the layers in the subtree are orphaned after a View is removed
2973 // from the parent.
2974 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
2975 View* content_view = new View;
2976 widget()->SetContentsView(content_view);
2978 View* v1 = new View;
2979 content_view->AddChildView(v1);
2981 View* v2 = new View;
2982 v1->AddChildView(v2);
2983 v2->SetPaintToLayer(true);
2984 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2985 v2->layer()));
2986 EXPECT_TRUE(v2->layer()->IsDrawn());
2988 content_view->RemoveChildView(v1);
2990 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2991 v2->layer()));
2993 // Reparent |v2|.
2994 content_view->AddChildView(v2);
2995 delete v1;
2996 v1 = NULL;
2997 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
2998 v2->layer()));
2999 EXPECT_TRUE(v2->layer()->IsDrawn());
3002 class PaintTrackingView : public View {
3003 public:
3004 PaintTrackingView() : painted_(false) {
3007 bool painted() const { return painted_; }
3008 void set_painted(bool value) { painted_ = value; }
3010 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
3011 painted_ = true;
3014 private:
3015 bool painted_;
3017 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView);
3020 // Makes sure child views with layers aren't painted when paint starts at an
3021 // ancestor.
3022 TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) {
3023 PaintTrackingView* content_view = new PaintTrackingView;
3024 widget()->SetContentsView(content_view);
3025 content_view->SetPaintToLayer(true);
3026 GetRootLayer()->GetCompositor()->ScheduleDraw();
3027 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3028 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3029 content_view->set_painted(false);
3030 // content_view no longer has a dirty rect. Paint from the root and make sure
3031 // PaintTrackingView isn't painted.
3032 GetRootLayer()->GetCompositor()->ScheduleDraw();
3033 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3034 EXPECT_FALSE(content_view->painted());
3036 // Make content_view have a dirty rect, paint the layers and make sure
3037 // PaintTrackingView is painted.
3038 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3039 GetRootLayer()->GetCompositor()->ScheduleDraw();
3040 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3041 EXPECT_TRUE(content_view->painted());
3044 // Tests that the visibility of child layers are updated correctly when a View's
3045 // visibility changes.
3046 TEST_F(ViewLayerTest, VisibilityChildLayers) {
3047 View* v1 = new View;
3048 v1->SetPaintToLayer(true);
3049 widget()->SetContentsView(v1);
3051 View* v2 = new View;
3052 v1->AddChildView(v2);
3054 View* v3 = new View;
3055 v2->AddChildView(v3);
3056 v3->SetVisible(false);
3058 View* v4 = new View;
3059 v4->SetPaintToLayer(true);
3060 v3->AddChildView(v4);
3062 EXPECT_TRUE(v1->layer()->IsDrawn());
3063 EXPECT_FALSE(v4->layer()->IsDrawn());
3065 v2->SetVisible(false);
3066 EXPECT_TRUE(v1->layer()->IsDrawn());
3067 EXPECT_FALSE(v4->layer()->IsDrawn());
3069 v2->SetVisible(true);
3070 EXPECT_TRUE(v1->layer()->IsDrawn());
3071 EXPECT_FALSE(v4->layer()->IsDrawn());
3073 v2->SetVisible(false);
3074 EXPECT_TRUE(v1->layer()->IsDrawn());
3075 EXPECT_FALSE(v4->layer()->IsDrawn());
3076 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3078 v3->SetVisible(true);
3079 EXPECT_TRUE(v1->layer()->IsDrawn());
3080 EXPECT_FALSE(v4->layer()->IsDrawn());
3081 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3083 // Reparent |v3| to |v1|.
3084 v1->AddChildView(v3);
3085 EXPECT_TRUE(v1->layer()->IsDrawn());
3086 EXPECT_TRUE(v4->layer()->IsDrawn());
3087 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3090 // This test creates a random View tree, and then randomly reorders child views,
3091 // reparents views etc. Unrelated changes can appear to break this test. So
3092 // marking this as FLAKY.
3093 TEST_F(ViewLayerTest, DISABLED_ViewLayerTreesInSync) {
3094 View* content = new View;
3095 content->SetPaintToLayer(true);
3096 widget()->SetContentsView(content);
3097 widget()->Show();
3099 ConstructTree(content, 5);
3100 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3102 ScrambleTree(content);
3103 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3105 ScrambleTree(content);
3106 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3108 ScrambleTree(content);
3109 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3112 // Verifies when views are reordered the layer is also reordered. The widget is
3113 // providing the parent layer.
3114 TEST_F(ViewLayerTest, ReorderUnderWidget) {
3115 View* content = new View;
3116 widget()->SetContentsView(content);
3117 View* c1 = new View;
3118 c1->SetPaintToLayer(true);
3119 content->AddChildView(c1);
3120 View* c2 = new View;
3121 c2->SetPaintToLayer(true);
3122 content->AddChildView(c2);
3124 ui::Layer* parent_layer = c1->layer()->parent();
3125 ASSERT_TRUE(parent_layer);
3126 ASSERT_EQ(2u, parent_layer->children().size());
3127 EXPECT_EQ(c1->layer(), parent_layer->children()[0]);
3128 EXPECT_EQ(c2->layer(), parent_layer->children()[1]);
3130 // Move c1 to the front. The layers should have moved too.
3131 content->ReorderChildView(c1, -1);
3132 EXPECT_EQ(c1->layer(), parent_layer->children()[1]);
3133 EXPECT_EQ(c2->layer(), parent_layer->children()[0]);
3136 // Verifies that the layer of a view can be acquired properly.
3137 TEST_F(ViewLayerTest, AcquireLayer) {
3138 View* content = new View;
3139 widget()->SetContentsView(content);
3140 scoped_ptr<View> c1(new View);
3141 c1->SetPaintToLayer(true);
3142 EXPECT_TRUE(c1->layer());
3143 content->AddChildView(c1.get());
3145 scoped_ptr<ui::Layer> layer(c1->AcquireLayer());
3146 EXPECT_EQ(layer.get(), c1->layer());
3148 scoped_ptr<ui::Layer> layer2(c1->RecreateLayer());
3149 EXPECT_NE(c1->layer(), layer2.get());
3151 // Destroy view before destroying layer.
3152 c1.reset();
3155 // Verify the z-order of the layers as a result of calling RecreateLayer().
3156 TEST_F(ViewLayerTest, RecreateLayerZOrder) {
3157 scoped_ptr<View> v(new View());
3158 v->SetPaintToLayer(true);
3160 View* v1 = new View();
3161 v1->SetPaintToLayer(true);
3162 v->AddChildView(v1);
3163 View* v2 = new View();
3164 v2->SetPaintToLayer(true);
3165 v->AddChildView(v2);
3167 // Test the initial z-order.
3168 const std::vector<ui::Layer*>& child_layers_pre = v->layer()->children();
3169 ASSERT_EQ(2u, child_layers_pre.size());
3170 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3171 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3173 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3175 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3176 // for |v1| and |v2|.
3177 const std::vector<ui::Layer*>& child_layers_post = v->layer()->children();
3178 ASSERT_EQ(3u, child_layers_post.size());
3179 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3180 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3181 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3184 // Verify the z-order of the layers as a result of calling RecreateLayer when
3185 // the widget is the parent with the layer.
3186 TEST_F(ViewLayerTest, RecreateLayerZOrderWidgetParent) {
3187 View* v = new View();
3188 widget()->SetContentsView(v);
3190 View* v1 = new View();
3191 v1->SetPaintToLayer(true);
3192 v->AddChildView(v1);
3193 View* v2 = new View();
3194 v2->SetPaintToLayer(true);
3195 v->AddChildView(v2);
3197 ui::Layer* root_layer = GetRootLayer();
3199 // Test the initial z-order.
3200 const std::vector<ui::Layer*>& child_layers_pre = root_layer->children();
3201 ASSERT_EQ(2u, child_layers_pre.size());
3202 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3203 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3205 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3207 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3208 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3209 ASSERT_EQ(3u, child_layers_post.size());
3210 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3211 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3212 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3215 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3216 // a View.
3217 TEST_F(ViewLayerTest, RecreateLayerMovesNonViewChildren) {
3218 View v;
3219 v.SetPaintToLayer(true);
3220 View child;
3221 child.SetPaintToLayer(true);
3222 v.AddChildView(&child);
3223 ASSERT_TRUE(v.layer() != NULL);
3224 ASSERT_EQ(1u, v.layer()->children().size());
3225 EXPECT_EQ(v.layer()->children()[0], child.layer());
3227 ui::Layer layer(ui::LAYER_NOT_DRAWN);
3228 v.layer()->Add(&layer);
3229 v.layer()->StackAtBottom(&layer);
3231 scoped_ptr<ui::Layer> old_layer(v.RecreateLayer());
3233 // All children should be moved from old layer to new layer.
3234 ASSERT_TRUE(old_layer.get() != NULL);
3235 EXPECT_TRUE(old_layer->children().empty());
3237 // And new layer should have the two children.
3238 ASSERT_TRUE(v.layer() != NULL);
3239 ASSERT_EQ(2u, v.layer()->children().size());
3240 EXPECT_EQ(v.layer()->children()[0], &layer);
3241 EXPECT_EQ(v.layer()->children()[1], child.layer());
3244 class BoundsTreeTestView : public View {
3245 public:
3246 BoundsTreeTestView() {}
3248 virtual void PaintChildren(gfx::Canvas* canvas,
3249 const CullSet& cull_set) OVERRIDE {
3250 // Save out a copy of the cull_set before calling the base implementation.
3251 last_cull_set_.clear();
3252 if (cull_set.cull_set_) {
3253 for (base::hash_set<intptr_t>::iterator it = cull_set.cull_set_->begin();
3254 it != cull_set.cull_set_->end();
3255 ++it) {
3256 last_cull_set_.insert(reinterpret_cast<View*>(*it));
3259 View::PaintChildren(canvas, cull_set);
3262 std::set<View*> last_cull_set_;
3265 TEST_F(ViewLayerTest, BoundsTreePaintUpdatesCullSet) {
3266 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3267 widget()->SetContentsView(test_view);
3269 View* v1 = new View();
3270 v1->SetBoundsRect(gfx::Rect(10, 15, 150, 151));
3271 test_view->AddChildView(v1);
3273 View* v2 = new View();
3274 v2->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3275 v1->AddChildView(v2);
3277 // Schedule a full-view paint to get everyone's rectangles updated.
3278 test_view->SchedulePaintInRect(test_view->bounds());
3279 GetRootLayer()->GetCompositor()->ScheduleDraw();
3280 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3282 // Now we have test_view - v1 - v2. Damage to only test_view should only
3283 // return root_view and test_view.
3284 test_view->SchedulePaintInRect(gfx::Rect(0, 0, 1, 1));
3285 GetRootLayer()->GetCompositor()->ScheduleDraw();
3286 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3287 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3288 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3289 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3291 // Damage to v1 only should only return root_view, test_view, and v1.
3292 test_view->SchedulePaintInRect(gfx::Rect(11, 16, 1, 1));
3293 GetRootLayer()->GetCompositor()->ScheduleDraw();
3294 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3295 EXPECT_EQ(3U, test_view->last_cull_set_.size());
3296 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3297 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3298 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3300 // A Damage rect inside v2 should get all 3 views back in the |last_cull_set_|
3301 // on call to TestView::Paint(), along with the widget root view.
3302 test_view->SchedulePaintInRect(gfx::Rect(31, 49, 1, 1));
3303 GetRootLayer()->GetCompositor()->ScheduleDraw();
3304 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3305 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3306 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3307 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3308 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3309 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3312 TEST_F(ViewLayerTest, BoundsTreeWithRTL) {
3313 std::string locale = l10n_util::GetApplicationLocale(std::string());
3314 base::i18n::SetICUDefaultLocale("ar");
3316 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3317 widget()->SetContentsView(test_view);
3319 // Add child views, which should be in RTL coordinate space of parent view.
3320 View* v1 = new View;
3321 v1->SetBoundsRect(gfx::Rect(10, 12, 25, 26));
3322 test_view->AddChildView(v1);
3324 View* v2 = new View;
3325 v2->SetBoundsRect(gfx::Rect(5, 6, 7, 8));
3326 v1->AddChildView(v2);
3328 // Schedule a full-view paint to get everyone's rectangles updated.
3329 test_view->SchedulePaintInRect(test_view->bounds());
3330 GetRootLayer()->GetCompositor()->ScheduleDraw();
3331 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3333 // Damage to the right side of the parent view should touch both child views.
3334 gfx::Rect rtl_damage(test_view->bounds().width() - 16, 18, 1, 1);
3335 test_view->SchedulePaintInRect(rtl_damage);
3336 GetRootLayer()->GetCompositor()->ScheduleDraw();
3337 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3338 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3339 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3340 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3341 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3342 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3344 // Damage to the left side of the parent view should only touch the
3345 // container views.
3346 gfx::Rect ltr_damage(16, 18, 1, 1);
3347 test_view->SchedulePaintInRect(ltr_damage);
3348 GetRootLayer()->GetCompositor()->ScheduleDraw();
3349 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3350 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3351 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3352 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3354 // Reset locale.
3355 base::i18n::SetICUDefaultLocale(locale);
3358 TEST_F(ViewLayerTest, BoundsTreeSetBoundsChangesCullSet) {
3359 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3360 widget()->SetContentsView(test_view);
3362 View* v1 = new View;
3363 v1->SetBoundsRect(gfx::Rect(5, 6, 100, 101));
3364 test_view->AddChildView(v1);
3366 View* v2 = new View;
3367 v2->SetBoundsRect(gfx::Rect(20, 33, 40, 50));
3368 v1->AddChildView(v2);
3370 // Schedule a full-view paint to get everyone's rectangles updated.
3371 test_view->SchedulePaintInRect(test_view->bounds());
3372 GetRootLayer()->GetCompositor()->ScheduleDraw();
3373 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3375 // Move v1 to a new origin out of the way of our next query.
3376 v1->SetBoundsRect(gfx::Rect(50, 60, 100, 101));
3377 // The move will force a repaint.
3378 GetRootLayer()->GetCompositor()->ScheduleDraw();
3379 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3381 // Schedule a paint with damage rect where v1 used to be.
3382 test_view->SchedulePaintInRect(gfx::Rect(5, 6, 10, 11));
3383 GetRootLayer()->GetCompositor()->ScheduleDraw();
3384 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3386 // Should only have picked up root_view and test_view.
3387 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3388 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3389 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3392 TEST_F(ViewLayerTest, BoundsTreeLayerChangeMakesNewTree) {
3393 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3394 widget()->SetContentsView(test_view);
3396 View* v1 = new View;
3397 v1->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3398 test_view->AddChildView(v1);
3400 View* v2 = new View;
3401 v2->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3402 v1->AddChildView(v2);
3404 // Schedule a full-view paint to get everyone's rectangles updated.
3405 test_view->SchedulePaintInRect(test_view->bounds());
3406 GetRootLayer()->GetCompositor()->ScheduleDraw();
3407 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3409 // Set v1 to paint to its own layer, it should remove itself from the
3410 // test_view heiarchy and no longer intersect with damage rects in that cull
3411 // set.
3412 v1->SetPaintToLayer(true);
3414 // Schedule another full-view paint.
3415 test_view->SchedulePaintInRect(test_view->bounds());
3416 GetRootLayer()->GetCompositor()->ScheduleDraw();
3417 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3418 // v1 and v2 should no longer be present in the test_view cull_set.
3419 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3420 EXPECT_EQ(0U, test_view->last_cull_set_.count(v1));
3421 EXPECT_EQ(0U, test_view->last_cull_set_.count(v2));
3423 // Now set v1 back to not painting to a layer.
3424 v1->SetPaintToLayer(false);
3425 // Schedule another full-view paint.
3426 test_view->SchedulePaintInRect(test_view->bounds());
3427 GetRootLayer()->GetCompositor()->ScheduleDraw();
3428 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3429 // We should be back to the full cull set including v1 and v2.
3430 EXPECT_EQ(4U, test_view->last_cull_set_.size());
3431 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3432 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3433 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3434 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3437 TEST_F(ViewLayerTest, BoundsTreeRemoveChildRemovesBounds) {
3438 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3439 widget()->SetContentsView(test_view);
3441 View* v1 = new View;
3442 v1->SetBoundsRect(gfx::Rect(5, 10, 15, 20));
3443 test_view->AddChildView(v1);
3445 View* v2 = new View;
3446 v2->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3447 v1->AddChildView(v2);
3449 // Schedule a full-view paint to get everyone's rectangles updated.
3450 test_view->SchedulePaintInRect(test_view->bounds());
3451 GetRootLayer()->GetCompositor()->ScheduleDraw();
3452 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3454 // Now remove v1 from the root view.
3455 test_view->RemoveChildView(v1);
3457 // Schedule another full-view paint.
3458 test_view->SchedulePaintInRect(test_view->bounds());
3459 GetRootLayer()->GetCompositor()->ScheduleDraw();
3460 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3461 // v1 and v2 should no longer be present in the test_view cull_set.
3462 EXPECT_EQ(2U, test_view->last_cull_set_.size());
3463 EXPECT_EQ(0U, test_view->last_cull_set_.count(v1));
3464 EXPECT_EQ(0U, test_view->last_cull_set_.count(v2));
3466 // View v1 and v2 are no longer part of view hierarchy and therefore won't be
3467 // deleted with that hierarchy.
3468 delete v1;
3471 TEST_F(ViewLayerTest, BoundsTreeMoveViewMovesBounds) {
3472 BoundsTreeTestView* test_view = new BoundsTreeTestView;
3473 widget()->SetContentsView(test_view);
3475 // Build hierarchy v1 - v2 - v3.
3476 View* v1 = new View;
3477 v1->SetBoundsRect(gfx::Rect(20, 30, 150, 160));
3478 test_view->AddChildView(v1);
3480 View* v2 = new View;
3481 v2->SetBoundsRect(gfx::Rect(5, 10, 40, 50));
3482 v1->AddChildView(v2);
3484 View* v3 = new View;
3485 v3->SetBoundsRect(gfx::Rect(1, 2, 3, 4));
3486 v2->AddChildView(v3);
3488 // Schedule a full-view paint and ensure all views are present in the cull.
3489 test_view->SchedulePaintInRect(test_view->bounds());
3490 GetRootLayer()->GetCompositor()->ScheduleDraw();
3491 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3492 EXPECT_EQ(5U, test_view->last_cull_set_.size());
3493 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3494 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3495 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3496 EXPECT_EQ(1U, test_view->last_cull_set_.count(v2));
3497 EXPECT_EQ(1U, test_view->last_cull_set_.count(v3));
3499 // Build an unrelated view hierarchy and move v2 in to it.
3500 scoped_ptr<Widget> test_widget(new Widget);
3501 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3502 params.bounds = gfx::Rect(10, 10, 500, 500);
3503 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3504 test_widget->Init(params);
3505 test_widget->Show();
3506 BoundsTreeTestView* widget_view = new BoundsTreeTestView;
3507 test_widget->SetContentsView(widget_view);
3508 widget_view->AddChildView(v2);
3510 // Now schedule full-view paints in both widgets.
3511 test_view->SchedulePaintInRect(test_view->bounds());
3512 widget_view->SchedulePaintInRect(widget_view->bounds());
3513 GetRootLayer()->GetCompositor()->ScheduleDraw();
3514 ui::DrawWaiterForTest::Wait(GetRootLayer()->GetCompositor());
3516 // Only v1 should be present in the first cull set.
3517 EXPECT_EQ(3U, test_view->last_cull_set_.size());
3518 EXPECT_EQ(1U, test_view->last_cull_set_.count(widget()->GetRootView()));
3519 EXPECT_EQ(1U, test_view->last_cull_set_.count(test_view));
3520 EXPECT_EQ(1U, test_view->last_cull_set_.count(v1));
3522 // We should find v2 and v3 in the widget_view cull_set.
3523 EXPECT_EQ(4U, widget_view->last_cull_set_.size());
3524 EXPECT_EQ(1U, widget_view->last_cull_set_.count(test_widget->GetRootView()));
3525 EXPECT_EQ(1U, widget_view->last_cull_set_.count(widget_view));
3526 EXPECT_EQ(1U, widget_view->last_cull_set_.count(v2));
3527 EXPECT_EQ(1U, widget_view->last_cull_set_.count(v3));
3530 namespace {
3532 std::string ToString(const gfx::Vector2dF& vector) {
3533 return base::StringPrintf("%.2f %0.2f", vector.x(), vector.y());
3536 } // namespace
3538 TEST_F(ViewLayerTest, SnapLayerToPixel) {
3539 View* v1 = new View;
3541 View* v11 = new View;
3542 v1->AddChildView(v11);
3544 widget()->SetContentsView(v1);
3546 const gfx::Size& size = GetRootLayer()->GetCompositor()->size();
3547 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f, size);
3549 v11->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3550 v1->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3551 v11->SetPaintToLayer(true);
3553 EXPECT_EQ("0.40 0.40", ToString(v11->layer()->subpixel_position_offset()));
3555 // Creating a layer in parent should update the child view's layer offset.
3556 v1->SetPaintToLayer(true);
3557 EXPECT_EQ("-0.20 -0.20", ToString(v1->layer()->subpixel_position_offset()));
3558 EXPECT_EQ("-0.20 -0.20", ToString(v11->layer()->subpixel_position_offset()));
3560 // DSF change should get propagated and update offsets.
3561 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f, size);
3562 EXPECT_EQ("0.33 0.33", ToString(v1->layer()->subpixel_position_offset()));
3563 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3565 // Deleting parent's layer should update the child view's layer's offset.
3566 v1->SetPaintToLayer(false);
3567 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3569 // Setting parent view should update the child view's layer's offset.
3570 v1->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3571 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3573 // Setting integral DSF should reset the offset.
3574 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f, size);
3575 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3578 TEST_F(ViewTest, FocusableAssertions) {
3579 // View subclasses may change insets based on whether they are focusable,
3580 // which effects the preferred size. To avoid preferred size changing around
3581 // these Views need to key off the last value set to SetFocusable(), not
3582 // whether the View is focusable right now. For this reason it's important
3583 // that focusable() return the last value passed to SetFocusable and not
3584 // whether the View is focusable right now.
3585 TestView view;
3586 view.SetFocusable(true);
3587 EXPECT_TRUE(view.focusable());
3588 view.SetEnabled(false);
3589 EXPECT_TRUE(view.focusable());
3590 view.SetFocusable(false);
3591 EXPECT_FALSE(view.focusable());
3594 // Verifies when a view is deleted it is removed from ViewStorage.
3595 TEST_F(ViewTest, UpdateViewStorageOnDelete) {
3596 ViewStorage* view_storage = ViewStorage::GetInstance();
3597 const int storage_id = view_storage->CreateStorageID();
3599 View view;
3600 view_storage->StoreView(storage_id, &view);
3602 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL);
3605 ////////////////////////////////////////////////////////////////////////////////
3606 // NativeTheme
3607 ////////////////////////////////////////////////////////////////////////////////
3609 void TestView::OnNativeThemeChanged(const ui::NativeTheme* native_theme) {
3610 native_theme_ = native_theme;
3613 TEST_F(ViewTest, OnNativeThemeChanged) {
3614 TestView* test_view = new TestView();
3615 EXPECT_FALSE(test_view->native_theme_);
3616 TestView* test_view_child = new TestView();
3617 EXPECT_FALSE(test_view_child->native_theme_);
3619 // Child view added before the widget hierarchy exists should get the
3620 // new native theme notification.
3621 test_view->AddChildView(test_view_child);
3623 scoped_ptr<Widget> widget(new Widget);
3624 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
3625 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3626 widget->Init(params);
3628 widget->GetRootView()->AddChildView(test_view);
3629 EXPECT_TRUE(test_view->native_theme_);
3630 EXPECT_EQ(widget->GetNativeTheme(), test_view->native_theme_);
3631 EXPECT_TRUE(test_view_child->native_theme_);
3632 EXPECT_EQ(widget->GetNativeTheme(), test_view_child->native_theme_);
3634 // Child view added after the widget hierarchy exists should also get the
3635 // notification.
3636 TestView* test_view_child_2 = new TestView();
3637 test_view->AddChildView(test_view_child_2);
3638 EXPECT_TRUE(test_view_child_2->native_theme_);
3639 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_);
3641 widget->CloseNow();
3644 } // namespace views