• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2018 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "update_engine/update_manager/weekly_time.h"
17 
18 #include <tuple>
19 
20 #include <base/time/time.h>
21 #include <gtest/gtest.h>
22 
23 using base::TimeDelta;
24 using std::tuple;
25 
26 namespace chromeos_update_manager {
27 
28 namespace {
29 
30 enum {
31   kSunday = 0,
32   kMonday,
33   kTuesday,
34   kWednesday,
35   kThursday,
36   kFriday,
37   kSaturday
38 };
39 
40 }  // namespace
41 
42 class WeeklyTimeDurationTest
43     : public testing::TestWithParam<tuple<int /* start_day_of_week */,
44                                           TimeDelta /* start_time */,
45                                           int /* end_day_of_week */,
46                                           TimeDelta /* end_time */,
47                                           TimeDelta /* expected result */>> {
48  protected:
start_day_of_week()49   int start_day_of_week() { return std::get<0>(GetParam()); }
start_time()50   TimeDelta start_time() { return std::get<1>(GetParam()); }
end_day_of_week()51   int end_day_of_week() { return std::get<2>(GetParam()); }
end_time()52   TimeDelta end_time() { return std::get<3>(GetParam()); }
result()53   TimeDelta result() { return std::get<4>(GetParam()); }
54 };
55 
TEST_P(WeeklyTimeDurationTest,GetDurationTo)56 TEST_P(WeeklyTimeDurationTest, GetDurationTo) {
57   WeeklyTime start = WeeklyTime(start_day_of_week(), start_time());
58   WeeklyTime end = WeeklyTime(end_day_of_week(), end_time());
59 
60   EXPECT_EQ(result(), start.GetDurationTo(end));
61 }
62 
63 INSTANTIATE_TEST_CASE_P(
64     SameMinutes,
65     WeeklyTimeDurationTest,
66     testing::Values(std::make_tuple(kThursday,
67                                     TimeDelta::FromMinutes(30),
68                                     kSaturday,
69                                     TimeDelta::FromMinutes(30),
70                                     TimeDelta::FromDays(2))));
71 
72 INSTANTIATE_TEST_CASE_P(
73     DifferentMinutes,
74     WeeklyTimeDurationTest,
75     testing::Values(std::make_tuple(kMonday,
76                                     TimeDelta::FromMinutes(10),
77                                     kWednesday,
78                                     TimeDelta::FromMinutes(30),
79                                     TimeDelta::FromDays(2) +
80                                         TimeDelta::FromMinutes(20))));
81 
82 INSTANTIATE_TEST_CASE_P(
83     EndLessThanStartSameMinutes,
84     WeeklyTimeDurationTest,
85     testing::Values(std::make_tuple(kSaturday,
86                                     TimeDelta::FromMinutes(100),
87                                     kTuesday,
88                                     TimeDelta::FromMinutes(100),
89                                     TimeDelta::FromDays(3))));
90 
91 INSTANTIATE_TEST_CASE_P(
92     EndLessThanStartDifferentMinutes,
93     WeeklyTimeDurationTest,
94     testing::Values(std::make_tuple(kSaturday,
95                                     TimeDelta::FromMinutes(150),
96                                     kMonday,
97                                     TimeDelta::FromMinutes(10),
98                                     TimeDelta::FromDays(2) -
99                                         TimeDelta::FromMinutes(140))));
100 
101 class WeeklyTimeOffsetTest
102     : public testing::TestWithParam<tuple<int /* day_of_week */,
103                                           TimeDelta /* time */,
104                                           TimeDelta /* offset */,
105                                           WeeklyTime /* expected result */>> {
106  protected:
day_of_week()107   int day_of_week() { return std::get<0>(GetParam()); }
time()108   TimeDelta time() { return std::get<1>(GetParam()); }
offset()109   TimeDelta offset() { return std::get<2>(GetParam()); }
result()110   WeeklyTime result() { return std::get<3>(GetParam()); }
111 };
112 
TEST_P(WeeklyTimeOffsetTest,WeekTimeAddTime)113 TEST_P(WeeklyTimeOffsetTest, WeekTimeAddTime) {
114   WeeklyTime test_time = WeeklyTime(day_of_week(), time());
115   test_time.AddTime(offset());
116 
117   EXPECT_EQ(result(), test_time);
118 }
119 
120 INSTANTIATE_TEST_CASE_P(
121     SameDayTest,
122     WeeklyTimeOffsetTest,
123     testing::Values(std::make_tuple(kTuesday,
124                                     TimeDelta::FromMinutes(200),
125                                     TimeDelta::FromMinutes(400),
126                                     WeeklyTime(kTuesday,
127                                                TimeDelta::FromMinutes(600)))));
128 
129 INSTANTIATE_TEST_CASE_P(DayChangeTest,
130                         WeeklyTimeOffsetTest,
131                         testing::Values(std::make_tuple(
132                             kThursday,
133                             TimeDelta::FromHours(23),
134                             TimeDelta::FromHours(2),
135                             WeeklyTime(kFriday, TimeDelta::FromHours(1)))));
136 
137 INSTANTIATE_TEST_CASE_P(DayChangeTestOver7,
138                         WeeklyTimeOffsetTest,
139                         testing::Values(std::make_tuple(
140                             kSunday,
141                             TimeDelta::FromHours(20),
142                             TimeDelta::FromDays(3),
143                             WeeklyTime(kWednesday, TimeDelta::FromHours(20)))));
144 
145 class WeeklyTimeIntervalRangeTest
146     : public testing::TestWithParam<tuple<int /* test_day_of_week */,
147                                           int /* test_time */,
148                                           bool /* in regular interval */,
149                                           bool /* in short interval */,
150                                           bool /* |start| < | */>> {
151  protected:
day_of_week()152   int day_of_week() { return std::get<0>(GetParam()); }
minutes()153   int minutes() { return std::get<1>(GetParam()); }
regular_result()154   bool regular_result() { return std::get<2>(GetParam()); }
short_result()155   bool short_result() { return std::get<3>(GetParam()); }
wraparound_result()156   bool wraparound_result() { return std::get<4>(GetParam()); }
157 };
158 
TEST_P(WeeklyTimeIntervalRangeTest,InRange)159 TEST_P(WeeklyTimeIntervalRangeTest, InRange) {
160   WeeklyTime test =
161       WeeklyTime(day_of_week(), TimeDelta::FromMinutes(minutes()));
162   WeeklyTimeInterval interval_regular =
163       WeeklyTimeInterval(WeeklyTime(kMonday, TimeDelta::FromMinutes(10)),
164                          WeeklyTime(kWednesday, TimeDelta::FromMinutes(30)));
165   WeeklyTimeInterval interval_short =
166       WeeklyTimeInterval(WeeklyTime(kThursday, TimeDelta::FromMinutes(10)),
167                          WeeklyTime(kThursday, TimeDelta::FromMinutes(11)));
168 
169   WeeklyTimeInterval interval_wraparound =
170       WeeklyTimeInterval(WeeklyTime(kFriday, TimeDelta::FromMinutes(10)),
171                          WeeklyTime(kTuesday, TimeDelta::FromMinutes(30)));
172 
173   EXPECT_EQ(regular_result(), interval_regular.InRange(test));
174   EXPECT_EQ(short_result(), interval_short.InRange(test));
175   EXPECT_EQ(wraparound_result(), interval_wraparound.InRange(test));
176 }
177 
178 // Test the left side of the range being inclusive.
179 INSTANTIATE_TEST_CASE_P(
180     InclusiveSuccessLeft,
181     WeeklyTimeIntervalRangeTest,
182     testing::Values(std::make_tuple(kThursday, 10, false, true, false)));
183 
184 // Test the right side of the range being exclusive.
185 INSTANTIATE_TEST_CASE_P(
186     ExclusiveSuccessRight,
187     WeeklyTimeIntervalRangeTest,
188     testing::Values(std::make_tuple(kThursday, 11, false, false, false)));
189 
190 // Test falling out of the interval by a small amount.
191 INSTANTIATE_TEST_CASE_P(
192     FailOutsideRangeSmall,
193     WeeklyTimeIntervalRangeTest,
194     testing::Values(std::make_tuple(kThursday, 12, false, false, false)));
195 
196 // These test cases check that intervals wrap around properly.
197 INSTANTIATE_TEST_CASE_P(
198     WraparoundOutside,
199     WeeklyTimeIntervalRangeTest,
200     testing::Values(std::make_tuple(kWednesday, 10, true, false, false)));
201 
202 INSTANTIATE_TEST_CASE_P(
203     WraparoundInsideRight,
204     WeeklyTimeIntervalRangeTest,
205     testing::Values(std::make_tuple(kSaturday, 10, false, false, true)));
206 
207 INSTANTIATE_TEST_CASE_P(
208     WraparoundInsideLeft,
209     WeeklyTimeIntervalRangeTest,
210     testing::Values(std::make_tuple(kMonday, 0, false, false, true)));
211 
212 }  // namespace chromeos_update_manager
213