summaryrefslogtreecommitdiff
path: root/chromium/components/feature_engagement/internal/feature_config_condition_validator_unittest.cc
blob: 84d5333c94b96e9d65bc1628495ae220f18d5add (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
// 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/internal/feature_config_condition_validator.h"

#include <map>
#include <memory>
#include <string>

#include "base/feature_list.h"
#include "base/metrics/field_trial.h"
#include "base/test/scoped_feature_list.h"
#include "components/feature_engagement/internal/availability_model.h"
#include "components/feature_engagement/internal/event_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/event_util.h"
#include "components/feature_engagement/public/configuration.h"
#include "components/feature_engagement/public/tracker.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace feature_engagement {

namespace {

const base::Feature kFeatureConfigTestFeatureFoo{
    "test_foo", base::FEATURE_DISABLED_BY_DEFAULT};
const base::Feature kFeatureConfigTestFeatureBar{
    "test_bar", base::FEATURE_DISABLED_BY_DEFAULT};
const base::Feature kFeatureConfigTestFeatureQux{
    "test_qux", base::FEATURE_DISABLED_BY_DEFAULT};
const base::Feature kFeatureConfigTestFeatureXyz{
    "test_xyz", base::FEATURE_DISABLED_BY_DEFAULT};

FeatureConfig GetValidFeatureConfig() {
  FeatureConfig config;
  config.valid = true;
  return config;
}

FeatureConfig GetAcceptingFeatureConfig() {
  FeatureConfig config;
  config.valid = true;
  config.used = EventConfig("used", Comparator(ANY, 0), 0, 0);
  config.trigger = EventConfig("trigger", Comparator(ANY, 0), 0, 0);
  config.session_rate = Comparator(ANY, 0);
  config.availability = Comparator(ANY, 0);
  return config;
}

SessionRateImpact CreateSessionRateImpactTypeExplicit(
    std::vector<std::string> affected_features) {
  SessionRateImpact impact;
  impact.type = SessionRateImpact::Type::EXPLICIT;
  impact.affected_features = affected_features;
  return impact;
}

class TestEventModel : public EventModel {
 public:
  TestEventModel() = default;

  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 {
    auto search = events_.find(event_name);
    if (search == events_.end())
      return nullptr;

    return &search->second;
  }

  uint32_t GetEventCount(const std::string& event_name,
                         uint32_t current_day,
                         uint32_t window_size) const override {
    // A same implementation for EventModelImpl.
    const Event* event = GetEvent(event_name);
    if (event == nullptr || window_size == 0u)
      return 0;

    DCHECK(window_size >= 0);

    uint32_t oldest_accepted_day = current_day - window_size + 1;
    if (window_size > current_day)
      oldest_accepted_day = 0u;

    // Calculate the number of events within the window.
    uint32_t event_count = 0;
    for (const auto& event_day : event->events()) {
      if (event_day.day() < oldest_accepted_day)
        continue;

      event_count += event_day.count();
    }

    return event_count;
  }

  void SetEvent(const Event& event) { events_[event.name()] = event; }

  void IncrementEvent(const std::string& event_name, uint32_t day) override {}

  void IncrementSnooze(const std::string& event_name,
                       uint32_t day,
                       base::Time time) override {
    last_snooze_time_us_ = time;
  }

  void DismissSnooze(const std::string& event_name) override {
    snooze_dismissed_ = true;
  }

  base::Time GetLastSnoozeTimestamp(
      const std::string& event_name) const override {
    return last_snooze_time_us_;
  }

  uint32_t GetSnoozeCount(const std::string& event_name,
                          uint32_t window,
                          uint32_t current_day) const override {
    const Event* event = GetEvent(event_name);
    if (!event || window == 0u)
      return 0;

    DCHECK(window >= 0);

    uint32_t oldest_accepted_day = current_day - window + 1;
    if (window > current_day)
      oldest_accepted_day = 0u;

    // Calculate the number of snooze within the window.
    uint32_t count = 0;
    for (const auto& event_day : event->events()) {
      if (event_day.day() < oldest_accepted_day ||
          event_day.day() > current_day)
        continue;

      count += event_day.snooze_count();
    }

    return count;
  }

  bool IsSnoozeDismissed(const std::string& event_name) const override {
    return snooze_dismissed_;
  }

 private:
  std::map<std::string, Event> events_;
  base::Time last_snooze_time_us_;
  bool ready_ = true;
  bool snooze_dismissed_ = false;
};

class TestAvailabilityModel : public AvailabilityModel {
 public:
  TestAvailabilityModel() : ready_(true) {}

  TestAvailabilityModel(const TestAvailabilityModel&) = delete;
  TestAvailabilityModel& operator=(const TestAvailabilityModel&) = delete;

  ~TestAvailabilityModel() override = default;

  void Initialize(AvailabilityModel::OnInitializedCallback callback,
                  uint32_t current_day) override {}

  bool IsReady() const override { return ready_; }

  void SetIsReady(bool ready) { ready_ = ready; }

  absl::optional<uint32_t> GetAvailability(
      const base::Feature& feature) const override {
    auto search = availabilities_.find(feature.name);
    if (search == availabilities_.end())
      return absl::nullopt;

    return search->second;
  }

  void SetAvailability(const base::Feature* feature,
                       absl::optional<uint32_t> availability) {
    availabilities_[feature->name] = availability;
  }

 private:
  bool ready_;

  std::map<std::string, absl::optional<uint32_t>> availabilities_;
};

class TestDisplayLockController : public DisplayLockController {
 public:
  TestDisplayLockController() = default;

  TestDisplayLockController(const TestDisplayLockController&) = delete;
  TestDisplayLockController& operator=(const TestDisplayLockController&) =
      delete;

  ~TestDisplayLockController() override = default;

  std::unique_ptr<DisplayLockHandle> AcquireDisplayLock() override {
    return nullptr;
  }

  bool IsDisplayLocked() const override { return next_display_locked_result_; }

  void SetNextIsDisplayLockedResult(bool result) {
    next_display_locked_result_ = result;
  }

 private:
  // The next result to return from IsDisplayLocked().
  bool next_display_locked_result_ = false;
};

class FeatureConfigConditionValidatorTest : public ::testing::Test {
 public:
  FeatureConfigConditionValidatorTest() = default;

  FeatureConfigConditionValidatorTest(
      const FeatureConfigConditionValidatorTest&) = delete;
  FeatureConfigConditionValidatorTest& operator=(
      const FeatureConfigConditionValidatorTest&) = delete;

 protected:
  ConditionValidator::Result GetResultForDayAndEventWindow(
      Comparator comparator,
      uint32_t window,
      uint32_t current_day) {
    FeatureConfig config = GetAcceptingFeatureConfig();
    config.event_configs.insert(EventConfig("event1", comparator, window, 0));
    return validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, current_day);
  }

  ConditionValidator::Result GetResultForDay(const FeatureConfig& config,
                                             uint32_t current_day) {
    return validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, current_day);
  }

  ConditionValidator::Result GetResultForDayZero(const FeatureConfig& config) {
    return validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, 0);
  }

  ConditionValidator::Result GetResultForDayZeroForFeature(
      const base::Feature& feature,
      const FeatureConfig& config) {
    return validator_.MeetsConditions(feature, config, event_model_,
                                      availability_model_,
                                      display_lock_controller_, 0);
  }

  TestEventModel event_model_;
  TestAvailabilityModel availability_model_;
  TestDisplayLockController display_lock_controller_;
  FeatureConfigConditionValidator validator_;
  uint32_t current_day_;
};

}  // namespace

