OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "components/autofill/content/browser/key_press_handler_manager.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "content/public/browser/native_web_keyboard_event.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace autofill { |
| 14 |
| 15 namespace { |
| 16 |
| 17 const content::NativeWebKeyboardEvent |
| 18 kDummyEvent(blink::WebInputEvent::Undefined, 0, 0); |
| 19 |
| 20 // Dummy keyboard event handler: ignores the event, but appends the given |name| |
| 21 // to a logging |target|. |
| 22 bool DummyHandler(const char* name, |
| 23 std::string* target, |
| 24 const content::NativeWebKeyboardEvent& /*event*/) { |
| 25 target->append(name); |
| 26 return false; |
| 27 } |
| 28 |
| 29 // A delegate which logs the handlers instead of adding and removing them. |
| 30 class LoggingDelegate : public KeyPressHandlerManager::Delegate { |
| 31 public: |
| 32 explicit LoggingDelegate(std::string* target) : target_(target) {} |
| 33 |
| 34 // KeyPressHandlerManager::Delegate: |
| 35 void AddHandler(const content::RenderWidgetHost::KeyPressEventCallback& |
| 36 handler) override { |
| 37 target_->append("A"); |
| 38 handler.Run(kDummyEvent); |
| 39 } |
| 40 void RemoveHandler(const content::RenderWidgetHost::KeyPressEventCallback& |
| 41 handler) override { |
| 42 target_->append("R"); |
| 43 handler.Run(kDummyEvent); |
| 44 } |
| 45 |
| 46 private: |
| 47 std::string* const target_; |
| 48 }; |
| 49 |
| 50 } // namespace |
| 51 |
| 52 class KeyPressHandlerManagerTest : public testing::Test { |
| 53 public: |
| 54 KeyPressHandlerManagerTest() : delegate_(&callback_trace_) {} |
| 55 |
| 56 protected: |
| 57 content::RenderWidgetHost::KeyPressEventCallback CallbackForName( |
| 58 const char* name) { |
| 59 return base::Bind(DummyHandler, name, &callback_trace_); |
| 60 } |
| 61 |
| 62 // String encoding the events related to adding and removing callbacks. For |
| 63 // example, "A1R1A2R2" means that callback "1" was added, then removed, and |
| 64 // then callback "2" was added and removed. |
| 65 std::string callback_trace_; |
| 66 |
| 67 LoggingDelegate delegate_; |
| 68 }; |
| 69 |
| 70 // Removing should remove the previously added callback. |
| 71 TEST_F(KeyPressHandlerManagerTest, AddRemove) { |
| 72 KeyPressHandlerManager manager(&delegate_); |
| 73 manager.RegisterKeyPressHandler(CallbackForName("1")); |
| 74 manager.RemoveKeyPressHandler(); |
| 75 EXPECT_EQ("A1R1", callback_trace_); |
| 76 } |
| 77 |
| 78 // Registering a new callback should remove the old one. |
| 79 TEST_F(KeyPressHandlerManagerTest, AddReplace) { |
| 80 KeyPressHandlerManager manager(&delegate_); |
| 81 manager.RegisterKeyPressHandler(CallbackForName("1")); |
| 82 manager.RegisterKeyPressHandler(CallbackForName("2")); |
| 83 manager.RemoveKeyPressHandler(); |
| 84 EXPECT_EQ("A1R1A2R2", callback_trace_); |
| 85 } |
| 86 |
| 87 // Without registering a callback first, none should be removed. |
| 88 TEST_F(KeyPressHandlerManagerTest, NoRemove) { |
| 89 KeyPressHandlerManager manager(&delegate_); |
| 90 manager.RemoveKeyPressHandler(); |
| 91 EXPECT_EQ(std::string(), callback_trace_); |
| 92 } |
| 93 |
| 94 // Adding one callback twice should be a no-op. |
| 95 TEST_F(KeyPressHandlerManagerTest, AddTwice) { |
| 96 KeyPressHandlerManager manager(&delegate_); |
| 97 auto callback = CallbackForName("1"); |
| 98 manager.RegisterKeyPressHandler(callback); |
| 99 manager.RegisterKeyPressHandler(callback); |
| 100 manager.RemoveKeyPressHandler(); |
| 101 EXPECT_EQ("A1R1", callback_trace_); |
| 102 } |
| 103 |
| 104 // Removing one callback twice should be a no-op. |
| 105 TEST_F(KeyPressHandlerManagerTest, RemoveTwice) { |
| 106 KeyPressHandlerManager manager(&delegate_); |
| 107 manager.RegisterKeyPressHandler(CallbackForName("1")); |
| 108 manager.RemoveKeyPressHandler(); |
| 109 manager.RemoveKeyPressHandler(); |
| 110 EXPECT_EQ("A1R1", callback_trace_); |
| 111 } |
| 112 |
| 113 } // namespace autofill |
OLD | NEW |