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> |
| 6 #include <utility> |
| 7 #include <vector> |
| 8 |
5 #include "chrome/common/metrics/metrics_log_base.h" | 9 #include "chrome/common/metrics/metrics_log_base.h" |
6 #include "chrome/common/metrics/metrics_log_manager.h" | 10 #include "chrome/common/metrics/metrics_log_manager.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
8 | 12 |
9 #include <string> | 13 typedef MetricsLogManager::SerializedLog SerializedLog; |
10 #include <vector> | |
11 | 14 |
12 namespace { | 15 namespace { |
| 16 |
13 class MetricsLogManagerTest : public testing::Test { | 17 class MetricsLogManagerTest : public testing::Test { |
14 }; | 18 }; |
15 | 19 |
16 // Dummy serializer that just stores logs in memory. | 20 // Dummy serializer that just stores logs in memory. |
17 class DummyLogSerializer : public MetricsLogManager::LogSerializer { | 21 class DummyLogSerializer : public MetricsLogManager::LogSerializer { |
18 public: | 22 public: |
19 virtual void SerializeLogs(const std::vector<std::string>& logs, | 23 virtual void SerializeLogs(const std::vector<SerializedLog>& logs, |
20 MetricsLogManager::LogType log_type) { | 24 MetricsLogManager::LogType log_type) { |
21 persisted_logs_[log_type] = logs; | 25 persisted_logs_[log_type] = logs; |
22 } | 26 } |
23 | 27 |
24 virtual void DeserializeLogs(MetricsLogManager::LogType log_type, | 28 virtual void DeserializeLogs(MetricsLogManager::LogType log_type, |
25 std::vector<std::string>* logs) { | 29 std::vector<SerializedLog>* logs) { |
26 ASSERT_NE(static_cast<void*>(NULL), logs); | 30 ASSERT_NE(static_cast<void*>(NULL), logs); |
27 *logs = persisted_logs_[log_type]; | 31 *logs = persisted_logs_[log_type]; |
28 } | 32 } |
29 | 33 |
30 // Returns the number of logs of the given type. | 34 // Returns the number of logs of the given type. |
31 size_t TypeCount(MetricsLogManager::LogType log_type) { | 35 size_t TypeCount(MetricsLogManager::LogType log_type) { |
32 return persisted_logs_[log_type].size(); | 36 return persisted_logs_[log_type].size(); |
33 } | 37 } |
34 | 38 |
35 // In-memory "persitent storage". | 39 // In-memory "persitent storage". |
36 std::vector<std::string> persisted_logs_[2]; | 40 std::vector<SerializedLog> persisted_logs_[2]; |
37 }; | 41 }; |
38 }; // namespace | 42 |
| 43 } // namespace |
39 | 44 |
40 TEST(MetricsLogManagerTest, StandardFlow) { | 45 TEST(MetricsLogManagerTest, StandardFlow) { |
41 MetricsLogManager log_manager; | 46 MetricsLogManager log_manager; |
42 | 47 |
43 // Make sure a new manager has a clean slate. | 48 // Make sure a new manager has a clean slate. |
44 EXPECT_EQ(NULL, log_manager.current_log()); | 49 EXPECT_EQ(NULL, log_manager.current_log()); |
45 EXPECT_FALSE(log_manager.has_staged_log()); | 50 EXPECT_FALSE(log_manager.has_staged_log()); |
46 EXPECT_FALSE(log_manager.has_unsent_logs()); | 51 EXPECT_FALSE(log_manager.has_unsent_logs()); |
47 | 52 |
48 // Check that the normal flow works. | 53 // Check that the normal flow works. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 EXPECT_FALSE(log_manager.has_staged_log()); | 107 EXPECT_FALSE(log_manager.has_staged_log()); |
103 | 108 |
104 log_manager.ResumePausedLog(); | 109 log_manager.ResumePausedLog(); |
105 EXPECT_EQ(ongoing_log, log_manager.current_log()); | 110 EXPECT_EQ(ongoing_log, log_manager.current_log()); |
106 EXPECT_FALSE(log_manager.has_staged_log()); | 111 EXPECT_FALSE(log_manager.has_staged_log()); |
107 | 112 |
108 EXPECT_FALSE(log_manager.has_unsent_logs()); | 113 EXPECT_FALSE(log_manager.has_unsent_logs()); |
109 } | 114 } |
110 | 115 |
111 TEST(MetricsLogManagerTest, StoreAndLoad) { | 116 TEST(MetricsLogManagerTest, StoreAndLoad) { |
112 std::vector<std::string> initial_logs; | 117 std::vector<SerializedLog> initial_logs; |
113 std::vector<std::string> ongoing_logs; | 118 std::vector<SerializedLog> ongoing_logs; |
114 | 119 |
115 // Set up some in-progress logging in a scoped log manager simulating the | 120 // Set up some in-progress logging in a scoped log manager simulating the |
116 // leadup to quitting, then persist as would be done on quit. | 121 // leadup to quitting, then persist as would be done on quit. |
117 { | 122 { |
118 MetricsLogManager log_manager; | 123 MetricsLogManager log_manager; |
119 DummyLogSerializer* serializer = new DummyLogSerializer; | 124 DummyLogSerializer* serializer = new DummyLogSerializer; |
120 log_manager.set_log_serializer(serializer); | 125 log_manager.set_log_serializer(serializer); |
121 // Simulate a log having already been unsent from a previous session. | 126 // Simulate a log having already been unsent from a previous session. |
122 serializer->persisted_logs_[MetricsLogManager::ONGOING_LOG].push_back("a"); | 127 SerializedLog log = {"xml", "proto"}; |
| 128 serializer->persisted_logs_[MetricsLogManager::ONGOING_LOG].push_back(log); |
123 EXPECT_FALSE(log_manager.has_unsent_logs()); | 129 EXPECT_FALSE(log_manager.has_unsent_logs()); |
124 log_manager.LoadPersistedUnsentLogs(); | 130 log_manager.LoadPersistedUnsentLogs(); |
125 EXPECT_TRUE(log_manager.has_unsent_logs()); | 131 EXPECT_TRUE(log_manager.has_unsent_logs()); |
126 | 132 |
127 MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version"); | 133 MetricsLogBase* log1 = new MetricsLogBase("id", 0, "version"); |
128 MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version"); | 134 MetricsLogBase* log2 = new MetricsLogBase("id", 0, "version"); |
129 log_manager.BeginLoggingWithLog(log1); | 135 log_manager.BeginLoggingWithLog(log1); |
130 log_manager.StageCurrentLogForUpload(); | 136 log_manager.StageCurrentLogForUpload(); |
131 log_manager.BeginLoggingWithLog(log2); | 137 log_manager.BeginLoggingWithLog(log2); |
132 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::INITIAL_LOG); | 138 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::INITIAL_LOG); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 log_manager.BeginLoggingWithLog(log2); | 208 log_manager.BeginLoggingWithLog(log2); |
203 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::INITIAL_LOG); | 209 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::INITIAL_LOG); |
204 log_manager.StageCurrentLogForUpload(); | 210 log_manager.StageCurrentLogForUpload(); |
205 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::ONGOING_LOG); | 211 log_manager.StoreStagedLogAsUnsent(MetricsLogManager::ONGOING_LOG); |
206 | 212 |
207 // Only the ongoing log should be written out, due to the threshold. | 213 // Only the ongoing log should be written out, due to the threshold. |
208 log_manager.PersistUnsentLogs(); | 214 log_manager.PersistUnsentLogs(); |
209 EXPECT_EQ(1U, serializer->TypeCount(MetricsLogManager::INITIAL_LOG)); | 215 EXPECT_EQ(1U, serializer->TypeCount(MetricsLogManager::INITIAL_LOG)); |
210 EXPECT_EQ(0U, serializer->TypeCount(MetricsLogManager::ONGOING_LOG)); | 216 EXPECT_EQ(0U, serializer->TypeCount(MetricsLogManager::ONGOING_LOG)); |
211 } | 217 } |
OLD | NEW |