| 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" |
| 18 #include "ui/gfx/x/x11_types.h" | 19 #include "ui/gfx/x/x11_types.h" |
| 19 | 20 |
| 20 #include <X11/Xlib.h> | 21 #include <X11/Xlib.h> |
| 21 | 22 |
| 22 namespace ui { | 23 namespace ui { |
| 23 | 24 |
| 24 class SelectionRequestorTest : public testing::Test { | 25 class SelectionRequestorTest : public testing::Test { |
| 25 public: | 26 public: |
| 26 SelectionRequestorTest() : x_display_(gfx::GetXDisplay()), x_window_(None) {} | 27 SelectionRequestorTest() : x_display_(gfx::GetXDisplay()), x_window_(None) {} |
| 27 | 28 |
| 28 ~SelectionRequestorTest() override {} | 29 ~SelectionRequestorTest() override {} |
| 29 | 30 |
| 30 // Responds to the SelectionRequestor's XConvertSelection() request by | 31 // Responds to the SelectionRequestor's XConvertSelection() request by |
| 31 // - Setting the property passed into the XConvertSelection() request to | 32 // - Setting the property passed into the XConvertSelection() request to |
| 32 // |value|. | 33 // |value|. |
| 33 // - Sending a SelectionNotify event. | 34 // - Sending a SelectionNotify event. |
| 34 void SendSelectionNotify(XAtom selection, | 35 void SendSelectionNotify(XAtom selection, |
| 35 XAtom target, | 36 XAtom target, |
| 36 const std::string& value) { | 37 const std::string& value) { |
| 37 ui::SetStringProperty(x_window_, requestor_->x_property_, GetAtom("STRING"), | 38 ui::SetStringProperty(x_window_, requestor_->x_property_, |
| 38 value); | 39 gfx::GetAtom("STRING"), value); |
| 39 | 40 |
| 40 XEvent xev; | 41 XEvent xev; |
| 41 xev.type = SelectionNotify; | 42 xev.type = SelectionNotify; |
| 42 xev.xselection.serial = 0u; | 43 xev.xselection.serial = 0u; |
| 43 xev.xselection.display = x_display_; | 44 xev.xselection.display = x_display_; |
| 44 xev.xselection.requestor = x_window_; | 45 xev.xselection.requestor = x_window_; |
| 45 xev.xselection.selection = selection; | 46 xev.xselection.selection = selection; |
| 46 xev.xselection.target = target; | 47 xev.xselection.target = target; |
| 47 xev.xselection.property = requestor_->x_property_; | 48 xev.xselection.property = requestor_->x_property_; |
| 48 xev.xselection.time = CurrentTime; | 49 xev.xselection.time = CurrentTime; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 XAtom selection, | 100 XAtom selection, |
| 100 XAtom target, | 101 XAtom target, |
| 101 const std::string& expected_data) { | 102 const std::string& expected_data) { |
| 102 scoped_refptr<base::RefCountedMemory> out_data; | 103 scoped_refptr<base::RefCountedMemory> out_data; |
| 103 size_t out_data_items = 0u; | 104 size_t out_data_items = 0u; |
| 104 XAtom out_type = None; | 105 XAtom out_type = None; |
| 105 EXPECT_TRUE(requestor->PerformBlockingConvertSelection( | 106 EXPECT_TRUE(requestor->PerformBlockingConvertSelection( |
| 106 selection, target, &out_data, &out_data_items, &out_type)); | 107 selection, target, &out_data, &out_data_items, &out_type)); |
| 107 EXPECT_EQ(expected_data, ui::RefCountedMemoryToString(out_data)); | 108 EXPECT_EQ(expected_data, ui::RefCountedMemoryToString(out_data)); |
| 108 EXPECT_EQ(expected_data.size(), out_data_items); | 109 EXPECT_EQ(expected_data.size(), out_data_items); |
| 109 EXPECT_EQ(GetAtom("STRING"), out_type); | 110 EXPECT_EQ(gfx::GetAtom("STRING"), out_type); |
| 110 } | 111 } |
| 111 | 112 |
| 112 } // namespace | 113 } // namespace |
| 113 | 114 |
| 114 // Test that SelectionRequestor correctly handles receiving a request while it | 115 // Test that SelectionRequestor correctly handles receiving a request while it |
| 115 // is processing another request. | 116 // is processing another request. |
| 116 TEST_F(SelectionRequestorTest, NestedRequests) { | 117 TEST_F(SelectionRequestorTest, NestedRequests) { |
| 117 // Assume that |selection| will have no owner. If there is an owner, the owner | 118 // Assume that |selection| will have no owner. If there is an owner, the owner |
| 118 // will set the property passed into the XConvertSelection() request which is | 119 // will set the property passed into the XConvertSelection() request which is |
| 119 // undesirable. | 120 // undesirable. |
| 120 XAtom selection = GetAtom("FAKE_SELECTION"); | 121 XAtom selection = gfx::GetAtom("FAKE_SELECTION"); |
| 121 | 122 |
| 122 XAtom target1 = GetAtom("TARGET1"); | 123 XAtom target1 = gfx::GetAtom("TARGET1"); |
| 123 XAtom target2 = GetAtom("TARGET2"); | 124 XAtom target2 = gfx::GetAtom("TARGET2"); |
| 124 | 125 |
| 125 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 126 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 126 loop->task_runner()->PostTask( | 127 loop->task_runner()->PostTask( |
| 127 FROM_HERE, base::Bind(&PerformBlockingConvertSelection, | 128 FROM_HERE, base::Bind(&PerformBlockingConvertSelection, |
| 128 base::Unretained(requestor_.get()), selection, | 129 base::Unretained(requestor_.get()), selection, |
| 129 target2, "Data2")); | 130 target2, "Data2")); |
| 130 loop->task_runner()->PostTask( | 131 loop->task_runner()->PostTask( |
| 131 FROM_HERE, | 132 FROM_HERE, |
| 132 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | 133 base::Bind(&SelectionRequestorTest::SendSelectionNotify, |
| 133 base::Unretained(this), selection, target1, "Data1")); | 134 base::Unretained(this), selection, target1, "Data1")); |
| 134 loop->task_runner()->PostTask( | 135 loop->task_runner()->PostTask( |
| 135 FROM_HERE, | 136 FROM_HERE, |
| 136 base::Bind(&SelectionRequestorTest::SendSelectionNotify, | 137 base::Bind(&SelectionRequestorTest::SendSelectionNotify, |
| 137 base::Unretained(this), selection, target2, "Data2")); | 138 base::Unretained(this), selection, target2, "Data2")); |
| 138 PerformBlockingConvertSelection(requestor_.get(), selection, target1, | 139 PerformBlockingConvertSelection(requestor_.get(), selection, target1, |
| 139 "Data1"); | 140 "Data1"); |
| 140 } | 141 } |
| 141 | 142 |
| 142 } // namespace ui | 143 } // namespace ui |
| OLD | NEW |