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

ash / glanceables / tasks / glanceables_task_view_pixeltest.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 <memory>
#include <optional>
#include <string>
#include <tuple>

#include "ash/api/tasks/tasks_types.h"
#include "ash/glanceables/common/glanceables_util.h"
#include "ash/glanceables/common/glanceables_view_id.h"
#include "ash/glanceables/tasks/glanceables_task_view.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/pixel/ash_pixel_differ.h"
#include "ash/test/pixel/ash_pixel_test_init_params.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_util.h"
#include "base/test/gtest_tags.h"
#include "base/time/time.h"
#include "base/types/cxx23_to_underlying.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_test_api.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"

namespace ash {

// Pixel tests for `GlanceablesTaskView` that covers all possible permutations
// of UI states.
class GlanceablesTaskViewPixelTest
    : public AshTestBase,
      public testing::WithParamInterface<
          std::tuple</*use_rtl=*/bool,
                     /*has_due_date=*/bool,
                     /*has_subtasks=*/bool,
                     /*has_notes=*/bool,
                     /*completed=*/bool,
                     /*is_in_edit_state=*/bool>> {
 public:
  void SetUp() override {
    AshTestBase::SetUp();

    glanceables_util::SetIsNetworkConnectedForTest(true);

    base::Time due_date;
    ASSERT_TRUE(base::Time::FromString("2022-12-21T00:00:00.000Z", &due_date));

    task_ = std::make_unique<api::Task>(
        "task-id", "Task title",
        has_due_date() ? std::make_optional(due_date) : std::nullopt,
        /*completed=*/false, has_subtasks(),
        /*has_email_link=*/false,
        /*has_notes=*/has_notes(), /*updated=*/base::Time(),
        /*web_view_link=*/GURL(), api::Task::OriginSurfaceType::kRegular);

    widget_ = CreateFramelessTestWidget();
    widget_->SetFullscreen(true);

    auto* const container =
        widget_->SetContentsView(std::make_unique<views::BoxLayoutView>());
    container->SetCrossAxisAlignment(
        views::BoxLayout::CrossAxisAlignment::kStart);
    view_ = container->AddChildView(std::make_unique<GlanceablesTaskView>(
        task_.get(), /*mark_as_completed_callback=*/base::DoNothing(),
        /*save_callback=*/base::DoNothing(),
        /*edit_in_browser_callback=*/base::DoNothing(),
        /*show_error_message_callback=*/base::DoNothing()));

    widget_->LayoutRootViewIfNecessary();
  }

  std::optional<pixel_test::InitParams> CreatePixelTestInitParams()
      const override {
    pixel_test::InitParams init_params;
    init_params.under_rtl = use_rtl();
    return init_params;
  }

  std::string GenerateScreenshotName() const {
    std::vector<std::string> parameters = {
        use_rtl() ? "rtl" : "ltr",
        has_due_date() ? "has_due_date=true" : "has_due_date=false",
        has_subtasks() ? "has_subtasks=true" : "has_subtasks=false",
        has_notes() ? "has_notes=true" : "has_notes=false",
        completed() ? "completed=true" : "completed=false",
        is_in_edit_state() ? "is_in_edit_state=true"
                           : "is_in_edit_state=false"};

    std::string stringified_params = base::JoinString(parameters, "|");
    return base::JoinString({"glanceables_task_view", stringified_params}, ".");
  }

  views::Widget* widget() const { return widget_.get(); }
  GlanceablesTaskView* view() const { return view_; }
  bool use_rtl() const { return std::get<0>(GetParam()); }
  bool has_due_date() const { return std::get<1>(GetParam()); }
  bool has_subtasks() const { return std::get<2>(GetParam()); }
  bool has_notes() const { return std::get<3>(GetParam()); }
  bool completed() const { return std::get<4>(GetParam()); }
  bool is_in_edit_state() const { return std::get<5>(GetParam()); }

 private:
  std::unique_ptr<api::Task> task_;
  std::unique_ptr<views::Widget> widget_;
  raw_ptr<GlanceablesTaskView> view_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         GlanceablesTaskViewPixelTest,
                         testing::Combine(
                             /*use_rtl=*/testing::Bool(),
                             /*has_due_date=*/testing::Bool(),
                             /*has_subtasks=*/testing::Bool(),
                             /*has_notes=*/testing::Bool(),
                             /*completed=*/testing::Bool(),
                             /*is_in_edit_state=*/testing::Bool()));

TEST_P(GlanceablesTaskViewPixelTest, GlanceablesTaskView) {
  base::AddFeatureIdTagToTestResult(
      "screenplay-c5b9a047-dd51-4ccd-8f47-50c0fdfae7d1");  // due date +
                                                           // description
  base::AddFeatureIdTagToTestResult(
      "screenplay-c67b5acd-d0e8-41e8-a921-8060756cd807");  // subtasks

  ASSERT_TRUE(widget());

  ASSERT_FALSE(view()->GetCompletedForTest());
  if (completed()) {
    const auto* const checkbox = view()->GetCheckButtonForTest();
    ASSERT_TRUE(checkbox);
    LeftClickOn(checkbox);
    ASSERT_TRUE(view()->GetCompletedForTest());
  }

  if (is_in_edit_state()) {
    const auto* const title_label =
        views::AsViewClass<views::Label>(view()->GetViewByID(
            base::to_underlying(GlanceablesViewId::kTaskItemTitleLabel)));
    ASSERT_TRUE(title_label);
    LeftClickOn(title_label);

    auto* const title_text_field =
        views::AsViewClass<views::Textfield>(view()->GetViewByID(
            base::to_underlying(GlanceablesViewId::kTaskItemTitleTextField)));
    ASSERT_TRUE(title_text_field);
    views::TextfieldTestApi(title_text_field).SetCursorLayerOpacity(0.f);
  }

  widget()->LayoutRootViewIfNecessary();

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      GenerateScreenshotName(), /*revision_number=*/0, widget()));
}

}  // namespace ash