| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 43 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 44 "appversion=\"%s\"/>", MetricsLog::GetVersionString().c_str()); | 44 "appversion=\"%s\"/>", MetricsLog::GetVersionString().c_str()); |
| 45 | 45 |
| 46 MetricsLog log("bogus client ID", 0); | 46 MetricsLog log("bogus client ID", 0); |
| 47 log.CloseLog(); | 47 log.CloseLog(); |
| 48 | 48 |
| 49 int size = log.GetEncodedLogSize(); | 49 int size = log.GetEncodedLogSize(); |
| 50 ASSERT_GT(size, 0); | 50 ASSERT_GT(size, 0); |
| 51 | 51 |
| 52 std::string encoded; | 52 std::string encoded; |
| 53 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size), size)); | 53 // Leave room for the NUL terminator. |
| 54 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 55 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 54 NormalizeBuildtime(&encoded); | 56 NormalizeBuildtime(&encoded); |
| 55 NormalizeBuildtime(&expected_output); | 57 NormalizeBuildtime(&expected_output); |
| 56 | 58 |
| 57 ASSERT_EQ(expected_output, encoded); | 59 ASSERT_EQ(expected_output, encoded); |
| 58 } | 60 } |
| 59 | 61 |
| 62 #if defined(OS_CHROMEOS) |
| 63 TEST(MetricsLogTest, ChromeOSEmptyRecord) { |
| 64 std::string expected_output = StringPrintf( |
| 65 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 66 "appversion=\"%s\" hardwareclass=\"sample-class\"/>", |
| 67 MetricsLog::GetVersionString().c_str()); |
| 68 |
| 69 MetricsLog log("bogus client ID", 0); |
| 70 log.set_hardware_class("sample-class"); |
| 71 log.CloseLog(); |
| 72 |
| 73 int size = log.GetEncodedLogSize(); |
| 74 ASSERT_GT(size, 0); |
| 75 |
| 76 std::string encoded; |
| 77 // Leave room for the NUL terminator. |
| 78 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 79 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 80 NormalizeBuildtime(&encoded); |
| 81 NormalizeBuildtime(&expected_output); |
| 82 |
| 83 ASSERT_EQ(expected_output, encoded); |
| 84 } |
| 85 #endif // OS_CHROMEOS |
| 86 |
| 60 namespace { | 87 namespace { |
| 61 | 88 |
| 62 class NoTimeMetricsLog : public MetricsLog { | 89 class NoTimeMetricsLog : public MetricsLog { |
| 63 public: | 90 public: |
| 64 NoTimeMetricsLog(std::string client_id, int session_id): | 91 NoTimeMetricsLog(std::string client_id, int session_id): |
| 65 MetricsLog(client_id, session_id) {} | 92 MetricsLog(client_id, session_id) {} |
| 66 virtual ~NoTimeMetricsLog() {} | 93 virtual ~NoTimeMetricsLog() {} |
| 67 | 94 |
| 68 // Override this so that output is testable. | 95 // Override this so that output is testable. |
| 69 virtual std::string GetCurrentTimeString() { | 96 virtual std::string GetCurrentTimeString() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 91 log.RecordWindowEvent(MetricsLog::WINDOW_CLOSE, 1, 0); | 118 log.RecordWindowEvent(MetricsLog::WINDOW_CLOSE, 1, 0); |
| 92 log.RecordWindowEvent(MetricsLog::WINDOW_DESTROY, 0, -1); | 119 log.RecordWindowEvent(MetricsLog::WINDOW_DESTROY, 0, -1); |
| 93 log.CloseLog(); | 120 log.CloseLog(); |
| 94 | 121 |
| 95 ASSERT_EQ(4, log.num_events()); | 122 ASSERT_EQ(4, log.num_events()); |
| 96 | 123 |
| 97 int size = log.GetEncodedLogSize(); | 124 int size = log.GetEncodedLogSize(); |
| 98 ASSERT_GT(size, 0); | 125 ASSERT_GT(size, 0); |
| 99 | 126 |
| 100 std::string encoded; | 127 std::string encoded; |
| 101 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size), size)); | 128 // Leave room for the NUL terminator. |
| 129 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 130 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 102 NormalizeBuildtime(&encoded); | 131 NormalizeBuildtime(&encoded); |
| 103 NormalizeBuildtime(&expected_output); | 132 NormalizeBuildtime(&expected_output); |
| 104 | 133 |
| 105 ASSERT_EQ(expected_output, encoded); | 134 ASSERT_EQ(expected_output, encoded); |
| 106 } | 135 } |
| 107 | 136 |
| 108 TEST(MetricsLogTest, LoadEvent) { | 137 TEST(MetricsLogTest, LoadEvent) { |
| 109 std::string expected_output = StringPrintf( | 138 std::string expected_output = StringPrintf( |
| 110 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 139 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 111 "appversion=\"%s\">\n" | 140 "appversion=\"%s\">\n" |
| 112 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " | 141 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " |
| 113 "origin=\"link\" session=\"0\" time=\"\"/>\n" | 142 "origin=\"link\" session=\"0\" time=\"\"/>\n" |
| 114 "</log>", MetricsLog::GetVersionString().c_str()); | 143 "</log>", MetricsLog::GetVersionString().c_str()); |
| 115 | 144 |
| 116 NoTimeMetricsLog log("bogus client ID", 0); | 145 NoTimeMetricsLog log("bogus client ID", 0); |
| 117 log.RecordLoadEvent(3, GURL("http://google.com"), PageTransition::LINK, | 146 log.RecordLoadEvent(3, GURL("http://google.com"), PageTransition::LINK, |
| 118 1, TimeDelta::FromMilliseconds(7219)); | 147 1, TimeDelta::FromMilliseconds(7219)); |
| 119 | 148 |
| 120 log.CloseLog(); | 149 log.CloseLog(); |
| 121 | 150 |
| 122 ASSERT_EQ(1, log.num_events()); | 151 ASSERT_EQ(1, log.num_events()); |
| 123 | 152 |
| 124 int size = log.GetEncodedLogSize(); | 153 int size = log.GetEncodedLogSize(); |
| 125 ASSERT_GT(size, 0); | 154 ASSERT_GT(size, 0); |
| 126 | 155 |
| 127 std::string encoded; | 156 std::string encoded; |
| 128 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size), size)); | 157 // Leave room for the NUL terminator. |
| 158 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
| 159 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
| 129 NormalizeBuildtime(&encoded); | 160 NormalizeBuildtime(&encoded); |
| 130 NormalizeBuildtime(&expected_output); | 161 NormalizeBuildtime(&expected_output); |
| 131 | 162 |
| 132 ASSERT_EQ(expected_output, encoded); | 163 ASSERT_EQ(expected_output, encoded); |
| 133 } | 164 } |
| 134 | 165 |
| 135 // Make sure our ID hashes are the same as what we see on the server side. | 166 // Make sure our ID hashes are the same as what we see on the server side. |
| 136 TEST(MetricsLogTest, CreateHash) { | 167 TEST(MetricsLogTest, CreateHash) { |
| 137 static const struct { | 168 static const struct { |
| 138 std::string input; | 169 std::string input; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 150 // We're only checking the first 8 bytes, because that's what | 181 // We're only checking the first 8 bytes, because that's what |
| 151 // the metrics server uses. | 182 // the metrics server uses. |
| 152 std::string hash_hex = "0x"; | 183 std::string hash_hex = "0x"; |
| 153 for (size_t j = 0; j < 8; j++) { | 184 for (size_t j = 0; j < 8; j++) { |
| 154 StringAppendF(&hash_hex, "%02x", | 185 StringAppendF(&hash_hex, "%02x", |
| 155 static_cast<uint8>(hash_string.data()[j])); | 186 static_cast<uint8>(hash_string.data()[j])); |
| 156 } | 187 } |
| 157 EXPECT_EQ(cases[i].output, hash_hex); | 188 EXPECT_EQ(cases[i].output, hash_hex); |
| 158 } | 189 } |
| 159 }; | 190 }; |
| OLD | NEW |