TEST_F(FeatureConfigConditionValidatorTest, ModelNotReadyShouldFail) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  event_model_.SetIsReady(false);

  ConditionValidator::Result result =
      GetResultForDayZero(GetValidFeatureConfig());
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.event_model_ready_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, ConfigInvalidShouldFail) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  ConditionValidator::Result result = GetResultForDayZero(FeatureConfig());
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.config_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, MultipleErrorsShouldBeSet) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  event_model_.SetIsReady(false);

  ConditionValidator::Result result = GetResultForDayZero(FeatureConfig());
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.event_model_ready_ok);
  EXPECT_FALSE(result.config_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, ReadyModelEmptyConfig) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  EXPECT_TRUE(GetResultForDayZero(GetValidFeatureConfig()).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, ReadyModelAcceptingConfig) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  EXPECT_TRUE(GetResultForDayZero(GetAcceptingFeatureConfig()).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, CurrentlyShowing) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      {kFeatureConfigTestFeatureFoo, kFeatureConfigTestFeatureBar}, {});

  validator_.NotifyIsShowing(
      kFeatureConfigTestFeatureBar, FeatureConfig(),
      {kFeatureConfigTestFeatureFoo.name, kFeatureConfigTestFeatureBar.name});
  ConditionValidator::Result result =
      GetResultForDayZero(GetAcceptingFeatureConfig());
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.currently_showing_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, Used) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  config.used = EventConfig("used", Comparator(LESS_THAN, 0), 0, 0);

  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.used_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, Trigger) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  config.trigger = EventConfig("trigger", Comparator(LESS_THAN, 0), 0, 0);

  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.trigger_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, SingleOKPrecondition) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  config.event_configs.insert(EventConfig("event1", Comparator(ANY, 0), 0, 0));

  EXPECT_TRUE(GetResultForDayZero(config).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, MultipleOKPreconditions) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  config.event_configs.insert(EventConfig("event1", Comparator(ANY, 0), 0, 0));
  config.event_configs.insert(EventConfig("event2", Comparator(ANY, 0), 0, 0));

  EXPECT_TRUE(GetResultForDayZero(config).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, OneOKThenOneFailingPrecondition) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  config.event_configs.insert(EventConfig("event1", Comparator(ANY, 0), 0, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(LESS_THAN, 0), 0, 0));

  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.preconditions_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, OneFailingThenOneOKPrecondition) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  config.event_configs.insert(EventConfig("event1", Comparator(ANY, 0), 0, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(LESS_THAN, 0), 0, 0));

  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.preconditions_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, TwoFailingPreconditions) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(LESS_THAN, 0), 0, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(LESS_THAN, 0), 0, 0));

  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.preconditions_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, SessionRate) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      {kFeatureConfigTestFeatureFoo, kFeatureConfigTestFeatureBar}, {});
  std::vector<std::string> all_feature_names = {
      kFeatureConfigTestFeatureFoo.name, kFeatureConfigTestFeatureBar.name};

  FeatureConfig foo_config = GetAcceptingFeatureConfig();
  foo_config.session_rate = Comparator(LESS_THAN, 2u);
  FeatureConfig bar_config = GetAcceptingFeatureConfig();

  EXPECT_TRUE(GetResultForDayZero(foo_config).NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureBar, bar_config,
                             all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureBar);
  EXPECT_TRUE(GetResultForDayZero(foo_config).NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureBar, bar_config,
                             all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureBar);
  ConditionValidator::Result result = GetResultForDayZero(foo_config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.session_rate_ok);

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureBar, bar_config,
                             all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureBar);
  result = GetResultForDayZero(foo_config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.session_rate_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, SessionRateImpactAffectsNone) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      {kFeatureConfigTestFeatureFoo, kFeatureConfigTestFeatureBar}, {});
  std::vector<std::string> all_feature_names = {
      kFeatureConfigTestFeatureFoo.name, kFeatureConfigTestFeatureBar.name};

  FeatureConfig foo_config = GetAcceptingFeatureConfig();
  foo_config.session_rate = Comparator(LESS_THAN, 2u);
  FeatureConfig affects_none_config = GetAcceptingFeatureConfig();
  affects_none_config.session_rate_impact = SessionRateImpact();
  affects_none_config.session_rate_impact.type = SessionRateImpact::Type::NONE;

  EXPECT_TRUE(GetResultForDayZero(foo_config).NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureBar, affects_none_config,
                             all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureBar);
  EXPECT_TRUE(GetResultForDayZero(foo_config).NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureBar, affects_none_config,
                             all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureBar);
  EXPECT_TRUE(GetResultForDayZero(foo_config).NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureBar, affects_none_config,
                             all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureBar);
  EXPECT_TRUE(GetResultForDayZero(foo_config).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, SessionRateImpactAffectsExplicit) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      {kFeatureConfigTestFeatureFoo, kFeatureConfigTestFeatureBar,
       kFeatureConfigTestFeatureQux},
      {});
  std::vector<std::string> all_feature_names = {
      kFeatureConfigTestFeatureFoo.name, kFeatureConfigTestFeatureBar.name,
      kFeatureConfigTestFeatureQux.name};

  FeatureConfig foo_config = GetAcceptingFeatureConfig();
  foo_config.session_rate = Comparator(LESS_THAN, 2u);
  FeatureConfig bar_config = GetAcceptingFeatureConfig();
  bar_config.session_rate = Comparator(LESS_THAN, 2u);

  FeatureConfig affects_only_foo_config = GetAcceptingFeatureConfig();
  affects_only_foo_config.session_rate_impact =
      CreateSessionRateImpactTypeExplicit({kFeatureConfigTestFeatureFoo.name});

  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureQux,
                             affects_only_foo_config, all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureQux);
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureQux,
                             affects_only_foo_config, all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureQux);
  ConditionValidator::Result result =
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.session_rate_ok);
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, SessionRateImpactAffectsSelf) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      {kFeatureConfigTestFeatureFoo, kFeatureConfigTestFeatureBar,
       kFeatureConfigTestFeatureQux},
      {});
  std::vector<std::string> all_feature_names = {
      kFeatureConfigTestFeatureFoo.name, kFeatureConfigTestFeatureBar.name};

  FeatureConfig foo_config = GetAcceptingFeatureConfig();
  foo_config.session_rate = Comparator(LESS_THAN, 2u);
  FeatureConfig bar_config = GetAcceptingFeatureConfig();
  bar_config.session_rate = Comparator(LESS_THAN, 2u);

  FeatureConfig affects_only_foo_config = GetAcceptingFeatureConfig();
  affects_only_foo_config.session_rate_impact =
      CreateSessionRateImpactTypeExplicit({kFeatureConfigTestFeatureFoo.name});

  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureFoo,
                             affects_only_foo_config, all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureFoo);
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureFoo,
                             affects_only_foo_config, all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureFoo);
  ConditionValidator::Result result =
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.session_rate_ok);
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest,
       SessionRateImpactAffectsExplicitMultipleFeatures) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      {kFeatureConfigTestFeatureFoo, kFeatureConfigTestFeatureBar,
       kFeatureConfigTestFeatureQux, kFeatureConfigTestFeatureXyz},
      {});
  std::vector<std::string> all_feature_names = {
      kFeatureConfigTestFeatureFoo.name, kFeatureConfigTestFeatureBar.name,
      kFeatureConfigTestFeatureQux.name, kFeatureConfigTestFeatureXyz.name};

  FeatureConfig foo_config = GetAcceptingFeatureConfig();
  foo_config.session_rate = Comparator(LESS_THAN, 2u);
  FeatureConfig bar_config = GetAcceptingFeatureConfig();
  bar_config.session_rate = Comparator(LESS_THAN, 2u);
  FeatureConfig xyz_config = GetAcceptingFeatureConfig();
  xyz_config.session_rate = Comparator(LESS_THAN, 2u);

  FeatureConfig affects_foo_and_bar_config = GetAcceptingFeatureConfig();
  affects_foo_and_bar_config.session_rate_impact =
      CreateSessionRateImpactTypeExplicit({kFeatureConfigTestFeatureFoo.name,
                                           kFeatureConfigTestFeatureBar.name});

  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureXyz, xyz_config)
          .NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureQux,
                             affects_foo_and_bar_config, all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureQux);
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureBar, bar_config)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureXyz, xyz_config)
          .NoErrors());

  validator_.NotifyIsShowing(kFeatureConfigTestFeatureQux,
                             affects_foo_and_bar_config, all_feature_names);
  validator_.NotifyDismissed(kFeatureConfigTestFeatureQux);
  ConditionValidator::Result foo_result =
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, foo_config);
  EXPECT_FALSE(foo_result.NoErrors());
  EXPECT_FALSE(foo_result.session_rate_ok);
  ConditionValidator::Result bar_result =
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureFoo, bar_config);
  EXPECT_FALSE(bar_result.NoErrors());
  EXPECT_FALSE(bar_result.session_rate_ok);
  EXPECT_TRUE(
      GetResultForDayZeroForFeature(kFeatureConfigTestFeatureXyz, xyz_config)
          .NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, Availability) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      {kFeatureConfigTestFeatureFoo, kFeatureConfigTestFeatureBar}, {});

  FeatureConfig config = GetAcceptingFeatureConfig();
  EXPECT_TRUE(GetResultForDayZero(config).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 100u).NoErrors());

  // When the AvailabilityModel is not ready, it should fail.
  availability_model_.SetIsReady(false);
  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.availability_model_ready_ok);
  result = GetResultForDay(config, 100u);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.availability_model_ready_ok);

  // Reset state back to ready.
  availability_model_.SetIsReady(true);

  // For a feature that became available on day 2 that has to have been
  // available for at least 1 day, it should start being accepted on day 3.
  availability_model_.SetAvailability(&kFeatureConfigTestFeatureFoo, 2u);
  config.availability = Comparator(GREATER_THAN_OR_EQUAL, 1u);
  result = GetResultForDay(config, 1u);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.availability_ok);
  result = GetResultForDay(config, 2u);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.availability_ok);
  EXPECT_TRUE(GetResultForDay(config, 3u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 4u).NoErrors());

  // For a feature that became available on day 10 that has to have been
  // available for at least 3 days, it should start being accepted on day 13.
  availability_model_.SetAvailability(&kFeatureConfigTestFeatureFoo, 10u);
  config.availability = Comparator(GREATER_THAN_OR_EQUAL, 3u);
  result = GetResultForDay(config, 11u);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.availability_ok);
  result = GetResultForDay(config, 12u);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.availability_ok);
  EXPECT_TRUE(GetResultForDay(config, 13u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 14u).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, SnoozeExpiration) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});
  FeatureConfig config = GetAcceptingFeatureConfig();
  base::Time baseline = base::Time::Now();

  // Set up snooze params.
  SnoozeParams snooze_params;
  snooze_params.max_limit = 5;
  snooze_params.snooze_interval = 3;
  config.snooze_params = snooze_params;
  config.trigger.window = 5;

  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_TRUE(result.NoErrors());
  EXPECT_TRUE(result.snooze_expiration_ok);
  EXPECT_TRUE(result.should_show_snooze);

  // Adding snooze count for |event|.
  Event event;
  event.set_name(config.trigger.name);
  test::SetSnoozeCountForDay(&event, 1u, 1u);
  test::SetSnoozeCountForDay(&event, 3u, 2u);
  test::SetSnoozeCountForDay(&event, 5u, 2u);
  event_model_.SetEvent(event);

  // Updating last snooze timestamp.
  event_model_.IncrementSnooze(config.trigger.name, 1u,
                               baseline - base::Days(4));

  // Verify that snooze conditions are met at day 3.
  result = GetResultForDay(config, 3u);
  EXPECT_TRUE(result.NoErrors());
  EXPECT_TRUE(result.snooze_expiration_ok);
  EXPECT_TRUE(result.should_show_snooze);

  // When last snooze timestamp is too recent.
  event_model_.IncrementSnooze(config.trigger.name, 1u,
                               baseline - base::Days(2));
  result = GetResultForDay(config, 3u);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.snooze_expiration_ok);
  EXPECT_FALSE(result.should_show_snooze);

  // Reset the last snooze timestamp.
  event_model_.IncrementSnooze(config.trigger.name, 1u,
                               baseline - base::Days(4));
  result = GetResultForDay(config, 3u);
  EXPECT_TRUE(result.NoErrors());
  EXPECT_TRUE(result.snooze_expiration_ok);
  EXPECT_TRUE(result.should_show_snooze);

  // When snooze is dismissed.
  event_model_.DismissSnooze(config.trigger.name);
  result = GetResultForDay(config, 3u);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.snooze_expiration_ok);
  EXPECT_FALSE(result.should_show_snooze);
}

