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/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 PrefService* GetPrefService() OVERRIDE { |
| 49 return &prefs_; |
37 } | 50 } |
38 | 51 |
39 // Insert a single fake buildtime. | 52 const metrics::SystemProfileProto& system_profile() const { |
40 xml_encoded->replace(offset, postfix_position - offset, "123246"); | 53 return uma_proto()->system_profile(); |
41 } | 54 } |
42 | 55 |
43 class NoTimeMetricsLog : public MetricsLog { | 56 private: |
44 public: | 57 virtual std::string GetCurrentTimeString() OVERRIDE { |
45 NoTimeMetricsLog(std::string client_id, int session_id): | |
46 MetricsLog(client_id, session_id) {} | |
47 virtual ~NoTimeMetricsLog() {} | |
48 | |
49 // Override this so that output is testable. | |
50 virtual std::string GetCurrentTimeString() { | |
51 return std::string(); | 58 return std::string(); |
52 } | 59 } |
| 60 |
| 61 virtual void GetFieldTrialIds( |
| 62 std::vector<base::FieldTrial::NameGroupId>* field_trial_ids) const |
| 63 OVERRIDE { |
| 64 ASSERT_TRUE(field_trial_ids->empty()); |
| 65 |
| 66 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) { |
| 67 field_trial_ids->push_back(kFieldTrialIds[i]); |
| 68 } |
| 69 } |
| 70 |
| 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. |
79 log.WriteStabilityElement(plugins, &prefs); | 108 std::vector<webkit::WebPluginInfo> plugins; |
| 109 PrefService* prefs = log.GetPrefService(); |
| 110 log.WriteStabilityElement(plugins, prefs); |
80 log.CloseLog(); | 111 log.CloseLog(); |
81 | 112 |
82 int size = log.GetEncodedLogSizeXml(); | 113 int size = log.GetEncodedLogSizeXml(); |
83 ASSERT_GT(size, 0); | 114 ASSERT_GT(size, 0); |
84 | 115 |
85 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityChildProcessCrashCount)); | 116 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityChildProcessCrashCount)); |
86 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityOtherUserCrashCount)); | 117 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityOtherUserCrashCount)); |
87 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityKernelCrashCount)); | 118 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityKernelCrashCount)); |
88 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); | 119 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); |
89 | 120 |
90 std::string encoded; | 121 std::string encoded; |
91 // Leave room for the NUL terminator. | 122 // Leave room for the NUL terminator. |
92 bool encoding_result = log.GetEncodedLogXml( | 123 bool encoding_result = log.GetEncodedLogXml( |
93 WriteInto(&encoded, size + 1), size); | 124 WriteInto(&encoded, size + 1), size); |
94 ASSERT_TRUE(encoding_result); | 125 ASSERT_TRUE(encoding_result); |
95 | 126 |
96 // Check that we can find childprocesscrashcount, but not | 127 // Check that we can find childprocesscrashcount, but not |
97 // any of the ChromeOS ones that we are not emitting until log | 128 // any of the ChromeOS ones that we are not emitting until log |
98 // servers can handle them. | 129 // servers can handle them. |
99 EXPECT_NE(std::string::npos, | 130 EXPECT_NE(std::string::npos, |
100 encoded.find(" childprocesscrashcount=\"10\"")); | 131 encoded.find(" childprocesscrashcount=\"10\"")); |
101 EXPECT_EQ(std::string::npos, | 132 EXPECT_EQ(std::string::npos, |
102 encoded.find(" otherusercrashcount=")); | 133 encoded.find(" otherusercrashcount=")); |
103 EXPECT_EQ(std::string::npos, | 134 EXPECT_EQ(std::string::npos, |
104 encoded.find(" kernelcrashcount=")); | 135 encoded.find(" kernelcrashcount=")); |
105 EXPECT_EQ(std::string::npos, | 136 EXPECT_EQ(std::string::npos, |
106 encoded.find(" systemuncleanshutdowns=")); | 137 encoded.find(" systemuncleanshutdowns=")); |
107 } | 138 } |
108 #endif // OS_CHROMEOS | 139 #endif // OS_CHROMEOS |
OLD | NEW |