OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 |
| 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" |
| 16 |
| 17 const char kTestReportId[] = "123456789"; |
| 18 const char kTestTime[] = "987654321"; |
| 19 |
| 20 class WebRtcLogUploaderTest : public testing::Test { |
| 21 public: |
| 22 WebRtcLogUploaderTest() {} |
| 23 |
| 24 bool VerifyNumberOfLinesAndContentsOfLastLine(int expected_lines) { |
| 25 std::string contents; |
| 26 int read = file_util::ReadFileToString(test_list_path_, &contents); |
| 27 EXPECT_GT(read, 0); |
| 28 if (read <= 0) |
| 29 return false; |
| 30 |
| 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())) |
| 37 return false; |
| 38 EXPECT_TRUE(lines[expected_lines].empty()); |
| 39 |
| 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()) |
| 47 return false; |
| 48 EXPECT_FALSE(line_parts[0].empty()); |
| 49 EXPECT_STREQ(kTestReportId, line_parts[1].c_str()); |
| 50 |
| 51 return true; |
| 52 } |
| 53 |
| 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) { |
| 64 return false; |
| 65 } |
| 66 |
| 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, |
| 70 kTestTime, |
| 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, |
| 75 kTestReportId, |
| 76 sizeof(kTestReportId) - 1)); |
| 77 EXPECT_EQ(1, base::WritePlatformFileAtCurrentPos(test_list_file, |
| 78 "\n", 1)); |
| 79 } |
| 80 EXPECT_TRUE(base::ClosePlatformFile(test_list_file)); |
| 81 |
| 82 return true; |
| 83 } |
| 84 |
| 85 base::FilePath test_list_path_; |
| 86 }; |
| 87 |
| 88 TEST_F(WebRtcLogUploaderTest, AddUploadedLogInfoToUploadListFile) { |
| 89 ASSERT_TRUE(file_util::CreateTemporaryFile(&test_list_path_)); |
| 90 scoped_ptr<WebRtcLogUploader> webrtc_log_uploader_( |
| 91 new WebRtcLogUploader()); |
| 92 webrtc_log_uploader_->SetUploadPathForTesting(test_list_path_); |
| 93 |
| 94 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| 95 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| 96 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(2)); |
| 97 |
| 98 const int expected_line_limit = 50; |
| 99 ASSERT_TRUE(AddLinesToTestFile(expected_line_limit - 2)); |
| 100 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit)); |
| 101 |
| 102 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| 103 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit)); |
| 104 |
| 105 ASSERT_TRUE(AddLinesToTestFile(10)); |
| 106 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(60)); |
| 107 |
| 108 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| 109 ASSERT_TRUE(VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit)); |
| 110 } |
OLD | NEW |