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.
5 #include "ui/base/x/selection_requestor.h"
7 #include "base/memory/ref_counted_memory.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/base/x/selection_utils.h"
12 #include "ui/base/x/x11_util.h"
13 #include "ui/events/platform/platform_event_source.h"
14 #include "ui/gfx/x/x11_atom_cache.h"
15 #include "ui/gfx/x/x11_types.h"
23 const char* kAtomsToCache[] = {
30 class SelectionRequestorTest : public testing::Test {
32 SelectionRequestorTest()
33 : x_display_(gfx::GetXDisplay()),
35 atom_cache_(gfx::GetXDisplay(), kAtomsToCache) {
36 atom_cache_.allow_uncached_atoms();
39 virtual ~SelectionRequestorTest() {
42 // Responds to the SelectionRequestor's XConvertSelection() request by
43 // - Setting the property passed into the XConvertSelection() request to
45 // - Sending a SelectionNotify event.
46 void SendSelectionNotify(XAtom selection,
48 const std::string& value) {
49 ui::SetStringProperty(x_window_,
50 requestor_->x_property_,
51 atom_cache_.GetAtom("STRING"),
55 xev.type = SelectionNotify;
56 xev.xselection.serial = 0u;
57 xev.xselection.display = x_display_;
58 xev.xselection.requestor = x_window_;
59 xev.xselection.selection = selection;
60 xev.xselection.target = target;
61 xev.xselection.property = requestor_->x_property_;
62 xev.xselection.time = CurrentTime;
63 xev.xselection.type = SelectionNotify;
64 requestor_->OnSelectionNotify(xev);
68 virtual void SetUp() OVERRIDE {
69 // Make X11 synchronous for our display connection.
70 XSynchronize(x_display_, True);
72 // Create a window for the selection requestor to use.
73 x_window_ = XCreateWindow(x_display_,
74 DefaultRootWindow(x_display_),
75 0, 0, 10, 10, // x, y, width, height
77 CopyFromParent, // depth
79 CopyFromParent, // visual
83 event_source_ = ui::PlatformEventSource::CreateDefault();
84 CHECK(ui::PlatformEventSource::GetInstance());
85 requestor_.reset(new SelectionRequestor(x_display_, x_window_, NULL));
88 virtual void TearDown() OVERRIDE {
90 event_source_.reset();
91 XDestroyWindow(x_display_, x_window_);
92 XSynchronize(x_display_, False);
97 // |requestor_|'s window.
100 scoped_ptr<ui::PlatformEventSource> event_source_;
101 scoped_ptr<SelectionRequestor> requestor_;
103 base::MessageLoopForUI message_loop_;
104 X11AtomCache atom_cache_;
107 DISALLOW_COPY_AND_ASSIGN(SelectionRequestorTest);
112 // Converts |selection| to |target| and checks the returned values.
113 void PerformBlockingConvertSelection(SelectionRequestor* requestor,
114 X11AtomCache* atom_cache,
117 const std::string& expected_data) {
118 scoped_refptr<base::RefCountedMemory> out_data;
119 size_t out_data_items = 0u;
120 XAtom out_type = None;
121 EXPECT_TRUE(requestor->PerformBlockingConvertSelection(
122 selection, target, &out_data, &out_data_items, &out_type));
123 EXPECT_EQ(expected_data, ui::RefCountedMemoryToString(out_data));
124 EXPECT_EQ(expected_data.size(), out_data_items);
125 EXPECT_EQ(atom_cache->GetAtom("STRING"), out_type);
130 // Test that SelectionRequestor correctly handles receiving a request while it
131 // is processing another request.
132 TEST_F(SelectionRequestorTest, NestedRequests) {
133 // Assume that |selection| will have no owner. If there is an owner, the owner
134 // will set the property passed into the XConvertSelection() request which is
136 XAtom selection = atom_cache_.GetAtom("FAKE_SELECTION");
138 XAtom target1 = atom_cache_.GetAtom("TARGET1");
139 XAtom target2 = atom_cache_.GetAtom("TARGET2");
141 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
142 loop->PostTask(FROM_HERE,
143 base::Bind(&PerformBlockingConvertSelection,
144 base::Unretained(requestor_.get()),
145 base::Unretained(&atom_cache_),
149 loop->PostTask(FROM_HERE,
150 base::Bind(&SelectionRequestorTest::SendSelectionNotify,
151 base::Unretained(this),
155 loop->PostTask(FROM_HERE,
156 base::Bind(&SelectionRequestorTest::SendSelectionNotify,
157 base::Unretained(this),
161 PerformBlockingConvertSelection(
162 requestor_.get(), &atom_cache_, selection, target1, "Data1");