OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chrome/browser/android/history_report/usage_reports_buffer_backend.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "chrome/browser/android/history_report/usage_report_util.h" |
| 11 #include "chrome/browser/android/proto/delta_file.pb.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 void VerifyUsageReport(history_report::UsageReport& actual, |
| 16 const std::string& expected_id, |
| 17 int64 expected_timestamp_ms, |
| 18 bool expected_typed_visit) { |
| 19 EXPECT_EQ(expected_id, actual.id()); |
| 20 EXPECT_EQ(expected_timestamp_ms, actual.timestamp_ms()); |
| 21 EXPECT_EQ(expected_typed_visit, actual.typed_visit()); |
| 22 } |
| 23 } // namespace |
| 24 |
| 25 namespace history_report { |
| 26 |
| 27 class UsageReportsBufferBackendTest : public testing::Test { |
| 28 public: |
| 29 UsageReportsBufferBackendTest() {} |
| 30 ~UsageReportsBufferBackendTest() override {} |
| 31 |
| 32 protected: |
| 33 void SetUp() override { |
| 34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 35 buffer_.reset(new UsageReportsBufferBackend(temp_dir_.path())); |
| 36 EXPECT_TRUE(buffer_->Init()); |
| 37 } |
| 38 |
| 39 scoped_ptr<UsageReportsBufferBackend> buffer_; |
| 40 base::ScopedTempDir temp_dir_; |
| 41 |
| 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(UsageReportsBufferBackendTest); |
| 44 }; |
| 45 |
| 46 TEST_F(UsageReportsBufferBackendTest, AddTypedVisit) { |
| 47 buffer_->AddVisit("id", 7, true); |
| 48 |
| 49 scoped_ptr<std::vector<UsageReport> > result = |
| 50 buffer_->GetUsageReportsBatch(1); |
| 51 |
| 52 EXPECT_TRUE(result.get() != NULL); |
| 53 EXPECT_EQ(1u, result->size()); |
| 54 VerifyUsageReport((*result)[0], "id", 7, true); |
| 55 } |
| 56 |
| 57 TEST_F(UsageReportsBufferBackendTest, AddNotTypedVisit) { |
| 58 buffer_->AddVisit("id", 7, false); |
| 59 |
| 60 scoped_ptr<std::vector<UsageReport> > result = |
| 61 buffer_->GetUsageReportsBatch(1); |
| 62 |
| 63 EXPECT_TRUE(result.get() != NULL); |
| 64 EXPECT_EQ(1u, result->size()); |
| 65 VerifyUsageReport((*result)[0], "id", 7, false); |
| 66 } |
| 67 |
| 68 TEST_F(UsageReportsBufferBackendTest, GetUsageReportsBatchNotEnoughReports) { |
| 69 buffer_->AddVisit("id", 7, true); |
| 70 buffer_->AddVisit("id2", 10, false); |
| 71 buffer_->AddVisit("id3", 12, false); |
| 72 buffer_->AddVisit("id4", 5, true); |
| 73 |
| 74 scoped_ptr<std::vector<UsageReport> > result = |
| 75 buffer_->GetUsageReportsBatch(5); |
| 76 |
| 77 EXPECT_TRUE(result.get() != NULL); |
| 78 EXPECT_EQ(4u, result->size()); |
| 79 std::set<std::string> ids; |
| 80 for (std::vector<UsageReport>::iterator it = result->begin(); |
| 81 it != result->end(); |
| 82 ++it) { |
| 83 ids.insert(it->id()); |
| 84 if (it->id() == "id") { |
| 85 VerifyUsageReport(*it, "id", 7, true); |
| 86 continue; |
| 87 } |
| 88 if (it->id() == "id2") { |
| 89 VerifyUsageReport(*it, "id2", 10, false); |
| 90 continue; |
| 91 } |
| 92 if (it->id() == "id3") { |
| 93 VerifyUsageReport(*it, "id3", 12, false); |
| 94 continue; |
| 95 } |
| 96 if (it->id() == "id4") { |
| 97 VerifyUsageReport(*it, "id4", 5, true); |
| 98 continue; |
| 99 } |
| 100 FAIL(); |
| 101 } |
| 102 EXPECT_EQ(4u, ids.size()); |
| 103 } |
| 104 |
| 105 TEST_F(UsageReportsBufferBackendTest, GetUsageReportsBatchTooManyReports) { |
| 106 buffer_->AddVisit("id", 7, true); |
| 107 buffer_->AddVisit("id2", 10, false); |
| 108 buffer_->AddVisit("id3", 12, false); |
| 109 buffer_->AddVisit("id4", 5, true); |
| 110 |
| 111 scoped_ptr<std::vector<UsageReport> > result = |
| 112 buffer_->GetUsageReportsBatch(3); |
| 113 |
| 114 EXPECT_TRUE(result.get() != NULL); |
| 115 EXPECT_EQ(3u, result->size()); |
| 116 std::set<std::string> ids; |
| 117 for (std::vector<UsageReport>::iterator it = result->begin(); |
| 118 it != result->end(); |
| 119 ++it) { |
| 120 ids.insert(it->id()); |
| 121 if (it->id() == "id") { |
| 122 VerifyUsageReport(*it, "id", 7, true); |
| 123 continue; |
| 124 } |
| 125 if (it->id() == "id2") { |
| 126 VerifyUsageReport(*it, "id2", 10, false); |
| 127 continue; |
| 128 } |
| 129 if (it->id() == "id3") { |
| 130 VerifyUsageReport(*it, "id3", 12, false); |
| 131 continue; |
| 132 } |
| 133 if (it->id() == "id4") { |
| 134 VerifyUsageReport(*it, "id4", 5, true); |
| 135 continue; |
| 136 } |
| 137 FAIL(); |
| 138 } |
| 139 EXPECT_EQ(3u, ids.size()); |
| 140 } |
| 141 |
| 142 TEST_F(UsageReportsBufferBackendTest, Remove) { |
| 143 buffer_->AddVisit("id", 7, true); |
| 144 |
| 145 scoped_ptr<std::vector<UsageReport> > result = |
| 146 buffer_->GetUsageReportsBatch(1); |
| 147 |
| 148 EXPECT_TRUE(result.get() != NULL); |
| 149 EXPECT_EQ(1u, result->size()); |
| 150 VerifyUsageReport((*result)[0], "id", 7, true); |
| 151 |
| 152 // Query for a second time to make sure that previous query didn't remove |
| 153 // anything from the buffer. |
| 154 result = buffer_->GetUsageReportsBatch(1); |
| 155 |
| 156 EXPECT_TRUE(result.get() != NULL); |
| 157 EXPECT_EQ(1u, result->size()); |
| 158 VerifyUsageReport((*result)[0], "id", 7, true); |
| 159 |
| 160 std::vector<std::string> to_remove; |
| 161 |
| 162 for (std::vector<UsageReport>::const_iterator it = result->begin(); |
| 163 it != result->end(); |
| 164 ++it) { |
| 165 to_remove.push_back(usage_report_util::ReportToKey(*it)); |
| 166 } |
| 167 |
| 168 buffer_->Remove(to_remove); |
| 169 |
| 170 result = buffer_->GetUsageReportsBatch(1); |
| 171 |
| 172 EXPECT_TRUE(result.get() != NULL); |
| 173 EXPECT_EQ(0u, result->size()); |
| 174 } |
| 175 |
| 176 TEST_F(UsageReportsBufferBackendTest, Persistence) { |
| 177 buffer_->AddVisit("id", 7, true); |
| 178 |
| 179 scoped_ptr<std::vector<UsageReport> > result = |
| 180 buffer_->GetUsageReportsBatch(2); |
| 181 |
| 182 EXPECT_TRUE(result.get() != NULL); |
| 183 EXPECT_EQ(1u, result->size()); |
| 184 VerifyUsageReport((*result)[0], "id", 7, true); |
| 185 |
| 186 buffer_.reset(NULL); |
| 187 buffer_.reset(new UsageReportsBufferBackend(temp_dir_.path())); |
| 188 EXPECT_TRUE(buffer_->Init()); |
| 189 |
| 190 result = buffer_->GetUsageReportsBatch(2); |
| 191 EXPECT_TRUE(result.get() != NULL); |
| 192 EXPECT_EQ(1u, result->size()); |
| 193 VerifyUsageReport((*result)[0], "id", 7, true); |
| 194 } |
| 195 |
| 196 } // namespace history_report |
OLD | NEW |