File: display_decider_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (279 lines) | stat: -rw-r--r-- 10,572 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/scheduler/internal/display_decider.h"

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

#include "base/strings/stringprintf.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chrome/browser/notifications/scheduler/internal/notification_entry.h"
#include "chrome/browser/notifications/scheduler/internal/scheduler_config.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_types.h"
#include "chrome/browser/notifications/scheduler/test/fake_clock.h"
#include "chrome/browser/notifications/scheduler/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace notifications {
namespace {

// Initial state for test cases with a single registered client.
const std::vector<test::ImpressionTestData> kSingleClientImpressionTestData = {
    {SchedulerClientType::kTest1,
     2 /* current_max_daily_show */,
     {} /* impressions */,
     std::nullopt /* suppression_info */,
     0 /* negative_events_count */,
     std::nullopt /* negative_event_ts */,
     std::nullopt /* last_shown_ts */}};

const std::vector<test::ImpressionTestData> kClientsImpressionTestData = {
    {SchedulerClientType::kTest1,
     2 /* current_max_daily_show */,
     {} /* impressions */,
     std::nullopt /* suppression_info */,
     0 /* negative_events_count */,
     std::nullopt /* negative_event_ts */,
     std::nullopt /* last_shown_ts */},
    {SchedulerClientType::kTest2,
     5 /* current_max_daily_show */,
     {} /* impressions */,
     std::nullopt /* suppression_info */,
     0 /* negative_events_count */,
     std::nullopt /* negative_event_ts */,
     std::nullopt /* last_shown_ts */},
    {SchedulerClientType::kTest3,
     1 /* current_max_daily_show */,
     {} /* impressions */,
     std::nullopt /* suppression_info */,
     0 /* negative_events_count */,
     std::nullopt /* negative_event_ts */,
     std::nullopt /* last_shown_ts */}};

struct TestData {
  // Impression data as the input.
  std::vector<test::ImpressionTestData> impression_test_data;

  // Notification entries as the input.
  std::vector<NotificationEntry> notification_entries;

  // Expected output data.
  DisplayDecider::Results expected;
};

std::string DebugString(const DisplayDecider::Results& results) {
  std::string debug_string("notifications_to_show: \n");
  for (const auto& guid : results)
    debug_string += base::StringPrintf("%s ", guid.c_str());

  return debug_string;
}

class DisplayDeciderTest : public testing::Test {
 public:
  DisplayDeciderTest() = default;
  DisplayDeciderTest(const DisplayDeciderTest&) = delete;
  DisplayDeciderTest& operator=(const DisplayDeciderTest&) = delete;
  ~DisplayDeciderTest() override = default;

  void SetUp() override {
    // Setup configuration used by this test.
    config_.max_daily_shown_all_type = 3;

    // Fake Now() timestamp.
    base::Time fake_now;
    ASSERT_TRUE(base::Time::FromString("04/25/20 01:00:00 AM", &fake_now));
    clock_.SetNow(fake_now);
  }

 protected:
  // Initializes a test case with input data.
  void RunTestCase(const TestData& test_data) {
    test_data_ = test_data;
    test::AddImpressionTestData(test_data_.impression_test_data,
                                &client_states_);

    DisplayDecider::Notifications notifications;
    for (const auto& entry : test_data_.notification_entries) {
      notifications[entry.type].emplace_back(&entry);
    }
    std::vector<SchedulerClientType> clients;

    std::map<SchedulerClientType, const ClientState*> client_states;
    for (const auto& type : client_states_) {
      client_states.emplace(type.first, type.second.get());
      clients.emplace_back(type.first);
    }

    // Copy test inputs into |decider_|.
    decider_ = DisplayDecider::Create(&config_, clients, &clock_);
    decider_->FindNotificationsToShow(std::move(notifications),
                                      std::move(client_states), &results_);

    // Verify output.
    EXPECT_EQ(results_, test_data_.expected)
        << "Actual result: \n"
        << DebugString(results_) << " \n Expected result: \n"
        << DebugString(test_data_.expected);
  }

  // Creates a notification entry. Now() timestamp falls into its deliver time
  // window.
  NotificationEntry CreateNotification(SchedulerClientType type,
                                       const std::string& guid) {
    return CreateNotification(type, guid, base::TimeDelta(), base::Hours(1));
  }

  // Creates a notification entry with specific deliver time window.
  NotificationEntry CreateNotification(
      SchedulerClientType type,
      const std::string& guid,
      std::optional<base::TimeDelta> deliver_time_start_delta,
      std::optional<base::TimeDelta> deliver_time_end_delta) {
    NotificationEntry entry(type, guid);
    if (deliver_time_start_delta.has_value())
      entry.schedule_params.deliver_time_start =
          Now() + deliver_time_start_delta.value();
    if (deliver_time_end_delta.has_value())
      entry.schedule_params.deliver_time_end =
          Now() + deliver_time_end_delta.value();

    return entry;
  }

  SchedulerConfig* config() { return &config_; }

  base::Time Now() { return clock_.Now(); }

 private:
  base::test::TaskEnvironment task_environment_;

  TestData test_data_;
  SchedulerConfig config_;
  test::FakeClock clock_;

  std::map<SchedulerClientType, std::unique_ptr<ClientState>> client_states_;

