selection_requestor_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/base/x/selection_requestor.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include "base/bind.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted_memory.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/test/task_environment.h"
  12. #include "base/threading/thread_task_runner_handle.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "ui/base/x/selection_utils.h"
  15. #include "ui/base/x/x11_clipboard_helper.h"
  16. #include "ui/base/x/x11_util.h"
  17. #include "ui/gfx/x/connection.h"
  18. #include "ui/gfx/x/event.h"
  19. #include "ui/gfx/x/x11_atom_cache.h"
  20. #include "ui/gfx/x/xproto.h"
  21. #include "ui/gfx/x/xproto_util.h"
  22. namespace ui {
  23. class SelectionRequestorTest : public testing::Test {
  24. public:
  25. explicit SelectionRequestorTest() : connection_(x11::Connection::Get()) {}
  26. SelectionRequestorTest(const SelectionRequestorTest&) = delete;
  27. SelectionRequestorTest& operator=(const SelectionRequestorTest&) = delete;
  28. ~SelectionRequestorTest() override = default;
  29. // Responds to the SelectionRequestor's XConvertSelection() request by
  30. // - Setting the property passed into the XConvertSelection() request to
  31. // |value|.
  32. // - Sending a SelectionNotify event.
  33. void SendSelectionNotify(x11::Atom selection,
  34. x11::Atom target,
  35. const std::string& value) {
  36. x11::SetStringProperty(x_window_, requestor_->x_property_,
  37. x11::Atom::STRING, value);
  38. requestor_->OnSelectionNotify({
  39. .requestor = x_window_,
  40. .selection = selection,
  41. .target = target,
  42. .property = requestor_->x_property_,
  43. });
  44. }
  45. protected:
  46. void SetUp() override {
  47. // Create a window for the selection requestor to use.
  48. x_window_ = x11::CreateDummyWindow();
  49. helper_ = std::make_unique<XClipboardHelper>(
  50. base::BindRepeating([](ClipboardBuffer buffer) {}));
  51. requestor_ = helper_->GetSelectionRequestorForTest();
  52. }
  53. void TearDown() override {
  54. helper_.reset();
  55. requestor_ = nullptr;
  56. connection_->DestroyWindow({x_window_});
  57. }
  58. raw_ptr<x11::Connection> connection_;
  59. // |requestor_|'s window.
  60. x11::Window x_window_ = x11::Window::None;
  61. std::unique_ptr<XClipboardHelper> helper_;
  62. raw_ptr<SelectionRequestor> requestor_ = nullptr;
  63. base::test::SingleThreadTaskEnvironment task_environment_{
  64. base::test::SingleThreadTaskEnvironment::MainThreadType::UI};
  65. };
  66. namespace {
  67. // Converts |selection| to |target| and checks the returned values.
  68. void PerformBlockingConvertSelection(SelectionRequestor* requestor,
  69. x11::Atom selection,
  70. x11::Atom target,
  71. const std::string& expected_data) {
  72. std::vector<uint8_t> out_data;
  73. x11::Atom out_type = x11::Atom::None;
  74. EXPECT_TRUE(requestor->PerformBlockingConvertSelection(selection, target,
  75. &out_data, &out_type));
  76. EXPECT_EQ(expected_data.size(), out_data.size());
  77. EXPECT_EQ(expected_data, ui::RefCountedMemoryToString(
  78. base::RefCountedBytes::TakeVector(&out_data)));
  79. EXPECT_EQ(x11::Atom::STRING, out_type);
  80. }
  81. } // namespace
  82. // Test that SelectionRequestor correctly handles receiving a request while it
  83. // is processing another request.
  84. // TODO(https://crbug.com/443355): Reenable once clipboard interface is async.
  85. TEST_F(SelectionRequestorTest, DISABLED_NestedRequests) {
  86. // Assume that |selection| will have no owner. If there is an owner, the owner
  87. // will set the property passed into the XConvertSelection() request which is
  88. // undesirable.
  89. x11::Atom selection = x11::GetAtom("FAKE_SELECTION");
  90. x11::Atom target1 = x11::GetAtom("TARGET1");
  91. x11::Atom target2 = x11::GetAtom("TARGET2");
  92. base::ThreadTaskRunnerHandle::Get()->PostTask(
  93. FROM_HERE, base::BindOnce(&PerformBlockingConvertSelection,
  94. base::Unretained(requestor_), selection,
  95. target2, "Data2"));
  96. base::ThreadTaskRunnerHandle::Get()->PostTask(
  97. FROM_HERE,
  98. base::BindOnce(&SelectionRequestorTest::SendSelectionNotify,
  99. base::Unretained(this), selection, target1, "Data1"));
  100. base::ThreadTaskRunnerHandle::Get()->PostTask(
  101. FROM_HERE,
  102. base::BindOnce(&SelectionRequestorTest::SendSelectionNotify,
  103. base::Unretained(this), selection, target2, "Data2"));
  104. PerformBlockingConvertSelection(requestor_, selection, target1, "Data1");
  105. }
  106. } // namespace ui