| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "chrome/browser/metrics/metrics_log.h" | 9 #include "chrome/browser/metrics/metrics_log.h" |
| 10 #include "chrome/browser/prefs/browser_prefs.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/testing_profile.h" | 13 #include "chrome/test/testing_pref_service.h" |
| 13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using base::TimeDelta; | 17 using base::TimeDelta; |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 class MetricsLogTest : public testing::Test { | 20 class MetricsLogTest : public testing::Test { |
| 20 }; | 21 }; |
| 21 }; | 22 }; |
| 22 | 23 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | 192 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 192 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | 193 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 193 NormalizeBuildtime(&encoded); | 194 NormalizeBuildtime(&encoded); |
| 194 NormalizeBuildtime(&expected_output); | 195 NormalizeBuildtime(&expected_output); |
| 195 | 196 |
| 196 ASSERT_EQ(expected_output, encoded); | 197 ASSERT_EQ(expected_output, encoded); |
| 197 } | 198 } |
| 198 | 199 |
| 199 TEST(MetricsLogTest, ChromeOSStabilityData) { | 200 TEST(MetricsLogTest, ChromeOSStabilityData) { |
| 200 NoTimeMetricsLog log("bogus client ID", 0); | 201 NoTimeMetricsLog log("bogus client ID", 0); |
| 201 TestingProfile profile; | 202 TestingPrefService prefs; |
| 202 PrefService* pref = profile.GetPrefs(); | 203 browser::RegisterLocalState(&prefs); |
| 203 | 204 |
| 204 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 10); | 205 prefs.SetInteger(prefs::kStabilityChildProcessCrashCount, 10); |
| 205 pref->SetInteger(prefs::kStabilityOtherUserCrashCount, 11); | 206 prefs.SetInteger(prefs::kStabilityOtherUserCrashCount, 11); |
| 206 pref->SetInteger(prefs::kStabilityKernelCrashCount, 12); | 207 prefs.SetInteger(prefs::kStabilityKernelCrashCount, 12); |
| 207 pref->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); | 208 prefs.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); |
| 208 std::string expected_output = StringPrintf( | 209 std::string expected_output = StringPrintf( |
| 209 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 210 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 210 "appversion=\"%s\">\n" | 211 "appversion=\"%s\">\n" |
| 211 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); | 212 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); |
| 212 // Expect 3 warnings about not yet being able to send the | 213 // Expect 3 warnings about not yet being able to send the |
| 213 // Chrome OS stability stats. | 214 // Chrome OS stability stats. |
| 214 log.WriteStabilityElement(profile.GetPrefs()); | 215 log.WriteStabilityElement(&prefs); |
| 215 log.CloseLog(); | 216 log.CloseLog(); |
| 216 | 217 |
| 217 int size = log.GetEncodedLogSize(); | 218 int size = log.GetEncodedLogSize(); |
| 218 ASSERT_GT(size, 0); | 219 ASSERT_GT(size, 0); |
| 219 | 220 |
| 220 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilityChildProcessCrashCount)); | 221 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityChildProcessCrashCount)); |
| 221 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilityOtherUserCrashCount)); | 222 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityOtherUserCrashCount)); |
| 222 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilityKernelCrashCount)); | 223 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityKernelCrashCount)); |
| 223 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); | 224 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); |
| 224 | 225 |
| 225 std::string encoded; | 226 std::string encoded; |
| 226 // Leave room for the NUL terminator. | 227 // Leave room for the NUL terminator. |
| 227 bool encoding_result = log.GetEncodedLog( | 228 bool encoding_result = log.GetEncodedLog( |
| 228 WriteInto(&encoded, size + 1), size); | 229 WriteInto(&encoded, size + 1), size); |
| 229 ASSERT_TRUE(encoding_result); | 230 ASSERT_TRUE(encoding_result); |
| 230 | 231 |
| 231 // Check that we can find childprocesscrashcount, but not | 232 // Check that we can find childprocesscrashcount, but not |
| 232 // any of the ChromeOS ones that we are not emitting until log | 233 // any of the ChromeOS ones that we are not emitting until log |
| 233 // servers can handle them. | 234 // servers can handle them. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 261 // We're only checking the first 8 bytes, because that's what | 262 // We're only checking the first 8 bytes, because that's what |
| 262 // the metrics server uses. | 263 // the metrics server uses. |
| 263 std::string hash_hex = "0x"; | 264 std::string hash_hex = "0x"; |
| 264 for (size_t j = 0; j < 8; j++) { | 265 for (size_t j = 0; j < 8; j++) { |
| 265 base::StringAppendF(&hash_hex, "%02x", | 266 base::StringAppendF(&hash_hex, "%02x", |
| 266 static_cast<uint8>(hash_string.data()[j])); | 267 static_cast<uint8>(hash_string.data()[j])); |
| 267 } | 268 } |
| 268 EXPECT_EQ(cases[i].output, hash_hex); | 269 EXPECT_EQ(cases[i].output, hash_hex); |
| 269 } | 270 } |
| 270 }; | 271 }; |
| OLD | NEW |