File: wayland_extensions.cpp

package info (click to toggle)
mir 2.25.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,104 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (791 lines) | stat: -rw-r--r-- 23,197 bytes parent folder | download
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
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <miral/test_server.h>
#include "server_example_decoration.h"
#include "org_kde_kwin_server_decoration.h"

#include <mir/server.h>
#include <miral/internal_client.h>
#include <miral/wayland_extensions.h>

#include <wayland-client.h>

#include <memory>
#include <mutex>
#include <vector>

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <mir/abnormal_exit.h>

using namespace testing;

namespace
{
class WaylandClient
{
public:

    void operator()(struct wl_display* display)
    {
        code(display);
    }

    void operator()(std::weak_ptr<mir::scene::Session> const& session)
    {
        std::lock_guard lock{mutex};
        session_ = session;
    }

    std::shared_ptr<mir::scene::Session> session() const
    {
        std::lock_guard lock{mutex};
        return session_.lock();
    }

    std::function<void (struct wl_display*)> code = [](auto){};

private:
    std::mutex mutable mutex;
    std::weak_ptr<mir::scene::Session> session_;
};

struct WaylandExtensions : miral::TestServer
{
    WaylandExtensions()
    {
        start_server_in_setup = false;
        add_server_init(launcher);
    }

    void run_as_client(std::function<void (struct wl_display*)>&& code)
    {
        bool client_run = false;
        std::condition_variable cv;
        std::mutex mutex;

        client.code = [&](struct wl_display* display)
            {
                {
                    std::lock_guard lock{mutex};
                    code(display);
                    client_run = true;
                }
                cv.notify_one();
            };

        std::unique_lock lock{mutex};
        launcher.launch(client);
        cv.wait(lock, [&]{ return client_run; });
    }

private:
    miral::InternalClientLauncher launcher;
    WaylandClient client;
};

template<typename Type>
auto make_scoped(Type* owned, void(*deleter)(Type*)) -> std::unique_ptr<Type, void(*)(Type*)>
{
    return {owned, deleter};
}

struct ClientGlobalEnumerator
{
    void run()
    {
        auto display = wl_display_connect(nullptr);
        auto const registry = make_scoped(wl_display_get_registry(display), &wl_registry_destroy);
        wl_registry_add_listener(registry.get(), &registry_listener, nullptr);
        wl_display_roundtrip(display);
        wl_display_roundtrip(display);
    }

    static void new_global(
        void* /*data*/,
        struct wl_registry* /*registry*/,
        uint32_t /*id*/,
        char const* interface,
        uint32_t /*version*/)
    {
        std::cout << interface << std::endl;
    }

    static void global_remove(
        void* /*data*/,
        struct wl_registry* /*registry*/,
        uint32_t /*name*/)
    {

    }

    static wl_registry_listener constexpr registry_listener = {
        new_global,
        global_remove
    };
};

wl_registry_listener constexpr ClientGlobalEnumerator::registry_listener;

/// Runs the client enumerator in a subprocess.
///
/// Mir will allow clients that originate from the compositor itself to access
/// any protocols that they like. However, we would like to test the case
/// where an external client asks the compositor for exstensions that it does
/// not have access to. To do this, we need to fork this process, record the
/// interfaces that come out of the child process, and return them to the
/// test code via a pipe.
///
/// \param server
/// \returns the list of interfaces that are available to the process
std::vector<std::string> run_client_enumerator(mir::Server& server)
{
    std::vector<std::string> interfaces;

    int pipefd[2];
    if (pipe(pipefd) == -1)
    {
        std::cerr << "run_client_enumerator: pipe failed" << std::endl;
        return {};
    }

    auto const pid = fork();
    if (pid == 0)
    {
        close(pipefd[0]);
        dup2(pipefd[1], STDOUT_FILENO);
        close(pipefd[1]);

        auto const wayland_display = server.wayland_display();
        setenv("WAYLAND_DISPLAY", wayland_display.value().c_str(),  true);

        ClientGlobalEnumerator enumerator;
        enumerator.run();
        _exit(0);
    }

    close(pipefd[1]);
    FILE *stream = fdopen(pipefd[0], "r");
    if (!stream)
    {
        std::cerr << "run_client_enumerator: fdopen failed" << std::endl;
        return {};
    }

    char line[256];
    while (fgets(line, sizeof(line), stream))
    {
        size_t const len = strlen(line);
        interfaces.emplace_back(line, line + len - 1);
    }
    fclose(stream);

    int status;
    waitpid(pid, &status, 0);
    return interfaces;
}

struct ClientDecorationCreator
{
    ClientDecorationCreator(std::function<void()>&& test) :
        test{test} {}

