Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / views / view_unittest.cc
blob7fc1c1305f31bae8e4bfd5e9baa95a35d8e24b85
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <map>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/rand_util.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/base/accelerators/accelerator.h"
13 #include "ui/base/clipboard/clipboard.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/compositor/compositor.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/compositor/layer_animator.h"
18 #include "ui/compositor/paint_context.h"
19 #include "ui/compositor/test/draw_waiter_for_test.h"
20 #include "ui/events/event.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/keycodes/keyboard_codes.h"
23 #include "ui/gfx/canvas.h"
24 #include "ui/gfx/path.h"
25 #include "ui/gfx/transform.h"
26 #include "ui/strings/grit/ui_strings.h"
27 #include "ui/views/background.h"
28 #include "ui/views/controls/native/native_view_host.h"
29 #include "ui/views/controls/scroll_view.h"
30 #include "ui/views/controls/textfield/textfield.h"
31 #include "ui/views/focus/view_storage.h"
32 #include "ui/views/test/views_test_base.h"
33 #include "ui/views/view.h"
34 #include "ui/views/views_delegate.h"
35 #include "ui/views/widget/native_widget.h"
36 #include "ui/views/widget/root_view.h"
37 #include "ui/views/window/dialog_client_view.h"
38 #include "ui/views/window/dialog_delegate.h"
40 using base::ASCIIToUTF16;
42 namespace {
44 // Returns true if |ancestor| is an ancestor of |layer|.
45 bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) {
46 while (layer && layer != ancestor)
47 layer = layer->parent();
48 return layer == ancestor;
51 // Convenience functions for walking a View tree.
52 const views::View* FirstView(const views::View* view) {
53 const views::View* v = view;
54 while (v->has_children())
55 v = v->child_at(0);
56 return v;
59 const views::View* NextView(const views::View* view) {
60 const views::View* v = view;
61 const views::View* parent = v->parent();
62 if (!parent)
63 return NULL;
64 int next = parent->GetIndexOf(v) + 1;
65 if (next != parent->child_count())
66 return FirstView(parent->child_at(next));
67 return parent;
70 // Convenience functions for walking a Layer tree.
71 const ui::Layer* FirstLayer(const ui::Layer* layer) {
72 const ui::Layer* l = layer;
73 while (l->children().size() > 0)
74 l = l->children()[0];
75 return l;
78 const ui::Layer* NextLayer(const ui::Layer* layer) {
79 const ui::Layer* parent = layer->parent();
80 if (!parent)
81 return NULL;
82 const std::vector<ui::Layer*> children = parent->children();
83 size_t index;
84 for (index = 0; index < children.size(); index++) {
85 if (children[index] == layer)
86 break;
88 size_t next = index + 1;
89 if (next < children.size())
90 return FirstLayer(children[next]);
91 return parent;
94 // Given the root nodes of a View tree and a Layer tree, makes sure the two
95 // trees are in sync.
96 bool ViewAndLayerTreeAreConsistent(const views::View* view,
97 const ui::Layer* layer) {
98 const views::View* v = FirstView(view);
99 const ui::Layer* l = FirstLayer(layer);
100 while (v && l) {
101 // Find the view with a layer.
102 while (v && !v->layer())
103 v = NextView(v);
104 EXPECT_TRUE(v);
105 if (!v)
106 return false;
108 // Check if the View tree and the Layer tree are in sync.
109 EXPECT_EQ(l, v->layer());
110 if (v->layer() != l)
111 return false;
113 // Check if the visibility states of the View and the Layer are in sync.
114 EXPECT_EQ(l->IsDrawn(), v->IsDrawn());
115 if (v->IsDrawn() != l->IsDrawn()) {
116 for (const views::View* vv = v; vv; vv = vv->parent())
117 LOG(ERROR) << "V: " << vv << " " << vv->visible() << " "
118 << vv->IsDrawn() << " " << vv->layer();
119 for (const ui::Layer* ll = l; ll; ll = ll->parent())
120 LOG(ERROR) << "L: " << ll << " " << ll->IsDrawn();
121 return false;
124 // Check if the size of the View and the Layer are in sync.
125 EXPECT_EQ(l->bounds(), v->bounds());
126 if (v->bounds() != l->bounds())
127 return false;
129 if (v == view || l == layer)
130 return v == view && l == layer;
132 v = NextView(v);
133 l = NextLayer(l);
136 return false;
139 // Constructs a View tree with the specified depth.
140 void ConstructTree(views::View* view, int depth) {
141 if (depth == 0)
142 return;
143 int count = base::RandInt(1, 5);
144 for (int i = 0; i < count; i++) {
145 views::View* v = new views::View;
146 view->AddChildView(v);
147 if (base::RandDouble() > 0.5)
148 v->SetPaintToLayer(true);
149 if (base::RandDouble() < 0.2)
150 v->SetVisible(false);
152 ConstructTree(v, depth - 1);
156 void ScrambleTree(views::View* view) {
157 int count = view->child_count();
158 if (count == 0)
159 return;
160 for (int i = 0; i < count; i++) {
161 ScrambleTree(view->child_at(i));
164 if (count > 1) {
165 int a = base::RandInt(0, count - 1);
166 int b = base::RandInt(0, count - 1);
168 views::View* view_a = view->child_at(a);
169 views::View* view_b = view->child_at(b);
170 view->ReorderChildView(view_a, b);
171 view->ReorderChildView(view_b, a);
174 if (!view->layer() && base::RandDouble() < 0.1)
175 view->SetPaintToLayer(true);
177 if (base::RandDouble() < 0.1)
178 view->SetVisible(!view->visible());
181 class ScopedRTL {
182 public:
183 ScopedRTL() {
184 locale_ = l10n_util::GetApplicationLocale(std::string());
185 base::i18n::SetICUDefaultLocale("he");
187 ~ScopedRTL() { base::i18n::SetICUDefaultLocale(locale_); }
189 private:
190 std::string locale_;
193 } // namespace
195 namespace views {
197 typedef ViewsTestBase ViewTest;
199 // A derived class for testing purpose.
200 class TestView : public View {
201 public:
202 TestView()
203 : View(),
204 delete_on_pressed_(false),
205 did_paint_(false),
206 native_theme_(NULL),
207 can_process_events_within_subtree_(true) {}
208 ~TestView() override {}
210 // Reset all test state
211 void Reset() {
212 did_change_bounds_ = false;
213 last_mouse_event_type_ = 0;
214 location_.SetPoint(0, 0);
215 received_mouse_enter_ = false;
216 received_mouse_exit_ = false;
217 did_paint_ = false;
218 accelerator_count_map_.clear();
219 can_process_events_within_subtree_ = true;
222 // Exposed as public for testing.
223 void DoFocus() {
224 views::View::Focus();
227 void DoBlur() {
228 views::View::Blur();
231 bool focusable() const { return View::focusable(); }
233 void set_can_process_events_within_subtree(bool can_process) {
234 can_process_events_within_subtree_ = can_process;
237 bool CanProcessEventsWithinSubtree() const override {
238 return can_process_events_within_subtree_;
241 void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
242 bool OnMousePressed(const ui::MouseEvent& event) override;
243 bool OnMouseDragged(const ui::MouseEvent& event) override;
244 void OnMouseReleased(const ui::MouseEvent& event) override;
245 void OnMouseEntered(const ui::MouseEvent& event) override;
246 void OnMouseExited(const ui::MouseEvent& event) override;
248 void OnPaint(gfx::Canvas* canvas) override;
249 void SchedulePaintInRect(const gfx::Rect& rect) override;
250 bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
252 void OnNativeThemeChanged(const ui::NativeTheme* native_theme) override;
254 // OnBoundsChanged.
255 bool did_change_bounds_;
256 gfx::Rect new_bounds_;
258 // MouseEvent.
259 int last_mouse_event_type_;
260 gfx::Point location_;
261 bool received_mouse_enter_;
262 bool received_mouse_exit_;
263 bool delete_on_pressed_;
265 // Painting.
266 std::vector<gfx::Rect> scheduled_paint_rects_;
267 bool did_paint_;
269 // Accelerators.
270 std::map<ui::Accelerator, int> accelerator_count_map_;
272 // Native theme.
273 const ui::NativeTheme* native_theme_;
275 // Value to return from CanProcessEventsWithinSubtree().
276 bool can_process_events_within_subtree_;
279 ////////////////////////////////////////////////////////////////////////////////
280 // OnBoundsChanged
281 ////////////////////////////////////////////////////////////////////////////////
283 void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
284 did_change_bounds_ = true;
285 new_bounds_ = bounds();
288 TEST_F(ViewTest, OnBoundsChanged) {
289 TestView v;
291 gfx::Rect prev_rect(0, 0, 200, 200);
292 gfx::Rect new_rect(100, 100, 250, 250);
294 v.SetBoundsRect(prev_rect);
295 v.Reset();
296 v.SetBoundsRect(new_rect);
298 EXPECT_TRUE(v.did_change_bounds_);
299 EXPECT_EQ(v.new_bounds_, new_rect);
300 EXPECT_EQ(v.bounds(), new_rect);
303 ////////////////////////////////////////////////////////////////////////////////
304 // MouseEvent
305 ////////////////////////////////////////////////////////////////////////////////
307 bool TestView::OnMousePressed(const ui::MouseEvent& event) {
308 last_mouse_event_type_ = event.type();
309 location_.SetPoint(event.x(), event.y());
310 if (delete_on_pressed_)
311 delete this;
312 return true;
315 bool TestView::OnMouseDragged(const ui::MouseEvent& event) {
316 last_mouse_event_type_ = event.type();
317 location_.SetPoint(event.x(), event.y());
318 return true;
321 void TestView::OnMouseReleased(const ui::MouseEvent& event) {
322 last_mouse_event_type_ = event.type();
323 location_.SetPoint(event.x(), event.y());
326 void TestView::OnMouseEntered(const ui::MouseEvent& event) {
327 received_mouse_enter_ = true;
330 void TestView::OnMouseExited(const ui::MouseEvent& event) {
331 received_mouse_exit_ = true;
334 TEST_F(ViewTest, MouseEvent) {
335 TestView* v1 = new TestView();
336 v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
338 TestView* v2 = new TestView();
339 v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
341 scoped_ptr<Widget> widget(new Widget);
342 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
343 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
344 params.bounds = gfx::Rect(50, 50, 650, 650);
345 widget->Init(params);
346 internal::RootView* root =
347 static_cast<internal::RootView*>(widget->GetRootView());
349 root->AddChildView(v1);
350 v1->AddChildView(v2);
352 v1->Reset();
353 v2->Reset();
355 gfx::Point p1(110, 120);
356 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
357 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
358 root->OnMousePressed(pressed);
359 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_PRESSED);
360 EXPECT_EQ(v2->location_.x(), 10);
361 EXPECT_EQ(v2->location_.y(), 20);
362 // Make sure v1 did not receive the event
363 EXPECT_EQ(v1->last_mouse_event_type_, 0);
365 // Drag event out of bounds. Should still go to v2
366 v1->Reset();
367 v2->Reset();
368 gfx::Point p2(50, 40);
369 ui::MouseEvent dragged(ui::ET_MOUSE_DRAGGED, p2, p2, ui::EventTimeForNow(),
370 ui::EF_LEFT_MOUSE_BUTTON, 0);
371 root->OnMouseDragged(dragged);
372 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_DRAGGED);
373 EXPECT_EQ(v2->location_.x(), -50);
374 EXPECT_EQ(v2->location_.y(), -60);
375 // Make sure v1 did not receive the event
376 EXPECT_EQ(v1->last_mouse_event_type_, 0);
378 // Releasted event out of bounds. Should still go to v2
379 v1->Reset();
380 v2->Reset();
381 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
382 ui::EventTimeForNow(), 0, 0);
383 root->OnMouseDragged(released);
384 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_RELEASED);
385 EXPECT_EQ(v2->location_.x(), -100);
386 EXPECT_EQ(v2->location_.y(), -100);
387 // Make sure v1 did not receive the event
388 EXPECT_EQ(v1->last_mouse_event_type_, 0);
390 widget->CloseNow();
393 // Confirm that a view can be deleted as part of processing a mouse press.
394 TEST_F(ViewTest, DeleteOnPressed) {
395 TestView* v1 = new TestView();
396 v1->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
398 TestView* v2 = new TestView();
399 v2->SetBoundsRect(gfx::Rect(100, 100, 100, 100));
401 v1->Reset();
402 v2->Reset();
404 scoped_ptr<Widget> widget(new Widget);
405 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
406 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
407 params.bounds = gfx::Rect(50, 50, 650, 650);
408 widget->Init(params);
409 View* root = widget->GetRootView();
411 root->AddChildView(v1);
412 v1->AddChildView(v2);
414 v2->delete_on_pressed_ = true;
415 gfx::Point point(110, 120);
416 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, point, point,
417 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
418 ui::EF_LEFT_MOUSE_BUTTON);
419 root->OnMousePressed(pressed);
420 EXPECT_EQ(0, v1->child_count());
422 widget->CloseNow();
425 ////////////////////////////////////////////////////////////////////////////////
426 // Painting
427 ////////////////////////////////////////////////////////////////////////////////
429 void TestView::OnPaint(gfx::Canvas* canvas) {
430 did_paint_ = true;
433 TEST_F(ViewTest, PaintWithUnknownInvalidation) {
434 Widget* widget = new Widget;
435 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
436 widget->Init(params);
437 View* root_view = widget->GetRootView();
438 root_view->SetBounds(0, 0, 25, 26);
440 TestView* v1 = new TestView;
441 v1->SetBounds(10, 11, 12, 13);
442 root_view->AddChildView(v1);
444 TestView* v2 = new TestView;
445 v2->SetBounds(3, 4, 6, 5);
446 v1->AddChildView(v2);
448 gfx::Canvas canvas(root_view->size(), 1.f, true);
449 gfx::Rect paint_area(1, 1);
451 EXPECT_FALSE(v1->did_paint_);
452 EXPECT_FALSE(v2->did_paint_);
453 root_view->Paint(ui::PaintContext(&canvas));
454 EXPECT_TRUE(v1->did_paint_);
455 EXPECT_TRUE(v2->did_paint_);
457 v1->Reset();
458 v2->Reset();
459 EXPECT_FALSE(v1->did_paint_);
460 EXPECT_FALSE(v2->did_paint_);
461 root_view->Paint(
462 ui::PaintContext(&canvas, paint_area).CloneWithoutInvalidation());
463 EXPECT_TRUE(v1->did_paint_);
464 EXPECT_TRUE(v2->did_paint_);
467 TEST_F(ViewTest, PaintContainsChildren) {
468 Widget* widget = new Widget;
469 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
470 widget->Init(params);
471 View* root_view = widget->GetRootView();
472 root_view->SetBounds(0, 0, 25, 26);
474 TestView* v1 = new TestView;
475 v1->SetBounds(10, 11, 12, 13);
476 root_view->AddChildView(v1);
478 TestView* v2 = new TestView;
479 v2->SetBounds(3, 4, 6, 5);
480 v1->AddChildView(v2);
482 gfx::Canvas canvas(root_view->size(), 1.f, true);
483 gfx::Rect paint_area(25, 26);
485 EXPECT_FALSE(v1->did_paint_);
486 EXPECT_FALSE(v2->did_paint_);
487 root_view->Paint(ui::PaintContext(&canvas, paint_area));
488 EXPECT_TRUE(v1->did_paint_);
489 EXPECT_TRUE(v2->did_paint_);
492 TEST_F(ViewTest, PaintContainsChildrenInRTL) {
493 ScopedRTL rtl;
495 Widget* widget = new Widget;
496 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
497 widget->Init(params);
498 View* root_view = widget->GetRootView();
499 root_view->SetBounds(0, 0, 25, 26);
501 TestView* v1 = new TestView;
502 v1->SetBounds(10, 11, 12, 13);
503 root_view->AddChildView(v1);
505 TestView* v2 = new TestView;
506 v2->SetBounds(3, 4, 6, 5);
507 v1->AddChildView(v2);
509 // Verify where the layers actually appear.
510 v1->SetPaintToLayer(true);
511 // x: 25 - 10(x) - 12(width) = 3
512 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
513 v1->SetPaintToLayer(false);
515 v2->SetPaintToLayer(true);
516 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
517 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
518 v2->SetPaintToLayer(false);
520 gfx::Canvas canvas(root_view->size(), 1.f, true);
521 gfx::Rect paint_area(25, 26);
523 EXPECT_FALSE(v1->did_paint_);
524 EXPECT_FALSE(v2->did_paint_);
525 root_view->Paint(ui::PaintContext(&canvas, paint_area));
526 EXPECT_TRUE(v1->did_paint_);
527 EXPECT_TRUE(v2->did_paint_);
530 TEST_F(ViewTest, PaintIntersectsChildren) {
531 Widget* widget = new Widget;
532 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
533 widget->Init(params);
534 View* root_view = widget->GetRootView();
535 root_view->SetBounds(0, 0, 25, 26);
537 TestView* v1 = new TestView;
538 v1->SetBounds(10, 11, 12, 13);
539 root_view->AddChildView(v1);
541 TestView* v2 = new TestView;
542 v2->SetBounds(3, 4, 6, 5);
543 v1->AddChildView(v2);
545 gfx::Canvas canvas(root_view->size(), 1.f, true);
546 gfx::Rect paint_area(9, 10, 5, 6);
548 EXPECT_FALSE(v1->did_paint_);
549 EXPECT_FALSE(v2->did_paint_);
550 root_view->Paint(ui::PaintContext(&canvas, paint_area));
551 EXPECT_TRUE(v1->did_paint_);
552 EXPECT_TRUE(v2->did_paint_);
555 TEST_F(ViewTest, PaintIntersectsChildrenInRTL) {
556 ScopedRTL rtl;
558 Widget* widget = new Widget;
559 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
560 widget->Init(params);
561 View* root_view = widget->GetRootView();
562 root_view->SetBounds(0, 0, 25, 26);
564 TestView* v1 = new TestView;
565 v1->SetBounds(10, 11, 12, 13);
566 root_view->AddChildView(v1);
568 TestView* v2 = new TestView;
569 v2->SetBounds(3, 4, 6, 5);
570 v1->AddChildView(v2);
572 // Verify where the layers actually appear.
573 v1->SetPaintToLayer(true);
574 // x: 25 - 10(x) - 12(width) = 3
575 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
576 v1->SetPaintToLayer(false);
578 v2->SetPaintToLayer(true);
579 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
580 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
581 v2->SetPaintToLayer(false);
583 gfx::Canvas canvas(root_view->size(), 1.f, true);
584 gfx::Rect paint_area(2, 10, 5, 6);
586 EXPECT_FALSE(v1->did_paint_);
587 EXPECT_FALSE(v2->did_paint_);
588 root_view->Paint(ui::PaintContext(&canvas, paint_area));
589 EXPECT_TRUE(v1->did_paint_);
590 EXPECT_TRUE(v2->did_paint_);
593 TEST_F(ViewTest, PaintIntersectsChildButNotGrandChild) {
594 Widget* widget = new Widget;
595 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
596 widget->Init(params);
597 View* root_view = widget->GetRootView();
598 root_view->SetBounds(0, 0, 25, 26);
600 TestView* v1 = new TestView;
601 v1->SetBounds(10, 11, 12, 13);
602 root_view->AddChildView(v1);
604 TestView* v2 = new TestView;
605 v2->SetBounds(3, 4, 6, 5);
606 v1->AddChildView(v2);
608 gfx::Canvas canvas(root_view->size(), 1.f, true);
609 gfx::Rect paint_area(9, 10, 2, 3);
611 EXPECT_FALSE(v1->did_paint_);
612 EXPECT_FALSE(v2->did_paint_);
613 root_view->Paint(ui::PaintContext(&canvas, paint_area));
614 EXPECT_TRUE(v1->did_paint_);
615 EXPECT_FALSE(v2->did_paint_);
618 TEST_F(ViewTest, PaintIntersectsChildButNotGrandChildInRTL) {
619 ScopedRTL rtl;
621 Widget* widget = new Widget;
622 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
623 widget->Init(params);
624 View* root_view = widget->GetRootView();
625 root_view->SetBounds(0, 0, 25, 26);
627 TestView* v1 = new TestView;
628 v1->SetBounds(10, 11, 12, 13);
629 root_view->AddChildView(v1);
631 TestView* v2 = new TestView;
632 v2->SetBounds(3, 4, 6, 5);
633 v1->AddChildView(v2);
635 // Verify where the layers actually appear.
636 v1->SetPaintToLayer(true);
637 // x: 25 - 10(x) - 12(width) = 3
638 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
639 v1->SetPaintToLayer(false);
641 v2->SetPaintToLayer(true);
642 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
643 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
644 v2->SetPaintToLayer(false);
646 gfx::Canvas canvas(root_view->size(), 1.f, true);
647 gfx::Rect paint_area(2, 10, 2, 3);
649 EXPECT_FALSE(v1->did_paint_);
650 EXPECT_FALSE(v2->did_paint_);
651 root_view->Paint(ui::PaintContext(&canvas, paint_area));
652 EXPECT_TRUE(v1->did_paint_);
653 EXPECT_FALSE(v2->did_paint_);
656 TEST_F(ViewTest, PaintIntersectsNoChildren) {
657 Widget* widget = new Widget;
658 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
659 widget->Init(params);
660 View* root_view = widget->GetRootView();
661 root_view->SetBounds(0, 0, 25, 26);
663 TestView* v1 = new TestView;
664 v1->SetBounds(10, 11, 12, 13);
665 root_view->AddChildView(v1);
667 TestView* v2 = new TestView;
668 v2->SetBounds(3, 4, 6, 5);
669 v1->AddChildView(v2);
671 gfx::Canvas canvas(root_view->size(), 1.f, true);
672 gfx::Rect paint_area(9, 10, 2, 1);
674 EXPECT_FALSE(v1->did_paint_);
675 EXPECT_FALSE(v2->did_paint_);
676 root_view->Paint(ui::PaintContext(&canvas, paint_area));
677 EXPECT_FALSE(v1->did_paint_);
678 EXPECT_FALSE(v2->did_paint_);
681 TEST_F(ViewTest, PaintIntersectsNoChildrenInRTL) {
682 ScopedRTL rtl;
684 Widget* widget = new Widget;
685 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
686 widget->Init(params);
687 View* root_view = widget->GetRootView();
688 root_view->SetBounds(0, 0, 25, 26);
690 TestView* v1 = new TestView;
691 v1->SetBounds(10, 11, 12, 13);
692 root_view->AddChildView(v1);
694 TestView* v2 = new TestView;
695 v2->SetBounds(3, 4, 6, 5);
696 v1->AddChildView(v2);
698 // Verify where the layers actually appear.
699 v1->SetPaintToLayer(true);
700 // x: 25 - 10(x) - 12(width) = 3
701 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
702 v1->SetPaintToLayer(false);
704 v2->SetPaintToLayer(true);
705 // x: 25 - 10(parent x) - 3(x) - 6(width) = 6
706 EXPECT_EQ(gfx::Rect(6, 15, 6, 5), v2->layer()->bounds());
707 v2->SetPaintToLayer(false);
709 gfx::Canvas canvas(root_view->size(), 1.f, true);
710 gfx::Rect paint_area(2, 10, 2, 1);
712 EXPECT_FALSE(v1->did_paint_);
713 EXPECT_FALSE(v2->did_paint_);
714 root_view->Paint(ui::PaintContext(&canvas, paint_area));
715 EXPECT_FALSE(v1->did_paint_);
716 EXPECT_FALSE(v2->did_paint_);
719 TEST_F(ViewTest, PaintIntersectsOneChild) {
720 Widget* widget = new Widget;
721 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
722 widget->Init(params);
723 View* root_view = widget->GetRootView();
724 root_view->SetBounds(0, 0, 25, 26);
726 TestView* v1 = new TestView;
727 v1->SetBounds(10, 11, 12, 13);
728 root_view->AddChildView(v1);
730 TestView* v2 = new TestView;
731 v2->SetBounds(3, 4, 6, 5);
732 root_view->AddChildView(v2);
734 gfx::Canvas canvas(root_view->size(), 1.f, true);
735 // Intersects with the second child only.
736 gfx::Rect paint_area(3, 3, 1, 2);
738 EXPECT_FALSE(v1->did_paint_);
739 EXPECT_FALSE(v2->did_paint_);
740 root_view->Paint(ui::PaintContext(&canvas, paint_area));
741 EXPECT_FALSE(v1->did_paint_);
742 EXPECT_TRUE(v2->did_paint_);
744 // Intersects with the first child only.
745 paint_area = gfx::Rect(20, 10, 1, 2);
747 v1->Reset();
748 v2->Reset();
749 EXPECT_FALSE(v1->did_paint_);
750 EXPECT_FALSE(v2->did_paint_);
751 root_view->Paint(ui::PaintContext(&canvas, paint_area));
752 EXPECT_TRUE(v1->did_paint_);
753 EXPECT_FALSE(v2->did_paint_);
756 TEST_F(ViewTest, PaintIntersectsOneChildInRTL) {
757 ScopedRTL rtl;
759 Widget* widget = new Widget;
760 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
761 widget->Init(params);
762 View* root_view = widget->GetRootView();
763 root_view->SetBounds(0, 0, 25, 26);
765 TestView* v1 = new TestView;
766 v1->SetBounds(10, 11, 12, 13);
767 root_view->AddChildView(v1);
769 TestView* v2 = new TestView;
770 v2->SetBounds(3, 4, 6, 5);
771 root_view->AddChildView(v2);
773 // Verify where the layers actually appear.
774 v1->SetPaintToLayer(true);
775 // x: 25 - 10(x) - 12(width) = 3
776 EXPECT_EQ(gfx::Rect(3, 11, 12, 13), v1->layer()->bounds());
777 v1->SetPaintToLayer(false);
779 v2->SetPaintToLayer(true);
780 // x: 25 - 3(x) - 6(width) = 16
781 EXPECT_EQ(gfx::Rect(16, 4, 6, 5), v2->layer()->bounds());
782 v2->SetPaintToLayer(false);
784 gfx::Canvas canvas(root_view->size(), 1.f, true);
785 // Intersects with the first child only.
786 gfx::Rect paint_area(3, 10, 1, 2);
788 EXPECT_FALSE(v1->did_paint_);
789 EXPECT_FALSE(v2->did_paint_);
790 root_view->Paint(ui::PaintContext(&canvas, paint_area));
791 EXPECT_TRUE(v1->did_paint_);
792 EXPECT_FALSE(v2->did_paint_);
794 // Intersects with the second child only.
795 paint_area = gfx::Rect(21, 3, 1, 2);
797 v1->Reset();
798 v2->Reset();
799 EXPECT_FALSE(v1->did_paint_);
800 EXPECT_FALSE(v2->did_paint_);
801 root_view->Paint(ui::PaintContext(&canvas, paint_area));
802 EXPECT_FALSE(v1->did_paint_);
803 EXPECT_TRUE(v2->did_paint_);
806 TEST_F(ViewTest, PaintInPromotedToLayer) {
807 Widget* widget = new Widget;
808 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
809 widget->Init(params);
810 View* root_view = widget->GetRootView();
811 root_view->SetBounds(0, 0, 25, 26);
813 TestView* v1 = new TestView;
814 v1->SetPaintToLayer(true);
815 v1->SetBounds(10, 11, 12, 13);
816 root_view->AddChildView(v1);
818 TestView* v2 = new TestView;
819 v2->SetBounds(3, 4, 6, 5);
820 v1->AddChildView(v2);
822 EXPECT_FALSE(v1->did_paint_);
823 EXPECT_FALSE(v2->did_paint_);
826 gfx::Canvas canvas(root_view->size(), 1.f, true);
827 gfx::Rect paint_area(25, 26);
829 // The promoted views are not painted as they are separate paint roots.
830 root_view->Paint(ui::PaintContext(&canvas, paint_area));
831 EXPECT_FALSE(v1->did_paint_);
832 EXPECT_FALSE(v2->did_paint_);
836 gfx::Canvas canvas(v1->size(), 1.f, true);
837 gfx::Rect paint_area(1, 1);
839 // The |v1| view is painted. If it used its offset incorrect, it would think
840 // its at (10,11) instead of at (0,0) since it is the paint root.
841 v1->Paint(ui::PaintContext(&canvas, paint_area));
842 EXPECT_TRUE(v1->did_paint_);
843 EXPECT_FALSE(v2->did_paint_);
846 v1->Reset();
849 gfx::Canvas canvas(v1->size(), 1.f, true);
850 gfx::Rect paint_area(3, 3, 1, 2);
852 // The |v2| view is painted also. If it used its offset incorrect, it would
853 // think its at (13,15) instead of at (3,4) since |v1| is the paint root.
854 v1->Paint(ui::PaintContext(&canvas, paint_area));
855 EXPECT_TRUE(v1->did_paint_);
856 EXPECT_TRUE(v2->did_paint_);
860 void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
861 scheduled_paint_rects_.push_back(rect);
862 View::SchedulePaintInRect(rect);
865 TEST_F(ViewTest, RemoveNotification) {
866 ViewStorage* vs = ViewStorage::GetInstance();
867 Widget* widget = new Widget;
868 widget->Init(CreateParams(Widget::InitParams::TYPE_POPUP));
869 View* root_view = widget->GetRootView();
871 View* v1 = new View;
872 int s1 = vs->CreateStorageID();
873 vs->StoreView(s1, v1);
874 root_view->AddChildView(v1);
875 View* v11 = new View;
876 int s11 = vs->CreateStorageID();
877 vs->StoreView(s11, v11);
878 v1->AddChildView(v11);
879 View* v111 = new View;
880 int s111 = vs->CreateStorageID();
881 vs->StoreView(s111, v111);
882 v11->AddChildView(v111);
883 View* v112 = new View;
884 int s112 = vs->CreateStorageID();
885 vs->StoreView(s112, v112);
886 v11->AddChildView(v112);
887 View* v113 = new View;
888 int s113 = vs->CreateStorageID();
889 vs->StoreView(s113, v113);
890 v11->AddChildView(v113);
891 View* v1131 = new View;
892 int s1131 = vs->CreateStorageID();
893 vs->StoreView(s1131, v1131);
894 v113->AddChildView(v1131);
895 View* v12 = new View;
896 int s12 = vs->CreateStorageID();
897 vs->StoreView(s12, v12);
898 v1->AddChildView(v12);
900 View* v2 = new View;
901 int s2 = vs->CreateStorageID();
902 vs->StoreView(s2, v2);
903 root_view->AddChildView(v2);
904 View* v21 = new View;
905 int s21 = vs->CreateStorageID();
906 vs->StoreView(s21, v21);
907 v2->AddChildView(v21);
908 View* v211 = new View;
909 int s211 = vs->CreateStorageID();
910 vs->StoreView(s211, v211);
911 v21->AddChildView(v211);
913 size_t stored_views = vs->view_count();
915 // Try removing a leaf view.
916 v21->RemoveChildView(v211);
917 EXPECT_EQ(stored_views - 1, vs->view_count());
918 EXPECT_EQ(NULL, vs->RetrieveView(s211));
919 delete v211; // We won't use this one anymore.
921 // Now try removing a view with a hierarchy of depth 1.
922 v11->RemoveChildView(v113);
923 EXPECT_EQ(stored_views - 3, vs->view_count());
924 EXPECT_EQ(NULL, vs->RetrieveView(s113));
925 EXPECT_EQ(NULL, vs->RetrieveView(s1131));
926 delete v113; // We won't use this one anymore.
928 // Now remove even more.
929 root_view->RemoveChildView(v1);
930 EXPECT_EQ(NULL, vs->RetrieveView(s1));
931 EXPECT_EQ(NULL, vs->RetrieveView(s11));
932 EXPECT_EQ(NULL, vs->RetrieveView(s12));
933 EXPECT_EQ(NULL, vs->RetrieveView(s111));
934 EXPECT_EQ(NULL, vs->RetrieveView(s112));
936 // Put v1 back for more tests.
937 root_view->AddChildView(v1);
938 vs->StoreView(s1, v1);
940 // Synchronously closing the window deletes the view hierarchy, which should
941 // remove all its views from ViewStorage.
942 widget->CloseNow();
943 EXPECT_EQ(stored_views - 10, vs->view_count());
944 EXPECT_EQ(NULL, vs->RetrieveView(s1));
945 EXPECT_EQ(NULL, vs->RetrieveView(s12));
946 EXPECT_EQ(NULL, vs->RetrieveView(s11));
947 EXPECT_EQ(NULL, vs->RetrieveView(s12));
948 EXPECT_EQ(NULL, vs->RetrieveView(s21));
949 EXPECT_EQ(NULL, vs->RetrieveView(s111));
950 EXPECT_EQ(NULL, vs->RetrieveView(s112));
953 namespace {
955 void RotateCounterclockwise(gfx::Transform* transform) {
956 transform->matrix().set3x3(0, -1, 0,
957 1, 0, 0,
958 0, 0, 1);
961 void RotateClockwise(gfx::Transform* transform) {
962 transform->matrix().set3x3( 0, 1, 0,
963 -1, 0, 0,
964 0, 0, 1);
967 } // namespace
969 // Tests the correctness of the rect-based targeting algorithm implemented in
970 // View::GetEventHandlerForRect(). See http://goo.gl/3Jp2BD for a description
971 // of rect-based targeting.
972 TEST_F(ViewTest, GetEventHandlerForRect) {
973 Widget* widget = new Widget;
974 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
975 widget->Init(params);
976 View* root_view = widget->GetRootView();
977 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
979 // Have this hierarchy of views (the coordinates here are all in
980 // the root view's coordinate space):
981 // v1 (0, 0, 100, 100)
982 // v2 (150, 0, 250, 100)
983 // v3 (0, 200, 150, 100)
984 // v31 (10, 210, 80, 80)
985 // v32 (110, 210, 30, 80)
986 // v4 (300, 200, 100, 100)
987 // v41 (310, 210, 80, 80)
988 // v411 (370, 275, 10, 5)
989 // v5 (450, 197, 30, 36)
990 // v51 (450, 200, 30, 30)
992 // The coordinates used for SetBounds are in parent coordinates.
994 TestView* v1 = new TestView;
995 v1->SetBounds(0, 0, 100, 100);
996 root_view->AddChildView(v1);
998 TestView* v2 = new TestView;
999 v2->SetBounds(150, 0, 250, 100);
1000 root_view->AddChildView(v2);
1002 TestView* v3 = new TestView;
1003 v3->SetBounds(0, 200, 150, 100);
1004 root_view->AddChildView(v3);
1006 TestView* v4 = new TestView;
1007 v4->SetBounds(300, 200, 100, 100);
1008 root_view->AddChildView(v4);
1010 TestView* v31 = new TestView;
1011 v31->SetBounds(10, 10, 80, 80);
1012 v3->AddChildView(v31);
1014 TestView* v32 = new TestView;
1015 v32->SetBounds(110, 10, 30, 80);
1016 v3->AddChildView(v32);
1018 TestView* v41 = new TestView;
1019 v41->SetBounds(10, 10, 80, 80);
1020 v4->AddChildView(v41);
1022 TestView* v411 = new TestView;
1023 v411->SetBounds(60, 65, 10, 5);
1024 v41->AddChildView(v411);
1026 TestView* v5 = new TestView;
1027 v5->SetBounds(450, 197, 30, 36);
1028 root_view->AddChildView(v5);
1030 TestView* v51 = new TestView;
1031 v51->SetBounds(0, 3, 30, 30);
1032 v5->AddChildView(v51);
1034 // |touch_rect| does not intersect any descendant view of |root_view|.
1035 gfx::Rect touch_rect(105, 105, 30, 45);
1036 View* result_view = root_view->GetEventHandlerForRect(touch_rect);
1037 EXPECT_EQ(root_view, result_view);
1038 result_view = NULL;
1040 // Covers |v1| by at least 60%.
1041 touch_rect.SetRect(15, 15, 100, 100);
1042 result_view = root_view->GetEventHandlerForRect(touch_rect);
1043 EXPECT_EQ(v1, result_view);
1044 result_view = NULL;
1046 // Intersects |v1| but does not cover it by at least 60%. The center
1047 // of |touch_rect| is within |v1|.
1048 touch_rect.SetRect(50, 50, 5, 10);
1049 result_view = root_view->GetEventHandlerForRect(touch_rect);
1050 EXPECT_EQ(v1, result_view);
1051 result_view = NULL;
1053 // Intersects |v1| but does not cover it by at least 60%. The center
1054 // of |touch_rect| is not within |v1|.
1055 touch_rect.SetRect(95, 96, 21, 22);
1056 result_view = root_view->GetEventHandlerForRect(touch_rect);
1057 EXPECT_EQ(root_view, result_view);
1058 result_view = NULL;
1060 // Intersects |v1| and |v2|, but only covers |v2| by at least 60%.
1061 touch_rect.SetRect(95, 10, 300, 120);
1062 result_view = root_view->GetEventHandlerForRect(touch_rect);
1063 EXPECT_EQ(v2, result_view);
1064 result_view = NULL;
1066 // Covers both |v1| and |v2| by at least 60%, but the center point
1067 // of |touch_rect| is closer to the center point of |v2|.
1068 touch_rect.SetRect(20, 20, 400, 100);
1069 result_view = root_view->GetEventHandlerForRect(touch_rect);
1070 EXPECT_EQ(v2, result_view);
1071 result_view = NULL;
1073 // Covers both |v1| and |v2| by at least 60%, but the center point
1074 // of |touch_rect| is closer to the center point of |v1|.
1075 touch_rect.SetRect(-700, -15, 1050, 110);
1076 result_view = root_view->GetEventHandlerForRect(touch_rect);
1077 EXPECT_EQ(v1, result_view);
1078 result_view = NULL;
1080 // A mouse click within |v1| will target |v1|.
1081 touch_rect.SetRect(15, 15, 1, 1);
1082 result_view = root_view->GetEventHandlerForRect(touch_rect);
1083 EXPECT_EQ(v1, result_view);
1084 result_view = NULL;
1086 // Intersects |v3| and |v31| by at least 60% and the center point
1087 // of |touch_rect| is closer to the center point of |v31|.
1088 touch_rect.SetRect(0, 200, 110, 100);
1089 result_view = root_view->GetEventHandlerForRect(touch_rect);
1090 EXPECT_EQ(v31, result_view);
1091 result_view = NULL;
1093 // Intersects |v3| and |v31|, but neither by at least 60%. The
1094 // center point of |touch_rect| lies within |v31|.
1095 touch_rect.SetRect(80, 280, 15, 15);
1096 result_view = root_view->GetEventHandlerForRect(touch_rect);
1097 EXPECT_EQ(v31, result_view);
1098 result_view = NULL;
1100 // Covers |v3|, |v31|, and |v32| all by at least 60%, and the
1101 // center point of |touch_rect| is closest to the center point
1102 // of |v32|.
1103 touch_rect.SetRect(0, 200, 200, 100);
1104 result_view = root_view->GetEventHandlerForRect(touch_rect);
1105 EXPECT_EQ(v32, result_view);
1106 result_view = NULL;
1108 // Intersects all of |v3|, |v31|, and |v32|, but only covers
1109 // |v31| and |v32| by at least 60%. The center point of
1110 // |touch_rect| is closest to the center point of |v32|.
1111 touch_rect.SetRect(30, 225, 180, 115);
1112 result_view = root_view->GetEventHandlerForRect(touch_rect);
1113 EXPECT_EQ(v32, result_view);
1114 result_view = NULL;
1116 // A mouse click at the corner of |v3| will target |v3|.
1117 touch_rect.SetRect(0, 200, 1, 1);
1118 result_view = root_view->GetEventHandlerForRect(touch_rect);
1119 EXPECT_EQ(v3, result_view);
1120 result_view = NULL;
1122 // A mouse click within |v32| will target |v32|.
1123 touch_rect.SetRect(112, 211, 1, 1);
1124 result_view = root_view->GetEventHandlerForRect(touch_rect);
1125 EXPECT_EQ(v32, result_view);
1126 result_view = NULL;
1128 // Covers all of |v4|, |v41|, and |v411| by at least 60%.
1129 // The center point of |touch_rect| is equally close to
1130 // the center points of |v4| and |v41|.
1131 touch_rect.SetRect(310, 210, 80, 80);
1132 result_view = root_view->GetEventHandlerForRect(touch_rect);
1133 EXPECT_EQ(v41, result_view);
1134 result_view = NULL;
1136 // Intersects all of |v4|, |v41|, and |v411| but only covers
1137 // |v411| by at least 60%.
1138 touch_rect.SetRect(370, 275, 7, 5);
1139 result_view = root_view->GetEventHandlerForRect(touch_rect);
1140 EXPECT_EQ(v411, result_view);
1141 result_view = NULL;
1143 // Intersects |v4| and |v41| but covers neither by at least 60%.
1144 // The center point of |touch_rect| is equally close to the center
1145 // points of |v4| and |v41|.
1146 touch_rect.SetRect(345, 245, 7, 7);
1147 result_view = root_view->GetEventHandlerForRect(touch_rect);
1148 EXPECT_EQ(v41, result_view);
1149 result_view = NULL;
1151 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1152 // them by at least 60%. The center point of |touch_rect| lies
1153 // within |v411|.
1154 touch_rect.SetRect(368, 272, 4, 6);
1155 result_view = root_view->GetEventHandlerForRect(touch_rect);
1156 EXPECT_EQ(v411, result_view);
1157 result_view = NULL;
1159 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1160 // them by at least 60%. The center point of |touch_rect| lies
1161 // within |v41|.
1162 touch_rect.SetRect(365, 270, 7, 7);
1163 result_view = root_view->GetEventHandlerForRect(touch_rect);
1164 EXPECT_EQ(v41, result_view);
1165 result_view = NULL;
1167 // Intersects all of |v4|, |v41|, and |v411| and covers none of
1168 // them by at least 60%. The center point of |touch_rect| lies
1169 // within |v4|.
1170 touch_rect.SetRect(205, 275, 200, 2);
1171 result_view = root_view->GetEventHandlerForRect(touch_rect);
1172 EXPECT_EQ(v4, result_view);
1173 result_view = NULL;
1175 // Intersects all of |v4|, |v41|, and |v411| but only covers
1176 // |v41| by at least 60%.
1177 touch_rect.SetRect(310, 210, 61, 66);
1178 result_view = root_view->GetEventHandlerForRect(touch_rect);
1179 EXPECT_EQ(v41, result_view);
1180 result_view = NULL;
1182 // A mouse click within |v411| will target |v411|.
1183 touch_rect.SetRect(372, 275, 1, 1);
1184 result_view = root_view->GetEventHandlerForRect(touch_rect);
1185 EXPECT_EQ(v411, result_view);
1186 result_view = NULL;
1188 // A mouse click within |v41| will target |v41|.
1189 touch_rect.SetRect(350, 215, 1, 1);
1190 result_view = root_view->GetEventHandlerForRect(touch_rect);
1191 EXPECT_EQ(v41, result_view);
1192 result_view = NULL;
1194 // Covers |v3|, |v4|, and all of their descendants by at
1195 // least 60%. The center point of |touch_rect| is closest
1196 // to the center point of |v32|.
1197 touch_rect.SetRect(0, 200, 400, 100);
1198 result_view = root_view->GetEventHandlerForRect(touch_rect);
1199 EXPECT_EQ(v32, result_view);
1200 result_view = NULL;
1202 // Intersects all of |v2|, |v3|, |v32|, |v4|, |v41|, and |v411|.
1203 // Covers |v2|, |v32|, |v4|, |v41|, and |v411| by at least 60%.
1204 // The center point of |touch_rect| is closest to the center
1205 // point of |root_view|.
1206 touch_rect.SetRect(110, 15, 375, 450);
1207 result_view = root_view->GetEventHandlerForRect(touch_rect);
1208 EXPECT_EQ(root_view, result_view);
1209 result_view = NULL;
1211 // Covers all views (except |v5| and |v51|) by at least 60%. The
1212 // center point of |touch_rect| is equally close to the center
1213 // points of |v2| and |v32|. One is not a descendant of the other,
1214 // so in this case the view selected is arbitrary (i.e.,
1215 // it depends only on the ordering of nodes in the views
1216 // hierarchy).
1217 touch_rect.SetRect(0, 0, 400, 300);
1218 result_view = root_view->GetEventHandlerForRect(touch_rect);
1219 EXPECT_EQ(v32, result_view);
1220 result_view = NULL;
1222 // Covers |v5| and |v51| by at least 60%, and the center point of
1223 // the touch is located within both views. Since both views share
1224 // the same center point, the child view should be selected.
1225 touch_rect.SetRect(440, 190, 40, 40);
1226 result_view = root_view->GetEventHandlerForRect(touch_rect);
1227 EXPECT_EQ(v51, result_view);
1228 result_view = NULL;
1230 // Covers |v5| and |v51| by at least 60%, but the center point of
1231 // the touch is not located within either view. Since both views
1232 // share the same center point, the child view should be selected.
1233 touch_rect.SetRect(455, 187, 60, 60);
1234 result_view = root_view->GetEventHandlerForRect(touch_rect);
1235 EXPECT_EQ(v51, result_view);
1236 result_view = NULL;
1238 // Covers neither |v5| nor |v51| by at least 60%, but the center
1239 // of the touch is located within |v51|.
1240 touch_rect.SetRect(450, 197, 10, 10);
1241 result_view = root_view->GetEventHandlerForRect(touch_rect);
1242 EXPECT_EQ(v51, result_view);
1243 result_view = NULL;
1245 // Covers neither |v5| nor |v51| by at least 60% but intersects both.
1246 // The center point is located outside of both views.
1247 touch_rect.SetRect(433, 180, 24, 24);
1248 result_view = root_view->GetEventHandlerForRect(touch_rect);
1249 EXPECT_EQ(root_view, result_view);
1250 result_view = NULL;
1252 // Only intersects |v5| but does not cover it by at least 60%. The
1253 // center point of the touch region is located within |v5|.
1254 touch_rect.SetRect(449, 196, 3, 3);
1255 result_view = root_view->GetEventHandlerForRect(touch_rect);
1256 EXPECT_EQ(v5, result_view);
1257 result_view = NULL;
1259 // A mouse click within |v5| (but not |v51|) should target |v5|.
1260 touch_rect.SetRect(462, 199, 1, 1);
1261 result_view = root_view->GetEventHandlerForRect(touch_rect);
1262 EXPECT_EQ(v5, result_view);
1263 result_view = NULL;
1265 // A mouse click |v5| and |v51| should target the child view.
1266 touch_rect.SetRect(452, 226, 1, 1);
1267 result_view = root_view->GetEventHandlerForRect(touch_rect);
1268 EXPECT_EQ(v51, result_view);
1269 result_view = NULL;
1271 // A mouse click on the center of |v5| and |v51| should target
1272 // the child view.
1273 touch_rect.SetRect(465, 215, 1, 1);
1274 result_view = root_view->GetEventHandlerForRect(touch_rect);
1275 EXPECT_EQ(v51, result_view);
1276 result_view = NULL;
1278 widget->CloseNow();
1281 // Tests that GetEventHandlerForRect() and GetTooltipHandlerForPoint() behave
1282 // as expected when different views in the view hierarchy return false
1283 // when CanProcessEventsWithinSubtree() is called.
1284 TEST_F(ViewTest, CanProcessEventsWithinSubtree) {
1285 Widget* widget = new Widget;
1286 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1287 widget->Init(params);
1288 View* root_view = widget->GetRootView();
1289 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1291 // Have this hierarchy of views (the coords here are in the coordinate
1292 // space of the root view):
1293 // v (0, 0, 100, 100)
1294 // - v_child (0, 0, 20, 30)
1295 // - v_grandchild (5, 5, 5, 15)
1297 TestView* v = new TestView;
1298 v->SetBounds(0, 0, 100, 100);
1299 root_view->AddChildView(v);
1300 v->set_notify_enter_exit_on_child(true);
1302 TestView* v_child = new TestView;
1303 v_child->SetBounds(0, 0, 20, 30);
1304 v->AddChildView(v_child);
1306 TestView* v_grandchild = new TestView;
1307 v_grandchild->SetBounds(5, 5, 5, 15);
1308 v_child->AddChildView(v_grandchild);
1310 v->Reset();
1311 v_child->Reset();
1312 v_grandchild->Reset();
1314 // Define rects and points within the views in the hierarchy.
1315 gfx::Rect rect_in_v_grandchild(7, 7, 3, 3);
1316 gfx::Point point_in_v_grandchild(rect_in_v_grandchild.origin());
1317 gfx::Rect rect_in_v_child(12, 3, 5, 5);
1318 gfx::Point point_in_v_child(rect_in_v_child.origin());
1319 gfx::Rect rect_in_v(50, 50, 25, 30);
1320 gfx::Point point_in_v(rect_in_v.origin());
1322 // When all three views return true when CanProcessEventsWithinSubtree()
1323 // is called, targeting should behave as expected.
1325 View* result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1326 EXPECT_EQ(v_grandchild, result_view);
1327 result_view = NULL;
1328 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1329 EXPECT_EQ(v_grandchild, result_view);
1330 result_view = NULL;
1332 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1333 EXPECT_EQ(v_child, result_view);
1334 result_view = NULL;
1335 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1336 EXPECT_EQ(v_child, result_view);
1337 result_view = NULL;
1339 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1340 EXPECT_EQ(v, result_view);
1341 result_view = NULL;
1342 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1343 EXPECT_EQ(v, result_view);
1344 result_view = NULL;
1346 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1347 // is called, then |v_grandchild| cannot be returned as a target.
1349 v_grandchild->set_can_process_events_within_subtree(false);
1351 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1352 EXPECT_EQ(v_child, result_view);
1353 result_view = NULL;
1354 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1355 EXPECT_EQ(v_child, result_view);
1356 result_view = NULL;
1358 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1359 EXPECT_EQ(v_child, result_view);
1360 result_view = NULL;
1361 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1362 EXPECT_EQ(v_child, result_view);
1363 result_view = NULL;
1365 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1366 EXPECT_EQ(v, result_view);
1367 result_view = NULL;
1368 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1369 EXPECT_EQ(v, result_view);
1371 // When |v_grandchild| returns false when CanProcessEventsWithinSubtree()
1372 // is called, then NULL should be returned as a target if we call
1373 // GetTooltipHandlerForPoint() with |v_grandchild| as the root of the
1374 // views tree. Note that the location must be in the coordinate space
1375 // of the root view (|v_grandchild| in this case), so use (1, 1).
1377 result_view = v_grandchild;
1378 result_view = v_grandchild->GetTooltipHandlerForPoint(gfx::Point(1, 1));
1379 EXPECT_EQ(NULL, result_view);
1380 result_view = NULL;
1382 // When |v_child| returns false when CanProcessEventsWithinSubtree()
1383 // is called, then neither |v_child| nor |v_grandchild| can be returned
1384 // as a target (|v| should be returned as the target for each case).
1386 v_grandchild->Reset();
1387 v_child->set_can_process_events_within_subtree(false);
1389 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1390 EXPECT_EQ(v, result_view);
1391 result_view = NULL;
1392 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1393 EXPECT_EQ(v, result_view);
1394 result_view = NULL;
1396 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1397 EXPECT_EQ(v, result_view);
1398 result_view = NULL;
1399 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1400 EXPECT_EQ(v, result_view);
1401 result_view = NULL;
1403 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1404 EXPECT_EQ(v, result_view);
1405 result_view = NULL;
1406 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1407 EXPECT_EQ(v, result_view);
1408 result_view = NULL;
1410 // When |v| returns false when CanProcessEventsWithinSubtree()
1411 // is called, then none of |v|, |v_child|, and |v_grandchild| can be returned
1412 // as a target (|root_view| should be returned as the target for each case).
1414 v_child->Reset();
1415 v->set_can_process_events_within_subtree(false);
1417 result_view = root_view->GetEventHandlerForRect(rect_in_v_grandchild);
1418 EXPECT_EQ(root_view, result_view);
1419 result_view = NULL;
1420 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_grandchild);
1421 EXPECT_EQ(root_view, result_view);
1422 result_view = NULL;
1424 result_view = root_view->GetEventHandlerForRect(rect_in_v_child);
1425 EXPECT_EQ(root_view, result_view);
1426 result_view = NULL;
1427 result_view = root_view->GetTooltipHandlerForPoint(point_in_v_child);
1428 EXPECT_EQ(root_view, result_view);
1429 result_view = NULL;
1431 result_view = root_view->GetEventHandlerForRect(rect_in_v);
1432 EXPECT_EQ(root_view, result_view);
1433 result_view = NULL;
1434 result_view = root_view->GetTooltipHandlerForPoint(point_in_v);
1435 EXPECT_EQ(root_view, result_view);
1438 TEST_F(ViewTest, NotifyEnterExitOnChild) {
1439 Widget* widget = new Widget;
1440 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1441 widget->Init(params);
1442 View* root_view = widget->GetRootView();
1443 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
1445 // Have this hierarchy of views (the coords here are in root coord):
1446 // v1 (0, 0, 100, 100)
1447 // - v11 (0, 0, 20, 30)
1448 // - v111 (5, 5, 5, 15)
1449 // - v12 (50, 10, 30, 90)
1450 // - v121 (60, 20, 10, 10)
1451 // v2 (105, 0, 100, 100)
1452 // - v21 (120, 10, 50, 20)
1454 TestView* v1 = new TestView;
1455 v1->SetBounds(0, 0, 100, 100);
1456 root_view->AddChildView(v1);
1457 v1->set_notify_enter_exit_on_child(true);
1459 TestView* v11 = new TestView;
1460 v11->SetBounds(0, 0, 20, 30);
1461 v1->AddChildView(v11);
1463 TestView* v111 = new TestView;
1464 v111->SetBounds(5, 5, 5, 15);
1465 v11->AddChildView(v111);
1467 TestView* v12 = new TestView;
1468 v12->SetBounds(50, 10, 30, 90);
1469 v1->AddChildView(v12);
1471 TestView* v121 = new TestView;
1472 v121->SetBounds(10, 10, 10, 10);
1473 v12->AddChildView(v121);
1475 TestView* v2 = new TestView;
1476 v2->SetBounds(105, 0, 100, 100);
1477 root_view->AddChildView(v2);
1479 TestView* v21 = new TestView;
1480 v21->SetBounds(15, 10, 50, 20);
1481 v2->AddChildView(v21);
1483 v1->Reset();
1484 v11->Reset();
1485 v111->Reset();
1486 v12->Reset();
1487 v121->Reset();
1488 v2->Reset();
1489 v21->Reset();
1491 // Move the mouse in v111.
1492 gfx::Point p1(6, 6);
1493 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, p1, p1, ui::EventTimeForNow(), 0, 0);
1494 root_view->OnMouseMoved(move1);
1495 EXPECT_TRUE(v111->received_mouse_enter_);
1496 EXPECT_FALSE(v11->last_mouse_event_type_);
1497 EXPECT_TRUE(v1->received_mouse_enter_);
1499 v111->Reset();
1500 v1->Reset();
1502 // Now, move into v121.
1503 gfx::Point p2(65, 21);
1504 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, p2, p2, ui::EventTimeForNow(), 0, 0);
1505 root_view->OnMouseMoved(move2);
1506 EXPECT_TRUE(v111->received_mouse_exit_);
1507 EXPECT_TRUE(v121->received_mouse_enter_);
1508 EXPECT_FALSE(v1->last_mouse_event_type_);
1510 v111->Reset();
1511 v121->Reset();
1513 // Now, move into v11.
1514 gfx::Point p3(1, 1);
1515 ui::MouseEvent move3(ui::ET_MOUSE_MOVED, p3, p3, ui::EventTimeForNow(), 0, 0);
1516 root_view->OnMouseMoved(move3);
1517 EXPECT_TRUE(v121->received_mouse_exit_);
1518 EXPECT_TRUE(v11->received_mouse_enter_);
1519 EXPECT_FALSE(v1->last_mouse_event_type_);
1521 v121->Reset();
1522 v11->Reset();
1524 // Move to v21.
1525 gfx::Point p4(121, 15);
1526 ui::MouseEvent move4(ui::ET_MOUSE_MOVED, p4, p4, ui::EventTimeForNow(), 0, 0);
1527 root_view->OnMouseMoved(move4);
1528 EXPECT_TRUE(v21->received_mouse_enter_);
1529 EXPECT_FALSE(v2->last_mouse_event_type_);
1530 EXPECT_TRUE(v11->received_mouse_exit_);
1531 EXPECT_TRUE(v1->received_mouse_exit_);
1533 v21->Reset();
1534 v11->Reset();
1535 v1->Reset();
1537 // Move to v1.
1538 gfx::Point p5(21, 0);
1539 ui::MouseEvent move5(ui::ET_MOUSE_MOVED, p5, p5, ui::EventTimeForNow(), 0, 0);
1540 root_view->OnMouseMoved(move5);
1541 EXPECT_TRUE(v21->received_mouse_exit_);
1542 EXPECT_TRUE(v1->received_mouse_enter_);
1544 v21->Reset();
1545 v1->Reset();
1547 // Now, move into v11.
1548 gfx::Point p6(15, 15);
1549 ui::MouseEvent mouse6(ui::ET_MOUSE_MOVED, p6, p6, ui::EventTimeForNow(), 0,
1551 root_view->OnMouseMoved(mouse6);
1552 EXPECT_TRUE(v11->received_mouse_enter_);
1553 EXPECT_FALSE(v1->last_mouse_event_type_);
1555 v11->Reset();
1556 v1->Reset();
1558 // Move back into v1. Although |v1| had already received an ENTER for mouse6,
1559 // and the mouse remains inside |v1| the whole time, it receives another ENTER
1560 // when the mouse leaves v11.
1561 gfx::Point p7(21, 0);
1562 ui::MouseEvent mouse7(ui::ET_MOUSE_MOVED, p7, p7, ui::EventTimeForNow(), 0,
1564 root_view->OnMouseMoved(mouse7);
1565 EXPECT_TRUE(v11->received_mouse_exit_);
1566 EXPECT_FALSE(v1->received_mouse_enter_);
1568 widget->CloseNow();
1571 TEST_F(ViewTest, Textfield) {
1572 const base::string16 kText = ASCIIToUTF16(
1573 "Reality is that which, when you stop believing it, doesn't go away.");
1574 const base::string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!");
1576 Widget* widget = new Widget;
1577 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1578 params.bounds = gfx::Rect(0, 0, 100, 100);
1579 widget->Init(params);
1580 View* root_view = widget->GetRootView();
1582 Textfield* textfield = new Textfield();
1583 root_view->AddChildView(textfield);
1585 // Test setting, appending text.
1586 textfield->SetText(kText);
1587 EXPECT_EQ(kText, textfield->text());
1588 textfield->AppendText(kExtraText);
1589 EXPECT_EQ(kText + kExtraText, textfield->text());
1590 textfield->SetText(base::string16());
1591 EXPECT_TRUE(textfield->text().empty());
1593 // Test selection related methods.
1594 textfield->SetText(kText);
1595 EXPECT_TRUE(textfield->GetSelectedText().empty());
1596 textfield->SelectAll(false);
1597 EXPECT_EQ(kText, textfield->text());
1598 textfield->ClearSelection();
1599 EXPECT_TRUE(textfield->GetSelectedText().empty());
1601 widget->CloseNow();
1604 // Tests that the Textfield view respond appropiately to cut/copy/paste.
1605 TEST_F(ViewTest, TextfieldCutCopyPaste) {
1606 const base::string16 kNormalText = ASCIIToUTF16("Normal");
1607 const base::string16 kReadOnlyText = ASCIIToUTF16("Read only");
1608 const base::string16 kPasswordText =
1609 ASCIIToUTF16("Password! ** Secret stuff **");
1611 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1613 Widget* widget = new Widget;
1614 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1615 params.bounds = gfx::Rect(0, 0, 100, 100);
1616 widget->Init(params);
1617 View* root_view = widget->GetRootView();
1619 Textfield* normal = new Textfield();
1620 Textfield* read_only = new Textfield();
1621 read_only->SetReadOnly(true);
1622 Textfield* password = new Textfield();
1623 password->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
1625 root_view->AddChildView(normal);
1626 root_view->AddChildView(read_only);
1627 root_view->AddChildView(password);
1629 normal->SetText(kNormalText);
1630 read_only->SetText(kReadOnlyText);
1631 password->SetText(kPasswordText);
1634 // Test cut.
1637 normal->SelectAll(false);
1638 normal->ExecuteCommand(IDS_APP_CUT);
1639 base::string16 result;
1640 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1641 EXPECT_EQ(kNormalText, result);
1642 normal->SetText(kNormalText); // Let's revert to the original content.
1644 read_only->SelectAll(false);
1645 read_only->ExecuteCommand(IDS_APP_CUT);
1646 result.clear();
1647 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1648 // Cut should have failed, so the clipboard content should not have changed.
1649 EXPECT_EQ(kNormalText, result);
1651 password->SelectAll(false);
1652 password->ExecuteCommand(IDS_APP_CUT);
1653 result.clear();
1654 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1655 // Cut should have failed, so the clipboard content should not have changed.
1656 EXPECT_EQ(kNormalText, result);
1659 // Test copy.
1662 // Start with |read_only| to observe a change in clipboard text.
1663 read_only->SelectAll(false);
1664 read_only->ExecuteCommand(IDS_APP_COPY);
1665 result.clear();
1666 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1667 EXPECT_EQ(kReadOnlyText, result);
1669 normal->SelectAll(false);
1670 normal->ExecuteCommand(IDS_APP_COPY);
1671 result.clear();
1672 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1673 EXPECT_EQ(kNormalText, result);
1675 password->SelectAll(false);
1676 password->ExecuteCommand(IDS_APP_COPY);
1677 result.clear();
1678 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &result);
1679 // Text cannot be copied from an obscured field; the clipboard won't change.
1680 EXPECT_EQ(kNormalText, result);
1683 // Test paste.
1686 // Attempting to paste kNormalText in a read-only text-field should fail.
1687 read_only->SelectAll(false);
1688 read_only->ExecuteCommand(IDS_APP_PASTE);
1689 EXPECT_EQ(kReadOnlyText, read_only->text());
1691 password->SelectAll(false);
1692 password->ExecuteCommand(IDS_APP_PASTE);
1693 EXPECT_EQ(kNormalText, password->text());
1695 // Copy from |read_only| to observe a change in the normal textfield text.
1696 read_only->SelectAll(false);
1697 read_only->ExecuteCommand(IDS_APP_COPY);
1698 normal->SelectAll(false);
1699 normal->ExecuteCommand(IDS_APP_PASTE);
1700 EXPECT_EQ(kReadOnlyText, normal->text());
1701 widget->CloseNow();
1704 ////////////////////////////////////////////////////////////////////////////////
1705 // Accelerators
1706 ////////////////////////////////////////////////////////////////////////////////
1707 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) {
1708 accelerator_count_map_[accelerator]++;
1709 return true;
1712 // TODO: these tests were initially commented out when getting aura to
1713 // run. Figure out if still valuable and either nuke or fix.
1714 #if 0
1715 TEST_F(ViewTest, ActivateAccelerator) {
1716 // Register a keyboard accelerator before the view is added to a window.
1717 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1718 TestView* view = new TestView();
1719 view->Reset();
1720 view->AddAccelerator(return_accelerator);
1721 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1723 // Create a window and add the view as its child.
1724 scoped_ptr<Widget> widget(new Widget);
1725 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1726 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1727 params.bounds = gfx::Rect(0, 0, 100, 100);
1728 widget->Init(params);
1729 View* root = widget->GetRootView();
1730 root->AddChildView(view);
1731 widget->Show();
1733 // Get the focus manager.
1734 FocusManager* focus_manager = widget->GetFocusManager();
1735 ASSERT_TRUE(focus_manager);
1737 // Hit the return key and see if it takes effect.
1738 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1739 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1741 // Hit the escape key. Nothing should happen.
1742 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, ui::EF_NONE);
1743 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1744 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1745 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0);
1747 // Now register the escape key and hit it again.
1748 view->AddAccelerator(escape_accelerator);
1749 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1750 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1751 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1753 // Remove the return key accelerator.
1754 view->RemoveAccelerator(return_accelerator);
1755 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1756 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1);
1757 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1759 // Add it again. Hit the return key and the escape key.
1760 view->AddAccelerator(return_accelerator);
1761 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1762 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1763 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1);
1764 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator));
1765 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1766 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1768 // Remove all the accelerators.
1769 view->ResetAccelerators();
1770 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1771 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1772 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1773 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator));
1774 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2);
1775 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2);
1777 widget->CloseNow();
1780 TEST_F(ViewTest, HiddenViewWithAccelerator) {
1781 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1782 TestView* view = new TestView();
1783 view->Reset();
1784 view->AddAccelerator(return_accelerator);
1785 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1787 scoped_ptr<Widget> widget(new Widget);
1788 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1789 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1790 params.bounds = gfx::Rect(0, 0, 100, 100);
1791 widget->Init(params);
1792 View* root = widget->GetRootView();
1793 root->AddChildView(view);
1794 widget->Show();
1796 FocusManager* focus_manager = widget->GetFocusManager();
1797 ASSERT_TRUE(focus_manager);
1799 view->SetVisible(false);
1800 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1802 view->SetVisible(true);
1803 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1805 widget->CloseNow();
1808 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) {
1809 ui::Accelerator return_accelerator(ui::VKEY_RETURN, ui::EF_NONE);
1810 TestView* view = new TestView();
1811 view->Reset();
1812 view->AddAccelerator(return_accelerator);
1813 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0);
1815 scoped_ptr<Widget> widget(new Widget);
1816 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
1817 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1818 params.bounds = gfx::Rect(0, 0, 100, 100);
1819 widget->Init(params);
1820 View* root = widget->GetRootView();
1821 root->AddChildView(view);
1823 FocusManager* focus_manager = widget->GetFocusManager();
1824 ASSERT_TRUE(focus_manager);
1826 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1827 EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
1829 widget->Show();
1830 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator));
1831 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1833 widget->Hide();
1834 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator));
1835 EXPECT_EQ(1, view->accelerator_count_map_[return_accelerator]);
1837 widget->CloseNow();
1840 ////////////////////////////////////////////////////////////////////////////////
1841 // Mouse-wheel message rerouting
1842 ////////////////////////////////////////////////////////////////////////////////
1843 class ScrollableTestView : public View {
1844 public:
1845 ScrollableTestView() { }
1847 virtual gfx::Size GetPreferredSize() {
1848 return gfx::Size(100, 10000);
1851 virtual void Layout() {
1852 SizeToPreferredSize();
1856 class TestViewWithControls : public View {
1857 public:
1858 TestViewWithControls() {
1859 text_field_ = new Textfield();
1860 AddChildView(text_field_);
1863 Textfield* text_field_;
1866 class SimpleWidgetDelegate : public WidgetDelegate {
1867 public:
1868 explicit SimpleWidgetDelegate(View* contents) : contents_(contents) { }
1870 virtual void DeleteDelegate() { delete this; }
1872 virtual View* GetContentsView() { return contents_; }
1874 virtual Widget* GetWidget() { return contents_->GetWidget(); }
1875 virtual const Widget* GetWidget() const { return contents_->GetWidget(); }
1877 private:
1878 View* contents_;
1881 // Tests that the mouse-wheel messages are correctly rerouted to the window
1882 // under the mouse.
1883 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build
1884 // bot.
1885 // Note that this fails for a variety of reasons:
1886 // - focused view is apparently reset across window activations and never
1887 // properly restored
1888 // - this test depends on you not having any other window visible open under the
1889 // area that it opens the test windows. --beng
1890 TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) {
1891 TestViewWithControls* view_with_controls = new TestViewWithControls();
1892 Widget* window1 = Widget::CreateWindowWithBounds(
1893 new SimpleWidgetDelegate(view_with_controls),
1894 gfx::Rect(0, 0, 100, 100));
1895 window1->Show();
1896 ScrollView* scroll_view = new ScrollView();
1897 scroll_view->SetContents(new ScrollableTestView());
1898 Widget* window2 = Widget::CreateWindowWithBounds(
1899 new SimpleWidgetDelegate(scroll_view),
1900 gfx::Rect(200, 200, 100, 100));
1901 window2->Show();
1902 EXPECT_EQ(0, scroll_view->GetVisibleRect().y());
1904 // Make the window1 active, as this is what it would be in real-world.
1905 window1->Activate();
1907 // Let's send a mouse-wheel message to the different controls and check that
1908 // it is rerouted to the window under the mouse (effectively scrolling the
1909 // scroll-view).
1911 // First to the Window's HWND.
1912 ::SendMessage(view_with_controls->GetWidget()->GetNativeView(),
1913 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250));
1914 EXPECT_EQ(20, scroll_view->GetVisibleRect().y());
1916 window1->CloseNow();
1917 window2->CloseNow();
1919 #endif // 0
1921 ////////////////////////////////////////////////////////////////////////////////
1922 // Native view hierachy
1923 ////////////////////////////////////////////////////////////////////////////////
1924 class ToplevelWidgetObserverView : public View {
1925 public:
1926 ToplevelWidgetObserverView() : toplevel_(NULL) {
1928 ~ToplevelWidgetObserverView() override {}
1930 // View overrides:
1931 void ViewHierarchyChanged(
1932 const ViewHierarchyChangedDetails& details) override {
1933 if (details.is_add) {
1934 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1935 } else {
1936 toplevel_ = NULL;
1939 void NativeViewHierarchyChanged() override {
1940 toplevel_ = GetWidget() ? GetWidget()->GetTopLevelWidget() : NULL;
1943 Widget* toplevel() { return toplevel_; }
1945 private:
1946 Widget* toplevel_;
1948 DISALLOW_COPY_AND_ASSIGN(ToplevelWidgetObserverView);
1951 // Test that a view can track the current top level widget by overriding
1952 // View::ViewHierarchyChanged() and View::NativeViewHierarchyChanged().
1953 TEST_F(ViewTest, NativeViewHierarchyChanged) {
1954 scoped_ptr<Widget> toplevel1(new Widget);
1955 Widget::InitParams toplevel1_params =
1956 CreateParams(Widget::InitParams::TYPE_POPUP);
1957 toplevel1_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1958 toplevel1->Init(toplevel1_params);
1960 scoped_ptr<Widget> toplevel2(new Widget);
1961 Widget::InitParams toplevel2_params =
1962 CreateParams(Widget::InitParams::TYPE_POPUP);
1963 toplevel2_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
1964 toplevel2->Init(toplevel2_params);
1966 Widget* child = new Widget;
1967 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
1968 child_params.parent = toplevel1->GetNativeView();
1969 child->Init(child_params);
1971 ToplevelWidgetObserverView* observer_view =
1972 new ToplevelWidgetObserverView();
1973 EXPECT_EQ(NULL, observer_view->toplevel());
1975 child->SetContentsView(observer_view);
1976 EXPECT_EQ(toplevel1, observer_view->toplevel());
1978 Widget::ReparentNativeView(child->GetNativeView(),
1979 toplevel2->GetNativeView());
1980 EXPECT_EQ(toplevel2, observer_view->toplevel());
1982 observer_view->parent()->RemoveChildView(observer_view);
1983 EXPECT_EQ(NULL, observer_view->toplevel());
1985 // Make |observer_view| |child|'s contents view again so that it gets deleted
1986 // with the widget.
1987 child->SetContentsView(observer_view);
1990 ////////////////////////////////////////////////////////////////////////////////
1991 // Transformations
1992 ////////////////////////////////////////////////////////////////////////////////
1994 class TransformPaintView : public TestView {
1995 public:
1996 TransformPaintView() {}
1997 ~TransformPaintView() override {}
1999 void ClearScheduledPaintRect() {
2000 scheduled_paint_rect_ = gfx::Rect();
2003 gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; }
2005 // Overridden from View:
2006 void SchedulePaintInRect(const gfx::Rect& rect) override {
2007 gfx::Rect xrect = ConvertRectToParent(rect);
2008 scheduled_paint_rect_.Union(xrect);
2011 private:
2012 gfx::Rect scheduled_paint_rect_;
2014 DISALLOW_COPY_AND_ASSIGN(TransformPaintView);
2017 TEST_F(ViewTest, TransformPaint) {
2018 TransformPaintView* v1 = new TransformPaintView();
2019 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2021 TestView* v2 = new TestView();
2022 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2024 Widget* widget = new Widget;
2025 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2026 params.bounds = gfx::Rect(50, 50, 650, 650);
2027 widget->Init(params);
2028 widget->Show();
2029 View* root = widget->GetRootView();
2031 root->AddChildView(v1);
2032 v1->AddChildView(v2);
2034 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2035 v1->ClearScheduledPaintRect();
2036 v2->SchedulePaint();
2038 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect());
2040 // Rotate |v1| counter-clockwise.
2041 gfx::Transform transform;
2042 RotateCounterclockwise(&transform);
2043 transform.matrix().set(1, 3, 500.0);
2044 v1->SetTransform(transform);
2046 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2048 v1->ClearScheduledPaintRect();
2049 v2->SchedulePaint();
2051 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect());
2053 widget->CloseNow();
2056 TEST_F(ViewTest, TransformEvent) {
2057 TestView* v1 = new TestView();
2058 v1->SetBoundsRect(gfx::Rect(0, 0, 500, 300));
2060 TestView* v2 = new TestView();
2061 v2->SetBoundsRect(gfx::Rect(100, 100, 200, 100));
2063 Widget* widget = new Widget;
2064 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2065 params.bounds = gfx::Rect(50, 50, 650, 650);
2066 widget->Init(params);
2067 View* root = widget->GetRootView();
2069 root->AddChildView(v1);
2070 v1->AddChildView(v2);
2072 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|.
2074 // Rotate |v1| counter-clockwise.
2075 gfx::Transform transform(v1->GetTransform());
2076 RotateCounterclockwise(&transform);
2077 transform.matrix().set(1, 3, 500.0);
2078 v1->SetTransform(transform);
2080 // |v2| now occupies (100, 200) to (200, 400) in |root|.
2081 v1->Reset();
2082 v2->Reset();
2084 gfx::Point p1(110, 210);
2085 ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1, ui::EventTimeForNow(),
2086 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2087 root->OnMousePressed(pressed);
2088 EXPECT_EQ(0, v1->last_mouse_event_type_);
2089 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
2090 EXPECT_EQ(190, v2->location_.x());
2091 EXPECT_EQ(10, v2->location_.y());
2093 ui::MouseEvent released(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
2094 ui::EventTimeForNow(), 0, 0);
2095 root->OnMouseReleased(released);
2097 // Now rotate |v2| inside |v1| clockwise.
2098 transform = v2->GetTransform();
2099 RotateClockwise(&transform);
2100 transform.matrix().set(0, 3, 100.f);
2101 v2->SetTransform(transform);
2103 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to
2104 // (300, 400) in |root|.
2106 v1->Reset();
2107 v2->Reset();
2109 gfx::Point point2(110, 320);
2110 ui::MouseEvent p2(ui::ET_MOUSE_PRESSED, point2, point2, ui::EventTimeForNow(),
2111 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2112 root->OnMousePressed(p2);
2113 EXPECT_EQ(0, v1->last_mouse_event_type_);
2114 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_);
2115 EXPECT_EQ(10, v2->location_.x());
2116 EXPECT_EQ(20, v2->location_.y());
2118 root->OnMouseReleased(released);
2120 v1->SetTransform(gfx::Transform());
2121 v2->SetTransform(gfx::Transform());
2123 TestView* v3 = new TestView();
2124 v3->SetBoundsRect(gfx::Rect(10, 10, 20, 30));
2125 v2->AddChildView(v3);
2127 // Rotate |v3| clockwise with respect to |v2|.
2128 transform = v1->GetTransform();
2129 RotateClockwise(&transform);
2130 transform.matrix().set(0, 3, 30.f);
2131 v3->SetTransform(transform);
2133 // Scale |v2| with respect to |v1| along both axis.
2134 transform = v2->GetTransform();
2135 transform.matrix().set(0, 0, 0.8f);
2136 transform.matrix().set(1, 1, 0.5f);
2137 v2->SetTransform(transform);
2139 // |v3| occupies (108, 105) to (132, 115) in |root|.
2141 v1->Reset();
2142 v2->Reset();
2143 v3->Reset();
2145 gfx::Point point(112, 110);
2146 ui::MouseEvent p3(ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
2147 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2148 root->OnMousePressed(p3);
2150 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
2151 EXPECT_EQ(10, v3->location_.x());
2152 EXPECT_EQ(25, v3->location_.y());
2154 root->OnMouseReleased(released);
2156 v1->SetTransform(gfx::Transform());
2157 v2->SetTransform(gfx::Transform());
2158 v3->SetTransform(gfx::Transform());
2160 v1->Reset();
2161 v2->Reset();
2162 v3->Reset();
2164 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis.
2165 transform = v3->GetTransform();
2166 RotateClockwise(&transform);
2167 transform.matrix().set(0, 3, 30.f);
2168 // Rotation sets some scaling transformation. Using SetScale would overwrite
2169 // that and pollute the rotation. So combine the scaling with the existing
2170 // transforamtion.
2171 gfx::Transform scale;
2172 scale.Scale(0.8f, 0.5f);
2173 transform.ConcatTransform(scale);
2174 v3->SetTransform(transform);
2176 // Translate |v2| with respect to |v1|.
2177 transform = v2->GetTransform();
2178 transform.matrix().set(0, 3, 10.f);
2179 transform.matrix().set(1, 3, 10.f);
2180 v2->SetTransform(transform);
2182 // |v3| now occupies (120, 120) to (144, 130) in |root|.
2184 gfx::Point point3(124, 125);
2185 ui::MouseEvent p4(ui::ET_MOUSE_PRESSED, point3, point3, ui::EventTimeForNow(),
2186 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
2187 root->OnMousePressed(p4);
2189 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_);
2190 EXPECT_EQ(10, v3->location_.x());
2191 EXPECT_EQ(25, v3->location_.y());
2193 root->OnMouseReleased(released);
2195 widget->CloseNow();
2198 TEST_F(ViewTest, TransformVisibleBound) {
2199 gfx::Rect viewport_bounds(0, 0, 100, 100);
2201 scoped_ptr<Widget> widget(new Widget);
2202 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2203 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2204 params.bounds = viewport_bounds;
2205 widget->Init(params);
2206 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2208 View* viewport = new View;
2209 widget->SetContentsView(viewport);
2210 View* contents = new View;
2211 viewport->AddChildView(contents);
2212 viewport->SetBoundsRect(viewport_bounds);
2213 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2215 View* child = new View;
2216 contents->AddChildView(child);
2217 child->SetBoundsRect(gfx::Rect(10, 90, 50, 50));
2218 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds());
2220 // Rotate |child| counter-clockwise
2221 gfx::Transform transform;
2222 RotateCounterclockwise(&transform);
2223 transform.matrix().set(1, 3, 50.f);
2224 child->SetTransform(transform);
2225 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds());
2227 widget->CloseNow();
2230 ////////////////////////////////////////////////////////////////////////////////
2231 // OnVisibleBoundsChanged()
2233 class VisibleBoundsView : public View {
2234 public:
2235 VisibleBoundsView() : received_notification_(false) {}
2236 ~VisibleBoundsView() override {}
2238 bool received_notification() const { return received_notification_; }
2239 void set_received_notification(bool received) {
2240 received_notification_ = received;
2243 private:
2244 // Overridden from View:
2245 bool GetNeedsNotificationWhenVisibleBoundsChange() const override {
2246 return true;
2248 void OnVisibleBoundsChanged() override { received_notification_ = true; }
2250 bool received_notification_;
2252 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView);
2255 TEST_F(ViewTest, OnVisibleBoundsChanged) {
2256 gfx::Rect viewport_bounds(0, 0, 100, 100);
2258 scoped_ptr<Widget> widget(new Widget);
2259 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2260 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2261 params.bounds = viewport_bounds;
2262 widget->Init(params);
2263 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2265 View* viewport = new View;
2266 widget->SetContentsView(viewport);
2267 View* contents = new View;
2268 viewport->AddChildView(contents);
2269 viewport->SetBoundsRect(viewport_bounds);
2270 contents->SetBoundsRect(gfx::Rect(0, 0, 100, 200));
2272 // Create a view that cares about visible bounds notifications, and position
2273 // it just outside the visible bounds of the viewport.
2274 VisibleBoundsView* child = new VisibleBoundsView;
2275 contents->AddChildView(child);
2276 child->SetBoundsRect(gfx::Rect(10, 110, 50, 50));
2278 // The child bound should be fully clipped.
2279 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
2281 // Now scroll the contents, but not enough to make the child visible.
2282 contents->SetY(contents->y() - 1);
2284 // We should have received the notification since the visible bounds may have
2285 // changed (even though they didn't).
2286 EXPECT_TRUE(child->received_notification());
2287 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty());
2288 child->set_received_notification(false);
2290 // Now scroll the contents, this time by enough to make the child visible by
2291 // one pixel.
2292 contents->SetY(contents->y() - 10);
2293 EXPECT_TRUE(child->received_notification());
2294 EXPECT_EQ(1, child->GetVisibleBounds().height());
2295 child->set_received_notification(false);
2297 widget->CloseNow();
2300 TEST_F(ViewTest, SetBoundsPaint) {
2301 TestView top_view;
2302 TestView* child_view = new TestView;
2304 top_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2305 top_view.scheduled_paint_rects_.clear();
2306 child_view->SetBoundsRect(gfx::Rect(10, 10, 20, 20));
2307 top_view.AddChildView(child_view);
2309 top_view.scheduled_paint_rects_.clear();
2310 child_view->SetBoundsRect(gfx::Rect(30, 30, 20, 20));
2311 EXPECT_EQ(2U, top_view.scheduled_paint_rects_.size());
2313 // There should be 2 rects, spanning from (10, 10) to (50, 50).
2314 gfx::Rect paint_rect = top_view.scheduled_paint_rects_[0];
2315 paint_rect.Union(top_view.scheduled_paint_rects_[1]);
2316 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect);
2319 // Assertions around painting and focus gain/lost.
2320 TEST_F(ViewTest, FocusBlurPaints) {
2321 TestView parent_view;
2322 TestView* child_view1 = new TestView; // Owned by |parent_view|.
2324 parent_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2326 child_view1->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2327 parent_view.AddChildView(child_view1);
2329 parent_view.scheduled_paint_rects_.clear();
2330 child_view1->scheduled_paint_rects_.clear();
2332 // Focus change shouldn't trigger paints.
2333 child_view1->DoFocus();
2335 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
2336 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
2338 child_view1->DoBlur();
2339 EXPECT_TRUE(parent_view.scheduled_paint_rects_.empty());
2340 EXPECT_TRUE(child_view1->scheduled_paint_rects_.empty());
2343 // Verifies SetBounds(same bounds) doesn't trigger a SchedulePaint().
2344 TEST_F(ViewTest, SetBoundsSameBoundsDoesntSchedulePaint) {
2345 TestView view;
2347 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2348 view.InvalidateLayout();
2349 view.scheduled_paint_rects_.clear();
2350 view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
2351 EXPECT_TRUE(view.scheduled_paint_rects_.empty());
2354 // Verifies AddChildView() and RemoveChildView() schedule appropriate paints.
2355 TEST_F(ViewTest, AddAndRemoveSchedulePaints) {
2356 gfx::Rect viewport_bounds(0, 0, 100, 100);
2358 // We have to put the View hierarchy into a Widget or no paints will be
2359 // scheduled.
2360 scoped_ptr<Widget> widget(new Widget);
2361 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2362 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2363 params.bounds = viewport_bounds;
2364 widget->Init(params);
2365 widget->GetRootView()->SetBoundsRect(viewport_bounds);
2367 TestView* parent_view = new TestView;
2368 widget->SetContentsView(parent_view);
2369 parent_view->SetBoundsRect(viewport_bounds);
2370 parent_view->scheduled_paint_rects_.clear();
2372 View* child_view = new View;
2373 child_view->SetBoundsRect(gfx::Rect(0, 0, 20, 20));
2374 parent_view->AddChildView(child_view);
2375 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
2376 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
2378 parent_view->scheduled_paint_rects_.clear();
2379 parent_view->RemoveChildView(child_view);
2380 scoped_ptr<View> child_deleter(child_view);
2381 ASSERT_EQ(1U, parent_view->scheduled_paint_rects_.size());
2382 EXPECT_EQ(child_view->bounds(), parent_view->scheduled_paint_rects_.front());
2384 widget->CloseNow();
2387 // Tests conversion methods with a transform.
2388 TEST_F(ViewTest, ConversionsWithTransform) {
2389 TestView top_view;
2391 // View hierarchy used to test scale transforms.
2392 TestView* child = new TestView;
2393 TestView* child_child = new TestView;
2395 // View used to test a rotation transform.
2396 TestView* child_2 = new TestView;
2398 top_view.AddChildView(child);
2399 child->AddChildView(child_child);
2401 top_view.SetBoundsRect(gfx::Rect(0, 0, 1000, 1000));
2403 child->SetBoundsRect(gfx::Rect(7, 19, 500, 500));
2404 gfx::Transform transform;
2405 transform.Scale(3.0, 4.0);
2406 child->SetTransform(transform);
2408 child_child->SetBoundsRect(gfx::Rect(17, 13, 100, 100));
2409 transform.MakeIdentity();
2410 transform.Scale(5.0, 7.0);
2411 child_child->SetTransform(transform);
2413 top_view.AddChildView(child_2);
2414 child_2->SetBoundsRect(gfx::Rect(700, 725, 100, 100));
2415 transform.MakeIdentity();
2416 RotateClockwise(&transform);
2417 child_2->SetTransform(transform);
2419 // Sanity check to make sure basic transforms act as expected.
2421 gfx::Transform transform;
2422 transform.Translate(110.0, -110.0);
2423 transform.Scale(100.0, 55.0);
2424 transform.Translate(1.0, 1.0);
2426 // convert to a 3x3 matrix.
2427 const SkMatrix& matrix = transform.matrix();
2429 EXPECT_EQ(210, matrix.getTranslateX());
2430 EXPECT_EQ(-55, matrix.getTranslateY());
2431 EXPECT_EQ(100, matrix.getScaleX());
2432 EXPECT_EQ(55, matrix.getScaleY());
2433 EXPECT_EQ(0, matrix.getSkewX());
2434 EXPECT_EQ(0, matrix.getSkewY());
2438 gfx::Transform transform;
2439 transform.Translate(1.0, 1.0);
2440 gfx::Transform t2;
2441 t2.Scale(100.0, 55.0);
2442 gfx::Transform t3;
2443 t3.Translate(110.0, -110.0);
2444 transform.ConcatTransform(t2);
2445 transform.ConcatTransform(t3);
2447 // convert to a 3x3 matrix
2448 const SkMatrix& matrix = transform.matrix();
2450 EXPECT_EQ(210, matrix.getTranslateX());
2451 EXPECT_EQ(-55, matrix.getTranslateY());
2452 EXPECT_EQ(100, matrix.getScaleX());
2453 EXPECT_EQ(55, matrix.getScaleY());
2454 EXPECT_EQ(0, matrix.getSkewX());
2455 EXPECT_EQ(0, matrix.getSkewY());
2458 // Conversions from child->top and top->child.
2460 gfx::Point point(5, 5);
2461 View::ConvertPointToTarget(child, &top_view, &point);
2462 EXPECT_EQ(22, point.x());
2463 EXPECT_EQ(39, point.y());
2465 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2466 View::ConvertRectToTarget(child, &top_view, &rect);
2467 EXPECT_FLOAT_EQ(22.0f, rect.x());
2468 EXPECT_FLOAT_EQ(39.0f, rect.y());
2469 EXPECT_FLOAT_EQ(30.0f, rect.width());
2470 EXPECT_FLOAT_EQ(80.0f, rect.height());
2472 point.SetPoint(22, 39);
2473 View::ConvertPointToTarget(&top_view, child, &point);
2474 EXPECT_EQ(5, point.x());
2475 EXPECT_EQ(5, point.y());
2477 rect.SetRect(22.0f, 39.0f, 30.0f, 80.0f);
2478 View::ConvertRectToTarget(&top_view, child, &rect);
2479 EXPECT_FLOAT_EQ(5.0f, rect.x());
2480 EXPECT_FLOAT_EQ(5.0f, rect.y());
2481 EXPECT_FLOAT_EQ(10.0f, rect.width());
2482 EXPECT_FLOAT_EQ(20.0f, rect.height());
2485 // Conversions from child_child->top and top->child_child.
2487 gfx::Point point(5, 5);
2488 View::ConvertPointToTarget(child_child, &top_view, &point);
2489 EXPECT_EQ(133, point.x());
2490 EXPECT_EQ(211, point.y());
2492 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2493 View::ConvertRectToTarget(child_child, &top_view, &rect);
2494 EXPECT_FLOAT_EQ(133.0f, rect.x());
2495 EXPECT_FLOAT_EQ(211.0f, rect.y());
2496 EXPECT_FLOAT_EQ(150.0f, rect.width());
2497 EXPECT_FLOAT_EQ(560.0f, rect.height());
2499 point.SetPoint(133, 211);
2500 View::ConvertPointToTarget(&top_view, child_child, &point);
2501 EXPECT_EQ(5, point.x());
2502 EXPECT_EQ(5, point.y());
2504 rect.SetRect(133.0f, 211.0f, 150.0f, 560.0f);
2505 View::ConvertRectToTarget(&top_view, child_child, &rect);
2506 EXPECT_FLOAT_EQ(5.0f, rect.x());
2507 EXPECT_FLOAT_EQ(5.0f, rect.y());
2508 EXPECT_FLOAT_EQ(10.0f, rect.width());
2509 EXPECT_FLOAT_EQ(20.0f, rect.height());
2512 // Conversions from child_child->child and child->child_child
2514 gfx::Point point(5, 5);
2515 View::ConvertPointToTarget(child_child, child, &point);
2516 EXPECT_EQ(42, point.x());
2517 EXPECT_EQ(48, point.y());
2519 gfx::RectF rect(5.0f, 5.0f, 10.0f, 20.0f);
2520 View::ConvertRectToTarget(child_child, child, &rect);
2521 EXPECT_FLOAT_EQ(42.0f, rect.x());
2522 EXPECT_FLOAT_EQ(48.0f, rect.y());
2523 EXPECT_FLOAT_EQ(50.0f, rect.width());
2524 EXPECT_FLOAT_EQ(140.0f, rect.height());
2526 point.SetPoint(42, 48);
2527 View::ConvertPointToTarget(child, child_child, &point);
2528 EXPECT_EQ(5, point.x());
2529 EXPECT_EQ(5, point.y());
2531 rect.SetRect(42.0f, 48.0f, 50.0f, 140.0f);
2532 View::ConvertRectToTarget(child, child_child, &rect);
2533 EXPECT_FLOAT_EQ(5.0f, rect.x());
2534 EXPECT_FLOAT_EQ(5.0f, rect.y());
2535 EXPECT_FLOAT_EQ(10.0f, rect.width());
2536 EXPECT_FLOAT_EQ(20.0f, rect.height());
2539 // Conversions from top_view to child with a value that should be negative.
2540 // This ensures we don't round up with negative numbers.
2542 gfx::Point point(6, 18);
2543 View::ConvertPointToTarget(&top_view, child, &point);
2544 EXPECT_EQ(-1, point.x());
2545 EXPECT_EQ(-1, point.y());
2547 float error = 0.01f;
2548 gfx::RectF rect(6.0f, 18.0f, 10.0f, 39.0f);
2549 View::ConvertRectToTarget(&top_view, child, &rect);
2550 EXPECT_NEAR(-0.33f, rect.x(), error);
2551 EXPECT_NEAR(-0.25f, rect.y(), error);
2552 EXPECT_NEAR(3.33f, rect.width(), error);
2553 EXPECT_NEAR(9.75f, rect.height(), error);
2556 // Rect conversions from top_view->child_2 and child_2->top_view.
2558 gfx::RectF rect(50.0f, 55.0f, 20.0f, 30.0f);
2559 View::ConvertRectToTarget(child_2, &top_view, &rect);
2560 EXPECT_FLOAT_EQ(615.0f, rect.x());
2561 EXPECT_FLOAT_EQ(775.0f, rect.y());
2562 EXPECT_FLOAT_EQ(30.0f, rect.width());
2563 EXPECT_FLOAT_EQ(20.0f, rect.height());
2565 rect.SetRect(615.0f, 775.0f, 30.0f, 20.0f);
2566 View::ConvertRectToTarget(&top_view, child_2, &rect);
2567 EXPECT_FLOAT_EQ(50.0f, rect.x());
2568 EXPECT_FLOAT_EQ(55.0f, rect.y());
2569 EXPECT_FLOAT_EQ(20.0f, rect.width());
2570 EXPECT_FLOAT_EQ(30.0f, rect.height());
2574 // Tests conversion methods to and from screen coordinates.
2575 TEST_F(ViewTest, ConversionsToFromScreen) {
2576 scoped_ptr<Widget> widget(new Widget);
2577 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2578 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2579 params.bounds = gfx::Rect(50, 50, 650, 650);
2580 widget->Init(params);
2582 View* child = new View;
2583 widget->GetRootView()->AddChildView(child);
2584 child->SetBounds(10, 10, 100, 200);
2585 gfx::Transform t;
2586 t.Scale(0.5, 0.5);
2587 child->SetTransform(t);
2589 gfx::Point point_in_screen(100, 90);
2590 gfx::Point point_in_child(80, 60);
2592 gfx::Point point = point_in_screen;
2593 View::ConvertPointFromScreen(child, &point);
2594 EXPECT_EQ(point_in_child.ToString(), point.ToString());
2596 View::ConvertPointToScreen(child, &point);
2597 EXPECT_EQ(point_in_screen.ToString(), point.ToString());
2600 // Tests conversion methods for rectangles.
2601 TEST_F(ViewTest, ConvertRectWithTransform) {
2602 scoped_ptr<Widget> widget(new Widget);
2603 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2604 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2605 params.bounds = gfx::Rect(50, 50, 650, 650);
2606 widget->Init(params);
2607 View* root = widget->GetRootView();
2609 TestView* v1 = new TestView;
2610 TestView* v2 = new TestView;
2611 root->AddChildView(v1);
2612 v1->AddChildView(v2);
2614 v1->SetBoundsRect(gfx::Rect(10, 10, 500, 500));
2615 v2->SetBoundsRect(gfx::Rect(20, 20, 100, 200));
2617 // |v2| now occupies (30, 30) to (130, 230) in |widget|
2618 gfx::Rect rect(5, 5, 15, 40);
2619 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2->ConvertRectToParent(rect));
2620 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2->ConvertRectToWidget(rect));
2622 // Rotate |v2|
2623 gfx::Transform t2;
2624 RotateCounterclockwise(&t2);
2625 t2.matrix().set(1, 3, 100.f);
2626 v2->SetTransform(t2);
2628 // |v2| now occupies (30, 30) to (230, 130) in |widget|
2629 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2630 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2->ConvertRectToWidget(rect));
2632 // Scale down |v1|
2633 gfx::Transform t1;
2634 t1.Scale(0.5, 0.5);
2635 v1->SetTransform(t1);
2637 // The rectangle should remain the same for |v1|.
2638 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect));
2640 // |v2| now occupies (20, 20) to (120, 70) in |widget|
2641 EXPECT_EQ(gfx::Rect(22, 60, 21, 8).ToString(),
2642 v2->ConvertRectToWidget(rect).ToString());
2644 widget->CloseNow();
2647 class ObserverView : public View {
2648 public:
2649 ObserverView();
2650 ~ObserverView() override;
2652 void ResetTestState();
2654 bool has_add_details() const { return has_add_details_; }
2655 bool has_remove_details() const { return has_remove_details_; }
2657 const ViewHierarchyChangedDetails& add_details() const {
2658 return add_details_;
2661 const ViewHierarchyChangedDetails& remove_details() const {
2662 return remove_details_;
2665 private:
2666 // View:
2667 void ViewHierarchyChanged(
2668 const ViewHierarchyChangedDetails& details) override;
2670 bool has_add_details_;
2671 bool has_remove_details_;
2672 ViewHierarchyChangedDetails add_details_;
2673 ViewHierarchyChangedDetails remove_details_;
2675 DISALLOW_COPY_AND_ASSIGN(ObserverView);
2678 ObserverView::ObserverView()
2679 : has_add_details_(false),
2680 has_remove_details_(false) {
2683 ObserverView::~ObserverView() {}
2685 void ObserverView::ResetTestState() {
2686 has_add_details_ = false;
2687 has_remove_details_ = false;
2688 add_details_ = ViewHierarchyChangedDetails();
2689 remove_details_ = ViewHierarchyChangedDetails();
2692 void ObserverView::ViewHierarchyChanged(
2693 const ViewHierarchyChangedDetails& details) {
2694 if (details.is_add) {
2695 has_add_details_ = true;
2696 add_details_ = details;
2697 } else {
2698 has_remove_details_ = true;
2699 remove_details_ = details;
2703 // Verifies that the ViewHierarchyChanged() notification is sent correctly when
2704 // a child view is added or removed to all the views in the hierarchy (up and
2705 // down).
2706 // The tree looks like this:
2707 // v1
2708 // +-- v2
2709 // +-- v3
2710 // +-- v4 (starts here, then get reparented to v1)
2711 TEST_F(ViewTest, ViewHierarchyChanged) {
2712 ObserverView v1;
2714 ObserverView* v3 = new ObserverView();
2716 // Add |v3| to |v2|.
2717 scoped_ptr<ObserverView> v2(new ObserverView());
2718 v2->AddChildView(v3);
2720 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged()
2721 // notification.
2722 EXPECT_TRUE(v2->has_add_details());
2723 EXPECT_FALSE(v2->has_remove_details());
2724 EXPECT_EQ(v2.get(), v2->add_details().parent);
2725 EXPECT_EQ(v3, v2->add_details().child);
2726 EXPECT_EQ(NULL, v2->add_details().move_view);
2728 EXPECT_TRUE(v3->has_add_details());
2729 EXPECT_FALSE(v3->has_remove_details());
2730 EXPECT_EQ(v2.get(), v3->add_details().parent);
2731 EXPECT_EQ(v3, v3->add_details().child);
2732 EXPECT_EQ(NULL, v3->add_details().move_view);
2734 // Reset everything to the initial state.
2735 v2->ResetTestState();
2736 v3->ResetTestState();
2738 // Add |v2| to v1.
2739 v1.AddChildView(v2.get());
2741 // Verifies that |v2| is the child view *added* and the parent view is |v1|.
2742 // Make sure all the views (v1, v2, v3) received _that_ information.
2743 EXPECT_TRUE(v1.has_add_details());
2744 EXPECT_FALSE(v1.has_remove_details());
2745 EXPECT_EQ(&v1, v1.add_details().parent);
2746 EXPECT_EQ(v2.get(), v1.add_details().child);
2747 EXPECT_EQ(NULL, v1.add_details().move_view);
2749 EXPECT_TRUE(v2->has_add_details());
2750 EXPECT_FALSE(v2->has_remove_details());
2751 EXPECT_EQ(&v1, v2->add_details().parent);
2752 EXPECT_EQ(v2.get(), v2->add_details().child);
2753 EXPECT_EQ(NULL, v2->add_details().move_view);
2755 EXPECT_TRUE(v3->has_add_details());
2756 EXPECT_FALSE(v3->has_remove_details());
2757 EXPECT_EQ(&v1, v3->add_details().parent);
2758 EXPECT_EQ(v2.get(), v3->add_details().child);
2759 EXPECT_EQ(NULL, v3->add_details().move_view);
2761 // Reset everything to the initial state.
2762 v1.ResetTestState();
2763 v2->ResetTestState();
2764 v3->ResetTestState();
2766 // Remove |v2| from |v1|.
2767 v1.RemoveChildView(v2.get());
2769 // Verifies that |v2| is the child view *removed* and the parent view is |v1|.
2770 // Make sure all the views (v1, v2, v3) received _that_ information.
2771 EXPECT_FALSE(v1.has_add_details());
2772 EXPECT_TRUE(v1.has_remove_details());
2773 EXPECT_EQ(&v1, v1.remove_details().parent);
2774 EXPECT_EQ(v2.get(), v1.remove_details().child);
2775 EXPECT_EQ(NULL, v1.remove_details().move_view);
2777 EXPECT_FALSE(v2->has_add_details());
2778 EXPECT_TRUE(v2->has_remove_details());
2779 EXPECT_EQ(&v1, v2->remove_details().parent);
2780 EXPECT_EQ(v2.get(), v2->remove_details().child);
2781 EXPECT_EQ(NULL, v2->remove_details().move_view);
2783 EXPECT_FALSE(v3->has_add_details());
2784 EXPECT_TRUE(v3->has_remove_details());
2785 EXPECT_EQ(&v1, v3->remove_details().parent);
2786 EXPECT_EQ(v3, v3->remove_details().child);
2787 EXPECT_EQ(NULL, v3->remove_details().move_view);
2789 // Verifies notifications when reparenting a view.
2790 ObserverView* v4 = new ObserverView();
2791 // Add |v4| to |v2|.
2792 v2->AddChildView(v4);
2794 // Reset everything to the initial state.
2795 v1.ResetTestState();
2796 v2->ResetTestState();
2797 v3->ResetTestState();
2798 v4->ResetTestState();
2800 // Reparent |v4| to |v1|.
2801 v1.AddChildView(v4);
2803 // Verifies that all views receive the correct information for all the child,
2804 // parent and move views.
2806 // |v1| is the new parent, |v4| is the child for add, |v2| is the old parent.
2807 EXPECT_TRUE(v1.has_add_details());
2808 EXPECT_FALSE(v1.has_remove_details());
2809 EXPECT_EQ(&v1, v1.add_details().parent);
2810 EXPECT_EQ(v4, v1.add_details().child);
2811 EXPECT_EQ(v2.get(), v1.add_details().move_view);
2813 // |v2| is the old parent, |v4| is the child for remove, |v1| is the new
2814 // parent.
2815 EXPECT_FALSE(v2->has_add_details());
2816 EXPECT_TRUE(v2->has_remove_details());
2817 EXPECT_EQ(v2.get(), v2->remove_details().parent);
2818 EXPECT_EQ(v4, v2->remove_details().child);
2819 EXPECT_EQ(&v1, v2->remove_details().move_view);
2821 // |v3| is not impacted by this operation, and hence receives no notification.
2822 EXPECT_FALSE(v3->has_add_details());
2823 EXPECT_FALSE(v3->has_remove_details());
2825 // |v4| is the reparented child, so it receives notifications for the remove
2826 // and then the add. |v2| is its old parent, |v1| is its new parent.
2827 EXPECT_TRUE(v4->has_remove_details());
2828 EXPECT_TRUE(v4->has_add_details());
2829 EXPECT_EQ(v2.get(), v4->remove_details().parent);
2830 EXPECT_EQ(&v1, v4->add_details().parent);
2831 EXPECT_EQ(v4, v4->add_details().child);
2832 EXPECT_EQ(v4, v4->remove_details().child);
2833 EXPECT_EQ(&v1, v4->remove_details().move_view);
2834 EXPECT_EQ(v2.get(), v4->add_details().move_view);
2837 // Verifies if the child views added under the root are all deleted when calling
2838 // RemoveAllChildViews.
2839 // The tree looks like this:
2840 // root
2841 // +-- child1
2842 // +-- foo
2843 // +-- bar0
2844 // +-- bar1
2845 // +-- bar2
2846 // +-- child2
2847 // +-- child3
2848 TEST_F(ViewTest, RemoveAllChildViews) {
2849 View root;
2851 View* child1 = new View;
2852 root.AddChildView(child1);
2854 for (int i = 0; i < 2; ++i)
2855 root.AddChildView(new View);
2857 View* foo = new View;
2858 child1->AddChildView(foo);
2860 // Add some nodes to |foo|.
2861 for (int i = 0; i < 3; ++i)
2862 foo->AddChildView(new View);
2864 EXPECT_EQ(3, root.child_count());
2865 EXPECT_EQ(1, child1->child_count());
2866 EXPECT_EQ(3, foo->child_count());
2868 // Now remove all child views from root.
2869 root.RemoveAllChildViews(true);
2871 EXPECT_EQ(0, root.child_count());
2872 EXPECT_FALSE(root.has_children());
2875 TEST_F(ViewTest, Contains) {
2876 View v1;
2877 View* v2 = new View;
2878 View* v3 = new View;
2880 v1.AddChildView(v2);
2881 v2->AddChildView(v3);
2883 EXPECT_FALSE(v1.Contains(NULL));
2884 EXPECT_TRUE(v1.Contains(&v1));
2885 EXPECT_TRUE(v1.Contains(v2));
2886 EXPECT_TRUE(v1.Contains(v3));
2888 EXPECT_FALSE(v2->Contains(NULL));
2889 EXPECT_TRUE(v2->Contains(v2));
2890 EXPECT_FALSE(v2->Contains(&v1));
2891 EXPECT_TRUE(v2->Contains(v3));
2893 EXPECT_FALSE(v3->Contains(NULL));
2894 EXPECT_TRUE(v3->Contains(v3));
2895 EXPECT_FALSE(v3->Contains(&v1));
2896 EXPECT_FALSE(v3->Contains(v2));
2899 // Verifies if GetIndexOf() returns the correct index for the specified child
2900 // view.
2901 // The tree looks like this:
2902 // root
2903 // +-- child1
2904 // +-- foo1
2905 // +-- child2
2906 TEST_F(ViewTest, GetIndexOf) {
2907 View root;
2909 View* child1 = new View;
2910 root.AddChildView(child1);
2912 View* child2 = new View;
2913 root.AddChildView(child2);
2915 View* foo1 = new View;
2916 child1->AddChildView(foo1);
2918 EXPECT_EQ(-1, root.GetIndexOf(NULL));
2919 EXPECT_EQ(-1, root.GetIndexOf(&root));
2920 EXPECT_EQ(0, root.GetIndexOf(child1));
2921 EXPECT_EQ(1, root.GetIndexOf(child2));
2922 EXPECT_EQ(-1, root.GetIndexOf(foo1));
2924 EXPECT_EQ(-1, child1->GetIndexOf(NULL));
2925 EXPECT_EQ(-1, child1->GetIndexOf(&root));
2926 EXPECT_EQ(-1, child1->GetIndexOf(child1));
2927 EXPECT_EQ(-1, child1->GetIndexOf(child2));
2928 EXPECT_EQ(0, child1->GetIndexOf(foo1));
2930 EXPECT_EQ(-1, child2->GetIndexOf(NULL));
2931 EXPECT_EQ(-1, child2->GetIndexOf(&root));
2932 EXPECT_EQ(-1, child2->GetIndexOf(child2));
2933 EXPECT_EQ(-1, child2->GetIndexOf(child1));
2934 EXPECT_EQ(-1, child2->GetIndexOf(foo1));
2937 // Verifies that the child views can be reordered correctly.
2938 TEST_F(ViewTest, ReorderChildren) {
2939 View root;
2941 View* child = new View();
2942 root.AddChildView(child);
2944 View* foo1 = new View();
2945 child->AddChildView(foo1);
2946 View* foo2 = new View();
2947 child->AddChildView(foo2);
2948 View* foo3 = new View();
2949 child->AddChildView(foo3);
2950 foo1->SetFocusable(true);
2951 foo2->SetFocusable(true);
2952 foo3->SetFocusable(true);
2954 ASSERT_EQ(0, child->GetIndexOf(foo1));
2955 ASSERT_EQ(1, child->GetIndexOf(foo2));
2956 ASSERT_EQ(2, child->GetIndexOf(foo3));
2957 ASSERT_EQ(foo2, foo1->GetNextFocusableView());
2958 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
2959 ASSERT_EQ(NULL, foo3->GetNextFocusableView());
2961 // Move |foo2| at the end.
2962 child->ReorderChildView(foo2, -1);
2963 ASSERT_EQ(0, child->GetIndexOf(foo1));
2964 ASSERT_EQ(1, child->GetIndexOf(foo3));
2965 ASSERT_EQ(2, child->GetIndexOf(foo2));
2966 ASSERT_EQ(foo3, foo1->GetNextFocusableView());
2967 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
2968 ASSERT_EQ(NULL, foo2->GetNextFocusableView());
2970 // Move |foo1| at the end.
2971 child->ReorderChildView(foo1, -1);
2972 ASSERT_EQ(0, child->GetIndexOf(foo3));
2973 ASSERT_EQ(1, child->GetIndexOf(foo2));
2974 ASSERT_EQ(2, child->GetIndexOf(foo1));
2975 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
2976 ASSERT_EQ(foo2, foo1->GetPreviousFocusableView());
2977 ASSERT_EQ(foo2, foo3->GetNextFocusableView());
2978 ASSERT_EQ(foo1, foo2->GetNextFocusableView());
2980 // Move |foo2| to the front.
2981 child->ReorderChildView(foo2, 0);
2982 ASSERT_EQ(0, child->GetIndexOf(foo2));
2983 ASSERT_EQ(1, child->GetIndexOf(foo3));
2984 ASSERT_EQ(2, child->GetIndexOf(foo1));
2985 ASSERT_EQ(NULL, foo1->GetNextFocusableView());
2986 ASSERT_EQ(foo3, foo1->GetPreviousFocusableView());
2987 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
2988 ASSERT_EQ(foo1, foo3->GetNextFocusableView());
2991 // Verifies that GetViewByID returns the correctly child view from the specified
2992 // ID.
2993 // The tree looks like this:
2994 // v1
2995 // +-- v2
2996 // +-- v3
2997 // +-- v4
2998 TEST_F(ViewTest, GetViewByID) {
2999 View v1;
3000 const int kV1ID = 1;
3001 v1.set_id(kV1ID);
3003 View v2;
3004 const int kV2ID = 2;
3005 v2.set_id(kV2ID);
3007 View v3;
3008 const int kV3ID = 3;
3009 v3.set_id(kV3ID);
3011 View v4;
3012 const int kV4ID = 4;
3013 v4.set_id(kV4ID);
3015 const int kV5ID = 5;
3017 v1.AddChildView(&v2);
3018 v2.AddChildView(&v3);
3019 v2.AddChildView(&v4);
3021 EXPECT_EQ(&v1, v1.GetViewByID(kV1ID));
3022 EXPECT_EQ(&v2, v1.GetViewByID(kV2ID));
3023 EXPECT_EQ(&v4, v1.GetViewByID(kV4ID));
3025 EXPECT_EQ(NULL, v1.GetViewByID(kV5ID)); // No V5 exists.
3026 EXPECT_EQ(NULL, v2.GetViewByID(kV1ID)); // It can get only from child views.
3028 const int kGroup = 1;
3029 v3.SetGroup(kGroup);
3030 v4.SetGroup(kGroup);
3032 View::Views views;
3033 v1.GetViewsInGroup(kGroup, &views);
3034 EXPECT_EQ(2U, views.size());
3036 View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3));
3037 EXPECT_NE(views.end(), i);
3039 i = std::find(views.begin(), views.end(), &v4);
3040 EXPECT_NE(views.end(), i);
3043 TEST_F(ViewTest, AddExistingChild) {
3044 View v1, v2, v3;
3046 v1.AddChildView(&v2);
3047 v1.AddChildView(&v3);
3048 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3049 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3051 // Check that there's no change in order when adding at same index.
3052 v1.AddChildViewAt(&v2, 0);
3053 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3054 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3055 v1.AddChildViewAt(&v3, 1);
3056 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3057 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3059 // Add it at a different index and check for change in order.
3060 v1.AddChildViewAt(&v2, 1);
3061 EXPECT_EQ(1, v1.GetIndexOf(&v2));
3062 EXPECT_EQ(0, v1.GetIndexOf(&v3));
3063 v1.AddChildViewAt(&v2, 0);
3064 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3065 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3067 // Check that calling |AddChildView()| does not change the order.
3068 v1.AddChildView(&v2);
3069 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3070 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3071 v1.AddChildView(&v3);
3072 EXPECT_EQ(0, v1.GetIndexOf(&v2));
3073 EXPECT_EQ(1, v1.GetIndexOf(&v3));
3076 ////////////////////////////////////////////////////////////////////////////////
3077 // FocusManager
3078 ////////////////////////////////////////////////////////////////////////////////
3080 // A widget that always claims to be active, regardless of its real activation
3081 // status.
3082 class ActiveWidget : public Widget {
3083 public:
3084 ActiveWidget() {}
3085 ~ActiveWidget() override {}
3087 bool IsActive() const override { return true; }
3089 private:
3090 DISALLOW_COPY_AND_ASSIGN(ActiveWidget);
3093 TEST_F(ViewTest, AdvanceFocusIfNecessaryForUnfocusableView) {
3094 // Create a widget with two views and give the first one focus.
3095 ActiveWidget widget;
3096 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3097 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3098 widget.Init(params);
3100 View* view1 = new View();
3101 view1->SetFocusable(true);
3102 widget.GetRootView()->AddChildView(view1);
3103 View* view2 = new View();
3104 view2->SetFocusable(true);
3105 widget.GetRootView()->AddChildView(view2);
3107 FocusManager* focus_manager = widget.GetFocusManager();
3108 ASSERT_TRUE(focus_manager);
3110 focus_manager->SetFocusedView(view1);
3111 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3113 // Disable the focused view and check if the next view gets focused.
3114 view1->SetEnabled(false);
3115 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3117 // Re-enable and re-focus.
3118 view1->SetEnabled(true);
3119 focus_manager->SetFocusedView(view1);
3120 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3122 // Hide the focused view and check it the next view gets focused.
3123 view1->SetVisible(false);
3124 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3126 // Re-show and re-focus.
3127 view1->SetVisible(true);
3128 focus_manager->SetFocusedView(view1);
3129 EXPECT_EQ(view1, focus_manager->GetFocusedView());
3131 // Set the focused view as not focusable and check if the next view gets
3132 // focused.
3133 view1->SetFocusable(false);
3134 EXPECT_EQ(view2, focus_manager->GetFocusedView());
3137 ////////////////////////////////////////////////////////////////////////////////
3138 // Layers
3139 ////////////////////////////////////////////////////////////////////////////////
3141 namespace {
3143 // Test implementation of LayerAnimator.
3144 class TestLayerAnimator : public ui::LayerAnimator {
3145 public:
3146 TestLayerAnimator();
3148 const gfx::Rect& last_bounds() const { return last_bounds_; }
3150 // LayerAnimator.
3151 void SetBounds(const gfx::Rect& bounds) override;
3153 protected:
3154 ~TestLayerAnimator() override {}
3156 private:
3157 gfx::Rect last_bounds_;
3159 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
3162 TestLayerAnimator::TestLayerAnimator()
3163 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) {
3166 void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) {
3167 last_bounds_ = bounds;
3170 } // namespace
3172 class ViewLayerTest : public ViewsTestBase {
3173 public:
3174 ViewLayerTest() : widget_(NULL) {}
3176 ~ViewLayerTest() override {}
3178 // Returns the Layer used by the RootView.
3179 ui::Layer* GetRootLayer() {
3180 return widget()->GetLayer();
3183 void SetUp() override {
3184 ViewTest::SetUp();
3185 widget_ = new Widget;
3186 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3187 params.bounds = gfx::Rect(50, 50, 200, 200);
3188 widget_->Init(params);
3189 widget_->Show();
3190 widget_->GetRootView()->SetBounds(0, 0, 200, 200);
3193 void TearDown() override {
3194 widget_->CloseNow();
3195 ViewsTestBase::TearDown();
3198 Widget* widget() { return widget_; }
3200 private:
3201 Widget* widget_;
3205 TEST_F(ViewLayerTest, LayerToggling) {
3206 // Because we lazily create textures the calls to DrawTree are necessary to
3207 // ensure we trigger creation of textures.
3208 ui::Layer* root_layer = widget()->GetLayer();
3209 View* content_view = new View;
3210 widget()->SetContentsView(content_view);
3212 // Create v1, give it a bounds and verify everything is set up correctly.
3213 View* v1 = new View;
3214 v1->SetPaintToLayer(true);
3215 EXPECT_TRUE(v1->layer() != NULL);
3216 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3217 content_view->AddChildView(v1);
3218 ASSERT_TRUE(v1->layer() != NULL);
3219 EXPECT_EQ(root_layer, v1->layer()->parent());
3220 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds());
3222 // Create v2 as a child of v1 and do basic assertion testing.
3223 View* v2 = new View;
3224 v1->AddChildView(v2);
3225 EXPECT_TRUE(v2->layer() == NULL);
3226 v2->SetBoundsRect(gfx::Rect(10, 20, 30, 40));
3227 v2->SetPaintToLayer(true);
3228 ASSERT_TRUE(v2->layer() != NULL);
3229 EXPECT_EQ(v1->layer(), v2->layer()->parent());
3230 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
3232 // Turn off v1s layer. v2 should still have a layer but its parent should have
3233 // changed.
3234 v1->SetPaintToLayer(false);
3235 EXPECT_TRUE(v1->layer() == NULL);
3236 EXPECT_TRUE(v2->layer() != NULL);
3237 EXPECT_EQ(root_layer, v2->layer()->parent());
3238 ASSERT_EQ(1u, root_layer->children().size());
3239 EXPECT_EQ(root_layer->children()[0], v2->layer());
3240 // The bounds of the layer should have changed to be relative to the root view
3241 // now.
3242 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds());
3244 // Make v1 have a layer again and verify v2s layer is wired up correctly.
3245 gfx::Transform transform;
3246 transform.Scale(2.0, 2.0);
3247 v1->SetTransform(transform);
3248 EXPECT_TRUE(v1->layer() != NULL);
3249 EXPECT_TRUE(v2->layer() != NULL);
3250 EXPECT_EQ(root_layer, v1->layer()->parent());
3251 EXPECT_EQ(v1->layer(), v2->layer()->parent());
3252 ASSERT_EQ(1u, root_layer->children().size());
3253 EXPECT_EQ(root_layer->children()[0], v1->layer());
3254 ASSERT_EQ(1u, v1->layer()->children().size());
3255 EXPECT_EQ(v1->layer()->children()[0], v2->layer());
3256 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds());
3259 // Verifies turning on a layer wires up children correctly.
3260 TEST_F(ViewLayerTest, NestedLayerToggling) {
3261 View* content_view = new View;
3262 widget()->SetContentsView(content_view);
3264 // Create v1, give it a bounds and verify everything is set up correctly.
3265 View* v1 = new View;
3266 content_view->AddChildView(v1);
3267 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3269 View* v2 = new View;
3270 v1->AddChildView(v2);
3272 View* v3 = new View;
3273 v3->SetPaintToLayer(true);
3274 v2->AddChildView(v3);
3275 ASSERT_TRUE(v3->layer() != NULL);
3277 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't.
3279 v1->SetPaintToLayer(true);
3280 EXPECT_EQ(v1->layer(), v3->layer()->parent());
3283 TEST_F(ViewLayerTest, LayerAnimator) {
3284 View* content_view = new View;
3285 widget()->SetContentsView(content_view);
3287 View* v1 = new View;
3288 content_view->AddChildView(v1);
3289 v1->SetPaintToLayer(true);
3290 EXPECT_TRUE(v1->layer() != NULL);
3292 TestLayerAnimator* animator = new TestLayerAnimator();
3293 v1->layer()->SetAnimator(animator);
3295 gfx::Rect bounds(1, 2, 3, 4);
3296 v1->SetBoundsRect(bounds);
3297 EXPECT_EQ(bounds, animator->last_bounds());
3298 // TestLayerAnimator doesn't update the layer.
3299 EXPECT_NE(bounds, v1->layer()->bounds());
3302 // Verifies the bounds of a layer are updated if the bounds of ancestor that
3303 // doesn't have a layer change.
3304 TEST_F(ViewLayerTest, BoundsChangeWithLayer) {
3305 View* content_view = new View;
3306 widget()->SetContentsView(content_view);
3308 View* v1 = new View;
3309 content_view->AddChildView(v1);
3310 v1->SetBoundsRect(gfx::Rect(20, 30, 140, 150));
3312 View* v2 = new View;
3313 v2->SetBoundsRect(gfx::Rect(10, 11, 40, 50));
3314 v1->AddChildView(v2);
3315 v2->SetPaintToLayer(true);
3316 ASSERT_TRUE(v2->layer() != NULL);
3317 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds());
3319 v1->SetPosition(gfx::Point(25, 36));
3320 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds());
3322 v2->SetPosition(gfx::Point(11, 12));
3323 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds());
3325 // Bounds of the layer should change even if the view is not invisible.
3326 v1->SetVisible(false);
3327 v1->SetPosition(gfx::Point(20, 30));
3328 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds());
3330 v2->SetVisible(false);
3331 v2->SetBoundsRect(gfx::Rect(10, 11, 20, 30));
3332 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds());
3335 // Make sure layers are positioned correctly in RTL.
3336 TEST_F(ViewLayerTest, BoundInRTL) {
3337 ScopedRTL rtl;
3339 View* view = new View;
3340 widget()->SetContentsView(view);
3342 int content_width = view->width();
3344 // |v1| is initially not attached to anything. So its layer will have the same
3345 // bounds as the view.
3346 View* v1 = new View;
3347 v1->SetPaintToLayer(true);
3348 v1->SetBounds(10, 10, 20, 10);
3349 EXPECT_EQ(gfx::Rect(10, 10, 20, 10),
3350 v1->layer()->bounds());
3352 // Once |v1| is attached to the widget, its layer will get RTL-appropriate
3353 // bounds.
3354 view->AddChildView(v1);
3355 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
3356 v1->layer()->bounds());
3357 gfx::Rect l1bounds = v1->layer()->bounds();
3359 // Now attach a View to the widget first, then create a layer for it. Make
3360 // sure the bounds are correct.
3361 View* v2 = new View;
3362 v2->SetBounds(50, 10, 30, 10);
3363 EXPECT_FALSE(v2->layer());
3364 view->AddChildView(v2);
3365 v2->SetPaintToLayer(true);
3366 EXPECT_EQ(gfx::Rect(content_width - 80, 10, 30, 10),
3367 v2->layer()->bounds());
3368 gfx::Rect l2bounds = v2->layer()->bounds();
3370 view->SetPaintToLayer(true);
3371 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3372 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3374 // Move one of the views. Make sure the layer is positioned correctly
3375 // afterwards.
3376 v1->SetBounds(v1->x() - 5, v1->y(), v1->width(), v1->height());
3377 l1bounds.set_x(l1bounds.x() + 5);
3378 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3380 view->SetPaintToLayer(false);
3381 EXPECT_EQ(l1bounds, v1->layer()->bounds());
3382 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3384 // Move a view again.
3385 v2->SetBounds(v2->x() + 5, v2->y(), v2->width(), v2->height());
3386 l2bounds.set_x(l2bounds.x() - 5);
3387 EXPECT_EQ(l2bounds, v2->layer()->bounds());
3390 // Make sure that resizing a parent in RTL correctly repositions its children.
3391 TEST_F(ViewLayerTest, ResizeParentInRTL) {
3392 ScopedRTL rtl;
3394 View* view = new View;
3395 widget()->SetContentsView(view);
3397 int content_width = view->width();
3399 // Create a paints-to-layer view |v1|.
3400 View* v1 = new View;
3401 v1->SetPaintToLayer(true);
3402 v1->SetBounds(10, 10, 20, 10);
3403 view->AddChildView(v1);
3404 EXPECT_EQ(gfx::Rect(content_width - 30, 10, 20, 10),
3405 v1->layer()->bounds());
3407 // Attach a paints-to-layer child view to |v1|.
3408 View* v2 = new View;
3409 v2->SetPaintToLayer(true);
3410 v2->SetBounds(3, 5, 6, 4);
3411 EXPECT_EQ(gfx::Rect(3, 5, 6, 4),
3412 v2->layer()->bounds());
3413 v1->AddChildView(v2);
3414 // Check that |v2| now has RTL-appropriate bounds.
3415 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3416 v2->layer()->bounds());
3418 // Attach a non-layer child view to |v1|, and give it a paints-to-layer child.
3419 View* v3 = new View;
3420 v3->SetBounds(1, 1, 18, 8);
3421 View* v4 = new View;
3422 v4->SetPaintToLayer(true);
3423 v4->SetBounds(2, 4, 6, 4);
3424 EXPECT_EQ(gfx::Rect(2, 4, 6, 4),
3425 v4->layer()->bounds());
3426 v3->AddChildView(v4);
3427 EXPECT_EQ(gfx::Rect(10, 4, 6, 4),
3428 v4->layer()->bounds());
3429 v1->AddChildView(v3);
3430 // Check that |v4| now has RTL-appropriate bounds.
3431 EXPECT_EQ(gfx::Rect(11, 5, 6, 4),
3432 v4->layer()->bounds());
3434 // Resize |v1|. Make sure that |v2| and |v4|'s layers have been moved
3435 // correctly to RTL-appropriate bounds.
3436 v1->SetSize(gfx::Size(30, 10));
3437 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3438 v2->layer()->bounds());
3439 EXPECT_EQ(gfx::Rect(21, 5, 6, 4),
3440 v4->layer()->bounds());
3442 // Move and resize |v3|. Make sure that |v4|'s layer has been moved correctly
3443 // to RTL-appropriate bounds.
3444 v3->SetBounds(2, 1, 12, 8);
3445 EXPECT_EQ(gfx::Rect(20, 5, 6, 4),
3446 v4->layer()->bounds());
3449 // Makes sure a transform persists after toggling the visibility.
3450 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) {
3451 View* view = new View;
3452 gfx::Transform transform;
3453 transform.Scale(2.0, 2.0);
3454 view->SetTransform(transform);
3455 widget()->SetContentsView(view);
3456 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3458 view->SetVisible(false);
3459 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3461 view->SetVisible(true);
3462 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3465 // Verifies a transform persists after removing/adding a view with a transform.
3466 TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) {
3467 View* view = new View;
3468 gfx::Transform transform;
3469 transform.Scale(2.0, 2.0);
3470 view->SetTransform(transform);
3471 widget()->SetContentsView(view);
3472 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3473 ASSERT_TRUE(view->layer() != NULL);
3474 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3476 View* parent = view->parent();
3477 parent->RemoveChildView(view);
3478 parent->AddChildView(view);
3480 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0));
3481 ASSERT_TRUE(view->layer() != NULL);
3482 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0));
3485 // Makes sure that layer visibility is correct after toggling View visibility.
3486 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) {
3487 View* content_view = new View;
3488 widget()->SetContentsView(content_view);
3490 // The view isn't attached to a widget or a parent view yet. But it should
3491 // still have a layer, but the layer should not be attached to the root
3492 // layer.
3493 View* v1 = new View;
3494 v1->SetPaintToLayer(true);
3495 EXPECT_TRUE(v1->layer());
3496 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3497 v1->layer()));
3499 // Once the view is attached to a widget, its layer should be attached to the
3500 // root layer and visible.
3501 content_view->AddChildView(v1);
3502 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3503 v1->layer()));
3504 EXPECT_TRUE(v1->layer()->IsDrawn());
3506 v1->SetVisible(false);
3507 EXPECT_FALSE(v1->layer()->IsDrawn());
3509 v1->SetVisible(true);
3510 EXPECT_TRUE(v1->layer()->IsDrawn());
3512 widget()->Hide();
3513 EXPECT_FALSE(v1->layer()->IsDrawn());
3515 widget()->Show();
3516 EXPECT_TRUE(v1->layer()->IsDrawn());
3519 // Tests that the layers in the subtree are orphaned after a View is removed
3520 // from the parent.
3521 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) {
3522 View* content_view = new View;
3523 widget()->SetContentsView(content_view);
3525 View* v1 = new View;
3526 content_view->AddChildView(v1);
3528 View* v2 = new View;
3529 v1->AddChildView(v2);
3530 v2->SetPaintToLayer(true);
3531 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3532 v2->layer()));
3533 EXPECT_TRUE(v2->layer()->IsDrawn());
3535 content_view->RemoveChildView(v1);
3537 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3538 v2->layer()));
3540 // Reparent |v2|.
3541 content_view->AddChildView(v2);
3542 delete v1;
3543 v1 = NULL;
3544 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(),
3545 v2->layer()));
3546 EXPECT_TRUE(v2->layer()->IsDrawn());
3549 class PaintTrackingView : public View {
3550 public:
3551 PaintTrackingView() : painted_(false) {
3554 bool painted() const { return painted_; }
3555 void set_painted(bool value) { painted_ = value; }
3557 void OnPaint(gfx::Canvas* canvas) override { painted_ = true; }
3559 private:
3560 bool painted_;
3562 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView);
3565 // Makes sure child views with layers aren't painted when paint starts at an
3566 // ancestor.
3567 TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) {
3568 PaintTrackingView* content_view = new PaintTrackingView;
3569 widget()->SetContentsView(content_view);
3570 content_view->SetPaintToLayer(true);
3571 GetRootLayer()->GetCompositor()->ScheduleDraw();
3572 ui::DrawWaiterForTest::WaitForCompositingEnded(
3573 GetRootLayer()->GetCompositor());
3574 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3575 content_view->set_painted(false);
3576 // content_view no longer has a dirty rect. Paint from the root and make sure
3577 // PaintTrackingView isn't painted.
3578 GetRootLayer()->GetCompositor()->ScheduleDraw();
3579 ui::DrawWaiterForTest::WaitForCompositingEnded(
3580 GetRootLayer()->GetCompositor());
3581 EXPECT_FALSE(content_view->painted());
3583 // Make content_view have a dirty rect, paint the layers and make sure
3584 // PaintTrackingView is painted.
3585 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10));
3586 GetRootLayer()->GetCompositor()->ScheduleDraw();
3587 ui::DrawWaiterForTest::WaitForCompositingEnded(
3588 GetRootLayer()->GetCompositor());
3589 EXPECT_TRUE(content_view->painted());
3592 // Tests that the visibility of child layers are updated correctly when a View's
3593 // visibility changes.
3594 TEST_F(ViewLayerTest, VisibilityChildLayers) {
3595 View* v1 = new View;
3596 v1->SetPaintToLayer(true);
3597 widget()->SetContentsView(v1);
3599 View* v2 = new View;
3600 v1->AddChildView(v2);
3602 View* v3 = new View;
3603 v2->AddChildView(v3);
3604 v3->SetVisible(false);
3606 View* v4 = new View;
3607 v4->SetPaintToLayer(true);
3608 v3->AddChildView(v4);
3610 EXPECT_TRUE(v1->layer()->IsDrawn());
3611 EXPECT_FALSE(v4->layer()->IsDrawn());
3613 v2->SetVisible(false);
3614 EXPECT_TRUE(v1->layer()->IsDrawn());
3615 EXPECT_FALSE(v4->layer()->IsDrawn());
3617 v2->SetVisible(true);
3618 EXPECT_TRUE(v1->layer()->IsDrawn());
3619 EXPECT_FALSE(v4->layer()->IsDrawn());
3621 v2->SetVisible(false);
3622 EXPECT_TRUE(v1->layer()->IsDrawn());
3623 EXPECT_FALSE(v4->layer()->IsDrawn());
3624 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3626 v3->SetVisible(true);
3627 EXPECT_TRUE(v1->layer()->IsDrawn());
3628 EXPECT_FALSE(v4->layer()->IsDrawn());
3629 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3631 // Reparent |v3| to |v1|.
3632 v1->AddChildView(v3);
3633 EXPECT_TRUE(v1->layer()->IsDrawn());
3634 EXPECT_TRUE(v4->layer()->IsDrawn());
3635 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer()));
3638 // This test creates a random View tree, and then randomly reorders child views,
3639 // reparents views etc. Unrelated changes can appear to break this test. So
3640 // marking this as FLAKY.
3641 TEST_F(ViewLayerTest, DISABLED_ViewLayerTreesInSync) {
3642 View* content = new View;
3643 content->SetPaintToLayer(true);
3644 widget()->SetContentsView(content);
3645 widget()->Show();
3647 ConstructTree(content, 5);
3648 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3650 ScrambleTree(content);
3651 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3653 ScrambleTree(content);
3654 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3656 ScrambleTree(content);
3657 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3660 // Verifies when views are reordered the layer is also reordered. The widget is
3661 // providing the parent layer.
3662 TEST_F(ViewLayerTest, ReorderUnderWidget) {
3663 View* content = new View;
3664 widget()->SetContentsView(content);
3665 View* c1 = new View;
3666 c1->SetPaintToLayer(true);
3667 content->AddChildView(c1);
3668 View* c2 = new View;
3669 c2->SetPaintToLayer(true);
3670 content->AddChildView(c2);
3672 ui::Layer* parent_layer = c1->layer()->parent();
3673 ASSERT_TRUE(parent_layer);
3674 ASSERT_EQ(2u, parent_layer->children().size());
3675 EXPECT_EQ(c1->layer(), parent_layer->children()[0]);
3676 EXPECT_EQ(c2->layer(), parent_layer->children()[1]);
3678 // Move c1 to the front. The layers should have moved too.
3679 content->ReorderChildView(c1, -1);
3680 EXPECT_EQ(c1->layer(), parent_layer->children()[1]);
3681 EXPECT_EQ(c2->layer(), parent_layer->children()[0]);
3684 // Verifies that the layer of a view can be acquired properly.
3685 TEST_F(ViewLayerTest, AcquireLayer) {
3686 View* content = new View;
3687 widget()->SetContentsView(content);
3688 scoped_ptr<View> c1(new View);
3689 c1->SetPaintToLayer(true);
3690 EXPECT_TRUE(c1->layer());
3691 content->AddChildView(c1.get());
3693 scoped_ptr<ui::Layer> layer(c1->AcquireLayer());
3694 EXPECT_EQ(layer.get(), c1->layer());
3696 scoped_ptr<ui::Layer> layer2(c1->RecreateLayer());
3697 EXPECT_NE(c1->layer(), layer2.get());
3699 // Destroy view before destroying layer.
3700 c1.reset();
3703 // Verify the z-order of the layers as a result of calling RecreateLayer().
3704 TEST_F(ViewLayerTest, RecreateLayerZOrder) {
3705 scoped_ptr<View> v(new View());
3706 v->SetPaintToLayer(true);
3708 View* v1 = new View();
3709 v1->SetPaintToLayer(true);
3710 v->AddChildView(v1);
3711 View* v2 = new View();
3712 v2->SetPaintToLayer(true);
3713 v->AddChildView(v2);
3715 // Test the initial z-order.
3716 const std::vector<ui::Layer*>& child_layers_pre = v->layer()->children();
3717 ASSERT_EQ(2u, child_layers_pre.size());
3718 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3719 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3721 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3723 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3724 // for |v1| and |v2|.
3725 const std::vector<ui::Layer*>& child_layers_post = v->layer()->children();
3726 ASSERT_EQ(3u, child_layers_post.size());
3727 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3728 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3729 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3732 // Verify the z-order of the layers as a result of calling RecreateLayer when
3733 // the widget is the parent with the layer.
3734 TEST_F(ViewLayerTest, RecreateLayerZOrderWidgetParent) {
3735 View* v = new View();
3736 widget()->SetContentsView(v);
3738 View* v1 = new View();
3739 v1->SetPaintToLayer(true);
3740 v->AddChildView(v1);
3741 View* v2 = new View();
3742 v2->SetPaintToLayer(true);
3743 v->AddChildView(v2);
3745 ui::Layer* root_layer = GetRootLayer();
3747 // Test the initial z-order.
3748 const std::vector<ui::Layer*>& child_layers_pre = root_layer->children();
3749 ASSERT_EQ(2u, child_layers_pre.size());
3750 EXPECT_EQ(v1->layer(), child_layers_pre[0]);
3751 EXPECT_EQ(v2->layer(), child_layers_pre[1]);
3753 scoped_ptr<ui::Layer> v1_old_layer(v1->RecreateLayer());
3755 // Test the new layer order. We expect: |v1| |v1_old_layer| |v2|.
3756 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3757 ASSERT_EQ(3u, child_layers_post.size());
3758 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3759 EXPECT_EQ(v1_old_layer, child_layers_post[1]);
3760 EXPECT_EQ(v2->layer(), child_layers_post[2]);
3763 // Verifies RecreateLayer() moves all Layers over, even those that don't have
3764 // a View.
3765 TEST_F(ViewLayerTest, RecreateLayerMovesNonViewChildren) {
3766 View v;
3767 v.SetPaintToLayer(true);
3768 View child;
3769 child.SetPaintToLayer(true);
3770 v.AddChildView(&child);
3771 ASSERT_TRUE(v.layer() != NULL);
3772 ASSERT_EQ(1u, v.layer()->children().size());
3773 EXPECT_EQ(v.layer()->children()[0], child.layer());
3775 ui::Layer layer(ui::LAYER_NOT_DRAWN);
3776 v.layer()->Add(&layer);
3777 v.layer()->StackAtBottom(&layer);
3779 scoped_ptr<ui::Layer> old_layer(v.RecreateLayer());
3781 // All children should be moved from old layer to new layer.
3782 ASSERT_TRUE(old_layer.get() != NULL);
3783 EXPECT_TRUE(old_layer->children().empty());
3785 // And new layer should have the two children.
3786 ASSERT_TRUE(v.layer() != NULL);
3787 ASSERT_EQ(2u, v.layer()->children().size());
3788 EXPECT_EQ(v.layer()->children()[0], &layer);
3789 EXPECT_EQ(v.layer()->children()[1], child.layer());
3792 namespace {
3794 std::string ToString(const gfx::Vector2dF& vector) {
3795 return base::StringPrintf("%.2f %0.2f", vector.x(), vector.y());
3798 } // namespace
3800 TEST_F(ViewLayerTest, SnapLayerToPixel) {
3801 View* v1 = new View;
3803 View* v11 = new View;
3804 v1->AddChildView(v11);
3806 widget()->SetContentsView(v1);
3808 const gfx::Size& size = GetRootLayer()->GetCompositor()->size();
3809 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.25f, size);
3811 v11->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3812 v1->SetBoundsRect(gfx::Rect(1, 1, 10, 10));
3813 v11->SetPaintToLayer(true);
3815 EXPECT_EQ("0.40 0.40", ToString(v11->layer()->subpixel_position_offset()));
3817 // Creating a layer in parent should update the child view's layer offset.
3818 v1->SetPaintToLayer(true);
3819 EXPECT_EQ("-0.20 -0.20", ToString(v1->layer()->subpixel_position_offset()));
3820 EXPECT_EQ("-0.20 -0.20", ToString(v11->layer()->subpixel_position_offset()));
3822 // DSF change should get propagated and update offsets.
3823 GetRootLayer()->GetCompositor()->SetScaleAndSize(1.5f, size);
3824 EXPECT_EQ("0.33 0.33", ToString(v1->layer()->subpixel_position_offset()));
3825 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3827 // Deleting parent's layer should update the child view's layer's offset.
3828 v1->SetPaintToLayer(false);
3829 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3831 // Setting parent view should update the child view's layer's offset.
3832 v1->SetBoundsRect(gfx::Rect(2, 2, 10, 10));
3833 EXPECT_EQ("0.33 0.33", ToString(v11->layer()->subpixel_position_offset()));
3835 // Setting integral DSF should reset the offset.
3836 GetRootLayer()->GetCompositor()->SetScaleAndSize(2.0f, size);
3837 EXPECT_EQ("0.00 0.00", ToString(v11->layer()->subpixel_position_offset()));
3840 TEST_F(ViewTest, FocusableAssertions) {
3841 // View subclasses may change insets based on whether they are focusable,
3842 // which effects the preferred size. To avoid preferred size changing around
3843 // these Views need to key off the last value set to SetFocusable(), not
3844 // whether the View is focusable right now. For this reason it's important
3845 // that focusable() return the last value passed to SetFocusable and not
3846 // whether the View is focusable right now.
3847 TestView view;
3848 view.SetFocusable(true);
3849 EXPECT_TRUE(view.focusable());
3850 view.SetEnabled(false);
3851 EXPECT_TRUE(view.focusable());
3852 view.SetFocusable(false);
3853 EXPECT_FALSE(view.focusable());
3856 // Verifies when a view is deleted it is removed from ViewStorage.
3857 TEST_F(ViewTest, UpdateViewStorageOnDelete) {
3858 ViewStorage* view_storage = ViewStorage::GetInstance();
3859 const int storage_id = view_storage->CreateStorageID();
3861 View view;
3862 view_storage->StoreView(storage_id, &view);
3864 EXPECT_TRUE(view_storage->RetrieveView(storage_id) == NULL);
3867 ////////////////////////////////////////////////////////////////////////////////
3868 // NativeTheme
3869 ////////////////////////////////////////////////////////////////////////////////
3871 void TestView::OnNativeThemeChanged(const ui::NativeTheme* native_theme) {
3872 native_theme_ = native_theme;
3875 TEST_F(ViewTest, OnNativeThemeChanged) {
3876 TestView* test_view = new TestView();
3877 EXPECT_FALSE(test_view->native_theme_);
3878 TestView* test_view_child = new TestView();
3879 EXPECT_FALSE(test_view_child->native_theme_);
3881 // Child view added before the widget hierarchy exists should get the
3882 // new native theme notification.
3883 test_view->AddChildView(test_view_child);
3885 scoped_ptr<Widget> widget(new Widget);
3886 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
3887 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3888 widget->Init(params);
3890 widget->GetRootView()->AddChildView(test_view);
3891 EXPECT_TRUE(test_view->native_theme_);
3892 EXPECT_EQ(widget->GetNativeTheme(), test_view->native_theme_);
3893 EXPECT_TRUE(test_view_child->native_theme_);
3894 EXPECT_EQ(widget->GetNativeTheme(), test_view_child->native_theme_);
3896 // Child view added after the widget hierarchy exists should also get the
3897 // notification.
3898 TestView* test_view_child_2 = new TestView();
3899 test_view->AddChildView(test_view_child_2);
3900 EXPECT_TRUE(test_view_child_2->native_theme_);
3901 EXPECT_EQ(widget->GetNativeTheme(), test_view_child_2->native_theme_);
3903 widget->CloseNow();
3906 } // namespace views