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
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364

ash / system / time / calendar_unittest_utils.h [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.

#ifndef ASH_SYSTEM_TIME_CALENDAR_UNITTEST_UTILS_H_
#define ASH_SYSTEM_TIME_CALENDAR_UNITTEST_UTILS_H_

#include <list>
#include <memory>
#include <set>
#include <string>

#include "ash/calendar/calendar_client.h"
#include "ash/system/time/calendar_utils.h"
#include "base/time/time.h"
#include "google_apis/calendar/calendar_api_response_types.h"

namespace ash {

namespace {
// This list is from "chromeos/ash/components/settings/timezone_settings.cc".
const char* kAllTimeZones[] = {"Pacific/Midway",
                               "Pacific/Honolulu",
                               "America/Anchorage",
                               "America/Los_Angeles",
                               "America/Vancouver",
                               "America/Tijuana",
                               "America/Phoenix",
                               "America/Chihuahua",
                               "America/Denver",
                               "America/Edmonton",
                               "America/Mazatlan",
                               "America/Regina",
                               "America/Costa_Rica",
                               "America/Chicago",
                               "America/Mexico_City",
                               "America/Tegucigalpa",
                               "America/Winnipeg",
                               "Pacific/Easter",
                               "America/Bogota",
                               "America/Lima",
                               "America/New_York",
                               "America/Toronto",
                               "America/Caracas",
                               "America/Barbados",
                               "America/Halifax",
                               "America/Manaus",
                               "America/Santiago",
                               "America/St_Johns",
                               "America/Araguaina",
                               "America/Argentina/Buenos_Aires",
                               "America/Argentina/San_Luis",
                               "America/Montevideo",
                               "America/Santiago",
                               "America/Sao_Paulo",
                               "America/Godthab",
                               "Atlantic/South_Georgia",
                               "Atlantic/Cape_Verde",
                               "Etc/GMT",
                               "Atlantic/Azores",
                               "Atlantic/Reykjavik",
                               "Atlantic/St_Helena",
                               "Africa/Casablanca",
                               "Atlantic/Faroe",
                               "Europe/Dublin",
                               "Europe/Lisbon",
                               "Europe/London",
                               "Europe/Amsterdam",
                               "Europe/Belgrade",
                               "Europe/Berlin",
                               "Europe/Bratislava",
                               "Europe/Brussels",
                               "Europe/Budapest",
                               "Europe/Copenhagen",
                               "Europe/Ljubljana",
                               "Europe/Madrid",
                               "Europe/Malta",
                               "Europe/Oslo",
                               "Europe/Paris",
                               "Europe/Prague",
                               "Europe/Rome",
                               "Europe/Stockholm",
                               "Europe/Sarajevo",
                               "Europe/Tirane",
                               "Europe/Vaduz",
                               "Europe/Vienna",
                               "Europe/Warsaw",
                               "Europe/Zagreb",
                               "Europe/Zurich",
                               "Africa/Windhoek",
                               "Africa/Lagos",
                               "Africa/Brazzaville",
                               "Africa/Cairo",
                               "Africa/Harare",
                               "Africa/Maputo",
                               "Africa/Johannesburg",
                               "Europe/Kaliningrad",
                               "Europe/Athens",
                               "Europe/Bucharest",
                               "Europe/Chisinau",
                               "Europe/Helsinki",
                               "Europe/Istanbul",
                               "Europe/Kiev",
                               "Europe/Riga",
                               "Europe/Sofia",
                               "Europe/Tallinn",
                               "Europe/Vilnius",
                               "Asia/Amman",
                               "Asia/Beirut",
                               "Asia/Jerusalem",
                               "Africa/Nairobi",
                               "Asia/Baghdad",
                               "Asia/Riyadh",
                               "Asia/Kuwait",
                               "Europe/Minsk",
                               "Europe/Moscow",
                               "Asia/Tehran",
                               "Europe/Samara",
                               "Asia/Dubai",
                               "Asia/Tbilisi",
                               "Indian/Mauritius",
                               "Asia/Baku",
                               "Asia/Yerevan",
                               "Asia/Kabul",
                               "Asia/Karachi",
                               "Asia/Aqtobe",
                               "Asia/Ashgabat",
                               "Asia/Oral",
                               "Asia/Yekaterinburg",
                               "Asia/Calcutta",
                               "Asia/Colombo",
                               "Asia/Katmandu",
                               "Asia/Omsk",
                               "Asia/Almaty",
                               "Asia/Dhaka",
                               "Asia/Novosibirsk",
                               "Asia/Rangoon",
                               "Asia/Bangkok",
                               "Asia/Jakarta",
                               "Asia/Krasnoyarsk",
                               "Asia/Novokuznetsk",
                               "Asia/Ho_Chi_Minh",
                               "Asia/Phnom_Penh",
                               "Asia/Vientiane",
                               "Asia/Shanghai",
                               "Asia/Hong_Kong",
                               "Asia/Kuala_Lumpur",
                               "Asia/Singapore",
                               "Asia/Manila",
                               "Asia/Taipei",
                               "Asia/Ulaanbaatar",
                               "Asia/Makassar",
                               "Asia/Irkutsk",
                               "Asia/Yakutsk",
                               "Australia/Perth",
                               "Australia/Eucla",
                               "Asia/Seoul",
                               "Asia/Tokyo",
                               "Asia/Jayapura",
                               "Asia/Sakhalin",
                               "Asia/Vladivostok",
                               "Asia/Magadan",
                               "Australia/Darwin",
                               "Australia/Adelaide",
                               "Pacific/Guam",
                               "Australia/Brisbane",
                               "Australia/Hobart",
                               "Australia/Sydney",
                               "Asia/Anadyr",
                               "Pacific/Port_Moresby",
                               "Asia/Kamchatka",
                               "Pacific/Fiji",
                               "Pacific/Majuro",
                               "Pacific/Auckland",
                               "Pacific/Tongatapu",
                               "Pacific/Apia",
                               "Pacific/Kiritimati"};

// These are from "third_party/fontconfig/include/fc-lang/fclang.h" data.
// Locales "und-zmth" and "und-zsye" are omitted since they cannot be set.
const char* kLocales[] = {
    "aa",     "ab",  "af",    "ak",    "am",    "an",    "ar",    "as",
    "ast",    "av",  "ay",    "az-az", "az-ir", "ba",    "be",    "ber-dz",
    "ber-ma", "bg",  "bh",    "bho",   "bi",    "bin",   "bm",    "bn",
    "bo",     "br",  "brx",   "bs",    "bua",   "byn",   "ca",    "ce",
    "ch",     "chm", "chr",   "co",    "crh",   "cs",    "csb",   "cu",
    "cv",     "cy",  "da",    "de",    "doi",   "dv",    "dz",    "ee",
    "el",     "en",  "eo",    "es",    "et",    "eu",    "fa",    "fat",
    "ff",     "fi",  "fil",   "fj",    "fo",    "fr",    "fur",   "fy",
    "ga",     "gd",  "gez",   "gl",    "gn",    "gu",    "gv",    "ha",
    "haw",    "he",  "hi",    "hne",   "ho",    "hr",    "ht",    "hu",
    "hy",     "hz",  "ia",    "id",    "ie",    "ig",    "ii",    "ik",
    "io",     "is",  "it",    "iu",    "ja",    "jv",    "ka",    "kaa",
    "kab",    "ki",  "kj",    "kk",    "kl",    "km",    "kn",    "ko",
    "kok",    "kr",  "ks",    "ku-am", "ku-iq", "ku-ir", "ku-tr", "kum",
    "kv",     "kw",  "kwm",   "ky",    "la",    "lah",   "lb",    "lez",
    "lg",     "li",  "ln",    "lo",    "lt",    "lv",    "mai",   "mg",
    "mh",     "mi",  "mk",    "ml",    "mn-cn", "mn-mn", "mni",   "mo",
    "mr",     "ms",  "mt",    "my",    "na",    "nb",    "nds",   "ne",
    "ng",     "nl",  "nn",    "no",    "nr",    "nso",   "nv",    "ny",
    "oc",     "om",  "or",    "os",    "ota",   "pa",    "pa-pk", "pap-an",
    "pap-aw", "pl",  "ps-af", "ps-pk", "pt",    "qu",    "quz",   "rm",
    "rn",     "ro",  "ru",    "rw",    "sa",    "sah",   "sat",   "sc",
    "sco",    "sd",  "se",    "sel",   "sg",    "sh",    "shs",   "si",
    "sid",    "sk",  "sl",    "sm",    "sma",   "smj",   "smn",   "sms",
    "sn",     "so",  "sq",    "sr",    "ss",    "st",    "su",    "sv",
    "sw",     "syr", "ta",    "te",    "tg",    "th",    "ti-er", "ti-et",
    "tig",    "tk",  "tl",    "tn",    "to",    "tr",    "ts",    "tt",
    "tw",     "ty",  "tyv",   "ug",    "uk",    "ur",    "uz",    "ve",
    "vi",     "vo",  "vot",   "wa",    "wal",   "wen",   "wo",    "xh",
    "yap",    "yi",  "yo",    "za",    "zh-cn", "zh-hk", "zh-mo", "zh-sg",
    "zh-tw",  "zu"};

std::set<std::string> kLocalesWithUniqueNumerals{"bn", "fa", "mr", "pa-pk"};

}  // namespace

namespace calendar_test_utils {

// Used for over-riding the locale that `base::Time` uses. Copied from
// `time_unittest.cc`.
class ScopedLibcTimeZone {
 public:
  explicit ScopedLibcTimeZone(const std::string& timezone);
  ~ScopedLibcTimeZone();

  ScopedLibcTimeZone(const ScopedLibcTimeZone& other) = delete;
  ScopedLibcTimeZone& operator=(const ScopedLibcTimeZone& other) = delete;

  bool is_success() const { return success_; }

 private:
  static constexpr char kTimeZoneEnvVarName[] = "TZ";

  bool success_ = true;
  std::optional<std::string> old_timezone_;
};

// A duration to let the animation finish and pass the cool down duration in
// tests.
constexpr base::TimeDelta kAnimationSettleDownDuration = base::Seconds(3);

// A duration which is smaller than any of the animation duration. So if there's
// an animation, the view should be in the middle of the animation.
constexpr base::TimeDelta kAnimationStartBufferDuration =
    base::Milliseconds(90);

// Creates a `google_apis::calendar::SingleCalendar` for testing only.
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);

std::unique_ptr<google_apis::calendar::CalendarList> CreateMockCalendarList(
    std::list<std::unique_ptr<google_apis::calendar::SingleCalendar>>
        calendars);

// Creates a `google_apis::calendar::CalendarEvent` for testing, that converts
// start/end time strings to `google_apis::calendar::DateTime`.
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 =
        google_apis::calendar::CalendarEvent::EventStatus::kConfirmed,
    const google_apis::calendar::CalendarEvent::ResponseStatus
        self_response_status =
            google_apis::calendar::CalendarEvent::ResponseStatus::kAccepted,
    bool all_day_event = false,
    GURL video_conference_url = GURL());

// Creates a `google_apis::calendar::CalendarEvent` for testing, that converts
// start/end `base::Time` objects to `google_apis::calendar::DateTime`.
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 =
        google_apis::calendar::CalendarEvent::EventStatus::kConfirmed,
    const google_apis::calendar::CalendarEvent::ResponseStatus
        self_response_status =
            google_apis::calendar::CalendarEvent::ResponseStatus::kAccepted,
    bool all_day_event = false,
    GURL video_conference_url = GURL());

