Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/common/metrics/metrics_log_base_unittest.cc

Issue 9232071: Upload UMA data using protocol buffers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/metrics/metrics_log_base.cc ('k') | chrome/common/metrics/metrics_log_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/base64.h"
8 #include "base/format_macros.h"
9 #include "base/stringprintf.h"
10 #include "base/string_util.h"
11 #include "base/time.h"
12 #include "chrome/common/metrics/metrics_log_base.h"
13 #include "googleurl/src/gurl.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using base::TimeDelta;
17
18 namespace {
19
20 // Since buildtime is highly variable, this function will scan an output log and
21 // replace it with a consistent number.
22 void NormalizeBuildtime(std::string* xml_encoded) {
23 std::string prefix = "buildtime=\"";
24 const char postfix = '\"';
25 size_t offset = xml_encoded->find(prefix);
26 ASSERT_NE(std::string::npos, offset);
27 offset += prefix.size();
28 size_t postfix_position = xml_encoded->find(postfix, offset);
29 ASSERT_NE(std::string::npos, postfix_position);
30 for (size_t i = offset; i < postfix_position; ++i) {
31 char digit = xml_encoded->at(i);
32 ASSERT_GE(digit, '0');
33 ASSERT_LE(digit, '9');
34 }
35
36 // Insert a single fake buildtime.
37 xml_encoded->replace(offset, postfix_position - offset, "123246");
38 }
39
40 class NoTimeMetricsLogBase : public MetricsLogBase {
41 public:
42 NoTimeMetricsLogBase(std::string client_id,
43 int session_id,
44 std::string version_string):
45 MetricsLogBase(client_id, session_id, version_string) {}
46 virtual ~NoTimeMetricsLogBase() {}
47
48 // Override this so that output is testable.
49 virtual std::string GetCurrentTimeString() OVERRIDE {
50 return std::string();
51 }
52 };
53
54 } // namespace
55
56 TEST(MetricsLogBaseTest, EmptyRecord) {
57 std::string expected_output =
58 #if defined(OS_CHROMEOS)
59 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" "
60 "appversion=\"bogus version\" hardwareclass=\"sample-class\"/>";
61 #else
62 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" "
63 "appversion=\"bogus version\"/>";
64 #endif // OS_CHROMEOS
65
66 MetricsLogBase log("bogus client ID", 0, "bogus version");
67 log.set_hardware_class("sample-class");
68 log.CloseLog();
69
70 int size = log.GetEncodedLogSizeXml();
71 ASSERT_GT(size, 0);
72
73 std::string encoded;
74 // Leave room for the NUL terminator.
75 ASSERT_TRUE(log.GetEncodedLogXml(WriteInto(&encoded, size + 1), size));
76 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded);
77 NormalizeBuildtime(&encoded);
78 NormalizeBuildtime(&expected_output);
79
80 ASSERT_EQ(expected_output, encoded);
81 }
82
83 TEST(MetricsLogBaseTest, WindowEvent) {
84 std::string expected_output =
85 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" "
86 "appversion=\"bogus version\">\n"
87 " <window action=\"create\" windowid=\"0\" session=\"0\" time=\"\"/>\n"
88 " <window action=\"open\" windowid=\"1\" parent=\"0\" "
89 "session=\"0\" time=\"\"/>\n"
90 " <window action=\"close\" windowid=\"1\" parent=\"0\" "
91 "session=\"0\" time=\"\"/>\n"
92 " <window action=\"destroy\" windowid=\"0\" session=\"0\" time=\"\"/>\n"
93 "</log>";
94
95 NoTimeMetricsLogBase log("bogus client ID", 0, "bogus version");
96 log.RecordWindowEvent(MetricsLogBase::WINDOW_CREATE, 0, -1);
97 log.RecordWindowEvent(MetricsLogBase::WINDOW_OPEN, 1, 0);
98 log.RecordWindowEvent(MetricsLogBase::WINDOW_CLOSE, 1, 0);
99 log.RecordWindowEvent(MetricsLogBase::WINDOW_DESTROY, 0, -1);
100 log.CloseLog();
101
102 ASSERT_EQ(4, log.num_events());
103
104 int size = log.GetEncodedLogSizeXml();
105 ASSERT_GT(size, 0);
106
107 std::string encoded;
108 // Leave room for the NUL terminator.
109 ASSERT_TRUE(log.GetEncodedLogXml(WriteInto(&encoded, size + 1), size));
110 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded);
111 NormalizeBuildtime(&encoded);
112 NormalizeBuildtime(&expected_output);
113
114 ASSERT_EQ(expected_output, encoded);
115 }
116
117 TEST(MetricsLogBaseTest, LoadEvent) {
118 std::string expected_output =
119 #if defined(OS_CHROMEOS)
120 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" "
121 "appversion=\"bogus version\" hardwareclass=\"sample-class\">\n"
122 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" "
123 "origin=\"link\" session=\"0\" time=\"\"/>\n"
124 "</log>";
125 #else
126 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" "
127 "appversion=\"bogus version\">\n"
128 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" "
129 "origin=\"link\" session=\"0\" time=\"\"/>\n"
130 "</log>";
131 #endif // OS_CHROMEOS
132
133 NoTimeMetricsLogBase log("bogus client ID", 0, "bogus version");
134 log.RecordLoadEvent(3, GURL("http://google.com"),
135 content::PAGE_TRANSITION_LINK, 1,
136 TimeDelta::FromMilliseconds(7219));
137 log.set_hardware_class("sample-class");
138 log.CloseLog();
139
140 ASSERT_EQ(1, log.num_events());
141
142 int size = log.GetEncodedLogSizeXml();
143 ASSERT_GT(size, 0);
144
145 std::string encoded;
146 // Leave room for the NUL terminator.
147 ASSERT_TRUE(log.GetEncodedLogXml(WriteInto(&encoded, size + 1), size));
148 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded);
149 NormalizeBuildtime(&encoded);
150 NormalizeBuildtime(&expected_output);
151
152 ASSERT_EQ(expected_output, encoded);
153 }
154
155 // Make sure our ID hashes are the same as what we see on the server side.
156 TEST(MetricsLogBaseTest, CreateHashes) {
157 static const struct {
158 std::string input;
159 std::string output;
160 } cases[] = {
161 {"Back", "0x0557fa923dcee4d0"},
162 {"Forward", "0x67d2f6740a8eaebf"},
163 {"NewTab", "0x290eb683f96572f1"},
164 };
165
166 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
167 std::string hash_string;
168 uint64 hash_numeric;
169 MetricsLogBase::CreateHashes(cases[i].input, &hash_string, &hash_numeric);
170
171 // Verify the numeric representation.
172 {
173 std::string hash_numeric_hex =
174 base::StringPrintf("0x%016" PRIx64, hash_numeric);
175 EXPECT_EQ(cases[i].output, hash_numeric_hex);
176 }
177
178 // Verify the string representation.
179 {
180 std::string hash_string_decoded;
181 ASSERT_TRUE(base::Base64Decode(hash_string, &hash_string_decoded));
182
183 // Convert to hex string
184 // We're only checking the first 8 bytes, because that's what
185 // the metrics server uses.
186 std::string hash_string_hex = "0x";
187 ASSERT_GE(hash_string_decoded.size(), 8U);
188 for (size_t j = 0; j < 8; j++) {
189 base::StringAppendF(&hash_string_hex, "%02x",
190 static_cast<uint8>(hash_string_decoded.data()[j]));
191 }
192 EXPECT_EQ(cases[i].output, hash_string_hex);
193 }
194 }
195 };
OLDNEW
« no previous file with comments | « chrome/common/metrics/metrics_log_base.cc ('k') | chrome/common/metrics/metrics_log_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698