summaryrefslogtreecommitdiffstats
path: root/components/mus/public/cpp/tests/window_tree_client_impl_unittest.cc
blob: 909c873ae0c8e3c2f6097136c29adcacc31c5683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/mus/public/cpp/lib/window_tree_client_impl.h"

#include <stdint.h>

#include "base/logging.h"
#include "base/macros.h"
#include "components/mus/common/util.h"
#include "components/mus/public/cpp/input_event_handler.h"
#include "components/mus/public/cpp/lib/window_private.h"
#include "components/mus/public/cpp/property_type_converters.h"
#include "components/mus/public/cpp/tests/test_window.h"
#include "components/mus/public/cpp/tests/test_window_tree.h"
#include "components/mus/public/cpp/window.h"
#include "components/mus/public/cpp/window_observer.h"
#include "components/mus/public/cpp/window_property.h"
#include "components/mus/public/cpp/window_tracker.h"
#include "components/mus/public/cpp/window_tree_delegate.h"
#include "mojo/common/common_type_converters.h"
#include "mojo/converters/geometry/geometry_type_converters.h"
#include "mojo/converters/input_events/input_events_type_converters.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/geometry/rect.h"

namespace {

void DoNothingBool(bool result) {}

}  // namespace

namespace mus {

mojo::Array<uint8_t> Int32ToPropertyTransportValue(int32_t value) {
  const std::vector<uint8_t> bytes =
      mojo::TypeConverter<const std::vector<uint8_t>, int32_t>::Convert(value);
  mojo::Array<uint8_t> transport_value;
  transport_value.resize(bytes.size());
  memcpy(&transport_value.front(), &(bytes.front()), bytes.size());
  return transport_value;
}

class TestWindowTreeDelegate : public WindowTreeDelegate {
 public:
  TestWindowTreeDelegate() {}
  ~TestWindowTreeDelegate() override {}

  // WindowTreeDelegate:
  void OnEmbed(Window* root) override {}
  void OnConnectionLost(WindowTreeConnection* connection) override {}

 private:
  DISALLOW_COPY_AND_ASSIGN(TestWindowTreeDelegate);
};

class WindowTreeClientImplPrivate {
 public:
  WindowTreeClientImplPrivate(WindowTreeClientImpl* tree_client_impl)
      : tree_client_impl_(tree_client_impl) {}
  ~WindowTreeClientImplPrivate() {}

  void Init(mojom::WindowTree* window_tree) {
    mojom::WindowDataPtr root_data(mojom::WindowData::New());
    root_data->parent_id = 0;
    root_data->window_id = 1;
    root_data->bounds = mojo::Rect::From(gfx::Rect());
    root_data->properties.SetToEmpty();
    root_data->visible = true;
    root_data->drawn = true;
    root_data->viewport_metrics = mojom::ViewportMetrics::New();
    root_data->viewport_metrics->size_in_pixels =
        mojo::Size::From(gfx::Size(1000, 1000));
    root_data->viewport_metrics->device_pixel_ratio = 1;
    tree_client_impl_->OnEmbedImpl(window_tree, 1, std::move(root_data), 0);
  }

 private:
  WindowTreeClientImpl* tree_client_impl_;

  DISALLOW_COPY_AND_ASSIGN(WindowTreeClientImplPrivate);
};

class WindowTreeSetup {
 public:
  WindowTreeSetup() : tree_client_(&window_tree_delegate_, nullptr, nullptr) {
    WindowTreeClientImplPrivate(&tree_client_).Init(&window_tree_);
    window_tree_.GetAndClearChangeId(nullptr);
  }

  WindowTreeConnection* window_tree_connection() {
    return static_cast<WindowTreeConnection*>(&tree_client_);
  }

  mojom::WindowTreeClient* window_tree_client() {
    return static_cast<mojom::WindowTreeClient*>(&tree_client_);
  }

  TestWindowTree* window_tree() { return &window_tree_; }

