OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/dbus/flimflam_client_unittest_base.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "dbus/message.h" | |
9 #include "dbus/object_path.h" | |
10 #include "dbus/values_util.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
14 | |
15 using ::testing::_; | |
16 using ::testing::Invoke; | |
17 using ::testing::Return; | |
18 | |
19 namespace chromeos { | |
20 | |
21 FlimflamClientUnittestBase::FlimflamClientUnittestBase( | |
22 const std::string& interface_name) : interface_name_(interface_name) { | |
23 } | |
24 | |
25 FlimflamClientUnittestBase::~FlimflamClientUnittestBase() { | |
26 } | |
27 | |
28 void FlimflamClientUnittestBase::SetUp() { | |
29 // Create a mock bus. | |
30 dbus::Bus::Options options; | |
31 options.bus_type = dbus::Bus::SYSTEM; | |
32 mock_bus_ = new dbus::MockBus(options); | |
33 | |
34 // Create a mock proxy. | |
35 mock_proxy_ = new dbus::MockObjectProxy( | |
36 mock_bus_.get(), | |
37 flimflam::kFlimflamServiceName, | |
38 dbus::ObjectPath(flimflam::kFlimflamServicePath)); | |
39 | |
40 // Set an expectation so mock_proxy's CallMethod() will use OnCallMethod() | |
41 // to return responses. | |
42 EXPECT_CALL(*mock_proxy_, CallMethod(_, _, _)) | |
43 .WillRepeatedly(Invoke(this, &FlimflamClientUnittestBase::OnCallMethod)); | |
44 | |
45 // Set an expectation so mock_proxy's ConnectToSignal() will use | |
46 // OnConnectToSignal() to run the callback. | |
47 EXPECT_CALL(*mock_proxy_, ConnectToSignal( | |
48 interface_name_, | |
49 flimflam::kMonitorPropertyChanged, _, _)) | |
50 .WillRepeatedly(Invoke(this, | |
51 &FlimflamClientUnittestBase::OnConnectToSignal)); | |
52 | |
53 // Set an expectation so mock_bus's GetObjectProxy() for the given | |
54 // service name and the object path will return mock_proxy_. | |
55 EXPECT_CALL(*mock_bus_, GetObjectProxy( | |
56 flimflam::kFlimflamServiceName, | |
57 dbus::ObjectPath(flimflam::kFlimflamServicePath))) | |
58 .WillOnce(Return(mock_proxy_.get())); | |
59 | |
60 // ShutdownAndBlock() will be called in TearDown(). | |
61 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); | |
62 } | |
63 | |
64 void FlimflamClientUnittestBase::TearDown() { | |
65 mock_bus_->ShutdownAndBlock(); | |
66 } | |
67 | |
68 void FlimflamClientUnittestBase::PrepareForMethodCall( | |
69 const std::string& method_name, | |
70 ArgumentCheckCallback argument_checker, | |
71 dbus::Response* response) { | |
72 expected_method_name_ = method_name; | |
73 argument_checker_ = argument_checker; | |
74 response_ = response; | |
75 } | |
76 | |
77 void FlimflamClientUnittestBase::SendPropertyChangedSignal( | |
78 dbus::Signal* signal) { | |
79 ASSERT_FALSE(property_changed_handler_.is_null()); | |
80 property_changed_handler_.Run(signal); | |
81 } | |
82 | |
83 // static | |
84 void FlimflamClientUnittestBase::ExpectPropertyChanged( | |
85 const std::string& expected_name, | |
86 const base::Value* expected_value, | |
87 const std::string& name, | |
88 const base::Value& value) { | |
89 EXPECT_EQ(expected_name, name); | |
90 EXPECT_TRUE(expected_value->Equals(&value)); | |
91 } | |
92 | |
93 // static | |
94 void FlimflamClientUnittestBase::ExpectNoArgument(dbus::MessageReader* reader) { | |
95 EXPECT_FALSE(reader->HasMoreData()); | |
96 } | |
97 | |
98 // static | |
99 void FlimflamClientUnittestBase::ExpectStringArgument( | |
100 const std::string& expected_string, | |
101 dbus::MessageReader* reader) { | |
102 std::string str; | |
103 ASSERT_TRUE(reader->PopString(&str)); | |
104 EXPECT_EQ(expected_string, str); | |
105 EXPECT_FALSE(reader->HasMoreData()); | |
106 } | |
107 | |
108 // static | |
109 void FlimflamClientUnittestBase::ExpectValueArgument( | |
110 const base::Value* expected_value, | |
111 dbus::MessageReader* reader) { | |
112 scoped_ptr<base::Value> value(dbus::PopDataAsValue(reader)); | |
113 ASSERT_TRUE(value.get()); | |
114 EXPECT_TRUE(value->Equals(expected_value)); | |
115 EXPECT_FALSE(reader->HasMoreData()); | |
116 } | |
117 | |
118 // static | |
119 void FlimflamClientUnittestBase::ExpectStringAndValueArguments( | |
120 const std::string& expected_string, | |
121 const base::Value* expected_value, | |
122 dbus::MessageReader* reader) { | |
123 std::string str; | |
124 ASSERT_TRUE(reader->PopString(&str)); | |
125 EXPECT_EQ(expected_string, str); | |
126 scoped_ptr<base::Value> value(dbus::PopDataAsValue(reader)); | |
127 ASSERT_TRUE(value.get()); | |
128 EXPECT_TRUE(value->Equals(expected_value)); | |
129 EXPECT_FALSE(reader->HasMoreData()); | |
130 } | |
131 | |
132 // static | |
133 void FlimflamClientUnittestBase::ExpectNoResultValue( | |
134 DBusMethodCallStatus call_status) { | |
135 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, call_status); | |
136 } | |
137 | |
138 // static | |
139 void FlimflamClientUnittestBase::ExpectDictionaryValueResult( | |
140 const base::DictionaryValue* expected_result, | |
141 DBusMethodCallStatus call_status, | |
142 const base::DictionaryValue& result) { | |
143 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, call_status); | |
144 EXPECT_TRUE(expected_result->Equals(&result)); | |
145 } | |
146 | |
147 void FlimflamClientUnittestBase::OnConnectToSignal( | |
148 const std::string& interface_name, | |
149 const std::string& signal_name, | |
150 dbus::ObjectProxy::SignalCallback signal_callback, | |
151 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { | |
152 property_changed_handler_ = signal_callback; | |
153 const bool success = true; | |
154 message_loop_.PostTask(FROM_HERE, | |
155 base::Bind(on_connected_callback, | |
156 interface_name, | |
157 signal_name, | |
158 success)); | |
159 } | |
160 | |
161 void FlimflamClientUnittestBase::OnCallMethod( | |
162 dbus::MethodCall* method_call, | |
163 int timeout_ms, | |
164 dbus::ObjectProxy::ResponseCallback response_callback) { | |
165 EXPECT_EQ(interface_name_, method_call->GetInterface()); | |
166 EXPECT_EQ(expected_method_name_, method_call->GetMember()); | |
167 dbus::MessageReader reader(method_call); | |
168 argument_checker_.Run(&reader); | |
169 message_loop_.PostTask(FROM_HERE, | |
170 base::Bind(response_callback, response_)); | |
171 } | |
172 | |
173 } // namespace chromeos | |
OLD | NEW |