std::unique_ptr<google_apis::calendar::EventList> CreateMockEventList(
    std::list<std::unique_ptr<google_apis::calendar::CalendarEvent>> events);

// Checks if the two exploded are in the same month.
bool IsTheSameMonth(const base::Time date_a, const base::Time date_b);

// Returns the `base:Time` from the given string.
base::Time GetTimeFromString(const char* start_time);

// A mock `CalendarClient` which uses `SetEventList` and `SetError` to set the
// response. This mock client's `GetEventList` waits for a short duration to
// mock the fetching process. This client can be used in calendar unittests to
// mock the process of fetching events and getting back event list or error
// message. It needs to be registered to a calendar client in `SetUp`.
class CalendarClientTestImpl : public CalendarClient {
 public:
  CalendarClientTestImpl();
  CalendarClientTestImpl(const CalendarClientTestImpl& other) = delete;
  CalendarClientTestImpl& operator=(const CalendarClientTestImpl& other) =
      delete;
  ~CalendarClientTestImpl() override;

  void set_is_disabled_by_admin(bool is_disabled_by_admin) {
    is_disabled_by_admin_ = is_disabled_by_admin;
  }

  // CalendarClient:
  bool IsDisabledByAdmin() const override;
  base::OnceClosure GetCalendarList(
      google_apis::calendar::CalendarListCallback callback) override;
  base::OnceClosure GetEventList(
      google_apis::calendar::CalendarEventListCallback callback,
      const base::Time start_time,
      const base::Time end_time) override;
  base::OnceClosure 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) override;

  // Sets `calendars` as the fetched calendar list.
  void SetCalendarList(
      std::unique_ptr<google_apis::calendar::CalendarList> calendars);

  // Sets `events` as the fetched event list.
  void SetEventList(std::unique_ptr<google_apis::calendar::EventList> events);

  // Sets `error` as the error message. The value of `error` is
  // `google_apis::HTTP_SUCCESS` by default.
  void SetError(google_apis::ApiErrorCode error) { error_ = error; }

  // Sets `delay` as the response delay. By default, the response delay is
  // `kAnimationSettleDownDuration` plus 2 seconds.
  void SetResponseDelay(const base::TimeDelta delay) { task_delay_ = delay; }

  // Force the task to take longer than the default timeout, causing an internal
  // error to be propagated.
  void ForceTimeout() {
    task_delay_ = calendar_utils::kCalendarDataFetchTimeout + base::Seconds(1);
  }

 private:
  bool is_disabled_by_admin_ = false;
  google_apis::ApiErrorCode error_ = google_apis::HTTP_SUCCESS;
  std::unique_ptr<google_apis::calendar::CalendarList> calendars_;
  std::unique_ptr<google_apis::calendar::EventList> events_;
  base::TimeDelta task_delay_ = kAnimationSettleDownDuration + base::Seconds(2);
};

}  // namespace calendar_test_utils

}  // namespace ash

#endif  // ASH_SYSTEM_TIME_CALENDAR_UNITTEST_UTILS_H_