[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 5 | #include "components/metrics/metrics_log.h" |
[email protected] | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 6 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include "base/base64.h" |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 11 | #include "base/metrics/bucket_ranges.h" |
| 12 | #include "base/metrics/sample_vector.h" |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 13 | #include "base/prefs/pref_service.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 14 | #include "base/prefs/testing_pref_service.h" |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
| 16 | #include "base/time/time.h" |
| 17 | #include "components/metrics/metrics_pref_names.h" |
| 18 | #include "components/metrics/metrics_state_manager.h" |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 19 | #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 20 | #include "components/metrics/test_metrics_service_client.h" |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 21 | #include "components/variations/active_field_trials.h" |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 23 | |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 24 | namespace metrics { |
| 25 | |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 28 | const char kClientId[] = "bogus client ID"; |
| 29 | const int64 kInstallDate = 1373051956; |
| 30 | const int64 kInstallDateExpected = 1373050800; // Computed from kInstallDate. |
| 31 | const int64 kEnabledDate = 1373001211; |
| 32 | const int64 kEnabledDateExpected = 1373000400; // Computed from kEnabledDate. |
| 33 | const int kSessionId = 127; |
| 34 | const variations::ActiveGroupId kFieldTrialIds[] = { |
| 35 | {37, 43}, |
| 36 | {13, 47}, |
| 37 | {23, 17} |
| 38 | }; |
| 39 | const variations::ActiveGroupId kSyntheticTrials[] = { |
| 40 | {55, 15}, |
| 41 | {66, 16} |
| 42 | }; |
| 43 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 44 | class TestMetricsLog : public MetricsLog { |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 45 | public: |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 46 | TestMetricsLog(const std::string& client_id, |
| 47 | int session_id, |
| 48 | LogType log_type, |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 49 | MetricsServiceClient* client, |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 50 | TestingPrefServiceSimple* prefs) |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 51 | : MetricsLog(client_id, session_id, log_type, client, prefs), |
| 52 | prefs_(prefs) { |
| 53 | InitPrefs(); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 54 | } |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 55 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 56 | ~TestMetricsLog() override {} |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 57 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 58 | const ChromeUserMetricsExtension& uma_proto() const { |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 59 | return *MetricsLog::uma_proto(); |
| 60 | } |
| 61 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 62 | const SystemProfileProto& system_profile() const { |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 63 | return uma_proto().system_profile(); |
| 64 | } |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 65 | |
| 66 | private: |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 67 | void InitPrefs() { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 68 | prefs_->SetString(prefs::kMetricsReportingEnabledTimestamp, |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 69 | base::Int64ToString(kEnabledDate)); |
| 70 | } |
| 71 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 72 | void GetFieldTrialIds( |
| 73 | std::vector<variations::ActiveGroupId>* field_trial_ids) const override { |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 74 | ASSERT_TRUE(field_trial_ids->empty()); |
| 75 | |
| 76 | for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) { |
| 77 | field_trial_ids->push_back(kFieldTrialIds[i]); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Weak pointer to the PrefsService used by this log. |
| 82 | TestingPrefServiceSimple* prefs_; |
| 83 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 84 | DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | } // namespace |
| 88 | |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 89 | class MetricsLogTest : public testing::Test { |
| 90 | public: |
| 91 | MetricsLogTest() { |
| 92 | MetricsLog::RegisterPrefs(prefs_.registry()); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 93 | MetricsStateManager::RegisterPrefs(prefs_.registry()); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 94 | } |
| 95 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 96 | ~MetricsLogTest() override {} |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 97 | |
| 98 | protected: |
| 99 | // Check that the values in |system_values| correspond to the test data |
| 100 | // defined at the top of this file. |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 101 | void CheckSystemProfile(const SystemProfileProto& system_profile) { |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 102 | EXPECT_EQ(kInstallDateExpected, system_profile.install_date()); |
| 103 | EXPECT_EQ(kEnabledDateExpected, system_profile.uma_enabled_date()); |
| 104 | |
| 105 | ASSERT_EQ(arraysize(kFieldTrialIds) + arraysize(kSyntheticTrials), |
| 106 | static_cast<size_t>(system_profile.field_trial_size())); |
| 107 | for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 108 | const SystemProfileProto::FieldTrial& field_trial = |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 109 | system_profile.field_trial(i); |
| 110 | EXPECT_EQ(kFieldTrialIds[i].name, field_trial.name_id()); |
| 111 | EXPECT_EQ(kFieldTrialIds[i].group, field_trial.group_id()); |
| 112 | } |
| 113 | // Verify the right data is present for the synthetic trials. |
| 114 | for (size_t i = 0; i < arraysize(kSyntheticTrials); ++i) { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 115 | const SystemProfileProto::FieldTrial& field_trial = |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 116 | system_profile.field_trial(i + arraysize(kFieldTrialIds)); |
| 117 | EXPECT_EQ(kSyntheticTrials[i].name, field_trial.name_id()); |
| 118 | EXPECT_EQ(kSyntheticTrials[i].group, field_trial.group_id()); |
| 119 | } |
| 120 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 121 | EXPECT_EQ(TestMetricsServiceClient::kBrandForTesting, |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 122 | system_profile.brand_code()); |
| 123 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 124 | const SystemProfileProto::Hardware& hardware = |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 125 | system_profile.hardware(); |
| 126 | |
| 127 | EXPECT_TRUE(hardware.has_cpu()); |
| 128 | EXPECT_TRUE(hardware.cpu().has_vendor_name()); |
| 129 | EXPECT_TRUE(hardware.cpu().has_signature()); |
gayane | f550f50f | 2015-03-11 00:44:59 | [diff] [blame] | 130 | EXPECT_TRUE(hardware.cpu().has_num_cores()); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 131 | |
| 132 | // TODO(isherman): Verify other data written into the protobuf as a result |
| 133 | // of this call. |
| 134 | } |
| 135 | |
| 136 | protected: |
| 137 | TestingPrefServiceSimple prefs_; |
| 138 | |
| 139 | private: |
| 140 | DISALLOW_COPY_AND_ASSIGN(MetricsLogTest); |
| 141 | }; |
| 142 | |
| 143 | TEST_F(MetricsLogTest, LogType) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 144 | TestMetricsServiceClient client; |
| 145 | TestingPrefServiceSimple prefs; |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 146 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 147 | MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client, &prefs); |
| 148 | EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type()); |
| 149 | |
| 150 | MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &prefs); |
| 151 | EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type()); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 154 | TEST_F(MetricsLogTest, EmptyRecord) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 155 | TestMetricsServiceClient client; |
| 156 | client.set_version_string("bogus version"); |
| 157 | TestingPrefServiceSimple prefs; |
| 158 | MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG, |
| 159 | &client, &prefs); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 160 | log.CloseLog(); |
| 161 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 162 | std::string encoded; |
[email protected] | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 163 | log.GetEncodedLog(&encoded); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 164 | |
[email protected] | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 165 | // A couple of fields are hard to mock, so these will be copied over directly |
| 166 | // for the expected output. |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 167 | ChromeUserMetricsExtension parsed; |
[email protected] | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 168 | ASSERT_TRUE(parsed.ParseFromString(encoded)); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 169 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 170 | ChromeUserMetricsExtension expected; |
[email protected] | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 171 | expected.set_client_id(5217101509553811875); // Hashed bogus client ID |
| 172 | expected.set_session_id(137); |
| 173 | expected.mutable_system_profile()->set_build_timestamp( |
| 174 | parsed.system_profile().build_timestamp()); |
| 175 | expected.mutable_system_profile()->set_app_version("bogus version"); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 176 | expected.mutable_system_profile()->set_channel(client.GetChannel()); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 177 | |
[email protected] | 46a0efc | 2013-07-17 15:40:47 | [diff] [blame] | 178 | EXPECT_EQ(expected.SerializeAsString(), encoded); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 181 | TEST_F(MetricsLogTest, HistogramBucketFields) { |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 182 | // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. |
| 183 | base::BucketRanges ranges(8); |
| 184 | ranges.set_range(0, 1); |
| 185 | ranges.set_range(1, 5); |
| 186 | ranges.set_range(2, 7); |
| 187 | ranges.set_range(3, 8); |
| 188 | ranges.set_range(4, 9); |
| 189 | ranges.set_range(5, 10); |
| 190 | ranges.set_range(6, 11); |
| 191 | ranges.set_range(7, 12); |
| 192 | |
| 193 | base::SampleVector samples(&ranges); |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 194 | samples.Accumulate(3, 1); // Bucket 1-5. |
| 195 | samples.Accumulate(6, 1); // Bucket 5-7. |
| 196 | samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 197 | samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) |
| 198 | samples.Accumulate(11, 1); // Bucket 11-12. |
| 199 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 200 | TestMetricsServiceClient client; |
| 201 | TestingPrefServiceSimple prefs; |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 202 | TestMetricsLog log( |
| 203 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 204 | log.RecordHistogramDelta("Test", samples); |
| 205 | |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 206 | const ChromeUserMetricsExtension& uma_proto = log.uma_proto(); |
| 207 | const HistogramEventProto& histogram_proto = |
| 208 | uma_proto.histogram_event(uma_proto.histogram_event_size() - 1); |
[email protected] | 838f770 | 2013-10-04 22:20:51 | [diff] [blame] | 209 | |
| 210 | // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12. |
| 211 | // Should become: 1-/, 5-7, /-9, 10-/, /-12. |
| 212 | ASSERT_EQ(5, histogram_proto.bucket_size()); |
| 213 | |
| 214 | // 1-5 becomes 1-/ (max is same as next min). |
| 215 | EXPECT_TRUE(histogram_proto.bucket(0).has_min()); |
| 216 | EXPECT_FALSE(histogram_proto.bucket(0).has_max()); |
| 217 | EXPECT_EQ(1, histogram_proto.bucket(0).min()); |
| 218 | |
| 219 | // 5-7 stays 5-7 (no optimization possible). |
| 220 | EXPECT_TRUE(histogram_proto.bucket(1).has_min()); |
| 221 | EXPECT_TRUE(histogram_proto.bucket(1).has_max()); |
| 222 | EXPECT_EQ(5, histogram_proto.bucket(1).min()); |
| 223 | EXPECT_EQ(7, histogram_proto.bucket(1).max()); |
| 224 | |
| 225 | // 8-9 becomes /-9 (min is same as max - 1). |
| 226 | EXPECT_FALSE(histogram_proto.bucket(2).has_min()); |
| 227 | EXPECT_TRUE(histogram_proto.bucket(2).has_max()); |
| 228 | EXPECT_EQ(9, histogram_proto.bucket(2).max()); |
| 229 | |
| 230 | // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). |
| 231 | EXPECT_TRUE(histogram_proto.bucket(3).has_min()); |
| 232 | EXPECT_FALSE(histogram_proto.bucket(3).has_max()); |
| 233 | EXPECT_EQ(10, histogram_proto.bucket(3).min()); |
| 234 | |
| 235 | // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). |
| 236 | EXPECT_FALSE(histogram_proto.bucket(4).has_min()); |
| 237 | EXPECT_TRUE(histogram_proto.bucket(4).has_max()); |
| 238 | EXPECT_EQ(12, histogram_proto.bucket(4).max()); |
| 239 | } |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 240 | |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 241 | TEST_F(MetricsLogTest, RecordEnvironment) { |
| 242 | TestMetricsServiceClient client; |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 243 | TestMetricsLog log( |
| 244 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 245 | |
| 246 | std::vector<variations::ActiveGroupId> synthetic_trials; |
| 247 | // Add two synthetic trials. |
| 248 | synthetic_trials.push_back(kSyntheticTrials[0]); |
| 249 | synthetic_trials.push_back(kSyntheticTrials[1]); |
| 250 | |
| 251 | log.RecordEnvironment(std::vector<MetricsProvider*>(), |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 252 | synthetic_trials, |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 253 | kInstallDate, kEnabledDate); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 254 | // Check that the system profile on the log has the correct values set. |
| 255 | CheckSystemProfile(log.system_profile()); |
| 256 | |
| 257 | // Check that the system profile has also been written to prefs. |
| 258 | const std::string base64_system_profile = |
| 259 | prefs_.GetString(prefs::kStabilitySavedSystemProfile); |
| 260 | EXPECT_FALSE(base64_system_profile.empty()); |
| 261 | std::string serialied_system_profile; |
| 262 | EXPECT_TRUE(base::Base64Decode(base64_system_profile, |
| 263 | &serialied_system_profile)); |
| 264 | SystemProfileProto decoded_system_profile; |
| 265 | EXPECT_TRUE(decoded_system_profile.ParseFromString(serialied_system_profile)); |
| 266 | CheckSystemProfile(decoded_system_profile); |
| 267 | } |
| 268 | |
| 269 | TEST_F(MetricsLogTest, LoadSavedEnvironmentFromPrefs) { |
| 270 | const char* kSystemProfilePref = prefs::kStabilitySavedSystemProfile; |
| 271 | const char* kSystemProfileHashPref = |
| 272 | prefs::kStabilitySavedSystemProfileHash; |
| 273 | |
| 274 | TestMetricsServiceClient client; |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 275 | |
| 276 | // The pref value is empty, so loading it from prefs should fail. |
| 277 | { |
| 278 | TestMetricsLog log( |
| 279 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 280 | EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs()); |
| 281 | } |
| 282 | |
| 283 | // Do a RecordEnvironment() call and check whether the pref is recorded. |
| 284 | { |
| 285 | TestMetricsLog log( |
| 286 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 287 | log.RecordEnvironment(std::vector<MetricsProvider*>(), |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 288 | std::vector<variations::ActiveGroupId>(), |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 289 | kInstallDate, kEnabledDate); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 290 | EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty()); |
| 291 | EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty()); |
| 292 | } |
| 293 | |
| 294 | { |
| 295 | TestMetricsLog log( |
| 296 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 297 | EXPECT_TRUE(log.LoadSavedEnvironmentFromPrefs()); |
| 298 | // Check some values in the system profile. |
| 299 | EXPECT_EQ(kInstallDateExpected, log.system_profile().install_date()); |
| 300 | EXPECT_EQ(kEnabledDateExpected, log.system_profile().uma_enabled_date()); |
| 301 | // Ensure that the call cleared the prefs. |
| 302 | EXPECT_TRUE(prefs_.GetString(kSystemProfilePref).empty()); |
| 303 | EXPECT_TRUE(prefs_.GetString(kSystemProfileHashPref).empty()); |
| 304 | } |
| 305 | |
| 306 | // Ensure that a non-matching hash results in the pref being invalid. |
| 307 | { |
| 308 | TestMetricsLog log( |
| 309 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 310 | // Call RecordEnvironment() to record the pref again. |
| 311 | log.RecordEnvironment(std::vector<MetricsProvider*>(), |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 312 | std::vector<variations::ActiveGroupId>(), |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 313 | kInstallDate, kEnabledDate); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | { |
| 317 | // Set the hash to a bad value. |
| 318 | prefs_.SetString(kSystemProfileHashPref, "deadbeef"); |
| 319 | TestMetricsLog log( |
| 320 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 321 | EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs()); |
| 322 | // Ensure that the prefs are cleared, even if the call failed. |
| 323 | EXPECT_TRUE(prefs_.GetString(kSystemProfilePref).empty()); |
| 324 | EXPECT_TRUE(prefs_.GetString(kSystemProfileHashPref).empty()); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | TEST_F(MetricsLogTest, InitialLogStabilityMetrics) { |
| 329 | TestMetricsServiceClient client; |
| 330 | TestMetricsLog log(kClientId, |
| 331 | kSessionId, |
| 332 | MetricsLog::INITIAL_STABILITY_LOG, |
| 333 | &client, |
| 334 | &prefs_); |
| 335 | std::vector<MetricsProvider*> metrics_providers; |
| 336 | log.RecordEnvironment(metrics_providers, |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 337 | std::vector<variations::ActiveGroupId>(), |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 338 | kInstallDate, kEnabledDate); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 339 | log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(), |
| 340 | base::TimeDelta()); |
| 341 | const SystemProfileProto_Stability& stability = |
| 342 | log.system_profile().stability(); |
| 343 | // Required metrics: |
| 344 | EXPECT_TRUE(stability.has_launch_count()); |
| 345 | EXPECT_TRUE(stability.has_crash_count()); |
| 346 | // Initial log metrics: |
| 347 | EXPECT_TRUE(stability.has_incomplete_shutdown_count()); |
| 348 | EXPECT_TRUE(stability.has_breakpad_registration_success_count()); |
| 349 | EXPECT_TRUE(stability.has_breakpad_registration_failure_count()); |
| 350 | EXPECT_TRUE(stability.has_debugger_present_count()); |
| 351 | EXPECT_TRUE(stability.has_debugger_not_present_count()); |
| 352 | } |
| 353 | |
| 354 | TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) { |
| 355 | TestMetricsServiceClient client; |
| 356 | TestMetricsLog log( |
| 357 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 358 | std::vector<MetricsProvider*> metrics_providers; |
| 359 | log.RecordEnvironment(metrics_providers, |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 360 | std::vector<variations::ActiveGroupId>(), |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 361 | kInstallDate, kEnabledDate); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 362 | log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(), |
| 363 | base::TimeDelta()); |
| 364 | const SystemProfileProto_Stability& stability = |
| 365 | log.system_profile().stability(); |
| 366 | // Required metrics: |
| 367 | EXPECT_TRUE(stability.has_launch_count()); |
| 368 | EXPECT_TRUE(stability.has_crash_count()); |
| 369 | // Initial log metrics: |
| 370 | EXPECT_FALSE(stability.has_incomplete_shutdown_count()); |
| 371 | EXPECT_FALSE(stability.has_breakpad_registration_success_count()); |
| 372 | EXPECT_FALSE(stability.has_breakpad_registration_failure_count()); |
| 373 | EXPECT_FALSE(stability.has_debugger_present_count()); |
| 374 | EXPECT_FALSE(stability.has_debugger_not_present_count()); |
| 375 | } |
| 376 | |
| 377 | TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) { |
| 378 | TestMetricsServiceClient client; |
| 379 | TestMetricsLog log( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 380 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
[email protected] | b0122eb | 2014-06-11 18:58:32 | [diff] [blame] | 381 | EXPECT_TRUE(log.uma_proto().system_profile().has_channel()); |
| 382 | } |
| 383 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 384 | TEST_F(MetricsLogTest, ProductNotSetIfDefault) { |
| 385 | TestMetricsServiceClient client; |
| 386 | EXPECT_EQ(ChromeUserMetricsExtension::CHROME, client.GetProduct()); |
| 387 | TestMetricsLog log( |
| 388 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 389 | // Check that the product isn't set, since it's default and also verify the |
| 390 | // default value is indeed equal to Chrome. |
| 391 | EXPECT_FALSE(log.uma_proto().has_product()); |
| 392 | EXPECT_EQ(ChromeUserMetricsExtension::CHROME, log.uma_proto().product()); |
| 393 | } |
| 394 | |
| 395 | TEST_F(MetricsLogTest, ProductSetIfNotDefault) { |
| 396 | const int32_t kTestProduct = 100; |
| 397 | EXPECT_NE(ChromeUserMetricsExtension::CHROME, kTestProduct); |
| 398 | |
| 399 | TestMetricsServiceClient client; |
| 400 | client.set_product(kTestProduct); |
| 401 | TestMetricsLog log( |
| 402 | kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); |
| 403 | // Check that the product is set to |kTestProduct|. |
| 404 | EXPECT_TRUE(log.uma_proto().has_product()); |
| 405 | EXPECT_EQ(kTestProduct, log.uma_proto().product()); |
| 406 | } |
| 407 | |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 408 | } // namespace metrics |