#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/permissions/permission_request_manager.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/color_utils.h"
#if BUILDFLAG(IS_MAC)
#include "chrome/browser/apps/app_shim/app_shim_manager_mac.h"
#endif
namespace web_app {
class WebAppNotificationsBrowserTest : public WebAppBrowserTestBase { … };
#if BUILDFLAG(IS_CHROMEOS)
using WebAppNotificationsBrowserTest_IconAndTitleEnabled =
WebAppNotificationsBrowserTest;
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_IconAndTitleEnabled,
PersistentNotificationIconAndTitle) {
const GURL app_url =
https_server()->GetURL("/web_app_notifications/index.html");
const webapps::AppId app_id = InstallWebAppFromPage(browser(), app_url);
SetAppBrowserForAppId(app_id);
EXPECT_TRUE(RequestAndAcceptPermission());
EXPECT_TRUE(AwaitScript("awaitServiceWorkerActivation()").ExtractBool());
{
EXPECT_TRUE(AwaitScript("displayPersistentNotification()").ExtractBool());
std::vector<message_center::Notification> notifications =
GetDisplayedNotifications(true);
ASSERT_EQ(1u, notifications.size());
const message_center::Notification& notification = notifications[0];
EXPECT_EQ(u"Notification Title", notification.title());
ASSERT_TRUE(notification.notifier_id().title.has_value());
EXPECT_EQ(u"Web App Notifications Test",
notification.notifier_id().title.value());
ASSERT_FALSE(notification.small_image().IsEmpty());
const SkBitmap monochrome_badge = *notification.small_image().ToSkBitmap();
EXPECT_EQ(16, monochrome_badge.width());
EXPECT_EQ(16, monochrome_badge.height());
EXPECT_EQ(
color_utils::SkColorToRgbaString(SK_ColorTRANSPARENT),
color_utils::SkColorToRgbaString(monochrome_badge.getColor(8, 8)));
EXPECT_EQ(
color_utils::SkColorToRgbaString(SK_ColorRED),
color_utils::SkColorToRgbaString(monochrome_badge.getColor(0, 0)));
EXPECT_TRUE(AwaitScript("closeAllPersistentNotifications()").ExtractBool());
}
{
EXPECT_TRUE(
AwaitScript("displayPersistentNotificationWithBadge()").ExtractBool());
std::vector<message_center::Notification> notifications =
GetDisplayedNotifications(true);
ASSERT_EQ(1u, notifications.size());
const message_center::Notification& notification = notifications[0];
EXPECT_EQ(u"Notification With Badge", notification.title());
ASSERT_TRUE(notification.notifier_id().title.has_value());
EXPECT_EQ(u"Web App Notifications Test",
notification.notifier_id().title.value());
ASSERT_FALSE(notification.small_image().IsEmpty());
const SkBitmap badge_from_js = *notification.small_image().ToSkBitmap();
EXPECT_EQ(32, badge_from_js.width());
EXPECT_EQ(32, badge_from_js.height());
EXPECT_EQ(color_utils::SkColorToRgbaString(SK_ColorBLUE),
color_utils::SkColorToRgbaString(badge_from_js.getColor(8, 8)));
EXPECT_TRUE(AwaitScript("closeAllPersistentNotifications()").ExtractBool());
}
}
#else
WebAppNotificationsBrowserTest_IconAndTitleDisabled;
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_IconAndTitleDisabled,
PersistentNotificationIconAndTitle) { … }
#endif
#if BUILDFLAG(IS_MAC)
class WebAppNotificationsBrowserTest_MacPermissions
: public WebAppNotificationsBrowserTest {
public:
WebAppNotificationsBrowserTest_MacPermissions()
: WebAppNotificationsBrowserTest(
{features::kAppShimNotificationAttribution},
{}) {}
void SetUpOnMainThread() override {
WebAppNotificationsBrowserTest::SetUpOnMainThread();
const GURL app_url =
https_server()->GetURL("/web_app_notifications/index.html");
const webapps::AppId app_id = InstallWebAppFromPage(browser(), app_url);
SetAppBrowserForAppId(app_id);
}
void TearDownOnMainThread() override {
test::UninstallAllWebApps(browser()->profile());
WebAppNotificationsBrowserTest::TearDownOnMainThread();
}
};
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_MacPermissions, Granted) {
EXPECT_EQ("granted", RequestAndRespondToPermission(
mac_notifications::mojom::RequestPermissionResult::
kPermissionGranted));
}
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_MacPermissions,
PreviouslyGranted_AcceptBubble) {
EXPECT_EQ("granted", RequestAndRespondToPermission(
mac_notifications::mojom::RequestPermissionResult::
kPermissionPreviouslyGranted,
permissions::PermissionRequestManager::ACCEPT_ALL));
}
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_MacPermissions,
PreviouslyGranted_DenyBubble) {
EXPECT_EQ("denied", RequestAndRespondToPermission(
mac_notifications::mojom::RequestPermissionResult::
kPermissionPreviouslyGranted,
permissions::PermissionRequestManager::DENY_ALL));
}
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_MacPermissions,
RequestFailed_AcceptBubble) {
EXPECT_EQ(
"granted",
RequestAndRespondToPermission(
mac_notifications::mojom::RequestPermissionResult::kRequestFailed,
permissions::PermissionRequestManager::ACCEPT_ALL));
}
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_MacPermissions,
RequestFailed_DenyBubble) {
EXPECT_EQ(
"denied",
RequestAndRespondToPermission(
mac_notifications::mojom::RequestPermissionResult::kRequestFailed,
permissions::PermissionRequestManager::DENY_ALL));
}
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_MacPermissions, Denied) {
EXPECT_EQ("denied", RequestAndRespondToPermission(
mac_notifications::mojom::RequestPermissionResult::
kPermissionDenied));
}
IN_PROC_BROWSER_TEST_F(WebAppNotificationsBrowserTest_MacPermissions,
PreviouslyDenied) {
EXPECT_EQ("default", RequestAndRespondToPermission(
mac_notifications::mojom::RequestPermissionResult::
kPermissionPreviouslyDenied));
}
#endif
}