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
16namespace media {
17
18class MockMouseListener : public UserInputMonitor::MouseEventListener {
19 public:
20  MOCK_METHOD1(OnMouseMoved, void(const SkIPoint& position));
21
22  virtual ~MockMouseListener() {}
23};
24
25#if defined(OS_LINUX) || defined(OS_WIN)
26TEST(UserInputMonitorTest, KeyPressCounter) {
27  KeyboardEventCounter counter;
28
29  EXPECT_EQ(0u, counter.GetKeyPressCount());
30
31  counter.OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0);
32  EXPECT_EQ(1u, counter.GetKeyPressCount());
33
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());
37
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());
41
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());
45}
46#endif  // defined(OS_LINUX) || defined(OS_WIN)
47
48TEST(UserInputMonitorTest, CreatePlatformSpecific) {
49#if defined(OS_LINUX)
50  base::MessageLoopForIO message_loop;
51#else
52  base::MessageLoopForUI message_loop;
53#endif  // defined(OS_LINUX)
54
55  base::RunLoop run_loop;
56  scoped_ptr<UserInputMonitor> monitor = UserInputMonitor::Create(
57      message_loop.message_loop_proxy(), message_loop.message_loop_proxy());
58
59  if (!monitor)
60    return;
61
62  MockMouseListener listener;
63  // Ignore any callbacks.
64  EXPECT_CALL(listener, OnMouseMoved(testing::_)).Times(testing::AnyNumber());
65
66#if !defined(OS_MACOSX)
67  monitor->AddMouseListener(&listener);
68  monitor->RemoveMouseListener(&listener);
69#endif  // !define(OS_MACOSX)
70
71  monitor->EnableKeyPressMonitoring();
72  monitor->DisableKeyPressMonitoring();
73
74  monitor.reset();
75  run_loop.RunUntilIdle();
76}
77
78}  // namespace media
79