OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <algorithm> | |
6 #include <string> | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/stl_util.h" | |
13 #include "base/threading/thread.h" | |
14 #include "base/threading/thread_restrictions.h" | |
15 #include "dbus/bus.h" | |
16 #include "dbus/message.h" | |
17 #include "dbus/object_proxy.h" | |
18 #include "dbus/test_service.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 // The end-to-end test exercises the asynchronos APIs in ObjectProxy and | |
22 // ExportedObject. | |
23 class EndToEndAsyncTest : public testing::Test { | |
24 public: | |
25 EndToEndAsyncTest() { | |
26 } | |
27 | |
28 void SetUp() { | |
29 // Make the main thread not to allow IO. | |
30 base::ThreadRestrictions::SetIOAllowed(false); | |
31 | |
32 // Start the test service; | |
33 test_service_.reset(new dbus::TestService); | |
34 test_service_->StartService(); | |
35 test_service_->WaitUntilServiceIsStarted(); | |
36 | |
37 // Start the D-Bus thread. | |
38 dbus_thread_.reset(new base::Thread("D-Bus Thread")); | |
39 base::Thread::Options thread_options; | |
40 thread_options.message_loop_type = MessageLoop::TYPE_IO; | |
41 dbus_thread_->StartWithOptions(thread_options); | |
42 | |
43 // Create the client. | |
44 dbus::Bus::Options bus_options; | |
stevenjb
2011/08/15 21:42:04
nit: Might be convenient to have a constructor for
satorux1
2011/08/16 22:25:37
I thought about that but I think having a construc
stevenjb
2011/08/16 22:53:29
Fair enough.
| |
45 bus_options.bus_type = dbus::Bus::SESSION; | |
46 bus_options.connection_type = dbus::Bus::PRIVATE; | |
47 bus_options.dbus_thread = dbus_thread_.get(); | |
48 bus_ = new dbus::Bus(bus_options); | |
49 object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService", | |
50 "/org/chromium/TestObject"); | |
51 } | |
52 | |
53 void TearDown() { | |
54 bus_->Shutdown(base::Bind(&EndToEndAsyncTest::OnShutdown, | |
55 base::Unretained(this))); | |
56 // Wait until the bus is shutdown. OnShutdown() will be called in | |
57 // mesage_loop_. | |
58 message_loop_.Run(); | |
59 | |
60 // Reset to the default. | |
61 base::ThreadRestrictions::SetIOAllowed(true); | |
62 | |
63 // Stopping a thread is considred an IO operation, so do this after | |
64 // allowing IO. | |
65 test_service_->Stop(); | |
66 } | |
67 | |
68 protected: | |
69 // Calls the method asynchronosly. OnResponse() will be called once the | |
70 // response is received. | |
71 void CallMethod(dbus::MethodCall* method_call, | |
72 int timeout_ms) { | |
73 object_proxy_->CallMethod(method_call, | |
74 timeout_ms, | |
75 base::Bind(&EndToEndAsyncTest::OnResponse, | |
76 base::Unretained(this))); | |
77 } | |
78 | |
79 // Wait for the give number of responses. | |
80 void WaitForResponses(size_t num_responses) { | |
81 while (response_strings_.size() < num_responses) { | |
stevenjb
2011/08/15 21:42:04
nit: add timeout here? Default test timeout is pre
satorux1
2011/08/16 22:25:37
In theory, we don't need timeout here, as the test
stevenjb
2011/08/16 22:53:29
OK, but if we ever see these tests timing out due
| |
82 message_loop_.Run(); | |
83 } | |
84 } | |
85 | |
86 // Called when the response is received. | |
87 void OnResponse(dbus::Response* response) { | |
88 // |response| will be deleted on exit of the function. Copy the | |
89 // payload to |response_strings_|. | |
90 if (response) { | |
91 dbus::MessageReader reader(response); | |
92 std::string response_string; | |
93 ASSERT_TRUE(reader.PopString(&response_string)); | |
94 response_strings_.push_back(response_string); | |
95 } else { | |
96 response_strings_.push_back(""); | |
97 } | |
98 message_loop_.Quit(); | |
99 }; | |
100 | |
101 // Called when the shutdown is complete. | |
102 void OnShutdown() { | |
103 message_loop_.Quit(); | |
104 } | |
105 | |
106 MessageLoop message_loop_; | |
107 std::vector<std::string> response_strings_; | |
108 scoped_ptr<base::Thread> dbus_thread_; | |
109 scoped_refptr<dbus::Bus> bus_; | |
110 dbus::ObjectProxy* object_proxy_; | |
111 scoped_ptr<dbus::TestService> test_service_; | |
112 }; | |
113 | |
114 TEST_F(EndToEndAsyncTest, Echo) { | |
115 const char* kHello = "hello"; | |
116 | |
117 // Create the method call. | |
118 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | |
119 dbus::MessageWriter writer(&method_call); | |
120 writer.AppendString(kHello); | |
121 | |
122 // Call the method. | |
123 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | |
124 CallMethod(&method_call, timeout_ms); | |
125 | |
126 // Check the response. | |
127 WaitForResponses(1); | |
128 EXPECT_EQ(kHello, response_strings_[0]); | |
129 } | |
130 | |
131 // Call Echo method three times. | |
132 TEST_F(EndToEndAsyncTest, EchoThreeTimes) { | |
133 const char* kMessages[] = { "foo", "bar", "baz" }; | |
134 | |
135 for (size_t i = 0; i < arraysize(kMessages); ++i) { | |
136 // Create the method call. | |
137 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | |
138 dbus::MessageWriter writer(&method_call); | |
139 writer.AppendString(kMessages[i]); | |
140 | |
141 // Call the method. | |
142 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | |
143 CallMethod(&method_call, timeout_ms); | |
144 } | |
145 | |
146 // Check the responses. | |
147 WaitForResponses(3); | |
148 // Sort as the order of the returned messages is not deterministic. | |
149 std::sort(response_strings_.begin(), response_strings_.end()); | |
150 EXPECT_EQ("bar", response_strings_[0]); | |
151 EXPECT_EQ("baz", response_strings_[1]); | |
152 EXPECT_EQ("foo", response_strings_[2]); | |
153 } | |
154 | |
155 TEST_F(EndToEndAsyncTest, Timeout) { | |
156 const char* kHello = "hello"; | |
157 | |
158 // Create the method call. | |
159 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho"); | |
160 dbus::MessageWriter writer(&method_call); | |
161 writer.AppendString(kHello); | |
162 | |
163 // Call the method with timeout smaller than TestService::kSlowEchoSleepMs. | |
164 const int timeout_ms = dbus::TestService::kSlowEchoSleepMs / 10; | |
165 CallMethod(&method_call, timeout_ms); | |
166 WaitForResponses(1); | |
167 | |
168 // Should fail because of timeout. | |
169 ASSERT_EQ("", response_strings_[0]); | |
170 } | |
171 | |
172 TEST_F(EndToEndAsyncTest, NonexistentMethod) { | |
173 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent"); | |
174 | |
175 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | |
176 CallMethod(&method_call, timeout_ms); | |
177 WaitForResponses(1); | |
178 | |
179 // Should fail because the method is nonexistent. | |
180 ASSERT_EQ("", response_strings_[0]); | |
181 } | |
182 | |
183 TEST_F(EndToEndAsyncTest, BrokenMethod) { | |
184 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod"); | |
185 | |
186 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; | |
187 CallMethod(&method_call, timeout_ms); | |
188 WaitForResponses(1); | |
189 | |
190 // Should fail because the method is broken. | |
191 ASSERT_EQ("", response_strings_[0]); | |
192 } | |
OLD | NEW |