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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// 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/ash/growth/show_notification_action_performer.h"
#include <memory>
#include <optional>
#include "ash/test/ash_test_base.h"
#include "base/json/json_reader.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/ash/growth/mock_ui_performer_observer.h"
#include "chromeos/ash/components/growth/campaigns_logger.h"
#include "chromeos/ash/grit/ash_resources.h"
#include "chromeos/ui/vector_icons/vector_icons.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/message_center/message_center.h"
#include "url/gurl.h"
namespace {
constexpr char kShowNotificationParamTemplate[] = R"(
{
"title": "%s",
"message": "%s",
"sourceIcon": {
"builtInVectorIcon": 0
},
"image": {
"builtInImage": 2
},
"shouldLogCrOSEvents": true
}
)";
constexpr char kTestTitle[] = "test title";
constexpr char kTestMessage[] = "test message";
constexpr int kTestCampaignId = 10;
constexpr char kNotificationIdTemplate[] = "growth_campaign_%d";
} // namespace
class ShowNotificationActionPerformerTest : public ash::AshTestBase {
public:
ShowNotificationActionPerformerTest() = default;
ShowNotificationActionPerformerTest(
const ShowNotificationActionPerformerTest&) = delete;
ShowNotificationActionPerformerTest& operator=(
const ShowNotificationActionPerformerTest&) = delete;
~ShowNotificationActionPerformerTest() override = default;
void SetUp() override {
ash::AshTestBase::SetUp();
action_ = std::make_unique<ShowNotificationActionPerformer>();
scoped_observation_.Observe(action_.get());
message_center_ = message_center::MessageCenter::Get();
// No notifications should have been posted yet.
ASSERT_EQ(0u, message_center_->NotificationCount());
}
void TearDown() override {
message_center_ = nullptr;
AshTestBase::TearDown();
scoped_observation_.Reset();
}
ShowNotificationActionPerformer& action() { return *action_; }
void RunShowNotificationActionPerformerCallback(
growth::ActionResult result,
std::optional<growth::ActionResultReason> reason) {
if (result == growth::ActionResult::kSuccess) {
std::move(action_success_closure_).Run();
} else {
std::move(action_failed_closure_).Run();
}
}
bool VerifyActionResult(bool success) {
if (success) {
action_success_run_loop_.Run();
} else {
action_failed_run_loop_.Run();
}
return true;
}
protected:
raw_ptr<message_center::MessageCenter> message_center_;
MockUiPerformerObserver mock_observer_;
private:
base::RunLoop action_success_run_loop_;
base::RunLoop action_failed_run_loop_;
base::OnceClosure action_success_closure_ =
action_success_run_loop_.QuitClosure();
base::OnceClosure action_failed_closure_ =
action_failed_run_loop_.QuitClosure();
std::unique_ptr<ShowNotificationActionPerformer> action_;
growth::CampaignsLogger logger_;
base::ScopedObservation<UiActionPerformer, UiActionPerformer::Observer>
scoped_observation_{&mock_observer_};
};
TEST_F(ShowNotificationActionPerformerTest, TestValidParams) {
const auto valid_params = base::StringPrintf(kShowNotificationParamTemplate,
kTestTitle, kTestMessage);
auto value = base::JSONReader::Read(valid_params);
ASSERT_TRUE(value.has_value());
EXPECT_CALL(mock_observer_, OnReadyToLogImpression(
testing::Eq(kTestCampaignId),
testing::Eq(std::nullopt), testing::Eq(true)))
.Times(1);
action().Run(
/*campaign_id=*/kTestCampaignId, /*group_id=*/std::nullopt,
&value->GetDict(),
base::BindOnce(&ShowNotificationActionPerformerTest::
RunShowNotificationActionPerformerCallback,
base::Unretained(this)));
EXPECT_TRUE(VerifyActionResult(/*success=*/true));
const auto notification_id =
base::StringPrintf(kNotificationIdTemplate, kTestCampaignId);
message_center::Notification* notification =
message_center_->FindVisibleNotificationById(notification_id);
EXPECT_TRUE(notification);
EXPECT_EQ(notification->title(), base::UTF8ToUTF16(std::string(kTestTitle)));
EXPECT_EQ(notification->message(),
base::UTF8ToUTF16(std::string(kTestMessage)));
EXPECT_STREQ(chromeos::kRedeemIcon.name,
notification->vector_small_image().name);
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
const auto& expected_image =
ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_GROWTH_FRAMEWORK_SPARK_REBUY_PNG);
EXPECT_EQ(expected_image, notification->rich_notification_data().image);
#else
EXPECT_EQ(gfx::Image(), notification->rich_notification_data().image);
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
}
TEST_F(ShowNotificationActionPerformerTest, TestUnrecognizedImage) {
auto* valid_params = R"(
{
"title": "test title",
"message": "test message",
"sourceIcon": {
"builtInVectorIcon": 0
},
"image": {
"builtInImage": 20000
}
}
)";
auto value = base::JSONReader::Read(valid_params);
ASSERT_TRUE(value.has_value());
EXPECT_CALL(
mock_observer_,
OnReadyToLogImpression(testing::Eq(kTestCampaignId),
testing::Eq(std::nullopt), testing::Eq(false)))
.Times(1);
action().Run(
/*campaign_id=*/kTestCampaignId, /*group_id=*/std::nullopt,
&value->GetDict(),
base::BindOnce(&ShowNotificationActionPerformerTest::
RunShowNotificationActionPerformerCallback,
base::Unretained(this)));
EXPECT_TRUE(VerifyActionResult(/*success=*/true));
const auto notification_id =
base::StringPrintf(kNotificationIdTemplate, kTestCampaignId);
message_center::Notification* notification =
message_center_->FindVisibleNotificationById(notification_id);
EXPECT_TRUE(notification);
EXPECT_EQ(notification->title(), base::UTF8ToUTF16(std::string(kTestTitle)));
EXPECT_EQ(notification->message(),
base::UTF8ToUTF16(std::string(kTestMessage)));
EXPECT_STREQ(chromeos::kRedeemIcon.name,
notification->vector_small_image().name);
EXPECT_EQ(gfx::Image(), notification->rich_notification_data().image);
}
TEST_F(ShowNotificationActionPerformerTest, TestInvalidParams) {
auto* const invalid_params = "{}";
auto value = base::JSONReader::Read(invalid_params);
ASSERT_TRUE(value.has_value());
EXPECT_CALL(mock_observer_, OnReadyToLogImpression(
testing::Eq(kTestCampaignId),
testing::Eq(std::nullopt), testing::Eq(true)))
.Times(0);
action().Run(
/*campaign_id=*/kTestCampaignId, /*group_id=*/std::nullopt,
&value->GetDict(),
base::BindOnce(&ShowNotificationActionPerformerTest::
RunShowNotificationActionPerformerCallback,
base::Unretained(this)));
EXPECT_TRUE(VerifyActionResult(/*success=*/false));
EXPECT_FALSE(message_center_->FindVisibleNotificationById(
base::StringPrintf(kNotificationIdTemplate, kTestCampaignId)));
}
|