OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/feature_list.h" | 5 #include "base/feature_list.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
| 16 #include "base/metrics/persistent_memory_allocator.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 const char kFeatureOnByDefaultName[] = "OnByDefault"; | 25 const char kFeatureOnByDefaultName[] = "OnByDefault"; |
25 struct Feature kFeatureOnByDefault { | 26 struct Feature kFeatureOnByDefault { |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 ClearFeatureListInstance(); | 462 ClearFeatureListInstance(); |
462 // This test case simulates the calling pattern found in code which does not | 463 // This test case simulates the calling pattern found in code which does not |
463 // explicitly initialize the features list. | 464 // explicitly initialize the features list. |
464 // All IsEnabled() calls should return the default value in this scenario. | 465 // All IsEnabled() calls should return the default value in this scenario. |
465 EXPECT_EQ(nullptr, FeatureList::GetInstance()); | 466 EXPECT_EQ(nullptr, FeatureList::GetInstance()); |
466 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOnByDefault)); | 467 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOnByDefault)); |
467 EXPECT_EQ(nullptr, FeatureList::GetInstance()); | 468 EXPECT_EQ(nullptr, FeatureList::GetInstance()); |
468 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); | 469 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
469 } | 470 } |
470 | 471 |
| 472 TEST_F(FeatureListTest, StoreAndRetrieveFeaturesFromSharedMemory) { |
| 473 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 474 |
| 475 // Create some overrides. |
| 476 feature_list->RegisterOverride(kFeatureOffByDefaultName, |
| 477 FeatureList::OVERRIDE_ENABLE_FEATURE, nullptr); |
| 478 feature_list->RegisterOverride( |
| 479 kFeatureOnByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, nullptr); |
| 480 feature_list->FinalizeInitialization(); |
| 481 |
| 482 // Create an allocator and store the overrides. |
| 483 std::unique_ptr<SharedMemory> shm(new SharedMemory()); |
| 484 shm->CreateAndMapAnonymous(4 << 10); |
| 485 SharedPersistentMemoryAllocator allocator(std::move(shm), 1, "", false); |
| 486 feature_list->AddFeaturesToAllocator(&allocator); |
| 487 |
| 488 std::unique_ptr<base::FeatureList> feature_list2(new base::FeatureList); |
| 489 |
| 490 // Check that the new feature list is empty. |
| 491 EXPECT_FALSE(feature_list2->IsFeatureOverriddenFromCommandLine( |
| 492 kFeatureOffByDefaultName, FeatureList::OVERRIDE_ENABLE_FEATURE)); |
| 493 EXPECT_FALSE(feature_list2->IsFeatureOverriddenFromCommandLine( |
| 494 kFeatureOnByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE)); |
| 495 |
| 496 feature_list2->InitializeFromSharedMemory(&allocator); |
| 497 // Check that the new feature list now has 2 overrides. |
| 498 EXPECT_TRUE(feature_list2->IsFeatureOverriddenFromCommandLine( |
| 499 kFeatureOffByDefaultName, FeatureList::OVERRIDE_ENABLE_FEATURE)); |
| 500 EXPECT_TRUE(feature_list2->IsFeatureOverriddenFromCommandLine( |
| 501 kFeatureOnByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE)); |
| 502 } |
| 503 |
| 504 TEST_F(FeatureListTest, StoreAndRetrieveAssociatedFeaturesFromSharedMemory) { |
| 505 FieldTrialList field_trial_list(nullptr); |
| 506 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 507 |
| 508 // Create some overrides. |
| 509 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("TrialExample1", "A"); |
| 510 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("TrialExample2", "B"); |
| 511 feature_list->RegisterFieldTrialOverride( |
| 512 kFeatureOnByDefaultName, FeatureList::OVERRIDE_USE_DEFAULT, trial1); |
| 513 feature_list->RegisterFieldTrialOverride( |
| 514 kFeatureOffByDefaultName, FeatureList::OVERRIDE_USE_DEFAULT, trial2); |
| 515 feature_list->FinalizeInitialization(); |
| 516 |
| 517 // Create an allocator and store the overrides. |
| 518 std::unique_ptr<SharedMemory> shm(new SharedMemory()); |
| 519 shm->CreateAndMapAnonymous(4 << 10); |
| 520 SharedPersistentMemoryAllocator allocator(std::move(shm), 1, "", false); |
| 521 feature_list->AddFeaturesToAllocator(&allocator); |
| 522 |
| 523 std::unique_ptr<base::FeatureList> feature_list2(new base::FeatureList); |
| 524 feature_list2->InitializeFromSharedMemory(&allocator); |
| 525 feature_list2->FinalizeInitialization(); |
| 526 |
| 527 // Check that the field trials are still associated. |
| 528 FieldTrial* associated_trial1 = |
| 529 feature_list2->GetAssociatedFieldTrial(kFeatureOnByDefault); |
| 530 FieldTrial* associated_trial2 = |
| 531 feature_list2->GetAssociatedFieldTrial(kFeatureOffByDefault); |
| 532 EXPECT_EQ(associated_trial1, trial1); |
| 533 EXPECT_EQ(associated_trial2, trial2); |
| 534 } |
| 535 |
471 } // namespace base | 536 } // namespace base |
OLD | NEW |