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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/optimization_guide/content/browser/page_text_observer.h"
#include <optional>
#include <string>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/optimization_guide/content/mojom/page_text_service.mojom.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace optimization_guide {
namespace {
FrameTextDumpResult MakeFrameDump(mojom::TextDumpEvent event,
content::GlobalRenderFrameHostId rfh_id,
bool amp_frame,
int nav_id,
const std::u16string& contents) {
return FrameTextDumpResult::Initialize(event, rfh_id, amp_frame, nav_id)
.CompleteWithContents(contents);
}
class TestConsumer : public PageTextObserver::Consumer {
public:
TestConsumer() = default;
~TestConsumer() = default;
void Reset() { was_called_ = false; }
void PopulateRequest(uint32_t max_size,
const std::set<mojom::TextDumpEvent>& events,
bool request_amp = false) {
request_ = std::make_unique<PageTextObserver::ConsumerTextDumpRequest>();
request_->max_size = max_size;
request_->events = events;
request_->callback =
base::BindOnce(&TestConsumer::OnGotTextDump, base::Unretained(this));
request_->dump_amp_subframes = request_amp;
}
void WaitForPageText() {
if (result_) {
return;
}
base::RunLoop run_loop;
on_page_text_closure_ = run_loop.QuitClosure();
run_loop.Run();
}
bool was_called() const { return was_called_; }
PageTextDumpResult* result() { return result_.get(); }
// PageTextObserver::Consumer:
std::unique_ptr<PageTextObserver::ConsumerTextDumpRequest>
MaybeRequestFrameTextDump(content::NavigationHandle* handle) override {
was_called_ = true;
return std::move(request_);
}
private:
void OnGotTextDump(const PageTextDumpResult& result) {
result_ = std::make_unique<PageTextDumpResult>(result);
if (on_page_text_closure_) {
std::move(on_page_text_closure_).Run();
}
}
bool was_called_ = false;
std::unique_ptr<PageTextObserver::ConsumerTextDumpRequest> request_;
base::OnceClosure on_page_text_closure_;
std::unique_ptr<PageTextDumpResult> result_ = nullptr;
};
} // namespace
// This tests code in
// //components/optimization_guide/content/browser/page_text_observer.h, but
// this test is in //chrome because the components browsertests do not fully
// standup a renderer process.
class PageTextObserverBrowserTest : public InProcessBrowserTest {
public:
PageTextObserverBrowserTest() = default;
~PageTextObserverBrowserTest() override = default;
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
InProcessBrowserTest::SetUpOnMainThread();
embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
&PageTextObserverBrowserTest::RequestHandler, base::Unretained(this)));
embedded_test_server()->ServeFilesFromSourceDirectory(
"chrome/test/data/optimization_guide");
ASSERT_TRUE(embedded_test_server()->Start());
}
content::WebContents* web_contents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
PageTextObserver* observer() {
return PageTextObserver::FromWebContents(web_contents());
}
protected:
std::string dynamic_response_body_;
private:
std::unique_ptr<net::test_server::HttpResponse> RequestHandler(
const net::test_server::HttpRequest& request) {
std::string path_value;
// This script is render blocking in the HTML, but is intentionally slow.
// This provides important time between commit and first layout for any text
// dump requests to make it to the renderer, reducing flakes.
if (request.GetURL().path() == "/slow-first-layout.js") {
std::unique_ptr<net::test_server::DelayedHttpResponse> resp =
std::make_unique<net::test_server::DelayedHttpResponse>(
base::Milliseconds(1500));
resp->set_code(net::HTTP_OK);
resp->set_content_type("application/javascript");
resp->set_content(std::string());
return resp;
}
// This script is onLoad-blocking in the HTML, but is intentionally slow.
// This provides important time between first layout and finish load for
// tests that need it.
if (request.GetURL().path() == "/slow-add-world-text.js") {
std::unique_ptr<net::test_server::DelayedHttpResponse> resp =
std::make_unique<net::test_server::DelayedHttpResponse>(
base::Milliseconds(500));
resp->set_code(net::HTTP_OK);
resp->set_content_type("application/javascript");
resp->set_content(
"var p = document.createElement('p'); p.innerHTML = 'world'; "
"document.body.appendChild(p); ");
return resp;
}
if (request.GetURL().path() == "/dynamic.html") {
std::unique_ptr<net::test_server::BasicHttpResponse> resp =
std::make_unique<net::test_server::BasicHttpResponse>();
resp->set_code(net::HTTP_OK);
resp->set_content_type("text/html");
resp->set_content(dynamic_response_body_);
return resp;
}
return nullptr;
}
};
IN_PROC_BROWSER_TEST_F(PageTextObserverBrowserTest, SimpleCaseNoSubframes) {
PageTextObserver::CreateForWebContents(web_contents());
ASSERT_TRUE(observer());
TestConsumer consumer;
observer()->AddConsumer(&consumer);
consumer.PopulateRequest(/*max_size=*/1024,
/*events=*/{mojom::TextDumpEvent::kFirstLayout});
GURL url(embedded_test_server()->GetURL("a.com", "/hello.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(consumer.was_called());
consumer.WaitForPageText();
ASSERT_TRUE(consumer.result());
EXPECT_THAT(
consumer.result()->frame_results(),
::testing::UnorderedElementsAreArray({
MakeFrameDump(
mojom::TextDumpEvent::kFirstLayout,
web_contents()->GetPrimaryMainFrame()->GetGlobalId(),
/*amp_frame=*/false,
web_contents()->GetController().GetVisibleEntry()->GetUniqueID(),
u"hello"),
}));
}
IN_PROC_BROWSER_TEST_F(PageTextObserverBrowserTest, FirstLayoutAndOnLoad) {
PageTextObserver::CreateForWebContents(web_contents());
ASSERT_TRUE(observer());
// This test can be flaky (crbug.com/1187264), and it always seems to be
// caused by the renderer never finishing the page load and is thus outside
// the control of this feature. Thorough testing shows that this flake does
// not repeat itself, so running the test an extra time is sufficient.
for (size_t i = 0; i < 2; i++) {
TestConsumer first_layout_consumer;
TestConsumer on_load_consumer;
observer()->AddConsumer(&first_layout_consumer);
observer()->AddConsumer(&on_load_consumer);
first_layout_consumer.PopulateRequest(
/*max_size=*/1024,
/*events=*/{mojom::TextDumpEvent::kFirstLayout});
on_load_consumer.PopulateRequest(
/*max_size=*/1024,
/*events=*/{mojom::TextDumpEvent::kFinishedLoad});
GURL url(embedded_test_server()->GetURL("a.com", "/hello_world.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(first_layout_consumer.was_called());
ASSERT_TRUE(on_load_consumer.was_called());
first_layout_consumer.WaitForPageText();
on_load_consumer.WaitForPageText();
if (observer()->outstanding_requests() > 0) {
// This is a flake. Reset for the next attempt.
observer()->RemoveConsumer(&first_layout_consumer);
observer()->RemoveConsumer(&on_load_consumer);
// A new navigation should unblock any missing requests, which are then
// discarded.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("about:blank")));
continue;
}
ASSERT_TRUE(first_layout_consumer.result());
bool has_first_layout_event = false;
bool has_finished_load_event = false;
for (const FrameTextDumpResult& result :
first_layout_consumer.result()->frame_results()) {
SCOPED_TRACE(result);
// These fields are the same for both events.
EXPECT_EQ(web_contents()->GetPrimaryMainFrame()->GetGlobalId(),
result.rfh_id());
EXPECT_FALSE(result.amp_frame());
EXPECT_EQ(
web_contents()->GetController().GetVisibleEntry()->GetUniqueID(),
result.unique_navigation_id());
ASSERT_TRUE(result.contents().has_value());
// The text that is dumped during first layout may or may not include the
// text that is dynamically added by the test page's JavaScript. Checking
// for text equality is inherently flaky, and this determinism is not a
// guarantee that we make to callers.
if (result.event() == mojom::TextDumpEvent::kFirstLayout) {
EXPECT_TRUE(base::Contains(*result.contents(), u"hello"));
has_first_layout_event = true;
}
// The finished load event is deterministic in what text should be
// populated on the page.
if (result.event() == mojom::TextDumpEvent::kFinishedLoad) {
EXPECT_EQ(u"hello\n\nworld", *result.contents());
has_finished_load_event = true;
}
}
EXPECT_TRUE(has_first_layout_event);
EXPECT_TRUE(has_finished_load_event);
EXPECT_EQ(*first_layout_consumer.result(), *on_load_consumer.result());
return;
}
FAIL();
}
IN_PROC_BROWSER_TEST_F(PageTextObserverBrowserTest, OOPIFAMPSubframe) {
if (content::IsIsolatedOriginRequiredToGuaranteeDedicatedProcess()) {
// Isolate b.com so that it is guaranteed to be in a different process.
content::IsolateOriginsForTesting(
embedded_test_server(),
browser()->tab_strip_model()->GetActiveWebContents(), {"b.com"});
}
PageTextObserver::CreateForWebContents(web_contents());
ASSERT_TRUE(observer());
TestConsumer consumer;
observer()->AddConsumer(&consumer);
consumer.PopulateRequest(
/*max_size=*/1024,
/*events=*/{mojom::TextDumpEvent::kFirstLayout},
/*request_amp=*/true);
GURL url(embedded_test_server()->GetURL("a.com", "/dynamic.html"));
dynamic_response_body_ = base::StringPrintf(
"<html><body>"
"<script type=\"text/javascript\" src=\"/slow-first-layout.js\"></script>"
"<p>mainframe</p>"
"<iframe name=\"amp\" src=\"%s\"></iframe>"
"</body></html>",
embedded_test_server()->GetURL("b.com", "/amp.html").spec().c_str());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(consumer.was_called());
consumer.WaitForPageText();
content::GlobalRenderFrameHostId amp_frame_id;
content::RenderFrameHost* amp_frame = content::FrameMatchingPredicate(
web_contents()->GetPrimaryPage(),
base::BindRepeating(&content::FrameMatchesName, "amp"));
ASSERT_TRUE(amp_frame);
amp_frame_id = amp_frame->GetGlobalId();
ASSERT_TRUE(consumer.result());
bool has_amp_result = false;
bool has_mainframe_result = false;
for (const FrameTextDumpResult& result : consumer.result()->frame_results()) {
if (result.amp_frame()) {
EXPECT_EQ(mojom::TextDumpEvent::kFinishedLoad, result.event());
EXPECT_EQ(amp_frame_id, result.rfh_id());
EXPECT_EQ(
web_contents()->GetController().GetVisibleEntry()->GetUniqueID(),
result.unique_navigation_id());
// Sometimes blink adds trailing whitespace since there's another element
// on the page. Happens non-deterministically.
EXPECT_EQ(u"AMP",
std::u16string(base::TrimWhitespace(
*result.contents(), base::TrimPositions::TRIM_ALL)));
has_amp_result = true;
} else {
EXPECT_EQ(mojom::TextDumpEvent::kFirstLayout, result.event());
EXPECT_EQ(web_contents()->GetPrimaryMainFrame()->GetGlobalId(),
result.rfh_id());
EXPECT_EQ(
web_contents()->GetController().GetVisibleEntry()->GetUniqueID(),
result.unique_navigation_id());
// Sometimes blink adds trailing whitespace since there's another element
// on the page. Happens non-deterministically.
EXPECT_EQ(u"mainframe",
std::u16string(base::TrimWhitespace(
*result.contents(), base::TrimPositions::TRIM_ALL)));
has_mainframe_result = true;
}
}
EXPECT_TRUE(has_amp_result);
EXPECT_TRUE(has_mainframe_result);
}
IN_PROC_BROWSER_TEST_F(PageTextObserverBrowserTest, OOPIFNotAmpSubframe) {
PageTextObserver::CreateForWebContents(web_contents());
ASSERT_TRUE(observer());
TestConsumer consumer;
observer()->AddConsumer(&consumer);
consumer.PopulateRequest(
/*max_size=*/1024,
/*events=*/{mojom::TextDumpEvent::kFirstLayout},
/*request_amp=*/true);
GURL url(embedded_test_server()->GetURL("a.com", "/dynamic.html"));
dynamic_response_body_ = base::StringPrintf(
"<html><body>"
"<script type=\"text/javascript\" src=\"/slow-first-layout.js\"></script>"
"<p>mainframe</p>"
"<iframe src=\"%s\"></iframe>"
"</body></html>",
embedded_test_server()->GetURL("b.com", "/hello.html").spec().c_str());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(consumer.was_called());
consumer.WaitForPageText();
ASSERT_TRUE(consumer.result());
ASSERT_EQ(1U, consumer.result()->frame_results().size());
const auto& result = *consumer.result()->frame_results().begin();
EXPECT_EQ(mojom::TextDumpEvent::kFirstLayout, result.event());
EXPECT_EQ(web_contents()->GetPrimaryMainFrame()->GetGlobalId(),
result.rfh_id());
EXPECT_FALSE(result.amp_frame());
EXPECT_EQ(web_contents()->GetController().GetVisibleEntry()->GetUniqueID(),
result.unique_navigation_id());
EXPECT_EQ(u"mainframe", base::TrimWhitespace(*result.contents(),
base::TrimPositions::TRIM_ALL));
}
IN_PROC_BROWSER_TEST_F(PageTextObserverBrowserTest, SameProcessIframe) {
// Give the browser a moment to startup (helps to reduce flakes by ensuring
// renderer and browser are ready to go).
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), GURL(embedded_test_server()->GetURL("a.com", "/hello.html"))));
PageTextObserver::CreateForWebContents(web_contents());
ASSERT_TRUE(observer());
TestConsumer consumer;
observer()->AddConsumer(&consumer);
consumer.PopulateRequest(/*max_size=*/1024,
/*events=*/{mojom::TextDumpEvent::kFinishedLoad});
GURL url(embedded_test_server()->GetURL("a.com", "/dynamic.html"));
dynamic_response_body_ = base::StringPrintf(
"<html><body>"
"<script type=\"text/javascript\" src=\"/slow-first-layout.js\"></script>"
"<p>mainframe</p>"
"<iframe src=\"%s\"></iframe>"
"</body></html>",
embedded_test_server()->GetURL("a.com", "/hello.html").spec().c_str());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(consumer.was_called());
consumer.WaitForPageText();
ASSERT_TRUE(consumer.result());
EXPECT_THAT(
consumer.result()->frame_results(),
::testing::UnorderedElementsAreArray({
MakeFrameDump(
mojom::TextDumpEvent::kFinishedLoad,
web_contents()->GetPrimaryMainFrame()->GetGlobalId(),
/*amp_frame=*/false,
web_contents()->GetController().GetVisibleEntry()->GetUniqueID(),
u"mainframe\n\nhello"),
}));
}
IN_PROC_BROWSER_TEST_F(PageTextObserverBrowserTest, SameProcessAMPSubframe) {
// Give the browser a moment to startup (helps to reduce flakes by ensuring
// renderer and browser are ready to go).
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), GURL(embedded_test_server()->GetURL("a.com", "/hello.html"))));
PageTextObserver::CreateForWebContents(web_contents());
ASSERT_TRUE(observer());
TestConsumer consumer;
observer()->AddConsumer(&consumer);
consumer.PopulateRequest(
/*max_size=*/1024,
/*events=*/{mojom::TextDumpEvent::kFirstLayout},
/*request_amp=*/true);
GURL url(embedded_test_server()->GetURL("a.com", "/dynamic.html"));
dynamic_response_body_ = base::StringPrintf(
"<html><body>"
"<script type=\"text/javascript\" src=\"/slow-first-layout.js\"></script>"
"<p>mainframe</p>"
"<iframe src=\"%s\"></iframe>"
"</body></html>",
embedded_test_server()->GetURL("a.com", "/amp.html").spec().c_str());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(consumer.was_called());
consumer.WaitForPageText();
ASSERT_TRUE(consumer.result());
EXPECT_THAT(
consumer.result()->frame_results(),
::testing::UnorderedElementsAreArray({
MakeFrameDump(
mojom::TextDumpEvent::kFirstLayout,
web_contents()->GetPrimaryMainFrame()->GetGlobalId(),
/*amp_frame=*/false,
web_contents()->GetController().GetVisibleEntry()->GetUniqueID(),
u"mainframe"),
}));
}
class PageTextObserverFencedFrameBrowserTest
: public PageTextObserverBrowserTest {
public:
PageTextObserverFencedFrameBrowserTest() = default;
~PageTextObserverFencedFrameBrowserTest() override = default;
content::WebContents* web_contents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
return fenced_frame_helper_;
}
private:
content::test::FencedFrameTestHelper fenced_frame_helper_;
};
IN_PROC_BROWSER_TEST_F(PageTextObserverFencedFrameBrowserTest,
DoNotDispatchResponseOnFencedFrame) {
base::HistogramTester histogram_tester;
PageTextObserver::CreateForWebContents(web_contents());
const GURL initial_url = embedded_test_server()->GetURL("/empty.html");
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), initial_url));
histogram_tester.ExpectTotalCount(
"OptimizationGuide.PageTextDump.AbandonedRequests", 1);
histogram_tester.ExpectTotalCount(
"OptimizationGuide.PageTextDump.OutstandingRequests.DidFinishLoad", 1);
// Create a fenced frame.
GURL fenced_frame_url(
embedded_test_server()->GetURL("/fenced_frames/title1.html"));
content::RenderFrameHost* fenced_frame_host =
fenced_frame_test_helper().CreateFencedFrame(
web_contents()->GetPrimaryMainFrame(), fenced_frame_url);
ASSERT_TRUE(fenced_frame_host);
// Loading a URL in a fenced frame should not increase
// OptimizationGuide.PageTextDump.AbandonedRequests and
// OptimizationGuide.PageTextDump.OutstandingRequests.DidFinishLoad count.
histogram_tester.ExpectTotalCount(
"OptimizationGuide.PageTextDump.AbandonedRequests", 1);
histogram_tester.ExpectTotalCount(
"OptimizationGuide.PageTextDump.OutstandingRequests.DidFinishLoad", 1);
}
} // namespace optimization_guide
|