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