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/threading/thread.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 TEST(UserInputMonitorTest, CreatePlatformSpecific) { | |
88 base::Thread io_thread("UserInputMonitorTestIOThread"); | |
DaleCurtis
2013/08/29 19:02:42
Do you actually need these threads? Can you just c
jiayl
2013/08/29 21:05:40
Done.
| |
89 io_thread.StartWithOptions( | |
90 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
91 base::Thread ui_thread("UserInputMonitorTestUIThread"); | |
92 ui_thread.StartWithOptions( | |
93 base::Thread::Options(base::MessageLoop::TYPE_UI, 0)); | |
94 | |
95 scoped_ptr<UserInputMonitor> monitor = UserInputMonitor::Create( | |
96 io_thread.message_loop_proxy(), ui_thread.message_loop_proxy()); | |
97 | |
98 MockMouseListener listener; | |
99 // Ignore any callbacks. | |
100 EXPECT_CALL(listener, OnMouseMoved(testing::_)).Times(testing::AnyNumber()); | |
101 | |
102 #if defined(OS_MACOSX) | |
DaleCurtis
2013/08/29 19:02:42
!defined ?
jiayl
2013/08/29 21:05:40
Done.
| |
103 monitor->AddMouseListener(&listener); | |
104 monitor->RemoveMouseListener(&listener); | |
105 #endif // define(OS_MACOSX) | |
106 | |
107 monitor->EnableKeyPressMonitoring(); | |
108 monitor->DisableKeyPressMonitoring(); | |
109 | |
110 // Must stop the threads before destroying |monitor|. | |
111 ui_thread.Stop(); | |
112 io_thread.Stop(); | |
113 } | |
114 | |
115 } // namespace media | |
OLD | NEW |