  // Test target class and output.
  std::unique_ptr<DisplayDecider> decider_;
  DisplayDecider::Results results_;
};

TEST_F(DisplayDeciderTest, NoNotification) {
  TestData data{kClientsImpressionTestData, {}, DisplayDecider::Results()};
  RunTestCase(data);
}

// Simple test case to verify new notifcaiton can be selected to show.
TEST_F(DisplayDeciderTest, PickOneNotification) {
  auto entry = CreateNotification(SchedulerClientType::kTest1, "guid123");
  DisplayDecider::Results expected = {"guid123"};

  TestData data{kSingleClientImpressionTestData, {entry}, std::move(expected)};
  RunTestCase(data);
}

// Notification falls out of the target deliver time window will not be picked.
TEST_F(DisplayDeciderTest, OutOfDeliverTimeWindow) {
  auto entry0 = CreateNotification(SchedulerClientType::kTest2, "guid0",
                                   base::Days(1), base::Days(2));
  auto entry1 = CreateNotification(SchedulerClientType::kTest2, "guid1",
                                   base::TimeDelta() - base::Days(2),
                                   base::TimeDelta() - base::Days(1));
  auto entry2 = CreateNotification(SchedulerClientType::kTest2, "guid2",
                                   std::nullopt, std::nullopt);

  TestData data{kSingleClientImpressionTestData,
                {entry0, entry1, entry2},
                DisplayDecider::Results()};
  RunTestCase(data);
}

// Picks a notification for the next client if possible.
// Rotation client type sequence:  kTest3 => kTest2 => kTest1
// The last shown type is kTest1. Expected to show for client kTest3.
TEST_F(DisplayDeciderTest, ClientRotation) {
  config()->max_daily_shown_all_type = 2;
  auto impression_test_data = kClientsImpressionTestData;

  auto entry1 = CreateNotification(SchedulerClientType::kTest1, "guid1");

  // Create an impression shown today.
  Impression impression(SchedulerClientType::kTest1, "shown_guid1",
                        Now() - base::Hours(1));
  impression_test_data.front().impressions.emplace_back(impression);

  auto entry2 = CreateNotification(SchedulerClientType::kTest2, "guid2");
  auto entry3 = CreateNotification(SchedulerClientType::kTest3, "guid3");

  TestData data{impression_test_data, {entry1, entry2, entry3}, {"guid3"}};
  RunTestCase(data);
}

// After reaching maximum daily shown throttle, no notifications will be picked.
TEST_F(DisplayDeciderTest, ThrottleMaxDailyShowAllTypes) {
  auto impression_test_data = kClientsImpressionTestData;

  auto entry1 = CreateNotification(SchedulerClientType::kTest1, "guid1");

  // Create an impression shown today, but only allow to show one per day for
  // all clients.
  Impression impression(SchedulerClientType::kTest1, "shown_guid1",
                        Now() - base::Hours(1));
  impression_test_data.front().impressions.emplace_back(impression);
  config()->max_daily_shown_all_type = 1;

  TestData data{impression_test_data, {entry1}, DisplayDecider::Results()};
  RunTestCase(data);
}

// After reaching client's maximum daily shown throttle, notifications scheduled
// for this client will not be show.
TEST_F(DisplayDeciderTest, ThrottlePerClient) {
  config()->max_daily_shown_all_type = 10;
  auto impression_test_data = kClientsImpressionTestData;

  // Have 2 notifications, but only one can be shown due to per client throttle.
  impression_test_data.front().current_max_daily_show = 2;
  auto entry1 = CreateNotification(SchedulerClientType::kTest1, "guid1");
  auto entry2 = CreateNotification(SchedulerClientType::kTest1, "guid2");

  // Create an impression shown today.
  Impression impression(SchedulerClientType::kTest1, "shown_guid1",
                        Now() - base::Hours(1));
  impression_test_data.front().impressions.emplace_back(impression);

  TestData data{impression_test_data, {entry1, entry2}, {"guid2"}};
  RunTestCase(data);
}

// Client with suppression will not have notification shown.
TEST_F(DisplayDeciderTest, ThrottleSuppressedClient) {
  auto impression_test_data = kClientsImpressionTestData;
  impression_test_data.front().suppression_info =
      SuppressionInfo(Now(), base::Days(10));
  auto entry1 = CreateNotification(SchedulerClientType::kTest1, "guid1");

  TestData data{impression_test_data, {entry1}, DisplayDecider::Results()};
  RunTestCase(data);
}

// Notifitions with NoThrottle Priority should always show.
TEST_F(DisplayDeciderTest, UnthrottlePriority) {
  auto impression_test_data = kClientsImpressionTestData;
  auto entry1 = CreateNotification(SchedulerClientType::kTest1, "guid1");
  entry1.schedule_params.priority = ScheduleParams::Priority::kNoThrottle;
  auto entry2 = CreateNotification(SchedulerClientType::kTest1, "guid2");
  entry2.schedule_params.priority = ScheduleParams::Priority::kNoThrottle;
  config()->max_daily_shown_all_type = 0;
  TestData data{impression_test_data, {entry1, entry2}, {"guid1", "guid2"}};
  RunTestCase(data);
}

}  // namespace
}  // namespace notifications