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 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
|
// Copyright 2022 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/web_applications/commands/fetch_manifest_and_install_command.h"
#include <memory>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/shortcuts/shortcut_icon_generator.h"
#include "chrome/browser/ui/web_applications/web_app_dialog_utils.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/test/command_metrics_test_helper.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/fake_web_app_ui_manager.h"
#include "chrome/browser/web_applications/test/fake_web_contents_manager.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/test_file_utils.h"
#include "chrome/browser/web_applications/test/web_app_icon_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/browser/web_applications/web_app_command_manager.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_icon_generator.h"
#include "chrome/browser/web_applications/web_app_icon_manager.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_params.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_screenshot_fetcher.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/browser/web_applications/web_contents/web_app_data_retriever.h"
#include "chrome/common/chrome_features.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_logging.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/browser/web_contents/web_app_url_loader.h"
#include "components/webapps/common/web_app_id.h"
#include "components/webapps/common/web_page_metadata.mojom.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
#include "net/http/http_status_code.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/manifest/manifest_util.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom-shared.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom-forward.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/color/color_provider_utils.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/test/sk_gmock_support.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#include "chromeos/ash/experiences/arc/mojom/intent_helper.mojom.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/test/connection_holder_util.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "chromeos/ash/experiences/arc/test/fake_intent_helper_host.h"
#include "chromeos/ash/experiences/arc/test/fake_intent_helper_instance.h"
#endif
namespace web_app {
namespace {
class FetchManifestAndInstallCommandTest : public WebAppTest {
public:
const GURL kWebAppUrl = GURL("https://example.com/path/index.html");
const webapps::AppId kWebAppId =
GenerateAppId(/*manifest_id=*/std::nullopt, kWebAppUrl);
const GURL kWebAppManifestUrl =
GURL("https://example.com/path/manifest.json");
const GURL kDefaultIconUrl = GURL("https://example.com/path/def_icon.png");
const SkColor kDefaultIconColor = SK_ColorYELLOW;
void SetUp() override {
WebAppTest::SetUp();
FakeWebAppProvider::Get(profile())->UseRealOsIntegrationManager();
test::AwaitStartWebAppProviderAndSubsystems(profile());
web_contents_manager().SetUrlLoaded(web_contents(), kWebAppUrl);
#if BUILDFLAG(IS_CHROMEOS)
arc_test_.SetUp(profile());
auto* arc_bridge_service =
arc_test_.arc_service_manager()->arc_bridge_service();
fake_intent_helper_host_ = std::make_unique<arc::FakeIntentHelperHost>(
arc_bridge_service->intent_helper());
fake_intent_helper_instance_ =
std::make_unique<arc::FakeIntentHelperInstance>();
arc_bridge_service->intent_helper()->SetInstance(
fake_intent_helper_instance_.get());
WaitForInstanceReady(arc_bridge_service->intent_helper());
#endif
}
void TearDown() override {
#if BUILDFLAG(IS_CHROMEOS)
arc_test_.arc_service_manager()
->arc_bridge_service()
->intent_helper()
->CloseInstance(fake_intent_helper_instance_.get());
fake_intent_helper_instance_.reset();
fake_intent_helper_host_.reset();
arc_test_.TearDown();
#endif
WebAppTest::TearDown();
}
WebAppProvider* provider() { return WebAppProvider::GetForTest(profile()); }
FakeWebAppUiManager& fake_ui_manager() {
return static_cast<FakeWebAppUiManager&>(fake_provider().ui_manager());
}
const base::HistogramTester& histogram_tester() const {
return histogram_tester_;
}
TestFileUtils& file_utils() {
return *fake_provider().file_utils()->AsTestFileUtils();
}
#if BUILDFLAG(IS_CHROMEOS)
ArcAppTest& arc_test() { return arc_test_; }
#endif
WebAppInstallDialogCallback CreateDialogCallback(
bool accept = true,
mojom::UserDisplayMode user_display_mode =
mojom::UserDisplayMode::kBrowser) {
return base::BindOnce(
[](bool accept, mojom::UserDisplayMode user_display_mode,
base::WeakPtr<WebAppScreenshotFetcher>,
content::WebContents* initiator_web_contents,
std::unique_ptr<WebAppInstallInfo> web_app_info,
WebAppInstallationAcceptanceCallback acceptance_callback) {
web_app_info->user_display_mode = user_display_mode;
std::move(acceptance_callback).Run(accept, std::move(web_app_info));
},
accept, user_display_mode);
}
blink::mojom::ManifestPtr CreateValidManifest() {
blink::mojom::ManifestPtr manifest = blink::mojom::Manifest::New();
manifest->name = u"foo";
manifest->short_name = u"bar";
manifest->start_url = kWebAppUrl;
manifest->id = GenerateManifestIdFromStartUrlOnly(kWebAppUrl);
manifest->display = blink::mojom::DisplayMode::kStandalone;
blink::Manifest::ImageResource icon;
icon.src = kDefaultIconUrl;
icon.sizes = {{144, 144}};
icon.purpose = {blink::mojom::ManifestImageResource_Purpose::ANY};
manifest->icons = {icon};
return manifest;
}
FakeWebContentsManager& web_contents_manager() {
return static_cast<FakeWebContentsManager&>(
provider()->web_contents_manager());
}
void SetupPageState(
blink::mojom::ManifestPtr opt_manifest = blink::mojom::ManifestPtr()) {
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.has_service_worker = true;
page_state.manifest_before_default_processing =
opt_manifest ? std::move(opt_manifest) : CreateValidManifest();
page_state.valid_manifest_for_web_app = true;
page_state.error_code = webapps::InstallableStatusCode::NO_ERROR_DETECTED;
// When using the default manifest, populate the default icon.
if (!opt_manifest) {
auto& icon_state =
web_contents_manager().GetOrCreateIconState(kDefaultIconUrl);
icon_state.bitmaps = {CreateSquareIcon(144, kDefaultIconColor)};
icon_state.http_status_code = 200;
}
}
void SetupIconState(IconsMap icons,
bool trigger_primary_page_changed = false,
int http_status_codes = 200) {
for (const auto& [url, icon] : icons) {
auto& icon_state = web_contents_manager().GetOrCreateIconState(url);
icon_state.http_status_code = http_status_codes;
icon_state.bitmaps = icon;
icon_state.trigger_primary_page_changed_if_fetched =
trigger_primary_page_changed;
}
}
webapps::InstallResultCode InstallAndWait(
webapps::WebappInstallSource install_surface,
WebAppInstallDialogCallback dialog_callback,
FallbackBehavior fallback_behavior =
FallbackBehavior::kCraftedManifestOnly) {
base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
install_future;
provider()->scheduler().FetchManifestAndInstall(
install_surface, web_contents()->GetWeakPtr(),
std::move(dialog_callback), install_future.GetCallback(),
fallback_behavior);
EXPECT_TRUE(install_future.Wait());
return install_future.Get<webapps::InstallResultCode>();
}
private:
base::HistogramTester histogram_tester_;
#if BUILDFLAG(IS_CHROMEOS)
ArcAppTest arc_test_;
std::unique_ptr<arc::FakeIntentHelperHost> fake_intent_helper_host_;
std::unique_ptr<arc::FakeIntentHelperInstance> fake_intent_helper_instance_;
#endif
};
TEST_F(FetchManifestAndInstallCommandTest, SuccessWithManifest) {
SetupPageState();
EXPECT_EQ(InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(
true, mojom::UserDisplayMode::kStandalone)),
webapps::InstallResultCode::kSuccessNewInstall);
auto& registrar = provider()->registrar_unsafe();
EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
registrar.GetInstallState(kWebAppId));
EXPECT_EQ(1, fake_ui_manager().num_reparent_tab_calls());
}
TEST_F(FetchManifestAndInstallCommandTest,
SuccessWithFallbackInstallWithManifest) {
SetupPageState();
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.opt_metadata =
FakeWebContentsManager::CreateMetadataWithTitle(u"test app");
EXPECT_EQ(InstallAndWait(
webapps::WebappInstallSource::MENU_CREATE_SHORTCUT,
CreateDialogCallback(true, mojom::UserDisplayMode::kStandalone),
FallbackBehavior::kAllowFallbackDataAlways),
webapps::InstallResultCode::kSuccessNewInstall);
EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
provider()->registrar_unsafe().GetInstallState(kWebAppId));
EXPECT_EQ(provider()->registrar_unsafe().GetAppShortName(kWebAppId), "foo");
EXPECT_EQ(1, fake_ui_manager().num_reparent_tab_calls());
}
TEST_F(FetchManifestAndInstallCommandTest,
SuccessWithFallbackInstallNoManifest) {
SetupPageState();
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.manifest_before_default_processing = nullptr;
page_state.opt_metadata =
FakeWebContentsManager::CreateMetadataWithTitle(u"test app");
EXPECT_EQ(InstallAndWait(
webapps::WebappInstallSource::MENU_CREATE_SHORTCUT,
CreateDialogCallback(true, mojom::UserDisplayMode::kStandalone),
FallbackBehavior::kAllowFallbackDataAlways),
webapps::InstallResultCode::kSuccessNewInstall);
EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
provider()->registrar_unsafe().GetInstallState(kWebAppId));
EXPECT_EQ(provider()->registrar_unsafe().GetAppShortName(kWebAppId),
"test app");
EXPECT_EQ(1, fake_ui_manager().num_reparent_tab_calls());
}
TEST_F(FetchManifestAndInstallCommandTest,
FallbackInstallWithFailToGetInstallInfo) {
// Not invoking SetUpPageStateForTesting() ensures that there is no data
// set to be fetched.
EXPECT_EQ(InstallAndWait(
webapps::WebappInstallSource::MENU_CREATE_SHORTCUT,
CreateDialogCallback(true, mojom::UserDisplayMode::kStandalone),
FallbackBehavior::kAllowFallbackDataAlways),
webapps::InstallResultCode::kGetWebAppInstallInfoFailed);
EXPECT_FALSE(provider()->registrar_unsafe().IsInRegistrar(kWebAppId));
EXPECT_EQ(0, fake_ui_manager().num_reparent_tab_calls());
}
TEST_F(FetchManifestAndInstallCommandTest, SuccessWithoutReparent) {
SetupPageState();
EXPECT_EQ(InstallAndWait(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(true, mojom::UserDisplayMode::kBrowser)),
webapps::InstallResultCode::kSuccessNewInstall);
EXPECT_EQ(0, fake_ui_manager().num_reparent_tab_calls());
}
TEST_F(FetchManifestAndInstallCommandTest, UserInstallDeclined) {
SetupPageState();
EXPECT_EQ(InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(
false, mojom::UserDisplayMode::kStandalone)),
webapps::InstallResultCode::kUserInstallDeclined);
EXPECT_FALSE(provider()->registrar_unsafe().IsInRegistrar(kWebAppId));
EXPECT_EQ(0, fake_ui_manager().num_reparent_tab_calls());
}
TEST_F(FetchManifestAndInstallCommandTest, Shutdown) {
SetupPageState();
webapps::InstallResultCode result;
bool result_populated = false;
base::RunLoop dialog_runloop;
auto dialog_callback = base::BindLambdaForTesting(
[&](base::WeakPtr<WebAppScreenshotFetcher>,
content::WebContents* initiator_web_contents,
std::unique_ptr<WebAppInstallInfo> web_app_info,
WebAppInstallationAcceptanceCallback acceptance_callback) {
std::move(acceptance_callback).Run(true, std::move(web_app_info));
dialog_runloop.Quit();
});
provider()->scheduler().FetchManifestAndInstall(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
web_contents()->GetWeakPtr(), std::move(dialog_callback),
base::BindLambdaForTesting(
[&](const webapps::AppId& id, webapps::InstallResultCode code) {
result_populated = true;
result = code;
}),
FallbackBehavior::kCraftedManifestOnly);
dialog_runloop.Run();
provider()->command_manager().Shutdown();
ASSERT_TRUE(result_populated);
EXPECT_EQ(result,
webapps::InstallResultCode::kCancelledOnWebAppProviderShuttingDown);
}
TEST_F(FetchManifestAndInstallCommandTest, WebContentsDestroyed) {
SetupPageState();
base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
install_future;
provider()->scheduler().FetchManifestAndInstall(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
web_contents()->GetWeakPtr(), CreateDialogCallback(),
install_future.GetCallback(), FallbackBehavior::kCraftedManifestOnly);
DeleteContents();
ASSERT_TRUE(install_future.Wait());
EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
webapps::InstallResultCode::kWebContentsDestroyed);
}
TEST_F(FetchManifestAndInstallCommandTest,
MainFrameNavigationDifferentOriginDestroysCommand) {
SetupPageState();
base::test::TestFuture<void> manifest_fetch_future;
base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
install_future;
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.on_manifest_fetch = manifest_fetch_future.GetCallback();
provider()->scheduler().FetchManifestAndInstall(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
web_contents()->GetWeakPtr(), CreateDialogCallback(),
install_future.GetCallback(), FallbackBehavior::kCraftedManifestOnly);
// Wait till we reach an async process, like manifest fetching.
EXPECT_TRUE(manifest_fetch_future.Wait());
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(GURL("https://abc.com/"));
EXPECT_TRUE(install_future.Wait());
EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
webapps::InstallResultCode::kCancelledDueToMainFrameNavigation);
}
TEST_F(FetchManifestAndInstallCommandTest,
MainFrameNavigationSameOriginAllowsSuccessfulCompletion) {
// Mimic a first navigation to the app url.
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(kWebAppUrl);
const GURL kWebAppUrl2 = GURL("https://example.com/path/index1.html");
SetupPageState();
base::test::TestFuture<void> manifest_fetch_future;
base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
install_future;
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.on_manifest_fetch = manifest_fetch_future.GetCallback();
provider()->scheduler().FetchManifestAndInstall(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
web_contents()->GetWeakPtr(), CreateDialogCallback(),
install_future.GetCallback(), FallbackBehavior::kCraftedManifestOnly);
// Wait till we reach an async process, then trigger navigation to another url
// with the same origin.
EXPECT_TRUE(manifest_fetch_future.Wait());
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(kWebAppUrl2);
EXPECT_TRUE(install_future.Wait());
// Verify that installation is successful.
EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
webapps::InstallResultCode::kSuccessNewInstall);
}
TEST_F(FetchManifestAndInstallCommandTest, WriteDataToDisk) {
struct TestIconInfo {
IconPurpose purpose;
std::string icon_url_name;
SkColor color;
std::vector<SquareSizePx> sizes_px;
std::string dir;
};
const std::array<TestIconInfo, 3> purpose_infos = {
TestIconInfo{IconPurpose::ANY,
"any",
SK_ColorGREEN,
{icon_size::k16, icon_size::k512},
"Icons"},
TestIconInfo{IconPurpose::MONOCHROME,
"monochrome",
SkColorSetARGB(0x80, 0x00, 0x00, 0x00),
{icon_size::k32, icon_size::k256},
"Icons Monochrome"},
TestIconInfo{IconPurpose::MASKABLE,
"maskable",
SK_ColorRED,
{icon_size::k64, icon_size::k96, icon_size::k128},
"Icons Maskable"}};
static_assert(
purpose_infos.size() == static_cast<int>(IconPurpose::kMaxValue) -
static_cast<int>(IconPurpose::kMinValue) + 1,
"All purposes covered");
auto manifest = CreateValidManifest();
// Prepare all the data to be fetched or downloaded.
IconsMap icons_map;
const GURL url = GURL("https://example.com/path");
// Remove the default icon.
manifest->icons = {};
for (const TestIconInfo& purpose_info : purpose_infos) {
for (SquareSizePx s : purpose_info.sizes_px) {
std::string size_str = base::NumberToString(s);
GURL icon_url =
url.Resolve(purpose_info.icon_url_name + size_str + ".png");
manifest->icons.push_back(
CreateSquareImageResource(icon_url, s, {purpose_info.purpose}));
icons_map[icon_url] = {CreateSquareIcon(s, purpose_info.color)};
}
}
int num_icons = icons_map.size();
// TestingProfile creates temp directory if TestingProfile::path_ is empty
// (i.e. if TestingProfile::Builder::SetPath was not called by a test
// fixture)
const base::FilePath web_apps_dir = GetWebAppsRootDirectory(profile());
const base::FilePath manifest_resources_directory =
GetManifestResourcesDirectory(web_apps_dir);
EXPECT_FALSE(file_utils().DirectoryExists(manifest_resources_directory));
SetupPageState(std::move(manifest));
SetupIconState(icons_map);
EXPECT_EQ(InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(true)),
webapps::InstallResultCode::kSuccessNewInstall);
EXPECT_TRUE(file_utils().DirectoryExists(manifest_resources_directory));
const base::FilePath temp_dir = web_apps_dir.AppendASCII("Temp");
EXPECT_TRUE(file_utils().DirectoryExists(temp_dir));
EXPECT_TRUE(file_utils().IsDirectoryEmpty(temp_dir));
const base::FilePath app_dir =
manifest_resources_directory.AppendASCII(kWebAppId);
EXPECT_TRUE(file_utils().DirectoryExists(app_dir));
for (const TestIconInfo& purpose_info : purpose_infos) {
SCOPED_TRACE(purpose_info.purpose);
const base::FilePath icons_dir = app_dir.AppendASCII(purpose_info.dir);
EXPECT_TRUE(file_utils().DirectoryExists(icons_dir));
std::map<SquareSizePx, SkBitmap> pngs =
ReadPngsFromDirectory(&file_utils(), icons_dir);
// The install does ResizeIconsAndGenerateMissing() only for ANY icons.
if (purpose_info.purpose == IconPurpose::ANY) {
// Icons are generated for all mandatory sizes in GetIconSizes() in
// addition to the input k16 and k512 sizes.
EXPECT_EQ(GetIconSizes().size() + 2UL, pngs.size());
// Excludes autogenerated sizes.
for (SquareSizePx s : GetIconSizes()) {
pngs.erase(s);
}
} else {
EXPECT_EQ(purpose_info.sizes_px.size(), pngs.size());
}
for (SquareSizePx size_px : purpose_info.sizes_px) {
SCOPED_TRACE(size_px);
ASSERT_TRUE(base::Contains(pngs, size_px));
SkBitmap icon_bitmap = pngs[size_px];
EXPECT_EQ(icon_bitmap.width(), icon_bitmap.height());
EXPECT_EQ(size_px, icon_bitmap.height());
EXPECT_EQ(purpose_info.color, pngs[size_px].getColor(0, 0));
pngs.erase(size_px);
}
EXPECT_TRUE(pngs.empty());
}
const int http_code_class_ok = 2; // HTTP_OK is 200.
histogram_tester().ExpectBucketCount(
"WebApp.Icon.HttpStatusCodeClassOnCreate", http_code_class_ok, num_icons);
histogram_tester().ExpectTotalCount("WebApp.Icon.HttpStatusCodeClassOnSync",
0);
histogram_tester().ExpectBucketCount("WebApp.Icon.DownloadedResultOnCreate",
IconsDownloadedResult::kCompleted, 1);
histogram_tester().ExpectBucketCount(
"WebApp.Icon.DownloadedHttpStatusCodeOnCreate",
net::HttpStatusCode::HTTP_OK, 1);
}
TEST_F(FetchManifestAndInstallCommandTest, GetIcons_PrimaryPageChanged) {
const base::FilePath web_apps_dir = GetWebAppsRootDirectory(profile());
const base::FilePath manifest_resources_directory =
GetManifestResourcesDirectory(web_apps_dir);
EXPECT_FALSE(file_utils().DirectoryExists(manifest_resources_directory));
auto manifest = CreateValidManifest();
// Prepare all the data to be fetched or downloaded.
IconsMap icons_map;
const GURL url = GURL("https://example.com/path");
GURL icon_url = url.Resolve("color.png");
manifest->icons = {
CreateSquareImageResource(icon_url, icon_size::k64, {IconPurpose::ANY})};
icons_map[icon_url] = {CreateSquareIcon(icon_size::k64, SK_ColorBLUE)};
SetupPageState(std::move(manifest));
SetupIconState(icons_map, /*trigger_primary_page_changed=*/true);
EXPECT_EQ(InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(true)),
webapps::InstallResultCode::kSuccessNewInstall);
EXPECT_TRUE(file_utils().DirectoryExists(manifest_resources_directory));
const base::FilePath temp_dir = web_apps_dir.AppendASCII("Temp");
EXPECT_TRUE(file_utils().DirectoryExists(temp_dir));
EXPECT_TRUE(file_utils().IsDirectoryEmpty(temp_dir));
for (const std::string icon_dir :
{"Icons", "Icons Monochrome", "Icons Maskable"}) {
const base::FilePath app_dir =
manifest_resources_directory.AppendASCII(kWebAppId);
EXPECT_TRUE(file_utils().DirectoryExists(app_dir));
const base::FilePath icons_dir = app_dir.AppendASCII(icon_dir);
EXPECT_TRUE(file_utils().DirectoryExists(icons_dir));
std::map<SquareSizePx, SkBitmap> pngs =
ReadPngsFromDirectory(&file_utils(), icons_dir);
if (icon_dir == "Icons") {
// Auto generated ANY icons.
EXPECT_EQ(GetIconSizes().size(), pngs.size());
EXPECT_TRUE(ContainsOneIconOfEachSize(pngs));
} else {
EXPECT_TRUE(pngs.empty());
}
}
histogram_tester().ExpectTotalCount("WebApp.Icon.HttpStatusCodeClassOnCreate",
0);
histogram_tester().ExpectTotalCount("WebApp.Icon.HttpStatusCodeClassOnSync",
0);
histogram_tester().ExpectBucketCount(
"WebApp.Icon.DownloadedResultOnCreate",
IconsDownloadedResult::kPrimaryPageChanged, 1);
histogram_tester().ExpectTotalCount("WebApp.Icon.DownloadedResultOnSync", 0);
histogram_tester().ExpectTotalCount(
"WebApp.Icon.DownloadedHttpStatusCodeOnCreate", 0);
histogram_tester().ExpectTotalCount(
"WebApp.Icon.DownloadedHttpStatusCodeOnSync", 0);
}
TEST_F(FetchManifestAndInstallCommandTest, GetIcons_IconNotFound) {
const base::FilePath web_apps_dir = GetWebAppsRootDirectory(profile());
const base::FilePath manifest_resources_directory =
GetManifestResourcesDirectory(web_apps_dir);
EXPECT_FALSE(file_utils().DirectoryExists(manifest_resources_directory));
auto manifest = CreateValidManifest();
IconsMap icons_map;
const GURL url = GURL("https://example.com/path");
GURL icon_url = url.Resolve("color.png");
manifest->icons = {
CreateSquareImageResource(icon_url, icon_size::k64, {IconPurpose::ANY})};
SetupPageState(std::move(manifest));
// Setting up an empty icons map should trigger a 404 response.
SetupIconState(icons_map);
EXPECT_EQ(InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(true)),
webapps::InstallResultCode::kSuccessNewInstall);
EXPECT_TRUE(file_utils().DirectoryExists(manifest_resources_directory));
const base::FilePath temp_dir = web_apps_dir.AppendASCII("Temp");
EXPECT_TRUE(file_utils().DirectoryExists(temp_dir));
EXPECT_TRUE(file_utils().IsDirectoryEmpty(temp_dir));
for (const std::string icon_dir :
{"Icons", "Icons Monochrome", "Icons Maskable"}) {
const base::FilePath app_dir =
manifest_resources_directory.AppendASCII(kWebAppId);
EXPECT_TRUE(file_utils().DirectoryExists(app_dir));
const base::FilePath icons_dir = app_dir.AppendASCII(icon_dir);
EXPECT_TRUE(file_utils().DirectoryExists(icons_dir));
std::map<SquareSizePx, SkBitmap> pngs =
ReadPngsFromDirectory(&file_utils(), icons_dir);
if (icon_dir == "Icons") {
// Auto generated ANY icons.
EXPECT_EQ(GetIconSizes().size(), pngs.size());
EXPECT_TRUE(ContainsOneIconOfEachSize(pngs));
} else {
EXPECT_TRUE(pngs.empty());
}
}
histogram_tester().ExpectBucketCount("WebApp.Icon.DownloadedResultOnCreate",
IconsDownloadedResult::kCompleted, 1);
histogram_tester().ExpectTotalCount("WebApp.Icon.DownloadedResultOnSync", 0);
histogram_tester().ExpectBucketCount(
"WebApp.Icon.DownloadedHttpStatusCodeOnCreate",
net::HttpStatusCode::HTTP_NOT_FOUND, 1);
histogram_tester().ExpectTotalCount(
"WebApp.Icon.DownloadedHttpStatusCodeOnSync", 0);
}
TEST_F(FetchManifestAndInstallCommandTest, WriteDataToDiskFailed) {
auto manifest = CreateValidManifest();
IconsMap icons_map;
GURL icon_url("https://example.com/app.ico");
manifest->icons.push_back(
CreateSquareImageResource(icon_url, icon_size::k64, {IconPurpose::ANY}));
icons_map[icon_url] = {CreateSquareIcon(icon_size::k32, SK_ColorRED)};
SetupPageState(std::move(manifest));
SetupIconState(icons_map);
const base::FilePath web_apps_dir = GetWebAppsRootDirectory(profile());
const base::FilePath manifest_resources_directory =
GetManifestResourcesDirectory(web_apps_dir);
EXPECT_TRUE(file_utils().CreateDirectory(manifest_resources_directory));
// Induce an error: Simulate "Disk Full" for writing icon files.
file_utils().SetRemainingDiskSpaceSize(1024);
EXPECT_EQ(InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(true)),
webapps::InstallResultCode::kWriteDataFailed);
const base::FilePath temp_dir = web_apps_dir.AppendASCII("Temp");
EXPECT_TRUE(file_utils().DirectoryExists(temp_dir));
EXPECT_TRUE(file_utils().IsDirectoryEmpty(temp_dir));
const base::FilePath app_dir =
manifest_resources_directory.AppendASCII(kWebAppId);
EXPECT_FALSE(file_utils().DirectoryExists(app_dir));
}
TEST_F(FetchManifestAndInstallCommandTest, WebContentsNavigates) {
SetupPageState();
base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
install_future;
provider()->scheduler().FetchManifestAndInstall(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
web_contents()->GetWeakPtr(),
CreateDialogCallback(/*accept=*/true,
mojom::UserDisplayMode::kStandalone),
install_future.GetCallback(), FallbackBehavior::kCraftedManifestOnly);
// The command is always started asynchronously, so this immediate
// navigation should test that it correctly handles navigation before
// starting.
content::WebContentsTester* tester =
content::WebContentsTester::For(web_contents());
ASSERT_TRUE(tester);
tester->NavigateAndCommit(GURL("https://other_origin.com/path/index.html"));
ASSERT_TRUE(install_future.Wait());
EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
webapps::InstallResultCode::kCancelledDueToMainFrameNavigation);
EXPECT_FALSE(provider()->registrar_unsafe().IsInRegistrar(kWebAppId));
}
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(FetchManifestAndInstallCommandTest, IntentToPlayStore) {
arc_test().app_instance()->set_is_installable(true);
auto manifest = CreateValidManifest();
blink::Manifest::RelatedApplication related_app;
related_app.platform = u"chromeos_play";
related_app.id = u"com.app.id";
manifest->related_applications.push_back(std::move(related_app));
SetupPageState(std::move(manifest));
EXPECT_EQ(InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(true)),
webapps::InstallResultCode::kIntentToPlayStore);
}
#endif
class FetchManifestAndInstallCommandUniversalInstallTest
: public FetchManifestAndInstallCommandTest {
public:
const GURL kFaviconUrl = GURL("https://example.com/favicon.png");
static constexpr SkColor kFaviconColor = SK_ColorGREEN;
const GURL kIconUrl = GURL("https://example.com/icon.png");
static constexpr SkColor kIconColor = SK_ColorCYAN;
const std::u16string kPageTitle = u"Page Title";
const base::FilePath kUnmaskableFavicon{
FILE_PATH_LITERAL("chrome/test/data/web_apps/pattern3-256.png")};
~FetchManifestAndInstallCommandUniversalInstallTest() override = default;
void SetupPageTitleAndIcons() {
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.favicon_url = kFaviconUrl;
page_state.favicon = {CreateSquareIcon(32, kFaviconColor),
CreateSquareIcon(64, kFaviconColor)};
page_state.title = kPageTitle;
}
void CreateCraftedAppPage() {
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.has_service_worker = false;
page_state.manifest_url = kWebAppManifestUrl;
page_state.manifest_before_default_processing = CreateValidManifest();
page_state.valid_manifest_for_web_app = true;
page_state.error_code = webapps::InstallableStatusCode::NO_ERROR_DETECTED;
}
};
TEST_F(FetchManifestAndInstallCommandUniversalInstallTest, CraftedApp) {
SetupPageTitleAndIcons();
CreateCraftedAppPage();
base::HistogramTester histogram_tester;
EXPECT_EQ(
InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(/*accept=*/true,
mojom::UserDisplayMode::kStandalone),
FallbackBehavior::kCraftedManifestOnly),
webapps::InstallResultCode::kSuccessNewInstall);
}
TEST_F(FetchManifestAndInstallCommandUniversalInstallTest, NoManifest) {
SetupPageTitleAndIcons();
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.error_code = webapps::InstallableStatusCode::NO_MANIFEST;
EXPECT_EQ(
InstallAndWait(webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
CreateDialogCallback(/*accept=*/true,
mojom::UserDisplayMode::kStandalone),
FallbackBehavior::kUseFallbackInfoWhenNotInstallable),
webapps::InstallResultCode::kSuccessNewInstall);
}
gfx::Image LoadTestPNG(const base::FilePath& path) {
base::FilePath data_root =
base::PathService::CheckedGet(base::DIR_SRC_TEST_DATA_ROOT);
base::FilePath image_path = data_root.Append(path);
std::string png_data;
ReadFileToString(image_path, &png_data);
return gfx::Image::CreateFrom1xPNGBytes(base::as_byte_span(png_data));
}
using FaviconOptions = std::variant<std::monostate, SkColor, base::FilePath>;
using ManifestConfig = std::tuple<
/*app_name=*/std::optional<std::u16string>,
/*favicon=*/FaviconOptions,
/*start_url=*/std::optional<GURL>,
/*manifest_id=*/std::optional<webapps::ManifestId>,
/*display_mode=*/std::optional<blink::mojom::DisplayMode>,
/*manifest_icons=*/std::optional<blink::Manifest::ImageResource>>;
class UniversalInstallComboTest
: public FetchManifestAndInstallCommandUniversalInstallTest,
public testing::WithParamInterface<ManifestConfig> {
public:
static std::string ParamToString(
testing::TestParamInfo<ManifestConfig> param) {
ManifestConfig& config = param.param;
std::string icons_name_part = "Absent";
if (std::get<5>(config)) {
std::string filename = std::get<5>(config)->src.ExtractFileName();
if (base::Contains(filename, ".")) {
icons_name_part = filename.substr(0, filename.find('.'));
}
}
return base::StrCat(
{"AppName",
std::get<0>(config) ? base::UTF16ToUTF8(std::get<0>(config).value())
: "Absent",
"_Favicon",
std::holds_alternative<std::monostate>(std::get<1>(config)) ? "Absent"
: std::holds_alternative<SkColor>(std::get<1>(config))
? ui::SkColorName(std::get<SkColor>(std::get<1>(config)))
: "IconPathSpecified",
"_StartUrl", std::get<2>(config) ? "Specified" : "Absent",
"_ManifestId", std::get<3>(config) ? "Specified" : "Absent",
"_DisplayMode",
std::get<4>(config) ? base::ToString(*std::get<4>(config)) : "Absent",
"_ManifestIcons", icons_name_part});
}
UniversalInstallComboTest() = default;
std::optional<std::u16string> GetAppName() {
return std::get<std::optional<std::u16string>>(GetParam());
}
std::optional<SkColor> GetFaviconColor() {
auto param = std::get<1>(GetParam());
if (!std::holds_alternative<SkColor>(param)) {
return std::nullopt;
}
return std::get<SkColor>(param);
}
std::optional<std::string> GetFaviconFilePath() {
auto param = std::get<1>(GetParam());
if (!std::holds_alternative<base::FilePath>(param)) {
return std::nullopt;
}
base::FilePath file_path = std::get<base::FilePath>(param);
return file_path.AsUTF8Unsafe();
}
std::optional<GURL> GetStartUrl() { return std::get<2>(GetParam()); }
std::optional<webapps::ManifestId> GetManifestIdentity() {
return std::get<3>(GetParam());
}
std::optional<blink::mojom::DisplayMode> GetDisplayMode() {
return std::get<std::optional<blink::mojom::DisplayMode>>(GetParam());
}
std::optional<blink::Manifest::ImageResource> GetIcon() {
return std::get<std::optional<blink::Manifest::ImageResource>>(GetParam());
}
bool IsInstallableOtherThanDisplay() {
return GetAppName() && GetStartUrl() && GetIcon() &&
!base::Contains(GetIcon()->src.spec(), "not_found");
}
bool IsCraftedApp() {
return IsInstallableOtherThanDisplay() &&
GetDisplayMode().value_or(blink::mojom::DisplayMode::kBrowser) !=
blink::mojom::DisplayMode::kBrowser;
}
bool IsDiyApp() { return !IsCraftedApp(); }
std::string_view GetBucketName() {
if (IsCraftedApp()) {
return "WebApp.NewCraftedAppInstalled.ByUser";
}
// The only other alternative is a DIY app.
return "WebApp.NewDiyAppInstalled.ByUser";
}
SkBitmap GenerateExpected256Icon() {
if (GetIcon() && !base::Contains(GetIcon()->src.spec(), "not_found")) {
return CreateSquareIcon(icon_size::k256, kIconColor);
} else if (IsDiyApp()) {
if (GetFaviconColor()) {
return CreateSquareIcon(icon_size::k256, GetFaviconColor().value());
} else if (GetFaviconFilePath()) {
gfx::Image favicon_icon = LoadTestPNG(kUnmaskableFavicon);
return favicon_icon.AsBitmap();
}
}
// This generates the letter icons.
return GenerateIcons(GetAppName().value_or(kPageTitle))[icon_size::k256];
}
void SetupPageFromParams() {
auto& page_state = web_contents_manager().GetOrCreatePageState(kWebAppUrl);
page_state.title = kPageTitle;
if (GetFaviconColor()) {
page_state.favicon_url = kFaviconUrl;
page_state.favicon = {CreateSquareIcon(32, GetFaviconColor().value()),
CreateSquareIcon(64, GetFaviconColor().value()),
CreateSquareIcon(256, GetFaviconColor().value())};
} else if (GetFaviconFilePath()) {
page_state.favicon_url = kFaviconUrl;
gfx::Image test_icon = LoadTestPNG(kUnmaskableFavicon);
page_state.favicon = {test_icon.AsBitmap()};
}
page_state.manifest_before_default_processing =
blink::mojom::Manifest::New();
auto& manifest = page_state.manifest_before_default_processing;
page_state.error_code = webapps::InstallableStatusCode::NO_ERROR_DETECTED;
if (GetAppName()) {
manifest->name = GetAppName().value();
manifest->short_name = GetAppName().value();
} else {
page_state.error_code =
webapps::InstallableStatusCode::MANIFEST_MISSING_NAME_OR_SHORT_NAME;
}
if (GetStartUrl()) {
manifest->start_url = GetStartUrl().value();
} else {
page_state.error_code =
webapps::InstallableStatusCode::START_URL_NOT_VALID;
}
if (GetManifestIdentity()) {
manifest->id = GetManifestIdentity().value();
}
if (GetDisplayMode()) {
manifest->display = blink::mojom::DisplayMode::kBrowser;
manifest->display_override = {GetDisplayMode().value()};
} else {
page_state.error_code =
webapps::InstallableStatusCode::MANIFEST_DISPLAY_NOT_SUPPORTED;
}
if (GetIcon()) {
manifest->icons = {GetIcon().value()};
GURL url = GetIcon()->src;
auto& icon_state = web_contents_manager().GetOrCreateIconState(url);
if (base::Contains(url.spec(), "not_found")) {
icon_state.bitmaps = {};
icon_state.http_status_code = 404;
} else {
icon_state.bitmaps = {CreateSquareIcon(144, kIconColor)};
icon_state.http_status_code = 200;
}
} else {
page_state.error_code = webapps::InstallableStatusCode::NO_ICON_AVAILABLE;
}
if (!blink::IsEmptyManifest(manifest)) {
page_state.manifest_url = kWebAppManifestUrl;
}
page_state.valid_manifest_for_web_app = IsInstallableOtherThanDisplay();
}
SkBitmap GetExpectedPlatformIconAtSize(int width) {
#if BUILDFLAG(IS_MAC)
if (IsDiyApp()) {
if (GetIcon().has_value() &&
!base::Contains(GetIcon()->src.spec(), "not_found") &&
!base::Contains(GetIcon()->src.spec(), "Absent")) {
gfx::Image test_icon = LoadTestPNG(base::FilePath(FILE_PATH_LITERAL(
"chrome/test/data/web_apps/diyapp_icon_image.png")));
SkBitmap test_bitmap = test_icon.AsBitmap();
return test_bitmap;
}
auto favicon_path = GetFaviconFilePath();
if (favicon_path && base::Contains(*favicon_path, "pattern3")) {
gfx::Image test_icon = LoadTestPNG(base::FilePath(FILE_PATH_LITERAL(
"chrome/test/data/web_apps/masked_pattern3-256.png")));
SkBitmap test_bitmap = test_icon.AsBitmap();
return test_bitmap;
}
if (GetFaviconColor() == SK_ColorBLUE) {
gfx::Image test_icon = LoadTestPNG(base::FilePath(FILE_PATH_LITERAL(
"chrome/test/data/web_apps/diyapp_blue_image.png")));
SkBitmap test_bitmap = test_icon.AsBitmap();
return test_bitmap;
}
if (GetAppName() == u"AppName") {
gfx::Image test_icon = LoadTestPNG(base::FilePath(FILE_PATH_LITERAL(
"chrome/test/data/web_apps/diyapp_textA_image.png")));
SkBitmap test_bitmap = test_icon.AsBitmap();
return test_bitmap;
}
gfx::Image test_icon = LoadTestPNG(base::FilePath(FILE_PATH_LITERAL(
"chrome/test/data/web_apps/diyapp_textP_image.png")));
SkBitmap test_bitmap = test_icon.AsBitmap();
return test_bitmap;
}
#endif
// Note: These should be static test images instead of dynamically
// generating these using the same production code.
if (GetIcon().has_value() &&
!base::Contains(GetIcon()->src.spec(), "not_found")) {
return CreateSquareIcon(width, kIconColor);
}
if (GetFaviconColor()) {
return CreateSquareIcon(width, *GetFaviconColor());
}
// If no icon is provided, then an icon is generated using the first letter
// of the app name or page title.
const std::u16string name = GetAppName().value_or(u"P");
return shortcuts::GenerateBitmap(
width, shortcuts::GenerateIconLetterFromName(name));
}
};
TEST_P(UniversalInstallComboTest, InstallStateValid) {
SetupPageFromParams();
base::HistogramTester histogram_tester;
base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
install_future;
provider()->scheduler().FetchManifestAndInstall(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
web_contents()->GetWeakPtr(),
CreateDialogCallback(/*accept=*/true,
mojom::UserDisplayMode::kStandalone),
install_future.GetCallback(),
FallbackBehavior::kUseFallbackInfoWhenNotInstallable);
EXPECT_TRUE(install_future.Wait());
EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
webapps::InstallResultCode::kSuccessNewInstall);
webapps::AppId app_id = install_future.Get<webapps::AppId>();
ASSERT_EQ(proto::InstallState::INSTALLED_WITH_OS_INTEGRATION,
provider()->registrar_unsafe().GetInstallState(app_id));
auto& registrar = provider()->registrar_unsafe();
std::string name = base::UTF16ToUTF8(GetAppName().value_or(kPageTitle));
EXPECT_EQ(registrar.GetAppShortName(app_id), name);
GURL start_url = GetStartUrl().value_or(kWebAppUrl);
EXPECT_EQ(registrar.GetAppStartUrl(app_id), start_url);
webapps::ManifestId manifest_id =
GetManifestIdentity().value_or(GetStartUrl().value_or(kWebAppUrl));
EXPECT_EQ(registrar.GetAppManifestId(app_id), manifest_id);
auto display_mode =
GetDisplayMode().value_or(blink::mojom::DisplayMode::kMinimalUi);
EXPECT_EQ(registrar.GetAppEffectiveDisplayMode(app_id), display_mode);
base::test::TestFuture<std::map<SquareSizePx, SkBitmap>> icons_future;
provider()->icon_manager().ReadIcons(
app_id, IconPurpose::ANY, {icon_size::k256}, icons_future.GetCallback());
ASSERT_TRUE(icons_future.Wait());
std::map<SquareSizePx, SkBitmap> bitmaps = icons_future.Get();
EXPECT_THAT(bitmaps[icon_size::k256],
gfx::test::EqualsBitmap(GenerateExpected256Icon()));
EXPECT_EQ(IsDiyApp(), provider()->registrar_unsafe().IsDiyApp(app_id));
// TODO(https://crbug.com/385198125): Improve GetShortcutIcon to take a size
// that is actually respected across all platforms.
auto bitmap = fake_os_integration().GetShortcutIcon(
profile(), std::nullopt, app_id, name, icon_size::k128);
ASSERT_TRUE(bitmap.has_value());
// TODO(https://crbug.com/385218415): Check icons here against static test
// data, and on all platforms.
#if BUILDFLAG(IS_LINUX)
EXPECT_THAT(*bitmap,
gfx::test::EqualsBitmap(GetExpectedPlatformIconAtSize(128)))
<< bitmap->width() << "x" << bitmap->height() << ", with center color "
<< ui::SkColorName(
bitmap->getColor(bitmap->width() / 2, bitmap->height() / 2));
#elif BUILDFLAG(IS_MAC)
EXPECT_TRUE(gfx::test::AreBitmapsClose(
*bitmap, GetExpectedPlatformIconAtSize(bitmap->width()),
/*max_deviation=*/3))
<< bitmap->width() << "x" << bitmap->height() << ", with center color "
<< ui::SkColorName(
bitmap->getColor(bitmap->width() / 2, bitmap->height() / 2));
#endif
EXPECT_THAT(histogram_tester.GetAllSamples(GetBucketName()),
base::BucketsAre(base::Bucket(/*true=*/1, 1)));
std::string_view app_type_str = IsDiyApp() ? ".Diy" : ".Crafted";
EXPECT_THAT(histogram_tester,
test::ForAllGetAllSamples(
test::GetInstallCommandResultHistogramNames(
".FetchManifestAndInstall", app_type_str),
base::BucketsAre(base::Bucket(
webapps::InstallResultCode::kSuccessNewInstall, 1))));
EXPECT_THAT(histogram_tester,
test::ForAllGetAllSamples(
test::GetInstallCommandSourceHistogramNames(
".FetchManifestAndInstall", app_type_str),
base::BucketsAre(base::Bucket(
webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON, 1))));
}
INSTANTIATE_TEST_SUITE_P(
,
UniversalInstallComboTest,
::testing::Combine(
testing::Values(u"AppName", std::nullopt),
testing::Values(FaviconOptions(SK_ColorBLUE),
std::monostate()
#if BUILDFLAG(IS_MAC)
,
FaviconOptions(base::FilePath(FILE_PATH_LITERAL(
"chrome/test/data/web_apps/pattern3-256.png")))
#endif
),
// Note: the name just specified if the start_url exists or not - to add
// more values, the ParamToString function must be changed.
testing::Values(GURL("https://example.com/path/index.html?start_url"),
std::nullopt),
// Note: the name just specified if the manifest_id exists or not - to
// add more values, the ParamToString function must be changed.
testing::Values(GURL("https://example.com/path/index.html?manifest_id"),
std::nullopt),
testing::Values(blink::mojom::DisplayMode::kStandalone, std::nullopt),
testing::Values(
CreateSquareImageResource(GURL("https://example.com/icon.png"),
144,
{IconPurpose::ANY}),
CreateSquareImageResource(
GURL("https://example.com/not_found_icon.png"),
144,
{IconPurpose::ANY}),
std::nullopt)),
UniversalInstallComboTest::ParamToString);
} // namespace
} // namespace web_app
|