chromium/media/base/user_input_monitor_unittest.cc

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/user_input_monitor.h"

#include <memory>
#include <utility>

#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include "base/files/file_descriptor_watcher_posix.h"
#endif

#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_platform.h"  // nogncheck
#endif

#if BUILDFLAG(IS_WIN)
#include "ui/events/test/keyboard_hook_monitor_utils.h"
#endif

namespace media {

namespace {

class UserInputMonitorTest : public testing::Test {};

}  // namespace

TEST_F(UserInputMonitorTest, CreatePlatformSpecific) {}

TEST_F(UserInputMonitorTest, CreatePlatformSpecificWithMapping) {}

TEST_F(UserInputMonitorTest, ReadWriteKeyPressMonitorCount) {}

#if BUILDFLAG(IS_WIN)

//
// Windows specific scenarios which require simulating keyboard hook events.
//

TEST_F(UserInputMonitorTest, BlockMonitoringAfterMonitoringEnabled) {
  base::test::TaskEnvironment task_environment(
      base::test::TaskEnvironment::MainThreadType::UI);

  std::unique_ptr<UserInputMonitor> monitor = UserInputMonitor::Create(
      base::SingleThreadTaskRunner::GetCurrentDefault(),
      base::SingleThreadTaskRunner::GetCurrentDefault());

  if (!monitor)
    return;

  monitor->EnableKeyPressMonitoring();
  ui::SimulateKeyboardHookRegistered();
  ui::SimulateKeyboardHookUnregistered();
  monitor->DisableKeyPressMonitoring();

  monitor.reset();
  base::RunLoop().RunUntilIdle();
}

TEST_F(UserInputMonitorTest, BlockMonitoringBeforeMonitoringEnabled) {
  base::test::TaskEnvironment task_environment(
      base::test::TaskEnvironment::MainThreadType::UI);

  std::unique_ptr<UserInputMonitor> monitor = UserInputMonitor::Create(
      base::SingleThreadTaskRunner::GetCurrentDefault(),
      base::SingleThreadTaskRunner::GetCurrentDefault());

  if (!monitor)
    return;

  ui::SimulateKeyboardHookRegistered();
  monitor->EnableKeyPressMonitoring();
  ui::SimulateKeyboardHookUnregistered();
  monitor->DisableKeyPressMonitoring();

  monitor.reset();
  base::RunLoop().RunUntilIdle();
}

TEST_F(UserInputMonitorTest, UnblockMonitoringAfterMonitoringDisabled) {
  base::test::TaskEnvironment task_environment(
      base::test::TaskEnvironment::MainThreadType::UI);

  std::unique_ptr<UserInputMonitor> monitor = UserInputMonitor::Create(
      base::SingleThreadTaskRunner::GetCurrentDefault(),
      base::SingleThreadTaskRunner::GetCurrentDefault());

  if (!monitor)
    return;

  monitor->EnableKeyPressMonitoring();
  ui::SimulateKeyboardHookRegistered();
  monitor->DisableKeyPressMonitoring();
  ui::SimulateKeyboardHookUnregistered();

  monitor.reset();
  base::RunLoop().RunUntilIdle();
}

TEST_F(UserInputMonitorTest, BlockKeypressMonitoringWithSharedMemoryBuffer) {
  base::test::TaskEnvironment task_environment(
      base::test::TaskEnvironment::MainThreadType::UI);

  std::unique_ptr<UserInputMonitor> monitor = UserInputMonitor::Create(
      base::SingleThreadTaskRunner::GetCurrentDefault(),
      base::SingleThreadTaskRunner::GetCurrentDefault());

  if (!monitor)
    return;

  base::ReadOnlySharedMemoryMapping readonly_mapping =
      static_cast<UserInputMonitorBase*>(monitor.get())
          ->EnableKeyPressMonitoringWithMapping()
          .Map();
  EXPECT_EQ(0u, ReadKeyPressMonitorCount(readonly_mapping));
  ui::SimulateKeyboardHookRegistered();
  EXPECT_EQ(0u, ReadKeyPressMonitorCount(readonly_mapping));
  ui::SimulateKeyboardHookUnregistered();
  EXPECT_EQ(0u, ReadKeyPressMonitorCount(readonly_mapping));
  monitor->DisableKeyPressMonitoring();

  monitor.reset();
  base::RunLoop().RunUntilIdle();

  // Check that read only region remains valid after disable.
  EXPECT_EQ(0u, ReadKeyPressMonitorCount(readonly_mapping));
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace media