This repository was archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsommelier-window-test.cc
1171 lines (1022 loc) · 42 KB
/
sommelier-window-test.cc
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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mock-viewporter-shim.h" // NOLINT(build/include_directory)
#include "testing/x11-test-base.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
#include <wayland-server-core.h>
#include <wayland-server-protocol.h>
#include <wayland-util.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <cstdint>
#ifdef QUIRKS_SUPPORT
#include "quirks/sommelier-quirks.h"
#endif
namespace vm_tools {
namespace sommelier {
using ::testing::_;
using ::testing::AllOf;
using ::testing::PrintToString;
using X11Test = X11TestBase;
TEST_F(X11DirectScaleTest, ViewportOverrideStretchedVertically) {
// Arrange
ctx.viewport_resize = true;
AdvertiseOutputs(xwayland.get(),
{{.logical_width = 1536, .logical_height = 864}});
sl_window* window = CreateToplevelWindow();
const int pixel_width = 1920;
const int pixel_height = 1080;
window->managed = true;
window->max_width = pixel_width;
window->min_height = pixel_height;
window->min_width = pixel_width;
window->max_height = pixel_height;
window->width = pixel_width;
window->height = pixel_height;
window->paired_surface->contents_width = pixel_width;
window->paired_surface->contents_height = pixel_height;
wl_array states;
wl_array_init(&states);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, 1393, 784))
.Times(1);
// Act: Configure with height smaller than min_height.
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, 1536 /*1920*/, 784 /*980*/,
&states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123);
sl_host_surface_commit(nullptr, window->paired_surface->resource);
Pump();
// Assert: viewport size and pointer scale are set, width height are
// unchanged.
EXPECT_TRUE(window->viewport_override);
EXPECT_EQ(window->viewport_width, 1393);
EXPECT_EQ(window->viewport_height, 784);
EXPECT_FLOAT_EQ(window->viewport_pointer_scale, 1.1026561);
EXPECT_EQ(window->width, 1920);
EXPECT_EQ(window->height, 1080);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, -1, -1))
// Act: Configure with the size that is within the window bounds.
.Times(1);
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, 1536, 864, &states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 124);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, 1536, 864))
.Times(1);
// Act
sl_host_surface_commit(nullptr, window->paired_surface->resource);
Pump();
// Assert: viewport override is no longer used.
EXPECT_FALSE(window->viewport_override);
EXPECT_EQ(window->viewport_width, -1);
EXPECT_EQ(window->viewport_height, -1);
EXPECT_EQ(window->width, pixel_width);
EXPECT_EQ(window->height, pixel_height);
}
TEST_F(X11DirectScaleTest, ViewportOverrideStretchedHorizontally) {
// Arrange
ctx.viewport_resize = true;
AdvertiseOutputs(xwayland.get(),
{{.logical_width = 1536, .logical_height = 864}});
sl_window* window = CreateToplevelWindow();
window->managed = true;
window->max_width = 1700;
window->max_height = 900;
window->min_width = 1600;
window->min_height = 800;
window->width = 1650;
window->height = 800;
window->paired_surface->contents_width = 1650;
window->paired_surface->contents_height = 800;
wl_array states;
wl_array_init(&states);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, 1200, 581))
.Times(1);
// Act: Height is set to equal to max height, width smaller than min
// width.
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, 1200 /*1500*/, 720 /*900*/,
&states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123);
sl_host_surface_commit(nullptr, window->paired_surface->resource);
Pump();
// Assert: viewport size and pointer scale are set, width height are
// unchanged.
EXPECT_TRUE(window->viewport_override);
EXPECT_EQ(window->viewport_width, 1200);
EXPECT_EQ(window->viewport_height, 581);
EXPECT_FLOAT_EQ(window->viewport_pointer_scale, 1.1);
EXPECT_EQ(window->width, 1650);
EXPECT_EQ(window->height, 800);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, -1, -1))
.Times(1);
// Act: Configure with the size that is within the window bounds.
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, 1360 /*1700*/, 680 /*850*/,
&states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 124);
window->paired_surface->contents_width = 1700;
window->paired_surface->contents_height = 850;
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, 1360, 680))
.Times(1);
// Act
sl_host_surface_commit(nullptr, window->paired_surface->resource);
Pump();
// Assert: viewport override is no longer used.
EXPECT_FALSE(window->viewport_override);
EXPECT_EQ(window->viewport_width, -1);
EXPECT_EQ(window->viewport_height, -1);
EXPECT_EQ(window->width, 1700);
EXPECT_EQ(window->height, 850);
}
TEST_F(X11DirectScaleTest, ViewportOverrideSameAspectRatio) {
// Arrange
ctx.viewport_resize = true;
AdvertiseOutputs(xwayland.get(),
{{.logical_width = 1536, .logical_height = 864}});
sl_window* window = CreateToplevelWindow();
window->managed = true;
const int pixel_width = 1920;
const int pixel_height = 1080;
window->max_width = pixel_width;
window->min_height = pixel_height;
window->min_width = pixel_width;
window->max_height = pixel_height;
window->paired_surface->contents_width = pixel_width;
window->paired_surface->contents_height = pixel_height;
window->width = pixel_width;
window->height = pixel_height;
wl_array states;
wl_array_init(&states);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, 1280, 720))
.Times(1);
// Act: Height and width squished while maintaining aspect ratio.
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, 1280 /*1600*/, 720 /*900*/,
&states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123);
sl_host_surface_commit(nullptr, window->paired_surface->resource);
Pump();
// Assert: viewport size and pointer scale are set, width height are
// unchanged.
EXPECT_TRUE(window->viewport_override);
EXPECT_EQ(window->viewport_width, 1280);
EXPECT_EQ(window->viewport_height, 720);
EXPECT_FLOAT_EQ(window->viewport_pointer_scale, 1.2);
EXPECT_EQ(window->width, pixel_width);
EXPECT_EQ(window->height, pixel_height);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, -1, -1))
.Times(1);
// Act: Configure with the size that is within the window bounds.
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, 1536, 864, &states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123);
// Assert
EXPECT_CALL(mock_viewport_shim_,
set_destination(window->paired_surface->viewport, 1536, 864))
.Times(1);
// Act
sl_host_surface_commit(nullptr, window->paired_surface->resource);
Pump();
// Assert: viewport override is no longer used.
EXPECT_FALSE(window->viewport_override);
EXPECT_EQ(window->viewport_width, -1);
EXPECT_EQ(window->viewport_height, -1);
EXPECT_EQ(window->width, 1920);
EXPECT_EQ(window->height, 1080);
}
TEST_F(X11Test, TogglesFullscreenOnWmStateFullscreen) {
// Arrange: Create an xdg_toplevel surface. Initially it's not fullscreen.
AdvertiseOutputs(xwayland.get(), {OutputConfig()});
sl_window* window = CreateToplevelWindow();
uint32_t xdg_toplevel_id = XdgToplevelId(window);
EXPECT_EQ(window->fullscreen, 0);
Pump(); // exclude pending messages from EXPECT_CALL()s below
// Act: Pretend the window is owned by an X11 client requesting fullscreen.
// Sommelier receives the XCB_CLIENT_MESSAGE request due to its role as the
// X11 window manager. For test purposes, we skip creating a real X11
// connection and just call directly into the relevant handler.
xcb_client_message_event_t event;
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.window = window->id;
event.type = ctx.atoms[ATOM_NET_WM_STATE].value;
event.data.data32[0] = NET_WM_STATE_ADD;
event.data.data32[1] = ctx.atoms[ATOM_NET_WM_STATE_FULLSCREEN].value;
event.data.data32[2] = 0;
event.data.data32[3] = 0;
event.data.data32[4] = 0;
sl_handle_client_message(&ctx, &event);
// Assert: Sommelier records the fullscreen state.
EXPECT_EQ(window->fullscreen, 1);
// Assert: Sommelier forwards the fullscreen request to Exo.
EXPECT_CALL(
mock_wayland_channel_,
send(ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_SET_FULLSCREEN)))
.RetiresOnSaturation();
Pump();
// Act: Pretend the fictitious X11 client requests non-fullscreen.
event.data.data32[0] = NET_WM_STATE_REMOVE;
sl_handle_client_message(&ctx, &event);
// Assert: Sommelier records the fullscreen state.
EXPECT_EQ(window->fullscreen, 0);
// Assert: Sommelier forwards the unfullscreen request to Exo.
EXPECT_CALL(
mock_wayland_channel_,
send(ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_UNSET_FULLSCREEN)))
.RetiresOnSaturation();
}
TEST_F(X11Test, TogglesMaximizeOnWmStateMaximize) {
// Arrange: Create an xdg_toplevel surface. Initially it's not maximized.
sl_window* window = CreateToplevelWindow();
uint32_t xdg_toplevel_id = XdgToplevelId(window);
EXPECT_EQ(window->maximized, 0);
Pump(); // exclude pending messages from EXPECT_CALL()s below
// Act: Pretend an X11 client owns the surface, and requests to maximize it.
xcb_client_message_event_t event;
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.window = window->id;
event.type = ctx.atoms[ATOM_NET_WM_STATE].value;
event.data.data32[0] = NET_WM_STATE_ADD;
event.data.data32[1] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_HORZ].value;
event.data.data32[2] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_VERT].value;
event.data.data32[3] = 0;
event.data.data32[4] = 0;
sl_handle_client_message(&ctx, &event);
// Assert: Sommelier records the maximized state + forwards to Exo.
EXPECT_EQ(window->maximized, 1);
EXPECT_CALL(
mock_wayland_channel_,
send(ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_SET_MAXIMIZED)))
.RetiresOnSaturation();
Pump();
// Act: Pretend the fictitious X11 client requests to unmaximize.
event.data.data32[0] = NET_WM_STATE_REMOVE;
sl_handle_client_message(&ctx, &event);
// Assert: Sommelier records the unmaximized state + forwards to Exo.
EXPECT_EQ(window->maximized, 0);
EXPECT_CALL(
mock_wayland_channel_,
send(ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_UNSET_MAXIMIZED)))
.RetiresOnSaturation();
Pump();
}
TEST_F(X11Test, CanEnterFullscreenIfAlreadyMaximized) {
// Arrange
sl_window* window = CreateToplevelWindow();
uint32_t xdg_toplevel_id = XdgToplevelId(window);
Pump(); // exclude pending messages from EXPECT_CALL()s below
// Act: Pretend an X11 client owns the surface, and requests to maximize it.
xcb_client_message_event_t event;
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.window = window->id;
event.type = ctx.atoms[ATOM_NET_WM_STATE].value;
event.data.data32[0] = NET_WM_STATE_ADD;
event.data.data32[1] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_HORZ].value;
event.data.data32[2] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_VERT].value;
event.data.data32[3] = 0;
event.data.data32[4] = 0;
sl_handle_client_message(&ctx, &event);
// Assert: Sommelier records the maximized state + forwards to Exo.
EXPECT_EQ(window->maximized, 1);
EXPECT_CALL(
mock_wayland_channel_,
send(ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_SET_MAXIMIZED)))
.RetiresOnSaturation();
Pump();
// Act: Pretend the X11 client requests fullscreen.
xcb_client_message_event_t fsevent;
fsevent.response_type = XCB_CLIENT_MESSAGE;
fsevent.format = 32;
fsevent.window = window->id;
fsevent.type = ctx.atoms[ATOM_NET_WM_STATE].value;
fsevent.data.data32[0] = NET_WM_STATE_ADD;
fsevent.data.data32[1] = 0;
fsevent.data.data32[2] = ctx.atoms[ATOM_NET_WM_STATE_FULLSCREEN].value;
fsevent.data.data32[3] = 0;
fsevent.data.data32[4] = 0;
sl_handle_client_message(&ctx, &fsevent);
// Assert: Sommelier records the fullscreen state + forwards to Exo.
EXPECT_EQ(window->fullscreen, 1);
EXPECT_CALL(
mock_wayland_channel_,
send(ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_SET_FULLSCREEN)))
.RetiresOnSaturation();
Pump();
}
TEST_F(X11Test, UpdatesApplicationIdFromContext) {
sl_window* window = CreateToplevelWindow();
Pump();
window->managed = 1; // pretend window is mapped
// Should be ignored; global app id from context takes priority.
window->app_id_property = "org.chromium.guest_os.termina.appid.from.window";
ctx.application_id = "org.chromium.guest_os.termina.appid.from.context";
sl_update_application_id(&ctx, window);
EXPECT_CALL(mock_wayland_channel_,
send(AllOf(ExactlyOneMessage(AuraSurfaceId(window),
ZAURA_SURFACE_SET_APPLICATION_ID),
AnyMessageContainsString(ctx.application_id))))
.RetiresOnSaturation();
Pump();
}
TEST_F(X11Test, UpdatesApplicationIdFromWindow) {
sl_window* window = CreateToplevelWindow();
Pump();
window->managed = 1; // pretend window is mapped
window->app_id_property = "org.chromium.guest_os.termina.appid.from.window";
sl_update_application_id(&ctx, window);
EXPECT_CALL(mock_wayland_channel_,
send(AllOf(ExactlyOneMessage(AuraSurfaceId(window),
ZAURA_SURFACE_SET_APPLICATION_ID),
AnyMessageContainsString(window->app_id_property))))
.RetiresOnSaturation();
Pump();
}
TEST_F(X11Test, UpdatesApplicationIdFromWindowClass) {
sl_window* window = CreateToplevelWindow();
Pump();
window->managed = 1; // pretend window is mapped
window->clazz = strdup("very_classy"); // not const, can't use a literal
ctx.vm_id = "testvm";
sl_update_application_id(&ctx, window);
EXPECT_CALL(
mock_wayland_channel_,
send(AllOf(ExactlyOneMessage(AuraSurfaceId(window),
ZAURA_SURFACE_SET_APPLICATION_ID),
AnyMessageContainsString(
"org.chromium.guest_os.testvm.wmclass.very_classy"))))
.RetiresOnSaturation();
Pump();
free(window->clazz);
}
TEST_F(X11Test, UpdatesApplicationIdFromClientLeader) {
sl_window* window = CreateToplevelWindow();
Pump();
window->managed = 1; // pretend window is mapped
window->client_leader = window->id;
ctx.vm_id = "testvm";
sl_update_application_id(&ctx, window);
EXPECT_CALL(mock_wayland_channel_,
send(AllOf(ExactlyOneMessage(AuraSurfaceId(window),
ZAURA_SURFACE_SET_APPLICATION_ID),
AnyMessageContainsString(
"org.chromium.guest_os.testvm.wmclientleader."))))
.RetiresOnSaturation();
Pump();
}
TEST_F(X11Test, UpdatesApplicationIdFromXid) {
sl_window* window = CreateToplevelWindow();
Pump();
window->managed = 1; // pretend window is mapped
ctx.vm_id = "testvm";
sl_update_application_id(&ctx, window);
EXPECT_CALL(mock_wayland_channel_,
send(AllOf(ExactlyOneMessage(AuraSurfaceId(window),
ZAURA_SURFACE_SET_APPLICATION_ID),
AnyMessageContainsString(
"org.chromium.guest_os.testvm.xid."))))
.RetiresOnSaturation();
Pump();
}
TEST_F(X11Test, NonExistentWindowDoesNotCrash) {
// This test is testing cases where sl_lookup_window returns nullptr
// sl_handle_destroy_notify
xcb_destroy_notify_event_t destroy_event;
// Arrange: Use a window that does not exist.
destroy_event.window = 123;
// Act/Assert: Sommelier does not crash.
sl_handle_destroy_notify(&ctx, &destroy_event);
// sl_handle_client_message
xcb_client_message_event_t message_event;
message_event.window = 123;
message_event.data.data32[0] = WM_STATE_ICONIC;
message_event.type = ctx.atoms[ATOM_WL_SURFACE_ID].value;
sl_handle_client_message(&ctx, &message_event);
message_event.type = ctx.atoms[ATOM_NET_ACTIVE_WINDOW].value;
sl_handle_client_message(&ctx, &message_event);
message_event.type = ctx.atoms[ATOM_NET_WM_MOVERESIZE].value;
sl_handle_client_message(&ctx, &message_event);
message_event.type = ctx.atoms[ATOM_NET_WM_STATE].value;
sl_handle_client_message(&ctx, &message_event);
message_event.type = ctx.atoms[ATOM_WM_CHANGE_STATE].value;
sl_handle_client_message(&ctx, &message_event);
// sl_handle_map_request
xcb_map_request_event_t map_event;
map_event.window = 123;
sl_handle_map_request(&ctx, &map_event);
// sl_handle_unmap_notify
xcb_unmap_notify_event_t unmap_event;
unmap_event.window = 123;
unmap_event.response_type = 0;
sl_handle_unmap_notify(&ctx, &unmap_event);
// sl_handle_configure_request
xcb_configure_request_event_t configure_event;
configure_event.window = 123;
sl_handle_configure_request(&ctx, &configure_event);
// sl_handle_focus_in
xcb_focus_in_event_t focus_event;
focus_event.event = 123;
sl_handle_focus_in(&ctx, &focus_event);
// sl_handle_property_notify
xcb_property_notify_event_t notify_event;
notify_event.window = 123;
notify_event.atom = XCB_ATOM_WM_NAME;
sl_handle_property_notify(&ctx, ¬ify_event);
notify_event.atom = XCB_ATOM_WM_CLASS;
sl_handle_property_notify(&ctx, ¬ify_event);
notify_event.atom = ctx.application_id_property_atom;
sl_handle_property_notify(&ctx, ¬ify_event);
notify_event.atom = XCB_ATOM_WM_NORMAL_HINTS;
sl_handle_property_notify(&ctx, ¬ify_event);
notify_event.atom = XCB_ATOM_WM_HINTS;
sl_handle_property_notify(&ctx, ¬ify_event);
notify_event.atom = ATOM_MOTIF_WM_HINTS;
sl_handle_property_notify(&ctx, ¬ify_event);
notify_event.atom = ATOM_GTK_THEME_VARIANT;
sl_handle_property_notify(&ctx, ¬ify_event);
// sl_handle_reparent_notify
// Put this one last and used a different window id as it creates a window.
xcb_reparent_notify_event_t reparent_event;
reparent_event.window = 1234;
xcb_screen_t screen;
screen.root = 1234;
ctx.screen = &screen;
reparent_event.parent = ctx.screen->root;
reparent_event.x = 0;
reparent_event.y = 0;
sl_handle_reparent_notify(&ctx, &reparent_event);
}
#ifdef QUIRKS_SUPPORT
TEST_F(X11Test, IconifySuppressesFullscreen) {
// Arrange: Create an xdg_toplevel surface. Initially it's not iconified.
sl_window* window = CreateToplevelWindow();
uint32_t xdg_toplevel_id = XdgToplevelId(window);
EXPECT_EQ(window->iconified, 0);
window->steam_game_id = 123;
ctx.quirks.Load(
"sommelier { \n"
" condition { steam_game_id: 123 }\n"
" enable: FEATURE_BLACK_SCREEN_FIX\n"
"}");
// Act: Pretend an X11 client owns the surface, and requests to iconify it.
xcb_client_message_event_t event;
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.window = window->id;
event.type = ctx.atoms[ATOM_WM_CHANGE_STATE].value;
event.data.data32[0] = WM_STATE_ICONIC;
sl_handle_client_message(&ctx, &event);
Pump();
// Assert: Sommelier records the iconified state.
EXPECT_EQ(window->iconified, 1);
// Act: Pretend the surface is requested to be fullscreened.
event.type = ctx.atoms[ATOM_NET_WM_STATE].value;
event.data.data32[0] = NET_WM_STATE_ADD;
event.data.data32[1] = ctx.atoms[ATOM_NET_WM_STATE_FULLSCREEN].value;
event.data.data32[2] = 0;
event.data.data32[3] = 0;
event.data.data32[4] = 0;
sl_handle_client_message(&ctx, &event);
// Assert: Sommelier should not send the fullscreen call as we are iconified.
EXPECT_CALL(
mock_wayland_channel_,
send((ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_SET_FULLSCREEN))))
.Times(0);
Pump();
// Act: Pretend the surface receives focus.
xcb_focus_in_event_t focus_event;
focus_event.response_type = XCB_FOCUS_IN;
focus_event.event = window->id;
sl_handle_focus_in(&ctx, &focus_event);
// Assert: The window is deiconified.
EXPECT_EQ(window->iconified, 0);
// Assert: Sommelier should now send the fullscreen call.
EXPECT_CALL(
mock_wayland_channel_,
send((ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_SET_FULLSCREEN))))
.Times(1);
Pump();
}
TEST_F(X11Test, IconifySuppressesUnmaximize) {
// Arrange: Create an xdg_toplevel surface. Initially it's not iconified.
sl_window* window = CreateToplevelWindow();
uint32_t xdg_toplevel_id = XdgToplevelId(window);
EXPECT_EQ(window->iconified, 0);
window->steam_game_id = 123;
ctx.quirks.Load(
"sommelier { \n"
" condition { steam_game_id: 123 }\n"
" enable: FEATURE_BLACK_SCREEN_FIX\n"
"}");
// Arrange: Maximize it.
xcb_client_message_event_t event;
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.window = window->id;
event.type = ctx.atoms[ATOM_NET_WM_STATE].value;
event.data.data32[0] = NET_WM_STATE_ADD;
event.data.data32[1] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_VERT].value;
event.data.data32[2] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_HORZ].value;
event.data.data32[3] = 0;
event.data.data32[4] = 0;
sl_handle_client_message(&ctx, &event);
EXPECT_EQ(window->maximized, 1);
// Act: Pretend an X11 client owns the surface, and requests to iconify it.
event.type = ctx.atoms[ATOM_WM_CHANGE_STATE].value;
event.data.data32[0] = WM_STATE_ICONIC;
sl_handle_client_message(&ctx, &event);
Pump();
// Assert: Sommelier records the iconified state.
EXPECT_EQ(window->iconified, 1);
// Act: Pretend the surface is requested to be unmaximized.
event.type = ctx.atoms[ATOM_NET_WM_STATE].value;
event.data.data32[0] = NET_WM_STATE_REMOVE;
event.data.data32[1] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_VERT].value;
event.data.data32[2] = ctx.atoms[ATOM_NET_WM_STATE_MAXIMIZED_HORZ].value;
event.data.data32[3] = 0;
event.data.data32[4] = 0;
sl_handle_client_message(&ctx, &event);
// Assert: Sommelier should not send the unmiximize call as we are iconified.
EXPECT_CALL(
mock_wayland_channel_,
send((ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_UNSET_MAXIMIZED))))
.Times(0);
Pump();
// Act: Pretend the surface receives focus.
xcb_focus_in_event_t focus_event;
focus_event.response_type = XCB_FOCUS_IN;
focus_event.event = window->id;
sl_handle_focus_in(&ctx, &focus_event);
// Assert: The window is deiconified.
EXPECT_EQ(window->iconified, 0);
// Assert: Sommelier should now send the unmiximize call.
EXPECT_CALL(
mock_wayland_channel_,
send((ExactlyOneMessage(xdg_toplevel_id, XDG_TOPLEVEL_UNSET_MAXIMIZED))))
.Times(1);
Pump();
}
#endif // QUIRKS_SUPPORT
// Matcher for the value_list argument of an X11 ConfigureWindow request,
// which is a const void* pointing to an int array whose size is implied by
// the flags argument.
MATCHER_P(ValueListMatches, expected, "") {
const int* value_ptr = static_cast<const int*>(arg);
std::vector<int> values;
for (std::vector<int>::size_type i = 0; i < expected.size(); i++) {
values.push_back(value_ptr[i]);
}
*result_listener << PrintToString(values);
return values == expected;
}
// Matcher for the data argument of an X11 ChangeProperty request,
// which is a const void* pointing to an uint32_t array whose size is implied by
// the flags argument. Hence, this does not test if argument exceeds the size of
// expected since we have to explicitly state the size in the function call.
MATCHER_P(ChangePropertyDataMatches, expected, "") {
const uint32_t* received = static_cast<const uint32_t*>(arg);
for (uint32_t i = 0; i < expected.size(); i++) {
if (received[i] != expected[i]) {
return false;
}
}
return true;
}
TEST_F(X11Test, XdgToplevelConfigureTriggersX11Configure) {
// Arrange
AdvertiseOutputs(xwayland.get(), {OutputConfig()});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
window->size_flags = 0; // no hinted position or size
const int width = 1024;
const int height = 768;
// Assert: Set up expectations for Sommelier to send appropriate X11 requests.
// (output width/height - width/height) / 2
int x = 448;
int y = 156;
EXPECT_CALL(xcb, configure_window(
testing::_, window->frame_id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({x, y, width, height, 0}))))
.Times(1);
EXPECT_CALL(xcb, configure_window(
testing::_, window->id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({0, 0, width, height, 0}))))
.Times(1);
// Act: Pretend the host compositor sends us some xdg configure events.
wl_array states;
wl_array_init(&states);
uint32_t* state =
static_cast<uint32_t*>(wl_array_add(&states, sizeof(uint32_t)));
*state = XDG_TOPLEVEL_STATE_ACTIVATED;
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, width, height, &states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123 /* serial */);
}
TEST_F(X11Test, XdgToplevelConfigureCentersWindowOnRotatedOutput) {
// Arrange
AdvertiseOutputs(xwayland.get(), {{.transform = WL_OUTPUT_TRANSFORM_90}});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
window->size_flags = 0; // no hinted position or size
const int width = 1024;
const int height = 768;
// Assert: Set up expectations for Sommelier to send appropriate X11 requests.
// (rotated output width/height - width/height) / 2
int x = 28;
int y = 576;
EXPECT_CALL(xcb, configure_window(
testing::_, window->frame_id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({x, y, width, height, 0}))))
.Times(1);
EXPECT_CALL(xcb, configure_window(
testing::_, window->id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({0, 0, width, height, 0}))))
.Times(1);
// Act: Pretend the host compositor sends us some xdg configure events.
wl_array states;
wl_array_init(&states);
uint32_t* state =
static_cast<uint32_t*>(wl_array_add(&states, sizeof(uint32_t)));
*state = XDG_TOPLEVEL_STATE_ACTIVATED;
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, width, height, &states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123 /* serial */);
}
TEST_F(X11Test,
XdgToplevelConfigureCentersWindowCorrectlyWhenMultipleOutputsExist) {
// Arrange
AdvertiseOutputs(
xwayland.get(),
{{.x = 0, .y = 0, .width_pixels = 1920, .height_pixels = 1080},
{.x = 1920, .y = 500}});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
window->size_flags = 0; // no hinted position or size
const int width = 1024;
const int height = 768;
struct sl_host_output* output = nullptr;
output = ctx.host_outputs[1];
HostEventHandler(window->paired_surface->proxy)
->enter(nullptr, window->paired_surface->proxy, output->proxy);
// Assert: Set up expectations for Sommelier to send appropriate X11 requests.
int x = 1920 + 448;
int y = 156;
EXPECT_CALL(xcb, configure_window(
testing::_, window->frame_id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({x, y, width, height, 0}))))
.Times(1);
EXPECT_CALL(xcb, configure_window(
testing::_, window->id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({0, 0, width, height, 0}))))
.Times(1);
// Act: Pretend the host compositor sends us some xdg configure events.
wl_array states;
wl_array_init(&states);
uint32_t* state =
static_cast<uint32_t*>(wl_array_add(&states, sizeof(uint32_t)));
*state = XDG_TOPLEVEL_STATE_ACTIVATED;
HostEventHandler(window->xdg_toplevel)
->configure(nullptr, window->xdg_toplevel, width, height, &states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123 /* serial */);
}
TEST_F(X11DirectScaleTest, AuraToplevelConfigureTriggersX11Configure) {
// Arrange
ctx.enable_x11_move_windows = true;
AdvertiseOutputs(xwayland.get(), {OutputConfig()});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
window->size_flags = 0; // no hinted position or size
const int x = 50;
const int y = 60;
const int width = 1024;
const int height = 768;
// Assert: Set up expectations for Sommelier to send appropriate X11 requests.
EXPECT_CALL(xcb, configure_window(
testing::_, window->frame_id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({x, y, width, height, 0}))))
.Times(1);
EXPECT_CALL(xcb, configure_window(
testing::_, window->id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector({0, 0, width, height, 0}))))
.Times(1);
// Act: Pretend the host compositor sends us some configure events.
wl_array states;
wl_array_init(&states);
uint32_t* state =
static_cast<uint32_t*>(wl_array_add(&states, sizeof(uint32_t)));
*state = XDG_TOPLEVEL_STATE_ACTIVATED;
HostEventHandler(window->aura_toplevel)
->configure(nullptr, window->aura_toplevel, x, y, width, height, &states);
HostEventHandler(window->xdg_surface)
->configure(nullptr, window->xdg_surface, 123 /* serial */);
}
TEST_F(X11Test, AuraToplevelOriginChangeTriggersX11Configure) {
// Arrange
ctx.enable_x11_move_windows = true;
AdvertiseOutputs(xwayland.get(), {OutputConfig()});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
window->size_flags = 0; // no hinted position or size
const int x = 50;
const int y = 60;
// Assert
EXPECT_CALL(xcb, configure_window(testing::_, window->frame_id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
ValueListMatches(std::vector({x, y}))))
.Times(1);
// Act
HostEventHandler(window->aura_toplevel)
->origin_change(nullptr, window->aura_toplevel, x, y);
}
// When the host compositor sends a window position, make sure we don't send
// a bounds request back. Otherwise we get glitching due to rounding and race
// conditions.
TEST_F(X11Test, AuraToplevelOriginChangeDoesNotRoundtrip) {
// Arrange
ctx.enable_x11_move_windows = true;
AdvertiseOutputs(xwayland.get(), {OutputConfig()});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
window->size_flags = 0; // no hinted position or size
const int x = 50;
const int y = 60;
// Assert: set_window_bounds() never sent.
EXPECT_CALL(mock_wayland_channel_,
send(testing::Not(AtLeastOneMessage(
AuraToplevelId(window), ZAURA_TOPLEVEL_SET_WINDOW_BOUNDS))))
.Times(testing::AtLeast(0));
// Act
HostEventHandler(window->aura_toplevel)
->origin_change(nullptr, window->aura_toplevel, x, y);
Pump();
}
TEST_F(X11Test, X11ConfigureRequestPositionIsForwardedToAuraHost) {
// Arrange
ctx.enable_x11_move_windows = true;
AdvertiseOutputs(xwayland.get(), {OutputConfig()});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
Pump(); // discard Wayland requests sent in setup
// Assert
EXPECT_CALL(mock_wayland_channel_,
send(AtLeastOneMessage(AuraToplevelId(window),
ZAURA_TOPLEVEL_SET_WINDOW_BOUNDS)))
.RetiresOnSaturation();
// Act
xcb_configure_request_event_t configure = {
.response_type = XCB_CONFIGURE_REQUEST,
.sequence = 123,
.parent = window->frame_id,
.window = window->id,
.x = 10,
.y = 20,
.width = 300,
.height = 400,
.value_mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT};
sl_handle_configure_request(&ctx, &configure);
Pump();
}
TEST_F(X11Test,
X11ConfigureRequestPositionForwardingIgnoresStaleAuraToplevelConfigure) {
// Arrange
ctx.enable_x11_move_windows = true;
AdvertiseOutputs(xwayland.get(), {OutputConfig()});
sl_window* window = CreateToplevelWindow();
window->managed = 1; // pretend window is mapped
const int width = 300;
const int height = 400;
// Position requested by the client.
const int client_requested_x = 10;
const int client_requested_y = 0;
// Stale position received from the host compositor.
const int stale_x = 50;
const int stale_y = 60;
// Host compositor's adjusted response to the client's request.
// (In this scenario, it moved the window down so its server-side
// decorations wouldn't be offscreen.)
const int granted_x = client_requested_x;
const int granted_y = 32;
//
// Assert
//
// Barrier should prevent forwarding the host's stale coords to the X server.
EXPECT_CALL(
xcb, configure_window(testing::_, window->frame_id, testing::_,
ValueListMatches(std::vector({stale_x, stale_y}))))
.Times(0);
// Do forward the correct coordinates to the X server.
EXPECT_CALL(xcb, configure_window(
testing::_, window->frame_id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH,
ValueListMatches(std::vector(
{granted_x, granted_y, width, height, 0}))))
.Times(1);
// The reparented child window may also get configured.
// The details are not important for this test case.
EXPECT_CALL(xcb,
configure_window(testing::_, window->id, testing::_, testing::_))
.Times(testing::AtLeast(0));
//
// Act
//