    std::function<void()> test = []{};

    void operator()(wl_display* display)
    {
        auto const registry = make_scoped(wl_display_get_registry(display), &wl_registry_destroy);
        wl_registry_add_listener(registry.get(), &registry_listener, this);
        wl_display_roundtrip(display);
        wl_display_roundtrip(display);

        ASSERT_THAT(compositor, NotNull());
        ASSERT_THAT(decoration_manager, NotNull());
        ASSERT_THAT(shell, NotNull());

        auto const surface = make_scoped(
            wl_compositor_create_surface(compositor),
            &wl_surface_destroy);
        wl_display_roundtrip(display);

        auto const decoration = make_scoped(
            org_kde_kwin_server_decoration_manager_create(decoration_manager, surface.get()),
            &org_kde_kwin_server_decoration_release);
        wl_display_roundtrip(display);

        auto const window = make_scoped(wl_shell_get_shell_surface(shell, surface.get()), &wl_shell_surface_destroy);
        wl_shell_surface_set_toplevel(window.get());
        wl_display_roundtrip(display);

        wl_surface_commit(surface.get());
        wl_display_roundtrip(display);

        test();
    }

    static void new_global(
        void* data,
        struct wl_registry* registry,
        uint32_t id,
        char const* interface,
        uint32_t /*version*/)
    {
        ClientDecorationCreator* self = static_cast<decltype(self)>(data);

        if (strcmp(interface, wl_compositor_interface.name) == 0)
        {
            self->compositor = static_cast<decltype(self->compositor)>
            (wl_registry_bind(registry, id, &wl_compositor_interface, 1));
        }

        if (strcmp(interface, org_kde_kwin_server_decoration_manager_interface.name) == 0)
        {
            self->decoration_manager = static_cast<decltype(self->decoration_manager)>
                (wl_registry_bind(registry, id, &org_kde_kwin_server_decoration_manager_interface, 1));
        }

        if (strcmp(interface, wl_shell_interface.name) == 0)
        {
            self->shell = static_cast<decltype(self->shell)>(wl_registry_bind(registry, id, &wl_shell_interface, 1));
        }
    }

    static void global_remove(
        void* /*data*/,
        struct wl_registry* /*registry*/,
        uint32_t /*name*/)
    {

    }

    static wl_registry_listener constexpr registry_listener = {
        new_global,
        global_remove
    };

    wl_compositor* compositor = nullptr;
    org_kde_kwin_server_decoration_manager* decoration_manager = nullptr;
    wl_shell* shell = nullptr;
};

wl_registry_listener constexpr ClientDecorationCreator::registry_listener;
}

TEST_F(WaylandExtensions, client_connects)
{
    bool client_connected = false;
    start_server();

    run_as_client([&](auto) { client_connected = true; });

    EXPECT_THAT(client_connected, Eq(true));
}

TEST_F(WaylandExtensions, client_sees_default_extensions)
{

    start_server();

    auto const interfaces = run_client_enumerator(server());

    auto const available_extensions = miral::WaylandExtensions::recommended();

    // Some extension names do not include the *_manager like their globals's name does.
    // Fix by adding a non-manager version to the available list for each interface that contains "_manager".
    std::vector<std::string> available = interfaces;
    std::string const to_remove = "_manager";
    for (unsigned i = 0; i < available.size(); i++)
    {
        auto const pos = available[i].find(to_remove);
        if (pos != std::string::npos)
        {
            auto str = available[i];
            str.erase(pos, to_remove.size());
            available.push_back(str);
        }
    }

    for (auto const& extension : available_extensions)
    {
        EXPECT_THAT(available, Contains(Eq(extension)));
    }
}

