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