[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 7 | #include <algorithm> |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 8 | #include <memory> |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/bind.h" |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 2a9ec0e | 2013-07-17 23:00:30 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 15 | #include "base/run_loop.h" |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 16 | #include "base/single_thread_task_runner.h" |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 17 | #include "base/stl_util.h" |
[email protected] | 65e8e25 | 2011-11-11 08:36:22 | [diff] [blame] | 18 | #include "base/test/test_timeouts.h" |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 19 | #include "base/threading/thread.h" |
| 20 | #include "base/threading/thread_restrictions.h" |
| 21 | #include "dbus/bus.h" |
| 22 | #include "dbus/message.h" |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 23 | #include "dbus/object_path.h" |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 24 | #include "dbus/object_proxy.h" |
| 25 | #include "dbus/test_service.h" |
| 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 28 | namespace dbus { |
| 29 | |
[email protected] | 3635d32 | 2012-06-02 02:53:20 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // See comments in ObjectProxy::RunResponseCallback() for why the number was |
| 33 | // chosen. |
| 34 | const int kHugePayloadSize = 64 << 20; // 64 MB |
| 35 | |
| 36 | } // namespace |
| 37 | |
[email protected] | 829f0e4c | 2011-08-31 18:02:43 | [diff] [blame] | 38 | // The end-to-end test exercises the asynchronous APIs in ObjectProxy and |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 39 | // ExportedObject. |
| 40 | class EndToEndAsyncTest : public testing::Test { |
| 41 | public: |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 42 | void SetUp() override { |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 43 | // Make the main thread not to allow IO. |
| 44 | base::ThreadRestrictions::SetIOAllowed(false); |
| 45 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 46 | // Start the D-Bus thread. |
| 47 | dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
| 48 | base::Thread::Options thread_options; |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 49 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 50 | ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 51 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 52 | // Start the test service, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 53 | TestService::Options options; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 54 | options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 55 | test_service_.reset(new TestService(options)); |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 56 | ASSERT_TRUE(test_service_->StartService()); |
| 57 | ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 58 | ASSERT_TRUE(test_service_->HasDBusThread()); |
| 59 | |
| 60 | // Create the client, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 61 | Bus::Options bus_options; |
| 62 | bus_options.bus_type = Bus::SESSION; |
| 63 | bus_options.connection_type = Bus::PRIVATE; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 64 | bus_options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 65 | bus_ = new Bus(bus_options); |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 66 | object_proxy_ = bus_->GetObjectProxy( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 67 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 68 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 69 | ASSERT_TRUE(bus_->HasDBusThread()); |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 70 | |
[email protected] | 0ad9ef8 | 2011-11-23 22:08:38 | [diff] [blame] | 71 | // Connect to the "Test" signal of "org.chromium.TestInterface" from |
| 72 | // the remote object. |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 73 | object_proxy_->ConnectToSignal( |
| 74 | "org.chromium.TestInterface", |
| 75 | "Test", |
| 76 | base::Bind(&EndToEndAsyncTest::OnTestSignal, |
| 77 | base::Unretained(this)), |
| 78 | base::Bind(&EndToEndAsyncTest::OnConnected, |
| 79 | base::Unretained(this))); |
[email protected] | 4c985c6 | 2011-12-13 17:08:42 | [diff] [blame] | 80 | // Wait until the object proxy is connected to the signal. |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 81 | run_loop_.reset(new base::RunLoop()); |
| 82 | run_loop_->Run(); |
[email protected] | 4c985c6 | 2011-12-13 17:08:42 | [diff] [blame] | 83 | |
[email protected] | 0ad9ef8 | 2011-11-23 22:08:38 | [diff] [blame] | 84 | // Connect to the "Test2" signal of "org.chromium.TestInterface" from |
| 85 | // the remote object. There was a bug where we were emitting error |
| 86 | // messages like "Requested to remove an unknown match rule: ..." at |
| 87 | // the shutdown of Bus when an object proxy is connected to more than |
| 88 | // one signal of the same interface. See crosbug.com/23382 for details. |
| 89 | object_proxy_->ConnectToSignal( |
| 90 | "org.chromium.TestInterface", |
| 91 | "Test2", |
| 92 | base::Bind(&EndToEndAsyncTest::OnTest2Signal, |
| 93 | base::Unretained(this)), |
| 94 | base::Bind(&EndToEndAsyncTest::OnConnected, |
| 95 | base::Unretained(this))); |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 96 | // Wait until the object proxy is connected to the signal. |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 97 | run_loop_.reset(new base::RunLoop()); |
| 98 | run_loop_->Run(); |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 99 | |
| 100 | // Create a second object proxy for the root object. |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 101 | root_object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 102 | ObjectPath("/")); |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 103 | ASSERT_TRUE(bus_->HasDBusThread()); |
| 104 | |
| 105 | // Connect to the "Test" signal of "org.chromium.TestInterface" from |
| 106 | // the root remote object too. |
| 107 | root_object_proxy_->ConnectToSignal( |
| 108 | "org.chromium.TestInterface", |
| 109 | "Test", |
| 110 | base::Bind(&EndToEndAsyncTest::OnRootTestSignal, |
| 111 | base::Unretained(this)), |
| 112 | base::Bind(&EndToEndAsyncTest::OnConnected, |
| 113 | base::Unretained(this))); |
| 114 | // Wait until the root object proxy is connected to the signal. |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 115 | run_loop_.reset(new base::RunLoop()); |
| 116 | run_loop_->Run(); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 117 | } |
| 118 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 119 | void TearDown() override { |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 120 | bus_->ShutdownOnDBusThreadAndBlock(); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 121 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 122 | // Shut down the service. |
[email protected] | e20d39fe | 2011-09-02 06:56:23 | [diff] [blame] | 123 | test_service_->ShutdownAndBlock(); |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 124 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 125 | // Reset to the default. |
| 126 | base::ThreadRestrictions::SetIOAllowed(true); |
| 127 | |
[email protected] | 829f0e4c | 2011-08-31 18:02:43 | [diff] [blame] | 128 | // Stopping a thread is considered an IO operation, so do this after |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 129 | // allowing IO. |
| 130 | test_service_->Stop(); |
| 131 | } |
| 132 | |
| 133 | protected: |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 134 | // Replaces the bus with a broken one. |
| 135 | void SetUpBrokenBus() { |
| 136 | // Shut down the existing bus. |
| 137 | bus_->ShutdownOnDBusThreadAndBlock(); |
| 138 | |
| 139 | // Create new bus with invalid address. |
| 140 | const char kInvalidAddress[] = ""; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 141 | Bus::Options bus_options; |
| 142 | bus_options.bus_type = Bus::CUSTOM_ADDRESS; |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 143 | bus_options.address = kInvalidAddress; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 144 | bus_options.connection_type = Bus::PRIVATE; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 145 | bus_options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 146 | bus_ = new Bus(bus_options); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 147 | ASSERT_TRUE(bus_->HasDBusThread()); |
| 148 | |
| 149 | // Create new object proxy. |
| 150 | object_proxy_ = bus_->GetObjectProxy( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 151 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 152 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 829f0e4c | 2011-08-31 18:02:43 | [diff] [blame] | 155 | // Calls the method asynchronously. OnResponse() will be called once the |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 156 | // response is received. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 157 | void CallMethod(MethodCall* method_call, |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 158 | int timeout_ms) { |
| 159 | object_proxy_->CallMethod(method_call, |
| 160 | timeout_ms, |
| 161 | base::Bind(&EndToEndAsyncTest::OnResponse, |
| 162 | base::Unretained(this))); |
| 163 | } |
| 164 | |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 165 | // Calls the method asynchronously. OnResponse() will be called once the |
| 166 | // response is received without error, otherwise OnError() will be called. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 167 | void CallMethodWithErrorCallback(MethodCall* method_call, |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 168 | int timeout_ms) { |
| 169 | object_proxy_->CallMethodWithErrorCallback( |
| 170 | method_call, |
| 171 | timeout_ms, |
| 172 | base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)), |
| 173 | base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this))); |
| 174 | } |
| 175 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 176 | // Wait for the give number of responses. |
| 177 | void WaitForResponses(size_t num_responses) { |
| 178 | while (response_strings_.size() < num_responses) { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 179 | run_loop_.reset(new base::RunLoop); |
| 180 | run_loop_->Run(); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // Called when the response is received. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 185 | void OnResponse(Response* response) { |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 186 | // |response| will be deleted on exit of the function. Copy the |
| 187 | // payload to |response_strings_|. |
| 188 | if (response) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 189 | MessageReader reader(response); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 190 | std::string response_string; |
| 191 | ASSERT_TRUE(reader.PopString(&response_string)); |
| 192 | response_strings_.push_back(response_string); |
| 193 | } else { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 194 | response_strings_.push_back(std::string()); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 195 | } |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 196 | run_loop_->Quit(); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 197 | }; |
| 198 | |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 199 | // Wait for the given number of errors. |
| 200 | void WaitForErrors(size_t num_errors) { |
| 201 | while (error_names_.size() < num_errors) { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 202 | run_loop_.reset(new base::RunLoop); |
| 203 | run_loop_->Run(); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | // Called when an error is received. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 208 | void OnError(ErrorResponse* error) { |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 209 | // |error| will be deleted on exit of the function. Copy the payload to |
| 210 | // |error_names_|. |
| 211 | if (error) { |
| 212 | ASSERT_NE("", error->GetErrorName()); |
| 213 | error_names_.push_back(error->GetErrorName()); |
| 214 | } else { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 215 | error_names_.push_back(std::string()); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 216 | } |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 217 | run_loop_->Quit(); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 220 | // Called when the "Test" signal is received, in the main thread. |
| 221 | // Copy the string payload to |test_signal_string_|. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 222 | void OnTestSignal(Signal* signal) { |
| 223 | MessageReader reader(signal); |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 224 | ASSERT_TRUE(reader.PopString(&test_signal_string_)); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 225 | run_loop_->Quit(); |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 228 | // Called when the "Test" signal is received, in the main thread, by |
| 229 | // the root object proxy. Copy the string payload to |
| 230 | // |root_test_signal_string_|. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 231 | void OnRootTestSignal(Signal* signal) { |
| 232 | MessageReader reader(signal); |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 233 | ASSERT_TRUE(reader.PopString(&root_test_signal_string_)); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 234 | run_loop_->Quit(); |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | 0ad9ef8 | 2011-11-23 22:08:38 | [diff] [blame] | 237 | // Called when the "Test2" signal is received, in the main thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 238 | void OnTest2Signal(Signal* signal) { |
| 239 | MessageReader reader(signal); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 240 | run_loop_->Quit(); |
[email protected] | 0ad9ef8 | 2011-11-23 22:08:38 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 243 | // Called when connected to the signal. |
| 244 | void OnConnected(const std::string& interface_name, |
| 245 | const std::string& signal_name, |
| 246 | bool success) { |
| 247 | ASSERT_TRUE(success); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 248 | run_loop_->Quit(); |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Wait for the hey signal to be received. |
| 252 | void WaitForTestSignal() { |
| 253 | // OnTestSignal() will quit the message loop. |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 254 | run_loop_.reset(new base::RunLoop); |
| 255 | run_loop_->Run(); |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 258 | base::MessageLoop message_loop_; |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 259 | std::unique_ptr<base::RunLoop> run_loop_; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 260 | std::vector<std::string> response_strings_; |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 261 | std::vector<std::string> error_names_; |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 262 | std::unique_ptr<base::Thread> dbus_thread_; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 263 | scoped_refptr<Bus> bus_; |
| 264 | ObjectProxy* object_proxy_; |
| 265 | ObjectProxy* root_object_proxy_; |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 266 | std::unique_ptr<TestService> test_service_; |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 267 | // Text message from "Test" signal. |
| 268 | std::string test_signal_string_; |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 269 | // Text message from "Test" signal delivered to root. |
| 270 | std::string root_test_signal_string_; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | TEST_F(EndToEndAsyncTest, Echo) { |
| 274 | const char* kHello = "hello"; |
| 275 | |
| 276 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 277 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 278 | MessageWriter writer(&method_call); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 279 | writer.AppendString(kHello); |
| 280 | |
| 281 | // Call the method. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 282 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 283 | CallMethod(&method_call, timeout_ms); |
| 284 | |
| 285 | // Check the response. |
| 286 | WaitForResponses(1); |
| 287 | EXPECT_EQ(kHello, response_strings_[0]); |
| 288 | } |
| 289 | |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 290 | TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) { |
| 291 | const char* kHello = "hello"; |
| 292 | |
| 293 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 294 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 295 | MessageWriter writer(&method_call); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 296 | writer.AppendString(kHello); |
| 297 | |
| 298 | // Call the method. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 299 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 300 | CallMethodWithErrorCallback(&method_call, timeout_ms); |
| 301 | |
| 302 | // Check the response. |
| 303 | WaitForResponses(1); |
| 304 | EXPECT_EQ(kHello, response_strings_[0]); |
| 305 | EXPECT_TRUE(error_names_.empty()); |
| 306 | } |
| 307 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 308 | // Call Echo method three times. |
| 309 | TEST_F(EndToEndAsyncTest, EchoThreeTimes) { |
| 310 | const char* kMessages[] = { "foo", "bar", "baz" }; |
| 311 | |
| 312 | for (size_t i = 0; i < arraysize(kMessages); ++i) { |
| 313 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 314 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 315 | MessageWriter writer(&method_call); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 316 | writer.AppendString(kMessages[i]); |
| 317 | |
| 318 | // Call the method. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 319 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 320 | CallMethod(&method_call, timeout_ms); |
| 321 | } |
| 322 | |
| 323 | // Check the responses. |
| 324 | WaitForResponses(3); |
| 325 | // Sort as the order of the returned messages is not deterministic. |
| 326 | std::sort(response_strings_.begin(), response_strings_.end()); |
| 327 | EXPECT_EQ("bar", response_strings_[0]); |
| 328 | EXPECT_EQ("baz", response_strings_[1]); |
| 329 | EXPECT_EQ("foo", response_strings_[2]); |
| 330 | } |
| 331 | |
[email protected] | 3635d32 | 2012-06-02 02:53:20 | [diff] [blame] | 332 | TEST_F(EndToEndAsyncTest, Echo_HugePayload) { |
| 333 | const std::string kHugePayload(kHugePayloadSize, 'o'); |
| 334 | |
| 335 | // Create the method call with a huge payload. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 336 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 337 | MessageWriter writer(&method_call); |
[email protected] | 3635d32 | 2012-06-02 02:53:20 | [diff] [blame] | 338 | writer.AppendString(kHugePayload); |
| 339 | |
| 340 | // Call the method. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 341 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 3635d32 | 2012-06-02 02:53:20 | [diff] [blame] | 342 | CallMethod(&method_call, timeout_ms); |
| 343 | |
| 344 | // This caused a DCHECK failure before. Ensure that the issue is fixed. |
| 345 | WaitForResponses(1); |
| 346 | EXPECT_EQ(kHugePayload, response_strings_[0]); |
| 347 | } |
| 348 | |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 349 | TEST_F(EndToEndAsyncTest, BrokenBus) { |
| 350 | const char* kHello = "hello"; |
| 351 | |
| 352 | // Set up a broken bus. |
| 353 | SetUpBrokenBus(); |
| 354 | |
| 355 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 356 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 357 | MessageWriter writer(&method_call); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 358 | writer.AppendString(kHello); |
| 359 | |
| 360 | // Call the method. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 361 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 362 | CallMethod(&method_call, timeout_ms); |
| 363 | WaitForResponses(1); |
| 364 | |
| 365 | // Should fail because of the broken bus. |
| 366 | ASSERT_EQ("", response_strings_[0]); |
| 367 | } |
| 368 | |
| 369 | TEST_F(EndToEndAsyncTest, BrokenBusWithErrorCallback) { |
| 370 | const char* kHello = "hello"; |
| 371 | |
| 372 | // Set up a broken bus. |
| 373 | SetUpBrokenBus(); |
| 374 | |
| 375 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 376 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 377 | MessageWriter writer(&method_call); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 378 | writer.AppendString(kHello); |
| 379 | |
| 380 | // Call the method. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 381 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 382 | CallMethodWithErrorCallback(&method_call, timeout_ms); |
| 383 | WaitForErrors(1); |
| 384 | |
| 385 | // Should fail because of the broken bus. |
| 386 | ASSERT_TRUE(response_strings_.empty()); |
| 387 | ASSERT_EQ("", error_names_[0]); |
| 388 | } |
| 389 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 390 | TEST_F(EndToEndAsyncTest, Timeout) { |
| 391 | const char* kHello = "hello"; |
| 392 | |
| 393 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 394 | MethodCall method_call("org.chromium.TestInterface", "SlowEcho"); |
| 395 | MessageWriter writer(&method_call); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 396 | writer.AppendString(kHello); |
| 397 | |
[email protected] | 12f9766 | 2011-08-20 01:07:17 | [diff] [blame] | 398 | // Call the method with timeout of 0ms. |
| 399 | const int timeout_ms = 0; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 400 | CallMethod(&method_call, timeout_ms); |
| 401 | WaitForResponses(1); |
| 402 | |
| 403 | // Should fail because of timeout. |
| 404 | ASSERT_EQ("", response_strings_[0]); |
| 405 | } |
| 406 | |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 407 | TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) { |
| 408 | const char* kHello = "hello"; |
| 409 | |
| 410 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 411 | MethodCall method_call("org.chromium.TestInterface", "SlowEcho"); |
| 412 | MessageWriter writer(&method_call); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 413 | writer.AppendString(kHello); |
| 414 | |
| 415 | // Call the method with timeout of 0ms. |
| 416 | const int timeout_ms = 0; |
| 417 | CallMethodWithErrorCallback(&method_call, timeout_ms); |
| 418 | WaitForErrors(1); |
| 419 | |
| 420 | // Should fail because of timeout. |
| 421 | ASSERT_TRUE(response_strings_.empty()); |
| 422 | ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]); |
| 423 | } |
| 424 | |
hashimoto | 41ece52 | 2015-03-30 07:01:39 | [diff] [blame] | 425 | TEST_F(EndToEndAsyncTest, CancelPendingCalls) { |
| 426 | const char* kHello = "hello"; |
| 427 | |
| 428 | // Create the method call. |
| 429 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 430 | MessageWriter writer(&method_call); |
| 431 | writer.AppendString(kHello); |
| 432 | |
| 433 | // Call the method. |
| 434 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
| 435 | CallMethod(&method_call, timeout_ms); |
| 436 | |
| 437 | // Remove the object proxy before receiving the result. |
| 438 | // This results in cancelling the pending method call. |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 439 | bus_->RemoveObjectProxy(test_service_->service_name(), |
hashimoto | 41ece52 | 2015-03-30 07:01:39 | [diff] [blame] | 440 | ObjectPath("/org/chromium/TestObject"), |
| 441 | base::Bind(&base::DoNothing)); |
| 442 | |
| 443 | // We shouldn't receive any responses. Wait for a while just to make sure. |
| 444 | run_loop_.reset(new base::RunLoop); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 445 | message_loop_.task_runner()->PostDelayedTask( |
| 446 | FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout()); |
hashimoto | 41ece52 | 2015-03-30 07:01:39 | [diff] [blame] | 447 | run_loop_->Run(); |
| 448 | EXPECT_TRUE(response_strings_.empty()); |
| 449 | } |
| 450 | |
[email protected] | 9aa74cc | 2011-11-30 04:57:42 | [diff] [blame] | 451 | // Tests calling a method that sends its reply asynchronously. |
| 452 | TEST_F(EndToEndAsyncTest, AsyncEcho) { |
| 453 | const char* kHello = "hello"; |
| 454 | |
| 455 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 456 | MethodCall method_call("org.chromium.TestInterface", "AsyncEcho"); |
| 457 | MessageWriter writer(&method_call); |
[email protected] | 9aa74cc | 2011-11-30 04:57:42 | [diff] [blame] | 458 | writer.AppendString(kHello); |
| 459 | |
| 460 | // Call the method. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 461 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 9aa74cc | 2011-11-30 04:57:42 | [diff] [blame] | 462 | CallMethod(&method_call, timeout_ms); |
| 463 | |
| 464 | // Check the response. |
| 465 | WaitForResponses(1); |
| 466 | EXPECT_EQ(kHello, response_strings_[0]); |
| 467 | } |
| 468 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 469 | TEST_F(EndToEndAsyncTest, NonexistentMethod) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 470 | MethodCall method_call("org.chromium.TestInterface", "Nonexistent"); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 471 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 472 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 473 | CallMethod(&method_call, timeout_ms); |
| 474 | WaitForResponses(1); |
| 475 | |
| 476 | // Should fail because the method is nonexistent. |
| 477 | ASSERT_EQ("", response_strings_[0]); |
| 478 | } |
| 479 | |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 480 | TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 481 | MethodCall method_call("org.chromium.TestInterface", "Nonexistent"); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 482 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 483 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 484 | CallMethodWithErrorCallback(&method_call, timeout_ms); |
| 485 | WaitForErrors(1); |
| 486 | |
| 487 | // Should fail because the method is nonexistent. |
| 488 | ASSERT_TRUE(response_strings_.empty()); |
| 489 | ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]); |
| 490 | } |
| 491 | |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 492 | TEST_F(EndToEndAsyncTest, BrokenMethod) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 493 | MethodCall method_call("org.chromium.TestInterface", "BrokenMethod"); |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 494 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 495 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | a5107611 | 2011-08-17 20:58:12 | [diff] [blame] | 496 | CallMethod(&method_call, timeout_ms); |
| 497 | WaitForResponses(1); |
| 498 | |
| 499 | // Should fail because the method is broken. |
| 500 | ASSERT_EQ("", response_strings_[0]); |
| 501 | } |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 502 | |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 503 | TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 504 | MethodCall method_call("org.chromium.TestInterface", "BrokenMethod"); |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 505 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 506 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 91fe7ea | 2012-04-20 03:18:27 | [diff] [blame] | 507 | CallMethodWithErrorCallback(&method_call, timeout_ms); |
| 508 | WaitForErrors(1); |
| 509 | |
| 510 | // Should fail because the method is broken. |
| 511 | ASSERT_TRUE(response_strings_.empty()); |
| 512 | ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]); |
| 513 | } |
| 514 | |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 515 | TEST_F(EndToEndAsyncTest, InvalidObjectPath) { |
| 516 | // Trailing '/' is only allowed for the root path. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 517 | const ObjectPath invalid_object_path("/org/chromium/TestObject/"); |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 518 | |
| 519 | // Replace object proxy with new one. |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 520 | object_proxy_ = bus_->GetObjectProxy(test_service_->service_name(), |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 521 | invalid_object_path); |
| 522 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 523 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 524 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 525 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 526 | CallMethodWithErrorCallback(&method_call, timeout_ms); |
| 527 | WaitForErrors(1); |
| 528 | |
| 529 | // Should fail because of the invalid path. |
| 530 | ASSERT_TRUE(response_strings_.empty()); |
| 531 | ASSERT_EQ("", error_names_[0]); |
| 532 | } |
| 533 | |
| 534 | TEST_F(EndToEndAsyncTest, InvalidServiceName) { |
| 535 | // Bus name cannot contain '/'. |
| 536 | const std::string invalid_service_name = ":1/2"; |
| 537 | |
| 538 | // Replace object proxy with new one. |
| 539 | object_proxy_ = bus_->GetObjectProxy( |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 540 | invalid_service_name, ObjectPath("org.chromium.TestObject")); |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 541 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 542 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 543 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 544 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | ca72ff2 | 2012-05-23 06:55:22 | [diff] [blame] | 545 | CallMethodWithErrorCallback(&method_call, timeout_ms); |
| 546 | WaitForErrors(1); |
| 547 | |
| 548 | // Should fail because of the invalid bus name. |
| 549 | ASSERT_TRUE(response_strings_.empty()); |
| 550 | ASSERT_EQ("", error_names_[0]); |
| 551 | } |
| 552 | |
[email protected] | 65e8e25 | 2011-11-11 08:36:22 | [diff] [blame] | 553 | TEST_F(EndToEndAsyncTest, EmptyResponseCallback) { |
| 554 | const char* kHello = "hello"; |
| 555 | |
| 556 | // Create the method call. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 557 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 558 | MessageWriter writer(&method_call); |
[email protected] | 65e8e25 | 2011-11-11 08:36:22 | [diff] [blame] | 559 | writer.AppendString(kHello); |
| 560 | |
| 561 | // Call the method with an empty callback. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 562 | const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
[email protected] | 65e8e25 | 2011-11-11 08:36:22 | [diff] [blame] | 563 | object_proxy_->CallMethod(&method_call, |
| 564 | timeout_ms, |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 565 | ObjectProxy::EmptyResponseCallback()); |
[email protected] | 65e8e25 | 2011-11-11 08:36:22 | [diff] [blame] | 566 | // Post a delayed task to quit the message loop. |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 567 | run_loop_.reset(new base::RunLoop); |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 568 | message_loop_.task_runner()->PostDelayedTask( |
| 569 | FROM_HERE, run_loop_->QuitClosure(), TestTimeouts::tiny_timeout()); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 570 | run_loop_->Run(); |
[email protected] | 65e8e25 | 2011-11-11 08:36:22 | [diff] [blame] | 571 | // We cannot tell if the empty callback is called, but at least we can |
| 572 | // check if the test does not crash. |
| 573 | } |
| 574 | |
[email protected] | 4c985c6 | 2011-12-13 17:08:42 | [diff] [blame] | 575 | TEST_F(EndToEndAsyncTest, TestSignal) { |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 576 | const char kMessage[] = "hello, world"; |
| 577 | // Send the test signal from the exported object. |
| 578 | test_service_->SendTestSignal(kMessage); |
[email protected] | 829f0e4c | 2011-08-31 18:02:43 | [diff] [blame] | 579 | // Receive the signal with the object proxy. The signal is handled in |
[email protected] | 3beaaa4e | 2011-08-23 07:29:21 | [diff] [blame] | 580 | // EndToEndAsyncTest::OnTestSignal() in the main thread. |
| 581 | WaitForTestSignal(); |
| 582 | ASSERT_EQ(kMessage, test_signal_string_); |
| 583 | } |
[email protected] | df159b2 | 2011-09-14 22:10:24 | [diff] [blame] | 584 | |
[email protected] | 4c985c6 | 2011-12-13 17:08:42 | [diff] [blame] | 585 | TEST_F(EndToEndAsyncTest, TestSignalFromRoot) { |
[email protected] | df159b2 | 2011-09-14 22:10:24 | [diff] [blame] | 586 | const char kMessage[] = "hello, world"; |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 587 | // Object proxies are tied to a particular object path, if a signal |
| 588 | // arrives from a different object path like "/" the first object proxy |
| 589 | // |object_proxy_| should not handle it, and should leave it for the root |
| 590 | // object proxy |root_object_proxy_|. |
[email protected] | df159b2 | 2011-09-14 22:10:24 | [diff] [blame] | 591 | test_service_->SendTestSignalFromRoot(kMessage); |
[email protected] | df159b2 | 2011-09-14 22:10:24 | [diff] [blame] | 592 | WaitForTestSignal(); |
[email protected] | 4d97f2337 | 2012-03-01 04:01:05 | [diff] [blame] | 593 | // Verify the signal was not received by the specific proxy. |
| 594 | ASSERT_TRUE(test_signal_string_.empty()); |
| 595 | // Verify the string WAS received by the root proxy. |
| 596 | ASSERT_EQ(kMessage, root_test_signal_string_); |
[email protected] | df159b2 | 2011-09-14 22:10:24 | [diff] [blame] | 597 | } |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 598 | |
[email protected] | 3635d32 | 2012-06-02 02:53:20 | [diff] [blame] | 599 | TEST_F(EndToEndAsyncTest, TestHugeSignal) { |
| 600 | const std::string kHugeMessage(kHugePayloadSize, 'o'); |
| 601 | |
| 602 | // Send the huge signal from the exported object. |
| 603 | test_service_->SendTestSignal(kHugeMessage); |
| 604 | // This caused a DCHECK failure before. Ensure that the issue is fixed. |
| 605 | WaitForTestSignal(); |
| 606 | ASSERT_EQ(kHugeMessage, test_signal_string_); |
| 607 | } |
| 608 | |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 609 | class SignalMultipleHandlerTest : public EndToEndAsyncTest { |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 610 | public: |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 611 | SignalMultipleHandlerTest() { |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 612 | } |
| 613 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 614 | void SetUp() override { |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 615 | // Set up base class. |
| 616 | EndToEndAsyncTest::SetUp(); |
| 617 | |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 618 | // Connect the root object proxy's signal handler to a new handler |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 619 | // so that we can verify that a second call to ConnectSignal() delivers |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 620 | // to both our new handler and the old. |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 621 | object_proxy_->ConnectToSignal( |
| 622 | "org.chromium.TestInterface", |
| 623 | "Test", |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 624 | base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal, |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 625 | base::Unretained(this)), |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 626 | base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected, |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 627 | base::Unretained(this))); |
| 628 | // Wait until the object proxy is connected to the signal. |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 629 | run_loop_.reset(new base::RunLoop); |
| 630 | run_loop_->Run(); |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | protected: |
| 634 | // Called when the "Test" signal is received, in the main thread. |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 635 | // Copy the string payload to |additional_test_signal_string_|. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 636 | void OnAdditionalTestSignal(Signal* signal) { |
| 637 | MessageReader reader(signal); |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 638 | ASSERT_TRUE(reader.PopString(&additional_test_signal_string_)); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 639 | run_loop_->Quit(); |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // Called when connected to the signal. |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 643 | void OnAdditionalConnected(const std::string& interface_name, |
| 644 | const std::string& signal_name, |
| 645 | bool success) { |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 646 | ASSERT_TRUE(success); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 647 | run_loop_->Quit(); |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 648 | } |
| 649 | |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 650 | // Text message from "Test" signal delivered to additional handler. |
| 651 | std::string additional_test_signal_string_; |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 652 | }; |
| 653 | |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 654 | TEST_F(SignalMultipleHandlerTest, TestMultipleHandlers) { |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 655 | const char kMessage[] = "hello, world"; |
| 656 | // Send the test signal from the exported object. |
| 657 | test_service_->SendTestSignal(kMessage); |
| 658 | // Receive the signal with the object proxy. |
| 659 | WaitForTestSignal(); |
[email protected] | 012e781dc | 2013-04-24 00:12:35 | [diff] [blame] | 660 | // Verify the string WAS received by the original handler. |
| 661 | ASSERT_EQ(kMessage, test_signal_string_); |
| 662 | // Verify the signal WAS ALSO received by the additional handler. |
| 663 | ASSERT_EQ(kMessage, additional_test_signal_string_); |
[email protected] | 3b6205c | 2012-03-21 23:39:17 | [diff] [blame] | 664 | } |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 665 | |
| 666 | } // namespace dbus |