TEST_F(WaylandExtensions, add_extension_adds_protocol_to_supported_enabled_extensions)
{
    miral::WaylandExtensions extensions;

    EXPECT_THAT(extensions.all_supported(), Not(Contains(Eq(mir::examples::server_decoration_extension().name))));

    extensions.add_extension(mir::examples::server_decoration_extension());

    EXPECT_THAT(extensions.all_supported(), Contains(Eq(mir::examples::server_decoration_extension().name)));
}

TEST_F(WaylandExtensions, add_extension_disabled_by_default_adds_protocol_to_supported_extensions_only)
{
    miral::WaylandExtensions extensions;
    extensions.add_extension_disabled_by_default(mir::examples::server_decoration_extension());


    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Not(Contains(Eq(mir::examples::server_decoration_extension().name))));
}

TEST_F(WaylandExtensions, can_retrieve_application_for_client)
{
    miral::WaylandExtensions extensions;
    wl_client* client = nullptr;

    extensions.add_extension(mir::examples::server_decoration_extension([&](wl_client* c, auto){ client = c; }));

    add_server_init(extensions);
    start_server();

    run_as_client(ClientDecorationCreator{[&]
        {
            EXPECT_THAT(client, NotNull());
            EXPECT_THAT(miral::application_for(client), NotNull());
        }});
}

TEST_F(WaylandExtensions, can_retrieve_application_for_surface)
{
    miral::WaylandExtensions extensions;

    wl_resource* surface = nullptr;
    extensions.add_extension(mir::examples::server_decoration_extension([&](auto, wl_resource* s){ surface =s; }));

    add_server_init(extensions);
    start_server();

    run_as_client(ClientDecorationCreator{[&]
        {
            EXPECT_THAT(surface, NotNull());
            EXPECT_THAT(miral::application_for(surface), NotNull());
        }});
}


TEST_F(WaylandExtensions, can_retrieve_window_for_surface)
{
    miral::WaylandExtensions extensions;

    wl_resource* surface = nullptr;
    extensions.add_extension(mir::examples::server_decoration_extension([&](auto, wl_resource* s){ surface =s; }));

    add_server_init(extensions);
    start_server();

    run_as_client(ClientDecorationCreator{[&]
        {
            EXPECT_THAT(surface, NotNull());
            EXPECT_THAT(miral::window_for(surface), Ne(nullptr));   // NotNull() fails to build on 16.04LTS
        }});
}

TEST_F(WaylandExtensions, some_extensions_are_supported)
{
    EXPECT_THAT(miral::WaylandExtensions::supported(), Not(IsEmpty()));
}

TEST_F(WaylandExtensions, some_extensions_are_recommended)
{
    EXPECT_THAT(miral::WaylandExtensions::recommended(), Not(IsEmpty()));
}

TEST_F(WaylandExtensions, recommeded_extensions_subset_of_supported_extensions)
{
    for (auto const& e : miral::WaylandExtensions::recommended())
    {
        EXPECT_THAT(miral::WaylandExtensions::supported(), Contains(e));
    }
}

TEST_F(WaylandExtensions, disable_can_remove_default_extensions)
{
    std::string const extension_to_remove{"zxdg_shell_v6"};
    miral::WaylandExtensions extensions;
    extensions.disable(extension_to_remove);

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Not(Contains(Eq(extension_to_remove))));
}

TEST_F(WaylandExtensions, enable_can_enable_non_standard_extensions)
{
    std::string const extension_to_enable{"zwlr_layer_shell_v1"};
    miral::WaylandExtensions extensions;
    extensions.enable(extension_to_enable);

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq(extension_to_enable)));
}

TEST_F(WaylandExtensions, enable_can_enable_bespoke_extension)
{
    miral::WaylandExtensions extensions;
    extensions.add_extension_disabled_by_default(mir::examples::server_decoration_extension());
    extensions.enable(mir::examples::server_decoration_extension().name);


    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq(mir::examples::server_decoration_extension().name)));
}

TEST_F(WaylandExtensions, disable_can_disable_bespoke_extension)
{
    miral::WaylandExtensions extensions;
    extensions.add_extension(mir::examples::server_decoration_extension());
    extensions.disable(mir::examples::server_decoration_extension().name);


    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Not(Contains(Eq(mir::examples::server_decoration_extension().name))));
}

