Chromium Code Reviews| Index: base/metrics/field_trial_unittest.cc |
| diff --git a/base/metrics/field_trial_unittest.cc b/base/metrics/field_trial_unittest.cc |
| index 890b3e5824e3fb39a586b247377a724b992b8de9..a5bad5b87a6037d512eb6597e709eb22408b60f7 100644 |
| --- a/base/metrics/field_trial_unittest.cc |
| +++ b/base/metrics/field_trial_unittest.cc |
| @@ -12,6 +12,7 @@ |
| #include "base/macros.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/message_loop/message_loop.h" |
| +#include "base/metrics/field_trial_param_associator.h" |
| #include "base/rand_util.h" |
| #include "base/run_loop.h" |
| #include "base/strings/string_number_conversions.h" |
| @@ -1230,4 +1231,77 @@ TEST(FieldTrialListTest, DoNotAddSimulatedFieldTrialsToAllocator) { |
| ASSERT_EQ(check_string.find("Simulated"), std::string::npos); |
| } |
| +TEST(FieldTrialListTest, AssociateFieldTrialParams) { |
| + std::string trial_name("Trial1"); |
| + std::string group_name("Group1"); |
| + |
| + // Create a field trial with some params. |
| + FieldTrialList field_trial_list(nullptr); |
| + FieldTrialList::CreateFieldTrial(trial_name, group_name); |
| + std::map<std::string, std::string> params; |
| + params["key1"] = "value1"; |
| + params["key2"] = "value2"; |
| + FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( |
| + trial_name, group_name, params); |
| + FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| + |
| + // Clear all cached params from the associator. |
| + FieldTrialParamAssociator::GetInstance()->ClearAllCachedParamsForTesting(); |
| + |
| + // Check that we fetch the param from shared memory properly. |
| + std::map<std::string, std::string> new_params; |
| + FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name, |
| + &new_params); |
| + EXPECT_EQ("value1", new_params["key1"]); |
| + EXPECT_EQ("value2", new_params["key2"]); |
| + EXPECT_EQ(2U, new_params.size()); |
| +} |
| + |
| +TEST(FieldTrialListTest, ClearParamsFromSharedMemory) { |
| + std::string trial_name("Trial1"); |
| + std::string group_name("Group1"); |
| + |
| + base::SharedMemoryHandle handle; |
| + { |
| + // Create a field trial with some params. |
| + FieldTrialList field_trial_list(nullptr); |
| + FieldTrial* trial = |
| + FieldTrialList::CreateFieldTrial(trial_name, group_name); |
| + std::map<std::string, std::string> params; |
| + params["key1"] = "value1"; |
| + params["key2"] = "value2"; |
| + FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( |
| + trial_name, group_name, params); |
| + FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| + |
| + // Clear all params from the associator AND shared memory. The allocated |
| + // segments should be different. |
| + FieldTrial::FieldTrialRef old_ref = trial->ref_; |
| + FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); |
| + FieldTrial::FieldTrialRef new_ref = trial->ref_; |
| + EXPECT_NE(old_ref, new_ref); |
| + |
| + // Check that there are no params associated with the field trial anymore. |
| + std::map<std::string, std::string> new_params; |
| + FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name, |
| + &new_params); |
| + EXPECT_EQ(0U, new_params.size()); |
| + |
| + // Now duplicate the handle so we can easily check that the trial is still |
| + // in shared memory via AllStatesToString. |
| + handle = base::SharedMemory::DuplicateHandle( |
| + field_trial_list.field_trial_allocator_->shared_memory()->handle()); |
| + } |
| + |
| + // Check that we have the trial. |
| + FieldTrialList field_trial_list2(nullptr); |
| + std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); |
| + // 4 KiB is enough to hold the trials only created for this test. |
| + shm.get()->Map(4 << 10); |
| + FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); |
| + std::string check_string; |
| + FieldTrialList::AllStatesToString(&check_string); |
| + EXPECT_EQ("*Trial1/Group1/", check_string); |
|
bcwhite
2016/11/28 21:57:41
Don't you need to check that params come through h
lawrencewu
2016/11/28 22:51:26
We cleared the params, so we're verifying that the
bcwhite
2016/11/30 15:05:33
Where do you check that params come through after
lawrencewu
2016/11/30 15:08:12
That's in the test right above this one, Associate
|
| +} |
| + |
| } // namespace base |