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

Side by Side Diff: chromeos/dbus/ibus/ibus_engine_factory_service_unittest.cc

Issue 10836047: Revise IBus related DBus module. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Style fix Created 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chromeos/dbus/ibus/ibus_engine_factory_service.h" 5 #include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
6 6
7 #include <map>
8 #include <string>
7 #include "base/message_loop.h" 9 #include "base/message_loop.h"
8 #include "chromeos/dbus/ibus/ibus_constants.h" 10 #include "chromeos/dbus/ibus/ibus_constants.h"
9 #include "dbus/message.h" 11 #include "dbus/message.h"
10 #include "dbus/mock_bus.h" 12 #include "dbus/mock_bus.h"
11 #include "dbus/mock_exported_object.h" 13 #include "dbus/mock_exported_object.h"
12 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 using ::testing::Invoke; 17 using ::testing::Invoke;
16 using ::testing::Return; 18 using ::testing::Return;
17 using ::testing::StrEq; 19 using ::testing::StrEq;
18 using ::testing::_; 20 using ::testing::_;
19 21
20 namespace chromeos { 22 namespace chromeos {
21 23
22 class MockCreateEngineHandler { 24 class SynchronousCreateEngineHandler {
23 public: 25 public:
24 MOCK_METHOD1(Run, dbus::ObjectPath(const std::string& engine_name)); 26 explicit SynchronousCreateEngineHandler(const dbus::ObjectPath& path)
27 : path_(path) {}
28
29 void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
30 sender.Run(path_);
31 }
32
33 private:
34 dbus::ObjectPath path_;
35
36 DISALLOW_COPY_AND_ASSIGN(SynchronousCreateEngineHandler);
37 };
38
39 class AsynchronousCreateEngineHandler {
40 public:
41 AsynchronousCreateEngineHandler(const dbus::ObjectPath& path,
42 MessageLoop* message_loop)
43 : path_(path),
44 message_loop_(message_loop) {}
45
46 void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
47 message_loop_->PostTask(FROM_HERE, base::Bind(sender, path_));
48 }
49
50 private:
51 dbus::ObjectPath path_;
52 MessageLoop* message_loop_;
53
54 DISALLOW_COPY_AND_ASSIGN(AsynchronousCreateEngineHandler);
25 }; 55 };
26 56
27 class MockCreateEngineResponseSender { 57 class MockCreateEngineResponseSender {
28 public: 58 public:
29 MockCreateEngineResponseSender(const dbus::ObjectPath expected_path) 59 explicit MockCreateEngineResponseSender(const dbus::ObjectPath& expected_path)
30 : expected_path_(expected_path) {} 60 : expected_path_(expected_path) {}
31 MOCK_METHOD1(Run, void(dbus::Response*)); 61 MOCK_METHOD1(Run, void(dbus::Response*));
32 62
33 // Checks the given |response| meets expectation for the CreateEngine method. 63 // Checks the given |response| meets expectation for the CreateEngine method.
34 void CheckCreateEngineResponse(dbus::Response* response) { 64 void CheckCreateEngineResponse(dbus::Response* response) {
35 scoped_ptr<dbus::Response> response_deleter(response); 65 scoped_ptr<dbus::Response> response_deleter(response);
36 dbus::MessageReader reader(response); 66 dbus::MessageReader reader(response);
37 dbus::ObjectPath actual_path; 67 dbus::ObjectPath actual_path;
38 ASSERT_TRUE(reader.PopObjectPath(&actual_path)); 68 ASSERT_TRUE(reader.PopObjectPath(&actual_path));
39 EXPECT_EQ(expected_path_, actual_path); 69 EXPECT_EQ(expected_path_, actual_path);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 const dbus::ExportedObject::OnExportedCallback& on_exported_callback) { 131 const dbus::ExportedObject::OnExportedCallback& on_exported_callback) {
102 method_exported_map_[method_name] = method_callback; 132 method_exported_map_[method_name] = method_callback;
103 const bool success = true; 133 const bool success = true;
104 message_loop_.PostTask(FROM_HERE, base::Bind(on_exported_callback, 134 message_loop_.PostTask(FROM_HERE, base::Bind(on_exported_callback,
105 interface_name, 135 interface_name,
106 method_name, 136 method_name,
107 success)); 137 success));
108 } 138 }
109 }; 139 };
110 140
111 TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) { 141 TEST_F(IBusEngineFactoryServiceTest, SyncCreateEngineTest) {
112 // Set expectations. 142 // Set expectations.
113 const char kSampleEngine[] = "Sample Engine"; 143 const char kSampleEngine[] = "Sample Engine";
114 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10"); 144 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
115 MockCreateEngineResponseSender response_sender(kObjectPath); 145 MockCreateEngineResponseSender response_sender(kObjectPath);
116 EXPECT_CALL(response_sender, Run(_)) 146 EXPECT_CALL(response_sender, Run(_))
117 .WillOnce( 147 .WillOnce(
118 Invoke(&response_sender, 148 Invoke(&response_sender,
119 &MockCreateEngineResponseSender::CheckCreateEngineResponse)); 149 &MockCreateEngineResponseSender::CheckCreateEngineResponse));
120 150
151 SynchronousCreateEngineHandler handler(kObjectPath);
121 // Set handler expectations. 152 // Set handler expectations.
122 MockCreateEngineHandler handler; 153 service_->SetCreateEngineHandler(
123 EXPECT_CALL(handler, Run(StrEq(kSampleEngine))) 154 kSampleEngine,
124 .WillOnce(Return(kObjectPath)); 155 base::Bind(&SynchronousCreateEngineHandler::Run,
125 service_->SetCreateEngineHandler(base::Bind(&MockCreateEngineHandler::Run, 156 base::Unretained(&handler)));
126 base::Unretained(&handler)));
127 message_loop_.RunAllPending(); 157 message_loop_.RunAllPending();
128 158
129 // Invoke method call. 159 // Invoke method call.
130 dbus::MethodCall method_call( 160 dbus::MethodCall method_call(
131 ibus::engine_factory::kServiceInterface, 161 ibus::engine_factory::kServiceInterface,
132 ibus::engine_factory::kCreateEngineMethod); 162 ibus::engine_factory::kCreateEngineMethod);
133 method_call.SetSerial(10); 163 method_call.SetSerial(10);
134 dbus::MessageWriter writer(&method_call); 164 dbus::MessageWriter writer(&method_call);
135 writer.AppendString(kSampleEngine); 165 writer.AppendString(kSampleEngine);
136 ASSERT_FALSE( 166 ASSERT_FALSE(
137 method_exported_map_[ibus::engine_factory::kCreateEngineMethod] 167 method_exported_map_[ibus::engine_factory::kCreateEngineMethod]
138 .is_null()); 168 .is_null());
139 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run( 169 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
140 &method_call, 170 &method_call,
141 base::Bind(&MockCreateEngineResponseSender::Run, 171 base::Bind(&MockCreateEngineResponseSender::Run,
142 base::Unretained(&response_sender))); 172 base::Unretained(&response_sender)));
143 173
144 // Unset the handler so expect not calling handler. 174 // Unset the handler so expect not calling handler.
145 service_->UnsetCreateEngineHandler(); 175 service_->UnsetCreateEngineHandler(kSampleEngine);
146 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run( 176 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
147 &method_call, 177 &method_call,
148 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse, 178 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
149 base::Unretained(&response_sender))); 179 base::Unretained(&response_sender)));
150
151 message_loop_.RunAllPending(); 180 message_loop_.RunAllPending();
152 } 181 }
153 182
183 TEST_F(IBusEngineFactoryServiceTest, AsyncCreateEngineTest) {
184 // Set expectations.
185 const char kSampleEngine[] = "Sample Engine";
186 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
187 MockCreateEngineResponseSender response_sender(kObjectPath);
188 EXPECT_CALL(response_sender, Run(_))
189 .WillOnce(
190 Invoke(&response_sender,
191 &MockCreateEngineResponseSender::CheckCreateEngineResponse));
192
193 AsynchronousCreateEngineHandler handler(kObjectPath, &message_loop_);
194 // Set handler expectations.
195 service_->SetCreateEngineHandler(
196 kSampleEngine,
197 base::Bind(&AsynchronousCreateEngineHandler::Run,
198 base::Unretained(&handler)));
199 message_loop_.RunAllPending();
200
201 // Invoke method call.
202 dbus::MethodCall method_call(
203 ibus::engine_factory::kServiceInterface,
204 ibus::engine_factory::kCreateEngineMethod);
205 method_call.SetSerial(10);
206 dbus::MessageWriter writer(&method_call);
207 writer.AppendString(kSampleEngine);
208 ASSERT_FALSE(
209 method_exported_map_[ibus::engine_factory::kCreateEngineMethod]
210 .is_null());
211 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
212 &method_call,
213 base::Bind(&MockCreateEngineResponseSender::Run,
214 base::Unretained(&response_sender)));
215
216 // Unset the handler so expect not calling handler.
217 service_->UnsetCreateEngineHandler(kSampleEngine);
218 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
219 &method_call,
220 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
221 base::Unretained(&response_sender)));
222 message_loop_.RunAllPending();
223 }
224
154 } // namespace chromeos 225 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/ibus/ibus_engine_factory_service.cc ('k') | chromeos/dbus/ibus/mock_ibus_engine_factory_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698