Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: ui/aura/window_unittest.cc

Issue 851853002: It is time. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Trying to reup because the last upload failed. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/aura/window_tree_host_x11.cc ('k') | ui/base/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "ui/aura/window.h"
6
7 #include <string>
8 #include <utility>
9 #include <vector>
10
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/gestures/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/screen.h"
44 #include "ui/gfx/skia_util.h"
45 #include "ui/gfx/vector2d.h"
46
47 DECLARE_WINDOW_PROPERTY_TYPE(const char*)
48 DECLARE_WINDOW_PROPERTY_TYPE(int)
49
50 namespace aura {
51 namespace test {
52
53 class WindowTest : public AuraTestBase {
54 public:
55 WindowTest() : max_separation_(0) {
56 }
57
58 virtual void SetUp() override {
59 AuraTestBase::SetUp();
60 // TODO: there needs to be an easier way to do this.
61 max_separation_ = ui::GestureConfiguration::
62 max_separation_for_gesture_touches_in_pixels();
63 ui::GestureConfiguration::
64 set_max_separation_for_gesture_touches_in_pixels(0);
65 }
66
67 virtual void TearDown() override {
68 AuraTestBase::TearDown();
69 ui::GestureConfiguration::
70 set_max_separation_for_gesture_touches_in_pixels(max_separation_);
71 }
72
73 private:
74 float max_separation_;
75
76 DISALLOW_COPY_AND_ASSIGN(WindowTest);
77 };
78
79 namespace {
80
81 // Used for verifying destruction methods are invoked.
82 class DestroyTrackingDelegateImpl : public TestWindowDelegate {
83 public:
84 DestroyTrackingDelegateImpl()
85 : destroying_count_(0),
86 destroyed_count_(0),
87 in_destroying_(false) {}
88
89 void clear_destroying_count() { destroying_count_ = 0; }
90 int destroying_count() const { return destroying_count_; }
91
92 void clear_destroyed_count() { destroyed_count_ = 0; }
93 int destroyed_count() const { return destroyed_count_; }
94
95 bool in_destroying() const { return in_destroying_; }
96
97 virtual void OnWindowDestroying(Window* window) override {
98 EXPECT_FALSE(in_destroying_);
99 in_destroying_ = true;
100 destroying_count_++;
101 }
102
103 virtual void OnWindowDestroyed(Window* window) override {
104 EXPECT_TRUE(in_destroying_);
105 in_destroying_ = false;
106 destroyed_count_++;
107 }
108
109 private:
110 int destroying_count_;
111 int destroyed_count_;
112 bool in_destroying_;
113
114 DISALLOW_COPY_AND_ASSIGN(DestroyTrackingDelegateImpl);
115 };
116
117 // Used to verify that when OnWindowDestroying is invoked the parent is also
118 // is in the process of being destroyed.
119 class ChildWindowDelegateImpl : public DestroyTrackingDelegateImpl {
120 public:
121 explicit ChildWindowDelegateImpl(
122 DestroyTrackingDelegateImpl* parent_delegate)
123 : parent_delegate_(parent_delegate) {
124 }
125
126 virtual void OnWindowDestroying(Window* window) override {
127 EXPECT_TRUE(parent_delegate_->in_destroying());
128 DestroyTrackingDelegateImpl::OnWindowDestroying(window);
129 }
130
131 private:
132 DestroyTrackingDelegateImpl* parent_delegate_;
133
134 DISALLOW_COPY_AND_ASSIGN(ChildWindowDelegateImpl);
135 };
136
137 // Used to verify that a Window is removed from its parent when
138 // OnWindowDestroyed is called.
139 class DestroyOrphanDelegate : public TestWindowDelegate {
140 public:
141 DestroyOrphanDelegate() : window_(NULL) {
142 }
143
144 void set_window(Window* window) { window_ = window; }
145
146 virtual void OnWindowDestroyed(Window* window) override {
147 EXPECT_FALSE(window_->parent());
148 }
149
150 private:
151 Window* window_;
152 DISALLOW_COPY_AND_ASSIGN(DestroyOrphanDelegate);
153 };
154
155 // Used in verifying mouse capture.
156 class CaptureWindowDelegateImpl : public TestWindowDelegate {
157 public:
158 CaptureWindowDelegateImpl() {
159 ResetCounts();
160 }
161
162 void ResetCounts() {
163 capture_changed_event_count_ = 0;
164 capture_lost_count_ = 0;
165 mouse_event_count_ = 0;
166 touch_event_count_ = 0;
167 gesture_event_count_ = 0;
168 }
169
170 int capture_changed_event_count() const {
171 return capture_changed_event_count_;
172 }
173 int capture_lost_count() const { return capture_lost_count_; }
174 int mouse_event_count() const { return mouse_event_count_; }
175 int touch_event_count() const { return touch_event_count_; }
176 int gesture_event_count() const { return gesture_event_count_; }
177
178 virtual void OnMouseEvent(ui::MouseEvent* event) override {
179 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED)
180 capture_changed_event_count_++;
181 mouse_event_count_++;
182 }
183 virtual void OnTouchEvent(ui::TouchEvent* event) override {
184 touch_event_count_++;
185 }
186 virtual void OnGestureEvent(ui::GestureEvent* event) override {
187 gesture_event_count_++;
188 }
189 virtual void OnCaptureLost() override {
190 capture_lost_count_++;
191 }
192
193 private:
194 int capture_changed_event_count_;
195 int capture_lost_count_;
196 int mouse_event_count_;
197 int touch_event_count_;
198 int gesture_event_count_;
199
200 DISALLOW_COPY_AND_ASSIGN(CaptureWindowDelegateImpl);
201 };
202
203 // Keeps track of the location of the gesture.
204 class GestureTrackPositionDelegate : public TestWindowDelegate {
205 public:
206 GestureTrackPositionDelegate() {}
207
208 virtual void OnGestureEvent(ui::GestureEvent* event) override {
209 position_ = event->location();
210 event->StopPropagation();
211 }
212
213 const gfx::Point& position() const { return position_; }
214
215 private:
216 gfx::Point position_;
217
218 DISALLOW_COPY_AND_ASSIGN(GestureTrackPositionDelegate);
219 };
220
221 base::TimeDelta getTime() {
222 return ui::EventTimeForNow();
223 }
224
225 class SelfEventHandlingWindowDelegate : public TestWindowDelegate {
226 public:
227 SelfEventHandlingWindowDelegate() {}
228
229 virtual bool ShouldDescendIntoChildForEventHandling(
230 Window* child,
231 const gfx::Point& location) override {
232 return false;
233 }
234
235 private:
236 DISALLOW_COPY_AND_ASSIGN(SelfEventHandlingWindowDelegate);
237 };
238
239 // The delegate deletes itself when the window is being destroyed.
240 class DestroyWindowDelegate : public TestWindowDelegate {
241 public:
242 DestroyWindowDelegate() {}
243
244 private:
245 virtual ~DestroyWindowDelegate() {}
246
247 // Overridden from WindowDelegate.
248 virtual void OnWindowDestroyed(Window* window) override {
249 delete this;
250 }
251
252 DISALLOW_COPY_AND_ASSIGN(DestroyWindowDelegate);
253 };
254
255 } // namespace
256
257 TEST_F(WindowTest, GetChildById) {
258 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
259 scoped_ptr<Window> w11(CreateTestWindowWithId(11, w1.get()));
260 scoped_ptr<Window> w111(CreateTestWindowWithId(111, w11.get()));
261 scoped_ptr<Window> w12(CreateTestWindowWithId(12, w1.get()));
262
263 EXPECT_EQ(NULL, w1->GetChildById(57));
264 EXPECT_EQ(w12.get(), w1->GetChildById(12));
265 EXPECT_EQ(w111.get(), w1->GetChildById(111));
266 }
267
268 // Make sure that Window::Contains correctly handles children, grandchildren,
269 // and not containing NULL or parents.
270 TEST_F(WindowTest, Contains) {
271 Window parent(NULL);
272 parent.Init(aura::WINDOW_LAYER_NOT_DRAWN);
273 Window child1(NULL);
274 child1.Init(aura::WINDOW_LAYER_NOT_DRAWN);
275 Window child2(NULL);
276 child2.Init(aura::WINDOW_LAYER_NOT_DRAWN);
277
278 parent.AddChild(&child1);
279 child1.AddChild(&child2);
280
281 EXPECT_TRUE(parent.Contains(&parent));
282 EXPECT_TRUE(parent.Contains(&child1));
283 EXPECT_TRUE(parent.Contains(&child2));
284
285 EXPECT_FALSE(parent.Contains(NULL));
286 EXPECT_FALSE(child1.Contains(&parent));
287 EXPECT_FALSE(child2.Contains(&child1));
288 }
289
290 TEST_F(WindowTest, ContainsPointInRoot) {
291 scoped_ptr<Window> w(
292 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 5, 5),
293 root_window()));
294 EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(9, 9)));
295 EXPECT_TRUE(w->ContainsPointInRoot(gfx::Point(10, 10)));
296 EXPECT_TRUE(w->ContainsPointInRoot(gfx::Point(14, 14)));
297 EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(15, 15)));
298 EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(20, 20)));
299 }
300
301 TEST_F(WindowTest, ContainsPoint) {
302 scoped_ptr<Window> w(
303 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 5, 5),
304 root_window()));
305 EXPECT_TRUE(w->ContainsPoint(gfx::Point(0, 0)));
306 EXPECT_TRUE(w->ContainsPoint(gfx::Point(4, 4)));
307 EXPECT_FALSE(w->ContainsPoint(gfx::Point(5, 5)));
308 EXPECT_FALSE(w->ContainsPoint(gfx::Point(10, 10)));
309 }
310
311 TEST_F(WindowTest, ConvertPointToWindow) {
312 // Window::ConvertPointToWindow is mostly identical to
313 // Layer::ConvertPointToLayer, except NULL values for |source| are permitted,
314 // in which case the function just returns.
315 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
316 gfx::Point reference_point(100, 100);
317 gfx::Point test_point = reference_point;
318 Window::ConvertPointToTarget(NULL, w1.get(), &test_point);
319 EXPECT_EQ(reference_point, test_point);
320 }
321
322 TEST_F(WindowTest, MoveCursorTo) {
323 scoped_ptr<Window> w1(
324 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
325 root_window()));
326 scoped_ptr<Window> w11(
327 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
328 scoped_ptr<Window> w111(
329 CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
330 scoped_ptr<Window> w1111(
331 CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
332
333 Window* root = root_window();
334 root->MoveCursorTo(gfx::Point(10, 10));
335 EXPECT_EQ("10,10",
336 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
337 w1->MoveCursorTo(gfx::Point(10, 10));
338 EXPECT_EQ("20,20",
339 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
340 w11->MoveCursorTo(gfx::Point(10, 10));
341 EXPECT_EQ("25,25",
342 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
343 w111->MoveCursorTo(gfx::Point(10, 10));
344 EXPECT_EQ("30,30",
345 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
346 w1111->MoveCursorTo(gfx::Point(10, 10));
347 EXPECT_EQ("35,35",
348 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
349 }
350
351 TEST_F(WindowTest, ContainsMouse) {
352 scoped_ptr<Window> w(
353 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
354 root_window()));
355 w->Show();
356 WindowTestApi w_test_api(w.get());
357 Window* root = root_window();
358 root->MoveCursorTo(gfx::Point(10, 10));
359 EXPECT_TRUE(w_test_api.ContainsMouse());
360 root->MoveCursorTo(gfx::Point(9, 10));
361 EXPECT_FALSE(w_test_api.ContainsMouse());
362 }
363
364 // Test Window::ConvertPointToWindow() with transform to root_window.
365 TEST_F(WindowTest, MoveCursorToWithTransformRootWindow) {
366 gfx::Transform transform;
367 transform.Translate(100.0, 100.0);
368 transform.Rotate(90.0);
369 transform.Scale(2.0, 5.0);
370 host()->SetRootTransform(transform);
371 host()->MoveCursorTo(gfx::Point(10, 10));
372 #if !defined(OS_WIN)
373 // TODO(yoshiki): fix this to build on Windows. See crbug.com/133413.OD
374 EXPECT_EQ("50,120", QueryLatestMousePositionRequestInHost(host()).ToString());
375 #endif
376 EXPECT_EQ("10,10", gfx::Screen::GetScreenFor(
377 root_window())->GetCursorScreenPoint().ToString());
378 }
379
380 // Tests Window::ConvertPointToWindow() with transform to non-root windows.
381 TEST_F(WindowTest, MoveCursorToWithTransformWindow) {
382 scoped_ptr<Window> w1(
383 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
384 root_window()));
385
386 gfx::Transform transform1;
387 transform1.Scale(2, 2);
388 w1->SetTransform(transform1);
389 w1->MoveCursorTo(gfx::Point(10, 10));
390 EXPECT_EQ("30,30",
391 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
392
393 gfx::Transform transform2;
394 transform2.Translate(-10, 20);
395 w1->SetTransform(transform2);
396 w1->MoveCursorTo(gfx::Point(10, 10));
397 EXPECT_EQ("10,40",
398 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
399
400 gfx::Transform transform3;
401 transform3.Rotate(90.0);
402 w1->SetTransform(transform3);
403 w1->MoveCursorTo(gfx::Point(5, 5));
404 EXPECT_EQ("5,15",
405 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
406
407 gfx::Transform transform4;
408 transform4.Translate(100.0, 100.0);
409 transform4.Rotate(90.0);
410 transform4.Scale(2.0, 5.0);
411 w1->SetTransform(transform4);
412 w1->MoveCursorTo(gfx::Point(10, 10));
413 EXPECT_EQ("60,130",
414 gfx::Screen::GetScreenFor(w1.get())->GetCursorScreenPoint().ToString());
415 }
416
417 // Test Window::ConvertPointToWindow() with complex transforms to both root and
418 // non-root windows.
419 // Test Window::ConvertPointToWindow() with transform to root_window.
420 TEST_F(WindowTest, MoveCursorToWithComplexTransform) {
421 scoped_ptr<Window> w1(
422 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
423 root_window()));
424 scoped_ptr<Window> w11(
425 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
426 scoped_ptr<Window> w111(
427 CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
428 scoped_ptr<Window> w1111(
429 CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
430
431 Window* root = root_window();
432
433 // The root window expects transforms that produce integer rects.
434 gfx::Transform root_transform;
435 root_transform.Translate(60.0, 70.0);
436 root_transform.Rotate(-90.0);
437 root_transform.Translate(-50.0, -50.0);
438 root_transform.Scale(2.0, 3.0);
439
440 gfx::Transform transform;
441 transform.Translate(10.0, 20.0);
442 transform.Rotate(10.0);
443 transform.Scale(0.3f, 0.5f);
444 host()->SetRootTransform(root_transform);
445 w1->SetTransform(transform);
446 w11->SetTransform(transform);
447 w111->SetTransform(transform);
448 w1111->SetTransform(transform);
449
450 w1111->MoveCursorTo(gfx::Point(10, 10));
451
452 #if !defined(OS_WIN)
453 // TODO(yoshiki): fix this to build on Windows. See crbug.com/133413.
454 EXPECT_EQ("169,80", QueryLatestMousePositionRequestInHost(host()).ToString());
455 #endif
456 EXPECT_EQ("20,53",
457 gfx::Screen::GetScreenFor(root)->GetCursorScreenPoint().ToString());
458 }
459
460 TEST_F(WindowTest, GetEventHandlerForPoint) {
461 scoped_ptr<Window> w1(
462 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500),
463 root_window()));
464 scoped_ptr<Window> w11(
465 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
466 scoped_ptr<Window> w111(
467 CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
468 scoped_ptr<Window> w1111(
469 CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
470 scoped_ptr<Window> w12(
471 CreateTestWindow(SK_ColorMAGENTA, 12, gfx::Rect(10, 420, 25, 25),
472 w1.get()));
473 scoped_ptr<Window> w121(
474 CreateTestWindow(SK_ColorYELLOW, 121, gfx::Rect(5, 5, 5, 5), w12.get()));
475 scoped_ptr<Window> w13(
476 CreateTestWindow(SK_ColorGRAY, 13, gfx::Rect(5, 470, 50, 50), w1.get()));
477
478 Window* root = root_window();
479 w1->parent()->SetBounds(gfx::Rect(500, 500));
480 EXPECT_EQ(NULL, root->GetEventHandlerForPoint(gfx::Point(5, 5)));
481 EXPECT_EQ(w1.get(), root->GetEventHandlerForPoint(gfx::Point(11, 11)));
482 EXPECT_EQ(w11.get(), root->GetEventHandlerForPoint(gfx::Point(16, 16)));
483 EXPECT_EQ(w111.get(), root->GetEventHandlerForPoint(gfx::Point(21, 21)));
484 EXPECT_EQ(w1111.get(), root->GetEventHandlerForPoint(gfx::Point(26, 26)));
485 EXPECT_EQ(w12.get(), root->GetEventHandlerForPoint(gfx::Point(21, 431)));
486 EXPECT_EQ(w121.get(), root->GetEventHandlerForPoint(gfx::Point(26, 436)));
487 EXPECT_EQ(w13.get(), root->GetEventHandlerForPoint(gfx::Point(26, 481)));
488 }
489
490 TEST_F(WindowTest, GetEventHandlerForPointWithOverride) {
491 // If our child is flush to our top-left corner he gets events just inside the
492 // window edges.
493 scoped_ptr<Window> parent(
494 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 20, 400, 500),
495 root_window()));
496 scoped_ptr<Window> child(
497 CreateTestWindow(SK_ColorRED, 2, gfx::Rect(0, 0, 60, 70), parent.get()));
498 EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
499 EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(1, 1)));
500
501 // We can override the hit test bounds of the parent to make the parent grab
502 // events along that edge.
503 parent->set_hit_test_bounds_override_inner(gfx::Insets(1, 1, 1, 1));
504 EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
505 EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(1, 1)));
506 }
507
508 TEST_F(WindowTest, GetEventHandlerForPointWithOverrideDescendingOrder) {
509 scoped_ptr<SelfEventHandlingWindowDelegate> parent_delegate(
510 new SelfEventHandlingWindowDelegate);
511 scoped_ptr<Window> parent(CreateTestWindowWithDelegate(
512 parent_delegate.get(), 1, gfx::Rect(10, 20, 400, 500), root_window()));
513 scoped_ptr<Window> child(
514 CreateTestWindow(SK_ColorRED, 2, gfx::Rect(0, 0, 390, 480),
515 parent.get()));
516
517 // We can override ShouldDescendIntoChildForEventHandling to make the parent
518 // grab all events.
519 EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
520 EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(50, 50)));
521 }
522
523 TEST_F(WindowTest, GetTopWindowContainingPoint) {
524 Window* root = root_window();
525 root->SetBounds(gfx::Rect(0, 0, 300, 300));
526
527 scoped_ptr<Window> w1(
528 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 100, 100),
529 root_window()));
530 scoped_ptr<Window> w11(
531 CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(0, 0, 120, 120), w1.get()));
532
533 scoped_ptr<Window> w2(
534 CreateTestWindow(SK_ColorRED, 2, gfx::Rect(5, 5, 55, 55),
535 root_window()));
536
537 scoped_ptr<Window> w3(
538 CreateTestWindowWithDelegate(
539 NULL, 3, gfx::Rect(200, 200, 100, 100), root_window()));
540 scoped_ptr<Window> w31(
541 CreateTestWindow(SK_ColorCYAN, 31, gfx::Rect(0, 0, 50, 50), w3.get()));
542 scoped_ptr<Window> w311(
543 CreateTestWindow(SK_ColorBLUE, 311, gfx::Rect(0, 0, 10, 10), w31.get()));
544
545 EXPECT_EQ(NULL, root->GetTopWindowContainingPoint(gfx::Point(0, 0)));
546 EXPECT_EQ(w2.get(), root->GetTopWindowContainingPoint(gfx::Point(5, 5)));
547 EXPECT_EQ(w2.get(), root->GetTopWindowContainingPoint(gfx::Point(10, 10)));
548 EXPECT_EQ(w2.get(), root->GetTopWindowContainingPoint(gfx::Point(59, 59)));
549 EXPECT_EQ(w1.get(), root->GetTopWindowContainingPoint(gfx::Point(60, 60)));
550 EXPECT_EQ(w1.get(), root->GetTopWindowContainingPoint(gfx::Point(109, 109)));
551 EXPECT_EQ(NULL, root->GetTopWindowContainingPoint(gfx::Point(110, 110)));
552 EXPECT_EQ(w31.get(), root->GetTopWindowContainingPoint(gfx::Point(200, 200)));
553 EXPECT_EQ(w31.get(), root->GetTopWindowContainingPoint(gfx::Point(220, 220)));
554 EXPECT_EQ(NULL, root->GetTopWindowContainingPoint(gfx::Point(260, 260)));
555 }
556
557 TEST_F(WindowTest, GetToplevelWindow) {
558 const gfx::Rect kBounds(0, 0, 10, 10);
559 TestWindowDelegate delegate;
560
561 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
562 scoped_ptr<Window> w11(
563 CreateTestWindowWithDelegate(&delegate, 11, kBounds, w1.get()));
564 scoped_ptr<Window> w111(CreateTestWindowWithId(111, w11.get()));
565 scoped_ptr<Window> w1111(
566 CreateTestWindowWithDelegate(&delegate, 1111, kBounds, w111.get()));
567
568 EXPECT_TRUE(root_window()->GetToplevelWindow() == NULL);
569 EXPECT_TRUE(w1->GetToplevelWindow() == NULL);
570 EXPECT_EQ(w11.get(), w11->GetToplevelWindow());
571 EXPECT_EQ(w11.get(), w111->GetToplevelWindow());
572 EXPECT_EQ(w11.get(), w1111->GetToplevelWindow());
573 }
574
575 class AddedToRootWindowObserver : public WindowObserver {
576 public:
577 AddedToRootWindowObserver() : called_(false) {}
578
579 virtual void OnWindowAddedToRootWindow(Window* window) override {
580 called_ = true;
581 }
582
583 bool called() const { return called_; }
584
585 private:
586 bool called_;
587
588 DISALLOW_COPY_AND_ASSIGN(AddedToRootWindowObserver);
589 };
590
591 TEST_F(WindowTest, WindowAddedToRootWindowShouldNotifyChildAndNotParent) {
592 AddedToRootWindowObserver parent_observer;
593 AddedToRootWindowObserver child_observer;
594 scoped_ptr<Window> parent_window(CreateTestWindowWithId(1, root_window()));
595 scoped_ptr<Window> child_window(new Window(NULL));
596 child_window->Init(aura::WINDOW_LAYER_TEXTURED);
597 child_window->Show();
598
599 parent_window->AddObserver(&parent_observer);
600 child_window->AddObserver(&child_observer);
601
602 parent_window->AddChild(child_window.get());
603
604 EXPECT_FALSE(parent_observer.called());
605 EXPECT_TRUE(child_observer.called());
606
607 parent_window->RemoveObserver(&parent_observer);
608 child_window->RemoveObserver(&child_observer);
609 }
610
611 // Various destruction assertions.
612 TEST_F(WindowTest, DestroyTest) {
613 DestroyTrackingDelegateImpl parent_delegate;
614 ChildWindowDelegateImpl child_delegate(&parent_delegate);
615 {
616 scoped_ptr<Window> parent(
617 CreateTestWindowWithDelegate(&parent_delegate, 0, gfx::Rect(),
618 root_window()));
619 CreateTestWindowWithDelegate(&child_delegate, 0, gfx::Rect(), parent.get());
620 }
621 // Both the parent and child should have been destroyed.
622 EXPECT_EQ(1, parent_delegate.destroying_count());
623 EXPECT_EQ(1, parent_delegate.destroyed_count());
624 EXPECT_EQ(1, child_delegate.destroying_count());
625 EXPECT_EQ(1, child_delegate.destroyed_count());
626 }
627
628 // Tests that a window is orphaned before OnWindowDestroyed is called.
629 TEST_F(WindowTest, OrphanedBeforeOnDestroyed) {
630 TestWindowDelegate parent_delegate;
631 DestroyOrphanDelegate child_delegate;
632 {
633 scoped_ptr<Window> parent(
634 CreateTestWindowWithDelegate(&parent_delegate, 0, gfx::Rect(),
635 root_window()));
636 scoped_ptr<Window> child(CreateTestWindowWithDelegate(&child_delegate, 0,
637 gfx::Rect(), parent.get()));
638 child_delegate.set_window(child.get());
639 }
640 }
641
642 // Make sure StackChildAtTop moves both the window and layer to the front.
643 TEST_F(WindowTest, StackChildAtTop) {
644 Window parent(NULL);
645 parent.Init(aura::WINDOW_LAYER_NOT_DRAWN);
646 Window child1(NULL);
647 child1.Init(aura::WINDOW_LAYER_NOT_DRAWN);
648 Window child2(NULL);
649 child2.Init(aura::WINDOW_LAYER_NOT_DRAWN);
650
651 parent.AddChild(&child1);
652 parent.AddChild(&child2);
653 ASSERT_EQ(2u, parent.children().size());
654 EXPECT_EQ(&child1, parent.children()[0]);
655 EXPECT_EQ(&child2, parent.children()[1]);
656 ASSERT_EQ(2u, parent.layer()->children().size());
657 EXPECT_EQ(child1.layer(), parent.layer()->children()[0]);
658 EXPECT_EQ(child2.layer(), parent.layer()->children()[1]);
659
660 parent.StackChildAtTop(&child1);
661 ASSERT_EQ(2u, parent.children().size());
662 EXPECT_EQ(&child1, parent.children()[1]);
663 EXPECT_EQ(&child2, parent.children()[0]);
664 ASSERT_EQ(2u, parent.layer()->children().size());
665 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
666 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
667 }
668
669 // Make sure StackChildBelow works.
670 TEST_F(WindowTest, StackChildBelow) {
671 Window parent(NULL);
672 parent.Init(aura::WINDOW_LAYER_NOT_DRAWN);
673 Window child1(NULL);
674 child1.Init(aura::WINDOW_LAYER_NOT_DRAWN);
675 child1.set_id(1);
676 Window child2(NULL);
677 child2.Init(aura::WINDOW_LAYER_NOT_DRAWN);
678 child2.set_id(2);
679 Window child3(NULL);
680 child3.Init(aura::WINDOW_LAYER_NOT_DRAWN);
681 child3.set_id(3);
682
683 parent.AddChild(&child1);
684 parent.AddChild(&child2);
685 parent.AddChild(&child3);
686 EXPECT_EQ("1 2 3", ChildWindowIDsAsString(&parent));
687
688 parent.StackChildBelow(&child1, &child2);
689 EXPECT_EQ("1 2 3", ChildWindowIDsAsString(&parent));
690
691 parent.StackChildBelow(&child2, &child1);
692 EXPECT_EQ("2 1 3", ChildWindowIDsAsString(&parent));
693
694 parent.StackChildBelow(&child3, &child2);
695 EXPECT_EQ("3 2 1", ChildWindowIDsAsString(&parent));
696
697 parent.StackChildBelow(&child3, &child1);
698 EXPECT_EQ("2 3 1", ChildWindowIDsAsString(&parent));
699 }
700
701 // Various assertions for StackChildAbove.
702 TEST_F(WindowTest, StackChildAbove) {
703 Window parent(NULL);
704 parent.Init(aura::WINDOW_LAYER_NOT_DRAWN);
705 Window child1(NULL);
706 child1.Init(aura::WINDOW_LAYER_NOT_DRAWN);
707 Window child2(NULL);
708 child2.Init(aura::WINDOW_LAYER_NOT_DRAWN);
709 Window child3(NULL);
710 child3.Init(aura::WINDOW_LAYER_NOT_DRAWN);
711
712 parent.AddChild(&child1);
713 parent.AddChild(&child2);
714
715 // Move 1 in front of 2.
716 parent.StackChildAbove(&child1, &child2);
717 ASSERT_EQ(2u, parent.children().size());
718 EXPECT_EQ(&child2, parent.children()[0]);
719 EXPECT_EQ(&child1, parent.children()[1]);
720 ASSERT_EQ(2u, parent.layer()->children().size());
721 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
722 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
723
724 // Add 3, resulting in order [2, 1, 3], then move 2 in front of 1, resulting
725 // in [1, 2, 3].
726 parent.AddChild(&child3);
727 parent.StackChildAbove(&child2, &child1);
728 ASSERT_EQ(3u, parent.children().size());
729 EXPECT_EQ(&child1, parent.children()[0]);
730 EXPECT_EQ(&child2, parent.children()[1]);
731 EXPECT_EQ(&child3, parent.children()[2]);
732 ASSERT_EQ(3u, parent.layer()->children().size());
733 EXPECT_EQ(child1.layer(), parent.layer()->children()[0]);
734 EXPECT_EQ(child2.layer(), parent.layer()->children()[1]);
735 EXPECT_EQ(child3.layer(), parent.layer()->children()[2]);
736
737 // Move 1 in front of 3, resulting in [2, 3, 1].
738 parent.StackChildAbove(&child1, &child3);
739 ASSERT_EQ(3u, parent.children().size());
740 EXPECT_EQ(&child2, parent.children()[0]);
741 EXPECT_EQ(&child3, parent.children()[1]);
742 EXPECT_EQ(&child1, parent.children()[2]);
743 ASSERT_EQ(3u, parent.layer()->children().size());
744 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
745 EXPECT_EQ(child3.layer(), parent.layer()->children()[1]);
746 EXPECT_EQ(child1.layer(), parent.layer()->children()[2]);
747
748 // Moving 1 in front of 2 should lower it, resulting in [2, 1, 3].
749 parent.StackChildAbove(&child1, &child2);
750 ASSERT_EQ(3u, parent.children().size());
751 EXPECT_EQ(&child2, parent.children()[0]);
752 EXPECT_EQ(&child1, parent.children()[1]);
753 EXPECT_EQ(&child3, parent.children()[2]);
754 ASSERT_EQ(3u, parent.layer()->children().size());
755 EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
756 EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
757 EXPECT_EQ(child3.layer(), parent.layer()->children()[2]);
758 }
759
760 // Various capture assertions.
761 TEST_F(WindowTest, CaptureTests) {
762 CaptureWindowDelegateImpl delegate;
763 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
764 &delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
765 EXPECT_FALSE(window->HasCapture());
766
767 delegate.ResetCounts();
768
769 // Do a capture.
770 window->SetCapture();
771 EXPECT_TRUE(window->HasCapture());
772 EXPECT_EQ(0, delegate.capture_lost_count());
773 EXPECT_EQ(0, delegate.capture_changed_event_count());
774 ui::test::EventGenerator generator(root_window(), gfx::Point(50, 50));
775 generator.PressLeftButton();
776 EXPECT_EQ(1, delegate.mouse_event_count());
777 generator.ReleaseLeftButton();
778
779 EXPECT_EQ(2, delegate.mouse_event_count());
780 delegate.ResetCounts();
781
782 ui::TouchEvent touchev(
783 ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), 0, getTime());
784 DispatchEventUsingWindowDispatcher(&touchev);
785 EXPECT_EQ(1, delegate.touch_event_count());
786 delegate.ResetCounts();
787
788 window->ReleaseCapture();
789 EXPECT_FALSE(window->HasCapture());
790 EXPECT_EQ(1, delegate.capture_lost_count());
791 EXPECT_EQ(1, delegate.capture_changed_event_count());
792 EXPECT_EQ(1, delegate.mouse_event_count());
793 EXPECT_EQ(0, delegate.touch_event_count());
794
795 generator.PressLeftButton();
796 EXPECT_EQ(1, delegate.mouse_event_count());
797
798 ui::TouchEvent touchev2(
799 ui::ET_TOUCH_PRESSED, gfx::Point(250, 250), 1, getTime());
800 DispatchEventUsingWindowDispatcher(&touchev2);
801 EXPECT_EQ(0, delegate.touch_event_count());
802
803 // Removing the capture window from parent should reset the capture window
804 // in the root window.
805 window->SetCapture();
806 EXPECT_EQ(window.get(), aura::client::GetCaptureWindow(root_window()));
807 window->parent()->RemoveChild(window.get());
808 EXPECT_FALSE(window->HasCapture());
809 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(root_window()));
810 }
811
812 TEST_F(WindowTest, TouchCaptureCancelsOtherTouches) {
813 CaptureWindowDelegateImpl delegate1;
814 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(
815 &delegate1, 0, gfx::Rect(0, 0, 50, 50), root_window()));
816 CaptureWindowDelegateImpl delegate2;
817 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
818 &delegate2, 0, gfx::Rect(50, 50, 50, 50), root_window()));
819
820 // Press on w1.
821 ui::TouchEvent press1(
822 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, getTime());
823 DispatchEventUsingWindowDispatcher(&press1);
824 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
825 EXPECT_EQ(2, delegate1.gesture_event_count());
826 delegate1.ResetCounts();
827
828 // Capturing to w2 should cause the touch to be canceled.
829 w2->SetCapture();
830 EXPECT_EQ(1, delegate1.touch_event_count());
831 EXPECT_EQ(0, delegate2.touch_event_count());
832 delegate1.ResetCounts();
833 delegate2.ResetCounts();
834
835 // Events now go to w2.
836 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 20), 0, getTime());
837 DispatchEventUsingWindowDispatcher(&move);
838 EXPECT_EQ(0, delegate1.gesture_event_count());
839 EXPECT_EQ(0, delegate1.touch_event_count());
840 EXPECT_EQ(0, delegate2.gesture_event_count());
841 EXPECT_EQ(1, delegate2.touch_event_count());
842
843 ui::TouchEvent release(
844 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), 0, getTime());
845 DispatchEventUsingWindowDispatcher(&release);
846 EXPECT_EQ(0, delegate1.gesture_event_count());
847 EXPECT_EQ(0, delegate2.gesture_event_count());
848
849 // A new press is captured by w2.
850 ui::TouchEvent press2(
851 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, getTime());
852 DispatchEventUsingWindowDispatcher(&press2);
853 EXPECT_EQ(0, delegate1.gesture_event_count());
854 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
855 EXPECT_EQ(2, delegate2.gesture_event_count());
856 delegate1.ResetCounts();
857 delegate2.ResetCounts();
858
859 // And releasing capture changes nothing.
860 w2->ReleaseCapture();
861 EXPECT_EQ(0, delegate1.gesture_event_count());
862 EXPECT_EQ(0, delegate1.touch_event_count());
863 EXPECT_EQ(0, delegate2.gesture_event_count());
864 EXPECT_EQ(0, delegate2.touch_event_count());
865 }
866
867 TEST_F(WindowTest, TouchCaptureDoesntCancelCapturedTouches) {
868 CaptureWindowDelegateImpl delegate;
869 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
870 &delegate, 0, gfx::Rect(0, 0, 50, 50), root_window()));
871 base::TimeDelta time = getTime();
872 const int kTimeDelta = 100;
873
874 ui::TouchEvent press(
875 ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, time);
876 DispatchEventUsingWindowDispatcher(&press);
877
878 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
879 EXPECT_EQ(2, delegate.gesture_event_count());
880 EXPECT_EQ(1, delegate.touch_event_count());
881 delegate.ResetCounts();
882
883 window->SetCapture();
884 EXPECT_EQ(0, delegate.gesture_event_count());
885 EXPECT_EQ(0, delegate.touch_event_count());
886 delegate.ResetCounts();
887
888 // On move We will get TOUCH_MOVED, GESTURE_TAP_CANCEL,
889 // GESTURE_SCROLL_START and GESTURE_SCROLL_UPDATE.
890 time += base::TimeDelta::FromMilliseconds(kTimeDelta);
891 ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 20), 0, time);
892 DispatchEventUsingWindowDispatcher(&move);
893 EXPECT_EQ(1, delegate.touch_event_count());
894 EXPECT_EQ(3, delegate.gesture_event_count());
895 delegate.ResetCounts();
896
897 // Release capture shouldn't change anything.
898 window->ReleaseCapture();
899 EXPECT_EQ(0, delegate.touch_event_count());
900 EXPECT_EQ(0, delegate.gesture_event_count());
901 delegate.ResetCounts();
902
903 // On move we still get TOUCH_MOVED and GESTURE_SCROLL_UPDATE.
904 time += base::TimeDelta::FromMilliseconds(kTimeDelta);
905 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(10, 30), 0, time);
906 DispatchEventUsingWindowDispatcher(&move2);
907 EXPECT_EQ(1, delegate.touch_event_count());
908 EXPECT_EQ(1, delegate.gesture_event_count());
909 delegate.ResetCounts();
910
911 // And on release we get TOUCH_RELEASED, GESTURE_SCROLL_END, GESTURE_END
912 time += base::TimeDelta::FromMilliseconds(kTimeDelta);
913 ui::TouchEvent release(
914 ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), 0, time);
915 DispatchEventUsingWindowDispatcher(&release);
916 EXPECT_EQ(1, delegate.touch_event_count());
917 EXPECT_EQ(2, delegate.gesture_event_count());
918 }
919
920
921 // Assertions around SetCapture() and touch/gestures.
922 TEST_F(WindowTest, TransferCaptureTouchEvents) {
923 // Touch on |w1|.
924 CaptureWindowDelegateImpl d1;
925 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(
926 &d1, 0, gfx::Rect(0, 0, 20, 20), root_window()));
927 ui::TouchEvent p1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), 0, getTime());
928 DispatchEventUsingWindowDispatcher(&p1);
929 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
930 EXPECT_EQ(1, d1.touch_event_count());
931 EXPECT_EQ(2, d1.gesture_event_count());
932 d1.ResetCounts();
933
934 // Touch on |w2| with a different id.
935 CaptureWindowDelegateImpl d2;
936 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
937 &d2, 0, gfx::Rect(40, 0, 40, 20), root_window()));
938 ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(41, 10), 1, getTime());
939 DispatchEventUsingWindowDispatcher(&p2);
940 EXPECT_EQ(0, d1.touch_event_count());
941 EXPECT_EQ(0, d1.gesture_event_count());
942 // We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN for new target window.
943 EXPECT_EQ(1, d2.touch_event_count());
944 EXPECT_EQ(2, d2.gesture_event_count());
945 d1.ResetCounts();
946 d2.ResetCounts();
947
948 // Set capture on |w2|, this should send a cancel (TAP_CANCEL, END) to |w1|
949 // but not |w2|.
950 w2->SetCapture();
951 EXPECT_EQ(1, d1.touch_event_count());
952 EXPECT_EQ(2, d1.gesture_event_count());
953 EXPECT_EQ(0, d2.touch_event_count());
954 EXPECT_EQ(0, d2.gesture_event_count());
955 d1.ResetCounts();
956 d2.ResetCounts();
957
958 CaptureWindowDelegateImpl d3;
959 scoped_ptr<Window> w3(CreateTestWindowWithDelegate(
960 &d3, 0, gfx::Rect(0, 0, 100, 101), root_window()));
961 // Set capture on w3. No new events should be received.
962 // Note this difference in behavior between the first and second capture
963 // is confusing and error prone. http://crbug.com/236930
964 w3->SetCapture();
965 EXPECT_EQ(0, d1.touch_event_count());
966 EXPECT_EQ(0, d1.gesture_event_count());
967 EXPECT_EQ(0, d2.touch_event_count());
968 EXPECT_EQ(0, d2.gesture_event_count());
969 EXPECT_EQ(0, d3.touch_event_count());
970 EXPECT_EQ(0, d3.gesture_event_count());
971
972 // Move touch id originally associated with |w2|. Since capture was transfered
973 // from 2 to 3 only |w3| should get the event.
974 ui::TouchEvent m3(ui::ET_TOUCH_MOVED, gfx::Point(110, 105), 1, getTime());
975 DispatchEventUsingWindowDispatcher(&m3);
976 EXPECT_EQ(0, d1.touch_event_count());
977 EXPECT_EQ(0, d1.gesture_event_count());
978 EXPECT_EQ(0, d2.touch_event_count());
979 EXPECT_EQ(0, d2.gesture_event_count());
980 // |w3| gets a TOUCH_MOVE, TAP_CANCEL and two scroll related events.
981 EXPECT_EQ(1, d3.touch_event_count());
982 EXPECT_EQ(3, d3.gesture_event_count());
983 d1.ResetCounts();
984 d2.ResetCounts();
985 d3.ResetCounts();
986
987 // When we release capture, no touches are canceled.
988 w3->ReleaseCapture();
989 EXPECT_EQ(0, d1.touch_event_count());
990 EXPECT_EQ(0, d1.gesture_event_count());
991 EXPECT_EQ(0, d2.touch_event_count());
992 EXPECT_EQ(0, d2.gesture_event_count());
993 EXPECT_EQ(0, d3.touch_event_count());
994 EXPECT_EQ(0, d3.gesture_event_count());
995
996 // And when we move the touch again, |w3| still gets the events.
997 ui::TouchEvent m4(ui::ET_TOUCH_MOVED, gfx::Point(120, 105), 1, getTime());
998 DispatchEventUsingWindowDispatcher(&m4);
999 EXPECT_EQ(0, d1.touch_event_count());
1000 EXPECT_EQ(0, d1.gesture_event_count());
1001 EXPECT_EQ(0, d2.touch_event_count());
1002 EXPECT_EQ(0, d2.gesture_event_count());
1003 EXPECT_EQ(1, d3.touch_event_count());
1004 EXPECT_EQ(1, d3.gesture_event_count());
1005 d1.ResetCounts();
1006 d2.ResetCounts();
1007 d3.ResetCounts();
1008 }
1009
1010 // Changes capture while capture is already ongoing.
1011 TEST_F(WindowTest, ChangeCaptureWhileMouseDown) {
1012 CaptureWindowDelegateImpl delegate;
1013 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
1014 &delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
1015 CaptureWindowDelegateImpl delegate2;
1016 scoped_ptr<Window> w2(CreateTestWindowWithDelegate(
1017 &delegate2, 0, gfx::Rect(20, 20, 20, 20), root_window()));
1018
1019 // Execute the scheduled draws so that mouse events are not
1020 // aggregated.
1021 RunAllPendingInMessageLoop();
1022
1023 EXPECT_FALSE(window->HasCapture());
1024
1025 // Do a capture.
1026 delegate.ResetCounts();
1027 window->SetCapture();
1028 EXPECT_TRUE(window->HasCapture());
1029 EXPECT_EQ(0, delegate.capture_lost_count());
1030 EXPECT_EQ(0, delegate.capture_changed_event_count());
1031 ui::test::EventGenerator generator(root_window(), gfx::Point(50, 50));
1032 generator.PressLeftButton();
1033 EXPECT_EQ(0, delegate.capture_lost_count());
1034 EXPECT_EQ(0, delegate.capture_changed_event_count());
1035 EXPECT_EQ(1, delegate.mouse_event_count());
1036
1037 // Set capture to |w2|, should implicitly unset capture for |window|.
1038 delegate.ResetCounts();
1039 delegate2.ResetCounts();
1040 w2->SetCapture();
1041
1042 generator.MoveMouseTo(gfx::Point(40, 40), 2);
1043 EXPECT_EQ(1, delegate.capture_lost_count());
1044 EXPECT_EQ(1, delegate.capture_changed_event_count());
1045 EXPECT_EQ(1, delegate.mouse_event_count());
1046 EXPECT_EQ(2, delegate2.mouse_event_count());
1047 }
1048
1049 // Verifies capture is reset when a window is destroyed.
1050 TEST_F(WindowTest, ReleaseCaptureOnDestroy) {
1051 CaptureWindowDelegateImpl delegate;
1052 scoped_ptr<Window> window(CreateTestWindowWithDelegate(
1053 &delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
1054 EXPECT_FALSE(window->HasCapture());
1055
1056 // Do a capture.
1057 window->SetCapture();
1058 EXPECT_TRUE(window->HasCapture());
1059
1060 // Destroy the window.
1061 window.reset();
1062
1063 // Make sure the root window doesn't reference the window anymore.
1064 EXPECT_EQ(NULL, host()->dispatcher()->mouse_pressed_handler());
1065 EXPECT_EQ(NULL, aura::client::GetCaptureWindow(root_window()));
1066 }
1067
1068 TEST_F(WindowTest, GetBoundsInRootWindow) {
1069 scoped_ptr<Window> viewport(CreateTestWindowWithBounds(
1070 gfx::Rect(0, 0, 300, 300), root_window()));
1071 scoped_ptr<Window> child(CreateTestWindowWithBounds(
1072 gfx::Rect(0, 0, 100, 100), viewport.get()));
1073 // Sanity check.
1074 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1075
1076 // The |child| window's screen bounds should move along with the |viewport|.
1077 viewport->SetBounds(gfx::Rect(-100, -100, 300, 300));
1078 EXPECT_EQ("-100,-100 100x100", child->GetBoundsInRootWindow().ToString());
1079
1080 // The |child| window is moved to the 0,0 in screen coordinates.
1081 // |GetBoundsInRootWindow()| should return 0,0.
1082 child->SetBounds(gfx::Rect(100, 100, 100, 100));
1083 EXPECT_EQ("0,0 100x100", child->GetBoundsInRootWindow().ToString());
1084 }
1085
1086 class MouseEnterExitWindowDelegate : public TestWindowDelegate {
1087 public:
1088 MouseEnterExitWindowDelegate() : entered_(false), exited_(false) {}
1089
1090 virtual void OnMouseEvent(ui::MouseEvent* event) override {
1091 switch (event->type()) {
1092 case ui::ET_MOUSE_ENTERED:
1093 EXPECT_TRUE(event->flags() & ui::EF_IS_SYNTHESIZED);
1094 entered_ = true;
1095 break;
1096 case ui::ET_MOUSE_EXITED:
1097 EXPECT_TRUE(event->flags() & ui::EF_IS_SYNTHESIZED);
1098 exited_ = true;
1099 break;
1100 default:
1101 break;
1102 }
1103 }
1104
1105 bool entered() const { return entered_; }
1106 bool exited() const { return exited_; }
1107
1108 // Clear the entered / exited states.
1109 void ResetExpectations() {
1110 entered_ = false;
1111 exited_ = false;
1112 }
1113
1114 private:
1115 bool entered_;
1116 bool exited_;
1117
1118 DISALLOW_COPY_AND_ASSIGN(MouseEnterExitWindowDelegate);
1119 };
1120
1121
1122 // Verifies that the WindowDelegate receives MouseExit and MouseEnter events for
1123 // mouse transitions from window to window.
1124 TEST_F(WindowTest, MouseEnterExit) {
1125 MouseEnterExitWindowDelegate d1;
1126 scoped_ptr<Window> w1(
1127 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1128 root_window()));
1129 MouseEnterExitWindowDelegate d2;
1130 scoped_ptr<Window> w2(
1131 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(70, 70, 50, 50),
1132 root_window()));
1133
1134 ui::test::EventGenerator generator(root_window());
1135 generator.MoveMouseToCenterOf(w1.get());
1136 EXPECT_TRUE(d1.entered());
1137 EXPECT_FALSE(d1.exited());
1138 EXPECT_FALSE(d2.entered());
1139 EXPECT_FALSE(d2.exited());
1140
1141 generator.MoveMouseToCenterOf(w2.get());
1142 EXPECT_TRUE(d1.entered());
1143 EXPECT_TRUE(d1.exited());
1144 EXPECT_TRUE(d2.entered());
1145 EXPECT_FALSE(d2.exited());
1146 }
1147
1148 // Verifies that the WindowDelegate receives MouseExit from ET_MOUSE_EXITED.
1149 TEST_F(WindowTest, WindowTreeHostExit) {
1150 MouseEnterExitWindowDelegate d1;
1151 scoped_ptr<Window> w1(
1152 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1153 root_window()));
1154
1155 ui::test::EventGenerator generator(root_window());
1156 generator.MoveMouseToCenterOf(w1.get());
1157 EXPECT_TRUE(d1.entered());
1158 EXPECT_FALSE(d1.exited());
1159 d1.ResetExpectations();
1160
1161 ui::MouseEvent exit_event(
1162 ui::ET_MOUSE_EXITED, gfx::Point(), gfx::Point(), 0, 0);
1163 DispatchEventUsingWindowDispatcher(&exit_event);
1164 EXPECT_FALSE(d1.entered());
1165 EXPECT_TRUE(d1.exited());
1166 }
1167
1168 // Verifies that the WindowDelegate receives MouseExit and MouseEnter events for
1169 // mouse transitions from window to window, even if the entered window sets
1170 // and releases capture.
1171 TEST_F(WindowTest, MouseEnterExitWithClick) {
1172 MouseEnterExitWindowDelegate d1;
1173 scoped_ptr<Window> w1(
1174 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1175 root_window()));
1176 MouseEnterExitWindowDelegate d2;
1177 scoped_ptr<Window> w2(
1178 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(70, 70, 50, 50),
1179 root_window()));
1180
1181 ui::test::EventGenerator generator(root_window());
1182 generator.MoveMouseToCenterOf(w1.get());
1183 EXPECT_TRUE(d1.entered());
1184 EXPECT_FALSE(d1.exited());
1185 EXPECT_FALSE(d2.entered());
1186 EXPECT_FALSE(d2.exited());
1187
1188 // Emmulate what Views does on a click by grabbing and releasing capture.
1189 generator.PressLeftButton();
1190 w1->SetCapture();
1191 w1->ReleaseCapture();
1192 generator.ReleaseLeftButton();
1193
1194 generator.MoveMouseToCenterOf(w2.get());
1195 EXPECT_TRUE(d1.entered());
1196 EXPECT_TRUE(d1.exited());
1197 EXPECT_TRUE(d2.entered());
1198 EXPECT_FALSE(d2.exited());
1199 }
1200
1201 TEST_F(WindowTest, MouseEnterExitWhenDeleteWithCapture) {
1202 MouseEnterExitWindowDelegate delegate;
1203 scoped_ptr<Window> window(
1204 CreateTestWindowWithDelegate(&delegate, 1, gfx::Rect(10, 10, 50, 50),
1205 root_window()));
1206
1207 ui::test::EventGenerator generator(root_window());
1208 generator.MoveMouseToCenterOf(window.get());
1209 EXPECT_TRUE(delegate.entered());
1210 EXPECT_FALSE(delegate.exited());
1211
1212 // Emmulate what Views does on a click by grabbing and releasing capture.
1213 generator.PressLeftButton();
1214 window->SetCapture();
1215
1216 delegate.ResetExpectations();
1217 generator.MoveMouseTo(0, 0);
1218 EXPECT_FALSE(delegate.entered());
1219 EXPECT_FALSE(delegate.exited());
1220
1221 delegate.ResetExpectations();
1222 window.reset();
1223 EXPECT_FALSE(delegate.entered());
1224 EXPECT_FALSE(delegate.exited());
1225 }
1226
1227 // Verifies that enter / exits are sent if windows appear and are deleted
1228 // under the current mouse position..
1229 TEST_F(WindowTest, MouseEnterExitWithDelete) {
1230 MouseEnterExitWindowDelegate d1;
1231 scoped_ptr<Window> w1(
1232 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1233 root_window()));
1234
1235 ui::test::EventGenerator generator(root_window());
1236 generator.MoveMouseToCenterOf(w1.get());
1237 EXPECT_TRUE(d1.entered());
1238 EXPECT_FALSE(d1.exited());
1239 d1.ResetExpectations();
1240
1241 MouseEnterExitWindowDelegate d2;
1242 {
1243 scoped_ptr<Window> w2(
1244 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1245 root_window()));
1246 // Enters / exits can be sent asynchronously.
1247 RunAllPendingInMessageLoop();
1248 EXPECT_FALSE(d1.entered());
1249 EXPECT_TRUE(d1.exited());
1250 EXPECT_TRUE(d2.entered());
1251 EXPECT_FALSE(d2.exited());
1252 d1.ResetExpectations();
1253 d2.ResetExpectations();
1254 }
1255 // Enters / exits can be sent asynchronously.
1256 RunAllPendingInMessageLoop();
1257 EXPECT_TRUE(d2.exited());
1258 EXPECT_TRUE(d1.entered());
1259 }
1260
1261 // Verifies that enter / exits are sent if windows appear and are hidden
1262 // under the current mouse position..
1263 TEST_F(WindowTest, MouseEnterExitWithHide) {
1264 MouseEnterExitWindowDelegate d1;
1265 scoped_ptr<Window> w1(
1266 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1267 root_window()));
1268
1269 ui::test::EventGenerator generator(root_window());
1270 generator.MoveMouseToCenterOf(w1.get());
1271 EXPECT_TRUE(d1.entered());
1272 EXPECT_FALSE(d1.exited());
1273
1274 MouseEnterExitWindowDelegate d2;
1275 scoped_ptr<Window> w2(
1276 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1277 root_window()));
1278 // Enters / exits can be send asynchronously.
1279 RunAllPendingInMessageLoop();
1280 EXPECT_TRUE(d1.entered());
1281 EXPECT_TRUE(d1.exited());
1282 EXPECT_TRUE(d2.entered());
1283 EXPECT_FALSE(d2.exited());
1284
1285 d1.ResetExpectations();
1286 w2->Hide();
1287 // Enters / exits can be send asynchronously.
1288 RunAllPendingInMessageLoop();
1289 EXPECT_TRUE(d2.exited());
1290 EXPECT_TRUE(d1.entered());
1291 }
1292
1293 TEST_F(WindowTest, MouseEnterExitWithParentHide) {
1294 MouseEnterExitWindowDelegate d1;
1295 scoped_ptr<Window> w1(
1296 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1297 root_window()));
1298 MouseEnterExitWindowDelegate d2;
1299 Window* w2 = CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1300 w1.get());
1301 ui::test::EventGenerator generator(root_window());
1302 generator.MoveMouseToCenterOf(w2);
1303 // Enters / exits can be send asynchronously.
1304 RunAllPendingInMessageLoop();
1305 EXPECT_TRUE(d2.entered());
1306 EXPECT_FALSE(d2.exited());
1307
1308 d2.ResetExpectations();
1309 w1->Hide();
1310 RunAllPendingInMessageLoop();
1311 EXPECT_FALSE(d2.entered());
1312 EXPECT_TRUE(d2.exited());
1313
1314 w1.reset();
1315 }
1316
1317 TEST_F(WindowTest, MouseEnterExitWithParentDelete) {
1318 MouseEnterExitWindowDelegate d1;
1319 scoped_ptr<Window> w1(
1320 CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50),
1321 root_window()));
1322 MouseEnterExitWindowDelegate d2;
1323 Window* w2 = CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(10, 10, 50, 50),
1324 w1.get());
1325 ui::test::EventGenerator generator(root_window());
1326 generator.MoveMouseToCenterOf(w2);
1327
1328 // Enters / exits can be send asynchronously.
1329 RunAllPendingInMessageLoop();
1330 EXPECT_TRUE(d2.entered());
1331 EXPECT_FALSE(d2.exited());
1332
1333 d2.ResetExpectations();
1334 w1.reset();
1335 RunAllPendingInMessageLoop();
1336 EXPECT_FALSE(d2.entered());
1337 EXPECT_TRUE(d2.exited());
1338 }
1339
1340 // Creates a window with a delegate (w111) that can handle events at a lower
1341 // z-index than a window without a delegate (w12). w12 is sized to fill the
1342 // entire bounds of the container. This test verifies that
1343 // GetEventHandlerForPoint() skips w12 even though its bounds contain the event,
1344 // because it has no children that can handle the event and it has no delegate
1345 // allowing it to handle the event itself.
1346 TEST_F(WindowTest, GetEventHandlerForPoint_NoDelegate) {
1347 TestWindowDelegate d111;
1348 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(NULL, 1,
1349 gfx::Rect(0, 0, 500, 500), root_window()));
1350 scoped_ptr<Window> w11(CreateTestWindowWithDelegate(NULL, 11,
1351 gfx::Rect(0, 0, 500, 500), w1.get()));
1352 scoped_ptr<Window> w111(CreateTestWindowWithDelegate(&d111, 111,
1353 gfx::Rect(50, 50, 450, 450), w11.get()));
1354 scoped_ptr<Window> w12(CreateTestWindowWithDelegate(NULL, 12,
1355 gfx::Rect(0, 0, 500, 500), w1.get()));
1356
1357 gfx::Point target_point = w111->bounds().CenterPoint();
1358 EXPECT_EQ(w111.get(), w1->GetEventHandlerForPoint(target_point));
1359 }
1360
1361 class VisibilityWindowDelegate : public TestWindowDelegate {
1362 public:
1363 VisibilityWindowDelegate()
1364 : shown_(0),
1365 hidden_(0) {
1366 }
1367
1368 int shown() const { return shown_; }
1369 int hidden() const { return hidden_; }
1370 void Clear() {
1371 shown_ = 0;
1372 hidden_ = 0;
1373 }
1374
1375 virtual void OnWindowTargetVisibilityChanged(bool visible) override {
1376 if (visible)
1377 shown_++;
1378 else
1379 hidden_++;
1380 }
1381
1382 private:
1383 int shown_;
1384 int hidden_;
1385
1386 DISALLOW_COPY_AND_ASSIGN(VisibilityWindowDelegate);
1387 };
1388
1389 // Verifies show/hide propagate correctly to children and the layer.
1390 TEST_F(WindowTest, Visibility) {
1391 VisibilityWindowDelegate d;
1392 VisibilityWindowDelegate d2;
1393 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(&d, 1, gfx::Rect(),
1394 root_window()));
1395 scoped_ptr<Window> w2(
1396 CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(), w1.get()));
1397 scoped_ptr<Window> w3(CreateTestWindowWithId(3, w2.get()));
1398
1399 // Create shows all the windows.
1400 EXPECT_TRUE(w1->IsVisible());
1401 EXPECT_TRUE(w2->IsVisible());
1402 EXPECT_TRUE(w3->IsVisible());
1403 EXPECT_EQ(1, d.shown());
1404
1405 d.Clear();
1406 w1->Hide();
1407 EXPECT_FALSE(w1->IsVisible());
1408 EXPECT_FALSE(w2->IsVisible());
1409 EXPECT_FALSE(w3->IsVisible());
1410 EXPECT_EQ(1, d.hidden());
1411 EXPECT_EQ(0, d.shown());
1412
1413 w2->Show();
1414 EXPECT_FALSE(w1->IsVisible());
1415 EXPECT_FALSE(w2->IsVisible());
1416 EXPECT_FALSE(w3->IsVisible());
1417
1418 w3->Hide();
1419 EXPECT_FALSE(w1->IsVisible());
1420 EXPECT_FALSE(w2->IsVisible());
1421 EXPECT_FALSE(w3->IsVisible());
1422
1423 d.Clear();
1424 w1->Show();
1425 EXPECT_TRUE(w1->IsVisible());
1426 EXPECT_TRUE(w2->IsVisible());
1427 EXPECT_FALSE(w3->IsVisible());
1428 EXPECT_EQ(0, d.hidden());
1429 EXPECT_EQ(1, d.shown());
1430
1431 w3->Show();
1432 EXPECT_TRUE(w1->IsVisible());
1433 EXPECT_TRUE(w2->IsVisible());
1434 EXPECT_TRUE(w3->IsVisible());
1435
1436 // Verify that if an ancestor isn't visible and we change the visibility of a
1437 // child window that OnChildWindowVisibilityChanged() is still invoked.
1438 w1->Hide();
1439 d2.Clear();
1440 w2->Hide();
1441 EXPECT_EQ(1, d2.hidden());
1442 EXPECT_EQ(0, d2.shown());
1443 d2.Clear();
1444 w2->Show();
1445 EXPECT_EQ(0, d2.hidden());
1446 EXPECT_EQ(1, d2.shown());
1447 }
1448
1449 TEST_F(WindowTest, IgnoreEventsTest) {
1450 TestWindowDelegate d11;
1451 TestWindowDelegate d12;
1452 TestWindowDelegate d111;
1453 TestWindowDelegate d121;
1454 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(NULL, 1,
1455 gfx::Rect(0, 0, 500, 500), root_window()));
1456 scoped_ptr<Window> w11(CreateTestWindowWithDelegate(&d11, 11,
1457 gfx::Rect(0, 0, 500, 500), w1.get()));
1458 scoped_ptr<Window> w111(CreateTestWindowWithDelegate(&d111, 111,
1459 gfx::Rect(50, 50, 450, 450), w11.get()));
1460 scoped_ptr<Window> w12(CreateTestWindowWithDelegate(&d12, 12,
1461 gfx::Rect(0, 0, 500, 500), w1.get()));
1462 scoped_ptr<Window> w121(CreateTestWindowWithDelegate(&d121, 121,
1463 gfx::Rect(150, 150, 50, 50), w12.get()));
1464
1465 EXPECT_EQ(w12.get(), w1->GetEventHandlerForPoint(gfx::Point(10, 10)));
1466 w12->set_ignore_events(true);
1467 EXPECT_EQ(w11.get(), w1->GetEventHandlerForPoint(gfx::Point(10, 10)));
1468 w12->set_ignore_events(false);
1469
1470 EXPECT_EQ(w121.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1471 w121->set_ignore_events(true);
1472 EXPECT_EQ(w12.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1473 w12->set_ignore_events(true);
1474 EXPECT_EQ(w111.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1475 w111->set_ignore_events(true);
1476 EXPECT_EQ(w11.get(), w1->GetEventHandlerForPoint(gfx::Point(160, 160)));
1477 }
1478
1479 // Tests transformation on the root window.
1480 TEST_F(WindowTest, Transform) {
1481 gfx::Size size = host()->GetBounds().size();
1482 EXPECT_EQ(gfx::Rect(size),
1483 gfx::Screen::GetScreenFor(root_window())->GetDisplayNearestPoint(
1484 gfx::Point()).bounds());
1485
1486 // Rotate it clock-wise 90 degrees.
1487 gfx::Transform transform;
1488 transform.Translate(size.height(), 0);
1489 transform.Rotate(90.0);
1490 host()->SetRootTransform(transform);
1491
1492 // The size should be the transformed size.
1493 gfx::Size transformed_size(size.height(), size.width());
1494 EXPECT_EQ(transformed_size.ToString(),
1495 root_window()->bounds().size().ToString());
1496 EXPECT_EQ(
1497 gfx::Rect(transformed_size).ToString(),
1498 gfx::Screen::GetScreenFor(root_window())->GetDisplayNearestPoint(
1499 gfx::Point()).bounds().ToString());
1500
1501 // Host size shouldn't change.
1502 EXPECT_EQ(size.ToString(), host()->GetBounds().size().ToString());
1503 }
1504
1505 TEST_F(WindowTest, TransformGesture) {
1506 gfx::Size size = host()->GetBounds().size();
1507
1508 scoped_ptr<GestureTrackPositionDelegate> delegate(
1509 new GestureTrackPositionDelegate);
1510 scoped_ptr<Window> window(CreateTestWindowWithDelegate(delegate.get(), -1234,
1511 gfx::Rect(0, 0, 20, 20), root_window()));
1512
1513 // Rotate the root-window clock-wise 90 degrees.
1514 gfx::Transform transform;
1515 transform.Translate(size.height(), 0.0);
1516 transform.Rotate(90.0);
1517 host()->SetRootTransform(transform);
1518
1519 ui::TouchEvent press(
1520 ui::ET_TOUCH_PRESSED, gfx::Point(size.height() - 10, 10), 0, getTime());
1521 DispatchEventUsingWindowDispatcher(&press);
1522 EXPECT_EQ(gfx::Point(10, 10).ToString(), delegate->position().ToString());
1523 }
1524
1525 namespace {
1526 DEFINE_WINDOW_PROPERTY_KEY(int, kIntKey, -2);
1527 DEFINE_WINDOW_PROPERTY_KEY(const char*, kStringKey, "squeamish");
1528 }
1529
1530 TEST_F(WindowTest, Property) {
1531 scoped_ptr<Window> w(CreateTestWindowWithId(0, root_window()));
1532
1533 static const char native_prop_key[] = "fnord";
1534
1535 // Non-existent properties should return the default values.
1536 EXPECT_EQ(-2, w->GetProperty(kIntKey));
1537 EXPECT_EQ(std::string("squeamish"), w->GetProperty(kStringKey));
1538 EXPECT_EQ(NULL, w->GetNativeWindowProperty(native_prop_key));
1539
1540 // A set property value should be returned again (even if it's the default
1541 // value).
1542 w->SetProperty(kIntKey, INT_MAX);
1543 EXPECT_EQ(INT_MAX, w->GetProperty(kIntKey));
1544 w->SetProperty(kIntKey, -2);
1545 EXPECT_EQ(-2, w->GetProperty(kIntKey));
1546 w->SetProperty(kIntKey, INT_MIN);
1547 EXPECT_EQ(INT_MIN, w->GetProperty(kIntKey));
1548
1549 w->SetProperty(kStringKey, static_cast<const char*>(NULL));
1550 EXPECT_EQ(NULL, w->GetProperty(kStringKey));
1551 w->SetProperty(kStringKey, "squeamish");
1552 EXPECT_EQ(std::string("squeamish"), w->GetProperty(kStringKey));
1553 w->SetProperty(kStringKey, "ossifrage");
1554 EXPECT_EQ(std::string("ossifrage"), w->GetProperty(kStringKey));
1555
1556 w->SetNativeWindowProperty(native_prop_key, &*w);
1557 EXPECT_EQ(&*w, w->GetNativeWindowProperty(native_prop_key));
1558 w->SetNativeWindowProperty(native_prop_key, NULL);
1559 EXPECT_EQ(NULL, w->GetNativeWindowProperty(native_prop_key));
1560
1561 // ClearProperty should restore the default value.
1562 w->ClearProperty(kIntKey);
1563 EXPECT_EQ(-2, w->GetProperty(kIntKey));
1564 w->ClearProperty(kStringKey);
1565 EXPECT_EQ(std::string("squeamish"), w->GetProperty(kStringKey));
1566 }
1567
1568 namespace {
1569
1570 class TestProperty {
1571 public:
1572 TestProperty() {}
1573 virtual ~TestProperty() {
1574 last_deleted_ = this;
1575 }
1576 static TestProperty* last_deleted() { return last_deleted_; }
1577
1578 private:
1579 static TestProperty* last_deleted_;
1580 DISALLOW_COPY_AND_ASSIGN(TestProperty);
1581 };
1582
1583 TestProperty* TestProperty::last_deleted_ = NULL;
1584
1585 DEFINE_OWNED_WINDOW_PROPERTY_KEY(TestProperty, kOwnedKey, NULL);
1586
1587 } // namespace
1588
1589 TEST_F(WindowTest, OwnedProperty) {
1590 scoped_ptr<Window> w(CreateTestWindowWithId(0, root_window()));
1591 EXPECT_EQ(NULL, w->GetProperty(kOwnedKey));
1592 TestProperty* p1 = new TestProperty();
1593 w->SetProperty(kOwnedKey, p1);
1594 EXPECT_EQ(p1, w->GetProperty(kOwnedKey));
1595 EXPECT_EQ(NULL, TestProperty::last_deleted());
1596
1597 TestProperty* p2 = new TestProperty();
1598 w->SetProperty(kOwnedKey, p2);
1599 EXPECT_EQ(p2, w->GetProperty(kOwnedKey));
1600 EXPECT_EQ(p1, TestProperty::last_deleted());
1601
1602 w->ClearProperty(kOwnedKey);
1603 EXPECT_EQ(NULL, w->GetProperty(kOwnedKey));
1604 EXPECT_EQ(p2, TestProperty::last_deleted());
1605
1606 TestProperty* p3 = new TestProperty();
1607 w->SetProperty(kOwnedKey, p3);
1608 EXPECT_EQ(p3, w->GetProperty(kOwnedKey));
1609 EXPECT_EQ(p2, TestProperty::last_deleted());
1610 w.reset();
1611 EXPECT_EQ(p3, TestProperty::last_deleted());
1612 }
1613
1614 TEST_F(WindowTest, SetBoundsInternalShouldCheckTargetBounds) {
1615 // We cannot short-circuit animations in this test.
1616 ui::ScopedAnimationDurationScaleMode test_duration_mode(
1617 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
1618
1619 scoped_ptr<Window> w1(
1620 CreateTestWindowWithBounds(gfx::Rect(0, 0, 100, 100), root_window()));
1621
1622 EXPECT_FALSE(!w1->layer());
1623 w1->layer()->GetAnimator()->set_disable_timer_for_test(true);
1624 ui::LayerAnimator* animator = w1->layer()->GetAnimator();
1625
1626 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1627 EXPECT_EQ("0,0 100x100", w1->layer()->GetTargetBounds().ToString());
1628
1629 // Animate to a different position.
1630 {
1631 ui::ScopedLayerAnimationSettings settings(w1->layer()->GetAnimator());
1632 w1->SetBounds(gfx::Rect(100, 100, 100, 100));
1633 }
1634
1635 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1636 EXPECT_EQ("100,100 100x100", w1->layer()->GetTargetBounds().ToString());
1637
1638 // Animate back to the first position. The animation hasn't started yet, so
1639 // the current bounds are still (0, 0, 100, 100), but the target bounds are
1640 // (100, 100, 100, 100). If we step the animator ahead, we should find that
1641 // we're at (0, 0, 100, 100). That is, the second animation should be applied.
1642 {
1643 ui::ScopedLayerAnimationSettings settings(w1->layer()->GetAnimator());
1644 w1->SetBounds(gfx::Rect(0, 0, 100, 100));
1645 }
1646
1647 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1648 EXPECT_EQ("0,0 100x100", w1->layer()->GetTargetBounds().ToString());
1649
1650 // Confirm that the target bounds are reached.
1651 base::TimeTicks start_time =
1652 w1->layer()->GetAnimator()->last_step_time();
1653
1654 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1655
1656 EXPECT_EQ("0,0 100x100", w1->bounds().ToString());
1657 }
1658
1659
1660 typedef std::pair<const void*, intptr_t> PropertyChangeInfo;
1661
1662 class WindowObserverTest : public WindowTest,
1663 public WindowObserver {
1664 public:
1665 struct VisibilityInfo {
1666 bool window_visible;
1667 bool visible_param;
1668 int changed_count;
1669 };
1670
1671 WindowObserverTest()
1672 : added_count_(0),
1673 removed_count_(0),
1674 destroyed_count_(0),
1675 old_property_value_(-3) {
1676 }
1677
1678 virtual ~WindowObserverTest() {}
1679
1680 const VisibilityInfo* GetVisibilityInfo() const {
1681 return visibility_info_.get();
1682 }
1683
1684 void ResetVisibilityInfo() {
1685 visibility_info_.reset();
1686 }
1687
1688 // Returns a description of the WindowObserver methods that have been invoked.
1689 std::string WindowObserverCountStateAndClear() {
1690 std::string result(
1691 base::StringPrintf("added=%d removed=%d",
1692 added_count_, removed_count_));
1693 added_count_ = removed_count_ = 0;
1694 return result;
1695 }
1696
1697 int DestroyedCountAndClear() {
1698 int result = destroyed_count_;
1699 destroyed_count_ = 0;
1700 return result;
1701 }
1702
1703 // Return a tuple of the arguments passed in OnPropertyChanged callback.
1704 PropertyChangeInfo PropertyChangeInfoAndClear() {
1705 PropertyChangeInfo result(property_key_, old_property_value_);
1706 property_key_ = NULL;
1707 old_property_value_ = -3;
1708 return result;
1709 }
1710
1711 std::string TransformNotificationsAndClear() {
1712 std::string result;
1713 for (std::vector<std::pair<int, int> >::iterator it =
1714 transform_notifications_.begin();
1715 it != transform_notifications_.end();
1716 ++it) {
1717 base::StringAppendF(&result, "(%d,%d)", it->first, it->second);
1718 }
1719 transform_notifications_.clear();
1720 return result;
1721 }
1722
1723 private:
1724 virtual void OnWindowAdded(Window* new_window) override {
1725 added_count_++;
1726 }
1727
1728 virtual void OnWillRemoveWindow(Window* window) override {
1729 removed_count_++;
1730 }
1731
1732 virtual void OnWindowVisibilityChanged(Window* window,
1733 bool visible) override {
1734 if (!visibility_info_) {
1735 visibility_info_.reset(new VisibilityInfo);
1736 visibility_info_->changed_count = 0;
1737 }
1738 visibility_info_->window_visible = window->IsVisible();
1739 visibility_info_->visible_param = visible;
1740 visibility_info_->changed_count++;
1741 }
1742
1743 virtual void OnWindowDestroyed(Window* window) override {
1744 EXPECT_FALSE(window->parent());
1745 destroyed_count_++;
1746 }
1747
1748 virtual void OnWindowPropertyChanged(Window* window,
1749 const void* key,
1750 intptr_t old) override {
1751 property_key_ = key;
1752 old_property_value_ = old;
1753 }
1754
1755 virtual void OnAncestorWindowTransformed(Window* source,
1756 Window* window) override {
1757 transform_notifications_.push_back(
1758 std::make_pair(source->id(), window->id()));
1759 }
1760
1761 int added_count_;
1762 int removed_count_;
1763 int destroyed_count_;
1764 scoped_ptr<VisibilityInfo> visibility_info_;
1765 const void* property_key_;
1766 intptr_t old_property_value_;
1767 std::vector<std::pair<int, int> > transform_notifications_;
1768
1769 DISALLOW_COPY_AND_ASSIGN(WindowObserverTest);
1770 };
1771
1772 // Various assertions for WindowObserver.
1773 TEST_F(WindowObserverTest, WindowObserver) {
1774 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1775 w1->AddObserver(this);
1776
1777 // Create a new window as a child of w1, our observer should be notified.
1778 scoped_ptr<Window> w2(CreateTestWindowWithId(2, w1.get()));
1779 EXPECT_EQ("added=1 removed=0", WindowObserverCountStateAndClear());
1780
1781 // Delete w2, which should result in the remove notification.
1782 w2.reset();
1783 EXPECT_EQ("added=0 removed=1", WindowObserverCountStateAndClear());
1784
1785 // Create a window that isn't parented to w1, we shouldn't get any
1786 // notification.
1787 scoped_ptr<Window> w3(CreateTestWindowWithId(3, root_window()));
1788 EXPECT_EQ("added=0 removed=0", WindowObserverCountStateAndClear());
1789
1790 // Similarly destroying w3 shouldn't notify us either.
1791 w3.reset();
1792 EXPECT_EQ("added=0 removed=0", WindowObserverCountStateAndClear());
1793 w1->RemoveObserver(this);
1794 }
1795
1796 // Test if OnWindowVisibilityChagned is invoked with expected
1797 // parameters.
1798 TEST_F(WindowObserverTest, WindowVisibility) {
1799 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1800 scoped_ptr<Window> w2(CreateTestWindowWithId(1, w1.get()));
1801 w2->AddObserver(this);
1802
1803 // Hide should make the window invisible and the passed visible
1804 // parameter is false.
1805 w2->Hide();
1806 EXPECT_FALSE(!GetVisibilityInfo());
1807 EXPECT_FALSE(!GetVisibilityInfo());
1808 if (!GetVisibilityInfo())
1809 return;
1810 EXPECT_FALSE(GetVisibilityInfo()->window_visible);
1811 EXPECT_FALSE(GetVisibilityInfo()->visible_param);
1812 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1813
1814 // If parent isn't visible, showing window won't make the window visible, but
1815 // passed visible value must be true.
1816 w1->Hide();
1817 ResetVisibilityInfo();
1818 EXPECT_TRUE(!GetVisibilityInfo());
1819 w2->Show();
1820 EXPECT_FALSE(!GetVisibilityInfo());
1821 if (!GetVisibilityInfo())
1822 return;
1823 EXPECT_FALSE(GetVisibilityInfo()->window_visible);
1824 EXPECT_TRUE(GetVisibilityInfo()->visible_param);
1825 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1826
1827 // If parent is visible, showing window will make the window
1828 // visible and the passed visible value is true.
1829 w1->Show();
1830 w2->Hide();
1831 ResetVisibilityInfo();
1832 w2->Show();
1833 EXPECT_FALSE(!GetVisibilityInfo());
1834 if (!GetVisibilityInfo())
1835 return;
1836 EXPECT_TRUE(GetVisibilityInfo()->window_visible);
1837 EXPECT_TRUE(GetVisibilityInfo()->visible_param);
1838 EXPECT_EQ(1, GetVisibilityInfo()->changed_count);
1839
1840 // Verify that the OnWindowVisibilityChanged only once
1841 // per visibility change.
1842 w2->Hide();
1843 EXPECT_EQ(2, GetVisibilityInfo()->changed_count);
1844
1845 w2->Hide();
1846 EXPECT_EQ(2, GetVisibilityInfo()->changed_count);
1847 }
1848
1849 // Test if OnWindowDestroyed is invoked as expected.
1850 TEST_F(WindowObserverTest, WindowDestroyed) {
1851 // Delete a window should fire a destroyed notification.
1852 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1853 w1->AddObserver(this);
1854 w1.reset();
1855 EXPECT_EQ(1, DestroyedCountAndClear());
1856
1857 // Observe on child and delete parent window should fire a notification.
1858 scoped_ptr<Window> parent(CreateTestWindowWithId(1, root_window()));
1859 Window* child = CreateTestWindowWithId(1, parent.get()); // owned by parent
1860 child->AddObserver(this);
1861 parent.reset();
1862 EXPECT_EQ(1, DestroyedCountAndClear());
1863 }
1864
1865 TEST_F(WindowObserverTest, PropertyChanged) {
1866 // Setting property should fire a property change notification.
1867 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1868 w1->AddObserver(this);
1869
1870 static const WindowProperty<int> prop = {-2};
1871 static const char native_prop_key[] = "fnord";
1872
1873 w1->SetProperty(&prop, 1);
1874 EXPECT_EQ(PropertyChangeInfo(&prop, -2), PropertyChangeInfoAndClear());
1875 w1->SetProperty(&prop, -2);
1876 EXPECT_EQ(PropertyChangeInfo(&prop, 1), PropertyChangeInfoAndClear());
1877 w1->SetProperty(&prop, 3);
1878 EXPECT_EQ(PropertyChangeInfo(&prop, -2), PropertyChangeInfoAndClear());
1879 w1->ClearProperty(&prop);
1880 EXPECT_EQ(PropertyChangeInfo(&prop, 3), PropertyChangeInfoAndClear());
1881
1882 w1->SetNativeWindowProperty(native_prop_key, &*w1);
1883 EXPECT_EQ(PropertyChangeInfo(native_prop_key, 0),
1884 PropertyChangeInfoAndClear());
1885 w1->SetNativeWindowProperty(native_prop_key, NULL);
1886 EXPECT_EQ(PropertyChangeInfo(native_prop_key,
1887 reinterpret_cast<intptr_t>(&*w1)),
1888 PropertyChangeInfoAndClear());
1889
1890 // Sanity check to see if |PropertyChangeInfoAndClear| really clears.
1891 EXPECT_EQ(PropertyChangeInfo(
1892 reinterpret_cast<const void*>(NULL), -3), PropertyChangeInfoAndClear());
1893 }
1894
1895 TEST_F(WindowObserverTest, AncestorTransformed) {
1896 // Create following window hierarchy:
1897 // root_window
1898 // +-- w1
1899 // +-- w2
1900 // +-- w3
1901 // +-- w4
1902 // Then, apply a transform to |w1| and ensure all its descendants are
1903 // notified.
1904 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
1905 w1->AddObserver(this);
1906 scoped_ptr<Window> w2(CreateTestWindowWithId(2, w1.get()));
1907 w2->AddObserver(this);
1908 scoped_ptr<Window> w3(CreateTestWindowWithId(3, w1.get()));
1909 w3->AddObserver(this);
1910 scoped_ptr<Window> w4(CreateTestWindowWithId(4, w3.get()));
1911 w4->AddObserver(this);
1912
1913 EXPECT_EQ(std::string(), TransformNotificationsAndClear());
1914
1915 gfx::Transform transform;
1916 transform.Translate(10, 10);
1917 w1->SetTransform(transform);
1918
1919 EXPECT_EQ("(1,1)(1,2)(1,3)(1,4)", TransformNotificationsAndClear());
1920 }
1921
1922 TEST_F(WindowTest, AcquireLayer) {
1923 scoped_ptr<Window> window1(CreateTestWindowWithId(1, root_window()));
1924 scoped_ptr<Window> window2(CreateTestWindowWithId(2, root_window()));
1925 ui::Layer* parent = window1->parent()->layer();
1926 EXPECT_EQ(2U, parent->children().size());
1927
1928 WindowTestApi window1_test_api(window1.get());
1929 WindowTestApi window2_test_api(window2.get());
1930
1931 EXPECT_TRUE(window1_test_api.OwnsLayer());
1932 EXPECT_TRUE(window2_test_api.OwnsLayer());
1933
1934 // After acquisition, window1 should not own its layer, but it should still
1935 // be available to the window.
1936 scoped_ptr<ui::Layer> window1_layer(window1->AcquireLayer());
1937 EXPECT_FALSE(window1_test_api.OwnsLayer());
1938 EXPECT_TRUE(window1_layer.get() == window1->layer());
1939
1940 // The acquired layer's owner should be set NULL and re-acquring
1941 // should return NULL.
1942 EXPECT_FALSE(window1_layer->owner());
1943 scoped_ptr<ui::Layer> window1_layer_reacquired(window1->AcquireLayer());
1944 EXPECT_FALSE(window1_layer_reacquired.get());
1945
1946 // Upon destruction, window1's layer should still be valid, and in the layer
1947 // hierarchy, but window2's should be gone, and no longer in the hierarchy.
1948 window1.reset();
1949 window2.reset();
1950
1951 // This should be set by the window's destructor.
1952 EXPECT_TRUE(window1_layer->delegate() == NULL);
1953 EXPECT_EQ(1U, parent->children().size());
1954 }
1955
1956 // Make sure that properties which should persist from the old layer to the new
1957 // layer actually do.
1958 TEST_F(WindowTest, RecreateLayer) {
1959 // Set properties to non default values.
1960 Window w(new ColorTestWindowDelegate(SK_ColorWHITE));
1961 w.set_id(1);
1962 w.Init(aura::WINDOW_LAYER_SOLID_COLOR);
1963 w.SetBounds(gfx::Rect(0, 0, 100, 100));
1964
1965 ui::Layer* layer = w.layer();
1966 layer->SetVisible(false);
1967 layer->SetMasksToBounds(true);
1968
1969 ui::Layer child_layer;
1970 layer->Add(&child_layer);
1971
1972 scoped_ptr<ui::Layer> old_layer(w.RecreateLayer());
1973 layer = w.layer();
1974 EXPECT_EQ(ui::LAYER_SOLID_COLOR, layer->type());
1975 EXPECT_FALSE(layer->visible());
1976 EXPECT_EQ(1u, layer->children().size());
1977 EXPECT_TRUE(layer->GetMasksToBounds());
1978 EXPECT_EQ("0,0 100x100", w.bounds().ToString());
1979 EXPECT_EQ("0,0 100x100", layer->bounds().ToString());
1980 }
1981
1982 // Verify that RecreateLayer() stacks the old layer above the newly creatd
1983 // layer.
1984 TEST_F(WindowTest, RecreateLayerZOrder) {
1985 scoped_ptr<Window> w(
1986 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(0, 0, 100, 100),
1987 root_window()));
1988 scoped_ptr<ui::Layer> old_layer(w->RecreateLayer());
1989
1990 const std::vector<ui::Layer*>& child_layers =
1991 root_window()->layer()->children();
1992 ASSERT_EQ(2u, child_layers.size());
1993 EXPECT_EQ(w->layer(), child_layers[0]);
1994 EXPECT_EQ(old_layer.get(), child_layers[1]);
1995 }
1996
1997 // Ensure that acquiring a layer then recreating a layer does not crash
1998 // and that RecreateLayer returns null.
1999 TEST_F(WindowTest, AcquireThenRecreateLayer) {
2000 scoped_ptr<Window> w(
2001 CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(0, 0, 100, 100),
2002 root_window()));
2003 scoped_ptr<ui::Layer> acquired_layer(w->AcquireLayer());
2004 scoped_ptr<ui::Layer> doubly_acquired_layer(w->RecreateLayer());
2005 EXPECT_EQ(NULL, doubly_acquired_layer.get());
2006
2007 // Destroy window before layer gets destroyed.
2008 w.reset();
2009 }
2010
2011 TEST_F(WindowTest, StackWindowAtBottomBelowWindowWhoseLayerHasNoDelegate) {
2012 scoped_ptr<Window> window1(CreateTestWindowWithId(1, root_window()));
2013 window1->layer()->set_name("1");
2014 scoped_ptr<Window> window2(CreateTestWindowWithId(2, root_window()));
2015 window2->layer()->set_name("2");
2016 scoped_ptr<Window> window3(CreateTestWindowWithId(3, root_window()));
2017 window3->layer()->set_name("3");
2018
2019 EXPECT_EQ("1 2 3", ChildWindowIDsAsString(root_window()));
2020 EXPECT_EQ("1 2 3",
2021 ui::test::ChildLayerNamesAsString(*root_window()->layer()));
2022 window1->layer()->set_delegate(NULL);
2023 root_window()->StackChildAtBottom(window3.get());
2024
2025 // Window 3 should have moved to the bottom.
2026 EXPECT_EQ("3 1 2", ChildWindowIDsAsString(root_window()));
2027 EXPECT_EQ("3 1 2",
2028 ui::test::ChildLayerNamesAsString(*root_window()->layer()));
2029 }
2030
2031 class TestVisibilityClient : public client::VisibilityClient {
2032 public:
2033 explicit TestVisibilityClient(Window* root_window)
2034 : ignore_visibility_changes_(false) {
2035 client::SetVisibilityClient(root_window, this);
2036 }
2037 virtual ~TestVisibilityClient() {
2038 }
2039
2040 void set_ignore_visibility_changes(bool ignore_visibility_changes) {
2041 ignore_visibility_changes_ = ignore_visibility_changes;
2042 }
2043
2044 // Overridden from client::VisibilityClient:
2045 virtual void UpdateLayerVisibility(aura::Window* window,
2046 bool visible) override {
2047 if (!ignore_visibility_changes_)
2048 window->layer()->SetVisible(visible);
2049 }
2050
2051 private:
2052 bool ignore_visibility_changes_;
2053 DISALLOW_COPY_AND_ASSIGN(TestVisibilityClient);
2054 };
2055
2056 TEST_F(WindowTest, VisibilityClientIsVisible) {
2057 TestVisibilityClient client(root_window());
2058
2059 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
2060 EXPECT_TRUE(window->IsVisible());
2061 EXPECT_TRUE(window->layer()->visible());
2062
2063 window->Hide();
2064 EXPECT_FALSE(window->IsVisible());
2065 EXPECT_FALSE(window->layer()->visible());
2066 window->Show();
2067
2068 client.set_ignore_visibility_changes(true);
2069 window->Hide();
2070 EXPECT_FALSE(window->IsVisible());
2071 EXPECT_TRUE(window->layer()->visible());
2072 }
2073
2074 // Tests mouse events on window change.
2075 TEST_F(WindowTest, MouseEventsOnWindowChange) {
2076 gfx::Size size = host()->GetBounds().size();
2077
2078 ui::test::EventGenerator generator(root_window());
2079 generator.MoveMouseTo(50, 50);
2080
2081 EventCountDelegate d1;
2082 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(&d1, 1,
2083 gfx::Rect(0, 0, 100, 100), root_window()));
2084 RunAllPendingInMessageLoop();
2085 // The format of result is "Enter/Mouse/Leave".
2086 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2087
2088 // Adding new window.
2089 EventCountDelegate d11;
2090 scoped_ptr<Window> w11(CreateTestWindowWithDelegate(
2091 &d11, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2092 RunAllPendingInMessageLoop();
2093 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2094 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2095
2096 // Move bounds.
2097 w11->SetBounds(gfx::Rect(0, 0, 10, 10));
2098 RunAllPendingInMessageLoop();
2099 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2100 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2101
2102 w11->SetBounds(gfx::Rect(0, 0, 60, 60));
2103 RunAllPendingInMessageLoop();
2104 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2105 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2106
2107 // Detach, then re-attach.
2108 w1->RemoveChild(w11.get());
2109 RunAllPendingInMessageLoop();
2110 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2111 // Window is detached, so no event is set.
2112 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2113
2114 w1->AddChild(w11.get());
2115 RunAllPendingInMessageLoop();
2116 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2117 // Window is detached, so no event is set.
2118 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2119
2120 // Visibility Change
2121 w11->Hide();
2122 RunAllPendingInMessageLoop();
2123 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2124 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2125
2126 w11->Show();
2127 RunAllPendingInMessageLoop();
2128 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2129 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2130
2131 // Transform: move d11 by 100 100.
2132 gfx::Transform transform;
2133 transform.Translate(100, 100);
2134 w11->SetTransform(transform);
2135 RunAllPendingInMessageLoop();
2136 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2137 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2138
2139 w11->SetTransform(gfx::Transform());
2140 RunAllPendingInMessageLoop();
2141 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2142 EXPECT_EQ("1 1 0", d11.GetMouseMotionCountsAndReset());
2143
2144 // Closing a window.
2145 w11.reset();
2146 RunAllPendingInMessageLoop();
2147 EXPECT_EQ("1 1 0", d1.GetMouseMotionCountsAndReset());
2148
2149 // Make sure we don't synthesize events if the mouse
2150 // is outside of the root window.
2151 generator.MoveMouseTo(-10, -10);
2152 EXPECT_EQ("0 0 1", d1.GetMouseMotionCountsAndReset());
2153
2154 // Adding new windows.
2155 w11.reset(CreateTestWindowWithDelegate(
2156 &d11, 1, gfx::Rect(0, 0, 100, 100), w1.get()));
2157 RunAllPendingInMessageLoop();
2158 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2159 EXPECT_EQ("0 0 1", d11.GetMouseMotionCountsAndReset());
2160
2161 // Closing windows
2162 w11.reset();
2163 RunAllPendingInMessageLoop();
2164 EXPECT_EQ("0 0 0", d1.GetMouseMotionCountsAndReset());
2165 EXPECT_EQ("0 0 0", d11.GetMouseMotionCountsAndReset());
2166 }
2167
2168 class RootWindowAttachmentObserver : public WindowObserver {
2169 public:
2170 RootWindowAttachmentObserver() : added_count_(0), removed_count_(0) {}
2171 virtual ~RootWindowAttachmentObserver() {}
2172
2173 int added_count() const { return added_count_; }
2174 int removed_count() const { return removed_count_; }
2175
2176 void Clear() {
2177 added_count_ = 0;
2178 removed_count_ = 0;
2179 }
2180
2181 // Overridden from WindowObserver:
2182 virtual void OnWindowAddedToRootWindow(Window* window) override {
2183 ++added_count_;
2184 }
2185 virtual void OnWindowRemovingFromRootWindow(Window* window,
2186 Window* new_root) override {
2187 ++removed_count_;
2188 }
2189
2190 private:
2191 int added_count_;
2192 int removed_count_;
2193
2194 DISALLOW_COPY_AND_ASSIGN(RootWindowAttachmentObserver);
2195 };
2196
2197 TEST_F(WindowTest, RootWindowAttachment) {
2198 RootWindowAttachmentObserver observer;
2199
2200 // Test a direct add/remove from the RootWindow.
2201 scoped_ptr<Window> w1(new Window(NULL));
2202 w1->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2203 w1->AddObserver(&observer);
2204
2205 ParentWindow(w1.get());
2206 EXPECT_EQ(1, observer.added_count());
2207 EXPECT_EQ(0, observer.removed_count());
2208
2209 w1.reset();
2210 EXPECT_EQ(1, observer.added_count());
2211 EXPECT_EQ(1, observer.removed_count());
2212
2213 observer.Clear();
2214
2215 // Test an indirect add/remove from the RootWindow.
2216 w1.reset(new Window(NULL));
2217 w1->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2218 Window* w11 = new Window(NULL);
2219 w11->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2220 w11->AddObserver(&observer);
2221 w1->AddChild(w11);
2222 EXPECT_EQ(0, observer.added_count());
2223 EXPECT_EQ(0, observer.removed_count());
2224
2225 ParentWindow(w1.get());
2226 EXPECT_EQ(1, observer.added_count());
2227 EXPECT_EQ(0, observer.removed_count());
2228
2229 w1.reset(); // Deletes w11.
2230 w11 = NULL;
2231 EXPECT_EQ(1, observer.added_count());
2232 EXPECT_EQ(1, observer.removed_count());
2233
2234 observer.Clear();
2235
2236 // Test an indirect add/remove with nested observers.
2237 w1.reset(new Window(NULL));
2238 w1->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2239 w11 = new Window(NULL);
2240 w11->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2241 w11->AddObserver(&observer);
2242 w1->AddChild(w11);
2243 Window* w111 = new Window(NULL);
2244 w111->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2245 w111->AddObserver(&observer);
2246 w11->AddChild(w111);
2247
2248 EXPECT_EQ(0, observer.added_count());
2249 EXPECT_EQ(0, observer.removed_count());
2250
2251 ParentWindow(w1.get());
2252 EXPECT_EQ(2, observer.added_count());
2253 EXPECT_EQ(0, observer.removed_count());
2254
2255 w1.reset(); // Deletes w11 and w111.
2256 w11 = NULL;
2257 w111 = NULL;
2258 EXPECT_EQ(2, observer.added_count());
2259 EXPECT_EQ(2, observer.removed_count());
2260 }
2261
2262 class BoundsChangedWindowObserver : public WindowObserver {
2263 public:
2264 BoundsChangedWindowObserver() : root_set_(false) {}
2265
2266 virtual void OnWindowBoundsChanged(Window* window,
2267 const gfx::Rect& old_bounds,
2268 const gfx::Rect& new_bounds) override {
2269 root_set_ = window->GetRootWindow() != NULL;
2270 }
2271
2272 bool root_set() const { return root_set_; }
2273
2274 private:
2275 bool root_set_;
2276
2277 DISALLOW_COPY_AND_ASSIGN(BoundsChangedWindowObserver);
2278 };
2279
2280 TEST_F(WindowTest, RootWindowSetWhenReparenting) {
2281 Window parent1(NULL);
2282 parent1.Init(aura::WINDOW_LAYER_NOT_DRAWN);
2283 Window parent2(NULL);
2284 parent2.Init(aura::WINDOW_LAYER_NOT_DRAWN);
2285 ParentWindow(&parent1);
2286 ParentWindow(&parent2);
2287 parent1.SetBounds(gfx::Rect(10, 10, 300, 300));
2288 parent2.SetBounds(gfx::Rect(20, 20, 300, 300));
2289
2290 BoundsChangedWindowObserver observer;
2291 Window child(NULL);
2292 child.Init(aura::WINDOW_LAYER_NOT_DRAWN);
2293 child.SetBounds(gfx::Rect(5, 5, 100, 100));
2294 parent1.AddChild(&child);
2295
2296 // We need animations to start in order to observe the bounds changes.
2297 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2298 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2299 ui::ScopedLayerAnimationSettings settings1(child.layer()->GetAnimator());
2300 settings1.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100));
2301 gfx::Rect new_bounds(gfx::Rect(35, 35, 50, 50));
2302 child.SetBounds(new_bounds);
2303
2304 child.AddObserver(&observer);
2305
2306 // Reparenting the |child| will cause it to get moved. During this move
2307 // the window should still have root window set.
2308 parent2.AddChild(&child);
2309 EXPECT_TRUE(observer.root_set());
2310
2311 // Animations should stop and the bounds should be as set before the |child|
2312 // got reparented.
2313 EXPECT_EQ(new_bounds.ToString(), child.GetTargetBounds().ToString());
2314 EXPECT_EQ(new_bounds.ToString(), child.bounds().ToString());
2315 EXPECT_EQ("55,55 50x50", child.GetBoundsInRootWindow().ToString());
2316 }
2317
2318 TEST_F(WindowTest, OwnedByParentFalse) {
2319 // By default, a window is owned by its parent. If this is set to false, the
2320 // window will not be destroyed when its parent is.
2321
2322 scoped_ptr<Window> w1(new Window(NULL));
2323 w1->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2324 scoped_ptr<Window> w2(new Window(NULL));
2325 w2->set_owned_by_parent(false);
2326 w2->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2327 w1->AddChild(w2.get());
2328
2329 w1.reset();
2330
2331 // We should be able to deref w2 still, but its parent should now be NULL.
2332 EXPECT_EQ(NULL, w2->parent());
2333 }
2334
2335 namespace {
2336
2337 // Used By DeleteWindowFromOnWindowDestroyed. Destroys a Window from
2338 // OnWindowDestroyed().
2339 class OwningWindowDelegate : public TestWindowDelegate {
2340 public:
2341 OwningWindowDelegate() {}
2342
2343 void SetOwnedWindow(Window* window) {
2344 owned_window_.reset(window);
2345 }
2346
2347 virtual void OnWindowDestroyed(Window* window) override {
2348 owned_window_.reset(NULL);
2349 }
2350
2351 private:
2352 scoped_ptr<Window> owned_window_;
2353
2354 DISALLOW_COPY_AND_ASSIGN(OwningWindowDelegate);
2355 };
2356
2357 } // namespace
2358
2359 // Creates a window with two child windows. When the first child window is
2360 // destroyed (WindowDelegate::OnWindowDestroyed) it deletes the second child.
2361 // This synthesizes BrowserView and the status bubble. Both are children of the
2362 // same parent and destroying BrowserView triggers it destroying the status
2363 // bubble.
2364 TEST_F(WindowTest, DeleteWindowFromOnWindowDestroyed) {
2365 scoped_ptr<Window> parent(new Window(NULL));
2366 parent->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2367 OwningWindowDelegate delegate;
2368 Window* c1 = new Window(&delegate);
2369 c1->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2370 parent->AddChild(c1);
2371 Window* c2 = new Window(NULL);
2372 c2->Init(aura::WINDOW_LAYER_NOT_DRAWN);
2373 parent->AddChild(c2);
2374 delegate.SetOwnedWindow(c2);
2375 parent.reset();
2376 }
2377
2378 namespace {
2379
2380 // Used by DelegateNotifiedAsBoundsChange to verify OnBoundsChanged() is
2381 // invoked.
2382 class BoundsChangeDelegate : public TestWindowDelegate {
2383 public:
2384 BoundsChangeDelegate() : bounds_changed_(false) {}
2385
2386 void clear_bounds_changed() { bounds_changed_ = false; }
2387 bool bounds_changed() const {
2388 return bounds_changed_;
2389 }
2390
2391 // Window
2392 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
2393 const gfx::Rect& new_bounds) override {
2394 bounds_changed_ = true;
2395 }
2396
2397 private:
2398 // Was OnBoundsChanged() invoked?
2399 bool bounds_changed_;
2400
2401 DISALLOW_COPY_AND_ASSIGN(BoundsChangeDelegate);
2402 };
2403
2404 } // namespace
2405
2406 // Verifies the delegate is notified when the actual bounds of the layer
2407 // change.
2408 TEST_F(WindowTest, DelegateNotifiedAsBoundsChange) {
2409 BoundsChangeDelegate delegate;
2410
2411 // We cannot short-circuit animations in this test.
2412 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2413 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2414
2415 scoped_ptr<Window> window(
2416 CreateTestWindowWithDelegate(&delegate, 1,
2417 gfx::Rect(0, 0, 100, 100), root_window()));
2418 window->layer()->GetAnimator()->set_disable_timer_for_test(true);
2419
2420 delegate.clear_bounds_changed();
2421
2422 // Animate to a different position.
2423 {
2424 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
2425 window->SetBounds(gfx::Rect(100, 100, 100, 100));
2426 }
2427
2428 // Bounds shouldn't immediately have changed.
2429 EXPECT_EQ("0,0 100x100", window->bounds().ToString());
2430 EXPECT_FALSE(delegate.bounds_changed());
2431
2432 // Animate to the end, which should notify of the change.
2433 base::TimeTicks start_time =
2434 window->layer()->GetAnimator()->last_step_time();
2435 ui::LayerAnimator* animator = window->layer()->GetAnimator();
2436 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2437 EXPECT_TRUE(delegate.bounds_changed());
2438 EXPECT_NE("0,0 100x100", window->bounds().ToString());
2439 }
2440
2441 // Verifies the delegate is notified when the actual bounds of the layer
2442 // change even when the window is not the layer's delegate
2443 TEST_F(WindowTest, DelegateNotifiedAsBoundsChangeInHiddenLayer) {
2444 BoundsChangeDelegate delegate;
2445
2446 // We cannot short-circuit animations in this test.
2447 ui::ScopedAnimationDurationScaleMode test_duration_mode(
2448 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2449
2450 scoped_ptr<Window> window(
2451 CreateTestWindowWithDelegate(&delegate, 1,
2452 gfx::Rect(0, 0, 100, 100), root_window()));
2453 window->layer()->GetAnimator()->set_disable_timer_for_test(true);
2454
2455 delegate.clear_bounds_changed();
2456
2457 // Suppress paint on the window since it is hidden (should reset the layer's
2458 // delegate to NULL)
2459 window->SuppressPaint();
2460 EXPECT_EQ(NULL, window->layer()->delegate());
2461
2462 // Animate to a different position.
2463 {
2464 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
2465 window->SetBounds(gfx::Rect(100, 100, 110, 100));
2466 }
2467
2468 // Layer delegate is NULL but we should still get bounds changed notification.
2469 EXPECT_EQ("100,100 110x100", window->GetTargetBounds().ToString());
2470 EXPECT_TRUE(delegate.bounds_changed());
2471
2472 delegate.clear_bounds_changed();
2473
2474 // Animate to the end: will *not* notify of the change since we are hidden.
2475 base::TimeTicks start_time =
2476 window->layer()->GetAnimator()->last_step_time();
2477 ui::LayerAnimator* animator = window->layer()->GetAnimator();
2478 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2479
2480 // No bounds changed notification at the end of animation since layer
2481 // delegate is NULL.
2482 EXPECT_FALSE(delegate.bounds_changed());
2483 EXPECT_NE("0,0 100x100", window->layer()->bounds().ToString());
2484 }
2485
2486 namespace {
2487
2488 // Used by AddChildNotifications to track notification counts.
2489 class AddChildNotificationsObserver : public WindowObserver {
2490 public:
2491 AddChildNotificationsObserver() : added_count_(0), removed_count_(0) {}
2492
2493 std::string CountStringAndReset() {
2494 std::string result = base::IntToString(added_count_) + " " +
2495 base::IntToString(removed_count_);
2496 added_count_ = removed_count_ = 0;
2497 return result;
2498 }
2499
2500 // WindowObserver overrides:
2501 virtual void OnWindowAddedToRootWindow(Window* window) override {
2502 added_count_++;
2503 }
2504 virtual void OnWindowRemovingFromRootWindow(Window* window,
2505 Window* new_root) override {
2506 removed_count_++;
2507 }
2508
2509 private:
2510 int added_count_;
2511 int removed_count_;
2512
2513 DISALLOW_COPY_AND_ASSIGN(AddChildNotificationsObserver);
2514 };
2515
2516 } // namespace
2517
2518 // Assertions around when root window notifications are sent.
2519 TEST_F(WindowTest, AddChildNotifications) {
2520 AddChildNotificationsObserver observer;
2521 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
2522 scoped_ptr<Window> w2(CreateTestWindowWithId(1, root_window()));
2523 w2->AddObserver(&observer);
2524 w2->Focus();
2525 EXPECT_TRUE(w2->HasFocus());
2526
2527 // Move |w2| to be a child of |w1|.
2528 w1->AddChild(w2.get());
2529 // Sine we moved in the same root, observer shouldn't be notified.
2530 EXPECT_EQ("0 0", observer.CountStringAndReset());
2531 // |w2| should still have focus after moving.
2532 EXPECT_TRUE(w2->HasFocus());
2533 }
2534
2535 // Tests that a delegate that destroys itself when the window is destroyed does
2536 // not break.
2537 TEST_F(WindowTest, DelegateDestroysSelfOnWindowDestroy) {
2538 scoped_ptr<Window> w1(CreateTestWindowWithDelegate(
2539 new DestroyWindowDelegate(),
2540 0,
2541 gfx::Rect(10, 20, 30, 40),
2542 root_window()));
2543 }
2544
2545 class HierarchyObserver : public WindowObserver {
2546 public:
2547 HierarchyObserver(Window* target) : target_(target) {
2548 target_->AddObserver(this);
2549 }
2550 virtual ~HierarchyObserver() {
2551 target_->RemoveObserver(this);
2552 }
2553
2554 void ValidateState(
2555 int index,
2556 const WindowObserver::HierarchyChangeParams& params) const {
2557 ParamsMatch(params_[index], params);
2558 }
2559
2560 void Reset() {
2561 params_.clear();
2562 }
2563
2564 private:
2565 // Overridden from WindowObserver:
2566 virtual void OnWindowHierarchyChanging(
2567 const HierarchyChangeParams& params) override {
2568 params_.push_back(params);
2569 }
2570 virtual void OnWindowHierarchyChanged(
2571 const HierarchyChangeParams& params) override {
2572 params_.push_back(params);
2573 }
2574
2575 void ParamsMatch(const WindowObserver::HierarchyChangeParams& p1,
2576 const WindowObserver::HierarchyChangeParams& p2) const {
2577 EXPECT_EQ(p1.phase, p2.phase);
2578 EXPECT_EQ(p1.target, p2.target);
2579 EXPECT_EQ(p1.new_parent, p2.new_parent);
2580 EXPECT_EQ(p1.old_parent, p2.old_parent);
2581 EXPECT_EQ(p1.receiver, p2.receiver);
2582 }
2583
2584 Window* target_;
2585 std::vector<WindowObserver::HierarchyChangeParams> params_;
2586
2587 DISALLOW_COPY_AND_ASSIGN(HierarchyObserver);
2588 };
2589
2590 // Tests hierarchy change notifications.
2591 TEST_F(WindowTest, OnWindowHierarchyChange) {
2592 {
2593 // Simple add & remove.
2594 HierarchyObserver oroot(root_window());
2595
2596 scoped_ptr<Window> w1(CreateTestWindowWithId(1, NULL));
2597 HierarchyObserver o1(w1.get());
2598
2599 // Add.
2600 root_window()->AddChild(w1.get());
2601
2602 WindowObserver::HierarchyChangeParams params;
2603 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2604 params.target = w1.get();
2605 params.old_parent = NULL;
2606 params.new_parent = root_window();
2607 params.receiver = w1.get();
2608 o1.ValidateState(0, params);
2609
2610 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2611 params.receiver = w1.get();
2612 o1.ValidateState(1, params);
2613
2614 params.receiver = root_window();
2615 oroot.ValidateState(0, params);
2616
2617 // Remove.
2618 o1.Reset();
2619 oroot.Reset();
2620
2621 root_window()->RemoveChild(w1.get());
2622
2623 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2624 params.old_parent = root_window();
2625 params.new_parent = NULL;
2626 params.receiver = w1.get();
2627
2628 o1.ValidateState(0, params);
2629
2630 params.receiver = root_window();
2631 oroot.ValidateState(0, params);
2632
2633 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2634 params.receiver = w1.get();
2635 o1.ValidateState(1, params);
2636 }
2637
2638 {
2639 // Add & remove of hierarchy. Tests notification order per documentation in
2640 // WindowObserver.
2641 HierarchyObserver o(root_window());
2642 scoped_ptr<Window> w1(CreateTestWindowWithId(1, NULL));
2643 Window* w11 = CreateTestWindowWithId(11, w1.get());
2644 w1->AddObserver(&o);
2645 w11->AddObserver(&o);
2646
2647 // Add.
2648 root_window()->AddChild(w1.get());
2649
2650 // Dispatched to target first.
2651 int index = 0;
2652 WindowObserver::HierarchyChangeParams params;
2653 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2654 params.target = w1.get();
2655 params.old_parent = NULL;
2656 params.new_parent = root_window();
2657 params.receiver = w1.get();
2658 o.ValidateState(index++, params);
2659
2660 // Dispatched to target's children.
2661 params.receiver = w11;
2662 o.ValidateState(index++, params);
2663
2664 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2665
2666 // Now process the "changed" phase.
2667 params.receiver = w1.get();
2668 o.ValidateState(index++, params);
2669 params.receiver = w11;
2670 o.ValidateState(index++, params);
2671 params.receiver = root_window();
2672 o.ValidateState(index++, params);
2673
2674 // Remove.
2675 root_window()->RemoveChild(w1.get());
2676 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2677 params.old_parent = root_window();
2678 params.new_parent = NULL;
2679 params.receiver = w1.get();
2680 o.ValidateState(index++, params);
2681 params.receiver = w11;
2682 o.ValidateState(index++, params);
2683 params.receiver = root_window();
2684 o.ValidateState(index++, params);
2685 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2686 params.receiver = w1.get();
2687 o.ValidateState(index++, params);
2688 params.receiver = w11;
2689 o.ValidateState(index++, params);
2690
2691 w1.reset();
2692 }
2693
2694 {
2695 // Reparent. Tests notification order per documentation in WindowObserver.
2696 scoped_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
2697 Window* w11 = CreateTestWindowWithId(11, w1.get());
2698 Window* w111 = CreateTestWindowWithId(111, w11);
2699 scoped_ptr<Window> w2(CreateTestWindowWithId(2, root_window()));
2700
2701 HierarchyObserver o(root_window());
2702 w1->AddObserver(&o);
2703 w11->AddObserver(&o);
2704 w111->AddObserver(&o);
2705 w2->AddObserver(&o);
2706
2707 w2->AddChild(w11);
2708
2709 // Dispatched to target first.
2710 int index = 0;
2711 WindowObserver::HierarchyChangeParams params;
2712 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
2713 params.target = w11;
2714 params.old_parent = w1.get();
2715 params.new_parent = w2.get();
2716 params.receiver = w11;
2717 o.ValidateState(index++, params);
2718
2719 // Then to target's children.
2720 params.receiver = w111;
2721 o.ValidateState(index++, params);
2722
2723 // Then to target's old parent chain.
2724 params.receiver = w1.get();
2725 o.ValidateState(index++, params);
2726 params.receiver = root_window();
2727 o.ValidateState(index++, params);
2728
2729 // "Changed" phase.
2730 params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
2731 params.receiver = w11;
2732 o.ValidateState(index++, params);
2733 params.receiver = w111;
2734 o.ValidateState(index++, params);
2735 params.receiver = w2.get();
2736 o.ValidateState(index++, params);
2737 params.receiver = root_window();
2738 o.ValidateState(index++, params);
2739
2740 w1.reset();
2741 w2.reset();
2742 }
2743
2744 }
2745
2746 // Verifies SchedulePaint() on a layerless window results in damaging the right
2747 // thing.
2748 TEST_F(WindowTest, LayerlessWindowSchedulePaint) {
2749 Window root(NULL);
2750 root.Init(aura::WINDOW_LAYER_NOT_DRAWN);
2751 root.SetBounds(gfx::Rect(0, 0, 100, 100));
2752
2753 Window* layerless_window = new Window(NULL); // Owned by |root|.
2754 layerless_window->Init(WINDOW_LAYER_NONE);
2755 layerless_window->SetBounds(gfx::Rect(10, 11, 12, 13));
2756 root.AddChild(layerless_window);
2757
2758 root.layer()->SendDamagedRects();
2759 layerless_window->SchedulePaintInRect(gfx::Rect(1, 2, 100, 4));
2760 // Note the the region is clipped by the parent hence 100 going to 11.
2761 EXPECT_EQ("11,13 11x4",
2762 gfx::SkIRectToRect(root.layer()->damaged_region().getBounds()).
2763 ToString());
2764
2765 Window* layerless_window2 = new Window(NULL); // Owned by |layerless_window|.
2766 layerless_window2->Init(WINDOW_LAYER_NONE);
2767 layerless_window2->SetBounds(gfx::Rect(1, 2, 3, 4));
2768 layerless_window->AddChild(layerless_window2);
2769
2770 root.layer()->SendDamagedRects();
2771 layerless_window2->SchedulePaintInRect(gfx::Rect(1, 2, 100, 4));
2772 // Note the the region is clipped by the |layerless_window| hence 100 going to
2773 // 2.
2774 EXPECT_EQ("12,15 2x2",
2775 gfx::SkIRectToRect(root.layer()->damaged_region().getBounds()).
2776 ToString());
2777 }
2778
2779 // Verifies bounds of layerless windows are correctly updated when adding
2780 // removing.
2781 TEST_F(WindowTest, NestedLayerlessWindowsBoundsOnAddRemove) {
2782 // Creates the following structure (all children owned by root):
2783 // root
2784 // w1ll 1,2
2785 // w11ll 3,4
2786 // w111 5,6
2787 // w12 7,8
2788 // w121 9,10
2789 //
2790 // ll: layer less, eg no layer
2791 Window root(NULL);
2792 root.Init(WINDOW_LAYER_NOT_DRAWN);
2793 root.SetBounds(gfx::Rect(0, 0, 100, 100));
2794
2795 Window* w1ll = new Window(NULL);
2796 w1ll->Init(WINDOW_LAYER_NONE);
2797 w1ll->SetBounds(gfx::Rect(1, 2, 100, 100));
2798
2799 Window* w11ll = new Window(NULL);
2800 w11ll->Init(WINDOW_LAYER_NONE);
2801 w11ll->SetBounds(gfx::Rect(3, 4, 100, 100));
2802 w1ll->AddChild(w11ll);
2803
2804 Window* w111 = new Window(NULL);
2805 w111->Init(WINDOW_LAYER_NOT_DRAWN);
2806 w111->SetBounds(gfx::Rect(5, 6, 100, 100));
2807 w11ll->AddChild(w111);
2808
2809 Window* w12 = new Window(NULL);
2810 w12->Init(WINDOW_LAYER_NOT_DRAWN);
2811 w12->SetBounds(gfx::Rect(7, 8, 100, 100));
2812 w1ll->AddChild(w12);
2813
2814 Window* w121 = new Window(NULL);
2815 w121->Init(WINDOW_LAYER_NOT_DRAWN);
2816 w121->SetBounds(gfx::Rect(9, 10, 100, 100));
2817 w12->AddChild(w121);
2818
2819 root.AddChild(w1ll);
2820
2821 // All layers should be parented to the root.
2822 EXPECT_EQ(root.layer(), w111->layer()->parent());
2823 EXPECT_EQ(root.layer(), w12->layer()->parent());
2824 EXPECT_EQ(w12->layer(), w121->layer()->parent());
2825
2826 // Ensure bounds are what we expect.
2827 EXPECT_EQ("1,2 100x100", w1ll->bounds().ToString());
2828 EXPECT_EQ("3,4 100x100", w11ll->bounds().ToString());
2829 EXPECT_EQ("5,6 100x100", w111->bounds().ToString());
2830 EXPECT_EQ("7,8 100x100", w12->bounds().ToString());
2831 EXPECT_EQ("9,10 100x100", w121->bounds().ToString());
2832
2833 // Bounds of layers are relative to the nearest ancestor with a layer.
2834 EXPECT_EQ("8,10 100x100", w12->layer()->bounds().ToString());
2835 EXPECT_EQ("9,12 100x100", w111->layer()->bounds().ToString());
2836 EXPECT_EQ("9,10 100x100", w121->layer()->bounds().ToString());
2837
2838 // Remove and repeat.
2839 root.RemoveChild(w1ll);
2840
2841 EXPECT_TRUE(w111->layer()->parent() == NULL);
2842 EXPECT_TRUE(w12->layer()->parent() == NULL);
2843
2844 // Verify bounds haven't changed again.
2845 EXPECT_EQ("1,2 100x100", w1ll->bounds().ToString());
2846 EXPECT_EQ("3,4 100x100", w11ll->bounds().ToString());
2847 EXPECT_EQ("5,6 100x100", w111->bounds().ToString());
2848 EXPECT_EQ("7,8 100x100", w12->bounds().ToString());
2849 EXPECT_EQ("9,10 100x100", w121->bounds().ToString());
2850
2851 // Bounds of layers should now match that of windows.
2852 EXPECT_EQ("7,8 100x100", w12->layer()->bounds().ToString());
2853 EXPECT_EQ("5,6 100x100", w111->layer()->bounds().ToString());
2854 EXPECT_EQ("9,10 100x100", w121->layer()->bounds().ToString());
2855
2856 delete w1ll;
2857 }
2858
2859 // Verifies bounds of layerless windows are correctly updated when bounds
2860 // of ancestor changes.
2861 TEST_F(WindowTest, NestedLayerlessWindowsBoundsOnSetBounds) {
2862 // Creates the following structure (all children owned by root):
2863 // root
2864 // w1ll 1,2
2865 // w11ll 3,4
2866 // w111 5,6
2867 // w12 7,8
2868 // w121 9,10
2869 //
2870 // ll: layer less, eg no layer
2871 Window root(NULL);
2872 root.Init(WINDOW_LAYER_NOT_DRAWN);
2873 root.SetBounds(gfx::Rect(0, 0, 100, 100));
2874
2875 Window* w1ll = new Window(NULL);
2876 w1ll->Init(WINDOW_LAYER_NONE);
2877 w1ll->SetBounds(gfx::Rect(1, 2, 100, 100));
2878
2879 Window* w11ll = new Window(NULL);
2880 w11ll->Init(WINDOW_LAYER_NONE);
2881 w11ll->SetBounds(gfx::Rect(3, 4, 100, 100));
2882 w1ll->AddChild(w11ll);
2883
2884 Window* w111 = new Window(NULL);
2885 w111->Init(WINDOW_LAYER_NOT_DRAWN);
2886 w111->SetBounds(gfx::Rect(5, 6, 100, 100));
2887 w11ll->AddChild(w111);
2888
2889 Window* w12 = new Window(NULL);
2890 w12->Init(WINDOW_LAYER_NOT_DRAWN);
2891 w12->SetBounds(gfx::Rect(7, 8, 100, 100));
2892 w1ll->AddChild(w12);
2893
2894 Window* w121 = new Window(NULL);
2895 w121->Init(WINDOW_LAYER_NOT_DRAWN);
2896 w121->SetBounds(gfx::Rect(9, 10, 100, 100));
2897 w12->AddChild(w121);
2898
2899 root.AddChild(w1ll);
2900
2901 w111->SetBounds(gfx::Rect(7, 8, 11, 12));
2902 EXPECT_EQ("7,8 11x12", w111->bounds().ToString());
2903 EXPECT_EQ("7,8 11x12", w111->GetTargetBounds().ToString());
2904 EXPECT_EQ("11,14 11x12", w111->layer()->bounds().ToString());
2905
2906 // Set back.
2907 w111->SetBounds(gfx::Rect(5, 6, 100, 100));
2908 EXPECT_EQ("5,6 100x100", w111->bounds().ToString());
2909 EXPECT_EQ("5,6 100x100", w111->GetTargetBounds().ToString());
2910 EXPECT_EQ("9,12 100x100", w111->layer()->bounds().ToString());
2911
2912 // Setting the bounds of a layerless window needs to adjust the bounds of
2913 // layered children.
2914 w11ll->SetBounds(gfx::Rect(5, 6, 100, 100));
2915 EXPECT_EQ("5,6 100x100", w11ll->bounds().ToString());
2916 EXPECT_EQ("5,6 100x100", w11ll->GetTargetBounds().ToString());
2917 EXPECT_EQ("5,6 100x100", w111->bounds().ToString());
2918 EXPECT_EQ("5,6 100x100", w111->GetTargetBounds().ToString());
2919 EXPECT_EQ("11,14 100x100", w111->layer()->bounds().ToString());
2920
2921 root.RemoveChild(w1ll);
2922
2923 w111->SetBounds(gfx::Rect(7, 8, 11, 12));
2924 EXPECT_EQ("7,8 11x12", w111->bounds().ToString());
2925 EXPECT_EQ("7,8 11x12", w111->GetTargetBounds().ToString());
2926 EXPECT_EQ("7,8 11x12", w111->layer()->bounds().ToString());
2927
2928 delete w1ll;
2929 }
2930
2931 namespace {
2932
2933 // Tracks the number of times paint is invoked along with what the clip and
2934 // translate was.
2935 class PaintWindowDelegate : public TestWindowDelegate {
2936 public:
2937 PaintWindowDelegate() : paint_count_(0) {}
2938 virtual ~PaintWindowDelegate() {}
2939
2940 const gfx::Rect& most_recent_paint_clip_bounds() const {
2941 return most_recent_paint_clip_bounds_;
2942 }
2943
2944 const gfx::Vector2d& most_recent_paint_matrix_offset() const {
2945 return most_recent_paint_matrix_offset_;
2946 }
2947
2948 void clear_paint_count() { paint_count_ = 0; }
2949 int paint_count() const { return paint_count_; }
2950
2951 // TestWindowDelegate::
2952 virtual void OnPaint(gfx::Canvas* canvas) override {
2953 paint_count_++;
2954 canvas->GetClipBounds(&most_recent_paint_clip_bounds_);
2955 const SkMatrix& matrix = canvas->sk_canvas()->getTotalMatrix();
2956 most_recent_paint_matrix_offset_ = gfx::Vector2d(
2957 SkScalarFloorToInt(matrix.getTranslateX()),
2958 SkScalarFloorToInt(matrix.getTranslateY()));
2959 }
2960
2961 private:
2962 int paint_count_;
2963 gfx::Rect most_recent_paint_clip_bounds_;
2964 gfx::Vector2d most_recent_paint_matrix_offset_;
2965
2966 DISALLOW_COPY_AND_ASSIGN(PaintWindowDelegate);
2967 };
2968
2969 } // namespace
2970
2971 // Assertions around layerless children being painted when non-layerless window
2972 // is painted.
2973 TEST_F(WindowTest, PaintLayerless) {
2974 // Creates the following structure (all children owned by root):
2975 // root
2976 // w1ll 1,2 40x50
2977 // w11ll 3,4 11x12
2978 // w111 5,6
2979 //
2980 // ll: layer less, eg no layer
2981 PaintWindowDelegate w1ll_delegate;
2982 PaintWindowDelegate w11ll_delegate;
2983 PaintWindowDelegate w111_delegate;
2984
2985 Window root(NULL);
2986 root.Init(WINDOW_LAYER_NOT_DRAWN);
2987 root.SetBounds(gfx::Rect(0, 0, 100, 100));
2988
2989 Window* w1ll = new Window(&w1ll_delegate);
2990 w1ll->Init(WINDOW_LAYER_NONE);
2991 w1ll->SetBounds(gfx::Rect(1, 2, 40, 50));
2992 w1ll->Show();
2993 root.AddChild(w1ll);
2994
2995 Window* w11ll = new Window(&w11ll_delegate);
2996 w11ll->Init(WINDOW_LAYER_NONE);
2997 w11ll->SetBounds(gfx::Rect(3, 4, 11, 12));
2998 w11ll->Show();
2999 w1ll->AddChild(w11ll);
3000
3001 Window* w111 = new Window(&w111_delegate);
3002 w111->Init(WINDOW_LAYER_NOT_DRAWN);
3003 w111->SetBounds(gfx::Rect(5, 6, 100, 100));
3004 w111->Show();
3005 w11ll->AddChild(w111);
3006
3007 EXPECT_EQ(0, w1ll_delegate.paint_count());
3008 EXPECT_EQ(0, w11ll_delegate.paint_count());
3009 EXPECT_EQ(0, w111_delegate.paint_count());
3010
3011 // Paint the root, this should trigger painting of the two layerless
3012 // descendants but not the layered descendant.
3013 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true);
3014 static_cast<ui::LayerDelegate&>(root).OnPaintLayer(&canvas);
3015
3016 // NOTE: SkCanvas::getClipBounds() extends the clip 1 pixel to the left and up
3017 // and 2 pixels down and to the right.
3018 EXPECT_EQ(1, w1ll_delegate.paint_count());
3019 EXPECT_EQ("-1,-1 42x52",
3020 w1ll_delegate.most_recent_paint_clip_bounds().ToString());
3021 EXPECT_EQ("[1 2]",
3022 w1ll_delegate.most_recent_paint_matrix_offset().ToString());
3023 EXPECT_EQ(1, w11ll_delegate.paint_count());
3024 EXPECT_EQ("-1,-1 13x14",
3025 w11ll_delegate.most_recent_paint_clip_bounds().ToString());
3026 EXPECT_EQ("[4 6]",
3027 w11ll_delegate.most_recent_paint_matrix_offset().ToString());
3028 EXPECT_EQ(0, w111_delegate.paint_count());
3029 }
3030
3031 namespace {
3032
3033 std::string ConvertPointToTargetString(const Window* source,
3034 const Window* target) {
3035 gfx::Point location;
3036 Window::ConvertPointToTarget(source, target, &location);
3037 return location.ToString();
3038 }
3039
3040 } // namespace
3041
3042 // Assertions around Window::ConvertPointToTarget() with layerless windows.
3043 TEST_F(WindowTest, ConvertPointToTargetLayerless) {
3044 // Creates the following structure (all children owned by root):
3045 // root
3046 // w1ll 1,2
3047 // w11ll 3,4
3048 // w111 5,6
3049 // w12 7,8
3050 // w121 9,10
3051 //
3052 // ll: layer less, eg no layer
3053 Window root(NULL);
3054 root.Init(WINDOW_LAYER_NOT_DRAWN);
3055 root.SetBounds(gfx::Rect(0, 0, 100, 100));
3056
3057 Window* w1ll = new Window(NULL);
3058 w1ll->Init(WINDOW_LAYER_NONE);
3059 w1ll->SetBounds(gfx::Rect(1, 2, 100, 100));
3060
3061 Window* w11ll = new Window(NULL);
3062 w11ll->Init(WINDOW_LAYER_NONE);
3063 w11ll->SetBounds(gfx::Rect(3, 4, 100, 100));
3064 w1ll->AddChild(w11ll);
3065
3066 Window* w111 = new Window(NULL);
3067 w111->Init(WINDOW_LAYER_NOT_DRAWN);
3068 w111->SetBounds(gfx::Rect(5, 6, 100, 100));
3069 w11ll->AddChild(w111);
3070
3071 Window* w12 = new Window(NULL);
3072 w12->Init(WINDOW_LAYER_NOT_DRAWN);
3073 w12->SetBounds(gfx::Rect(7, 8, 100, 100));
3074 w1ll->AddChild(w12);
3075
3076 Window* w121 = new Window(NULL);
3077 w121->Init(WINDOW_LAYER_NOT_DRAWN);
3078 w121->SetBounds(gfx::Rect(9, 10, 100, 100));
3079 w12->AddChild(w121);
3080
3081 root.AddChild(w1ll);
3082
3083 // w111->w11ll
3084 EXPECT_EQ("5,6", ConvertPointToTargetString(w111, w11ll));
3085
3086 // w111->w1ll
3087 EXPECT_EQ("8,10", ConvertPointToTargetString(w111, w1ll));
3088
3089 // w111->root
3090 EXPECT_EQ("9,12", ConvertPointToTargetString(w111, &root));
3091
3092 // w111->w12
3093 EXPECT_EQ("1,2", ConvertPointToTargetString(w111, w12));
3094
3095 // w111->w121
3096 EXPECT_EQ("-8,-8", ConvertPointToTargetString(w111, w121));
3097
3098 // w11ll->w111
3099 EXPECT_EQ("-5,-6", ConvertPointToTargetString(w11ll, w111));
3100
3101 // w11ll->w11ll
3102 EXPECT_EQ("3,4", ConvertPointToTargetString(w11ll, w1ll));
3103
3104 // w11ll->root
3105 EXPECT_EQ("4,6", ConvertPointToTargetString(w11ll, &root));
3106
3107 // w11ll->w12
3108 EXPECT_EQ("-4,-4", ConvertPointToTargetString(w11ll, w12));
3109 }
3110
3111 #if !defined(NDEBUG)
3112 // Verifies PrintWindowHierarchy() doesn't crash with a layerless window.
3113 TEST_F(WindowTest, PrintWindowHierarchyNotCrashLayerless) {
3114 Window root(NULL);
3115 root.Init(WINDOW_LAYER_NONE);
3116 root.SetBounds(gfx::Rect(0, 0, 100, 100));
3117 root.PrintWindowHierarchy(0);
3118 }
3119 #endif
3120
3121 namespace {
3122
3123 // See AddWindowsFromString() for details.
3124 aura::Window* CreateWindowFromDescription(const std::string& description,
3125 WindowDelegate* delegate) {
3126 WindowLayerType window_type = WINDOW_LAYER_NOT_DRAWN;
3127 std::vector<std::string> tokens;
3128 Tokenize(description, ":", &tokens);
3129 DCHECK(!tokens.empty());
3130 std::string name(tokens[0]);
3131 tokens.erase(tokens.begin());
3132 if (!tokens.empty()) {
3133 if (tokens[0] == "ll") {
3134 window_type = WINDOW_LAYER_NONE;
3135 tokens.erase(tokens.begin());
3136 }
3137 DCHECK(tokens.empty()) << "unknown tokens for creating window "
3138 << description;
3139 }
3140 Window* window = new Window(delegate);
3141 window->Init(window_type);
3142 window->SetName(name);
3143 // Window name is only propagated to layer in debug builds.
3144 if (window->layer())
3145 window->layer()->set_name(name);
3146 return window;
3147 }
3148
3149 // Creates and adds a tree of windows to |parent|. |description| consists
3150 // of the following pieces:
3151 // X: Identifies a new window. Consists of a name and optionally ":ll" to
3152 // specify WINDOW_LAYER_NONE, eg "w1:ll".
3153 // []: optionally used to specify the children of the window. Contains any
3154 // number of window identifiers and their corresponding children.
3155 // For example: "[ a [ a1 a2:ll ] b c [ c1 ] ]" creates the tree:
3156 // a
3157 // a1
3158 // a2 -> WINDOW_LAYER_NONE.
3159 // b
3160 // c
3161 // c1
3162 // NOTE: you must have a space after every token.
3163 std::string::size_type AddWindowsFromString(aura::Window* parent,
3164 const std::string& description,
3165 std::string::size_type start_pos,
3166 WindowDelegate* delegate) {
3167 DCHECK(parent);
3168 std::string::size_type end_pos = description.find(' ', start_pos);
3169 while (end_pos != std::string::npos) {
3170 const std::string::size_type part_length = end_pos - start_pos;
3171 const std::string window_description =
3172 description.substr(start_pos, part_length);
3173 if (window_description == "[") {
3174 start_pos = AddWindowsFromString(parent->children().back(),
3175 description,
3176 end_pos + 1,
3177 delegate);
3178 end_pos = description.find(' ', start_pos);
3179 if (end_pos == std::string::npos && start_pos != end_pos)
3180 end_pos = description.length();
3181 } else if (window_description == "]") {
3182 ++end_pos;
3183 break;
3184 } else {
3185 Window* window =
3186 CreateWindowFromDescription(window_description, delegate);
3187 parent->AddChild(window);
3188 start_pos = ++end_pos;
3189 end_pos = description.find(' ', start_pos);
3190 }
3191 }
3192 return end_pos;
3193 }
3194
3195 // Used by BuildRootWindowTreeDescription().
3196 std::string BuildWindowTreeDescription(const aura::Window& window) {
3197 std::string result;
3198 result += window.name();
3199 if (window.children().empty())
3200 return result;
3201
3202 result += " [ ";
3203 for (size_t i = 0; i < window.children().size(); ++i) {
3204 if (i != 0)
3205 result += " ";
3206 result += BuildWindowTreeDescription(*(window.children()[i]));
3207 }
3208 result += " ]";
3209 return result;
3210 }
3211
3212 // Creates a string from |window|. See AddWindowsFromString() for details of the
3213 // returned string. This does *not* include the layer type in the description,
3214 // on the name.
3215 std::string BuildRootWindowTreeDescription(const aura::Window& window) {
3216 std::string result;
3217 for (size_t i = 0; i < window.children().size(); ++i) {
3218 if (i != 0)
3219 result += " ";
3220 result += BuildWindowTreeDescription(*(window.children()[i]));
3221 }
3222 return result;
3223 }
3224
3225 // Used by BuildRootWindowTreeDescription().
3226 std::string BuildLayerTreeDescription(const ui::Layer& layer) {
3227 std::string result;
3228 result += layer.name();
3229 if (layer.children().empty())
3230 return result;
3231
3232 result += " [ ";
3233 for (size_t i = 0; i < layer.children().size(); ++i) {
3234 if (i != 0)
3235 result += " ";
3236 result += BuildLayerTreeDescription(*(layer.children()[i]));
3237 }
3238 result += " ]";
3239 return result;
3240 }
3241
3242 // Builds a string for all the children of |layer|. The returned string is in
3243 // the same format as AddWindowsFromString() but only includes the name of the
3244 // layers.
3245 std::string BuildRootLayerTreeDescription(const ui::Layer& layer) {
3246 std::string result;
3247 for (size_t i = 0; i < layer.children().size(); ++i) {
3248 if (i != 0)
3249 result += " ";
3250 result += BuildLayerTreeDescription(*(layer.children()[i]));
3251 }
3252 return result;
3253 }
3254
3255 // Returns the first window whose name matches |name| in |parent|.
3256 aura::Window* FindWindowByName(aura::Window* parent,
3257 const std::string& name) {
3258 if (parent->name() == name)
3259 return parent;
3260 for (size_t i = 0; i < parent->children().size(); ++i) {
3261 aura::Window* child = FindWindowByName(parent->children()[i], name);
3262 if (child)
3263 return child;
3264 }
3265 return NULL;
3266 }
3267
3268 } // namespace
3269
3270 // Direction to stack.
3271 enum StackType {
3272 STACK_ABOVE,
3273 STACK_BELOW,
3274 STACK_AT_BOTTOM,
3275 STACK_AT_TOP,
3276 };
3277
3278 // Permutations of StackChildAt with various data.
3279 TEST_F(WindowTest, StackChildAtLayerless) {
3280 struct TestData {
3281 // Describes the window tree to create. See AddWindowsFromString() for
3282 // details.
3283 const std::string initial_description;
3284
3285 // Identifies the window to move.
3286 const std::string source_window;
3287
3288 // Window to move |source_window| relative to. Not used for STACK_AT_BOTTOM
3289 // or STACK_AT_TOP.
3290 const std::string target_window;
3291
3292 StackType stack_type;
3293
3294 // Expected window and layer results.
3295 const std::string expected_description;
3296 const std::string expected_layer_description;
3297 } data[] = {
3298 // 1 at top.
3299 {
3300 "1:ll [ 11 12 ] 2:ll [ 21 ]",
3301 "1",
3302 "",
3303 STACK_AT_TOP,
3304 "2 [ 21 ] 1 [ 11 12 ]",
3305 "21 11 12",
3306 },
3307
3308 // 1 at bottom.
3309 {
3310 "1:ll [ 11 12 ] 2:ll [ 21 ]",
3311 "1",
3312 "",
3313 STACK_AT_BOTTOM,
3314 "1 [ 11 12 ] 2 [ 21 ]",
3315 "11 12 21",
3316 },
3317
3318 // 2 at bottom.
3319 {
3320 "1:ll [ 11 12 ] 2:ll [ 21 ]",
3321 "2",
3322 "",
3323 STACK_AT_BOTTOM,
3324 "2 [ 21 ] 1 [ 11 12 ]",
3325 "21 11 12",
3326 },
3327
3328 // 3 below 2.
3329 {
3330 "1:ll [ 11 12 ] 2:ll [ 21 ] 3:ll",
3331 "3",
3332 "2",
3333 STACK_BELOW,
3334 "1 [ 11 12 ] 3 2 [ 21 ]",
3335 "11 12 21",
3336 },
3337
3338 // 2 below 1.
3339 {
3340 "1:ll [ 11 12 ] 2:ll [ 21 ]",
3341 "2",
3342 "1",
3343 STACK_BELOW,
3344 "2 [ 21 ] 1 [ 11 12 ]",
3345 "21 11 12",
3346 },
3347
3348 // 1 above 3.
3349 {
3350 "1:ll [ 11 12 ] 2:ll [ 21 ] 3:ll",
3351 "1",
3352 "3",
3353 STACK_ABOVE,
3354 "2 [ 21 ] 3 1 [ 11 12 ]",
3355 "21 11 12",
3356 },
3357
3358 // 1 above 2.
3359 {
3360 "1:ll [ 11 12 ] 2:ll [ 21 ]",
3361 "1",
3362 "2",
3363 STACK_ABOVE,
3364 "2 [ 21 ] 1 [ 11 12 ]",
3365 "21 11 12",
3366 },
3367 };
3368 for (size_t i = 0; i < arraysize(data); ++i) {
3369 test::TestWindowDelegate delegate;
3370 Window root(NULL);
3371 root.Init(WINDOW_LAYER_NOT_DRAWN);
3372 root.SetBounds(gfx::Rect(0, 0, 100, 100));
3373 AddWindowsFromString(
3374 &root,
3375 data[i].initial_description,
3376 static_cast<std::string::size_type>(0), &delegate);
3377 aura::Window* source = FindWindowByName(&root, data[i].source_window);
3378 ASSERT_TRUE(source != NULL) << "unable to find source window "
3379 << data[i].source_window << " at " << i;
3380 aura::Window* target = FindWindowByName(&root, data[i].target_window);
3381 switch (data[i].stack_type) {
3382 case STACK_ABOVE:
3383 ASSERT_TRUE(target != NULL) << "unable to find target window "
3384 << data[i].target_window << " at " << i;
3385 source->parent()->StackChildAbove(source, target);
3386 break;
3387 case STACK_BELOW:
3388 ASSERT_TRUE(target != NULL) << "unable to find target window "
3389 << data[i].target_window << " at " << i;
3390 source->parent()->StackChildBelow(source, target);
3391 break;
3392 case STACK_AT_BOTTOM:
3393 source->parent()->StackChildAtBottom(source);
3394 break;
3395 case STACK_AT_TOP:
3396 source->parent()->StackChildAtTop(source);
3397 break;
3398 }
3399 EXPECT_EQ(data[i].expected_layer_description,
3400 BuildRootLayerTreeDescription(*root.layer()))
3401 << "layer tree doesn't match at " << i;
3402 EXPECT_EQ(data[i].expected_description,
3403 BuildRootWindowTreeDescription(root))
3404 << "window tree doesn't match at " << i;
3405 }
3406 }
3407
3408 namespace {
3409
3410 class TestLayerAnimationObserver : public ui::LayerAnimationObserver {
3411 public:
3412 TestLayerAnimationObserver()
3413 : animation_completed_(false),
3414 animation_aborted_(false) {}
3415 virtual ~TestLayerAnimationObserver() {}
3416
3417 bool animation_completed() const { return animation_completed_; }
3418 bool animation_aborted() const { return animation_aborted_; }
3419
3420 void Reset() {
3421 animation_completed_ = false;
3422 animation_aborted_ = false;
3423 }
3424
3425 private:
3426 // ui::LayerAnimationObserver:
3427 virtual void OnLayerAnimationEnded(
3428 ui::LayerAnimationSequence* sequence) override {
3429 animation_completed_ = true;
3430 }
3431
3432 virtual void OnLayerAnimationAborted(
3433 ui::LayerAnimationSequence* sequence) override {
3434 animation_aborted_ = true;
3435 }
3436
3437 virtual void OnLayerAnimationScheduled(
3438 ui::LayerAnimationSequence* sequence) override {
3439 }
3440
3441 bool animation_completed_;
3442 bool animation_aborted_;
3443
3444 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationObserver);
3445 };
3446
3447 }
3448
3449 TEST_F(WindowTest, WindowDestroyCompletesAnimations) {
3450 ui::ScopedAnimationDurationScaleMode test_duration_mode(
3451 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
3452 scoped_refptr<ui::LayerAnimator> animator =
3453 ui::LayerAnimator::CreateImplicitAnimator();
3454 TestLayerAnimationObserver observer;
3455 animator->AddObserver(&observer);
3456 // Make sure destroying a Window completes the animation.
3457 {
3458 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
3459 window->layer()->SetAnimator(animator.get());
3460
3461 gfx::Transform transform;
3462 transform.Scale(0.5f, 0.5f);
3463 window->SetTransform(transform);
3464
3465 EXPECT_TRUE(animator->is_animating());
3466 EXPECT_FALSE(observer.animation_completed());
3467 }
3468 EXPECT_TRUE(animator.get());
3469 EXPECT_FALSE(animator->is_animating());
3470 EXPECT_TRUE(observer.animation_completed());
3471 EXPECT_FALSE(observer.animation_aborted());
3472 animator->RemoveObserver(&observer);
3473 observer.Reset();
3474
3475 animator = ui::LayerAnimator::CreateImplicitAnimator();
3476 animator->AddObserver(&observer);
3477 ui::Layer layer;
3478 layer.SetAnimator(animator.get());
3479 {
3480 scoped_ptr<Window> window(CreateTestWindowWithId(1, root_window()));
3481 window->layer()->Add(&layer);
3482
3483 gfx::Transform transform;
3484 transform.Scale(0.5f, 0.5f);
3485 layer.SetTransform(transform);
3486
3487 EXPECT_TRUE(animator->is_animating());
3488 EXPECT_FALSE(observer.animation_completed());
3489 }
3490
3491 EXPECT_TRUE(animator.get());
3492 EXPECT_FALSE(animator->is_animating());
3493 EXPECT_TRUE(observer.animation_completed());
3494 EXPECT_FALSE(observer.animation_aborted());
3495 animator->RemoveObserver(&observer);
3496 }
3497
3498 } // namespace test
3499 } // namespace aura
OLDNEW
« no previous file with comments | « ui/aura/window_tree_host_x11.cc ('k') | ui/base/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698