OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/variations/variations_seed_processor.h" | 5 #include "components/variations/variations_seed_processor.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "components/variations/processed_study.h" |
11 #include "components/variations/variations_associated_data.h" | 12 #include "components/variations/variations_associated_data.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace chrome_variations { | 15 namespace chrome_variations { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 // Converts |time| to Study proto format. | 19 // Converts |time| to Study proto format. |
19 int64 TimeToProtoTime(const base::Time& time) { | 20 int64 TimeToProtoTime(const base::Time& time) { |
20 return (time - base::Time::UnixEpoch()).InSeconds(); | 21 return (time - base::Time::UnixEpoch()).InSeconds(); |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 if (!max_test_cases[j].expected_result) { | 332 if (!max_test_cases[j].expected_result) { |
332 const bool result = | 333 const bool result = |
333 seed_processor.CheckStudyVersion( | 334 seed_processor.CheckStudyVersion( |
334 filter, Version(max_test_cases[j].version)); | 335 filter, Version(max_test_cases[j].version)); |
335 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; | 336 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; |
336 } | 337 } |
337 } | 338 } |
338 } | 339 } |
339 } | 340 } |
340 | 341 |
| 342 TEST_F(VariationsSeedProcessorTest, FilterAndValidateStudies) { |
| 343 const std::string kTrial1Name = "A"; |
| 344 const std::string kGroup1Name = "Group1"; |
| 345 const std::string kTrial3Name = "B"; |
| 346 |
| 347 VariationsSeed seed; |
| 348 Study* study1 = seed.add_study(); |
| 349 study1->set_name(kTrial1Name); |
| 350 study1->set_default_experiment_name("Default"); |
| 351 AddExperiment(kGroup1Name, 100, study1); |
| 352 AddExperiment("Default", 0, study1); |
| 353 |
| 354 Study* study2 = seed.add_study(); |
| 355 *study2 = *study1; |
| 356 study2->mutable_experiment(0)->set_name("Bam"); |
| 357 ASSERT_EQ(seed.study(0).name(), seed.study(1).name()); |
| 358 |
| 359 Study* study3 = seed.add_study(); |
| 360 study3->set_name(kTrial3Name); |
| 361 study3->set_default_experiment_name("Default"); |
| 362 AddExperiment("A", 10, study3); |
| 363 AddExperiment("Default", 25, study3); |
| 364 |
| 365 std::vector<ProcessedStudy> processed_studies; |
| 366 VariationsSeedProcessor().FilterAndValidateStudies( |
| 367 seed, "en-CA", base::Time::Now(), base::Version("20.0.0.0"), |
| 368 Study_Channel_STABLE, &processed_studies); |
| 369 |
| 370 // Check that only the first kTrial1Name study was kept. |
| 371 ASSERT_EQ(2U, processed_studies.size()); |
| 372 EXPECT_EQ(kTrial1Name, processed_studies[0].study->name()); |
| 373 EXPECT_EQ(kGroup1Name, processed_studies[0].study->experiment(0).name()); |
| 374 EXPECT_EQ(kTrial3Name, processed_studies[1].study->name()); |
| 375 } |
| 376 |
341 // Test that the group for kForcingFlag1 is forced. | 377 // Test that the group for kForcingFlag1 is forced. |
342 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag1) { | 378 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag1) { |
343 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); | 379 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); |
344 | 380 |
345 base::FieldTrialList field_trial_list(NULL); | 381 base::FieldTrialList field_trial_list(NULL); |
346 | 382 |
347 Study study = CreateStudyWithFlagGroups(100, 0, 0); | 383 Study study = CreateStudyWithFlagGroups(100, 0, 0); |
348 VariationsSeedProcessor().CreateTrialFromStudy(study, false); | 384 VariationsSeedProcessor().CreateTrialFromStudy( |
| 385 ProcessedStudy(&study, 100, false)); |
349 | 386 |
350 EXPECT_EQ(kFlagGroup1Name, | 387 EXPECT_EQ(kFlagGroup1Name, |
351 base::FieldTrialList::FindFullName(kFlagStudyName)); | 388 base::FieldTrialList::FindFullName(kFlagStudyName)); |
352 } | 389 } |
353 | 390 |
354 // Test that the group for kForcingFlag2 is forced. | 391 // Test that the group for kForcingFlag2 is forced. |
355 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag2) { | 392 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag2) { |
356 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2); | 393 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2); |
357 | 394 |
358 base::FieldTrialList field_trial_list(NULL); | 395 base::FieldTrialList field_trial_list(NULL); |
359 | 396 |
360 Study study = CreateStudyWithFlagGroups(100, 0, 0); | 397 Study study = CreateStudyWithFlagGroups(100, 0, 0); |
361 VariationsSeedProcessor().CreateTrialFromStudy(study, false); | 398 VariationsSeedProcessor().CreateTrialFromStudy( |
| 399 ProcessedStudy(&study, 100, false)); |
362 | 400 |
363 EXPECT_EQ(kFlagGroup2Name, | 401 EXPECT_EQ(kFlagGroup2Name, |
364 base::FieldTrialList::FindFullName(kFlagStudyName)); | 402 base::FieldTrialList::FindFullName(kFlagStudyName)); |
365 } | 403 } |
366 | 404 |
367 TEST_F(VariationsSeedProcessorTest, ForceGroup_ChooseFirstGroupWithFlag) { | 405 TEST_F(VariationsSeedProcessorTest, ForceGroup_ChooseFirstGroupWithFlag) { |
368 // Add the flag to the command line arguments so the flag group is forced. | 406 // Add the flag to the command line arguments so the flag group is forced. |
369 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); | 407 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); |
370 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2); | 408 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag2); |
371 | 409 |
372 base::FieldTrialList field_trial_list(NULL); | 410 base::FieldTrialList field_trial_list(NULL); |
373 | 411 |
374 Study study = CreateStudyWithFlagGroups(100, 0, 0); | 412 Study study = CreateStudyWithFlagGroups(100, 0, 0); |
375 VariationsSeedProcessor().CreateTrialFromStudy(study, false); | 413 VariationsSeedProcessor().CreateTrialFromStudy( |
| 414 ProcessedStudy(&study, 100, false)); |
376 | 415 |
377 EXPECT_EQ(kFlagGroup1Name, | 416 EXPECT_EQ(kFlagGroup1Name, |
378 base::FieldTrialList::FindFullName(kFlagStudyName)); | 417 base::FieldTrialList::FindFullName(kFlagStudyName)); |
379 } | 418 } |
380 | 419 |
381 TEST_F(VariationsSeedProcessorTest, ForceGroup_DontChooseGroupWithFlag) { | 420 TEST_F(VariationsSeedProcessorTest, ForceGroup_DontChooseGroupWithFlag) { |
382 base::FieldTrialList field_trial_list(NULL); | 421 base::FieldTrialList field_trial_list(NULL); |
383 | 422 |
384 // The two flag groups are given high probability, which would normally make | 423 // The two flag groups are given high probability, which would normally make |
385 // them very likely to be chosen. They won't be chosen since flag groups are | 424 // them very likely to be chosen. They won't be chosen since flag groups are |
386 // never chosen when their flag isn't present. | 425 // never chosen when their flag isn't present. |
387 Study study = CreateStudyWithFlagGroups(1, 999, 999); | 426 Study study = CreateStudyWithFlagGroups(1, 999, 999); |
388 VariationsSeedProcessor().CreateTrialFromStudy(study, false); | 427 VariationsSeedProcessor().CreateTrialFromStudy( |
| 428 ProcessedStudy(&study, 1 + 999 + 999, false)); |
389 EXPECT_EQ(kNonFlagGroupName, | 429 EXPECT_EQ(kNonFlagGroupName, |
390 base::FieldTrialList::FindFullName(kFlagStudyName)); | 430 base::FieldTrialList::FindFullName(kFlagStudyName)); |
391 } | 431 } |
392 | 432 |
393 TEST_F(VariationsSeedProcessorTest, IsStudyExpired) { | 433 TEST_F(VariationsSeedProcessorTest, IsStudyExpired) { |
394 VariationsSeedProcessor seed_processor; | 434 VariationsSeedProcessor seed_processor; |
395 | 435 |
396 const base::Time now = base::Time::Now(); | 436 const base::Time now = base::Time::Now(); |
397 const base::TimeDelta delta = base::TimeDelta::FromHours(1); | 437 const base::TimeDelta delta = base::TimeDelta::FromHours(1); |
398 const struct { | 438 const struct { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 study.set_name("Study1"); | 579 study.set_name("Study1"); |
540 study.set_default_experiment_name("B"); | 580 study.set_default_experiment_name("B"); |
541 | 581 |
542 Study_Experiment* experiment1 = AddExperiment("A", 1, &study); | 582 Study_Experiment* experiment1 = AddExperiment("A", 1, &study); |
543 Study_Experiment_Param* param = experiment1->add_param(); | 583 Study_Experiment_Param* param = experiment1->add_param(); |
544 param->set_name("x"); | 584 param->set_name("x"); |
545 param->set_value("y"); | 585 param->set_value("y"); |
546 | 586 |
547 Study_Experiment* experiment2 = AddExperiment("B", 0, &study); | 587 Study_Experiment* experiment2 = AddExperiment("B", 0, &study); |
548 | 588 |
549 seed_processor.CreateTrialFromStudy(study, false); | 589 seed_processor.CreateTrialFromStudy(ProcessedStudy(&study, 1, false)); |
550 EXPECT_EQ("y", GetVariationParamValue("Study1", "x")); | 590 EXPECT_EQ("y", GetVariationParamValue("Study1", "x")); |
551 | 591 |
552 study.set_name("Study2"); | 592 study.set_name("Study2"); |
553 experiment1->set_probability_weight(0); | 593 experiment1->set_probability_weight(0); |
554 experiment2->set_probability_weight(1); | 594 experiment2->set_probability_weight(1); |
555 seed_processor.CreateTrialFromStudy(study, false); | 595 seed_processor.CreateTrialFromStudy(ProcessedStudy(&study, 1, false)); |
556 EXPECT_EQ(std::string(), GetVariationParamValue("Study2", "x")); | 596 EXPECT_EQ(std::string(), GetVariationParamValue("Study2", "x")); |
557 } | 597 } |
558 | 598 |
559 TEST_F(VariationsSeedProcessorTest, VariationParamsWithForcingFlag) { | 599 TEST_F(VariationsSeedProcessorTest, VariationParamsWithForcingFlag) { |
560 Study study = CreateStudyWithFlagGroups(100, 0, 0); | 600 Study study = CreateStudyWithFlagGroups(100, 0, 0); |
561 ASSERT_EQ(kForcingFlag1, study.experiment(1).forcing_flag()); | 601 ASSERT_EQ(kForcingFlag1, study.experiment(1).forcing_flag()); |
562 Study_Experiment_Param* param = study.mutable_experiment(1)->add_param(); | 602 Study_Experiment_Param* param = study.mutable_experiment(1)->add_param(); |
563 param->set_name("x"); | 603 param->set_name("x"); |
564 param->set_value("y"); | 604 param->set_value("y"); |
565 | 605 |
566 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); | 606 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); |
567 base::FieldTrialList field_trial_list(NULL); | 607 base::FieldTrialList field_trial_list(NULL); |
568 VariationsSeedProcessor().CreateTrialFromStudy(study, false); | 608 VariationsSeedProcessor().CreateTrialFromStudy( |
| 609 ProcessedStudy(&study, 100, false)); |
569 EXPECT_EQ(kFlagGroup1Name, base::FieldTrialList::FindFullName(study.name())); | 610 EXPECT_EQ(kFlagGroup1Name, base::FieldTrialList::FindFullName(study.name())); |
570 EXPECT_EQ("y", GetVariationParamValue(study.name(), "x")); | 611 EXPECT_EQ("y", GetVariationParamValue(study.name(), "x")); |
571 } | 612 } |
572 | 613 |
573 TEST_F(VariationsSeedProcessorTest, StartsActive) { | 614 TEST_F(VariationsSeedProcessorTest, StartsActive) { |
574 base::FieldTrialList field_trial_list(NULL); | 615 base::FieldTrialList field_trial_list(NULL); |
575 | 616 |
576 VariationsSeed seed; | 617 VariationsSeed seed; |
577 Study* study1 = seed.add_study(); | 618 Study* study1 = seed.add_study(); |
578 study1->set_name("A"); | 619 study1->set_name("A"); |
(...skipping 30 matching lines...) Expand all Loading... |
609 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B")); | 650 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B")); |
610 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C")); | 651 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C")); |
611 | 652 |
612 // Now, all studies should be active. | 653 // Now, all studies should be active. |
613 EXPECT_TRUE(IsFieldTrialActive("A")); | 654 EXPECT_TRUE(IsFieldTrialActive("A")); |
614 EXPECT_TRUE(IsFieldTrialActive("B")); | 655 EXPECT_TRUE(IsFieldTrialActive("B")); |
615 EXPECT_TRUE(IsFieldTrialActive("C")); | 656 EXPECT_TRUE(IsFieldTrialActive("C")); |
616 } | 657 } |
617 | 658 |
618 } // namespace chrome_variations | 659 } // namespace chrome_variations |
OLD | NEW |