  Window* GetFirstRoot() {
    return window_tree_connection()->GetRoots().empty()
               ? nullptr
               : *window_tree_connection()->GetRoots().begin();
  }

 private:
  TestWindowTree window_tree_;
  TestWindowTreeDelegate window_tree_delegate_;
  WindowTreeClientImpl tree_client_;

  DISALLOW_COPY_AND_ASSIGN(WindowTreeSetup);
};

class TestInputEventHandler : public InputEventHandler {
 public:
  TestInputEventHandler()
      : received_event_(false), should_manually_ack_(false) {}
  ~TestInputEventHandler() override {}

  void set_should_manually_ack() { should_manually_ack_ = true; }

  void AckEvent() {
    DCHECK(should_manually_ack_);
    DCHECK(!ack_callback_.is_null());
    ack_callback_.Run(true);
    ack_callback_ = base::Bind(&::DoNothingBool);
  }

  void Reset() {
    received_event_ = false;
    ack_callback_ = base::Bind(&::DoNothingBool);
  }
  bool received_event() const { return received_event_; }

 private:
  // InputEventHandler:
  void OnWindowInputEvent(
      Window* target,
      const ui::Event& event,
      scoped_ptr<base::Callback<void(bool)>>* ack_callback) override {
    EXPECT_FALSE(received_event_)
        << "Observer was not reset after receiving event.";
    received_event_ = true;
    if (should_manually_ack_) {
      ack_callback_ = *ack_callback->get();
      ack_callback->reset();
    }
  }

  bool received_event_;
  bool should_manually_ack_;
  base::Callback<void(bool)> ack_callback_;

  DISALLOW_COPY_AND_ASSIGN(TestInputEventHandler);
};

using WindowTreeClientImplTest = testing::Test;

// Verifies bounds are reverted if the server replied that the change failed.
TEST_F(WindowTreeClientImplTest, SetBoundsFailed) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  const gfx::Rect original_bounds(root->bounds());
  const gfx::Rect new_bounds(gfx::Rect(0, 0, 100, 100));
  ASSERT_NE(new_bounds, root->bounds());
  root->SetBounds(new_bounds);
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_EQ(original_bounds, root->bounds());
}

// Simulates a bounds change, and while the bounds change is in flight the
// server replies with a new bounds and the original bounds change fails.
TEST_F(WindowTreeClientImplTest, SetBoundsFailedWithPendingChange) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  const gfx::Rect original_bounds(root->bounds());
  const gfx::Rect new_bounds(gfx::Rect(0, 0, 100, 100));
  ASSERT_NE(new_bounds, root->bounds());
  root->SetBounds(new_bounds);
  EXPECT_EQ(new_bounds, root->bounds());
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));

  // Simulate the server responding with a bounds change.
  const gfx::Rect server_changed_bounds(gfx::Rect(0, 0, 101, 102));
  setup.window_tree_client()->OnWindowBoundsChanged(
      root->id(), mojo::Rect::From(original_bounds),
      mojo::Rect::From(server_changed_bounds));

  // This shouldn't trigger the bounds changing yet.
  EXPECT_EQ(new_bounds, root->bounds());

  // Tell the client the change failed, which should trigger failing to the
  // most recent bounds from server.
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_EQ(server_changed_bounds, root->bounds());

  // Simulate server changing back to original bounds. Should take immediately.
  setup.window_tree_client()->OnWindowBoundsChanged(
      root->id(), mojo::Rect::From(server_changed_bounds),
      mojo::Rect::From(original_bounds));
  EXPECT_EQ(original_bounds, root->bounds());
}

