| OLD | NEW |
| 1 // Copyright (c) 2011 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/stringprintf.h" |
| 7 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 8 #include "base/time.h" | 9 #include "base/time.h" |
| 9 #include "chrome/browser/metrics/metrics_log.h" | 10 #include "chrome/browser/metrics/metrics_log.h" |
| 10 #include "chrome/browser/prefs/browser_prefs.h" | 11 #include "chrome/browser/prefs/browser_prefs.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/testing_pref_service.h" | 14 #include "chrome/test/testing_pref_service.h" |
| 14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 36 char digit = xml_encoded->at(i); | 37 char digit = xml_encoded->at(i); |
| 37 ASSERT_GE(digit, '0'); | 38 ASSERT_GE(digit, '0'); |
| 38 ASSERT_LE(digit, '9'); | 39 ASSERT_LE(digit, '9'); |
| 39 } | 40 } |
| 40 | 41 |
| 41 // Insert a single fake buildtime. | 42 // Insert a single fake buildtime. |
| 42 xml_encoded->replace(offset, postfix_position - offset, "123246"); | 43 xml_encoded->replace(offset, postfix_position - offset, "123246"); |
| 43 } | 44 } |
| 44 | 45 |
| 45 TEST(MetricsLogTest, EmptyRecord) { | 46 TEST(MetricsLogTest, EmptyRecord) { |
| 46 std::string expected_output = StringPrintf( | 47 std::string expected_output = base::StringPrintf( |
| 47 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 48 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 48 "appversion=\"%s\"/>", MetricsLog::GetVersionString().c_str()); | 49 "appversion=\"%s\"/>", MetricsLog::GetVersionString().c_str()); |
| 49 | 50 |
| 50 MetricsLog log("bogus client ID", 0); | 51 MetricsLog log("bogus client ID", 0); |
| 51 log.CloseLog(); | 52 log.CloseLog(); |
| 52 | 53 |
| 53 int size = log.GetEncodedLogSize(); | 54 int size = log.GetEncodedLogSize(); |
| 54 ASSERT_GT(size, 0); | 55 ASSERT_GT(size, 0); |
| 55 | 56 |
| 56 std::string encoded; | 57 std::string encoded; |
| 57 // Leave room for the NUL terminator. | 58 // Leave room for the NUL terminator. |
| 58 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | 59 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 59 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | 60 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 60 NormalizeBuildtime(&encoded); | 61 NormalizeBuildtime(&encoded); |
| 61 NormalizeBuildtime(&expected_output); | 62 NormalizeBuildtime(&expected_output); |
| 62 | 63 |
| 63 ASSERT_EQ(expected_output, encoded); | 64 ASSERT_EQ(expected_output, encoded); |
| 64 } | 65 } |
| 65 | 66 |
| 66 #if defined(OS_CHROMEOS) | 67 #if defined(OS_CHROMEOS) |
| 67 TEST(MetricsLogTest, ChromeOSEmptyRecord) { | 68 TEST(MetricsLogTest, ChromeOSEmptyRecord) { |
| 68 std::string expected_output = StringPrintf( | 69 std::string expected_output = base::StringPrintf( |
| 69 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 70 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 70 "appversion=\"%s\" hardwareclass=\"sample-class\"/>", | 71 "appversion=\"%s\" hardwareclass=\"sample-class\"/>", |
| 71 MetricsLog::GetVersionString().c_str()); | 72 MetricsLog::GetVersionString().c_str()); |
| 72 | 73 |
| 73 MetricsLog log("bogus client ID", 0); | 74 MetricsLog log("bogus client ID", 0); |
| 74 log.set_hardware_class("sample-class"); | 75 log.set_hardware_class("sample-class"); |
| 75 log.CloseLog(); | 76 log.CloseLog(); |
| 76 | 77 |
| 77 int size = log.GetEncodedLogSize(); | 78 int size = log.GetEncodedLogSize(); |
| 78 ASSERT_GT(size, 0); | 79 ASSERT_GT(size, 0); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 98 | 99 |
| 99 // Override this so that output is testable. | 100 // Override this so that output is testable. |
| 100 virtual std::string GetCurrentTimeString() { | 101 virtual std::string GetCurrentTimeString() { |
| 101 return std::string(); | 102 return std::string(); |
| 102 } | 103 } |
| 103 }; | 104 }; |
| 104 | 105 |
| 105 }; // namespace | 106 }; // namespace |
| 106 | 107 |
| 107 TEST(MetricsLogTest, WindowEvent) { | 108 TEST(MetricsLogTest, WindowEvent) { |
| 108 std::string expected_output = StringPrintf( | 109 std::string expected_output = base::StringPrintf( |
| 109 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 110 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 110 "appversion=\"%s\">\n" | 111 "appversion=\"%s\">\n" |
| 111 " <window action=\"create\" windowid=\"0\" session=\"0\" time=\"\"/>\n" | 112 " <window action=\"create\" windowid=\"0\" session=\"0\" time=\"\"/>\n" |
| 112 " <window action=\"open\" windowid=\"1\" parent=\"0\" " | 113 " <window action=\"open\" windowid=\"1\" parent=\"0\" " |
| 113 "session=\"0\" time=\"\"/>\n" | 114 "session=\"0\" time=\"\"/>\n" |
| 114 " <window action=\"close\" windowid=\"1\" parent=\"0\" " | 115 " <window action=\"close\" windowid=\"1\" parent=\"0\" " |
| 115 "session=\"0\" time=\"\"/>\n" | 116 "session=\"0\" time=\"\"/>\n" |
| 116 " <window action=\"destroy\" windowid=\"0\" session=\"0\" time=\"\"/>\n" | 117 " <window action=\"destroy\" windowid=\"0\" session=\"0\" time=\"\"/>\n" |
| 117 "</log>", MetricsLog::GetVersionString().c_str()); | 118 "</log>", MetricsLog::GetVersionString().c_str()); |
| 118 | 119 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 132 // Leave room for the NUL terminator. | 133 // Leave room for the NUL terminator. |
| 133 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | 134 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 134 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | 135 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 135 NormalizeBuildtime(&encoded); | 136 NormalizeBuildtime(&encoded); |
| 136 NormalizeBuildtime(&expected_output); | 137 NormalizeBuildtime(&expected_output); |
| 137 | 138 |
| 138 ASSERT_EQ(expected_output, encoded); | 139 ASSERT_EQ(expected_output, encoded); |
| 139 } | 140 } |
| 140 | 141 |
| 141 TEST(MetricsLogTest, LoadEvent) { | 142 TEST(MetricsLogTest, LoadEvent) { |
| 142 std::string expected_output = StringPrintf( | 143 std::string expected_output = base::StringPrintf( |
| 143 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 144 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 144 "appversion=\"%s\">\n" | 145 "appversion=\"%s\">\n" |
| 145 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " | 146 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " |
| 146 "origin=\"link\" session=\"0\" time=\"\"/>\n" | 147 "origin=\"link\" session=\"0\" time=\"\"/>\n" |
| 147 "</log>", MetricsLog::GetVersionString().c_str()); | 148 "</log>", MetricsLog::GetVersionString().c_str()); |
| 148 | 149 |
| 149 NoTimeMetricsLog log("bogus client ID", 0); | 150 NoTimeMetricsLog log("bogus client ID", 0); |
| 150 log.RecordLoadEvent(3, GURL("http://google.com"), PageTransition::LINK, | 151 log.RecordLoadEvent(3, GURL("http://google.com"), PageTransition::LINK, |
| 151 1, TimeDelta::FromMilliseconds(7219)); | 152 1, TimeDelta::FromMilliseconds(7219)); |
| 152 | 153 |
| 153 log.CloseLog(); | 154 log.CloseLog(); |
| 154 | 155 |
| 155 ASSERT_EQ(1, log.num_events()); | 156 ASSERT_EQ(1, log.num_events()); |
| 156 | 157 |
| 157 int size = log.GetEncodedLogSize(); | 158 int size = log.GetEncodedLogSize(); |
| 158 ASSERT_GT(size, 0); | 159 ASSERT_GT(size, 0); |
| 159 | 160 |
| 160 std::string encoded; | 161 std::string encoded; |
| 161 // Leave room for the NUL terminator. | 162 // Leave room for the NUL terminator. |
| 162 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | 163 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 163 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | 164 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 164 NormalizeBuildtime(&encoded); | 165 NormalizeBuildtime(&encoded); |
| 165 NormalizeBuildtime(&expected_output); | 166 NormalizeBuildtime(&expected_output); |
| 166 | 167 |
| 167 ASSERT_EQ(expected_output, encoded); | 168 ASSERT_EQ(expected_output, encoded); |
| 168 } | 169 } |
| 169 | 170 |
| 170 #if defined(OS_CHROMEOS) | 171 #if defined(OS_CHROMEOS) |
| 171 TEST(MetricsLogTest, ChromeOSLoadEvent) { | 172 TEST(MetricsLogTest, ChromeOSLoadEvent) { |
| 172 std::string expected_output = StringPrintf( | 173 std::string expected_output = base::StringPrintf( |
| 173 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 174 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 174 "appversion=\"%s\" hardwareclass=\"sample-class\">\n" | 175 "appversion=\"%s\" hardwareclass=\"sample-class\">\n" |
| 175 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " | 176 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " |
| 176 "origin=\"link\" session=\"0\" time=\"\"/>\n" | 177 "origin=\"link\" session=\"0\" time=\"\"/>\n" |
| 177 "</log>", MetricsLog::GetVersionString().c_str()); | 178 "</log>", MetricsLog::GetVersionString().c_str()); |
| 178 | 179 |
| 179 NoTimeMetricsLog log("bogus client ID", 0); | 180 NoTimeMetricsLog log("bogus client ID", 0); |
| 180 log.RecordLoadEvent(3, GURL("http://google.com"), PageTransition::LINK, | 181 log.RecordLoadEvent(3, GURL("http://google.com"), PageTransition::LINK, |
| 181 1, TimeDelta::FromMilliseconds(7219)); | 182 1, TimeDelta::FromMilliseconds(7219)); |
| 182 log.set_hardware_class("sample-class"); | 183 log.set_hardware_class("sample-class"); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 199 | 200 |
| 200 TEST(MetricsLogTest, ChromeOSStabilityData) { | 201 TEST(MetricsLogTest, ChromeOSStabilityData) { |
| 201 NoTimeMetricsLog log("bogus client ID", 0); | 202 NoTimeMetricsLog log("bogus client ID", 0); |
| 202 TestingPrefService prefs; | 203 TestingPrefService prefs; |
| 203 browser::RegisterLocalState(&prefs); | 204 browser::RegisterLocalState(&prefs); |
| 204 | 205 |
| 205 prefs.SetInteger(prefs::kStabilityChildProcessCrashCount, 10); | 206 prefs.SetInteger(prefs::kStabilityChildProcessCrashCount, 10); |
| 206 prefs.SetInteger(prefs::kStabilityOtherUserCrashCount, 11); | 207 prefs.SetInteger(prefs::kStabilityOtherUserCrashCount, 11); |
| 207 prefs.SetInteger(prefs::kStabilityKernelCrashCount, 12); | 208 prefs.SetInteger(prefs::kStabilityKernelCrashCount, 12); |
| 208 prefs.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); | 209 prefs.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); |
| 209 std::string expected_output = StringPrintf( | 210 std::string expected_output = base::StringPrintf( |
| 210 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 211 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 211 "appversion=\"%s\">\n" | 212 "appversion=\"%s\">\n" |
| 212 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); | 213 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); |
| 213 // Expect 3 warnings about not yet being able to send the | 214 // Expect 3 warnings about not yet being able to send the |
| 214 // Chrome OS stability stats. | 215 // Chrome OS stability stats. |
| 215 log.WriteStabilityElement(&prefs); | 216 log.WriteStabilityElement(&prefs); |
| 216 log.CloseLog(); | 217 log.CloseLog(); |
| 217 | 218 |
| 218 int size = log.GetEncodedLogSize(); | 219 int size = log.GetEncodedLogSize(); |
| 219 ASSERT_GT(size, 0); | 220 ASSERT_GT(size, 0); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 // We're only checking the first 8 bytes, because that's what | 263 // We're only checking the first 8 bytes, because that's what |
| 263 // the metrics server uses. | 264 // the metrics server uses. |
| 264 std::string hash_hex = "0x"; | 265 std::string hash_hex = "0x"; |
| 265 for (size_t j = 0; j < 8; j++) { | 266 for (size_t j = 0; j < 8; j++) { |
| 266 base::StringAppendF(&hash_hex, "%02x", | 267 base::StringAppendF(&hash_hex, "%02x", |
| 267 static_cast<uint8>(hash_string.data()[j])); | 268 static_cast<uint8>(hash_string.data()[j])); |
| 268 } | 269 } |
| 269 EXPECT_EQ(cases[i].output, hash_hex); | 270 EXPECT_EQ(cases[i].output, hash_hex); |
| 270 } | 271 } |
| 271 }; | 272 }; |
| OLD | NEW |