OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdint.h> | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/memory/scoped_vector.h" | |
9 #include "base/path_service.h" | |
10 /*#include "base/prefs/pref_registry_simple.h" | |
11 #include "base/prefs/testing_pref_service.h"*/ | |
Ilya Sherman
2014/08/08 03:49:45
Please update this.
Alexander Alekseev
2014/08/09 01:30:01
Done.
| |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/about_flags.h" | |
17 //#include "chrome/browser/pref_service_flags_storage.h" | |
18 //#include "chrome/common/chrome_switches.h" | |
19 //#include "chrome/common/pref_names.h" | |
20 //#include "grit/chromium_strings.h" | |
Ilya Sherman
2014/08/08 03:49:45
Please update this.
Alexander Alekseev
2014/08/09 01:30:01
Done.
| |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 #include "third_party/libxml/chromium/libxml_utils.h" | |
23 | |
24 namespace { | |
25 | |
26 typedef std::map<std::string, uint32_t> SwitchToIdMap; | |
27 | |
28 // This is a helper function to the next one. | |
29 // Extracts single enum (with integer values) from histograms.xml. | |
30 // Expects |reader| to point at given enum. | |
31 // Returns map { value => label }. | |
32 // Returns empty map on error. | |
33 std::map<uint32_t, std::string> ParseEnumFromHistogramsXml( | |
34 const std::string& enum_name, | |
35 XmlReader* reader) { | |
36 int entries_index = -1; | |
37 | |
38 std::map<uint32_t, std::string> result; | |
39 bool success = true; | |
40 | |
41 while (true) { | |
42 const std::string node_name = reader->NodeName(); | |
43 if (node_name == "enum" && reader->IsClosingElement()) | |
44 break; | |
45 | |
46 if (node_name == "int") { | |
47 ++entries_index; | |
48 std::string value_str; | |
49 std::string label; | |
50 const bool has_value = reader->NodeAttribute("value", &value_str); | |
51 const bool has_label = reader->NodeAttribute("label", &label); | |
52 if (!has_value) { | |
53 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " | |
54 << entries_index << ", label='" << label | |
55 << "'): No 'value' attribute."; | |
56 success = false; | |
57 } | |
58 if (!has_label) { | |
59 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " | |
60 << entries_index << ", value_str='" << value_str | |
61 << "'): No 'label' attribute."; | |
62 success = false; | |
63 } | |
64 | |
65 uint32_t value; | |
66 if (has_value && !base::StringToUint(value_str, &value)) { | |
67 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " | |
68 << entries_index << ", label='" << label | |
69 << "', value_str='" << value_str | |
70 << "'): 'value' attribute is not integer."; | |
71 success = false; | |
72 } | |
73 if (result.count(value)) { | |
74 ADD_FAILURE() << "Bad " << enum_name << " enum entry (at index " | |
75 << entries_index << ", label='" << label | |
76 << "', value_str='" << value_str | |
77 << "'): duplicate value '" << value_str | |
78 << "' found in enum. The previous one has label='" | |
79 << result[value] << "'."; | |
80 success = false; | |
81 } | |
82 if (success) { | |
83 result[value] = label; | |
84 } | |
85 } | |
86 // All enum entries are on the same level, so it is enough to iterate | |
87 // until possible. | |
88 reader->Next(); | |
89 } | |
90 return (success ? result : std::map<uint32_t, std::string>()); | |
91 } | |
92 | |
93 // Find and read given enum (with integer values) from histograms.xml. | |
94 // |enum_name| - enum name. | |
95 // |histograms_xml| - must be loaded histograms.xml file. | |
96 // | |
97 // Returns map { value => label } so that: | |
98 // <int value="9" label="enable-pinch-virtual-viewport"/> | |
99 // becomes: | |
100 // { 9 => "enable-pinch-virtual-viewport" } | |
101 // Returns empty map on error. | |
102 std::map<uint32_t, std::string> ReadEnumFromHistogramsXml( | |
103 const std::string& enum_name, | |
104 XmlReader* histograms_xml) { | |
105 std::map<uint32_t, std::string> login_custom_flags; | |
106 | |
107 // Implement simple depth first search. | |
108 while (true) { | |
109 const std::string node_name = histograms_xml->NodeName(); | |
110 if (node_name == "enum") { | |
111 std::string name; | |
112 if (histograms_xml->NodeAttribute("name", &name) && name == enum_name) { | |
113 if (!login_custom_flags.empty()) { | |
114 EXPECT_TRUE(login_custom_flags.empty()) | |
115 << "Duplicate enum '" << enum_name << "' found in histograms.xml"; | |
116 return std::map<uint32_t, std::string>(); | |
117 } | |
118 | |
119 const bool got_into_enum = histograms_xml->Read(); | |
120 if (got_into_enum) { | |
121 login_custom_flags = | |
122 ParseEnumFromHistogramsXml(enum_name, histograms_xml); | |
123 EXPECT_FALSE(login_custom_flags.empty()) | |
124 << "Bad enum '" << enum_name | |
125 << "' found in histograms.xml (format error)."; | |
126 } else { | |
127 EXPECT_TRUE(got_into_enum) | |
128 << "Bad enum '" << enum_name | |
129 << "' (looks empty) found in histograms.xml."; | |
130 } | |
131 if (login_custom_flags.empty()) | |
132 return std::map<uint32_t, std::string>(); | |
133 } | |
134 } | |
135 // Go deeper if possible (stops at the closing tag of the deepest node). | |
136 if (histograms_xml->Read()) | |
137 continue; | |
138 | |
139 // Try next node on the same level (skips closing tag). | |
140 if (histograms_xml->Next()) | |
141 continue; | |
142 | |
143 // Go up until next node on the same level exists. | |
144 while (histograms_xml->Depth() && !histograms_xml->SkipToElement()) { | |
145 } | |
146 | |
147 // Reached top. histograms.xml consists of the single top level node | |
148 // 'histogram-configuration', so this is the end. | |
149 if (!histograms_xml->Depth()) | |
150 break; | |
151 } | |
152 EXPECT_FALSE(login_custom_flags.empty()) | |
153 << "Enum '" << enum_name << "' is not found in histograms.xml."; | |
154 return login_custom_flags; | |
155 } | |
156 | |
157 std::string FilePathStringTypeToString(const base::FilePath::StringType& path) { | |
158 #if defined(OS_WIN) | |
159 return UTF16ToUTF8(path); | |
160 #else | |
161 return path; | |
162 #endif | |
163 } | |
164 | |
165 std::set<std::string> GetAllSwitchesForTesting() { | |
166 std::set<std::string> result; | |
167 | |
168 size_t num_experiments = 0; | |
169 const about_flags::Experiment* experiments = | |
170 about_flags::testing::GetExperiments(&num_experiments); | |
171 | |
172 for (size_t i = 0; i < num_experiments; ++i) { | |
173 const about_flags::Experiment& experiment = experiments[i]; | |
174 if (experiment.type == about_flags::Experiment::SINGLE_VALUE) { | |
175 result.insert(experiment.command_line_switch); | |
176 } else if (experiment.type == about_flags::Experiment::MULTI_VALUE) { | |
177 for (int j = 0; j < experiment.num_choices; ++j) { | |
178 result.insert(experiment.choices[j].command_line_switch); | |
179 } | |
180 } else { | |
181 DCHECK_EQ(experiment.type, about_flags::Experiment::ENABLE_DISABLE_VALUE); | |
182 result.insert(experiment.command_line_switch); | |
183 result.insert(experiment.disable_command_line_switch); | |
184 } | |
185 } | |
186 return result; | |
187 } | |
188 | |
189 } // anonymous namespace | |
190 | |
191 namespace about_flags { | |
192 | |
193 class AboutFlagsHistogramTest : public ::testing::Test { | |
194 protected: | |
195 // This is a helper function to check that all IDs in enum LoginCustomFlags in | |
196 // histograms.xml are unique. | |
197 void SetSwitchToHistogramIdMapping(const std::string& switch_name, | |
198 const uint32_t switch_histogram_id, | |
199 std::map<std::string, uint32_t>* out_map) { | |
200 const std::pair<std::map<std::string, uint32_t>::iterator, bool> status = | |
201 out_map->insert(std::make_pair(switch_name, switch_histogram_id)); | |
202 if (!status.second) { | |
203 EXPECT_TRUE(status.first->second == switch_histogram_id) | |
204 << "Duplicate switch '" << switch_name | |
205 << "' found in enum 'LoginCustomFlags' in histograms.xml."; | |
206 } | |
207 } | |
208 | |
209 // This method generates a hint for the user for what string should be added | |
210 // to the enum LoginCustomFlags to make in consistent. | |
211 std::string GetHistogramEnumEntryText(const std::string& switch_name, | |
212 uint32_t value) { | |
213 return base::StringPrintf( | |
214 "<int value=\"%u\" label=\"%s\"/>", value, switch_name.c_str()); | |
215 } | |
216 }; | |
217 | |
218 TEST_F(AboutFlagsHistogramTest, CheckHistograms) { | |
219 base::FilePath histograms_xml_file_path; | |
220 ASSERT_TRUE( | |
221 PathService::Get(base::DIR_SOURCE_ROOT, &histograms_xml_file_path)); | |
222 histograms_xml_file_path = histograms_xml_file_path.AppendASCII("tools") | |
223 .AppendASCII("metrics") | |
224 .AppendASCII("histograms") | |
225 .AppendASCII("histograms.xml"); | |
226 | |
227 XmlReader histograms_xml; | |
228 ASSERT_TRUE(histograms_xml.LoadFile( | |
229 FilePathStringTypeToString(histograms_xml_file_path.value()))); | |
230 std::map<uint32_t, std::string> login_custom_flags = | |
231 ReadEnumFromHistogramsXml("LoginCustomFlags", &histograms_xml); | |
232 ASSERT_TRUE(login_custom_flags.size()) | |
233 << "Error reading enum 'LoginCustomFlags' from histograms.xml."; | |
234 | |
235 // Build reverse map {switch_name => id} from login_custom_flags. | |
236 SwitchToIdMap histograms_xml_switches_ids; | |
237 | |
238 EXPECT_TRUE(login_custom_flags.count(kBadSwitchFormatHistogramId)) | |
239 << "Entry for UMA ID of incorrect command-line flag is not found in " | |
240 "histograms.xml enum LoginCustomFlags. " | |
241 "Consider adding entry:\n" | |
242 << " " << GetHistogramEnumEntryText("BAD_FLAG_FORMAT", 0); | |
243 // Check that all LoginCustomFlags entries have correct values. | |
244 for (std::map<uint32_t, std::string>::const_iterator it = | |
245 login_custom_flags.begin(); | |
246 it != login_custom_flags.end(); | |
247 ++it) { | |
248 if (it->first == kBadSwitchFormatHistogramId) { | |
249 // Add eror value with empty name. | |
250 SetSwitchToHistogramIdMapping( | |
251 "", it->first, &histograms_xml_switches_ids); | |
252 continue; | |
253 } | |
254 const uint32_t uma_id = GetSwitchUMAId(it->second); | |
255 EXPECT_EQ(uma_id, it->first) | |
256 << "histograms.xml enum LoginCustomFlags " | |
257 "entry '" << it->second << "' has incorrect value=" << it->first | |
258 << ", but " << uma_id << " is expected. Consider changing entry to:\n" | |
259 << " " << GetHistogramEnumEntryText(it->second, uma_id); | |
260 SetSwitchToHistogramIdMapping( | |
261 it->second, it->first, &histograms_xml_switches_ids); | |
262 } | |
263 | |
264 // Check that all flags in about_flags.cc have entries in login_custom_flags. | |
265 std::set<std::string> all_switches = GetAllSwitchesForTesting(); | |
266 for (std::set<std::string>::const_iterator it = all_switches.begin(); | |
267 it != all_switches.end(); | |
268 ++it) { | |
269 // Skip empty placeholders. | |
270 if (it->empty()) | |
271 continue; | |
272 const uint32_t uma_id = GetSwitchUMAId(*it); | |
273 EXPECT_NE(kBadSwitchFormatHistogramId, uma_id) | |
274 << "Command-line switch '" << *it | |
275 << "' from about_flags.cc has UMA ID equal to reserved value " | |
276 "kBadSwitchFormatHistogramId=" << kBadSwitchFormatHistogramId | |
277 << ". Please modify switch name."; | |
278 SwitchToIdMap::iterator enum_entry = | |
279 histograms_xml_switches_ids.lower_bound(*it); | |
280 | |
281 // Ignore case here when switch ID is incorrect - it has already been | |
282 // reported in the previous loop. | |
283 EXPECT_TRUE(enum_entry != histograms_xml_switches_ids.end() && | |
284 enum_entry->first == *it) | |
285 << "histograms.xml enum LoginCustomFlags doesn't contain switch '" | |
286 << *it << "' (value=" << uma_id | |
287 << " expected). Consider adding entry:\n" | |
288 << " " << GetHistogramEnumEntryText(*it, uma_id); | |
289 } | |
290 } | |
291 | |
292 } // namespace about_flags | |
OLD | NEW |