OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
11 #include "dbus/mock_bus.h" | 11 #include "dbus/mock_bus.h" |
12 #include "dbus/mock_object_proxy.h" | 12 #include "dbus/mock_object_proxy.h" |
13 #include "dbus/mock_exported_object.h" | 13 #include "dbus/mock_exported_object.h" |
14 #include "dbus/object_path.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 using ::testing::_; | 17 using ::testing::_; |
19 using ::testing::Invoke; | 18 using ::testing::Invoke; |
20 using ::testing::Return; | 19 using ::testing::Return; |
21 using ::testing::Unused; | 20 using ::testing::Unused; |
22 | 21 |
23 class MockTest : public testing::Test { | 22 class MockTest : public testing::Test { |
24 public: | 23 public: |
25 MockTest() { | 24 MockTest() { |
26 } | 25 } |
27 | 26 |
28 virtual void SetUp() { | 27 virtual void SetUp() { |
29 // Create a mock bus. | 28 // Create a mock bus. |
30 dbus::Bus::Options options; | 29 dbus::Bus::Options options; |
31 options.bus_type = dbus::Bus::SYSTEM; | 30 options.bus_type = dbus::Bus::SYSTEM; |
32 mock_bus_ = new dbus::MockBus(options); | 31 mock_bus_ = new dbus::MockBus(options); |
33 | 32 |
34 // Create a mock proxy. | 33 // Create a mock proxy. |
35 mock_proxy_ = new dbus::MockObjectProxy( | 34 mock_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), |
36 mock_bus_.get(), | 35 "org.chromium.TestService", |
37 "org.chromium.TestService", | 36 "/org/chromium/TestObject"); |
38 dbus::ObjectPath("/org/chromium/TestObject")); | |
39 | 37 |
40 // Set an expectation so mock_proxy's CallMethodAndBlock() will use | 38 // Set an expectation so mock_proxy's CallMethodAndBlock() will use |
41 // CreateMockProxyResponse() to return responses. | 39 // CreateMockProxyResponse() to return responses. |
42 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _)) | 40 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _)) |
43 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); | 41 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); |
44 | 42 |
45 // Set an expectation so mock_proxy's CallMethod() will use | 43 // Set an expectation so mock_proxy's CallMethod() will use |
46 // HandleMockProxyResponseWithMessageLoop() to return responses. | 44 // HandleMockProxyResponseWithMessageLoop() to return responses. |
47 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) | 45 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) |
48 .WillRepeatedly( | 46 .WillRepeatedly( |
49 Invoke(this, | 47 Invoke(this, |
50 &MockTest::HandleMockProxyResponseWithMessageLoop)); | 48 &MockTest::HandleMockProxyResponseWithMessageLoop)); |
51 | 49 |
52 // Set an expectation so mock_bus's GetObjectProxy() for the given | 50 // Set an expectation so mock_bus's GetObjectProxy() for the given |
53 // service name and the object path will return mock_proxy_. | 51 // service name and the object path will return mock_proxy_. |
54 EXPECT_CALL(*mock_bus_, GetObjectProxy( | 52 EXPECT_CALL(*mock_bus_, GetObjectProxy("org.chromium.TestService", |
55 "org.chromium.TestService", | 53 "/org/chromium/TestObject")) |
56 dbus::ObjectPath("/org/chromium/TestObject"))) | |
57 .WillOnce(Return(mock_proxy_.get())); | 54 .WillOnce(Return(mock_proxy_.get())); |
58 | 55 |
59 // ShutdownAndBlock() will be called in TearDown(). | 56 // ShutdownAndBlock() will be called in TearDown(). |
60 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); | 57 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); |
61 } | 58 } |
62 | 59 |
63 virtual void TearDown() { | 60 virtual void TearDown() { |
64 mock_bus_->ShutdownAndBlock(); | 61 mock_bus_->ShutdownAndBlock(); |
65 } | 62 } |
66 | 63 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 123 } |
127 }; | 124 }; |
128 | 125 |
129 // This test demonstrates how to mock a synchronos method call using the | 126 // This test demonstrates how to mock a synchronos method call using the |
130 // mock classes. | 127 // mock classes. |
131 TEST_F(MockTest, CallMethodAndBlock) { | 128 TEST_F(MockTest, CallMethodAndBlock) { |
132 const char kHello[] = "Hello"; | 129 const char kHello[] = "Hello"; |
133 // Get an object proxy from the mock bus. | 130 // Get an object proxy from the mock bus. |
134 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 131 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
135 "org.chromium.TestService", | 132 "org.chromium.TestService", |
136 dbus::ObjectPath("/org/chromium/TestObject")); | 133 "/org/chromium/TestObject"); |
137 | 134 |
138 // Create a method call. | 135 // Create a method call. |
139 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 136 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); |
140 dbus::MessageWriter writer(&method_call); | 137 dbus::MessageWriter writer(&method_call); |
141 writer.AppendString(kHello); | 138 writer.AppendString(kHello); |
142 | 139 |
143 // Call the method. | 140 // Call the method. |
144 scoped_ptr<dbus::Response> response( | 141 scoped_ptr<dbus::Response> response( |
145 proxy->CallMethodAndBlock(&method_call, | 142 proxy->CallMethodAndBlock(&method_call, |
146 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 143 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
147 | 144 |
148 // Check the response. | 145 // Check the response. |
149 ASSERT_TRUE(response.get()); | 146 ASSERT_TRUE(response.get()); |
150 dbus::MessageReader reader(response.get()); | 147 dbus::MessageReader reader(response.get()); |
151 std::string text_message; | 148 std::string text_message; |
152 ASSERT_TRUE(reader.PopString(&text_message)); | 149 ASSERT_TRUE(reader.PopString(&text_message)); |
153 // The text message should be echo'ed back. | 150 // The text message should be echo'ed back. |
154 EXPECT_EQ(kHello, text_message); | 151 EXPECT_EQ(kHello, text_message); |
155 } | 152 } |
156 | 153 |
157 // This test demonstrates how to mock an asynchronos method call using the | 154 // This test demonstrates how to mock an asynchronos method call using the |
158 // mock classes. | 155 // mock classes. |
159 TEST_F(MockTest, CallMethod) { | 156 TEST_F(MockTest, CallMethod) { |
160 const char kHello[] = "hello"; | 157 const char kHello[] = "hello"; |
161 | 158 |
162 // Get an object proxy from the mock bus. | 159 // Get an object proxy from the mock bus. |
163 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 160 dbus::ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
164 "org.chromium.TestService", | 161 "org.chromium.TestService", |
165 dbus::ObjectPath("/org/chromium/TestObject")); | 162 "/org/chromium/TestObject"); |
166 | 163 |
167 // Create a method call. | 164 // Create a method call. |
168 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 165 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); |
169 dbus::MessageWriter writer(&method_call); | 166 dbus::MessageWriter writer(&method_call); |
170 writer.AppendString(kHello); | 167 writer.AppendString(kHello); |
171 | 168 |
172 // Call the method. | 169 // Call the method. |
173 proxy->CallMethod(&method_call, | 170 proxy->CallMethod(&method_call, |
174 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 171 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
175 base::Bind(&MockTest::OnResponse, | 172 base::Bind(&MockTest::OnResponse, |
176 base::Unretained(this))); | 173 base::Unretained(this))); |
177 // Run the message loop to let OnResponse be called. | 174 // Run the message loop to let OnResponse be called. |
178 message_loop_.Run(); | 175 message_loop_.Run(); |
179 | 176 |
180 EXPECT_EQ(kHello, response_string_); | 177 EXPECT_EQ(kHello, response_string_); |
181 } | 178 } |
OLD | NEW |