1 // Copyright 2013 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.
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/platform_file.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/media/webrtc_log_uploader.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 const char kTestReportId
[] = "123456789";
18 const char kTestTime
[] = "987654321";
20 class WebRtcLogUploaderTest
: public testing::Test
{
22 WebRtcLogUploaderTest() {}
24 bool VerifyNumberOfLinesAndContentsOfLastLine(int expected_lines
) {
26 int read
= base::ReadFileToString(test_list_path_
, &contents
);
31 // Verify expected number of lines. Since every line should end with '\n',
32 // there should be an additional empty line after splitting.
33 std::vector
<std::string
> lines
;
34 base::SplitString(contents
, '\n', &lines
);
35 EXPECT_EQ(expected_lines
+ 1, static_cast<int>(lines
.size()));
36 if (expected_lines
+ 1 != static_cast<int>(lines
.size()))
38 EXPECT_TRUE(lines
[expected_lines
].empty());
40 // Verify the contents of the last line. The time (line_parts[0]) is the
41 // time when the info was written to the file which we don't know, so just
42 // verify that it's not empty.
43 std::vector
<std::string
> line_parts
;
44 base::SplitString(lines
[expected_lines
- 1], ',', &line_parts
);
45 EXPECT_EQ(2u, line_parts
.size());
46 if (2u != line_parts
.size())
48 EXPECT_FALSE(line_parts
[0].empty());
49 EXPECT_STREQ(kTestReportId
, line_parts
[1].c_str());
54 bool AddLinesToTestFile(int number_of_lines
) {
55 int flags
= base::PLATFORM_FILE_OPEN
|
56 base::PLATFORM_FILE_APPEND
;
57 base::PlatformFileError error
= base::PLATFORM_FILE_OK
;
58 base::PlatformFile test_list_file
=
59 base::CreatePlatformFile(test_list_path_
, flags
, NULL
, &error
);
60 EXPECT_EQ(base::PLATFORM_FILE_OK
, error
);
61 EXPECT_NE(base::kInvalidPlatformFileValue
, test_list_file
);
62 if (base::PLATFORM_FILE_OK
!= error
||
63 base::kInvalidPlatformFileValue
== test_list_file
) {
67 for (int i
= 0; i
< number_of_lines
; ++i
) {
68 EXPECT_EQ(static_cast<int>(sizeof(kTestTime
)) - 1,
69 base::WritePlatformFileAtCurrentPos(test_list_file
,
71 sizeof(kTestTime
) - 1));
72 EXPECT_EQ(1, base::WritePlatformFileAtCurrentPos(test_list_file
, ",", 1));
73 EXPECT_EQ(static_cast<int>(sizeof(kTestReportId
)) - 1,
74 base::WritePlatformFileAtCurrentPos(test_list_file
,
76 sizeof(kTestReportId
) - 1));
77 EXPECT_EQ(1, base::WritePlatformFileAtCurrentPos(test_list_file
,
80 EXPECT_TRUE(base::ClosePlatformFile(test_list_file
));
85 base::FilePath test_list_path_
;
88 TEST_F(WebRtcLogUploaderTest
, AddUploadedLogInfoToUploadListFile
) {
89 // Get a temporary filename. We don't want the file to exist to begin with
90 // since that's the normal use case, hence the delete.
91 ASSERT_TRUE(base::CreateTemporaryFile(&test_list_path_
));
92 EXPECT_TRUE(base::DeleteFile(test_list_path_
, false));
93 scoped_ptr
<WebRtcLogUploader
> webrtc_log_uploader_(
94 new WebRtcLogUploader());
96 webrtc_log_uploader_
->AddUploadedLogInfoToUploadListFile(test_list_path_
,
98 webrtc_log_uploader_
->AddUploadedLogInfoToUploadListFile(test_list_path_
,
100 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(2));
102 const int expected_line_limit
= 50;
103 ASSERT_TRUE(AddLinesToTestFile(expected_line_limit
- 2));
104 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit
));
106 webrtc_log_uploader_
->AddUploadedLogInfoToUploadListFile(test_list_path_
,
108 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit
));
110 ASSERT_TRUE(AddLinesToTestFile(10));
111 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(60));
113 webrtc_log_uploader_
->AddUploadedLogInfoToUploadListFile(test_list_path_
,
115 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit
));