TEST_F(WaylandExtensions, cannot_duplicate_existing_extension)
{
    miral::WaylandExtensions::Builder duplicate_extension{
        "zwlr_layer_shell_v1",
        [&](auto const&){ return std::shared_ptr<void>{};}};

    miral::WaylandExtensions extensions;


    EXPECT_THROW(
        extensions.add_extension(duplicate_extension),
        mir::AbnormalExit);

    EXPECT_THROW(
        extensions.add_extension_disabled_by_default(duplicate_extension),
        mir::AbnormalExit);
}

TEST_F(WaylandExtensions, cannot_duplicate_bespoke_extension)
{
    miral::WaylandExtensions extensions;


    extensions.add_extension(mir::examples::server_decoration_extension());

    EXPECT_THROW(
        extensions.add_extension(mir::examples::server_decoration_extension()),
        mir::AbnormalExit);

    EXPECT_THROW(
        extensions.add_extension_disabled_by_default(mir::examples::server_decoration_extension()),
        mir::AbnormalExit);
}

TEST_F(WaylandExtensions, wayland_extensions_option_sets_extensions)
{
    miral::WaylandExtensions extensions;


    add_to_environment("MIR_SERVER_WAYLAND_EXTENSIONS", "xdg_wm_base:zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq("xdg_wm_base")));
    EXPECT_THAT(interfaces, Contains(Eq("zwlr_layer_shell_v1")));
    EXPECT_THAT(interfaces, Not(Contains(Eq("wl_shell"))));
}

TEST_F(WaylandExtensions, add_extensions_option_adds_extensions)
{
    miral::WaylandExtensions extensions;


    add_to_environment("MIR_SERVER_ADD_WAYLAND_EXTENSIONS", "xdg_wm_base:zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq("wl_shell")));
    EXPECT_THAT(interfaces, Contains(Eq("xdg_wm_base")));
    EXPECT_THAT(interfaces, Contains(Eq("zwlr_layer_shell_v1")));
}

TEST_F(WaylandExtensions, drop_extensions_option_removes_extensions)
{
    miral::WaylandExtensions extensions;


    add_to_environment("MIR_SERVER_DROP_WAYLAND_EXTENSIONS", "xdg_wm_base:zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq("wl_shell")));
    EXPECT_THAT(interfaces, Not(Contains(Eq("xdg_wm_base"))));
    EXPECT_THAT(interfaces, Not(Contains(Eq("zwlr_layer_shell_v1"))));
}

TEST_F(WaylandExtensions, add_extensions_option_adds_extensions_if_name_is_not_global_name)
{
    miral::WaylandExtensions extensions;


    add_to_environment("MIR_SERVER_ADD_WAYLAND_EXTENSIONS", "zwp_input_method_v2:zwp_virtual_keyboard_v1");

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq("zwp_input_method_manager_v2")));
    EXPECT_THAT(interfaces, Contains(Eq("zwp_virtual_keyboard_manager_v1")));
}

TEST_F(WaylandExtensions, drop_extensions_option_overrides_enable)
{
    miral::WaylandExtensions extensions;
    extensions.enable("zwlr_layer_shell_v1");


    add_to_environment("MIR_SERVER_DROP_WAYLAND_EXTENSIONS", "zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Not(Contains(Eq("zwlr_layer_shell_v1"))));
}

TEST_F(WaylandExtensions, add_extensions_option_overrides_disable)
{
    miral::WaylandExtensions extensions;
    extensions.disable("zwlr_layer_shell_v1");


    add_to_environment("MIR_SERVER_ADD_WAYLAND_EXTENSIONS", "zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();

    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq("zwlr_layer_shell_v1")));
}

TEST_F(WaylandExtensions, conditionally_enable_given_correct_extension_name)
{
    miral::WaylandExtensions extensions;
    bool condition_called = false;
    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const& info)
        {
            EXPECT_THAT(info.name(), StrEq("zwlr_layer_shell_v1"));
            condition_called = true;
            return true;
        });

    add_server_init(extensions);
    start_server();

    run_client_enumerator(server());

    EXPECT_THAT(condition_called, Eq(true));
}

