#include <string>
#include "ash/constants/ash_features.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/system/toast/system_toast_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 "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icon_types.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 {
const std::u16string kTestText = u"text";
const std::u16string kTestLongText =
u"Nudge body text should be clear, short and succint (80 characters "
u"recommended)";
const std::u16string kTestButtonText = u"Button";
const gfx::VectorIcon* kTestIcon = &kSystemMenuBusinessIcon;
}
class SystemToastViewPixelTest
: public AshTestBase,
public testing::WithParamInterface</*enable_system_blur=*/bool> {
public:
void SetUp() override {
AshTestBase::SetUp();
test_widget_ = CreateFramelessTestWidget();
test_widget_->SetBounds(gfx::Rect(700, 70));
test_widget_->SetContentsView(
views::Builder<views::FlexLayoutView>()
.SetMainAxisAlignment(views::LayoutAlignment::kCenter)
.SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
.SetBackground(views::CreateSolidBackground(
cros_tokens::kCrosSysSystemBaseElevatedOpaque))
.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(
,
SystemToastViewPixelTest,
testing::Bool());
TEST_P(SystemToastViewPixelTest, TextOnly) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(kTestText));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
"screenshot",
pixel_test_helper()->IsSystemBlurEnabled() ? 7 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, WithLeadingIcon) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(
kTestText, SystemToastView::ButtonType::kNone,
std::u16string(),
&gfx::VectorIcon::EmptyIcon(),
base::DoNothing(),
kTestIcon));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 8 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, WithTextButton) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(
kTestText, SystemToastView::ButtonType::kTextButton,
kTestButtonText));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 3 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, WithIconButton) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(
kTestText, SystemToastView::ButtonType::kIconButton,
kTestButtonText, kTestIcon));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 3 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, WithLeadingIconAndTextButton) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(
kTestText, SystemToastView::ButtonType::kTextButton,
kTestButtonText,
&gfx::VectorIcon::EmptyIcon(),
base::DoNothing(),
kTestIcon));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 3 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, Multiline_TextOnly) {
GetContentsView()->AddChildView(
std::make_unique<SystemToastView>(kTestLongText));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 7 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, Multiline_WithLeadingIcon) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(
kTestLongText, SystemToastView::ButtonType::kNone,
std::u16string(),
&gfx::VectorIcon::EmptyIcon(),
base::DoNothing(),
kTestIcon));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 7 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, Multiline_WithTextButton) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(
kTestLongText, SystemToastView::ButtonType::kTextButton,
kTestButtonText));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 3 : 1,
GetContentsView()));
}
TEST_P(SystemToastViewPixelTest, Multiline_WithLeadingIconAndTextButton) {
GetContentsView()->AddChildView(std::make_unique<SystemToastView>(
kTestLongText, SystemToastView::ButtonType::kTextButton,
kTestButtonText,
&gfx::VectorIcon::EmptyIcon(),
base::DoNothing(),
kTestIcon));
EXPECT_TRUE(GetPixelDiffer()->CompareUiComponentsOnPrimaryScreen(
GenerateScreenshotName("screenshot"),
pixel_test_helper()->IsSystemBlurEnabled() ? 3 : 1,
GetContentsView()));
}
}