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

ash / system / toast / system_nudge_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 <string>

#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/system/toast/system_nudge_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 "components/vector_icons/vector_icons.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/background.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

// Creates an `AnchoredNudgeData` object with only the required elements.
// This will create a nudge shown on its default location.
AnchoredNudgeData CreateBaseNudgeData() {
  // Set up nudge data contents.
  const std::string id = "id";
  const std::u16string body_text = u"text";
  auto catalog_name = NudgeCatalogName::kTestCatalogName;

  return AnchoredNudgeData(id, catalog_name, body_text);
}

// Nudge constants
const std::u16string button_text = u"Button";
const std::u16string title_text = u"Title text";
const std::u16string long_body_text =
    u"Nudge body text should be clear, short and succinct (80 characters "
    u"recommended)";

}  // namespace

class SystemNudgeViewPixelTest : public AshTestBase {
 public:
  void SetUp() override {
    AshTestBase::SetUp();

    test_widget_ = CreateFramelessTestWidget();
    // Set a size larger than the nudge max dimensions.
    test_widget_->SetBounds(gfx::Rect(800, 600));
    test_widget_->SetContentsView(
        views::Builder<views::FlexLayoutView>()
            .SetMainAxisAlignment(views::LayoutAlignment::kCenter)
            .SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
            .SetBackground(views::CreateThemedSolidBackground(
                cros_tokens::kCrosSysSystemBase))
            .Build());
  }

  views::View* GetContentsView() { return test_widget_->GetContentsView(); }

  void TearDown() override {
    test_widget_.reset();
    AshTestBase::TearDown();
  }

  // AshTestBase:
  std::optional<pixel_test::InitParams> CreatePixelTestInitParams()
      const override {
    return pixel_test::InitParams();
  }

 private:
  std::unique_ptr<views::Widget> test_widget_;
};

TEST_F(SystemNudgeViewPixelTest, TextOnly) {
  // Set up base nudge data, which has an id, a catalog name and a body text.
  auto nudge_data = CreateBaseNudgeData();

  GetContentsView()->AddChildView(
      std::make_unique<SystemNudgeView>(nudge_data));

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "screenshot", /*revision_number=*/0, GetContentsView()));
}

TEST_F(SystemNudgeViewPixelTest, TextOnly_LongText) {
  // Set up base nudge data and set a long text.
  auto nudge_data = CreateBaseNudgeData();
  nudge_data.body_text = long_body_text;

  GetContentsView()->AddChildView(
      std::make_unique<SystemNudgeView>(nudge_data));

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "screenshot", /*revision_number=*/0, GetContentsView()));
}

TEST_F(SystemNudgeViewPixelTest, WithButtons) {
  // Set up base nudge data, set a long text and add buttons.
  auto nudge_data = CreateBaseNudgeData();
  nudge_data.body_text = long_body_text;
  nudge_data.primary_button_text = button_text;
  nudge_data.secondary_button_text = button_text;

  GetContentsView()->AddChildView(
      std::make_unique<SystemNudgeView>(nudge_data));

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "screenshot", /*revision_number=*/0, GetContentsView()));
}

TEST_F(SystemNudgeViewPixelTest, TitleAndLeadingImage) {
  // Set up base nudge data, set a long text, a title and a leading image.
  auto nudge_data = CreateBaseNudgeData();
  nudge_data.image_model = ui::ImageModel::FromVectorIcon(
      vector_icons::kDogfoodIcon, cros_tokens::kCrosSysOnSurface,
      /*icon_size=*/60);
  nudge_data.title_text = title_text;
  nudge_data.body_text = long_body_text;

  GetContentsView()->AddChildView(
      std::make_unique<SystemNudgeView>(nudge_data));

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "screenshot", /*revision_number=*/0, GetContentsView()));
}

TEST_F(SystemNudgeViewPixelTest, TitleAndLeadingImageWithButtons) {
  // Set up base nudge data, set a long text, title, leading image and buttons.
  auto nudge_data = CreateBaseNudgeData();
  nudge_data.image_model = ui::ImageModel::FromVectorIcon(
      vector_icons::kDogfoodIcon, cros_tokens::kCrosSysOnSurface,
      /*icon_size=*/60);
  nudge_data.title_text = title_text;
  nudge_data.body_text = long_body_text;
  nudge_data.primary_button_text = button_text;
  nudge_data.secondary_button_text = button_text;

  GetContentsView()->AddChildView(
      std::make_unique<SystemNudgeView>(nudge_data));

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "screenshot", /*revision_number=*/0, GetContentsView()));
}

TEST_F(SystemNudgeViewPixelTest, AnchoredNudgeWithPointyCorner) {
  // Set up base nudge data and add an anchor view.
  auto nudge_data = CreateBaseNudgeData();
  auto* anchor_view =
      GetContentsView()->AddChildView(std::make_unique<views::View>());
  nudge_data.SetAnchorView(anchor_view);
  // Set a corner-anchored arrow that will create a pointy corner.
  nudge_data.arrow = views::BubbleBorder::Arrow::BOTTOM_RIGHT;

  GetContentsView()->AddChildView(
      std::make_unique<SystemNudgeView>(nudge_data));

  EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
      "screenshot", /*revision_number=*/0, GetContentsView()));
}

}  // namespace ash