TEST_F(WaylandExtensions, conditionally_enable_not_given_user_preference_if_none_specified)
{
    miral::WaylandExtensions extensions;
    bool condition_called = false;
    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const& info)
        {
            EXPECT_THAT(info.user_preference(), Eq(std::nullopt));
            condition_called = true;
            return true;
        });

    add_server_init(extensions);
    start_server();
    run_client_enumerator(server());

    EXPECT_THAT(condition_called, Eq(true));
}

TEST_F(WaylandExtensions, conditionally_enable_given_user_preference_true_if_added)
{
    miral::WaylandExtensions extensions;
    bool condition_called = false;
    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const& info)
        {
            EXPECT_THAT(info.user_preference(), Eq(std::make_optional(true)));
            condition_called = true;
            return true;
        });
    add_to_environment("MIR_SERVER_ADD_WAYLAND_EXTENSIONS", "zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();
    run_client_enumerator(server());

    EXPECT_THAT(condition_called, Eq(true));
}

TEST_F(WaylandExtensions, conditionally_enable_given_user_preference_false_if_dropped)
{
    miral::WaylandExtensions extensions;
    bool condition_called = false;
    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const& info)
        {
            EXPECT_THAT(info.user_preference(), Eq(std::make_optional(false)));
            condition_called = true;
            return true;
        });
    add_to_environment("MIR_SERVER_DROP_WAYLAND_EXTENSIONS", "zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();
    run_client_enumerator(server());

    EXPECT_THAT(condition_called, Eq(true));
}

TEST_F(WaylandExtensions, conditionally_enable_given_user_preference_true_if_in_extensions_list)
{
    miral::WaylandExtensions extensions;
    bool condition_called = false;
    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const& info)
        {
            EXPECT_THAT(info.user_preference(), Eq(std::make_optional(true)));
            condition_called = true;
            return true;
        });
    add_to_environment("MIR_SERVER_WAYLAND_EXTENSIONS", "xdg_wm_base:zwlr_layer_shell_v1");

    add_server_init(extensions);
    start_server();
    run_client_enumerator(server());

    EXPECT_THAT(condition_called, Eq(true));
}

TEST_F(WaylandExtensions, conditionally_enable_given_user_preference_false_if_not_in_extensions_list)
{
    miral::WaylandExtensions extensions;
    bool condition_called = false;
    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const& info)
        {
            EXPECT_THAT(info.user_preference(), Eq(std::make_optional(false)));
            condition_called = true;
            return true;
        });
    add_to_environment("MIR_SERVER_WAYLAND_EXTENSIONS", "xdg_wm_base");

    add_server_init(extensions);
    start_server();
    run_client_enumerator(server());

    EXPECT_THAT(condition_called, Eq(true));
}

TEST_F(WaylandExtensions, conditionally_enable_can_enable_extension)
{
    miral::WaylandExtensions extensions;

    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const&)
        {
            return true;
        });

    add_server_init(extensions);
    start_server();
    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq("zwlr_layer_shell_v1")));
}

TEST_F(WaylandExtensions, conditionally_enable_can_disable_extension)
{
    miral::WaylandExtensions extensions;

    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const&)
        {
            return false;
        });

    add_server_init(extensions);
    start_server();
    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Not(Contains(Eq("zwlr_layer_shell_v1"))));
}

TEST_F(WaylandExtensions, conditionally_enable_can_enable_extension_disabled_by_user)
{
    miral::WaylandExtensions extensions;

    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const&)
        {
            return true;
        });

    add_to_environment("MIR_SERVER_DROP_WAYLAND_EXTENSIONS", "zwlr_layer_shell_v1");
    add_server_init(extensions);
    start_server();
    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Contains(Eq("zwlr_layer_shell_v1")));
}

TEST_F(WaylandExtensions, conditionally_enable_can_disable_extension_enabled_by_user)
{
    miral::WaylandExtensions extensions;

    extensions.conditionally_enable("zwlr_layer_shell_v1", [&](auto const&)
        {
            return false;
        });

    add_to_environment("MIR_SERVER_ADD_WAYLAND_EXTENSIONS", "zwlr_layer_shell_v1");
    add_server_init(extensions);
    start_server();
    auto const interfaces = run_client_enumerator(server());

    EXPECT_THAT(interfaces, Not(Contains(Eq("zwlr_layer_shell_v1"))));
}