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

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
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/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h"
16 #include "ipc/ipc_channel_proxy.h"
17 #include "ipc/ipc_listener.h"
18 #include "remoting/base/auto_thread_task_runner.h"
19 #include "remoting/host/chromoting_messages.h"
20 #include "remoting/host/desktop_environment_options.h"
21 #include "remoting/host/fake_desktop_environment.h"
22 #include "remoting/host/screen_resolution.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace remoting {
26
27 namespace {
28
29 class MockDelegate : public DesktopSessionAgent::Delegate {
joedow 2017/06/15 22:50:44 Sorry I missed this in the first iteration, but th
Hzj_jie 2017/06/16 00:39:28 It looks like I still cannot choose the right name
30 public:
31 MockDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner);
32 ~MockDelegate() override = default;
33
34 DesktopEnvironmentFactory& desktop_environment_factory() override {
35 return factory_;
36 }
37
38 void OnNetworkProcessDisconnected() override {}
39
40 base::WeakPtr<Delegate> GetWeakPtr() { return weak_ptr_.GetWeakPtr(); }
41
42 private:
43 FakeDesktopEnvironmentFactory factory_;
44
45 base::WeakPtrFactory<MockDelegate> weak_ptr_;
46 };
47
48 MockDelegate::MockDelegate(scoped_refptr<base::SingleThreadTaskRunner> runner)
49 : factory_(runner),
50 weak_ptr_(this) {}
51
52 class ProcessStatsListener : public IPC::Listener {
53 public:
54 ProcessStatsListener(base::Closure action_after_received)
55 : action_after_received_(action_after_received) {}
56
57 ~ProcessStatsListener() override = default;
58
59 private:
60 // IPC::Listener implementation.
61 bool OnMessageReceived(const IPC::Message& message) override;
62
63 void OnProcessResourceUsage(
64 const remoting::protocol::AggregatedProcessResourceUsage& usage);
65
66 const base::Closure action_after_received_;
67 };
68
69 bool ProcessStatsListener::OnMessageReceived(const IPC::Message& message) {
70 bool handled = false;
71 IPC_BEGIN_MESSAGE_MAP(ProcessStatsListener, message)
72 IPC_MESSAGE_HANDLER(ChromotingAnyToNetworkMsg_ReportProcessStats,
73 OnProcessResourceUsage);
74 IPC_MESSAGE_UNHANDLED(handled = false);
75 IPC_END_MESSAGE_MAP()
76 return handled;
77 }
78
79 void ProcessStatsListener::OnProcessResourceUsage(
80 const remoting::protocol::AggregatedProcessResourceUsage& usage) {
81 action_after_received_.Run();
82 }
83
84 } // namespace
85
86 class DesktopSessionAgentTest : public ::testing::Test {
87 public:
88 DesktopSessionAgentTest();
89 ~DesktopSessionAgentTest() override = default;
90
91 void Shutdown();
92
93 protected:
94 base::MessageLoop message_loop_;
95 base::RunLoop run_loop_;
96 scoped_refptr<AutoThreadTaskRunner> task_runner_;
97 scoped_refptr<DesktopSessionAgent> agent_;
98 };
99
100 DesktopSessionAgentTest::DesktopSessionAgentTest()
101 : task_runner_(new AutoThreadTaskRunner(
102 message_loop_.task_runner(), run_loop_.QuitClosure())),
103 agent_(new DesktopSessionAgent(
104 task_runner_, task_runner_, task_runner_, task_runner_)) {}
105
106 void DesktopSessionAgentTest::Shutdown() {
107 task_runner_ = nullptr;
108 agent_->Stop();
109 agent_ = nullptr;
110 ASSERT_FALSE(static_cast<bool>(task_runner_));
joedow 2017/06/15 22:50:44 I might be missing something, but you assign nullp
Hzj_jie 2017/06/16 00:39:28 No, it's my fault. They should be removed.
111 ASSERT_FALSE(static_cast<bool>(agent_));
joedow 2017/06/15 22:50:44 If you do need to keep the ASSERTS, I think ASSERT
Hzj_jie 2017/06/16 00:39:28 Done.
112 }
113
114 TEST_F(DesktopSessionAgentTest, StartProcessStatsReport) {
115 std::unique_ptr<MockDelegate> delegate(new MockDelegate(task_runner_));
116 std::unique_ptr<IPC::ChannelProxy> proxy;
117 ProcessStatsListener listener(base::Bind([](
118 DesktopSessionAgentTest* test,
119 std::unique_ptr<MockDelegate>* delegate,
120 std::unique_ptr<IPC::ChannelProxy>* proxy) {
121 test->Shutdown();
122 delegate->reset();
123 proxy->reset();
124 ASSERT_FALSE(static_cast<bool>(*delegate));
125 ASSERT_FALSE(static_cast<bool>(*proxy));
126 },
127 base::Unretained(this),
128 base::Unretained(&delegate),
129 base::Unretained(&proxy)));
130 proxy = IPC::ChannelProxy::Create(
131 agent_->Start(delegate->GetWeakPtr()).release(),
132 IPC::Channel::MODE_CLIENT,
133 &listener,
134 task_runner_);
135 ASSERT_TRUE(proxy->Send(new ChromotingNetworkDesktopMsg_StartSessionAgent(
136 "jid", ScreenResolution(), DesktopEnvironmentOptions())));
137 ASSERT_TRUE(proxy->Send(
138 new ChromotingNetworkToAnyMsg_StartProcessStatsReport(1)));
139 run_loop_.Run();
joedow 2017/06/15 22:50:44 Can you add a test where you send a stop MSG?
Hzj_jie 2017/06/16 00:39:28 Done.
140 }
141
142 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698