File: wayland_clipboard_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (597 lines) | stat: -rw-r--r-- 24,208 bytes parent folder | download | duplicates (2)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string_view>

#include "base/containers/span.h"

#include <linux/input.h>
#include <wayland-server.h>

#include <cstring>
#include <memory>
#include <string>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/containers/to_vector.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/bind.h"
#include "base/test/mock_callback.h"
#include "build/buildflag.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/clipboard_buffer.h"
#include "ui/base/clipboard/clipboard_constants.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/geometry/point.h"
#include "ui/ozone/platform/wayland/host/wayland_clipboard.h"
#include "ui/ozone/platform/wayland/host/wayland_keyboard.h"
#include "ui/ozone/platform/wayland/host/wayland_pointer.h"
#include "ui/ozone/platform/wayland/host/wayland_seat.h"
#include "ui/ozone/platform/wayland/host/wayland_serial_tracker.h"
#include "ui/ozone/platform/wayland/host/wayland_touch.h"
#include "ui/ozone/platform/wayland/test/mock_pointer.h"
#include "ui/ozone/platform/wayland/test/mock_surface.h"
#include "ui/ozone/platform/wayland/test/test_data_device.h"
#include "ui/ozone/platform/wayland/test/test_data_device_manager.h"
#include "ui/ozone/platform/wayland/test/test_data_offer.h"
#include "ui/ozone/platform/wayland/test/test_data_source.h"
#include "ui/ozone/platform/wayland/test/test_keyboard.h"
#include "ui/ozone/platform/wayland/test/test_selection_device_manager.h"
#include "ui/ozone/platform/wayland/test/test_touch.h"
#include "ui/ozone/platform/wayland/test/test_wayland_server_thread.h"
#include "ui/ozone/platform/wayland/test/wayland_test.h"
#include "ui/ozone/public/platform_clipboard.h"

using testing::_;
using testing::Eq;
using testing::IsEmpty;
using testing::IsNull;
using testing::Mock;
using testing::Values;

namespace ui {

namespace {

constexpr char kSampleClipboardText[] = "This is a sample text for clipboard.";

ui::PlatformClipboard::Data ToClipboardData(std::string_view data_string) {
  return base::MakeRefCounted<base::RefCountedBytes>(
      base::ToVector(base::as_byte_span(data_string)));
}

// This must be called on the server thread.
wl::TestSelectionDevice* GetSelectionDevice(wl::TestWaylandServerThread* server,
                                            ClipboardBuffer buffer) {
  DCHECK(server->task_runner()->BelongsToCurrentThread());

  return buffer == ClipboardBuffer::kSelection
             ? server->primary_selection_device_manager()->device()
             : server->data_device_manager()->data_device();
}

// This must be called on the server thread.
wl::TestSelectionSource* GetSelectionSource(wl::TestWaylandServerThread* server,
                                            ClipboardBuffer buffer) {
  DCHECK(server->task_runner()->BelongsToCurrentThread());

  return buffer == ClipboardBuffer::kSelection
             ? server->primary_selection_device_manager()->source()
             : server->data_device_manager()->data_source();
}

}  // namespace

class WaylandClipboardTestBase : public WaylandTest {
 public:
  WaylandClipboardTestBase() = default;
  WaylandClipboardTestBase(const WaylandClipboardTestBase&) = delete;
  WaylandClipboardTestBase& operator=(const WaylandClipboardTestBase&) = delete;
  ~WaylandClipboardTestBase() override = default;

  void SetUp() override {
    WaylandTest::SetUp();

    PostToServerAndWait([](wl::TestWaylandServerThread* server) {
      wl_seat_send_capabilities(server->seat()->resource(),
                                WL_SEAT_CAPABILITY_POINTER |
                                    WL_SEAT_CAPABILITY_TOUCH |
                                    WL_SEAT_CAPABILITY_KEYBOARD);

      ASSERT_TRUE(server->data_device_manager());
    });
    ASSERT_TRUE(connection_->seat()->pointer());
    ASSERT_TRUE(connection_->seat()->touch());
    ASSERT_TRUE(connection_->seat()->keyboard());

    clipboard_ = connection_->clipboard();
    ASSERT_TRUE(clipboard_);

    MaybeSetUpXkb();
  }

