| 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_input_context_client.h" | 5 #include "chromeos/dbus/ibus/ibus_input_context_client.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chromeos/dbus/ibus/ibus_constants.h" | 10 #include "chromeos/dbus/ibus/ibus_constants.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 // Following variables are used in callback expectations. | 30 // Following variables are used in callback expectations. |
| 31 const uint32 kCapabilities = 12345; | 31 const uint32 kCapabilities = 12345; |
| 32 const int32 kCursorX = 30; | 32 const int32 kCursorX = 30; |
| 33 const int32 kCursorY = 31; | 33 const int32 kCursorY = 31; |
| 34 const int32 kCursorWidth = 32; | 34 const int32 kCursorWidth = 32; |
| 35 const int32 kCursorHeight = 33; | 35 const int32 kCursorHeight = 33; |
| 36 const uint32 kKeyval = 34; | 36 const uint32 kKeyval = 34; |
| 37 const uint32 kKeycode = 35; | 37 const uint32 kKeycode = 35; |
| 38 const uint32 kState = 36; | 38 const uint32 kState = 36; |
| 39 const int32 kCompositionX = 37; |
| 40 const int32 kCompositionY = 38; |
| 41 const int32 kCompositionWidth = 39; |
| 42 const int32 kCompositionHeight = 40; |
| 39 const bool kIsKeyHandled = false; | 43 const bool kIsKeyHandled = false; |
| 40 const char kSurroundingText[] = "Surrounding Text"; | 44 const char kSurroundingText[] = "Surrounding Text"; |
| 41 const uint32 kCursorPos = 2; | 45 const uint32 kCursorPos = 2; |
| 42 const uint32 kAnchorPos = 7; | 46 const uint32 kAnchorPos = 7; |
| 43 const char kPropertyKey[] = "Property Key"; | 47 const char kPropertyKey[] = "Property Key"; |
| 44 const ibus::IBusPropertyState kPropertyState = | 48 const ibus::IBusPropertyState kPropertyState = |
| 45 ibus::IBUS_PROPERTY_STATE_CHECKED; | 49 ibus::IBUS_PROPERTY_STATE_CHECKED; |
| 46 | 50 |
| 47 class MockInputContextHandler : public IBusInputContextHandlerInterface { | 51 class MockInputContextHandler : public IBusInputContextHandlerInterface { |
| 48 public: | 52 public: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 | 71 |
| 68 MATCHER_P(IBusTextEq, expected_text, "The expected IBusText does not match") { | 72 MATCHER_P(IBusTextEq, expected_text, "The expected IBusText does not match") { |
| 69 // TODO(nona): Check attributes. | 73 // TODO(nona): Check attributes. |
| 70 return (arg.text() == expected_text->text()); | 74 return (arg.text() == expected_text->text()); |
| 71 } | 75 } |
| 72 | 76 |
| 73 } // namespace | 77 } // namespace |
| 74 | 78 |
| 75 class IBusInputContextClientTest : public testing::Test { | 79 class IBusInputContextClientTest : public testing::Test { |
| 76 public: | 80 public: |
| 77 IBusInputContextClientTest() : response_(NULL) {} | 81 IBusInputContextClientTest() |
| 82 : response_(NULL), |
| 83 on_set_cursor_location_call_count_(0) {} |
| 78 | 84 |
| 79 virtual void SetUp() OVERRIDE { | 85 virtual void SetUp() OVERRIDE { |
| 80 // Create a mock bus. | 86 // Create a mock bus. |
| 81 dbus::Bus::Options options; | 87 dbus::Bus::Options options; |
| 82 options.bus_type = dbus::Bus::SYSTEM; | 88 options.bus_type = dbus::Bus::SYSTEM; |
| 83 mock_bus_ = new dbus::MockBus(options); | 89 mock_bus_ = new dbus::MockBus(options); |
| 84 | 90 |
| 85 // Create a mock proxy. | 91 // Create a mock proxy. |
| 86 mock_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), | 92 mock_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), |
| 87 ibus::kServiceName, | 93 ibus::kServiceName, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 EXPECT_EQ(ibus::input_context::kServiceInterface, | 179 EXPECT_EQ(ibus::input_context::kServiceInterface, |
| 174 method_call->GetInterface()); | 180 method_call->GetInterface()); |
| 175 EXPECT_EQ(ibus::input_context::kResetMethod, method_call->GetMember()); | 181 EXPECT_EQ(ibus::input_context::kResetMethod, method_call->GetMember()); |
| 176 dbus::MessageReader reader(method_call); | 182 dbus::MessageReader reader(method_call); |
| 177 EXPECT_FALSE(reader.HasMoreData()); | 183 EXPECT_FALSE(reader.HasMoreData()); |
| 178 | 184 |
| 179 message_loop_.PostTask(FROM_HERE, base::Bind(callback, response_)); | 185 message_loop_.PostTask(FROM_HERE, base::Bind(callback, response_)); |
| 180 } | 186 } |
| 181 | 187 |
| 182 // Handles SetCursorLocation method call. | 188 // Handles SetCursorLocation method call. |
| 183 void OnSetCursorLocation( | 189 void OnSetCursorLocation(const ibus::Rect& cursor_location, |
| 184 dbus::MethodCall* method_call, | 190 const ibus::Rect& composition_head) { |
| 185 int timeout_ms, | 191 ++on_set_cursor_location_call_count_; |
| 186 const dbus::ObjectProxy::ResponseCallback& callback, | |
| 187 const dbus::ObjectProxy::ErrorCallback& error_callback) { | |
| 188 EXPECT_EQ(ibus::input_context::kServiceInterface, | |
| 189 method_call->GetInterface()); | |
| 190 EXPECT_EQ(ibus::input_context::kSetCursorLocationMethod, | |
| 191 method_call->GetMember()); | |
| 192 dbus::MessageReader reader(method_call); | |
| 193 int32 x, y, width, height; | |
| 194 EXPECT_TRUE(reader.PopInt32(&x)); | |
| 195 EXPECT_TRUE(reader.PopInt32(&y)); | |
| 196 EXPECT_TRUE(reader.PopInt32(&width)); | |
| 197 EXPECT_TRUE(reader.PopInt32(&height)); | |
| 198 EXPECT_FALSE(reader.HasMoreData()); | |
| 199 | |
| 200 message_loop_.PostTask(FROM_HERE, base::Bind(callback, response_)); | |
| 201 } | 192 } |
| 202 | 193 |
| 203 // Handles SetCapabilities method call. | 194 // Handles SetCapabilities method call. |
| 204 void OnSetCapabilities( | 195 void OnSetCapabilities( |
| 205 dbus::MethodCall* method_call, | 196 dbus::MethodCall* method_call, |
| 206 int timeout_ms, | 197 int timeout_ms, |
| 207 const dbus::ObjectProxy::ResponseCallback& callback, | 198 const dbus::ObjectProxy::ResponseCallback& callback, |
| 208 const dbus::ObjectProxy::ErrorCallback& error_callback) { | 199 const dbus::ObjectProxy::ErrorCallback& error_callback) { |
| 209 EXPECT_EQ(ibus::input_context::kServiceInterface, | 200 EXPECT_EQ(ibus::input_context::kServiceInterface, |
| 210 method_call->GetInterface()); | 201 method_call->GetInterface()); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 scoped_refptr<dbus::MockBus> mock_bus_; | 308 scoped_refptr<dbus::MockBus> mock_bus_; |
| 318 // The mock object proxy. | 309 // The mock object proxy. |
| 319 scoped_refptr<dbus::MockObjectProxy> mock_proxy_; | 310 scoped_refptr<dbus::MockObjectProxy> mock_proxy_; |
| 320 // Response returned by mock methods. | 311 // Response returned by mock methods. |
| 321 dbus::Response* response_; | 312 dbus::Response* response_; |
| 322 dbus::ErrorResponse* error_response_; | 313 dbus::ErrorResponse* error_response_; |
| 323 // A message loop to emulate asynchronous behavior. | 314 // A message loop to emulate asynchronous behavior. |
| 324 MessageLoop message_loop_; | 315 MessageLoop message_loop_; |
| 325 // The map from signal to signal handler. | 316 // The map from signal to signal handler. |
| 326 std::map<std::string, dbus::ObjectProxy::SignalCallback> signal_callback_map_; | 317 std::map<std::string, dbus::ObjectProxy::SignalCallback> signal_callback_map_; |
| 318 // Call count of OnSetCursorLocation. |
| 319 int on_set_cursor_location_call_count_; |
| 327 | 320 |
| 328 private: | 321 private: |
| 329 // Used to implement the mock proxy. | 322 // Used to implement the mock proxy. |
| 330 void OnConnectToSignal( | 323 void OnConnectToSignal( |
| 331 const std::string& interface_name, | 324 const std::string& interface_name, |
| 332 const std::string& signal_name, | 325 const std::string& signal_name, |
| 333 const dbus::ObjectProxy::SignalCallback& signal_callback, | 326 const dbus::ObjectProxy::SignalCallback& signal_callback, |
| 334 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { | 327 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
| 335 signal_callback_map_[signal_name] = signal_callback; | 328 signal_callback_map_[signal_name] = signal_callback; |
| 336 const bool success = true; | 329 const bool success = true; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 512 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 520 response_ = response.get(); | 513 response_ = response.get(); |
| 521 | 514 |
| 522 // Call SetCapabilities. | 515 // Call SetCapabilities. |
| 523 client_->SetCapabilities(kCapabilities); | 516 client_->SetCapabilities(kCapabilities); |
| 524 // Run the message loop. | 517 // Run the message loop. |
| 525 message_loop_.RunUntilIdle(); | 518 message_loop_.RunUntilIdle(); |
| 526 } | 519 } |
| 527 | 520 |
| 528 TEST_F(IBusInputContextClientTest, SetCursorLocationTest) { | 521 TEST_F(IBusInputContextClientTest, SetCursorLocationTest) { |
| 529 // Set expectations. | 522 on_set_cursor_location_call_count_ = 0; |
| 530 EXPECT_CALL(*mock_proxy_, CallMethodWithErrorCallback(_, _, _, _)) | 523 client_->SetSetCursorLocationHandler( |
| 531 .WillOnce(Invoke(this, | 524 base::Bind(&IBusInputContextClientTest::OnSetCursorLocation, |
| 532 &IBusInputContextClientTest::OnSetCursorLocation)); | 525 base::Unretained(this))); |
| 533 // Create response. | 526 const ibus::Rect cursor_location(kCursorX, |
| 534 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 527 kCursorY, |
| 535 response_ = response.get(); | 528 kCursorWidth, |
| 529 kCursorHeight); |
| 530 const ibus::Rect composition_location(kCompositionX, |
| 531 kCompositionY, |
| 532 kCompositionWidth, |
| 533 kCompositionHeight); |
| 534 // Call SetCursorLocation. |
| 535 client_->SetCursorLocation(cursor_location, composition_location); |
| 536 | 536 |
| 537 // Call SetCursorLocation. | 537 EXPECT_EQ(1, on_set_cursor_location_call_count_); |
| 538 client_->SetCursorLocation(kCursorX, kCursorY, kCursorWidth, kCursorHeight); | 538 client_->UnsetSetCursorLocationHandler(); |
| 539 // Run the message loop. | |
| 540 message_loop_.RunUntilIdle(); | |
| 541 } | 539 } |
| 542 | 540 |
| 543 TEST_F(IBusInputContextClientTest, OnProcessKeyEvent) { | 541 TEST_F(IBusInputContextClientTest, OnProcessKeyEvent) { |
| 544 // Set expectations. | 542 // Set expectations. |
| 545 EXPECT_CALL(*mock_proxy_, CallMethodWithErrorCallback(_, _, _, _)) | 543 EXPECT_CALL(*mock_proxy_, CallMethodWithErrorCallback(_, _, _, _)) |
| 546 .WillOnce(Invoke(this, &IBusInputContextClientTest::OnProcessKeyEvent)); | 544 .WillOnce(Invoke(this, &IBusInputContextClientTest::OnProcessKeyEvent)); |
| 547 MockProcessKeyEventHandler callback; | 545 MockProcessKeyEventHandler callback; |
| 548 MockProcessKeyEventErrorHandler error_callback; | 546 MockProcessKeyEventErrorHandler error_callback; |
| 549 | 547 |
| 550 EXPECT_CALL(callback, Run(kIsKeyHandled)); | 548 EXPECT_CALL(callback, Run(kIsKeyHandled)); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 // Create response. | 616 // Create response. |
| 619 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 617 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 620 response_ = response.get(); | 618 response_ = response.get(); |
| 621 | 619 |
| 622 // Call SetCursorLocation. | 620 // Call SetCursorLocation. |
| 623 client_->PropertyActivate(kPropertyKey, kPropertyState); | 621 client_->PropertyActivate(kPropertyKey, kPropertyState); |
| 624 // Run the message loop. | 622 // Run the message loop. |
| 625 message_loop_.RunUntilIdle(); | 623 message_loop_.RunUntilIdle(); |
| 626 } | 624 } |
| 627 } // namespace chromeos | 625 } // namespace chromeos |
| OLD | NEW |