Commit c15329b4 authored by Hesen Zhang's avatar Hesen Zhang Committed by Commit Bot

Update notification utils.

- This CL creates utils for update notification service.
- Implemented and tested CalculateDeliverTime, which will
be used in building notification params.

Bug: 1013685
Change-Id: Ibdf465e9b1d0285c02bac8bca5ac602e2b048e01
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1947923Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Commit-Queue: Hesen Zhang <hesen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721815}
parent 7b0d8e54
......@@ -35,5 +35,6 @@ group("unit_tests") {
testonly = true
deps = [
"//chrome/browser/notifications/scheduler/internal:unit_tests",
"//chrome/browser/notifications/scheduler/public:unit_tests",
]
}
......@@ -15,27 +15,6 @@
namespace notifications {
bool ToLocalHour(int hour,
const base::Time& today,
int day_delta,
base::Time* out) {
DCHECK_GE(hour, 0);
DCHECK_LE(hour, 23);
DCHECK(out);
// Gets the local time at |hour| in yesterday.
base::Time another_day = today + base::TimeDelta::FromDays(day_delta);
base::Time::Exploded another_day_exploded;
another_day.LocalExplode(&another_day_exploded);
another_day_exploded.hour = hour;
another_day_exploded.minute = 0;
another_day_exploded.second = 0;
another_day_exploded.millisecond = 0;
// Converts local exploded time to time stamp.
return base::Time::FromLocalExploded(another_day_exploded, out);
}
int NotificationsShownToday(const ClientState* state, base::Clock* clock) {
std::map<SchedulerClientType, const ClientState*> client_states;
std::map<SchedulerClientType, int> shown_per_type;
......
......@@ -13,6 +13,7 @@
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_types.h"
#include "chrome/browser/notifications/scheduler/public/schedule_service_utils.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace notifications {
......@@ -20,17 +21,6 @@ namespace notifications {
struct ClientState;
struct SchedulerConfig;
// Retrieves the time stamp of a certain hour at a certain day from today.
// |hour| must be in the range of [0, 23].
// |today| is a timestamp to define today, usually caller can directly pass in
// the current system time.
// |day_delta| is the different between the output date and today.
// Returns false if the conversion is failed.
bool ToLocalHour(int hour,
const base::Time& today,
int day_delta,
base::Time* out);
// Calculates the notifications shown today from impression data.
void NotificationsShownToday(
const std::map<SchedulerClientType, const ClientState*>& client_states,
......
......@@ -32,6 +32,8 @@ source_set("public") {
"notification_scheduler_types.h",
"schedule_params.cc",
"schedule_params.h",
"schedule_service_utils.cc",
"schedule_service_utils.h",
"user_action_handler.h",
]
......@@ -49,3 +51,17 @@ if (is_android) {
]
}
}
source_set("unit_tests") {
testonly = true
sources = [
"schedule_service_utils_unittest.cc",
]
deps = [
"//chrome/browser/notifications/scheduler/public",
"//chrome/browser/notifications/scheduler/test:test_lib",
"//chrome/browser/notifications/scheduler/test:test_support",
"//testing/gtest",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// 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/public/schedule_service_utils.h"
namespace notifications {
namespace {
bool ValidateTimeWindow(const TimeDeltaPair& window) {
return (window.second - window.first < base::TimeDelta::FromHours(12) &&
window.second >= window.first);
}
} // namespace
bool ToLocalHour(int hour,
const base::Time& today,
int day_delta,
base::Time* out) {
DCHECK_GE(hour, 0);
DCHECK_LE(hour, 23);
DCHECK(out);
// Gets the local time at |hour| in yesterday.
base::Time another_day = today + base::TimeDelta::FromDays(day_delta);
base::Time::Exploded another_day_exploded;
another_day.LocalExplode(&another_day_exploded);
another_day_exploded.hour = hour;
another_day_exploded.minute = 0;
another_day_exploded.second = 0;
another_day_exploded.millisecond = 0;
// Converts local exploded time to time stamp.
return base::Time::FromLocalExploded(another_day_exploded, out);
}
bool NextTimeWindow(base::Clock* clock,
const TimeDeltaPair& morning,
const TimeDeltaPair& evening,
TimePair* out) {
auto now = clock->Now();
base::Time beginning_of_today;
// verify the inputs.
if (!ToLocalHour(0, now, 0, &beginning_of_today) ||
!ValidateTimeWindow(morning) || !ValidateTimeWindow(evening) ||
morning.second > evening.first) {
return false;
}
auto today_morning_window = std::pair<base::Time, base::Time>(
beginning_of_today + morning.first, beginning_of_today + morning.second);
if (now <= today_morning_window.second) {
*out = std::move(today_morning_window);
return true;
}
auto today_evening_window = std::pair<base::Time, base::Time>(
beginning_of_today + evening.first, beginning_of_today + evening.second);
if (now <= today_evening_window.second) {
*out = std::move(today_evening_window);
return true;
}
// tomorrow morning window.
*out = std::pair<base::Time, base::Time>(
beginning_of_today + base::TimeDelta::FromDays(1) + morning.first,
beginning_of_today + base::TimeDelta::FromDays(1) + morning.second);
return true;
}
} // namespace notifications
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_SCHEDULE_SERVICE_UTILS_H_
#define CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_SCHEDULE_SERVICE_UTILS_H_
#include <utility>
#include "base/optional.h"
#include "base/time/clock.h"
#include "base/time/time.h"
namespace notifications {
using TimePair = std::pair<base::Time, base::Time>;
using TimeDeltaPair = std::pair<base::TimeDelta, base::TimeDelta>;
// Given suggested |morning| and |evening| windows together with current time,
// calculate actual deliver time window from |out_start_time| to |out_end_time|.
// Both windows duration should be within 12 hours, and start of |evening|
// should be later than end of |morning|.
// Return false if the inputs are invalid.
// [begining_of_today,morning_window_end] => deliver on today morning.
// (morning_window_end, evening_window_end] => deliver on today evening.
// (evening_window_end, end_of_today] => deliver on tomorrow morning.
bool NextTimeWindow(base::Clock* clock,
const TimeDeltaPair& morning,
const TimeDeltaPair& evening,
TimePair* out);
// Retrieves the time stamp of a certain hour at a certain day from today.
// |hour| must be in the range of [0, 23].
// |today| is a timestamp to define today, usually caller can directly pass in
// the current system time.
// |day_delta| is the different between the output date and today.
// Returns false if the conversion is failed.
bool ToLocalHour(int hour,
const base::Time& today,
int day_delta,
base::Time* out);
} // namespace notifications
#endif // CHROME_BROWSER_NOTIFICATIONS_SCHEDULER_PUBLIC_SCHEDULE_SERVICE_UTILS_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// 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/public/schedule_service_utils.h"
#include <string>
#include <vector>
#include "chrome/browser/notifications/scheduler/test/fake_clock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace notifications {
namespace {
struct TestCaseInput {
// A list of fake current time inputs.
base::Time fake_now;
// Expected output data.
TimePair expected;
TestCaseInput(std::string fake_now_str,
std::string expected_window_start_str,
std::string expected_window_end_str) {
EXPECT_TRUE(base::Time::FromString(fake_now_str.c_str(), &this->fake_now));
EXPECT_TRUE(base::Time::FromString(expected_window_start_str.c_str(),
&this->expected.first));
EXPECT_TRUE(base::Time::FromString(expected_window_end_str.c_str(),
&this->expected.second));
}
};
// Assume fixed suggested time window, and try different faked current time to
// verify next actual deliver time window calculation.
TEST(NotificationScheduleServiceUtilsTest, NextTimeWindow) {
// Build test cases.
TimeDeltaPair morning_window_input = {base::TimeDelta::FromHours(5),
base::TimeDelta::FromHours(7)};
TimeDeltaPair evening_window_input = {base::TimeDelta::FromHours(18),
base::TimeDelta::FromHours(20)};
std::vector<TestCaseInput> test_cases = {
{"03/24/19 04:05:55 AM", "03/24/19 05:00:00 AM", "03/24/19 07:00:00 AM"},
{"03/24/19 11:45:22 AM", "03/24/19 06:00:00 PM", "03/24/19 08:00:00 PM"},
{"03/24/19 11:45:22 PM", "03/25/19 05:00:00 AM", "03/25/19 07:00:00 AM"},
};
// Run test cases.
for (size_t i = 0; i < test_cases.size(); i++) {
notifications::test::FakeClock clock;
clock.SetNow(test_cases[i].fake_now);
TimePair actual_output;
EXPECT_TRUE(NextTimeWindow(&clock, morning_window_input,
evening_window_input, &actual_output));
EXPECT_EQ(actual_output, test_cases[i].expected);
}
}
} // namespace
} // namespace notifications
......@@ -27,8 +27,10 @@ source_set("test_support") {
# Test library that is used internally and can't be exposed to the embedder.
source_set("test_lib") {
testonly = true
visibility =
[ "//chrome/browser/notifications/scheduler/internal:unit_tests" ]
visibility = [
"//chrome/browser/notifications/scheduler/internal:unit_tests",
"//chrome/browser/notifications/scheduler/public:unit_tests",
]
sources = [
"fake_clock.cc",
......
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