  void TearDown() override {
    WaylandTest::TearDown();
    clipboard_ = nullptr;
  }

 protected:
  // Ensure the requests/events are flushed and posted tasks get processed.
  // wl::TestSelection{Source,Offer} use ThreadPool task runners to read/write
  // selection data, so the pool must be explicitly flushed as well.
  void WaitForClipboardTasks() {
    WaylandTestBase::SyncDisplay();
    base::ThreadPoolInstance::Get()->FlushForTesting();
    base::RunLoop().RunUntilIdle();
  }

  void SentPointerButtonPress(const gfx::Point& location) {
    PostToServerAndWait(
        [&location, surface_id = window_->root_surface()->get_surface_id()](
            wl::TestWaylandServerThread* server) {
          auto* const pointer = server->seat()->pointer()->resource();
          auto* const surface =
              server->GetObject<wl::MockSurface>(surface_id)->resource();

          wl_pointer_send_enter(pointer, server->GetNextSerial(), surface,
                                wl_fixed_from_int(location.x()),
                                wl_fixed_from_int(location.y()));
          wl_pointer_send_button(pointer, server->GetNextSerial(),
                                 server->GetNextTime(), BTN_LEFT,
                                 WL_POINTER_BUTTON_STATE_PRESSED);
        });
  }
  void SendTouchDown(const gfx::Point& location) {
    PostToServerAndWait(
        [location, surface_id = window_->root_surface()->get_surface_id()](
            wl::TestWaylandServerThread* server) {
          auto* const touch = server->seat()->touch()->resource();
          auto* const surface =
              server->GetObject<wl::MockSurface>(surface_id)->resource();

          wl_touch_send_down(touch, server->GetNextSerial(),
                             server->GetNextTime(), surface, 0 /* id */,
                             wl_fixed_from_int(location.x()),
                             wl_fixed_from_int(location.y()));
          wl_touch_send_frame(touch);
        });
  }

  void SendTouchUp() {
    PostToServerAndWait([](wl::TestWaylandServerThread* server) {
      auto* const touch = server->seat()->touch()->resource();

      wl_touch_send_up(touch, server->GetNextSerial(), server->GetNextTime(),
                       0 /* id */);
    });
  }

  void SendKeyboardKey() {
    PostToServerAndWait(
        [surface_id = window_->root_surface()->get_surface_id()](
            wl::TestWaylandServerThread* server) {
          auto* const keyboard = server->seat()->keyboard()->resource();
          auto* const surface =
              server->GetObject<wl::MockSurface>(surface_id)->resource();

          wl::ScopedWlArray empty({});
          wl_keyboard_send_enter(keyboard, server->GetNextSerial(), surface,
                                 empty.get());
          wl_keyboard_send_key(keyboard, server->GetNextSerial(),
                               server->GetNextTime(), 30 /* a */,
                               WL_KEYBOARD_KEY_STATE_PRESSED);
        });
  }

  raw_ptr<WaylandClipboard> clipboard_ = nullptr;
};

class WaylandClipboardTest : public WaylandClipboardTestBase {
 public:
  WaylandClipboardTest() = default;
  WaylandClipboardTest(const WaylandClipboardTest&) = delete;
  WaylandClipboardTest& operator=(const WaylandClipboardTest&) = delete;
  ~WaylandClipboardTest() override = default;

  void SetUp() override {
    WaylandClipboardTestBase::SetUp();

    PostToServerAndWait(
        [primary_selection_protocol = GetParam().primary_selection_protocol](
            wl::TestWaylandServerThread* server) {
          ASSERT_TRUE(primary_selection_protocol ==
                          wl::PrimarySelectionProtocol::kNone ||
                      server->primary_selection_device_manager());
        });

    // Make sure clipboard instance for the available primary selection protocol
    // gets properly created, ie: the corresponding 'get_device' request is
    // issued, so server-side objects are created prior to test-case specific
    // calls, otherwise tests, such as ReadFromClipboard, would crash.
    ASSERT_EQ(WhichBufferToUse() == ClipboardBuffer::kSelection,
              !!clipboard_->GetClipboard(ClipboardBuffer::kSelection));
    WaylandTestBase::SyncDisplay();

    offered_data_.clear();
  }