TEST_F(WindowTreeClientImplTest, TwoInFlightBoundsChangesBothCanceled) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  const gfx::Rect original_bounds(root->bounds());
  const gfx::Rect bounds1(gfx::Rect(0, 0, 100, 100));
  const gfx::Rect bounds2(gfx::Rect(0, 0, 100, 102));
  root->SetBounds(bounds1);
  EXPECT_EQ(bounds1, root->bounds());
  uint32_t change_id1;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id1));

  root->SetBounds(bounds2);
  EXPECT_EQ(bounds2, root->bounds());
  uint32_t change_id2;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id2));

  // Tell the client change 1 failed. As there is a still a change in flight
  // nothing should happen.
  setup.window_tree_client()->OnChangeCompleted(change_id1, false);
  EXPECT_EQ(bounds2, root->bounds());

  // And tell the client change 2 failed too. Should now fallback to original
  // bounds.
  setup.window_tree_client()->OnChangeCompleted(change_id2, false);
  EXPECT_EQ(original_bounds, root->bounds());
}

// Verifies properties are reverted if the server replied that the change
// failed.
TEST_F(WindowTreeClientImplTest, SetPropertyFailed) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  ASSERT_FALSE(root->HasSharedProperty("foo"));
  const int32_t new_value = 11;
  root->SetSharedProperty("foo", new_value);
  ASSERT_TRUE(root->HasSharedProperty("foo"));
  EXPECT_EQ(new_value, root->GetSharedProperty<int32_t>("foo"));
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_FALSE(root->HasSharedProperty("foo"));
}

// Simulates a property change, and while the property change is in flight the
// server replies with a new property and the original property change fails.
TEST_F(WindowTreeClientImplTest, SetPropertyFailedWithPendingChange) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  const int32_t value1 = 11;
  root->SetSharedProperty("foo", value1);
  ASSERT_TRUE(root->HasSharedProperty("foo"));
  EXPECT_EQ(value1, root->GetSharedProperty<int32_t>("foo"));
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));

  // Simulate the server responding with a different value.
  const int32_t server_value = 12;
  setup.window_tree_client()->OnWindowSharedPropertyChanged(
      root->id(), "foo", Int32ToPropertyTransportValue(server_value));

  // This shouldn't trigger the property changing yet.
  ASSERT_TRUE(root->HasSharedProperty("foo"));
  EXPECT_EQ(value1, root->GetSharedProperty<int32_t>("foo"));

  // Tell the client the change failed, which should trigger failing to the
  // most recent value from server.
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  ASSERT_TRUE(root->HasSharedProperty("foo"));
  EXPECT_EQ(server_value, root->GetSharedProperty<int32_t>("foo"));

  // Simulate server changing back to value1. Should take immediately.
  setup.window_tree_client()->OnWindowSharedPropertyChanged(
      root->id(), "foo", Int32ToPropertyTransportValue(value1));
  ASSERT_TRUE(root->HasSharedProperty("foo"));
  EXPECT_EQ(value1, root->GetSharedProperty<int32_t>("foo"));
}

// Verifies visible is reverted if the server replied that the change failed.
TEST_F(WindowTreeClientImplTest, SetVisibleFailed) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  const bool original_visible = root->visible();
  const bool new_visible = !original_visible;
  ASSERT_NE(new_visible, root->visible());
  root->SetVisible(new_visible);
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_EQ(original_visible, root->visible());
}

// Simulates a visible change, and while the visible change is in flight the
// server replies with a new visible and the original visible change fails.
TEST_F(WindowTreeClientImplTest, SetVisibleFailedWithPendingChange) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  const bool original_visible = root->visible();
  const bool new_visible = !original_visible;
  ASSERT_NE(new_visible, root->visible());
  root->SetVisible(new_visible);
  EXPECT_EQ(new_visible, root->visible());
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));

  // Simulate the server responding with a visible change.
  const bool server_changed_visible = !new_visible;
  setup.window_tree_client()->OnWindowVisibilityChanged(root->id(),
                                                        server_changed_visible);

  // This shouldn't trigger visible changing yet.
  EXPECT_EQ(new_visible, root->visible());

  // Tell the client the change failed, which should trigger failing to the
  // most recent visible from server.
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_EQ(server_changed_visible, root->visible());

  // Simulate server changing back to original visible. Should take immediately.
  setup.window_tree_client()->OnWindowVisibilityChanged(root->id(),
                                                        original_visible);
  EXPECT_EQ(original_visible, root->visible());
}

