| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "base/metrics/field_trial.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/base_switches.h" | 9 #include "base/base_switches.h" |
| 10 #include "base/build_time.h" | 10 #include "base/build_time.h" |
| 11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/metrics/field_trial_param_associator.h" |
| 15 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 19 #include "base/test/gtest_util.h" | 20 #include "base/test/gtest_util.h" |
| 20 #include "base/test/mock_entropy_provider.h" | 21 #include "base/test/mock_entropy_provider.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 | 25 |
| (...skipping 1198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1223 FieldTrialList field_trial_list2(nullptr); | 1224 FieldTrialList field_trial_list2(nullptr); |
| 1224 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); | 1225 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); |
| 1225 // 4 KiB is enough to hold the trials only created for this test. | 1226 // 4 KiB is enough to hold the trials only created for this test. |
| 1226 shm.get()->Map(4 << 10); | 1227 shm.get()->Map(4 << 10); |
| 1227 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); | 1228 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); |
| 1228 std::string check_string; | 1229 std::string check_string; |
| 1229 FieldTrialList::AllStatesToString(&check_string); | 1230 FieldTrialList::AllStatesToString(&check_string); |
| 1230 ASSERT_EQ(check_string.find("Simulated"), std::string::npos); | 1231 ASSERT_EQ(check_string.find("Simulated"), std::string::npos); |
| 1231 } | 1232 } |
| 1232 | 1233 |
| 1234 TEST(FieldTrialListTest, AssociateFieldTrialParams) { |
| 1235 std::string trial_name("Trial1"); |
| 1236 std::string group_name("Group1"); |
| 1237 |
| 1238 // Create a field trial with some params. |
| 1239 FieldTrialList field_trial_list(nullptr); |
| 1240 FieldTrialList::CreateFieldTrial(trial_name, group_name); |
| 1241 std::map<std::string, std::string> params; |
| 1242 params["key1"] = "value1"; |
| 1243 params["key2"] = "value2"; |
| 1244 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( |
| 1245 trial_name, group_name, params); |
| 1246 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| 1247 |
| 1248 // Clear all cached params from the associator. |
| 1249 FieldTrialParamAssociator::GetInstance()->ClearAllCachedParamsForTesting(); |
| 1250 |
| 1251 // Check that we fetch the param from shared memory properly. |
| 1252 std::map<std::string, std::string> new_params; |
| 1253 FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name, |
| 1254 &new_params); |
| 1255 EXPECT_EQ("value1", new_params["key1"]); |
| 1256 EXPECT_EQ("value2", new_params["key2"]); |
| 1257 EXPECT_EQ(2U, new_params.size()); |
| 1258 } |
| 1259 |
| 1260 TEST(FieldTrialListTest, ClearParamsFromSharedMemory) { |
| 1261 std::string trial_name("Trial1"); |
| 1262 std::string group_name("Group1"); |
| 1263 |
| 1264 base::SharedMemoryHandle handle; |
| 1265 { |
| 1266 // Create a field trial with some params. |
| 1267 FieldTrialList field_trial_list(nullptr); |
| 1268 FieldTrial* trial = |
| 1269 FieldTrialList::CreateFieldTrial(trial_name, group_name); |
| 1270 std::map<std::string, std::string> params; |
| 1271 params["key1"] = "value1"; |
| 1272 params["key2"] = "value2"; |
| 1273 FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( |
| 1274 trial_name, group_name, params); |
| 1275 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| 1276 |
| 1277 // Clear all params from the associator AND shared memory. The allocated |
| 1278 // segments should be different. |
| 1279 FieldTrial::FieldTrialRef old_ref = trial->ref_; |
| 1280 FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); |
| 1281 FieldTrial::FieldTrialRef new_ref = trial->ref_; |
| 1282 EXPECT_NE(old_ref, new_ref); |
| 1283 |
| 1284 // Check that there are no params associated with the field trial anymore. |
| 1285 std::map<std::string, std::string> new_params; |
| 1286 FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams(trial_name, |
| 1287 &new_params); |
| 1288 EXPECT_EQ(0U, new_params.size()); |
| 1289 |
| 1290 // Now duplicate the handle so we can easily check that the trial is still |
| 1291 // in shared memory via AllStatesToString. |
| 1292 handle = base::SharedMemory::DuplicateHandle( |
| 1293 field_trial_list.field_trial_allocator_->shared_memory()->handle()); |
| 1294 } |
| 1295 |
| 1296 // Check that we have the trial. |
| 1297 FieldTrialList field_trial_list2(nullptr); |
| 1298 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); |
| 1299 // 4 KiB is enough to hold the trials only created for this test. |
| 1300 shm.get()->Map(4 << 10); |
| 1301 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); |
| 1302 std::string check_string; |
| 1303 FieldTrialList::AllStatesToString(&check_string); |
| 1304 EXPECT_EQ("*Trial1/Group1/", check_string); |
| 1305 } |
| 1306 |
| 1233 } // namespace base | 1307 } // namespace base |
| OLD | NEW |