Fix OOP <webview> resize and autosize.
[chromium-blink-merge.git] / ui / aura / window_unittest.cc
blob5fc481665ab88f0ba85791a8fa2510d7eeac1690
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* last_deleted = TestProperty::last_deleted();
1677 TestProperty* p1 = new TestProperty();
1678 w->SetProperty(kOwnedKey, p1);
1679 EXPECT_EQ(p1, w->GetProperty(kOwnedKey));
1680 EXPECT_EQ(last_deleted, TestProperty::last_deleted());
1682 TestProperty* p2 = new TestProperty();
1683 w->SetProperty(kOwnedKey, p2);
1684 EXPECT_EQ(p2, w->GetProperty(kOwnedKey));
1685 EXPECT_EQ(p1, TestProperty::last_deleted());
1687 w->ClearProperty(kOwnedKey);
1688 EXPECT_EQ(NULL, w->GetProperty(kOwnedKey));
1689 EXPECT_EQ(p2, TestProperty::last_deleted());
1691 TestProperty* p3 = new TestProperty();
1692 w->SetProperty(kOwnedKey, p3);
1693 EXPECT_EQ(p3, w->GetProperty(kOwnedKey));
1694 EXPECT_EQ(p2, TestProperty::last_deleted());
1695 w.reset();
1696 EXPECT_EQ(p3, TestProperty::last_deleted());
1699 TEST_F(WindowTest, SetBoundsInternalShouldCheckTargetBounds) {
1700 // We cannot short-circuit animations in this test.
1701 ui::ScopedAnimationDurationScaleMode test_duration_mode(
1702 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
1704 scoped_ptr<Window> w1(
1705 CreateTestWindowWithBounds(gfx::Rect(0, 0, 100, 100), root_window()));
1707 EXPECT_TRUE(w1->layer());
1708 w1->layer()->GetAnimator()->set_disable_timer_for_test(true);
1709 ui::LayerAnimator* animator = w1->layer()->GetAnimator();
1711 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1712 EXPECT_EQ("0,0 100x100", w1->layer()->GetTargetBounds().ToString());
1714 // Animate to a different position.
1716 ui::ScopedLayerAnimationSettings settings(w1->layer()->GetAnimator());
1717 w1->SetBounds(gfx::Rect(100, 100, 100, 100));
1720 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1721 EXPECT_EQ("100,100 100x100", w1->layer()->GetTargetBounds().ToString());
1723 // Animate back to the first position. The animation hasn't started yet, so
1724 // the current bounds are still (0, 0, 100, 100), but the target bounds are
1725 // (100, 100, 100, 100). If we step the animator ahead, we should find that
1726 // we're at (0, 0, 100, 100). That is, the second animation should be applied.
1728 ui::ScopedLayerAnimationSettings settings(w1->layer()->GetAnimator());
1729 w1->SetBounds(gfx::Rect(0, 0, 100, 100));
1732 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1733 EXPECT_EQ("0,0 100x100", w1->layer()->GetTargetBounds().ToString());
1735 // Confirm that the target bounds are reached.
1736 base::TimeTicks start_time =
1737 w1->layer()->GetAnimator()->last_step_time();
1739 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1741 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1745 typedef std::pair<const void*, intptr_t> PropertyChangeInfo;
1747 class WindowObserverTest : public WindowTest,
1748 public WindowObserver {
1749 public:
1750 struct VisibilityInfo {
1751 bool window_visible;
1752 bool visible_param;
1753 int changed_count;
1756 WindowObserverTest()
1757 : added_count_(0),
1758 removed_count_(0),
1759 destroyed_count_(0),
1760 old_property_value_(-3) {
1763 ~WindowObserverTest() override {}
1765 const VisibilityInfo* GetVisibilityInfo() const {
1766 return visibility_info_.get();
1769 void ResetVisibilityInfo() {
1770 visibility_info_.reset();
1773 // Returns a description of the WindowObserver methods that have been invoked.
1774 std::string WindowObserverCountStateAndClear() {
1775 std::string result(
1776 base::StringPrintf("added=%d removed=%d",
1777 added_count_, removed_count_));
1778 added_count_ = removed_count_ = 0;
1779 return result;
1782 int DestroyedCountAndClear() {
1783 int result = destroyed_count_;
1784 destroyed_count_ = 0;
1785 return result;
1788 // Return a tuple of the arguments passed in OnPropertyChanged callback.
1789 PropertyChangeInfo PropertyChangeInfoAndClear() {
1790 PropertyChangeInfo result(property_key_, old_property_value_);
1791 property_key_ = NULL;
1792 old_property_value_ = -3;
1793 return result;
1796 std::string TransformNotificationsAndClear() {
1797 std::string result;
1798 for (std::vector<std::pair<int, int> >::iterator it =
1799 transform_notifications_.begin();
1800 it != transform_notifications_.end();
1801 ++it) {
1802 base::StringAppendF(&result, "(%d,%d)", it->first, it->second);
1804 transform_notifications_.clear();
1805 return result;
1808 private:
1809 void OnWindowAdded(Window* new_window) override { added_count_++; }
1811 void OnWillRemoveWindow(Window* window) override { removed_count_++; }
1813 void OnWindowVisibilityChanged(Window* window, bool visible) override {
1814 if (!visibility_info_) {
1815 visibility_info_.reset(new VisibilityInfo);
1816 visibility_info_->changed_count = 0;
1818 visibility_info_->window_visible = window->IsVisible();
1819 visibility_info_->visible_param = visible;
1820 visibility_info_->changed_count++;
1823 void OnWindowDestroyed(Window* window) override {
1824 EXPECT_FALSE(window->parent());
1825 destroyed_count_++;
1828 void OnWindowPropertyChanged(Window* window,
1829 const void* key,
1830 intptr_t old) override {
1831 property_key_ = key;
1832 old_property_value_ = old;
1835 void OnAncestorWindowTransformed(Window* source, Window* window) override {
1836 transform_notifications_.push_back(
1837 std::make_pair(source->id(), window->id()));
1840 int added_count_;
1841 int removed_count_;
1842 int destroyed_count_;
1843 scoped_ptr<VisibilityInfo> visibility_info_;
1844 const void* property_key_;
1845 intptr_t old_property_value_;
1846 std::vector<std::pair<int, int> > transform_notifications_;
1848 DISALLOW_COPY_AND_ASSIGN(WindowObserverTest);
1851 // Various assertions for WindowObserver.
1852 TEST_F(WindowObserverTest, WindowObserver) {
1853 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1854 w1->AddObserver(this);
1856 // Create a new window as a child of w1, our observer should be notified.
1857 scoped_ptr<Window> w2(CreateTestWindowWithId(2, w1.get()));
1858 EXPECT_EQ("added=1 removed=0", WindowObserverCountStateAndClear());
1860 // Delete w2, which should result in the remove notification.
1861 w2.reset();
1862 EXPECT_EQ("added=0 removed=1", WindowObserverCountStateAndClear());
1864 // Create a window that isn't parented to w1, we shouldn't get any
1865 // notification.
1866 scoped_ptr<Window> w3(CreateTestWindowWithId(3, root_window()));
1867 EXPECT_EQ("added=0 removed=0", WindowObserverCountStateAndClear());
1869 // Similarly destroying w3 shouldn't notify us either.
1870 w3.reset();
1871 EXPECT_EQ("added=0 removed=0", WindowObserverCountStateAndClear());
1872 w1->RemoveObserver(this);
1875 // Test if OnWindowVisibilityChanged is invoked with expected
1876 // parameters.
1877 TEST_F(WindowObserverTest, WindowVisibility) {
1878 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1879 scoped_ptr<Window> w2(CreateTestWindowWithId(1, w1.get()));
1880 w2->AddObserver(this);
1882 // Hide should make the window invisible and the passed visible
1883 // parameter is false.
1884 w2->Hide();
1885 EXPECT_TRUE(GetVisibilityInfo());
1886 EXPECT_TRUE(GetVisibilityInfo());
1887 if (!GetVisibilityInfo())
1888 return;
1889 EXPECT_FALSE(GetVisibilityInfo()->window_visible);
1890 EXPECT_FALSE(GetVisibilityInfo()->visible_param);
1891 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1893 // If parent isn't visible, showing window won't make the window visible, but
1894 // passed visible value must be true.
1895 w1->Hide();
1896 ResetVisibilityInfo();
1897 EXPECT_TRUE(!GetVisibilityInfo());
1898 w2->Show();
1899 EXPECT_TRUE(GetVisibilityInfo());
1900 if (!GetVisibilityInfo())
1901 return;
1902 EXPECT_FALSE(GetVisibilityInfo()->window_visible);
1903 EXPECT_TRUE(GetVisibilityInfo()->visible_param);
1904 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1906 // If parent is visible, showing window will make the window
1907 // visible and the passed visible value is true.
1908 w1->Show();
1909 w2->Hide();
1910 ResetVisibilityInfo();
1911 w2->Show();
1912 EXPECT_TRUE(GetVisibilityInfo());
1913 if (!GetVisibilityInfo())
1914 return;
1915 EXPECT_TRUE(GetVisibilityInfo()->window_visible);
1916 EXPECT_TRUE(GetVisibilityInfo()->visible_param);
1917 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1919 // Verify that the OnWindowVisibilityChanged only once
1920 // per visibility change.
1921 w2->Hide();
1922 EXPECT_EQ(2, GetVisibilityInfo()->changed_count);
1924 w2->Hide();
1925 EXPECT_EQ(2, GetVisibilityInfo()->changed_count);
1928 // Test if OnWindowDestroyed is invoked as expected.
1929 TEST_F(WindowObserverTest, WindowDestroyed) {
1930 // Delete a window should fire a destroyed notification.
1931 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1932 w1->AddObserver(this);
1933 w1.reset();
1934 EXPECT_EQ(1, DestroyedCountAndClear());
1936 // Observe on child and delete parent window should fire a notification.
1937 scoped_ptr<Window> parent(CreateTestWindowWithId(1, root_window()));
1938 Window* child = CreateTestWindowWithId(1, parent.get()); // owned by parent
1939 child->AddObserver(this);
1940 parent.reset();
1941 EXPECT_EQ(1, DestroyedCountAndClear());
1944 TEST_F(WindowObserverTest, PropertyChanged) {
1945 // Setting property should fire a property change notification.
1946 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1947 w1->AddObserver(this);
1949 static const WindowProperty<int> prop = {-2};
1950 static const char native_prop_key[] = "fnord";
1952 w1->SetProperty(&prop, 1);
1953 EXPECT_EQ(PropertyChangeInfo(&prop, -2), PropertyChangeInfoAndClear());
1954 w1->SetProperty(&prop, -2);
1955 EXPECT_EQ(PropertyChangeInfo(&prop, 1), PropertyChangeInfoAndClear());
1956 w1->SetProperty(&prop, 3);
1957 EXPECT_EQ(PropertyChangeInfo(&prop, -2), PropertyChangeInfoAndClear());
1958 w1->ClearProperty(&prop);
1959 EXPECT_EQ(PropertyChangeInfo(&prop, 3), PropertyChangeInfoAndClear());
1961 w1->SetNativeWindowProperty(native_prop_key, &*w1);
1962 EXPECT_EQ(PropertyChangeInfo(native_prop_key, 0),
1963 PropertyChangeInfoAndClear());
1964 w1->SetNativeWindowProperty(native_prop_key, NULL);
1965 EXPECT_EQ(PropertyChangeInfo(native_prop_key,
1966 reinterpret_cast<intptr_t>(&*w1)),
1967 PropertyChangeInfoAndClear());
1969 // Sanity check to see if |PropertyChangeInfoAndClear| really clears.
1970 EXPECT_EQ(PropertyChangeInfo(
1971 reinterpret_cast<const void*>(NULL), -3), PropertyChangeInfoAndClear());
1974 TEST_F(WindowObserverTest, AncestorTransformed) {
1975 // Create following window hierarchy:
1976 // root_window
1977 // +-- w1
1978 // +-- w2
1979 // +-- w3
1980 // +-- w4
1981 // Then, apply a transform to |w1| and ensure all its descendants are
1982 // notified.
1983 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1984 w1->AddObserver(this);
1985 scoped_ptr<Window> w2(CreateTestWindowWithId(2, w1.get()));
1986 w2->AddObserver(this);
1987 scoped_ptr<Window> w3(CreateTestWindowWithId(3, w1.get()));
1988 w3->AddObserver(this);
1989 scoped_ptr<Window> w4(CreateTestWindowWithId(4, w3.get()));
1990 w4->AddObserver(this);
1992 EXPECT_EQ(std::string(), TransformNotificationsAndClear());
1994 gfx::Transform transform;
1995 transform.Translate(10, 10);
1996 w1->SetTransform(transform);
1998 EXPECT_EQ("(1,1)(1,2)(1,3)(1,4)", TransformNotificationsAndClear());
2001 TEST_F(WindowTest, AcquireLayer) {
2002 scoped_ptr<Window> window1(CreateTestWindowWithId(1, root_window()));
2003 scoped_ptr<Window> window2(CreateTestWindowWithId(2, root_window()));
2004 ui::Layer* parent = window1->parent()->layer();
2005 EXPECT_EQ(2U, parent->children().size());
2007 WindowTestApi window1_test_api(window1.get());
2008 WindowTestApi window2_test_api(window2.get());
2010 EXPECT_TRUE(window1_test_api.OwnsLayer());
2011 EXPECT_TRUE(window2_test_api.OwnsLayer());
2013 // After acquisition, window1 should not own its layer, but it should still
2014 // be available to the window.
2015 scoped_ptr<ui::Layer> window1_layer(window1->AcquireLayer());
2016 EXPECT_FALSE(window1_test_api.OwnsLayer());
2017 EXPECT_TRUE(window1_layer.get() == window1->layer());
2019 // The acquired layer's owner should be set NULL and re-acquring
2020 // should return NULL.
2021 EXPECT_FALSE(window1_layer->owner());
2022 scoped_ptr<ui::Layer> window1_layer_reacquired(window1->AcquireLayer());
2023 EXPECT_FALSE(window1_layer_reacquired.get());
2025 // Upon destruction, window1's layer should still be valid, and in the layer
2026 // hierarchy, but window2's should be gone, and no longer in the hierarchy.
2027 window1.reset();
2028 window2.reset();
2030 // This should be set by the window's destructor.
2031 EXPECT_TRUE(window1_layer->delegate() == NULL);
2032 EXPECT_EQ(1U, parent->children().size());
2035 // Make sure that properties which should persist from the old layer to the new
2036 // layer actually do.
2037 TEST_F(WindowTest, RecreateLayer) {
2038 // Set properties to non default values.
2039 gfx::Rect window_bounds(100, 100);
2040 Window w(new ColorTestWindowDelegate(SK_ColorWHITE));
2041 w.set_id(1);
2042 w.Init(ui::LAYER_SOLID_COLOR);
2043 w.SetBounds(window_bounds);
2045 ui::Layer* layer = w.layer();
2046 layer->SetVisible(false);
2047 layer->SetMasksToBounds(true);
2049 ui::Layer child_layer;
2050 layer->Add(&child_layer);
2052 scoped_ptr<ui::Layer> old_layer(w.RecreateLayer());
2053 layer = w.layer();
2054 EXPECT_EQ(ui::LAYER_SOLID_COLOR, layer->type());
2055 EXPECT_FALSE(layer->visible());
2056 EXPECT_EQ(1u, layer->children().size());
2057 EXPECT_TRUE(layer->GetMasksToBounds());
2058 EXPECT_EQ("0,0 100x100", w.bounds().ToString());
2059 EXPECT_EQ("0,0 100x100", layer->bounds().ToString());
2062 // Verify that RecreateLayer() stacks the old layer above the newly creatd
2063 // layer.
2064 TEST_F(WindowTest, RecreateLayerZOrder) {
2065 scoped_ptr<Window> w(
2066 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(0, 0, 100, 100),
2067 root_window()));
2068 scoped_ptr<ui::Layer> old_layer(w->RecreateLayer());
2070 const std::vector<ui::Layer*>& child_layers =
2071 root_window()->layer()->children();
2072 ASSERT_EQ(2u, child_layers.size());
2073 EXPECT_EQ(w->layer(), child_layers[0]);
2074 EXPECT_EQ(old_layer.get(), child_layers[1]);
2077 // Ensure that acquiring a layer then recreating a layer does not crash
2078 // and that RecreateLayer returns null.
2079 TEST_F(WindowTest, AcquireThenRecreateLayer) {
2080 scoped_ptr<Window> w(
2081 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(0, 0, 100, 100),
2082 root_window()));
2083 scoped_ptr<ui::Layer> acquired_layer(w->AcquireLayer());
2084 scoped_ptr<ui::Layer> doubly_acquired_layer(w->RecreateLayer());
2085 EXPECT_EQ(NULL, doubly_acquired_layer.get());
2087 // Destroy window before layer gets destroyed.
2088 w.reset();
2091 class TestVisibilityClient : public client::VisibilityClient {
2092 public:
2093 explicit TestVisibilityClient(Window* root_window)
2094 : ignore_visibility_changes_(false) {
2095 client::SetVisibilityClient(root_window, this);
2097 ~TestVisibilityClient() override {}
2099 void set_ignore_visibility_changes(bool ignore_visibility_changes) {
2100 ignore_visibility_changes_ = ignore_visibility_changes;
2103 // Overridden from client::VisibilityClient:
2104 void UpdateLayerVisibility(aura::Window* window, bool visible) override {
2105 if (!ignore_visibility_changes_)
2106 window->layer()->SetVisible(visible);
2109 private:
2110 bool ignore_visibility_changes_;
2111 DISALLOW_COPY_AND_ASSIGN(TestVisibilityClient);
2114 TEST_F(WindowTest, VisibilityClientIsVisible) {
2115 TestVisibilityClient client(root_window());
2117 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
2118 EXPECT_TRUE(window->IsVisible());
2119 EXPECT_TRUE(window->layer()->visible());
2121 window->Hide();
2122 EXPECT_FALSE(window->IsVisible());
2123 EXPECT_FALSE(window->layer()->visible());
2124 window->Show();
2126 client.set_ignore_visibility_changes(true);
2127 window->Hide();
2128 EXPECT_FALSE(window->IsVisible());
2129 EXPECT_TRUE(window->layer()->visible());
2132 // Tests the mouse events seen by WindowDelegates in a Window hierarchy when
2133 // changing the properties of a leaf Window.
2134 TEST_F(WindowTest, MouseEventsOnLeafWindowChange) {
2135 gfx::Size size = host()->GetBounds().size();
2137 ui::test::EventGenerator generator(root_window());
2138 generator.MoveMouseTo(50, 50);
2140 EventCountDelegate d1;
2141 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(&d1, 1,
2142 gfx::Rect(0, 0, 100, 100), root_window()));
2143 RunAllPendingInMessageLoop();
2144 // The format of result is "Enter/Move/Leave".
2145 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2147 // Add new window |w11| on top of |w1| which contains the cursor.
2148 EventCountDelegate d11;
2149 scoped_ptr<Window> w11(CreateTestWindowWithDelegate(
2150 &d11, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2151 RunAllPendingInMessageLoop();
2152 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2153 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2155 // Resize |w11| so that it does not contain the cursor.
2156 w11->SetBounds(gfx::Rect(0, 0, 10, 10));
2157 RunAllPendingInMessageLoop();
2158 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2159 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2161 // Resize |w11| so that it does contain the cursor.
2162 w11->SetBounds(gfx::Rect(0, 0, 60, 60));
2163 RunAllPendingInMessageLoop();
2164 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2165 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2167 // Detach |w11| from |w1|.
2168 w1->RemoveChild(w11.get());
2169 RunAllPendingInMessageLoop();
2170 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2171 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2173 // Re-attach |w11| to |w1|.
2174 w1->AddChild(w11.get());
2175 RunAllPendingInMessageLoop();
2176 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2177 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2179 // Hide |w11|.
2180 w11->Hide();
2181 RunAllPendingInMessageLoop();
2182 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2183 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2185 // Show |w11|.
2186 w11->Show();
2187 RunAllPendingInMessageLoop();
2188 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2189 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2191 // Translate |w11| so that it does not contain the mouse cursor.
2192 gfx::Transform transform;
2193 transform.Translate(100, 100);
2194 w11->SetTransform(transform);
2195 RunAllPendingInMessageLoop();
2196 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2197 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2199 // Clear the transform on |w11| so that it does contain the cursor.
2200 w11->SetTransform(gfx::Transform());
2201 RunAllPendingInMessageLoop();
2202 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2203 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2205 // Close |w11|. Note that since |w11| is being destroyed, its delegate should
2206 // not see any further events.
2207 w11.reset();
2208 RunAllPendingInMessageLoop();
2209 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2210 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2212 // Move the mouse outside the bounds of the root window. Since the mouse
2213 // cursor is no longer within their bounds, the delegates of the child
2214 // windows should not see any mouse events.
2215 generator.MoveMouseTo(-10, -10);
2216 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2217 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2219 // Add |w11|.
2220 w11.reset(CreateTestWindowWithDelegate(
2221 &d11, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2222 RunAllPendingInMessageLoop();
2223 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2224 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2226 // Close |w11|.
2227 w11.reset();
2228 RunAllPendingInMessageLoop();
2229 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2230 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2233 // Tests the mouse events seen by WindowDelegates in a Window hierarchy when
2234 // deleting a non-leaf Window.
2235 TEST_F(WindowTest, MouseEventsOnNonLeafWindowDelete) {
2236 gfx::Size size = host()->GetBounds().size();
2238 ui::test::EventGenerator generator(root_window());
2239 generator.MoveMouseTo(50, 50);
2241 EventCountDelegate d1;
2242 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(&d1, 1,
2243 gfx::Rect(0, 0, 100, 100), root_window()));
2244 RunAllPendingInMessageLoop();
2245 // The format of result is "Enter/Move/Leave".
2246 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2248 // Add new window |w2| on top of |w1| which contains the cursor.
2249 EventCountDelegate d2;
2250 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
2251 &d2, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2252 RunAllPendingInMessageLoop();
2253 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2254 EXPECT_EQ("1 1 0", d2.GetMouseMotionCountsAndReset());
2256 // Add new window on top of |w2| which contains the cursor.
2257 EventCountDelegate d3;
2258 CreateTestWindowWithDelegate(
2259 &d3, 1, gfx::Rect(0, 0, 100, 100), w2.get());
2260 RunAllPendingInMessageLoop();
2261 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2262 EXPECT_EQ("0 0 1", d2.GetMouseMotionCountsAndReset());
2263 EXPECT_EQ("1 1 0", d3.GetMouseMotionCountsAndReset());
2265 // Delete |w2|, which will also delete its owned child window. Since |w2| and
2266 // its child are in the process of being destroyed, their delegates should
2267 // not see any further events.
2268 w2.reset();
2269 RunAllPendingInMessageLoop();
2270 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2271 EXPECT_EQ("0 0 0", d2.GetMouseMotionCountsAndReset());
2272 EXPECT_EQ("0 0 0", d3.GetMouseMotionCountsAndReset());
2275 class RootWindowAttachmentObserver : public WindowObserver {
2276 public:
2277 RootWindowAttachmentObserver() : added_count_(0), removed_count_(0) {}
2278 ~RootWindowAttachmentObserver() override {}
2280 int added_count() const { return added_count_; }
2281 int removed_count() const { return removed_count_; }
2283 void Clear() {
2284 added_count_ = 0;
2285 removed_count_ = 0;
2288 // Overridden from WindowObserver:
2289 void OnWindowAddedToRootWindow(Window* window) override { ++added_count_; }
2290 void OnWindowRemovingFromRootWindow(Window* window,
2291 Window* new_root) override {
2292 ++removed_count_;
2295 private:
2296 int added_count_;
2297 int removed_count_;
2299 DISALLOW_COPY_AND_ASSIGN(RootWindowAttachmentObserver);
2302 TEST_F(WindowTest, RootWindowAttachment) {
2303 RootWindowAttachmentObserver observer;
2305 // Test a direct add/remove from the RootWindow.
2306 scoped_ptr<Window> w1(new Window(NULL));
2307 w1->Init(ui::LAYER_NOT_DRAWN);
2308 w1->AddObserver(&observer);
2310 ParentWindow(w1.get());
2311 EXPECT_EQ(1, observer.added_count());
2312 EXPECT_EQ(0, observer.removed_count());
2314 w1.reset();
2315 EXPECT_EQ(1, observer.added_count());
2316 EXPECT_EQ(1, observer.removed_count());
2318 observer.Clear();
2320 // Test an indirect add/remove from the RootWindow.
2321 w1.reset(new Window(NULL));
2322 w1->Init(ui::LAYER_NOT_DRAWN);
2323 Window* w11 = new Window(NULL);
2324 w11->Init(ui::LAYER_NOT_DRAWN);
2325 w11->AddObserver(&observer);
2326 w1->AddChild(w11);
2327 EXPECT_EQ(0, observer.added_count());
2328 EXPECT_EQ(0, observer.removed_count());
2330 ParentWindow(w1.get());
2331 EXPECT_EQ(1, observer.added_count());
2332 EXPECT_EQ(0, observer.removed_count());
2334 w1.reset(); // Deletes w11.
2335 w11 = NULL;
2336 EXPECT_EQ(1, observer.added_count());
2337 EXPECT_EQ(1, observer.removed_count());
2339 observer.Clear();
2341 // Test an indirect add/remove with nested observers.
2342 w1.reset(new Window(NULL));
2343 w1->Init(ui::LAYER_NOT_DRAWN);
2344 w11 = new Window(NULL);
2345 w11->Init(ui::LAYER_NOT_DRAWN);
2346 w11->AddObserver(&observer);
2347 w1->AddChild(w11);
2348 Window* w111 = new Window(NULL);
2349 w111->Init(ui::LAYER_NOT_DRAWN);
2350 w111->AddObserver(&observer);
2351 w11->AddChild(w111);
2353 EXPECT_EQ(0, observer.added_count());
2354 EXPECT_EQ(0, observer.removed_count());
2356 ParentWindow(w1.get());
2357 EXPECT_EQ(2, observer.added_count());
2358 EXPECT_EQ(0, observer.removed_count());
2360 w1.reset(); // Deletes w11 and w111.
2361 w11 = NULL;
2362 w111 = NULL;
2363 EXPECT_EQ(2, observer.added_count());
2364 EXPECT_EQ(2, observer.removed_count());
2367 class BoundsChangedWindowObserver : public WindowObserver {
2368 public:
2369 BoundsChangedWindowObserver() : root_set_(false) {}
2371 void OnWindowBoundsChanged(Window* window,
2372 const gfx::Rect& old_bounds,
2373 const gfx::Rect& new_bounds) override {
2374 root_set_ = window->GetRootWindow() != NULL;
2377 bool root_set() const { return root_set_; }
2379 private:
2380 bool root_set_;
2382 DISALLOW_COPY_AND_ASSIGN(BoundsChangedWindowObserver);
2385 TEST_F(WindowTest, RootWindowSetWhenReparenting) {
2386 Window parent1(NULL);
2387 parent1.Init(ui::LAYER_NOT_DRAWN);
2388 Window parent2(NULL);
2389 parent2.Init(ui::LAYER_NOT_DRAWN);
2390 ParentWindow(&parent1);
2391 ParentWindow(&parent2);
2392 parent1.SetBounds(gfx::Rect(10, 10, 300, 300));
2393 parent2.SetBounds(gfx::Rect(20, 20, 300, 300));
2395 BoundsChangedWindowObserver observer;
2396 Window child(NULL);
2397 child.Init(ui::LAYER_NOT_DRAWN);
2398 child.SetBounds(gfx::Rect(5, 5, 100, 100));
2399 parent1.AddChild(&child);
2401 // We need animations to start in order to observe the bounds changes.
2402 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2403 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2404 ui::ScopedLayerAnimationSettings settings1(child.layer()->GetAnimator());
2405 settings1.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100));
2406 gfx::Rect new_bounds(gfx::Rect(35, 35, 50, 50));
2407 child.SetBounds(new_bounds);
2409 child.AddObserver(&observer);
2411 // Reparenting the |child| will cause it to get moved. During this move
2412 // the window should still have root window set.
2413 parent2.AddChild(&child);
2414 EXPECT_TRUE(observer.root_set());
2416 // Animations should stop and the bounds should be as set before the |child|
2417 // got reparented.
2418 EXPECT_EQ(new_bounds.ToString(), child.GetTargetBounds().ToString());
2419 EXPECT_EQ(new_bounds.ToString(), child.bounds().ToString());
2420 EXPECT_EQ("55,55 50x50", child.GetBoundsInRootWindow().ToString());
2423 TEST_F(WindowTest, OwnedByParentFalse) {
2424 // By default, a window is owned by its parent. If this is set to false, the
2425 // window will not be destroyed when its parent is.
2427 scoped_ptr<Window> w1(new Window(NULL));
2428 w1->Init(ui::LAYER_NOT_DRAWN);
2429 scoped_ptr<Window> w2(new Window(NULL));
2430 w2->set_owned_by_parent(false);
2431 w2->Init(ui::LAYER_NOT_DRAWN);
2432 w1->AddChild(w2.get());
2434 w1.reset();
2436 // We should be able to deref w2 still, but its parent should now be NULL.
2437 EXPECT_EQ(NULL, w2->parent());
2440 namespace {
2442 // Used By DeleteWindowFromOnWindowDestroyed. Destroys a Window from
2443 // OnWindowDestroyed().
2444 class OwningWindowDelegate : public TestWindowDelegate {
2445 public:
2446 OwningWindowDelegate() {}
2448 void SetOwnedWindow(Window* window) {
2449 owned_window_.reset(window);
2452 void OnWindowDestroyed(Window* window) override { owned_window_.reset(NULL); }
2454 private:
2455 scoped_ptr<Window> owned_window_;
2457 DISALLOW_COPY_AND_ASSIGN(OwningWindowDelegate);
2460 } // namespace
2462 // Creates a window with two child windows. When the first child window is
2463 // destroyed (WindowDelegate::OnWindowDestroyed) it deletes the second child.
2464 // This synthesizes BrowserView and the status bubble. Both are children of the
2465 // same parent and destroying BrowserView triggers it destroying the status
2466 // bubble.
2467 TEST_F(WindowTest, DeleteWindowFromOnWindowDestroyed) {
2468 scoped_ptr<Window> parent(new Window(NULL));
2469 parent->Init(ui::LAYER_NOT_DRAWN);
2470 OwningWindowDelegate delegate;
2471 Window* c1 = new Window(&delegate);
2472 c1->Init(ui::LAYER_NOT_DRAWN);
2473 parent->AddChild(c1);
2474 Window* c2 = new Window(NULL);
2475 c2->Init(ui::LAYER_NOT_DRAWN);
2476 parent->AddChild(c2);
2477 delegate.SetOwnedWindow(c2);
2478 parent.reset();
2481 namespace {
2483 // Used by DelegateNotifiedAsBoundsChange to verify OnBoundsChanged() is
2484 // invoked.
2485 class BoundsChangeDelegate : public TestWindowDelegate {
2486 public:
2487 BoundsChangeDelegate() : bounds_changed_(false) {}
2489 void clear_bounds_changed() { bounds_changed_ = false; }
2490 bool bounds_changed() const {
2491 return bounds_changed_;
2494 // Window
2495 void OnBoundsChanged(const gfx::Rect& old_bounds,
2496 const gfx::Rect& new_bounds) override {
2497 bounds_changed_ = true;
2500 private:
2501 // Was OnBoundsChanged() invoked?
2502 bool bounds_changed_;
2504 DISALLOW_COPY_AND_ASSIGN(BoundsChangeDelegate);
2507 } // namespace
2509 // Verifies the delegate is notified when the actual bounds of the layer
2510 // change.
2511 TEST_F(WindowTest, DelegateNotifiedAsBoundsChange) {
2512 BoundsChangeDelegate delegate;
2514 // We cannot short-circuit animations in this test.
2515 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2516 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2518 scoped_ptr<Window> window(
2519 CreateTestWindowWithDelegate(&delegate, 1,
2520 gfx::Rect(0, 0, 100, 100), root_window()));
2521 window->layer()->GetAnimator()->set_disable_timer_for_test(true);
2523 delegate.clear_bounds_changed();
2525 // Animate to a different position.
2527 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
2528 window->SetBounds(gfx::Rect(100, 100, 100, 100));
2531 // Bounds shouldn't immediately have changed.
2532 EXPECT_EQ("0,0 100x100", window->bounds().ToString());
2533 EXPECT_FALSE(delegate.bounds_changed());
2535 // Animate to the end, which should notify of the change.
2536 base::TimeTicks start_time =
2537 window->layer()->GetAnimator()->last_step_time();
2538 ui::LayerAnimator* animator = window->layer()->GetAnimator();
2539 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2540 EXPECT_TRUE(delegate.bounds_changed());
2541 EXPECT_NE("0,0 100x100", window->bounds().ToString());
2544 // Verifies the delegate is notified when the actual bounds of the layer
2545 // change even when the window is not the layer's delegate
2546 TEST_F(WindowTest, DelegateNotifiedAsBoundsChangeInHiddenLayer) {
2547 BoundsChangeDelegate delegate;
2549 // We cannot short-circuit animations in this test.
2550 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2551 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2553 scoped_ptr<Window> window(
2554 CreateTestWindowWithDelegate(&delegate, 1,
2555 gfx::Rect(0, 0, 100, 100), root_window()));
2556 window->layer()->GetAnimator()->set_disable_timer_for_test(true);
2558 delegate.clear_bounds_changed();
2560 // Suppress paint on the window since it is hidden (should reset the layer's
2561 // delegate to NULL)
2562 window->SuppressPaint();
2563 EXPECT_EQ(NULL, window->layer()->delegate());
2565 // Animate to a different position.
2567 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
2568 window->SetBounds(gfx::Rect(100, 100, 110, 100));
2571 // Layer delegate is NULL but we should still get bounds changed notification.
2572 EXPECT_EQ("100,100 110x100", window->GetTargetBounds().ToString());
2573 EXPECT_TRUE(delegate.bounds_changed());
2575 delegate.clear_bounds_changed();
2577 // Animate to the end: will *not* notify of the change since we are hidden.
2578 base::TimeTicks start_time =
2579 window->layer()->GetAnimator()->last_step_time();
2580 ui::LayerAnimator* animator = window->layer()->GetAnimator();
2581 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2583 // No bounds changed notification at the end of animation since layer
2584 // delegate is NULL.
2585 EXPECT_FALSE(delegate.bounds_changed());
2586 EXPECT_NE("0,0 100x100", window->layer()->bounds().ToString());
2589 namespace {
2591 // Used by AddChildNotifications to track notification counts.
2592 class AddChildNotificationsObserver : public WindowObserver {
2593 public:
2594 AddChildNotificationsObserver() : added_count_(0), removed_count_(0) {}
2596 std::string CountStringAndReset() {
2597 std::string result = base::IntToString(added_count_) + " " +
2598 base::IntToString(removed_count_);
2599 added_count_ = removed_count_ = 0;
2600 return result;
2603 // WindowObserver overrides:
2604 void OnWindowAddedToRootWindow(Window* window) override { added_count_++; }
2605 void OnWindowRemovingFromRootWindow(Window* window,
2606 Window* new_root) override {
2607 removed_count_++;
2610 private:
2611 int added_count_;
2612 int removed_count_;
2614 DISALLOW_COPY_AND_ASSIGN(AddChildNotificationsObserver);
2617 } // namespace
2619 // Assertions around when root window notifications are sent.
2620 TEST_F(WindowTest, AddChildNotifications) {
2621 AddChildNotificationsObserver observer;
2622 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
2623 scoped_ptr<Window> w2(CreateTestWindowWithId(1, root_window()));
2624 w2->AddObserver(&observer);
2625 w2->Focus();
2626 EXPECT_TRUE(w2->HasFocus());
2628 // Move |w2| to be a child of |w1|.
2629 w1->AddChild(w2.get());
2630 // Sine we moved in the same root, observer shouldn't be notified.
2631 EXPECT_EQ("0 0", observer.CountStringAndReset());
2632 // |w2| should still have focus after moving.
2633 EXPECT_TRUE(w2->HasFocus());
2636 // Tests that a delegate that destroys itself when the window is destroyed does
2637 // not break.
2638 TEST_F(WindowTest, DelegateDestroysSelfOnWindowDestroy) {
2639 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(
2640 new DestroyWindowDelegate(),
2642 gfx::Rect(10, 20, 30, 40),
2643 root_window()));
2646 class HierarchyObserver : public WindowObserver {
2647 public:
2648 explicit HierarchyObserver(Window* target) : target_(target) {
2649 target_->AddObserver(this);
2651 ~HierarchyObserver() override { target_->RemoveObserver(this); }
2653 void ValidateState(
2654 int index,
2655 const WindowObserver::HierarchyChangeParams& params) const {
2656 ParamsMatch(params_[index], params);
2659 void Reset() {
2660 params_.clear();
2663 private:
2664 // Overridden from WindowObserver:
2665 void OnWindowHierarchyChanging(const HierarchyChangeParams& params) override {
2666 params_.push_back(params);
2668 void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override {
2669 params_.push_back(params);
2672 void ParamsMatch(const WindowObserver::HierarchyChangeParams& p1,
2673 const WindowObserver::HierarchyChangeParams& p2) const {
2674 EXPECT_EQ(p1.phase, p2.phase);
2675 EXPECT_EQ(p1.target, p2.target);
2676 EXPECT_EQ(p1.new_parent, p2.new_parent);
2677 EXPECT_EQ(p1.old_parent, p2.old_parent);
2678 EXPECT_EQ(p1.receiver, p2.receiver);
2681 Window* target_;
2682 std::vector<WindowObserver::HierarchyChangeParams> params_;
2684 DISALLOW_COPY_AND_ASSIGN(HierarchyObserver);
2687 // Tests hierarchy change notifications.
2688 TEST_F(WindowTest, OnWindowHierarchyChange) {
2690 // Simple add & remove.
2691 HierarchyObserver oroot(root_window());
2693 scoped_ptr<Window> w1(CreateTestWindowWithId(1, NULL));
2694 HierarchyObserver o1(w1.get());
2696 // Add.
2697 root_window()->AddChild(w1.get());
2699 WindowObserver::HierarchyChangeParams params;
2700 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2701 params.target = w1.get();
2702 params.old_parent = NULL;
2703 params.new_parent = root_window();
2704 params.receiver = w1.get();
2705 o1.ValidateState(0, params);
2707 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2708 params.receiver = w1.get();
2709 o1.ValidateState(1, params);
2711 params.receiver = root_window();
2712 oroot.ValidateState(0, params);
2714 // Remove.
2715 o1.Reset();
2716 oroot.Reset();
2718 root_window()->RemoveChild(w1.get());
2720 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2721 params.old_parent = root_window();
2722 params.new_parent = NULL;
2723 params.receiver = w1.get();
2725 o1.ValidateState(0, params);
2727 params.receiver = root_window();
2728 oroot.ValidateState(0, params);
2730 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2731 params.receiver = w1.get();
2732 o1.ValidateState(1, params);
2736 // Add & remove of hierarchy. Tests notification order per documentation in
2737 // WindowObserver.
2738 HierarchyObserver o(root_window());
2739 scoped_ptr<Window> w1(CreateTestWindowWithId(1, NULL));
2740 Window* w11 = CreateTestWindowWithId(11, w1.get());
2741 w1->AddObserver(&o);
2742 w11->AddObserver(&o);
2744 // Add.
2745 root_window()->AddChild(w1.get());
2747 // Dispatched to target first.
2748 int index = 0;
2749 WindowObserver::HierarchyChangeParams params;
2750 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2751 params.target = w1.get();
2752 params.old_parent = NULL;
2753 params.new_parent = root_window();
2754 params.receiver = w1.get();
2755 o.ValidateState(index++, params);
2757 // Dispatched to target's children.
2758 params.receiver = w11;
2759 o.ValidateState(index++, params);
2761 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2763 // Now process the "changed" phase.
2764 params.receiver = w1.get();
2765 o.ValidateState(index++, params);
2766 params.receiver = w11;
2767 o.ValidateState(index++, params);
2768 params.receiver = root_window();
2769 o.ValidateState(index++, params);
2771 // Remove.
2772 root_window()->RemoveChild(w1.get());
2773 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2774 params.old_parent = root_window();
2775 params.new_parent = NULL;
2776 params.receiver = w1.get();
2777 o.ValidateState(index++, params);
2778 params.receiver = w11;
2779 o.ValidateState(index++, params);
2780 params.receiver = root_window();
2781 o.ValidateState(index++, params);
2782 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2783 params.receiver = w1.get();
2784 o.ValidateState(index++, params);
2785 params.receiver = w11;
2786 o.ValidateState(index++, params);
2788 w1.reset();
2792 // Reparent. Tests notification order per documentation in WindowObserver.
2793 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
2794 Window* w11 = CreateTestWindowWithId(11, w1.get());
2795 Window* w111 = CreateTestWindowWithId(111, w11);
2796 scoped_ptr<Window> w2(CreateTestWindowWithId(2, root_window()));
2798 HierarchyObserver o(root_window());
2799 w1->AddObserver(&o);
2800 w11->AddObserver(&o);
2801 w111->AddObserver(&o);
2802 w2->AddObserver(&o);
2804 w2->AddChild(w11);
2806 // Dispatched to target first.
2807 int index = 0;
2808 WindowObserver::HierarchyChangeParams params;
2809 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2810 params.target = w11;
2811 params.old_parent = w1.get();
2812 params.new_parent = w2.get();
2813 params.receiver = w11;
2814 o.ValidateState(index++, params);
2816 // Then to target's children.
2817 params.receiver = w111;
2818 o.ValidateState(index++, params);
2820 // Then to target's old parent chain.
2821 params.receiver = w1.get();
2822 o.ValidateState(index++, params);
2823 params.receiver = root_window();
2824 o.ValidateState(index++, params);
2826 // "Changed" phase.
2827 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2828 params.receiver = w11;
2829 o.ValidateState(index++, params);
2830 params.receiver = w111;
2831 o.ValidateState(index++, params);
2832 params.receiver = w2.get();
2833 o.ValidateState(index++, params);
2834 params.receiver = root_window();
2835 o.ValidateState(index++, params);
2837 w1.reset();
2838 w2.reset();
2842 namespace {
2844 class TestLayerAnimationObserver : public ui::LayerAnimationObserver {
2845 public:
2846 TestLayerAnimationObserver()
2847 : animation_completed_(false),
2848 animation_aborted_(false) {}
2849 ~TestLayerAnimationObserver() override {}
2851 bool animation_completed() const { return animation_completed_; }
2852 bool animation_aborted() const { return animation_aborted_; }
2854 void Reset() {
2855 animation_completed_ = false;
2856 animation_aborted_ = false;
2859 private:
2860 // ui::LayerAnimationObserver:
2861 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
2862 animation_completed_ = true;
2865 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {
2866 animation_aborted_ = true;
2869 void OnLayerAnimationScheduled(
2870 ui::LayerAnimationSequence* sequence) override {}
2872 bool animation_completed_;
2873 bool animation_aborted_;
2875 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationObserver);
2878 } // namespace
2880 TEST_F(WindowTest, WindowDestroyCompletesAnimations) {
2881 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2882 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2883 scoped_refptr<ui::LayerAnimator> animator =
2884 ui::LayerAnimator::CreateImplicitAnimator();
2885 TestLayerAnimationObserver observer;
2886 animator->AddObserver(&observer);
2887 // Make sure destroying a Window completes the animation.
2889 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
2890 window->layer()->SetAnimator(animator.get());
2892 gfx::Transform transform;
2893 transform.Scale(0.5f, 0.5f);
2894 window->SetTransform(transform);
2896 EXPECT_TRUE(animator->is_animating());
2897 EXPECT_FALSE(observer.animation_completed());
2899 EXPECT_TRUE(animator.get());
2900 EXPECT_FALSE(animator->is_animating());
2901 EXPECT_TRUE(observer.animation_completed());
2902 EXPECT_FALSE(observer.animation_aborted());
2903 animator->RemoveObserver(&observer);
2904 observer.Reset();
2906 animator = ui::LayerAnimator::CreateImplicitAnimator();
2907 animator->AddObserver(&observer);
2908 ui::Layer layer;
2909 layer.SetAnimator(animator.get());
2911 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
2912 window->layer()->Add(&layer);
2914 gfx::Transform transform;
2915 transform.Scale(0.5f, 0.5f);
2916 layer.SetTransform(transform);
2918 EXPECT_TRUE(animator->is_animating());
2919 EXPECT_FALSE(observer.animation_completed());
2922 EXPECT_TRUE(animator.get());
2923 EXPECT_FALSE(animator->is_animating());
2924 EXPECT_TRUE(observer.animation_completed());
2925 EXPECT_FALSE(observer.animation_aborted());
2926 animator->RemoveObserver(&observer);
2929 } // namespace test
2930 } // namespace aura