| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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 "base/basictypes.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "media/base/keyboard_event_counter.h" |
| 11 #include "media/base/user_input_monitor.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/skia/include/core/SkPoint.h" |
| 15 |
| 16 namespace media { |
| 17 |
| 18 class MockMouseListener : public UserInputMonitor::MouseEventListener { |
| 19 public: |
| 20 MOCK_METHOD1(OnMouseMoved, void(const SkIPoint& position)); |
| 21 |
| 22 virtual ~MockMouseListener() {} |
| 23 }; |
| 24 |
| 25 class UserInputMonitorForTest : public UserInputMonitor { |
| 26 public: |
| 27 UserInputMonitorForTest() {} |
| 28 virtual ~UserInputMonitorForTest() {} |
| 29 |
| 30 virtual size_t GetKeyPressCount() const OVERRIDE { |
| 31 return counter_.GetKeyPressCount(); |
| 32 } |
| 33 |
| 34 void EmulateMouseEvent(const SkIPoint& position) { |
| 35 UserInputMonitor::OnMouseEvent(position); |
| 36 } |
| 37 |
| 38 void EmulateKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code) { |
| 39 counter_.OnKeyboardEvent(event, key_code); |
| 40 } |
| 41 |
| 42 private: |
| 43 virtual void StartMouseMonitoring() OVERRIDE {} |
| 44 virtual void StopMouseMonitoring() OVERRIDE {} |
| 45 virtual void StartKeyboardMonitoring() OVERRIDE { counter_.Reset(); } |
| 46 virtual void StopKeyboardMonitoring() OVERRIDE {} |
| 47 |
| 48 KeyboardEventCounter counter_; |
| 49 }; |
| 50 |
| 51 TEST(UserInputMonitorTest, MouseEvent) { |
| 52 UserInputMonitorForTest monitor; |
| 53 MockMouseListener listener; |
| 54 |
| 55 monitor.AddMouseListener(&listener); |
| 56 |
| 57 SkIPoint pos(SkIPoint::Make(100, 100)); |
| 58 EXPECT_CALL(listener, OnMouseMoved(pos)); |
| 59 monitor.EmulateMouseEvent(pos); |
| 60 |
| 61 monitor.RemoveMouseListener(&listener); |
| 62 } |
| 63 |
| 64 TEST(UserInputMonitorTest, KeyPressCounter) { |
| 65 UserInputMonitorForTest monitor; |
| 66 |
| 67 monitor.EnableKeyPressMonitoring(); |
| 68 EXPECT_EQ(0u, monitor.GetKeyPressCount()); |
| 69 |
| 70 monitor.EmulateKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0); |
| 71 EXPECT_EQ(1u, monitor.GetKeyPressCount()); |
| 72 |
| 73 // Holding the same key without releasing it does not increase the count. |
| 74 monitor.EmulateKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0); |
| 75 EXPECT_EQ(1u, monitor.GetKeyPressCount()); |
| 76 |
| 77 // Releasing the key does not affect the total count. |
| 78 monitor.EmulateKeyboardEvent(ui::ET_KEY_RELEASED, ui::VKEY_0); |
| 79 EXPECT_EQ(1u, monitor.GetKeyPressCount()); |
| 80 |
| 81 monitor.EmulateKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0); |
| 82 monitor.EmulateKeyboardEvent(ui::ET_KEY_RELEASED, ui::VKEY_0); |
| 83 EXPECT_EQ(2u, monitor.GetKeyPressCount()); |
| 84 monitor.DisableKeyPressMonitoring(); |
| 85 } |
| 86 |
| 87 #ifndef DISABLE_USER_INPUT_MONITOR |
| 88 TEST(UserInputMonitorTest, CreatePlatformSpecific) { |
| 89 #if defined(OS_LINUX) |
| 90 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 91 #else |
| 92 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 93 #endif // defined(OS_LINUX) |
| 94 |
| 95 base::RunLoop run_loop_; |
| 96 scoped_ptr<UserInputMonitor> monitor = UserInputMonitor::Create( |
| 97 message_loop.message_loop_proxy(), message_loop.message_loop_proxy()); |
| 98 |
| 99 MockMouseListener listener; |
| 100 // Ignore any callbacks. |
| 101 EXPECT_CALL(listener, OnMouseMoved(testing::_)).Times(testing::AnyNumber()); |
| 102 |
| 103 #if !defined(OS_MACOSX) |
| 104 monitor->AddMouseListener(&listener); |
| 105 monitor->RemoveMouseListener(&listener); |
| 106 #endif // !define(OS_MACOSX) |
| 107 |
| 108 monitor->EnableKeyPressMonitoring(); |
| 109 monitor->DisableKeyPressMonitoring(); |
| 110 |
| 111 // Must wait for all tasks to finish before destroying |monitor|. |
| 112 run_loop_.RunUntilIdle(); |
| 113 } |
| 114 #endif // DISABLE_USER_INPUT_MONITOR |
| 115 |
| 116 } // namespace media |
| OLD | NEW |