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
ash / system / scheduled_feature / schedule_utils_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/scheduled_feature/schedule_utils.h"
#include <string_view>
#include "ash/public/cpp/schedule_enums.h"
#include "ash/system/time/time_of_day.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Eq;
using ::testing::FieldsAre;
namespace ash::schedule_utils {
namespace {
class ScheduleUtilsTest : public ::testing::Test {
protected:
ScheduleUtilsTest() {
// Pick an arbitrary time to start the clock at for determinism.
clock_.SetNow(BuildTime("00:00:00"));
}
void BuildTimeWithAssert(std::string_view time_of_day, base::Time* output) {
// The date here is arbitrary.
ASSERT_TRUE(base::Time::FromString(
base::StrCat({"23 Dec 2021 ", time_of_day}).c_str(), output));
}
base::Time BuildTime(std::string_view time_of_day) {
base::Time output;
BuildTimeWithAssert(time_of_day, &output);
return output;
}
void AdvanceClockTo(std::string_view time_of_day) {
const base::Time target_time = BuildTime(time_of_day);
const base::TimeDelta target_time_adjustment =
(target_time - clock_.Now()).FloorToMultiple(base::Days(1));
clock_.SetNow(target_time - target_time_adjustment);
}
base::SimpleTestClock clock_;
};
TEST_F(ScheduleUtilsTest, SunsetToSunriseDetectsAllCheckpoints) {
// Sunrise: 6 AM
// Morning: 10 AM
// LateAfternoon: 4 PM
// Sunset: 6 PM
const base::Time sunrise_time = BuildTime("06:00:00");
const base::Time sunset_time = BuildTime("18:00:00");
AdvanceClockTo("05:59:59");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunset,
ScheduleCheckpoint::kSunrise, base::Seconds(1)));
AdvanceClockTo("06:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunrise,
ScheduleCheckpoint::kMorning, base::Hours(4)));
AdvanceClockTo("09:59:59");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunrise,
ScheduleCheckpoint::kMorning, base::Seconds(1)));
AdvanceClockTo("10:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kMorning,
ScheduleCheckpoint::kLateAfternoon, base::Hours(6)));
AdvanceClockTo("15:59:59");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kMorning,
ScheduleCheckpoint::kLateAfternoon, base::Seconds(1)));
AdvanceClockTo("16:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kLateAfternoon,
ScheduleCheckpoint::kSunset, base::Hours(2)));
AdvanceClockTo("17:59:59");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kLateAfternoon,
ScheduleCheckpoint::kSunset, base::Seconds(1)));
AdvanceClockTo("18:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunset,
ScheduleCheckpoint::kSunrise, base::Hours(12)));
}
TEST_F(ScheduleUtilsTest, SunsetToSunriseSunsetJustAfterSunrise) {
// Sunrise: 12:00 PM
// Morning: 12:00 PM + (1.5 hours / 3) = 12:30 PM (usually 10 AM)
// LateAfternoon: 1:30 PM - (1.5 hours / 6) = 1:15 PM (usually 4 PM)
// Sunset: 1:30 PM
const base::Time sunrise_time = BuildTime("12:00:00");
const base::Time sunset_time = BuildTime("13:30:00");
AdvanceClockTo("12:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunrise,
ScheduleCheckpoint::kMorning, base::Minutes(30)));
AdvanceClockTo("12:30:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kMorning,
ScheduleCheckpoint::kLateAfternoon, base::Minutes(45)));
AdvanceClockTo("13:15:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kLateAfternoon,
ScheduleCheckpoint::kSunset, base::Minutes(15)));
AdvanceClockTo("13:30:00");
EXPECT_THAT(
GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunset, ScheduleCheckpoint::kSunrise,
base::Days(1) - base::Minutes(90)));
}
TEST_F(ScheduleUtilsTest, SunsetToSunriseSunriseJustAfterSunset) {
// Sunrise: 2:00 PM
// Morning: 2:00 PM + (22 hours / 3) = 9:20 PM (usually 10 AM)
// LateAfternoon: 12:00 PM - (22 hours / 6) = 8:20 AM (usually 4 PM)
// Sunset: 12:00 PM
const base::Time sunrise_time = BuildTime("14:00:00");
const base::Time sunset_time = BuildTime("12:00:00");
AdvanceClockTo("14:00:00");
EXPECT_THAT(
GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunrise, ScheduleCheckpoint::kMorning,
base::Hours(7) + base::Minutes(20)));
AdvanceClockTo("21:20:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kMorning,
ScheduleCheckpoint::kLateAfternoon, base::Hours(11)));
AdvanceClockTo("08:20:00");
EXPECT_THAT(
GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kLateAfternoon, ScheduleCheckpoint::kSunset,
base::Hours(3) + base::Minutes(40)));
AdvanceClockTo("12:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunset,
ScheduleCheckpoint::kSunrise, base::Hours(2)));
}
TEST_F(ScheduleUtilsTest, SunsetToSunriseShiftsScheduleToMatchDate) {
// Sunrise: 6 AM
// Morning: 10 AM
// LateAfternoon: 4 PM
// Sunset: 6 PM
const base::Time sunrise_time = BuildTime("06:00:00");
const base::Time sunset_time = BuildTime("18:00:00");
const auto test_all_checkpoints = [this, sunrise_time, sunset_time]() {
AdvanceClockTo("08:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunrise,
ScheduleCheckpoint::kMorning, base::Hours(2)));
AdvanceClockTo("12:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kMorning,
ScheduleCheckpoint::kLateAfternoon, base::Hours(4)));
AdvanceClockTo("17:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kLateAfternoon,
ScheduleCheckpoint::kSunset, base::Hours(1)));
AdvanceClockTo("00:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunset,
ScheduleCheckpoint::kSunrise, base::Hours(6)));
};
// Set now to be 5 days earlier. The schedule should still hold.
clock_.SetNow(BuildTime("00:00:00") - base::Days(5));
test_all_checkpoints();
// Set now to be 5 days later. The schedule should still hold.
clock_.SetNow(BuildTime("00:00:00") + base::Days(5));
test_all_checkpoints();
}
TEST_F(ScheduleUtilsTest, SunsetToSunriseMinimalSpaceFromSunriseToSunset) {
// 6 seconds from sunrise to sunset
base::Time sunrise_time = BuildTime("00:00:00");
base::Time sunset_time = sunrise_time + base::Microseconds(6);
AdvanceClockTo("00:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunrise,
ScheduleCheckpoint::kMorning, base::Microseconds(2)));
clock_.Advance(base::Microseconds(2));
EXPECT_THAT(
GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kMorning,
ScheduleCheckpoint::kLateAfternoon, base::Microseconds(3)));
clock_.Advance(base::Microseconds(3));
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kLateAfternoon,
ScheduleCheckpoint::kSunset, base::Microseconds(1)));
clock_.Advance(base::Microseconds(1));
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunset, _, _));
}
TEST_F(ScheduleUtilsTest, SunsetToSunriseSunriseExactlyEqualsSunset) {
// Sunrise and sunset are equal. In this case, there literally is no sunset
// or sunrise. The main thing is that this does not crash the device or cause
// the code to enter some bad unpredictable state.
const base::Time sunrise_time = BuildTime("00:00:00");
const base::Time sunset_time = sunrise_time;
AdvanceClockTo("00:00:00");
Position current_position = GetCurrentPosition(
clock_.Now(), sunset_time, sunrise_time, ScheduleType::kSunsetToSunrise);
EXPECT_EQ(current_position.current_checkpoint,
current_position.next_checkpoint);
EXPECT_EQ(current_position.time_until_next_checkpoint, base::Days(1));
AdvanceClockTo("12:00:00");
current_position = GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise);
EXPECT_EQ(current_position.current_checkpoint,
current_position.next_checkpoint);
EXPECT_EQ(current_position.time_until_next_checkpoint, base::Hours(12));
}
TEST_F(ScheduleUtilsTest, SunsetToSunriseNoSpaceForMorningAndLateAfternoon) {
// Sunrise and sunset are 1 microsecond apart. It's impossible to place
// morning and late afternoon between them since that's the smallest
// resolution of `base::Time`. In this case, morning and late afternoon
// should be omitted.
const base::Time sunrise_time = BuildTime("00:00:00");
const base::Time sunset_time = sunrise_time + base::Microseconds(1);
AdvanceClockTo("00:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunrise,
ScheduleCheckpoint::kSunset, base::Microseconds(1)));
clock_.Advance(base::Microseconds(1));
EXPECT_THAT(
GetCurrentPosition(clock_.Now(), sunset_time, sunrise_time,
ScheduleType::kSunsetToSunrise),
FieldsAre(ScheduleCheckpoint::kSunset, ScheduleCheckpoint::kSunrise,
base::Days(1) - base::Microseconds(1)));
}
TEST_F(ScheduleUtilsTest, CustomDetectsAllCheckpoints) {
// End: 6 AM
// Start: 6 PM
const base::Time end_time = BuildTime("06:00:00");
const base::Time start_time = BuildTime("18:00:00");
AdvanceClockTo("05:59:59");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), start_time, end_time,
ScheduleType::kCustom),
FieldsAre(ScheduleCheckpoint::kEnabled,
ScheduleCheckpoint::kDisabled, base::Seconds(1)));
AdvanceClockTo("06:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), start_time, end_time,
ScheduleType::kCustom),
FieldsAre(ScheduleCheckpoint::kDisabled,
ScheduleCheckpoint::kEnabled, base::Hours(12)));
AdvanceClockTo("17:59:59");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), start_time, end_time,
ScheduleType::kCustom),
FieldsAre(ScheduleCheckpoint::kDisabled,
ScheduleCheckpoint::kEnabled, base::Seconds(1)));
AdvanceClockTo("18:00:00");
EXPECT_THAT(GetCurrentPosition(clock_.Now(), start_time, end_time,
ScheduleType::kCustom),
FieldsAre(ScheduleCheckpoint::kEnabled,
ScheduleCheckpoint::kDisabled, base::Hours(12)));
}
TEST_F(ScheduleUtilsTest, SunsetToSunriseGetTimeUntilNextEvent) {
const base::Time origin = clock_.Now();
// Event time is same as now.
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin), origin);
// Event time is ahead of now.
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin + base::Hours(6)),
origin + base::Hours(6));
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin + base::Hours(18)),
origin + base::Hours(18));
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin + base::Days(1)), origin);
EXPECT_EQ(
ShiftWithinOneDayFrom(origin, origin + base::Days(1) + base::Hours(6)),
origin + base::Hours(6));
EXPECT_EQ(
ShiftWithinOneDayFrom(origin, origin + base::Days(1) + base::Hours(18)),
origin + base::Hours(18));
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin + base::Days(2)), origin);
// Event time is behind now.
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin - base::Hours(6)),
origin + base::Hours(18));
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin - base::Hours(18)),
origin + base::Hours(6));
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin - base::Days(1)), origin);
EXPECT_EQ(
ShiftWithinOneDayFrom(origin, origin - base::Days(1) - base::Hours(6)),
origin + base::Hours(18));
EXPECT_EQ(
ShiftWithinOneDayFrom(origin, origin - base::Days(1) - base::Hours(18)),
origin + base::Hours(6));
EXPECT_EQ(ShiftWithinOneDayFrom(origin, origin - base::Days(2)), origin);
}
} // namespace
} // namespace ash::schedule_utils