 protected:
  ClipboardBuffer WhichBufferToUse() const {
    return GetParam().primary_selection_protocol !=
                   wl::PrimarySelectionProtocol::kNone
               ? ClipboardBuffer::kSelection
               : ClipboardBuffer::kCopyPaste;
  }

  // Fill the clipboard backing store with sample data.
  void OfferData(ClipboardBuffer buffer,
                 std::string_view data,
                 const std::string& mime_type) {
    offered_data_[mime_type] = base::MakeRefCounted<base::RefCountedBytes>(
        base::ToVector(base::as_byte_span(data)));

    base::MockCallback<PlatformClipboard::OfferDataClosure> offer_callback;
    EXPECT_CALL(offer_callback, Run()).Times(1);
    clipboard_->OfferClipboardData(buffer, offered_data_, offer_callback.Get());
  }

  PlatformClipboard::DataMap offered_data_;
};

class CopyPasteOnlyClipboardTest : public WaylandClipboardTestBase {
 public:
  void SetUp() override {
    WaylandClipboardTestBase::SetUp();

    ASSERT_FALSE(clipboard_->IsSelectionBufferAvailable());

    ASSERT_EQ(wl::PrimarySelectionProtocol::kNone,
              GetParam().primary_selection_protocol);

    PostToServerAndWait([](wl::TestWaylandServerThread* server) {
      ASSERT_TRUE(server->data_device_manager());
      ASSERT_FALSE(server->primary_selection_device_manager());
    });
  }
};

// Verifies that copy-to-clipboard works as expected. Actual Wayland input
// events are used in order to exercise all the components involved, e.g:
// Wayland{Pointer,Keyboard,Touch}, Serial tracker and WaylandClipboard.
//
// Regression test for https://crbug.com/1282220.
// TODO(crbug.com/41495216): Flaky test.
TEST_P(WaylandClipboardTest, DISABLED_WriteToClipboard) {
  const base::RepeatingClosure send_input_event_closures[]{
      // Mouse button press
      base::BindLambdaForTesting([&]() {
        SentPointerButtonPress({10, 10});
      }),
      // Key press
      base::BindLambdaForTesting([&]() { SendKeyboardKey(); }),
      // Touch down
      base::BindLambdaForTesting([&]() {
        SendTouchDown({200, 200});
      }),
      // Touch tap (down > up)
      base::BindLambdaForTesting([&]() {
        SendTouchDown({300, 300});
        SendTouchUp();
      })};

  auto* window_manager = connection_->window_manager();

  // Triggering copy on touch-down event.
  for (auto send_input_event : send_input_event_closures) {
    PostToServerAndWait([](wl::TestWaylandServerThread* server) {
      if (GetParam().primary_selection_protocol !=
          wl::PrimarySelectionProtocol::kNone) {
        server->primary_selection_device_manager()->set_source(nullptr);
      } else {
        server->data_device_manager()->set_data_source(nullptr);
      }
    });

    send_input_event.Run();
    auto client_selection_serial = connection_->serial_tracker().GetSerial(
        {wl::SerialType::kTouchPress, wl::SerialType::kMousePress,
         wl::SerialType::kKeyPress});
    ASSERT_TRUE(client_selection_serial.has_value());

    // 1. Offer sample text as selection data.
    OfferData(WhichBufferToUse(), kSampleClipboardText,
              {kMimeTypeUtf8PlainText});
    WaylandTestBase::SyncDisplay();

    // 2. Emulate an external client requesting to read the offered data and
    // make sure the appropriate string gets delivered.
    // These three objects are accessed from the server thread, but must persist
    // several server calls, that is why they are allocated here.
    std::string delivered_text;
    base::MockCallback<wl::TestSelectionSource::ReadDataCallback> callback;
    base::RunLoop run_loop;

    PostToServerAndWait([&delivered_text, &callback,
                         buffer = WhichBufferToUse(),
                         serial = client_selection_serial->value,
                         quit_closure = run_loop.QuitClosure()](
                            wl::TestWaylandServerThread* server) {
      ASSERT_TRUE(GetSelectionSource(server, buffer));

      EXPECT_EQ(serial, GetSelectionDevice(server, buffer)->selection_serial());

      EXPECT_CALL(callback, Run(_))
          .WillOnce(
              [&delivered_text, quit_closure](std::vector<uint8_t>&& data) {
                delivered_text = std::string(data.begin(), data.end());
                quit_closure.Run();
              });

      GetSelectionSource(server, buffer)
          ->ReadData(kMimeTypeUtf8PlainText, callback.Get());
    });

    WaitForClipboardTasks();

    PostToServerAndWait([&delivered_text](wl::TestWaylandServerThread* server) {
      EXPECT_EQ(kSampleClipboardText, delivered_text);
    });

    window_manager->SetPointerFocusedWindow(nullptr);
    window_manager->SetTouchFocusedWindow(nullptr);
    window_manager->SetKeyboardFocusedWindow(nullptr);
  }
}

TEST_P(WaylandClipboardTest, ReadFromClipboard) {
  // 1. Emulate a selection data offer coming in.
  PostToServerAndWait(
      [buffer = WhichBufferToUse()](wl::TestWaylandServerThread* server) {
        auto* device = GetSelectionDevice(server, buffer);
        auto* data_offer = device->OnDataOffer();
        data_offer->OnOffer(kMimeTypeUtf8PlainText,
                            ToClipboardData(kSampleClipboardText));
        device->OnSelection(data_offer);
      });

  // 2. Request to read the offered data and check whether the read text matches
  // the previously offered one.
  std::string text;
  base::MockCallback<PlatformClipboard::RequestDataClosure> callback;
  EXPECT_CALL(callback, Run(_)).WillOnce([&text](PlatformClipboard::Data data) {
    ASSERT_TRUE(data);
    text = std::string(base::as_string_view(*data));
  });

  clipboard_->RequestClipboardData(WhichBufferToUse(), kMimeTypeUtf8PlainText,
                                   callback.Get());
  EXPECT_EQ(kSampleClipboardText, text);
}

// Regression test for crbug.com/1183939. Ensures unicode mime types take
// priority over text/plain when reading text.
TEST_P(WaylandClipboardTest, ReadFromClipboardPrioritizeUtf) {
  PostToServerAndWait(
      [buffer = WhichBufferToUse()](wl::TestWaylandServerThread* server) {
        auto* data_offer = GetSelectionDevice(server, buffer)->OnDataOffer();
        data_offer->OnOffer(kMimeTypePlainText, ToClipboardData("ascii_text"));
        data_offer->OnOffer(kMimeTypeUtf8PlainText,
                            ToClipboardData("utf8_text"));
        GetSelectionDevice(server, buffer)->OnSelection(data_offer);
      });

  std::string text;
  base::MockCallback<PlatformClipboard::RequestDataClosure> callback;
  EXPECT_CALL(callback, Run(_)).WillOnce([&text](PlatformClipboard::Data data) {
    ASSERT_TRUE(data);
    text = std::string(base::as_string_view(*data));
  });

  clipboard_->RequestClipboardData(WhichBufferToUse(), kMimeTypeUtf8PlainText,
                                   callback.Get());
  EXPECT_EQ("utf8_text", text);
}

TEST_P(WaylandClipboardTest, ReadFromClipboardWithoutOffer) {
  // When no data offer is advertised and client requests clipboard data from
  // the server, the response callback should be gracefully called with null
  // data.
  base::MockCallback<PlatformClipboard::RequestDataClosure> callback;
  EXPECT_CALL(callback, Run(Eq(nullptr))).Times(1);
  clipboard_->RequestClipboardData(WhichBufferToUse(), kMimeTypeUtf8PlainText,
                                   callback.Get());
}

TEST_P(WaylandClipboardTest, IsSelectionOwner) {
  connection_->serial_tracker().UpdateSerial(wl::SerialType::kMousePress, 1);

  OfferData(WhichBufferToUse(), kSampleClipboardText, {kMimeTypeUtf8PlainText});

  PostToServerAndWait(
      [buffer = WhichBufferToUse()](wl::TestWaylandServerThread* server) {
        ASSERT_TRUE(GetSelectionSource(server, buffer));
      });

  ASSERT_TRUE(clipboard_->IsSelectionOwner(WhichBufferToUse()));

  // The compositor sends OnCancelled whenever another application on the system
  // sets a new selection. It means we are not the application that owns the
  // current selection data.
  PostToServerAndWait(
      [buffer = WhichBufferToUse()](wl::TestWaylandServerThread* server) {
        GetSelectionSource(server, buffer)->OnCancelled();
      });

  ASSERT_FALSE(clipboard_->IsSelectionOwner(WhichBufferToUse()));

  connection_->serial_tracker().ResetSerial(wl::SerialType::kMousePress);
}

// Ensures WaylandClipboard correctly handles overlapping read requests for
// different clipboard buffers.
TEST_P(WaylandClipboardTest, OverlapReadingFromDifferentBuffers) {
  // Offer a sample text as selection data.
  PostToServerAndWait(
      [buffer = WhichBufferToUse()](wl::TestWaylandServerThread* server) {
        auto* data_offer = GetSelectionDevice(server, buffer)->OnDataOffer();
        data_offer->OnOffer(kMimeTypeUtf8PlainText,
                            ToClipboardData(kSampleClipboardText));
        GetSelectionDevice(server, buffer)->OnSelection(data_offer);
      });

  // Post a read request for the other buffer, which will start its execution
  // after the request above.
  auto other_buffer = WhichBufferToUse() == ClipboardBuffer::kSelection
                          ? ClipboardBuffer::kCopyPaste
                          : ClipboardBuffer::kSelection;
  base::MockCallback<PlatformClipboard::RequestDataClosure> callback;
  EXPECT_CALL(callback, Run(Eq(nullptr))).Times(1);
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(&PlatformClipboard::RequestClipboardData,
                                base::Unretained(clipboard_), other_buffer,
                                kMimeTypeUtf8PlainText, callback.Get()));

