Add ICU message format support
[chromium-blink-merge.git] / ui / aura / window_unittest.cc
blob0df1a0d8e77cbbf3ba42f64d65d6f2cea81ab2d3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/aura/window.h"
7 #include <string>
8 #include <utility>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/aura/client/capture_client.h"
18 #include "ui/aura/client/focus_change_observer.h"
19 #include "ui/aura/client/visibility_client.h"
20 #include "ui/aura/client/window_tree_client.h"
21 #include "ui/aura/test/aura_test_base.h"
22 #include "ui/aura/test/aura_test_utils.h"
23 #include "ui/aura/test/test_window_delegate.h"
24 #include "ui/aura/test/test_windows.h"
25 #include "ui/aura/test/window_test_api.h"
26 #include "ui/aura/window_delegate.h"
27 #include "ui/aura/window_event_dispatcher.h"
28 #include "ui/aura/window_observer.h"
29 #include "ui/aura/window_property.h"
30 #include "ui/aura/window_tree_host.h"
31 #include "ui/base/hit_test.h"
32 #include "ui/compositor/layer.h"
33 #include "ui/compositor/layer_animation_observer.h"
34 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
35 #include "ui/compositor/scoped_layer_animation_settings.h"
36 #include "ui/compositor/test/test_layers.h"
37 #include "ui/events/event.h"
38 #include "ui/events/event_utils.h"
39 #include "ui/events/gesture_detection/gesture_configuration.h"
40 #include "ui/events/keycodes/keyboard_codes.h"
41 #include "ui/events/test/event_generator.h"
42 #include "ui/gfx/canvas.h"
43 #include "ui/gfx/geometry/vector2d.h"
44 #include "ui/gfx/screen.h"
45 #include "ui/gfx/skia_util.h"
47 DECLARE_WINDOW_PROPERTY_TYPE(const char*)
49 namespace {
51 class TestProperty {
52 public:
53 TestProperty() {}
54 ~TestProperty() {
55 last_deleted_ = this;
57 static TestProperty* last_deleted() { return last_deleted_; }
59 private:
60 static TestProperty* last_deleted_;
61 DISALLOW_COPY_AND_ASSIGN(TestProperty);
64 TestProperty* TestProperty::last_deleted_ = nullptr;
66 DEFINE_OWNED_WINDOW_PROPERTY_KEY(TestProperty, kOwnedKey, NULL);
68 } // namespace
70 DECLARE_WINDOW_PROPERTY_TYPE(TestProperty*);
72 namespace aura {
73 namespace test {
75 class WindowTest : public AuraTestBase {
76 public:
77 WindowTest() : max_separation_(0) {
80 void SetUp() override {
81 AuraTestBase::SetUp();
82 // TODO: there needs to be an easier way to do this.
83 max_separation_ = ui::GestureConfiguration::GetInstance()
84 ->max_separation_for_gesture_touches_in_pixels();
85 ui::GestureConfiguration::GetInstance()
86 ->set_max_separation_for_gesture_touches_in_pixels(0);
89 void TearDown() override {
90 AuraTestBase::TearDown();
91 ui::GestureConfiguration::GetInstance()
92 ->set_max_separation_for_gesture_touches_in_pixels(max_separation_);
95 private:
96 float max_separation_;
98 DISALLOW_COPY_AND_ASSIGN(WindowTest);
101 namespace {
103 // Used for verifying destruction methods are invoked.
104 class DestroyTrackingDelegateImpl : public TestWindowDelegate {
105 public:
106 DestroyTrackingDelegateImpl()
107 : destroying_count_(0),
108 destroyed_count_(0),
109 in_destroying_(false) {}
111 void clear_destroying_count() { destroying_count_ = 0; }
112 int destroying_count() const { return destroying_count_; }
114 void clear_destroyed_count() { destroyed_count_ = 0; }
115 int destroyed_count() const { return destroyed_count_; }
117 bool in_destroying() const { return in_destroying_; }
119 void OnWindowDestroying(Window* window) override {
120 EXPECT_FALSE(in_destroying_);
121 in_destroying_ = true;
122 destroying_count_++;
125 void OnWindowDestroyed(Window* window) override {
126 EXPECT_TRUE(in_destroying_);
127 in_destroying_ = false;
128 destroyed_count_++;
131 private:
132 int destroying_count_;
133 int destroyed_count_;
134 bool in_destroying_;
136 DISALLOW_COPY_AND_ASSIGN(DestroyTrackingDelegateImpl);
139 // Used to verify that when OnWindowDestroying is invoked the parent is also
140 // is in the process of being destroyed.
141 class ChildWindowDelegateImpl : public DestroyTrackingDelegateImpl {
142 public:
143 explicit ChildWindowDelegateImpl(
144 DestroyTrackingDelegateImpl* parent_delegate)
145 : parent_delegate_(parent_delegate) {
148 void OnWindowDestroying(Window* window) override {
149 EXPECT_TRUE(parent_delegate_->in_destroying());
150 DestroyTrackingDelegateImpl::OnWindowDestroying(window);
153 private:
154 DestroyTrackingDelegateImpl* parent_delegate_;
156 DISALLOW_COPY_AND_ASSIGN(ChildWindowDelegateImpl);
159 // Used to verify that a Window is removed from its parent when
160 // OnWindowDestroyed is called.
161 class DestroyOrphanDelegate : public TestWindowDelegate {
162 public:
163 DestroyOrphanDelegate() : window_(NULL) {
166 void set_window(Window* window) { window_ = window; }
168 void OnWindowDestroyed(Window* window) override {
169 EXPECT_FALSE(window_->parent());
172 private:
173 Window* window_;
174 DISALLOW_COPY_AND_ASSIGN(DestroyOrphanDelegate);
177 // Used in verifying mouse capture.
178 class CaptureWindowDelegateImpl : public TestWindowDelegate {
179 public:
180 CaptureWindowDelegateImpl() {
181 ResetCounts();
184 void ResetCounts() {
185 capture_changed_event_count_ = 0;
186 capture_lost_count_ = 0;
187 mouse_event_count_ = 0;
188 touch_event_count_ = 0;
189 gesture_event_count_ = 0;
192 int capture_changed_event_count() const {
193 return capture_changed_event_count_;
195 int capture_lost_count() const { return capture_lost_count_; }
196 int mouse_event_count() const { return mouse_event_count_; }
197 int touch_event_count() const { return touch_event_count_; }
198 int gesture_event_count() const { return gesture_event_count_; }
200 void OnMouseEvent(ui::MouseEvent* event) override {
201 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED)
202 capture_changed_event_count_++;
203 mouse_event_count_++;
205 void OnTouchEvent(ui::TouchEvent* event) override { touch_event_count_++; }
206 void OnGestureEvent(ui::GestureEvent* event) override {
207 gesture_event_count_++;
209 void OnCaptureLost() override { capture_lost_count_++; }
211 private:
212 int capture_changed_event_count_;
213 int capture_lost_count_;
214 int mouse_event_count_;
215 int touch_event_count_;
216 int gesture_event_count_;
218 DISALLOW_COPY_AND_ASSIGN(CaptureWindowDelegateImpl);
221 // Keeps track of the location of the gesture.
222 class GestureTrackPositionDelegate : public TestWindowDelegate {
223 public:
224 GestureTrackPositionDelegate() {}
226 void OnGestureEvent(ui::GestureEvent* event) override {
227 position_ = event->location();
228 event->StopPropagation();
231 const gfx::Point& position() const { return position_; }
233 private:
234 gfx::Point position_;
236 DISALLOW_COPY_AND_ASSIGN(GestureTrackPositionDelegate);
239 base::TimeDelta getTime() {
240 return ui::EventTimeForNow();
243 class SelfEventHandlingWindowDelegate : public TestWindowDelegate {
244 public:
245 SelfEventHandlingWindowDelegate() {}
247 bool ShouldDescendIntoChildForEventHandling(
248 Window* child,
249 const gfx::Point& location) override {
250 return false;
253 private:
254 DISALLOW_COPY_AND_ASSIGN(SelfEventHandlingWindowDelegate);
257 // The delegate deletes itself when the window is being destroyed.
258 class DestroyWindowDelegate : public TestWindowDelegate {
259 public:
260 DestroyWindowDelegate() {}
262 private:
263 ~DestroyWindowDelegate() override {}
265 // Overridden from WindowDelegate.
266 void OnWindowDestroyed(Window* window) override { delete this; }
268 DISALLOW_COPY_AND_ASSIGN(DestroyWindowDelegate);
271 void OffsetBounds(Window* window, int horizontal, int vertical) {
272 gfx::Rect bounds = window->bounds();
273 bounds.Offset(horizontal, vertical);
274 window->SetBounds(bounds);
277 } // namespace
279 TEST_F(WindowTest, GetChildById) {
280 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
281 scoped_ptr<Window> w11(CreateTestWindowWithId(11, w1.get()));
282 scoped_ptr<Window> w111(CreateTestWindowWithId(111, w11.get()));
283 scoped_ptr<Window> w12(CreateTestWindowWithId(12, w1.get()));
285 EXPECT_EQ(NULL, w1->GetChildById(57));
286 EXPECT_EQ(w12.get(), w1->GetChildById(12));
287 EXPECT_EQ(w111.get(), w1->GetChildById(111));
290 // Make sure that Window::Contains correctly handles children, grandchildren,
291 // and not containing NULL or parents.
292 TEST_F(WindowTest, Contains) {
293 Window parent(NULL);
294 parent.Init(ui::LAYER_NOT_DRAWN);
295 Window child1(NULL);
296 child1.Init(ui::LAYER_NOT_DRAWN);
297 Window child2(NULL);
298 child2.Init(ui::LAYER_NOT_DRAWN);
300 parent.AddChild(&child1);
301 child1.AddChild(&child2);
303 EXPECT_TRUE(parent.Contains(&parent));
304 EXPECT_TRUE(parent.Contains(&child1));
305 EXPECT_TRUE(parent.Contains(&child2));
307 EXPECT_FALSE(parent.Contains(NULL));
308 EXPECT_FALSE(child1.Contains(&parent));
309 EXPECT_FALSE(child2.Contains(&child1));
312 TEST_F(WindowTest, ContainsPointInRoot) {
313 scoped_ptr<Window> w(
314 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 5, 5),
315 root_window()));
316 EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(9, 9)));
317 EXPECT_TRUE(w->ContainsPointInRoot(gfx::Point(10, 10)));
318 EXPECT_TRUE(w->ContainsPointInRoot(gfx::Point(14, 14)));
319 EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(15, 15)));
320 EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(20, 20)));
323 TEST_F(WindowTest, ContainsPoint) {
324 scoped_ptr<Window> w(
325 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 5, 5),
326 root_window()));
327 EXPECT_TRUE(w->ContainsPoint(gfx::Point(0, 0)));
328 EXPECT_TRUE(w->ContainsPoint(gfx::Point(4, 4)));
329 EXPECT_FALSE(w->ContainsPoint(gfx::Point(5, 5)));
330 EXPECT_FALSE(w->ContainsPoint(gfx::Point(10, 10)));
333 TEST_F(WindowTest, ConvertPointToWindow) {
334 // Window::ConvertPointToWindow is mostly identical to
335 // Layer::ConvertPointToLayer, except NULL values for |source| are permitted,
336 // in which case the function just returns.
337 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
338 gfx::Point reference_point(100, 100);
339 gfx::Point test_point = reference_point;
340 Window::ConvertPointToTarget(NULL, w1.get(), &test_point);
341 EXPECT_EQ(reference_point, test_point);
344 TEST_F(WindowTest, MoveCursorTo) {
345 scoped_ptr<Window> w1(
346 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
347 root_window()));
348 scoped_ptr<Window> w11(
349 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
350 scoped_ptr<Window> w111(
351 CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
352 scoped_ptr<Window> w1111(
353 CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
355 Window* root = root_window();
356 root->MoveCursorTo(gfx::Point(10, 10));
357 EXPECT_EQ("10,10",
358 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
359 w1->MoveCursorTo(gfx::Point(10, 10));
360 EXPECT_EQ("20,20",
361 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
362 w11->MoveCursorTo(gfx::Point(10, 10));
363 EXPECT_EQ("25,25",
364 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
365 w111->MoveCursorTo(gfx::Point(10, 10));
366 EXPECT_EQ("30,30",
367 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
368 w1111->MoveCursorTo(gfx::Point(10, 10));
369 EXPECT_EQ("35,35",
370 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
373 TEST_F(WindowTest, ContainsMouse) {
374 scoped_ptr<Window> w(
375 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
376 root_window()));
377 w->Show();
378 WindowTestApi w_test_api(w.get());
379 Window* root = root_window();
380 root->MoveCursorTo(gfx::Point(10, 10));
381 EXPECT_TRUE(w_test_api.ContainsMouse());
382 root->MoveCursorTo(gfx::Point(9, 10));
383 EXPECT_FALSE(w_test_api.ContainsMouse());
386 // Test Window::ConvertPointToWindow() with transform to root_window.
387 TEST_F(WindowTest, MoveCursorToWithTransformRootWindow) {
388 gfx::Transform transform;
389 transform.Translate(100.0, 100.0);
390 transform.Rotate(90.0);
391 transform.Scale(2.0, 5.0);
392 host()->SetRootTransform(transform);
393 host()->MoveCursorTo(gfx::Point(10, 10));
394 #if !defined(OS_WIN)
395 // TODO(yoshiki): fix this to build on Windows. See crbug.com/133413.OD
396 EXPECT_EQ("50,120", QueryLatestMousePositionRequestInHost(host()).ToString());
397 #endif
398 EXPECT_EQ("10,10", gfx::Screen::GetScreenFor(
399 root_window())->GetCursorScreenPoint().ToString());
402 // Tests Window::ConvertPointToWindow() with transform to non-root windows.
403 TEST_F(WindowTest, MoveCursorToWithTransformWindow) {
404 scoped_ptr<Window> w1(
405 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
406 root_window()));
408 gfx::Transform transform1;
409 transform1.Scale(2, 2);
410 w1->SetTransform(transform1);
411 w1->MoveCursorTo(gfx::Point(10, 10));
412 EXPECT_EQ("30,30",
413 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
415 gfx::Transform transform2;
416 transform2.Translate(-10, 20);
417 w1->SetTransform(transform2);
418 w1->MoveCursorTo(gfx::Point(10, 10));
419 EXPECT_EQ("10,40",
420 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
422 gfx::Transform transform3;
423 transform3.Rotate(90.0);
424 w1->SetTransform(transform3);
425 w1->MoveCursorTo(gfx::Point(5, 5));
426 EXPECT_EQ("5,15",
427 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
429 gfx::Transform transform4;
430 transform4.Translate(100.0, 100.0);
431 transform4.Rotate(90.0);
432 transform4.Scale(2.0, 5.0);
433 w1->SetTransform(transform4);
434 w1->MoveCursorTo(gfx::Point(10, 10));
435 EXPECT_EQ("60,130",
436 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
439 // Test Window::ConvertPointToWindow() with complex transforms to both root and
440 // non-root windows.
441 // Test Window::ConvertPointToWindow() with transform to root_window.
442 TEST_F(WindowTest, MoveCursorToWithComplexTransform) {
443 scoped_ptr<Window> w1(
444 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
445 root_window()));
446 scoped_ptr<Window> w11(
447 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
448 scoped_ptr<Window> w111(
449 CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
450 scoped_ptr<Window> w1111(
451 CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
453 Window* root = root_window();
455 // The root window expects transforms that produce integer rects.
456 gfx::Transform root_transform;
457 root_transform.Translate(60.0, 70.0);
458 root_transform.Rotate(-90.0);
459 root_transform.Translate(-50.0, -50.0);
460 root_transform.Scale(2.0, 3.0);
462 gfx::Transform transform;
463 transform.Translate(10.0, 20.0);
464 transform.Rotate(10.0);
465 transform.Scale(0.3f, 0.5f);
466 host()->SetRootTransform(root_transform);
467 w1->SetTransform(transform);
468 w11->SetTransform(transform);
469 w111->SetTransform(transform);
470 w1111->SetTransform(transform);
472 w1111->MoveCursorTo(gfx::Point(10, 10));
474 #if !defined(OS_WIN)
475 // TODO(yoshiki): fix this to build on Windows. See crbug.com/133413.
476 EXPECT_EQ("169,80", QueryLatestMousePositionRequestInHost(host()).ToString());
477 #endif
478 EXPECT_EQ("20,53",
479 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
482 // Tests that we do not crash when a Window is destroyed by going out of
483 // scope (as opposed to being explicitly deleted by its WindowDelegate).
484 TEST_F(WindowTest, NoCrashOnWindowDelete) {
485 CaptureWindowDelegateImpl delegate;
486 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
487 &delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
490 TEST_F(WindowTest, GetEventHandlerForPoint) {
491 scoped_ptr<Window> w1(
492 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
493 root_window()));
494 scoped_ptr<Window> w11(
495 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
496 scoped_ptr<Window> w111(
497 CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
498 scoped_ptr<Window> w1111(
499 CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
500 scoped_ptr<Window> w12(
501 CreateTestWindow(SK_ColorMAGENTA, 12, gfx::Rect(10, 420, 25, 25),
502 w1.get()));
503 scoped_ptr<Window> w121(
504 CreateTestWindow(SK_ColorYELLOW, 121, gfx::Rect(5, 5, 5, 5), w12.get()));
505 scoped_ptr<Window> w13(
506 CreateTestWindow(SK_ColorGRAY, 13, gfx::Rect(5, 470, 50, 50), w1.get()));
508 Window* root = root_window();
509 w1->parent()->SetBounds(gfx::Rect(500, 500));
510 EXPECT_EQ(NULL, root->GetEventHandlerForPoint(gfx::Point(5, 5)));
511 EXPECT_EQ(w1.get(), root->GetEventHandlerForPoint(gfx::Point(11, 11)));
512 EXPECT_EQ(w11.get(), root->GetEventHandlerForPoint(gfx::Point(16, 16)));
513 EXPECT_EQ(w111.get(), root->GetEventHandlerForPoint(gfx::Point(21, 21)));
514 EXPECT_EQ(w1111.get(), root->GetEventHandlerForPoint(gfx::Point(26, 26)));
515 EXPECT_EQ(w12.get(), root->GetEventHandlerForPoint(gfx::Point(21, 431)));
516 EXPECT_EQ(w121.get(), root->GetEventHandlerForPoint(gfx::Point(26, 436)));
517 EXPECT_EQ(w13.get(), root->GetEventHandlerForPoint(gfx::Point(26, 481)));
520 TEST_F(WindowTest, GetEventHandlerForPointWithOverride) {
521 // If our child is flush to our top-left corner he gets events just inside the
522 // window edges.
523 scoped_ptr<Window> parent(
524 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 20, 400, 500),
525 root_window()));
526 scoped_ptr<Window> child(
527 CreateTestWindow(SK_ColorRED, 2, gfx::Rect(0, 0, 60, 70), parent.get()));
528 EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
529 EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(1, 1)));
531 // We can override the hit test bounds of the parent to make the parent grab
532 // events along that edge.
533 parent->set_hit_test_bounds_override_inner(gfx::Insets(1, 1, 1, 1));
534 EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
535 EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(1, 1)));
538 TEST_F(WindowTest, GetEventHandlerForPointWithOverrideDescendingOrder) {
539 scoped_ptr<SelfEventHandlingWindowDelegate> parent_delegate(
540 new SelfEventHandlingWindowDelegate);
541 scoped_ptr<Window> parent(CreateTestWindowWithDelegate(
542 parent_delegate.get(), 1, gfx::Rect(10, 20, 400, 500), root_window()));
543 scoped_ptr<Window> child(
544 CreateTestWindow(SK_ColorRED, 2, gfx::Rect(0, 0, 390, 480),
545 parent.get()));
547 // We can override ShouldDescendIntoChildForEventHandling to make the parent
548 // grab all events.
549 EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
550 EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(50, 50)));
553 TEST_F(WindowTest, GetTopWindowContainingPoint) {
554 Window* root = root_window();
555 root->SetBounds(gfx::Rect(0, 0, 300, 300));
557 scoped_ptr<Window> w1(
558 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 100, 100),
559 root_window()));
560 scoped_ptr<Window> w11(
561 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(0, 0, 120, 120), w1.get()));
563 scoped_ptr<Window> w2(
564 CreateTestWindow(SK_ColorRED, 2, gfx::Rect(5, 5, 55, 55),
565 root_window()));
567 scoped_ptr<Window> w3(
568 CreateTestWindowWithDelegate(
569 NULL, 3, gfx::Rect(200, 200, 100, 100), root_window()));
570 scoped_ptr<Window> w31(
571 CreateTestWindow(SK_ColorCYAN, 31, gfx::Rect(0, 0, 50, 50), w3.get()));
572 scoped_ptr<Window> w311(
573 CreateTestWindow(SK_ColorBLUE, 311, gfx::Rect(0, 0, 10, 10), w31.get()));
575 EXPECT_EQ(NULL, root->GetTopWindowContainingPoint(gfx::Point(0, 0)));
576 EXPECT_EQ(w2.get(), root->GetTopWindowContainingPoint(gfx::Point(5, 5)));
577 EXPECT_EQ(w2.get(), root->GetTopWindowContainingPoint(gfx::Point(10, 10)));
578 EXPECT_EQ(w2.get(), root->GetTopWindowContainingPoint(gfx::Point(59, 59)));
579 EXPECT_EQ(w1.get(), root->GetTopWindowContainingPoint(gfx::Point(60, 60)));
580 EXPECT_EQ(w1.get(), root->GetTopWindowContainingPoint(gfx::Point(109, 109)));
581 EXPECT_EQ(NULL, root->GetTopWindowContainingPoint(gfx::Point(110, 110)));
582 EXPECT_EQ(w31.get(), root->GetTopWindowContainingPoint(gfx::Point(200, 200)));
583 EXPECT_EQ(w31.get(), root->GetTopWindowContainingPoint(gfx::Point(220, 220)));
584 EXPECT_EQ(NULL, root->GetTopWindowContainingPoint(gfx::Point(260, 260)));
587 TEST_F(WindowTest, GetToplevelWindow) {
588 const gfx::Rect kBounds(0, 0, 10, 10);
589 TestWindowDelegate delegate;
591 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
592 scoped_ptr<Window> w11(
593 CreateTestWindowWithDelegate(&delegate, 11, kBounds, w1.get()));
594 scoped_ptr<Window> w111(CreateTestWindowWithId(111, w11.get()));
595 scoped_ptr<Window> w1111(
596 CreateTestWindowWithDelegate(&delegate, 1111, kBounds, w111.get()));
598 EXPECT_TRUE(root_window()->GetToplevelWindow() == NULL);
599 EXPECT_TRUE(w1->GetToplevelWindow() == NULL);
600 EXPECT_EQ(w11.get(), w11->GetToplevelWindow());
601 EXPECT_EQ(w11.get(), w111->GetToplevelWindow());
602 EXPECT_EQ(w11.get(), w1111->GetToplevelWindow());
605 class AddedToRootWindowObserver : public WindowObserver {
606 public:
607 AddedToRootWindowObserver() : called_(false) {}
609 void OnWindowAddedToRootWindow(Window* window) override { called_ = true; }
611 bool called() const { return called_; }
613 private:
614 bool called_;
616 DISALLOW_COPY_AND_ASSIGN(AddedToRootWindowObserver);
619 TEST_F(WindowTest, WindowAddedToRootWindowShouldNotifyChildAndNotParent) {
620 AddedToRootWindowObserver parent_observer;
621 AddedToRootWindowObserver child_observer;
622 scoped_ptr<Window> parent_window(CreateTestWindowWithId(1, root_window()));
623 scoped_ptr<Window> child_window(new Window(NULL));
624 child_window->Init(ui::LAYER_TEXTURED);
625 child_window->Show();
627 parent_window->AddObserver(&parent_observer);
628 child_window->AddObserver(&child_observer);
630 parent_window->AddChild(child_window.get());
632 EXPECT_FALSE(parent_observer.called());
633 EXPECT_TRUE(child_observer.called());
635 parent_window->RemoveObserver(&parent_observer);
636 child_window->RemoveObserver(&child_observer);
639 // Various destruction assertions.
640 TEST_F(WindowTest, DestroyTest) {
641 DestroyTrackingDelegateImpl parent_delegate;
642 ChildWindowDelegateImpl child_delegate(&parent_delegate);
644 scoped_ptr<Window> parent(
645 CreateTestWindowWithDelegate(&parent_delegate, 0, gfx::Rect(),
646 root_window()));
647 CreateTestWindowWithDelegate(&child_delegate, 0, gfx::Rect(), parent.get());
649 // Both the parent and child should have been destroyed.
650 EXPECT_EQ(1, parent_delegate.destroying_count());
651 EXPECT_EQ(1, parent_delegate.destroyed_count());
652 EXPECT_EQ(1, child_delegate.destroying_count());
653 EXPECT_EQ(1, child_delegate.destroyed_count());
656 // Tests that a window is orphaned before OnWindowDestroyed is called.
657 TEST_F(WindowTest, OrphanedBeforeOnDestroyed) {
658 TestWindowDelegate parent_delegate;
659 DestroyOrphanDelegate child_delegate;
661 scoped_ptr<Window> parent(
662 CreateTestWindowWithDelegate(&parent_delegate, 0, gfx::Rect(),
663 root_window()));
664 scoped_ptr<Window> child(CreateTestWindowWithDelegate(&child_delegate, 0,
665 gfx::Rect(), parent.get()));
666 child_delegate.set_window(child.get());
670 // Make sure StackChildAtTop moves both the window and layer to the front.
671 TEST_F(WindowTest, StackChildAtTop) {
672 Window parent(NULL);
673 parent.Init(ui::LAYER_NOT_DRAWN);
674 Window child1(NULL);
675 child1.Init(ui::LAYER_NOT_DRAWN);
676 Window child2(NULL);
677 child2.Init(ui::LAYER_NOT_DRAWN);
679 parent.AddChild(&child1);
680 parent.AddChild(&child2);
681 ASSERT_EQ(2u, parent.children().size());
682 EXPECT_EQ(&child1, parent.children()[0]);
683 EXPECT_EQ(&child2, parent.children()[1]);
684 ASSERT_EQ(2u, parent.layer()->children().size());
685 EXPECT_EQ(child1.layer(), parent.layer()->children()[0]);
686 EXPECT_EQ(child2.layer(), parent.layer()->children()[1]);
688 parent.StackChildAtTop(&child1);
689 ASSERT_EQ(2u, parent.children().size());
690 EXPECT_EQ(&child1, parent.children()[1]);
691 EXPECT_EQ(&child2, parent.children()[0]);
692 ASSERT_EQ(2u, parent.layer()->children().size());
693 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
694 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
697 // Make sure StackChildBelow works.
698 TEST_F(WindowTest, StackChildBelow) {
699 Window parent(NULL);
700 parent.Init(ui::LAYER_NOT_DRAWN);
701 Window child1(NULL);
702 child1.Init(ui::LAYER_NOT_DRAWN);
703 child1.set_id(1);
704 Window child2(NULL);
705 child2.Init(ui::LAYER_NOT_DRAWN);
706 child2.set_id(2);
707 Window child3(NULL);
708 child3.Init(ui::LAYER_NOT_DRAWN);
709 child3.set_id(3);
711 parent.AddChild(&child1);
712 parent.AddChild(&child2);
713 parent.AddChild(&child3);
714 EXPECT_EQ("1 2 3", ChildWindowIDsAsString(&parent));
716 parent.StackChildBelow(&child1, &child2);
717 EXPECT_EQ("1 2 3", ChildWindowIDsAsString(&parent));
719 parent.StackChildBelow(&child2, &child1);
720 EXPECT_EQ("2 1 3", ChildWindowIDsAsString(&parent));
722 parent.StackChildBelow(&child3, &child2);
723 EXPECT_EQ("3 2 1", ChildWindowIDsAsString(&parent));
725 parent.StackChildBelow(&child3, &child1);
726 EXPECT_EQ("2 3 1", ChildWindowIDsAsString(&parent));
729 // Various assertions for StackChildAbove.
730 TEST_F(WindowTest, StackChildAbove) {
731 Window parent(NULL);
732 parent.Init(ui::LAYER_NOT_DRAWN);
733 Window child1(NULL);
734 child1.Init(ui::LAYER_NOT_DRAWN);
735 Window child2(NULL);
736 child2.Init(ui::LAYER_NOT_DRAWN);
737 Window child3(NULL);
738 child3.Init(ui::LAYER_NOT_DRAWN);
740 parent.AddChild(&child1);
741 parent.AddChild(&child2);
743 // Move 1 in front of 2.
744 parent.StackChildAbove(&child1, &child2);
745 ASSERT_EQ(2u, parent.children().size());
746 EXPECT_EQ(&child2, parent.children()[0]);
747 EXPECT_EQ(&child1, parent.children()[1]);
748 ASSERT_EQ(2u, parent.layer()->children().size());
749 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
750 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
752 // Add 3, resulting in order [2, 1, 3], then move 2 in front of 1, resulting
753 // in [1, 2, 3].
754 parent.AddChild(&child3);
755 parent.StackChildAbove(&child2, &child1);
756 ASSERT_EQ(3u, parent.children().size());
757 EXPECT_EQ(&child1, parent.children()[0]);
758 EXPECT_EQ(&child2, parent.children()[1]);
759 EXPECT_EQ(&child3, parent.children()[2]);
760 ASSERT_EQ(3u, parent.layer()->children().size());
761 EXPECT_EQ(child1.layer(), parent.layer()->children()[0]);
762 EXPECT_EQ(child2.layer(), parent.layer()->children()[1]);
763 EXPECT_EQ(child3.layer(), parent.layer()->children()[2]);
765 // Move 1 in front of 3, resulting in [2, 3, 1].
766 parent.StackChildAbove(&child1, &child3);
767 ASSERT_EQ(3u, parent.children().size());
768 EXPECT_EQ(&child2, parent.children()[0]);
769 EXPECT_EQ(&child3, parent.children()[1]);
770 EXPECT_EQ(&child1, parent.children()[2]);
771 ASSERT_EQ(3u, parent.layer()->children().size());
772 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
773 EXPECT_EQ(child3.layer(), parent.layer()->children()[1]);
774 EXPECT_EQ(child1.layer(), parent.layer()->children()[2]);
776 // Moving 1 in front of 2 should lower it, resulting in [2, 1, 3].
777 parent.StackChildAbove(&child1, &child2);
778 ASSERT_EQ(3u, parent.children().size());
779 EXPECT_EQ(&child2, parent.children()[0]);
780 EXPECT_EQ(&child1, parent.children()[1]);
781 EXPECT_EQ(&child3, parent.children()[2]);
782 ASSERT_EQ(3u, parent.layer()->children().size());
783 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
784 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
785 EXPECT_EQ(child3.layer(), parent.layer()->children()[2]);
788 // Various capture assertions.
789 TEST_F(WindowTest, CaptureTests) {
790 CaptureWindowDelegateImpl delegate;
791 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
792 &delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
793 EXPECT_FALSE(window->HasCapture());
795 delegate.ResetCounts();
797 // Do a capture.
798 window->SetCapture();
799 EXPECT_TRUE(window->HasCapture());
800 EXPECT_EQ(0, delegate.capture_lost_count());
801 EXPECT_EQ(0, delegate.capture_changed_event_count());
802 ui::test::EventGenerator generator(root_window(), gfx::Point(50, 50));
803 generator.PressLeftButton();
804 EXPECT_EQ(1, delegate.mouse_event_count());
805 generator.ReleaseLeftButton();
807 EXPECT_EQ(2, delegate.mouse_event_count());
808 delegate.ResetCounts();
810 ui::TouchEvent touchev(
811 ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 0, getTime());
812 DispatchEventUsingWindowDispatcher(&touchev);
813 EXPECT_EQ(1, delegate.touch_event_count());
814 delegate.ResetCounts();
816 window->ReleaseCapture();
817 EXPECT_FALSE(window->HasCapture());
818 EXPECT_EQ(1, delegate.capture_lost_count());
819 EXPECT_EQ(1, delegate.capture_changed_event_count());
820 EXPECT_EQ(1, delegate.mouse_event_count());
821 EXPECT_EQ(0, delegate.touch_event_count());
823 generator.PressLeftButton();
824 EXPECT_EQ(1, delegate.mouse_event_count());
826 ui::TouchEvent touchev2(
827 ui::ET_TOUCH_PRESSED, gfx::Point(250, 250), 1, getTime());
828 DispatchEventUsingWindowDispatcher(&touchev2);
829 EXPECT_EQ(0, delegate.touch_event_count());
831 // Removing the capture window from parent should reset the capture window
832 // in the root window.
833 window->SetCapture();
834 EXPECT_EQ(window.get(), aura::client::GetCaptureWindow(root_window()));
835 window->parent()->RemoveChild(window.get());
836 EXPECT_FALSE(window->HasCapture());
837 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(root_window()));
840 TEST_F(WindowTest, TouchCaptureCancelsOtherTouches) {
841 CaptureWindowDelegateImpl delegate1;
842 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(
843 &delegate1, 0, gfx::Rect(0, 0, 50, 50), root_window()));
844 CaptureWindowDelegateImpl delegate2;
845 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
846 &delegate2, 0, gfx::Rect(50, 50, 50, 50), root_window()));
848 // Press on w1.
849 ui::TouchEvent press1(
850 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, getTime());
851 DispatchEventUsingWindowDispatcher(&press1);
852 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
853 EXPECT_EQ(2, delegate1.gesture_event_count());
854 delegate1.ResetCounts();
856 // Capturing to w2 should cause the touch to be canceled.
857 w2->SetCapture();
858 EXPECT_EQ(1, delegate1.touch_event_count());
859 EXPECT_EQ(0, delegate2.touch_event_count());
860 delegate1.ResetCounts();
861 delegate2.ResetCounts();
863 // Events are now untargetted.
864 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 20), 0, getTime());
865 DispatchEventUsingWindowDispatcher(&move);
866 EXPECT_EQ(0, delegate1.gesture_event_count());
867 EXPECT_EQ(0, delegate1.touch_event_count());
868 EXPECT_EQ(0, delegate2.gesture_event_count());
869 EXPECT_EQ(0, delegate2.touch_event_count());
871 ui::TouchEvent release(
872 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), 0, getTime());
873 DispatchEventUsingWindowDispatcher(&release);
874 EXPECT_EQ(0, delegate1.gesture_event_count());
875 EXPECT_EQ(0, delegate2.gesture_event_count());
877 // A new press is captured by w2.
878 ui::TouchEvent press2(
879 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, getTime());
880 DispatchEventUsingWindowDispatcher(&press2);
881 EXPECT_EQ(0, delegate1.gesture_event_count());
882 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
883 EXPECT_EQ(2, delegate2.gesture_event_count());
884 delegate1.ResetCounts();
885 delegate2.ResetCounts();
887 // And releasing capture changes nothing.
888 w2->ReleaseCapture();
889 EXPECT_EQ(0, delegate1.gesture_event_count());
890 EXPECT_EQ(0, delegate1.touch_event_count());
891 EXPECT_EQ(0, delegate2.gesture_event_count());
892 EXPECT_EQ(0, delegate2.touch_event_count());
895 TEST_F(WindowTest, TouchCaptureDoesntCancelCapturedTouches) {
896 CaptureWindowDelegateImpl delegate;
897 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
898 &delegate, 0, gfx::Rect(0, 0, 50, 50), root_window()));
899 base::TimeDelta time = getTime();
900 const int kTimeDelta = 100;
902 ui::TouchEvent press(
903 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, time);
904 DispatchEventUsingWindowDispatcher(&press);
906 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
907 EXPECT_EQ(2, delegate.gesture_event_count());
908 EXPECT_EQ(1, delegate.touch_event_count());
909 delegate.ResetCounts();
911 window->SetCapture();
912 EXPECT_EQ(0, delegate.gesture_event_count());
913 EXPECT_EQ(0, delegate.touch_event_count());
914 delegate.ResetCounts();
916 // On move We will get TOUCH_MOVED, GESTURE_TAP_CANCEL,
917 // GESTURE_SCROLL_START and GESTURE_SCROLL_UPDATE.
918 time += base::TimeDelta::FromMilliseconds(kTimeDelta);
919 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 20), 0, time);
920 DispatchEventUsingWindowDispatcher(&move);
921 EXPECT_EQ(1, delegate.touch_event_count());
922 EXPECT_EQ(3, delegate.gesture_event_count());
923 delegate.ResetCounts();
925 // Release capture shouldn't change anything.
926 window->ReleaseCapture();
927 EXPECT_EQ(0, delegate.touch_event_count());
928 EXPECT_EQ(0, delegate.gesture_event_count());
929 delegate.ResetCounts();
931 // On move we still get TOUCH_MOVED and GESTURE_SCROLL_UPDATE.
932 time += base::TimeDelta::FromMilliseconds(kTimeDelta);
933 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(10, 30), 0, time);
934 DispatchEventUsingWindowDispatcher(&move2);
935 EXPECT_EQ(1, delegate.touch_event_count());
936 EXPECT_EQ(1, delegate.gesture_event_count());
937 delegate.ResetCounts();
939 // And on release we get TOUCH_RELEASED, GESTURE_SCROLL_END, GESTURE_END
940 time += base::TimeDelta::FromMilliseconds(kTimeDelta);
941 ui::TouchEvent release(
942 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), 0, time);
943 DispatchEventUsingWindowDispatcher(&release);
944 EXPECT_EQ(1, delegate.touch_event_count());
945 EXPECT_EQ(2, delegate.gesture_event_count());
949 // Assertions around SetCapture() and touch/gestures.
950 TEST_F(WindowTest, TransferCaptureTouchEvents) {
951 // Touch on |w1|.
952 CaptureWindowDelegateImpl d1;
953 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(
954 &d1, 0, gfx::Rect(0, 0, 20, 20), root_window()));
955 ui::TouchEvent p1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, getTime());
956 DispatchEventUsingWindowDispatcher(&p1);
957 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
958 EXPECT_EQ(1, d1.touch_event_count());
959 EXPECT_EQ(2, d1.gesture_event_count());
960 d1.ResetCounts();
962 // Touch on |w2| with a different id.
963 CaptureWindowDelegateImpl d2;
964 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
965 &d2, 0, gfx::Rect(40, 0, 40, 20), root_window()));
966 ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(41, 10), 1, getTime());
967 DispatchEventUsingWindowDispatcher(&p2);
968 EXPECT_EQ(0, d1.touch_event_count());
969 EXPECT_EQ(0, d1.gesture_event_count());
970 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN for new target window.
971 EXPECT_EQ(1, d2.touch_event_count());
972 EXPECT_EQ(2, d2.gesture_event_count());
973 d1.ResetCounts();
974 d2.ResetCounts();
976 // Set capture on |w2|, this should send a cancel (TAP_CANCEL, END) to |w1|
977 // but not |w2|.
978 w2->SetCapture();
979 EXPECT_EQ(1, d1.touch_event_count());
980 EXPECT_EQ(2, d1.gesture_event_count());
981 EXPECT_EQ(0, d2.touch_event_count());
982 EXPECT_EQ(0, d2.gesture_event_count());
983 d1.ResetCounts();
984 d2.ResetCounts();
986 CaptureWindowDelegateImpl d3;
987 scoped_ptr<Window> w3(CreateTestWindowWithDelegate(
988 &d3, 0, gfx::Rect(0, 0, 100, 101), root_window()));
989 // Set capture on |w3|. All touches have already been cancelled.
990 w3->SetCapture();
991 EXPECT_EQ(0, d1.touch_event_count());
992 EXPECT_EQ(0, d1.gesture_event_count());
993 EXPECT_EQ(1, d2.touch_event_count());
994 EXPECT_EQ(2, d2.gesture_event_count());
995 EXPECT_EQ(0, d3.touch_event_count());
996 EXPECT_EQ(0, d3.gesture_event_count());
997 d2.ResetCounts();
999 // Move touch id originally associated with |w2|. The touch has been
1000 // cancelled, so no events should be dispatched.
1001 ui::TouchEvent m3(ui::ET_TOUCH_MOVED, gfx::Point(110, 105), 1, getTime());
1002 DispatchEventUsingWindowDispatcher(&m3);
1003 EXPECT_EQ(0, d1.touch_event_count());
1004 EXPECT_EQ(0, d1.gesture_event_count());
1005 EXPECT_EQ(0, d2.touch_event_count());
1006 EXPECT_EQ(0, d2.gesture_event_count());
1007 EXPECT_EQ(0, d3.touch_event_count());
1008 EXPECT_EQ(0, d3.gesture_event_count());
1010 // When we release capture, no touches are canceled.
1011 w3->ReleaseCapture();
1012 EXPECT_EQ(0, d1.touch_event_count());
1013 EXPECT_EQ(0, d1.gesture_event_count());
1014 EXPECT_EQ(0, d2.touch_event_count());
1015 EXPECT_EQ(0, d2.gesture_event_count());
1016 EXPECT_EQ(0, d3.touch_event_count());
1017 EXPECT_EQ(0, d3.gesture_event_count());
1019 // The touch has been cancelled, so no events are dispatched.
1020 ui::TouchEvent m4(ui::ET_TOUCH_MOVED, gfx::Point(120, 105), 1, getTime());
1021 DispatchEventUsingWindowDispatcher(&m4);
1022 EXPECT_EQ(0, d1.touch_event_count());
1023 EXPECT_EQ(0, d1.gesture_event_count());
1024 EXPECT_EQ(0, d2.touch_event_count());
1025 EXPECT_EQ(0, d2.gesture_event_count());
1026 EXPECT_EQ(0, d3.touch_event_count());
1027 EXPECT_EQ(0, d3.gesture_event_count());
1030 // Changes capture while capture is already ongoing.
1031 TEST_F(WindowTest, ChangeCaptureWhileMouseDown) {
1032 CaptureWindowDelegateImpl delegate;
1033 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
1034 &delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
1035 CaptureWindowDelegateImpl delegate2;
1036 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
1037 &delegate2, 0, gfx::Rect(20, 20, 20, 20), root_window()));
1039 // Execute the scheduled draws so that mouse events are not
1040 // aggregated.
1041 RunAllPendingInMessageLoop();
1043 EXPECT_FALSE(window->HasCapture());
1045 // Do a capture.
1046 delegate.ResetCounts();
1047 window->SetCapture();
1048 EXPECT_TRUE(window->HasCapture());
1049 EXPECT_EQ(0, delegate.capture_lost_count());
1050 EXPECT_EQ(0, delegate.capture_changed_event_count());
1051 ui::test::EventGenerator generator(root_window(), gfx::Point(50, 50));
1052 generator.PressLeftButton();
1053 EXPECT_EQ(0, delegate.capture_lost_count());
1054 EXPECT_EQ(0, delegate.capture_changed_event_count());
1055 EXPECT_EQ(1, delegate.mouse_event_count());
1057 // Set capture to |w2|, should implicitly unset capture for |window|.
1058 delegate.ResetCounts();
1059 delegate2.ResetCounts();
1060 w2->SetCapture();
1062 generator.MoveMouseTo(gfx::Point(40, 40), 2);
1063 EXPECT_EQ(1, delegate.capture_lost_count());
1064 EXPECT_EQ(1, delegate.capture_changed_event_count());
1065 EXPECT_EQ(1, delegate.mouse_event_count());
1066 EXPECT_EQ(2, delegate2.mouse_event_count());
1069 // Verifies capture is reset when a window is destroyed.
1070 TEST_F(WindowTest, ReleaseCaptureOnDestroy) {
1071 CaptureWindowDelegateImpl delegate;
1072 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
1073 &delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
1074 EXPECT_FALSE(window->HasCapture());
1076 // Do a capture.
1077 window->SetCapture();
1078 EXPECT_TRUE(window->HasCapture());
1080 // Destroy the window.
1081 window.reset();
1083 // Make sure the root window doesn't reference the window anymore.
1084 EXPECT_EQ(NULL, host()->dispatcher()->mouse_pressed_handler());
1085 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(root_window()));
1088 TEST_F(WindowTest, GetBoundsInRootWindow) {
1089 scoped_ptr<Window> viewport(CreateTestWindowWithBounds(
1090 gfx::Rect(0, 0, 300, 300), root_window()));
1091 scoped_ptr<Window> child(CreateTestWindowWithBounds(
1092 gfx::Rect(0, 0, 100, 100), viewport.get()));
1093 // Sanity check.
1094 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1096 // The |child| window's screen bounds should move along with the |viewport|.
1097 viewport->SetBounds(gfx::Rect(-100, -100, 300, 300));
1098 EXPECT_EQ("-100,-100 100x100", child->GetBoundsInRootWindow().ToString());
1100 // The |child| window is moved to the 0,0 in screen coordinates.
1101 // |GetBoundsInRootWindow()| should return 0,0.
1102 child->SetBounds(gfx::Rect(100, 100, 100, 100));
1103 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1106 TEST_F(WindowTest, GetBoundsInRootWindowWithLayers) {
1107 scoped_ptr<Window> viewport(
1108 CreateTestWindowWithBounds(gfx::Rect(0, 0, 300, 300), root_window()));
1110 scoped_ptr<Window> widget(
1111 CreateTestWindowWithBounds(gfx::Rect(0, 0, 200, 200), viewport.get()));
1113 scoped_ptr<Window> child(
1114 CreateTestWindowWithBounds(gfx::Rect(0, 0, 100, 100), widget.get()));
1116 // Sanity check.
1117 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1119 // The |child| window's screen bounds should move along with the |viewport|.
1120 OffsetBounds(viewport.get(), -100, -100);
1121 EXPECT_EQ("-100,-100 100x100", child->GetBoundsInRootWindow().ToString());
1123 OffsetBounds(widget.get(), 50, 50);
1124 EXPECT_EQ("-50,-50 100x100", child->GetBoundsInRootWindow().ToString());
1126 // The |child| window is moved to the 0,0 in screen coordinates.
1127 // |GetBoundsInRootWindow()| should return 0,0.
1128 OffsetBounds(child.get(), 50, 50);
1129 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1132 TEST_F(WindowTest, GetBoundsInRootWindowWithLayersAndTranslations) {
1133 scoped_ptr<Window> viewport(
1134 CreateTestWindowWithBounds(gfx::Rect(0, 0, 300, 300), root_window()));
1136 scoped_ptr<Window> widget(
1137 CreateTestWindowWithBounds(gfx::Rect(0, 0, 200, 200), viewport.get()));
1139 scoped_ptr<Window> child(
1140 CreateTestWindowWithBounds(gfx::Rect(0, 0, 100, 100), widget.get()));
1142 // Sanity check.
1143 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1145 // The |child| window's screen bounds should move along with the |viewport|.
1146 viewport->SetBounds(gfx::Rect(-100, -100, 300, 300));
1147 EXPECT_EQ("-100,-100 100x100", child->GetBoundsInRootWindow().ToString());
1149 widget->SetBounds(gfx::Rect(50, 50, 200, 200));
1150 EXPECT_EQ("-50,-50 100x100", child->GetBoundsInRootWindow().ToString());
1152 // The |child| window is moved to the 0,0 in screen coordinates.
1153 // |GetBoundsInRootWindow()| should return 0,0.
1154 child->SetBounds(gfx::Rect(50, 50, 100, 100));
1155 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1157 gfx::Transform transform1;
1158 transform1.Translate(-10, 20);
1159 viewport->SetTransform(transform1);
1160 EXPECT_EQ("-10,20 100x100", child->GetBoundsInRootWindow().ToString());
1162 gfx::Transform transform2;
1163 transform2.Translate(40, 100);
1164 widget->SetTransform(transform2);
1165 EXPECT_EQ("30,120 100x100", child->GetBoundsInRootWindow().ToString());
1167 // Testing potentially buggy place
1168 gfx::Transform transform3;
1169 transform3.Translate(-30, -120);
1170 child->SetTransform(transform3);
1171 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1174 // TODO(tdanderson): Remove this class and use
1175 // test::EventCountDelegate in its place.
1176 class MouseEnterExitWindowDelegate : public TestWindowDelegate {
1177 public:
1178 MouseEnterExitWindowDelegate() : entered_(false), exited_(false) {}
1180 void OnMouseEvent(ui::MouseEvent* event) override {
1181 switch (event->type()) {
1182 case ui::ET_MOUSE_ENTERED:
1183 EXPECT_TRUE(event->flags() & ui::EF_IS_SYNTHESIZED);
1184 entered_ = true;
1185 break;
1186 case ui::ET_MOUSE_EXITED:
1187 EXPECT_TRUE(event->flags() & ui::EF_IS_SYNTHESIZED);
1188 exited_ = true;
1189 break;
1190 default:
1191 break;
1195 bool entered() const { return entered_; }
1196 bool exited() const { return exited_; }
1198 // Clear the entered / exited states.
1199 void ResetExpectations() {
1200 entered_ = false;
1201 exited_ = false;
1204 private:
1205 bool entered_;
1206 bool exited_;
1208 DISALLOW_COPY_AND_ASSIGN(MouseEnterExitWindowDelegate);
1211 // Verifies that the WindowDelegate receives MouseExit and MouseEnter events for
1212 // mouse transitions from window to window.
1213 TEST_F(WindowTest, MouseEnterExit) {
1214 MouseEnterExitWindowDelegate d1;
1215 scoped_ptr<Window> w1(
1216 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1217 root_window()));
1218 MouseEnterExitWindowDelegate d2;
1219 scoped_ptr<Window> w2(
1220 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(70, 70, 50, 50),
1221 root_window()));
1223 ui::test::EventGenerator generator(root_window());
1224 generator.MoveMouseToCenterOf(w1.get());
1225 EXPECT_TRUE(d1.entered());
1226 EXPECT_FALSE(d1.exited());
1227 EXPECT_FALSE(d2.entered());
1228 EXPECT_FALSE(d2.exited());
1230 generator.MoveMouseToCenterOf(w2.get());
1231 EXPECT_TRUE(d1.entered());
1232 EXPECT_TRUE(d1.exited());
1233 EXPECT_TRUE(d2.entered());
1234 EXPECT_FALSE(d2.exited());
1237 // Verifies that the WindowDelegate receives MouseExit from ET_MOUSE_EXITED.
1238 TEST_F(WindowTest, WindowTreeHostExit) {
1239 MouseEnterExitWindowDelegate d1;
1240 scoped_ptr<Window> w1(
1241 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1242 root_window()));
1244 ui::test::EventGenerator generator(root_window());
1245 generator.MoveMouseToCenterOf(w1.get());
1246 EXPECT_TRUE(d1.entered());
1247 EXPECT_FALSE(d1.exited());
1248 d1.ResetExpectations();
1250 ui::MouseEvent exit_event(ui::ET_MOUSE_EXITED, gfx::Point(), gfx::Point(),
1251 ui::EventTimeForNow(), 0, 0);
1252 DispatchEventUsingWindowDispatcher(&exit_event);
1253 EXPECT_FALSE(d1.entered());
1254 EXPECT_TRUE(d1.exited());
1257 // Verifies that the WindowDelegate receives MouseExit and MouseEnter events for
1258 // mouse transitions from window to window, even if the entered window sets
1259 // and releases capture.
1260 TEST_F(WindowTest, MouseEnterExitWithClick) {
1261 MouseEnterExitWindowDelegate d1;
1262 scoped_ptr<Window> w1(
1263 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1264 root_window()));
1265 MouseEnterExitWindowDelegate d2;
1266 scoped_ptr<Window> w2(
1267 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(70, 70, 50, 50),
1268 root_window()));
1270 ui::test::EventGenerator generator(root_window());
1271 generator.MoveMouseToCenterOf(w1.get());
1272 EXPECT_TRUE(d1.entered());
1273 EXPECT_FALSE(d1.exited());
1274 EXPECT_FALSE(d2.entered());
1275 EXPECT_FALSE(d2.exited());
1277 // Emmulate what Views does on a click by grabbing and releasing capture.
1278 generator.PressLeftButton();
1279 w1->SetCapture();
1280 w1->ReleaseCapture();
1281 generator.ReleaseLeftButton();
1283 generator.MoveMouseToCenterOf(w2.get());
1284 EXPECT_TRUE(d1.entered());
1285 EXPECT_TRUE(d1.exited());
1286 EXPECT_TRUE(d2.entered());
1287 EXPECT_FALSE(d2.exited());
1290 TEST_F(WindowTest, MouseEnterExitWhenDeleteWithCapture) {
1291 MouseEnterExitWindowDelegate delegate;
1292 scoped_ptr<Window> window(
1293 CreateTestWindowWithDelegate(&delegate, 1, gfx::Rect(10, 10, 50, 50),
1294 root_window()));
1296 ui::test::EventGenerator generator(root_window());
1297 generator.MoveMouseToCenterOf(window.get());
1298 EXPECT_TRUE(delegate.entered());
1299 EXPECT_FALSE(delegate.exited());
1301 // Emmulate what Views does on a click by grabbing and releasing capture.
1302 generator.PressLeftButton();
1303 window->SetCapture();
1305 delegate.ResetExpectations();
1306 generator.MoveMouseTo(0, 0);
1307 EXPECT_FALSE(delegate.entered());
1308 EXPECT_FALSE(delegate.exited());
1310 delegate.ResetExpectations();
1311 window.reset();
1312 EXPECT_FALSE(delegate.entered());
1313 EXPECT_FALSE(delegate.exited());
1316 // Verifies that the correct enter / exits are sent if windows appear and are
1317 // deleted under the current mouse position.
1318 TEST_F(WindowTest, MouseEnterExitWithWindowAppearAndDelete) {
1319 MouseEnterExitWindowDelegate d1;
1320 scoped_ptr<Window> w1(
1321 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1322 root_window()));
1324 // The cursor is moved into the bounds of |w1|. We expect the delegate
1325 // of |w1| to see an ET_MOUSE_ENTERED event.
1326 ui::test::EventGenerator generator(root_window());
1327 generator.MoveMouseToCenterOf(w1.get());
1328 EXPECT_TRUE(d1.entered());
1329 EXPECT_FALSE(d1.exited());
1330 d1.ResetExpectations();
1332 MouseEnterExitWindowDelegate d2;
1334 scoped_ptr<Window> w2(
1335 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1336 root_window()));
1337 // Enters / exits can be sent asynchronously.
1338 RunAllPendingInMessageLoop();
1340 // |w2| appears over top of |w1|. We expect the delegate of |w1| to see
1341 // an ET_MOUSE_EXITED and the delegate of |w2| to see an ET_MOUSE_ENTERED.
1342 EXPECT_FALSE(d1.entered());
1343 EXPECT_TRUE(d1.exited());
1344 EXPECT_TRUE(d2.entered());
1345 EXPECT_FALSE(d2.exited());
1346 d1.ResetExpectations();
1347 d2.ResetExpectations();
1350 // Enters / exits can be sent asynchronously.
1351 RunAllPendingInMessageLoop();
1353 // |w2| has been destroyed, so its delegate should see no further events.
1354 // The delegate of |w1| should see an ET_MOUSE_ENTERED event.
1355 EXPECT_TRUE(d1.entered());
1356 EXPECT_FALSE(d1.exited());
1357 EXPECT_FALSE(d2.entered());
1358 EXPECT_FALSE(d2.exited());
1361 // Verifies that enter / exits are sent if windows appear and are hidden
1362 // under the current mouse position..
1363 TEST_F(WindowTest, MouseEnterExitWithHide) {
1364 MouseEnterExitWindowDelegate d1;
1365 scoped_ptr<Window> w1(
1366 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1367 root_window()));
1369 ui::test::EventGenerator generator(root_window());
1370 generator.MoveMouseToCenterOf(w1.get());
1371 EXPECT_TRUE(d1.entered());
1372 EXPECT_FALSE(d1.exited());
1374 MouseEnterExitWindowDelegate d2;
1375 scoped_ptr<Window> w2(
1376 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1377 root_window()));
1378 // Enters / exits can be send asynchronously.
1379 RunAllPendingInMessageLoop();
1380 EXPECT_TRUE(d1.entered());
1381 EXPECT_TRUE(d1.exited());
1382 EXPECT_TRUE(d2.entered());
1383 EXPECT_FALSE(d2.exited());
1385 d1.ResetExpectations();
1386 w2->Hide();
1387 // Enters / exits can be send asynchronously.
1388 RunAllPendingInMessageLoop();
1389 EXPECT_TRUE(d2.exited());
1390 EXPECT_TRUE(d1.entered());
1393 TEST_F(WindowTest, MouseEnterExitWithParentHide) {
1394 MouseEnterExitWindowDelegate d1;
1395 scoped_ptr<Window> w1(
1396 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1397 root_window()));
1398 MouseEnterExitWindowDelegate d2;
1399 Window* w2 = CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1400 w1.get());
1401 ui::test::EventGenerator generator(root_window());
1402 generator.MoveMouseToCenterOf(w2);
1403 // Enters / exits can be send asynchronously.
1404 RunAllPendingInMessageLoop();
1405 EXPECT_TRUE(d2.entered());
1406 EXPECT_FALSE(d2.exited());
1408 d2.ResetExpectations();
1409 w1->Hide();
1410 RunAllPendingInMessageLoop();
1411 EXPECT_FALSE(d2.entered());
1412 EXPECT_TRUE(d2.exited());
1414 w1.reset();
1417 TEST_F(WindowTest, MouseEnterExitWithParentDelete) {
1418 MouseEnterExitWindowDelegate d1;
1419 scoped_ptr<Window> w1(
1420 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1421 root_window()));
1422 MouseEnterExitWindowDelegate d2;
1423 Window* w2 = CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1424 w1.get());
1425 ui::test::EventGenerator generator(root_window());
1426 generator.MoveMouseToCenterOf(w2);
1428 // Enters / exits can be send asynchronously.
1429 RunAllPendingInMessageLoop();
1430 EXPECT_TRUE(d2.entered());
1431 EXPECT_FALSE(d2.exited());
1433 d2.ResetExpectations();
1434 w1.reset();
1435 RunAllPendingInMessageLoop();
1437 // Both windows are in the process of destroying, so their delegates should
1438 // not see any mouse events.
1439 EXPECT_FALSE(d1.entered());
1440 EXPECT_FALSE(d1.exited());
1441 EXPECT_FALSE(d2.entered());
1442 EXPECT_FALSE(d2.exited());
1445 // Creates a window with a delegate (w111) that can handle events at a lower
1446 // z-index than a window without a delegate (w12). w12 is sized to fill the
1447 // entire bounds of the container. This test verifies that
1448 // GetEventHandlerForPoint() skips w12 even though its bounds contain the event,
1449 // because it has no children that can handle the event and it has no delegate
1450 // allowing it to handle the event itself.
1451 TEST_F(WindowTest, GetEventHandlerForPoint_NoDelegate) {
1452 TestWindowDelegate d111;
1453 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(NULL, 1,
1454 gfx::Rect(0, 0, 500, 500), root_window()));
1455 scoped_ptr<Window> w11(CreateTestWindowWithDelegate(NULL, 11,
1456 gfx::Rect(0, 0, 500, 500), w1.get()));
1457 scoped_ptr<Window> w111(CreateTestWindowWithDelegate(&d111, 111,
1458 gfx::Rect(50, 50, 450, 450), w11.get()));
1459 scoped_ptr<Window> w12(CreateTestWindowWithDelegate(NULL, 12,
1460 gfx::Rect(0, 0, 500, 500), w1.get()));
1462 gfx::Point target_point = w111->bounds().CenterPoint();
1463 EXPECT_EQ(w111.get(), w1->GetEventHandlerForPoint(target_point));
1466 class VisibilityWindowDelegate : public TestWindowDelegate {
1467 public:
1468 VisibilityWindowDelegate()
1469 : shown_(0),
1470 hidden_(0) {
1473 int shown() const { return shown_; }
1474 int hidden() const { return hidden_; }
1475 void Clear() {
1476 shown_ = 0;
1477 hidden_ = 0;
1480 void OnWindowTargetVisibilityChanged(bool visible) override {
1481 if (visible)
1482 shown_++;
1483 else
1484 hidden_++;
1487 private:
1488 int shown_;
1489 int hidden_;
1491 DISALLOW_COPY_AND_ASSIGN(VisibilityWindowDelegate);
1494 // Verifies show/hide propagate correctly to children and the layer.
1495 TEST_F(WindowTest, Visibility) {
1496 VisibilityWindowDelegate d;
1497 VisibilityWindowDelegate d2;
1498 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(&d, 1, gfx::Rect(),
1499 root_window()));
1500 scoped_ptr<Window> w2(
1501 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(), w1.get()));
1502 scoped_ptr<Window> w3(CreateTestWindowWithId(3, w2.get()));
1504 // Create shows all the windows.
1505 EXPECT_TRUE(w1->IsVisible());
1506 EXPECT_TRUE(w2->IsVisible());
1507 EXPECT_TRUE(w3->IsVisible());
1508 EXPECT_EQ(1, d.shown());
1510 d.Clear();
1511 w1->Hide();
1512 EXPECT_FALSE(w1->IsVisible());
1513 EXPECT_FALSE(w2->IsVisible());
1514 EXPECT_FALSE(w3->IsVisible());
1515 EXPECT_EQ(1, d.hidden());
1516 EXPECT_EQ(0, d.shown());
1518 w2->Show();
1519 EXPECT_FALSE(w1->IsVisible());
1520 EXPECT_FALSE(w2->IsVisible());
1521 EXPECT_FALSE(w3->IsVisible());
1523 w3->Hide();
1524 EXPECT_FALSE(w1->IsVisible());
1525 EXPECT_FALSE(w2->IsVisible());
1526 EXPECT_FALSE(w3->IsVisible());
1528 d.Clear();
1529 w1->Show();
1530 EXPECT_TRUE(w1->IsVisible());
1531 EXPECT_TRUE(w2->IsVisible());
1532 EXPECT_FALSE(w3->IsVisible());
1533 EXPECT_EQ(0, d.hidden());
1534 EXPECT_EQ(1, d.shown());
1536 w3->Show();
1537 EXPECT_TRUE(w1->IsVisible());
1538 EXPECT_TRUE(w2->IsVisible());
1539 EXPECT_TRUE(w3->IsVisible());
1541 // Verify that if an ancestor isn't visible and we change the visibility of a
1542 // child window that OnChildWindowVisibilityChanged() is still invoked.
1543 w1->Hide();
1544 d2.Clear();
1545 w2->Hide();
1546 EXPECT_EQ(1, d2.hidden());
1547 EXPECT_EQ(0, d2.shown());
1548 d2.Clear();
1549 w2->Show();
1550 EXPECT_EQ(0, d2.hidden());
1551 EXPECT_EQ(1, d2.shown());
1554 TEST_F(WindowTest, IgnoreEventsTest) {
1555 TestWindowDelegate d11;
1556 TestWindowDelegate d12;
1557 TestWindowDelegate d111;
1558 TestWindowDelegate d121;
1559 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(NULL, 1,
1560 gfx::Rect(0, 0, 500, 500), root_window()));
1561 scoped_ptr<Window> w11(CreateTestWindowWithDelegate(&d11, 11,
1562 gfx::Rect(0, 0, 500, 500), w1.get()));
1563 scoped_ptr<Window> w111(CreateTestWindowWithDelegate(&d111, 111,
1564 gfx::Rect(50, 50, 450, 450), w11.get()));
1565 scoped_ptr<Window> w12(CreateTestWindowWithDelegate(&d12, 12,
1566 gfx::Rect(0, 0, 500, 500), w1.get()));
1567 scoped_ptr<Window> w121(CreateTestWindowWithDelegate(&d121, 121,
1568 gfx::Rect(150, 150, 50, 50), w12.get()));
1570 EXPECT_EQ(w12.get(), w1->GetEventHandlerForPoint(gfx::Point(10, 10)));
1571 w12->set_ignore_events(true);
1572 EXPECT_EQ(w11.get(), w1->GetEventHandlerForPoint(gfx::Point(10, 10)));
1573 w12->set_ignore_events(false);
1575 EXPECT_EQ(w121.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1576 w121->set_ignore_events(true);
1577 EXPECT_EQ(w12.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1578 w12->set_ignore_events(true);
1579 EXPECT_EQ(w111.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1580 w111->set_ignore_events(true);
1581 EXPECT_EQ(w11.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1584 // Tests transformation on the root window.
1585 TEST_F(WindowTest, Transform) {
1586 gfx::Size size = host()->GetBounds().size();
1587 EXPECT_EQ(gfx::Rect(size),
1588 gfx::Screen::GetScreenFor(root_window())->GetDisplayNearestPoint(
1589 gfx::Point()).bounds());
1591 // Rotate it clock-wise 90 degrees.
1592 gfx::Transform transform;
1593 transform.Translate(size.height(), 0);
1594 transform.Rotate(90.0);
1595 host()->SetRootTransform(transform);
1597 // The size should be the transformed size.
1598 gfx::Size transformed_size(size.height(), size.width());
1599 EXPECT_EQ(transformed_size.ToString(),
1600 root_window()->bounds().size().ToString());
1601 EXPECT_EQ(
1602 gfx::Rect(transformed_size).ToString(),
1603 gfx::Screen::GetScreenFor(root_window())->GetDisplayNearestPoint(
1604 gfx::Point()).bounds().ToString());
1606 // Host size shouldn't change.
1607 EXPECT_EQ(size.ToString(), host()->GetBounds().size().ToString());
1610 TEST_F(WindowTest, TransformGesture) {
1611 gfx::Size size = host()->GetBounds().size();
1613 scoped_ptr<GestureTrackPositionDelegate> delegate(
1614 new GestureTrackPositionDelegate);
1615 scoped_ptr<Window> window(CreateTestWindowWithDelegate(delegate.get(), -1234,
1616 gfx::Rect(0, 0, 20, 20), root_window()));
1618 // Rotate the root-window clock-wise 90 degrees.
1619 gfx::Transform transform;
1620 transform.Translate(size.height(), 0.0);
1621 transform.Rotate(90.0);
1622 host()->SetRootTransform(transform);
1624 ui::TouchEvent press(
1625 ui::ET_TOUCH_PRESSED, gfx::Point(size.height() - 10, 10), 0, getTime());
1626 DispatchEventUsingWindowDispatcher(&press);
1627 EXPECT_EQ(gfx::Point(10, 10).ToString(), delegate->position().ToString());
1630 namespace {
1631 DEFINE_WINDOW_PROPERTY_KEY(int, kIntKey, -2);
1632 DEFINE_WINDOW_PROPERTY_KEY(const char*, kStringKey, "squeamish");
1635 TEST_F(WindowTest, Property) {
1636 scoped_ptr<Window> w(CreateTestWindowWithId(0, root_window()));
1638 static const char native_prop_key[] = "fnord";
1640 // Non-existent properties should return the default values.
1641 EXPECT_EQ(-2, w->GetProperty(kIntKey));
1642 EXPECT_EQ(std::string("squeamish"), w->GetProperty(kStringKey));
1643 EXPECT_EQ(NULL, w->GetNativeWindowProperty(native_prop_key));
1645 // A set property value should be returned again (even if it's the default
1646 // value).
1647 w->SetProperty(kIntKey, INT_MAX);
1648 EXPECT_EQ(INT_MAX, w->GetProperty(kIntKey));
1649 w->SetProperty(kIntKey, -2);
1650 EXPECT_EQ(-2, w->GetProperty(kIntKey));
1651 w->SetProperty(kIntKey, INT_MIN);
1652 EXPECT_EQ(INT_MIN, w->GetProperty(kIntKey));
1654 w->SetProperty(kStringKey, static_cast<const char*>(NULL));
1655 EXPECT_EQ(NULL, w->GetProperty(kStringKey));
1656 w->SetProperty(kStringKey, "squeamish");
1657 EXPECT_EQ(std::string("squeamish"), w->GetProperty(kStringKey));
1658 w->SetProperty(kStringKey, "ossifrage");
1659 EXPECT_EQ(std::string("ossifrage"), w->GetProperty(kStringKey));
1661 w->SetNativeWindowProperty(native_prop_key, &*w);
1662 EXPECT_EQ(&*w, w->GetNativeWindowProperty(native_prop_key));
1663 w->SetNativeWindowProperty(native_prop_key, NULL);
1664 EXPECT_EQ(NULL, w->GetNativeWindowProperty(native_prop_key));
1666 // ClearProperty should restore the default value.
1667 w->ClearProperty(kIntKey);
1668 EXPECT_EQ(-2, w->GetProperty(kIntKey));
1669 w->ClearProperty(kStringKey);
1670 EXPECT_EQ(std::string("squeamish"), w->GetProperty(kStringKey));
1673 TEST_F(WindowTest, OwnedProperty) {
1674 scoped_ptr<Window> w(CreateTestWindowWithId(0, root_window()));
1675 EXPECT_EQ(NULL, w->GetProperty(kOwnedKey));
1676 TestProperty* p1 = new TestProperty();
1677 w->SetProperty(kOwnedKey, p1);
1678 EXPECT_EQ(p1, w->GetProperty(kOwnedKey));
1679 EXPECT_EQ(NULL, TestProperty::last_deleted());
1681 TestProperty* p2 = new TestProperty();
1682 w->SetProperty(kOwnedKey, p2);
1683 EXPECT_EQ(p2, w->GetProperty(kOwnedKey));
1684 EXPECT_EQ(p1, TestProperty::last_deleted());
1686 w->ClearProperty(kOwnedKey);
1687 EXPECT_EQ(NULL, w->GetProperty(kOwnedKey));
1688 EXPECT_EQ(p2, TestProperty::last_deleted());
1690 TestProperty* p3 = new TestProperty();
1691 w->SetProperty(kOwnedKey, p3);
1692 EXPECT_EQ(p3, w->GetProperty(kOwnedKey));
1693 EXPECT_EQ(p2, TestProperty::last_deleted());
1694 w.reset();
1695 EXPECT_EQ(p3, TestProperty::last_deleted());
1698 TEST_F(WindowTest, SetBoundsInternalShouldCheckTargetBounds) {
1699 // We cannot short-circuit animations in this test.
1700 ui::ScopedAnimationDurationScaleMode test_duration_mode(
1701 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
1703 scoped_ptr<Window> w1(
1704 CreateTestWindowWithBounds(gfx::Rect(0, 0, 100, 100), root_window()));
1706 EXPECT_TRUE(w1->layer());
1707 w1->layer()->GetAnimator()->set_disable_timer_for_test(true);
1708 ui::LayerAnimator* animator = w1->layer()->GetAnimator();
1710 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1711 EXPECT_EQ("0,0 100x100", w1->layer()->GetTargetBounds().ToString());
1713 // Animate to a different position.
1715 ui::ScopedLayerAnimationSettings settings(w1->layer()->GetAnimator());
1716 w1->SetBounds(gfx::Rect(100, 100, 100, 100));
1719 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1720 EXPECT_EQ("100,100 100x100", w1->layer()->GetTargetBounds().ToString());
1722 // Animate back to the first position. The animation hasn't started yet, so
1723 // the current bounds are still (0, 0, 100, 100), but the target bounds are
1724 // (100, 100, 100, 100). If we step the animator ahead, we should find that
1725 // we're at (0, 0, 100, 100). That is, the second animation should be applied.
1727 ui::ScopedLayerAnimationSettings settings(w1->layer()->GetAnimator());
1728 w1->SetBounds(gfx::Rect(0, 0, 100, 100));
1731 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1732 EXPECT_EQ("0,0 100x100", w1->layer()->GetTargetBounds().ToString());
1734 // Confirm that the target bounds are reached.
1735 base::TimeTicks start_time =
1736 w1->layer()->GetAnimator()->last_step_time();
1738 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1740 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1744 typedef std::pair<const void*, intptr_t> PropertyChangeInfo;
1746 class WindowObserverTest : public WindowTest,
1747 public WindowObserver {
1748 public:
1749 struct VisibilityInfo {
1750 bool window_visible;
1751 bool visible_param;
1752 int changed_count;
1755 WindowObserverTest()
1756 : added_count_(0),
1757 removed_count_(0),
1758 destroyed_count_(0),
1759 old_property_value_(-3) {
1762 ~WindowObserverTest() override {}
1764 const VisibilityInfo* GetVisibilityInfo() const {
1765 return visibility_info_.get();
1768 void ResetVisibilityInfo() {
1769 visibility_info_.reset();
1772 // Returns a description of the WindowObserver methods that have been invoked.
1773 std::string WindowObserverCountStateAndClear() {
1774 std::string result(
1775 base::StringPrintf("added=%d removed=%d",
1776 added_count_, removed_count_));
1777 added_count_ = removed_count_ = 0;
1778 return result;
1781 int DestroyedCountAndClear() {
1782 int result = destroyed_count_;
1783 destroyed_count_ = 0;
1784 return result;
1787 // Return a tuple of the arguments passed in OnPropertyChanged callback.
1788 PropertyChangeInfo PropertyChangeInfoAndClear() {
1789 PropertyChangeInfo result(property_key_, old_property_value_);
1790 property_key_ = NULL;
1791 old_property_value_ = -3;
1792 return result;
1795 std::string TransformNotificationsAndClear() {
1796 std::string result;
1797 for (std::vector<std::pair<int, int> >::iterator it =
1798 transform_notifications_.begin();
1799 it != transform_notifications_.end();
1800 ++it) {
1801 base::StringAppendF(&result, "(%d,%d)", it->first, it->second);
1803 transform_notifications_.clear();
1804 return result;
1807 private:
1808 void OnWindowAdded(Window* new_window) override { added_count_++; }
1810 void OnWillRemoveWindow(Window* window) override { removed_count_++; }
1812 void OnWindowVisibilityChanged(Window* window, bool visible) override {
1813 if (!visibility_info_) {
1814 visibility_info_.reset(new VisibilityInfo);
1815 visibility_info_->changed_count = 0;
1817 visibility_info_->window_visible = window->IsVisible();
1818 visibility_info_->visible_param = visible;
1819 visibility_info_->changed_count++;
1822 void OnWindowDestroyed(Window* window) override {
1823 EXPECT_FALSE(window->parent());
1824 destroyed_count_++;
1827 void OnWindowPropertyChanged(Window* window,
1828 const void* key,
1829 intptr_t old) override {
1830 property_key_ = key;
1831 old_property_value_ = old;
1834 void OnAncestorWindowTransformed(Window* source, Window* window) override {
1835 transform_notifications_.push_back(
1836 std::make_pair(source->id(), window->id()));
1839 int added_count_;
1840 int removed_count_;
1841 int destroyed_count_;
1842 scoped_ptr<VisibilityInfo> visibility_info_;
1843 const void* property_key_;
1844 intptr_t old_property_value_;
1845 std::vector<std::pair<int, int> > transform_notifications_;
1847 DISALLOW_COPY_AND_ASSIGN(WindowObserverTest);
1850 // Various assertions for WindowObserver.
1851 TEST_F(WindowObserverTest, WindowObserver) {
1852 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1853 w1->AddObserver(this);
1855 // Create a new window as a child of w1, our observer should be notified.
1856 scoped_ptr<Window> w2(CreateTestWindowWithId(2, w1.get()));
1857 EXPECT_EQ("added=1 removed=0", WindowObserverCountStateAndClear());
1859 // Delete w2, which should result in the remove notification.
1860 w2.reset();
1861 EXPECT_EQ("added=0 removed=1", WindowObserverCountStateAndClear());
1863 // Create a window that isn't parented to w1, we shouldn't get any
1864 // notification.
1865 scoped_ptr<Window> w3(CreateTestWindowWithId(3, root_window()));
1866 EXPECT_EQ("added=0 removed=0", WindowObserverCountStateAndClear());
1868 // Similarly destroying w3 shouldn't notify us either.
1869 w3.reset();
1870 EXPECT_EQ("added=0 removed=0", WindowObserverCountStateAndClear());
1871 w1->RemoveObserver(this);
1874 // Test if OnWindowVisibilityChanged is invoked with expected
1875 // parameters.
1876 TEST_F(WindowObserverTest, WindowVisibility) {
1877 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1878 scoped_ptr<Window> w2(CreateTestWindowWithId(1, w1.get()));
1879 w2->AddObserver(this);
1881 // Hide should make the window invisible and the passed visible
1882 // parameter is false.
1883 w2->Hide();
1884 EXPECT_TRUE(GetVisibilityInfo());
1885 EXPECT_TRUE(GetVisibilityInfo());
1886 if (!GetVisibilityInfo())
1887 return;
1888 EXPECT_FALSE(GetVisibilityInfo()->window_visible);
1889 EXPECT_FALSE(GetVisibilityInfo()->visible_param);
1890 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1892 // If parent isn't visible, showing window won't make the window visible, but
1893 // passed visible value must be true.
1894 w1->Hide();
1895 ResetVisibilityInfo();
1896 EXPECT_TRUE(!GetVisibilityInfo());
1897 w2->Show();
1898 EXPECT_TRUE(GetVisibilityInfo());
1899 if (!GetVisibilityInfo())
1900 return;
1901 EXPECT_FALSE(GetVisibilityInfo()->window_visible);
1902 EXPECT_TRUE(GetVisibilityInfo()->visible_param);
1903 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1905 // If parent is visible, showing window will make the window
1906 // visible and the passed visible value is true.
1907 w1->Show();
1908 w2->Hide();
1909 ResetVisibilityInfo();
1910 w2->Show();
1911 EXPECT_TRUE(GetVisibilityInfo());
1912 if (!GetVisibilityInfo())
1913 return;
1914 EXPECT_TRUE(GetVisibilityInfo()->window_visible);
1915 EXPECT_TRUE(GetVisibilityInfo()->visible_param);
1916 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1918 // Verify that the OnWindowVisibilityChanged only once
1919 // per visibility change.
1920 w2->Hide();
1921 EXPECT_EQ(2, GetVisibilityInfo()->changed_count);
1923 w2->Hide();
1924 EXPECT_EQ(2, GetVisibilityInfo()->changed_count);
1927 // Test if OnWindowDestroyed is invoked as expected.
1928 TEST_F(WindowObserverTest, WindowDestroyed) {
1929 // Delete a window should fire a destroyed notification.
1930 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1931 w1->AddObserver(this);
1932 w1.reset();
1933 EXPECT_EQ(1, DestroyedCountAndClear());
1935 // Observe on child and delete parent window should fire a notification.
1936 scoped_ptr<Window> parent(CreateTestWindowWithId(1, root_window()));
1937 Window* child = CreateTestWindowWithId(1, parent.get()); // owned by parent
1938 child->AddObserver(this);
1939 parent.reset();
1940 EXPECT_EQ(1, DestroyedCountAndClear());
1943 TEST_F(WindowObserverTest, PropertyChanged) {
1944 // Setting property should fire a property change notification.
1945 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1946 w1->AddObserver(this);
1948 static const WindowProperty<int> prop = {-2};
1949 static const char native_prop_key[] = "fnord";
1951 w1->SetProperty(&prop, 1);
1952 EXPECT_EQ(PropertyChangeInfo(&prop, -2), PropertyChangeInfoAndClear());
1953 w1->SetProperty(&prop, -2);
1954 EXPECT_EQ(PropertyChangeInfo(&prop, 1), PropertyChangeInfoAndClear());
1955 w1->SetProperty(&prop, 3);
1956 EXPECT_EQ(PropertyChangeInfo(&prop, -2), PropertyChangeInfoAndClear());
1957 w1->ClearProperty(&prop);
1958 EXPECT_EQ(PropertyChangeInfo(&prop, 3), PropertyChangeInfoAndClear());
1960 w1->SetNativeWindowProperty(native_prop_key, &*w1);
1961 EXPECT_EQ(PropertyChangeInfo(native_prop_key, 0),
1962 PropertyChangeInfoAndClear());
1963 w1->SetNativeWindowProperty(native_prop_key, NULL);
1964 EXPECT_EQ(PropertyChangeInfo(native_prop_key,
1965 reinterpret_cast<intptr_t>(&*w1)),
1966 PropertyChangeInfoAndClear());
1968 // Sanity check to see if |PropertyChangeInfoAndClear| really clears.
1969 EXPECT_EQ(PropertyChangeInfo(
1970 reinterpret_cast<const void*>(NULL), -3), PropertyChangeInfoAndClear());
1973 TEST_F(WindowObserverTest, AncestorTransformed) {
1974 // Create following window hierarchy:
1975 // root_window
1976 // +-- w1
1977 // +-- w2
1978 // +-- w3
1979 // +-- w4
1980 // Then, apply a transform to |w1| and ensure all its descendants are
1981 // notified.
1982 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1983 w1->AddObserver(this);
1984 scoped_ptr<Window> w2(CreateTestWindowWithId(2, w1.get()));
1985 w2->AddObserver(this);
1986 scoped_ptr<Window> w3(CreateTestWindowWithId(3, w1.get()));
1987 w3->AddObserver(this);
1988 scoped_ptr<Window> w4(CreateTestWindowWithId(4, w3.get()));
1989 w4->AddObserver(this);
1991 EXPECT_EQ(std::string(), TransformNotificationsAndClear());
1993 gfx::Transform transform;
1994 transform.Translate(10, 10);
1995 w1->SetTransform(transform);
1997 EXPECT_EQ("(1,1)(1,2)(1,3)(1,4)", TransformNotificationsAndClear());
2000 TEST_F(WindowTest, AcquireLayer) {
2001 scoped_ptr<Window> window1(CreateTestWindowWithId(1, root_window()));
2002 scoped_ptr<Window> window2(CreateTestWindowWithId(2, root_window()));
2003 ui::Layer* parent = window1->parent()->layer();
2004 EXPECT_EQ(2U, parent->children().size());
2006 WindowTestApi window1_test_api(window1.get());
2007 WindowTestApi window2_test_api(window2.get());
2009 EXPECT_TRUE(window1_test_api.OwnsLayer());
2010 EXPECT_TRUE(window2_test_api.OwnsLayer());
2012 // After acquisition, window1 should not own its layer, but it should still
2013 // be available to the window.
2014 scoped_ptr<ui::Layer> window1_layer(window1->AcquireLayer());
2015 EXPECT_FALSE(window1_test_api.OwnsLayer());
2016 EXPECT_TRUE(window1_layer.get() == window1->layer());
2018 // The acquired layer's owner should be set NULL and re-acquring
2019 // should return NULL.
2020 EXPECT_FALSE(window1_layer->owner());
2021 scoped_ptr<ui::Layer> window1_layer_reacquired(window1->AcquireLayer());
2022 EXPECT_FALSE(window1_layer_reacquired.get());
2024 // Upon destruction, window1's layer should still be valid, and in the layer
2025 // hierarchy, but window2's should be gone, and no longer in the hierarchy.
2026 window1.reset();
2027 window2.reset();
2029 // This should be set by the window's destructor.
2030 EXPECT_TRUE(window1_layer->delegate() == NULL);
2031 EXPECT_EQ(1U, parent->children().size());
2034 // Make sure that properties which should persist from the old layer to the new
2035 // layer actually do.
2036 TEST_F(WindowTest, RecreateLayer) {
2037 // Set properties to non default values.
2038 gfx::Rect window_bounds(100, 100);
2039 Window w(new ColorTestWindowDelegate(SK_ColorWHITE));
2040 w.set_id(1);
2041 w.Init(ui::LAYER_SOLID_COLOR);
2042 w.SetBounds(window_bounds);
2044 ui::Layer* layer = w.layer();
2045 layer->SetVisible(false);
2046 layer->SetMasksToBounds(true);
2048 ui::Layer child_layer;
2049 layer->Add(&child_layer);
2051 scoped_ptr<ui::Layer> old_layer(w.RecreateLayer());
2052 layer = w.layer();
2053 EXPECT_EQ(ui::LAYER_SOLID_COLOR, layer->type());
2054 EXPECT_FALSE(layer->visible());
2055 EXPECT_EQ(1u, layer->children().size());
2056 EXPECT_TRUE(layer->GetMasksToBounds());
2057 EXPECT_EQ("0,0 100x100", w.bounds().ToString());
2058 EXPECT_EQ("0,0 100x100", layer->bounds().ToString());
2061 // Verify that RecreateLayer() stacks the old layer above the newly creatd
2062 // layer.
2063 TEST_F(WindowTest, RecreateLayerZOrder) {
2064 scoped_ptr<Window> w(
2065 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(0, 0, 100, 100),
2066 root_window()));
2067 scoped_ptr<ui::Layer> old_layer(w->RecreateLayer());
2069 const std::vector<ui::Layer*>& child_layers =
2070 root_window()->layer()->children();
2071 ASSERT_EQ(2u, child_layers.size());
2072 EXPECT_EQ(w->layer(), child_layers[0]);
2073 EXPECT_EQ(old_layer.get(), child_layers[1]);
2076 // Ensure that acquiring a layer then recreating a layer does not crash
2077 // and that RecreateLayer returns null.
2078 TEST_F(WindowTest, AcquireThenRecreateLayer) {
2079 scoped_ptr<Window> w(
2080 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(0, 0, 100, 100),
2081 root_window()));
2082 scoped_ptr<ui::Layer> acquired_layer(w->AcquireLayer());
2083 scoped_ptr<ui::Layer> doubly_acquired_layer(w->RecreateLayer());
2084 EXPECT_EQ(NULL, doubly_acquired_layer.get());
2086 // Destroy window before layer gets destroyed.
2087 w.reset();
2090 class TestVisibilityClient : public client::VisibilityClient {
2091 public:
2092 explicit TestVisibilityClient(Window* root_window)
2093 : ignore_visibility_changes_(false) {
2094 client::SetVisibilityClient(root_window, this);
2096 ~TestVisibilityClient() override {}
2098 void set_ignore_visibility_changes(bool ignore_visibility_changes) {
2099 ignore_visibility_changes_ = ignore_visibility_changes;
2102 // Overridden from client::VisibilityClient:
2103 void UpdateLayerVisibility(aura::Window* window, bool visible) override {
2104 if (!ignore_visibility_changes_)
2105 window->layer()->SetVisible(visible);
2108 private:
2109 bool ignore_visibility_changes_;
2110 DISALLOW_COPY_AND_ASSIGN(TestVisibilityClient);
2113 TEST_F(WindowTest, VisibilityClientIsVisible) {
2114 TestVisibilityClient client(root_window());
2116 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
2117 EXPECT_TRUE(window->IsVisible());
2118 EXPECT_TRUE(window->layer()->visible());
2120 window->Hide();
2121 EXPECT_FALSE(window->IsVisible());
2122 EXPECT_FALSE(window->layer()->visible());
2123 window->Show();
2125 client.set_ignore_visibility_changes(true);
2126 window->Hide();
2127 EXPECT_FALSE(window->IsVisible());
2128 EXPECT_TRUE(window->layer()->visible());
2131 // Tests the mouse events seen by WindowDelegates in a Window hierarchy when
2132 // changing the properties of a leaf Window.
2133 TEST_F(WindowTest, MouseEventsOnLeafWindowChange) {
2134 gfx::Size size = host()->GetBounds().size();
2136 ui::test::EventGenerator generator(root_window());
2137 generator.MoveMouseTo(50, 50);
2139 EventCountDelegate d1;
2140 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(&d1, 1,
2141 gfx::Rect(0, 0, 100, 100), root_window()));
2142 RunAllPendingInMessageLoop();
2143 // The format of result is "Enter/Move/Leave".
2144 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2146 // Add new window |w11| on top of |w1| which contains the cursor.
2147 EventCountDelegate d11;
2148 scoped_ptr<Window> w11(CreateTestWindowWithDelegate(
2149 &d11, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2150 RunAllPendingInMessageLoop();
2151 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2152 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2154 // Resize |w11| so that it does not contain the cursor.
2155 w11->SetBounds(gfx::Rect(0, 0, 10, 10));
2156 RunAllPendingInMessageLoop();
2157 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2158 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2160 // Resize |w11| so that it does contain the cursor.
2161 w11->SetBounds(gfx::Rect(0, 0, 60, 60));
2162 RunAllPendingInMessageLoop();
2163 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2164 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2166 // Detach |w11| from |w1|.
2167 w1->RemoveChild(w11.get());
2168 RunAllPendingInMessageLoop();
2169 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2170 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2172 // Re-attach |w11| to |w1|.
2173 w1->AddChild(w11.get());
2174 RunAllPendingInMessageLoop();
2175 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2176 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2178 // Hide |w11|.
2179 w11->Hide();
2180 RunAllPendingInMessageLoop();
2181 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2182 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2184 // Show |w11|.
2185 w11->Show();
2186 RunAllPendingInMessageLoop();
2187 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2188 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2190 // Translate |w11| so that it does not contain the mouse cursor.
2191 gfx::Transform transform;
2192 transform.Translate(100, 100);
2193 w11->SetTransform(transform);
2194 RunAllPendingInMessageLoop();
2195 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2196 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2198 // Clear the transform on |w11| so that it does contain the cursor.
2199 w11->SetTransform(gfx::Transform());
2200 RunAllPendingInMessageLoop();
2201 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2202 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2204 // Close |w11|. Note that since |w11| is being destroyed, its delegate should
2205 // not see any further events.
2206 w11.reset();
2207 RunAllPendingInMessageLoop();
2208 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2209 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2211 // Move the mouse outside the bounds of the root window. Since the mouse
2212 // cursor is no longer within their bounds, the delegates of the child
2213 // windows should not see any mouse events.
2214 generator.MoveMouseTo(-10, -10);
2215 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2216 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2218 // Add |w11|.
2219 w11.reset(CreateTestWindowWithDelegate(
2220 &d11, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2221 RunAllPendingInMessageLoop();
2222 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2223 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2225 // Close |w11|.
2226 w11.reset();
2227 RunAllPendingInMessageLoop();
2228 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2229 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2232 // Tests the mouse events seen by WindowDelegates in a Window hierarchy when
2233 // deleting a non-leaf Window.
2234 TEST_F(WindowTest, MouseEventsOnNonLeafWindowDelete) {
2235 gfx::Size size = host()->GetBounds().size();
2237 ui::test::EventGenerator generator(root_window());
2238 generator.MoveMouseTo(50, 50);
2240 EventCountDelegate d1;
2241 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(&d1, 1,
2242 gfx::Rect(0, 0, 100, 100), root_window()));
2243 RunAllPendingInMessageLoop();
2244 // The format of result is "Enter/Move/Leave".
2245 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2247 // Add new window |w2| on top of |w1| which contains the cursor.
2248 EventCountDelegate d2;
2249 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
2250 &d2, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2251 RunAllPendingInMessageLoop();
2252 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2253 EXPECT_EQ("1 1 0", d2.GetMouseMotionCountsAndReset());
2255 // Add new window on top of |w2| which contains the cursor.
2256 EventCountDelegate d3;
2257 CreateTestWindowWithDelegate(
2258 &d3, 1, gfx::Rect(0, 0, 100, 100), w2.get());
2259 RunAllPendingInMessageLoop();
2260 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2261 EXPECT_EQ("0 0 1", d2.GetMouseMotionCountsAndReset());
2262 EXPECT_EQ("1 1 0", d3.GetMouseMotionCountsAndReset());
2264 // Delete |w2|, which will also delete its owned child window. Since |w2| and
2265 // its child are in the process of being destroyed, their delegates should
2266 // not see any further events.
2267 w2.reset();
2268 RunAllPendingInMessageLoop();
2269 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2270 EXPECT_EQ("0 0 0", d2.GetMouseMotionCountsAndReset());
2271 EXPECT_EQ("0 0 0", d3.GetMouseMotionCountsAndReset());
2274 class RootWindowAttachmentObserver : public WindowObserver {
2275 public:
2276 RootWindowAttachmentObserver() : added_count_(0), removed_count_(0) {}
2277 ~RootWindowAttachmentObserver() override {}
2279 int added_count() const { return added_count_; }
2280 int removed_count() const { return removed_count_; }
2282 void Clear() {
2283 added_count_ = 0;
2284 removed_count_ = 0;
2287 // Overridden from WindowObserver:
2288 void OnWindowAddedToRootWindow(Window* window) override { ++added_count_; }
2289 void OnWindowRemovingFromRootWindow(Window* window,
2290 Window* new_root) override {
2291 ++removed_count_;
2294 private:
2295 int added_count_;
2296 int removed_count_;
2298 DISALLOW_COPY_AND_ASSIGN(RootWindowAttachmentObserver);
2301 TEST_F(WindowTest, RootWindowAttachment) {
2302 RootWindowAttachmentObserver observer;
2304 // Test a direct add/remove from the RootWindow.
2305 scoped_ptr<Window> w1(new Window(NULL));
2306 w1->Init(ui::LAYER_NOT_DRAWN);
2307 w1->AddObserver(&observer);
2309 ParentWindow(w1.get());
2310 EXPECT_EQ(1, observer.added_count());
2311 EXPECT_EQ(0, observer.removed_count());
2313 w1.reset();
2314 EXPECT_EQ(1, observer.added_count());
2315 EXPECT_EQ(1, observer.removed_count());
2317 observer.Clear();
2319 // Test an indirect add/remove from the RootWindow.
2320 w1.reset(new Window(NULL));
2321 w1->Init(ui::LAYER_NOT_DRAWN);
2322 Window* w11 = new Window(NULL);
2323 w11->Init(ui::LAYER_NOT_DRAWN);
2324 w11->AddObserver(&observer);
2325 w1->AddChild(w11);
2326 EXPECT_EQ(0, observer.added_count());
2327 EXPECT_EQ(0, observer.removed_count());
2329 ParentWindow(w1.get());
2330 EXPECT_EQ(1, observer.added_count());
2331 EXPECT_EQ(0, observer.removed_count());
2333 w1.reset(); // Deletes w11.
2334 w11 = NULL;
2335 EXPECT_EQ(1, observer.added_count());
2336 EXPECT_EQ(1, observer.removed_count());
2338 observer.Clear();
2340 // Test an indirect add/remove with nested observers.
2341 w1.reset(new Window(NULL));
2342 w1->Init(ui::LAYER_NOT_DRAWN);
2343 w11 = new Window(NULL);
2344 w11->Init(ui::LAYER_NOT_DRAWN);
2345 w11->AddObserver(&observer);
2346 w1->AddChild(w11);
2347 Window* w111 = new Window(NULL);
2348 w111->Init(ui::LAYER_NOT_DRAWN);
2349 w111->AddObserver(&observer);
2350 w11->AddChild(w111);
2352 EXPECT_EQ(0, observer.added_count());
2353 EXPECT_EQ(0, observer.removed_count());
2355 ParentWindow(w1.get());
2356 EXPECT_EQ(2, observer.added_count());
2357 EXPECT_EQ(0, observer.removed_count());
2359 w1.reset(); // Deletes w11 and w111.
2360 w11 = NULL;
2361 w111 = NULL;
2362 EXPECT_EQ(2, observer.added_count());
2363 EXPECT_EQ(2, observer.removed_count());
2366 class BoundsChangedWindowObserver : public WindowObserver {
2367 public:
2368 BoundsChangedWindowObserver() : root_set_(false) {}
2370 void OnWindowBoundsChanged(Window* window,
2371 const gfx::Rect& old_bounds,
2372 const gfx::Rect& new_bounds) override {
2373 root_set_ = window->GetRootWindow() != NULL;
2376 bool root_set() const { return root_set_; }
2378 private:
2379 bool root_set_;
2381 DISALLOW_COPY_AND_ASSIGN(BoundsChangedWindowObserver);
2384 TEST_F(WindowTest, RootWindowSetWhenReparenting) {
2385 Window parent1(NULL);
2386 parent1.Init(ui::LAYER_NOT_DRAWN);
2387 Window parent2(NULL);
2388 parent2.Init(ui::LAYER_NOT_DRAWN);
2389 ParentWindow(&parent1);
2390 ParentWindow(&parent2);
2391 parent1.SetBounds(gfx::Rect(10, 10, 300, 300));
2392 parent2.SetBounds(gfx::Rect(20, 20, 300, 300));
2394 BoundsChangedWindowObserver observer;
2395 Window child(NULL);
2396 child.Init(ui::LAYER_NOT_DRAWN);
2397 child.SetBounds(gfx::Rect(5, 5, 100, 100));
2398 parent1.AddChild(&child);
2400 // We need animations to start in order to observe the bounds changes.
2401 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2402 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2403 ui::ScopedLayerAnimationSettings settings1(child.layer()->GetAnimator());
2404 settings1.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100));
2405 gfx::Rect new_bounds(gfx::Rect(35, 35, 50, 50));
2406 child.SetBounds(new_bounds);
2408 child.AddObserver(&observer);
2410 // Reparenting the |child| will cause it to get moved. During this move
2411 // the window should still have root window set.
2412 parent2.AddChild(&child);
2413 EXPECT_TRUE(observer.root_set());
2415 // Animations should stop and the bounds should be as set before the |child|
2416 // got reparented.
2417 EXPECT_EQ(new_bounds.ToString(), child.GetTargetBounds().ToString());
2418 EXPECT_EQ(new_bounds.ToString(), child.bounds().ToString());
2419 EXPECT_EQ("55,55 50x50", child.GetBoundsInRootWindow().ToString());
2422 TEST_F(WindowTest, OwnedByParentFalse) {
2423 // By default, a window is owned by its parent. If this is set to false, the
2424 // window will not be destroyed when its parent is.
2426 scoped_ptr<Window> w1(new Window(NULL));
2427 w1->Init(ui::LAYER_NOT_DRAWN);
2428 scoped_ptr<Window> w2(new Window(NULL));
2429 w2->set_owned_by_parent(false);
2430 w2->Init(ui::LAYER_NOT_DRAWN);
2431 w1->AddChild(w2.get());
2433 w1.reset();
2435 // We should be able to deref w2 still, but its parent should now be NULL.
2436 EXPECT_EQ(NULL, w2->parent());
2439 namespace {
2441 // Used By DeleteWindowFromOnWindowDestroyed. Destroys a Window from
2442 // OnWindowDestroyed().
2443 class OwningWindowDelegate : public TestWindowDelegate {
2444 public:
2445 OwningWindowDelegate() {}
2447 void SetOwnedWindow(Window* window) {
2448 owned_window_.reset(window);
2451 void OnWindowDestroyed(Window* window) override { owned_window_.reset(NULL); }
2453 private:
2454 scoped_ptr<Window> owned_window_;
2456 DISALLOW_COPY_AND_ASSIGN(OwningWindowDelegate);
2459 } // namespace
2461 // Creates a window with two child windows. When the first child window is
2462 // destroyed (WindowDelegate::OnWindowDestroyed) it deletes the second child.
2463 // This synthesizes BrowserView and the status bubble. Both are children of the
2464 // same parent and destroying BrowserView triggers it destroying the status
2465 // bubble.
2466 TEST_F(WindowTest, DeleteWindowFromOnWindowDestroyed) {
2467 scoped_ptr<Window> parent(new Window(NULL));
2468 parent->Init(ui::LAYER_NOT_DRAWN);
2469 OwningWindowDelegate delegate;
2470 Window* c1 = new Window(&delegate);
2471 c1->Init(ui::LAYER_NOT_DRAWN);
2472 parent->AddChild(c1);
2473 Window* c2 = new Window(NULL);
2474 c2->Init(ui::LAYER_NOT_DRAWN);
2475 parent->AddChild(c2);
2476 delegate.SetOwnedWindow(c2);
2477 parent.reset();
2480 namespace {
2482 // Used by DelegateNotifiedAsBoundsChange to verify OnBoundsChanged() is
2483 // invoked.
2484 class BoundsChangeDelegate : public TestWindowDelegate {
2485 public:
2486 BoundsChangeDelegate() : bounds_changed_(false) {}
2488 void clear_bounds_changed() { bounds_changed_ = false; }
2489 bool bounds_changed() const {
2490 return bounds_changed_;
2493 // Window
2494 void OnBoundsChanged(const gfx::Rect& old_bounds,
2495 const gfx::Rect& new_bounds) override {
2496 bounds_changed_ = true;
2499 private:
2500 // Was OnBoundsChanged() invoked?
2501 bool bounds_changed_;
2503 DISALLOW_COPY_AND_ASSIGN(BoundsChangeDelegate);
2506 } // namespace
2508 // Verifies the delegate is notified when the actual bounds of the layer
2509 // change.
2510 TEST_F(WindowTest, DelegateNotifiedAsBoundsChange) {
2511 BoundsChangeDelegate delegate;
2513 // We cannot short-circuit animations in this test.
2514 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2515 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2517 scoped_ptr<Window> window(
2518 CreateTestWindowWithDelegate(&delegate, 1,
2519 gfx::Rect(0, 0, 100, 100), root_window()));
2520 window->layer()->GetAnimator()->set_disable_timer_for_test(true);
2522 delegate.clear_bounds_changed();
2524 // Animate to a different position.
2526 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
2527 window->SetBounds(gfx::Rect(100, 100, 100, 100));
2530 // Bounds shouldn't immediately have changed.
2531 EXPECT_EQ("0,0 100x100", window->bounds().ToString());
2532 EXPECT_FALSE(delegate.bounds_changed());
2534 // Animate to the end, which should notify of the change.
2535 base::TimeTicks start_time =
2536 window->layer()->GetAnimator()->last_step_time();
2537 ui::LayerAnimator* animator = window->layer()->GetAnimator();
2538 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2539 EXPECT_TRUE(delegate.bounds_changed());
2540 EXPECT_NE("0,0 100x100", window->bounds().ToString());
2543 // Verifies the delegate is notified when the actual bounds of the layer
2544 // change even when the window is not the layer's delegate
2545 TEST_F(WindowTest, DelegateNotifiedAsBoundsChangeInHiddenLayer) {
2546 BoundsChangeDelegate delegate;
2548 // We cannot short-circuit animations in this test.
2549 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2550 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2552 scoped_ptr<Window> window(
2553 CreateTestWindowWithDelegate(&delegate, 1,
2554 gfx::Rect(0, 0, 100, 100), root_window()));
2555 window->layer()->GetAnimator()->set_disable_timer_for_test(true);
2557 delegate.clear_bounds_changed();
2559 // Suppress paint on the window since it is hidden (should reset the layer's
2560 // delegate to NULL)
2561 window->SuppressPaint();
2562 EXPECT_EQ(NULL, window->layer()->delegate());
2564 // Animate to a different position.
2566 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
2567 window->SetBounds(gfx::Rect(100, 100, 110, 100));
2570 // Layer delegate is NULL but we should still get bounds changed notification.
2571 EXPECT_EQ("100,100 110x100", window->GetTargetBounds().ToString());
2572 EXPECT_TRUE(delegate.bounds_changed());
2574 delegate.clear_bounds_changed();
2576 // Animate to the end: will *not* notify of the change since we are hidden.
2577 base::TimeTicks start_time =
2578 window->layer()->GetAnimator()->last_step_time();
2579 ui::LayerAnimator* animator = window->layer()->GetAnimator();
2580 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2582 // No bounds changed notification at the end of animation since layer
2583 // delegate is NULL.
2584 EXPECT_FALSE(delegate.bounds_changed());
2585 EXPECT_NE("0,0 100x100", window->layer()->bounds().ToString());
2588 namespace {
2590 // Used by AddChildNotifications to track notification counts.
2591 class AddChildNotificationsObserver : public WindowObserver {
2592 public:
2593 AddChildNotificationsObserver() : added_count_(0), removed_count_(0) {}
2595 std::string CountStringAndReset() {
2596 std::string result = base::IntToString(added_count_) + " " +
2597 base::IntToString(removed_count_);
2598 added_count_ = removed_count_ = 0;
2599 return result;
2602 // WindowObserver overrides:
2603 void OnWindowAddedToRootWindow(Window* window) override { added_count_++; }
2604 void OnWindowRemovingFromRootWindow(Window* window,
2605 Window* new_root) override {
2606 removed_count_++;
2609 private:
2610 int added_count_;
2611 int removed_count_;
2613 DISALLOW_COPY_AND_ASSIGN(AddChildNotificationsObserver);
2616 } // namespace
2618 // Assertions around when root window notifications are sent.
2619 TEST_F(WindowTest, AddChildNotifications) {
2620 AddChildNotificationsObserver observer;
2621 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
2622 scoped_ptr<Window> w2(CreateTestWindowWithId(1, root_window()));
2623 w2->AddObserver(&observer);
2624 w2->Focus();
2625 EXPECT_TRUE(w2->HasFocus());
2627 // Move |w2| to be a child of |w1|.
2628 w1->AddChild(w2.get());
2629 // Sine we moved in the same root, observer shouldn't be notified.
2630 EXPECT_EQ("0 0", observer.CountStringAndReset());
2631 // |w2| should still have focus after moving.
2632 EXPECT_TRUE(w2->HasFocus());
2635 // Tests that a delegate that destroys itself when the window is destroyed does
2636 // not break.
2637 TEST_F(WindowTest, DelegateDestroysSelfOnWindowDestroy) {
2638 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(
2639 new DestroyWindowDelegate(),
2641 gfx::Rect(10, 20, 30, 40),
2642 root_window()));
2645 class HierarchyObserver : public WindowObserver {
2646 public:
2647 explicit HierarchyObserver(Window* target) : target_(target) {
2648 target_->AddObserver(this);
2650 ~HierarchyObserver() override { target_->RemoveObserver(this); }
2652 void ValidateState(
2653 int index,
2654 const WindowObserver::HierarchyChangeParams& params) const {
2655 ParamsMatch(params_[index], params);
2658 void Reset() {
2659 params_.clear();
2662 private:
2663 // Overridden from WindowObserver:
2664 void OnWindowHierarchyChanging(const HierarchyChangeParams& params) override {
2665 params_.push_back(params);
2667 void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override {
2668 params_.push_back(params);
2671 void ParamsMatch(const WindowObserver::HierarchyChangeParams& p1,
2672 const WindowObserver::HierarchyChangeParams& p2) const {
2673 EXPECT_EQ(p1.phase, p2.phase);
2674 EXPECT_EQ(p1.target, p2.target);
2675 EXPECT_EQ(p1.new_parent, p2.new_parent);
2676 EXPECT_EQ(p1.old_parent, p2.old_parent);
2677 EXPECT_EQ(p1.receiver, p2.receiver);
2680 Window* target_;
2681 std::vector<WindowObserver::HierarchyChangeParams> params_;
2683 DISALLOW_COPY_AND_ASSIGN(HierarchyObserver);
2686 // Tests hierarchy change notifications.
2687 TEST_F(WindowTest, OnWindowHierarchyChange) {
2689 // Simple add & remove.
2690 HierarchyObserver oroot(root_window());
2692 scoped_ptr<Window> w1(CreateTestWindowWithId(1, NULL));
2693 HierarchyObserver o1(w1.get());
2695 // Add.
2696 root_window()->AddChild(w1.get());
2698 WindowObserver::HierarchyChangeParams params;
2699 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2700 params.target = w1.get();
2701 params.old_parent = NULL;
2702 params.new_parent = root_window();
2703 params.receiver = w1.get();
2704 o1.ValidateState(0, params);
2706 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2707 params.receiver = w1.get();
2708 o1.ValidateState(1, params);
2710 params.receiver = root_window();
2711 oroot.ValidateState(0, params);
2713 // Remove.
2714 o1.Reset();
2715 oroot.Reset();
2717 root_window()->RemoveChild(w1.get());
2719 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2720 params.old_parent = root_window();
2721 params.new_parent = NULL;
2722 params.receiver = w1.get();
2724 o1.ValidateState(0, params);
2726 params.receiver = root_window();
2727 oroot.ValidateState(0, params);
2729 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2730 params.receiver = w1.get();
2731 o1.ValidateState(1, params);
2735 // Add & remove of hierarchy. Tests notification order per documentation in
2736 // WindowObserver.
2737 HierarchyObserver o(root_window());
2738 scoped_ptr<Window> w1(CreateTestWindowWithId(1, NULL));
2739 Window* w11 = CreateTestWindowWithId(11, w1.get());
2740 w1->AddObserver(&o);
2741 w11->AddObserver(&o);
2743 // Add.
2744 root_window()->AddChild(w1.get());
2746 // Dispatched to target first.
2747 int index = 0;
2748 WindowObserver::HierarchyChangeParams params;
2749 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2750 params.target = w1.get();
2751 params.old_parent = NULL;
2752 params.new_parent = root_window();
2753 params.receiver = w1.get();
2754 o.ValidateState(index++, params);
2756 // Dispatched to target's children.
2757 params.receiver = w11;
2758 o.ValidateState(index++, params);
2760 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2762 // Now process the "changed" phase.
2763 params.receiver = w1.get();
2764 o.ValidateState(index++, params);
2765 params.receiver = w11;
2766 o.ValidateState(index++, params);
2767 params.receiver = root_window();
2768 o.ValidateState(index++, params);
2770 // Remove.
2771 root_window()->RemoveChild(w1.get());
2772 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2773 params.old_parent = root_window();
2774 params.new_parent = NULL;
2775 params.receiver = w1.get();
2776 o.ValidateState(index++, params);
2777 params.receiver = w11;
2778 o.ValidateState(index++, params);
2779 params.receiver = root_window();
2780 o.ValidateState(index++, params);
2781 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2782 params.receiver = w1.get();
2783 o.ValidateState(index++, params);
2784 params.receiver = w11;
2785 o.ValidateState(index++, params);
2787 w1.reset();
2791 // Reparent. Tests notification order per documentation in WindowObserver.
2792 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
2793 Window* w11 = CreateTestWindowWithId(11, w1.get());
2794 Window* w111 = CreateTestWindowWithId(111, w11);
2795 scoped_ptr<Window> w2(CreateTestWindowWithId(2, root_window()));
2797 HierarchyObserver o(root_window());
2798 w1->AddObserver(&o);
2799 w11->AddObserver(&o);
2800 w111->AddObserver(&o);
2801 w2->AddObserver(&o);
2803 w2->AddChild(w11);
2805 // Dispatched to target first.
2806 int index = 0;
2807 WindowObserver::HierarchyChangeParams params;
2808 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2809 params.target = w11;
2810 params.old_parent = w1.get();
2811 params.new_parent = w2.get();
2812 params.receiver = w11;
2813 o.ValidateState(index++, params);
2815 // Then to target's children.
2816 params.receiver = w111;
2817 o.ValidateState(index++, params);
2819 // Then to target's old parent chain.
2820 params.receiver = w1.get();
2821 o.ValidateState(index++, params);
2822 params.receiver = root_window();
2823 o.ValidateState(index++, params);
2825 // "Changed" phase.
2826 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2827 params.receiver = w11;
2828 o.ValidateState(index++, params);
2829 params.receiver = w111;
2830 o.ValidateState(index++, params);
2831 params.receiver = w2.get();
2832 o.ValidateState(index++, params);
2833 params.receiver = root_window();
2834 o.ValidateState(index++, params);
2836 w1.reset();
2837 w2.reset();
2841 namespace {
2843 class TestLayerAnimationObserver : public ui::LayerAnimationObserver {
2844 public:
2845 TestLayerAnimationObserver()
2846 : animation_completed_(false),
2847 animation_aborted_(false) {}
2848 ~TestLayerAnimationObserver() override {}
2850 bool animation_completed() const { return animation_completed_; }
2851 bool animation_aborted() const { return animation_aborted_; }
2853 void Reset() {
2854 animation_completed_ = false;
2855 animation_aborted_ = false;
2858 private:
2859 // ui::LayerAnimationObserver:
2860 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
2861 animation_completed_ = true;
2864 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {
2865 animation_aborted_ = true;
2868 void OnLayerAnimationScheduled(
2869 ui::LayerAnimationSequence* sequence) override {}
2871 bool animation_completed_;
2872 bool animation_aborted_;
2874 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationObserver);
2877 } // namespace
2879 TEST_F(WindowTest, WindowDestroyCompletesAnimations) {
2880 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2881 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2882 scoped_refptr<ui::LayerAnimator> animator =
2883 ui::LayerAnimator::CreateImplicitAnimator();
2884 TestLayerAnimationObserver observer;
2885 animator->AddObserver(&observer);
2886 // Make sure destroying a Window completes the animation.
2888 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
2889 window->layer()->SetAnimator(animator.get());
2891 gfx::Transform transform;
2892 transform.Scale(0.5f, 0.5f);
2893 window->SetTransform(transform);
2895 EXPECT_TRUE(animator->is_animating());
2896 EXPECT_FALSE(observer.animation_completed());
2898 EXPECT_TRUE(animator.get());
2899 EXPECT_FALSE(animator->is_animating());
2900 EXPECT_TRUE(observer.animation_completed());
2901 EXPECT_FALSE(observer.animation_aborted());
2902 animator->RemoveObserver(&observer);
2903 observer.Reset();
2905 animator = ui::LayerAnimator::CreateImplicitAnimator();
2906 animator->AddObserver(&observer);
2907 ui::Layer layer;
2908 layer.SetAnimator(animator.get());
2910 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
2911 window->layer()->Add(&layer);
2913 gfx::Transform transform;
2914 transform.Scale(0.5f, 0.5f);
2915 layer.SetTransform(transform);
2917 EXPECT_TRUE(animator->is_animating());
2918 EXPECT_FALSE(observer.animation_completed());
2921 EXPECT_TRUE(animator.get());
2922 EXPECT_FALSE(animator->is_animating());
2923 EXPECT_TRUE(observer.animation_completed());
2924 EXPECT_FALSE(observer.animation_aborted());
2925 animator->RemoveObserver(&observer);
2928 } // namespace test
2929 } // namespace aura