File: scheduler_utils_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (187 lines) | stat: -rw-r--r-- 7,344 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
// 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/scheduler_utils.h"

#include <algorithm>
#include <vector>

#include "base/containers/circular_deque.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "chrome/browser/notifications/scheduler/internal/impression_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 {

const char kFakeNow[] = "01/01/18 01:23:45 AM";

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

  void SetUp() override { config_.initial_daily_shown_per_type = 100; }

  void InitFakeClock() {
    clock_.SetNow(kFakeNow);
    ToLocalHour(0, clock_.Now(), 0, &beginning_of_today_);
  }

  void CreateFakeImpressions(ClientState* client_state,
                             const std::vector<base::Time>& times) {
    DCHECK(client_state);
    client_state->impressions.clear();
    auto type = client_state->type;
    for (const auto& time : times) {
      client_state->impressions.emplace_back(
          type, base::Uuid::GenerateRandomV4().AsLowercaseString(), time);
    }
  }

  std::unique_ptr<ClientState> CreateFakeClientStateWithImpression(
      SchedulerClientType type,
      const SchedulerConfig& config,
      const std::vector<base::Time>& times) {
    auto client_state = std::make_unique<ClientState>();
    client_state->type = type;
    client_state->current_max_daily_show = config.initial_daily_shown_per_type;
    CreateFakeImpressions(client_state.get(), times);
    return client_state;
  }

 protected:
  SchedulerConfig& config() { return config_; }
  test::FakeClock* clock() { return &clock_; }
  base::Time& beginning_of_today() { return beginning_of_today_; }

 private:
  base::test::TaskEnvironment task_environment_;
  test::FakeClock clock_;
  SchedulerConfig config_;
  base::Time beginning_of_today_;
};

// Verifies we can get the correct time stamp at certain hour in yesterday.
TEST_F(SchedulerUtilsTest, ToLocalHour) {
  base::Time today, another_day, expected;

  // Timestamp of another day in the past.
  EXPECT_TRUE(base::Time::FromString("10/15/07 12:45:12 PM", &today));
  EXPECT_TRUE(ToLocalHour(6, today, -1, &another_day));
  EXPECT_TRUE(base::Time::FromString("10/14/07 06:00:00 AM", &expected));
  EXPECT_EQ(expected, another_day);

  EXPECT_TRUE(base::Time::FromString("03/25/19 00:00:00 AM", &today));
  EXPECT_TRUE(ToLocalHour(0, today, -1, &another_day));
  EXPECT_TRUE(base::Time::FromString("03/24/19 00:00:00 AM", &expected));
  EXPECT_EQ(expected, another_day);

  // Timestamp of the same day.
  EXPECT_TRUE(base::Time::FromString("03/25/19 00:00:00 AM", &today));
  EXPECT_TRUE(ToLocalHour(0, today, 0, &another_day));
  EXPECT_TRUE(base::Time::FromString("03/25/19 00:00:00 AM", &expected));
  EXPECT_EQ(expected, another_day);

  // Timestamp of another day in the future.
  EXPECT_TRUE(base::Time::FromString("03/25/19 06:35:27 AM", &today));
  EXPECT_TRUE(ToLocalHour(16, today, 7, &another_day));
  EXPECT_TRUE(base::Time::FromString("04/01/19 16:00:00 PM", &expected));
  EXPECT_EQ(expected, another_day);
}

TEST_F(SchedulerUtilsTest, NotificationsShownTodayMultipleClients) {
  InitFakeClock();
  base::Time now = clock()->Now();
  // Create fake clients.
  std::map<SchedulerClientType, const ClientState*> client_states;
  //            begin_of_today         now                  end_of_today
  // client1  * |                   *  *                    | *
  // client2  * *   *                  |  *                 | *
  // client3  * |                      |                    | *

  std::vector<base::Time> create_times = {
      now - base::Seconds(2) /*today*/, now - base::Seconds(1) /*today*/,
      beginning_of_today() - base::Seconds(1) /*yesterday*/,
      beginning_of_today() + base::Days(1) /*tomorrow*/};
  auto new_client1 = CreateFakeClientStateWithImpression(
      SchedulerClientType::kTest1, config(), create_times);

  create_times = {now /*today*/,
                  beginning_of_today() + base::Days(1) /*tomorrow*/,
                  beginning_of_today() - base::Seconds(1) /*yesterday*/,
                  beginning_of_today() + base::Seconds(1) /*today*/,
                  beginning_of_today() /*today*/};
  auto new_client2 = CreateFakeClientStateWithImpression(
      SchedulerClientType::kTest2, config(), create_times);

  create_times = {
      beginning_of_today() - base::Seconds(2), /*yesterday*/
      beginning_of_today() + base::Days(1)     /*tomorrow*/
  };
  auto new_client3 = CreateFakeClientStateWithImpression(
      SchedulerClientType::kTest3, config(), create_times);

  client_states[SchedulerClientType::kTest1] = new_client1.get();
  client_states[SchedulerClientType::kTest2] = new_client2.get();
  client_states[SchedulerClientType::kTest3] = new_client3.get();

  std::map<SchedulerClientType, int> shown_per_type;
  int shown_total = 0;
  SchedulerClientType last_shown_type = SchedulerClientType::kUnknown;
  NotificationsShownToday(client_states, &shown_per_type, &shown_total,
                          &last_shown_type, clock());
  EXPECT_EQ(shown_total, 5);
  EXPECT_EQ(last_shown_type, SchedulerClientType::kTest2);
  EXPECT_EQ(shown_per_type.at(SchedulerClientType::kTest1), 2);
  EXPECT_EQ(shown_per_type.at(SchedulerClientType::kTest2), 3);
  EXPECT_EQ(shown_per_type.at(SchedulerClientType::kTest3), 0);
}

TEST_F(SchedulerUtilsTest, NotificationsShownToday) {
  // Create fake client.
  auto new_client = CreateNewClientState(SchedulerClientType::kTest1, config());
  InitFakeClock();
  base::Time now = clock()->Now();
  // Test case 1:
  int count = NotificationsShownToday(new_client.get(), clock());
  EXPECT_EQ(count, 0);

  // Test case 2:
  std::vector<base::Time> create_times = {
      now /*today*/, beginning_of_today() + base::Days(1) /*tomorrow*/,
      beginning_of_today() - base::Seconds(1) /*yesterday*/,
      beginning_of_today() + base::Seconds(1) /*today*/,
      beginning_of_today() /*today*/};

  CreateFakeImpressions(new_client.get(), create_times);
  count = NotificationsShownToday(new_client.get(), clock());
  EXPECT_EQ(count, 3);

  // Test case 3:
  create_times = {
      beginning_of_today() - base::Seconds(2), /*yesterday*/
      beginning_of_today() + base::Days(1),    /*tomorrow*/
  };
  CreateFakeImpressions(new_client.get(), create_times);
  count = NotificationsShownToday(new_client.get(), clock());
  EXPECT_EQ(count, 0);

  // Test case 4:
  create_times = {now /*today*/, now - base::Seconds(1) /*today*/,
                  beginning_of_today() - base::Seconds(1) /*yesterday*/,
                  beginning_of_today() + base::Days(1) /*tomorrow*/};
  CreateFakeImpressions(new_client.get(), create_times);
  count = NotificationsShownToday(new_client.get(), clock());
  EXPECT_EQ(count, 2);
}

}  // namespace
}  // namespace notifications