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/platform_file.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/time/time.h" | |
13 #include "chrome/browser/media/webrtc_log_uploader.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 const char kTestReportId[] = "123456789"; | |
17 const char kTestTime[] = "987654321"; | |
18 | |
19 class WebRtcLogUploaderTest : public testing::Test { | |
20 public: | |
21 WebRtcLogUploaderTest() {} | |
22 | |
23 void VerifyNumberOfLinesAndContentsOfLastLine(int expected_lines) { | |
24 // Expected max size is 50 lines * ~35 chars/bytes per line. Start with that | |
25 // size multiplied by 3 to have some margin. | |
26 std::string contents(3 * 50 * 35, '\0'); | |
27 int read = file_util::ReadFile(test_list_path_, &contents[0], | |
28 contents.size()); | |
29 EXPECT_NE(-1, read); | |
30 contents.resize(read); | |
31 | |
32 // Verify expected number of lines. | |
33 std::vector<std::string> lines; | |
34 base::SplitString(contents, '\n', &lines); | |
35 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.
| |
36 EXPECT_TRUE(lines[expected_lines].empty()); | |
37 | |
38 // Verify the contents of the last line, the time (line_parts[0]) is the | |
39 // time when the info was written to the file which we don't know, so just | |
40 // skip verify the exact contents. | |
41 std::vector<std::string> line_parts; | |
42 base::SplitString(lines[expected_lines - 1], ',', &line_parts); | |
43 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
:)
| |
44 EXPECT_FALSE(line_parts[0].empty()); | |
45 EXPECT_STREQ(kTestReportId, line_parts[1].c_str()); | |
46 } | |
47 | |
48 void AddLinesToTestFile(int number_of_lines) { | |
49 int flags = base::PLATFORM_FILE_OPEN | | |
50 base::PLATFORM_FILE_APPEND; | |
51 base::PlatformFileError error = base::PLATFORM_FILE_OK; | |
52 base::PlatformFile test_list_file = | |
53 base::CreatePlatformFile(test_list_path_, flags, NULL, &error); | |
54 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | |
55 ASSERT_NE(base::kInvalidPlatformFileValue, test_list_file); | |
56 | |
57 for (int i = 0; i < number_of_lines; ++i) { | |
58 EXPECT_EQ(static_cast<int>(sizeof(kTestTime)) - 1, | |
59 base::WritePlatformFileAtCurrentPos(test_list_file, | |
60 kTestTime, | |
61 sizeof(kTestTime) - 1)); | |
62 EXPECT_EQ(1, base::WritePlatformFileAtCurrentPos(test_list_file, ",", 1)); | |
63 EXPECT_EQ(static_cast<int>(sizeof(kTestReportId)) - 1, | |
64 base::WritePlatformFileAtCurrentPos(test_list_file, | |
65 kTestReportId, | |
66 sizeof(kTestReportId) - 1)); | |
67 EXPECT_EQ(1, base::WritePlatformFileAtCurrentPos(test_list_file, | |
68 "\n", 1)); | |
69 } | |
70 EXPECT_TRUE(base::ClosePlatformFile(test_list_file)); | |
71 } | |
72 | |
73 base::FilePath test_list_path_; | |
74 }; | |
75 | |
76 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
| |
77 ASSERT_TRUE(file_util::CreateTemporaryFile(&test_list_path_)); | |
78 scoped_ptr<WebRtcLogUploader> webrtc_log_uploader_( | |
79 new WebRtcLogUploader(test_list_path_)); | |
80 | |
81 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); | |
82 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); | |
83 VerifyNumberOfLinesAndContentsOfLastLine(2); | |
84 | |
85 const int expected_line_limit = 50; | |
86 AddLinesToTestFile(expected_line_limit - 2); | |
87 VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit); | |
88 | |
89 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); | |
90 VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit); | |
91 | |
92 AddLinesToTestFile(10); | |
93 VerifyNumberOfLinesAndContentsOfLastLine(60); | |
94 | |
95 webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(kTestReportId); | |
96 VerifyNumberOfLinesAndContentsOfLastLine(expected_line_limit); | |
97 } | |
OLD | NEW |