| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "media/base/user_input_monitor.h" | 5 #include "media/base/user_input_monitor.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "media/base/keyboard_event_counter.h" | 13 #include "media/base/keyboard_event_counter.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/skia/include/core/SkPoint.h" | 16 #include "third_party/skia/include/core/SkPoint.h" |
| 17 | 17 |
| 18 #if defined(OS_LINUX) | 18 #if defined(OS_LINUX) |
| 19 #include "base/files/file_descriptor_watcher_posix.h" | 19 #include "base/files/file_descriptor_watcher_posix.h" |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 namespace media { | 22 namespace media { |
| 23 | 23 |
| 24 class MockMouseListener : public UserInputMonitor::MouseEventListener { | |
| 25 public: | |
| 26 MOCK_METHOD1(OnMouseMoved, void(const SkIPoint& position)); | |
| 27 | |
| 28 virtual ~MockMouseListener() {} | |
| 29 }; | |
| 30 | |
| 31 #if defined(OS_LINUX) || defined(OS_WIN) | |
| 32 TEST(UserInputMonitorTest, KeyPressCounter) { | |
| 33 KeyboardEventCounter counter; | |
| 34 | |
| 35 EXPECT_EQ(0u, counter.GetKeyPressCount()); | |
| 36 | |
| 37 counter.OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0); | |
| 38 EXPECT_EQ(1u, counter.GetKeyPressCount()); | |
| 39 | |
| 40 // Holding the same key without releasing it does not increase the count. | |
| 41 counter.OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0); | |
| 42 EXPECT_EQ(1u, counter.GetKeyPressCount()); | |
| 43 | |
| 44 // Releasing the key does not affect the total count. | |
| 45 counter.OnKeyboardEvent(ui::ET_KEY_RELEASED, ui::VKEY_0); | |
| 46 EXPECT_EQ(1u, counter.GetKeyPressCount()); | |
| 47 | |
| 48 counter.OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0); | |
| 49 counter.OnKeyboardEvent(ui::ET_KEY_RELEASED, ui::VKEY_0); | |
| 50 EXPECT_EQ(2u, counter.GetKeyPressCount()); | |
| 51 } | |
| 52 #endif // defined(OS_LINUX) || defined(OS_WIN) | |
| 53 | |
| 54 TEST(UserInputMonitorTest, CreatePlatformSpecific) { | 24 TEST(UserInputMonitorTest, CreatePlatformSpecific) { |
| 55 #if defined(OS_LINUX) | 25 #if defined(OS_LINUX) |
| 56 base::MessageLoopForIO message_loop; | 26 base::MessageLoopForIO message_loop; |
| 57 base::FileDescriptorWatcher file_descriptor_watcher(&message_loop); | 27 base::FileDescriptorWatcher file_descriptor_watcher(&message_loop); |
| 58 #else | 28 #else |
| 59 base::MessageLoopForUI message_loop; | 29 base::MessageLoopForUI message_loop; |
| 60 #endif // defined(OS_LINUX) | 30 #endif // defined(OS_LINUX) |
| 61 | 31 |
| 62 base::RunLoop run_loop; | 32 base::RunLoop run_loop; |
| 63 std::unique_ptr<UserInputMonitor> monitor = UserInputMonitor::Create( | 33 std::unique_ptr<UserInputMonitor> monitor = UserInputMonitor::Create( |
| 64 message_loop.task_runner(), message_loop.task_runner()); | 34 message_loop.task_runner(), message_loop.task_runner()); |
| 65 | 35 |
| 66 if (!monitor) | 36 if (!monitor) |
| 67 return; | 37 return; |
| 68 | 38 |
| 69 MockMouseListener listener; | 39 MockMouseListener listener; |
| 70 // Ignore any callbacks. | 40 // Ignore any callbacks. |
| 71 EXPECT_CALL(listener, OnMouseMoved(testing::_)).Times(testing::AnyNumber()); | 41 EXPECT_CALL(listener, OnMouseMoved(testing::_)).Times(testing::AnyNumber()); |
| 72 | 42 |
| 73 #if !defined(OS_MACOSX) | |
| 74 monitor->AddMouseListener(&listener); | |
| 75 monitor->RemoveMouseListener(&listener); | |
| 76 #endif // !define(OS_MACOSX) | |
| 77 | |
| 78 monitor->EnableKeyPressMonitoring(); | 43 monitor->EnableKeyPressMonitoring(); |
| 79 monitor->DisableKeyPressMonitoring(); | 44 monitor->DisableKeyPressMonitoring(); |
| 80 | 45 |
| 81 monitor.reset(); | 46 monitor.reset(); |
| 82 run_loop.RunUntilIdle(); | 47 run_loop.RunUntilIdle(); |
| 83 } | 48 } |
| 84 | 49 |
| 85 } // namespace media | 50 } // namespace media |
| OLD | NEW |