Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / webrtc_log_uploader_unittest.cc
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.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.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 kTestTime[] = "time";
18 const char kTestReportId[] = "report-id";
19 const char kTestLocalId[] = "local-id";
20
21 class WebRtcLogUploaderTest : public testing::Test {
22  public:
23   WebRtcLogUploaderTest() {}
24
25   bool VerifyNumberOfLines(int expected_lines) {
26     std::vector<std::string> lines = GetLinesFromListFile();
27     EXPECT_EQ(expected_lines, static_cast<int>(lines.size()));
28     return expected_lines == static_cast<int>(lines.size());
29   }
30
31   bool VerifyLastLineHasAllInfo() {
32     std::string last_line = GetLastLineFromListFile();
33     if (last_line.empty())
34       return false;
35     std::vector<std::string> line_parts;
36     base::SplitString(last_line, ',', &line_parts);
37     EXPECT_EQ(3u, line_parts.size());
38     if (3u != line_parts.size())
39       return false;
40     // The time (line_parts[0]) is the time when the info was written to the
41     // file which we don't know, so just verify that it's not empty.
42     EXPECT_FALSE(line_parts[0].empty());
43     EXPECT_STREQ(kTestReportId, line_parts[1].c_str());
44     EXPECT_STREQ(kTestLocalId, line_parts[2].c_str());
45     return true;
46   }
47
48   bool VerifyLastLineHasLocalIdOnly() {
49     std::string last_line = GetLastLineFromListFile();
50     if (last_line.empty())
51       return false;
52     std::vector<std::string> line_parts;
53     base::SplitString(last_line, ',', &line_parts);
54     EXPECT_EQ(3u, line_parts.size());
55     if (3u != line_parts.size())
56       return false;
57     EXPECT_TRUE(line_parts[0].empty());
58     EXPECT_TRUE(line_parts[1].empty());
59     EXPECT_STREQ(kTestLocalId, line_parts[2].c_str());
60     return true;
61   }
62
63   bool VerifyLastLineHasUploadTimeAndIdOnly() {
64     std::string last_line = GetLastLineFromListFile();
65     if (last_line.empty())
66       return false;
67     std::vector<std::string> line_parts;
68     base::SplitString(last_line, ',', &line_parts);
69     EXPECT_EQ(3u, line_parts.size());
70     if (3u != line_parts.size())
71       return false;
72     EXPECT_FALSE(line_parts[0].empty());
73     EXPECT_STREQ(kTestReportId, line_parts[1].c_str());
74     EXPECT_TRUE(line_parts[2].empty());
75     return true;
76   }
77
78   bool AddLinesToTestFile(int number_of_lines) {
79     base::File test_list_file(test_list_path_,
80                               base::File::FLAG_OPEN | base::File::FLAG_APPEND);
81     EXPECT_TRUE(test_list_file.IsValid());
82     if (!test_list_file.IsValid())
83       return false;
84
85     for (int i = 0; i < number_of_lines; ++i) {
86       EXPECT_EQ(static_cast<int>(sizeof(kTestTime)) - 1,
87                 test_list_file.WriteAtCurrentPos(kTestTime,
88                                                  sizeof(kTestTime) - 1));
89       EXPECT_EQ(1, test_list_file.WriteAtCurrentPos(",", 1));
90       EXPECT_EQ(static_cast<int>(sizeof(kTestReportId)) - 1,
91                 test_list_file.WriteAtCurrentPos(kTestReportId,
92                                                  sizeof(kTestReportId) - 1));
93       EXPECT_EQ(1, test_list_file.WriteAtCurrentPos(",", 1));
94       EXPECT_EQ(static_cast<int>(sizeof(kTestLocalId)) - 1,
95                 test_list_file.WriteAtCurrentPos(kTestLocalId,
96                                                  sizeof(kTestLocalId) - 1));
97       EXPECT_EQ(1, test_list_file.WriteAtCurrentPos("\n", 1));
98     }
99     return true;
100   }
101
102   std::vector<std::string> GetLinesFromListFile() {
103     std::string contents;
104     int read = base::ReadFileToString(test_list_path_, &contents);
105     EXPECT_GT(read, 0);
106     if (read == 0)
107       return std::vector<std::string>();
108     // Since every line should end with '\n', the last line should be empty. So
109     // we expect at least two lines including the final empty. Remove the empty
110     // line before returning.
111     std::vector<std::string> lines;
112     base::SplitString(contents, '\n', &lines);
113     EXPECT_GT(lines.size(), 1u);
114     if (lines.size() < 2)
115       return std::vector<std::string>();
116     EXPECT_TRUE(lines[lines.size() - 1].empty());
117     if (!lines[lines.size() - 1].empty())
118       return std::vector<std::string>();
119     lines.pop_back();
120     return lines;
121   }
122
123   std::string GetLastLineFromListFile() {
124     std::vector<std::string> lines = GetLinesFromListFile();
125     EXPECT_GT(lines.size(), 0u);
126     if (lines.empty())
127       return std::string();
128     return lines[lines.size() - 1];
129   }
130
131   base::FilePath test_list_path_;
132 };
133
134 TEST_F(WebRtcLogUploaderTest, AddLocallyStoredLogInfoToUploadListFile) {
135   // Get a temporary filename. We don't want the file to exist to begin with
136   // since that's the normal use case, hence the delete.
137   ASSERT_TRUE(base::CreateTemporaryFile(&test_list_path_));
138   EXPECT_TRUE(base::DeleteFile(test_list_path_, false));
139   scoped_ptr<WebRtcLogUploader> webrtc_log_uploader_(
140       new WebRtcLogUploader());
141
142   webrtc_log_uploader_->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
143                                                                 kTestLocalId);
144   webrtc_log_uploader_->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
145                                                                 kTestLocalId);
146   ASSERT_TRUE(VerifyNumberOfLines(2));
147   ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
148
149   const int expected_line_limit = 50;
150   ASSERT_TRUE(AddLinesToTestFile(expected_line_limit - 2));
151   ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit));
152   ASSERT_TRUE(VerifyLastLineHasAllInfo());
153
154   webrtc_log_uploader_->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
155                                                                 kTestLocalId);
156   ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit));
157   ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
158
159   ASSERT_TRUE(AddLinesToTestFile(10));
160   ASSERT_TRUE(VerifyNumberOfLines(60));
161   ASSERT_TRUE(VerifyLastLineHasAllInfo());
162
163   webrtc_log_uploader_->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
164                                                                 kTestLocalId);
165   ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit));
166   ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
167
168   webrtc_log_uploader_->StartShutdown();
169 }
170
171 TEST_F(WebRtcLogUploaderTest, AddUploadedLogInfoToUploadListFile) {
172   // Get a temporary filename. We don't want the file to exist to begin with
173   // since that's the normal use case, hence the delete.
174   ASSERT_TRUE(base::CreateTemporaryFile(&test_list_path_));
175   EXPECT_TRUE(base::DeleteFile(test_list_path_, false));
176   scoped_ptr<WebRtcLogUploader> webrtc_log_uploader_(new WebRtcLogUploader());
177
178   webrtc_log_uploader_->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
179                                                                 kTestLocalId);
180   ASSERT_TRUE(VerifyNumberOfLines(1));
181   ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
182
183   webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(
184       test_list_path_, kTestLocalId, kTestReportId);
185   ASSERT_TRUE(VerifyNumberOfLines(1));
186   ASSERT_TRUE(VerifyLastLineHasAllInfo());
187
188   // Use a local ID that should not be found in the list.
189   webrtc_log_uploader_->AddUploadedLogInfoToUploadListFile(
190       test_list_path_, "dummy id", kTestReportId);
191   ASSERT_TRUE(VerifyNumberOfLines(2));
192   ASSERT_TRUE(VerifyLastLineHasUploadTimeAndIdOnly());
193
194   webrtc_log_uploader_->StartShutdown();
195 }