1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/feature_engagement/internal/once_condition_validator.h"
#include <string>
#include "base/feature_list.h"
#include "components/feature_engagement/internal/editable_configuration.h"
#include "components/feature_engagement/internal/event_model.h"
#include "components/feature_engagement/internal/never_availability_model.h"
#include "components/feature_engagement/internal/noop_display_lock_controller.h"
#include "components/feature_engagement/internal/proto/feature_event.pb.h"
#include "components/feature_engagement/internal/test/test_time_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace feature_engagement {
namespace {
BASE_FEATURE(kOnceTestFeatureFoo,
"test_foo",
base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kOnceTestFeatureBar,
"test_bar",
base::FEATURE_DISABLED_BY_DEFAULT);
FeatureConfig kValidFeatureConfig;
FeatureConfig kInvalidFeatureConfig;
// An EventModel that is easily configurable at runtime.
class OnceTestEventModel : public EventModel {
public:
OnceTestEventModel() : ready_(false) { kValidFeatureConfig.valid = true; }
void Initialize(OnModelInitializationFinished callback,
uint32_t current_day) override {}
bool IsReady() const override { return ready_; }
void SetIsReady(bool ready) { ready_ = ready; }
const Event* GetEvent(const std::string& event_name) const override {
return nullptr;
}
uint32_t GetEventCount(const std::string& event_name,
uint32_t current_day,
uint32_t window_size) const override {
return 0;
}
void IncrementEvent(const std::string& event_name, uint32_t day) override {}
void ClearEvent(const std::string& event_name) override {}
void IncrementSnooze(const std::string& event_name,
uint32_t day,
base::Time time) override {}
void DismissSnooze(const std::string& event_name) override {}
base::Time GetLastSnoozeTimestamp(
const std::string& event_name) const override {
return base::Time();
}
uint32_t GetSnoozeCount(const std::string& event_name,
uint32_t window,
uint32_t current_day) const override {
return 0;
}
bool IsSnoozeDismissed(const std::string& event_name) const override {
return false;
}
private:
bool ready_;
};
class OnceConditionValidatorTest : public ::testing::Test {
public:
OnceConditionValidatorTest() {
// By default, event model should be ready.
event_model_.SetIsReady(true);
}
OnceConditionValidatorTest(const OnceConditionValidatorTest&) = delete;
OnceConditionValidatorTest& operator=(const OnceConditionValidatorTest&) =
delete;
protected:
EditableConfiguration configuration_;
OnceTestEventModel event_model_;
NeverAvailabilityModel availability_model_;
NoopDisplayLockController display_lock_controller_;
OnceConditionValidator validator_;
TestTimeProvider time_provider_;
};
} // namespace
TEST_F(OnceConditionValidatorTest, EnabledFeatureShouldTriggerOnce) {
// Only the first call to MeetsConditions() should lead to enlightenment.
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
validator_.NotifyIsShowing(kOnceTestFeatureFoo, FeatureConfig(), {""});
ConditionValidator::Result result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_FALSE(result.NoErrors());
EXPECT_FALSE(result.session_rate_ok);
EXPECT_FALSE(result.trigger_ok);
}
TEST_F(OnceConditionValidatorTest,
BothEnabledAndDisabledFeaturesShouldTrigger) {
// Only the kOnceTestFeatureFoo feature should lead to enlightenment, since
// kOnceTestFeatureBar is disabled. Ordering disabled feature first to ensure
// this captures a different behavior than the
// OnlyOneFeatureShouldTriggerPerSession test below.
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureBar, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
}
TEST_F(OnceConditionValidatorTest, StillTriggerWhenAllFeaturesDisabled) {
// No features should get to show enlightenment.
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureBar, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
}
TEST_F(OnceConditionValidatorTest, OnlyTriggerWhenModelIsReady) {
event_model_.SetIsReady(false);
ConditionValidator::Result result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_FALSE(result.NoErrors());
EXPECT_FALSE(result.event_model_ready_ok);
event_model_.SetIsReady(true);
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
}
TEST_F(OnceConditionValidatorTest, OnlyTriggerIfNothingElseIsShowing) {
validator_.NotifyIsShowing(kOnceTestFeatureBar, FeatureConfig(), {""});
ConditionValidator::Result result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_FALSE(result.NoErrors());
EXPECT_FALSE(result.currently_showing_ok);
validator_.NotifyDismissed(kOnceTestFeatureBar);
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
}
TEST_F(OnceConditionValidatorTest,
AlsoTriggerWhenSomethingElseIsShowingForTesting) {
validator_.AllowMultipleFeaturesForTesting(true);
validator_.NotifyIsShowing(kOnceTestFeatureBar, FeatureConfig(), {""});
ConditionValidator::Result result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_TRUE(result.NoErrors());
validator_.NotifyDismissed(kOnceTestFeatureBar);
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
}
TEST_F(OnceConditionValidatorTest, PriorityNotificationBlocksOtherIPHs) {
validator_.SetPriorityNotification("test_bar");
ConditionValidator::Result result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_FALSE(result.NoErrors());
EXPECT_FALSE(result.priority_notification_ok);
validator_.SetPriorityNotification(std::nullopt);
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
}
TEST_F(OnceConditionValidatorTest, DoNotTriggerForInvalidConfig) {
ConditionValidator::Result result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kInvalidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_FALSE(result.NoErrors());
EXPECT_FALSE(result.config_ok);
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
}
TEST_F(OnceConditionValidatorTest,
EnabledFeatureShouldTriggerAgainAfterSessionReset) {
// Only the first call to MeetsConditions() should lead to enlightenment.
EXPECT_TRUE(validator_
.MeetsConditions(kOnceTestFeatureFoo, kValidFeatureConfig, {},
event_model_, availability_model_,
display_lock_controller_, nullptr,
time_provider_)
.NoErrors());
validator_.NotifyIsShowing(kOnceTestFeatureFoo, FeatureConfig(), {""});
validator_.NotifyDismissed(kOnceTestFeatureFoo);
// Do not show before session reset.
ConditionValidator::Result result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_FALSE(result.NoErrors());
EXPECT_FALSE(result.session_rate_ok);
EXPECT_FALSE(result.trigger_ok);
// Reset the session.
validator_.ResetSession();
// Can show again after session reset.
result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_TRUE(result.NoErrors());
EXPECT_TRUE(result.session_rate_ok);
EXPECT_TRUE(result.trigger_ok);
validator_.NotifyIsShowing(kOnceTestFeatureFoo, FeatureConfig(), {""});
validator_.NotifyDismissed(kOnceTestFeatureFoo);
// The second call should fail.
result = validator_.MeetsConditions(
kOnceTestFeatureFoo, kValidFeatureConfig, {}, event_model_,
availability_model_, display_lock_controller_, nullptr, time_provider_);
EXPECT_FALSE(result.NoErrors());
EXPECT_FALSE(result.session_rate_ok);
EXPECT_FALSE(result.trigger_ok);
}
} // namespace feature_engagement
|