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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/prefs/pref_service.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"
namespace {
const char url1[] = "/page_with_image.html";
const char url2[] = "/page_with_svg_image.html";
const char url3[] = "/page_with_iframe.html";
// Values for kRestoreOnStartup pref.
const int kRestoreLastSession = 1;
const int kRestoreUrls = 4;
const int kRestoreNtp = 5;
struct StartupPrefs {
StartupPrefs() : restore_on_startup(kRestoreNtp) {}
explicit StartupPrefs(int restore_on_startup_pref_value,
const std::vector<std::string>& urls = {})
: restore_on_startup(restore_on_startup_pref_value),
urls_to_restore(std::move(urls)) {}
// The value set for the kRestoreOnStartup pref.
int restore_on_startup;
// The list of URLs we set for the kURLsToRestoreOnStartup pref.
std::vector<std::string> urls_to_restore;
};
struct LaunchNavigationBrowserTestParam {
LaunchNavigationBrowserTestParam(
bool enable_feature,
StartupPrefs prefs,
const std::vector<std::string>& urls,
const std::vector<std::string>& urls_after_restart = {},
const std::vector<std::string>& cmd_line_switches = {})
: enable_feature(enable_feature),
startup_prefs(prefs),
cmd_line_urls(std::move(urls)),
cmd_line_urls_after_restart(std::move(urls_after_restart)),
cmd_line_switches(std::move(cmd_line_switches)) {}
const bool enable_feature;
const StartupPrefs startup_prefs;
const std::vector<std::string> cmd_line_urls;
// `cmd_line_urls_after_restart` is only applicable to test cases under
// LaunchNavigationBrowserRestartTest.
const std::vector<std::string> cmd_line_urls_after_restart;
const std::vector<std::string> cmd_line_switches;
};
void AppendUrlsToCmdLine(base::CommandLine* command_line,
net::EmbeddedTestServer* embedded_test_server,
const std::vector<std::string>& urls) {
for (const std::string& url : urls) {
command_line->AppendArg(embedded_test_server->GetURL(url).spec());
}
}
base::Value::List ListValueFromTestUrls(
const std::vector<std::string>& test_urls,
net::EmbeddedTestServer* embedded_test_server) {
base::Value::List url_list;
for (const std::string& url : test_urls) {
url_list.Append(base::Value(embedded_test_server->GetURL(url).spec()));
}
return url_list;
}
bool IsPerformanceNavigationTimingConfidenceEnabled() {
return base::FeatureList::IsEnabled(
blink::features::kPerformanceNavigationTimingConfidence);
}
} // namespace
class LaunchNavigationBrowserTest
: public InProcessBrowserTest,
public BrowserListObserver,
public testing::WithParamInterface<LaunchNavigationBrowserTestParam> {
public:
LaunchNavigationBrowserTest() {
if (GetParam().enable_feature) {
// Set the noise probability to 18.0 to ensure we always get the
// computed value back.
scoped_feature_list_.InitWithFeaturesAndParameters(
/*enabled_features=*/{{features::kNavigationConfidenceEpsilon,
{{"navigation-confidence-epsilon-value",
"18.0"}}},
{blink::features::
kPerformanceNavigationTimingConfidence,
{}}},
/*disabled_features=*/{});
} else {
scoped_feature_list_.InitAndDisableFeature(
blink::features::kPerformanceNavigationTimingConfidence);
}
}
~LaunchNavigationBrowserTest() override = default;
// InProcessBrowserTest:
void SetUpCommandLine(base::CommandLine* command_line) override {
// Backgrounded renderer processes run at a lower priority, causing the
// tests to take more time to complete. Disable backgrounding so that the
// tests don't time out.
command_line->AppendSwitch(switches::kDisableRendererBackgrounding);
embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data");
ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
embedded_test_server()->StartAcceptingConnections();
SetLaunchUrls(command_line);
for (const std::string& cmd_line_switch : GetParam().cmd_line_switches) {
command_line->AppendSwitch(cmd_line_switch);
}
}
void CheckActivePageNavigationConfidence(
const std::string& expected_navigation_confidence) {
CheckPageNavigationConfidenceForWebContents(
browser()->tab_strip_model()->GetActiveWebContents(),
expected_navigation_confidence);
}
void CheckUseCounterCount(int expected_count) {
// Fetch the Blink.UseCounter.Features histogram in every renderer process
// until reaching, but not exceeding, `expected_count`.
while (true) {
content::FetchHistogramsFromChildProcesses();
metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting();
int count = histogram_tester_.GetBucketCount(
"Blink.UseCounter.Features",
blink::mojom::WebFeature::kPerformanceNavigationTimingConfidence);
CHECK_LE(count, expected_count);
if (count == expected_count) {
return;
}
base::PlatformThread::Sleep(base::Milliseconds(5));
}
}
void CheckPageNavigationConfidenceAt(
int tab_index,
const std::string& expected_navigation_confidence) {
CheckPageNavigationConfidenceForWebContents(
browser()->tab_strip_model()->GetWebContentsAt(tab_index),
expected_navigation_confidence);
}
void CheckPageNavigationConfidenceForWebContents(
content::WebContents* const web_contents,
const std::string& expected_navigation_confidence) {
CHECK(web_contents);
AwaitDocumentOnLoadCompleted(web_contents);
std::string result =
ExtractNavigationConfidenceFromTargetRenderFrameHost(web_contents);
EXPECT_EQ(result, expected_navigation_confidence);
}
void Navigate(const std::string& url) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(url)));
}
std::string ExtractNavigationConfidenceFromTargetRenderFrameHost(
const content::ToRenderFrameHost& frame_host) {
return content::EvalJs(
frame_host,
"let navigationEntry = "
"window.performance.getEntriesByType('navigation')[0];"
"if ('confidence' in navigationEntry) {"
" if (navigationEntry.confidence) {"
" navigationEntry.confidence.value;"
" } else {"
" 'null';"
" }"
"} else {"
" 'undefined';"
"}")
.ExtractString();
}
private:
virtual void SetLaunchUrls(base::CommandLine*) = 0;
base::test::ScopedFeatureList scoped_feature_list_;
base::HistogramTester histogram_tester_;
};
class LaunchNavigationBrowserBasicTest : public LaunchNavigationBrowserTest {
private:
void SetLaunchUrls(base::CommandLine* command_line) override {
if (!GetParam().cmd_line_urls.empty()) {
AppendUrlsToCmdLine(command_line, embedded_test_server(),
GetParam().cmd_line_urls);
}
}
};
IN_PROC_BROWSER_TEST_P(LaunchNavigationBrowserBasicTest, CmdLineLaunch) {
std::vector<size_t> expected_usecounter_count = {0, 0, 0};
std::vector<std::string> expected_navigation_confidence = {
"undefined", "undefined", "undefined"};
if (IsPerformanceNavigationTimingConfidenceEnabled()) {
expected_navigation_confidence = {"low", "high", "high"};
expected_usecounter_count = {1, 2, 3};
}
CheckActivePageNavigationConfidence(expected_navigation_confidence[0]);
CheckUseCounterCount(expected_usecounter_count[0]);
Navigate("/page_with_image.html");
CheckActivePageNavigationConfidence(expected_navigation_confidence[1]);
CheckUseCounterCount(expected_usecounter_count[1]);
Navigate("/hello.html");
CheckActivePageNavigationConfidence(expected_navigation_confidence[2]);
CheckUseCounterCount(expected_usecounter_count[2]);
}
INSTANTIATE_TEST_SUITE_P(
CmdLineURLBasicTestFeatureEnabled,
LaunchNavigationBrowserBasicTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/true,
StartupPrefs(),
{url1})));
INSTANTIATE_TEST_SUITE_P(
CmdLineURLBasicTestFeatureDisabled,
LaunchNavigationBrowserBasicTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/false,
StartupPrefs(),
{url1})));
INSTANTIATE_TEST_SUITE_P(
CmdLineURLIncognitoBasicTestFeatureEnabled,
LaunchNavigationBrowserBasicTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/true,
StartupPrefs(),
{url1},
{},
{switches::kIncognito})));
INSTANTIATE_TEST_SUITE_P(
CmdLineURLIncognitoBasicTestFeatureDisabled,
LaunchNavigationBrowserBasicTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/false,
StartupPrefs(),
{url1},
{},
{switches::kIncognito})));
class LaunchNavigationBrowserRestartTest : public LaunchNavigationBrowserTest {
public:
// This function returns the expected number of tabs in various restore
// scenarios.
size_t GetExpectedTabCountFromRestore() {
const ParamType& test_params = GetParam();
size_t cmd_line_tab_count = test_params.cmd_line_urls_after_restart.size();
// The default is 1, since we will launch NTP if we aren't restoring other
// tabs.
size_t total_tab_count = 1;
if (test_params.startup_prefs.restore_on_startup == kRestoreLastSession) {
// If restoring previous session on startup we expect the navigation
// confidence field to return "low" for all opened tabs.
CHECK(!test_params.cmd_line_urls.empty());
total_tab_count = test_params.cmd_line_urls.size() + cmd_line_tab_count;
} else if (cmd_line_tab_count) {
// Open URLs passed via the command line
total_tab_count = cmd_line_tab_count;
} else if (test_params.startup_prefs.restore_on_startup == kRestoreUrls) {
// Restore from the user-specified list in prefs
total_tab_count = test_params.startup_prefs.urls_to_restore.size();
}
return total_tab_count;
}
private:
void SetLaunchUrls(base::CommandLine* command_line) override {
const std::string test_case_name(
testing::UnitTest::GetInstance()->current_test_info()->name());
const auto& cmd_line_urls = (test_case_name.find("PRE_") == 0
? GetParam().cmd_line_urls
: GetParam().cmd_line_urls_after_restart);
if (!cmd_line_urls.empty()) {
AppendUrlsToCmdLine(command_line, embedded_test_server(), cmd_line_urls);
}
}
};
IN_PROC_BROWSER_TEST_P(LaunchNavigationBrowserRestartTest,
PRE_CmdLineURLRestartTest) {
std::vector<size_t> expected_usecounter_count = {0, 0, 0};
std::vector<std::string> expected_navigation_confidence = {
"undefined", "undefined", "undefined"};
if (IsPerformanceNavigationTimingConfidenceEnabled()) {
expected_navigation_confidence = {"high", "high", "high"};
expected_usecounter_count = {1, 2, 3};
}
Navigate("/hello.html");
CheckActivePageNavigationConfidence(expected_navigation_confidence[0]);
CheckUseCounterCount(expected_usecounter_count[0]);
Navigate("/page_with_image.html");
CheckActivePageNavigationConfidence(expected_navigation_confidence[1]);
CheckUseCounterCount(expected_usecounter_count[1]);
Navigate("/hello.html");
CheckActivePageNavigationConfidence(expected_navigation_confidence[2]);
CheckUseCounterCount(expected_usecounter_count[2]);
// Set browser startup behavior here for the non-PRE split test.
browser()->profile()->GetPrefs()->SetInteger(
prefs::kRestoreOnStartup, GetParam().startup_prefs.restore_on_startup);
browser()->profile()->GetPrefs()->SetList(
prefs::kURLsToRestoreOnStartup,
ListValueFromTestUrls(GetParam().startup_prefs.urls_to_restore,
embedded_test_server()));
}
// We attempt to restart the browser in this test body.
IN_PROC_BROWSER_TEST_P(LaunchNavigationBrowserRestartTest,
CmdLineURLRestartTest) {
const ParamType& test_params = GetParam();
const size_t expected_initial_tab_count = GetExpectedTabCountFromRestore();
const bool expect_valid_navigation_confidence =
IsPerformanceNavigationTimingConfidenceEnabled();
std::string expected_initial_navigation_confidence_value =
expect_valid_navigation_confidence ? "low" : "undefined";
std::vector<std::string> expected_initial_navigation_confidence;
expected_initial_navigation_confidence.insert(
expected_initial_navigation_confidence.end(), expected_initial_tab_count,
expected_initial_navigation_confidence_value);
PrefService* prefs = browser()->profile()->GetPrefs();
ASSERT_EQ(prefs->GetUserPrefValue(prefs::kRestoreOnStartup)->GetInt(),
test_params.startup_prefs.restore_on_startup);
ASSERT_EQ(expected_initial_tab_count,
static_cast<size_t>(browser()->tab_strip_model()->count()));
ASSERT_EQ(expected_initial_tab_count,
expected_initial_navigation_confidence.size());
// Validate initial navigation confidence state.
for (size_t i = 0; i < expected_initial_navigation_confidence.size(); i++) {
CheckPageNavigationConfidenceAt(i,
expected_initial_navigation_confidence[i]);
}
// Confirm new navigations get "high" navigation confidence.
{
std::vector<size_t> expected_usecounter_count = {0, 0};
std::vector<std::string> expected_navigation_confidence = {"undefined",
"undefined"};
if (IsPerformanceNavigationTimingConfidenceEnabled()) {
expected_navigation_confidence = {"high", "high"};
expected_usecounter_count = {expected_initial_tab_count + 1,
expected_initial_tab_count + 2};
}
Navigate("/page_with_image.html");
CheckActivePageNavigationConfidence(expected_navigation_confidence[0]);
Navigate("/hello.html");
CheckActivePageNavigationConfidence(expected_navigation_confidence[1]);
}
}
// Tests navigation type for pages when the browser is restarted with
// session.restore_on_startup pref set to restore last session on startup.
INSTANTIATE_TEST_SUITE_P(CmdLineURLRestartTestRestorePreviousSession,
LaunchNavigationBrowserRestartTest,
testing::Values(LaunchNavigationBrowserTestParam(
/*enable_feature=*/true,
StartupPrefs(kRestoreLastSession),
{url1},
{url1, url2})));
// Tests navigation type for pages when the browser is restarted with
// session.restore_on_startup pref set to restore a list of urls specified by
// session.startup_urls on startup.
INSTANTIATE_TEST_SUITE_P(CmdLineURLRestartTestRestoreUrlList,
LaunchNavigationBrowserRestartTest,
testing::Values(LaunchNavigationBrowserTestParam(
/*enable_feature=*/true,
StartupPrefs(kRestoreUrls, {url1, url2}),
{url1},
{url1, url2})));
// Tests navigation type for pages when the browser is restarted with
// session.restore_on_startup pref set to open NTP on startup.
INSTANTIATE_TEST_SUITE_P(CmdLineURLRestartTestBasic,
LaunchNavigationBrowserRestartTest,
testing::Values(LaunchNavigationBrowserTestParam(
/*enable_feature=*/true,
StartupPrefs(),
{url1},
{url1, url2})));
class LaunchNavigationBrowserWithIFrameTest
: public LaunchNavigationBrowserTest {
protected:
void CheckActivePageFrameNavigationConfidence(
const std::string& expected_navigation_confidence) {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::RenderFrameHost* main_rfh = web_contents->GetPrimaryMainFrame();
AwaitDocumentOnLoadCompleted(web_contents);
content::RenderFrameHost* frame_rfh = ChildFrameAt(main_rfh, 0);
std::string frame_navigation_confidence_result =
ExtractNavigationConfidenceFromTargetRenderFrameHost(frame_rfh);
EXPECT_EQ(frame_navigation_confidence_result,
expected_navigation_confidence);
}
private:
void SetLaunchUrls(base::CommandLine* command_line) override {
if (!GetParam().cmd_line_urls.empty()) {
AppendUrlsToCmdLine(command_line, embedded_test_server(),
GetParam().cmd_line_urls);
}
}
};
IN_PROC_BROWSER_TEST_P(LaunchNavigationBrowserWithIFrameTest,
CmdLineLaunchWithIFrame) {
std::vector<std::string> expected_main_frame_navigation_confidence = {
"undefined", "undefined"};
std::vector<std::string> expected_iframe_navigation_confidence = {
"undefined", "undefined"};
if (IsPerformanceNavigationTimingConfidenceEnabled()) {
expected_main_frame_navigation_confidence = {"low", "high"};
expected_iframe_navigation_confidence = {"null", "null"};
}
CheckActivePageNavigationConfidence(
expected_main_frame_navigation_confidence[0]);
CheckActivePageFrameNavigationConfidence(
expected_iframe_navigation_confidence[0]);
Navigate("/page_with_iframe_and_image.html");
CheckActivePageNavigationConfidence(
expected_main_frame_navigation_confidence[1]);
CheckActivePageFrameNavigationConfidence(
expected_iframe_navigation_confidence[1]);
}
IN_PROC_BROWSER_TEST_P(LaunchNavigationBrowserWithIFrameTest,
CreateEmptyFrame) {
Navigate("/launch_navigation_frame.html");
CheckActivePageNavigationConfidence(
IsPerformanceNavigationTimingConfidenceEnabled() ? "high" : "undefined");
CheckActivePageFrameNavigationConfidence(
IsPerformanceNavigationTimingConfidenceEnabled() ? "null" : "undefined");
}
INSTANTIATE_TEST_SUITE_P(
CmdLineURLIFrameTestFeatureEnabled,
LaunchNavigationBrowserWithIFrameTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/true,
StartupPrefs(),
{url3})));
INSTANTIATE_TEST_SUITE_P(
CmdLineURLIFrameTestFeatureDisabled,
LaunchNavigationBrowserWithIFrameTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/false,
StartupPrefs(),
{url3})));
class LaunchNavigationBrowserWithPopupTest
: public LaunchNavigationBrowserTest {
protected:
content::WebContents* OpenPopupFromActiveWebContents() const {
auto* contents = browser()->tab_strip_model()->GetActiveWebContents();
content::ExecuteScriptAsync(
contents, "w = open('about:blank', '', 'width=200,height=200');");
Browser* popup = ui_test_utils::WaitForBrowserToOpen();
EXPECT_NE(popup, browser());
auto* popup_contents = popup->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(WaitForLoadStop(popup_contents));
EXPECT_TRUE(WaitForRenderFrameReady(popup_contents->GetPrimaryMainFrame()));
return popup_contents;
}
private:
void SetLaunchUrls(base::CommandLine* command_line) override {
if (!GetParam().cmd_line_urls.empty()) {
AppendUrlsToCmdLine(command_line, embedded_test_server(),
GetParam().cmd_line_urls);
}
}
};
IN_PROC_BROWSER_TEST_P(LaunchNavigationBrowserWithPopupTest,
CmdLineLaunchWithPopup) {
std::vector<std::string> expected_main_frame_navigation_confidence = {
"undefined", "undefined"};
std::vector<std::string> expected_popup_navigation_confidence = {"undefined",
"undefined"};
if (IsPerformanceNavigationTimingConfidenceEnabled()) {
expected_main_frame_navigation_confidence = {"low", "high"};
expected_popup_navigation_confidence = {"high", "high"};
}
{
auto* popup_contents = OpenPopupFromActiveWebContents();
CheckActivePageNavigationConfidence(
expected_main_frame_navigation_confidence[0]);
CheckPageNavigationConfidenceForWebContents(
popup_contents, expected_popup_navigation_confidence[0]);
}
{
Navigate(url2);
auto* popup_contents = OpenPopupFromActiveWebContents();
CheckActivePageNavigationConfidence(
expected_main_frame_navigation_confidence[1]);
CheckPageNavigationConfidenceForWebContents(
popup_contents, expected_popup_navigation_confidence[1]);
}
}
INSTANTIATE_TEST_SUITE_P(
CmdLineURLPopupTestFeatureEnabled,
LaunchNavigationBrowserWithPopupTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/true,
StartupPrefs(),
{url1})));
INSTANTIATE_TEST_SUITE_P(
CmdLineURLPopupTestFeatureDisabled,
LaunchNavigationBrowserWithPopupTest,
testing::Values(LaunchNavigationBrowserTestParam(/*enable_feature=*/false,
StartupPrefs(),
{url1})));
|