OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include <map> | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/rand_util.h" | |
9 #include "base/string_util.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | |
12 #include "ui/base/accelerators/accelerator.h" | |
13 #include "ui/base/clipboard/clipboard.h" | |
14 #include "ui/base/keycodes/keyboard_codes.h" | |
15 #include "ui/base/models/simple_menu_model.h" | |
16 #include "ui/gfx/canvas_skia.h" | |
17 #include "ui/gfx/compositor/compositor.h" | |
18 #include "ui/gfx/compositor/layer.h" | |
19 #include "ui/gfx/compositor/layer_animator.h" | |
20 #include "ui/gfx/compositor/test/test_compositor.h" | |
21 #include "ui/gfx/compositor/test/test_texture.h" | |
22 #include "ui/gfx/path.h" | |
23 #include "ui/gfx/transform.h" | |
24 #include "ui/views/controls/button/button_dropdown.h" | |
25 #include "ui/views/controls/button/checkbox.h" | |
26 #include "ui/views/controls/native/native_view_host.h" | |
27 #include "ui/views/controls/scroll_view.h" | |
28 #include "ui/views/controls/textfield/textfield.h" | |
29 #include "ui/views/events/event.h" | |
30 #include "ui/views/focus/accelerator_handler.h" | |
31 #include "ui/views/focus/view_storage.h" | |
32 #include "ui/views/test/views_test_base.h" | |
33 #include "ui/views/touchui/gesture_manager.h" | |
34 #include "ui/views/widget/native_widget.h" | |
35 #include "ui/views/widget/root_view.h" | |
36 #include "ui/views/window/dialog_delegate.h" | |
37 #include "views/background.h" | |
38 #include "views/view.h" | |
39 #include "views/views_delegate.h" | |
40 | |
41 #if defined(OS_WIN) | |
42 #include "ui/views/test/test_views_delegate.h" | |
43 #endif | |
44 #if defined(USE_AURA) | |
45 #include "ui/aura/desktop.h" | |
46 #endif | |
47 | |
48 using ::testing::_; | |
49 | |
50 namespace { | |
51 | |
52 // Returns true if |ancestor| is an ancestor of |layer|. | |
53 bool LayerIsAncestor(const ui::Layer* ancestor, const ui::Layer* layer) { | |
54 while (layer && layer != ancestor) | |
55 layer = layer->parent(); | |
56 return layer == ancestor; | |
57 } | |
58 | |
59 // Convenience functions for walking a View tree. | |
60 const views::View* FirstView(const views::View* view) { | |
61 const views::View* v = view; | |
62 while (v->has_children()) | |
63 v = v->child_at(0); | |
64 return v; | |
65 } | |
66 | |
67 const views::View* NextView(const views::View* view) { | |
68 const views::View* v = view; | |
69 const views::View* parent = v->parent(); | |
70 if (!parent) | |
71 return NULL; | |
72 int next = parent->GetIndexOf(v) + 1; | |
73 if (next != parent->child_count()) | |
74 return FirstView(parent->child_at(next)); | |
75 return parent; | |
76 } | |
77 | |
78 // Convenience functions for walking a Layer tree. | |
79 const ui::Layer* FirstLayer(const ui::Layer* layer) { | |
80 const ui::Layer* l = layer; | |
81 while (l->children().size() > 0) | |
82 l = l->children()[0]; | |
83 return l; | |
84 } | |
85 | |
86 const ui::Layer* NextLayer(const ui::Layer* layer) { | |
87 const ui::Layer* parent = layer->parent(); | |
88 if (!parent) | |
89 return NULL; | |
90 const std::vector<ui::Layer*> children = parent->children(); | |
91 size_t index; | |
92 for (index = 0; index < children.size(); index++) { | |
93 if (children[index] == layer) | |
94 break; | |
95 } | |
96 size_t next = index + 1; | |
97 if (next < children.size()) | |
98 return FirstLayer(children[next]); | |
99 return parent; | |
100 } | |
101 | |
102 // Given the root nodes of a View tree and a Layer tree, makes sure the two | |
103 // trees are in sync. | |
104 bool ViewAndLayerTreeAreConsistent(const views::View* view, | |
105 const ui::Layer* layer) { | |
106 const views::View* v = FirstView(view); | |
107 const ui::Layer* l = FirstLayer(layer); | |
108 while (v && l) { | |
109 // Find the view with a layer. | |
110 while (v && !v->layer()) | |
111 v = NextView(v); | |
112 EXPECT_TRUE(v); | |
113 if (!v) | |
114 return false; | |
115 | |
116 // Check if the View tree and the Layer tree are in sync. | |
117 EXPECT_EQ(l, v->layer()); | |
118 if (v->layer() != l) | |
119 return false; | |
120 | |
121 // Check if the visibility states of the View and the Layer are in sync. | |
122 EXPECT_EQ(l->IsDrawn(), v->IsVisibleInRootView()); | |
123 if (v->IsVisibleInRootView() != l->IsDrawn()) { | |
124 for (const views::View* vv = v; vv; vv = vv->parent()) | |
125 LOG(ERROR) << "V: " << vv << " " << vv->IsVisible() << " " | |
126 << vv->IsVisibleInRootView() << " " << vv->layer(); | |
127 for (const ui::Layer* ll = l; ll; ll = ll->parent()) | |
128 LOG(ERROR) << "L: " << ll << " " << ll->IsDrawn(); | |
129 return false; | |
130 } | |
131 | |
132 // Check if the size of the View and the Layer are in sync. | |
133 EXPECT_EQ(l->bounds(), v->bounds()); | |
134 if (v->bounds() != l->bounds()) | |
135 return false; | |
136 | |
137 if (v == view || l == layer) | |
138 return v == view && l == layer; | |
139 | |
140 v = NextView(v); | |
141 l = NextLayer(l); | |
142 } | |
143 | |
144 return false; | |
145 } | |
146 | |
147 // Constructs a View tree with the specified depth. | |
148 void ConstructTree(views::View* view, int depth) { | |
149 if (depth == 0) | |
150 return; | |
151 int count = base::RandInt(1, 5); | |
152 for (int i = 0; i < count; i++) { | |
153 views::View* v = new views::View; | |
154 view->AddChildView(v); | |
155 if (base::RandDouble() > 0.5) | |
156 v->SetPaintToLayer(true); | |
157 if (base::RandDouble() < 0.2) | |
158 v->SetVisible(false); | |
159 | |
160 ConstructTree(v, depth - 1); | |
161 } | |
162 } | |
163 | |
164 void ScrambleTree(views::View* view) { | |
165 int count = view->child_count(); | |
166 if (count == 0) | |
167 return; | |
168 for (int i = 0; i < count; i++) { | |
169 ScrambleTree(view->child_at(i)); | |
170 } | |
171 | |
172 if (count > 1) { | |
173 int a = base::RandInt(0, count - 1); | |
174 int b = base::RandInt(0, count - 1); | |
175 | |
176 views::View* view_a = view->child_at(a); | |
177 views::View* view_b = view->child_at(b); | |
178 view->ReorderChildView(view_a, b); | |
179 view->ReorderChildView(view_b, a); | |
180 } | |
181 | |
182 if (!view->layer() && base::RandDouble() < 0.1) | |
183 view->SetPaintToLayer(true); | |
184 | |
185 if (base::RandDouble() < 0.1) | |
186 view->SetVisible(!view->IsVisible()); | |
187 } | |
188 | |
189 } | |
190 | |
191 namespace views { | |
192 | |
193 typedef ViewsTestBase ViewTest; | |
194 | |
195 // A derived class for testing purpose. | |
196 class TestView : public View { | |
197 public: | |
198 TestView() : View(), in_touch_sequence_(false) {} | |
199 virtual ~TestView() {} | |
200 | |
201 // Reset all test state | |
202 void Reset() { | |
203 did_change_bounds_ = false; | |
204 last_mouse_event_type_ = 0; | |
205 location_.SetPoint(0, 0); | |
206 last_touch_event_type_ = 0; | |
207 last_touch_event_was_handled_ = false; | |
208 last_clip_.setEmpty(); | |
209 accelerator_count_map_.clear(); | |
210 } | |
211 | |
212 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; | |
213 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE; | |
214 virtual bool OnMouseDragged(const MouseEvent& event) OVERRIDE; | |
215 virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE; | |
216 virtual ui::TouchStatus OnTouchEvent(const TouchEvent& event) OVERRIDE; | |
217 virtual void Paint(gfx::Canvas* canvas) OVERRIDE; | |
218 virtual void SchedulePaintInRect(const gfx::Rect& rect) OVERRIDE; | |
219 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; | |
220 | |
221 // OnBoundsChanged. | |
222 bool did_change_bounds_; | |
223 gfx::Rect new_bounds_; | |
224 | |
225 // MouseEvent. | |
226 int last_mouse_event_type_; | |
227 gfx::Point location_; | |
228 | |
229 // Painting. | |
230 std::vector<gfx::Rect> scheduled_paint_rects_; | |
231 | |
232 // TouchEvent. | |
233 int last_touch_event_type_; | |
234 bool last_touch_event_was_handled_; | |
235 bool in_touch_sequence_; | |
236 | |
237 // Painting. | |
238 SkRect last_clip_; | |
239 | |
240 // Accelerators. | |
241 std::map<ui::Accelerator, int> accelerator_count_map_; | |
242 }; | |
243 | |
244 // Mock instance of the GestureManager for testing. | |
245 class MockGestureManager : public GestureManager { | |
246 public: | |
247 // Reset all test state. | |
248 void Reset() { | |
249 last_touch_event_ = 0; | |
250 last_view_ = NULL; | |
251 previously_handled_flag_ = false; | |
252 dispatched_synthetic_event_ = false; | |
253 } | |
254 | |
255 bool ProcessTouchEventForGesture(const TouchEvent& event, | |
256 View* source, | |
257 ui::TouchStatus status); | |
258 MockGestureManager(); | |
259 | |
260 bool previously_handled_flag_; | |
261 int last_touch_event_; | |
262 View *last_view_; | |
263 bool dispatched_synthetic_event_; | |
264 | |
265 DISALLOW_COPY_AND_ASSIGN(MockGestureManager); | |
266 }; | |
267 | |
268 // A view subclass that ignores all touch events for testing purposes. | |
269 class TestViewIgnoreTouch : public TestView { | |
270 public: | |
271 TestViewIgnoreTouch() : TestView() {} | |
272 virtual ~TestViewIgnoreTouch() {} | |
273 | |
274 private: | |
275 virtual ui::TouchStatus OnTouchEvent(const TouchEvent& event) OVERRIDE; | |
276 }; | |
277 | |
278 //////////////////////////////////////////////////////////////////////////////// | |
279 // OnBoundsChanged | |
280 //////////////////////////////////////////////////////////////////////////////// | |
281 | |
282 void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
283 did_change_bounds_ = true; | |
284 new_bounds_ = bounds(); | |
285 } | |
286 | |
287 TEST_F(ViewTest, OnBoundsChanged) { | |
288 TestView v; | |
289 | |
290 gfx::Rect prev_rect(0, 0, 200, 200); | |
291 gfx::Rect new_rect(100, 100, 250, 250); | |
292 | |
293 v.SetBoundsRect(prev_rect); | |
294 v.Reset(); | |
295 v.SetBoundsRect(new_rect); | |
296 | |
297 EXPECT_EQ(v.did_change_bounds_, true); | |
298 EXPECT_EQ(v.new_bounds_, new_rect); | |
299 EXPECT_EQ(v.bounds(), new_rect); | |
300 } | |
301 | |
302 //////////////////////////////////////////////////////////////////////////////// | |
303 // MouseEvent | |
304 //////////////////////////////////////////////////////////////////////////////// | |
305 | |
306 bool TestView::OnMousePressed(const MouseEvent& event) { | |
307 last_mouse_event_type_ = event.type(); | |
308 location_.SetPoint(event.x(), event.y()); | |
309 return true; | |
310 } | |
311 | |
312 bool TestView::OnMouseDragged(const MouseEvent& event) { | |
313 last_mouse_event_type_ = event.type(); | |
314 location_.SetPoint(event.x(), event.y()); | |
315 return true; | |
316 } | |
317 | |
318 void TestView::OnMouseReleased(const MouseEvent& event) { | |
319 last_mouse_event_type_ = event.type(); | |
320 location_.SetPoint(event.x(), event.y()); | |
321 } | |
322 | |
323 TEST_F(ViewTest, MouseEvent) { | |
324 TestView* v1 = new TestView(); | |
325 v1->SetBounds(0, 0, 300, 300); | |
326 | |
327 TestView* v2 = new TestView(); | |
328 v2->SetBounds(100, 100, 100, 100); | |
329 | |
330 scoped_ptr<Widget> widget(new Widget); | |
331 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
332 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
333 params.bounds = gfx::Rect(50, 50, 650, 650); | |
334 widget->Init(params); | |
335 View* root = widget->GetRootView(); | |
336 | |
337 root->AddChildView(v1); | |
338 v1->AddChildView(v2); | |
339 | |
340 v1->Reset(); | |
341 v2->Reset(); | |
342 | |
343 MouseEvent pressed(ui::ET_MOUSE_PRESSED, | |
344 110, | |
345 120, | |
346 ui::EF_LEFT_BUTTON_DOWN); | |
347 root->OnMousePressed(pressed); | |
348 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_PRESSED); | |
349 EXPECT_EQ(v2->location_.x(), 10); | |
350 EXPECT_EQ(v2->location_.y(), 20); | |
351 // Make sure v1 did not receive the event | |
352 EXPECT_EQ(v1->last_mouse_event_type_, 0); | |
353 | |
354 // Drag event out of bounds. Should still go to v2 | |
355 v1->Reset(); | |
356 v2->Reset(); | |
357 MouseEvent dragged(ui::ET_MOUSE_DRAGGED, | |
358 50, | |
359 40, | |
360 ui::EF_LEFT_BUTTON_DOWN); | |
361 root->OnMouseDragged(dragged); | |
362 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_DRAGGED); | |
363 EXPECT_EQ(v2->location_.x(), -50); | |
364 EXPECT_EQ(v2->location_.y(), -60); | |
365 // Make sure v1 did not receive the event | |
366 EXPECT_EQ(v1->last_mouse_event_type_, 0); | |
367 | |
368 // Releasted event out of bounds. Should still go to v2 | |
369 v1->Reset(); | |
370 v2->Reset(); | |
371 MouseEvent released(ui::ET_MOUSE_RELEASED, 0, 0, 0); | |
372 root->OnMouseDragged(released); | |
373 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_RELEASED); | |
374 EXPECT_EQ(v2->location_.x(), -100); | |
375 EXPECT_EQ(v2->location_.y(), -100); | |
376 // Make sure v1 did not receive the event | |
377 EXPECT_EQ(v1->last_mouse_event_type_, 0); | |
378 | |
379 widget->CloseNow(); | |
380 } | |
381 | |
382 //////////////////////////////////////////////////////////////////////////////// | |
383 // TouchEvent | |
384 //////////////////////////////////////////////////////////////////////////////// | |
385 bool MockGestureManager::ProcessTouchEventForGesture( | |
386 const TouchEvent& event, | |
387 View* source, | |
388 ui::TouchStatus status) { | |
389 if (status != ui::TOUCH_STATUS_UNKNOWN) { | |
390 dispatched_synthetic_event_ = false; | |
391 return false; | |
392 } | |
393 last_touch_event_ = event.type(); | |
394 last_view_ = source; | |
395 previously_handled_flag_ = status != ui::TOUCH_STATUS_UNKNOWN; | |
396 dispatched_synthetic_event_ = true; | |
397 return true; | |
398 } | |
399 | |
400 MockGestureManager::MockGestureManager() { | |
401 } | |
402 | |
403 ui::TouchStatus TestView::OnTouchEvent(const TouchEvent& event) { | |
404 last_touch_event_type_ = event.type(); | |
405 location_.SetPoint(event.x(), event.y()); | |
406 if (!in_touch_sequence_) { | |
407 if (event.type() == ui::ET_TOUCH_PRESSED) { | |
408 in_touch_sequence_ = true; | |
409 return ui::TOUCH_STATUS_START; | |
410 } | |
411 } else { | |
412 if (event.type() == ui::ET_TOUCH_RELEASED) { | |
413 in_touch_sequence_ = false; | |
414 return ui::TOUCH_STATUS_END; | |
415 } | |
416 return ui::TOUCH_STATUS_CONTINUE; | |
417 } | |
418 return last_touch_event_was_handled_ ? ui::TOUCH_STATUS_CONTINUE : | |
419 ui::TOUCH_STATUS_UNKNOWN; | |
420 } | |
421 | |
422 ui::TouchStatus TestViewIgnoreTouch::OnTouchEvent(const TouchEvent& event) { | |
423 return ui::TOUCH_STATUS_UNKNOWN; | |
424 } | |
425 | |
426 TEST_F(ViewTest, TouchEvent) { | |
427 MockGestureManager gm; | |
428 | |
429 TestView* v1 = new TestView(); | |
430 v1->SetBounds(0, 0, 300, 300); | |
431 | |
432 TestView* v2 = new TestView(); | |
433 v2->SetBounds(100, 100, 100, 100); | |
434 | |
435 TestView* v3 = new TestViewIgnoreTouch(); | |
436 v3->SetBounds(0, 0, 100, 100); | |
437 | |
438 scoped_ptr<Widget> widget(new Widget()); | |
439 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
440 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
441 params.bounds = gfx::Rect(50, 50, 650, 650); | |
442 widget->Init(params); | |
443 View* root = widget->GetRootView(); | |
444 | |
445 root->AddChildView(v1); | |
446 static_cast<internal::RootView*>(root)->SetGestureManagerForTesting(&gm); | |
447 v1->AddChildView(v2); | |
448 v2->AddChildView(v3); | |
449 | |
450 // |v3| completely obscures |v2|, but all the touch events on |v3| should | |
451 // reach |v2| because |v3| doesn't process any touch events. | |
452 | |
453 // Make sure if none of the views handle the touch event, the gesture manager | |
454 // does. | |
455 v1->Reset(); | |
456 v2->Reset(); | |
457 gm.Reset(); | |
458 | |
459 TouchEvent unhandled(ui::ET_TOUCH_MOVED, | |
460 400, | |
461 400, | |
462 0, /* no flags */ | |
463 0, /* first finger touch */ | |
464 1.0, 0.0, 1.0, 0.0); | |
465 root->OnTouchEvent(unhandled); | |
466 | |
467 EXPECT_EQ(v1->last_touch_event_type_, 0); | |
468 EXPECT_EQ(v2->last_touch_event_type_, 0); | |
469 | |
470 EXPECT_EQ(gm.previously_handled_flag_, false); | |
471 EXPECT_EQ(gm.last_touch_event_, ui::ET_TOUCH_MOVED); | |
472 EXPECT_EQ(gm.last_view_, root); | |
473 EXPECT_EQ(gm.dispatched_synthetic_event_, true); | |
474 | |
475 // Test press, drag, release touch sequence. | |
476 v1->Reset(); | |
477 v2->Reset(); | |
478 gm.Reset(); | |
479 | |
480 TouchEvent pressed(ui::ET_TOUCH_PRESSED, | |
481 110, | |
482 120, | |
483 0, /* no flags */ | |
484 0, /* first finger touch */ | |
485 1.0, 0.0, 1.0, 0.0); | |
486 v2->last_touch_event_was_handled_ = true; | |
487 root->OnTouchEvent(pressed); | |
488 | |
489 EXPECT_EQ(v2->last_touch_event_type_, ui::ET_TOUCH_PRESSED); | |
490 EXPECT_EQ(v2->location_.x(), 10); | |
491 EXPECT_EQ(v2->location_.y(), 20); | |
492 // Make sure v1 did not receive the event | |
493 EXPECT_EQ(v1->last_touch_event_type_, 0); | |
494 | |
495 // Since v2 handled the touch-event, the gesture manager should not handle it. | |
496 EXPECT_EQ(gm.last_touch_event_, 0); | |
497 EXPECT_EQ(NULL, gm.last_view_); | |
498 EXPECT_EQ(gm.previously_handled_flag_, false); | |
499 | |
500 // Drag event out of bounds. Should still go to v2 | |
501 v1->Reset(); | |
502 v2->Reset(); | |
503 TouchEvent dragged(ui::ET_TOUCH_MOVED, | |
504 50, | |
505 40, | |
506 0, /* no flags */ | |
507 0, /* first finger touch */ | |
508 1.0, 0.0, 1.0, 0.0); | |
509 | |
510 root->OnTouchEvent(dragged); | |
511 EXPECT_EQ(v2->last_touch_event_type_, ui::ET_TOUCH_MOVED); | |
512 EXPECT_EQ(v2->location_.x(), -50); | |
513 EXPECT_EQ(v2->location_.y(), -60); | |
514 // Make sure v1 did not receive the event | |
515 EXPECT_EQ(v1->last_touch_event_type_, 0); | |
516 | |
517 EXPECT_EQ(gm.last_touch_event_, 0); | |
518 EXPECT_EQ(NULL, gm.last_view_); | |
519 EXPECT_EQ(gm.previously_handled_flag_, false); | |
520 | |
521 // Released event out of bounds. Should still go to v2 | |
522 v1->Reset(); | |
523 v2->Reset(); | |
524 TouchEvent released(ui::ET_TOUCH_RELEASED, 0, 0, 0, 0 /* first finger */, | |
525 1.0, 0.0, 1.0, 0.0); | |
526 v2->last_touch_event_was_handled_ = true; | |
527 root->OnTouchEvent(released); | |
528 EXPECT_EQ(v2->last_touch_event_type_, ui::ET_TOUCH_RELEASED); | |
529 EXPECT_EQ(v2->location_.x(), -100); | |
530 EXPECT_EQ(v2->location_.y(), -100); | |
531 // Make sure v1 did not receive the event | |
532 EXPECT_EQ(v1->last_touch_event_type_, 0); | |
533 | |
534 EXPECT_EQ(gm.last_touch_event_, 0); | |
535 EXPECT_EQ(NULL, gm.last_view_); | |
536 EXPECT_EQ(gm.previously_handled_flag_, false); | |
537 | |
538 widget->CloseNow(); | |
539 } | |
540 | |
541 //////////////////////////////////////////////////////////////////////////////// | |
542 // Painting | |
543 //////////////////////////////////////////////////////////////////////////////// | |
544 | |
545 void TestView::Paint(gfx::Canvas* canvas) { | |
546 canvas->GetSkCanvas()->getClipBounds(&last_clip_); | |
547 } | |
548 | |
549 void TestView::SchedulePaintInRect(const gfx::Rect& rect) { | |
550 scheduled_paint_rects_.push_back(rect); | |
551 View::SchedulePaintInRect(rect); | |
552 } | |
553 | |
554 void CheckRect(const SkRect& check_rect, const SkRect& target_rect) { | |
555 EXPECT_EQ(target_rect.fLeft, check_rect.fLeft); | |
556 EXPECT_EQ(target_rect.fRight, check_rect.fRight); | |
557 EXPECT_EQ(target_rect.fTop, check_rect.fTop); | |
558 EXPECT_EQ(target_rect.fBottom, check_rect.fBottom); | |
559 } | |
560 | |
561 /* This test is disabled because it is flakey on some systems. | |
562 TEST_F(ViewTest, DISABLED_Painting) { | |
563 // Determine if InvalidateRect generates an empty paint rectangle. | |
564 EmptyWindow paint_window(CRect(50, 50, 650, 650)); | |
565 paint_window.RedrawWindow(CRect(0, 0, 600, 600), NULL, | |
566 RDW_UPDATENOW | RDW_INVALIDATE | RDW_ALLCHILDREN); | |
567 bool empty_paint = paint_window.empty_paint(); | |
568 | |
569 NativeWidgetWin window; | |
570 window.set_delete_on_destroy(false); | |
571 window.set_window_style(WS_OVERLAPPEDWINDOW); | |
572 window.Init(NULL, gfx::Rect(50, 50, 650, 650), NULL); | |
573 View* root = window.GetRootView(); | |
574 | |
575 TestView* v1 = new TestView(); | |
576 v1->SetBounds(0, 0, 650, 650); | |
577 root->AddChildView(v1); | |
578 | |
579 TestView* v2 = new TestView(); | |
580 v2->SetBounds(10, 10, 80, 80); | |
581 v1->AddChildView(v2); | |
582 | |
583 TestView* v3 = new TestView(); | |
584 v3->SetBounds(10, 10, 60, 60); | |
585 v2->AddChildView(v3); | |
586 | |
587 TestView* v4 = new TestView(); | |
588 v4->SetBounds(10, 200, 100, 100); | |
589 v1->AddChildView(v4); | |
590 | |
591 // Make sure to paint current rects | |
592 PaintRootView(root, empty_paint); | |
593 | |
594 | |
595 v1->Reset(); | |
596 v2->Reset(); | |
597 v3->Reset(); | |
598 v4->Reset(); | |
599 v3->SchedulePaintInRect(gfx::Rect(10, 10, 10, 10)); | |
600 PaintRootView(root, empty_paint); | |
601 | |
602 SkRect tmp_rect; | |
603 | |
604 tmp_rect.set(SkIntToScalar(10), | |
605 SkIntToScalar(10), | |
606 SkIntToScalar(20), | |
607 SkIntToScalar(20)); | |
608 CheckRect(v3->last_clip_, tmp_rect); | |
609 | |
610 tmp_rect.set(SkIntToScalar(20), | |
611 SkIntToScalar(20), | |
612 SkIntToScalar(30), | |
613 SkIntToScalar(30)); | |
614 CheckRect(v2->last_clip_, tmp_rect); | |
615 | |
616 tmp_rect.set(SkIntToScalar(30), | |
617 SkIntToScalar(30), | |
618 SkIntToScalar(40), | |
619 SkIntToScalar(40)); | |
620 CheckRect(v1->last_clip_, tmp_rect); | |
621 | |
622 // Make sure v4 was not painted | |
623 tmp_rect.setEmpty(); | |
624 CheckRect(v4->last_clip_, tmp_rect); | |
625 | |
626 window.DestroyWindow(); | |
627 } | |
628 */ | |
629 | |
630 #if defined(OS_WIN) | |
631 TEST_F(ViewTest, RemoveNotification) { | |
632 #else | |
633 // TODO(beng): stopped working with widget hierarchy split, | |
634 // http://crbug.com/82364 | |
635 TEST_F(ViewTest, DISABLED_RemoveNotification) { | |
636 #endif | |
637 ViewStorage* vs = ViewStorage::GetInstance(); | |
638 Widget* widget = new Widget; | |
639 widget->Init(Widget::InitParams(Widget::InitParams::TYPE_POPUP)); | |
640 View* root_view = widget->GetRootView(); | |
641 | |
642 View* v1 = new View; | |
643 int s1 = vs->CreateStorageID(); | |
644 vs->StoreView(s1, v1); | |
645 root_view->AddChildView(v1); | |
646 View* v11 = new View; | |
647 int s11 = vs->CreateStorageID(); | |
648 vs->StoreView(s11, v11); | |
649 v1->AddChildView(v11); | |
650 View* v111 = new View; | |
651 int s111 = vs->CreateStorageID(); | |
652 vs->StoreView(s111, v111); | |
653 v11->AddChildView(v111); | |
654 View* v112 = new View; | |
655 int s112 = vs->CreateStorageID(); | |
656 vs->StoreView(s112, v112); | |
657 v11->AddChildView(v112); | |
658 View* v113 = new View; | |
659 int s113 = vs->CreateStorageID(); | |
660 vs->StoreView(s113, v113); | |
661 v11->AddChildView(v113); | |
662 View* v1131 = new View; | |
663 int s1131 = vs->CreateStorageID(); | |
664 vs->StoreView(s1131, v1131); | |
665 v113->AddChildView(v1131); | |
666 View* v12 = new View; | |
667 int s12 = vs->CreateStorageID(); | |
668 vs->StoreView(s12, v12); | |
669 v1->AddChildView(v12); | |
670 | |
671 View* v2 = new View; | |
672 int s2 = vs->CreateStorageID(); | |
673 vs->StoreView(s2, v2); | |
674 root_view->AddChildView(v2); | |
675 View* v21 = new View; | |
676 int s21 = vs->CreateStorageID(); | |
677 vs->StoreView(s21, v21); | |
678 v2->AddChildView(v21); | |
679 View* v211 = new View; | |
680 int s211 = vs->CreateStorageID(); | |
681 vs->StoreView(s211, v211); | |
682 v21->AddChildView(v211); | |
683 | |
684 size_t stored_views = vs->view_count(); | |
685 | |
686 // Try removing a leaf view. | |
687 v21->RemoveChildView(v211); | |
688 EXPECT_EQ(stored_views - 1, vs->view_count()); | |
689 EXPECT_EQ(NULL, vs->RetrieveView(s211)); | |
690 delete v211; // We won't use this one anymore. | |
691 | |
692 // Now try removing a view with a hierarchy of depth 1. | |
693 v11->RemoveChildView(v113); | |
694 EXPECT_EQ(stored_views - 3, vs->view_count()); | |
695 EXPECT_EQ(NULL, vs->RetrieveView(s113)); | |
696 EXPECT_EQ(NULL, vs->RetrieveView(s1131)); | |
697 delete v113; // We won't use this one anymore. | |
698 | |
699 // Now remove even more. | |
700 root_view->RemoveChildView(v1); | |
701 EXPECT_EQ(NULL, vs->RetrieveView(s1)); | |
702 EXPECT_EQ(NULL, vs->RetrieveView(s11)); | |
703 EXPECT_EQ(NULL, vs->RetrieveView(s12)); | |
704 EXPECT_EQ(NULL, vs->RetrieveView(s111)); | |
705 EXPECT_EQ(NULL, vs->RetrieveView(s112)); | |
706 | |
707 // Put v1 back for more tests. | |
708 root_view->AddChildView(v1); | |
709 vs->StoreView(s1, v1); | |
710 | |
711 // Synchronously closing the window deletes the view hierarchy, which should | |
712 // remove all its views from ViewStorage. | |
713 widget->CloseNow(); | |
714 EXPECT_EQ(stored_views - 10, vs->view_count()); | |
715 EXPECT_EQ(NULL, vs->RetrieveView(s1)); | |
716 EXPECT_EQ(NULL, vs->RetrieveView(s12)); | |
717 EXPECT_EQ(NULL, vs->RetrieveView(s11)); | |
718 EXPECT_EQ(NULL, vs->RetrieveView(s12)); | |
719 EXPECT_EQ(NULL, vs->RetrieveView(s21)); | |
720 EXPECT_EQ(NULL, vs->RetrieveView(s111)); | |
721 EXPECT_EQ(NULL, vs->RetrieveView(s112)); | |
722 } | |
723 | |
724 namespace { | |
725 class HitTestView : public View { | |
726 public: | |
727 explicit HitTestView(bool has_hittest_mask) | |
728 : has_hittest_mask_(has_hittest_mask) { | |
729 } | |
730 virtual ~HitTestView() {} | |
731 | |
732 protected: | |
733 // Overridden from View: | |
734 virtual bool HasHitTestMask() const { | |
735 return has_hittest_mask_; | |
736 } | |
737 virtual void GetHitTestMask(gfx::Path* mask) const { | |
738 DCHECK(has_hittest_mask_); | |
739 DCHECK(mask); | |
740 | |
741 SkScalar w = SkIntToScalar(width()); | |
742 SkScalar h = SkIntToScalar(height()); | |
743 | |
744 // Create a triangular mask within the bounds of this View. | |
745 mask->moveTo(w / 2, 0); | |
746 mask->lineTo(w, h); | |
747 mask->lineTo(0, h); | |
748 mask->close(); | |
749 } | |
750 | |
751 private: | |
752 bool has_hittest_mask_; | |
753 | |
754 DISALLOW_COPY_AND_ASSIGN(HitTestView); | |
755 }; | |
756 | |
757 gfx::Point ConvertPointToView(View* view, const gfx::Point& p) { | |
758 gfx::Point tmp(p); | |
759 View::ConvertPointToView(view->GetWidget()->GetRootView(), view, &tmp); | |
760 return tmp; | |
761 } | |
762 | |
763 void RotateCounterclockwise(ui::Transform& transform) { | |
764 transform.matrix().set3x3(0, -1, 0, | |
765 1, 0, 0, | |
766 0, 0, 1); | |
767 } | |
768 | |
769 void RotateClockwise(ui::Transform& transform) { | |
770 transform.matrix().set3x3( 0, 1, 0, | |
771 -1, 0, 0, | |
772 0, 0, 1); | |
773 } | |
774 | |
775 } // namespace | |
776 | |
777 TEST_F(ViewTest, HitTestMasks) { | |
778 Widget* widget = new Widget; | |
779 widget->Init(Widget::InitParams(Widget::InitParams::TYPE_POPUP)); | |
780 View* root_view = widget->GetRootView(); | |
781 root_view->SetBounds(0, 0, 500, 500); | |
782 | |
783 gfx::Rect v1_bounds = gfx::Rect(0, 0, 100, 100); | |
784 HitTestView* v1 = new HitTestView(false); | |
785 v1->SetBoundsRect(v1_bounds); | |
786 root_view->AddChildView(v1); | |
787 | |
788 gfx::Rect v2_bounds = gfx::Rect(105, 0, 100, 100); | |
789 HitTestView* v2 = new HitTestView(true); | |
790 v2->SetBoundsRect(v2_bounds); | |
791 root_view->AddChildView(v2); | |
792 | |
793 gfx::Point v1_centerpoint = v1_bounds.CenterPoint(); | |
794 gfx::Point v2_centerpoint = v2_bounds.CenterPoint(); | |
795 gfx::Point v1_origin = v1_bounds.origin(); | |
796 gfx::Point v2_origin = v2_bounds.origin(); | |
797 | |
798 // Test HitTest | |
799 EXPECT_TRUE(v1->HitTest(ConvertPointToView(v1, v1_centerpoint))); | |
800 EXPECT_TRUE(v2->HitTest(ConvertPointToView(v2, v2_centerpoint))); | |
801 | |
802 EXPECT_TRUE(v1->HitTest(ConvertPointToView(v1, v1_origin))); | |
803 EXPECT_FALSE(v2->HitTest(ConvertPointToView(v2, v2_origin))); | |
804 | |
805 // Test GetEventHandlerForPoint | |
806 EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_centerpoint)); | |
807 EXPECT_EQ(v2, root_view->GetEventHandlerForPoint(v2_centerpoint)); | |
808 EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_origin)); | |
809 EXPECT_EQ(root_view, root_view->GetEventHandlerForPoint(v2_origin)); | |
810 | |
811 widget->CloseNow(); | |
812 } | |
813 | |
814 TEST_F(ViewTest, Textfield) { | |
815 const string16 kText = ASCIIToUTF16("Reality is that which, when you stop " | |
816 "believing it, doesn't go away."); | |
817 const string16 kExtraText = ASCIIToUTF16("Pretty deep, Philip!"); | |
818 const string16 kEmptyString; | |
819 | |
820 ui::Clipboard clipboard; | |
821 | |
822 Widget* widget = new Widget; | |
823 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
824 params.bounds = gfx::Rect(0, 0, 100, 100); | |
825 widget->Init(params); | |
826 View* root_view = widget->GetRootView(); | |
827 | |
828 Textfield* textfield = new Textfield(); | |
829 root_view->AddChildView(textfield); | |
830 | |
831 // Test setting, appending text. | |
832 textfield->SetText(kText); | |
833 EXPECT_EQ(kText, textfield->text()); | |
834 textfield->AppendText(kExtraText); | |
835 EXPECT_EQ(kText + kExtraText, textfield->text()); | |
836 textfield->SetText(string16()); | |
837 EXPECT_EQ(kEmptyString, textfield->text()); | |
838 | |
839 // Test selection related methods. | |
840 textfield->SetText(kText); | |
841 EXPECT_EQ(kEmptyString, textfield->GetSelectedText()); | |
842 textfield->SelectAll(); | |
843 EXPECT_EQ(kText, textfield->text()); | |
844 textfield->ClearSelection(); | |
845 EXPECT_EQ(kEmptyString, textfield->GetSelectedText()); | |
846 | |
847 widget->CloseNow(); | |
848 } | |
849 | |
850 #if defined(OS_WIN) && !defined(USE_AURA) | |
851 | |
852 // Tests that the Textfield view respond appropiately to cut/copy/paste. | |
853 TEST_F(ViewTest, TextfieldCutCopyPaste) { | |
854 const string16 kNormalText = ASCIIToUTF16("Normal"); | |
855 const string16 kReadOnlyText = ASCIIToUTF16("Read only"); | |
856 const string16 kPasswordText = ASCIIToUTF16("Password! ** Secret stuff **"); | |
857 | |
858 ui::Clipboard clipboard; | |
859 | |
860 Widget* widget = new Widget; | |
861 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
862 params.bounds = gfx::Rect(0, 0, 100, 100); | |
863 widget->Init(params); | |
864 View* root_view = widget->GetRootView(); | |
865 | |
866 Textfield* normal = new Textfield(); | |
867 Textfield* read_only = new Textfield(); | |
868 read_only->SetReadOnly(true); | |
869 Textfield* password = new Textfield(Textfield::STYLE_PASSWORD); | |
870 | |
871 root_view->AddChildView(normal); | |
872 root_view->AddChildView(read_only); | |
873 root_view->AddChildView(password); | |
874 | |
875 normal->SetText(kNormalText); | |
876 read_only->SetText(kReadOnlyText); | |
877 password->SetText(kPasswordText); | |
878 | |
879 // | |
880 // Test cut. | |
881 // | |
882 ASSERT_TRUE(normal->GetTestingHandle()); | |
883 normal->SelectAll(); | |
884 ::SendMessage(normal->GetTestingHandle(), WM_CUT, 0, 0); | |
885 | |
886 string16 result; | |
887 clipboard.ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | |
888 EXPECT_EQ(kNormalText, result); | |
889 normal->SetText(kNormalText); // Let's revert to the original content. | |
890 | |
891 ASSERT_TRUE(read_only->GetTestingHandle()); | |
892 read_only->SelectAll(); | |
893 ::SendMessage(read_only->GetTestingHandle(), WM_CUT, 0, 0); | |
894 result.clear(); | |
895 clipboard.ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | |
896 // Cut should have failed, so the clipboard content should not have changed. | |
897 EXPECT_EQ(kNormalText, result); | |
898 | |
899 ASSERT_TRUE(password->GetTestingHandle()); | |
900 password->SelectAll(); | |
901 ::SendMessage(password->GetTestingHandle(), WM_CUT, 0, 0); | |
902 result.clear(); | |
903 clipboard.ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | |
904 // Cut should have failed, so the clipboard content should not have changed. | |
905 EXPECT_EQ(kNormalText, result); | |
906 | |
907 // | |
908 // Test copy. | |
909 // | |
910 | |
911 // Let's start with read_only as the clipboard already contains the content | |
912 // of normal. | |
913 read_only->SelectAll(); | |
914 ::SendMessage(read_only->GetTestingHandle(), WM_COPY, 0, 0); | |
915 result.clear(); | |
916 clipboard.ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | |
917 EXPECT_EQ(kReadOnlyText, result); | |
918 | |
919 normal->SelectAll(); | |
920 ::SendMessage(normal->GetTestingHandle(), WM_COPY, 0, 0); | |
921 result.clear(); | |
922 clipboard.ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | |
923 EXPECT_EQ(kNormalText, result); | |
924 | |
925 password->SelectAll(); | |
926 ::SendMessage(password->GetTestingHandle(), WM_COPY, 0, 0); | |
927 result.clear(); | |
928 clipboard.ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | |
929 // We don't let you copy from a password field, clipboard should not have | |
930 // changed. | |
931 EXPECT_EQ(kNormalText, result); | |
932 | |
933 // | |
934 // Test Paste. | |
935 // | |
936 // Note that we use GetWindowText instead of Textfield::GetText below as the | |
937 // text in the Textfield class is synced to the text of the HWND on | |
938 // WM_KEYDOWN messages that we are not simulating here. | |
939 | |
940 // Attempting to copy kNormalText in a read-only text-field should fail. | |
941 read_only->SelectAll(); | |
942 ::SendMessage(read_only->GetTestingHandle(), WM_KEYDOWN, 0, 0); | |
943 wchar_t buffer[1024] = { 0 }; | |
944 ::GetWindowText(read_only->GetTestingHandle(), buffer, 1024); | |
945 EXPECT_EQ(kReadOnlyText, string16(buffer)); | |
946 | |
947 password->SelectAll(); | |
948 ::SendMessage(password->GetTestingHandle(), WM_PASTE, 0, 0); | |
949 ::GetWindowText(password->GetTestingHandle(), buffer, 1024); | |
950 EXPECT_EQ(kNormalText, string16(buffer)); | |
951 | |
952 // Copy from read_only so the string we are pasting is not the same as the | |
953 // current one. | |
954 read_only->SelectAll(); | |
955 ::SendMessage(read_only->GetTestingHandle(), WM_COPY, 0, 0); | |
956 normal->SelectAll(); | |
957 ::SendMessage(normal->GetTestingHandle(), WM_PASTE, 0, 0); | |
958 ::GetWindowText(normal->GetTestingHandle(), buffer, 1024); | |
959 EXPECT_EQ(kReadOnlyText, string16(buffer)); | |
960 widget->CloseNow(); | |
961 } | |
962 #endif | |
963 | |
964 //////////////////////////////////////////////////////////////////////////////// | |
965 // Accelerators | |
966 //////////////////////////////////////////////////////////////////////////////// | |
967 bool TestView::AcceleratorPressed(const ui::Accelerator& accelerator) { | |
968 accelerator_count_map_[accelerator]++; | |
969 return true; | |
970 } | |
971 | |
972 #if defined(OS_WIN) && !defined(USE_AURA) | |
973 TEST_F(ViewTest, ActivateAccelerator) { | |
974 // Register a keyboard accelerator before the view is added to a window. | |
975 ui::Accelerator return_accelerator(ui::VKEY_RETURN, false, false, false); | |
976 TestView* view = new TestView(); | |
977 view->Reset(); | |
978 view->AddAccelerator(return_accelerator); | |
979 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | |
980 | |
981 // Create a window and add the view as its child. | |
982 scoped_ptr<Widget> widget(new Widget); | |
983 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
984 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
985 params.bounds = gfx::Rect(0, 0, 100, 100); | |
986 widget->Init(params); | |
987 View* root = widget->GetRootView(); | |
988 root->AddChildView(view); | |
989 widget->Show(); | |
990 | |
991 // Get the focus manager. | |
992 FocusManager* focus_manager = widget->GetFocusManager(); | |
993 ASSERT_TRUE(focus_manager); | |
994 | |
995 // Hit the return key and see if it takes effect. | |
996 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); | |
997 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); | |
998 | |
999 // Hit the escape key. Nothing should happen. | |
1000 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, false, false, false); | |
1001 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); | |
1002 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); | |
1003 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 0); | |
1004 | |
1005 // Now register the escape key and hit it again. | |
1006 view->AddAccelerator(escape_accelerator); | |
1007 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator)); | |
1008 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); | |
1009 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1); | |
1010 | |
1011 // Remove the return key accelerator. | |
1012 view->RemoveAccelerator(return_accelerator); | |
1013 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | |
1014 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 1); | |
1015 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1); | |
1016 | |
1017 // Add it again. Hit the return key and the escape key. | |
1018 view->AddAccelerator(return_accelerator); | |
1019 EXPECT_TRUE(focus_manager->ProcessAccelerator(return_accelerator)); | |
1020 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); | |
1021 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 1); | |
1022 EXPECT_TRUE(focus_manager->ProcessAccelerator(escape_accelerator)); | |
1023 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); | |
1024 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); | |
1025 | |
1026 // Remove all the accelerators. | |
1027 view->ResetAccelerators(); | |
1028 EXPECT_FALSE(focus_manager->ProcessAccelerator(return_accelerator)); | |
1029 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); | |
1030 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); | |
1031 EXPECT_FALSE(focus_manager->ProcessAccelerator(escape_accelerator)); | |
1032 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 2); | |
1033 EXPECT_EQ(view->accelerator_count_map_[escape_accelerator], 2); | |
1034 | |
1035 widget->CloseNow(); | |
1036 } | |
1037 #endif | |
1038 | |
1039 #if defined(OS_WIN) && !defined(USE_AURA) | |
1040 TEST_F(ViewTest, HiddenViewWithAccelerator) { | |
1041 ui::Accelerator return_accelerator(ui::VKEY_RETURN, false, false, false); | |
1042 TestView* view = new TestView(); | |
1043 view->Reset(); | |
1044 view->AddAccelerator(return_accelerator); | |
1045 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | |
1046 | |
1047 scoped_ptr<Widget> widget(new Widget); | |
1048 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
1049 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
1050 params.bounds = gfx::Rect(0, 0, 100, 100); | |
1051 widget->Init(params); | |
1052 View* root = widget->GetRootView(); | |
1053 root->AddChildView(view); | |
1054 widget->Show(); | |
1055 | |
1056 FocusManager* focus_manager = widget->GetFocusManager(); | |
1057 ASSERT_TRUE(focus_manager); | |
1058 | |
1059 view->SetVisible(false); | |
1060 EXPECT_EQ(NULL, | |
1061 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | |
1062 | |
1063 view->SetVisible(true); | |
1064 EXPECT_EQ(view, | |
1065 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | |
1066 | |
1067 widget->CloseNow(); | |
1068 } | |
1069 #endif | |
1070 | |
1071 #if defined(OS_WIN) && !defined(USE_AURA) | |
1072 TEST_F(ViewTest, ViewInHiddenWidgetWithAccelerator) { | |
1073 ui::Accelerator return_accelerator(ui::VKEY_RETURN, false, false, false); | |
1074 TestView* view = new TestView(); | |
1075 view->Reset(); | |
1076 view->AddAccelerator(return_accelerator); | |
1077 EXPECT_EQ(view->accelerator_count_map_[return_accelerator], 0); | |
1078 | |
1079 scoped_ptr<Widget> widget(new Widget); | |
1080 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
1081 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
1082 params.bounds = gfx::Rect(0, 0, 100, 100); | |
1083 widget->Init(params); | |
1084 View* root = widget->GetRootView(); | |
1085 root->AddChildView(view); | |
1086 | |
1087 FocusManager* focus_manager = widget->GetFocusManager(); | |
1088 ASSERT_TRUE(focus_manager); | |
1089 | |
1090 EXPECT_EQ(NULL, | |
1091 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | |
1092 | |
1093 widget->Show(); | |
1094 EXPECT_EQ(view, | |
1095 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | |
1096 | |
1097 widget->Hide(); | |
1098 EXPECT_EQ(NULL, | |
1099 focus_manager->GetCurrentTargetForAccelerator(return_accelerator)); | |
1100 | |
1101 widget->CloseNow(); | |
1102 } | |
1103 #endif | |
1104 | |
1105 #if defined(OS_WIN) && !defined(USE_AURA) | |
1106 //////////////////////////////////////////////////////////////////////////////// | |
1107 // Mouse-wheel message rerouting | |
1108 //////////////////////////////////////////////////////////////////////////////// | |
1109 class ScrollableTestView : public View { | |
1110 public: | |
1111 ScrollableTestView() { } | |
1112 | |
1113 virtual gfx::Size GetPreferredSize() { | |
1114 return gfx::Size(100, 10000); | |
1115 } | |
1116 | |
1117 virtual void Layout() { | |
1118 SizeToPreferredSize(); | |
1119 } | |
1120 }; | |
1121 | |
1122 class TestViewWithControls : public View { | |
1123 public: | |
1124 TestViewWithControls() { | |
1125 text_field_ = new Textfield(); | |
1126 AddChildView(text_field_); | |
1127 } | |
1128 | |
1129 Textfield* text_field_; | |
1130 }; | |
1131 | |
1132 class SimpleWidgetDelegate : public WidgetDelegate { | |
1133 public: | |
1134 explicit SimpleWidgetDelegate(View* contents) : contents_(contents) { } | |
1135 | |
1136 virtual void DeleteDelegate() { delete this; } | |
1137 | |
1138 virtual View* GetContentsView() { return contents_; } | |
1139 | |
1140 virtual Widget* GetWidget() { return contents_->GetWidget(); } | |
1141 virtual const Widget* GetWidget() const { return contents_->GetWidget(); } | |
1142 | |
1143 private: | |
1144 View* contents_; | |
1145 }; | |
1146 | |
1147 // Tests that the mouse-wheel messages are correctly rerouted to the window | |
1148 // under the mouse. | |
1149 // TODO(jcampan): http://crbug.com/10572 Disabled as it fails on the Vista build | |
1150 // bot. | |
1151 // Note that this fails for a variety of reasons: | |
1152 // - focused view is apparently reset across window activations and never | |
1153 // properly restored | |
1154 // - this test depends on you not having any other window visible open under the | |
1155 // area that it opens the test windows. --beng | |
1156 TEST_F(ViewTest, DISABLED_RerouteMouseWheelTest) { | |
1157 TestViewWithControls* view_with_controls = new TestViewWithControls(); | |
1158 Widget* window1 = Widget::CreateWindowWithBounds( | |
1159 new SimpleWidgetDelegate(view_with_controls), | |
1160 gfx::Rect(0, 0, 100, 100)); | |
1161 window1->Show(); | |
1162 ScrollView* scroll_view = new ScrollView(); | |
1163 scroll_view->SetContents(new ScrollableTestView()); | |
1164 Widget* window2 = Widget::CreateWindowWithBounds( | |
1165 new SimpleWidgetDelegate(scroll_view), | |
1166 gfx::Rect(200, 200, 100, 100)); | |
1167 window2->Show(); | |
1168 EXPECT_EQ(0, scroll_view->GetVisibleRect().y()); | |
1169 | |
1170 // Make the window1 active, as this is what it would be in real-world. | |
1171 window1->Activate(); | |
1172 | |
1173 // Let's send a mouse-wheel message to the different controls and check that | |
1174 // it is rerouted to the window under the mouse (effectively scrolling the | |
1175 // scroll-view). | |
1176 | |
1177 // First to the Window's HWND. | |
1178 ::SendMessage(view_with_controls->GetWidget()->GetNativeView(), | |
1179 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250)); | |
1180 EXPECT_EQ(20, scroll_view->GetVisibleRect().y()); | |
1181 | |
1182 // Then the text-field. | |
1183 ::SendMessage(view_with_controls->text_field_->GetTestingHandle(), | |
1184 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(250, 250)); | |
1185 EXPECT_EQ(80, scroll_view->GetVisibleRect().y()); | |
1186 | |
1187 // Ensure we don't scroll when the mouse is not over that window. | |
1188 ::SendMessage(view_with_controls->text_field_->GetTestingHandle(), | |
1189 WM_MOUSEWHEEL, MAKEWPARAM(0, -20), MAKELPARAM(50, 50)); | |
1190 EXPECT_EQ(80, scroll_view->GetVisibleRect().y()); | |
1191 | |
1192 window1->CloseNow(); | |
1193 window2->CloseNow(); | |
1194 } | |
1195 #endif | |
1196 | |
1197 //////////////////////////////////////////////////////////////////////////////// | |
1198 // Dialogs' default button | |
1199 //////////////////////////////////////////////////////////////////////////////// | |
1200 | |
1201 class MockMenuModel : public ui::MenuModel { | |
1202 public: | |
1203 MOCK_CONST_METHOD0(HasIcons, bool()); | |
1204 MOCK_CONST_METHOD1(GetFirstItemIndex, int(gfx::NativeMenu native_menu)); | |
1205 MOCK_CONST_METHOD0(GetItemCount, int()); | |
1206 MOCK_CONST_METHOD1(GetTypeAt, ItemType(int index)); | |
1207 MOCK_CONST_METHOD1(GetCommandIdAt, int(int index)); | |
1208 MOCK_CONST_METHOD1(GetLabelAt, string16(int index)); | |
1209 MOCK_CONST_METHOD1(IsItemDynamicAt, bool(int index)); | |
1210 MOCK_CONST_METHOD1(GetLabelFontAt, const gfx::Font* (int index)); | |
1211 MOCK_CONST_METHOD2(GetAcceleratorAt, bool(int index, | |
1212 ui::Accelerator* accelerator)); | |
1213 MOCK_CONST_METHOD1(IsItemCheckedAt, bool(int index)); | |
1214 MOCK_CONST_METHOD1(GetGroupIdAt, int(int index)); | |
1215 MOCK_METHOD2(GetIconAt, bool(int index, SkBitmap* icon)); | |
1216 MOCK_CONST_METHOD1(GetButtonMenuItemAt, ui::ButtonMenuItemModel*(int index)); | |
1217 MOCK_CONST_METHOD1(IsEnabledAt, bool(int index)); | |
1218 MOCK_CONST_METHOD1(IsVisibleAt, bool(int index)); | |
1219 MOCK_CONST_METHOD1(GetSubmenuModelAt, MenuModel*(int index)); | |
1220 MOCK_METHOD1(HighlightChangedTo, void(int index)); | |
1221 MOCK_METHOD1(ActivatedAt, void(int index)); | |
1222 MOCK_METHOD2(ActivatedAt, void(int index, int disposition)); | |
1223 MOCK_METHOD0(MenuWillShow, void()); | |
1224 MOCK_METHOD0(MenuClosed, void()); | |
1225 MOCK_METHOD1(SetMenuModelDelegate, void(ui::MenuModelDelegate* delegate)); | |
1226 MOCK_METHOD3(GetModelAndIndexForCommandId, bool(int command_id, | |
1227 MenuModel** model, int* index)); | |
1228 }; | |
1229 | |
1230 class TestDialog : public DialogDelegate, public ButtonListener { | |
1231 public: | |
1232 explicit TestDialog(MockMenuModel* mock_menu_model) | |
1233 : contents_(NULL), | |
1234 button1_(NULL), | |
1235 button2_(NULL), | |
1236 checkbox_(NULL), | |
1237 button_drop_(NULL), | |
1238 last_pressed_button_(NULL), | |
1239 mock_menu_model_(mock_menu_model), | |
1240 canceled_(false), | |
1241 oked_(false), | |
1242 closeable_(false), | |
1243 widget_(NULL) { | |
1244 } | |
1245 | |
1246 void TearDown() { | |
1247 // Now we can close safely. | |
1248 closeable_ = true; | |
1249 widget_->Close(); | |
1250 widget_ = NULL; | |
1251 // delegate has to be alive while shutting down. | |
1252 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
1253 } | |
1254 | |
1255 // DialogDelegate implementation: | |
1256 virtual int GetDefaultDialogButton() const OVERRIDE { | |
1257 return ui::DIALOG_BUTTON_OK; | |
1258 } | |
1259 | |
1260 virtual View* GetContentsView() OVERRIDE { | |
1261 if (!contents_) { | |
1262 contents_ = new View; | |
1263 button1_ = new NativeTextButton(this, ASCIIToUTF16("Button1")); | |
1264 button2_ = new NativeTextButton(this, ASCIIToUTF16("Button2")); | |
1265 checkbox_ = new Checkbox(ASCIIToUTF16("My checkbox")); | |
1266 button_drop_ = new ButtonDropDown(this, mock_menu_model_); | |
1267 contents_->AddChildView(button1_); | |
1268 contents_->AddChildView(button2_); | |
1269 contents_->AddChildView(checkbox_); | |
1270 contents_->AddChildView(button_drop_); | |
1271 } | |
1272 return contents_; | |
1273 } | |
1274 | |
1275 // Prevent the dialog from really closing (so we can click the OK/Cancel | |
1276 // buttons to our heart's content). | |
1277 virtual bool Cancel() OVERRIDE { | |
1278 canceled_ = true; | |
1279 return closeable_; | |
1280 } | |
1281 virtual bool Accept() OVERRIDE { | |
1282 oked_ = true; | |
1283 return closeable_; | |
1284 } | |
1285 | |
1286 virtual Widget* GetWidget() OVERRIDE { | |
1287 return widget_; | |
1288 } | |
1289 virtual const Widget* GetWidget() const OVERRIDE { | |
1290 return widget_; | |
1291 } | |
1292 | |
1293 // ButtonListener implementation. | |
1294 virtual void ButtonPressed(Button* sender, const Event& event) OVERRIDE { | |
1295 last_pressed_button_ = sender; | |
1296 } | |
1297 | |
1298 void ResetStates() { | |
1299 oked_ = false; | |
1300 canceled_ = false; | |
1301 last_pressed_button_ = NULL; | |
1302 } | |
1303 | |
1304 // Set up expectations for methods that are called when an (empty) menu is | |
1305 // shown from a drop down button. | |
1306 void ExpectShowDropMenu() { | |
1307 if (mock_menu_model_) { | |
1308 EXPECT_CALL(*mock_menu_model_, HasIcons()); | |
1309 EXPECT_CALL(*mock_menu_model_, GetFirstItemIndex(_)); | |
1310 EXPECT_CALL(*mock_menu_model_, GetItemCount()); | |
1311 EXPECT_CALL(*mock_menu_model_, MenuClosed()); | |
1312 } | |
1313 } | |
1314 | |
1315 View* contents_; | |
1316 NativeTextButton* button1_; | |
1317 NativeTextButton* button2_; | |
1318 Checkbox* checkbox_; | |
1319 ButtonDropDown* button_drop_; | |
1320 Button* last_pressed_button_; | |
1321 MockMenuModel* mock_menu_model_; | |
1322 | |
1323 bool canceled_; | |
1324 bool oked_; | |
1325 bool closeable_; | |
1326 Widget* widget_; | |
1327 }; | |
1328 | |
1329 class DefaultButtonTest : public ViewTest { | |
1330 public: | |
1331 enum ButtonID { | |
1332 OK, | |
1333 CANCEL, | |
1334 BUTTON1, | |
1335 BUTTON2 | |
1336 }; | |
1337 | |
1338 DefaultButtonTest() | |
1339 : focus_manager_(NULL), | |
1340 test_dialog_(NULL), | |
1341 client_view_(NULL), | |
1342 ok_button_(NULL), | |
1343 cancel_button_(NULL) { | |
1344 } | |
1345 | |
1346 virtual void SetUp() OVERRIDE { | |
1347 ViewTest::SetUp(); | |
1348 test_dialog_ = new TestDialog(NULL); | |
1349 Widget* window = | |
1350 Widget::CreateWindowWithBounds(test_dialog_, gfx::Rect(0, 0, 100, 100)); | |
1351 test_dialog_->widget_ = window; | |
1352 window->Show(); | |
1353 focus_manager_ = test_dialog_->contents_->GetFocusManager(); | |
1354 ASSERT_TRUE(focus_manager_ != NULL); | |
1355 client_view_ = | |
1356 static_cast<DialogClientView*>(window->client_view()); | |
1357 ok_button_ = client_view_->ok_button(); | |
1358 cancel_button_ = client_view_->cancel_button(); | |
1359 } | |
1360 | |
1361 virtual void TearDown() OVERRIDE { | |
1362 test_dialog_->TearDown(); | |
1363 ViewTest::TearDown(); | |
1364 } | |
1365 | |
1366 void SimulatePressingEnterAndCheckDefaultButton(ButtonID button_id) { | |
1367 KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0); | |
1368 focus_manager_->OnKeyEvent(event); | |
1369 switch (button_id) { | |
1370 case OK: | |
1371 EXPECT_TRUE(test_dialog_->oked_); | |
1372 EXPECT_FALSE(test_dialog_->canceled_); | |
1373 EXPECT_FALSE(test_dialog_->last_pressed_button_); | |
1374 break; | |
1375 case CANCEL: | |
1376 EXPECT_FALSE(test_dialog_->oked_); | |
1377 EXPECT_TRUE(test_dialog_->canceled_); | |
1378 EXPECT_FALSE(test_dialog_->last_pressed_button_); | |
1379 break; | |
1380 case BUTTON1: | |
1381 EXPECT_FALSE(test_dialog_->oked_); | |
1382 EXPECT_FALSE(test_dialog_->canceled_); | |
1383 EXPECT_TRUE(test_dialog_->last_pressed_button_ == | |
1384 test_dialog_->button1_); | |
1385 break; | |
1386 case BUTTON2: | |
1387 EXPECT_FALSE(test_dialog_->oked_); | |
1388 EXPECT_FALSE(test_dialog_->canceled_); | |
1389 EXPECT_TRUE(test_dialog_->last_pressed_button_ == | |
1390 test_dialog_->button2_); | |
1391 break; | |
1392 } | |
1393 test_dialog_->ResetStates(); | |
1394 } | |
1395 | |
1396 FocusManager* focus_manager_; | |
1397 TestDialog* test_dialog_; | |
1398 DialogClientView* client_view_; | |
1399 NativeTextButton* ok_button_; | |
1400 NativeTextButton* cancel_button_; | |
1401 }; | |
1402 | |
1403 TEST_F(DefaultButtonTest, DialogDefaultButtonTest) { | |
1404 // Window has just been shown, we expect the default button specified in the | |
1405 // DialogDelegate. | |
1406 EXPECT_TRUE(ok_button_->is_default()); | |
1407 | |
1408 // Simulate pressing enter, that should trigger the OK button. | |
1409 SimulatePressingEnterAndCheckDefaultButton(OK); | |
1410 | |
1411 // Simulate focusing another button, it should become the default button. | |
1412 client_view_->OnWillChangeFocus(ok_button_, test_dialog_->button1_); | |
1413 EXPECT_FALSE(ok_button_->is_default()); | |
1414 EXPECT_TRUE(test_dialog_->button1_->is_default()); | |
1415 // Simulate pressing enter, that should trigger button1. | |
1416 SimulatePressingEnterAndCheckDefaultButton(BUTTON1); | |
1417 | |
1418 // Now select something that is not a button, the OK should become the default | |
1419 // button again. | |
1420 client_view_->OnWillChangeFocus(test_dialog_->button1_, | |
1421 test_dialog_->checkbox_); | |
1422 EXPECT_TRUE(ok_button_->is_default()); | |
1423 EXPECT_FALSE(test_dialog_->button1_->is_default()); | |
1424 SimulatePressingEnterAndCheckDefaultButton(OK); | |
1425 | |
1426 // Select yet another button. | |
1427 client_view_->OnWillChangeFocus(test_dialog_->checkbox_, | |
1428 test_dialog_->button2_); | |
1429 EXPECT_FALSE(ok_button_->is_default()); | |
1430 EXPECT_FALSE(test_dialog_->button1_->is_default()); | |
1431 EXPECT_TRUE(test_dialog_->button2_->is_default()); | |
1432 SimulatePressingEnterAndCheckDefaultButton(BUTTON2); | |
1433 | |
1434 // Focus nothing. | |
1435 client_view_->OnWillChangeFocus(test_dialog_->button2_, NULL); | |
1436 EXPECT_TRUE(ok_button_->is_default()); | |
1437 EXPECT_FALSE(test_dialog_->button1_->is_default()); | |
1438 EXPECT_FALSE(test_dialog_->button2_->is_default()); | |
1439 SimulatePressingEnterAndCheckDefaultButton(OK); | |
1440 | |
1441 // Focus the cancel button. | |
1442 client_view_->OnWillChangeFocus(NULL, cancel_button_); | |
1443 EXPECT_FALSE(ok_button_->is_default()); | |
1444 EXPECT_TRUE(cancel_button_->is_default()); | |
1445 EXPECT_FALSE(test_dialog_->button1_->is_default()); | |
1446 EXPECT_FALSE(test_dialog_->button2_->is_default()); | |
1447 SimulatePressingEnterAndCheckDefaultButton(CANCEL); | |
1448 } | |
1449 | |
1450 class ButtonDropDownTest : public ViewTest { | |
1451 public: | |
1452 ButtonDropDownTest() | |
1453 : test_dialog_(NULL), | |
1454 button_as_view_(NULL) { | |
1455 } | |
1456 | |
1457 virtual void SetUp() OVERRIDE { | |
1458 ViewTest::SetUp(); | |
1459 test_dialog_ = new TestDialog(&mock_menu_model_); | |
1460 Widget* window = | |
1461 Widget::CreateWindowWithBounds(test_dialog_, gfx::Rect(0, 0, 100, 100)); | |
1462 test_dialog_->widget_ = window; | |
1463 window->Show(); | |
1464 test_dialog_->button_drop_->SetBounds(0, 0, 100, 100); | |
1465 // We have to cast the button back into a View in order to invoke it's | |
1466 // OnMouseReleased method. | |
1467 button_as_view_ = static_cast<View*>(test_dialog_->button_drop_); | |
1468 } | |
1469 | |
1470 virtual void TearDown() OVERRIDE { | |
1471 test_dialog_->TearDown(); | |
1472 ViewTest::TearDown(); | |
1473 } | |
1474 | |
1475 TestDialog* test_dialog_; | |
1476 MockMenuModel mock_menu_model_; | |
1477 // This is owned by test_dialog_. | |
1478 View* button_as_view_; | |
1479 | |
1480 private: | |
1481 DISALLOW_COPY_AND_ASSIGN(ButtonDropDownTest); | |
1482 }; | |
1483 | |
1484 // Ensure that regular clicks on the drop down button still work. (i.e. - the | |
1485 // click events are processed and the listener gets the click) | |
1486 TEST_F(ButtonDropDownTest, RegularClickTest) { | |
1487 MouseEvent press_event(ui::ET_MOUSE_PRESSED, 1, 1, ui::EF_LEFT_BUTTON_DOWN); | |
1488 MouseEvent release_event(ui::ET_MOUSE_RELEASED, 1, 1, | |
1489 ui::EF_LEFT_BUTTON_DOWN); | |
1490 button_as_view_->OnMousePressed(press_event); | |
1491 button_as_view_->OnMouseReleased(release_event); | |
1492 EXPECT_EQ(test_dialog_->last_pressed_button_, test_dialog_->button_drop_); | |
1493 } | |
1494 | |
1495 //////////////////////////////////////////////////////////////////////////////// | |
1496 // View hierarchy / Visibility changes | |
1497 //////////////////////////////////////////////////////////////////////////////// | |
1498 /* | |
1499 TEST_F(ViewTest, ChangeVisibility) { | |
1500 #if defined(OS_LINUX) | |
1501 // Make CRITICAL messages fatal | |
1502 // TODO(oshima): we probably should enable this for entire tests on linux. | |
1503 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL); | |
1504 #endif | |
1505 scoped_ptr<Widget> window(CreateWidget()); | |
1506 window->Init(NULL, gfx::Rect(0, 0, 500, 300)); | |
1507 View* root_view = window->GetRootView(); | |
1508 NativeTextButton* native = new NativeTextButton(NULL, ASCIIToUTF16("Native")); | |
1509 | |
1510 root_view->SetContentsView(native); | |
1511 native->SetVisible(true); | |
1512 | |
1513 root_view->RemoveChildView(native); | |
1514 native->SetVisible(false); | |
1515 // Change visibility to true with no widget. | |
1516 native->SetVisible(true); | |
1517 | |
1518 root_view->SetContentsView(native); | |
1519 native->SetVisible(true); | |
1520 } | |
1521 */ | |
1522 | |
1523 //////////////////////////////////////////////////////////////////////////////// | |
1524 // Native view hierachy | |
1525 //////////////////////////////////////////////////////////////////////////////// | |
1526 class TestNativeViewHierarchy : public View { | |
1527 public: | |
1528 TestNativeViewHierarchy() { | |
1529 } | |
1530 | |
1531 virtual void NativeViewHierarchyChanged(bool attached, | |
1532 gfx::NativeView native_view, | |
1533 internal::RootView* root_view) { | |
1534 NotificationInfo info; | |
1535 info.attached = attached; | |
1536 info.native_view = native_view; | |
1537 info.root_view = root_view; | |
1538 notifications_.push_back(info); | |
1539 }; | |
1540 struct NotificationInfo { | |
1541 bool attached; | |
1542 gfx::NativeView native_view; | |
1543 internal::RootView* root_view; | |
1544 }; | |
1545 static const size_t kTotalViews = 2; | |
1546 std::vector<NotificationInfo> notifications_; | |
1547 }; | |
1548 | |
1549 class TestChangeNativeViewHierarchy { | |
1550 public: | |
1551 explicit TestChangeNativeViewHierarchy(ViewTest *view_test) { | |
1552 view_test_ = view_test; | |
1553 native_host_ = new NativeViewHost(); | |
1554 host_ = new Widget; | |
1555 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
1556 params.bounds = gfx::Rect(0, 0, 500, 300); | |
1557 host_->Init(params); | |
1558 host_->GetRootView()->AddChildView(native_host_); | |
1559 for (size_t i = 0; i < TestNativeViewHierarchy::kTotalViews; ++i) { | |
1560 windows_[i] = new Widget; | |
1561 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); | |
1562 params.parent = host_->GetNativeView(); | |
1563 params.bounds = gfx::Rect(0, 0, 500, 300); | |
1564 windows_[i]->Init(params); | |
1565 root_views_[i] = windows_[i]->GetRootView(); | |
1566 test_views_[i] = new TestNativeViewHierarchy; | |
1567 root_views_[i]->AddChildView(test_views_[i]); | |
1568 } | |
1569 } | |
1570 | |
1571 ~TestChangeNativeViewHierarchy() { | |
1572 for (size_t i = 0; i < TestNativeViewHierarchy::kTotalViews; ++i) { | |
1573 windows_[i]->Close(); | |
1574 } | |
1575 host_->Close(); | |
1576 // Will close and self-delete widgets - no need to manually delete them. | |
1577 view_test_->RunPendingMessages(); | |
1578 } | |
1579 | |
1580 void CheckEnumeratingNativeWidgets() { | |
1581 if (!host_->GetTopLevelWidget()) | |
1582 return; | |
1583 Widget::Widgets widgets; | |
1584 Widget::GetAllChildWidgets(host_->GetNativeView(), &widgets); | |
1585 EXPECT_EQ(TestNativeViewHierarchy::kTotalViews + 1, widgets.size()); | |
1586 // Unfortunately there is no guarantee the sequence of views here so always | |
1587 // go through all of them. | |
1588 for (Widget::Widgets::iterator i = widgets.begin(); | |
1589 i != widgets.end(); ++i) { | |
1590 View* root_view = (*i)->GetRootView(); | |
1591 if (host_->GetRootView() == root_view) | |
1592 continue; | |
1593 size_t j; | |
1594 for (j = 0; j < TestNativeViewHierarchy::kTotalViews; ++j) | |
1595 if (root_views_[j] == root_view) | |
1596 break; | |
1597 // EXPECT_LT/GT/GE() fails to compile with class-defined constants | |
1598 // with gcc, with error | |
1599 // "error: undefined reference to 'TestNativeViewHierarchy::kTotalViews'" | |
1600 // so I forced to use EXPECT_TRUE() instead. | |
1601 EXPECT_TRUE(TestNativeViewHierarchy::kTotalViews > j); | |
1602 } | |
1603 } | |
1604 | |
1605 void CheckChangingHierarhy() { | |
1606 size_t i; | |
1607 for (i = 0; i < TestNativeViewHierarchy::kTotalViews; ++i) { | |
1608 // TODO(georgey): use actual hierarchy changes to send notifications. | |
1609 static_cast<internal::RootView*>(root_views_[i])-> | |
1610 NotifyNativeViewHierarchyChanged(false, host_->GetNativeView()); | |
1611 static_cast<internal::RootView*>(root_views_[i])-> | |
1612 NotifyNativeViewHierarchyChanged(true, host_->GetNativeView()); | |
1613 } | |
1614 for (i = 0; i < TestNativeViewHierarchy::kTotalViews; ++i) { | |
1615 ASSERT_EQ(static_cast<size_t>(2), test_views_[i]->notifications_.size()); | |
1616 EXPECT_FALSE(test_views_[i]->notifications_[0].attached); | |
1617 EXPECT_EQ(host_->GetNativeView(), | |
1618 test_views_[i]->notifications_[0].native_view); | |
1619 EXPECT_EQ(root_views_[i], test_views_[i]->notifications_[0].root_view); | |
1620 EXPECT_TRUE(test_views_[i]->notifications_[1].attached); | |
1621 EXPECT_EQ(host_->GetNativeView(), | |
1622 test_views_[i]->notifications_[1].native_view); | |
1623 EXPECT_EQ(root_views_[i], test_views_[i]->notifications_[1].root_view); | |
1624 } | |
1625 } | |
1626 | |
1627 NativeViewHost* native_host_; | |
1628 Widget* host_; | |
1629 Widget* windows_[TestNativeViewHierarchy::kTotalViews]; | |
1630 View* root_views_[TestNativeViewHierarchy::kTotalViews]; | |
1631 TestNativeViewHierarchy* test_views_[TestNativeViewHierarchy::kTotalViews]; | |
1632 ViewTest* view_test_; | |
1633 }; | |
1634 | |
1635 TEST_F(ViewTest, ChangeNativeViewHierarchyFindRoots) { | |
1636 // TODO(georgey): Fix the test for Linux | |
1637 #if defined(OS_WIN) | |
1638 TestChangeNativeViewHierarchy test(this); | |
1639 test.CheckEnumeratingNativeWidgets(); | |
1640 #endif | |
1641 } | |
1642 | |
1643 TEST_F(ViewTest, ChangeNativeViewHierarchyChangeHierarchy) { | |
1644 // TODO(georgey): Fix the test for Linux | |
1645 #if defined(OS_WIN) | |
1646 TestChangeNativeViewHierarchy test(this); | |
1647 test.CheckChangingHierarhy(); | |
1648 #endif | |
1649 } | |
1650 | |
1651 //////////////////////////////////////////////////////////////////////////////// | |
1652 // Transformations | |
1653 //////////////////////////////////////////////////////////////////////////////// | |
1654 | |
1655 class TransformPaintView : public TestView { | |
1656 public: | |
1657 TransformPaintView() {} | |
1658 virtual ~TransformPaintView() {} | |
1659 | |
1660 void ClearScheduledPaintRect() { | |
1661 scheduled_paint_rect_ = gfx::Rect(); | |
1662 } | |
1663 | |
1664 gfx::Rect scheduled_paint_rect() const { return scheduled_paint_rect_; } | |
1665 | |
1666 // Overridden from View: | |
1667 virtual void SchedulePaintInRect(const gfx::Rect& rect) { | |
1668 gfx::Rect xrect = ConvertRectToParent(rect); | |
1669 scheduled_paint_rect_ = scheduled_paint_rect_.Union(xrect); | |
1670 } | |
1671 | |
1672 private: | |
1673 gfx::Rect scheduled_paint_rect_; | |
1674 | |
1675 DISALLOW_COPY_AND_ASSIGN(TransformPaintView); | |
1676 }; | |
1677 | |
1678 TEST_F(ViewTest, TransformPaint) { | |
1679 TransformPaintView* v1 = new TransformPaintView(); | |
1680 v1->SetBounds(0, 0, 500, 300); | |
1681 | |
1682 TestView* v2 = new TestView(); | |
1683 v2->SetBounds(100, 100, 200, 100); | |
1684 | |
1685 Widget* widget = new Widget; | |
1686 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
1687 params.bounds = gfx::Rect(50, 50, 650, 650); | |
1688 widget->Init(params); | |
1689 widget->Show(); | |
1690 View* root = widget->GetRootView(); | |
1691 | |
1692 root->AddChildView(v1); | |
1693 v1->AddChildView(v2); | |
1694 | |
1695 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|. | |
1696 v1->ClearScheduledPaintRect(); | |
1697 v2->SchedulePaint(); | |
1698 | |
1699 EXPECT_EQ(gfx::Rect(100, 100, 200, 100), v1->scheduled_paint_rect()); | |
1700 | |
1701 // Rotate |v1| counter-clockwise. | |
1702 ui::Transform transform; | |
1703 RotateCounterclockwise(transform); | |
1704 transform.SetTranslateY(500.0f); | |
1705 v1->SetTransform(transform); | |
1706 | |
1707 // |v2| now occupies (100, 200) to (200, 400) in |root|. | |
1708 | |
1709 v1->ClearScheduledPaintRect(); | |
1710 v2->SchedulePaint(); | |
1711 | |
1712 EXPECT_EQ(gfx::Rect(100, 200, 100, 200), v1->scheduled_paint_rect()); | |
1713 | |
1714 widget->CloseNow(); | |
1715 } | |
1716 | |
1717 TEST_F(ViewTest, TransformEvent) { | |
1718 TestView* v1 = new TestView(); | |
1719 v1->SetBounds(0, 0, 500, 300); | |
1720 | |
1721 TestView* v2 = new TestView(); | |
1722 v2->SetBounds(100, 100, 200, 100); | |
1723 | |
1724 Widget* widget = new Widget; | |
1725 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
1726 params.bounds = gfx::Rect(50, 50, 650, 650); | |
1727 widget->Init(params); | |
1728 View* root = widget->GetRootView(); | |
1729 | |
1730 root->AddChildView(v1); | |
1731 v1->AddChildView(v2); | |
1732 | |
1733 // At this moment, |v2| occupies (100, 100) to (300, 200) in |root|. | |
1734 | |
1735 // Rotate |v1| counter-clockwise. | |
1736 ui::Transform transform(v1->GetTransform()); | |
1737 RotateCounterclockwise(transform); | |
1738 transform.SetTranslateY(500.0f); | |
1739 v1->SetTransform(transform); | |
1740 | |
1741 // |v2| now occupies (100, 200) to (200, 400) in |root|. | |
1742 v1->Reset(); | |
1743 v2->Reset(); | |
1744 | |
1745 MouseEvent pressed(ui::ET_MOUSE_PRESSED, | |
1746 110, 210, | |
1747 ui::EF_LEFT_BUTTON_DOWN); | |
1748 root->OnMousePressed(pressed); | |
1749 EXPECT_EQ(0, v1->last_mouse_event_type_); | |
1750 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_); | |
1751 EXPECT_EQ(190, v2->location_.x()); | |
1752 EXPECT_EQ(10, v2->location_.y()); | |
1753 | |
1754 MouseEvent released(ui::ET_MOUSE_RELEASED, 0, 0, 0); | |
1755 root->OnMouseReleased(released); | |
1756 | |
1757 // Now rotate |v2| inside |v1| clockwise. | |
1758 transform = v2->GetTransform(); | |
1759 RotateClockwise(transform); | |
1760 transform.SetTranslateX(100.0f); | |
1761 v2->SetTransform(transform); | |
1762 | |
1763 // Now, |v2| occupies (100, 100) to (200, 300) in |v1|, and (100, 300) to | |
1764 // (300, 400) in |root|. | |
1765 | |
1766 v1->Reset(); | |
1767 v2->Reset(); | |
1768 | |
1769 MouseEvent p2(ui::ET_MOUSE_PRESSED, | |
1770 110, 320, | |
1771 ui::EF_LEFT_BUTTON_DOWN); | |
1772 root->OnMousePressed(p2); | |
1773 EXPECT_EQ(0, v1->last_mouse_event_type_); | |
1774 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v2->last_mouse_event_type_); | |
1775 EXPECT_EQ(10, v2->location_.x()); | |
1776 EXPECT_EQ(20, v2->location_.y()); | |
1777 | |
1778 root->OnMouseReleased(released); | |
1779 | |
1780 v1->SetTransform(ui::Transform()); | |
1781 v2->SetTransform(ui::Transform()); | |
1782 | |
1783 TestView* v3 = new TestView(); | |
1784 v3->SetBounds(10, 10, 20, 30); | |
1785 v2->AddChildView(v3); | |
1786 | |
1787 // Rotate |v3| clockwise with respect to |v2|. | |
1788 transform = v1->GetTransform(); | |
1789 RotateClockwise(transform); | |
1790 transform.SetTranslateX(30.0f); | |
1791 v3->SetTransform(transform); | |
1792 | |
1793 // Scale |v2| with respect to |v1| along both axis. | |
1794 transform = v2->GetTransform(); | |
1795 transform.SetScale(0.8f, 0.5f); | |
1796 v2->SetTransform(transform); | |
1797 | |
1798 // |v3| occupies (108, 105) to (132, 115) in |root|. | |
1799 | |
1800 v1->Reset(); | |
1801 v2->Reset(); | |
1802 v3->Reset(); | |
1803 | |
1804 MouseEvent p3(ui::ET_MOUSE_PRESSED, | |
1805 112, 110, | |
1806 ui::EF_LEFT_BUTTON_DOWN); | |
1807 root->OnMousePressed(p3); | |
1808 | |
1809 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_); | |
1810 EXPECT_EQ(10, v3->location_.x()); | |
1811 EXPECT_EQ(25, v3->location_.y()); | |
1812 | |
1813 root->OnMouseReleased(released); | |
1814 | |
1815 v1->SetTransform(ui::Transform()); | |
1816 v2->SetTransform(ui::Transform()); | |
1817 v3->SetTransform(ui::Transform()); | |
1818 | |
1819 v1->Reset(); | |
1820 v2->Reset(); | |
1821 v3->Reset(); | |
1822 | |
1823 // Rotate |v3| clockwise with respect to |v2|, and scale it along both axis. | |
1824 transform = v3->GetTransform(); | |
1825 RotateClockwise(transform); | |
1826 transform.SetTranslateX(30.0f); | |
1827 // Rotation sets some scaling transformation. Using SetScale would overwrite | |
1828 // that and pollute the rotation. So combine the scaling with the existing | |
1829 // transforamtion. | |
1830 transform.ConcatScale(0.8f, 0.5f); | |
1831 v3->SetTransform(transform); | |
1832 | |
1833 // Translate |v2| with respect to |v1|. | |
1834 transform = v2->GetTransform(); | |
1835 transform.SetTranslate(10, 10); | |
1836 v2->SetTransform(transform); | |
1837 | |
1838 // |v3| now occupies (120, 120) to (144, 130) in |root|. | |
1839 | |
1840 MouseEvent p4(ui::ET_MOUSE_PRESSED, | |
1841 124, 125, | |
1842 ui::EF_LEFT_BUTTON_DOWN); | |
1843 root->OnMousePressed(p4); | |
1844 | |
1845 EXPECT_EQ(ui::ET_MOUSE_PRESSED, v3->last_mouse_event_type_); | |
1846 EXPECT_EQ(10, v3->location_.x()); | |
1847 EXPECT_EQ(25, v3->location_.y()); | |
1848 | |
1849 root->OnMouseReleased(released); | |
1850 | |
1851 widget->CloseNow(); | |
1852 } | |
1853 | |
1854 TEST_F(ViewTest, TransformVisibleBound) { | |
1855 gfx::Rect viewport_bounds(0, 0, 100, 100); | |
1856 | |
1857 scoped_ptr<Widget> widget(new Widget); | |
1858 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
1859 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
1860 params.bounds = viewport_bounds; | |
1861 widget->Init(params); | |
1862 widget->GetRootView()->SetBoundsRect(viewport_bounds); | |
1863 | |
1864 View* viewport = new View; | |
1865 widget->SetContentsView(viewport); | |
1866 View* contents = new View; | |
1867 viewport->AddChildView(contents); | |
1868 viewport->SetBoundsRect(viewport_bounds); | |
1869 contents->SetBounds(0, 0, 100, 200); | |
1870 | |
1871 View* child = new View; | |
1872 contents->AddChildView(child); | |
1873 child->SetBounds(10, 90, 50, 50); | |
1874 EXPECT_EQ(gfx::Rect(0, 0, 50, 10), child->GetVisibleBounds()); | |
1875 | |
1876 // Rotate |child| counter-clockwise | |
1877 ui::Transform transform; | |
1878 RotateCounterclockwise(transform); | |
1879 transform.SetTranslateY(50.0f); | |
1880 child->SetTransform(transform); | |
1881 EXPECT_EQ(gfx::Rect(40, 0, 10, 50), child->GetVisibleBounds()); | |
1882 | |
1883 widget->CloseNow(); | |
1884 } | |
1885 | |
1886 //////////////////////////////////////////////////////////////////////////////// | |
1887 // OnVisibleBoundsChanged() | |
1888 | |
1889 class VisibleBoundsView : public View { | |
1890 public: | |
1891 VisibleBoundsView() : received_notification_(false) {} | |
1892 virtual ~VisibleBoundsView() {} | |
1893 | |
1894 bool received_notification() const { return received_notification_; } | |
1895 void set_received_notification(bool received) { | |
1896 received_notification_ = received; | |
1897 } | |
1898 | |
1899 private: | |
1900 // Overridden from View: | |
1901 virtual bool NeedsNotificationWhenVisibleBoundsChange() const { | |
1902 return true; | |
1903 } | |
1904 virtual void OnVisibleBoundsChanged() { | |
1905 received_notification_ = true; | |
1906 } | |
1907 | |
1908 bool received_notification_; | |
1909 | |
1910 DISALLOW_COPY_AND_ASSIGN(VisibleBoundsView); | |
1911 }; | |
1912 | |
1913 TEST_F(ViewTest, OnVisibleBoundsChanged) { | |
1914 gfx::Rect viewport_bounds(0, 0, 100, 100); | |
1915 | |
1916 scoped_ptr<Widget> widget(new Widget); | |
1917 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
1918 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
1919 params.bounds = viewport_bounds; | |
1920 widget->Init(params); | |
1921 widget->GetRootView()->SetBoundsRect(viewport_bounds); | |
1922 | |
1923 View* viewport = new View; | |
1924 widget->SetContentsView(viewport); | |
1925 View* contents = new View; | |
1926 viewport->AddChildView(contents); | |
1927 viewport->SetBoundsRect(viewport_bounds); | |
1928 contents->SetBounds(0, 0, 100, 200); | |
1929 | |
1930 // Create a view that cares about visible bounds notifications, and position | |
1931 // it just outside the visible bounds of the viewport. | |
1932 VisibleBoundsView* child = new VisibleBoundsView; | |
1933 contents->AddChildView(child); | |
1934 child->SetBounds(10, 110, 50, 50); | |
1935 | |
1936 // The child bound should be fully clipped. | |
1937 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty()); | |
1938 | |
1939 // Now scroll the contents, but not enough to make the child visible. | |
1940 contents->SetY(contents->y() - 1); | |
1941 | |
1942 // We should have received the notification since the visible bounds may have | |
1943 // changed (even though they didn't). | |
1944 EXPECT_TRUE(child->received_notification()); | |
1945 EXPECT_TRUE(child->GetVisibleBounds().IsEmpty()); | |
1946 child->set_received_notification(false); | |
1947 | |
1948 // Now scroll the contents, this time by enough to make the child visible by | |
1949 // one pixel. | |
1950 contents->SetY(contents->y() - 10); | |
1951 EXPECT_TRUE(child->received_notification()); | |
1952 EXPECT_EQ(1, child->GetVisibleBounds().height()); | |
1953 child->set_received_notification(false); | |
1954 | |
1955 widget->CloseNow(); | |
1956 } | |
1957 | |
1958 //////////////////////////////////////////////////////////////////////////////// | |
1959 // BoundsChanged() | |
1960 | |
1961 TEST_F(ViewTest, SetBoundsPaint) { | |
1962 TestView top_view; | |
1963 TestView* child_view = new TestView; | |
1964 | |
1965 top_view.SetBounds(0, 0, 100, 100); | |
1966 top_view.scheduled_paint_rects_.clear(); | |
1967 child_view->SetBounds(10, 10, 20, 20); | |
1968 top_view.AddChildView(child_view); | |
1969 | |
1970 top_view.scheduled_paint_rects_.clear(); | |
1971 child_view->SetBounds(30, 30, 20, 20); | |
1972 EXPECT_EQ(2U, top_view.scheduled_paint_rects_.size()); | |
1973 | |
1974 // There should be 2 rects, spanning from (10, 10) to (50, 50). | |
1975 gfx::Rect paint_rect = | |
1976 top_view.scheduled_paint_rects_[0].Union( | |
1977 top_view.scheduled_paint_rects_[1]); | |
1978 EXPECT_EQ(gfx::Rect(10, 10, 40, 40), paint_rect); | |
1979 } | |
1980 | |
1981 // Tests conversion methods with a transform. | |
1982 TEST_F(ViewTest, ConvertPointToViewWithTransform) { | |
1983 TestView top_view; | |
1984 TestView* child = new TestView; | |
1985 TestView* child_child = new TestView; | |
1986 | |
1987 top_view.AddChildView(child); | |
1988 child->AddChildView(child_child); | |
1989 | |
1990 top_view.SetBounds(0, 0, 1000, 1000); | |
1991 | |
1992 child->SetBounds(7, 19, 500, 500); | |
1993 ui::Transform transform; | |
1994 transform.SetScale(3.0f, 4.0f); | |
1995 child->SetTransform(transform); | |
1996 | |
1997 child_child->SetBounds(17, 13, 100, 100); | |
1998 transform = ui::Transform(); | |
1999 transform.SetScale(5.0f, 7.0f); | |
2000 child_child->SetTransform(transform); | |
2001 | |
2002 // Sanity check to make sure basic transforms act as expected. | |
2003 { | |
2004 ui::Transform transform; | |
2005 transform.ConcatTranslate(1, 1); | |
2006 transform.ConcatScale(100, 55); | |
2007 transform.ConcatTranslate(110, -110); | |
2008 | |
2009 // convert to a 3x3 matrix. | |
2010 const SkMatrix& matrix = transform.matrix(); | |
2011 | |
2012 EXPECT_EQ(210, matrix.getTranslateX()); | |
2013 EXPECT_EQ(-55, matrix.getTranslateY()); | |
2014 EXPECT_EQ(100, matrix.getScaleX()); | |
2015 EXPECT_EQ(55, matrix.getScaleY()); | |
2016 EXPECT_EQ(0, matrix.getSkewX()); | |
2017 EXPECT_EQ(0, matrix.getSkewY()); | |
2018 } | |
2019 | |
2020 { | |
2021 ui::Transform transform; | |
2022 transform.SetTranslate(1, 1); | |
2023 ui::Transform t2; | |
2024 t2.SetScale(100, 55); | |
2025 ui::Transform t3; | |
2026 t3.SetTranslate(110, -110); | |
2027 transform.ConcatTransform(t2); | |
2028 transform.ConcatTransform(t3); | |
2029 | |
2030 // convert to a 3x3 matrix | |
2031 const SkMatrix& matrix = transform.matrix(); | |
2032 | |
2033 EXPECT_EQ(210, matrix.getTranslateX()); | |
2034 EXPECT_EQ(-55, matrix.getTranslateY()); | |
2035 EXPECT_EQ(100, matrix.getScaleX()); | |
2036 EXPECT_EQ(55, matrix.getScaleY()); | |
2037 EXPECT_EQ(0, matrix.getSkewX()); | |
2038 EXPECT_EQ(0, matrix.getSkewY()); | |
2039 } | |
2040 | |
2041 // Conversions from child->top and top->child. | |
2042 { | |
2043 gfx::Point point(5, 5); | |
2044 View::ConvertPointToView(child, &top_view, &point); | |
2045 EXPECT_EQ(22, point.x()); | |
2046 EXPECT_EQ(39, point.y()); | |
2047 | |
2048 point.SetPoint(22, 39); | |
2049 View::ConvertPointToView(&top_view, child, &point); | |
2050 EXPECT_EQ(5, point.x()); | |
2051 EXPECT_EQ(5, point.y()); | |
2052 } | |
2053 | |
2054 // Conversions from child_child->top and top->child_child. | |
2055 { | |
2056 gfx::Point point(5, 5); | |
2057 View::ConvertPointToView(child_child, &top_view, &point); | |
2058 EXPECT_EQ(133, point.x()); | |
2059 EXPECT_EQ(211, point.y()); | |
2060 | |
2061 point.SetPoint(133, 211); | |
2062 View::ConvertPointToView(&top_view, child_child, &point); | |
2063 EXPECT_EQ(5, point.x()); | |
2064 EXPECT_EQ(5, point.y()); | |
2065 } | |
2066 | |
2067 // Conversions from child_child->child and child->child_child | |
2068 { | |
2069 gfx::Point point(5, 5); | |
2070 View::ConvertPointToView(child_child, child, &point); | |
2071 EXPECT_EQ(42, point.x()); | |
2072 EXPECT_EQ(48, point.y()); | |
2073 | |
2074 point.SetPoint(42, 48); | |
2075 View::ConvertPointToView(child, child_child, &point); | |
2076 EXPECT_EQ(5, point.x()); | |
2077 EXPECT_EQ(5, point.y()); | |
2078 } | |
2079 | |
2080 // Conversions from top_view to child with a value that should be negative. | |
2081 // This ensures we don't round up with negative numbers. | |
2082 { | |
2083 gfx::Point point(6, 18); | |
2084 View::ConvertPointToView(&top_view, child, &point); | |
2085 EXPECT_EQ(-1, point.x()); | |
2086 EXPECT_EQ(-1, point.y()); | |
2087 } | |
2088 } | |
2089 | |
2090 // Tests conversion methods for rectangles. | |
2091 TEST_F(ViewTest, ConvertRectWithTransform) { | |
2092 scoped_ptr<Widget> widget(new Widget); | |
2093 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
2094 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
2095 params.bounds = gfx::Rect(50, 50, 650, 650); | |
2096 widget->Init(params); | |
2097 View* root = widget->GetRootView(); | |
2098 | |
2099 TestView* v1 = new TestView; | |
2100 TestView* v2 = new TestView; | |
2101 root->AddChildView(v1); | |
2102 v1->AddChildView(v2); | |
2103 | |
2104 v1->SetBounds(10, 10, 500, 500); | |
2105 v2->SetBounds(20, 20, 100, 200); | |
2106 | |
2107 // |v2| now occupies (30, 30) to (130, 230) in |widget| | |
2108 gfx::Rect rect(5, 5, 15, 40); | |
2109 EXPECT_EQ(gfx::Rect(25, 25, 15, 40), v2->ConvertRectToParent(rect)); | |
2110 EXPECT_EQ(gfx::Rect(35, 35, 15, 40), v2->ConvertRectToWidget(rect)); | |
2111 | |
2112 // Rotate |v2| | |
2113 ui::Transform t2; | |
2114 RotateCounterclockwise(t2); | |
2115 t2.SetTranslateY(100.0f); | |
2116 v2->SetTransform(t2); | |
2117 | |
2118 // |v2| now occupies (30, 30) to (230, 130) in |widget| | |
2119 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect)); | |
2120 EXPECT_EQ(gfx::Rect(35, 110, 40, 15), v2->ConvertRectToWidget(rect)); | |
2121 | |
2122 // Scale down |v1| | |
2123 ui::Transform t1; | |
2124 t1.SetScale(0.5, 0.5); | |
2125 v1->SetTransform(t1); | |
2126 | |
2127 // The rectangle should remain the same for |v1|. | |
2128 EXPECT_EQ(gfx::Rect(25, 100, 40, 15), v2->ConvertRectToParent(rect)); | |
2129 | |
2130 // |v2| now occupies (20, 20) to (120, 70) in |widget| | |
2131 // There are some rounding of floating values here. These values may change if | |
2132 // floating operations are improved/changed. | |
2133 EXPECT_EQ(gfx::Rect(22, 60, 20, 7), v2->ConvertRectToWidget(rect)); | |
2134 | |
2135 widget->CloseNow(); | |
2136 } | |
2137 | |
2138 class ObserverView : public View { | |
2139 public: | |
2140 ObserverView(); | |
2141 virtual ~ObserverView(); | |
2142 | |
2143 void ResetTestState(); | |
2144 | |
2145 bool child_added() const { return child_added_; } | |
2146 bool child_removed() const { return child_removed_; } | |
2147 const View* parent_view() const { return parent_view_; } | |
2148 const View* child_view() const { return child_view_; } | |
2149 | |
2150 private: | |
2151 // View: | |
2152 virtual void ViewHierarchyChanged(bool is_add, | |
2153 View* parent, | |
2154 View* child) OVERRIDE; | |
2155 | |
2156 bool child_added_; | |
2157 bool child_removed_; | |
2158 View* parent_view_; | |
2159 View* child_view_; | |
2160 | |
2161 DISALLOW_COPY_AND_ASSIGN(ObserverView); | |
2162 }; | |
2163 | |
2164 ObserverView::ObserverView() | |
2165 : child_added_(false), | |
2166 child_removed_(false), | |
2167 parent_view_(NULL), | |
2168 child_view_(NULL) { | |
2169 } | |
2170 | |
2171 ObserverView::~ObserverView() {} | |
2172 | |
2173 void ObserverView::ResetTestState() { | |
2174 child_added_ = false; | |
2175 child_removed_ = false; | |
2176 parent_view_ = NULL; | |
2177 child_view_ = NULL; | |
2178 } | |
2179 | |
2180 void ObserverView::ViewHierarchyChanged(bool is_add, | |
2181 View* parent, | |
2182 View* child) { | |
2183 if (is_add) | |
2184 child_added_ = true; | |
2185 else | |
2186 child_removed_ = true; | |
2187 | |
2188 parent_view_ = parent; | |
2189 child_view_ = child; | |
2190 } | |
2191 | |
2192 // Verifies that the ViewHierarchyChanged() notification is sent correctly when | |
2193 // a child view is added or removed to all the views in the hierarchy (up and | |
2194 // down). | |
2195 // The tree looks like this: | |
2196 // v1 | |
2197 // +-- v2 | |
2198 // +-- v3 | |
2199 TEST_F(ViewTest, ViewHierarchyChanged) { | |
2200 ObserverView v1; | |
2201 | |
2202 ObserverView* v3 = new ObserverView(); | |
2203 | |
2204 // Add |v3| to |v2|. | |
2205 scoped_ptr<ObserverView> v2(new ObserverView()); | |
2206 v2->AddChildView(v3); | |
2207 | |
2208 // Make sure both |v2| and |v3| receive the ViewHierarchyChanged() | |
2209 // notification. | |
2210 EXPECT_TRUE(v2->child_added()); | |
2211 EXPECT_FALSE(v2->child_removed()); | |
2212 EXPECT_EQ(v2.get(), v2->parent_view()); | |
2213 EXPECT_EQ(v3, v2->child_view()); | |
2214 | |
2215 EXPECT_TRUE(v3->child_added()); | |
2216 EXPECT_FALSE(v3->child_removed()); | |
2217 EXPECT_EQ(v2.get(), v3->parent_view()); | |
2218 EXPECT_EQ(v3, v3->child_view()); | |
2219 | |
2220 // Reset everything to the initial state. | |
2221 v2->ResetTestState(); | |
2222 v3->ResetTestState(); | |
2223 | |
2224 // Add |v2| to v1. | |
2225 v1.AddChildView(v2.get()); | |
2226 | |
2227 // Verifies that |v2| is the child view *added* and the parent view is |v1|. | |
2228 // Make sure all the views (v1, v2, v3) received _that_ information. | |
2229 EXPECT_TRUE(v1.child_added()); | |
2230 EXPECT_FALSE(v1.child_removed()); | |
2231 EXPECT_EQ(&v1, v1.parent_view()); | |
2232 EXPECT_EQ(v2.get(), v1.child_view()); | |
2233 | |
2234 EXPECT_TRUE(v2->child_added()); | |
2235 EXPECT_FALSE(v2->child_removed()); | |
2236 EXPECT_EQ(&v1, v2->parent_view()); | |
2237 EXPECT_EQ(v2.get(), v2->child_view()); | |
2238 | |
2239 EXPECT_TRUE(v3->child_added()); | |
2240 EXPECT_FALSE(v3->child_removed()); | |
2241 EXPECT_EQ(&v1, v3->parent_view()); | |
2242 EXPECT_EQ(v2.get(), v3->child_view()); | |
2243 | |
2244 // Reset everything to the initial state. | |
2245 v1.ResetTestState(); | |
2246 v2->ResetTestState(); | |
2247 v3->ResetTestState(); | |
2248 | |
2249 // Remove |v2| from |v1|. | |
2250 v1.RemoveChildView(v2.get()); | |
2251 | |
2252 // Verifies that |v2| is the child view *removed* and the parent view is |v1|. | |
2253 // Make sure all the views (v1, v2, v3) received _that_ information. | |
2254 EXPECT_FALSE(v1.child_added()); | |
2255 EXPECT_TRUE(v1.child_removed()); | |
2256 EXPECT_EQ(&v1, v1.parent_view()); | |
2257 EXPECT_EQ(v2.get(), v1.child_view()); | |
2258 | |
2259 EXPECT_FALSE(v2->child_added()); | |
2260 EXPECT_TRUE(v2->child_removed()); | |
2261 EXPECT_EQ(&v1, v2->parent_view()); | |
2262 EXPECT_EQ(v2.get(), v2->child_view()); | |
2263 | |
2264 EXPECT_FALSE(v3->child_added()); | |
2265 EXPECT_TRUE(v3->child_removed()); | |
2266 EXPECT_EQ(&v1, v3->parent_view()); | |
2267 EXPECT_EQ(v3, v3->child_view()); | |
2268 } | |
2269 | |
2270 // Verifies if the child views added under the root are all deleted when calling | |
2271 // RemoveAllChildViews. | |
2272 // The tree looks like this: | |
2273 // root | |
2274 // +-- child1 | |
2275 // +-- foo | |
2276 // +-- bar0 | |
2277 // +-- bar1 | |
2278 // +-- bar2 | |
2279 // +-- child2 | |
2280 // +-- child3 | |
2281 TEST_F(ViewTest, RemoveAllChildViews) { | |
2282 View root; | |
2283 | |
2284 View* child1 = new View; | |
2285 root.AddChildView(child1); | |
2286 | |
2287 for (int i = 0; i < 2; ++i) | |
2288 root.AddChildView(new View); | |
2289 | |
2290 View* foo = new View; | |
2291 child1->AddChildView(foo); | |
2292 | |
2293 // Add some nodes to |foo|. | |
2294 for (int i = 0; i < 3; ++i) | |
2295 foo->AddChildView(new View); | |
2296 | |
2297 EXPECT_EQ(3, root.child_count()); | |
2298 EXPECT_EQ(1, child1->child_count()); | |
2299 EXPECT_EQ(3, foo->child_count()); | |
2300 | |
2301 // Now remove all child views from root. | |
2302 root.RemoveAllChildViews(true); | |
2303 | |
2304 EXPECT_EQ(0, root.child_count()); | |
2305 EXPECT_FALSE(root.has_children()); | |
2306 } | |
2307 | |
2308 TEST_F(ViewTest, Contains) { | |
2309 View v1; | |
2310 View* v2 = new View; | |
2311 View* v3 = new View; | |
2312 | |
2313 v1.AddChildView(v2); | |
2314 v2->AddChildView(v3); | |
2315 | |
2316 EXPECT_FALSE(v1.Contains(NULL)); | |
2317 EXPECT_TRUE(v1.Contains(&v1)); | |
2318 EXPECT_TRUE(v1.Contains(v2)); | |
2319 EXPECT_TRUE(v1.Contains(v3)); | |
2320 | |
2321 EXPECT_FALSE(v2->Contains(NULL)); | |
2322 EXPECT_TRUE(v2->Contains(v2)); | |
2323 EXPECT_FALSE(v2->Contains(&v1)); | |
2324 EXPECT_TRUE(v2->Contains(v3)); | |
2325 | |
2326 EXPECT_FALSE(v3->Contains(NULL)); | |
2327 EXPECT_TRUE(v3->Contains(v3)); | |
2328 EXPECT_FALSE(v3->Contains(&v1)); | |
2329 EXPECT_FALSE(v3->Contains(v2)); | |
2330 } | |
2331 | |
2332 // Verifies if GetIndexOf() returns the correct index for the specified child | |
2333 // view. | |
2334 // The tree looks like this: | |
2335 // root | |
2336 // +-- child1 | |
2337 // +-- foo1 | |
2338 // +-- child2 | |
2339 TEST_F(ViewTest, GetIndexOf) { | |
2340 View root; | |
2341 | |
2342 View* child1 = new View; | |
2343 root.AddChildView(child1); | |
2344 | |
2345 View* child2 = new View; | |
2346 root.AddChildView(child2); | |
2347 | |
2348 View* foo1 = new View; | |
2349 child1->AddChildView(foo1); | |
2350 | |
2351 EXPECT_EQ(-1, root.GetIndexOf(NULL)); | |
2352 EXPECT_EQ(-1, root.GetIndexOf(&root)); | |
2353 EXPECT_EQ(0, root.GetIndexOf(child1)); | |
2354 EXPECT_EQ(1, root.GetIndexOf(child2)); | |
2355 EXPECT_EQ(-1, root.GetIndexOf(foo1)); | |
2356 | |
2357 EXPECT_EQ(-1, child1->GetIndexOf(NULL)); | |
2358 EXPECT_EQ(-1, child1->GetIndexOf(&root)); | |
2359 EXPECT_EQ(-1, child1->GetIndexOf(child1)); | |
2360 EXPECT_EQ(-1, child1->GetIndexOf(child2)); | |
2361 EXPECT_EQ(0, child1->GetIndexOf(foo1)); | |
2362 | |
2363 EXPECT_EQ(-1, child2->GetIndexOf(NULL)); | |
2364 EXPECT_EQ(-1, child2->GetIndexOf(&root)); | |
2365 EXPECT_EQ(-1, child2->GetIndexOf(child2)); | |
2366 EXPECT_EQ(-1, child2->GetIndexOf(child1)); | |
2367 EXPECT_EQ(-1, child2->GetIndexOf(foo1)); | |
2368 } | |
2369 | |
2370 // Verifies that the child views can be reordered correctly. | |
2371 TEST_F(ViewTest, ReorderChildren) { | |
2372 View root; | |
2373 | |
2374 View* child = new View(); | |
2375 root.AddChildView(child); | |
2376 | |
2377 View* foo1 = new View(); | |
2378 child->AddChildView(foo1); | |
2379 View* foo2 = new View(); | |
2380 child->AddChildView(foo2); | |
2381 View* foo3 = new View(); | |
2382 child->AddChildView(foo3); | |
2383 foo1->set_focusable(true); | |
2384 foo2->set_focusable(true); | |
2385 foo3->set_focusable(true); | |
2386 | |
2387 ASSERT_EQ(0, child->GetIndexOf(foo1)); | |
2388 ASSERT_EQ(1, child->GetIndexOf(foo2)); | |
2389 ASSERT_EQ(2, child->GetIndexOf(foo3)); | |
2390 ASSERT_EQ(foo2, foo1->GetNextFocusableView()); | |
2391 ASSERT_EQ(foo3, foo2->GetNextFocusableView()); | |
2392 ASSERT_EQ(NULL, foo3->GetNextFocusableView()); | |
2393 | |
2394 // Move |foo2| at the end. | |
2395 child->ReorderChildView(foo2, -1); | |
2396 ASSERT_EQ(0, child->GetIndexOf(foo1)); | |
2397 ASSERT_EQ(1, child->GetIndexOf(foo3)); | |
2398 ASSERT_EQ(2, child->GetIndexOf(foo2)); | |
2399 ASSERT_EQ(foo3, foo1->GetNextFocusableView()); | |
2400 ASSERT_EQ(foo2, foo3->GetNextFocusableView()); | |
2401 ASSERT_EQ(NULL, foo2->GetNextFocusableView()); | |
2402 | |
2403 // Move |foo1| at the end. | |
2404 child->ReorderChildView(foo1, -1); | |
2405 ASSERT_EQ(0, child->GetIndexOf(foo3)); | |
2406 ASSERT_EQ(1, child->GetIndexOf(foo2)); | |
2407 ASSERT_EQ(2, child->GetIndexOf(foo1)); | |
2408 ASSERT_EQ(NULL, foo1->GetNextFocusableView()); | |
2409 ASSERT_EQ(foo2, foo1->GetPreviousFocusableView()); | |
2410 ASSERT_EQ(foo2, foo3->GetNextFocusableView()); | |
2411 ASSERT_EQ(foo1, foo2->GetNextFocusableView()); | |
2412 | |
2413 // Move |foo2| to the front. | |
2414 child->ReorderChildView(foo2, 0); | |
2415 ASSERT_EQ(0, child->GetIndexOf(foo2)); | |
2416 ASSERT_EQ(1, child->GetIndexOf(foo3)); | |
2417 ASSERT_EQ(2, child->GetIndexOf(foo1)); | |
2418 ASSERT_EQ(NULL, foo1->GetNextFocusableView()); | |
2419 ASSERT_EQ(foo3, foo1->GetPreviousFocusableView()); | |
2420 ASSERT_EQ(foo3, foo2->GetNextFocusableView()); | |
2421 ASSERT_EQ(foo1, foo3->GetNextFocusableView()); | |
2422 } | |
2423 | |
2424 // Verifies that GetViewByID returns the correctly child view from the specified | |
2425 // ID. | |
2426 // The tree looks like this: | |
2427 // v1 | |
2428 // +-- v2 | |
2429 // +-- v3 | |
2430 // +-- v4 | |
2431 TEST_F(ViewTest, GetViewByID) { | |
2432 View v1; | |
2433 const int kV1ID = 1; | |
2434 v1.set_id(kV1ID); | |
2435 | |
2436 View v2; | |
2437 const int kV2ID = 2; | |
2438 v2.set_id(kV2ID); | |
2439 | |
2440 View v3; | |
2441 const int kV3ID = 3; | |
2442 v3.set_id(kV3ID); | |
2443 | |
2444 View v4; | |
2445 const int kV4ID = 4; | |
2446 v4.set_id(kV4ID); | |
2447 | |
2448 const int kV5ID = 5; | |
2449 | |
2450 v1.AddChildView(&v2); | |
2451 v2.AddChildView(&v3); | |
2452 v2.AddChildView(&v4); | |
2453 | |
2454 EXPECT_EQ(&v1, v1.GetViewByID(kV1ID)); | |
2455 EXPECT_EQ(&v2, v1.GetViewByID(kV2ID)); | |
2456 EXPECT_EQ(&v4, v1.GetViewByID(kV4ID)); | |
2457 | |
2458 EXPECT_EQ(NULL, v1.GetViewByID(kV5ID)); // No V5 exists. | |
2459 EXPECT_EQ(NULL, v2.GetViewByID(kV1ID)); // It can get only from child views. | |
2460 | |
2461 const int kGroup = 1; | |
2462 v3.SetGroup(kGroup); | |
2463 v4.SetGroup(kGroup); | |
2464 | |
2465 View::Views views; | |
2466 v1.GetViewsInGroup(kGroup, &views); | |
2467 EXPECT_EQ(2U, views.size()); | |
2468 | |
2469 View::Views::const_iterator i(std::find(views.begin(), views.end(), &v3)); | |
2470 EXPECT_NE(views.end(), i); | |
2471 | |
2472 i = std::find(views.begin(), views.end(), &v4); | |
2473 EXPECT_NE(views.end(), i); | |
2474 } | |
2475 | |
2476 //////////////////////////////////////////////////////////////////////////////// | |
2477 // Layers | |
2478 //////////////////////////////////////////////////////////////////////////////// | |
2479 | |
2480 #if defined(VIEWS_COMPOSITOR) | |
2481 | |
2482 namespace { | |
2483 | |
2484 // Test implementation of LayerAnimator. | |
2485 class TestLayerAnimator : public ui::LayerAnimator { | |
2486 public: | |
2487 TestLayerAnimator(); | |
2488 | |
2489 const gfx::Rect& last_bounds() const { return last_bounds_; } | |
2490 | |
2491 // LayerAnimator. | |
2492 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE; | |
2493 | |
2494 private: | |
2495 gfx::Rect last_bounds_; | |
2496 | |
2497 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator); | |
2498 }; | |
2499 | |
2500 TestLayerAnimator::TestLayerAnimator() | |
2501 : ui::LayerAnimator(base::TimeDelta::FromMilliseconds(0)) { | |
2502 } | |
2503 | |
2504 void TestLayerAnimator::SetBounds(const gfx::Rect& bounds) { | |
2505 last_bounds_ = bounds; | |
2506 } | |
2507 | |
2508 } // namespace | |
2509 | |
2510 class ViewLayerTest : public ViewsTestBase { | |
2511 public: | |
2512 ViewLayerTest() : widget_(NULL), old_use_acceleration_(false) {} | |
2513 | |
2514 virtual ~ViewLayerTest() { | |
2515 } | |
2516 | |
2517 // Returns the Layer used by the RootView. | |
2518 ui::Layer* GetRootLayer() { | |
2519 #if defined(USE_AURA) | |
2520 ui::Layer* root_layer = NULL; | |
2521 gfx::Point origin; | |
2522 widget()->CalculateOffsetToAncestorWithLayer(&origin, &root_layer); | |
2523 return root_layer; | |
2524 #else | |
2525 return widget()->GetRootView()->layer(); | |
2526 #endif | |
2527 } | |
2528 | |
2529 virtual void SetUp() OVERRIDE { | |
2530 ViewTest::SetUp(); | |
2531 old_use_acceleration_ = View::get_use_acceleration_when_possible(); | |
2532 View::set_use_acceleration_when_possible(true); | |
2533 | |
2534 ui::TestTexture::reset_live_count(); | |
2535 | |
2536 widget_ = new Widget; | |
2537 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
2538 params.bounds = gfx::Rect(50, 50, 200, 200); | |
2539 widget_->Init(params); | |
2540 widget_->Show(); | |
2541 widget_->GetRootView()->SetBounds(0, 0, 200, 200); | |
2542 } | |
2543 | |
2544 virtual void TearDown() OVERRIDE { | |
2545 View::set_use_acceleration_when_possible(old_use_acceleration_); | |
2546 widget_->CloseNow(); | |
2547 Widget::SetPureViews(false); | |
2548 ViewsTestBase::TearDown(); | |
2549 } | |
2550 | |
2551 Widget* widget() { return widget_; } | |
2552 | |
2553 private: | |
2554 Widget* widget_; | |
2555 bool old_use_acceleration_; | |
2556 }; | |
2557 | |
2558 #if !defined(USE_AURA) | |
2559 // This test assumes a particular layer hierarchy that isn't valid for aura. | |
2560 // Ensures the RootView has a layer and its set up correctly. | |
2561 TEST_F(ViewLayerTest, RootState) { | |
2562 ui::Layer* layer = widget()->GetRootView()->layer(); | |
2563 ASSERT_TRUE(layer); | |
2564 EXPECT_FALSE(layer->parent()); | |
2565 EXPECT_EQ(0u, layer->children().size()); | |
2566 EXPECT_FALSE(layer->transform().HasChange()); | |
2567 EXPECT_EQ(widget()->GetRootView()->bounds(), layer->bounds()); | |
2568 EXPECT_TRUE(layer->GetCompositor() != NULL); | |
2569 } | |
2570 | |
2571 // Verifies that the complete bounds of a texture are updated if the texture | |
2572 // needs to be refreshed and paint with a clip is invoked. | |
2573 // This test invokes OnNativeWidgetPaintAccelerated, which is not used by aura. | |
2574 TEST_F(ViewLayerTest, PaintAll) { | |
2575 View* view = widget()->GetRootView(); | |
2576 ui::Layer* layer = GetRootLayer(); | |
2577 view->SetBounds(0, 0, 200, 200); | |
2578 widget()->OnNativeWidgetPaintAccelerated(gfx::Rect(0, 0, 1, 1)); | |
2579 ASSERT_TRUE(layer != NULL); | |
2580 const ui::TestTexture* texture = | |
2581 static_cast<const ui::TestTexture*>(layer->texture()); | |
2582 ASSERT_TRUE(texture != NULL); | |
2583 EXPECT_EQ(view->GetLocalBounds(), texture->bounds_of_last_paint()); | |
2584 } | |
2585 #endif | |
2586 | |
2587 TEST_F(ViewLayerTest, LayerToggling) { | |
2588 // Because we lazily create textures the calls to DrawTree are necessary to | |
2589 // ensure we trigger creation of textures. | |
2590 #if defined(USE_AURA) | |
2591 ui::Layer* root_layer = NULL; | |
2592 gfx::Point origin; | |
2593 widget()->CalculateOffsetToAncestorWithLayer(&origin, &root_layer); | |
2594 #else | |
2595 ui::Layer* root_layer = widget()->GetRootView()->layer(); | |
2596 #endif | |
2597 View* content_view = new View; | |
2598 widget()->SetContentsView(content_view); | |
2599 | |
2600 #if !defined(USE_WEBKIT_COMPOSITOR) | |
2601 // TODO(piman): with the webkit compositor, we don't create Textures on | |
2602 // Layers. We're not supposed to be calling Layer::DrawTree. This test needs | |
2603 // refactoring to fully work in that case. | |
2604 root_layer->DrawTree(); | |
2605 ui::TestTexture::reset_live_count(); | |
2606 #endif | |
2607 | |
2608 // Create v1, give it a bounds and verify everything is set up correctly. | |
2609 View* v1 = new View; | |
2610 v1->SetPaintToLayer(true); | |
2611 #if !defined(USE_WEBKIT_COMPOSITOR) | |
2612 root_layer->DrawTree(); | |
2613 EXPECT_EQ(0, ui::TestTexture::live_count()); | |
2614 #endif | |
2615 EXPECT_TRUE(v1->layer() != NULL); | |
2616 v1->SetBounds(20, 30, 140, 150); | |
2617 content_view->AddChildView(v1); | |
2618 #if !defined(USE_WEBKIT_COMPOSITOR) | |
2619 root_layer->DrawTree(); | |
2620 EXPECT_EQ(1, ui::TestTexture::live_count()); | |
2621 #endif | |
2622 ASSERT_TRUE(v1->layer() != NULL); | |
2623 EXPECT_EQ(root_layer, v1->layer()->parent()); | |
2624 EXPECT_EQ(gfx::Rect(20, 30, 140, 150), v1->layer()->bounds()); | |
2625 | |
2626 // Create v2 as a child of v1 and do basic assertion testing. | |
2627 View* v2 = new View; | |
2628 v1->AddChildView(v2); | |
2629 EXPECT_TRUE(v2->layer() == NULL); | |
2630 v2->SetBounds(10, 20, 30, 40); | |
2631 v2->SetPaintToLayer(true); | |
2632 #if !defined(USE_WEBKIT_COMPOSITOR) | |
2633 root_layer->DrawTree(); | |
2634 EXPECT_EQ(2, ui::TestTexture::live_count()); | |
2635 #endif | |
2636 ASSERT_TRUE(v2->layer() != NULL); | |
2637 EXPECT_EQ(v1->layer(), v2->layer()->parent()); | |
2638 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds()); | |
2639 | |
2640 // Turn off v1s layer. v2 should still have a layer but its parent should have | |
2641 // changed. | |
2642 v1->SetPaintToLayer(false); | |
2643 #if !defined(USE_WEBKIT_COMPOSITOR) | |
2644 root_layer->DrawTree(); | |
2645 EXPECT_EQ(1, ui::TestTexture::live_count()); | |
2646 #endif | |
2647 EXPECT_TRUE(v1->layer() == NULL); | |
2648 EXPECT_TRUE(v2->layer() != NULL); | |
2649 EXPECT_EQ(root_layer, v2->layer()->parent()); | |
2650 ASSERT_EQ(1u, root_layer->children().size()); | |
2651 EXPECT_EQ(root_layer->children()[0], v2->layer()); | |
2652 // The bounds of the layer should have changed to be relative to the root view | |
2653 // now. | |
2654 EXPECT_EQ(gfx::Rect(30, 50, 30, 40), v2->layer()->bounds()); | |
2655 | |
2656 // Make v1 have a layer again and verify v2s layer is wired up correctly. | |
2657 ui::Transform transform; | |
2658 transform.SetScale(2.0f, 2.0f); | |
2659 v1->SetTransform(transform); | |
2660 #if !defined(USE_WEBKIT_COMPOSITOR) | |
2661 root_layer->DrawTree(); | |
2662 EXPECT_EQ(2, ui::TestTexture::live_count()); | |
2663 #endif | |
2664 EXPECT_TRUE(v1->layer() != NULL); | |
2665 EXPECT_TRUE(v2->layer() != NULL); | |
2666 EXPECT_EQ(root_layer, v1->layer()->parent()); | |
2667 EXPECT_EQ(v1->layer(), v2->layer()->parent()); | |
2668 ASSERT_EQ(1u, root_layer->children().size()); | |
2669 EXPECT_EQ(root_layer->children()[0], v1->layer()); | |
2670 ASSERT_EQ(1u, v1->layer()->children().size()); | |
2671 EXPECT_EQ(v1->layer()->children()[0], v2->layer()); | |
2672 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), v2->layer()->bounds()); | |
2673 } | |
2674 | |
2675 // Verifies turning on a layer wires up children correctly. | |
2676 TEST_F(ViewLayerTest, NestedLayerToggling) { | |
2677 View* content_view = new View; | |
2678 widget()->SetContentsView(content_view); | |
2679 | |
2680 // Create v1, give it a bounds and verify everything is set up correctly. | |
2681 View* v1 = new View; | |
2682 content_view->AddChildView(v1); | |
2683 v1->SetBounds(20, 30, 140, 150); | |
2684 | |
2685 View* v2 = new View; | |
2686 v1->AddChildView(v2); | |
2687 | |
2688 View* v3 = new View; | |
2689 v3->SetPaintToLayer(true); | |
2690 v2->AddChildView(v3); | |
2691 ASSERT_TRUE(v3->layer() != NULL); | |
2692 | |
2693 // At this point we have v1-v2-v3. v3 has a layer, v1 and v2 don't. | |
2694 | |
2695 v1->SetPaintToLayer(true); | |
2696 EXPECT_EQ(v1->layer(), v3->layer()->parent()); | |
2697 } | |
2698 | |
2699 TEST_F(ViewLayerTest, LayerAnimator) { | |
2700 View* content_view = new View; | |
2701 widget()->SetContentsView(content_view); | |
2702 | |
2703 View* v1 = new View; | |
2704 content_view->AddChildView(v1); | |
2705 v1->SetPaintToLayer(true); | |
2706 EXPECT_TRUE(v1->layer() != NULL); | |
2707 | |
2708 TestLayerAnimator* animator = new TestLayerAnimator(); | |
2709 v1->layer()->SetAnimator(animator); | |
2710 | |
2711 gfx::Rect bounds(1, 2, 3, 4); | |
2712 v1->SetBoundsRect(bounds); | |
2713 EXPECT_EQ(bounds, animator->last_bounds()); | |
2714 // TestLayerAnimator doesn't update the layer. | |
2715 EXPECT_NE(bounds, v1->layer()->bounds()); | |
2716 } | |
2717 | |
2718 // Verifies the bounds of a layer are updated if the bounds of ancestor that | |
2719 // doesn't have a layer change. | |
2720 TEST_F(ViewLayerTest, BoundsChangeWithLayer) { | |
2721 View* content_view = new View; | |
2722 widget()->SetContentsView(content_view); | |
2723 | |
2724 View* v1 = new View; | |
2725 content_view->AddChildView(v1); | |
2726 v1->SetBounds(20, 30, 140, 150); | |
2727 | |
2728 View* v2 = new View; | |
2729 v2->SetBounds(10, 11, 40, 50); | |
2730 v1->AddChildView(v2); | |
2731 v2->SetPaintToLayer(true); | |
2732 ASSERT_TRUE(v2->layer() != NULL); | |
2733 EXPECT_EQ(gfx::Rect(30, 41, 40, 50), v2->layer()->bounds()); | |
2734 | |
2735 v1->SetPosition(gfx::Point(25, 36)); | |
2736 EXPECT_EQ(gfx::Rect(35, 47, 40, 50), v2->layer()->bounds()); | |
2737 | |
2738 v2->SetPosition(gfx::Point(11, 12)); | |
2739 EXPECT_EQ(gfx::Rect(36, 48, 40, 50), v2->layer()->bounds()); | |
2740 | |
2741 // Bounds of the layer should change even if the view is not invisible. | |
2742 v1->SetVisible(false); | |
2743 v1->SetPosition(gfx::Point(20, 30)); | |
2744 EXPECT_EQ(gfx::Rect(31, 42, 40, 50), v2->layer()->bounds()); | |
2745 | |
2746 v2->SetVisible(false); | |
2747 v2->SetBounds(10, 11, 20, 30); | |
2748 EXPECT_EQ(gfx::Rect(30, 41, 20, 30), v2->layer()->bounds()); | |
2749 } | |
2750 | |
2751 // Makes sure a transform persists after toggling the visibility. | |
2752 TEST_F(ViewLayerTest, ToggleVisibilityWithTransform) { | |
2753 View* view = new View; | |
2754 ui::Transform transform; | |
2755 transform.SetScale(2.0f, 2.0f); | |
2756 view->SetTransform(transform); | |
2757 widget()->SetContentsView(view); | |
2758 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0)); | |
2759 | |
2760 view->SetVisible(false); | |
2761 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0)); | |
2762 | |
2763 view->SetVisible(true); | |
2764 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0)); | |
2765 } | |
2766 | |
2767 // Verifies a transform persists after removing/adding a view with a transform. | |
2768 TEST_F(ViewLayerTest, ResetTransformOnLayerAfterAdd) { | |
2769 View* view = new View; | |
2770 ui::Transform transform; | |
2771 transform.SetScale(2.0f, 2.0f); | |
2772 view->SetTransform(transform); | |
2773 widget()->SetContentsView(view); | |
2774 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0)); | |
2775 ASSERT_TRUE(view->layer() != NULL); | |
2776 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0)); | |
2777 | |
2778 View* parent = view->parent(); | |
2779 parent->RemoveChildView(view); | |
2780 parent->AddChildView(view); | |
2781 | |
2782 EXPECT_EQ(2.0f, view->GetTransform().matrix().get(0, 0)); | |
2783 ASSERT_TRUE(view->layer() != NULL); | |
2784 EXPECT_EQ(2.0f, view->layer()->transform().matrix().get(0, 0)); | |
2785 } | |
2786 | |
2787 // Makes sure that layer visibility is correct after toggling View visibility. | |
2788 TEST_F(ViewLayerTest, ToggleVisibilityWithLayer) { | |
2789 View* content_view = new View; | |
2790 widget()->SetContentsView(content_view); | |
2791 | |
2792 // The view isn't attached to a widget or a parent view yet. But it should | |
2793 // still have a layer, but the layer should not be attached to the root | |
2794 // layer. | |
2795 View* v1 = new View; | |
2796 v1->SetPaintToLayer(true); | |
2797 EXPECT_TRUE(v1->layer()); | |
2798 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(), | |
2799 v1->layer())); | |
2800 | |
2801 // Once the view is attached to a widget, its layer should be attached to the | |
2802 // root layer and visible. | |
2803 content_view->AddChildView(v1); | |
2804 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(), | |
2805 v1->layer())); | |
2806 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
2807 | |
2808 v1->SetVisible(false); | |
2809 EXPECT_FALSE(v1->layer()->IsDrawn()); | |
2810 | |
2811 v1->SetVisible(true); | |
2812 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
2813 | |
2814 widget()->Hide(); | |
2815 EXPECT_FALSE(v1->layer()->IsDrawn()); | |
2816 | |
2817 widget()->Show(); | |
2818 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
2819 } | |
2820 | |
2821 // Test that a hole in a layer is correctly created regardless of whether | |
2822 // the opacity attribute is set before or after the layer is created. | |
2823 TEST_F(ViewLayerTest, ToggleOpacityWithLayer) { | |
2824 View* content_view = new View; | |
2825 widget()->SetContentsView(content_view); | |
2826 | |
2827 View* parent_view = new View; | |
2828 content_view->AddChildView(parent_view); | |
2829 parent_view->SetPaintToLayer(true); | |
2830 parent_view->SetBounds(0, 0, 400, 400); | |
2831 | |
2832 View* child_view = new View; | |
2833 child_view->SetBounds(50, 50, 100, 100); | |
2834 parent_view->AddChildView(child_view); | |
2835 | |
2836 widget()->GetCompositor()->Draw(false); | |
2837 | |
2838 ASSERT_TRUE(child_view->layer() == NULL); | |
2839 child_view->SetPaintToLayer(true); | |
2840 child_view->SetFillsBoundsOpaquely(true); | |
2841 widget()->GetCompositor()->Draw(false); | |
2842 ASSERT_TRUE(child_view->layer()); | |
2843 EXPECT_EQ( | |
2844 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect()); | |
2845 | |
2846 child_view->SetFillsBoundsOpaquely(false); | |
2847 widget()->GetCompositor()->Draw(false); | |
2848 EXPECT_TRUE(parent_view->layer()->hole_rect().IsEmpty()); | |
2849 } | |
2850 | |
2851 // Test that a hole in a layer always corresponds to the bounds of opaque | |
2852 // layers. | |
2853 TEST_F(ViewLayerTest, MultipleOpaqueLayers) { | |
2854 View* content_view = new View; | |
2855 widget()->SetContentsView(content_view); | |
2856 | |
2857 View* parent_view = new View; | |
2858 parent_view->SetPaintToLayer(true); | |
2859 parent_view->SetBounds(0, 0, 400, 400); | |
2860 content_view->AddChildView(parent_view); | |
2861 | |
2862 View* child_view1 = new View; | |
2863 child_view1->SetPaintToLayer(true); | |
2864 child_view1->SetFillsBoundsOpaquely(true); | |
2865 child_view1->SetBounds(50, 50, 100, 100); | |
2866 parent_view->AddChildView(child_view1); | |
2867 | |
2868 View* child_view2 = new View; | |
2869 child_view2->SetPaintToLayer(true); | |
2870 child_view2->SetFillsBoundsOpaquely(false); | |
2871 child_view2->SetBounds(150, 150, 200, 200); | |
2872 parent_view->AddChildView(child_view2); | |
2873 | |
2874 widget()->GetCompositor()->Draw(false); | |
2875 | |
2876 // Only child_view1 is opaque | |
2877 EXPECT_EQ( | |
2878 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect()); | |
2879 | |
2880 // Both child views are opaque | |
2881 child_view2->SetFillsBoundsOpaquely(true); | |
2882 widget()->GetCompositor()->Draw(false); | |
2883 EXPECT_TRUE( | |
2884 gfx::Rect(50, 50, 100, 100) == parent_view->layer()->hole_rect() || | |
2885 gfx::Rect(150, 150, 200, 200) == parent_view->layer()->hole_rect()); | |
2886 | |
2887 // Only child_view2 is opaque | |
2888 delete child_view1; | |
2889 EXPECT_EQ( | |
2890 gfx::Rect(150, 150, 200, 200), parent_view->layer()->hole_rect()); | |
2891 } | |
2892 | |
2893 // Makes sure that opacity of layer persists after toggling visibilty. | |
2894 TEST_F(ViewLayerTest, ToggleVisibilityWithOpaqueLayer) { | |
2895 View* content_view = new View; | |
2896 widget()->SetContentsView(content_view); | |
2897 | |
2898 View* parent_view = new View; | |
2899 parent_view->SetPaintToLayer(true); | |
2900 parent_view->SetBounds(0, 0, 400, 400); | |
2901 content_view->AddChildView(parent_view); | |
2902 | |
2903 parent_view->SetPaintToLayer(true); | |
2904 parent_view->SetBounds(0, 0, 400, 400); | |
2905 | |
2906 View* child_view = new View; | |
2907 child_view->SetBounds(50, 50, 100, 100); | |
2908 child_view->SetPaintToLayer(true); | |
2909 child_view->SetFillsBoundsOpaquely(true); | |
2910 parent_view->AddChildView(child_view); | |
2911 widget()->GetCompositor()->Draw(false); | |
2912 EXPECT_EQ( | |
2913 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect()); | |
2914 | |
2915 child_view->SetVisible(false); | |
2916 widget()->GetCompositor()->Draw(false); | |
2917 EXPECT_TRUE(parent_view->layer()->hole_rect().IsEmpty()); | |
2918 | |
2919 child_view->SetVisible(true); | |
2920 widget()->GetCompositor()->Draw(false); | |
2921 EXPECT_EQ( | |
2922 gfx::Rect(50, 50, 100, 100), parent_view->layer()->hole_rect()); | |
2923 } | |
2924 | |
2925 // Tests that the layers in the subtree are orphaned after a View is removed | |
2926 // from the parent. | |
2927 TEST_F(ViewLayerTest, OrphanLayerAfterViewRemove) { | |
2928 View* content_view = new View; | |
2929 widget()->SetContentsView(content_view); | |
2930 | |
2931 View* v1 = new View; | |
2932 content_view->AddChildView(v1); | |
2933 | |
2934 View* v2 = new View; | |
2935 v1->AddChildView(v2); | |
2936 v2->SetPaintToLayer(true); | |
2937 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(), | |
2938 v2->layer())); | |
2939 EXPECT_TRUE(v2->layer()->IsDrawn()); | |
2940 | |
2941 content_view->RemoveChildView(v1); | |
2942 EXPECT_FALSE(LayerIsAncestor(widget()->GetCompositor()->root_layer(), | |
2943 v2->layer())); | |
2944 | |
2945 // Reparent |v2|. | |
2946 content_view->AddChildView(v2); | |
2947 EXPECT_TRUE(LayerIsAncestor(widget()->GetCompositor()->root_layer(), | |
2948 v2->layer())); | |
2949 EXPECT_TRUE(v2->layer()->IsDrawn()); | |
2950 } | |
2951 | |
2952 class PaintTrackingView : public View { | |
2953 public: | |
2954 PaintTrackingView() : painted_(false) { | |
2955 } | |
2956 | |
2957 bool painted() const { return painted_; } | |
2958 void set_painted(bool value) { painted_ = value; } | |
2959 | |
2960 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
2961 painted_ = true; | |
2962 } | |
2963 | |
2964 private: | |
2965 bool painted_; | |
2966 | |
2967 DISALLOW_COPY_AND_ASSIGN(PaintTrackingView); | |
2968 }; | |
2969 | |
2970 #if !defined(USE_WEBKIT_COMPOSITOR) | |
2971 // TODO(piman): this test relies on the way the non-webkit compositor works. | |
2972 // Layer::DrawTree should not be called with the webkit compositor. In the | |
2973 // WebKit case, it needs to go through the "real" compositor (not the test one) | |
2974 // to do the paints on the layer/views. | |
2975 | |
2976 // Makes sure child views with layers aren't painted when paint starts at an | |
2977 // ancestor. | |
2978 TEST_F(ViewLayerTest, DontPaintChildrenWithLayers) { | |
2979 PaintTrackingView* content_view = new PaintTrackingView; | |
2980 widget()->SetContentsView(content_view); | |
2981 content_view->SetPaintToLayer(true); | |
2982 GetRootLayer()->DrawTree(); | |
2983 GetRootLayer()->SchedulePaint(gfx::Rect(0, 0, 10, 10)); | |
2984 content_view->set_painted(false); | |
2985 // content_view no longer has a dirty rect. Paint from the root and make sure | |
2986 // PaintTrackingView isn't painted. | |
2987 GetRootLayer()->DrawTree(); | |
2988 EXPECT_FALSE(content_view->painted()); | |
2989 | |
2990 // Make content_view have a dirty rect, paint the layers and make sure | |
2991 // PaintTrackingView is painted. | |
2992 content_view->layer()->SchedulePaint(gfx::Rect(0, 0, 10, 10)); | |
2993 GetRootLayer()->DrawTree(); | |
2994 EXPECT_TRUE(content_view->painted()); | |
2995 } | |
2996 #endif | |
2997 | |
2998 // Tests that the visibility of child layers are updated correctly when a View's | |
2999 // visibility changes. | |
3000 TEST_F(ViewLayerTest, VisibilityChildLayers) { | |
3001 View* v1 = new View; | |
3002 v1->SetPaintToLayer(true); | |
3003 widget()->SetContentsView(v1); | |
3004 | |
3005 View* v2 = new View; | |
3006 v1->AddChildView(v2); | |
3007 | |
3008 View* v3 = new View; | |
3009 v2->AddChildView(v3); | |
3010 v3->SetVisible(false); | |
3011 | |
3012 View* v4 = new View; | |
3013 v4->SetPaintToLayer(true); | |
3014 v3->AddChildView(v4); | |
3015 | |
3016 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
3017 EXPECT_FALSE(v4->layer()->IsDrawn()); | |
3018 | |
3019 v2->SetVisible(false); | |
3020 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
3021 EXPECT_FALSE(v4->layer()->IsDrawn()); | |
3022 | |
3023 v2->SetVisible(true); | |
3024 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
3025 EXPECT_FALSE(v4->layer()->IsDrawn()); | |
3026 | |
3027 v2->SetVisible(false); | |
3028 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
3029 EXPECT_FALSE(v4->layer()->IsDrawn()); | |
3030 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer())); | |
3031 | |
3032 v3->SetVisible(true); | |
3033 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
3034 EXPECT_FALSE(v4->layer()->IsDrawn()); | |
3035 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer())); | |
3036 | |
3037 // Reparent |v3| to |v1|. | |
3038 v1->AddChildView(v3); | |
3039 EXPECT_TRUE(v1->layer()->IsDrawn()); | |
3040 EXPECT_TRUE(v4->layer()->IsDrawn()); | |
3041 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(v1, v1->layer())); | |
3042 } | |
3043 | |
3044 // This test creates a random View tree, and then randomly reorders child views, | |
3045 // reparents views etc. Unrelated changes can appear to break this test. So | |
3046 // marking this as FLAKY. | |
3047 TEST_F(ViewLayerTest, FLAKY_ViewLayerTreesInSync) { | |
3048 View* content = new View; | |
3049 content->SetPaintToLayer(true); | |
3050 widget()->SetContentsView(content); | |
3051 widget()->Show(); | |
3052 | |
3053 ConstructTree(content, 5); | |
3054 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer())); | |
3055 | |
3056 ScrambleTree(content); | |
3057 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer())); | |
3058 | |
3059 ScrambleTree(content); | |
3060 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer())); | |
3061 | |
3062 ScrambleTree(content); | |
3063 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer())); | |
3064 } | |
3065 | |
3066 #endif // VIEWS_COMPOSITOR | |
3067 | |
3068 } // namespace views | |
OLD | NEW |