OLD | NEW |
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_panel_service.h" | 5 #include "chromeos/dbus/ibus/ibus_panel_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chromeos/dbus/ibus/ibus_constants.h" | 11 #include "chromeos/dbus/ibus/ibus_constants.h" |
12 #include "chromeos/dbus/ibus/ibus_input_context_client.h" | 12 #include "chromeos/dbus/ibus/ibus_input_context_client.h" |
13 #include "chromeos/dbus/ibus/ibus_lookup_table.h" | 13 #include "chromeos/dbus/ibus/ibus_lookup_table.h" |
14 #include "chromeos/dbus/ibus/ibus_property.h" | 14 #include "chromeos/dbus/ibus/ibus_property.h" |
15 #include "chromeos/dbus/ibus/ibus_text.h" | 15 #include "chromeos/dbus/ibus/ibus_text.h" |
16 #include "dbus/message.h" | 16 #include "dbus/message.h" |
17 #include "dbus/mock_bus.h" | 17 #include "dbus/mock_bus.h" |
18 #include "dbus/mock_exported_object.h" | 18 #include "dbus/mock_exported_object.h" |
19 #include "dbus/object_path.h" | 19 #include "dbus/object_path.h" |
20 #include "dbus/values_util.h" | 20 #include "dbus/values_util.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using testing::Invoke; | 24 using testing::Invoke; |
25 using testing::Return; | 25 using testing::Return; |
26 using testing::_; | 26 using testing::_; |
27 | 27 |
28 namespace chromeos { | 28 namespace chromeos { |
29 // TODO(nona): Remove ibus namespace after complete libibus removal. | |
30 namespace ibus { | |
31 | 29 |
32 namespace { | 30 namespace { |
33 | 31 |
34 class MockIBusPanelCandidateWindowHandler | 32 class MockIBusPanelCandidateWindowHandler |
35 : public IBusPanelCandidateWindowHandlerInterface { | 33 : public IBusPanelCandidateWindowHandlerInterface { |
36 public: | 34 public: |
37 MockIBusPanelCandidateWindowHandler() {} | 35 MockIBusPanelCandidateWindowHandler() {} |
38 MOCK_METHOD2(UpdateLookupTable, void(const ibus::IBusLookupTable& table, | 36 MOCK_METHOD2(UpdateLookupTable, void(const IBusLookupTable& table, |
39 bool visible)); | 37 bool visible)); |
40 MOCK_METHOD0(HideLookupTable, void()); | 38 MOCK_METHOD0(HideLookupTable, void()); |
41 MOCK_METHOD2(UpdateAuxiliaryText, void(const std::string& text, | 39 MOCK_METHOD2(UpdateAuxiliaryText, void(const std::string& text, |
42 bool visible)); | 40 bool visible)); |
43 MOCK_METHOD0(HideAuxiliaryText, void()); | 41 MOCK_METHOD0(HideAuxiliaryText, void()); |
44 MOCK_METHOD3(UpdatePreeditText, void(const std::string& text, | 42 MOCK_METHOD3(UpdatePreeditText, void(const std::string& text, |
45 uint32 cursor_pos, | 43 uint32 cursor_pos, |
46 bool visible) ); | 44 bool visible) ); |
47 MOCK_METHOD0(HidePreeditText, void()); | 45 MOCK_METHOD0(HidePreeditText, void()); |
48 MOCK_METHOD2(SetCursorLocation, void(const ibus::Rect& cursor_location, | 46 MOCK_METHOD2(SetCursorLocation, void(const ibus::Rect& cursor_location, |
49 const ibus::Rect& composition_head)); | 47 const ibus::Rect& composition_head)); |
50 | 48 |
51 private: | 49 private: |
52 DISALLOW_COPY_AND_ASSIGN(MockIBusPanelCandidateWindowHandler); | 50 DISALLOW_COPY_AND_ASSIGN(MockIBusPanelCandidateWindowHandler); |
53 }; | 51 }; |
54 | 52 |
55 class MockIBusPanelPropertyHandler : public IBusPanelPropertyHandlerInterface { | 53 class MockIBusPanelPropertyHandler : public IBusPanelPropertyHandlerInterface { |
56 public: | 54 public: |
57 MockIBusPanelPropertyHandler() {} | 55 MockIBusPanelPropertyHandler() {} |
58 MOCK_METHOD1(RegisterProperties, | 56 MOCK_METHOD1(RegisterProperties, |
59 void(const ibus::IBusPropertyList& properties)); | 57 void(const IBusPropertyList& properties)); |
60 MOCK_METHOD1(UpdateProperty, void(const ibus::IBusProperty& property)); | 58 MOCK_METHOD1(UpdateProperty, void(const IBusProperty& property)); |
61 | 59 |
62 private: | 60 private: |
63 DISALLOW_COPY_AND_ASSIGN(MockIBusPanelPropertyHandler); | 61 DISALLOW_COPY_AND_ASSIGN(MockIBusPanelPropertyHandler); |
64 }; | 62 }; |
65 | 63 |
66 class MockResponseSender { | 64 class MockResponseSender { |
67 public: | 65 public: |
68 MOCK_METHOD1(Run, void(dbus::Response* reponse)); | 66 MOCK_METHOD1(Run, void(dbus::Response* reponse)); |
69 }; | 67 }; |
70 | 68 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 EXPECT_FALSE(reader.HasMoreData()); | 138 EXPECT_FALSE(reader.HasMoreData()); |
141 } | 139 } |
142 | 140 |
143 private: | 141 private: |
144 std::string expected_signal_name_; | 142 std::string expected_signal_name_; |
145 DISALLOW_COPY_AND_ASSIGN(NullArgumentVerifier); | 143 DISALLOW_COPY_AND_ASSIGN(NullArgumentVerifier); |
146 }; | 144 }; |
147 | 145 |
148 class UpdateLookupTableVerifier { | 146 class UpdateLookupTableVerifier { |
149 public: | 147 public: |
150 UpdateLookupTableVerifier(const ibus::IBusLookupTable& table, bool visible) | 148 UpdateLookupTableVerifier(const IBusLookupTable& table, bool visible) |
151 : table_(table), | 149 : table_(table), |
152 visible_(visible) {} | 150 visible_(visible) {} |
153 | 151 |
154 void Verify(const ibus::IBusLookupTable& table, bool visible) { | 152 void Verify(const IBusLookupTable& table, bool visible) { |
155 EXPECT_EQ(table_.page_size(), table.page_size()); | 153 EXPECT_EQ(table_.page_size(), table.page_size()); |
156 EXPECT_EQ(table_.cursor_position(), table.cursor_position()); | 154 EXPECT_EQ(table_.cursor_position(), table.cursor_position()); |
157 EXPECT_EQ(visible_, visible); | 155 EXPECT_EQ(visible_, visible); |
158 } | 156 } |
159 | 157 |
160 private: | 158 private: |
161 const ibus::IBusLookupTable& table_; | 159 const IBusLookupTable& table_; |
162 const bool visible_; | 160 const bool visible_; |
163 | 161 |
164 DISALLOW_COPY_AND_ASSIGN(UpdateLookupTableVerifier); | 162 DISALLOW_COPY_AND_ASSIGN(UpdateLookupTableVerifier); |
165 }; | 163 }; |
166 | 164 |
167 // This class is used to verify that a method call which has a PropertyList | 165 // This class is used to verify that a method call which has a PropertyList |
168 // object. This class verifies a method call has correct arguments based on | 166 // object. This class verifies a method call has correct arguments based on |
169 // checking given |keys|. | 167 // checking given |keys|. |
170 class PropertyListVerifier { | 168 class PropertyListVerifier { |
171 public: | 169 public: |
172 explicit PropertyListVerifier(const std::vector<std::string>& expected_keys) | 170 explicit PropertyListVerifier(const std::vector<std::string>& expected_keys) |
173 : expected_keys_(expected_keys) { | 171 : expected_keys_(expected_keys) { |
174 } | 172 } |
175 | 173 |
176 // Verifies the given |resposne| has IBusPropertyList. | 174 // Verifies the given |resposne| has IBusPropertyList. |
177 void Verify(const ibus::IBusPropertyList& properties) { | 175 void Verify(const IBusPropertyList& properties) { |
178 ASSERT_EQ(expected_keys_.size(), properties.size()); | 176 ASSERT_EQ(expected_keys_.size(), properties.size()); |
179 for (size_t i = 0; i < properties.size(); ++i) { | 177 for (size_t i = 0; i < properties.size(); ++i) { |
180 EXPECT_EQ(expected_keys_[i], properties[i]->key()); | 178 EXPECT_EQ(expected_keys_[i], properties[i]->key()); |
181 } | 179 } |
182 } | 180 } |
183 | 181 |
184 private: | 182 private: |
185 const std::vector<std::string> expected_keys_; | 183 const std::vector<std::string> expected_keys_; |
186 }; | 184 }; |
187 | 185 |
188 // This class is used to verify that a method call which has a Property object. | 186 // This class is used to verify that a method call which has a Property object. |
189 // This class verifies a method call has correct argument based on |key|. | 187 // This class verifies a method call has correct argument based on |key|. |
190 class PropertyVerifier { | 188 class PropertyVerifier { |
191 public: | 189 public: |
192 explicit PropertyVerifier(const std::string& key) : key_(key) {} | 190 explicit PropertyVerifier(const std::string& key) : key_(key) {} |
193 | 191 |
194 // Verifies the given |resposne| has IBusPropertyList. | 192 // Verifies the given |resposne| has IBusPropertyList. |
195 void Verify(const ibus::IBusProperty& property) { | 193 void Verify(const IBusProperty& property) { |
196 EXPECT_EQ(key_, property.key()); | 194 EXPECT_EQ(key_, property.key()); |
197 } | 195 } |
198 | 196 |
199 private: | 197 private: |
200 const std::string key_; | 198 const std::string key_; |
201 }; | 199 }; |
202 | 200 |
203 } // namespace | 201 } // namespace |
204 | 202 |
205 class IBusPanelServiceTest : public testing::Test { | 203 class IBusPanelServiceTest : public testing::Test { |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 method_callback_map_.end()); | 411 method_callback_map_.end()); |
414 method_callback_map_[ibus::panel::kHidePreeditTextMethod].Run( | 412 method_callback_map_[ibus::panel::kHidePreeditTextMethod].Run( |
415 &method_call, | 413 &method_call, |
416 base::Bind(&MockResponseSender::Run, | 414 base::Bind(&MockResponseSender::Run, |
417 base::Unretained(&response_sender))); | 415 base::Unretained(&response_sender))); |
418 } | 416 } |
419 | 417 |
420 TEST_F(IBusPanelServiceTest, UpdateLookupTableTest) { | 418 TEST_F(IBusPanelServiceTest, UpdateLookupTableTest) { |
421 // Set expectations. | 419 // Set expectations. |
422 const uint32 kSerialNo = 1; | 420 const uint32 kSerialNo = 1; |
423 ibus::IBusLookupTable table; | 421 IBusLookupTable table; |
424 table.set_page_size(3); | 422 table.set_page_size(3); |
425 table.set_cursor_position(4); | 423 table.set_cursor_position(4); |
426 const bool kVisible = false; | 424 const bool kVisible = false; |
427 | 425 |
428 | 426 |
429 UpdateLookupTableVerifier evaluator(table, kVisible); | 427 UpdateLookupTableVerifier evaluator(table, kVisible); |
430 EXPECT_CALL(*candidate_window_handler_, UpdateLookupTable(_, _)) | 428 EXPECT_CALL(*candidate_window_handler_, UpdateLookupTable(_, _)) |
431 .WillOnce(Invoke(&evaluator, | 429 .WillOnce(Invoke(&evaluator, |
432 &UpdateLookupTableVerifier::Verify)); | 430 &UpdateLookupTableVerifier::Verify)); |
433 MockResponseSender response_sender; | 431 MockResponseSender response_sender; |
(...skipping 30 matching lines...) Expand all Loading... |
464 EmptyResponseVerifier response_expectation(kSerialNo); | 462 EmptyResponseVerifier response_expectation(kSerialNo); |
465 EXPECT_CALL(response_sender, Run(_)) | 463 EXPECT_CALL(response_sender, Run(_)) |
466 .WillOnce(Invoke(&response_expectation, | 464 .WillOnce(Invoke(&response_expectation, |
467 &EmptyResponseVerifier::Verify)); | 465 &EmptyResponseVerifier::Verify)); |
468 | 466 |
469 // Create method call; | 467 // Create method call; |
470 dbus::MethodCall method_call(ibus::panel::kServiceInterface, | 468 dbus::MethodCall method_call(ibus::panel::kServiceInterface, |
471 ibus::panel::kUpdateAuxiliaryTextMethod); | 469 ibus::panel::kUpdateAuxiliaryTextMethod); |
472 method_call.SetSerial(kSerialNo); | 470 method_call.SetSerial(kSerialNo); |
473 dbus::MessageWriter writer(&method_call); | 471 dbus::MessageWriter writer(&method_call); |
474 ibus::AppendStringAsIBusText(text, &writer); | 472 AppendStringAsIBusText(text, &writer); |
475 writer.AppendBool(kVisible); | 473 writer.AppendBool(kVisible); |
476 | 474 |
477 // Call exported function. | 475 // Call exported function. |
478 EXPECT_NE(method_callback_map_.find(ibus::panel::kUpdateAuxiliaryTextMethod), | 476 EXPECT_NE(method_callback_map_.find(ibus::panel::kUpdateAuxiliaryTextMethod), |
479 method_callback_map_.end()); | 477 method_callback_map_.end()); |
480 method_callback_map_[ibus::panel::kUpdateAuxiliaryTextMethod].Run( | 478 method_callback_map_[ibus::panel::kUpdateAuxiliaryTextMethod].Run( |
481 &method_call, | 479 &method_call, |
482 base::Bind(&MockResponseSender::Run, | 480 base::Bind(&MockResponseSender::Run, |
483 base::Unretained(&response_sender))); | 481 base::Unretained(&response_sender))); |
484 } | 482 } |
(...skipping 11 matching lines...) Expand all Loading... |
496 EmptyResponseVerifier response_expectation(kSerialNo); | 494 EmptyResponseVerifier response_expectation(kSerialNo); |
497 EXPECT_CALL(response_sender, Run(_)) | 495 EXPECT_CALL(response_sender, Run(_)) |
498 .WillOnce(Invoke(&response_expectation, | 496 .WillOnce(Invoke(&response_expectation, |
499 &EmptyResponseVerifier::Verify)); | 497 &EmptyResponseVerifier::Verify)); |
500 | 498 |
501 // Create method call; | 499 // Create method call; |
502 dbus::MethodCall method_call(ibus::panel::kServiceInterface, | 500 dbus::MethodCall method_call(ibus::panel::kServiceInterface, |
503 ibus::panel::kUpdatePreeditTextMethod); | 501 ibus::panel::kUpdatePreeditTextMethod); |
504 method_call.SetSerial(kSerialNo); | 502 method_call.SetSerial(kSerialNo); |
505 dbus::MessageWriter writer(&method_call); | 503 dbus::MessageWriter writer(&method_call); |
506 ibus::AppendStringAsIBusText(text, &writer); | 504 AppendStringAsIBusText(text, &writer); |
507 writer.AppendUint32(kCursorPos); | 505 writer.AppendUint32(kCursorPos); |
508 writer.AppendBool(kVisible); | 506 writer.AppendBool(kVisible); |
509 | 507 |
510 // Call exported function. | 508 // Call exported function. |
511 EXPECT_NE(method_callback_map_.find(ibus::panel::kUpdatePreeditTextMethod), | 509 EXPECT_NE(method_callback_map_.find(ibus::panel::kUpdatePreeditTextMethod), |
512 method_callback_map_.end()); | 510 method_callback_map_.end()); |
513 method_callback_map_[ibus::panel::kUpdatePreeditTextMethod].Run( | 511 method_callback_map_[ibus::panel::kUpdatePreeditTextMethod].Run( |
514 &method_call, | 512 &method_call, |
515 base::Bind(&MockResponseSender::Run, | 513 base::Bind(&MockResponseSender::Run, |
516 base::Unretained(&response_sender))); | 514 base::Unretained(&response_sender))); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 // Emit signal. | 553 // Emit signal. |
556 service_->PageDown(); | 554 service_->PageDown(); |
557 } | 555 } |
558 | 556 |
559 TEST_F(IBusPanelServiceTest, RegisterPropertiesTest) { | 557 TEST_F(IBusPanelServiceTest, RegisterPropertiesTest) { |
560 // Set expectations. | 558 // Set expectations. |
561 std::vector<std::string> keys; | 559 std::vector<std::string> keys; |
562 keys.push_back("key1"); | 560 keys.push_back("key1"); |
563 keys.push_back("key2"); | 561 keys.push_back("key2"); |
564 keys.push_back("key3"); | 562 keys.push_back("key3"); |
565 ibus::IBusPropertyList properties; | 563 IBusPropertyList properties; |
566 for (size_t i = 0; i < keys.size(); ++i) { | 564 for (size_t i = 0; i < keys.size(); ++i) { |
567 ibus::IBusProperty* property = new ibus::IBusProperty; | 565 IBusProperty* property = new IBusProperty; |
568 property->set_key(keys[i]); | 566 property->set_key(keys[i]); |
569 properties.push_back(property); | 567 properties.push_back(property); |
570 } | 568 } |
571 | 569 |
572 PropertyListVerifier response_expectation(keys); | 570 PropertyListVerifier response_expectation(keys); |
573 EXPECT_CALL(*property_handler_, RegisterProperties(_)) | 571 EXPECT_CALL(*property_handler_, RegisterProperties(_)) |
574 .WillOnce(Invoke(&response_expectation, | 572 .WillOnce(Invoke(&response_expectation, |
575 &PropertyListVerifier::Verify)); | 573 &PropertyListVerifier::Verify)); |
576 | 574 |
577 MockResponseSender response_sender; | 575 MockResponseSender response_sender; |
578 EXPECT_CALL(response_sender, Run(_)); | 576 EXPECT_CALL(response_sender, Run(_)); |
579 | 577 |
580 // Create method call; | 578 // Create method call; |
581 dbus::MethodCall method_call(ibus::panel::kServiceInterface, | 579 dbus::MethodCall method_call(ibus::panel::kServiceInterface, |
582 ibus::panel::kRegisterPropertiesMethod); | 580 ibus::panel::kRegisterPropertiesMethod); |
583 method_call.SetSerial(1UL); | 581 method_call.SetSerial(1UL); |
584 dbus::MessageWriter writer(&method_call); | 582 dbus::MessageWriter writer(&method_call); |
585 ibus::AppendIBusPropertyList(properties, &writer); | 583 AppendIBusPropertyList(properties, &writer); |
586 | 584 |
587 // Call exported function. | 585 // Call exported function. |
588 EXPECT_NE(method_callback_map_.find(ibus::panel::kRegisterPropertiesMethod), | 586 EXPECT_NE(method_callback_map_.find(ibus::panel::kRegisterPropertiesMethod), |
589 method_callback_map_.end()); | 587 method_callback_map_.end()); |
590 method_callback_map_[ibus::panel::kRegisterPropertiesMethod].Run( | 588 method_callback_map_[ibus::panel::kRegisterPropertiesMethod].Run( |
591 &method_call, | 589 &method_call, |
592 base::Bind(&MockResponseSender::Run, base::Unretained(&response_sender))); | 590 base::Bind(&MockResponseSender::Run, base::Unretained(&response_sender))); |
593 } | 591 } |
594 | 592 |
595 TEST_F(IBusPanelServiceTest, UpdatePropertyTest) { | 593 TEST_F(IBusPanelServiceTest, UpdatePropertyTest) { |
596 // Set expectations. | 594 // Set expectations. |
597 const char kKey[] = "key"; | 595 const char kKey[] = "key"; |
598 ibus::IBusProperty property; | 596 IBusProperty property; |
599 property.set_key(kKey); | 597 property.set_key(kKey); |
600 | 598 |
601 PropertyVerifier response_expectation(kKey); | 599 PropertyVerifier response_expectation(kKey); |
602 EXPECT_CALL(*property_handler_, UpdateProperty(_)) | 600 EXPECT_CALL(*property_handler_, UpdateProperty(_)) |
603 .WillOnce(Invoke(&response_expectation, &PropertyVerifier::Verify)); | 601 .WillOnce(Invoke(&response_expectation, &PropertyVerifier::Verify)); |
604 | 602 |
605 MockResponseSender response_sender; | 603 MockResponseSender response_sender; |
606 EXPECT_CALL(response_sender, Run(_)); | 604 EXPECT_CALL(response_sender, Run(_)); |
607 | 605 |
608 // Create method call; | 606 // Create method call; |
609 dbus::MethodCall method_call(ibus::panel::kServiceInterface, | 607 dbus::MethodCall method_call(ibus::panel::kServiceInterface, |
610 ibus::panel::kUpdatePropertyMethod); | 608 ibus::panel::kUpdatePropertyMethod); |
611 method_call.SetSerial(1UL); | 609 method_call.SetSerial(1UL); |
612 dbus::MessageWriter writer(&method_call); | 610 dbus::MessageWriter writer(&method_call); |
613 ibus::AppendIBusProperty(property, &writer); | 611 AppendIBusProperty(property, &writer); |
614 | 612 |
615 // Call exported function. | 613 // Call exported function. |
616 EXPECT_NE(method_callback_map_.find(ibus::panel::kUpdatePropertyMethod), | 614 EXPECT_NE(method_callback_map_.find(ibus::panel::kUpdatePropertyMethod), |
617 method_callback_map_.end()); | 615 method_callback_map_.end()); |
618 method_callback_map_[ibus::panel::kUpdatePropertyMethod].Run( | 616 method_callback_map_[ibus::panel::kUpdatePropertyMethod].Run( |
619 &method_call, | 617 &method_call, |
620 base::Bind(&MockResponseSender::Run, | 618 base::Bind(&MockResponseSender::Run, |
621 base::Unretained(&response_sender))); | 619 base::Unretained(&response_sender))); |
622 } | 620 } |
623 | 621 |
624 } // namespace ibus | |
625 } // namespace chromeos | 622 } // namespace chromeos |
OLD | NEW |