diff options
Diffstat (limited to 'chromium/components/feature_engagement_tracker/internal/once_condition_validator_unittest.cc')
-rw-r--r-- | chromium/components/feature_engagement_tracker/internal/once_condition_validator_unittest.cc | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/chromium/components/feature_engagement_tracker/internal/once_condition_validator_unittest.cc b/chromium/components/feature_engagement_tracker/internal/once_condition_validator_unittest.cc new file mode 100644 index 00000000000..c8ee8c7b333 --- /dev/null +++ b/chromium/components/feature_engagement_tracker/internal/once_condition_validator_unittest.cc @@ -0,0 +1,163 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/feature_engagement_tracker/internal/once_condition_validator.h" + +#include <string> + +#include "base/feature_list.h" +#include "base/metrics/field_trial.h" +#include "base/test/scoped_feature_list.h" +#include "components/feature_engagement_tracker/internal/editable_configuration.h" +#include "components/feature_engagement_tracker/internal/model.h" +#include "components/feature_engagement_tracker/internal/proto/event.pb.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace feature_engagement_tracker { + +namespace { + +const base::Feature kTestFeatureFoo{"test_foo", + base::FEATURE_DISABLED_BY_DEFAULT}; +const base::Feature kTestFeatureBar{"test_bar", + base::FEATURE_DISABLED_BY_DEFAULT}; + +// A Model that is easily configurable at runtime. +class TestModel : public Model { + public: + TestModel() : ready_(false), is_showing_(false) {} + + void Initialize(const OnModelInitializationFinished& callback) override {} + + bool IsReady() const override { return ready_; } + + void SetIsReady(bool ready) { ready_ = ready; } + + const FeatureConfig& GetFeatureConfig( + const base::Feature& feature) const override { + return configuration_.GetFeatureConfig(feature); + } + + void SetIsCurrentlyShowing(bool is_showing) override { + is_showing_ = is_showing; + } + + bool IsCurrentlyShowing() const override { return is_showing_; } + + EditableConfiguration& GetConfiguration() { return configuration_; } + + const Event& GetEvent(const std::string& event_name) override { + return empty_event_; + } + + void IncrementEvent(const std::string& event_name) override {} + + private: + EditableConfiguration configuration_; + Event empty_event_; + bool ready_; + bool is_showing_; +}; + +class OnceConditionValidatorTest : public ::testing::Test { + public: + OnceConditionValidatorTest() { + // By default, model should be ready. + model_.SetIsReady(true); + } + + void AddFeature(const base::Feature& feature, bool valid) { + FeatureConfig feature_config; + feature_config.valid = valid; + model_.GetConfiguration().SetConfiguration(&feature, feature_config); + } + + protected: + base::test::ScopedFeatureList scoped_feature_list_; + TestModel model_; + OnceConditionValidator validator_; + + private: + DISALLOW_COPY_AND_ASSIGN(OnceConditionValidatorTest); +}; + +} // namespace + +TEST_F(OnceConditionValidatorTest, EnabledFeatureShouldTriggerOnce) { + scoped_feature_list_.InitWithFeatures({kTestFeatureFoo}, {}); + + // Initialize validator with one enabled and valid feature. + AddFeature(kTestFeatureFoo, true); + + // Only the first call to MeetsConditions() should lead to enlightenment. + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureFoo, model_)); + EXPECT_FALSE(validator_.MeetsConditions(kTestFeatureFoo, model_)); +} + +TEST_F(OnceConditionValidatorTest, + BothEnabledAndDisabledFeaturesShouldTrigger) { + scoped_feature_list_.InitWithFeatures({kTestFeatureFoo}, {kTestFeatureBar}); + + // Initialize validator with one enabled and one disabled feature, both valid. + AddFeature(kTestFeatureFoo, true); + AddFeature(kTestFeatureBar, true); + + // Only the kTestFeatureFoo feature should lead to enlightenment, since + // kTestFeatureBar is disabled. Ordering disabled feature first to ensure this + // captures a different behavior than the + // OnlyOneFeatureShouldTriggerPerSession test below. + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureBar, model_)); + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureFoo, model_)); +} + +TEST_F(OnceConditionValidatorTest, StillTriggerWhenAllFeaturesDisabled) { + scoped_feature_list_.InitWithFeatures({}, {kTestFeatureFoo, kTestFeatureBar}); + + // Initialize validator with two enabled features, both valid. + AddFeature(kTestFeatureFoo, true); + AddFeature(kTestFeatureBar, true); + + // No features should get to show enlightenment. + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureFoo, model_)); + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureBar, model_)); +} + +TEST_F(OnceConditionValidatorTest, OnlyTriggerWhenModelIsReady) { + scoped_feature_list_.InitWithFeatures({kTestFeatureFoo}, {}); + + // Initialize validator with a single valid feature. + AddFeature(kTestFeatureFoo, true); + + model_.SetIsReady(false); + EXPECT_FALSE(validator_.MeetsConditions(kTestFeatureFoo, model_)); + + model_.SetIsReady(true); + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureFoo, model_)); +} + +TEST_F(OnceConditionValidatorTest, OnlyTriggerIfNothingElseIsShowing) { + scoped_feature_list_.InitWithFeatures({kTestFeatureFoo}, {}); + + // Initialize validator with a single valid feature. + AddFeature(kTestFeatureFoo, true); + + model_.SetIsCurrentlyShowing(true); + EXPECT_FALSE(validator_.MeetsConditions(kTestFeatureFoo, model_)); + + model_.SetIsCurrentlyShowing(false); + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureFoo, model_)); +} + +TEST_F(OnceConditionValidatorTest, DoNotTriggerForInvalidConfig) { + scoped_feature_list_.InitWithFeatures({kTestFeatureFoo}, {}); + + AddFeature(kTestFeatureFoo, false); + EXPECT_FALSE(validator_.MeetsConditions(kTestFeatureFoo, model_)); + + // Override config to be valid. + AddFeature(kTestFeatureFoo, true); + EXPECT_TRUE(validator_.MeetsConditions(kTestFeatureFoo, model_)); +} + +} // namespace feature_engagement_tracker |