File: weekly_time_interval_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (458 lines) | stat: -rw-r--r-- 19,509 bytes parent folder | download | duplicates (7)
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
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chromeos/ash/components/policy/weekly_time/weekly_time_interval.h"

#include <tuple>
#include <utility>

#include "base/time/time.h"
#include "base/values.h"
#include "chromeos/ash/components/policy/weekly_time/weekly_time.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace em = enterprise_management;

namespace policy {
namespace {

enum {
  kMonday = 1,
  kTuesday = 2,
  kWednesday = 3,
  kThursday = 4,
  kFriday = 5,
  kSaturday = 6,
  kSunday = 7,
};

const int kMinutesInHour = 60;

constexpr base::TimeDelta kMinute = base::Minutes(1);

constexpr em::WeeklyTimeProto_DayOfWeek kWeekdays[] = {
    em::WeeklyTimeProto::DAY_OF_WEEK_UNSPECIFIED,
    em::WeeklyTimeProto::MONDAY,
    em::WeeklyTimeProto::TUESDAY,
    em::WeeklyTimeProto::WEDNESDAY,
    em::WeeklyTimeProto::THURSDAY,
    em::WeeklyTimeProto::FRIDAY,
    em::WeeklyTimeProto::SATURDAY,
    em::WeeklyTimeProto::SUNDAY};

}  // namespace

class SingleWeeklyTimeIntervalTest
    : public testing::TestWithParam<std::tuple<int, int, int, int>> {
 protected:
  int start_day_of_week() const { return std::get<0>(GetParam()); }
  int start_time() const { return std::get<1>(GetParam()); }
  int end_day_of_week() const { return std::get<2>(GetParam()); }
  int end_time() const { return std::get<3>(GetParam()); }
};

TEST_P(SingleWeeklyTimeIntervalTest, Constructor) {
  WeeklyTime start = WeeklyTime(start_day_of_week(),
                                start_time() * kMinute.InMilliseconds(), 0);
  WeeklyTime end =
      WeeklyTime(end_day_of_week(), end_time() * kMinute.InMilliseconds(), 0);
  WeeklyTimeInterval interval = WeeklyTimeInterval(start, end);
  EXPECT_EQ(interval.start().day_of_week(), start_day_of_week());
  EXPECT_EQ(interval.start().milliseconds(),
            start_time() * kMinute.InMilliseconds());
  EXPECT_EQ(interval.end().day_of_week(), end_day_of_week());
  EXPECT_EQ(interval.end().milliseconds(),
            end_time() * kMinute.InMilliseconds());
  EXPECT_EQ(interval.start().timezone_offset(),
            interval.end().timezone_offset());
  EXPECT_EQ(interval.start().timezone_offset(), 0);
}

TEST_P(SingleWeeklyTimeIntervalTest, ToValue) {
  WeeklyTime start = WeeklyTime(start_day_of_week(), start_time(), 0);
  WeeklyTime end = WeeklyTime(end_day_of_week(), end_time(), 0);
  WeeklyTimeInterval interval = WeeklyTimeInterval(start, end);
  base::Value expected_interval_value(base::Value::Type::DICT);
  base::Value::Dict& dict = expected_interval_value.GetDict();
  dict.Set(WeeklyTimeInterval::kStart, start.ToValue());
  dict.Set(WeeklyTimeInterval::kEnd, end.ToValue());

  EXPECT_EQ(interval.ToValue(), expected_interval_value);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromProto_Empty) {
  em::WeeklyTimeIntervalProto interval_proto;
  auto result = WeeklyTimeInterval::ExtractFromProto(interval_proto, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromProto_NoEnd) {
  em::WeeklyTimeIntervalProto interval_proto;
  em::WeeklyTimeProto* start = interval_proto.mutable_start();
  start->set_day_of_week(kWeekdays[start_day_of_week()]);
  start->set_time(start_time());
  auto result = WeeklyTimeInterval::ExtractFromProto(interval_proto, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromProto_NoStart) {
  em::WeeklyTimeIntervalProto interval_proto;
  em::WeeklyTimeProto* end = interval_proto.mutable_end();
  end->set_day_of_week(kWeekdays[end_day_of_week()]);
  end->set_time(end_time());
  auto result = WeeklyTimeInterval::ExtractFromProto(interval_proto, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromProto_InvalidStart) {
  em::WeeklyTimeIntervalProto interval_proto;
  em::WeeklyTimeProto* start = interval_proto.mutable_start();
  em::WeeklyTimeProto* end = interval_proto.mutable_end();
  start->set_day_of_week(kWeekdays[0]);
  start->set_time(start_time());
  end->set_day_of_week(kWeekdays[end_day_of_week()]);
  end->set_time(end_time());
  auto result = WeeklyTimeInterval::ExtractFromProto(interval_proto, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromProto_InvalidEnd) {
  em::WeeklyTimeIntervalProto interval_proto;
  em::WeeklyTimeProto* start = interval_proto.mutable_start();
  em::WeeklyTimeProto* end = interval_proto.mutable_end();
  start->set_day_of_week(kWeekdays[start_day_of_week()]);
  start->set_time(start_time());
  end->set_day_of_week(kWeekdays[0]);
  end->set_time(end_time());
  auto result = WeeklyTimeInterval::ExtractFromProto(interval_proto, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromProto_InvalidStartEqualsEnd) {
  em::WeeklyTimeIntervalProto interval_proto;
  em::WeeklyTimeProto* start = interval_proto.mutable_start();
  em::WeeklyTimeProto* end = interval_proto.mutable_end();
  start->set_day_of_week(kWeekdays[start_day_of_week()]);
  start->set_time(start_time());
  end->set_day_of_week(kWeekdays[start_day_of_week()]);
  end->set_time(start_time());
  auto result = WeeklyTimeInterval::ExtractFromProto(interval_proto, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromProto_Valid) {
  em::WeeklyTimeIntervalProto interval_proto;
  em::WeeklyTimeProto* start = interval_proto.mutable_start();
  em::WeeklyTimeProto* end = interval_proto.mutable_end();
  start->set_day_of_week(kWeekdays[start_day_of_week()]);
  start->set_time(start_time());
  end->set_day_of_week(kWeekdays[end_day_of_week()]);
  end->set_time(end_time());
  auto result = WeeklyTimeInterval::ExtractFromProto(interval_proto, 0);
  ASSERT_TRUE(result);
  EXPECT_EQ(result->start().day_of_week(), start_day_of_week());
  EXPECT_EQ(result->start().milliseconds(), start_time());
  EXPECT_EQ(result->end().day_of_week(), end_day_of_week());
  EXPECT_EQ(result->end().milliseconds(), end_time());
  EXPECT_EQ(result->start().timezone_offset(), 0);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromDict_Empty) {
  base::Value::Dict dict;
  auto result = WeeklyTimeInterval::ExtractFromDict(dict, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromDict_NoEnd) {
  base::Value::Dict dict;
  base::Value::Dict start;
  EXPECT_TRUE(start.Set(WeeklyTime::kDayOfWeek,
                        WeeklyTime::kWeekDays[start_day_of_week()]));
  EXPECT_TRUE(start.Set(WeeklyTime::kTime, start_time()));
  dict.Set(WeeklyTimeInterval::kStart, std::move(start));

  auto result = WeeklyTimeInterval::ExtractFromDict(dict, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromDict_NoStart) {
  base::Value::Dict dict;
  base::Value::Dict end;
  EXPECT_TRUE(end.Set(WeeklyTime::kDayOfWeek,
                      WeeklyTime::kWeekDays[end_day_of_week()]));
  EXPECT_TRUE(end.Set(WeeklyTime::kTime, end_time()));
  dict.Set(WeeklyTimeInterval::kEnd, std::move(end));

  auto result = WeeklyTimeInterval::ExtractFromDict(dict, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromDict_InvalidStart) {
  base::Value::Dict dict;
  base::Value::Dict start;
  EXPECT_TRUE(start.Set(WeeklyTime::kDayOfWeek, WeeklyTime::kWeekDays[0]));
  EXPECT_TRUE(start.Set(WeeklyTime::kTime, start_time()));
  dict.Set(WeeklyTimeInterval::kStart, std::move(start));
  base::Value::Dict end;
  EXPECT_TRUE(end.Set(WeeklyTime::kDayOfWeek,
                      WeeklyTime::kWeekDays[end_day_of_week()]));
  EXPECT_TRUE(end.Set(WeeklyTime::kTime, end_time()));
  dict.Set(WeeklyTimeInterval::kEnd, std::move(end));

  auto result = WeeklyTimeInterval::ExtractFromDict(dict, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromDict_InvalidEnd) {
  base::Value::Dict dict;
  base::Value::Dict start;
  EXPECT_TRUE(start.Set(WeeklyTime::kDayOfWeek,
                        WeeklyTime::kWeekDays[start_day_of_week()]));
  EXPECT_TRUE(start.Set(WeeklyTime::kTime, start_time()));
  dict.Set(WeeklyTimeInterval::kStart, std::move(start));
  base::Value::Dict end;
  EXPECT_TRUE(end.Set(WeeklyTime::kDayOfWeek, WeeklyTime::kWeekDays[0]));
  EXPECT_TRUE(end.Set(WeeklyTime::kTime, end_time()));
  dict.Set(WeeklyTimeInterval::kEnd, std::move(end));

  auto result = WeeklyTimeInterval::ExtractFromDict(dict, 0);
  ASSERT_FALSE(result);
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromDict_InvalidStartEqualsEnd) {
  base::Value::Dict start = base::Value::Dict()
                                .Set(WeeklyTime::kDayOfWeek,
                                     WeeklyTime::kWeekDays[start_day_of_week()])
                                .Set(WeeklyTime::kTime, start_time());
  base::Value::Dict end = start.Clone();
  base::Value::Dict test_dict =
      base::Value::Dict()
          .Set(WeeklyTimeInterval::kStart, std::move(start))
          .Set(WeeklyTimeInterval::kEnd, std::move(end));

  EXPECT_FALSE(WeeklyTimeInterval::ExtractFromDict(test_dict, 0));
}

TEST_P(SingleWeeklyTimeIntervalTest, ExtractFromDict_Valid) {
  base::Value::Dict dict;
  base::Value::Dict start;
  EXPECT_TRUE(start.Set(WeeklyTime::kDayOfWeek,
                        WeeklyTime::kWeekDays[start_day_of_week()]));
  EXPECT_TRUE(start.Set(WeeklyTime::kTime, start_time()));
  dict.Set(WeeklyTimeInterval::kStart, std::move(start));
  base::Value::Dict end;
  EXPECT_TRUE(end.Set(WeeklyTime::kDayOfWeek,
                      WeeklyTime::kWeekDays[end_day_of_week()]));
  EXPECT_TRUE(end.Set(WeeklyTime::kTime, end_time()));
  dict.Set(WeeklyTimeInterval::kEnd, std::move(end));

  auto result = WeeklyTimeInterval::ExtractFromDict(dict, 0);
  ASSERT_TRUE(result);
  EXPECT_EQ(result->start().day_of_week(), start_day_of_week());
  EXPECT_EQ(result->start().milliseconds(), start_time());
  EXPECT_EQ(result->end().day_of_week(), end_day_of_week());
  EXPECT_EQ(result->end().milliseconds(), end_time());
  EXPECT_EQ(result->start().timezone_offset(), 0);
}

INSTANTIATE_TEST_SUITE_P(OneMinuteInterval,
                         SingleWeeklyTimeIntervalTest,
                         testing::Values(std::make_tuple(kWednesday,
                                                         kMinutesInHour,
                                                         kWednesday,
                                                         kMinutesInHour + 1)));
INSTANTIATE_TEST_SUITE_P(
    TheLongestInterval,
    SingleWeeklyTimeIntervalTest,
    testing::Values(
        std::make_tuple(kMonday, 0, kSunday, 24 * kMinutesInHour - 1)));

INSTANTIATE_TEST_SUITE_P(RandomInterval,
                         SingleWeeklyTimeIntervalTest,
                         testing::Values(std::make_tuple(kTuesday,
                                                         10 * kMinutesInHour,
                                                         kFriday,
                                                         14 * kMinutesInHour +
                                                             15)));

class WeeklyTimeIntervalAndWeeklyTimeTest
    : public testing::TestWithParam<
          std::tuple<int, int, int, int, int, int, bool>> {
 protected:
  int start_day_of_week() const { return std::get<0>(GetParam()); }
  int start_time() const { return std::get<1>(GetParam()); }
  int end_day_of_week() const { return std::get<2>(GetParam()); }
  int end_time() const { return std::get<3>(GetParam()); }
  int current_day_of_week() const { return std::get<4>(GetParam()); }
  int current_time() const { return std::get<5>(GetParam()); }
  bool expected_contains() const { return std::get<6>(GetParam()); }
};

TEST_P(WeeklyTimeIntervalAndWeeklyTimeTest, Contains) {
  WeeklyTime start = WeeklyTime(start_day_of_week(),
                                start_time() * kMinute.InMilliseconds(), 0);
  WeeklyTime end =
      WeeklyTime(end_day_of_week(), end_time() * kMinute.InMilliseconds(), 0);
  WeeklyTimeInterval interval = WeeklyTimeInterval(start, end);
  WeeklyTime weekly_time = WeeklyTime(
      current_day_of_week(), current_time() * kMinute.InMilliseconds(), 0);
  EXPECT_EQ(interval.Contains(weekly_time), expected_contains());
}

INSTANTIATE_TEST_SUITE_P(
    TheLongestInterval,
    WeeklyTimeIntervalAndWeeklyTimeTest,
    testing::Values(std::make_tuple(kMonday,
                                    0,
                                    kSunday,
                                    24 * kMinutesInHour - 1,
                                    kWednesday,
                                    10 * kMinutesInHour,
                                    true),
                    std::make_tuple(kSunday,
                                    24 * kMinutesInHour - 1,
                                    kMonday,
                                    0,
                                    kWednesday,
                                    10 * kMinutesInHour,
                                    false)));

INSTANTIATE_TEST_SUITE_P(
    TheShortestInterval,
    WeeklyTimeIntervalAndWeeklyTimeTest,
    testing::Values(std::make_tuple(kMonday,
                                    0,
                                    kMonday,
                                    1,
                                    kTuesday,
                                    9 * kMinutesInHour,
                                    false),
                    std::make_tuple(kMonday, 0, kMonday, 1, kMonday, 1, false),
                    std::make_tuple(kMonday, 0, kMonday, 1, kMonday, 0, true)));

INSTANTIATE_TEST_SUITE_P(
    CheckStartInterval,
    WeeklyTimeIntervalAndWeeklyTimeTest,
    testing::Values(std::make_tuple(kTuesday,
                                    10 * kMinutesInHour + 30,
                                    kFriday,
                                    14 * kMinutesInHour + 45,
                                    kTuesday,
                                    10 * kMinutesInHour + 30,
                                    true)));

INSTANTIATE_TEST_SUITE_P(
    CheckEndInterval,
    WeeklyTimeIntervalAndWeeklyTimeTest,
    testing::Values(std::make_tuple(kTuesday,
                                    10 * kMinutesInHour + 30,
                                    kFriday,
                                    14 * kMinutesInHour + 45,
                                    kFriday,
                                    14 * kMinutesInHour + 45,
                                    false)));

INSTANTIATE_TEST_SUITE_P(RandomInterval,
                         WeeklyTimeIntervalAndWeeklyTimeTest,
                         testing::Values(std::make_tuple(kFriday,
                                                         17 * kMinutesInHour +
                                                             60,
                                                         kMonday,
                                                         9 * kMinutesInHour,
                                                         kSunday,
                                                         14 * kMinutesInHour,
                                                         true),
                                         std::make_tuple(kMonday,
                                                         9 * kMinutesInHour,
                                                         kFriday,
                                                         17 * kMinutesInHour,
                                                         kSunday,
                                                         14 * kMinutesInHour,
                                                         false)));

using WeeklyTimeIntervalOverlapTest = testing::Test;

TEST_F(WeeklyTimeIntervalOverlapTest, NoOverlap) {
  WeeklyTimeInterval interval_a =
      WeeklyTimeInterval(WeeklyTime(kMonday, base::Hours(18).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt),
                         WeeklyTime(kTuesday, base::Hours(8).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt));

  WeeklyTimeInterval interval_b =
      WeeklyTimeInterval(WeeklyTime(kFriday, base::Hours(8).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt),
                         WeeklyTime(kSaturday, base::Hours(8).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt));

  WeeklyTimeInterval interval_c = WeeklyTimeInterval(
      WeeklyTime(kTuesday, base::Hours(8).InMilliseconds(),
                 /*timezone_offset=*/std::nullopt),
      WeeklyTime(kWednesday, base::Hours(14).InMilliseconds(),
                 /*timezone_offset=*/std::nullopt));

  /*
  Mon         Tue         Wed         Thu         Fri         Sat         Sun
          |---------|                               |--------------|
               a                                            b
                    |------------|
                          c
  */
  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_a, interval_b));
  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_b, interval_a));

  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_a, interval_c));
  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_c, interval_a));

  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_b, interval_c));
  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_c, interval_b));
}

