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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
|
// 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_event_list_view.h"
#include <string_view>
#include "ash/calendar/calendar_controller.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/time/calendar_event_list_item_view.h"
#include "ash/system/time/calendar_list_model.h"
#include "ash/system/time/calendar_model.h"
#include "ash/system/time/calendar_unittest_utils.h"
#include "ash/system/time/calendar_utils.h"
#include "ash/system/time/calendar_view_controller.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
#include "chromeos/ash/components/settings/scoped_timezone_settings.h"
#include "google_apis/calendar/calendar_api_requests.h"
#include "google_apis/common/api_error_codes.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/test/views_test_utils.h"
namespace ash {
namespace {
std::unique_ptr<google_apis::calendar::EventList> CreateMockEventList() {
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", "18 Nov 2021 8:30 GMT", "18 Nov 2021 9: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"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_2", "summary_2", "18 Nov 2021 11:30 GMT", "18 Nov 2021 12:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_3", "", "19 Nov 2021 8:30 GMT", "19 Nov 2021 10:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_4", "summary_4", "21 Nov 2021 8:30 GMT", "21 Nov 2021 9:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_5", "summary_5", "21 Nov 2021 10:30 GMT", "21 Nov 2021 11:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_6", "summary_6", "22 Nov 2021 20:30 GMT", "22 Nov 2021 21:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_7", "summary_7", "22 Nov 2021 23:30 GMT", "23 Nov 2021 0:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_8", "summary_8", "23 Nov 2021 01:30 GMT", "23 Nov 2021 02:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_9", "summary_9", "23 Nov 2021 02:30 GMT", "23 Nov 2021 03:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_10", "summary_10", "23 Nov 2021 03:30 GMT", "23 Nov 2021 04:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_11", "summary_11", "23 Nov 2021 04:30 GMT", "23 Nov 2021 05:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_12", "summary_12", "23 Nov 2021 05:30 GMT", "23 Nov 2021 06:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_13", "summary_13", "23 Nov 2021 06:30 GMT", "23 Nov 2021 07:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_14", "summary_14", "23 Nov 2021 06:30 GMT", "23 Nov 2021 07:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_15", "summary_15", "23 Nov 2021 07:30 GMT", "23 Nov 2021 08:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_16", "summary_16", "23 Nov 2021 08:30 GMT", "23 Nov 2021 09:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_17", "summary_17", "23 Nov 2021 09:30 GMT", "23 Nov 2021 10:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_18", "summary_18", "23 Nov 2021 10:30 GMT", "23 Nov 2021 11:30 GMT"));
return event_list;
}
const char* kCalendarId1 = "user1@email.com";
const char* kCalendarSummary1 = "user1@email.com";
const char* kCalendarColorId1 = "12";
bool kCalendarSelected1 = true;
bool kCalendarPrimary1 = true;
} // namespace
class CalendarViewEventListViewTest
: public AshTestBase,
public testing::WithParamInterface</*multi_calendar_enabled=*/bool> {
public:
CalendarViewEventListViewTest() {
scoped_feature_list_.InitWithFeatureState(
ash::features::kMultiCalendarSupport, IsMultiCalendarEnabled());
}
CalendarViewEventListViewTest(const CalendarViewEventListViewTest&) = delete;
CalendarViewEventListViewTest& operator=(
const CalendarViewEventListViewTest&) = delete;
~CalendarViewEventListViewTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
controller_ = std::make_unique<CalendarViewController>();
calendar_model_ = Shell::Get()->system_tray_model()->calendar_model();
widget_ = CreateFramelessTestWidget();
widget_->SetFullscreen(true);
}
void TearDown() override {
event_list_view_.reset();
controller_.reset();
scoped_feature_list_.Reset();
widget_.reset();
calendar_model_ = nullptr;
AshTestBase::TearDown();
}
bool IsMultiCalendarEnabled() { return GetParam(); }
void CreateEventListView(base::Time date) {
event_list_view_.reset();
controller_->UpdateMonth(date);
calendar_model_->OnEventsFetched(calendar_utils::GetStartOfMonthUTC(date),
google_apis::calendar::kPrimaryCalendarId,
google_apis::ApiErrorCode::HTTP_SUCCESS,
CreateMockEventList().get());
controller_->selected_date_ = date;
event_list_view_ =
std::make_unique<CalendarEventListView>(controller_.get());
widget_->SetContentsView(event_list_view_.get());
}
void SetSelectedDate(base::Time date) {
controller_->selected_date_ = date;
controller_->ShowEventListView(/*calendar_date_cell_view=*/nullptr, date,
/*row_index=*/0);
}
void UpdateEventList() { event_list_view_->UpdateListItems(); }
// The way we send metrics is slightly different for Jelly so we need to
// ensure this value is set to true in the controller.
void SetEventListIsShowingForMetrics() {
controller_->is_event_list_showing_ = true;
}
CalendarEventListView* event_list_view() { return event_list_view_.get(); }
views::View* content_view() { return event_list_view_->content_view_; }
views::ScrollView* scroll_view() { return event_list_view_->scroll_view_; }
CalendarViewController* controller() { return controller_.get(); }
int current_or_next_event_index() {
return event_list_view_->current_or_next_event_index_;
}
views::View* GetSameDayEventsContainer() {
views::View* container =
content_view()->GetViewByID(kEventListSameDayEventsContainer);
CHECK(container);
return container;
}
views::Label* GetSummary(int child_index) {
return static_cast<views::Label*>(
static_cast<CalendarEventListItemView*>(
GetSameDayEventsContainer()->children()[child_index])
->GetViewByID(kSummaryLabelID));
}
std::u16string_view GetEmptyLabel() {
return static_cast<views::LabelButton*>(
content_view()->children()[0]->children()[0])
->GetText();
}
views::View* GetHighlightView(int child_index) {
return GetSameDayEventsContainer()->children()[child_index];
}
size_t GetContentViewSize() {
return GetSameDayEventsContainer()->children().size();
}
size_t GetEmptyContentViewSize() { return content_view()->children().size(); }
static base::Time FakeTimeNow() { return fake_time_; }
static void SetFakeNow(base::Time fake_now) { fake_time_ = fake_now; }
private:
std::unique_ptr<views::Widget> widget_;
std::unique_ptr<calendar_test_utils::CalendarClientTestImpl> calendar_client_;
raw_ptr<CalendarModel> calendar_model_ = nullptr;
std::unique_ptr<CalendarEventListView> event_list_view_;
std::unique_ptr<CalendarViewController> controller_;
base::test::ScopedFeatureList scoped_feature_list_;
static base::Time fake_time_;
};
base::Time CalendarViewEventListViewTest::fake_time_;
INSTANTIATE_TEST_SUITE_P(MultiCalendar,
CalendarViewEventListViewTest,
testing::Bool());
TEST_P(CalendarViewEventListViewTest, ShowEvents) {
base::Time date;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 10:00 GMT", &date));
CreateEventListView(date - base::Days(1));
// No events on 17 Nov 2021, so we see the empty list default.
EXPECT_EQ(1u, GetEmptyContentViewSize());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_NO_EVENTS),
GetEmptyLabel());
SetSelectedDate(date);
// 3 events on 18 Nov 2021. And they should be sorted by the start time.
EXPECT_EQ(3u, GetContentViewSize());
EXPECT_EQ(u"summary_1", GetSummary(0)->GetText());
EXPECT_EQ(u"summary_0", GetSummary(1)->GetText());
EXPECT_EQ(u"summary_2", GetSummary(2)->GetText());
SetSelectedDate(date + base::Days(1));
// 1 event on 19 Nov 2021. For no title mettings, shows "No title" as the
// meeting summary.
EXPECT_EQ(1u, GetContentViewSize());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_NO_TITLE),
GetSummary(0)->GetText());
SetSelectedDate(date + base::Days(2));
// 0 event on 20 Nov 2021.
EXPECT_EQ(1u, GetEmptyContentViewSize());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_NO_EVENTS),
GetEmptyLabel());
SetSelectedDate(date + base::Days(3));
// 2 events on 21 Nov 2021.
EXPECT_EQ(2u, GetContentViewSize());
EXPECT_EQ(u"summary_4", GetSummary(0)->GetText());
EXPECT_EQ(u"summary_5", GetSummary(1)->GetText());
}
TEST_P(CalendarViewEventListViewTest, LaunchEmptyList) {
base::HistogramTester histogram_tester;
base::Time date;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 10:00 GMT", &date));
CreateEventListView(date - base::Days(1));
// No events, so we see the empty list by default.
EXPECT_EQ(1u, GetEmptyContentViewSize());
views::Button* empty_list_button =
static_cast<views::Button*>(content_view()->children()[0]->children()[0]);
empty_list_button->AcceleratorPressed(
ui::Accelerator(ui::KeyboardCode::VKEY_SPACE, /*modifiers=*/0));
histogram_tester.ExpectTotalCount(
"Ash.Calendar.UserJourneyTime.EventLaunched", 1);
EXPECT_EQ(histogram_tester.GetTotalSum(
"Ash.Calendar.EventListViewJelly.EventDisplayedCount"),
0);
}
TEST_P(CalendarViewEventListViewTest, LaunchItem) {
base::HistogramTester histogram_tester;
base::Time date;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 10:00 GMT", &date));
CreateEventListView(date);
SetEventListIsShowingForMetrics();
EXPECT_EQ(3u, GetContentViewSize());
// Launch the first item.
views::View* first_item = GetHighlightView(0);
first_item->AcceleratorPressed(
ui::Accelerator(ui::KeyboardCode::VKEY_SPACE, /*modifiers=*/0));
histogram_tester.ExpectTotalCount(
"Ash.Calendar.UserJourneyTime.EventLaunched", 1);
histogram_tester.ExpectTotalCount("Ash.Calendar.EventListItem.Activated", 1);
EXPECT_EQ(histogram_tester.GetTotalSum(
"Ash.Calendar.EventListViewJelly.EventDisplayedCount"),
3);
}
TEST_P(CalendarViewEventListViewTest, ScrollToCurrentOrNextEvent) {
// Sets the timezone to GMT. Otherwise in other timezones events can become
// multi-day events that will be ignored when calculating index.
ash::system::ScopedTimezoneSettings timezone_settings(u"GMT");
// Sets today to be a day with many events, so `event_list_view()` is
// scrollable.
base::Time date;
ASSERT_TRUE(base::Time::FromString("23 Nov 2021 08:00 GMT", &date));
CreateEventListView(date);
SetSelectedDate(date);
// Sets the current time to be a time that event id_9 has started.
base::Time current_time;
ASSERT_TRUE(base::Time::FromString("23 Nov 2021 02:40 GMT", ¤t_time));
SetFakeNow(current_time);
base::subtle::ScopedTimeClockOverrides time_override(
&CalendarViewEventListViewTest::FakeTimeNow,
/*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);
UpdateEventList();
views::test::RunScheduledLayout(event_list_view());
// The current or next event should be the second event(event id_9).
EXPECT_EQ(1, current_or_next_event_index());
// The top of `scroll_view()` visible rect should be the same with the top of
// the current or next event.
auto scroll_view_visible_bounds = scroll_view()->GetVisibleBounds();
views::View::ConvertRectToScreen(scroll_view(), &scroll_view_visible_bounds);
auto current_item_bounds =
GetHighlightView(current_or_next_event_index())->GetBoundsInScreen();
EXPECT_EQ(scroll_view_visible_bounds.y(), current_item_bounds.y());
}
TEST_P(CalendarViewEventListViewTest,
DoNotScrollToCurrentOrNextEventWhenFocused) {
// Sets the timezone to GMT. Otherwise in other timezones events can become
// multi-day events that will be ignored when calculating index.
ash::system::ScopedTimezoneSettings timezone_settings(u"GMT");
// Sets today to be a day with many events, so `event_list_view()` is
// scrollable.
base::Time date;
ASSERT_TRUE(base::Time::FromString("23 Nov 2021 08:00 GMT", &date));
CreateEventListView(date);
SetSelectedDate(date);
// Sets the current time to be a time that event id_9 has started.
base::Time current_time;
ASSERT_TRUE(base::Time::FromString("23 Nov 2021 02:40 GMT", ¤t_time));
SetFakeNow(current_time);
base::subtle::ScopedTimeClockOverrides time_override(
&CalendarViewEventListViewTest::FakeTimeNow,
/*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);
UpdateEventList();
views::test::RunScheduledLayout(event_list_view());
// The current or next event should be the second event(event id_9).
EXPECT_EQ(1, current_or_next_event_index());
// The top of `scroll_view()` visible rect should be the same with the top of
// the current or next event.
auto scroll_view_visible_bounds = scroll_view()->GetVisibleBounds();
views::View::ConvertRectToScreen(scroll_view(), &scroll_view_visible_bounds);
auto current_item_bounds =
GetHighlightView(current_or_next_event_index())->GetBoundsInScreen();
EXPECT_EQ(scroll_view_visible_bounds.y(), current_item_bounds.y());
// Focus on first item in `event_list_view()`, and `scroll_view()` should
// scroll up along with the focus change.
GetHighlightView(0)->RequestFocus();
views::test::RunScheduledLayout(event_list_view());
scroll_view_visible_bounds = scroll_view()->GetVisibleBounds();
views::View::ConvertRectToScreen(scroll_view(), &scroll_view_visible_bounds);
current_item_bounds =
GetHighlightView(current_or_next_event_index())->GetBoundsInScreen();
EXPECT_LT(scroll_view_visible_bounds.y(), current_item_bounds.y());
}
TEST_P(CalendarViewEventListViewTest,
ScrollToCurrentOrNextEvent_WithMultiDayEvents) {
// Sets the timezone to HST. The first two events become multi-day events, and
// will be ignored when calculating index.
ash::system::ScopedTimezoneSettings timezone_settings(u"Pacific/Honolulu");
// Sets the current time in HST to make today 18 Nov in local time.
base::Time date;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 8:10 HST", &date));
CreateEventListView(date);
SetSelectedDate(date);
// Sets the current time to be a time that event id_2 has started.
base::Time current_time;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 11:40 GMT", ¤t_time));
SetFakeNow(current_time);
base::subtle::ScopedTimeClockOverrides time_override(
&CalendarViewEventListViewTest::FakeTimeNow,
/*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);
UpdateEventList();
// The current or next event should be the first event with id_2 since the
// first two events are multi-day events.
EXPECT_EQ(0, current_or_next_event_index());
// The top of `scroll_view()` visible rect should be the same with the top of
// the current or next event.
auto scroll_view_visible_bounds = scroll_view()->GetVisibleBounds();
views::View::ConvertRectToScreen(scroll_view(), &scroll_view_visible_bounds);
auto current_item_bounds =
GetHighlightView(current_or_next_event_index())->GetBoundsInScreen();
EXPECT_EQ(scroll_view_visible_bounds.y(), current_item_bounds.y());
}
TEST_P(CalendarViewEventListViewTest,
ScrollToCurrentOrNextEvent_PassedDatesStayAtTop) {
// Sets the timezone to GMT. Otherwise in other timezones events can become
// multi-day events that will be ignored when calculating index.
ash::system::ScopedTimezoneSettings timezone_settings(u"GMT");
base::Time date;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 8:10 GMT", &date));
CreateEventListView(date);
SetSelectedDate(date);
// Sets the current time to be a time that all events for today have passed.
base::Time current_time;
ASSERT_TRUE(base::Time::FromString("19 Nov 2021 11:40 GMT", ¤t_time));
SetFakeNow(current_time);
base::subtle::ScopedTimeClockOverrides time_override(
&CalendarViewEventListViewTest::FakeTimeNow,
/*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);
UpdateEventList();
views::test::RunScheduledLayout(event_list_view());
// The current or next event should be the default value.
EXPECT_EQ(0, current_or_next_event_index());
// The `scroll_view()` should stay at the top of the list.
auto scroll_view_visible_bounds = scroll_view()->GetVisibleBounds();
views::View::ConvertRectToScreen(scroll_view(), &scroll_view_visible_bounds);
auto list_top_bounds =
GetHighlightView(current_or_next_event_index())->GetBoundsInScreen();
EXPECT_EQ(scroll_view_visible_bounds.y(), list_top_bounds.y());
}
TEST_P(CalendarViewEventListViewTest,
ScrollToCurrentOrNextEvent_FutureDatesStaysAtTop) {
// Sets the timezone to GMT. Otherwise in other timezones events can become
// multi-day events that will be ignored when calculating index.
ash::system::ScopedTimezoneSettings timezone_settings(u"GMT");
base::Time date;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 8:10 GMT", &date));
CreateEventListView(date);
SetSelectedDate(date);
// Sets the current time to be a time before today.
base::Time current_time;
ASSERT_TRUE(base::Time::FromString("17 Nov 2021 11:40 GMT", ¤t_time));
SetFakeNow(current_time);
base::subtle::ScopedTimeClockOverrides time_override(
&CalendarViewEventListViewTest::FakeTimeNow,
/*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);
UpdateEventList();
views::test::RunScheduledLayout(event_list_view());
// The current or next event should be the first event.
EXPECT_EQ(0, current_or_next_event_index());
// The `scroll_view()` should stay at the top of the list.
auto scroll_view_visible_bounds = scroll_view()->GetVisibleBounds();
views::View::ConvertRectToScreen(scroll_view(), &scroll_view_visible_bounds);
auto first_item_bounds =
GetHighlightView(current_or_next_event_index())->GetBoundsInScreen();
EXPECT_EQ(scroll_view_visible_bounds.y(), first_item_bounds.y());
}
class CalendarViewEventListViewFetchTest
: public AshTestBase,
public testing::WithParamInterface</*multi_calendar_enabled=*/bool> {
public:
CalendarViewEventListViewFetchTest()
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
scoped_feature_list_.InitWithFeatureState(
ash::features::kMultiCalendarSupport, IsMultiCalendarEnabled());
}
CalendarViewEventListViewFetchTest(
const CalendarViewEventListViewFetchTest&) = delete;
CalendarViewEventListViewFetchTest& operator=(
const CalendarViewEventListViewFetchTest&) = delete;
~CalendarViewEventListViewFetchTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
// Register a mock `CalendarClient` to the `CalendarController`.
const std::string email = "user1@email.com";
account_id_ = AccountId::FromUserEmail(email);
Shell::Get()->calendar_controller()->SetActiveUserAccountIdForTesting(
account_id_);
calendar_model_ = Shell::Get()->system_tray_model()->calendar_model();
calendar_client_ =
std::make_unique<calendar_test_utils::CalendarClientTestImpl>();
controller_ = std::make_unique<CalendarViewController>();
Shell::Get()->calendar_controller()->RegisterClientForUser(
account_id_, calendar_client_.get());
Shell::Get()->session_controller()->GetActivePrefService()->SetBoolean(
ash::prefs::kCalendarIntegrationEnabled, true);
}
void TearDown() override {
event_list_view_.reset();
controller_.reset();
scoped_feature_list_.Reset();
time_overrides_.reset();
calendar_model_ = nullptr;
AshTestBase::TearDown();
}
bool IsMultiCalendarEnabled() { return GetParam(); }
void WaitUntilFetched() {
task_environment()->FastForwardBy(base::Minutes(1));
base::RunLoop().RunUntilIdle();
}
void CreateEventListView(base::Time date) {
event_list_view_.reset();
controller_->UpdateMonth(date);
calendar_model_->OnEventsFetched(calendar_utils::GetStartOfMonthUTC(date),
google_apis::calendar::kPrimaryCalendarId,
google_apis::ApiErrorCode::HTTP_SUCCESS,
CreateMockEventList().get());
controller_->selected_date_ = date;
event_list_view_ =
std::make_unique<CalendarEventListView>(controller_.get());
}
void SetCalendarList() {
// Sets a mock calendar list.
std::list<std::unique_ptr<google_apis::calendar::SingleCalendar>> calendars;
calendars.push_back(calendar_test_utils::CreateCalendar(
kCalendarId1, kCalendarSummary1, kCalendarColorId1, kCalendarSelected1,
kCalendarPrimary1));
calendar_client_->SetCalendarList(
calendar_test_utils::CreateMockCalendarList(std::move(calendars)));
}
void SetEventList(std::unique_ptr<google_apis::calendar::EventList> events) {
calendar_client_->SetEventList(std::move(events));
}
void FetchCalendars() {
Shell::Get()->system_tray_model()->calendar_list_model()->FetchCalendars();
WaitUntilFetched();
}
void RefetchEvents(base::Time start_of_month) {
calendar_model_->FetchEvents(start_of_month);
WaitUntilFetched();
}
void SetTodayFromTime(base::Time date) {
std::set<base::Time> months = calendar_utils::GetSurroundingMonthsUTC(
date, calendar_utils::kNumSurroundingMonthsCached);
calendar_model_->non_prunable_months_.clear();
// Non-prunable months are today's date and the two surrounding months.
calendar_model_->AddNonPrunableMonths(months);
}
void SetSelectedDate(base::Time date) {
controller_->selected_date_ = date;
controller_->ShowEventListView(/*calendar_date_cell_view=*/nullptr, date,
/*row_index=*/0);
}
void UpdateEventList() { event_list_view_->UpdateListItems(); }
views::View* content_view() { return event_list_view_->content_view_; }
CalendarViewController* controller() { return controller_.get(); }
views::View* GetSameDayEventsContainer() {
views::View* container =
content_view()->GetViewByID(kEventListSameDayEventsContainer);
CHECK(container);
return container;
}
views::Label* GetSummary(int child_index) {
return static_cast<views::Label*>(
static_cast<CalendarEventListItemView*>(
GetSameDayEventsContainer()->children()[child_index])
->GetViewByID(kSummaryLabelID));
}
std::u16string_view GetEmptyLabel() {
return static_cast<views::LabelButton*>(
content_view()->children()[0]->children()[0])
->GetText();
}
size_t GetContentViewSize() {
return GetSameDayEventsContainer()->children().size();
}
size_t GetEmptyContentViewSize() { return content_view()->children().size(); }
private:
std::unique_ptr<base::subtle::ScopedTimeClockOverrides> time_overrides_;
std::unique_ptr<views::Widget> widget_;
std::unique_ptr<calendar_test_utils::CalendarClientTestImpl> calendar_client_;
raw_ptr<CalendarModel> calendar_model_ = nullptr;
std::unique_ptr<CalendarEventListView> event_list_view_;
std::unique_ptr<CalendarViewController> controller_;
base::test::ScopedFeatureList scoped_feature_list_;
AccountId account_id_;
};
INSTANTIATE_TEST_SUITE_P(MultiCalendar,
CalendarViewEventListViewFetchTest,
testing::Bool());
TEST_P(CalendarViewEventListViewFetchTest, RefreshEvents) {
base::Time date;
ASSERT_TRUE(base::Time::FromString("18 Nov 2021 10:00 GMT", &date));
CreateEventListView(date);
SetSelectedDate(date);
SetTodayFromTime(date);
// With the initial event list there should be 3 events on the 18th.
EXPECT_EQ(3u, GetContentViewSize());
if (IsMultiCalendarEnabled()) {
// Set and fetch a calendar list so FetchEvents can create an event fetch.
SetCalendarList();
FetchCalendars();
}
base::Time start_of_month = calendar_utils::GetStartOfMonthUTC(
controller()->selected_date_midnight());
auto event_list = std::make_unique<google_apis::calendar::EventList>();
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_4", "summary_4", "21 Nov 2021 8:30 GMT", "21 Nov 2021 9:30 GMT"));
SetEventList(std::move(event_list));
// Calls the `FetchEvents` method to update the events in the model.
// The event list view should be re-rendered automatically with the new event
// list.
RefetchEvents(start_of_month);
// Shows 0 events and shows open in google calendar button after the refresh.
EXPECT_EQ(1u, GetEmptyContentViewSize());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_NO_EVENTS),
GetEmptyLabel());
auto event_list2 = std::make_unique<google_apis::calendar::EventList>();
event_list2->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_0", "summary_0", "18 Nov 2021 8:30 GMT", "18 Nov 2021 9:30 GMT"));
SetEventList(std::move(event_list2));
RefetchEvents(start_of_month);
// Shows 1 event after the refresh.
EXPECT_EQ(1u, GetContentViewSize());
EXPECT_EQ(u"summary_0", GetSummary(0)->GetText());
}
} // namespace ash
|