// Verifies |is_modal| is reverted if the server replied that the change failed.
TEST_F(WindowTreeClientImplTest, SetModalFailed) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  EXPECT_FALSE(root->is_modal());
  root->SetModal();
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  EXPECT_TRUE(root->is_modal());
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_FALSE(root->is_modal());
}

TEST_F(WindowTreeClientImplTest, InputEventBasic) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);

  TestInputEventHandler event_handler;
  root->set_input_event_handler(&event_handler);

  scoped_ptr<ui::Event> ui_event(
      new ui::MouseEvent(ui::ET_MOUSE_MOVED, gfx::Point(), gfx::Point(),
                         ui::EventTimeForNow(), ui::EF_NONE, 0));
  setup.window_tree_client()->OnWindowInputEvent(
      1, root->id(), mojom::Event::From(*ui_event.get()));
  EXPECT_TRUE(event_handler.received_event());
  EXPECT_TRUE(setup.window_tree()->WasEventAcked(1));
  event_handler.Reset();

  event_handler.set_should_manually_ack();
  setup.window_tree_client()->OnWindowInputEvent(
      33, root->id(), mojom::Event::From(*ui_event.get()));
  EXPECT_TRUE(event_handler.received_event());
  EXPECT_FALSE(setup.window_tree()->WasEventAcked(33));

  event_handler.AckEvent();
  EXPECT_TRUE(setup.window_tree()->WasEventAcked(33));
}

// Verifies focus is reverted if the server replied that the change failed.
TEST_F(WindowTreeClientImplTest, SetFocusFailed) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  root->SetVisible(true);
  Window* child = setup.window_tree_connection()->NewWindow();
  child->SetVisible(true);
  root->AddChild(child);
  Window* original_focus = setup.window_tree_connection()->GetFocusedWindow();
  Window* new_focus = child;
  ASSERT_NE(new_focus, original_focus);
  new_focus->SetFocus();
  ASSERT_TRUE(new_focus->HasFocus());
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_EQ(original_focus, setup.window_tree_connection()->GetFocusedWindow());
}

// Simulates a focus change, and while the focus change is in flight the server
// replies with a new focus and the original focus change fails.
TEST_F(WindowTreeClientImplTest, SetFocusFailedWithPendingChange) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  root->SetVisible(true);
  Window* child1 = setup.window_tree_connection()->NewWindow();
  child1->SetVisible(true);
  root->AddChild(child1);
  Window* child2 = setup.window_tree_connection()->NewWindow();
  child2->SetVisible(true);
  root->AddChild(child2);
  Window* original_focus = setup.window_tree_connection()->GetFocusedWindow();
  Window* new_focus = child1;
  ASSERT_NE(new_focus, original_focus);
  new_focus->SetFocus();
  ASSERT_TRUE(new_focus->HasFocus());
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));

  // Simulate the server responding with a focus change.
  setup.window_tree_client()->OnWindowFocused(child2->id());

  // This shouldn't trigger focus changing yet.
  EXPECT_TRUE(child1->HasFocus());

  // Tell the client the change failed, which should trigger failing to the
  // most recent focus from server.
  setup.window_tree_client()->OnChangeCompleted(change_id, false);
  EXPECT_FALSE(child1->HasFocus());
  EXPECT_TRUE(child2->HasFocus());
  EXPECT_EQ(child2, setup.window_tree_connection()->GetFocusedWindow());

  // Simulate server changing focus to child1. Should take immediately.
  setup.window_tree_client()->OnWindowFocused(child1->id());
  EXPECT_TRUE(child1->HasFocus());
}

