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

ash / glanceables / classroom / glanceables_classroom_item_view_unittest.cc [blame]

// Copyright 2023 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/glanceables/classroom/glanceables_classroom_item_view.h"

#include <optional>
#include <string>
#include <vector>

#include "ash/glanceables/classroom/glanceables_classroom_types.h"
#include "ash/glanceables/common/glanceables_view_id.h"
#include "ash/shell.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/time/calendar_unittest_utils.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/callback_helpers.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
#include "base/types/cxx23_to_underlying.h"
#include "chromeos/ash/components/settings/scoped_timezone_settings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/view_utils.h"
#include "url/gurl.h"

namespace ash {

class GlanceablesClassroomItemViewTest : public AshTestBase {
 public:
  const views::ImageView* GetIconView(
      const GlanceablesClassroomItemView& view) const {
    return views::AsViewClass<views::ImageView>(view.GetViewByID(
        base::to_underlying(GlanceablesViewId::kClassroomItemIcon)));
  }

  const views::Label* GetCourseWorkTitleLabel(
      const GlanceablesClassroomItemView& view) const {
    return views::AsViewClass<views::Label>(
        view.GetViewByID(base::to_underlying(
            GlanceablesViewId::kClassroomItemCourseWorkTitleLabel)));
  }

  const views::Label* GetCourseTitleLabel(
      const GlanceablesClassroomItemView& view) const {
    return views::AsViewClass<views::Label>(
        view.GetViewByID(base::to_underlying(
            GlanceablesViewId::kClassroomItemCourseTitleLabel)));
  }

  const views::Label* GetDueDateLabel(
      const GlanceablesClassroomItemView& view) const {
    return views::AsViewClass<views::Label>(view.GetViewByID(
        base::to_underlying(GlanceablesViewId::kClassroomItemDueDateLabel)));
  }

  const views::Label* GetDueTimeLabel(
      const GlanceablesClassroomItemView& view) const {
    return views::AsViewClass<views::Label>(view.GetViewByID(
        base::to_underlying(GlanceablesViewId::kClassroomItemDueTimeLabel)));
  }
};

TEST_F(GlanceablesClassroomItemViewTest, RendersWithoutDueDateTime) {
  const auto assignment = GlanceablesClassroomAssignment(
      "Algebra", "Solve equation",
      GURL("https://classroom.google.com/test-link-1"), std::nullopt,
      base::Time(), std::nullopt);
  const auto view =
      GlanceablesClassroomItemView(&assignment, base::DoNothing());

  const auto* const icon_view = GetIconView(view);
  const auto* const course_work_title_label = GetCourseWorkTitleLabel(view);
  const auto* const course_title_label = GetCourseTitleLabel(view);
  const auto* const due_date_label = GetDueDateLabel(view);
  const auto* const due_time_label = GetDueTimeLabel(view);

  ASSERT_TRUE(icon_view);
  ASSERT_TRUE(course_work_title_label);
  ASSERT_TRUE(course_title_label);
  EXPECT_FALSE(due_date_label);
  EXPECT_FALSE(due_time_label);

  EXPECT_EQ(course_work_title_label->GetText(), u"Solve equation");
  EXPECT_EQ(course_title_label->GetText(), u"Algebra");
}

TEST_F(GlanceablesClassroomItemViewTest, RendersWithDueDateTime) {
  // "Now" equals to Sunday, 4/9/2023 5:15pm in the overridden timezone.
  base::subtle::ScopedTimeClockOverrides time_override(
      []() {
        base::Time now;
        EXPECT_TRUE(base::Time::FromString("10 Apr 2023 00:15 GMT", &now));
        return now;
      },
      nullptr, nullptr);
  ash::system::ScopedTimezoneSettings tz(u"America/Los_Angeles");

  const std::vector<std::u16string> expected_due_date_labels{
      u"Apr 8", u"Today", u"Mon", u"Tue",   u"Wed",
      u"Thu",   u"Fri",   u"Sat", u"Apr 16"};

  // For 9 subsequent assignments starting from "yesterday", check their due
  // date and time labels.
  base::Time due = base::Time::Now() - base::Days(1);
  for (size_t i = 0; i < 9; ++i) {
    const auto assignment = GlanceablesClassroomAssignment(
        "Algebra", "Solve equation",
        GURL("https://classroom.google.com/test-link-1"), due, base::Time(),
        std::nullopt);
    const auto view =
        GlanceablesClassroomItemView(&assignment, base::DoNothing());

    const auto* const due_date_label = GetDueDateLabel(view);
    const auto* const due_time_label = GetDueTimeLabel(view);

    EXPECT_EQ(due_date_label->GetText(), expected_due_date_labels.at(i));
    EXPECT_EQ(due_time_label->GetText(), u"5:15\x202FPM");

    due += base::Days(1);
  }
}

TEST_F(GlanceablesClassroomItemViewTest, RendersDueTimeIn24HrFormat) {
  ash::system::ScopedTimezoneSettings tz(u"America/Los_Angeles");
  Shell::Get()->system_tray_model()->SetUse24HourClock(true);

  base::Time due;
  ASSERT_TRUE(base::Time::FromString("10 Apr 2023 00:15 GMT", &due));

  const auto assignment = GlanceablesClassroomAssignment(
      "Algebra", "Solve equation",
      GURL("https://classroom.google.com/test-link-1"), due, base::Time(),
      std::nullopt);
  const auto view =
      GlanceablesClassroomItemView(&assignment, base::DoNothing());
  const auto* const due_time_label = GetDueTimeLabel(view);

  ASSERT_TRUE(due_time_label);
  EXPECT_EQ(due_time_label->GetText(), u"17:15");
}

TEST_F(GlanceablesClassroomItemViewTest, DoesNotRenderDueTimeFor2359) {
  // 1 - for ICU formatters; 2 - for `base::Time::LocalExplode`.
  ash::system::ScopedTimezoneSettings tz(u"America/Los_Angeles");
  calendar_test_utils::ScopedLibcTimeZone libc_tz("America/Los_Angeles");

  base::Time due;
  ASSERT_TRUE(base::Time::FromString("10 Apr 2023 06:59 GMT", &due));

  const auto assignment = GlanceablesClassroomAssignment(
      "Algebra", "Solve equation",
      GURL("https://classroom.google.com/test-link-1"), due, base::Time(),
      std::nullopt);
  const auto view =
      GlanceablesClassroomItemView(&assignment, base::DoNothing());
  const auto* const due_time_label = GetDueTimeLabel(view);

  ASSERT_TRUE(due_time_label);
  EXPECT_TRUE(due_time_label->GetText().empty());
}

TEST_F(GlanceablesClassroomItemViewTest, AccessibleProperties) {
  const auto assignment = GlanceablesClassroomAssignment(
      "Algebra", "Solve equation",
      GURL("https://classroom.google.com/test-link-1"), std::nullopt,
      base::Time(), std::nullopt);
  auto view = GlanceablesClassroomItemView(&assignment, base::DoNothing());
  ui::AXNodeData data;

  view.GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.role, ax::mojom::Role::kListItem);
  EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kClick);

  view.SetEnabled(false);
  data = ui::AXNodeData();
  view.GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kClick);
}

}  // namespace ash