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" |
(...skipping 1125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1136 ""); | 1136 ""); |
1137 } | 1137 } |
1138 | 1138 |
1139 TEST(FieldTrialListTest, TestCopyFieldTrialStateToFlags) { | 1139 TEST(FieldTrialListTest, TestCopyFieldTrialStateToFlags) { |
1140 base::FieldTrialList field_trial_list( | 1140 base::FieldTrialList field_trial_list( |
1141 base::MakeUnique<base::MockEntropyProvider>()); | 1141 base::MakeUnique<base::MockEntropyProvider>()); |
1142 base::FieldTrialList::CreateFieldTrial("Trial1", "Group1"); | 1142 base::FieldTrialList::CreateFieldTrial("Trial1", "Group1"); |
1143 base::FilePath test_file_path = base::FilePath(FILE_PATH_LITERAL("Program")); | 1143 base::FilePath test_file_path = base::FilePath(FILE_PATH_LITERAL("Program")); |
1144 base::CommandLine cmd_line = base::CommandLine(test_file_path); | 1144 base::CommandLine cmd_line = base::CommandLine(test_file_path); |
1145 | 1145 |
1146 std::unique_ptr<base::SharedMemory> field_trial_state = | 1146 base::FieldTrialList::CopyFieldTrialStateToFlags("field-trial-handle", |
1147 base::FieldTrialList::CopyFieldTrialStateToFlags("field-trial-handle", | 1147 &cmd_line); |
1148 &cmd_line); | |
1149 | 1148 |
1150 EXPECT_TRUE(field_trial_state.get() == nullptr); | |
1151 EXPECT_TRUE(cmd_line.HasSwitch(switches::kForceFieldTrials)); | 1149 EXPECT_TRUE(cmd_line.HasSwitch(switches::kForceFieldTrials)); |
1152 } | 1150 } |
1153 | 1151 |
| 1152 TEST(FieldTrialListTest, InstantiateAllocator) { |
| 1153 FieldTrialList field_trial_list(nullptr); |
| 1154 FieldTrialList::CreateFieldTrial("Trial1", "Group1"); |
| 1155 |
| 1156 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| 1157 void* memory = field_trial_list.field_trial_allocator->shared_memory(); |
| 1158 size_t used = field_trial_list.field_trial_allocator->used(); |
| 1159 |
| 1160 // Ensure that the function is idempotent. |
| 1161 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| 1162 void* new_memory = field_trial_list.field_trial_allocator->shared_memory(); |
| 1163 size_t new_used = field_trial_list.field_trial_allocator->used(); |
| 1164 EXPECT_EQ(memory, new_memory); |
| 1165 EXPECT_EQ(used, new_used); |
| 1166 } |
| 1167 |
| 1168 TEST(FieldTrialListTest, AddTrialsToAllocator) { |
| 1169 std::string save_string; |
| 1170 base::SharedMemoryHandle handle; |
| 1171 |
| 1172 // Scoping the first FieldTrialList, as we need another one to test that it |
| 1173 // matches. |
| 1174 { |
| 1175 FieldTrialList field_trial_list(nullptr); |
| 1176 FieldTrialList::CreateFieldTrial("Trial1", "Group1"); |
| 1177 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
| 1178 FieldTrialList::AllStatesToString(&save_string); |
| 1179 handle = base::SharedMemory::DuplicateHandle( |
| 1180 field_trial_list.field_trial_allocator->shared_memory()->handle()); |
| 1181 } |
| 1182 |
| 1183 FieldTrialList field_trial_list2(nullptr); |
| 1184 std::unique_ptr<base::SharedMemory> shm(new SharedMemory(handle, true)); |
| 1185 shm.get()->Map(4 << 10); // Hardcoded, equal to kFieldTrialAllocationSize. |
| 1186 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); |
| 1187 std::string check_string; |
| 1188 FieldTrialList::AllStatesToString(&check_string); |
| 1189 EXPECT_EQ(save_string, check_string); |
| 1190 } |
| 1191 |
1154 } // namespace base | 1192 } // namespace base |
OLD | NEW |