blob: bdc0a7d37cf4784e7a2783b5cdd1337d9d91f692 [file] [log] [blame]
zijiehe36763d82017-06-19 23:52:311// Copyright 2017 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 "remoting/host/desktop_session_agent.h"
6
7#include <memory>
8
9#include "base/bind.h"
10#include "base/callback.h"
danakjdb9ae7942020-11-11 16:01:3511#include "base/callback_helpers.h"
zijiehe36763d82017-06-19 23:52:3112#include "base/location.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/weak_ptr.h"
zijiehe36763d82017-06-19 23:52:3115#include "base/run_loop.h"
Patrick Monette643cdf62021-10-15 19:13:4216#include "base/task/single_thread_task_runner.h"
Gabriel Charettec7108742019-08-23 03:31:4017#include "base/test/task_environment.h"
zijiehef401c5182017-07-19 00:21:4618#include "base/threading/thread_task_runner_handle.h"
zijiehe36763d82017-06-19 23:52:3119#include "ipc/ipc_channel_proxy.h"
20#include "ipc/ipc_listener.h"
21#include "remoting/base/auto_thread_task_runner.h"
Joe Downing66c5aefc2021-12-14 20:56:3722#include "remoting/host/base/desktop_environment_options.h"
Joe Downingb0fb5422021-12-07 21:26:2023#include "remoting/host/base/screen_resolution.h"
zijiehe36763d82017-06-19 23:52:3124#include "remoting/host/chromoting_messages.h"
zijiehe36763d82017-06-19 23:52:3125#include "remoting/host/fake_desktop_environment.h"
zijiehe36763d82017-06-19 23:52:3126#include "testing/gtest/include/gtest/gtest.h"
27
28namespace remoting {
29
30namespace {
31
32class FakeDelegate : public DesktopSessionAgent::Delegate {
33 public:
34 FakeDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner);
35 ~FakeDelegate() override = default;
36
37 DesktopEnvironmentFactory& desktop_environment_factory() override {
38 return factory_;
39 }
40
41 void OnNetworkProcessDisconnected() override {}
42
Joe Downingd4d6f342021-09-23 22:12:0943 void CrashNetworkProcess(const base::Location& location) override {}
44
zijiehe36763d82017-06-19 23:52:3145 base::WeakPtr<Delegate> GetWeakPtr() { return weak_ptr_.GetWeakPtr(); }
46
47 private:
48 FakeDesktopEnvironmentFactory factory_;
49
Jeremy Roman7c5cfabd2019-08-12 15:45:2750 base::WeakPtrFactory<FakeDelegate> weak_ptr_{this};
zijiehe36763d82017-06-19 23:52:3151};
52
53FakeDelegate::FakeDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner)
Jeremy Roman7c5cfabd2019-08-12 15:45:2754 : factory_(runner) {}
zijiehe36763d82017-06-19 23:52:3155
Joe Downing95bba092021-10-14 16:58:5756class FakeListener : public IPC::Listener {
zijiehe36763d82017-06-19 23:52:3157 public:
Joe Downing95bba092021-10-14 16:58:5758 explicit FakeListener(base::RepeatingClosure action_after_received)
zijiehe36763d82017-06-19 23:52:3159 : action_after_received_(action_after_received) {}
Joe Downing95bba092021-10-14 16:58:5760 ~FakeListener() override = default;
zijiehe36763d82017-06-19 23:52:3161
62 private:
63 // IPC::Listener implementation.
64 bool OnMessageReceived(const IPC::Message& message) override;
Joe Downing95bba092021-10-14 16:58:5765 void OnAssociatedInterfaceRequest(
66 const std::string& interface_name,
67 mojo::ScopedInterfaceEndpointHandle handle) override;
zijiehe36763d82017-06-19 23:52:3168
Evan Stadeaf37ba32020-06-26 20:36:3769 const base::RepeatingClosure action_after_received_;
zijiehe36763d82017-06-19 23:52:3170};
71
Joe Downing95bba092021-10-14 16:58:5772bool FakeListener::OnMessageReceived(const IPC::Message& message) {
73 return false;
zijiehe36763d82017-06-19 23:52:3174}
75
Joe Downing95bba092021-10-14 16:58:5776void FakeListener::OnAssociatedInterfaceRequest(
77 const std::string& interface_name,
78 mojo::ScopedInterfaceEndpointHandle handle) {
79 EXPECT_EQ(mojom::DesktopSessionEventHandler::Name_, interface_name);
zijiehe36763d82017-06-19 23:52:3180 action_after_received_.Run();
81}
82
83} // namespace
84
85class DesktopSessionAgentTest : public ::testing::Test {
86 public:
87 DesktopSessionAgentTest();
88 ~DesktopSessionAgentTest() override = default;
89
90 void Shutdown();
91
92 protected:
Gabriel Charettedf0a7442019-09-05 17:32:3593 base::test::SingleThreadTaskEnvironment task_environment_;
zijiehe36763d82017-06-19 23:52:3194 base::RunLoop run_loop_;
95 scoped_refptr<AutoThreadTaskRunner> task_runner_;
96 scoped_refptr<DesktopSessionAgent> agent_;
97};
98
99DesktopSessionAgentTest::DesktopSessionAgentTest()
Gabriel Charettedfa36042019-08-19 17:30:11100 : task_runner_(
101 new AutoThreadTaskRunner(task_environment_.GetMainThreadTaskRunner(),
102 run_loop_.QuitClosure())),
Carlos Caballero1df57eb22019-05-30 17:07:12103 agent_(new DesktopSessionAgent(task_runner_,
104 task_runner_,
105 task_runner_,
106 task_runner_)) {}
zijiehe36763d82017-06-19 23:52:31107
108void DesktopSessionAgentTest::Shutdown() {
109 task_runner_ = nullptr;
110 agent_->Stop();
111 agent_ = nullptr;
112}
113
Joe Downing95bba092021-10-14 16:58:57114TEST_F(DesktopSessionAgentTest, StartDesktopSessionAgent) {
zijiehe36763d82017-06-19 23:52:31115 std::unique_ptr<FakeDelegate> delegate(new FakeDelegate(task_runner_));
116 std::unique_ptr<IPC::ChannelProxy> proxy;
Joe Downing95bba092021-10-14 16:58:57117 FakeListener listener(base::BindRepeating(
Evan Stade94ffcb62020-07-28 18:51:19118 [](DesktopSessionAgentTest* test, std::unique_ptr<FakeDelegate>* delegate,
119 std::unique_ptr<IPC::ChannelProxy>* proxy) {
zijiehe36763d82017-06-19 23:52:31120 test->Shutdown();
121 delegate->reset();
122 proxy->reset();
123 },
Evan Stade94ffcb62020-07-28 18:51:19124 base::Unretained(this), base::Unretained(&delegate),
zijiehe36763d82017-06-19 23:52:31125 base::Unretained(&proxy)));
126 proxy = IPC::ChannelProxy::Create(
127 agent_->Start(delegate->GetWeakPtr()).release(),
Hajime Hoshiff15e972017-11-09 06:37:09128 IPC::Channel::MODE_CLIENT, &listener, task_runner_,
129 base::ThreadTaskRunnerHandle::Get());
zijiehe36763d82017-06-19 23:52:31130 ASSERT_TRUE(proxy->Send(new ChromotingNetworkDesktopMsg_StartSessionAgent(
131 "jid", ScreenResolution(), DesktopEnvironmentOptions())));
zijiehef401c5182017-07-19 00:21:46132 run_loop_.Run();
133}
134
zijiehe36763d82017-06-19 23:52:31135} // namespace remoting