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

ash / system / time / calendar_unittest_utils.cc [blame]

// Copyright 2021 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/time/calendar_unittest_utils.h"

#include <string>

#include "ash/ash_export.h"
#include "base/environment.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/time_formatting.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "google_apis/calendar/calendar_api_response_types.h"

namespace ash {

namespace calendar_test_utils {

ScopedLibcTimeZone::ScopedLibcTimeZone(const std::string& timezone) {
  auto env = base::Environment::Create();
  std::string old_timezone_value;
  if (env->GetVar(kTimeZoneEnvVarName, &old_timezone_value)) {
    old_timezone_ = old_timezone_value;
  }
  if (!env->SetVar(kTimeZoneEnvVarName, timezone)) {
    success_ = false;
  }
  tzset();
}

ScopedLibcTimeZone::~ScopedLibcTimeZone() {
  auto env = base::Environment::Create();
  if (old_timezone_.has_value()) {
    CHECK(env->SetVar(kTimeZoneEnvVarName, old_timezone_.value()));
  } else {
    CHECK(env->UnSetVar(kTimeZoneEnvVarName));
  }
}

std::unique_ptr<google_apis::calendar::SingleCalendar> CreateCalendar(
    const std::string& id,
    const std::string& summary,
    const std::string& color_id,
    bool selected,
    bool primary) {
  auto calendar = std::make_unique<google_apis::calendar::SingleCalendar>();
  calendar->set_id(id);
  calendar->set_summary(summary);
  calendar->set_color_id(color_id);
  calendar->set_selected(selected);
  calendar->set_primary(primary);
  return calendar;
}

std::unique_ptr<google_apis::calendar::CalendarList> CreateMockCalendarList(
    std::list<std::unique_ptr<google_apis::calendar::SingleCalendar>>
        calendars) {
  auto calendar_list = std::make_unique<google_apis::calendar::CalendarList>();

  for (auto& calendar : calendars) {
    calendar_list->InjectItemForTesting(std::move(calendar));
  }

  return calendar_list;
}

std::unique_ptr<google_apis::calendar::CalendarEvent> CreateEvent(
    const char* id,
    const char* summary,
    const char* start_time,
    const char* end_time,
    const google_apis::calendar::CalendarEvent::EventStatus event_status,
    const google_apis::calendar::CalendarEvent::ResponseStatus
        self_response_status,
    bool all_day_event,
    GURL video_conference_url) {
  auto event = std::make_unique<google_apis::calendar::CalendarEvent>();
  base::Time start_time_base, end_time_base;
  google_apis::calendar::DateTime start_time_date, end_time_date;
  event->set_id(id);
  event->set_summary(summary);
  bool result;
  if (all_day_event)
    result = base::Time::FromUTCString(start_time, &start_time_base);
  else
    result = base::Time::FromString(start_time, &start_time_base);
  DCHECK(result);
  if (all_day_event)
    result = base::Time::FromUTCString(end_time, &end_time_base);
  else
    result = base::Time::FromString(end_time, &end_time_base);
  DCHECK(result);
  start_time_date.set_date_time(start_time_base);
  end_time_date.set_date_time(end_time_base);
  event->set_start_time(start_time_date);
  event->set_end_time(end_time_date);
  event->set_status(event_status);
  event->set_self_response_status(self_response_status);
  event->set_all_day_event(all_day_event);
  event->set_conference_data_uri(video_conference_url);
  return event;
}

std::unique_ptr<google_apis::calendar::CalendarEvent> CreateEvent(
    const char* id,
    const char* summary,
    base::Time start_time,
    base::Time end_time,
    const google_apis::calendar::CalendarEvent::EventStatus event_status,
    const google_apis::calendar::CalendarEvent::ResponseStatus
        self_response_status,
    bool all_day_event,
    GURL video_conference_url) {
  auto event = std::make_unique<google_apis::calendar::CalendarEvent>();
  google_apis::calendar::DateTime start_time_date, end_time_date;
  event->set_id(id);
  event->set_summary(summary);
  start_time_date.set_date_time(start_time);
  end_time_date.set_date_time(end_time);
  event->set_start_time(start_time_date);
  event->set_end_time(end_time_date);
  event->set_status(event_status);
  event->set_self_response_status(self_response_status);
  event->set_all_day_event(all_day_event);
  event->set_conference_data_uri(video_conference_url);
  return event;
}

std::unique_ptr<google_apis::calendar::EventList> CreateMockEventList(
    std::list<std::unique_ptr<google_apis::calendar::CalendarEvent>> events) {
  auto event_list = std::make_unique<google_apis::calendar::EventList>();
  event_list->set_time_zone("Greenwich Mean Time");

  for (auto& event : events)
    event_list->InjectItemForTesting(std::move(event));

  return event_list;
}

ASH_EXPORT bool IsTheSameMonth(const base::Time date_a,
                               const base::Time date_b) {
  return base::UnlocalizedTimeFormatWithPattern(date_a, "MM YYYY") ==
         base::UnlocalizedTimeFormatWithPattern(date_b, "MM YYYY");
}

base::Time GetTimeFromString(const char* start_time) {
  base::Time date;
  bool result = base::Time::FromString(start_time, &date);
  DCHECK(result);
  return date;
}

CalendarClientTestImpl::CalendarClientTestImpl() = default;

CalendarClientTestImpl::~CalendarClientTestImpl() = default;

bool CalendarClientTestImpl::IsDisabledByAdmin() const {
  return is_disabled_by_admin_;
}

base::OnceClosure CalendarClientTestImpl::GetCalendarList(
    google_apis::calendar::CalendarListCallback callback) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), error_, std::move(calendars_)),
      task_delay_);

  return base::DoNothing();
}

base::OnceClosure CalendarClientTestImpl::GetEventList(
    google_apis::calendar::CalendarEventListCallback callback,
    const base::Time start_time,
    const base::Time end_time) {
  // Give it a little bit of time to mock the api calling. This duration is a
  // little longer than the settle down duration, so in the test after the
  // animation settled down it can still be with `kFetching` status until
  // somemethod like `WaitUntilFetched` is called.
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), error_, std::move(events_)),
      task_delay_);

  return base::DoNothing();
}

base::OnceClosure CalendarClientTestImpl::GetEventList(
    google_apis::calendar::CalendarEventListCallback callback,
    const base::Time start_time,
    const base::Time end_time,
    const std::string& calendar_id,
    const std::string& calendar_color_id) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), error_, std::move(events_)),
      task_delay_);

  return base::DoNothing();
}

void CalendarClientTestImpl::SetCalendarList(
    std::unique_ptr<google_apis::calendar::CalendarList> calendars) {
  calendars_ = std::move(calendars);
}

void CalendarClientTestImpl::SetEventList(
    std::unique_ptr<google_apis::calendar::EventList> events) {
  events_.reset();
  events_ = std::move(events);
}

}  // namespace calendar_test_utils

}  // namespace ash