Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: dbus/end_to_end_async_unittest.cc

Issue 7491029: Implement Bus and ObjectProxy classes for our D-Bus library. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments updated Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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;
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 STLDeleteElements(&responses_);
55
56 bus_->Shutdown(base::Bind(&EndToEndAsyncTest::OnShutdown,
57 base::Unretained(this)));
58 // Wait until the bus is shutdown. OnShutdown() will be called in
59 // mesage_loop_.
60 message_loop_.Run();
61
62 // Reset to the default.
63 base::ThreadRestrictions::SetIOAllowed(true);
64
65 // Stopping a thread is considred an IO operation, so do this after
66 // allowing IO.
67 test_service_->Stop();
68 }
69
70 protected:
71 // Calls the method asynchronosly. OnResponse() will be called once the
72 // response is received.
73 void CallMethod(dbus::MethodCall* method_call,
74 int timeout_ms) {
75 object_proxy_->CallMethod(method_call,
76 timeout_ms,
77 base::Bind(&EndToEndAsyncTest::OnResponse,
78 base::Unretained(this)));
79 }
80
81 // Wait for the give number of responses.
82 void WaitForResponses(size_t num_responses) {
83 while (responses_.size() < num_responses) {
84 message_loop_.Run();
85 }
86 }
87
88 // Called when the response is received.
89 void OnResponse(dbus::Response* response) {
90 responses_.push_back(response);
91 message_loop_.Quit();
92 };
93
94 // Called when the shutdown is complete.
95 void OnShutdown() {
96 message_loop_.Quit();
97 }
98
99 MessageLoop message_loop_;
100 std::vector<dbus::Response*> responses_;
101 scoped_ptr<base::Thread> dbus_thread_;
102 scoped_refptr<dbus::Bus> bus_;
103 dbus::ObjectProxy* object_proxy_;
104 scoped_ptr<dbus::TestService> test_service_;
105 };
106
107 TEST_F(EndToEndAsyncTest, Echo) {
108 const char* kHello = "hello";
109
110 // Create the method call.
111 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
112 dbus::MessageWriter writer(&method_call);
113 writer.AppendString(kHello);
114
115 // Call the method.
116 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
117 CallMethod(&method_call, timeout_ms);
118
119 // Check the response.
120 WaitForResponses(1);
121 dbus::MessageReader reader(responses_[0]);
122 std::string returned_message;
123 ASSERT_TRUE(reader.PopString(&returned_message));
124 EXPECT_EQ(kHello, returned_message);
125 }
126
127 // Call Echo method three times.
128 TEST_F(EndToEndAsyncTest, EchoThreeTimes) {
129 const char* kMessages[] = { "foo", "bar", "baz" };
130
131 for (size_t i = 0; i < arraysize(kMessages); ++i) {
132 // Create the method call.
133 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
134 dbus::MessageWriter writer(&method_call);
135 writer.AppendString(kMessages[i]);
136
137 // Call the method.
138 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
139 CallMethod(&method_call, timeout_ms);
140 }
141
142 // Check the responses.
143 WaitForResponses(3);
144 std::vector<std::string> returned_messages;
145 for (size_t i = 0; i < arraysize(kMessages); ++i) {
146 dbus::MessageReader reader(responses_[i]);
147 std::string returned_message;
148 ASSERT_TRUE(reader.PopString(&returned_message));
149 returned_messages.push_back(returned_message);
150 }
151 // Sort as the order of the returned messages is not deterministic.
152 std::sort(returned_messages.begin(), returned_messages.end());
153 EXPECT_EQ("bar", returned_messages[0]);
154 EXPECT_EQ("baz", returned_messages[1]);
155 EXPECT_EQ("foo", returned_messages[2]);
156 }
157
158 TEST_F(EndToEndAsyncTest, Timeout) {
159 const char* kHello = "hello";
160
161 // Create the method call.
162 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
163 dbus::MessageWriter writer(&method_call);
164 writer.AppendString(kHello);
165
166 // Call the method with timeout smaller than TestService::kSlowEchoSleepMs.
167 const int timeout_ms = dbus::TestService::kSlowEchoSleepMs / 10;
168 CallMethod(&method_call, timeout_ms);
169 WaitForResponses(1);
170
171 // Should fail because of timeout.
172 ASSERT_TRUE(NULL == responses_[0]);
173 }
174
175 TEST_F(EndToEndAsyncTest, NonexistentMethod) {
176 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
177
178 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
179 CallMethod(&method_call, timeout_ms);
180 WaitForResponses(1);
181
182 // Should fail because the method is nonexistent.
183 ASSERT_TRUE(NULL == responses_[0]);
184 }
185
186 TEST_F(EndToEndAsyncTest, BrokenMethod) {
187 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
188
189 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
190 CallMethod(&method_call, timeout_ms);
191 WaitForResponses(1);
192
193 // Should fail because the method is broken.
194 ASSERT_TRUE(NULL == responses_[0]);
195 }
OLDNEW
« dbus/bus.cc ('K') | « dbus/dbus.gyp ('k') | dbus/end_to_end_sync_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698