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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/desktop_session_agent.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "ipc/ipc_channel_proxy.h"
#include "ipc/ipc_listener.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "remoting/base/auto_thread_task_runner.h"
#include "remoting/host/base/desktop_environment_options.h"
#include "remoting/host/base/screen_resolution.h"
#include "remoting/host/fake_desktop_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
namespace {
class FakeDelegate : public DesktopSessionAgent::Delegate {
public:
FakeDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner);
~FakeDelegate() override = default;
DesktopEnvironmentFactory& desktop_environment_factory() override {
return factory_;
}
void OnNetworkProcessDisconnected() override {}
void CrashNetworkProcess(const base::Location& location) override {}
base::WeakPtr<Delegate> GetWeakPtr() { return weak_ptr_.GetWeakPtr(); }
private:
FakeDesktopEnvironmentFactory factory_;
base::WeakPtrFactory<FakeDelegate> weak_ptr_{this};
};
FakeDelegate::FakeDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner)
: factory_(runner) {}
class FakeListener : public IPC::Listener {
public:
explicit FakeListener(base::RepeatingClosure action_after_received)
: action_after_received_(action_after_received) {}
~FakeListener() override = default;
private:
// IPC::Listener implementation.
bool OnMessageReceived(const IPC::Message& message) override;
void OnAssociatedInterfaceRequest(
const std::string& interface_name,
mojo::ScopedInterfaceEndpointHandle handle) override;
const base::RepeatingClosure action_after_received_;
};
bool FakeListener::OnMessageReceived(const IPC::Message& message) {
return false;
}
void FakeListener::OnAssociatedInterfaceRequest(
const std::string& interface_name,
mojo::ScopedInterfaceEndpointHandle handle) {
EXPECT_EQ(mojom::DesktopSessionEventHandler::Name_, interface_name);
action_after_received_.Run();
}
} // namespace
class DesktopSessionAgentTest : public ::testing::Test {
public:
DesktopSessionAgentTest();
~DesktopSessionAgentTest() override = default;
void Shutdown();
protected:
base::test::SingleThreadTaskEnvironment task_environment_;
base::RunLoop run_loop_;
scoped_refptr<AutoThreadTaskRunner> task_runner_;
scoped_refptr<DesktopSessionAgent> agent_;
};
DesktopSessionAgentTest::DesktopSessionAgentTest()
: task_runner_(
new AutoThreadTaskRunner(task_environment_.GetMainThreadTaskRunner(),
run_loop_.QuitClosure())),
agent_(new DesktopSessionAgent(task_runner_,
task_runner_,
task_runner_,
task_runner_)) {}
void DesktopSessionAgentTest::Shutdown() {
task_runner_ = nullptr;
agent_->Stop();
agent_ = nullptr;
}
TEST_F(DesktopSessionAgentTest, StartDesktopSessionAgent) {
std::unique_ptr<FakeDelegate> delegate(new FakeDelegate(task_runner_));
std::unique_ptr<IPC::ChannelProxy> proxy;
bool session_started = false;
FakeListener listener(base::BindLambdaForTesting([&]() {
session_started = true;
Shutdown();
delegate.reset();
proxy.reset();
}));
proxy = IPC::ChannelProxy::Create(
agent_->Initialize(delegate->GetWeakPtr()).release(),
IPC::Channel::MODE_CLIENT, &listener, task_runner_,
base::SingleThreadTaskRunner::GetCurrentDefault());
mojo::AssociatedRemote<mojom::DesktopSessionAgent> desktop_session_agent;
proxy->GetRemoteAssociatedInterface(&desktop_session_agent);
// Let the IPC machinery finish up the interface request before using it.
task_environment_.RunUntilIdle();
bool remote_received = false;
desktop_session_agent->Start(
"jid", ScreenResolution(), DesktopEnvironmentOptions(),
base::BindLambdaForTesting(
[&](mojo::PendingAssociatedRemote<mojom::DesktopSessionControl>
pending_remote) {
// Indicate that we received the desktop_session_control remote.
remote_received = true;
// Release any references to the other IPC classes.
desktop_session_agent.reset();
}));
run_loop_.Run();
ASSERT_TRUE(remote_received);
ASSERT_TRUE(session_started);
}
} // namespace remoting
|