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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
|
// Copyright 2014 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 <stddef.h>
#include <stdint.h>
#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "components/mus/public/interfaces/window_tree.mojom.h"
#include "components/mus/public/interfaces/window_tree_host.mojom.h"
#include "components/mus/ws/ids.h"
#include "components/mus/ws/test_change_tracker.h"
#include "mojo/converters/geometry/geometry_type_converters.h"
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "mojo/shell/public/cpp/application_test_base.h"
using mojo::Array;
using mojo::Callback;
using mojo::Connection;
using mojo::InterfaceRequest;
using mojo::RectPtr;
using mojo::ShellClient;
using mojo::String;
using mus::mojom::ErrorCode;
using mus::mojom::EventPtr;
using mus::mojom::ViewportMetricsPtr;
using mus::mojom::WindowDataPtr;
using mus::mojom::WindowTree;
using mus::mojom::WindowTreeClient;
namespace mus {
namespace ws {
namespace test {
namespace {
// Creates an id used for transport from the specified parameters.
Id BuildWindowId(ConnectionSpecificId connection_id,
ConnectionSpecificId window_id) {
return (connection_id << 16) | window_id;
}
// Callback function from WindowTree functions.
// ----------------------------------
void WindowTreeResultCallback(base::RunLoop* run_loop,
std::vector<TestWindow>* windows,
Array<WindowDataPtr> results) {
WindowDatasToTestWindows(results, windows);
run_loop->Quit();
}
void EmbedCallbackImpl(base::RunLoop* run_loop,
bool* result_cache,
bool result,
ConnectionSpecificId connection_id) {
*result_cache = result;
run_loop->Quit();
}
// -----------------------------------------------------------------------------
bool EmbedUrl(mojo::Connector* connector,
WindowTree* tree,
const String& url,
Id root_id) {
bool result = false;
base::RunLoop run_loop;
{
mojom::WindowTreeClientPtr client;
connector->ConnectToInterface(url.get(), &client);
tree->Embed(root_id, std::move(client),
mojom::WindowTree::kAccessPolicyDefault,
base::Bind(&EmbedCallbackImpl, &run_loop, &result));
}
run_loop.Run();
return result;
}
bool Embed(WindowTree* tree, Id root_id, mojom::WindowTreeClientPtr client) {
bool result = false;
base::RunLoop run_loop;
{
tree->Embed(root_id, std::move(client),
mojom::WindowTree::kAccessPolicyDefault,
base::Bind(&EmbedCallbackImpl, &run_loop, &result));
}
run_loop.Run();
return result;
}
void GetWindowTree(WindowTree* tree,
Id window_id,
std::vector<TestWindow>* windows) {
base::RunLoop run_loop;
tree->GetWindowTree(
window_id, base::Bind(&WindowTreeResultCallback, &run_loop, windows));
run_loop.Run();
}
// Utility functions -----------------------------------------------------------
const Id kNullParentId = 0;
std::string IdToString(Id id) {
return (id == kNullParentId) ? "null" : base::StringPrintf(
"%d,%d", HiWord(id), LoWord(id));
}
std::string WindowParentToString(Id window, Id parent) {
return base::StringPrintf("window=%s parent=%s", IdToString(window).c_str(),
IdToString(parent).c_str());
}
// -----------------------------------------------------------------------------
// A WindowTreeClient implementation that logs all changes to a tracker.
class TestWindowTreeClientImpl : public mojom::WindowTreeClient,
public TestChangeTracker::Delegate,
public mojom::WindowManager {
public:
TestWindowTreeClientImpl()
: binding_(this),
connection_id_(0),
root_window_id_(0),
// Start with a random large number so tests can use lower ids if they
// want.
next_change_id_(10000),
waiting_change_id_(0),
on_change_completed_result_(false),
track_root_bounds_changes_(false) {
tracker_.set_delegate(this);
}
void Bind(mojo::InterfaceRequest<mojom::WindowTreeClient> request) {
binding_.Bind(std::move(request));
}
mojom::WindowTree* tree() { return tree_.get(); }
TestChangeTracker* tracker() { return &tracker_; }
Id root_window_id() const { return root_window_id_; }
// Sets whether changes to the bounds of the root should be tracked. Normally
// they are ignored (as during startup we often times get random size
// changes).
void set_track_root_bounds_changes(bool value) {
track_root_bounds_changes_ = value;
}
// Runs a nested MessageLoop until |count| changes (calls to
// WindowTreeClient functions) have been received.
void WaitForChangeCount(size_t count) {
if (tracker_.changes()->size() >= count)
return;
ASSERT_TRUE(wait_state_.get() == nullptr);
wait_state_.reset(new WaitState);
wait_state_->change_count = count;
wait_state_->run_loop.Run();
wait_state_.reset();
}
uint32_t GetAndAdvanceChangeId() { return next_change_id_++; }
// Runs a nested MessageLoop until OnEmbed() has been encountered.
void WaitForOnEmbed() {
if (tree_)
return;
embed_run_loop_.reset(new base::RunLoop);
embed_run_loop_->Run();
embed_run_loop_.reset();
}
bool WaitForIncomingMethodCall() {
return binding_.WaitForIncomingMethodCall();
}
bool WaitForChangeCompleted(uint32_t id) {
waiting_change_id_ = id;
change_completed_run_loop_.reset(new base::RunLoop);
change_completed_run_loop_->Run();
return on_change_completed_result_;
}
bool DeleteWindow(Id id) {
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->DeleteWindow(change_id, id);
return WaitForChangeCompleted(change_id);
}
bool AddWindow(Id parent, Id child) {
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->AddWindow(change_id, parent, child);
return WaitForChangeCompleted(change_id);
}
bool RemoveWindowFromParent(Id window_id) {
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->RemoveWindowFromParent(change_id, window_id);
return WaitForChangeCompleted(change_id);
}
bool ReorderWindow(Id window_id,
Id relative_window_id,
mojom::OrderDirection direction) {
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->ReorderWindow(change_id, window_id, relative_window_id, direction);
return WaitForChangeCompleted(change_id);
}
// Waits for all messages to be received by |ws|. This is done by attempting
// to create a bogus window. When we get the response we know all messages
// have been processed.
bool WaitForAllMessages() {
return NewWindowWithCompleteId(WindowIdToTransportId(InvalidWindowId())) ==
0;
}
Id NewWindow(ConnectionSpecificId window_id) {
return NewWindowWithCompleteId(BuildWindowId(connection_id_, window_id));
}
// Generally you want NewWindow(), but use this if you need to test given
// a complete window id (NewWindow() ors with the connection id).
Id NewWindowWithCompleteId(Id id) {
mojo::Map<mojo::String, mojo::Array<uint8_t>> properties;
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->NewWindow(change_id, id, std::move(properties));
return WaitForChangeCompleted(change_id) ? id : 0;
}
bool SetWindowProperty(Id window_id,
const std::string& name,
const std::vector<uint8_t>* data) {
Array<uint8_t> mojo_data(nullptr);
if (data)
mojo_data = Array<uint8_t>::From(*data);
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->SetWindowProperty(change_id, window_id, name, std::move(mojo_data));
return WaitForChangeCompleted(change_id);
}
bool SetPredefinedCursor(Id window_id, mojom::Cursor cursor) {
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->SetPredefinedCursor(change_id, window_id, cursor);
return WaitForChangeCompleted(change_id);
}
bool SetWindowVisibility(Id window_id, bool visible) {
const uint32_t change_id = GetAndAdvanceChangeId();
tree()->SetWindowVisibility(change_id, window_id, visible);
return WaitForChangeCompleted(change_id);
}
private:
// Used when running a nested MessageLoop.
struct WaitState {
WaitState() : change_count(0) {}
// Number of changes waiting for.
size_t change_count;
base::RunLoop run_loop;
};
// TestChangeTracker::Delegate:
void OnChangeAdded() override {
if (wait_state_.get() &&
tracker_.changes()->size() >= wait_state_->change_count) {
wait_state_->run_loop.Quit();
}
}
// WindowTreeClient:
void OnEmbed(ConnectionSpecificId connection_id,
WindowDataPtr root,
mojom::WindowTreePtr tree,
Id focused_window_id,
uint32_t access_policy) override {
// TODO(sky): add coverage of |focused_window_id|.
ASSERT_TRUE(root);
root_window_id_ = root->window_id;
tree_ = std::move(tree);
connection_id_ = connection_id;
tracker()->OnEmbed(connection_id, std::move(root));
if (embed_run_loop_)
embed_run_loop_->Quit();
}
void OnEmbeddedAppDisconnected(Id window_id) override {
tracker()->OnEmbeddedAppDisconnected(window_id);
}
void OnUnembed(Id window_id) override { tracker()->OnUnembed(window_id); }
void OnLostCapture(Id window_id) override {
tracker()->OnLostCapture(window_id);
}
void OnTopLevelCreated(uint32_t change_id,
mojom::WindowDataPtr data) override {
tracker()->OnTopLevelCreated(change_id, std::move(data));
}
void OnWindowBoundsChanged(Id window_id,
RectPtr old_bounds,
RectPtr new_bounds) override {
// The bounds of the root may change during startup on Android at random
// times. As this doesn't matter, and shouldn't impact test exepctations,
// it is ignored.
if (window_id == root_window_id_ && !track_root_bounds_changes_)
return;
tracker()->OnWindowBoundsChanged(window_id, std::move(old_bounds),
std::move(new_bounds));
}
void OnClientAreaChanged(
uint32_t window_id,
mojo::InsetsPtr new_client_area,
mojo::Array<mojo::RectPtr> new_additional_client_areas) override {}
void OnTransientWindowAdded(uint32_t window_id,
uint32_t transient_window_id) override {
tracker()->OnTransientWindowAdded(window_id, transient_window_id);
}
void OnTransientWindowRemoved(uint32_t window_id,
uint32_t transient_window_id) override {
tracker()->OnTransientWindowRemoved(window_id, transient_window_id);
}
void OnWindowViewportMetricsChanged(mojo::Array<uint32_t> window_ids,
ViewportMetricsPtr old_metrics,
ViewportMetricsPtr new_metrics) override {
// Don't track the metrics as they are available at an indeterministic time
// on Android.
}
void OnWindowHierarchyChanged(Id window,
Id new_parent,
Id old_parent,
Array<WindowDataPtr> windows) override {
tracker()->OnWindowHierarchyChanged(window, new_parent, old_parent,
std::move(windows));
}
void OnWindowReordered(Id window_id,
Id relative_window_id,
mojom::OrderDirection direction) override {
tracker()->OnWindowReordered(window_id, relative_window_id, direction);
}
void OnWindowDeleted(Id window) override {
tracker()->OnWindowDeleted(window);
}
void OnWindowVisibilityChanged(uint32_t window, bool visible) override {
tracker()->OnWindowVisibilityChanged(window, visible);
}
void OnWindowDrawnStateChanged(uint32_t window, bool drawn) override {
tracker()->OnWindowDrawnStateChanged(window, drawn);
}
void OnWindowInputEvent(uint32_t event_id,
Id window_id,
EventPtr event) override {
// Ack input events to clear the state on the server. These can be received
// during test startup. X11Window::DispatchEvent sends a synthetic move
// event to notify of entry.
tree()->OnWindowInputEventAck(event_id);
// Don't log input events as none of the tests care about them and they
// may come in at random points.
}
void OnWindowSharedPropertyChanged(uint32_t window,
const String& name,
Array<uint8_t> new_data) override {
tracker_.OnWindowSharedPropertyChanged(window, name, std::move(new_data));
}
// TODO(sky): add testing coverage.
void OnWindowFocused(uint32_t focused_window_id) override {}
void OnWindowPredefinedCursorChanged(uint32_t window_id,
mojom::Cursor cursor_id) override {
tracker_.OnWindowPredefinedCursorChanged(window_id, cursor_id);
}
void OnChangeCompleted(uint32_t change_id, bool success) override {
if (waiting_change_id_ == change_id && change_completed_run_loop_) {
on_change_completed_result_ = success;
change_completed_run_loop_->Quit();
}
}
void RequestClose(uint32_t window_id) override {}
void GetWindowManager(mojo::AssociatedInterfaceRequest<mojom::WindowManager>
internal) override {
window_manager_binding_.reset(
new mojo::AssociatedBinding<mojom::WindowManager>(this,
std::move(internal)));
tree_->GetWindowManagerClient(
GetProxy(&window_manager_client_, tree_.associated_group()));
}
// mojom::WindowManager:
void WmSetBounds(uint32_t change_id,
uint32_t window_id,
mojo::RectPtr bounds) override {
window_manager_client_->WmResponse(change_id, false);
}
void WmSetProperty(uint32_t change_id,
uint32_t window_id,
const mojo::String& name,
mojo::Array<uint8_t> value) override {
window_manager_client_->WmResponse(change_id, false);
}
void WmCreateTopLevelWindow(
uint32_t change_id,
mojo::Map<mojo::String, mojo::Array<uint8_t>> properties) override {
NOTIMPLEMENTED();
}
void OnAccelerator(uint32_t id, mojom::EventPtr event) override {
NOTIMPLEMENTED();
}
TestChangeTracker tracker_;
mojom::WindowTreePtr tree_;
// If non-null we're waiting for OnEmbed() using this RunLoop.
scoped_ptr<base::RunLoop> embed_run_loop_;
// If non-null we're waiting for a certain number of change notifications to
// be encountered.
scoped_ptr<WaitState> wait_state_;
mojo::Binding<WindowTreeClient> binding_;
Id connection_id_;
Id root_window_id_;
uint32_t next_change_id_;
uint32_t waiting_change_id_;
bool on_change_completed_result_;
bool track_root_bounds_changes_;
scoped_ptr<base::RunLoop> change_completed_run_loop_;
scoped_ptr<mojo::AssociatedBinding<mojom::WindowManager>>
window_manager_binding_;
mojom::WindowManagerClientAssociatedPtr window_manager_client_;
DISALLOW_COPY_AND_ASSIGN(TestWindowTreeClientImpl);
};
// -----------------------------------------------------------------------------
// InterfaceFactory for vending TestWindowTreeClientImpls.
class WindowTreeClientFactory
: public mojo::InterfaceFactory<WindowTreeClient> {
public:
WindowTreeClientFactory() {}
~WindowTreeClientFactory() override {}
// Runs a nested MessageLoop until a new instance has been created.
scoped_ptr<TestWindowTreeClientImpl> WaitForInstance() {
if (!client_impl_.get()) {
DCHECK(!run_loop_);
run_loop_.reset(new base::RunLoop);
run_loop_->Run();
run_loop_.reset();
}
return std::move(client_impl_);
}
private:
// InterfaceFactory<WindowTreeClient>:
void Create(Connection* connection,
InterfaceRequest<WindowTreeClient> request) override {
client_impl_.reset(new TestWindowTreeClientImpl());
client_impl_->Bind(std::move(request));
if (run_loop_.get())
run_loop_->Quit();
}
scoped_ptr<TestWindowTreeClientImpl> client_impl_;
scoped_ptr<base::RunLoop> run_loop_;
DISALLOW_COPY_AND_ASSIGN(WindowTreeClientFactory);
};
} // namespace
class WindowTreeAppTest : public mojo::test::ApplicationTestBase,
public mojo::ShellClient {
public:
WindowTreeAppTest()
: connection_id_1_(0), connection_id_2_(0), root_window_id_(0) {}
~WindowTreeAppTest() override {}
protected:
// Returns the changes from the various connections.
std::vector<Change>* changes1() { return wt_client1_->tracker()->changes(); }
std::vector<Change>* changes2() { return wt_client2_->tracker()->changes(); }
std::vector<Change>* changes3() { return wt_client3_->tracker()->changes(); }
// Various connections. |wt1()|, being the first connection, has special
// permissions (it's treated as the window manager).
WindowTree* wt1() { return wt_client1_->tree(); }
WindowTree* wt2() { return wt_client2_->tree(); }
WindowTree* wt3() { return wt_client3_->tree(); }
TestWindowTreeClientImpl* wt_client1() { return wt_client1_.get(); }
TestWindowTreeClientImpl* wt_client2() { return wt_client2_.get(); }
TestWindowTreeClientImpl* wt_client3() { return wt_client3_.get(); }
Id root_window_id() const { return root_window_id_; }
int connection_id_1() const { return connection_id_1_; }
int connection_id_2() const { return connection_id_2_; }
void EstablishSecondConnectionWithRoot(Id root_id) {
ASSERT_TRUE(wt_client2_.get() == nullptr);
wt_client2_ =
EstablishConnectionViaEmbed(wt1(), root_id, &connection_id_2_);
ASSERT_GT(connection_id_2_, 0);
ASSERT_TRUE(wt_client2_.get() != nullptr);
}
void EstablishSecondConnection(bool create_initial_window) {
Id window_1_1 = 0;
if (create_initial_window) {
window_1_1 = wt_client1()->NewWindow(1);
ASSERT_TRUE(window_1_1);
}
ASSERT_NO_FATAL_FAILURE(
EstablishSecondConnectionWithRoot(BuildWindowId(connection_id_1(), 1)));
if (create_initial_window) {
EXPECT_EQ("[" + WindowParentToString(window_1_1, kNullParentId) + "]",
ChangeWindowDescription(*changes2()));
}
}
void EstablishThirdConnection(WindowTree* owner, Id root_id) {
ASSERT_TRUE(wt_client3_.get() == nullptr);
wt_client3_ = EstablishConnectionViaEmbed(owner, root_id, nullptr);
ASSERT_TRUE(wt_client3_.get() != nullptr);
}
scoped_ptr<TestWindowTreeClientImpl> WaitForWindowTreeClient() {
return client_factory_->WaitForInstance();
}
// Establishes a new connection by way of Embed() on the specified
// WindowTree.
scoped_ptr<TestWindowTreeClientImpl> EstablishConnectionViaEmbed(
WindowTree* owner,
Id root_id,
int* connection_id) {
return EstablishConnectionViaEmbedWithPolicyBitmask(
owner, root_id, mojom::WindowTree::kAccessPolicyDefault, connection_id);
}
scoped_ptr<TestWindowTreeClientImpl>
EstablishConnectionViaEmbedWithPolicyBitmask(WindowTree* owner,
Id root_id,
uint32_t policy_bitmask,
int* connection_id) {
if (!EmbedUrl(connector(), owner, test_name(), root_id)) {
ADD_FAILURE() << "Embed() failed";
return nullptr;
}
scoped_ptr<TestWindowTreeClientImpl> client =
client_factory_->WaitForInstance();
if (!client.get()) {
ADD_FAILURE() << "WaitForInstance failed";
return nullptr;
}
client->WaitForOnEmbed();
EXPECT_EQ("OnEmbed",
SingleChangeToDescription(*client->tracker()->changes()));
if (connection_id)
*connection_id = (*client->tracker()->changes())[0].connection_id;
return client;
}
// ApplicationTestBase:
mojo::ShellClient* GetShellClient() override { return this; }
void SetUp() override {
ApplicationTestBase::SetUp();
client_factory_.reset(new WindowTreeClientFactory());
mojom::WindowTreeHostFactoryPtr factory;
connector()->ConnectToInterface("mojo:mus", &factory);
mojom::WindowTreeClientPtr tree_client_ptr;
wt_client1_.reset(new TestWindowTreeClientImpl());
wt_client1_->Bind(GetProxy(&tree_client_ptr));
factory->CreateWindowTreeHost(GetProxy(&host_),
std::move(tree_client_ptr));
// Next we should get an embed call on the "window manager" client.
wt_client1_->WaitForIncomingMethodCall();
ASSERT_EQ(1u, changes1()->size());
EXPECT_EQ(CHANGE_TYPE_EMBED, (*changes1())[0].type);
// All these tests assume 1 for the client id. The only real assertion here
// is the client id is not zero, but adding this as rest of code here
// assumes 1.
ASSERT_GT((*changes1())[0].connection_id, 0);
connection_id_1_ = (*changes1())[0].connection_id;
ASSERT_FALSE((*changes1())[0].windows.empty());
root_window_id_ = (*changes1())[0].windows[0].window_id;
ASSERT_EQ(root_window_id_, wt_client1_->root_window_id());
changes1()->clear();
}
// mojo::ShellClient implementation.
bool AcceptConnection(Connection* connection) override {
connection->AddInterface(client_factory_.get());
return true;
}
scoped_ptr<TestWindowTreeClientImpl> wt_client1_;
scoped_ptr<TestWindowTreeClientImpl> wt_client2_;
scoped_ptr<TestWindowTreeClientImpl> wt_client3_;
mojom::WindowTreeHostPtr host_;
private:
scoped_ptr<WindowTreeClientFactory> client_factory_;
int connection_id_1_;
int connection_id_2_;
Id root_window_id_;
DISALLOW_COPY_AND_ASSIGN(WindowTreeAppTest);
};
// Verifies two clients/connections get different ids.
TEST_F(WindowTreeAppTest, TwoClientsGetDifferentConnectionIds) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
ASSERT_EQ(1u, changes2()->size());
ASSERT_NE(connection_id_1(), connection_id_2());
}
// Verifies when Embed() is invoked any child windows are removed.
TEST_F(WindowTreeAppTest, WindowsRemovedWhenEmbedding) {
// Two windows 1 and 2. 2 is parented to 1.
Id window_1_1 = wt_client1()->NewWindow(1);
ASSERT_TRUE(window_1_1);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(wt_client1()->AddWindow(window_1_1, window_1_2));
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
ASSERT_EQ(1u, changes2()->size());
ASSERT_EQ(1u, (*changes2())[0].windows.size());
EXPECT_EQ("[" + WindowParentToString(window_1_1, kNullParentId) + "]",
ChangeWindowDescription(*changes2()));
// Embed() removed window 2.
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_2, &windows);
EXPECT_EQ(WindowParentToString(window_1_2, kNullParentId),
SingleWindowDescription(windows));
}
// ws2 should not see window 2.
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), window_1_1, &windows);
EXPECT_EQ(WindowParentToString(window_1_1, kNullParentId),
SingleWindowDescription(windows));
}
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), window_1_2, &windows);
EXPECT_TRUE(windows.empty());
}
// Windows 3 and 4 in connection 2.
Id window_2_3 = wt_client2()->NewWindow(3);
Id window_2_4 = wt_client2()->NewWindow(4);
ASSERT_TRUE(window_2_3);
ASSERT_TRUE(window_2_4);
ASSERT_TRUE(wt_client2()->AddWindow(window_2_3, window_2_4));
// Connection 3 rooted at 2.
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt2(), window_2_3));
// Window 4 should no longer have a parent.
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), window_2_3, &windows);
EXPECT_EQ(WindowParentToString(window_2_3, kNullParentId),
SingleWindowDescription(windows));
windows.clear();
GetWindowTree(wt2(), window_2_4, &windows);
EXPECT_EQ(WindowParentToString(window_2_4, kNullParentId),
SingleWindowDescription(windows));
}
// And window 4 should not be visible to connection 3.
{
std::vector<TestWindow> windows;
GetWindowTree(wt3(), window_2_3, &windows);
EXPECT_EQ("no windows", SingleWindowDescription(windows));
}
}
// Verifies once Embed() has been invoked the parent connection can't see any
// children.
TEST_F(WindowTreeAppTest, CantAccessChildrenOfEmbeddedWindow) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_2 = wt_client2()->NewWindow(2);
ASSERT_TRUE(window_2_2);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_2));
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt2(), window_2_2));
Id window_3_3 = wt_client3()->NewWindow(3);
ASSERT_TRUE(window_3_3);
ASSERT_TRUE(
wt_client3()->AddWindow(wt_client3()->root_window_id(), window_3_3));
// Even though 3 is a child of 2 connection 2 can't see 3 as it's from a
// different connection.
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), window_2_2, &windows);
EXPECT_EQ(WindowParentToString(window_2_2, window_1_1),
SingleWindowDescription(windows));
}
// Connection 2 shouldn't be able to get window 3 at all.
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), window_3_3, &windows);
EXPECT_TRUE(windows.empty());
}
// Connection 1 should be able to see it all (its the root).
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_1, &windows);
ASSERT_EQ(3u, windows.size());
EXPECT_EQ(WindowParentToString(window_1_1, kNullParentId),
windows[0].ToString());
// NOTE: we expect a match of WindowParentToString(window_2_2, window_1_1),
// but the ids are in the id space of client2, which is not the same as
// the id space of wt1().
EXPECT_EQ("window=2,1 parent=1,1", windows[1].ToString());
// Same thing here, we really want to test for
// WindowParentToString(window_3_3, window_2_2).
EXPECT_EQ("window=3,1 parent=2,1", windows[2].ToString());
}
}
// Verifies once Embed() has been invoked the parent can't mutate the children.
TEST_F(WindowTreeAppTest, CantModifyChildrenOfEmbeddedWindow) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_1 = wt_client2()->NewWindow(1);
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt2(), window_2_1));
Id window_2_2 = wt_client2()->NewWindow(2);
ASSERT_TRUE(window_2_2);
// Connection 2 shouldn't be able to add anything to the window anymore.
ASSERT_FALSE(wt_client2()->AddWindow(window_2_1, window_2_2));
// Create window 3 in connection 3 and add it to window 3.
Id window_3_1 = wt_client3()->NewWindow(1);
ASSERT_TRUE(window_3_1);
ASSERT_TRUE(wt_client3()->AddWindow(window_2_1, window_3_1));
// Connection 2 shouldn't be able to remove window 3.
ASSERT_FALSE(wt_client2()->RemoveWindowFromParent(window_3_1));
}
// Verifies client gets a valid id.
TEST_F(WindowTreeAppTest, NewWindow) {
Id window_1_1 = wt_client1()->NewWindow(1);
ASSERT_TRUE(window_1_1);
EXPECT_TRUE(changes1()->empty());
// Can't create a window with the same id.
ASSERT_EQ(0u, wt_client1()->NewWindowWithCompleteId(window_1_1));
EXPECT_TRUE(changes1()->empty());
// Can't create a window with a bogus connection id.
ASSERT_EQ(0u, wt_client1()->NewWindowWithCompleteId(
BuildWindowId(connection_id_1() + 1, 1)));
EXPECT_TRUE(changes1()->empty());
}
// Verifies AddWindow fails when window is already in position.
TEST_F(WindowTreeAppTest, AddWindowWithNoChange) {
// Create the embed point now so that the ids line up.
ASSERT_TRUE(wt_client1()->NewWindow(1));
Id window_1_2 = wt_client1()->NewWindow(2);
Id window_1_3 = wt_client1()->NewWindow(3);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(window_1_3);
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
// Make 3 a child of 2.
ASSERT_TRUE(wt_client1()->AddWindow(window_1_2, window_1_3));
// Try again, this should fail.
EXPECT_FALSE(wt_client1()->AddWindow(window_1_2, window_1_3));
}
// Verifies AddWindow fails when window is already in position.
TEST_F(WindowTreeAppTest, AddAncestorFails) {
// Create the embed point now so that the ids line up.
ASSERT_TRUE(wt_client1()->NewWindow(1));
Id window_1_2 = wt_client1()->NewWindow(2);
Id window_1_3 = wt_client1()->NewWindow(3);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(window_1_3);
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
// Make 3 a child of 2.
ASSERT_TRUE(wt_client1()->AddWindow(window_1_2, window_1_3));
// Try to make 2 a child of 3, this should fail since 2 is an ancestor of 3.
EXPECT_FALSE(wt_client1()->AddWindow(window_1_3, window_1_2));
}
// Verifies adding to root sends right notifications.
TEST_F(WindowTreeAppTest, AddToRoot) {
// Create the embed point now so that the ids line up.
Id window_1_1 = wt_client1()->NewWindow(1);
ASSERT_TRUE(window_1_1);
Id window_1_21 = wt_client1()->NewWindow(21);
Id window_1_3 = wt_client1()->NewWindow(3);
ASSERT_TRUE(window_1_21);
ASSERT_TRUE(window_1_3);
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
changes2()->clear();
// Make 3 a child of 21.
ASSERT_TRUE(wt_client1()->AddWindow(window_1_21, window_1_3));
// Make 21 a child of 1.
ASSERT_TRUE(wt_client1()->AddWindow(window_1_1, window_1_21));
// Connection 2 should not be told anything (because the window is from a
// different connection). Create a window to ensure we got a response from
// the server.
ASSERT_TRUE(wt_client2()->NewWindow(100));
EXPECT_TRUE(changes2()->empty());
}
// Verifies HierarchyChanged is correctly sent for various adds/removes.
TEST_F(WindowTreeAppTest, WindowHierarchyChangedWindows) {
// Create the embed point now so that the ids line up.
Id window_1_1 = wt_client1()->NewWindow(1);
// 1,2->1,11.
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_2, true));
Id window_1_11 = wt_client1()->NewWindow(11);
ASSERT_TRUE(window_1_11);
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_11, true));
ASSERT_TRUE(wt_client1()->AddWindow(window_1_2, window_1_11));
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_1, true));
ASSERT_TRUE(wt_client2()->WaitForAllMessages());
changes2()->clear();
// 1,1->1,2->1,11
{
// Client 2 should not get anything (1,2 is from another connection).
ASSERT_TRUE(wt_client1()->AddWindow(window_1_1, window_1_2));
ASSERT_TRUE(wt_client2()->WaitForAllMessages());
EXPECT_TRUE(changes2()->empty());
}
// 0,1->1,1->1,2->1,11.
{
// Client 2 is now connected to the root, so it should have gotten a drawn
// notification.
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
wt_client2_->WaitForChangeCount(1u);
EXPECT_EQ(
"DrawnStateChanged window=" + IdToString(window_1_1) + " drawn=true",
SingleChangeToDescription(*changes2()));
}
// 1,1->1,2->1,11.
{
// Client 2 is no longer connected to the root, should get drawn state
// changed.
changes2()->clear();
ASSERT_TRUE(wt_client1()->RemoveWindowFromParent(window_1_1));
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"DrawnStateChanged window=" + IdToString(window_1_1) + " drawn=false",
SingleChangeToDescription(*changes2()));
}
// 1,1->1,2->1,11->1,111.
Id window_1_111 = wt_client1()->NewWindow(111);
ASSERT_TRUE(window_1_111);
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_111, true));
{
changes2()->clear();
ASSERT_TRUE(wt_client1()->AddWindow(window_1_11, window_1_111));
ASSERT_TRUE(wt_client2()->WaitForAllMessages());
EXPECT_TRUE(changes2()->empty());
}
// 0,1->1,1->1,2->1,11->1,111
{
changes2()->clear();
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"DrawnStateChanged window=" + IdToString(window_1_1) + " drawn=true",
SingleChangeToDescription(*changes2()));
}
}
TEST_F(WindowTreeAppTest, WindowHierarchyChangedAddingKnownToUnknown) {
// Create the following structure: root -> 1 -> 11 and 2->21 (2 has no
// parent).
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_11 = wt_client2()->NewWindow(11);
Id window_2_2 = wt_client2()->NewWindow(2);
Id window_2_21 = wt_client2()->NewWindow(21);
ASSERT_TRUE(window_2_11);
ASSERT_TRUE(window_2_2);
ASSERT_TRUE(window_2_21);
// Set up the hierarchy.
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_11));
ASSERT_TRUE(wt_client2()->AddWindow(window_2_2, window_2_21));
// Remove 11, should result in a hierarchy change for the root.
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->RemoveWindowFromParent(window_2_11));
wt_client1_->WaitForChangeCount(1);
// 2,1 should be IdToString(window_2_11), but window_2_11 is in the id
// space of client2, not client1.
EXPECT_EQ("HierarchyChanged window=2,1 new_parent=null old_parent=" +
IdToString(window_1_1),
SingleChangeToDescription(*changes1()));
}
// Add 2 to 1.
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_2));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("HierarchyChanged window=" + IdToString(window_2_2) +
" new_parent=" + IdToString(window_1_1) + " old_parent=null",
SingleChangeToDescription(*changes1()));
// "window=2,3 parent=2,2]" should be,
// WindowParentToString(window_2_21, window_2_2), but isn't because of
// differing id spaces.
EXPECT_EQ("[" + WindowParentToString(window_2_2, window_1_1) +
"],[window=2,3 parent=2,2]",
ChangeWindowDescription(*changes1()));
}
}
TEST_F(WindowTreeAppTest, ReorderWindow) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_2_1 = wt_client2()->NewWindow(1);
Id window_2_2 = wt_client2()->NewWindow(2);
Id window_2_3 = wt_client2()->NewWindow(3);
Id window_1_4 = wt_client1()->NewWindow(4); // Peer to 1,1
Id window_1_5 = wt_client1()->NewWindow(5); // Peer to 1,1
Id window_2_6 = wt_client2()->NewWindow(6); // Child of 1,2.
Id window_2_7 = wt_client2()->NewWindow(7); // Unparented.
Id window_2_8 = wt_client2()->NewWindow(8); // Unparented.
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(window_2_2);
ASSERT_TRUE(window_2_3);
ASSERT_TRUE(window_1_4);
ASSERT_TRUE(window_1_5);
ASSERT_TRUE(window_2_6);
ASSERT_TRUE(window_2_7);
ASSERT_TRUE(window_2_8);
ASSERT_TRUE(wt_client2()->AddWindow(window_2_1, window_2_2));
ASSERT_TRUE(wt_client2()->AddWindow(window_2_2, window_2_6));
ASSERT_TRUE(wt_client2()->AddWindow(window_2_1, window_2_3));
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_4));
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_5));
ASSERT_TRUE(
wt_client2()->AddWindow(BuildWindowId(connection_id_1(), 1), window_2_1));
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->ReorderWindow(window_2_2, window_2_3,
mojom::OrderDirection::ABOVE));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("Reordered window=" + IdToString(window_2_2) + " relative=" +
IdToString(window_2_3) + " direction=above",
SingleChangeToDescription(*changes1()));
}
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->ReorderWindow(window_2_2, window_2_3,
mojom::OrderDirection::BELOW));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("Reordered window=" + IdToString(window_2_2) + " relative=" +
IdToString(window_2_3) + " direction=below",
SingleChangeToDescription(*changes1()));
}
// view2 is already below view3.
EXPECT_FALSE(wt_client2()->ReorderWindow(window_2_2, window_2_3,
mojom::OrderDirection::BELOW));
// view4 & 5 are unknown to connection2_.
EXPECT_FALSE(wt_client2()->ReorderWindow(window_1_4, window_1_5,
mojom::OrderDirection::ABOVE));
// view6 & view3 have different parents.
EXPECT_FALSE(wt_client1()->ReorderWindow(window_2_3, window_2_6,
mojom::OrderDirection::ABOVE));
// Non-existent window-ids
EXPECT_FALSE(wt_client1()->ReorderWindow(BuildWindowId(connection_id_1(), 27),
BuildWindowId(connection_id_1(), 28),
mojom::OrderDirection::ABOVE));
// view7 & view8 are un-parented.
EXPECT_FALSE(wt_client1()->ReorderWindow(window_2_7, window_2_8,
mojom::OrderDirection::ABOVE));
}
// Verifies DeleteWindow works.
TEST_F(WindowTreeAppTest, DeleteWindow) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_1 = wt_client2()->NewWindow(1);
ASSERT_TRUE(window_2_1);
// Make 2 a child of 1.
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("HierarchyChanged window=" + IdToString(window_2_1) +
" new_parent=" + IdToString(window_1_1) + " old_parent=null",
SingleChangeToDescription(*changes1()));
}
// Delete 2.
{
changes1()->clear();
changes2()->clear();
ASSERT_TRUE(wt_client2()->DeleteWindow(window_2_1));
EXPECT_TRUE(changes2()->empty());
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("WindowDeleted window=" + IdToString(window_2_1),
SingleChangeToDescription(*changes1()));
}
}
// Verifies DeleteWindow isn't allowed from a separate connection.
TEST_F(WindowTreeAppTest, DeleteWindowFromAnotherConnectionDisallowed) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
EXPECT_FALSE(wt_client2()->DeleteWindow(BuildWindowId(connection_id_1(), 1)));
}
// Verifies if a window was deleted and then reused that other clients are
// properly notified.
TEST_F(WindowTreeAppTest, ReuseDeletedWindowId) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_1 = wt_client2()->NewWindow(1);
ASSERT_TRUE(window_2_1);
// Add 2 to 1.
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("HierarchyChanged window=" + IdToString(window_2_1) +
" new_parent=" + IdToString(window_1_1) + " old_parent=null",
SingleChangeToDescription(*changes1()));
EXPECT_EQ("[" + WindowParentToString(window_2_1, window_1_1) + "]",
ChangeWindowDescription(*changes1()));
}
// Delete 2.
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->DeleteWindow(window_2_1));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("WindowDeleted window=" + IdToString(window_2_1),
SingleChangeToDescription(*changes1()));
}
// Create 2 again, and add it back to 1. Should get the same notification.
window_2_1 = wt_client2()->NewWindow(2);
ASSERT_TRUE(window_2_1);
{
changes1()->clear();
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("HierarchyChanged window=" + IdToString(window_2_1) +
" new_parent=" + IdToString(window_1_1) + " old_parent=null",
SingleChangeToDescription(*changes1()));
EXPECT_EQ("[" + WindowParentToString(window_2_1, window_1_1) + "]",
ChangeWindowDescription(*changes1()));
}
}
// Assertions for GetWindowTree.
TEST_F(WindowTreeAppTest, GetWindowTree) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
// Create 11 in first connection and make it a child of 1.
Id window_1_11 = wt_client1()->NewWindow(11);
ASSERT_TRUE(window_1_11);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_TRUE(wt_client1()->AddWindow(window_1_1, window_1_11));
// Create two windows in second connection, 2 and 3, both children of 1.
Id window_2_1 = wt_client2()->NewWindow(1);
Id window_2_2 = wt_client2()->NewWindow(2);
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(window_2_2);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_2));
// Verifies GetWindowTree() on the root. The root connection sees all.
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), root_window_id(), &windows);
ASSERT_EQ(5u, windows.size());
EXPECT_EQ(WindowParentToString(root_window_id(), kNullParentId),
windows[0].ToString());
EXPECT_EQ(WindowParentToString(window_1_1, root_window_id()),
windows[1].ToString());
EXPECT_EQ(WindowParentToString(window_1_11, window_1_1),
windows[2].ToString());
EXPECT_EQ(WindowParentToString(window_2_1, window_1_1),
windows[3].ToString());
EXPECT_EQ(WindowParentToString(window_2_2, window_1_1),
windows[4].ToString());
}
// Verifies GetWindowTree() on the window 1,1 from wt2(). wt2() sees 1,1 as
// 1,1
// is wt2()'s root and wt2() sees all the windows it created.
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), window_1_1, &windows);
ASSERT_EQ(3u, windows.size());
EXPECT_EQ(WindowParentToString(window_1_1, kNullParentId),
windows[0].ToString());
EXPECT_EQ(WindowParentToString(window_2_1, window_1_1),
windows[1].ToString());
EXPECT_EQ(WindowParentToString(window_2_2, window_1_1),
windows[2].ToString());
}
// Connection 2 shouldn't be able to get the root tree.
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), root_window_id(), &windows);
ASSERT_EQ(0u, windows.size());
}
}
TEST_F(WindowTreeAppTest, SetWindowBounds) {
Id window_1_1 = wt_client1()->NewWindow(1);
ASSERT_TRUE(window_1_1);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
changes2()->clear();
wt_client2_->set_track_root_bounds_changes(true);
wt1()->SetWindowBounds(10, window_1_1,
mojo::Rect::From(gfx::Rect(0, 0, 100, 100)));
ASSERT_TRUE(wt_client1()->WaitForChangeCompleted(10));
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ("BoundsChanged window=" + IdToString(window_1_1) +
" old_bounds=0,0 0x0 new_bounds=0,0 100x100",
SingleChangeToDescription(*changes2()));
// Should not be possible to change the bounds of a window created by another
// connection.
wt2()->SetWindowBounds(11, window_1_1,
mojo::Rect::From(gfx::Rect(0, 0, 0, 0)));
ASSERT_FALSE(wt_client2()->WaitForChangeCompleted(11));
}
// Verify AddWindow fails when trying to manipulate windows in other roots.
TEST_F(WindowTreeAppTest, CantMoveWindowsFromOtherRoot) {
// Create 1 and 2 in the first connection.
Id window_1_1 = wt_client1()->NewWindow(1);
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_1);
ASSERT_TRUE(window_1_2);
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
// Try to move 2 to be a child of 1 from connection 2. This should fail as 2
// should not be able to access 1.
ASSERT_FALSE(wt_client2()->AddWindow(window_1_1, window_1_2));
// Try to reparent 1 to the root. A connection is not allowed to reparent its
// roots.
ASSERT_FALSE(wt_client2()->AddWindow(root_window_id(), window_1_1));
}
// Verify RemoveWindowFromParent fails for windows that are descendants of the
// roots.
TEST_F(WindowTreeAppTest, CantRemoveWindowsInOtherRoots) {
// Create 1 and 2 in the first connection and parent both to the root.
Id window_1_1 = wt_client1()->NewWindow(1);
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_1);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_2));
// Establish the second connection and give it the root 1.
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
// Connection 2 should not be able to remove window 2 or 1 from its parent.
ASSERT_FALSE(wt_client2()->RemoveWindowFromParent(window_1_2));
ASSERT_FALSE(wt_client2()->RemoveWindowFromParent(window_1_1));
// Create windows 10 and 11 in 2.
Id window_2_10 = wt_client2()->NewWindow(10);
Id window_2_11 = wt_client2()->NewWindow(11);
ASSERT_TRUE(window_2_10);
ASSERT_TRUE(window_2_11);
// Parent 11 to 10.
ASSERT_TRUE(wt_client2()->AddWindow(window_2_10, window_2_11));
// Remove 11 from 10.
ASSERT_TRUE(wt_client2()->RemoveWindowFromParent(window_2_11));
// Verify nothing was actually removed.
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), root_window_id(), &windows);
ASSERT_EQ(3u, windows.size());
EXPECT_EQ(WindowParentToString(root_window_id(), kNullParentId),
windows[0].ToString());
EXPECT_EQ(WindowParentToString(window_1_1, root_window_id()),
windows[1].ToString());
EXPECT_EQ(WindowParentToString(window_1_2, root_window_id()),
windows[2].ToString());
}
}
// Verify GetWindowTree fails for windows that are not descendants of the roots.
TEST_F(WindowTreeAppTest, CantGetWindowTreeOfOtherRoots) {
// Create 1 and 2 in the first connection and parent both to the root.
Id window_1_1 = wt_client1()->NewWindow(1);
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_1);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_2));
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
std::vector<TestWindow> windows;
// Should get nothing for the root.
GetWindowTree(wt2(), root_window_id(), &windows);
ASSERT_TRUE(windows.empty());
// Should get nothing for window 2.
GetWindowTree(wt2(), window_1_2, &windows);
ASSERT_TRUE(windows.empty());
// Should get window 1 if asked for.
GetWindowTree(wt2(), window_1_1, &windows);
ASSERT_EQ(1u, windows.size());
EXPECT_EQ(WindowParentToString(window_1_1, kNullParentId),
windows[0].ToString());
}
TEST_F(WindowTreeAppTest, EmbedWithSameWindowId) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
changes2()->clear();
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt1(), window_1_1));
// Connection2 should have been told of the unembed and delete.
{
wt_client2_->WaitForChangeCount(2);
EXPECT_EQ("OnUnembed window=" + IdToString(window_1_1),
ChangesToDescription1(*changes2())[0]);
EXPECT_EQ("WindowDeleted window=" + IdToString(window_1_1),
ChangesToDescription1(*changes2())[1]);
}
// Connection2 has no root. Verify it can't see window 1,1 anymore.
{
std::vector<TestWindow> windows;
GetWindowTree(wt2(), window_1_1, &windows);
EXPECT_TRUE(windows.empty());
}
}
TEST_F(WindowTreeAppTest, EmbedWithSameWindowId2) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
changes2()->clear();
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt1(), window_1_1));
// Connection2 should have been told about the unembed and delete.
wt_client2_->WaitForChangeCount(2);
changes2()->clear();
// Create a window in the third connection and parent it to the root.
Id window_3_1 = wt_client3()->NewWindow(1);
ASSERT_TRUE(window_3_1);
ASSERT_TRUE(wt_client3()->AddWindow(window_1_1, window_3_1));
// Connection 1 should have been told about the add (it owns the window).
{
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("HierarchyChanged window=" + IdToString(window_3_1) +
" new_parent=" + IdToString(window_1_1) + " old_parent=null",
SingleChangeToDescription(*changes1()));
}
// Embed 1,1 again.
{
changes3()->clear();
// We should get a new connection for the new embedding.
scoped_ptr<TestWindowTreeClientImpl> connection4(
EstablishConnectionViaEmbed(wt1(), window_1_1, nullptr));
ASSERT_TRUE(connection4.get());
EXPECT_EQ("[" + WindowParentToString(window_1_1, kNullParentId) + "]",
ChangeWindowDescription(*connection4->tracker()->changes()));
// And 3 should get an unembed and delete.
wt_client3_->WaitForChangeCount(2);
EXPECT_EQ("OnUnembed window=" + IdToString(window_1_1),
ChangesToDescription1(*changes3())[0]);
EXPECT_EQ("WindowDeleted window=" + IdToString(window_1_1),
ChangesToDescription1(*changes3())[1]);
}
// wt3() has no root. Verify it can't see window 1,1 anymore.
{
std::vector<TestWindow> windows;
GetWindowTree(wt3(), window_1_1, &windows);
EXPECT_TRUE(windows.empty());
}
// Verify 3,1 is no longer parented to 1,1. We have to do this from 1,1 as
// wt3() can no longer see 1,1.
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_1, &windows);
ASSERT_EQ(1u, windows.size());
EXPECT_EQ(WindowParentToString(window_1_1, kNullParentId),
windows[0].ToString());
}
// Verify wt3() can still see the window it created 3,1.
{
std::vector<TestWindow> windows;
GetWindowTree(wt3(), window_3_1, &windows);
ASSERT_EQ(1u, windows.size());
EXPECT_EQ(WindowParentToString(window_3_1, kNullParentId),
windows[0].ToString());
}
}
// Assertions for SetWindowVisibility.
TEST_F(WindowTreeAppTest, SetWindowVisibility) {
// Create 1 and 2 in the first connection and parent both to the root.
Id window_1_1 = wt_client1()->NewWindow(1);
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_1);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), root_window_id(), &windows);
ASSERT_EQ(2u, windows.size());
EXPECT_EQ(WindowParentToString(root_window_id(), kNullParentId) +
" visible=true drawn=true",
windows[0].ToString2());
EXPECT_EQ(WindowParentToString(window_1_1, root_window_id()) +
" visible=false drawn=false",
windows[1].ToString2());
}
// Show all the windows.
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_1, true));
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_2, true));
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), root_window_id(), &windows);
ASSERT_EQ(2u, windows.size());
EXPECT_EQ(WindowParentToString(root_window_id(), kNullParentId) +
" visible=true drawn=true",
windows[0].ToString2());
EXPECT_EQ(WindowParentToString(window_1_1, root_window_id()) +
" visible=true drawn=true",
windows[1].ToString2());
}
// Hide 1.
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_1, false));
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_1, &windows);
ASSERT_EQ(1u, windows.size());
EXPECT_EQ(WindowParentToString(window_1_1, root_window_id()) +
" visible=false drawn=false",
windows[0].ToString2());
}
// Attach 2 to 1.
ASSERT_TRUE(wt_client1()->AddWindow(window_1_1, window_1_2));
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_1, &windows);
ASSERT_EQ(2u, windows.size());
EXPECT_EQ(WindowParentToString(window_1_1, root_window_id()) +
" visible=false drawn=false",
windows[0].ToString2());
EXPECT_EQ(WindowParentToString(window_1_2, window_1_1) +
" visible=true drawn=false",
windows[1].ToString2());
}
// Show 1.
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_1, true));
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_1, &windows);
ASSERT_EQ(2u, windows.size());
EXPECT_EQ(WindowParentToString(window_1_1, root_window_id()) +
" visible=true drawn=true",
windows[0].ToString2());
EXPECT_EQ(WindowParentToString(window_1_2, window_1_1) +
" visible=true drawn=true",
windows[1].ToString2());
}
}
// Test that we hear the cursor change in other connections.
TEST_F(WindowTreeAppTest, SetCursor) {
// Get a second connection to listen in.
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
changes2()->clear();
ASSERT_TRUE(
wt_client1()->SetPredefinedCursor(window_1_1, mojom::Cursor::IBEAM));
wt_client2_->WaitForChangeCount(1u);
EXPECT_EQ("CursorChanged id=" + IdToString(window_1_1) + " cursor_id=4",
SingleChangeToDescription(*changes2()));
}
// Assertions for SetWindowVisibility sending notifications.
TEST_F(WindowTreeAppTest, SetWindowVisibilityNotifications) {
// Create 1,1 and 1,2. 1,2 is made a child of 1,1 and 1,1 a child of the root.
Id window_1_1 = wt_client1()->NewWindow(1);
ASSERT_TRUE(window_1_1);
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_1, true));
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_2, true));
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_TRUE(wt_client1()->AddWindow(window_1_1, window_1_2));
// Establish the second connection at 1,2.
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnectionWithRoot(window_1_2));
// Add 2,3 as a child of 1,2.
Id window_2_1 = wt_client2()->NewWindow(1);
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(wt_client2()->SetWindowVisibility(window_2_1, true));
ASSERT_TRUE(wt_client2()->AddWindow(window_1_2, window_2_1));
ASSERT_TRUE(wt_client1()->WaitForAllMessages());
changes2()->clear();
// Hide 1,2 from connection 1. Connection 2 should see this.
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_2, false));
{
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"VisibilityChanged window=" + IdToString(window_1_2) + " visible=false",
SingleChangeToDescription(*changes2()));
}
changes1()->clear();
// Show 1,2 from connection 2, connection 1 should be notified.
ASSERT_TRUE(wt_client2()->SetWindowVisibility(window_1_2, true));
{
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ(
"VisibilityChanged window=" + IdToString(window_1_2) + " visible=true",
SingleChangeToDescription(*changes1()));
}
changes2()->clear();
// Hide 1,1, connection 2 should be told the draw state changed.
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_1, false));
{
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"DrawnStateChanged window=" + IdToString(window_1_2) + " drawn=false",
SingleChangeToDescription(*changes2()));
}
changes2()->clear();
// Show 1,1 from connection 1. Connection 2 should see this.
ASSERT_TRUE(wt_client1()->SetWindowVisibility(window_1_1, true));
{
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"DrawnStateChanged window=" + IdToString(window_1_2) + " drawn=true",
SingleChangeToDescription(*changes2()));
}
// Change visibility of 2,3, connection 1 should see this.
changes1()->clear();
ASSERT_TRUE(wt_client2()->SetWindowVisibility(window_2_1, false));
{
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ(
"VisibilityChanged window=" + IdToString(window_2_1) + " visible=false",
SingleChangeToDescription(*changes1()));
}
changes2()->clear();
// Remove 1,1 from the root, connection 2 should see drawn state changed.
ASSERT_TRUE(wt_client1()->RemoveWindowFromParent(window_1_1));
{
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"DrawnStateChanged window=" + IdToString(window_1_2) + " drawn=false",
SingleChangeToDescription(*changes2()));
}
changes2()->clear();
// Add 1,1 back to the root, connection 2 should see drawn state changed.
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
{
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"DrawnStateChanged window=" + IdToString(window_1_2) + " drawn=true",
SingleChangeToDescription(*changes2()));
}
}
TEST_F(WindowTreeAppTest, SetWindowProperty) {
Id window_1_1 = wt_client1()->NewWindow(1);
ASSERT_TRUE(window_1_1);
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
changes2()->clear();
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), root_window_id(), &windows);
ASSERT_EQ(2u, windows.size());
EXPECT_EQ(root_window_id(), windows[0].window_id);
EXPECT_EQ(window_1_1, windows[1].window_id);
ASSERT_EQ(0u, windows[1].properties.size());
}
// Set properties on 1.
changes2()->clear();
std::vector<uint8_t> one(1, '1');
ASSERT_TRUE(wt_client1()->SetWindowProperty(window_1_1, "one", &one));
{
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ(
"PropertyChanged window=" + IdToString(window_1_1) + " key=one value=1",
SingleChangeToDescription(*changes2()));
}
// Test that our properties exist in the window tree
{
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_1, &windows);
ASSERT_EQ(1u, windows.size());
ASSERT_EQ(1u, windows[0].properties.size());
EXPECT_EQ(one, windows[0].properties["one"]);
}
changes2()->clear();
// Set back to null.
ASSERT_TRUE(wt_client1()->SetWindowProperty(window_1_1, "one", NULL));
{
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ("PropertyChanged window=" + IdToString(window_1_1) +
" key=one value=NULL",
SingleChangeToDescription(*changes2()));
}
}
TEST_F(WindowTreeAppTest, OnEmbeddedAppDisconnected) {
// Create connection 2 and 3.
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_1 = wt_client2()->NewWindow(1);
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
changes2()->clear();
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt2(), window_2_1));
// Connection 1 should get a hierarchy change for window_2_1.
wt_client1_->WaitForChangeCount(1);
changes1()->clear();
// Close connection 3. Connection 2 (which had previously embedded 3) should
// be notified of this.
wt_client3_.reset();
wt_client2_->WaitForChangeCount(1);
EXPECT_EQ("OnEmbeddedAppDisconnected window=" + IdToString(window_2_1),
SingleChangeToDescription(*changes2()));
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("OnEmbeddedAppDisconnected window=" + IdToString(window_2_1),
SingleChangeToDescription(*changes1()));
}
// Verifies when the parent of an Embed() is destroyed the embedded app gets
// a WindowDeleted (and doesn't trigger a DCHECK).
TEST_F(WindowTreeAppTest, OnParentOfEmbedDisconnects) {
// Create connection 2 and 3.
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
Id window_2_1 = wt_client2()->NewWindow(1);
Id window_2_2 = wt_client2()->NewWindow(2);
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(window_2_2);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
ASSERT_TRUE(wt_client2()->AddWindow(window_2_1, window_2_2));
changes2()->clear();
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt2(), window_2_2));
changes3()->clear();
// Close connection 2. Connection 3 should get a delete (for its root).
wt_client2_.reset();
wt_client3_->WaitForChangeCount(1);
EXPECT_EQ("WindowDeleted window=" + IdToString(window_2_2),
SingleChangeToDescription(*changes3()));
}
// Verifies WindowTreeImpl doesn't incorrectly erase from its internal
// map when a window from another connection with the same window_id is removed.
TEST_F(WindowTreeAppTest, DontCleanMapOnDestroy) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
ASSERT_TRUE(wt_client2()->NewWindow(1));
changes1()->clear();
wt_client2_.reset();
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("OnEmbeddedAppDisconnected window=" + IdToString(window_1_1),
SingleChangeToDescription(*changes1()));
std::vector<TestWindow> windows;
GetWindowTree(wt1(), window_1_1, &windows);
EXPECT_FALSE(windows.empty());
}
// Verifies Embed() works when supplying a WindowTreeClient.
TEST_F(WindowTreeAppTest, EmbedSupplyingWindowTreeClient) {
ASSERT_TRUE(wt_client1()->NewWindow(1));
TestWindowTreeClientImpl client2;
mojom::WindowTreeClientPtr client2_ptr;
mojo::Binding<WindowTreeClient> client2_binding(&client2, &client2_ptr);
ASSERT_TRUE(Embed(wt1(), BuildWindowId(connection_id_1(), 1),
std::move(client2_ptr)));
client2.WaitForOnEmbed();
EXPECT_EQ("OnEmbed",
SingleChangeToDescription(*client2.tracker()->changes()));
}
TEST_F(WindowTreeAppTest, EmbedFailsFromOtherConnection) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_1 = wt_client2()->NewWindow(1);
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt2(), window_2_1));
Id window_3_3 = wt_client3()->NewWindow(3);
ASSERT_TRUE(window_3_3);
ASSERT_TRUE(wt_client3()->AddWindow(window_2_1, window_3_3));
// 2 should not be able to embed in window_3_3 as window_3_3 was not created
// by
// 2.
EXPECT_FALSE(EmbedUrl(connector(), wt2(), test_name(), window_3_3));
}
// Verifies Embed() from window manager on another connections window works.
TEST_F(WindowTreeAppTest, EmbedFromOtherConnection) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_1 = wt_client2()->NewWindow(1);
ASSERT_TRUE(window_2_1);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
changes2()->clear();
// Establish a third connection in window_2_1.
ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(wt1(), window_2_1));
ASSERT_TRUE(wt_client2()->WaitForAllMessages());
EXPECT_EQ(std::string(), SingleChangeToDescription(*changes2()));
}
TEST_F(WindowTreeAppTest, CantEmbedFromConnectionRoot) {
// Shouldn't be able to embed into the root.
ASSERT_FALSE(EmbedUrl(connector(), wt1(), test_name(), root_window_id()));
// Even though the call above failed a WindowTreeClient was obtained. We need
// to
// wait for it else we throw off the next connect.
WaitForWindowTreeClient();
// Don't allow a connection to embed into its own root.
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
EXPECT_FALSE(EmbedUrl(connector(), wt2(), test_name(),
BuildWindowId(connection_id_1(), 1)));
// Need to wait for a WindowTreeClient for same reason as above.
WaitForWindowTreeClient();
Id window_1_2 = wt_client1()->NewWindow(2);
ASSERT_TRUE(window_1_2);
ASSERT_TRUE(
wt_client1()->AddWindow(BuildWindowId(connection_id_1(), 1), window_1_2));
ASSERT_TRUE(wt_client3_.get() == nullptr);
wt_client3_ = EstablishConnectionViaEmbedWithPolicyBitmask(
wt1(), window_1_2, mojom::WindowTree::kAccessPolicyEmbedRoot, nullptr);
ASSERT_TRUE(wt_client3_.get() != nullptr);
// window_1_2 is ws3's root, so even though v3 is an embed root it should not
// be able to Embed into itself.
ASSERT_FALSE(EmbedUrl(connector(), wt3(), test_name(), window_1_2));
}
// Verifies that a transient window tracks its parent's lifetime.
TEST_F(WindowTreeAppTest, TransientWindowTracksTransientParentLifetime) {
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
Id window_1_1 = BuildWindowId(connection_id_1(), 1);
Id window_2_1 = wt_client2()->NewWindow(1);
Id window_2_2 = wt_client2()->NewWindow(2);
Id window_2_3 = wt_client2()->NewWindow(3);
ASSERT_TRUE(window_2_1);
// root -> window_1_1 -> window_2_1
// root -> window_1_1 -> window_2_2
// root -> window_1_1 -> window_2_3
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_1));
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_2));
ASSERT_TRUE(wt_client2()->AddWindow(window_1_1, window_2_3));
// window_2_2 and window_2_3 now track the lifetime of window_2_1.
changes1()->clear();
wt2()->AddTransientWindow(10, window_2_1, window_2_2);
wt2()->AddTransientWindow(11, window_2_1, window_2_3);
wt_client1()->WaitForChangeCount(2);
EXPECT_EQ("AddTransientWindow parent = " + IdToString(window_2_1) +
" child = " + IdToString(window_2_2),
ChangesToDescription1(*changes1())[0]);
EXPECT_EQ("AddTransientWindow parent = " + IdToString(window_2_1) +
" child = " + IdToString(window_2_3),
ChangesToDescription1(*changes1())[1]);
changes1()->clear();
wt2()->RemoveTransientWindowFromParent(12, window_2_3);
wt_client1()->WaitForChangeCount(1);
EXPECT_EQ("RemoveTransientWindowFromParent parent = " +
IdToString(window_2_1) + " child = " + IdToString(window_2_3),
SingleChangeToDescription(*changes1()));
changes1()->clear();
ASSERT_TRUE(wt_client2()->DeleteWindow(window_2_1));
wt_client1()->WaitForChangeCount(2);
EXPECT_EQ("WindowDeleted window=" + IdToString(window_2_2),
ChangesToDescription1(*changes1())[0]);
EXPECT_EQ("WindowDeleted window=" + IdToString(window_2_1),
ChangesToDescription1(*changes1())[1]);
}
TEST_F(WindowTreeAppTest, Ids) {
const Id window_1_100 = wt_client1()->NewWindow(100);
ASSERT_TRUE(window_1_100);
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_100));
// Establish the second connection at 1,100.
ASSERT_NO_FATAL_FAILURE(EstablishSecondConnectionWithRoot(window_1_100));
// 1,100 is the id in the wt_client1's id space. The new client should see
// 2,1 (the server id).
const Id window_1_100_in_ws2 = BuildWindowId(connection_id_1(), 1);
EXPECT_EQ(window_1_100_in_ws2, wt_client2()->root_window_id());
// The first window created in the second connection gets a server id of 2,1
// regardless of the id the client uses.
const Id window_2_101 = wt_client2()->NewWindow(101);
ASSERT_TRUE(wt_client2()->AddWindow(window_1_100_in_ws2, window_2_101));
const Id window_2_101_in_ws1 = BuildWindowId(connection_id_2(), 1);
wt_client1()->WaitForChangeCount(1);
EXPECT_EQ("HierarchyChanged window=" + IdToString(window_2_101_in_ws1) +
" new_parent=" + IdToString(window_1_100) + " old_parent=null",
SingleChangeToDescription(*changes1()));
changes1()->clear();
// Change the bounds of window_2_101 and make sure server gets it.
wt2()->SetWindowBounds(11, window_2_101,
mojo::Rect::From(gfx::Rect(1, 2, 3, 4)));
ASSERT_TRUE(wt_client2()->WaitForChangeCompleted(11));
wt_client1()->WaitForChangeCount(1);
EXPECT_EQ("BoundsChanged window=" + IdToString(window_2_101_in_ws1) +
" old_bounds=0,0 0x0 new_bounds=1,2 3x4",
SingleChangeToDescription(*changes1()));
changes2()->clear();
// Remove 2_101 from wm, client1 should see the change.
wt1()->RemoveWindowFromParent(12, window_2_101_in_ws1);
ASSERT_TRUE(wt_client1()->WaitForChangeCompleted(12));
wt_client2()->WaitForChangeCount(1);
EXPECT_EQ("HierarchyChanged window=" + IdToString(window_2_101) +
" new_parent=null old_parent=" +
IdToString(window_1_100_in_ws2),
SingleChangeToDescription(*changes2()));
}
// Tests that setting capture fails when no input event has occurred, and there
// is no notification of lost capture.
TEST_F(WindowTreeAppTest, ExplicitCaptureWithoutInput) {
Id window_1_1 = wt_client1()->NewWindow(1);
// Add the window to the root, so that they have a Display to handle input
// capture.
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
changes1()->clear();
// Since there has been no input, capture should not succeed. No lost capture
// message is expected.
wt1()->SetCapture(1, window_1_1);
wt_client1_->WaitForAllMessages();
EXPECT_TRUE(changes1()->empty());
// Since there is no window with capture, lost capture should not be notified.
wt1()->ReleaseCapture(3, window_1_1);
wt_client1_->WaitForAllMessages();
EXPECT_TRUE(changes1()->empty());
}
// TODO(jonross): Enable this once apptests can send input events to the server.
// Enabling capture requires that the connection be processing events.
TEST_F(WindowTreeAppTest, DISABLED_ExplicitCapturePropagation) {
Id window_1_1 = wt_client1()->NewWindow(1);
Id window_1_2 = wt_client1()->NewWindow(2);
// Add the windows to the root, so that they have a Display to handle input
// capture.
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_1));
ASSERT_TRUE(wt_client1()->AddWindow(root_window_id(), window_1_2));
changes1()->clear();
// Window 1 takes capture then Window 2 takes capture.
// Verify that window 1 has lost capture.
wt1()->SetCapture(1, window_1_1);
wt1()->SetCapture(2, window_1_2);
wt_client1_->WaitForChangeCount(1);
EXPECT_EQ("OnLostCapture window=" + IdToString(window_1_1),
SingleChangeToDescription(*changes1()));
changes1()->clear();
// Explicitly releasing capture should not notify of lost capture.
wt1()->ReleaseCapture(3, window_1_2);
wt_client1_->WaitForAllMessages();
EXPECT_TRUE(changes1()->empty());
}
// TODO(sky): need to better track changes to initial connection. For example,
// that SetBounsdWindows/AddWindow and the like don't result in messages to the
// originating connection.
// TODO(sky): make sure coverage of what was
// WindowManagerTest.SecondEmbedRoot_InitService and
// WindowManagerTest.MultipleEmbedRootsBeforeWTHReady gets added to window
// manager
// tests.
} // namespace test
} // namespace ws
} // namespace mus
|