Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "content/browser/media/webrtc/webrtc_eventlog_host.h" | |
| 6 | |
| 7 #include <tuple> | |
| 8 | |
| 9 #include "base/files/file.h" | |
| 10 #include "base/files/file_util.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "content/browser/renderer_host/render_process_host_impl.h" | |
| 14 #include "content/common/media/peer_connection_tracker_messages.h" | |
| 15 #include "content/public/browser/browser_context.h" | |
| 16 #include "content/public/test/mock_render_process_host.h" | |
| 17 #include "content/public/test/test_browser_context.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Get the expected Rtc eventlog file name. The name will be | |
| 26 // <temporary path>.<render process id>.<peer connection local id> | |
| 27 base::FilePath GetExpectedEventLogFileName(const base::FilePath& base_file, | |
| 28 int render_process_id, | |
| 29 int peer_connection_local_id) { | |
| 30 return base_file.AddExtension(base::IntToString(render_process_id)) | |
| 31 .AddExtension(base::IntToString(peer_connection_local_id)); | |
| 32 } | |
| 33 | |
| 34 class WebRtcEventlogHostTest : public testing::Test { | |
| 35 public: | |
| 36 WebRtcEventlogHostTest() | |
| 37 : mock_render_process_host_(static_cast<MockRenderProcessHost*>( | |
| 38 mock_render_process_factory_.CreateRenderProcessHost( | |
| 39 &test_browser_context_, | |
| 40 nullptr))), | |
| 41 render_id_(mock_render_process_host_->GetID()), | |
| 42 event_log_host_(render_id_) {} | |
| 43 TestBrowserThreadBundle thread_bundle_; | |
| 44 MockRenderProcessHostFactory mock_render_process_factory_; | |
| 45 TestBrowserContext test_browser_context_; | |
| 46 std::unique_ptr<MockRenderProcessHost> mock_render_process_host_; | |
| 47 const int render_id_; | |
| 48 WebRTCEventLogHost event_log_host_; | |
| 49 base::FilePath base_file_; | |
| 50 | |
| 51 void StartLogging() { | |
| 52 ASSERT_TRUE(CreateTemporaryFile(&base_file_)); | |
|
Henrik Grunell
2016/06/08 10:08:50
base:: ? (Does this compile?)
Ivo-OOO until feb 6
2016/06/08 14:52:29
It does compile, but I will remove it
Henrik Grunell
2016/06/09 10:28:22
I think you should keep it. There's no using state
Ivo-OOO until feb 6
2016/06/09 12:59:39
Oh, I misunderstood, I thought you were suggesting
| |
| 53 EXPECT_TRUE(base::DeleteFile(base_file_, false)); | |
| 54 EXPECT_FALSE(base::PathExists(base_file_)); | |
| 55 EXPECT_TRUE(event_log_host_.StartWebRTCEventLog(base_file_)); | |
| 56 base::RunLoop().RunUntilIdle(); | |
| 57 } | |
| 58 | |
| 59 void StopLogging() { | |
| 60 EXPECT_TRUE(event_log_host_.StopWebRTCEventLog()); | |
| 61 base::RunLoop().RunUntilIdle(); | |
| 62 } | |
| 63 | |
| 64 void validateStartIPCMessageAndCloseFile(const IPC::Message* msg, | |
|
Henrik Grunell
2016/06/08 10:08:50
ValidateStart... (capital first letter).
Same bel
Ivo-OOO until feb 6
2016/06/08 14:52:29
Done.
| |
| 65 const int peer_connection_id) { | |
| 66 ASSERT_TRUE(msg); | |
|
Henrik Grunell
2016/06/08 10:08:50
Does this compile? Doesn't ASSERT_XYZ() return tru
Ivo-OOO until feb 6
2016/06/08 14:52:29
It compiles and runs. In this case, if the ASSERT
Henrik Grunell
2016/06/09 10:28:22
OK. I just recalled incorrectly then. (Thought it
| |
| 67 std::tuple<int, IPC::PlatformFileForTransit> start_params; | |
| 68 PeerConnectionTracker_StartEventLog::Read(msg, &start_params); | |
| 69 EXPECT_EQ(peer_connection_id, std::get<0>(start_params)); | |
| 70 ASSERT_NE(IPC::InvalidPlatformFileForTransit(), std::get<1>(start_params)); | |
| 71 IPC::PlatformFileForTransitToFile(std::get<1>(start_params)).Close(); | |
| 72 } | |
| 73 | |
| 74 void validateStopIPCMessage(const IPC::Message* msg, | |
| 75 const int peer_connection_id) { | |
| 76 ASSERT_TRUE(msg); | |
| 77 std::tuple<int> stop_params; | |
| 78 PeerConnectionTracker_StopEventLog::Read(msg, &stop_params); | |
| 79 EXPECT_EQ(peer_connection_id, std::get<0>(stop_params)); | |
| 80 } | |
| 81 }; | |
| 82 | |
| 83 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() without having | |
| 84 // added any PeerConnections. It is expected that no IPC messages will be sent. | |
| 85 TEST_F(WebRtcEventlogHostTest, NoPeerConnectionTest) { | |
| 86 mock_render_process_host_->sink().ClearMessages(); | |
| 87 | |
| 88 // Start logging and check that no IPC messages were sent. | |
| 89 StartLogging(); | |
| 90 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 91 | |
| 92 // Stop logging and check that no IPC messages were sent. | |
| 93 StopLogging(); | |
| 94 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 95 } | |
| 96 | |
| 97 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() after adding a | |
| 98 // single PeerConnection. It is expected that one IPC message will be sent for | |
| 99 // each of the Start and Stop calls, and that a logfile is created. | |
| 100 TEST_F(WebRtcEventlogHostTest, OnePeerConnectionTest) { | |
| 101 const int kTestPeerConnectionId = 123; | |
| 102 mock_render_process_host_->sink().ClearMessages(); | |
| 103 | |
| 104 // Add a PeerConnection and start logging. | |
| 105 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId); | |
| 106 StartLogging(); | |
| 107 | |
| 108 // Check that the correct IPC message was sent. | |
| 109 EXPECT_EQ(size_t(1), mock_render_process_host_->sink().message_count()); | |
| 110 const IPC::Message* start_msg = | |
| 111 mock_render_process_host_->sink().GetMessageAt(0); | |
| 112 validateStartIPCMessageAndCloseFile(start_msg, kTestPeerConnectionId); | |
| 113 | |
| 114 // Stop logging. | |
| 115 mock_render_process_host_->sink().ClearMessages(); | |
| 116 StopLogging(); | |
| 117 | |
| 118 // Check that the correct IPC message was sent. | |
| 119 EXPECT_EQ(size_t(1), mock_render_process_host_->sink().message_count()); | |
| 120 const IPC::Message* stop_msg = | |
| 121 mock_render_process_host_->sink().GetMessageAt(0); | |
| 122 validateStopIPCMessage(stop_msg, kTestPeerConnectionId); | |
| 123 | |
| 124 // Clean up the logfile. | |
| 125 base::FilePath expected_file = GetExpectedEventLogFileName( | |
| 126 base_file_, render_id_, kTestPeerConnectionId); | |
| 127 ASSERT_TRUE(base::PathExists(expected_file)); | |
| 128 EXPECT_TRUE(base::DeleteFile(expected_file, false)); | |
| 129 } | |
| 130 | |
| 131 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() after adding | |
| 132 // two PeerConnections. It is expected that two IPC messages will be sent for | |
| 133 // each of the Start and Stop calls, and that a file is created for both | |
| 134 // PeerConnections. | |
| 135 TEST_F(WebRtcEventlogHostTest, TwoPeerConnectionsTest) { | |
| 136 const int kTestPeerConnectionId1 = 123; | |
| 137 const int kTestPeerConnectionId2 = 321; | |
| 138 mock_render_process_host_->sink().ClearMessages(); | |
| 139 | |
| 140 // Add two PeerConnections and start logging. | |
| 141 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId1); | |
| 142 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId2); | |
| 143 StartLogging(); | |
| 144 | |
| 145 // Check that the correct IPC messages were sent. | |
| 146 EXPECT_EQ(size_t(2), mock_render_process_host_->sink().message_count()); | |
| 147 const IPC::Message* start_msg1 = | |
| 148 mock_render_process_host_->sink().GetMessageAt(0); | |
| 149 validateStartIPCMessageAndCloseFile(start_msg1, kTestPeerConnectionId1); | |
| 150 const IPC::Message* start_msg2 = | |
| 151 mock_render_process_host_->sink().GetMessageAt(1); | |
| 152 validateStartIPCMessageAndCloseFile(start_msg2, kTestPeerConnectionId2); | |
| 153 | |
| 154 // Stop logging. | |
| 155 mock_render_process_host_->sink().ClearMessages(); | |
| 156 StopLogging(); | |
| 157 | |
| 158 // Check that the correct IPC messages were sent. | |
| 159 EXPECT_EQ(size_t(2), mock_render_process_host_->sink().message_count()); | |
| 160 const IPC::Message* stop_msg1 = | |
| 161 mock_render_process_host_->sink().GetMessageAt(0); | |
| 162 validateStopIPCMessage(stop_msg1, kTestPeerConnectionId1); | |
| 163 const IPC::Message* stop_msg2 = | |
| 164 mock_render_process_host_->sink().GetMessageAt(1); | |
| 165 validateStopIPCMessage(stop_msg2, kTestPeerConnectionId2); | |
| 166 | |
| 167 // Clean up the logfiles. | |
| 168 base::FilePath expected_file1 = GetExpectedEventLogFileName( | |
| 169 base_file_, render_id_, kTestPeerConnectionId1); | |
| 170 base::FilePath expected_file2 = GetExpectedEventLogFileName( | |
| 171 base_file_, render_id_, kTestPeerConnectionId2); | |
| 172 ASSERT_TRUE(base::PathExists(expected_file1)); | |
| 173 EXPECT_TRUE(base::DeleteFile(expected_file1, false)); | |
| 174 ASSERT_TRUE(base::PathExists(expected_file2)); | |
| 175 EXPECT_TRUE(base::DeleteFile(expected_file2, false)); | |
| 176 } | |
| 177 | |
| 178 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() after first | |
| 179 // adding and then removing a single PeerConnection. It is expected that no IPC | |
| 180 // message will be sent. | |
| 181 TEST_F(WebRtcEventlogHostTest, AddRemovePeerConnectionTest) { | |
| 182 const int kTestPeerConnectionId = 123; | |
| 183 mock_render_process_host_->sink().ClearMessages(); | |
| 184 | |
| 185 // Add and immediately remove a PeerConnection. | |
| 186 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId); | |
| 187 event_log_host_.PeerConnectionRemoved(kTestPeerConnectionId); | |
| 188 | |
| 189 // Start logging and check that no IPC messages were sent. | |
| 190 StartLogging(); | |
| 191 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 192 | |
| 193 // Stop logging and check that no IPC messages were sent. | |
| 194 StopLogging(); | |
| 195 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 196 | |
| 197 // Check that no logfile was created. | |
| 198 base::FilePath expected_file = GetExpectedEventLogFileName( | |
| 199 base_file_, render_id_, kTestPeerConnectionId); | |
| 200 ASSERT_FALSE(base::PathExists(expected_file)); | |
| 201 } | |
|
Henrik Grunell
2016/06/08 10:08:50
Could you add a test case for going over the limit
Ivo-OOO until feb 6
2016/06/08 14:52:29
Done.
| |
| 202 | |
| 203 } // namespace | |
|
Henrik Grunell
2016/06/08 10:08:50
The tests are typically not put in an anonymous na
Ivo-OOO until feb 6
2016/06/08 14:52:29
Done.
| |
| 204 | |
| 205 } // namespace content | |
| OLD | NEW |