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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "ash/public/cpp/message_center/arc_notification_manager_delegate.h"
#include "ash/public/cpp/message_center/arc_notifications_host_initializer.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/simple_test_clock.h"
#include "base/time/default_clock.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/chrome_app_deprecation/chrome_app_deprecation.h"
#include "chrome/browser/apps/app_service/publishers/arc_apps.h"
#include "chrome/browser/apps/app_service/publishers/arc_apps_factory.h"
#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/session/arc_session_manager.h"
#include "chrome/browser/badging/badge_manager.h"
#include "chrome/browser/badging/badge_manager_factory.h"
#include "chrome/browser/extensions/api/notifications/extension_notification_display_helper.h"
#include "chrome/browser/extensions/api/notifications/extension_notification_display_helper_factory.h"
#include "chrome/browser/extensions/api/notifications/extension_notification_handler.h"
#include "chrome/browser/extensions/api/notifications/notifications_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/profile_notification.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/web_applications/test/web_app_install_test_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chromeos/ash/experiences/arc/message_center/arc_notification_manager.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "chromeos/ash/experiences/arc/session/connection_holder.h"
#include "chromeos/ash/experiences/arc/test/arc_util_test_support.h"
#include "chromeos/ash/experiences/arc/test/connection_holder_util.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/browser/api/test/test_api.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notifier_id.h"
using extensions::Extension;
using extensions::ExtensionNotificationDisplayHelper;
using extensions::ExtensionNotificationDisplayHelperFactory;
namespace {
constexpr char kTestAppName1[] = "Test ARC App1";
constexpr char kTestAppName2[] = "Test ARC App2";
constexpr char kTestAppPackage1[] = "test.arc.app1.package";
constexpr char kTestAppPackage2[] = "test.arc.app2.package";
constexpr char kTestAppActivity1[] = "test.arc.app1.package.activity";
constexpr char kTestAppActivity2[] = "test.arc.app2.package.activity";
std::string GetTestAppId(const std::string& package_name,
const std::string& activity) {
return ArcAppListPrefs::GetAppId(package_name, activity);
}
std::vector<arc::mojom::AppInfoPtr> GetTestAppsList() {
std::vector<arc::mojom::AppInfoPtr> apps;
arc::mojom::AppInfoPtr app(arc::mojom::AppInfo::New());
app->name = kTestAppName1;
app->package_name = kTestAppPackage1;
app->activity = kTestAppActivity1;
app->sticky = false;
apps.push_back(std::move(app));
app = arc::mojom::AppInfo::New();
app->name = kTestAppName2;
app->package_name = kTestAppPackage2;
app->activity = kTestAppActivity2;
app->sticky = false;
apps.push_back(std::move(app));
return apps;
}
std::optional<bool> HasBadge(Profile* profile, const std::string& app_id) {
auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
std::optional<bool> has_badge;
proxy->AppRegistryCache().ForOneApp(
app_id, [&has_badge](const apps::AppUpdate& update) {
has_badge = update.HasBadge();
});
return has_badge;
}
void RemoveNotification(Profile* profile, const std::string& notification_id) {
const std::string profile_notification_id =
ProfileNotification::GetProfileNotificationId(
notification_id, ProfileNotification::GetProfileID(profile));
message_center::MessageCenter::Get()->RemoveNotification(
profile_notification_id, true);
}
void UninstallApp(Profile* profile, const std::string& app_id) {
auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
proxy->UninstallSilently(app_id, apps::UninstallSource::kAppList);
}
class ScopedBadgingClockOverride {
public:
ScopedBadgingClockOverride(badging::BadgeManager* badge_manager,
const base::Clock* clock)
: badge_manager_(badge_manager) {
previous_clock_ = badge_manager_->SetClockForTesting(clock);
}
~ScopedBadgingClockOverride() {
badge_manager_->SetClockForTesting(previous_clock_);
}
private:
const raw_ptr<badging::BadgeManager> badge_manager_;
raw_ptr<const base::Clock> previous_clock_;
};
} // namespace
class AppNotificationsExtensionApiTest : public extensions::ExtensionApiTest {
public:
const Extension* LoadExtensionAndWait(const std::string& test_name) {
base::FilePath extdir = test_data_dir_.AppendASCII(test_name);
// ExtensionApiTest::LoadExtension uses ChromeTestExtensionLoader, which
// waits for the extension background page to be ready before returning.
return LoadExtension(extdir);
}
const Extension* LoadAppWithWindowState(const std::string& test_name) {
const std::string& create_window_options =
base::StringPrintf("{\"state\":\"normal\"}");
base::FilePath extdir = test_data_dir_.AppendASCII(test_name);
const extensions::Extension* extension = LoadExtension(extdir);
EXPECT_TRUE(extension);
apps::chrome_app_deprecation::ScopedAddAppToAllowlistForTesting allowlist(
extension->id());
ExtensionTestMessageListener launched_listener("launched",
ReplyBehavior::kWillReply);
apps::AppServiceProxyFactory::GetForProfile(profile())->Launch(
extension->id(), ui::EF_SHIFT_DOWN, apps::LaunchSource::kFromTest);
EXPECT_TRUE(launched_listener.WaitUntilSatisfied());
launched_listener.Reply(create_window_options);
return extension;
}
ExtensionNotificationDisplayHelper* GetDisplayHelper() {
return ExtensionNotificationDisplayHelperFactory::GetForProfile(profile());
}
protected:
// Returns the notification that's being displayed for |extension|, or nullptr
// when the notification count is not equal to one. It's not safe to rely on
// the Notification pointer after closing the notification, but a copy can be
// made to continue to be able to access the underlying information.
message_center::Notification* GetNotificationForExtension(
const extensions::Extension* extension) {
DCHECK(extension);
std::set<std::string> notifications =
GetDisplayHelper()->GetNotificationIdsForExtension(extension->url());
if (notifications.size() != 1) {
return nullptr;
}
return GetDisplayHelper()->GetByNotificationId(*notifications.begin());
}
};
IN_PROC_BROWSER_TEST_F(AppNotificationsExtensionApiTest,
AddAndRemoveNotification) {
// Load the permission app which should not generate notifications.
const Extension* extension1 =
LoadExtensionAndWait("notifications/api/permission");
ASSERT_TRUE(extension1);
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
// Load the basic app to generate a notification.
ExtensionTestMessageListener notification_created_listener("created");
const Extension* extension2 =
LoadAppWithWindowState("notifications/api/basic_app");
ASSERT_TRUE(extension2);
ASSERT_TRUE(notification_created_listener.WaitUntilSatisfied());
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
ASSERT_TRUE(HasBadge(profile(), extension2->id()).value());
message_center::Notification* notification =
GetNotificationForExtension(extension2);
ASSERT_TRUE(notification);
RemoveNotification(profile(), notification->id());
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
ASSERT_FALSE(HasBadge(profile(), extension2->id()).value());
}
IN_PROC_BROWSER_TEST_F(AppNotificationsExtensionApiTest,
InstallAndUninstallApp) {
// Load the permission app which should not generate notifications.
const Extension* extension1 =
LoadExtensionAndWait("notifications/api/permission");
ASSERT_TRUE(extension1);
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
// Load the basic app to generate a notification.
ExtensionTestMessageListener notification_created_listener1("created");
const Extension* extension2 =
LoadAppWithWindowState("notifications/api/basic_app");
ASSERT_TRUE(extension2);
ASSERT_TRUE(notification_created_listener1.WaitUntilSatisfied());
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
ASSERT_TRUE(HasBadge(profile(), extension2->id()).value());
// Uninstall the basic app.
UninstallApp(profile(), extension2->id());
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
// Re-load the basic app to generate a notification again.
ExtensionTestMessageListener notification_created_listener2("created");
const Extension* extension3 =
LoadAppWithWindowState("notifications/api/basic_app");
ASSERT_TRUE(extension3);
ASSERT_TRUE(notification_created_listener2.WaitUntilSatisfied());
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
ASSERT_TRUE(HasBadge(profile(), extension3->id()).value());
// Remove the notification.
message_center::Notification* notification =
GetNotificationForExtension(extension3);
ASSERT_TRUE(notification);
RemoveNotification(profile(), notification->id());
ASSERT_FALSE(HasBadge(profile(), extension1->id()).value());
ASSERT_FALSE(HasBadge(profile(), extension3->id()).value());
}
class AppNotificationsWebNotificationTest
: public extensions::PlatformAppBrowserTest {
protected:
AppNotificationsWebNotificationTest() = default;
~AppNotificationsWebNotificationTest() override = default;
// extensions::PlatformAppBrowserTest:
void SetUpOnMainThread() override {
extensions::PlatformAppBrowserTest::SetUpOnMainThread();
https_server_.AddDefaultHandlers(GetChromeTestDataDir());
ASSERT_TRUE(https_server_.Start());
}
std::string CreateWebApp(const GURL& url, const GURL& scope) const {
auto web_app_info =
web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(url);
web_app_info->scope = scope;
std::string app_id = web_app::test::InstallWebApp(browser()->profile(),
std::move(web_app_info));
content::TestNavigationObserver navigation_observer(url);
navigation_observer.StartWatchingNewWebContents();
web_app::LaunchWebAppBrowser(browser()->profile(), app_id);
navigation_observer.WaitForNavigationFinished();
return app_id;
}
std::unique_ptr<message_center::Notification> CreateNotification(
const std::string& notification_id,
const GURL& origin) {
return std::make_unique<message_center::Notification>(
message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
std::u16string(), std::u16string(), ui::ImageModel(),
base::UTF8ToUTF16(origin.host()), origin,
message_center::NotifierId(origin),
message_center::RichNotificationData(), nullptr);
}
void UninstallWebApp(const std::string& app_id) const {
web_app::test::UninstallWebApp(browser()->profile(), app_id);
}
GURL GetOrigin() const { return https_server_.GetURL("app.com", "/"); }
GURL GetUrl1() const {
return https_server_.GetURL("app.com", "/ssl/google.html");
}
GURL GetScope1() const { return https_server_.GetURL("app.com", "/ssl/"); }
GURL GetUrl2() const {
return https_server_.GetURL("app.com", "/google/google.html");
}
GURL GetScope2() const { return https_server_.GetURL("app.com", "/google/"); }
GURL GetUrl3() const {
return https_server_.GetURL("app1.com", "/google/google.html");
}
GURL GetScope3() const {
return https_server_.GetURL("app1.com", "/google/");
}
private:
// For mocking a secure site.
net::EmbeddedTestServer https_server_;
};
IN_PROC_BROWSER_TEST_F(AppNotificationsWebNotificationTest,
AddAndRemovePersistentNotification) {
std::string app_id1 = CreateWebApp(GetUrl1(), GetScope1());
std::string app_id2 = CreateWebApp(GetUrl2(), GetScope2());
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
const GURL origin = GetOrigin();
std::string notification_id = "notification-id1";
auto notification = CreateNotification(notification_id, origin);
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
notification_id = "notification-id2";
notification = CreateNotification(notification_id, origin);
metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope2();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
}
// TODO(crbug.com/40846781): Disabled AppNotificationsWebNotificationTest.
// PersistentNotificationWhenInstallAndUninstallApp on chromeos and linux,
// because it is failing on linux-chromeos-dbg.
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
#define MAYBE_PersistentNotificationWhenInstallAndUninstallApp \
DISABLED_PersistentNotificationWhenInstallAndUninstallApp
#else
#define MAYBE_PersistentNotificationWhenInstallAndUninstallApp \
PersistentNotificationWhenInstallAndUninstallApp
#endif
IN_PROC_BROWSER_TEST_F(AppNotificationsWebNotificationTest,
MAYBE_PersistentNotificationWhenInstallAndUninstallApp) {
// Send a notification before installing apps.
const GURL origin = GetOrigin();
std::string notification_id = "notification-id2";
auto notification = CreateNotification(notification_id, origin);
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope2();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
// Install apps, and verify the notification badge is not set.
std::string app_id1 = CreateWebApp(GetUrl1(), GetScope1());
std::string app_id2 = CreateWebApp(GetUrl2(), GetScope2());
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Remove the notification. It should not affect the notification badge.
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Send a notification for the installed app 2.
notification_id = "notification-id3";
notification = CreateNotification(notification_id, origin);
metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope2();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
// Uninstall the app 2.
UninstallApp(profile(), app_id2);
// Re-install the app 2.
app_id2 = CreateWebApp(GetUrl2(), GetScope2());
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Remove the notification.
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Resend the notifications for both apps.
std::string notification_id1 = "notification-id4";
notification = CreateNotification(notification_id1, origin);
metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
std::string notification_id2 = "notification-id5";
notification = CreateNotification(notification_id2, origin);
metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope2();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
// Remove notifications.
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id1);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id2);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
}
IN_PROC_BROWSER_TEST_F(AppNotificationsWebNotificationTest,
AddAndRemoveNonPersistentNotificationForOneApp) {
const GURL origin = GetOrigin();
std::string app_id1 = CreateWebApp(GetUrl1(), GetScope1());
std::string app_id3 = CreateWebApp(GetUrl3(), GetScope3());
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
const std::string notification_id = "notification-id";
auto notification = CreateNotification(notification_id, origin);
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_NON_PERSISTENT, *notification,
/*metadata=*/nullptr);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
RemoveNotification(profile(), notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
}
IN_PROC_BROWSER_TEST_F(AppNotificationsWebNotificationTest,
AddAndRemoveNonPersistentNotification) {
const GURL origin = GetOrigin();
std::string app_id1 = CreateWebApp(GetUrl1(), GetScope1());
std::string app_id2 = CreateWebApp(GetUrl2(), GetScope2());
std::string app_id3 = CreateWebApp(GetUrl3(), GetScope3());
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
const std::string notification_id = "notification-id";
auto notification = CreateNotification(notification_id, origin);
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_NON_PERSISTENT, *notification,
/*metadata=*/nullptr);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
RemoveNotification(profile(), notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
}
IN_PROC_BROWSER_TEST_F(AppNotificationsWebNotificationTest,
NonPersistentNotificationWhenInstallAndUninstallApp) {
// Send the notification 1 before installing apps.
const GURL origin = GetOrigin();
const std::string notification_id1 = "notification-id1";
auto notification = CreateNotification(notification_id1, origin);
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_NON_PERSISTENT, *notification,
/*metadata=*/nullptr);
// Install apps.
std::string app_id1 = CreateWebApp(GetUrl1(), GetScope1());
std::string app_id2 = CreateWebApp(GetUrl2(), GetScope2());
std::string app_id3 = CreateWebApp(GetUrl3(), GetScope3());
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
// Send the notification 2.
const std::string notification_id2 = "notification-id2";
notification = CreateNotification(notification_id2, origin);
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_NON_PERSISTENT, *notification,
/*metadata=*/nullptr);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
// Uninstall the app 1. The notification badge for app 2 and app 3 should not
// be affected.
UninstallWebApp(app_id1);
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
// Re-install the app 1.
app_id1 = CreateWebApp(GetUrl1(), GetScope1());
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
// Send the notification 3.
const std::string notification_id3 = "notification-id3";
notification = CreateNotification(notification_id3, origin);
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_NON_PERSISTENT, *notification,
/*metadata=*/nullptr);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
// Remove the notification 3
RemoveNotification(profile(), notification_id3);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
// Remove the notification 1
RemoveNotification(profile(), notification_id1);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
// Remove the notification 2
RemoveNotification(profile(), notification_id2);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
ASSERT_FALSE(HasBadge(profile(), app_id3).value());
}
class WebAppBadgingTest : public AppNotificationsWebNotificationTest {
protected:
WebAppBadgingTest() = default;
~WebAppBadgingTest() override = default;
};
IN_PROC_BROWSER_TEST_F(WebAppBadgingTest, SetAndClearBadgeWithApi) {
ukm::TestUkmRecorder test_recorder;
badging::BadgeManager* badge_manager_ =
badging::BadgeManagerFactory::GetForProfile(profile());
std::string app_id = CreateWebApp(GetUrl1(), GetScope1());
ASSERT_FALSE(HasBadge(profile(), app_id).value());
badge_manager_->SetBadgeForTesting(app_id, 1, &test_recorder);
ASSERT_TRUE(HasBadge(profile(), app_id).value());
badge_manager_->ClearBadgeForTesting(app_id, &test_recorder);
ASSERT_FALSE(HasBadge(profile(), app_id).value());
}
IN_PROC_BROWSER_TEST_F(WebAppBadgingTest,
SetAndClearBadgeWithApiAndNotifications) {
ukm::TestUkmRecorder test_recorder;
badging::BadgeManager* badge_manager_ =
badging::BadgeManagerFactory::GetForProfile(profile());
std::string app_id = CreateWebApp(GetUrl1(), GetScope1());
ASSERT_FALSE(HasBadge(profile(), app_id).value());
badge_manager_->SetBadgeForTesting(app_id, 1, &test_recorder);
ASSERT_TRUE(HasBadge(profile(), app_id).value());
const std::string notification_id = "notification-id";
auto notification = CreateNotification(notification_id, GetOrigin());
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
ASSERT_TRUE(HasBadge(profile(), app_id).value());
badge_manager_->ClearBadgeForTesting(app_id, &test_recorder);
ASSERT_FALSE(HasBadge(profile(), app_id).value());
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id).value());
}
IN_PROC_BROWSER_TEST_F(WebAppBadgingTest, NotificationBeforeClearBadge) {
ukm::TestUkmRecorder test_recorder;
badging::BadgeManager* const badge_manager_ =
badging::BadgeManagerFactory::GetForProfile(profile());
base::SimpleTestClock clock;
clock.SetNow(base::Time::Now());
ScopedBadgingClockOverride clock_override(badge_manager_, &clock);
const std::string app_id = CreateWebApp(GetUrl1(), GetScope1());
const std::string notification_id = "notification-id";
const auto notification = CreateNotification(notification_id, GetOrigin());
{
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
}
badge_manager_->ClearBadgeForTesting(app_id, &test_recorder);
// One day under the kBadgingOverrideLifetime threshold.
clock.Advance(base::Days(13));
ASSERT_FALSE(HasBadge(profile(), app_id).value());
clock.Advance(base::Days(2));
ASSERT_FALSE(HasBadge(profile(), app_id).value());
{
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
}
ASSERT_TRUE(HasBadge(profile(), app_id).value());
}
IN_PROC_BROWSER_TEST_F(WebAppBadgingTest, NotificationAfterClearBadge) {
ukm::TestUkmRecorder test_recorder;
badging::BadgeManager* const badge_manager_ =
badging::BadgeManagerFactory::GetForProfile(profile());
base::SimpleTestClock clock;
clock.SetNow(base::Time::Now());
ScopedBadgingClockOverride clock_override(badge_manager_, &clock);
const std::string app_id = CreateWebApp(GetUrl1(), GetScope1());
const std::string notification_id = "notification-id";
const auto notification = CreateNotification(notification_id, GetOrigin());
badge_manager_->ClearBadgeForTesting(app_id, &test_recorder);
// One day under the kBadgingOverrideLifetime threshold.
clock.Advance(base::Days(13));
ASSERT_FALSE(HasBadge(profile(), app_id).value());
{
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
}
ASSERT_FALSE(HasBadge(profile(), app_id).value());
clock.Advance(base::Days(2));
{
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
}
ASSERT_TRUE(HasBadge(profile(), app_id).value());
}
IN_PROC_BROWSER_TEST_F(WebAppBadgingTest, NotificationAfterShowBadge) {
ukm::TestUkmRecorder test_recorder;
badging::BadgeManager* const badge_manager_ =
badging::BadgeManagerFactory::GetForProfile(profile());
base::SimpleTestClock clock;
clock.SetNow(base::Time::Now());
ScopedBadgingClockOverride clock_override(badge_manager_, &clock);
const std::string app_id = CreateWebApp(GetUrl1(), GetScope1());
const std::string notification_id = "notification-id";
const auto notification = CreateNotification(notification_id, GetOrigin());
badge_manager_->SetBadgeForTesting(app_id, 1, &test_recorder);
// One day under the kBadgingOverrideLifetime threshold.
clock.Advance(base::Days(13));
ASSERT_TRUE(HasBadge(profile(), app_id).value());
clock.Advance(base::Days(2));
ASSERT_TRUE(HasBadge(profile(), app_id).value());
{
auto metadata = std::make_unique<PersistentNotificationMetadata>();
metadata->service_worker_scope = GetScope1();
NotificationDisplayServiceFactory::GetForProfile(profile())->Display(
NotificationHandler::Type::WEB_PERSISTENT, *notification,
std::move(metadata));
}
ASSERT_TRUE(HasBadge(profile(), app_id).value());
NotificationDisplayServiceFactory::GetForProfile(profile())->Close(
NotificationHandler::Type::WEB_PERSISTENT, notification_id);
ASSERT_FALSE(HasBadge(profile(), app_id).value());
}
class FakeArcNotificationManagerDelegate
: public ash::ArcNotificationManagerDelegate {
public:
FakeArcNotificationManagerDelegate() = default;
~FakeArcNotificationManagerDelegate() override = default;
// ArcNotificationManagerDelegate:
bool IsManagedGuestSessionOrKiosk() const override { return false; }
void ShowMessageCenter() override {}
void HideMessageCenter() override {}
};
class AppNotificationsArcNotificationTest
: public extensions::PlatformAppBrowserTest {
protected:
// extensions::PlatformAppBrowserTest:
void SetUpCommandLine(base::CommandLine* command_line) override {
extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
arc::SetArcAvailableCommandLineForTesting(command_line);
}
void SetUpInProcessBrowserTestFixture() override {
extensions::PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
arc::ArcSessionManager::SetUiEnabledForTesting(false);
}
void SetUpOnMainThread() override {
extensions::PlatformAppBrowserTest::SetUpOnMainThread();
arc::SetArcPlayStoreEnabledForProfile(profile(), true);
// This ensures app_prefs()->GetApp() below never returns nullptr.
base::RunLoop run_loop;
app_prefs()->SetDefaultAppsReadyCallback(run_loop.QuitClosure());
run_loop.Run();
StartInstance();
arc_notification_manager_ = std::make_unique<ash::ArcNotificationManager>();
arc_notification_manager_->Init(
std::make_unique<FakeArcNotificationManagerDelegate>(),
EmptyAccountId(), message_center::MessageCenter::Get());
ash::ArcNotificationsHostInitializer::Observer* observer =
apps::ArcAppsFactory::GetInstance()->GetForProfile(profile());
observer->OnSetArcNotificationsInstance(arc_notification_manager_.get());
}
void TearDownOnMainThread() override {
arc_notification_manager_.reset();
StopInstance();
base::RunLoop().RunUntilIdle();
extensions::PlatformAppBrowserTest::TearDownOnMainThread();
}
void InstallTestApps() {
app_host()->OnAppListRefreshed(GetTestAppsList());
SendPackageAdded(kTestAppPackage1, false);
SendPackageAdded(kTestAppPackage2, false);
}
void SendPackageAdded(const std::string& package_name, bool package_synced) {
auto package_info = arc::mojom::ArcPackageInfo::New();
package_info->package_name = package_name;
package_info->package_version = 1;
package_info->last_backup_android_id = 1;
package_info->last_backup_time = 1;
package_info->sync = package_synced;
app_instance_->SendPackageAdded(std::move(package_info));
base::RunLoop().RunUntilIdle();
}
void SendPackageRemoved(const std::string& package_name) {
app_host()->OnPackageRemoved(package_name);
// Ensure async callbacks from the resulting observer calls are run.
base::RunLoop().RunUntilIdle();
}
void StartInstance() {
app_instance_ = std::make_unique<arc::FakeAppInstance>(app_host());
arc_bridge_service()->app()->SetInstance(app_instance_.get());
}
void StopInstance() {
if (app_instance_) {
arc_bridge_service()->app()->CloseInstance(app_instance_.get());
}
arc_session_manager()->Shutdown();
}
void CreateNotificationWithKey(const std::string& key,
const std::string& package_name) {
auto data = arc::mojom::ArcNotificationData::New();
data->key = key;
data->title = "TITLE";
data->message = "MESSAGE";
data->package_name = package_name;
arc_notification_manager_->OnNotificationPosted(std::move(data));
}
void RemoveNotificationWithKey(const std::string& key) {
arc_notification_manager_->OnNotificationRemoved(key);
}
ArcAppListPrefs* app_prefs() { return ArcAppListPrefs::Get(profile()); }
// Returns as AppHost interface in order to access to private implementation
// of the interface.
arc::mojom::AppHost* app_host() { return app_prefs(); }
private:
arc::ArcSessionManager* arc_session_manager() {
return arc::ArcSessionManager::Get();
}
arc::ArcBridgeService* arc_bridge_service() {
return arc::ArcServiceManager::Get()->arc_bridge_service();
}
std::unique_ptr<ash::ArcNotificationManager> arc_notification_manager_;
std::unique_ptr<arc::FakeAppInstance> app_instance_;
};
IN_PROC_BROWSER_TEST_F(AppNotificationsArcNotificationTest,
AddAndRemoveNotification) {
// Install app to remember existing apps.
InstallTestApps();
const std::string app_id1 = GetTestAppId(kTestAppPackage1, kTestAppActivity1);
const std::string app_id2 = GetTestAppId(kTestAppPackage2, kTestAppActivity2);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
const std::string notification_key1 = "notification_key1";
CreateNotificationWithKey(notification_key1, kTestAppPackage1);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
const std::string notification_key2 = "notification_key2";
CreateNotificationWithKey(notification_key2, kTestAppPackage2);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
RemoveNotificationWithKey(notification_key1);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
RemoveNotificationWithKey(notification_key2);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
}
IN_PROC_BROWSER_TEST_F(AppNotificationsArcNotificationTest,
MultipleNotificationsWhenUninstallApp) {
// Install apps to remember existing apps.
InstallTestApps();
const std::string app_id1 = GetTestAppId(kTestAppPackage1, kTestAppActivity1);
const std::string app_id2 = GetTestAppId(kTestAppPackage2, kTestAppActivity2);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Sent 2 notifications for the app 1.
const std::string notification_key1 = "notification_key1";
CreateNotificationWithKey(notification_key1, kTestAppPackage1);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
const std::string notification_key2 = "notification_key2";
CreateNotificationWithKey(notification_key2, kTestAppPackage1);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Remove the app 1.
SendPackageRemoved(app_id1);
// Sent 1 notification for the app 2.
const std::string notification_key3 = "notification_key3";
CreateNotificationWithKey(notification_key3, kTestAppPackage2);
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
// Remove the notification for the app 2.
RemoveNotificationWithKey(notification_key3);
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Sent 2 notifications for the app 2.
const std::string notification_key4 = "notification_key4";
CreateNotificationWithKey(notification_key4, kTestAppPackage2);
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
const std::string notification_key5 = "notification_key5";
CreateNotificationWithKey(notification_key5, kTestAppPackage1);
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
// Remove notifications for the app2.
RemoveNotificationWithKey(notification_key5);
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
RemoveNotificationWithKey(notification_key4);
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Remove the app 2.
SendPackageRemoved(app_id2);
}
IN_PROC_BROWSER_TEST_F(AppNotificationsArcNotificationTest,
MultipleNotificationsWhenInstallAndUninstallApp) {
// Install apps to remember existing apps.
InstallTestApps();
const std::string app_id1 = GetTestAppId(kTestAppPackage1, kTestAppActivity1);
const std::string app_id2 = GetTestAppId(kTestAppPackage2, kTestAppActivity2);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Sent 2 notifications for the app 1, and 1 notification for the app 2.
const std::string notification_key1 = "notification_key1";
CreateNotificationWithKey(notification_key1, kTestAppPackage1);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
const std::string notification_key2 = "notification_key2";
CreateNotificationWithKey(notification_key2, kTestAppPackage1);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Sent 1 notification for the app 2.
const std::string notification_key3 = "notification_key3";
CreateNotificationWithKey(notification_key3, kTestAppPackage2);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
RemoveNotificationWithKey(notification_key1);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
// Uninstall the app 2.
UninstallApp(profile(), app_id2);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
// Uninstall the app 1.
UninstallApp(profile(), app_id1);
// Reinstall apps
InstallTestApps();
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
// Sent 2 notifications for the app 2, and 1 notification for the app 1.
const std::string notification_key4 = "notification_key4";
CreateNotificationWithKey(notification_key4, kTestAppPackage2);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
const std::string notification_key5 = "notification_key5";
CreateNotificationWithKey(notification_key5, kTestAppPackage1);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
const std::string notification_key6 = "notification_key6";
CreateNotificationWithKey(notification_key6, kTestAppPackage2);
ASSERT_TRUE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
// Remove notifications
RemoveNotificationWithKey(notification_key5);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
RemoveNotificationWithKey(notification_key4);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_TRUE(HasBadge(profile(), app_id2).value());
RemoveNotificationWithKey(notification_key6);
ASSERT_FALSE(HasBadge(profile(), app_id1).value());
ASSERT_FALSE(HasBadge(profile(), app_id2).value());
}
|