OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "base/metrics/field_trial_params.h" | 5 #include "base/metrics/field_trial_params.h" |
6 | 6 |
7 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/metrics/field_trial_param_associator.h" | 10 #include "base/metrics/field_trial_param_associator.h" |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 scoped_refptr<FieldTrial> trial( | 231 scoped_refptr<FieldTrial> trial( |
232 CreateFieldTrial(kTrialName, 100, "A", nullptr)); | 232 CreateFieldTrial(kTrialName, 100, "A", nullptr)); |
233 | 233 |
234 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_DISABLE_FEATURE, | 234 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_DISABLE_FEATURE, |
235 trial.get()); | 235 trial.get()); |
236 | 236 |
237 std::map<std::string, std::string> actualParams; | 237 std::map<std::string, std::string> actualParams; |
238 EXPECT_EQ(std::string(), GetFieldTrialParamValueByFeature(kFeature, "x")); | 238 EXPECT_EQ(std::string(), GetFieldTrialParamValueByFeature(kFeature, "x")); |
239 } | 239 } |
240 | 240 |
241 TEST_F(FieldTrialParamsTest, GetFieldTrialParamByFeatureAsInt) { | 241 TEST_F(FieldTrialParamsTest, FeatureParamString) { |
242 const std::string kTrialName = "GetFieldTrialParamsByFeature"; | 242 const std::string kTrialName = "GetFieldTrialParamsByFeature"; |
243 const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; | 243 |
| 244 static const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; |
| 245 static const FeatureParam<std::string> a{&kFeature, "a", "default"}; |
| 246 static const FeatureParam<std::string> b{&kFeature, "b", ""}; |
| 247 static const FeatureParam<std::string> c{&kFeature, "c", "default"}; |
| 248 static const FeatureParam<std::string> d{&kFeature, "d", ""}; |
| 249 static const FeatureParam<std::string> e{&kFeature, "e", "default"}; |
| 250 static const FeatureParam<std::string> f{&kFeature, "f", ""}; |
| 251 |
| 252 std::map<std::string, std::string> params; |
| 253 params["a"] = ""; |
| 254 params["b"] = "non-default"; |
| 255 params["c"] = "non-default"; |
| 256 params["d"] = ""; |
| 257 // "e" is not registered |
| 258 // "f" is not registered |
| 259 AssociateFieldTrialParams(kTrialName, "A", params); |
| 260 scoped_refptr<FieldTrial> trial( |
| 261 CreateFieldTrial(kTrialName, 100, "A", nullptr)); |
| 262 |
| 263 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 264 trial.get()); |
| 265 |
| 266 EXPECT_EQ("default", a.Get()); // empty |
| 267 EXPECT_EQ("non-default", b.Get()); |
| 268 EXPECT_EQ("non-default", c.Get()); |
| 269 EXPECT_EQ("", d.Get()); // empty |
| 270 EXPECT_EQ("default", e.Get()); // not registered |
| 271 EXPECT_EQ("", f.Get()); // not registered |
| 272 } |
| 273 |
| 274 TEST_F(FieldTrialParamsTest, FeatureParamInt) { |
| 275 const std::string kTrialName = "GetFieldTrialParamsByFeature"; |
| 276 |
| 277 static const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; |
| 278 static const FeatureParam<int> a{&kFeature, "a", 0}; |
| 279 static const FeatureParam<int> b{&kFeature, "b", 0}; |
| 280 static const FeatureParam<int> c{&kFeature, "c", 0}; |
| 281 static const FeatureParam<int> d{&kFeature, "d", 0}; |
| 282 static const FeatureParam<int> e{&kFeature, "e", 0}; |
244 | 283 |
245 std::map<std::string, std::string> params; | 284 std::map<std::string, std::string> params; |
246 params["a"] = "1"; | 285 params["a"] = "1"; |
247 params["b"] = "1.5"; | 286 params["b"] = "1.5"; |
248 params["c"] = "foo"; | 287 params["c"] = "foo"; |
249 params["d"] = ""; | 288 params["d"] = ""; |
250 // "e" is not registered | 289 // "e" is not registered |
251 AssociateFieldTrialParams(kTrialName, "A", params); | 290 AssociateFieldTrialParams(kTrialName, "A", params); |
252 scoped_refptr<FieldTrial> trial( | 291 scoped_refptr<FieldTrial> trial( |
253 CreateFieldTrial(kTrialName, 100, "A", nullptr)); | 292 CreateFieldTrial(kTrialName, 100, "A", nullptr)); |
254 | 293 |
255 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, | 294 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, |
256 trial.get()); | 295 trial.get()); |
257 | 296 |
258 std::map<std::string, std::string> actualParams; | |
259 EXPECT_EQ(1, GetFieldTrialParamByFeatureAsInt(kFeature, "a", 0)); | 297 EXPECT_EQ(1, GetFieldTrialParamByFeatureAsInt(kFeature, "a", 0)); |
260 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "b", 0)); // invalid | 298 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "b", 0)); // invalid |
261 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "c", 0)); // invalid | 299 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "c", 0)); // invalid |
262 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "d", 0)); // empty | 300 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "d", 0)); // empty |
263 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "e", 0)); // empty | 301 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsInt(kFeature, "e", 0)); // empty |
| 302 |
| 303 EXPECT_EQ(1, a.Get()); |
| 304 EXPECT_EQ(0, b.Get()); // invalid |
| 305 EXPECT_EQ(0, c.Get()); // invalid |
| 306 EXPECT_EQ(0, d.Get()); // empty |
| 307 EXPECT_EQ(0, e.Get()); // empty |
264 } | 308 } |
265 | 309 |
266 TEST_F(FieldTrialParamsTest, GetFieldTrialParamByFeatureAsDouble) { | 310 TEST_F(FieldTrialParamsTest, FeatureParamDouble) { |
267 const std::string kTrialName = "GetFieldTrialParamsByFeature"; | 311 const std::string kTrialName = "GetFieldTrialParamsByFeature"; |
268 const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; | 312 |
| 313 static const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; |
| 314 static const FeatureParam<double> a{&kFeature, "a", 0.0}; |
| 315 static const FeatureParam<double> b{&kFeature, "b", 0.0}; |
| 316 static const FeatureParam<double> c{&kFeature, "c", 0.0}; |
| 317 static const FeatureParam<double> d{&kFeature, "d", 0.0}; |
| 318 static const FeatureParam<double> e{&kFeature, "e", 0.0}; |
| 319 static const FeatureParam<double> f{&kFeature, "f", 0.0}; |
269 | 320 |
270 std::map<std::string, std::string> params; | 321 std::map<std::string, std::string> params; |
271 params["a"] = "1"; | 322 params["a"] = "1"; |
272 params["b"] = "1.5"; | 323 params["b"] = "1.5"; |
273 params["c"] = "1.0e-10"; | 324 params["c"] = "1.0e-10"; |
274 params["d"] = "foo"; | 325 params["d"] = "foo"; |
275 params["e"] = ""; | 326 params["e"] = ""; |
276 // "f" is not registered | 327 // "f" is not registered |
277 AssociateFieldTrialParams(kTrialName, "A", params); | 328 AssociateFieldTrialParams(kTrialName, "A", params); |
278 scoped_refptr<FieldTrial> trial( | 329 scoped_refptr<FieldTrial> trial( |
279 CreateFieldTrial(kTrialName, 100, "A", nullptr)); | 330 CreateFieldTrial(kTrialName, 100, "A", nullptr)); |
280 | 331 |
281 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, | 332 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, |
282 trial.get()); | 333 trial.get()); |
283 | 334 |
284 std::map<std::string, std::string> actualParams; | |
285 EXPECT_EQ(1, GetFieldTrialParamByFeatureAsDouble(kFeature, "a", 0)); | 335 EXPECT_EQ(1, GetFieldTrialParamByFeatureAsDouble(kFeature, "a", 0)); |
286 EXPECT_EQ(1.5, GetFieldTrialParamByFeatureAsDouble(kFeature, "b", 0)); | 336 EXPECT_EQ(1.5, GetFieldTrialParamByFeatureAsDouble(kFeature, "b", 0)); |
287 EXPECT_EQ(1.0e-10, GetFieldTrialParamByFeatureAsDouble(kFeature, "c", 0)); | 337 EXPECT_EQ(1.0e-10, GetFieldTrialParamByFeatureAsDouble(kFeature, "c", 0)); |
288 EXPECT_EQ(0, | 338 EXPECT_EQ(0, |
289 GetFieldTrialParamByFeatureAsDouble(kFeature, "d", 0)); // invalid | 339 GetFieldTrialParamByFeatureAsDouble(kFeature, "d", 0)); // invalid |
290 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsDouble(kFeature, "e", 0)); // empty | 340 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsDouble(kFeature, "e", 0)); // empty |
291 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsDouble(kFeature, "f", 0)); // empty | 341 EXPECT_EQ(0, GetFieldTrialParamByFeatureAsDouble(kFeature, "f", 0)); // empty |
| 342 |
| 343 EXPECT_EQ(1, a.Get()); |
| 344 EXPECT_EQ(1.5, b.Get()); |
| 345 EXPECT_EQ(1.0e-10, c.Get()); |
| 346 EXPECT_EQ(0, d.Get()); // invalid |
| 347 EXPECT_EQ(0, e.Get()); // empty |
| 348 EXPECT_EQ(0, f.Get()); // empty |
292 } | 349 } |
293 | 350 |
294 TEST_F(FieldTrialParamsTest, GetFieldTrialParamByFeatureAsBool) { | 351 TEST_F(FieldTrialParamsTest, FeatureParamBool) { |
295 const std::string kTrialName = "GetFieldTrialParamsByFeature"; | 352 const std::string kTrialName = "GetFieldTrialParamsByFeature"; |
296 const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; | 353 |
| 354 static const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; |
| 355 static const FeatureParam<bool> a{&kFeature, "a", false}; |
| 356 static const FeatureParam<bool> b{&kFeature, "b", true}; |
| 357 static const FeatureParam<bool> c{&kFeature, "c", false}; |
| 358 static const FeatureParam<bool> d{&kFeature, "d", true}; |
| 359 static const FeatureParam<bool> e{&kFeature, "e", true}; |
| 360 static const FeatureParam<bool> f{&kFeature, "f", true}; |
297 | 361 |
298 std::map<std::string, std::string> params; | 362 std::map<std::string, std::string> params; |
299 params["a"] = "true"; | 363 params["a"] = "true"; |
300 params["b"] = "false"; | 364 params["b"] = "false"; |
301 params["c"] = "1"; | 365 params["c"] = "1"; |
302 params["d"] = "False"; | 366 params["d"] = "False"; |
303 params["e"] = ""; | 367 params["e"] = ""; |
304 // "f" is not registered | 368 // "f" is not registered |
305 AssociateFieldTrialParams(kTrialName, "A", params); | 369 AssociateFieldTrialParams(kTrialName, "A", params); |
306 scoped_refptr<FieldTrial> trial( | 370 scoped_refptr<FieldTrial> trial( |
307 CreateFieldTrial(kTrialName, 100, "A", nullptr)); | 371 CreateFieldTrial(kTrialName, 100, "A", nullptr)); |
308 | 372 |
309 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, | 373 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, |
310 trial.get()); | 374 trial.get()); |
311 | 375 |
312 std::map<std::string, std::string> actualParams; | 376 EXPECT_TRUE(a.Get()); |
313 EXPECT_TRUE(GetFieldTrialParamByFeatureAsBool(kFeature, "a", false)); | 377 EXPECT_FALSE(b.Get()); |
314 EXPECT_FALSE(GetFieldTrialParamByFeatureAsBool(kFeature, "b", true)); | 378 EXPECT_FALSE(c.Get()); // invalid |
315 EXPECT_FALSE( | 379 EXPECT_TRUE(d.Get()); // invalid |
316 GetFieldTrialParamByFeatureAsBool(kFeature, "c", false)); // invalid | 380 EXPECT_TRUE(e.Get()); // empty |
317 EXPECT_TRUE( | 381 EXPECT_TRUE(f.Get()); // empty |
318 GetFieldTrialParamByFeatureAsBool(kFeature, "d", true)); // invalid | 382 } |
319 EXPECT_TRUE(GetFieldTrialParamByFeatureAsBool(kFeature, "e", true)); // empty | 383 |
320 EXPECT_TRUE(GetFieldTrialParamByFeatureAsBool(kFeature, "f", true)); // empty | 384 enum Hand { ROCK, PAPER, SCISSORS }; |
| 385 |
| 386 TEST_F(FieldTrialParamsTest, FeatureParamEnum) { |
| 387 const std::string kTrialName = "GetFieldTrialParamsByFeature"; |
| 388 |
| 389 static const FeatureParam<Hand>::Option hands[] = { |
| 390 {ROCK, "rock"}, {PAPER, "paper"}, {SCISSORS, "scissors"}}; |
| 391 static const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; |
| 392 static const FeatureParam<Hand> a{&kFeature, "a", ROCK, &hands}; |
| 393 static const FeatureParam<Hand> b{&kFeature, "b", ROCK, &hands}; |
| 394 static const FeatureParam<Hand> c{&kFeature, "c", ROCK, &hands}; |
| 395 static const FeatureParam<Hand> d{&kFeature, "d", ROCK, &hands}; |
| 396 static const FeatureParam<Hand> e{&kFeature, "e", PAPER, &hands}; |
| 397 static const FeatureParam<Hand> f{&kFeature, "f", SCISSORS, &hands}; |
| 398 |
| 399 std::map<std::string, std::string> params; |
| 400 params["a"] = "rock"; |
| 401 params["b"] = "paper"; |
| 402 params["c"] = "scissors"; |
| 403 params["d"] = "lizard"; |
| 404 params["e"] = ""; |
| 405 // "f" is not registered |
| 406 AssociateFieldTrialParams(kTrialName, "A", params); |
| 407 scoped_refptr<FieldTrial> trial( |
| 408 CreateFieldTrial(kTrialName, 100, "A", nullptr)); |
| 409 |
| 410 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 411 trial.get()); |
| 412 |
| 413 EXPECT_EQ(ROCK, a.Get()); |
| 414 EXPECT_EQ(PAPER, b.Get()); |
| 415 EXPECT_EQ(SCISSORS, c.Get()); |
| 416 EXPECT_EQ(ROCK, d.Get()); // invalid |
| 417 EXPECT_EQ(PAPER, e.Get()); // invalid/empty |
| 418 EXPECT_EQ(SCISSORS, f.Get()); // not registered |
| 419 } |
| 420 |
| 421 enum class UI { ONE_D, TWO_D, THREE_D }; |
| 422 |
| 423 TEST_F(FieldTrialParamsTest, FeatureParamEnumClass) { |
| 424 const std::string kTrialName = "GetFieldTrialParamsByFeature"; |
| 425 |
| 426 static const FeatureParam<UI>::Option uis[] = { |
| 427 {UI::ONE_D, "1d"}, {UI::TWO_D, "2d"}, {UI::THREE_D, "3d"}}; |
| 428 static const Feature kFeature{"TestFeature", FEATURE_DISABLED_BY_DEFAULT}; |
| 429 static const FeatureParam<UI> a{&kFeature, "a", UI::ONE_D, &uis}; |
| 430 static const FeatureParam<UI> b{&kFeature, "b", UI::ONE_D, &uis}; |
| 431 static const FeatureParam<UI> c{&kFeature, "c", UI::ONE_D, &uis}; |
| 432 static const FeatureParam<UI> d{&kFeature, "d", UI::ONE_D, &uis}; |
| 433 static const FeatureParam<UI> e{&kFeature, "e", UI::TWO_D, &uis}; |
| 434 static const FeatureParam<UI> f{&kFeature, "f", UI::THREE_D, &uis}; |
| 435 |
| 436 std::map<std::string, std::string> params; |
| 437 params["a"] = "1d"; |
| 438 params["b"] = "2d"; |
| 439 params["c"] = "3d"; |
| 440 params["d"] = "4d"; |
| 441 params["e"] = ""; |
| 442 // "f" is not registered |
| 443 AssociateFieldTrialParams(kTrialName, "A", params); |
| 444 scoped_refptr<FieldTrial> trial( |
| 445 CreateFieldTrial(kTrialName, 100, "A", nullptr)); |
| 446 |
| 447 CreateFeatureWithTrial(kFeature, FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 448 trial.get()); |
| 449 |
| 450 EXPECT_EQ(UI::ONE_D, a.Get()); |
| 451 EXPECT_EQ(UI::TWO_D, b.Get()); |
| 452 EXPECT_EQ(UI::THREE_D, c.Get()); |
| 453 EXPECT_EQ(UI::ONE_D, d.Get()); // invalid |
| 454 EXPECT_EQ(UI::TWO_D, e.Get()); // invalid/empty |
| 455 EXPECT_EQ(UI::THREE_D, f.Get()); // not registered |
321 } | 456 } |
322 | 457 |
323 } // namespace base | 458 } // namespace base |
OLD | NEW |