| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "ui/base/x/selection_requestor.h" | |
| 6 | |
| 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" | |
| 16 | |
| 17 #include <X11/Xlib.h> | |
| 18 | |
| 19 namespace ui { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char* kAtomsToCache[] = { | |
| 24 "STRING", | |
| 25 NULL | |
| 26 }; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 class SelectionRequestorTest : public testing::Test { | |
| 31 public: | |
| 32 SelectionRequestorTest() | |
| 33 : x_display_(gfx::GetXDisplay()), | |
| 34 x_window_(None), | |
| 35 atom_cache_(gfx::GetXDisplay(), kAtomsToCache) { | |
| 36 atom_cache_.allow_uncached_atoms(); | |
| 37 } | |
| 38 | |
| 39 virtual ~SelectionRequestorTest() { | |
| 40 } | |
| 41 | |
| 42 // Responds to the SelectionRequestor's XConvertSelection() request by | |
| 43 // - Setting the property passed into the XConvertSelection() request to | |
| 44 // |value|. | |
| 45 // - Sending a SelectionNotify event. | |
| 46 void SendSelectionNotify(XAtom selection, | |
| 47 XAtom target, | |
| 48 const std::string& value) { | |
| 49 ui::SetStringProperty(x_window_, | |
| 50 requestor_->x_property_, | |
| 51 atom_cache_.GetAtom("STRING"), | |
| 52 value); | |
| 53 | |
| 54 XEvent xev; | |
| 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); | |
| 65 } | |
| 66 | |
| 67 protected: | |
| 68 virtual void SetUp() override { | |
| 69 // Make X11 synchronous for our display connection. | |
| 70 XSynchronize(x_display_, True); | |
| 71 | |
| 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 | |
| 76 0, // border width | |
| 77 CopyFromParent, // depth | |
| 78 InputOnly, | |
| 79 CopyFromParent, // visual | |
| 80 0, | |
| 81 NULL); | |
| 82 | |
| 83 event_source_ = ui::PlatformEventSource::CreateDefault(); | |
| 84 CHECK(ui::PlatformEventSource::GetInstance()); | |
| 85 requestor_.reset(new SelectionRequestor(x_display_, x_window_, NULL)); | |
| 86 } | |
| 87 | |
| 88 virtual void TearDown() override { | |
| 89 requestor_.reset(); | |
| 90 event_source_.reset(); | |
| 91 XDestroyWindow(x_display_, x_window_); | |
| 92 XSynchronize(x_display_, False); | |
| 93 } | |
| 94 | |
| 95 Display* x_display_; | |
| 96 | |
| 97 // |requestor_|'s window. | |
| 98 XID x_window_; | |
| 99 | |
| 100 scoped_ptr<ui::PlatformEventSource> event_source_; | |
| 101 scoped_ptr<SelectionRequestor> requestor_; | |
| 102 | |
| 103 base::MessageLoopForUI message_loop_; | |
| 104 X11AtomCache atom_cache_; | |
| 105 | |
| 106 private: | |
| 107 DISALLOW_COPY_AND_ASSIGN(SelectionRequestorTest); | |
| 108 }; | |
| 109 | |
| 110 namespace { | |
| 111 | |
| 112 // Converts |selection| to |target| and checks the returned values. | |
| 113 void PerformBlockingConvertSelection(SelectionRequestor* requestor, | |
| 114 X11AtomCache* atom_cache, | |
| 115 XAtom selection, | |
| 116 XAtom target, | |
| 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); | |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| 129 | |
| 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 | |
| 135 // undesirable. | |
| 136 XAtom selection = atom_cache_.GetAtom("FAKE_SELECTION"); | |
| 137 | |
| 138 XAtom target1 = atom_cache_.GetAtom("TARGET1"); | |
| 139 XAtom target2 = atom_cache_.GetAtom("TARGET2"); | |
| 140 | |
| 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_), | |
| 146 selection, | |
| 147 target2, | |
| 148 "Data2")); | |
| 149 loop->PostTask(FROM_HERE, | |
| 150 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | |
| 151 base::Unretained(this), | |
| 152 selection, | |
| 153 target1, | |
| 154 "Data1")); | |
| 155 loop->PostTask(FROM_HERE, | |
| 156 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | |
| 157 base::Unretained(this), | |
| 158 selection, | |
| 159 target2, | |
| 160 "Data2")); | |
| 161 PerformBlockingConvertSelection( | |
| 162 requestor_.get(), &atom_cache_, selection, target1, "Data1"); | |
| 163 } | |
| 164 | |
| 165 } // namespace ui | |
| OLD | NEW |