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

Side by Side Diff: chrome/browser/metrics/metrics_log_unittest.cc

Issue 9559017: [UMA] Include field trial tuples in the UMA upload. (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
OLDNEW
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/basictypes.h"
7 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
8 #include "base/string_util.h" 9 #include "base/string_util.h"
9 #include "base/time.h" 10 #include "base/time.h"
10 #include "chrome/browser/metrics/metrics_log.h" 11 #include "chrome/browser/metrics/metrics_log.h"
11 #include "chrome/browser/prefs/browser_prefs.h" 12 #include "chrome/browser/prefs/browser_prefs.h"
12 #include "chrome/browser/prefs/pref_service.h" 13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/common/metrics/proto/system_profile.pb.h"
13 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/testing_pref_service.h" 16 #include "chrome/test/base/testing_pref_service.h"
15 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
16 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
17 #include "webkit/plugins/webplugininfo.h" 19 #include "webkit/plugins/webplugininfo.h"
18 20
19 using base::TimeDelta; 21 using base::TimeDelta;
20 22
21 namespace { 23 namespace {
22 24
23 // Since buildtime is highly variable, this function will scan an output log and 25 const char kClientId[] = "bogus client ID";
24 // replace it with a consistent number. 26 const int kSessionId = 127;
25 void NormalizeBuildtime(std::string* xml_encoded) { 27 const base::FieldTrial::NameGroupId kFieldTrialIds[] = {
26 std::string prefix = "buildtime=\""; 28 {37, 43},
27 const char postfix = '\"'; 29 {13, 47},
28 size_t offset = xml_encoded->find(prefix); 30 {23, 17}
29 ASSERT_NE(std::string::npos, offset); 31 };
30 offset += prefix.size(); 32
31 size_t postfix_position = xml_encoded->find(postfix, offset); 33 class TestMetricsLog : public MetricsLog {
32 ASSERT_NE(std::string::npos, postfix_position); 34 public:
33 for (size_t i = offset; i < postfix_position; ++i) { 35 TestMetricsLog(const std::string& client_id, int session_id)
34 char digit = xml_encoded->at(i); 36 : MetricsLog(client_id, session_id) {
35 ASSERT_GE(digit, '0'); 37 browser::RegisterLocalState(&prefs_);
36 ASSERT_LE(digit, '9'); 38
39 #if defined(OS_CHROMEOS)
40 prefs_.SetInteger(prefs::kStabilityChildProcessCrashCount, 10);
41 prefs_.SetInteger(prefs::kStabilityOtherUserCrashCount, 11);
42 prefs_.SetInteger(prefs::kStabilityKernelCrashCount, 12);
43 prefs_.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13);
44 #endif // OS_CHROMEOS
45 }
46 virtual ~TestMetricsLog() {}
47
48 virtual std::string GetCurrentTimeString() OVERRIDE {
49 return std::string();
37 } 50 }
38 51
39 // Insert a single fake buildtime. 52 virtual PrefService* GetPrefService() OVERRIDE {
40 xml_encoded->replace(offset, postfix_position - offset, "123246"); 53 return &prefs_;
41 } 54 }
42 55
43 class NoTimeMetricsLog : public MetricsLog { 56 virtual void GetFieldTrialIds(
44 public: 57 std::vector<base::FieldTrial::NameGroupId>* field_trial_ids) const
45 NoTimeMetricsLog(std::string client_id, int session_id): 58 OVERRIDE {
46 MetricsLog(client_id, session_id) {} 59 ASSERT_TRUE(field_trial_ids->empty());
47 virtual ~NoTimeMetricsLog() {}
48 60
49 // Override this so that output is testable. 61 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
50 virtual std::string GetCurrentTimeString() { 62 field_trial_ids->push_back(kFieldTrialIds[i]);
51 return std::string(); 63 }
52 } 64 }
65
66 const metrics::SystemProfileProto& system_profile() {
67 return uma_proto()->system_profile();
68 }
69
70 private:
71 TestingPrefService prefs_;
72
73 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
53 }; 74 };
54 75
55 } // namespace 76 } // namespace
56 77
57 class MetricsLogTest : public testing::Test { 78 class MetricsLogTest : public testing::Test {
58 }; 79 };
59 80
60 #if defined(OS_CHROMEOS) 81 TEST_F(MetricsLogTest, RecordEnvironment) {
61 TEST(MetricsLogTest, ChromeOSStabilityData) { 82 // Everything except build_timestamp and app_version
62 NoTimeMetricsLog log("bogus client ID", 0); 83 TestMetricsLog log(kClientId, kSessionId);
63 TestingPrefService prefs;
64 browser::RegisterLocalState(&prefs);
65
66 prefs.SetInteger(prefs::kStabilityChildProcessCrashCount, 10);
67 prefs.SetInteger(prefs::kStabilityOtherUserCrashCount, 11);
68 prefs.SetInteger(prefs::kStabilityKernelCrashCount, 12);
69 prefs.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13);
70 84
71 std::vector<webkit::WebPluginInfo> plugins; 85 std::vector<webkit::WebPluginInfo> plugins;
86 log.RecordEnvironment(plugins, NULL);
72 87
73 std::string expected_output = base::StringPrintf( 88 const metrics::SystemProfileProto& system_profile = log.system_profile();
74 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " 89 ASSERT_EQ(arraysize(kFieldTrialIds),
75 "appversion=\"%s\">\n" 90 static_cast<size_t>(system_profile.field_trial_size()));
76 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); 91 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
92 const metrics::SystemProfileProto::FieldTrial& field_trial =
93 system_profile.field_trial(i);
94 EXPECT_EQ(kFieldTrialIds[i].name, field_trial.name_id());
95 EXPECT_EQ(kFieldTrialIds[i].group, field_trial.group_id());
96 }
97
98 // TODO(isherman): Verify other data written into the protobuf as a result of
99 // this call.
100 }
101
102 #if defined(OS_CHROMEOS)
103 TEST_F(MetricsLogTest, ChromeOSStabilityData) {
104 TestMetricsLog log(kClientId, kSessionId);
105
77 // Expect 3 warnings about not yet being able to send the 106 // Expect 3 warnings about not yet being able to send the
78 // Chrome OS stability stats. 107 // Chrome OS stability stats.
108 std::vector<webkit::WebPluginInfo> plugins;
79 log.WriteStabilityElement(plugins, &prefs); 109 log.WriteStabilityElement(plugins, &prefs);
80 log.CloseLog(); 110 log.CloseLog();
81 111
82 int size = log.GetEncodedLogSizeXml(); 112 int size = log.GetEncodedLogSizeXml();
83 ASSERT_GT(size, 0); 113 ASSERT_GT(size, 0);
84 114
85 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityChildProcessCrashCount)); 115 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityChildProcessCrashCount));
86 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityOtherUserCrashCount)); 116 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityOtherUserCrashCount));
87 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityKernelCrashCount)); 117 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityKernelCrashCount));
88 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); 118 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilitySystemUncleanShutdownCount));
(...skipping 10 matching lines...) Expand all
99 EXPECT_NE(std::string::npos, 129 EXPECT_NE(std::string::npos,
100 encoded.find(" childprocesscrashcount=\"10\"")); 130 encoded.find(" childprocesscrashcount=\"10\""));
101 EXPECT_EQ(std::string::npos, 131 EXPECT_EQ(std::string::npos,
102 encoded.find(" otherusercrashcount=")); 132 encoded.find(" otherusercrashcount="));
103 EXPECT_EQ(std::string::npos, 133 EXPECT_EQ(std::string::npos,
104 encoded.find(" kernelcrashcount=")); 134 encoded.find(" kernelcrashcount="));
105 EXPECT_EQ(std::string::npos, 135 EXPECT_EQ(std::string::npos,
106 encoded.find(" systemuncleanshutdowns=")); 136 encoded.find(" systemuncleanshutdowns="));
107 } 137 }
108 #endif // OS_CHROMEOS 138 #endif // OS_CHROMEOS
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698