  // Instantly start a clipboard read request for kCopyPaste buffer (the actual
  // data transfer will take place asynchronously. See WaylandDataDevice impl)
  // and ensure read callback is called with the expected resulting data,
  // regardless any other request that may arrive in the meantime.
  std::string text;
  base::MockCallback<PlatformClipboard::RequestDataClosure> got_text;
  EXPECT_CALL(got_text, Run(_)).WillOnce([&](PlatformClipboard::Data data) {
    ASSERT_NE(nullptr, data);
    text = std::string(base::as_string_view(*data));
  });
  clipboard_->RequestClipboardData(WhichBufferToUse(), kMimeTypeUtf8PlainText,
                                   got_text.Get());

  WaitForClipboardTasks();
  EXPECT_EQ(kSampleClipboardText, text);
}

// Ensures clipboard change callback is fired only once per read/write.
TEST_P(WaylandClipboardTest, ClipboardChangeNotifications) {
  base::MockCallback<PlatformClipboard::ClipboardDataChangedCallback>
      clipboard_changed_callback;
  clipboard_->SetClipboardDataChangedCallback(clipboard_changed_callback.Get());
  const auto buffer = WhichBufferToUse();

  // 1. For selection offered by an external application.
  EXPECT_CALL(clipboard_changed_callback, Run(buffer)).Times(1);
  PostToServerAndWait(
      [buffer = WhichBufferToUse()](wl::TestWaylandServerThread* server) {
        auto* data_offer = GetSelectionDevice(server, buffer)->OnDataOffer();
        data_offer->OnOffer(kMimeTypeUtf8PlainText,
                            ToClipboardData(kSampleClipboardText));
        GetSelectionDevice(server, buffer)->OnSelection(data_offer);
      });
  EXPECT_FALSE(clipboard_->IsSelectionOwner(buffer));

  // 2. For selection offered by Chromium.
  connection_->serial_tracker().UpdateSerial(wl::SerialType::kMousePress, 1);
  EXPECT_CALL(clipboard_changed_callback, Run(buffer)).Times(1);
  OfferData(buffer, kSampleClipboardText, {kMimeTypeUtf8PlainText});
  PostToServerAndWait(
      [buffer = WhichBufferToUse()](wl::TestWaylandServerThread* server) {
        ASSERT_TRUE(GetSelectionSource(server, buffer));
      });
  EXPECT_TRUE(clipboard_->IsSelectionOwner(buffer));
  connection_->serial_tracker().ResetSerial(wl::SerialType::kMousePress);
}

