Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: remoting/host/desktop_session_agent_unittest.cc

Issue 2933203002: [Chromoting] Use ProcessStatsSender in DesktopSessionAgent (desktop process) (Closed)
Patch Set: Resolve review comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/host/desktop_session_agent.cc ('k') | remoting/host/process_stats_sender.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // 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"
11 #include "base/location.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/single_thread_task_runner.h"
17 #include "ipc/ipc_channel_proxy.h"
18 #include "ipc/ipc_listener.h"
19 #include "remoting/base/auto_thread_task_runner.h"
20 #include "remoting/host/chromoting_messages.h"
21 #include "remoting/host/desktop_environment_options.h"
22 #include "remoting/host/fake_desktop_environment.h"
23 #include "remoting/host/screen_resolution.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace remoting {
27
28 namespace {
29
30 class FakeDelegate : public DesktopSessionAgent::Delegate {
31 public:
32 FakeDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner);
33 ~FakeDelegate() override = default;
34
35 DesktopEnvironmentFactory& desktop_environment_factory() override {
36 return factory_;
37 }
38
39 void OnNetworkProcessDisconnected() override {}
40
41 base::WeakPtr<Delegate> GetWeakPtr() { return weak_ptr_.GetWeakPtr(); }
42
43 private:
44 FakeDesktopEnvironmentFactory factory_;
45
46 base::WeakPtrFactory<FakeDelegate> weak_ptr_;
47 };
48
49 FakeDelegate::FakeDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner)
50 : factory_(runner),
51 weak_ptr_(this) {}
52
53 class ProcessStatsListener : public IPC::Listener {
54 public:
55 ProcessStatsListener(base::Closure action_after_received)
56 : action_after_received_(action_after_received) {}
57
58 ~ProcessStatsListener() override = default;
59
60 private:
61 // IPC::Listener implementation.
62 bool OnMessageReceived(const IPC::Message& message) override;
63
64 void OnProcessResourceUsage(
65 const remoting::protocol::AggregatedProcessResourceUsage& usage);
66
67 const base::Closure action_after_received_;
68 };
69
70 bool ProcessStatsListener::OnMessageReceived(const IPC::Message& message) {
71 bool handled = false;
72 IPC_BEGIN_MESSAGE_MAP(ProcessStatsListener, message)
73 IPC_MESSAGE_HANDLER(ChromotingAnyToNetworkMsg_ReportProcessStats,
74 OnProcessResourceUsage);
75 IPC_MESSAGE_UNHANDLED(handled = false);
76 IPC_END_MESSAGE_MAP()
77 return handled;
78 }
79
80 void ProcessStatsListener::OnProcessResourceUsage(
81 const remoting::protocol::AggregatedProcessResourceUsage& usage) {
82 action_after_received_.Run();
83 }
84
85 } // namespace
86
87 class DesktopSessionAgentTest : public ::testing::Test {
88 public:
89 DesktopSessionAgentTest();
90 ~DesktopSessionAgentTest() override = default;
91
92 void Shutdown();
93
94 protected:
95 base::MessageLoop message_loop_;
96 base::RunLoop run_loop_;
97 scoped_refptr<AutoThreadTaskRunner> task_runner_;
98 scoped_refptr<DesktopSessionAgent> agent_;
99 };
100
101 DesktopSessionAgentTest::DesktopSessionAgentTest()
102 : task_runner_(new AutoThreadTaskRunner(
103 message_loop_.task_runner(), run_loop_.QuitClosure())),
104 agent_(new DesktopSessionAgent(
105 task_runner_, task_runner_, task_runner_, task_runner_)) {}
106
107 void DesktopSessionAgentTest::Shutdown() {
108 task_runner_ = nullptr;
109 agent_->Stop();
110 agent_ = nullptr;
111 }
112
113 TEST_F(DesktopSessionAgentTest, StartProcessStatsReport) {
114 std::unique_ptr<FakeDelegate> delegate(new FakeDelegate(task_runner_));
115 std::unique_ptr<IPC::ChannelProxy> proxy;
116 ProcessStatsListener listener(base::Bind([](
117 DesktopSessionAgentTest* test,
118 std::unique_ptr<FakeDelegate>* delegate,
119 std::unique_ptr<IPC::ChannelProxy>* proxy) {
120 test->Shutdown();
121 delegate->reset();
122 proxy->reset();
123 },
124 base::Unretained(this),
125 base::Unretained(&delegate),
126 base::Unretained(&proxy)));
127 proxy = IPC::ChannelProxy::Create(
128 agent_->Start(delegate->GetWeakPtr()).release(),
129 IPC::Channel::MODE_CLIENT,
130 &listener,
131 task_runner_);
132 ASSERT_TRUE(proxy->Send(new ChromotingNetworkDesktopMsg_StartSessionAgent(
133 "jid", ScreenResolution(), DesktopEnvironmentOptions())));
134 ASSERT_TRUE(proxy->Send(new ChromotingNetworkToAnyMsg_StartProcessStatsReport(
135 base::TimeDelta::FromMilliseconds(1))));
136 run_loop_.Run();
137 }
138
139 TEST_F(DesktopSessionAgentTest, StartProcessStatsReportWithInvalidInterval) {
140 std::unique_ptr<FakeDelegate> delegate(new FakeDelegate(task_runner_));
141 std::unique_ptr<IPC::ChannelProxy> proxy;
142 ProcessStatsListener listener(base::Bind([]() {}));
143 proxy = IPC::ChannelProxy::Create(
144 agent_->Start(delegate->GetWeakPtr()).release(),
145 IPC::Channel::MODE_CLIENT,
146 &listener,
147 task_runner_);
148 ASSERT_TRUE(proxy->Send(new ChromotingNetworkDesktopMsg_StartSessionAgent(
149 "jid", ScreenResolution(), DesktopEnvironmentOptions())));
150 ASSERT_TRUE(proxy->Send(new ChromotingNetworkToAnyMsg_StartProcessStatsReport(
151 base::TimeDelta::FromMilliseconds(-1))));
152 ASSERT_TRUE(proxy->Send(
153 new ChromotingNetworkToAnyMsg_StopProcessStatsReport()));
154 task_runner_->PostDelayedTask(FROM_HERE, base::Bind([](
155 DesktopSessionAgentTest* test,
156 std::unique_ptr<FakeDelegate>* delegate,
157 std::unique_ptr<IPC::ChannelProxy>* proxy) {
158 test->Shutdown();
159 delegate->reset();
160 proxy->reset();
161 },
162 base::Unretained(this),
163 base::Unretained(&delegate),
164 base::Unretained(&proxy)),
165 base::TimeDelta::FromMilliseconds(1));
166 run_loop_.Run();
167 }
168
169 TEST_F(DesktopSessionAgentTest, StartThenStopProcessStatsReport) {
170 std::unique_ptr<FakeDelegate> delegate(new FakeDelegate(task_runner_));
171 std::unique_ptr<IPC::ChannelProxy> proxy;
172 ProcessStatsListener listener(base::Bind([]() {}));
173 proxy = IPC::ChannelProxy::Create(
174 agent_->Start(delegate->GetWeakPtr()).release(),
175 IPC::Channel::MODE_CLIENT,
176 &listener,
177 task_runner_);
178 ASSERT_TRUE(proxy->Send(new ChromotingNetworkDesktopMsg_StartSessionAgent(
179 "jid", ScreenResolution(), DesktopEnvironmentOptions())));
180 ASSERT_TRUE(proxy->Send(new ChromotingNetworkToAnyMsg_StartProcessStatsReport(
181 base::TimeDelta::FromMilliseconds(1))));
182 ASSERT_TRUE(proxy->Send(
183 new ChromotingNetworkToAnyMsg_StopProcessStatsReport()));
184 task_runner_->PostDelayedTask(FROM_HERE, base::Bind([](
185 DesktopSessionAgentTest* test,
186 std::unique_ptr<FakeDelegate>* delegate,
187 std::unique_ptr<IPC::ChannelProxy>* proxy) {
188 test->Shutdown();
189 delegate->reset();
190 proxy->reset();
191 },
192 base::Unretained(this),
193 base::Unretained(&delegate),
194 base::Unretained(&proxy)),
195 base::TimeDelta::FromMilliseconds(1));
196 run_loop_.Run();
197 }
198
199 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/desktop_session_agent.cc ('k') | remoting/host/process_stats_sender.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698