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