TEST_F(WindowTreeClientImplTest, FocusOnRemovedWindowWithInFlightFocusChange) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  root->SetVisible(true);
  Window* child1 = setup.window_tree_connection()->NewWindow();
  child1->SetVisible(true);
  root->AddChild(child1);
  Window* child2 = setup.window_tree_connection()->NewWindow();
  child2->SetVisible(true);
  root->AddChild(child2);

  child1->SetFocus();
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));

  // Destroy child1, which should set focus to null.
  child1->Destroy();
  EXPECT_EQ(nullptr, setup.window_tree_connection()->GetFocusedWindow());

  // Server changes focus to 2.
  setup.window_tree_client()->OnWindowFocused(child2->id());
  // Shouldn't take immediately.
  EXPECT_FALSE(child2->HasFocus());

  // Ack the change, focus should still be null.
  setup.window_tree_client()->OnChangeCompleted(change_id, true);
  EXPECT_EQ(nullptr, setup.window_tree_connection()->GetFocusedWindow());

  // Change to 2 again, this time it should take.
  setup.window_tree_client()->OnWindowFocused(child2->id());
  EXPECT_TRUE(child2->HasFocus());
}

class ToggleVisibilityFromDestroyedObserver : public WindowObserver {
 public:
  explicit ToggleVisibilityFromDestroyedObserver(Window* window)
      : window_(window) {
    window_->AddObserver(this);
  }

  ToggleVisibilityFromDestroyedObserver() { EXPECT_FALSE(window_); }

  // WindowObserver:
  void OnWindowDestroyed(Window* window) override {
    window_->SetVisible(!window->visible());
    window_->RemoveObserver(this);
    window_ = nullptr;
  }

 private:
  Window* window_;

  DISALLOW_COPY_AND_ASSIGN(ToggleVisibilityFromDestroyedObserver);
};

TEST_F(WindowTreeClientImplTest, ToggleVisibilityFromWindowDestroyed) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);
  Window* child1 = setup.window_tree_connection()->NewWindow();
  root->AddChild(child1);
  ToggleVisibilityFromDestroyedObserver toggler(child1);
  // Destroying the window triggers
  // ToggleVisibilityFromDestroyedObserver::OnWindowDestroyed(), which toggles
  // the visibility of the window. Ack the change, which should not crash or
  // trigger DCHECKs.
  child1->Destroy();
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  setup.window_tree_client()->OnChangeCompleted(change_id, true);
}

TEST_F(WindowTreeClientImplTest, NewTopLevelWindow) {
  WindowTreeSetup setup;
  Window* root1 = setup.GetFirstRoot();
  ASSERT_TRUE(root1);
  Window* root2 = setup.window_tree_connection()->NewTopLevelWindow(nullptr);
  ASSERT_TRUE(root2);
  ASSERT_NE(root2, root1);
  EXPECT_NE(root2->id(), root1->id());
  EXPECT_EQ(2u, setup.window_tree_connection()->GetRoots().size());
  EXPECT_TRUE(setup.window_tree_connection()->GetRoots().count(root1) > 0u);
  EXPECT_TRUE(setup.window_tree_connection()->GetRoots().count(root2) > 0u);

  // Ack the request to the windowtree to create the new window.
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  EXPECT_EQ(setup.window_tree()->window_id(), root2->id());

  mojom::WindowDataPtr data = mojom::WindowData::New();
  data->window_id = root2->id();
  data->viewport_metrics = mojom::ViewportMetrics::New();
  setup.window_tree_client()->OnTopLevelCreated(change_id, std::move(data));

  // Should not be able to add a top level as a child of another window.
  root1->AddChild(root2);
  ASSERT_EQ(nullptr, root2->parent());

  // Destroy the first root, shouldn't initiate tear down.
  root1->Destroy();
  root1 = nullptr;
  EXPECT_EQ(1u, setup.window_tree_connection()->GetRoots().size());
  EXPECT_TRUE(setup.window_tree_connection()->GetRoots().count(root2) > 0u);
}

