Chromium Code Reviews| Index: chrome/browser/media/webrtc_log_uploader_unittest.cc |
| diff --git a/chrome/browser/media/webrtc_log_uploader_unittest.cc b/chrome/browser/media/webrtc_log_uploader_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..513a1bd30d8069bc4a51ad3af39cf5d179521009 |
| --- /dev/null |
| +++ b/chrome/browser/media/webrtc_log_uploader_unittest.cc |
| @@ -0,0 +1,97 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| + |
| +#include "base/file_util.h" |
| +#include "base/files/file_path.h" |
| +#include "base/platform_file.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/time/time.h" |
| +#include "chrome/browser/media/webrtc_log_uploader.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +const char kTestReportId[] = "123456789"; |
| +const char kTestTime[] = "987654321"; |
| + |
| +class WebRtcLogUploaderTest : public testing::Test { |
| + public: |
| + WebRtcLogUploaderTest() {} |
| + |
| + void VerifyNumberOfLinesAndContentsOfLastLine(int expected_lines) { |
| + // Expected max size is 50 lines * ~35 chars/bytes per line. Start with that |
| + // size multiplied by 3 to have some margin. |
| + std::string contents(3 * 50 * 35, '\0'); |
| + int read = file_util::ReadFile(test_list_path_, &contents[0], |
| + contents.size()); |
| + EXPECT_NE(-1, read); |
| + contents.resize(read); |
| + |
| + // Verify expected number of lines. |
| + std::vector<std::string> lines; |
| + base::SplitString(contents, '\n', &lines); |
| + EXPECT_EQ(expected_lines + 1, static_cast<int>(lines.size())); |
|
tommi (sloooow) - chröme
2013/07/01 12:01:01
maybe this should be ASSERT_EQ to avoid executing
Henrik Grunell
2013/07/01 13:26:22
Done.
|
| + EXPECT_TRUE(lines[expected_lines].empty()); |
| + |
| + // Verify the contents of the last line, the time (line_parts[0]) is the |
| + // time when the info was written to the file which we don't know, so just |
| + // skip verify the exact contents. |
| + std::vector<std::string> line_parts; |
| + base::SplitString(lines[expected_lines - 1], ',', &line_parts); |
| + ASSERT_EQ(2u, line_parts.size()); |
|
tommi (sloooow) - chröme
2013/07/01 12:01:01
... like you do here :)
Henrik Grunell
2013/07/01 13:26:22
:)
|
| + EXPECT_FALSE(line_parts[0].empty()); |
| + EXPECT_STREQ(kTestReportId, line_parts[1].c_str()); |
| + } |
| + |
| + void AddLinesToTestFile(int number_of_lines) { |
| + int flags = base::PLATFORM_FILE_OPEN | |
| + base::PLATFORM_FILE_APPEND; |
| + base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| + base::PlatformFile test_list_file = |
| + base::CreatePlatformFile(test_list_path_, flags, NULL, &error); |
| + ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| + ASSERT_NE(base::kInvalidPlatformFileValue, test_list_file); |
| + |
| + for (int i = 0; i < number_of_lines; ++i) { |
| + EXPECT_EQ(static_cast<int>(sizeof(kTestTime)) - 1, |
| + base::WritePlatformFileAtCurrentPos(test_list_file, |
| + kTestTime, |
| + sizeof(kTestTime) - 1)); |
| + EXPECT_EQ(1, base::WritePlatformFileAtCurrentPos(test_list_file, ",", 1)); |
| + EXPECT_EQ(static_cast<int>(sizeof(kTestReportId)) - 1, |
| + base::WritePlatformFileAtCurrentPos(test_list_file, |
| + kTestReportId, |
| + sizeof(kTestReportId) - 1)); |
| + EXPECT_EQ(1, base::WritePlatformFileAtCurrentPos(test_list_file, |
| + "\n", 1)); |
| + } |
| + EXPECT_TRUE(base::ClosePlatformFile(test_list_file)); |
| + } |
| + |
| + base::FilePath test_list_path_; |
| +}; |
| + |
| +TEST_F(WebRtcLogUploaderTest, Basic) { |
|
tommi (sloooow) - chröme
2013/07/01 12:01:01
does this test the trimming (or not) of the file?
Henrik Grunell
2013/07/01 13:26:22
Yes it does, the conditional error in the tested f
|
| + ASSERT_TRUE(file_util::CreateTemporaryFile(&test_list_path_)); |
| + scoped_ptr<WebRtcLogUploader> webrtc_log_uploader_( |
| + new WebRtcLogUploader(test_list_path_)); |
| + |
| + webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| + webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| + VerifyNumberOfLinesAndContentsOfLastLine(2); |
| + |
| + const int expected_line_limit = 50; |
| + AddLinesToTestFile(expected_line_limit - 2); |
| + VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit); |
| + |
| + webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| + VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit); |
| + |
| + AddLinesToTestFile(10); |
| + VerifyNumberOfLinesAndContentsOfLastLine(60); |
| + |
| + webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); |
| + VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit); |
| +} |