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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/toasts/api/toast_registry.h"
#include <memory>
#include "base/functional/callback_helpers.h"
#include "chrome/browser/ui/toasts/api/toast_id.h"
#include "chrome/browser/ui/toasts/api/toast_specification.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/vector_icon_types.h"
class ToastRegistryTest : public testing::Test {};
// By default, the toast specification must have an icon and body string.
TEST_F(ToastRegistryTest, DefaultToast) {
const int string_id = 0;
std::unique_ptr<ToastSpecification> spec =
ToastSpecification::Builder(vector_icons::kEmailIcon, string_id).Build();
EXPECT_EQ(string_id, spec->body_string_id());
EXPECT_FALSE(spec->has_close_button());
EXPECT_FALSE(spec->action_button_string_id().has_value());
EXPECT_TRUE(spec->action_button_callback().is_null());
EXPECT_FALSE(spec->has_menu());
}
TEST_F(ToastRegistryTest, ToastWithCloseButton) {
const int string_id = 0;
std::unique_ptr<ToastSpecification> spec =
ToastSpecification::Builder(vector_icons::kEmailIcon, string_id)
.AddCloseButton()
.Build();
EXPECT_EQ(string_id, spec->body_string_id());
EXPECT_TRUE(spec->has_close_button());
EXPECT_FALSE(spec->action_button_string_id().has_value());
EXPECT_TRUE(spec->action_button_callback().is_null());
EXPECT_FALSE(spec->has_menu());
}
TEST_F(ToastRegistryTest, ToastWithActionButton) {
const int body_string_id = 0;
const int action_button_string_id = 1;
std::unique_ptr<ToastSpecification> spec =
ToastSpecification::Builder(vector_icons::kEmailIcon, body_string_id)
.AddActionButton(action_button_string_id, base::DoNothing())
.AddCloseButton()
.Build();
EXPECT_EQ(body_string_id, spec->body_string_id());
EXPECT_TRUE(spec->has_close_button());
EXPECT_TRUE(spec->action_button_string_id().has_value());
EXPECT_EQ(action_button_string_id, spec->action_button_string_id().value());
EXPECT_FALSE(spec->action_button_callback().is_null());
EXPECT_FALSE(spec->has_menu());
// Toasts with an action button must have a close button.
EXPECT_DEATH(
ToastSpecification::Builder(vector_icons::kEmailIcon, body_string_id)
.AddActionButton(action_button_string_id, base::DoNothing())
.Build(),
"");
// A toast cannot have an action button, close button, and a menu.
EXPECT_DEATH(
ToastSpecification::Builder(vector_icons::kEmailIcon, body_string_id)
.AddActionButton(action_button_string_id, base::DoNothing())
.AddCloseButton()
.AddMenu()
.Build(),
"");
}
TEST_F(ToastRegistryTest, ToastWithMenu) {
const int body_string_id = 0;
std::unique_ptr<ToastSpecification> spec =
ToastSpecification::Builder(vector_icons::kEmailIcon, body_string_id)
.AddMenu()
.Build();
EXPECT_EQ(body_string_id, spec->body_string_id());
EXPECT_FALSE(spec->has_close_button());
EXPECT_FALSE(spec->action_button_string_id().has_value());
EXPECT_TRUE(spec->action_button_callback().is_null());
EXPECT_TRUE(spec->has_menu());
}
TEST_F(ToastRegistryTest, RegisterSpecification) {
std::unique_ptr<ToastSpecification> unique_spec =
ToastSpecification::Builder(vector_icons::kEmailIcon,
/*body_string_id=*/0)
.Build();
ToastSpecification* toast_specification = unique_spec.get();
std::unique_ptr<ToastRegistry> toast_registry =
std::make_unique<ToastRegistry>();
toast_registry->RegisterToast(ToastId::kImageCopied, std::move(unique_spec));
EXPECT_EQ(toast_specification,
toast_registry->GetToastSpecification(ToastId::kImageCopied));
EXPECT_FALSE(toast_registry->IsEmpty());
}
TEST_F(ToastRegistryTest, RegisterNullSpecification) {
std::unique_ptr<ToastRegistry> toast_registry =
std::make_unique<ToastRegistry>();
// ToastRegistry should hit a CHECK when we try to register a null
// ToastSpecification.
EXPECT_DEATH(toast_registry->RegisterToast(ToastId::kImageCopied, nullptr),
"");
}
TEST_F(ToastRegistryTest, RegisterDuplicateToastId) {
std::unique_ptr<ToastRegistry> toast_registry =
std::make_unique<ToastRegistry>();
toast_registry->RegisterToast(
ToastId::kImageCopied, ToastSpecification::Builder(
vector_icons::kEmailIcon, /*body_string_id=*/0)
.Build());
// Even though we are registering a slightly different toast, the
// ToastRegistry should still hit a CHECK because we are using an already
// registered ToastId.
EXPECT_DEATH(toast_registry->RegisterToast(
ToastId::kImageCopied,
ToastSpecification::Builder(vector_icons::kEmailIcon,
/*body_string_id=*/0)
.AddCloseButton()
.Build()),
"");
}
TEST_F(ToastRegistryTest, RetrieveUnregisteredToastId) {
std::unique_ptr<ToastRegistry> toast_registry =
std::make_unique<ToastRegistry>();
ASSERT_TRUE(toast_registry->IsEmpty());
// The ToastRegistry should hit a CHECK when we try to retrieve the
// ToastSpecification for an id that was not registered.
EXPECT_DEATH(toast_registry->GetToastSpecification(ToastId::kLinkCopied), "");
}
|