#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_helper.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 {
AnchoredNudgeData CreateBaseNudgeData() {
const std::string id = "id";
const std::u16string body_text = u"text";
auto catalog_name = NudgeCatalogName::kTestCatalogName;
return AnchoredNudgeData(id, catalog_name, body_text);
}
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)";
}
class SystemNudgeViewPixelTest
: public AshTestBase,
public testing::WithParamInterface</*enable_system_blur=*/bool> {
public:
void SetUp() override {
AshTestBase::SetUp();
test_widget_ = CreateFramelessTestWidget();
test_widget_->SetBounds(gfx::Rect(800, 600));
test_widget_->SetContentsView(
views::Builder<views::FlexLayoutView>()
.SetMainAxisAlignment(views::LayoutAlignment::kCenter)
.SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
.SetBackground(
views::CreateSolidBackground(cros_tokens::kCrosSysSystemBase))
.Build());
}
views::View* GetContentsView() { return test_widget_->GetContentsView(); }
void TearDown() override {
test_widget_.reset();
AshTestBase::TearDown();
}
std::optional<pixel_test::InitParams> CreatePixelTestInitParams()
const override {
pixel_test::InitParams init_params;
init_params.system_blur_enabled = GetParam();
return init_params;
}
private:
std::unique_ptr<views::Widget> test_widget_;
};
INSTANTIATE_TEST_SUITE_P(
,
SystemNudgeViewPixelTest,
testing::Bool());
TEST_P(SystemNudgeViewPixelTest, TextOnly) {
auto nudge_data = CreateBaseNudgeData();
GetContentsView()->AddChildView(
std::make_unique<SystemNudgeView>(nudge_data));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 1 : 1,
GetContentsView()));
}
TEST_P(SystemNudgeViewPixelTest, TextOnly_LongText) {
auto nudge_data = CreateBaseNudgeData();
nudge_data.body_text = long_body_text;
GetContentsView()->AddChildView(
std::make_unique<SystemNudgeView>(nudge_data));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 1 : 1,
GetContentsView()));
}
TEST_P(SystemNudgeViewPixelTest, WithButtons) {
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(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 1 : 1,
GetContentsView()));
}
TEST_P(SystemNudgeViewPixelTest, TitleAndLeadingImage) {
auto nudge_data = CreateBaseNudgeData();
nudge_data.image_model = ui::ImageModel::FromVectorIcon(
vector_icons::kDogfoodIcon, cros_tokens::kCrosSysOnSurface,
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(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 1 : 1,
GetContentsView()));
}
TEST_P(SystemNudgeViewPixelTest, TitleAndLeadingImageWithButtons) {
auto nudge_data = CreateBaseNudgeData();
nudge_data.image_model = ui::ImageModel::FromVectorIcon(
vector_icons::kDogfoodIcon, cros_tokens::kCrosSysOnSurface,
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(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 1 : 1,
GetContentsView()));
}
TEST_P(SystemNudgeViewPixelTest, AnchoredNudgeWithPointyCorner) {
auto nudge_data = CreateBaseNudgeData();
auto* anchor_view =
GetContentsView()->AddChildView(std::make_unique<views::View>());
nudge_data.SetAnchorView(anchor_view);
nudge_data.arrow = views::BubbleBorder::Arrow::BOTTOM_RIGHT;
GetContentsView()->AddChildView(
std::make_unique<SystemNudgeView>(nudge_data));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 1 : 1,
GetContentsView()));
}
}