TEST_F(WindowTreeClientImplTest, NewTopLevelWindowGetsPropertiesFromData) {
  WindowTreeSetup setup;
  Window* root1 = setup.GetFirstRoot();
  ASSERT_TRUE(root1);
  Window* root2 = setup.window_tree_connection()->NewTopLevelWindow(nullptr);
  ASSERT_TRUE(root2);

  EXPECT_FALSE(root2->IsDrawn());
  EXPECT_FALSE(root2->visible());

  // Ack the request to the windowtree to create the new window.
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  EXPECT_EQ(setup.window_tree()->window_id(), root2->id());

  mojom::WindowDataPtr data = mojom::WindowData::New();
  data->window_id = root2->id();
  data->viewport_metrics = mojom::ViewportMetrics::New();
  data->viewport_metrics->size_in_pixels = mojo::Size::From(gfx::Size(1, 2));
  data->bounds = mojo::Rect::From(gfx::Rect(1, 2, 3, 4));
  data->visible = true;
  data->drawn = true;
  setup.window_tree_client()->OnTopLevelCreated(change_id, std::move(data));

  // Make sure all the properties took.
  EXPECT_TRUE(root2->IsDrawn());
  EXPECT_TRUE(root2->visible());
  EXPECT_EQ(gfx::Size(1, 2),
            root2->viewport_metrics().size_in_pixels.To<gfx::Size>());
  EXPECT_EQ(gfx::Rect(1, 2, 3, 4), root2->bounds());
}

TEST_F(WindowTreeClientImplTest, NewTopLevelWindowGetsAllChangesInFlight) {
  WindowTreeSetup setup;
  Window* root1 = setup.GetFirstRoot();
  ASSERT_TRUE(root1);
  Window* root2 = setup.window_tree_connection()->NewTopLevelWindow(nullptr);
  ASSERT_TRUE(root2);

  EXPECT_FALSE(root2->IsDrawn());
  EXPECT_FALSE(root2->visible());

  // Get the id of the in flight change for creating the new window.
  uint32_t new_window_in_flight_change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(
      &new_window_in_flight_change_id));
  EXPECT_EQ(setup.window_tree()->window_id(), root2->id());

  // Make visibility go from false->true->false. Don't ack immediately.
  root2->SetVisible(true);
  uint32_t vis_in_flight_change_id1;
  ASSERT_TRUE(
      setup.window_tree()->GetAndClearChangeId(&vis_in_flight_change_id1));
  EXPECT_NE(new_window_in_flight_change_id, vis_in_flight_change_id1);
  root2->SetVisible(false);
  uint32_t vis_in_flight_change_id2;
  ASSERT_TRUE(
      setup.window_tree()->GetAndClearChangeId(&vis_in_flight_change_id2));
  EXPECT_NE(vis_in_flight_change_id1, vis_in_flight_change_id2);

  // Change bounds to 5, 6, 7, 8.
  root2->SetBounds(gfx::Rect(5, 6, 7, 8));
  uint32_t bounds_in_flight_change_id;
  ASSERT_TRUE(
      setup.window_tree()->GetAndClearChangeId(&bounds_in_flight_change_id));
  EXPECT_NE(vis_in_flight_change_id2, bounds_in_flight_change_id);

  root2->SetSharedProperty<std::string>("xx", "client_xx");
  uint32_t property_in_flight_change_id;
  ASSERT_TRUE(
      setup.window_tree()->GetAndClearChangeId(&property_in_flight_change_id));
  EXPECT_NE(bounds_in_flight_change_id, property_in_flight_change_id);

  // Ack the new window top level window. Vis and bounds shouldn't change.
  mojom::WindowDataPtr data = mojom::WindowData::New();
  data->window_id = root2->id();
  data->viewport_metrics = mojom::ViewportMetrics::New();
  data->viewport_metrics->size_in_pixels = mojo::Size::From(gfx::Size(1, 2));
  data->bounds = mojo::Rect::From(gfx::Rect(1, 2, 3, 4));
  data->visible = true;
  data->drawn = true;
  data->properties["xx"] = mojo::Array<uint8_t>::From(std::string("server_xx"));
  data->properties["yy"] = mojo::Array<uint8_t>::From(std::string("server_yy"));
  setup.window_tree_client()->OnTopLevelCreated(new_window_in_flight_change_id,
                                                std::move(data));

  // The only value that should take effect is the property for 'yy' as it was
  // not in flight.
  EXPECT_TRUE(WindowPrivate(root2).drawn());
  EXPECT_FALSE(root2->visible());
  EXPECT_EQ(gfx::Size(1, 2),
            root2->viewport_metrics().size_in_pixels.To<gfx::Size>());
  EXPECT_EQ(gfx::Rect(5, 6, 7, 8), root2->bounds());
  EXPECT_EQ(2u, root2->shared_properties().size());
  ASSERT_TRUE(root2->HasSharedProperty("yy"));
  EXPECT_EQ("server_yy", root2->GetSharedProperty<std::string>("yy"));
  ASSERT_TRUE(root2->HasSharedProperty("xx"));
  EXPECT_EQ("client_xx", root2->GetSharedProperty<std::string>("xx"));

  // Tell the client the changes failed. This should cause the values to change
  // to that of the server.
  setup.window_tree_client()->OnChangeCompleted(vis_in_flight_change_id1,
                                                false);
  EXPECT_FALSE(root2->visible());
  setup.window_tree_client()->OnChangeCompleted(vis_in_flight_change_id2,
                                                false);
  EXPECT_TRUE(root2->visible());
  setup.window_tree_client()->OnChangeCompleted(bounds_in_flight_change_id,
                                                false);
  EXPECT_EQ(gfx::Rect(1, 2, 3, 4), root2->bounds());
  setup.window_tree_client()->OnChangeCompleted(property_in_flight_change_id,
                                                false);
  EXPECT_EQ(2u, root2->shared_properties().size());
  ASSERT_TRUE(root2->HasSharedProperty("yy"));
  EXPECT_EQ("server_yy", root2->GetSharedProperty<std::string>("yy"));
  ASSERT_TRUE(root2->HasSharedProperty("xx"));
  EXPECT_EQ("server_xx", root2->GetSharedProperty<std::string>("xx"));
}

