| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/x/selection_requestor.h" | 5 #include "ui/base/x/selection_requestor.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/base/x/selection_utils.h" | 15 #include "ui/base/x/selection_utils.h" |
| 16 #include "ui/base/x/x11_util.h" | 16 #include "ui/base/x/x11_util.h" |
| 17 #include "ui/events/platform/platform_event_source.h" | 17 #include "ui/events/platform/platform_event_source.h" |
| 18 #include "ui/gfx/x/x11_atom_cache.h" | |
| 19 #include "ui/gfx/x/x11_types.h" | 18 #include "ui/gfx/x/x11_types.h" |
| 20 | 19 |
| 21 #include <X11/Xlib.h> | 20 #include <X11/Xlib.h> |
| 22 | 21 |
| 23 namespace ui { | 22 namespace ui { |
| 24 | 23 |
| 25 namespace { | |
| 26 | |
| 27 const char* kAtomsToCache[] = { | |
| 28 "STRING", | |
| 29 NULL | |
| 30 }; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 class SelectionRequestorTest : public testing::Test { | 24 class SelectionRequestorTest : public testing::Test { |
| 35 public: | 25 public: |
| 36 SelectionRequestorTest() | 26 SelectionRequestorTest() : x_display_(gfx::GetXDisplay()), x_window_(None) {} |
| 37 : x_display_(gfx::GetXDisplay()), | |
| 38 x_window_(None), | |
| 39 atom_cache_(gfx::GetXDisplay(), kAtomsToCache) { | |
| 40 atom_cache_.allow_uncached_atoms(); | |
| 41 } | |
| 42 | 27 |
| 43 ~SelectionRequestorTest() override {} | 28 ~SelectionRequestorTest() override {} |
| 44 | 29 |
| 45 // Responds to the SelectionRequestor's XConvertSelection() request by | 30 // Responds to the SelectionRequestor's XConvertSelection() request by |
| 46 // - Setting the property passed into the XConvertSelection() request to | 31 // - Setting the property passed into the XConvertSelection() request to |
| 47 // |value|. | 32 // |value|. |
| 48 // - Sending a SelectionNotify event. | 33 // - Sending a SelectionNotify event. |
| 49 void SendSelectionNotify(XAtom selection, | 34 void SendSelectionNotify(XAtom selection, |
| 50 XAtom target, | 35 XAtom target, |
| 51 const std::string& value) { | 36 const std::string& value) { |
| 52 ui::SetStringProperty(x_window_, | 37 ui::SetStringProperty(x_window_, requestor_->x_property_, GetAtom("STRING"), |
| 53 requestor_->x_property_, | |
| 54 atom_cache_.GetAtom("STRING"), | |
| 55 value); | 38 value); |
| 56 | 39 |
| 57 XEvent xev; | 40 XEvent xev; |
| 58 xev.type = SelectionNotify; | 41 xev.type = SelectionNotify; |
| 59 xev.xselection.serial = 0u; | 42 xev.xselection.serial = 0u; |
| 60 xev.xselection.display = x_display_; | 43 xev.xselection.display = x_display_; |
| 61 xev.xselection.requestor = x_window_; | 44 xev.xselection.requestor = x_window_; |
| 62 xev.xselection.selection = selection; | 45 xev.xselection.selection = selection; |
| 63 xev.xselection.target = target; | 46 xev.xselection.target = target; |
| 64 xev.xselection.property = requestor_->x_property_; | 47 xev.xselection.property = requestor_->x_property_; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 80 |
| 98 Display* x_display_; | 81 Display* x_display_; |
| 99 | 82 |
| 100 // |requestor_|'s window. | 83 // |requestor_|'s window. |
| 101 XID x_window_; | 84 XID x_window_; |
| 102 | 85 |
| 103 std::unique_ptr<ui::PlatformEventSource> event_source_; | 86 std::unique_ptr<ui::PlatformEventSource> event_source_; |
| 104 std::unique_ptr<SelectionRequestor> requestor_; | 87 std::unique_ptr<SelectionRequestor> requestor_; |
| 105 | 88 |
| 106 base::MessageLoopForUI message_loop_; | 89 base::MessageLoopForUI message_loop_; |
| 107 X11AtomCache atom_cache_; | |
| 108 | 90 |
| 109 private: | 91 private: |
| 110 DISALLOW_COPY_AND_ASSIGN(SelectionRequestorTest); | 92 DISALLOW_COPY_AND_ASSIGN(SelectionRequestorTest); |
| 111 }; | 93 }; |
| 112 | 94 |
| 113 namespace { | 95 namespace { |
| 114 | 96 |
| 115 // Converts |selection| to |target| and checks the returned values. | 97 // Converts |selection| to |target| and checks the returned values. |
| 116 void PerformBlockingConvertSelection(SelectionRequestor* requestor, | 98 void PerformBlockingConvertSelection(SelectionRequestor* requestor, |
| 117 X11AtomCache* atom_cache, | |
| 118 XAtom selection, | 99 XAtom selection, |
| 119 XAtom target, | 100 XAtom target, |
| 120 const std::string& expected_data) { | 101 const std::string& expected_data) { |
| 121 scoped_refptr<base::RefCountedMemory> out_data; | 102 scoped_refptr<base::RefCountedMemory> out_data; |
| 122 size_t out_data_items = 0u; | 103 size_t out_data_items = 0u; |
| 123 XAtom out_type = None; | 104 XAtom out_type = None; |
| 124 EXPECT_TRUE(requestor->PerformBlockingConvertSelection( | 105 EXPECT_TRUE(requestor->PerformBlockingConvertSelection( |
| 125 selection, target, &out_data, &out_data_items, &out_type)); | 106 selection, target, &out_data, &out_data_items, &out_type)); |
| 126 EXPECT_EQ(expected_data, ui::RefCountedMemoryToString(out_data)); | 107 EXPECT_EQ(expected_data, ui::RefCountedMemoryToString(out_data)); |
| 127 EXPECT_EQ(expected_data.size(), out_data_items); | 108 EXPECT_EQ(expected_data.size(), out_data_items); |
| 128 EXPECT_EQ(atom_cache->GetAtom("STRING"), out_type); | 109 EXPECT_EQ(GetAtom("STRING"), out_type); |
| 129 } | 110 } |
| 130 | 111 |
| 131 } // namespace | 112 } // namespace |
| 132 | 113 |
| 133 // Test that SelectionRequestor correctly handles receiving a request while it | 114 // Test that SelectionRequestor correctly handles receiving a request while it |
| 134 // is processing another request. | 115 // is processing another request. |
| 135 TEST_F(SelectionRequestorTest, NestedRequests) { | 116 TEST_F(SelectionRequestorTest, NestedRequests) { |
| 136 // Assume that |selection| will have no owner. If there is an owner, the owner | 117 // Assume that |selection| will have no owner. If there is an owner, the owner |
| 137 // will set the property passed into the XConvertSelection() request which is | 118 // will set the property passed into the XConvertSelection() request which is |
| 138 // undesirable. | 119 // undesirable. |
| 139 XAtom selection = atom_cache_.GetAtom("FAKE_SELECTION"); | 120 XAtom selection = GetAtom("FAKE_SELECTION"); |
| 140 | 121 |
| 141 XAtom target1 = atom_cache_.GetAtom("TARGET1"); | 122 XAtom target1 = GetAtom("TARGET1"); |
| 142 XAtom target2 = atom_cache_.GetAtom("TARGET2"); | 123 XAtom target2 = GetAtom("TARGET2"); |
| 143 | 124 |
| 144 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 125 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 145 loop->task_runner()->PostTask( | 126 loop->task_runner()->PostTask( |
| 146 FROM_HERE, | 127 FROM_HERE, base::Bind(&PerformBlockingConvertSelection, |
| 147 base::Bind(&PerformBlockingConvertSelection, | 128 base::Unretained(requestor_.get()), selection, |
| 148 base::Unretained(requestor_.get()), | 129 target2, "Data2")); |
| 149 base::Unretained(&atom_cache_), selection, target2, "Data2")); | |
| 150 loop->task_runner()->PostTask( | 130 loop->task_runner()->PostTask( |
| 151 FROM_HERE, | 131 FROM_HERE, |
| 152 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | 132 base::Bind(&SelectionRequestorTest::SendSelectionNotify, |
| 153 base::Unretained(this), selection, target1, "Data1")); | 133 base::Unretained(this), selection, target1, "Data1")); |
| 154 loop->task_runner()->PostTask( | 134 loop->task_runner()->PostTask( |
| 155 FROM_HERE, | 135 FROM_HERE, |
| 156 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | 136 base::Bind(&SelectionRequestorTest::SendSelectionNotify, |
| 157 base::Unretained(this), selection, target2, "Data2")); | 137 base::Unretained(this), selection, target2, "Data2")); |
| 158 PerformBlockingConvertSelection( | 138 PerformBlockingConvertSelection(requestor_.get(), selection, target1, |
| 159 requestor_.get(), &atom_cache_, selection, target1, "Data1"); | 139 "Data1"); |
| 160 } | 140 } |
| 161 | 141 |
| 162 } // namespace ui | 142 } // namespace ui |
| OLD | NEW |