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" |
| 19 #include "ui/gfx/size.h" |
17 #include "webkit/plugins/webplugininfo.h" | 20 #include "webkit/plugins/webplugininfo.h" |
18 | 21 |
19 using base::TimeDelta; | 22 using base::TimeDelta; |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 // Since buildtime is highly variable, this function will scan an output log and | 26 const char kClientId[] = "bogus client ID"; |
24 // replace it with a consistent number. | 27 const int kSessionId = 127; |
25 void NormalizeBuildtime(std::string* xml_encoded) { | 28 const int kScreenWidth = 1024; |
26 std::string prefix = "buildtime=\""; | 29 const int kScreenHeight = 768; |
27 const char postfix = '\"'; | 30 const int kScreenCount = 3; |
28 size_t offset = xml_encoded->find(prefix); | 31 const base::FieldTrial::NameGroupId kFieldTrialIds[] = { |
29 ASSERT_NE(std::string::npos, offset); | 32 {37, 43}, |
30 offset += prefix.size(); | 33 {13, 47}, |
31 size_t postfix_position = xml_encoded->find(postfix, offset); | 34 {23, 17} |
32 ASSERT_NE(std::string::npos, postfix_position); | 35 }; |
33 for (size_t i = offset; i < postfix_position; ++i) { | 36 |
34 char digit = xml_encoded->at(i); | 37 class TestMetricsLog : public MetricsLog { |
35 ASSERT_GE(digit, '0'); | 38 public: |
36 ASSERT_LE(digit, '9'); | 39 TestMetricsLog(const std::string& client_id, int session_id) |
| 40 : MetricsLog(client_id, session_id) { |
| 41 browser::RegisterLocalState(&prefs_); |
| 42 |
| 43 #if defined(OS_CHROMEOS) |
| 44 prefs_.SetInteger(prefs::kStabilityChildProcessCrashCount, 10); |
| 45 prefs_.SetInteger(prefs::kStabilityOtherUserCrashCount, 11); |
| 46 prefs_.SetInteger(prefs::kStabilityKernelCrashCount, 12); |
| 47 prefs_.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); |
| 48 #endif // OS_CHROMEOS |
| 49 } |
| 50 virtual ~TestMetricsLog() {} |
| 51 |
| 52 virtual PrefService* GetPrefService() OVERRIDE { |
| 53 return &prefs_; |
37 } | 54 } |
38 | 55 |
39 // Insert a single fake buildtime. | 56 const metrics::SystemProfileProto& system_profile() const { |
40 xml_encoded->replace(offset, postfix_position - offset, "123246"); | 57 return uma_proto()->system_profile(); |
41 } | 58 } |
42 | 59 |
43 class NoTimeMetricsLog : public MetricsLog { | 60 private: |
44 public: | 61 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(); | 62 return std::string(); |
52 } | 63 } |
| 64 |
| 65 virtual void GetFieldTrialIds( |
| 66 std::vector<base::FieldTrial::NameGroupId>* field_trial_ids) const |
| 67 OVERRIDE { |
| 68 ASSERT_TRUE(field_trial_ids->empty()); |
| 69 |
| 70 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) { |
| 71 field_trial_ids->push_back(kFieldTrialIds[i]); |
| 72 } |
| 73 } |
| 74 |
| 75 virtual gfx::Size GetScreenSize() const OVERRIDE { |
| 76 return gfx::Size(kScreenWidth, kScreenHeight); |
| 77 } |
| 78 |
| 79 virtual int GetScreenCount() const OVERRIDE { |
| 80 return kScreenCount; |
| 81 } |
| 82 |
| 83 TestingPrefService prefs_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
53 }; | 86 }; |
54 | 87 |
55 } // namespace | 88 } // namespace |
56 | 89 |
57 class MetricsLogTest : public testing::Test { | 90 class MetricsLogTest : public testing::Test { |
58 }; | 91 }; |
59 | 92 |
60 #if defined(OS_CHROMEOS) | 93 TEST_F(MetricsLogTest, RecordEnvironment) { |
61 TEST(MetricsLogTest, ChromeOSStabilityData) { | 94 // Everything except build_timestamp and app_version |
62 NoTimeMetricsLog log("bogus client ID", 0); | 95 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 | 96 |
71 std::vector<webkit::WebPluginInfo> plugins; | 97 std::vector<webkit::WebPluginInfo> plugins; |
| 98 log.RecordEnvironment(plugins, NULL); |
72 | 99 |
73 std::string expected_output = base::StringPrintf( | 100 const metrics::SystemProfileProto& system_profile = log.system_profile(); |
74 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 101 ASSERT_EQ(arraysize(kFieldTrialIds), |
75 "appversion=\"%s\">\n" | 102 static_cast<size_t>(system_profile.field_trial_size())); |
76 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); | 103 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) { |
| 104 const metrics::SystemProfileProto::FieldTrial& field_trial = |
| 105 system_profile.field_trial(i); |
| 106 EXPECT_EQ(kFieldTrialIds[i].name, field_trial.name_id()); |
| 107 EXPECT_EQ(kFieldTrialIds[i].group, field_trial.group_id()); |
| 108 } |
| 109 |
| 110 // TODO(isherman): Verify other data written into the protobuf as a result of |
| 111 // this call. |
| 112 } |
| 113 |
| 114 #if defined(OS_CHROMEOS) |
| 115 TEST_F(MetricsLogTest, ChromeOSStabilityData) { |
| 116 TestMetricsLog log(kClientId, kSessionId); |
| 117 |
77 // Expect 3 warnings about not yet being able to send the | 118 // Expect 3 warnings about not yet being able to send the |
78 // Chrome OS stability stats. | 119 // Chrome OS stability stats. |
79 log.WriteStabilityElement(plugins, &prefs); | 120 std::vector<webkit::WebPluginInfo> plugins; |
| 121 PrefService* prefs = log.GetPrefService(); |
| 122 log.WriteStabilityElement(plugins, prefs); |
80 log.CloseLog(); | 123 log.CloseLog(); |
81 | 124 |
82 int size = log.GetEncodedLogSizeXml(); | 125 int size = log.GetEncodedLogSizeXml(); |
83 ASSERT_GT(size, 0); | 126 ASSERT_GT(size, 0); |
84 | 127 |
85 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityChildProcessCrashCount)); | 128 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityChildProcessCrashCount)); |
86 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityOtherUserCrashCount)); | 129 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityOtherUserCrashCount)); |
87 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityKernelCrashCount)); | 130 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityKernelCrashCount)); |
88 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); | 131 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); |
89 | 132 |
90 std::string encoded; | 133 std::string encoded; |
91 // Leave room for the NUL terminator. | 134 // Leave room for the NUL terminator. |
92 bool encoding_result = log.GetEncodedLogXml( | 135 bool encoding_result = log.GetEncodedLogXml( |
93 WriteInto(&encoded, size + 1), size); | 136 WriteInto(&encoded, size + 1), size); |
94 ASSERT_TRUE(encoding_result); | 137 ASSERT_TRUE(encoding_result); |
95 | 138 |
96 // Check that we can find childprocesscrashcount, but not | 139 // Check that we can find childprocesscrashcount, but not |
97 // any of the ChromeOS ones that we are not emitting until log | 140 // any of the ChromeOS ones that we are not emitting until log |
98 // servers can handle them. | 141 // servers can handle them. |
99 EXPECT_NE(std::string::npos, | 142 EXPECT_NE(std::string::npos, |
100 encoded.find(" childprocesscrashcount=\"10\"")); | 143 encoded.find(" childprocesscrashcount=\"10\"")); |
101 EXPECT_EQ(std::string::npos, | 144 EXPECT_EQ(std::string::npos, |
102 encoded.find(" otherusercrashcount=")); | 145 encoded.find(" otherusercrashcount=")); |
103 EXPECT_EQ(std::string::npos, | 146 EXPECT_EQ(std::string::npos, |
104 encoded.find(" kernelcrashcount=")); | 147 encoded.find(" kernelcrashcount=")); |
105 EXPECT_EQ(std::string::npos, | 148 EXPECT_EQ(std::string::npos, |
106 encoded.find(" systemuncleanshutdowns=")); | 149 encoded.find(" systemuncleanshutdowns=")); |
107 } | 150 } |
108 #endif // OS_CHROMEOS | 151 #endif // OS_CHROMEOS |
OLD | NEW |