Commit c234788e authored by Henrique Grandinetti's avatar Henrique Grandinetti Committed by Commit Bot

Fix WeekdayShift to use the modulo function.

Bug: 900823
Change-Id: I1115d5731f19cf4e5b1ef545741b0d75ea0da062
Reviewed-on: https://chromium-review.googlesource.com/c/1312908
Commit-Queue: Henrique Grandinetti <hgrandinetti@chromium.org>
Reviewed-by: default avatarMichael Giuffrida <michaelpg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605430}
parent 57247968
...@@ -49,11 +49,19 @@ bool IsBefore(base::Time a, base::Time b) { ...@@ -49,11 +49,19 @@ bool IsBefore(base::Time a, base::Time b) {
return b.is_null() || a < b; return b.is_null() || a < b;
} }
// Implements the modulo operation. E.g. modulo(10, 7) == 3, modulo(-1, 7) == 6.
int Modulo(int dividend, int divisor) {
int remainder = dividend % divisor;
if (remainder < 0)
remainder += divisor;
return remainder;
}
// Shifts the current weekday, if the value is positive shifts forward and if // Shifts the current weekday, if the value is positive shifts forward and if
// negative backwards. // negative backwards.
Weekday WeekdayShift(Weekday current_day, int shift) { Weekday WeekdayShift(Weekday current_day, int shift) {
return static_cast<Weekday>((static_cast<int>(current_day) + shift) % return static_cast<Weekday>(Modulo(static_cast<int>(current_day) + shift,
static_cast<int>(Weekday::kCount)); static_cast<int>(Weekday::kCount)));
} }
// Returns usage limit reset time or default value if |time_usage_limit| is // Returns usage limit reset time or default value if |time_usage_limit| is
......
...@@ -878,6 +878,141 @@ TEST_F(UsageTimeLimitProcessorTest, GetStateDefaultBedtime) { ...@@ -878,6 +878,141 @@ TEST_F(UsageTimeLimitProcessorTest, GetStateDefaultBedtime) {
AssertEqState(expected_state_one, state_one); AssertEqState(expected_state_one, state_one);
} }
// Test if an overnight time window limit applies to the following day.
TEST_F(UsageTimeLimitProcessorTest, GetStateWithPreviousDayTimeWindowLimit) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("GMT"));
// Setup time window limit.
std::string last_updated = CreatePolicyTimestamp("1 Jan 2018 8:00 GMT");
base::Value saturday_time_limit =
CreateTimeWindow(base::Value("SATURDAY"), CreateTime(21, 0),
CreateTime(8, 30), base::Value(last_updated));
base::Value window_limit_entries(base::Value::Type::LIST);
window_limit_entries.GetList().push_back(std::move(saturday_time_limit));
base::Value time_window_limit(base::Value::Type::DICTIONARY);
time_window_limit.SetKey("entries", std::move(window_limit_entries));
// Setup time usage limit.
base::Value time_usage_limit = base::Value(base::Value::Type::DICTIONARY);
time_usage_limit.SetKey("reset_at", CreateTime(8, 0));
// Setup policy.
std::unique_ptr<base::Value> time_limit =
std::make_unique<base::Value>(base::Value::Type::DICTIONARY);
time_limit->SetKey("time_window_limit", std::move(time_window_limit));
time_limit->SetKey("time_usage_limit", std::move(time_usage_limit));
std::unique_ptr<base::DictionaryValue> time_limit_dictionary =
base::DictionaryValue::From(std::move(time_limit));
// Check that device is locked.
base::Time time_one = TimeFromString("Sun, 7 Jan 2018 8:00 GMT");
State state_one =
GetState(time_limit_dictionary, base::TimeDelta::FromMinutes(80),
time_one, time_one, timezone.get(), base::nullopt);
State expected_state_one;
expected_state_one.is_locked = true;
expected_state_one.active_policy = ActivePolicies::kFixedLimit;
expected_state_one.is_time_usage_limit_enabled = false;
expected_state_one.next_state_change_time =
TimeFromString("Sun, 7 Jan 2018 8:30 GMT");
expected_state_one.next_state_active_policy = ActivePolicies::kNoActivePolicy;
expected_state_one.next_unlock_time =
TimeFromString("Sun, 7 Jan 2018 8:30 GMT");
expected_state_one.last_state_changed = base::Time();
AssertEqState(expected_state_one, state_one);
}
// Test if a time usage limit applies to the morning of the following day.
TEST_F(UsageTimeLimitProcessorTest, GetStateWithPreviousDayTimeUsageLimit) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("GMT"));
// Setup time usage limit.
std::string last_updated = CreatePolicyTimestamp("1 Jan 2018 8:00 GMT");
base::Value saturday_time_usage =
CreateTimeUsage(base::Value(120), base::Value(last_updated));
base::Value time_usage_limit = base::Value(base::Value::Type::DICTIONARY);
time_usage_limit.SetKey("saturday", std::move(saturday_time_usage));
time_usage_limit.SetKey("reset_at", CreateTime(6, 0));
// Setup policy.
std::unique_ptr<base::Value> time_limit =
std::make_unique<base::Value>(base::Value::Type::DICTIONARY);
time_limit->SetKey("time_usage_limit", std::move(time_usage_limit));
std::unique_ptr<base::DictionaryValue> time_limit_dictionary =
base::DictionaryValue::From(std::move(time_limit));
// Check that device is locked.
base::Time time_one = TimeFromString("Sun, 7 Jan 2018 4:00 GMT");
State state_one =
GetState(time_limit_dictionary, base::TimeDelta::FromMinutes(120),
time_one, time_one, timezone.get(), base::nullopt);
State expected_state_one;
expected_state_one.is_locked = true;
expected_state_one.active_policy = ActivePolicies::kUsageLimit;
expected_state_one.is_time_usage_limit_enabled = true;
expected_state_one.remaining_usage = base::TimeDelta::FromMinutes(0);
expected_state_one.time_usage_limit_started = time_one;
expected_state_one.next_state_change_time =
TimeFromString("Sun, 7 Jan 2018 6:00 GMT");
expected_state_one.next_state_active_policy = ActivePolicies::kNoActivePolicy;
expected_state_one.next_unlock_time =
TimeFromString("Sun, 7 Jan 2018 6:00 GMT");
expected_state_one.last_state_changed = base::Time();
AssertEqState(expected_state_one, state_one);
}
// Test if a time usage limit applies to the current night.
TEST_F(UsageTimeLimitProcessorTest, GetStateWithWeekendTimeUsageLimit) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("PST"));
// Setup time usage limit.
std::string last_updated = CreatePolicyTimestamp("1 Jan 2018 8:00 PST");
base::Value saturday_time_usage =
CreateTimeUsage(base::Value(120), base::Value(last_updated));
base::Value time_usage_limit = base::Value(base::Value::Type::DICTIONARY);
time_usage_limit.SetKey("saturday", std::move(saturday_time_usage));
time_usage_limit.SetKey("reset_at", CreateTime(6, 0));
// Setup policy.
std::unique_ptr<base::Value> time_limit =
std::make_unique<base::Value>(base::Value::Type::DICTIONARY);
time_limit->SetKey("time_usage_limit", std::move(time_usage_limit));
std::unique_ptr<base::DictionaryValue> time_limit_dictionary =
base::DictionaryValue::From(std::move(time_limit));
// Check that device is locked.
base::Time time_one = TimeFromString("Sat, 6 Jan 2018 20:00 PST");
State state_one =
GetState(time_limit_dictionary, base::TimeDelta::FromMinutes(120),
time_one, time_one, timezone.get(), base::nullopt);
State expected_state_one;
expected_state_one.is_locked = true;
expected_state_one.active_policy = ActivePolicies::kUsageLimit;
expected_state_one.is_time_usage_limit_enabled = true;
expected_state_one.remaining_usage = base::TimeDelta::FromMinutes(0);
expected_state_one.time_usage_limit_started = time_one;
expected_state_one.next_state_change_time =
TimeFromString("Sun, 7 Jan 2018 6:00 PST");
expected_state_one.next_state_active_policy = ActivePolicies::kNoActivePolicy;
expected_state_one.next_unlock_time =
TimeFromString("Sun, 7 Jan 2018 6:00 PST");
expected_state_one.last_state_changed = base::Time();
AssertEqState(expected_state_one, state_one);
}
// Test GetExpectedResetTime with an empty policy. // Test GetExpectedResetTime with an empty policy.
TEST_F(UsageTimeLimitProcessorTest, GetExpectedResetTimeWithEmptyPolicy) { TEST_F(UsageTimeLimitProcessorTest, GetExpectedResetTimeWithEmptyPolicy) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("GMT")); std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone("GMT"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment