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

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

Powered by Google App Engine
This is Rietveld 408576698