// Verifies clipboard calls targeting primary selection buffer no-op and run
// gracefully when no primary selection protocol is available.
TEST_P(CopyPasteOnlyClipboardTest, PrimarySelectionRequestsNoop) {
  const auto buffer = ClipboardBuffer::kSelection;

  base::MockCallback<PlatformClipboard::OfferDataClosure> offer_done;
  EXPECT_CALL(offer_done, Run()).Times(1);
  clipboard_->OfferClipboardData(buffer, {}, offer_done.Get());
  EXPECT_FALSE(clipboard_->IsSelectionOwner(buffer));

  base::MockCallback<PlatformClipboard::RequestDataClosure> got_data;
  EXPECT_CALL(got_data, Run(IsNull())).Times(1);
  clipboard_->RequestClipboardData(buffer, kMimeTypeUtf8PlainText,
                                   got_data.Get());

  base::MockCallback<PlatformClipboard::GetMimeTypesClosure> got_mime_types;
  EXPECT_CALL(got_mime_types, Run(IsEmpty())).Times(1);
  clipboard_->GetAvailableMimeTypes(buffer, got_mime_types.Get());
}

// Makes sure overlapping read requests for the same clipboard buffer are
// properly handled.
// TODO(crbug.com/40398800): Re-enable once Clipboard API becomes async.
TEST_P(CopyPasteOnlyClipboardTest, DISABLED_OverlappingReadRequests) {
  // Create an selection data offer containing plain and html mime types.
  PostToServerAndWait([](wl::TestWaylandServerThread* server) {
    auto* data_device = server->data_device_manager()->data_device();
    auto* data_offer = data_device->OnDataOffer();
    data_offer->OnOffer(kMimeTypePlainText, ToClipboardData("text"));
    data_offer->OnOffer(kMimeTypeHtml, ToClipboardData("html"));
    data_device->OnSelection(data_offer);
  });

  // Schedule a clipboard read task (for text/html mime type). As read requests
  // are processed asynchronously, this will actually start when the request
  // below is already under processing, thus emulating 2 "overlapping" requests.
  std::string html;
  base::MockCallback<PlatformClipboard::RequestDataClosure> got_html;
  EXPECT_CALL(got_html, Run(_)).WillOnce([&](PlatformClipboard::Data data) {
    html = std::string(base::as_string_view(*data));
  });
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&PlatformClipboard::RequestClipboardData,
                     base::Unretained(clipboard_), ClipboardBuffer::kCopyPaste,
                     kMimeTypeHtml, got_html.Get()));

  // Instantly start a read request for text/plain mime type.
  std::string text;
  base::MockCallback<PlatformClipboard::RequestDataClosure> got_text;
  EXPECT_CALL(got_text, Run(_)).WillOnce([&](PlatformClipboard::Data data) {
    text = std::string(base::as_string_view(*data));
  });
  clipboard_->RequestClipboardData(ClipboardBuffer::kCopyPaste,
                                   kMimeTypePlainText, got_text.Get());

  // Wait for clipboard tasks to complete and ensure both requests were
  // processed correctly.
  WaitForClipboardTasks();
  EXPECT_EQ("html", html);
  EXPECT_EQ("text", text);
}

INSTANTIATE_TEST_SUITE_P(
    WithZwpPrimarySelection,
    WaylandClipboardTest,
    Values(wl::ServerConfig{
        .primary_selection_protocol = wl::PrimarySelectionProtocol::kZwp}));

INSTANTIATE_TEST_SUITE_P(
    WithGtkPrimarySelection,
    WaylandClipboardTest,
    Values(wl::ServerConfig{
        .primary_selection_protocol = wl::PrimarySelectionProtocol::kGtk}));

INSTANTIATE_TEST_SUITE_P(
    WithoutPrimarySelection,
    WaylandClipboardTest,
    Values(wl::ServerConfig{
        .primary_selection_protocol = wl::PrimarySelectionProtocol::kNone}));

INSTANTIATE_TEST_SUITE_P(
    WithoutPrimarySelection,
    CopyPasteOnlyClipboardTest,
    Values(wl::ServerConfig{
        .primary_selection_protocol = wl::PrimarySelectionProtocol::kNone}));

}  // namespace ui