// Tests that if the client has multiple unowned windows, and one of them is a
// transient child to another, the  teardown can happen cleanly.
TEST_F(WindowTreeClientImplTest, MultipleUnOwnedWindowsDuringDestruction) {
  scoped_ptr<WindowTreeSetup> setup(new WindowTreeSetup());
  Window* root1 = setup->GetFirstRoot();
  ASSERT_TRUE(root1);
  Window* root2 = setup->window_tree_connection()->NewTopLevelWindow(nullptr);
  ASSERT_TRUE(root2);
  root1->AddTransientWindow(root2);

  WindowTracker tracker;
  tracker.Add(root1);
  tracker.Add(root2);
  setup.reset();
  EXPECT_TRUE(tracker.windows().empty());
}

TEST_F(WindowTreeClientImplTest, TopLevelWindowDestroyedBeforeCreateComplete) {
  WindowTreeSetup setup;
  Window* root1 = setup.GetFirstRoot();
  ASSERT_TRUE(root1);
  Window* root2 = setup.window_tree_connection()->NewTopLevelWindow(nullptr);
  ASSERT_TRUE(root2);
  ASSERT_EQ(2u, setup.window_tree_connection()->GetRoots().size());

  // Get the id of the in flight change for creating the new window.
  uint32_t change_id;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id));
  EXPECT_EQ(setup.window_tree()->window_id(), root2->id());

  mojom::WindowDataPtr data = mojom::WindowData::New();
  data->window_id = root2->id();
  data->viewport_metrics = mojom::ViewportMetrics::New();

  // Destroy the window before the server has a chance to ack the window
  // creation.
  root2->Destroy();
  EXPECT_EQ(1u, setup.window_tree_connection()->GetRoots().size());

  setup.window_tree_client()->OnTopLevelCreated(change_id, std::move(data));
  EXPECT_EQ(1u, setup.window_tree_connection()->GetRoots().size());
}

