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

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: Fix style nits 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) {}
satorux1 2012/08/01 19:52:44 put a blank line here.
Seigo Nonaka 2012/08/02 04:11:23 Done.
28 void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
29 sender.Run(path_);
30 }
31
32 private:
33 dbus::ObjectPath path_;
34
35 DISALLOW_COPY_AND_ASSIGN(SynchronousCreateEngineHandler);
36 };
37
38 class AsynchronousCreateEngineHandler {
39 public:
40 AsynchronousCreateEngineHandler(const dbus::ObjectPath& path,
41 MessageLoop* message_loop)
42 : path_(path),
43 message_loop_(message_loop) {}
satorux1 2012/08/01 19:52:44 blank line.
Seigo Nonaka 2012/08/02 04:11:23 Done.
44 void Run(const IBusEngineFactoryService::CreateEngineResponseSender& sender) {
45 message_loop_->PostTask(FROM_HERE, base::Bind(sender, path_));
46 }
47
48 private:
49 dbus::ObjectPath path_;
50 MessageLoop* message_loop_;
51
52 DISALLOW_COPY_AND_ASSIGN(AsynchronousCreateEngineHandler);
25 }; 53 };
26 54
27 class MockCreateEngineResponseSender { 55 class MockCreateEngineResponseSender {
28 public: 56 public:
29 MockCreateEngineResponseSender(const dbus::ObjectPath expected_path) 57 explicit MockCreateEngineResponseSender(const dbus::ObjectPath& expected_path)
30 : expected_path_(expected_path) {} 58 : expected_path_(expected_path) {}
31 MOCK_METHOD1(Run, void(dbus::Response*)); 59 MOCK_METHOD1(Run, void(dbus::Response*));
32 60
33 // Checks the given |response| meets expectation for the CreateEngine method. 61 // Checks the given |response| meets expectation for the CreateEngine method.
34 void CheckCreateEngineResponse(dbus::Response* response) { 62 void CheckCreateEngineResponse(dbus::Response* response) {
35 scoped_ptr<dbus::Response> response_deleter(response); 63 scoped_ptr<dbus::Response> response_deleter(response);
36 dbus::MessageReader reader(response); 64 dbus::MessageReader reader(response);
37 dbus::ObjectPath actual_path; 65 dbus::ObjectPath actual_path;
38 ASSERT_TRUE(reader.PopObjectPath(&actual_path)); 66 ASSERT_TRUE(reader.PopObjectPath(&actual_path));
39 EXPECT_EQ(expected_path_, actual_path); 67 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) { 129 const dbus::ExportedObject::OnExportedCallback& on_exported_callback) {
102 method_exported_map_[method_name] = method_callback; 130 method_exported_map_[method_name] = method_callback;
103 const bool success = true; 131 const bool success = true;
104 message_loop_.PostTask(FROM_HERE, base::Bind(on_exported_callback, 132 message_loop_.PostTask(FROM_HERE, base::Bind(on_exported_callback,
105 interface_name, 133 interface_name,
106 method_name, 134 method_name,
107 success)); 135 success));
108 } 136 }
109 }; 137 };
110 138
111 TEST_F(IBusEngineFactoryServiceTest, CreateEngineTest) { 139 TEST_F(IBusEngineFactoryServiceTest, SyncCreateEngineTest) {
112 // Set expectations. 140 // Set expectations.
113 const char kSampleEngine[] = "Sample Engine"; 141 const char kSampleEngine[] = "Sample Engine";
114 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10"); 142 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
115 MockCreateEngineResponseSender response_sender(kObjectPath); 143 MockCreateEngineResponseSender response_sender(kObjectPath);
116 EXPECT_CALL(response_sender, Run(_)) 144 EXPECT_CALL(response_sender, Run(_))
117 .WillOnce( 145 .WillOnce(
118 Invoke(&response_sender, 146 Invoke(&response_sender,
119 &MockCreateEngineResponseSender::CheckCreateEngineResponse)); 147 &MockCreateEngineResponseSender::CheckCreateEngineResponse));
120 148
149 SynchronousCreateEngineHandler handler(kObjectPath);
121 // Set handler expectations. 150 // Set handler expectations.
122 MockCreateEngineHandler handler; 151 service_->SetCreateEngineHandler(
123 EXPECT_CALL(handler, Run(StrEq(kSampleEngine))) 152 kSampleEngine,
124 .WillOnce(Return(kObjectPath)); 153 base::Bind(&SynchronousCreateEngineHandler::Run,
125 service_->SetCreateEngineHandler(base::Bind(&MockCreateEngineHandler::Run, 154 base::Unretained(&handler)));
126 base::Unretained(&handler)));
127 message_loop_.RunAllPending(); 155 message_loop_.RunAllPending();
128 156
129 // Invoke method call. 157 // Invoke method call.
130 dbus::MethodCall method_call( 158 dbus::MethodCall method_call(
131 ibus::engine_factory::kServiceInterface, 159 ibus::engine_factory::kServiceInterface,
132 ibus::engine_factory::kCreateEngineMethod); 160 ibus::engine_factory::kCreateEngineMethod);
133 method_call.SetSerial(10); 161 method_call.SetSerial(10);
134 dbus::MessageWriter writer(&method_call); 162 dbus::MessageWriter writer(&method_call);
135 writer.AppendString(kSampleEngine); 163 writer.AppendString(kSampleEngine);
136 ASSERT_FALSE( 164 ASSERT_FALSE(
137 method_exported_map_[ibus::engine_factory::kCreateEngineMethod] 165 method_exported_map_[ibus::engine_factory::kCreateEngineMethod]
138 .is_null()); 166 .is_null());
139 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run( 167 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
140 &method_call, 168 &method_call,
141 base::Bind(&MockCreateEngineResponseSender::Run, 169 base::Bind(&MockCreateEngineResponseSender::Run,
142 base::Unretained(&response_sender))); 170 base::Unretained(&response_sender)));
143 171
144 // Unset the handler so expect not calling handler. 172 // Unset the handler so expect not calling handler.
145 service_->UnsetCreateEngineHandler(); 173 service_->UnsetCreateEngineHandler(kSampleEngine);
146 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run( 174 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
147 &method_call, 175 &method_call,
148 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse, 176 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
149 base::Unretained(&response_sender))); 177 base::Unretained(&response_sender)));
150
151 message_loop_.RunAllPending(); 178 message_loop_.RunAllPending();
152 } 179 }
153 180
181 TEST_F(IBusEngineFactoryServiceTest, AsyncCreateEngineTest) {
182 // Set expectations.
183 const char kSampleEngine[] = "Sample Engine";
184 const dbus::ObjectPath kObjectPath("/org/freedesktop/IBus/Engine/10");
185 MockCreateEngineResponseSender response_sender(kObjectPath);
186 EXPECT_CALL(response_sender, Run(_))
187 .WillOnce(
188 Invoke(&response_sender,
189 &MockCreateEngineResponseSender::CheckCreateEngineResponse));
190
191 AsynchronousCreateEngineHandler handler(kObjectPath, &message_loop_);
192 // Set handler expectations.
193 service_->SetCreateEngineHandler(
194 kSampleEngine,
195 base::Bind(&AsynchronousCreateEngineHandler::Run,
196 base::Unretained(&handler)));
197 message_loop_.RunAllPending();
198
199 // Invoke method call.
200 dbus::MethodCall method_call(
201 ibus::engine_factory::kServiceInterface,
202 ibus::engine_factory::kCreateEngineMethod);
203 method_call.SetSerial(10);
204 dbus::MessageWriter writer(&method_call);
205 writer.AppendString(kSampleEngine);
206 ASSERT_FALSE(
207 method_exported_map_[ibus::engine_factory::kCreateEngineMethod]
208 .is_null());
209 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
210 &method_call,
211 base::Bind(&MockCreateEngineResponseSender::Run,
212 base::Unretained(&response_sender)));
213
214 // Unset the handler so expect not calling handler.
215 service_->UnsetCreateEngineHandler(kSampleEngine);
216 method_exported_map_[ibus::engine_factory::kCreateEngineMethod].Run(
217 &method_call,
218 base::Bind(&MockCreateEngineResponseSender::CheckCreateEngineResponse,
219 base::Unretained(&response_sender)));
220 message_loop_.RunAllPending();
221 }
222
154 } // namespace chromeos 223 } // 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