local_input_monitor_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2012 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 "remoting/host/input_monitor/local_input_monitor.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "build/build_config.h"
  12. #include "remoting/base/auto_thread_task_runner.h"
  13. #include "remoting/host/client_session_control.h"
  14. #include "remoting/host/host_mock_objects.h"
  15. #include "remoting/protocol/protocol_mock_objects.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. namespace remoting {
  19. using testing::_;
  20. using testing::AnyNumber;
  21. using testing::ReturnRef;
  22. namespace {
  23. class LocalInputMonitorTest : public testing::Test {
  24. public:
  25. LocalInputMonitorTest();
  26. void SetUp() override;
  27. base::test::TaskEnvironment task_environment_ {
  28. #if BUILDFLAG(IS_WIN)
  29. base::test::TaskEnvironment::MainThreadType::UI
  30. #else // !BUILDFLAG(IS_WIN)
  31. // Required to watch a file descriptor from NativeMessageProcessHost.
  32. base::test::TaskEnvironment::MainThreadType::IO
  33. #endif // !BUILDFLAG(IS_WIN)
  34. };
  35. base::RunLoop run_loop_;
  36. scoped_refptr<AutoThreadTaskRunner> task_runner_;
  37. std::string client_jid_;
  38. MockClientSessionControl client_session_control_;
  39. base::WeakPtrFactory<ClientSessionControl> client_session_control_factory_;
  40. };
  41. LocalInputMonitorTest::LocalInputMonitorTest()
  42. : client_jid_("user@domain/rest-of-jid"),
  43. client_session_control_factory_(&client_session_control_) {}
  44. void LocalInputMonitorTest::SetUp() {
  45. // Run the task environment until no components depend on it.
  46. task_runner_ = new AutoThreadTaskRunner(base::ThreadTaskRunnerHandle::Get(),
  47. run_loop_.QuitClosure());
  48. }
  49. } // namespace
  50. // This test is really to exercise only the creation and destruction code in
  51. // LocalInputMonitor.
  52. TEST_F(LocalInputMonitorTest, BasicWithClientSession) {
  53. // Ignore all callbacks.
  54. EXPECT_CALL(client_session_control_, client_jid())
  55. .Times(AnyNumber())
  56. .WillRepeatedly(ReturnRef(client_jid_));
  57. EXPECT_CALL(client_session_control_, DisconnectSession(_)).Times(AnyNumber());
  58. EXPECT_CALL(client_session_control_, OnLocalPointerMoved(_, _))
  59. .Times(AnyNumber());
  60. EXPECT_CALL(client_session_control_, SetDisableInputs(_)).Times(0);
  61. {
  62. std::unique_ptr<LocalInputMonitor> local_input_monitor =
  63. LocalInputMonitor::Create(task_runner_, task_runner_, task_runner_);
  64. local_input_monitor->StartMonitoringForClientSession(
  65. client_session_control_factory_.GetWeakPtr());
  66. task_runner_ = nullptr;
  67. }
  68. run_loop_.Run();
  69. }
  70. TEST_F(LocalInputMonitorTest, BasicWithCallbacks) {
  71. // Ignore all callbacks.
  72. EXPECT_CALL(client_session_control_, client_jid())
  73. .Times(AnyNumber())
  74. .WillRepeatedly(ReturnRef(client_jid_));
  75. {
  76. std::unique_ptr<LocalInputMonitor> local_input_monitor =
  77. LocalInputMonitor::Create(task_runner_, task_runner_, task_runner_);
  78. local_input_monitor->StartMonitoring(base::DoNothing(), base::DoNothing(),
  79. base::DoNothing());
  80. task_runner_ = nullptr;
  81. }
  82. run_loop_.Run();
  83. }
  84. } // namespace remoting