OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/metrics/persisted_logs.h" | 5 #include "components/metrics/persisted_logs.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "components/metrics/persisted_logs_metrics_impl.h" | 14 #include "components/metrics/persisted_logs_metrics_impl.h" |
15 #include "components/prefs/pref_registry_simple.h" | 15 #include "components/prefs/pref_registry_simple.h" |
16 #include "components/prefs/scoped_user_pref_update.h" | 16 #include "components/prefs/scoped_user_pref_update.h" |
17 #include "components/prefs/testing_pref_service.h" | 17 #include "components/prefs/testing_pref_service.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/zlib/google/compression_utils.h" | 19 #include "third_party/zlib/google/compression_utils.h" |
20 | 20 |
21 namespace metrics { | 21 namespace metrics { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kTestPrefName[] = "TestPref"; | 25 const char kTestPrefName[] = "TestPref"; |
26 const char kTestOutdatedPrefName[] = "OutdatedTestPref"; | |
27 const size_t kLogCountLimit = 3; | 26 const size_t kLogCountLimit = 3; |
28 const size_t kLogByteLimit = 1000; | 27 const size_t kLogByteLimit = 1000; |
29 | 28 |
30 // Compresses |log_data| and returns the result. | 29 // Compresses |log_data| and returns the result. |
31 std::string Compress(const std::string& log_data) { | 30 std::string Compress(const std::string& log_data) { |
32 std::string compressed_log_data; | 31 std::string compressed_log_data; |
33 EXPECT_TRUE(compression::GzipCompress(log_data, &compressed_log_data)); | 32 EXPECT_TRUE(compression::GzipCompress(log_data, &compressed_log_data)); |
34 return compressed_log_data; | 33 return compressed_log_data; |
35 } | 34 } |
36 | 35 |
(...skipping 25 matching lines...) Expand all Loading... |
62 DISALLOW_COPY_AND_ASSIGN(PersistedLogsTest); | 61 DISALLOW_COPY_AND_ASSIGN(PersistedLogsTest); |
63 }; | 62 }; |
64 | 63 |
65 class TestPersistedLogs : public PersistedLogs { | 64 class TestPersistedLogs : public PersistedLogs { |
66 public: | 65 public: |
67 TestPersistedLogs(PrefService* service, size_t min_log_bytes) | 66 TestPersistedLogs(PrefService* service, size_t min_log_bytes) |
68 : PersistedLogs(std::unique_ptr<PersistedLogsMetricsImpl>( | 67 : PersistedLogs(std::unique_ptr<PersistedLogsMetricsImpl>( |
69 new PersistedLogsMetricsImpl()), | 68 new PersistedLogsMetricsImpl()), |
70 service, | 69 service, |
71 kTestPrefName, | 70 kTestPrefName, |
72 kTestOutdatedPrefName, | |
73 kLogCountLimit, | 71 kLogCountLimit, |
74 min_log_bytes, | 72 min_log_bytes, |
75 0) {} | 73 0) {} |
76 | 74 |
77 // Stages and removes the next log, while testing it's value. | 75 // Stages and removes the next log, while testing it's value. |
78 void ExpectNextLog(const std::string& expected_log) { | 76 void ExpectNextLog(const std::string& expected_log) { |
79 StageLog(); | 77 StageLog(); |
80 EXPECT_EQ(staged_log(), Compress(expected_log)); | 78 EXPECT_EQ(staged_log(), Compress(expected_log)); |
81 DiscardStagedLog(); | 79 DiscardStagedLog(); |
82 } | 80 } |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 | 287 |
290 TestPersistedLogs persisted_logs(&prefs_, kLogByteLimit); | 288 TestPersistedLogs persisted_logs(&prefs_, kLogByteLimit); |
291 persisted_logs.StoreLog(kFooText); | 289 persisted_logs.StoreLog(kFooText); |
292 persisted_logs.StageLog(); | 290 persisted_logs.StageLog(); |
293 | 291 |
294 EXPECT_EQ(Compress(kFooText), persisted_logs.staged_log()); | 292 EXPECT_EQ(Compress(kFooText), persisted_logs.staged_log()); |
295 EXPECT_EQ(foo_hash, persisted_logs.staged_log_hash()); | 293 EXPECT_EQ(foo_hash, persisted_logs.staged_log_hash()); |
296 } | 294 } |
297 | 295 |
298 } // namespace metrics | 296 } // namespace metrics |
OLD | NEW |