Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / remoting / host / linux / x_server_clipboard_unittest.cc
blobbeb7f61f2c5a32c45f04d83a51e523ded28503d9
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 "remoting/base/constants.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 // X11 headers must be #included after gtest.h, since the X11 headers define
12 // some macros that cause errors in gtest-type-util.h.
13 #include <X11/Xlib.h>
14 #include "remoting/host/linux/x_server_clipboard.h"
16 namespace remoting {
18 namespace {
20 class ClipboardTestClient {
21 public:
22 ClipboardTestClient() : display_(NULL) {}
23 ~ClipboardTestClient() {}
25 void Init(Display* display) {
26 display_ = display;
27 clipboard_.Init(display,
28 base::Bind(&ClipboardTestClient::OnClipboardChanged,
29 base::Unretained(this)));
32 void SetClipboardData(const std::string& clipboard_data) {
33 clipboard_data_ = clipboard_data;
34 clipboard_.SetClipboard(kMimeTypeTextUtf8, clipboard_data);
37 void OnClipboardChanged(const std::string& mime_type,
38 const std::string& data) {
39 clipboard_data_ = data;
42 // Process X events on the connection, returning true if any events were
43 // processed.
44 bool PumpXEvents() {
45 bool result = false;
46 while (XPending(display_)) {
47 XEvent event;
48 XNextEvent(display_, &event);
49 clipboard_.ProcessXEvent(&event);
50 result = true;
52 return result;
55 const std::string& clipboard_data() const { return clipboard_data_; }
56 Display* display() const { return display_; }
58 private:
59 std::string clipboard_data_;
60 XServerClipboard clipboard_;
61 Display* display_;
63 DISALLOW_COPY_AND_ASSIGN(ClipboardTestClient);
66 } // namespace
68 class XServerClipboardTest : public testing::Test {
69 public:
70 virtual void SetUp() OVERRIDE {
71 // XSynchronize() ensures that PumpXEvents() fully processes all X server
72 // requests and responses before returning to the caller.
73 Display* display1 = XOpenDisplay(NULL);
74 XSynchronize(display1, True);
75 client1_.Init(display1);
76 Display* display2 = XOpenDisplay(NULL);
77 XSynchronize(display2, True);
78 client2_.Init(display2);
81 virtual void TearDown() OVERRIDE {
82 XCloseDisplay(client1_.display());
83 XCloseDisplay(client2_.display());
86 void PumpXEvents() {
87 while (true) {
88 if (!client1_.PumpXEvents() && !client2_.PumpXEvents()) {
89 break;
94 ClipboardTestClient client1_;
95 ClipboardTestClient client2_;
98 // http://crbug.com/163428
99 TEST_F(XServerClipboardTest, DISABLED_CopyPaste) {
100 // Verify clipboard data can be transferred more than once. Then verify that
101 // the code continues to function in the opposite direction (so client1_ will
102 // send then receive, and client2_ will receive then send).
103 client1_.SetClipboardData("Text1");
104 PumpXEvents();
105 EXPECT_EQ("Text1", client2_.clipboard_data());
107 client1_.SetClipboardData("Text2");
108 PumpXEvents();
109 EXPECT_EQ("Text2", client2_.clipboard_data());
111 client2_.SetClipboardData("Text3");
112 PumpXEvents();
113 EXPECT_EQ("Text3", client1_.clipboard_data());
115 client2_.SetClipboardData("Text4");
116 PumpXEvents();
117 EXPECT_EQ("Text4", client1_.clipboard_data());
120 } // namespace remoting