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
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487

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

// Copyright 2022 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_event_fetch.h"

#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <utility>

#include "ash/calendar/calendar_client.h"
#include "ash/calendar/calendar_controller.h"
#include "ash/shell.h"
#include "ash/system/power/peripheral_battery_listener.h"
#include "ash/system/time/calendar_event_fetch_types.h"
#include "ash/system/time/calendar_unittest_utils.h"
#include "ash/system/time/calendar_utils.h"
#include "ash/test/ash_test_base.h"
#include "ash/test_shell_delegate.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/account_id/account_id.h"
#include "google_apis/common/api_error_codes.h"

namespace ash {

// Subclass of CalendarClient where we control the results of a call to
// GetEventList(), which is what's being called downstream when a
// CalendarEventFetch is instantiated.
class TestCalendarClient : public CalendarClient {
 public:
  TestCalendarClient() = default;
  TestCalendarClient(const TestCalendarClient& other) = delete;
  TestCalendarClient& operator=(const TestCalendarClient& other) = delete;
  ~TestCalendarClient() override = default;

  bool IsDisabledByAdmin() const override { return false; }

  base::OnceClosure GetCalendarList(
      google_apis::calendar::CalendarListCallback callback) override {
    // This method should not be used during the event fetch unit test.
    return base::DoNothing();
  }

  base::OnceClosure GetEventList(
      google_apis::calendar::CalendarEventListCallback callback,
      const base::Time start_time,
      const base::Time end_time) override {
    // Store these off.
    start_time_ = start_time;
    callback_ = std::move(callback);

    // By delaying the response, we make the unit tests behave a little more
    // like a event fetch in production.  Use set_response_delay() to use a
    // value that's different from the default.
    StartResponseDelayTimeout();
    return base::BindOnce(&TestCalendarClient::CancelCallback,
                          weak_factory_.GetWeakPtr());
  }

  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 {
    // Store these off.
    start_time_ = start_time;
    callback_ = std::move(callback);
    calendar_id_ = calendar_id;

    // By delaying the response, we make the unit tests behave a little more
    // like a event fetch in production.  Use set_response_delay() to use a
    // value that's different from the default.
    StartResponseDelayTimeout();
    return base::BindOnce(&TestCalendarClient::CancelCallback,
                          weak_factory_.GetWeakPtr());
  }

  void CancelCallback() { set_api_error_code(google_apis::CANCELLED); }

  void set_event_list(
      std::unique_ptr<google_apis::calendar::EventList> event_list) {
    event_list_ = std::move(event_list);
  }
  void set_api_error_code(google_apis::ApiErrorCode api_error_code) {
    api_error_code_ = api_error_code;
  }
  void set_response_delay(const base::TimeDelta& delay) {
    response_delay_ = delay;
  }
  const base::TimeDelta get_response_delay() { return response_delay_; }

 private:
  void StartResponseDelayTimeout() {
    fetch_response_timeout_.Start(
        FROM_HERE, response_delay_,
        base::BindRepeating(&TestCalendarClient::OnResponseDelayTimeout,
                            base::Unretained(this)));
  }

  void OnResponseDelayTimeout() {
    // We send back an event list, possibly empty, with every response.
    auto requested_event_list =
        std::make_unique<google_apis::calendar::EventList>();
    requested_event_list->set_time_zone("Greenwich Mean Time");

    // If we're set to return an error.
    if (api_error_code_ != google_apis::HTTP_SUCCESS) {
      std::move(callback_).Run(api_error_code_,
                               std::move(requested_event_list));
      return;
    }

    // If we have some events, send back any that start in the month we
    // requested.
    if (event_list_) {
      for (auto& event : event_list_->items()) {
        if (calendar_test_utils::IsTheSameMonth(event->start_time().date_time(),
                                                start_time_)) {
          requested_event_list->InjectItemForTesting(
              calendar_test_utils::CreateEvent(event->id().c_str(),
                                               event->summary().c_str(),
                                               event->start_time().date_time(),
                                               event->end_time().date_time()));
        }
      }
    }

    std::move(callback_).Run(api_error_code_, std::move(requested_event_list));
  }

  google_apis::calendar::CalendarEventListCallback callback_;
  base::Time start_time_;
  std::optional<std::string> calendar_id_;
  std::unique_ptr<google_apis::calendar::EventList> event_list_;
  google_apis::ApiErrorCode api_error_code_ = google_apis::HTTP_SUCCESS;
  base::RetainingOneShotTimer fetch_response_timeout_;
  base::TimeDelta response_delay_ = base::Milliseconds(100);

