user_input_monitor_unittest.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2018 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. #include "services/audio/user_input_monitor.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "media/base/user_input_monitor.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace audio {
  10. namespace {
  11. constexpr uint32_t kKeyPressCount = 10;
  12. }
  13. TEST(AudioServiceUserInputMonitorTest, CreateWithValidHandle) {
  14. std::unique_ptr<base::MappedReadOnlyRegion> shmem =
  15. std::make_unique<base::MappedReadOnlyRegion>(
  16. base::ReadOnlySharedMemoryRegion::Create(sizeof(uint32_t)));
  17. ASSERT_TRUE(shmem->IsValid());
  18. EXPECT_TRUE(UserInputMonitor::Create(shmem->region.Duplicate()));
  19. }
  20. TEST(AudioServiceUserInputMonitorTest, CreateWithInvalidHandle_ReturnsNullptr) {
  21. EXPECT_EQ(nullptr,
  22. UserInputMonitor::Create(base::ReadOnlySharedMemoryRegion()));
  23. }
  24. TEST(AudioServiceUserInputMonitorTest, GetKeyPressCount) {
  25. std::unique_ptr<base::MappedReadOnlyRegion> shmem =
  26. std::make_unique<base::MappedReadOnlyRegion>(
  27. base::ReadOnlySharedMemoryRegion::Create(sizeof(uint32_t)));
  28. ASSERT_TRUE(shmem->IsValid());
  29. std::unique_ptr<UserInputMonitor> monitor =
  30. UserInputMonitor::Create(shmem->region.Duplicate());
  31. EXPECT_TRUE(monitor);
  32. media::WriteKeyPressMonitorCount(shmem->mapping, kKeyPressCount);
  33. EXPECT_EQ(kKeyPressCount, monitor->GetKeyPressCount());
  34. }
  35. TEST(AudioServiceUserInputMonitorTest, GetKeyPressCountAfterMemoryUnmap) {
  36. std::unique_ptr<base::MappedReadOnlyRegion> shmem =
  37. std::make_unique<base::MappedReadOnlyRegion>(
  38. base::ReadOnlySharedMemoryRegion::Create(sizeof(uint32_t)));
  39. ASSERT_TRUE(shmem->IsValid());
  40. std::unique_ptr<UserInputMonitor> monitor =
  41. UserInputMonitor::Create(shmem->region.Duplicate());
  42. EXPECT_TRUE(monitor);
  43. media::WriteKeyPressMonitorCount(shmem->mapping, kKeyPressCount);
  44. EXPECT_EQ(kKeyPressCount, monitor->GetKeyPressCount());
  45. // ReadOnlyMapping should still be valid, containing the last updated value,
  46. // after shmem reset.
  47. shmem.reset();
  48. EXPECT_EQ(kKeyPressCount, monitor->GetKeyPressCount());
  49. }
  50. } // namespace audio