| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <utility> | 6 #include <utility> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "chrome/common/metrics/metrics_log_base.h" | 9 #include "chrome/common/metrics/metrics_log_base.h" |
| 10 #include "chrome/common/metrics/metrics_log_manager.h" | 10 #include "chrome/common/metrics/metrics_log_manager.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 class MetricsLogManagerTest : public testing::Test { | 15 class MetricsLogManagerTest : public testing::Test { |
| 16 }; | 16 }; |
| 17 | 17 |
| 18 // Dummy serializer that just stores logs in memory. | 18 // Dummy serializer that just stores logs in memory. |
| 19 class DummyLogSerializer : public MetricsLogManager::LogSerializer { | 19 class DummyLogSerializer : public MetricsLogManager::LogSerializer { |
| 20 public: | 20 public: |
| 21 virtual void SerializeLogs(const std::vector<std::string>& logs, | 21 virtual void SerializeLogs( |
| 22 MetricsLogManager::LogType log_type) OVERRIDE { | 22 const std::vector<MetricsLogManager::SerializedLog>& logs, |
| 23 MetricsLogManager::LogType log_type) OVERRIDE { |
| 23 persisted_logs_[log_type] = logs; | 24 persisted_logs_[log_type] = logs; |
| 24 } | 25 } |
| 25 | 26 |
| 26 virtual void DeserializeLogs(MetricsLogManager::LogType log_type, | 27 virtual void DeserializeLogs( |
| 27 std::vector<std::string>* logs) OVERRIDE { | 28 MetricsLogManager::LogType log_type, |
| 29 std::vector<MetricsLogManager::SerializedLog>* logs) OVERRIDE { |
| 28 ASSERT_NE(static_cast<void*>(NULL), logs); | 30 ASSERT_NE(static_cast<void*>(NULL), logs); |
| 29 *logs = persisted_logs_[log_type]; | 31 *logs = persisted_logs_[log_type]; |
| 30 } | 32 } |
| 31 | 33 |
| 32 // Returns the number of logs of the given type. | 34 // Returns the number of logs of the given type. |
| 33 size_t TypeCount(MetricsLogManager::LogType log_type) { | 35 size_t TypeCount(MetricsLogManager::LogType log_type) { |
| 34 return persisted_logs_[log_type].size(); | 36 return persisted_logs_[log_type].size(); |
| 35 } | 37 } |
| 36 | 38 |
| 37 // In-memory "persitent storage". | 39 // In-memory "persitent storage". |
| 38 std::vector<std::string> persisted_logs_[2]; | 40 std::vector<MetricsLogManager::SerializedLog> persisted_logs_[2]; |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 } // namespace | 43 } // namespace |
| 42 | 44 |
| 43 TEST(MetricsLogManagerTest, StandardFlow) { | 45 TEST(MetricsLogManagerTest, StandardFlow) { |
| 44 MetricsLogManager log_manager; | 46 MetricsLogManager log_manager; |
| 45 | 47 |
| 46 // Make sure a new manager has a clean slate. | 48 // Make sure a new manager has a clean slate. |
| 47 EXPECT_EQ(NULL, log_manager.current_log()); | 49 EXPECT_EQ(NULL, log_manager.current_log()); |
| 48 EXPECT_FALSE(log_manager.has_staged_log()); | 50 EXPECT_FALSE(log_manager.has_staged_log()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // has the right type. | 134 // has the right type. |
| 133 DummyLogSerializer* serializer = new DummyLogSerializer; | 135 DummyLogSerializer* serializer = new DummyLogSerializer; |
| 134 log_manager.set_log_serializer(serializer); | 136 log_manager.set_log_serializer(serializer); |
| 135 log_manager.FinishCurrentLog(); | 137 log_manager.FinishCurrentLog(); |
| 136 log_manager.PersistUnsentLogs(); | 138 log_manager.PersistUnsentLogs(); |
| 137 EXPECT_EQ(0U, serializer->TypeCount(MetricsLogManager::INITIAL_LOG)); | 139 EXPECT_EQ(0U, serializer->TypeCount(MetricsLogManager::INITIAL_LOG)); |
| 138 EXPECT_EQ(1U, serializer->TypeCount(MetricsLogManager::ONGOING_LOG)); | 140 EXPECT_EQ(1U, serializer->TypeCount(MetricsLogManager::ONGOING_LOG)); |
| 139 } | 141 } |
| 140 | 142 |
| 141 TEST(MetricsLogManagerTest, StoreAndLoad) { | 143 TEST(MetricsLogManagerTest, StoreAndLoad) { |
| 142 std::vector<std::string> initial_logs; | 144 std::vector<MetricsLogManager::SerializedLog> initial_logs; |
| 143 std::vector<std::string> ongoing_logs; | 145 std::vector<MetricsLogManager::SerializedLog> ongoing_logs; |
| 144 | 146 |
| 145 // Set up some in-progress logging in a scoped log manager simulating the | 147 // Set up some in-progress logging in a scoped log manager simulating the |
| 146 // leadup to quitting, then persist as would be done on quit. | 148 // leadup to quitting, then persist as would be done on quit. |
| 147 { | 149 { |
| 148 MetricsLogManager log_manager; | 150 MetricsLogManager log_manager; |
| 149 DummyLogSerializer* serializer = new DummyLogSerializer; | 151 DummyLogSerializer* serializer = new DummyLogSerializer; |
| 150 log_manager.set_log_serializer(serializer); | 152 log_manager.set_log_serializer(serializer); |
| 151 // Simulate a log having already been unsent from a previous session. | 153 // Simulate a log having already been unsent from a previous session. |
| 152 std::string log = "proto"; | 154 MetricsLogManager::SerializedLog log; |
| 155 log.SetLogText("proto"); |
| 153 serializer->persisted_logs_[MetricsLogManager::ONGOING_LOG].push_back(log); | 156 serializer->persisted_logs_[MetricsLogManager::ONGOING_LOG].push_back(log); |
| 154 EXPECT_FALSE(log_manager.has_unsent_logs()); | 157 EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 155 log_manager.LoadPersistedUnsentLogs(); | 158 log_manager.LoadPersistedUnsentLogs(); |
| 156 EXPECT_TRUE(log_manager.has_unsent_logs()); | 159 EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 157 | 160 |
| 158 MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version"); | 161 MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version"); |
| 159 MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version"); | 162 MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version"); |
| 160 log_manager.BeginLoggingWithLog(log1, MetricsLogManager::INITIAL_LOG); | 163 log_manager.BeginLoggingWithLog(log1, MetricsLogManager::INITIAL_LOG); |
| 161 log_manager.FinishCurrentLog(); | 164 log_manager.FinishCurrentLog(); |
| 162 log_manager.BeginLoggingWithLog(log2, MetricsLogManager::ONGOING_LOG); | 165 log_manager.BeginLoggingWithLog(log2, MetricsLogManager::ONGOING_LOG); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE); | 339 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE); |
| 337 log_manager.DiscardLastProvisionalStore(); | 340 log_manager.DiscardLastProvisionalStore(); |
| 338 log_manager.DiscardLastProvisionalStore(); | 341 log_manager.DiscardLastProvisionalStore(); |
| 339 | 342 |
| 340 DummyLogSerializer* serializer = new DummyLogSerializer; | 343 DummyLogSerializer* serializer = new DummyLogSerializer; |
| 341 log_manager.set_log_serializer(serializer); | 344 log_manager.set_log_serializer(serializer); |
| 342 log_manager.PersistUnsentLogs(); | 345 log_manager.PersistUnsentLogs(); |
| 343 EXPECT_EQ(1U, serializer->TypeCount(MetricsLogManager::ONGOING_LOG)); | 346 EXPECT_EQ(1U, serializer->TypeCount(MetricsLogManager::ONGOING_LOG)); |
| 344 } | 347 } |
| 345 } | 348 } |
| OLD | NEW |