  base::WeakPtrFactory<TestCalendarClient> weak_factory_{this};
};

class CalendarEventFetchTest : public NoSessionAshTestBase {
 public:
  CalendarEventFetchTest()
      : NoSessionAshTestBase(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  CalendarEventFetchTest(const CalendarEventFetchTest& other) = delete;
  CalendarEventFetchTest& operator=(const CalendarEventFetchTest& other) =
      delete;
  ~CalendarEventFetchTest() override = default;

  void RegisterClient() {
    DCHECK(Shell::HasInstance());
    Shell::Get()->calendar_controller()->SetActiveUserAccountIdForTesting(
        GetDefaultUserId());
    Shell::Get()->calendar_controller()->RegisterClientForUser(
        GetDefaultUserId(),
        /*client=*/&client_);
  }

  // Actual callback invoked when an event fetch is complete.
  void OnEventsFetched(base::Time start_of_month,
                       std::string calendar_id,
                       google_apis::ApiErrorCode error,
                       const google_apis::calendar::EventList* events) {
    calendar_id_ = calendar_id;
    api_error_code_ = error;
    events_fetched_count_ = 0;
    if (events)
      events_fetched_count_ = events->items().size();
  }

  // Callback invoked when an event fetch failed with an internal error.
  void OnEventFetchFailedInternalError(
      base::Time start_of_month,
      std::string calendar_id,
      CalendarEventFetchInternalErrorCode error) {
    calendar_id_ = calendar_id;
    internal_error_code_ = error;
  }

  base::Time GetStartOfMonthFromString(const char* str) {
    base::Time start_of_month;
    bool result = base::Time::FromString(str, &start_of_month);
    DCHECK(result);
    return start_of_month.UTCMidnight();
  }

  std::unique_ptr<CalendarEventFetch> PerformFetch(
      const base::Time start_of_month) {
    std::unique_ptr<CalendarEventFetch> fetch =
        std::make_unique<CalendarEventFetch>(
            start_of_month,
            base::BindRepeating(&CalendarEventFetchTest::OnEventsFetched,
                                base::Unretained(this)),
            base::BindRepeating(
                &CalendarEventFetchTest::OnEventFetchFailedInternalError,
                base::Unretained(this)),
            task_environment()->GetMockTickClock());
    return fetch;
  }

  std::unique_ptr<CalendarEventFetch> PerformFetchByCalendarId(
      const base::Time start_of_month,
      const std::string calendar_id,
      const std::string calendar_color_id) {
    std::unique_ptr<CalendarEventFetch> fetch =
        std::make_unique<CalendarEventFetch>(
            start_of_month,
            base::BindRepeating(&CalendarEventFetchTest::OnEventsFetched,
                                base::Unretained(this)),
            base::BindRepeating(
                &CalendarEventFetchTest::OnEventFetchFailedInternalError,
                base::Unretained(this)),
            task_environment()->GetMockTickClock(), calendar_id,
            calendar_color_id);
    return fetch;
  }

  std::optional<std::string> get_calendar_id() { return calendar_id_; }
  std::optional<int> events_fetched_count() { return events_fetched_count_; }
  std::optional<google_apis::ApiErrorCode> api_error_code() {
    return api_error_code_;
  }
  std::optional<CalendarEventFetchInternalErrorCode> internal_error_code() {
    return internal_error_code_;
  }

 protected:
  TestCalendarClient client_;

 private:
  const AccountId GetDefaultUserId() {
    return AccountId::FromUserEmail("user0@tray");
  }

  std::optional<std::string> calendar_id_;
  std::optional<int> events_fetched_count_;
  std::optional<google_apis::ApiErrorCode> api_error_code_;
  std::optional<CalendarEventFetchInternalErrorCode> internal_error_code_;

  base::WeakPtrFactory<CalendarEventFetchTest> weak_factory_{this};
};

TEST_F(CalendarEventFetchTest, NoEvents) {
  // Register our TestCalendarClient with the default user.
  RegisterClient();

  // Month whose events we want to fetch.
  base::Time start_of_month =
      GetStartOfMonthFromString("23 Oct 2009 11:30 GMT");

  // Perform the fetch.
  std::unique_ptr<CalendarEventFetch> fetch = PerformFetch(start_of_month);

  // Advance time to when the fetch is complete. `fetch` can no longer be used
  // after this.
  task_environment()->FastForwardBy(client_.get_response_delay());

  // No events were set in the client, so fetch should return no results.
  std::optional<int> count = events_fetched_count();
  EXPECT_TRUE(count.has_value() && count.value() == 0);

  // API error is HTTP_SUCCESS.
  std::optional<google_apis::ApiErrorCode> return_error_code = api_error_code();
  EXPECT_TRUE(return_error_code.has_value() &&
              return_error_code == google_apis::HTTP_SUCCESS);

  // No internal error.
  EXPECT_FALSE(internal_error_code().has_value());
}

TEST_F(CalendarEventFetchTest, HaveEvents) {
  // Register our TestCalendarClient with the default user.
  RegisterClient();

  // Inject two events, in different months.
  auto event_list = std::make_unique<google_apis::calendar::EventList>();
  event_list->set_time_zone("Greenwich Mean Time");
  event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
      "id_0", "summary_0", "23 Oct 2009 11:30 GMT", "23 Oct 2009 12:30 GMT"));
  event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
      "id_1", "summary_1", "18 Nov 2021 8:15 GMT", "18 Nov 2021 11:30 GMT"));
  client_.set_event_list(std::move(event_list));

  // Month whose events we want to fetch, for which no events have been
  // injected.
  base::Time start_of_month =
      GetStartOfMonthFromString("27 Sep 1971 11:30 GMT");

  // Perform the fetch.
  std::unique_ptr<CalendarEventFetch> fetch = PerformFetch(start_of_month);

  // Advance time to when the fetch is complete. `fetch` can no longer be used
  // after this.
  task_environment()->FastForwardBy(client_.get_response_delay());

  // No events for this month in the client, so fetch should return no results.
  std::optional<int> count = events_fetched_count();
  EXPECT_TRUE(count.has_value() && count.value() == 0);

  // No internal error.
  EXPECT_FALSE(internal_error_code().has_value());

  // Now use a month that has events
  start_of_month = GetStartOfMonthFromString("23 Oct 2009 11:30 GMT");

  // Perform the fetch.
  std::unique_ptr<CalendarEventFetch> fetch2 = PerformFetch(start_of_month);

  // Advance time to when the fetch is complete. `fetch2` can no longer be
  // used after this.
  task_environment()->FastForwardBy(client_.get_response_delay());

  // There is one event for this month in the client, so fetch should return one
  // event.
  count = events_fetched_count();
  EXPECT_TRUE(count.has_value() && count.value() == 1);

  // API error is HTTP_SUCCESS.
  std::optional<google_apis::ApiErrorCode> return_error_code = api_error_code();
  EXPECT_TRUE(return_error_code.has_value() &&
              return_error_code == google_apis::HTTP_SUCCESS);

  // No internal error.
  EXPECT_FALSE(internal_error_code().has_value());
}

