OLD | NEW |
1 // Copyright (c) 2011 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 "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using ::testing::_; | 17 using ::testing::_; |
18 using ::testing::Invoke; | 18 using ::testing::Invoke; |
19 using ::testing::Return; | 19 using ::testing::Return; |
20 using ::testing::Unused; | 20 using ::testing::Unused; |
21 | 21 |
22 class MockTest : public testing::Test { | 22 class MockTest : public testing::Test { |
23 public: | 23 public: |
24 MockTest() { | 24 MockTest() { |
25 } | 25 } |
26 | 26 |
27 void SetUp() { | 27 virtual void SetUp() { |
28 // Create a mock bus. | 28 // Create a mock bus. |
29 dbus::Bus::Options options; | 29 dbus::Bus::Options options; |
30 options.bus_type = dbus::Bus::SYSTEM; | 30 options.bus_type = dbus::Bus::SYSTEM; |
31 mock_bus_ = new dbus::MockBus(options); | 31 mock_bus_ = new dbus::MockBus(options); |
32 | 32 |
33 // Create a mock proxy. | 33 // Create a mock proxy. |
34 mock_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), | 34 mock_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), |
35 "org.chromium.TestService", | 35 "org.chromium.TestService", |
36 "/org/chromium/TestObject"); | 36 "/org/chromium/TestObject"); |
37 | 37 |
38 // Set an expectation so mock_proxy's CallMethodAndBlock() will use | 38 // Set an expectation so mock_proxy's CallMethodAndBlock() will use |
39 // CreateMockProxyResponse() to return responses. | 39 // CreateMockProxyResponse() to return responses. |
40 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _)) | 40 EXPECT_CALL(*mock_proxy_, CallMethodAndBlock(_, _)) |
41 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); | 41 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); |
42 | 42 |
43 // Set an expectation so mock_proxy's CallMethod() will use | 43 // Set an expectation so mock_proxy's CallMethod() will use |
44 // HandleMockProxyResponseWithMessageLoop() to return responses. | 44 // HandleMockProxyResponseWithMessageLoop() to return responses. |
45 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) | 45 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) |
46 .WillRepeatedly( | 46 .WillRepeatedly( |
47 Invoke(this, | 47 Invoke(this, |
48 &MockTest::HandleMockProxyResponseWithMessageLoop)); | 48 &MockTest::HandleMockProxyResponseWithMessageLoop)); |
49 | 49 |
50 // Set an expectation so mock_bus's GetObjectProxy() for the given | 50 // Set an expectation so mock_bus's GetObjectProxy() for the given |
51 // service name and the object path will return mock_proxy_. | 51 // service name and the object path will return mock_proxy_. |
52 EXPECT_CALL(*mock_bus_, GetObjectProxy("org.chromium.TestService", | 52 EXPECT_CALL(*mock_bus_, GetObjectProxy("org.chromium.TestService", |
53 "/org/chromium/TestObject")) | 53 "/org/chromium/TestObject")) |
54 .WillOnce(Return(mock_proxy_.get())); | 54 .WillOnce(Return(mock_proxy_.get())); |
| 55 |
| 56 // ShutdownAndBlock() will be called in TearDown(). |
| 57 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); |
| 58 } |
| 59 |
| 60 virtual void TearDown() { |
| 61 mock_bus_->ShutdownAndBlock(); |
55 } | 62 } |
56 | 63 |
57 // Called when the response is received. | 64 // Called when the response is received. |
58 void OnResponse(dbus::Response* response) { | 65 void OnResponse(dbus::Response* response) { |
59 // |response| will be deleted on exit of the function. Copy the | 66 // |response| will be deleted on exit of the function. Copy the |
60 // payload to |response_string_|. | 67 // payload to |response_string_|. |
61 if (response) { | 68 if (response) { |
62 dbus::MessageReader reader(response); | 69 dbus::MessageReader reader(response); |
63 ASSERT_TRUE(reader.PopString(&response_string_)); | 70 ASSERT_TRUE(reader.PopString(&response_string_)); |
64 } | 71 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 // Call the method. | 169 // Call the method. |
163 proxy->CallMethod(&method_call, | 170 proxy->CallMethod(&method_call, |
164 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 171 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
165 base::Bind(&MockTest::OnResponse, | 172 base::Bind(&MockTest::OnResponse, |
166 base::Unretained(this))); | 173 base::Unretained(this))); |
167 // Run the message loop to let OnResponse be called. | 174 // Run the message loop to let OnResponse be called. |
168 message_loop_.Run(); | 175 message_loop_.Run(); |
169 | 176 |
170 EXPECT_EQ(kHello, response_string_); | 177 EXPECT_EQ(kHello, response_string_); |
171 } | 178 } |
OLD | NEW |