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
|
// Copyright 2018 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/web_contents/web_app_data_retriever.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/webapps/browser/installable/fake_installable_manager.h"
#include "components/webapps/browser/installable/installable_data.h"
#include "components/webapps/browser/installable/installable_logging.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/common/web_app_id.h"
#include "components/webapps/common/web_page_metadata.mojom.h"
#include "components/webapps/common/web_page_metadata_agent.mojom-test-utils.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/site_instance.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_contents_tester.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
namespace web_app {
namespace {
const char16_t kFooTitle[] = u"Foo Title";
} // namespace
class FakeWebPageMetadataAgent
: public webapps::mojom::WebPageMetadataAgentInterceptorForTesting {
public:
FakeWebPageMetadataAgent() = default;
~FakeWebPageMetadataAgent() override = default;
WebPageMetadataAgent* GetForwardingInterface() override { NOTREACHED(); }
void Bind(mojo::ScopedInterfaceEndpointHandle handle) {
receiver_.Bind(mojo::PendingAssociatedReceiver<WebPageMetadataAgent>(
std::move(handle)));
}
void SetWebPageMetadata(const GURL& application_url,
const std::u16string& title,
const std::u16string& description) {
web_page_metadata_->application_name = title;
web_page_metadata_->description = description;
web_page_metadata_->application_url = application_url;
}
void GetWebPageMetadata(GetWebPageMetadataCallback callback) override {
std::move(callback).Run(web_page_metadata_.Clone());
}
private:
webapps::mojom::WebPageMetadataPtr web_page_metadata_ =
webapps::mojom::WebPageMetadata::New();
mojo::AssociatedReceiver<webapps::mojom::WebPageMetadataAgent> receiver_{
this};
};
class WebAppDataRetrieverTest : public ChromeRenderViewHostTestHarness {
public:
WebAppDataRetrieverTest() = default;
WebAppDataRetrieverTest(const WebAppDataRetrieverTest&) = delete;
WebAppDataRetrieverTest& operator=(const WebAppDataRetrieverTest&) = delete;
~WebAppDataRetrieverTest() override = default;
// Set fake WebPageMetadataAgent to avoid mojo connection errors.
void SetFakeWebPageMetadataAgent() {
web_contents()
->GetPrimaryMainFrame()
->GetRemoteAssociatedInterfaces()
->OverrideBinderForTesting(
webapps::mojom::WebPageMetadataAgent::Name_,
base::BindRepeating(&FakeWebPageMetadataAgent::Bind,
base::Unretained(&fake_chrome_render_frame_)));
// When ProactivelySwapBrowsingInstance or RenderDocument is enabled on
// same-site main-frame navigations, a same-site navigation might result in
// a change of RenderFrames, which WebAppDataRetriever does not track (it
// tracks the old RenderFrame where the navigation started in). So we
// should disable same-site proactive BrowsingInstance for the main frame.
// Note: this will not disable RenderDocument.
// TODO(crbug.com/40615943): Make WebAppDataRetriever support a change of
// RenderFrames.
content::DisableProactiveBrowsingInstanceSwapFor(
web_contents()->GetPrimaryMainFrame());
}
void SetRendererWebPageMetadata(const GURL& application_url,
const std::u16string& title,
const std::u16string& description) {
fake_chrome_render_frame_.SetWebPageMetadata(application_url, title,
description);
}
void GetWebAppInstallInfoCallback(
base::OnceClosure quit_closure,
std::unique_ptr<WebAppInstallInfo> web_app_info) {
web_app_info_ = std::move(web_app_info);
std::move(quit_closure).Run();
}
void GetIconsCallback(base::OnceClosure quit_closure,
std::vector<apps::IconInfo> icons) {
icons_ = std::move(icons);
std::move(quit_closure).Run();
}
protected:
content::WebContentsTester* web_contents_tester() {
return content::WebContentsTester::For(web_contents());
}
const std::unique_ptr<WebAppInstallInfo>& web_app_info() {
return web_app_info_.value();
}
const std::vector<apps::IconInfo>& icons() { return icons_; }
private:
FakeWebPageMetadataAgent fake_chrome_render_frame_;
std::optional<std::unique_ptr<WebAppInstallInfo>> web_app_info_;
std::vector<apps::IconInfo> icons_;
};
TEST_F(WebAppDataRetrieverTest, GetWebAppInstallInfo_NoEntry) {
SetFakeWebPageMetadataAgent();
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
EXPECT_EQ(nullptr, web_app_info());
}
TEST_F(WebAppDataRetrieverTest, GetWebAppInstallInfo_AppUrlAbsent) {
SetFakeWebPageMetadataAgent();
const GURL kFooUrl("https://foo.example");
web_contents_tester()->NavigateAndCommit(kFooUrl);
// No install info present.
SetRendererWebPageMetadata(/*application_url=*/GURL(), /*title=*/u"",
/*description=*/u"");
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
// If the WebAppInstallInfo has no URL, we fallback to the last committed
// URL.
EXPECT_EQ(kFooUrl, web_app_info()->start_url());
}
TEST_F(WebAppDataRetrieverTest, GetWebAppInstallInfo_AppUrlPresent) {
SetFakeWebPageMetadataAgent();
web_contents_tester()->NavigateAndCommit(GURL("https://foo.example"));
GURL other_app_url = GURL("https://bar.example");
std::u16string other_app_title = u"Other App Title";
SetRendererWebPageMetadata(other_app_url, other_app_title,
/*description=*/u"");
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
EXPECT_EQ(other_app_url, web_app_info()->start_url());
EXPECT_EQ(other_app_title, web_app_info()->title);
}
TEST_F(WebAppDataRetrieverTest, GetWebAppInstallInfo_TitleAbsentFromRenderer) {
SetFakeWebPageMetadataAgent();
web_contents_tester()->NavigateAndCommit(GURL("https://foo.example"));
web_contents_tester()->SetTitle(kFooTitle);
SetRendererWebPageMetadata(GURL("https://foo.example"), /*title=*/u"",
/*description=*/u"");
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
// If the metadata has no title, we fallback to the WebContents title.
EXPECT_EQ(kFooTitle, web_app_info()->title);
}
TEST_F(WebAppDataRetrieverTest,
GetWebAppInstallInfo_TitleAbsentFromWebContents) {
SetFakeWebPageMetadataAgent();
web_contents_tester()->NavigateAndCommit(GURL("https://foo.example"));
web_contents_tester()->SetTitle(u"");
SetRendererWebPageMetadata(GURL("https://foo.example"), /*title=*/u"",
/*description=*/u"");
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
// If the WebAppInstallInfo has no title and the WebContents has no title, we
// fallback to start_url.
EXPECT_EQ(base::UTF8ToUTF16(web_app_info()->start_url().spec()),
web_app_info()->title);
}
TEST_F(WebAppDataRetrieverTest, GetWebAppInstallInfo_ConnectionError) {
// Do not set fake WebPageMetadataAgent to simulate connection error.
web_contents_tester()->NavigateAndCommit(GURL("https://foo.example"));
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
EXPECT_EQ(nullptr, web_app_info());
}
TEST_F(WebAppDataRetrieverTest, GetWebAppInstallInfo_WebContentsDestroyed) {
SetFakeWebPageMetadataAgent();
web_contents_tester()->NavigateAndCommit(GURL("https://foo.example"));
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
DeleteContents();
run_loop.Run();
EXPECT_EQ(nullptr, web_app_info());
}
TEST_F(WebAppDataRetrieverTest,
CheckInstallabilityAndRetrieveManifest_WebContentsDestroyed) {
SetFakeWebPageMetadataAgent();
web_contents_tester()->NavigateAndCommit(GURL("https://foo.example"));
{
webapps::FakeInstallableManager::CreateForWebContentsWithManifest(
web_contents(), webapps::InstallableStatusCode::NO_MANIFEST, GURL(),
blink::mojom::Manifest::New());
}
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.CheckInstallabilityAndRetrieveManifest(
web_contents(),
base::BindLambdaForTesting(
[&](blink::mojom::ManifestPtr opt_manifest,
bool valid_manifest_for_web_app,
webapps::InstallableStatusCode error_code) {
EXPECT_FALSE(opt_manifest);
EXPECT_FALSE(valid_manifest_for_web_app);
EXPECT_EQ(error_code,
webapps::InstallableStatusCode::RENDERER_CANCELLED);
run_loop.Quit();
}));
DeleteContents();
run_loop.Run();
}
TEST_F(WebAppDataRetrieverTest, GetIcons_WebContentsDestroyed) {
SetFakeWebPageMetadataAgent();
web_contents_tester()->NavigateAndCommit(GURL("https://foo.example"));
bool skip_page_favicons = true;
bool fail_all_if_any_fail = false;
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetIcons(web_contents(),
/*extra_favicon_urls=*/IconUrlSizeSet(),
skip_page_favicons, fail_all_if_any_fail,
base::BindLambdaForTesting(
[&](IconsDownloadedResult result, IconsMap icons_map,
DownloadedIconsHttpResults icons_http_results) {
EXPECT_TRUE(icons_map.empty());
run_loop.Quit();
}));
DeleteContents();
run_loop.Run();
}
TEST_F(WebAppDataRetrieverTest, GetWebAppInstallInfo_FrameNavigated) {
SetFakeWebPageMetadataAgent();
web_contents_tester()->SetTitle(kFooTitle);
const GURL kFooUrl("https://foo.example/bar");
web_contents_tester()->NavigateAndCommit(kFooUrl.DeprecatedGetOriginAsURL());
base::RunLoop run_loop;
WebAppDataRetriever retriever;
retriever.GetWebAppInstallInfo(
web_contents(),
base::BindOnce(&WebAppDataRetrieverTest::GetWebAppInstallInfoCallback,
base::Unretained(this), run_loop.QuitClosure()));
web_contents_tester()->NavigateAndCommit(kFooUrl);
run_loop.Run();
if (web_contents()
->GetPrimaryMainFrame()
->ShouldChangeRenderFrameHostOnSameSiteNavigation()) {
// If the RenderFrameHost changes, the FakeWebPageMetadataAgent mojo
// connection will be disconnected, causing the callback to be called with
// a null info.
EXPECT_EQ(nullptr, web_app_info());
} else {
// Otherwise, the mojo connection will persist and the callback will get
// the info from the previous document.
EXPECT_EQ(kFooUrl.DeprecatedGetOriginAsURL(), web_app_info()->start_url());
EXPECT_EQ(kFooTitle, web_app_info()->title);
}
}
TEST_F(WebAppDataRetrieverTest, CheckInstallabilityAndRetrieveManifest) {
SetFakeWebPageMetadataAgent();
const GURL manifest_start_url = GURL("https://example.com/start");
const std::u16string manifest_short_name = u"Short Name from Manifest";
const std::u16string manifest_name = u"Name from Manifest";
const GURL manifest_scope = GURL("https://example.com/scope");
const SkColor manifest_theme_color = 0xAABBCCDD;
{
auto manifest = blink::mojom::Manifest::New();
manifest->manifest_url = GURL("https://example.com/manifest");
manifest->short_name = manifest_short_name;
manifest->name = manifest_name;
manifest->start_url = manifest_start_url;
manifest->id = GenerateManifestIdFromStartUrlOnly(manifest_start_url);
manifest->scope = manifest_scope;
manifest->has_theme_color = true;
manifest->theme_color = manifest_theme_color;
webapps::FakeInstallableManager::CreateForWebContentsWithManifest(
web_contents(), webapps::InstallableStatusCode::NO_ERROR_DETECTED,
GURL("https://example.com/manifest"), std::move(manifest));
}
base::RunLoop run_loop;
bool callback_called = false;
WebAppDataRetriever retriever;
retriever.CheckInstallabilityAndRetrieveManifest(
web_contents(),
base::BindLambdaForTesting(
[&](blink::mojom::ManifestPtr opt_manifest,
bool valid_manifest_for_web_app,
webapps::InstallableStatusCode error_code) {
EXPECT_EQ(error_code,
webapps::InstallableStatusCode::NO_ERROR_DETECTED);
EXPECT_EQ(manifest_short_name, opt_manifest->short_name);
EXPECT_EQ(manifest_name, opt_manifest->name);
EXPECT_EQ(manifest_start_url, opt_manifest->start_url);
EXPECT_EQ(manifest_scope, opt_manifest->scope);
EXPECT_EQ(manifest_theme_color, opt_manifest->theme_color);
EXPECT_EQ(GURL("https://example.com/manifest"),
opt_manifest->manifest_url);
callback_called = true;
run_loop.Quit();
}));
run_loop.Run();
EXPECT_TRUE(callback_called);
}
TEST_F(WebAppDataRetrieverTest, CheckInstallabilityFails) {
SetFakeWebPageMetadataAgent();
{
webapps::FakeInstallableManager::CreateForWebContentsWithManifest(
web_contents(), webapps::InstallableStatusCode::NO_MANIFEST, GURL(),
blink::mojom::Manifest::New());
}
base::RunLoop run_loop;
bool callback_called = false;
WebAppDataRetriever retriever;
retriever.CheckInstallabilityAndRetrieveManifest(
web_contents(),
base::BindLambdaForTesting(
[&](blink::mojom::ManifestPtr opt_manifest,
bool valid_manifest_for_web_app,
webapps::InstallableStatusCode error_code) {
EXPECT_EQ(error_code, webapps::InstallableStatusCode::NO_MANIFEST);
EXPECT_FALSE(valid_manifest_for_web_app);
callback_called = true;
run_loop.Quit();
}));
run_loop.Run();
EXPECT_TRUE(callback_called);
}
} // namespace web_app
|