TEST_F(CalendarEventFetchTest, FetchEventsForNonPrimaryCalendar) {
  RegisterClient();

  // The month for which we want to fetch events.
  base::Time start_of_month = GetStartOfMonthFromString("01 Oct 2023 8:00 GMT");

  // The ID and color ID of the calendar we want to fetch events for.
  std::string calendar_id = "google.com_zu5cft6test@group.calendar.google.com";
  std::string calendar_color_id = "14";

  // Inject some events.
  auto event_list = std::make_unique<google_apis::calendar::EventList>();
  event_list->set_time_zone("Greenwich Mean Time");
  event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
      "id_0", "summary_0", "02 Oct 2023 17:00 GMT", "02 Oct 2023 18:00 GMT"));
  event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
      "id_1", "summary_1", "05 Oct 2023 19:00 GMT", "05 Oct 2023 20:00 GMT"));
  client_.set_event_list(std::move(event_list));

  std::unique_ptr<CalendarEventFetch> fetch =
      PerformFetchByCalendarId(start_of_month, calendar_id, calendar_color_id);

  // Advance time to when the fetch is complete. `fetch` can no longer be used
  // after this.
  task_environment()->FastForwardBy(client_.get_response_delay());

  // There are two events for this month in the client, so fetch should return
  // two events.
  std::optional<int> count = events_fetched_count();
  EXPECT_TRUE(count.has_value() && count.value() == 2);

  // API error is HTTP_SUCCESS.
  std::optional<google_apis::ApiErrorCode> return_error_code = api_error_code();
  EXPECT_TRUE(return_error_code.has_value() &&
              return_error_code == google_apis::HTTP_SUCCESS);

  // No internal error.
  EXPECT_FALSE(internal_error_code().has_value());

  // The calendar ID assigned on fetch completion should equal the calendar ID
  // passed during the creation of the fetch.
  std::optional<std::string> fetch_calendar_id = get_calendar_id();
  EXPECT_TRUE(fetch_calendar_id.has_value() &&
              fetch_calendar_id == calendar_id);
}

