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