// Tests both SetCapture and ReleaseCapture, to ensure that Window is properly
// updated on failures.
TEST_F(WindowTreeClientImplTest, ExplicitCapture) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);

  root->SetCapture();
  EXPECT_TRUE(root->HasCapture());
  uint32_t change_id1;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id1));
  setup.window_tree_client()->OnChangeCompleted(change_id1, false);
  EXPECT_FALSE(root->HasCapture());

  root->SetCapture();
  EXPECT_TRUE(root->HasCapture());
  uint32_t change_id2;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id2));
  setup.window_tree_client()->OnChangeCompleted(change_id2, true);
  EXPECT_TRUE(root->HasCapture());

  root->ReleaseCapture();
  EXPECT_FALSE(root->HasCapture());
  uint32_t change_id3;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id3));
  setup.window_tree_client()->OnChangeCompleted(change_id3, false);
  EXPECT_TRUE(root->HasCapture());

  root->ReleaseCapture();
  uint32_t change_id4;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id4));
  setup.window_tree_client()->OnChangeCompleted(change_id4, true);
  EXPECT_FALSE(root->HasCapture());
}

// Tests that when capture is lost, that the window tree updates properly.
TEST_F(WindowTreeClientImplTest, LostCapture) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);

  root->SetCapture();
  EXPECT_TRUE(root->HasCapture());
  uint32_t change_id1;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id1));
  setup.window_tree_client()->OnChangeCompleted(change_id1, true);
  EXPECT_TRUE(root->HasCapture());

  // The second SetCapture should be ignored.
  root->SetCapture();
  uint32_t change_id2;
  ASSERT_FALSE(setup.window_tree()->GetAndClearChangeId(&change_id2));

  setup.window_tree_client()->OnLostCapture(root->id());
  EXPECT_FALSE(root->HasCapture());
}

// Tests that when capture is lost, while there is a release capture request
// inflight, that the revert value of that request is updated correctly.
TEST_F(WindowTreeClientImplTest, LostCaptureDifferentInFlightChange) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  ASSERT_TRUE(root);

  root->SetCapture();
  EXPECT_TRUE(root->HasCapture());
  uint32_t change_id1;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id1));
  setup.window_tree_client()->OnChangeCompleted(change_id1, true);
  EXPECT_TRUE(root->HasCapture());

  // The ReleaseCapture should be updated to the revert of the SetCapture.
  root->ReleaseCapture();
  uint32_t change_id2;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id2));

  setup.window_tree_client()->OnLostCapture(root->id());
  EXPECT_FALSE(root->HasCapture());

  setup.window_tree_client()->OnChangeCompleted(change_id2, false);
  EXPECT_FALSE(root->HasCapture());
}

// Tests that while two windows can inflight capture requests, that the
// WindowTreeClient only identifies one as having the current capture.
TEST_F(WindowTreeClientImplTest, TwoWindowsRequestCapture) {
  WindowTreeSetup setup;
  Window* root = setup.GetFirstRoot();
  Window* child = setup.window_tree_connection()->NewWindow();
  child->SetVisible(true);
  root->AddChild(child);

  root->SetCapture();
  EXPECT_TRUE(root->HasCapture());
  uint32_t change_id1;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id1));

  child->SetCapture();
  EXPECT_TRUE(child->HasCapture());
  EXPECT_FALSE(root->HasCapture());

  uint32_t change_id2;
  ASSERT_TRUE(setup.window_tree()->GetAndClearChangeId(&change_id2));

  setup.window_tree_client()->OnChangeCompleted(change_id1, true);
  EXPECT_FALSE(root->HasCapture());
  EXPECT_TRUE(child->HasCapture());

  setup.window_tree_client()->OnChangeCompleted(change_id2, false);
  EXPECT_FALSE(child->HasCapture());
  EXPECT_TRUE(root->HasCapture());

  setup.window_tree_client()->OnLostCapture(root->id());
  EXPECT_FALSE(root->HasCapture());
}

}  // namespace mus