TEST_F(CalendarEventFetchTest, ApiFailure) {
  // Specifically set up the fetch to fail with an API error.
  const google_apis::ApiErrorCode error_code = google_apis::HTTP_NOT_FOUND;
  client_.set_api_error_code(error_code);

  // Register our TestCalendarClient with the default user.
  RegisterClient();

  // Month whose events we want to fetch.
  base::Time start_of_month =
      GetStartOfMonthFromString("23 Oct 2009 11:30 GMT");

  // Perform the fetch.
  std::unique_ptr<CalendarEventFetch> fetch = PerformFetch(start_of_month);

  // Advance time to when the fetch is complete. `fetch` can no longer be used
  // after this.
  task_environment()->FastForwardBy(client_.get_response_delay());

  // No events were set in the client, so fetch should return no results.
  std::optional<int> count = events_fetched_count();
  EXPECT_TRUE(count.has_value() && count.value() == 0);

  // API error is what we set.
  std::optional<google_apis::ApiErrorCode> return_error_code = api_error_code();
  EXPECT_TRUE(return_error_code.has_value() && return_error_code == error_code);

  // No internal error.
  EXPECT_FALSE(internal_error_code().has_value());
}

TEST_F(CalendarEventFetchTest, Timeout) {
  base::HistogramTester histogram_tester;

  // No metrics recorded yet.
  histogram_tester.ExpectBucketCount("Ash.Calendar.FetchEvents.Timeout", true,
                                     /*expected_count=*/0);

  // Specifically delay the response until after CalendarEventFetch declares a
  // timeout.
  client_.set_response_delay(calendar_utils::kCalendarDataFetchTimeout +
                             base::Milliseconds(100));

  // Register our TestCalendarClient with the default user.
  RegisterClient();

  // Month whose events we want to fetch.
  base::Time start_of_month =
      GetStartOfMonthFromString("23 Oct 2009 11:30 GMT");

  // No internal error code reported.
  std::optional<CalendarEventFetchInternalErrorCode> internal_error =
      internal_error_code();
  EXPECT_FALSE(internal_error.has_value());

  // Perform the fetch.
  std::unique_ptr<CalendarEventFetch> fetch = PerformFetch(start_of_month);

  // Advance time to when the fetch times out. `fetch` can no longer be used
  // after this.
  task_environment()->FastForwardBy(calendar_utils::kCalendarDataFetchTimeout);

  // Events should be completely nonexistent.
  EXPECT_FALSE(events_fetched_count().has_value());

  // API error should be completely nonexistent.
  std::optional<google_apis::ApiErrorCode> return_error_code = api_error_code();
  EXPECT_FALSE(return_error_code.has_value());

  // Internal error code reported is kTimeout.
  internal_error = internal_error_code();
  EXPECT_TRUE(internal_error.has_value() &&
              internal_error == CalendarEventFetchInternalErrorCode::kTimeout);

  // Metrics now recorded.
  histogram_tester.ExpectBucketCount("Ash.Calendar.FetchEvents.Timeout", true,
                                     /*expected_count=*/1);
}

TEST_F(CalendarEventFetchTest, Cancel) {
  // Register our TestCalendarClient with the default user.
  RegisterClient();

  // Month whose events we want to fetch.
  base::Time start_of_month =
      GetStartOfMonthFromString("23 Oct 2009 11:30 GMT");

  // Perform the fetch.
  std::unique_ptr<CalendarEventFetch> fetch = PerformFetch(start_of_month);

  // Cancel the request.
  fetch->Cancel();

  // Advance time to when the fetch is complete. `fetch` can no longer be used
  // after this.
  task_environment()->FastForwardBy(client_.get_response_delay());

  // API error is CANCELLED.
  std::optional<google_apis::ApiErrorCode> return_error_code = api_error_code();
  EXPECT_TRUE(return_error_code.has_value() &&
              return_error_code == google_apis::CANCELLED);
}

}  // namespace ash