Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / host / linux / x_server_clipboard_unittest.cc
blobf7c090b9c362c3da9fedc3f528132453220ca322
1 // Copyright (c) 2012 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.
5 #include <string>
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "remoting/base/constants.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 // X11 headers must be #included after gtest.h, since the X11 headers define
13 // some macros that cause errors in gtest-type-util.h.
14 #include <X11/Xlib.h>
15 #include "remoting/host/linux/x_server_clipboard.h"
17 namespace remoting {
19 namespace {
21 class ClipboardTestClient {
22 public:
23 ClipboardTestClient() : display_(nullptr) {}
24 ~ClipboardTestClient() {}
26 void Init(Display* display) {
27 display_ = display;
28 clipboard_.Init(display,
29 base::Bind(&ClipboardTestClient::OnClipboardChanged,
30 base::Unretained(this)));
33 void SetClipboardData(const std::string& clipboard_data) {
34 clipboard_data_ = clipboard_data;
35 clipboard_.SetClipboard(kMimeTypeTextUtf8, clipboard_data);
38 void OnClipboardChanged(const std::string& mime_type,
39 const std::string& data) {
40 clipboard_data_ = data;
43 // Process X events on the connection, returning true if any events were
44 // processed.
45 bool PumpXEvents() {
46 bool result = false;
47 while (XPending(display_)) {
48 XEvent event;
49 XNextEvent(display_, &event);
50 clipboard_.ProcessXEvent(&event);
51 result = true;
53 return result;
56 const std::string& clipboard_data() const { return clipboard_data_; }
57 Display* display() const { return display_; }
59 private:
60 std::string clipboard_data_;
61 XServerClipboard clipboard_;
62 Display* display_;
64 DISALLOW_COPY_AND_ASSIGN(ClipboardTestClient);
67 } // namespace
69 class XServerClipboardTest : public testing::Test {
70 public:
71 void SetUp() override {
72 // XSynchronize() ensures that PumpXEvents() fully processes all X server
73 // requests and responses before returning to the caller.
74 Display* display1 = XOpenDisplay(nullptr);
75 XSynchronize(display1, True);
76 client1_.Init(display1);
77 Display* display2 = XOpenDisplay(nullptr);
78 XSynchronize(display2, True);
79 client2_.Init(display2);
82 void TearDown() override {
83 XCloseDisplay(client1_.display());
84 XCloseDisplay(client2_.display());
87 void PumpXEvents() {
88 while (true) {
89 if (!client1_.PumpXEvents() && !client2_.PumpXEvents()) {
90 break;
95 ClipboardTestClient client1_;
96 ClipboardTestClient client2_;
99 // http://crbug.com/163428
100 TEST_F(XServerClipboardTest, DISABLED_CopyPaste) {
101 // Verify clipboard data can be transferred more than once. Then verify that
102 // the code continues to function in the opposite direction (so client1_ will
103 // send then receive, and client2_ will receive then send).
104 client1_.SetClipboardData("Text1");
105 PumpXEvents();
106 EXPECT_EQ("Text1", client2_.clipboard_data());
108 client1_.SetClipboardData("Text2");
109 PumpXEvents();
110 EXPECT_EQ("Text2", client2_.clipboard_data());
112 client2_.SetClipboardData("Text3");
113 PumpXEvents();
114 EXPECT_EQ("Text3", client1_.clipboard_data());
116 client2_.SetClipboardData("Text4");
117 PumpXEvents();
118 EXPECT_EQ("Text4", client1_.clipboard_data());
121 } // namespace remoting