TEST_F(WeeklyTimeIntervalOverlapTest, Overlap) {
  WeeklyTimeInterval interval_a =
      WeeklyTimeInterval(WeeklyTime(kMonday, base::Hours(18).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt),
                         WeeklyTime(kThursday, base::Hours(18).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt));

  WeeklyTimeInterval interval_b =
      WeeklyTimeInterval(WeeklyTime(kThursday, base::Hours(6).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt),
                         WeeklyTime(kFriday, base::Hours(13).InMilliseconds(),
                                    /*timezone_offset=*/std::nullopt));

  WeeklyTimeInterval interval_c = WeeklyTimeInterval(
      WeeklyTime(kTuesday, base::Hours(8).InMilliseconds(),
                 /*timezone_offset=*/std::nullopt),
      WeeklyTime(kWednesday, base::Hours(14).InMilliseconds(),
                 /*timezone_offset=*/std::nullopt));

  /*
  Mon         Tue         Wed         Thu         Fri         Sat         Sun
          |------------------------------------|
               a
                                         |----------------|
                                                  b
                    |------------|
                          c
  */
  EXPECT_TRUE(WeeklyTimeInterval::IntervalsOverlap(interval_a, interval_a));

  EXPECT_TRUE(WeeklyTimeInterval::IntervalsOverlap(interval_a, interval_b));
  EXPECT_TRUE(WeeklyTimeInterval::IntervalsOverlap(interval_b, interval_a));

  EXPECT_TRUE(WeeklyTimeInterval::IntervalsOverlap(interval_a, interval_c));
  EXPECT_TRUE(WeeklyTimeInterval::IntervalsOverlap(interval_c, interval_a));

  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_b, interval_c));
  EXPECT_FALSE(WeeklyTimeInterval::IntervalsOverlap(interval_c, interval_b));
}

}  // namespace policy