Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / chromeos / dbus / ibus / ibus_engine_service_unittest.cc
blob0df3ffcce960a5f44f3108b7f73cd395c7d1b109
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.
5 #include "chromeos/dbus/ibus/ibus_engine_service.h"
7 #include <map>
8 #include "base/bind.h"
9 #include "base/message_loop.h"
10 #include "base/values.h"
11 #include "chromeos/dbus/ibus/ibus_constants.h"
12 #include "chromeos/dbus/ibus/ibus_lookup_table.h"
13 #include "chromeos/dbus/ibus/ibus_property.h"
14 #include "chromeos/dbus/ibus/ibus_text.h"
15 #include "dbus/message.h"
16 #include "dbus/mock_bus.h"
17 #include "dbus/mock_exported_object.h"
18 #include "dbus/object_path.h"
19 #include "dbus/values_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using testing::Invoke;
24 using testing::Return;
25 using testing::_;
27 namespace chromeos {
29 namespace {
30 const std::string kObjectPath = "/org/freedesktop/IBus/Engine/1";
32 class MockIBusEngineHandler : public IBusEngineHandlerInterface {
33 public:
34 MOCK_METHOD0(FocusIn, void());
35 MOCK_METHOD0(FocusOut, void());
36 MOCK_METHOD0(Enable, void());
37 MOCK_METHOD0(Disable, void());
38 MOCK_METHOD2(PropertyActivate, void(const std::string& property_name,
39 ibus::IBusPropertyState property_state));
40 MOCK_METHOD1(PropertyShow, void(const std::string& property_name));
41 MOCK_METHOD1(PropertyHide, void(const std::string& property_name));
42 MOCK_METHOD1(SetCapability, void(IBusCapability capability));
43 MOCK_METHOD0(Reset, void());
44 MOCK_METHOD4(ProcessKeyEvent, void(
45 uint32 keysym,
46 uint32 keycode,
47 uint32 state,
48 const KeyEventDoneCallback& callback));
49 MOCK_METHOD3(CandidateClicked, void(uint32 index,
50 ibus::IBusMouseButton button,
51 uint32 state));
52 MOCK_METHOD3(SetSurroundingText, void(const std::string& text,
53 uint32 cursor_pos,
54 uint32 anchor_pos));
57 class MockResponseSender {
58 public:
59 // GMock doesn't support mocking methods which take scoped_ptr<>.
60 MOCK_METHOD1(MockRun, void(dbus::Response* reponse));
61 void Run(scoped_ptr<dbus::Response> response) {
62 MockRun(response.get());
66 // Used for method call empty response evaluation.
67 class EmptyResponseExpectation {
68 public:
69 explicit EmptyResponseExpectation(const uint32 serial_no)
70 : serial_no_(serial_no) {}
72 // Evaluates the given |response| has no argument.
73 void Evaluate(dbus::Response* response) {
74 EXPECT_EQ(serial_no_, response->GetReplySerial());
75 dbus::MessageReader reader(response);
76 EXPECT_FALSE(reader.HasMoreData());
79 private:
80 const uint32 serial_no_;
82 DISALLOW_COPY_AND_ASSIGN(EmptyResponseExpectation);
85 // Used for method call a boolean response evaluation.
86 class BoolResponseExpectation {
87 public:
88 explicit BoolResponseExpectation(uint32 serial_no, bool result)
89 : serial_no_(serial_no),
90 result_(result) {}
92 // Evaluates the given |response| has only one boolean and which is equals to
93 // |result_| which is given in ctor.
94 void Evaluate(dbus::Response* response) {
95 EXPECT_EQ(serial_no_, response->GetReplySerial());
96 dbus::MessageReader reader(response);
97 bool result = false;
98 EXPECT_TRUE(reader.PopBool(&result));
99 EXPECT_EQ(result_, result);
100 EXPECT_FALSE(reader.HasMoreData());
103 private:
104 uint32 serial_no_;
105 bool result_;
107 DISALLOW_COPY_AND_ASSIGN(BoolResponseExpectation);
110 // Used for RegisterProperties signal message evaluation.
111 class RegisterPropertiesExpectation {
112 public:
113 explicit RegisterPropertiesExpectation(
114 const IBusPropertyList& property_list)
115 : property_list_(property_list) {}
117 // Evaluates the given |signal| is a valid message.
118 void Evaluate(dbus::Signal* signal) {
119 IBusPropertyList property_list;
121 // Read a signal argument.
122 dbus::MessageReader reader(signal);
123 EXPECT_TRUE(PopIBusPropertyList(&reader, &property_list));
124 EXPECT_FALSE(reader.HasMoreData());
126 // Check an argument.
127 EXPECT_EQ(property_list_.size(), property_list.size());
128 for (size_t i = 0; i < property_list_.size(); ++i) {
129 EXPECT_EQ(property_list_[i]->key(), property_list[i]->key());
130 EXPECT_EQ(property_list_[i]->type(), property_list[i]->type());
131 EXPECT_EQ(property_list_[i]->label(), property_list[i]->label());
132 EXPECT_EQ(property_list_[i]->tooltip(), property_list[i]->tooltip());
133 EXPECT_EQ(property_list_[i]->visible(), property_list[i]->visible());
134 EXPECT_EQ(property_list_[i]->checked(), property_list[i]->checked());
138 private:
139 const IBusPropertyList& property_list_;
141 DISALLOW_COPY_AND_ASSIGN(RegisterPropertiesExpectation);
144 // Used for mocking ProcessKeyEventHandler.
145 class ProcessKeyEventHandler {
146 public:
147 explicit ProcessKeyEventHandler(bool expected_value)
148 : expected_value_(expected_value) {
151 void ProcessKeyEvent(
152 uint32 keysym,
153 uint32 keycode,
154 uint32 state,
155 const IBusEngineHandlerInterface::KeyEventDoneCallback& callback) {
156 callback.Run(expected_value_);
159 private:
160 bool expected_value_;
162 DISALLOW_COPY_AND_ASSIGN(ProcessKeyEventHandler);
165 // Used for mocking asynchronous ProcessKeyEventHandler.
166 class DelayProcessKeyEventHandler {
167 public:
168 DelayProcessKeyEventHandler(bool expected_value,
169 MessageLoop* message_loop)
170 : expected_value_(expected_value),
171 message_loop_(message_loop) {
174 void ProcessKeyEvent(
175 uint32 keysym,
176 uint32 keycode,
177 uint32 state,
178 const IBusEngineHandlerInterface::KeyEventDoneCallback& callback) {
179 message_loop_->PostTask(FROM_HERE, base::Bind(callback, expected_value_));
182 private:
183 bool expected_value_;
184 MessageLoop* message_loop_;
186 DISALLOW_COPY_AND_ASSIGN(DelayProcessKeyEventHandler);
189 // Used for UpdatePreedit signal message evaluation.
190 class UpdatePreeditExpectation {
191 public:
192 UpdatePreeditExpectation(
193 const IBusText& ibus_text,
194 uint32 cursor_pos,
195 bool is_visible,
196 IBusEngineService::IBusEnginePreeditFocusOutMode mode)
197 : ibus_text_(ibus_text),
198 cursor_pos_(cursor_pos),
199 is_visible_(is_visible),
200 mode_(mode) {}
202 // Evaluates the given |signal| is a valid message.
203 void Evaluate(dbus::Signal* signal) {
204 IBusText ibus_text;
205 uint32 cursor_pos = 0;
206 bool is_visible = false;
207 uint32 preedit_mode = 0;
209 // Read signal arguments.
210 dbus::MessageReader reader(signal);
211 EXPECT_TRUE(PopIBusText(&reader, &ibus_text));
212 EXPECT_TRUE(reader.PopUint32(&cursor_pos));
213 EXPECT_TRUE(reader.PopBool(&is_visible));
214 EXPECT_TRUE(reader.PopUint32(&preedit_mode));
215 EXPECT_FALSE(reader.HasMoreData());
217 // Check arguments.
218 EXPECT_EQ(ibus_text_.text(), ibus_text.text());
219 EXPECT_EQ(cursor_pos_, cursor_pos);
220 EXPECT_EQ(is_visible_, is_visible);
221 EXPECT_EQ(mode_,
222 static_cast<IBusEngineService::IBusEnginePreeditFocusOutMode>(
223 preedit_mode));
226 private:
227 const IBusText& ibus_text_;
228 uint32 cursor_pos_;
229 bool is_visible_;
230 IBusEngineService::IBusEnginePreeditFocusOutMode mode_;
232 DISALLOW_COPY_AND_ASSIGN(UpdatePreeditExpectation);
235 // Used for UpdateAuxiliaryText signal message evaluation.
236 class UpdateAuxiliaryTextExpectation {
237 public:
238 UpdateAuxiliaryTextExpectation(const IBusText& ibus_text,
239 bool is_visible)
240 : ibus_text_(ibus_text), is_visible_(is_visible) {}
242 // Evaluates the given |signal| is a valid message.
243 void Evaluate(dbus::Signal* signal) {
244 IBusText ibus_text;
245 bool is_visible = false;
247 // Read signal arguments.
248 dbus::MessageReader reader(signal);
249 EXPECT_TRUE(PopIBusText(&reader, &ibus_text));
250 EXPECT_TRUE(reader.PopBool(&is_visible));
251 EXPECT_FALSE(reader.HasMoreData());
253 // Check arguments.
254 EXPECT_EQ(ibus_text_.text(), ibus_text.text());
255 EXPECT_EQ(is_visible_, is_visible);
258 private:
259 const IBusText& ibus_text_;
260 bool is_visible_;
262 DISALLOW_COPY_AND_ASSIGN(UpdateAuxiliaryTextExpectation);
265 // Used for UpdateLookupTable signal message evaluation.
266 class UpdateLookupTableExpectation {
267 public:
268 UpdateLookupTableExpectation(const IBusLookupTable& lookup_table,
269 bool is_visible)
270 : lookup_table_(lookup_table), is_visible_(is_visible) {}
272 // Evaluates the given |signal| is a valid message.
273 void Evaluate(dbus::Signal* signal) {
274 IBusLookupTable lookup_table;
275 bool is_visible = false;
277 // Read signal arguments.
278 dbus::MessageReader reader(signal);
279 EXPECT_TRUE(PopIBusLookupTable(&reader, &lookup_table));
280 EXPECT_TRUE(reader.PopBool(&is_visible));
281 EXPECT_FALSE(reader.HasMoreData());
283 // Check arguments.
284 EXPECT_EQ(lookup_table_.page_size(), lookup_table.page_size());
285 EXPECT_EQ(lookup_table_.cursor_position(), lookup_table.cursor_position());
286 EXPECT_EQ(lookup_table_.is_cursor_visible(),
287 lookup_table.is_cursor_visible());
288 EXPECT_EQ(is_visible_, is_visible);
291 private:
292 const IBusLookupTable& lookup_table_;
293 bool is_visible_;
295 DISALLOW_COPY_AND_ASSIGN(UpdateLookupTableExpectation);
298 // Used for UpdateProperty signal message evaluation.
299 class UpdatePropertyExpectation {
300 public:
301 explicit UpdatePropertyExpectation(const IBusProperty& property)
302 : property_(property) {}
304 // Evaluates the given |signal| is a valid message.
305 void Evaluate(dbus::Signal* signal) {
306 IBusProperty property;
308 // Read a signal argument.
309 dbus::MessageReader reader(signal);
310 EXPECT_TRUE(PopIBusProperty(&reader, &property));
311 EXPECT_FALSE(reader.HasMoreData());
313 // Check the argument.
314 EXPECT_EQ(property_.key(), property.key());
315 EXPECT_EQ(property_.type(), property.type());
316 EXPECT_EQ(property_.label(), property.label());
317 EXPECT_EQ(property_.tooltip(), property.tooltip());
318 EXPECT_EQ(property_.visible(), property.visible());
319 EXPECT_EQ(property_.checked(), property.checked());
322 private:
323 const IBusProperty& property_;
325 DISALLOW_COPY_AND_ASSIGN(UpdatePropertyExpectation);
328 // Used for ForwardKeyEvent signal message evaluation.
329 class ForwardKeyEventExpectation {
330 public:
331 ForwardKeyEventExpectation(uint32 keyval, uint32 keycode, uint32 state)
332 : keyval_(keyval),
333 keycode_(keycode),
334 state_(state) {}
336 // Evaluates the given |signal| is a valid message.
337 void Evaluate(dbus::Signal* signal) {
338 uint32 keyval = 0;
339 uint32 keycode = 0;
340 uint32 state = 0;
342 // Read signal arguments.
343 dbus::MessageReader reader(signal);
344 EXPECT_TRUE(reader.PopUint32(&keyval));
345 EXPECT_TRUE(reader.PopUint32(&keycode));
346 EXPECT_TRUE(reader.PopUint32(&state));
347 EXPECT_FALSE(reader.HasMoreData());
349 // Check arguments.
350 EXPECT_EQ(keyval_, keyval);
351 EXPECT_EQ(keycode_, keycode);
352 EXPECT_EQ(state_, state);
355 private:
356 uint32 keyval_;
357 uint32 keycode_;
358 uint32 state_;
360 DISALLOW_COPY_AND_ASSIGN(ForwardKeyEventExpectation);
363 // Used for RequireSurroundingText signal message evaluation.
364 class RequireSurroundingTextExpectation {
365 public:
366 RequireSurroundingTextExpectation() {}
368 // Evaluates the given |signal| is a valid message.
369 void Evaluate(dbus::Signal* signal) {
370 dbus::MessageReader reader(signal);
371 EXPECT_FALSE(reader.HasMoreData());
374 private:
375 DISALLOW_COPY_AND_ASSIGN(RequireSurroundingTextExpectation);
378 } // namespace
380 class IBusEngineServiceTest : public testing::Test {
381 public:
382 IBusEngineServiceTest() {}
384 virtual void SetUp() OVERRIDE {
385 // Create a mock bus.
386 dbus::Bus::Options options;
387 options.bus_type = dbus::Bus::SYSTEM;
388 mock_bus_ = new dbus::MockBus(options);
390 // Create a mock exported object.
391 mock_exported_object_ = new dbus::MockExportedObject(
392 mock_bus_.get(),
393 dbus::ObjectPath(kObjectPath));
395 EXPECT_CALL(*mock_bus_.get(),
396 GetExportedObject(dbus::ObjectPath(kObjectPath)))
397 .WillOnce(Return(mock_exported_object_.get()));
399 EXPECT_CALL(*mock_exported_object_, ExportMethod(
400 ibus::engine::kServiceInterface,
401 ibus::engine::kFocusInMethod , _, _))
402 .WillRepeatedly(
403 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
405 EXPECT_CALL(*mock_exported_object_, ExportMethod(
406 ibus::engine::kServiceInterface,
407 ibus::engine::kFocusOutMethod , _, _))
408 .WillRepeatedly(
409 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
411 EXPECT_CALL(*mock_exported_object_, ExportMethod(
412 ibus::engine::kServiceInterface,
413 ibus::engine::kEnableMethod , _, _))
414 .WillRepeatedly(
415 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
417 EXPECT_CALL(*mock_exported_object_, ExportMethod(
418 ibus::engine::kServiceInterface,
419 ibus::engine::kDisableMethod , _, _))
420 .WillRepeatedly(
421 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
423 EXPECT_CALL(*mock_exported_object_, ExportMethod(
424 ibus::engine::kServiceInterface,
425 ibus::engine::kPropertyActivateMethod , _, _))
426 .WillRepeatedly(
427 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
429 EXPECT_CALL(*mock_exported_object_, ExportMethod(
430 ibus::engine::kServiceInterface,
431 ibus::engine::kPropertyShowMethod , _, _))
432 .WillRepeatedly(
433 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
435 EXPECT_CALL(*mock_exported_object_, ExportMethod(
436 ibus::engine::kServiceInterface,
437 ibus::engine::kPropertyHideMethod , _, _))
438 .WillRepeatedly(
439 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
441 EXPECT_CALL(*mock_exported_object_, ExportMethod(
442 ibus::engine::kServiceInterface,
443 ibus::engine::kSetCapabilityMethod , _, _))
444 .WillRepeatedly(
445 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
447 EXPECT_CALL(*mock_exported_object_, ExportMethod(
448 ibus::engine::kServiceInterface,
449 ibus::engine::kResetMethod , _, _))
450 .WillRepeatedly(
451 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
453 EXPECT_CALL(*mock_exported_object_, ExportMethod(
454 ibus::engine::kServiceInterface,
455 ibus::engine::kProcessKeyEventMethod , _, _))
456 .WillRepeatedly(
457 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
459 EXPECT_CALL(*mock_exported_object_, ExportMethod(
460 ibus::engine::kServiceInterface,
461 ibus::engine::kCandidateClickedMethod , _, _))
462 .WillRepeatedly(
463 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
465 EXPECT_CALL(*mock_exported_object_, ExportMethod(
466 ibus::engine::kServiceInterface,
467 ibus::engine::kSetSurroundingTextMethod , _, _))
468 .WillRepeatedly(
469 Invoke(this, &IBusEngineServiceTest::OnMethodExported));
471 // Suppress uninteresting mock function call warning.
472 EXPECT_CALL(*mock_bus_.get(),
473 AssertOnOriginThread())
474 .WillRepeatedly(Return());
476 // Create a service
477 service_.reset(IBusEngineService::Create(
478 REAL_DBUS_CLIENT_IMPLEMENTATION,
479 mock_bus_.get(),
480 dbus::ObjectPath(kObjectPath)));
482 // Set engine handler.
483 engine_handler_.reset(new MockIBusEngineHandler());
484 service_->SetEngine(engine_handler_.get());
487 protected:
488 // The service to be tested.
489 scoped_ptr<IBusEngineService> service_;
490 // The mock engine handler. Do not free, this is owned by IBusEngineService.
491 scoped_ptr<MockIBusEngineHandler> engine_handler_;
492 // The mock bus.
493 scoped_refptr<dbus::MockBus> mock_bus_;
494 // The mock exported object.
495 scoped_refptr<dbus::MockExportedObject> mock_exported_object_;
496 // A message loop to emulate asynchronous behavior.
497 MessageLoop message_loop_;
498 // The map from method call to method call handler.
499 std::map<std::string, dbus::ExportedObject::MethodCallCallback>
500 method_callback_map_;
502 private:
503 // Used to implement the mock method call.
504 void OnMethodExported(
505 const std::string& interface_name,
506 const std::string& method_name,
507 const dbus::ExportedObject::MethodCallCallback& method_callback,
508 const dbus::ExportedObject::OnExportedCallback& on_exported_callback) {
509 method_callback_map_[method_name] = method_callback;
510 const bool success = true;
511 message_loop_.PostTask(FROM_HERE, base::Bind(on_exported_callback,
512 interface_name,
513 method_name,
514 success));
518 TEST_F(IBusEngineServiceTest, FocusInTest) {
519 // Set expectations.
520 const uint32 kSerialNo = 1;
521 EXPECT_CALL(*engine_handler_, FocusIn());
522 MockResponseSender response_sender;
523 EmptyResponseExpectation response_expectation(kSerialNo);
524 EXPECT_CALL(response_sender, MockRun(_))
525 .WillOnce(Invoke(&response_expectation,
526 &EmptyResponseExpectation::Evaluate));
528 // Create method call;
529 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
530 ibus::engine::kFocusInMethod);
531 method_call.SetSerial(kSerialNo);
533 // Call exported function.
534 EXPECT_NE(method_callback_map_.find(ibus::engine::kFocusInMethod),
535 method_callback_map_.end());
536 method_callback_map_[ibus::engine::kFocusInMethod].Run(
537 &method_call,
538 base::Bind(&MockResponseSender::Run,
539 base::Unretained(&response_sender)));
541 // Call exported function without engine.
542 service_->UnsetEngine();
543 EXPECT_CALL(*engine_handler_, FocusIn()).Times(0);
544 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
545 method_callback_map_[ibus::engine::kFocusInMethod].Run(
546 &method_call,
547 base::Bind(&MockResponseSender::Run,
548 base::Unretained(&response_sender)));
551 TEST_F(IBusEngineServiceTest, FocusOutTest) {
552 // Set expectations.
553 const uint32 kSerialNo = 1;
554 EXPECT_CALL(*engine_handler_, FocusOut());
555 MockResponseSender response_sender;
556 EmptyResponseExpectation response_expectation(kSerialNo);
557 EXPECT_CALL(response_sender, MockRun(_))
558 .WillOnce(Invoke(&response_expectation,
559 &EmptyResponseExpectation::Evaluate));
561 // Create method call;
562 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
563 ibus::engine::kFocusOutMethod);
564 method_call.SetSerial(kSerialNo);
566 // Call exported function.
567 EXPECT_NE(method_callback_map_.find(ibus::engine::kFocusOutMethod),
568 method_callback_map_.end());
569 method_callback_map_[ibus::engine::kFocusOutMethod].Run(
570 &method_call,
571 base::Bind(&MockResponseSender::Run,
572 base::Unretained(&response_sender)));
574 // Call exported function without engine.
575 service_->UnsetEngine();
576 EXPECT_CALL(*engine_handler_, FocusOut()).Times(0);
577 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
578 method_callback_map_[ibus::engine::kFocusOutMethod].Run(
579 &method_call,
580 base::Bind(&MockResponseSender::Run,
581 base::Unretained(&response_sender)));
584 TEST_F(IBusEngineServiceTest, EnableTest) {
585 // Set expectations.
586 const uint32 kSerialNo = 1;
587 EXPECT_CALL(*engine_handler_, Enable());
588 MockResponseSender response_sender;
589 EmptyResponseExpectation response_expectation(kSerialNo);
590 EXPECT_CALL(response_sender, MockRun(_))
591 .WillOnce(Invoke(&response_expectation,
592 &EmptyResponseExpectation::Evaluate));
594 // Create method call;
595 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
596 ibus::engine::kEnableMethod);
597 method_call.SetSerial(kSerialNo);
599 // Call exported function.
600 EXPECT_NE(method_callback_map_.find(ibus::engine::kEnableMethod),
601 method_callback_map_.end());
602 method_callback_map_[ibus::engine::kEnableMethod].Run(
603 &method_call,
604 base::Bind(&MockResponseSender::Run,
605 base::Unretained(&response_sender)));
607 // Call exported function without engine.
608 service_->UnsetEngine();
609 EXPECT_CALL(*engine_handler_, Enable()).Times(0);
610 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
611 method_callback_map_[ibus::engine::kEnableMethod].Run(
612 &method_call,
613 base::Bind(&MockResponseSender::Run,
614 base::Unretained(&response_sender)));
617 TEST_F(IBusEngineServiceTest, DisableTest) {
618 // Set expectations.
619 const uint32 kSerialNo = 1;
620 EXPECT_CALL(*engine_handler_, Disable());
621 MockResponseSender response_sender;
622 EmptyResponseExpectation response_expectation(kSerialNo);
623 EXPECT_CALL(response_sender, MockRun(_))
624 .WillOnce(Invoke(&response_expectation,
625 &EmptyResponseExpectation::Evaluate));
627 // Create method call;
628 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
629 ibus::engine::kDisableMethod);
630 method_call.SetSerial(kSerialNo);
632 // Call exported function.
633 EXPECT_NE(method_callback_map_.find(ibus::engine::kDisableMethod),
634 method_callback_map_.end());
635 method_callback_map_[ibus::engine::kDisableMethod].Run(
636 &method_call,
637 base::Bind(&MockResponseSender::Run,
638 base::Unretained(&response_sender)));
640 // Call exported function without engine.
641 service_->UnsetEngine();
642 EXPECT_CALL(*engine_handler_, Disable()).Times(0);
643 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
644 method_callback_map_[ibus::engine::kDisableMethod].Run(
645 &method_call,
646 base::Bind(&MockResponseSender::Run,
647 base::Unretained(&response_sender)));
650 TEST_F(IBusEngineServiceTest, PropertyActivateTest) {
651 // Set expectations.
652 const uint32 kSerialNo = 1;
653 const std::string kPropertyName = "Property Name";
654 const ibus::IBusPropertyState kIBusPropertyState =
655 ibus::IBUS_PROPERTY_STATE_UNCHECKED;
656 EXPECT_CALL(*engine_handler_, PropertyActivate(kPropertyName,
657 kIBusPropertyState));
658 MockResponseSender response_sender;
659 EmptyResponseExpectation response_expectation(kSerialNo);
660 EXPECT_CALL(response_sender, MockRun(_))
661 .WillOnce(Invoke(&response_expectation,
662 &EmptyResponseExpectation::Evaluate));
664 // Create method call;
665 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
666 ibus::engine::kPropertyActivateMethod);
667 method_call.SetSerial(kSerialNo);
668 dbus::MessageWriter writer(&method_call);
669 writer.AppendString(kPropertyName);
670 writer.AppendUint32(static_cast<uint32>(kIBusPropertyState));
672 // Call exported function.
673 EXPECT_NE(method_callback_map_.find(ibus::engine::kPropertyActivateMethod),
674 method_callback_map_.end());
675 method_callback_map_[ibus::engine::kPropertyActivateMethod].Run(
676 &method_call,
677 base::Bind(&MockResponseSender::Run,
678 base::Unretained(&response_sender)));
680 // Call exported function without engine.
681 service_->UnsetEngine();
682 EXPECT_CALL(*engine_handler_, PropertyActivate(kPropertyName,
683 kIBusPropertyState)).Times(0);
684 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
685 method_callback_map_[ibus::engine::kPropertyActivateMethod].Run(
686 &method_call,
687 base::Bind(&MockResponseSender::Run,
688 base::Unretained(&response_sender)));
691 TEST_F(IBusEngineServiceTest, ResetTest) {
692 // Set expectations.
693 const uint32 kSerialNo = 1;
694 EXPECT_CALL(*engine_handler_, Reset());
695 MockResponseSender response_sender;
696 EmptyResponseExpectation response_expectation(kSerialNo);
697 EXPECT_CALL(response_sender, MockRun(_))
698 .WillOnce(Invoke(&response_expectation,
699 &EmptyResponseExpectation::Evaluate));
701 // Create method call;
702 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
703 ibus::engine::kResetMethod);
704 method_call.SetSerial(kSerialNo);
706 // Call exported function.
707 EXPECT_NE(method_callback_map_.find(ibus::engine::kResetMethod),
708 method_callback_map_.end());
709 method_callback_map_[ibus::engine::kResetMethod].Run(
710 &method_call,
711 base::Bind(&MockResponseSender::Run,
712 base::Unretained(&response_sender)));
714 // Call exported function without engine.
715 service_->UnsetEngine();
716 EXPECT_CALL(*engine_handler_, Reset()).Times(0);
717 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
718 method_callback_map_[ibus::engine::kResetMethod].Run(
719 &method_call,
720 base::Bind(&MockResponseSender::Run,
721 base::Unretained(&response_sender)));
724 TEST_F(IBusEngineServiceTest, PropertyShowTest) {
725 // Set expectations.
726 const uint32 kSerialNo = 1;
727 const std::string kPropertyName = "Property Name";
728 EXPECT_CALL(*engine_handler_, PropertyShow(kPropertyName));
729 MockResponseSender response_sender;
730 EmptyResponseExpectation response_expectation(kSerialNo);
731 EXPECT_CALL(response_sender, MockRun(_))
732 .WillOnce(Invoke(&response_expectation,
733 &EmptyResponseExpectation::Evaluate));
735 // Create method call;
736 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
737 ibus::engine::kPropertyShowMethod);
738 method_call.SetSerial(kSerialNo);
739 dbus::MessageWriter writer(&method_call);
740 writer.AppendString(kPropertyName);
742 // Call exported function.
743 EXPECT_NE(method_callback_map_.find(ibus::engine::kPropertyShowMethod),
744 method_callback_map_.end());
745 method_callback_map_[ibus::engine::kPropertyShowMethod].Run(
746 &method_call,
747 base::Bind(&MockResponseSender::Run,
748 base::Unretained(&response_sender)));
750 // Call exported function without engine.
751 service_->UnsetEngine();
752 EXPECT_CALL(*engine_handler_, PropertyShow(kPropertyName)).Times(0);
753 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
754 method_callback_map_[ibus::engine::kPropertyShowMethod].Run(
755 &method_call,
756 base::Bind(&MockResponseSender::Run,
757 base::Unretained(&response_sender)));
760 TEST_F(IBusEngineServiceTest, PropertyHideTest) {
761 // Set expectations.
762 const uint32 kSerialNo = 1;
763 const std::string kPropertyName = "Property Name";
764 EXPECT_CALL(*engine_handler_, PropertyHide(kPropertyName));
765 MockResponseSender response_sender;
766 EmptyResponseExpectation response_expectation(kSerialNo);
767 EXPECT_CALL(response_sender, MockRun(_))
768 .WillOnce(Invoke(&response_expectation,
769 &EmptyResponseExpectation::Evaluate));
771 // Create method call;
772 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
773 ibus::engine::kPropertyHideMethod);
774 method_call.SetSerial(kSerialNo);
775 dbus::MessageWriter writer(&method_call);
776 writer.AppendString(kPropertyName);
778 // Call exported function.
779 EXPECT_NE(method_callback_map_.find(ibus::engine::kPropertyHideMethod),
780 method_callback_map_.end());
781 method_callback_map_[ibus::engine::kPropertyHideMethod].Run(
782 &method_call,
783 base::Bind(&MockResponseSender::Run,
784 base::Unretained(&response_sender)));
786 // Call exported function without engine.
787 service_->UnsetEngine();
788 EXPECT_CALL(*engine_handler_, PropertyHide(kPropertyName)).Times(0);
789 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
790 method_callback_map_[ibus::engine::kPropertyHideMethod].Run(
791 &method_call,
792 base::Bind(&MockResponseSender::Run,
793 base::Unretained(&response_sender)));
796 TEST_F(IBusEngineServiceTest, SetCapabilityTest) {
797 // Set expectations.
798 const uint32 kSerialNo = 1;
799 const IBusEngineHandlerInterface::IBusCapability kIBusCapability =
800 IBusEngineHandlerInterface::IBUS_CAPABILITY_PREEDIT_TEXT;
801 EXPECT_CALL(*engine_handler_, SetCapability(kIBusCapability));
802 MockResponseSender response_sender;
803 EmptyResponseExpectation response_expectation(kSerialNo);
804 EXPECT_CALL(response_sender, MockRun(_))
805 .WillOnce(Invoke(&response_expectation,
806 &EmptyResponseExpectation::Evaluate));
808 // Create method call;
809 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
810 ibus::engine::kSetCapabilityMethod);
811 method_call.SetSerial(kSerialNo);
812 dbus::MessageWriter writer(&method_call);
813 writer.AppendUint32(static_cast<uint32>(kIBusCapability));
815 // Call exported function.
816 EXPECT_NE(method_callback_map_.find(ibus::engine::kSetCapabilityMethod),
817 method_callback_map_.end());
818 method_callback_map_[ibus::engine::kSetCapabilityMethod].Run(
819 &method_call,
820 base::Bind(&MockResponseSender::Run,
821 base::Unretained(&response_sender)));
823 // Call exported function without engine.
824 service_->UnsetEngine();
825 EXPECT_CALL(*engine_handler_, SetCapability(kIBusCapability)).Times(0);
826 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
827 method_callback_map_[ibus::engine::kSetCapabilityMethod].Run(
828 &method_call,
829 base::Bind(&MockResponseSender::Run,
830 base::Unretained(&response_sender)));
833 TEST_F(IBusEngineServiceTest, ProcessKeyEventTest) {
834 // Set expectations.
835 const uint32 kSerialNo = 1;
836 const uint32 kKeySym = 0x64;
837 const uint32 kKeyCode = 0x20;
838 const uint32 kState = 0x00;
839 const bool kResult = true;
841 ProcessKeyEventHandler handler(kResult);
842 EXPECT_CALL(*engine_handler_, ProcessKeyEvent(kKeySym, kKeyCode, kState, _))
843 .WillOnce(Invoke(&handler,
844 &ProcessKeyEventHandler::ProcessKeyEvent));
845 MockResponseSender response_sender;
846 BoolResponseExpectation response_expectation(kSerialNo, kResult);
847 EXPECT_CALL(response_sender, MockRun(_))
848 .WillOnce(Invoke(&response_expectation,
849 &BoolResponseExpectation::Evaluate));
851 // Create method call;
852 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
853 ibus::engine::kProcessKeyEventMethod);
854 method_call.SetSerial(kSerialNo);
855 dbus::MessageWriter writer(&method_call);
856 writer.AppendUint32(kKeySym);
857 writer.AppendUint32(kKeyCode);
858 writer.AppendUint32(kState);
860 // Call exported function.
861 EXPECT_NE(method_callback_map_.find(ibus::engine::kProcessKeyEventMethod),
862 method_callback_map_.end());
863 method_callback_map_[ibus::engine::kProcessKeyEventMethod].Run(
864 &method_call,
865 base::Bind(&MockResponseSender::Run,
866 base::Unretained(&response_sender)));
868 // Call exported function without engine.
869 service_->UnsetEngine();
870 EXPECT_CALL(*engine_handler_,
871 ProcessKeyEvent(kKeySym, kKeyCode, kState, _)).Times(0);
872 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
873 method_callback_map_[ibus::engine::kProcessKeyEventMethod].Run(
874 &method_call,
875 base::Bind(&MockResponseSender::Run,
876 base::Unretained(&response_sender)));
879 TEST_F(IBusEngineServiceTest, DelayProcessKeyEventTest) {
880 // Set expectations.
881 const uint32 kSerialNo = 1;
882 const uint32 kKeySym = 0x64;
883 const uint32 kKeyCode = 0x20;
884 const uint32 kState = 0x00;
885 const bool kResult = true;
887 DelayProcessKeyEventHandler handler(kResult, &message_loop_);
888 EXPECT_CALL(*engine_handler_, ProcessKeyEvent(kKeySym, kKeyCode, kState, _))
889 .WillOnce(Invoke(&handler,
890 &DelayProcessKeyEventHandler::ProcessKeyEvent));
891 MockResponseSender response_sender;
892 BoolResponseExpectation response_expectation(kSerialNo, kResult);
893 EXPECT_CALL(response_sender, MockRun(_))
894 .WillOnce(Invoke(&response_expectation,
895 &BoolResponseExpectation::Evaluate));
897 // Create method call;
898 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
899 ibus::engine::kProcessKeyEventMethod);
900 method_call.SetSerial(kSerialNo);
901 dbus::MessageWriter writer(&method_call);
902 writer.AppendUint32(kKeySym);
903 writer.AppendUint32(kKeyCode);
904 writer.AppendUint32(kState);
906 // Call exported function.
907 EXPECT_NE(method_callback_map_.find(ibus::engine::kProcessKeyEventMethod),
908 method_callback_map_.end());
909 method_callback_map_[ibus::engine::kProcessKeyEventMethod].Run(
910 &method_call,
911 base::Bind(&MockResponseSender::Run,
912 base::Unretained(&response_sender)));
914 // Call KeyEventDone callback.
915 message_loop_.RunUntilIdle();
917 // Call exported function without engine.
918 service_->UnsetEngine();
919 EXPECT_CALL(*engine_handler_,
920 ProcessKeyEvent(kKeySym, kKeyCode, kState, _)).Times(0);
921 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
922 method_callback_map_[ibus::engine::kProcessKeyEventMethod].Run(
923 &method_call,
924 base::Bind(&MockResponseSender::Run,
925 base::Unretained(&response_sender)));
928 TEST_F(IBusEngineServiceTest, CandidateClickedTest) {
929 // Set expectations.
930 const uint32 kSerialNo = 1;
931 const uint32 kIndex = 4;
932 const ibus::IBusMouseButton kIBusMouseButton = ibus::IBUS_MOUSE_BUTTON_MIDDLE;
933 const uint32 kState = 3;
934 EXPECT_CALL(*engine_handler_, CandidateClicked(kIndex, kIBusMouseButton,
935 kState));
936 MockResponseSender response_sender;
937 EmptyResponseExpectation response_expectation(kSerialNo);
938 EXPECT_CALL(response_sender, MockRun(_))
939 .WillOnce(Invoke(&response_expectation,
940 &EmptyResponseExpectation::Evaluate));
942 // Create method call;
943 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
944 ibus::engine::kCandidateClickedMethod);
945 method_call.SetSerial(kSerialNo);
946 dbus::MessageWriter writer(&method_call);
947 writer.AppendUint32(kIndex);
948 writer.AppendUint32(static_cast<uint32>(kIBusMouseButton));
949 writer.AppendUint32(kState);
951 // Call exported function.
952 EXPECT_NE(method_callback_map_.find(ibus::engine::kCandidateClickedMethod),
953 method_callback_map_.end());
954 method_callback_map_[ibus::engine::kCandidateClickedMethod].Run(
955 &method_call,
956 base::Bind(&MockResponseSender::Run,
957 base::Unretained(&response_sender)));
959 // Call exported function without engine.
960 service_->UnsetEngine();
961 EXPECT_CALL(*engine_handler_, CandidateClicked(kIndex, kIBusMouseButton,
962 kState)).Times(0);
963 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
964 method_callback_map_[ibus::engine::kCandidateClickedMethod].Run(
965 &method_call,
966 base::Bind(&MockResponseSender::Run,
967 base::Unretained(&response_sender)));
970 TEST_F(IBusEngineServiceTest, SetSurroundingTextTest) {
971 // Set expectations.
972 const uint32 kSerialNo = 1;
973 const std::string kText = "Sample Text";
974 const uint32 kCursorPos = 3;
975 const uint32 kAnchorPos = 4;
976 EXPECT_CALL(*engine_handler_, SetSurroundingText(kText, kCursorPos,
977 kAnchorPos));
978 MockResponseSender response_sender;
979 EmptyResponseExpectation response_expectation(kSerialNo);
980 EXPECT_CALL(response_sender, MockRun(_))
981 .WillOnce(Invoke(&response_expectation,
982 &EmptyResponseExpectation::Evaluate));
984 // Create method call;
985 dbus::MethodCall method_call(ibus::engine::kServiceInterface,
986 ibus::engine::kSetSurroundingTextMethod);
987 method_call.SetSerial(kSerialNo);
988 dbus::MessageWriter writer(&method_call);
989 AppendStringAsIBusText(kText, &writer);
990 writer.AppendUint32(kCursorPos);
991 writer.AppendUint32(kAnchorPos);
993 // Call exported function.
994 EXPECT_NE(method_callback_map_.find(ibus::engine::kSetSurroundingTextMethod),
995 method_callback_map_.end());
996 method_callback_map_[ibus::engine::kSetSurroundingTextMethod].Run(
997 &method_call,
998 base::Bind(&MockResponseSender::Run,
999 base::Unretained(&response_sender)));
1001 // Call exported function without engine.
1002 service_->UnsetEngine();
1003 EXPECT_CALL(*engine_handler_, SetSurroundingText(kText, kCursorPos,
1004 kAnchorPos)).Times(0);
1005 EXPECT_CALL(response_sender, MockRun(_)).Times(0);
1006 method_callback_map_[ibus::engine::kSetSurroundingTextMethod].Run(
1007 &method_call,
1008 base::Bind(&MockResponseSender::Run,
1009 base::Unretained(&response_sender)));
1012 TEST_F(IBusEngineServiceTest, RegisterProperties) {
1013 // Set expectations.
1014 IBusPropertyList property_list;
1015 property_list.push_back(new IBusProperty());
1016 property_list[0]->set_key("Sample Key");
1017 property_list[0]->set_type(IBusProperty::IBUS_PROPERTY_TYPE_MENU);
1018 property_list[0]->set_label("Sample Label");
1019 property_list[0]->set_tooltip("Sample Tooltip");
1020 property_list[0]->set_visible(true);
1021 property_list[0]->set_checked(true);
1023 RegisterPropertiesExpectation expectation(property_list);
1024 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
1025 .WillOnce(Invoke(&expectation,
1026 &RegisterPropertiesExpectation::Evaluate));
1027 // Emit signal.
1028 service_->RegisterProperties(property_list);
1031 TEST_F(IBusEngineServiceTest, UpdatePreeditTest) {
1032 // Set expectations.
1033 IBusText ibus_text;
1034 ibus_text.set_text("Sample Text");
1035 const uint32 kCursorPos = 9;
1036 const bool kIsVisible = false;
1037 const IBusEngineService::IBusEnginePreeditFocusOutMode kPreeditMode =
1038 IBusEngineService::IBUS_ENGINE_PREEEDIT_FOCUS_OUT_MODE_CLEAR;
1039 UpdatePreeditExpectation expectation(ibus_text, kCursorPos, kIsVisible,
1040 kPreeditMode);
1041 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
1042 .WillOnce(Invoke(&expectation, &UpdatePreeditExpectation::Evaluate));
1044 // Emit signal.
1045 service_->UpdatePreedit(ibus_text, kCursorPos, kIsVisible, kPreeditMode);
1048 TEST_F(IBusEngineServiceTest, UpdateAuxiliaryText) {
1049 IBusText ibus_text;
1050 ibus_text.set_text("Sample Text");
1051 const bool kIsVisible = false;
1052 UpdateAuxiliaryTextExpectation expectation(ibus_text, kIsVisible);
1054 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
1055 .WillOnce(Invoke(&expectation,
1056 &UpdateAuxiliaryTextExpectation::Evaluate));
1058 // Emit signal.
1059 service_->UpdateAuxiliaryText(ibus_text, kIsVisible);
1062 TEST_F(IBusEngineServiceTest, UpdateLookupTableTest) {
1063 IBusLookupTable lookup_table;
1064 lookup_table.set_page_size(10);
1065 lookup_table.set_cursor_position(2);
1066 lookup_table.set_is_cursor_visible(false);
1067 const bool kIsVisible = true;
1069 UpdateLookupTableExpectation expectation(lookup_table, kIsVisible);
1070 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
1071 .WillOnce(Invoke(&expectation,
1072 &UpdateLookupTableExpectation::Evaluate));
1074 // Emit signal.
1075 service_->UpdateLookupTable(lookup_table, kIsVisible);
1078 TEST_F(IBusEngineServiceTest, UpdatePropertyTest) {
1079 IBusProperty property;
1080 property.set_key("Sample Key");
1081 property.set_type(IBusProperty::IBUS_PROPERTY_TYPE_MENU);
1082 property.set_label("Sample Label");
1083 property.set_tooltip("Sample Tooltip");
1084 property.set_visible(true);
1085 property.set_checked(true);
1087 UpdatePropertyExpectation expectation(property);
1088 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
1089 .WillOnce(Invoke(&expectation,
1090 &UpdatePropertyExpectation::Evaluate));
1092 // Emit signal.
1093 service_->UpdateProperty(property);
1096 TEST_F(IBusEngineServiceTest, ForwardKeyEventTest) {
1097 uint32 keyval = 0x20;
1098 uint32 keycode = 0x64;
1099 uint32 state = 0x00;
1101 ForwardKeyEventExpectation expectation(keyval, keycode, state);
1103 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
1104 .WillOnce(Invoke(&expectation,
1105 &ForwardKeyEventExpectation::Evaluate));
1107 // Emit signal.
1108 service_->ForwardKeyEvent(keyval, keycode, state);
1111 TEST_F(IBusEngineServiceTest, RequireSurroundingTextTest) {
1112 RequireSurroundingTextExpectation expectation;
1113 EXPECT_CALL(*mock_exported_object_, SendSignal(_))
1114 .WillOnce(Invoke(&expectation,
1115 &RequireSurroundingTextExpectation::Evaluate));
1117 // Emit signal.
1118 service_->RequireSurroundingText();
1121 } // namespace chromeos