File: x_server_clipboard_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 3,744 bytes parent folder | download | duplicates (7)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "remoting/base/constants.h"
#include "remoting/host/linux/x_server_clipboard.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/x/connection.h"

namespace remoting {

namespace {

class ClipboardTestClient : public x11::EventObserver {
 public:
  ClipboardTestClient() = default;

  ClipboardTestClient(const ClipboardTestClient&) = delete;
  ClipboardTestClient& operator=(const ClipboardTestClient&) = delete;

  ~ClipboardTestClient() override {
    DCHECK(connection_);
    connection_->RemoveEventObserver(this);
  }

  void Init(x11::Connection* connection) {
    connection_ = connection;
    connection_->AddEventObserver(this);
    clipboard_.Init(connection, base::BindRepeating(
                                    &ClipboardTestClient::OnClipboardChanged,
                                    base::Unretained(this)));
  }

  void SetClipboardData(const std::string& clipboard_data) {
    clipboard_data_ = clipboard_data;
    clipboard_.SetClipboard(kMimeTypeTextUtf8, clipboard_data);
  }

  void OnClipboardChanged(const std::string& mime_type,
                          const std::string& data) {
    clipboard_data_ = data;
  }

  // Process X events on the connection, returning true if any events were
  // processed.
  bool PumpXEvents() {
    dispatched_event_ = false;
    connection_->Sync();
    connection_->DispatchAll();
    return dispatched_event_;
  }

  void OnEvent(const x11::Event& event) override {
    dispatched_event_ = true;
    clipboard_.ProcessXEvent(event);
  }

  const std::string& clipboard_data() const { return clipboard_data_; }
  x11::Connection* connection() const { return connection_; }

 private:
  std::string clipboard_data_;
  XServerClipboard clipboard_;
  raw_ptr<x11::Connection> connection_ = nullptr;
  bool dispatched_event_ = false;
};

}  // namespace

class XServerClipboardTest : public testing::Test {
 public:
  void SetUp() override {
    // SynchronizeForTest() ensures that PumpXEvents() fully processes all X
    // server requests and responses before returning to the caller.
    connection1_ = std::make_unique<x11::Connection>();
    connection1_->SynchronizeForTest(true);
    client1_.Init(connection1_.get());
    connection2_ = std::make_unique<x11::Connection>();
    connection2_->SynchronizeForTest(true);
    client2_.Init(connection2_.get());
  }

  void TearDown() override {
    connection1_.reset();
    connection2_.reset();
  }

  void PumpXEvents() {
    while (true) {
      if (!client1_.PumpXEvents() && !client2_.PumpXEvents()) {
        break;
      }
    }
  }

  std::unique_ptr<x11::Connection> connection1_;
  std::unique_ptr<x11::Connection> connection2_;
  ClipboardTestClient client1_;
  ClipboardTestClient client2_;
};

// http://crbug.com/163428
TEST_F(XServerClipboardTest, DISABLED_CopyPaste) {
  // Verify clipboard data can be transferred more than once. Then verify that
  // the code continues to function in the opposite direction (so client1_ will
  // send then receive, and client2_ will receive then send).
  client1_.SetClipboardData("Text1");
  PumpXEvents();
  EXPECT_EQ("Text1", client2_.clipboard_data());

  client1_.SetClipboardData("Text2");
  PumpXEvents();
  EXPECT_EQ("Text2", client2_.clipboard_data());

  client2_.SetClipboardData("Text3");
  PumpXEvents();
  EXPECT_EQ("Text3", client1_.clipboard_data());

  client2_.SetClipboardData("Text4");
  PumpXEvents();
  EXPECT_EQ("Text4", client1_.clipboard_data());
}

}  // namespace remoting