Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 { | |
| 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(ChromotingNetworkMsg_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 TEST(DesktopSessionAgentTest, StartProcessStatsReport) { | |
|
joedow
2017/06/13 20:12:12
I think this test is doing too much. Can you move
Hzj_jie
2017/06/14 23:12:23
Done.
| |
| 87 base::MessageLoop message_loop; | |
| 88 base::RunLoop run_loop; | |
| 89 scoped_refptr<AutoThreadTaskRunner> task_runner = | |
| 90 new AutoThreadTaskRunner( | |
| 91 message_loop.task_runner(), run_loop.QuitClosure()); | |
| 92 std::unique_ptr<MockDelegate> delegate(new MockDelegate(task_runner)); | |
| 93 scoped_refptr<DesktopSessionAgent> agent(new DesktopSessionAgent( | |
| 94 task_runner, task_runner, task_runner, task_runner)); | |
| 95 std::unique_ptr<IPC::ChannelProxy> proxy; | |
| 96 ProcessStatsListener listener(base::Bind([]( | |
| 97 scoped_refptr<AutoThreadTaskRunner>* task_runner, | |
| 98 std::unique_ptr<MockDelegate>* delegate, | |
| 99 scoped_refptr<DesktopSessionAgent>* agent, | |
| 100 std::unique_ptr<IPC::ChannelProxy>* proxy) { | |
| 101 task_runner->operator=(nullptr); | |
| 102 delegate->reset(); | |
| 103 (*agent)->Stop(); | |
| 104 agent->operator=(nullptr); | |
| 105 proxy->reset(); | |
| 106 ASSERT_FALSE(static_cast<bool>(*task_runner)); | |
| 107 ASSERT_FALSE(static_cast<bool>(*delegate)); | |
| 108 ASSERT_FALSE(static_cast<bool>(*agent)); | |
| 109 ASSERT_FALSE(static_cast<bool>(*proxy)); | |
| 110 }, | |
| 111 base::Unretained(&task_runner), | |
| 112 base::Unretained(&delegate), | |
| 113 base::Unretained(&agent), | |
| 114 base::Unretained(&proxy))); | |
| 115 proxy = IPC::ChannelProxy::Create( | |
| 116 agent->Start(delegate->GetWeakPtr()).release(), | |
| 117 IPC::Channel::MODE_CLIENT, | |
| 118 &listener, | |
| 119 task_runner); | |
| 120 ASSERT_TRUE(proxy->Send(new ChromotingNetworkDesktopMsg_StartSessionAgent( | |
| 121 "jid", ScreenResolution(), DesktopEnvironmentOptions()))); | |
| 122 ASSERT_TRUE(proxy->Send(new ChromotingNetworkMsg_StartProcessStatsReport(1))); | |
| 123 task_runner = nullptr; | |
| 124 run_loop.Run(); | |
| 125 } | |
| 126 | |
| 127 } // namespace remoting | |
| OLD | NEW |