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.
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"
18 class MockMouseListener
: public UserInputMonitor::MouseEventListener
{
20 MOCK_METHOD1(OnMouseMoved
, void(const SkIPoint
& position
));
22 virtual ~MockMouseListener() {}
25 #if defined(OS_LINUX) || defined(OS_WIN)
26 TEST(UserInputMonitorTest
, KeyPressCounter
) {
27 KeyboardEventCounter counter
;
29 EXPECT_EQ(0u, counter
.GetKeyPressCount());
31 counter
.OnKeyboardEvent(ui::ET_KEY_PRESSED
, ui::VKEY_0
);
32 EXPECT_EQ(1u, counter
.GetKeyPressCount());
34 // Holding the same key without releasing it does not increase the count.
35 counter
.OnKeyboardEvent(ui::ET_KEY_PRESSED
, ui::VKEY_0
);
36 EXPECT_EQ(1u, counter
.GetKeyPressCount());
38 // Releasing the key does not affect the total count.
39 counter
.OnKeyboardEvent(ui::ET_KEY_RELEASED
, ui::VKEY_0
);
40 EXPECT_EQ(1u, counter
.GetKeyPressCount());
42 counter
.OnKeyboardEvent(ui::ET_KEY_PRESSED
, ui::VKEY_0
);
43 counter
.OnKeyboardEvent(ui::ET_KEY_RELEASED
, ui::VKEY_0
);
44 EXPECT_EQ(2u, counter
.GetKeyPressCount());
46 #endif // defined(OS_LINUX) || defined(OS_WIN)
48 TEST(UserInputMonitorTest
, CreatePlatformSpecific
) {
50 base::MessageLoopForIO message_loop
;
52 base::MessageLoopForUI message_loop
;
53 #endif // defined(OS_LINUX)
55 base::RunLoop run_loop
;
56 scoped_ptr
<UserInputMonitor
> monitor
= UserInputMonitor::Create(
57 message_loop
.task_runner(), message_loop
.task_runner());
62 MockMouseListener listener
;
63 // Ignore any callbacks.
64 EXPECT_CALL(listener
, OnMouseMoved(testing::_
)).Times(testing::AnyNumber());
66 #if !defined(OS_MACOSX)
67 monitor
->AddMouseListener(&listener
);
68 monitor
->RemoveMouseListener(&listener
);
69 #endif // !define(OS_MACOSX)
71 monitor
->EnableKeyPressMonitoring();
72 monitor
->DisableKeyPressMonitoring();
75 run_loop
.RunUntilIdle();