Chromium Code Reviews| 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() : x_display_(gfx::GetXDisplay()), | |
|
Daniel Erat
2014/07/16 22:17:01
nit: ':' of constructor list should be on the next
| |
| 33 x_window_(None), | |
| 34 atom_cache_(gfx::GetXDisplay(), kAtomsToCache) { | |
| 35 atom_cache_.allow_uncached_atoms(); | |
| 36 } | |
| 37 | |
| 38 virtual ~SelectionRequestorTest() { | |
| 39 } | |
| 40 | |
| 41 // Responds to the SelectionRequestor's XConvertSelection() request by | |
| 42 // - Setting to the property passed into the XConvertSelection() request to | |
| 43 // |value|. | |
| 44 // - Sending a SelectionNotify event. | |
| 45 void SendSelectionNotify(XAtom selection, | |
| 46 XAtom target, | |
| 47 const std::string& value) { | |
| 48 ui::SetStringProperty(x_window_, | |
| 49 requestor_->x_property_, | |
| 50 atom_cache_.GetAtom("STRING"), | |
| 51 value); | |
| 52 | |
| 53 XEvent xev; | |
| 54 xev.type = SelectionNotify; | |
| 55 xev.xselection.serial = 0u; | |
| 56 xev.xselection.display = x_display_; | |
| 57 xev.xselection.requestor = x_window_; | |
| 58 xev.xselection.selection = selection; | |
| 59 xev.xselection.target = target; | |
| 60 xev.xselection.property = requestor_->x_property_; | |
| 61 xev.xselection.time = CurrentTime; | |
| 62 xev.xselection.type = SelectionNotify; | |
| 63 requestor_->OnSelectionNotify(xev); | |
| 64 } | |
| 65 | |
| 66 SelectionRequestor* requestor() { | |
|
Daniel Erat
2014/07/16 22:17:01
nit: make the member protected so you don't need a
| |
| 67 return requestor_.get(); | |
| 68 } | |
| 69 | |
| 70 X11AtomCache* atom_cache() { | |
|
Daniel Erat
2014/07/16 22:17:01
nit: make the member protected so you don't need a
| |
| 71 return &atom_cache_; | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 virtual void SetUp() OVERRIDE { | |
| 76 // Make X11 synchronous for our display connection. | |
| 77 XSynchronize(x_display_, True); | |
| 78 | |
| 79 // Create a window for the selection requestor to use. | |
| 80 x_window_ = XCreateWindow(x_display_, | |
| 81 DefaultRootWindow(x_display_), | |
| 82 0, 0, 10, 10, // x, y, width, height | |
| 83 0, // border width | |
| 84 CopyFromParent, // depth | |
| 85 InputOnly, | |
| 86 CopyFromParent, // visual | |
| 87 0, | |
| 88 NULL); | |
| 89 | |
| 90 event_source_ = ui::PlatformEventSource::CreateDefault(); | |
| 91 CHECK(ui::PlatformEventSource::GetInstance()); | |
| 92 requestor_.reset(new SelectionRequestor(x_display_, x_window_, NULL)); | |
| 93 } | |
| 94 | |
| 95 virtual void TearDown() OVERRIDE { | |
| 96 requestor_.reset(); | |
| 97 event_source_.reset(); | |
| 98 XDestroyWindow(x_display_, x_window_); | |
| 99 XSynchronize(x_display_, False); | |
| 100 } | |
| 101 | |
| 102 Display* x_display_; | |
| 103 | |
| 104 // |requestor_|'s window. | |
| 105 XID x_window_; | |
| 106 | |
| 107 scoped_ptr<ui::PlatformEventSource> event_source_; | |
| 108 scoped_ptr<SelectionRequestor> requestor_; | |
| 109 | |
| 110 base::MessageLoopForUI message_loop_; | |
| 111 X11AtomCache atom_cache_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(SelectionRequestorTest); | |
| 114 }; | |
| 115 | |
| 116 namespace { | |
| 117 | |
| 118 // Converts |selection| to |target| and checks the returned values. | |
| 119 void PerformBlockingConvertSelection(SelectionRequestorTest* test, | |
| 120 XAtom selection, | |
| 121 XAtom target, | |
| 122 const std::string& expected_data) { | |
| 123 scoped_refptr<base::RefCountedMemory> out_data; | |
| 124 size_t out_data_items = 0u; | |
| 125 XAtom out_type = None; | |
| 126 EXPECT_TRUE(test->requestor()->PerformBlockingConvertSelection( | |
| 127 selection, target, &out_data, &out_data_items, &out_type)); | |
| 128 EXPECT_EQ(expected_data, ui::RefCountedMemoryToString(out_data)); | |
| 129 EXPECT_EQ(expected_data.size(), out_data_items); | |
| 130 EXPECT_EQ(test->atom_cache()->GetAtom("STRING"), out_type); | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 // Test that SelectionRequestor correctly handles receiving a request while it | |
| 136 // is processing another request. | |
| 137 TEST_F(SelectionRequestorTest, NestedRequests) { | |
| 138 // Assume that |selection| will have no owner. If there is an owner, the owner | |
| 139 // will set the property passed into the XConvertSelection() request which is | |
| 140 // undesirable. | |
| 141 XAtom selection = atom_cache()->GetAtom("FAKE_SELECTION"); | |
| 142 | |
| 143 XAtom target1 = atom_cache()->GetAtom("TARGET1"); | |
| 144 XAtom target2 = atom_cache()->GetAtom("TARGET2"); | |
| 145 | |
| 146 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | |
| 147 loop->PostTask(FROM_HERE, | |
| 148 base::Bind(&PerformBlockingConvertSelection, | |
| 149 base::Unretained(this), | |
| 150 selection, | |
| 151 target2, | |
| 152 "Data2")); | |
| 153 loop->PostTask(FROM_HERE, | |
| 154 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | |
| 155 base::Unretained(this), | |
| 156 selection, | |
| 157 target1, | |
| 158 "Data1")); | |
| 159 loop->PostTask(FROM_HERE, | |
| 160 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | |
| 161 base::Unretained(this), | |
| 162 selection, | |
| 163 target2, | |
| 164 "Data2")); | |
| 165 PerformBlockingConvertSelection(this, selection, target1, "Data1"); | |
| 166 } | |
| 167 | |
| 168 } // namespace ui | |
| OLD | NEW |