summaryrefslogtreecommitdiffstats
path: root/media/base/user_input_monitor_unittest.cc
blob: ab717d1a429b35afc78a08b8bb8094c4ef92a25e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "media/base/keyboard_event_counter.h"
#include "media/base/user_input_monitor.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkPoint.h"

namespace media {

class MockMouseListener : public UserInputMonitor::MouseEventListener {
 public:
  MOCK_METHOD1(OnMouseMoved, void(const SkIPoint& position));

  virtual ~MockMouseListener() {}
};

#if defined(OS_LINUX) || defined(OS_WIN)
TEST(UserInputMonitorTest, KeyPressCounter) {
  KeyboardEventCounter counter;

  EXPECT_EQ(0u, counter.GetKeyPressCount());

  counter.OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0);
  EXPECT_EQ(1u, counter.GetKeyPressCount());

  // Holding the same key without releasing it does not increase the count.
  counter.OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0);
  EXPECT_EQ(1u, counter.GetKeyPressCount());

  // Releasing the key does not affect the total count.
  counter.OnKeyboardEvent(ui::ET_KEY_RELEASED, ui::VKEY_0);
  EXPECT_EQ(1u, counter.GetKeyPressCount());

  counter.OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_0);
  counter.OnKeyboardEvent(ui::ET_KEY_RELEASED, ui::VKEY_0);
  EXPECT_EQ(2u, counter.GetKeyPressCount());
}
#endif  // defined(OS_LINUX) || defined(OS_WIN)

TEST(UserInputMonitorTest, CreatePlatformSpecific) {
#if defined(OS_LINUX)
  base::MessageLoopForIO message_loop;
#else
  base::MessageLoopForUI message_loop;
#endif  // defined(OS_LINUX)

  base::RunLoop run_loop;
  scoped_ptr<UserInputMonitor> monitor = UserInputMonitor::Create(
      message_loop.task_runner(), message_loop.task_runner());

  if (!monitor)
    return;

  MockMouseListener listener;
  // Ignore any callbacks.
  EXPECT_CALL(listener, OnMouseMoved(testing::_)).Times(testing::AnyNumber());

#if !defined(OS_MACOSX)
  monitor->AddMouseListener(&listener);
  monitor->RemoveMouseListener(&listener);
#endif  // !define(OS_MACOSX)

  monitor->EnableKeyPressMonitoring();
  monitor->DisableKeyPressMonitoring();

  monitor.reset();
  run_loop.RunUntilIdle();
}

}  // namespace media