TEST_F(FeatureConfigConditionValidatorTest, ShouldShowSnooze) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});
  FeatureConfig config = GetAcceptingFeatureConfig();

  // Set up snooze params.
  SnoozeParams snooze_params;
  snooze_params.max_limit = 5;
  snooze_params.snooze_interval = 3;
  config.snooze_params = snooze_params;
  config.trigger.window = 5;

  ConditionValidator::Result result = GetResultForDayZero(config);
  EXPECT_TRUE(result.NoErrors());
  EXPECT_TRUE(result.should_show_snooze);

  // Adding snooze count for |event|.
  Event event;
  event.set_name(config.trigger.name);
  test::SetSnoozeCountForDay(&event, 1u, 10u);
  event_model_.SetEvent(event);

  // When snooze count exceeds the maximum limit.
  result = GetResultForDay(config, 5u);
  EXPECT_TRUE(result.NoErrors());
  EXPECT_FALSE(result.should_show_snooze);
}

TEST_F(FeatureConfigConditionValidatorTest, SingleEventChangingComparator) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  uint32_t current_day = 102u;
  uint32_t window = 10u;

  // Create event with 10 events per day for three days.
  Event event1;
  event1.set_name("event1");
  test::SetEventCountForDay(&event1, 100u, 10u);
  test::SetEventCountForDay(&event1, 101u, 10u);
  test::SetEventCountForDay(&event1, 102u, 10u);
  event_model_.SetEvent(event1);

  EXPECT_TRUE(GetResultForDayAndEventWindow(Comparator(LESS_THAN, 50u), window,
                                            current_day)
                  .NoErrors());
  EXPECT_TRUE(
      GetResultForDayAndEventWindow(Comparator(EQUAL, 30u), window, current_day)
          .NoErrors());
  EXPECT_FALSE(GetResultForDayAndEventWindow(Comparator(LESS_THAN, 30u), window,
                                             current_day)
                   .NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, SingleEventChangingWindow) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  Event event1;
  event1.set_name("event1");
  test::SetEventCountForDay(&event1, 100u, 10u);
  test::SetEventCountForDay(&event1, 101u, 10u);
  test::SetEventCountForDay(&event1, 102u, 10u);
  test::SetEventCountForDay(&event1, 103u, 10u);
  test::SetEventCountForDay(&event1, 104u, 10u);
  event_model_.SetEvent(event1);

  uint32_t current_day = 104u;

  EXPECT_FALSE(GetResultForDayAndEventWindow(Comparator(GREATER_THAN, 30u), 0,
                                             current_day)
                   .NoErrors());
  EXPECT_FALSE(GetResultForDayAndEventWindow(Comparator(GREATER_THAN, 30u), 1u,
                                             current_day)
                   .NoErrors());
  EXPECT_FALSE(GetResultForDayAndEventWindow(Comparator(GREATER_THAN, 30u), 2u,
                                             current_day)
                   .NoErrors());
  EXPECT_FALSE(GetResultForDayAndEventWindow(Comparator(GREATER_THAN, 30u), 3u,
                                             current_day)
                   .NoErrors());
  EXPECT_TRUE(GetResultForDayAndEventWindow(Comparator(GREATER_THAN, 30u), 4u,
                                            current_day)
                  .NoErrors());
  EXPECT_TRUE(GetResultForDayAndEventWindow(Comparator(GREATER_THAN, 30u), 5u,
                                            current_day)
                  .NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, CapEarliestAcceptedDayAtEpoch) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  Event event1;
  event1.set_name("event1");
  test::SetEventCountForDay(&event1, 0, 10u);
  test::SetEventCountForDay(&event1, 1u, 10u);
  test::SetEventCountForDay(&event1, 2u, 10u);
  event_model_.SetEvent(event1);

  uint32_t current_day = 100u;

  EXPECT_TRUE(
      GetResultForDayAndEventWindow(Comparator(EQUAL, 10u), 99u, current_day)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayAndEventWindow(Comparator(EQUAL, 20u), 100u, current_day)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayAndEventWindow(Comparator(EQUAL, 30u), 101u, current_day)
          .NoErrors());
  EXPECT_TRUE(
      GetResultForDayAndEventWindow(Comparator(EQUAL, 30u), 1000u, current_day)
          .NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, TestMultipleEvents) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  Event event1;
  event1.set_name("event1");
  test::SetEventCountForDay(&event1, 0, 10u);
  test::SetEventCountForDay(&event1, 1u, 10u);
  test::SetEventCountForDay(&event1, 2u, 10u);
  event_model_.SetEvent(event1);

  Event event2;
  event2.set_name("event2");
  test::SetEventCountForDay(&event2, 0, 5u);
  test::SetEventCountForDay(&event2, 1u, 5u);
  test::SetEventCountForDay(&event2, 2u, 5u);
  event_model_.SetEvent(event2);

  uint32_t current_day = 100u;

  // Verify validator counts correctly for two events last 99 days.
  FeatureConfig config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(EQUAL, 10u), 99u, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(EQUAL, 5u), 99u, 0));
  ConditionValidator::Result result = validator_.MeetsConditions(
      kFeatureConfigTestFeatureFoo, config, event_model_, availability_model_,
      display_lock_controller_, current_day);
  EXPECT_TRUE(result.NoErrors());

  // Verify validator counts correctly for two events last 100 days.
  config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(EQUAL, 20u), 100u, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(EQUAL, 10u), 100u, 0));
  result = validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, current_day);
  EXPECT_TRUE(result.NoErrors());

  // Verify validator counts correctly for two events last 101 days.
  config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(EQUAL, 30u), 101u, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(EQUAL, 15u), 101u, 0));
  result = validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, current_day);
  EXPECT_TRUE(result.NoErrors());

  // Verify validator counts correctly for two events last 101 days, and returns
  // error when first event fails.
  config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(EQUAL, 0), 101u, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(EQUAL, 15u), 101u, 0));
  result = validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, current_day);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.preconditions_ok);

  // Verify validator counts correctly for two events last 101 days, and returns
  // error when second event fails.
  config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(EQUAL, 30u), 101u, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(EQUAL, 0), 101u, 0));
  result = validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, current_day);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.preconditions_ok);

  // Verify validator counts correctly for two events last 101 days, and returns
  // error when both events fail.
  config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(EQUAL, 0), 101u, 0));
  config.event_configs.insert(
      EventConfig("event2", Comparator(EQUAL, 0), 101u, 0));
  result = validator_.MeetsConditions(kFeatureConfigTestFeatureFoo, config,
                                      event_model_, availability_model_,
                                      display_lock_controller_, current_day);
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.preconditions_ok);
}

TEST_F(FeatureConfigConditionValidatorTest, TestStaggeredTriggering) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  // Trigger maximum 2 times, and only 1 time last 2 days (today + yesterday).
  FeatureConfig config;
  config.valid = true;
  config.used = EventConfig("used", Comparator(ANY, 0), 0, 0);
  config.trigger = EventConfig("trigger", Comparator(LESS_THAN, 2), 100u, 100u);
  config.session_rate = Comparator(ANY, 0);
  config.availability = Comparator(ANY, 0);
  config.event_configs.insert(
      EventConfig("trigger", Comparator(LESS_THAN, 1u), 2u, 100u));

  // Should be OK to trigger initially on day 0.
  EXPECT_TRUE(GetResultForDay(config, 0u).NoErrors());

  // Set that we triggered on day 0. We should then only trigger on day 2+.
  Event trigger_event;
  trigger_event.set_name("trigger");
  test::SetEventCountForDay(&trigger_event, 0u, 1u);
  event_model_.SetEvent(trigger_event);
  EXPECT_FALSE(GetResultForDay(config, 0u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 1u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 2u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 3u).NoErrors());

  // Set that we triggered again on day 2. We should then not trigger again
  // until max storage time has passed (100 days), which would expire the
  // trigger from day 0.
  test::SetEventCountForDay(&trigger_event, 2u, 1u);
  event_model_.SetEvent(trigger_event);
  EXPECT_FALSE(GetResultForDay(config, 2u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 3u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 4u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 5u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 99u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 100u).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, TestMultipleEventsWithSameName) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  // Trigger maximum 2 times, and only 1 time last 2 days (today + yesterday).
  FeatureConfig config = GetAcceptingFeatureConfig();
  config.event_configs.insert(
      EventConfig("event1", Comparator(LESS_THAN, 1u), 2u, 100u));
  config.event_configs.insert(
      EventConfig("event1", Comparator(LESS_THAN, 2u), 100u, 100u));

  // Should be OK to trigger initially on day 0.
  EXPECT_TRUE(GetResultForDay(config, 0u).NoErrors());

  // Set that we had event1 on day 0. We should then only trigger on day 2+.
  Event event1;
  event1.set_name("event1");
  test::SetEventCountForDay(&event1, 0u, 1u);
  event_model_.SetEvent(event1);
  EXPECT_FALSE(GetResultForDay(config, 0u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 1u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 2u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 3u).NoErrors());

  // Set that we had event1 again on day 2. We should then not trigger again
  // until max storage time has passed (100 days), which would expire the
  // trigger from day 0.
  test::SetEventCountForDay(&event1, 2u, 1u);
  event_model_.SetEvent(event1);
  EXPECT_FALSE(GetResultForDay(config, 2u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 3u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 4u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 5u).NoErrors());
  EXPECT_FALSE(GetResultForDay(config, 99u).NoErrors());
  EXPECT_TRUE(GetResultForDay(config, 100u).NoErrors());
}

TEST_F(FeatureConfigConditionValidatorTest, DisplayLockedStatus) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({kFeatureConfigTestFeatureFoo}, {});

  // When the display is locked, the result should be negative.
  display_lock_controller_.SetNextIsDisplayLockedResult(true);

  ConditionValidator::Result result =
      GetResultForDayZero(GetAcceptingFeatureConfig());
  EXPECT_FALSE(result.NoErrors());
  EXPECT_FALSE(result.display_lock_ok);

  // Setting the display to unlocked should make the result positive.
  display_lock_controller_.SetNextIsDisplayLockedResult(false);

  EXPECT_TRUE(GetResultForDayZero(GetAcceptingFeatureConfig()).NoErrors());
}

}  // namespace feature_engagement