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
|
// Copyright 2017 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/resource_coordinator/tab_lifecycle_unit_source.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/performance_manager/public/user_tuning/user_performance_tuning_manager.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_observer.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_source_observer.h"
#include "chrome/browser/resource_coordinator/tab_helper.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit.h"
#include "chrome/browser/resource_coordinator/tab_load_tracker.h"
#include "chrome/browser/resource_coordinator/test_lifecycle_unit.h"
#include "chrome/browser/resource_coordinator/time.h"
#include "chrome/browser/resource_coordinator/utils.h"
#include "chrome/browser/ui/recently_audible_helper.h"
#include "chrome/browser/ui/tabs/tab_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "chrome/browser/ui/tabs/test_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/performance_manager/test_support/graph_impl.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_web_contents_observer.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace resource_coordinator {
namespace {
using ::testing::_;
constexpr base::TimeDelta kShortDelay = base::Seconds(1);
using PreDiscardResourceUsage = performance_manager::user_tuning::
UserPerformanceTuningManager::PreDiscardResourceUsage;
class MockLifecycleUnitSourceObserver : public LifecycleUnitSourceObserver {
public:
MockLifecycleUnitSourceObserver() = default;
MockLifecycleUnitSourceObserver(const MockLifecycleUnitSourceObserver&) =
delete;
MockLifecycleUnitSourceObserver& operator=(
const MockLifecycleUnitSourceObserver&) = delete;
MOCK_METHOD1(OnLifecycleUnitCreated, void(LifecycleUnit*));
};
class MockLifecycleUnitObserver : public LifecycleUnitObserver {
public:
MockLifecycleUnitObserver() = default;
MockLifecycleUnitObserver(const MockLifecycleUnitObserver&) = delete;
MockLifecycleUnitObserver& operator=(const MockLifecycleUnitObserver&) =
delete;
// Invokes the mock method with the new state and discard reason as extra
// parameters, so they can be easily matched by gmock.
void OnLifecycleUnitStateChanged(
LifecycleUnit* lifecycle_unit,
LifecycleUnitState last_state,
LifecycleUnitStateChangeReason reason) override {
MockOnLifecycleUnitStateChanged(lifecycle_unit, last_state, reason,
lifecycle_unit->GetState(),
lifecycle_unit->GetDiscardReason());
}
MOCK_METHOD(void,
MockOnLifecycleUnitStateChanged,
(LifecycleUnit * lifecycle_unit,
mojom::LifecycleUnitState previous_state,
LifecycleUnitStateChangeReason reason,
mojom::LifecycleUnitState new_state,
LifecycleUnitDiscardReason discard_reason),
());
MOCK_METHOD(void,
OnLifecycleUnitDestroyed,
(LifecycleUnit * lifecycle_unit),
(override));
};
bool IsFocused(LifecycleUnit* lifecycle_unit) {
return lifecycle_unit->GetLastFocusedTimeTicks() == base::TimeTicks::Max();
}
class TabLifecycleUnitSourceTest : public ChromeRenderViewHostTestHarness {
public:
TabLifecycleUnitSourceTest(const TabLifecycleUnitSourceTest&) = delete;
TabLifecycleUnitSourceTest& operator=(const TabLifecycleUnitSourceTest&) =
delete;
protected:
TabLifecycleUnitSourceTest()
: ChromeRenderViewHostTestHarness(
base::test::SingleThreadTaskEnvironment::TimeSource::MOCK_TIME) {
task_runner_ = task_environment()->GetMainThreadTaskRunner();
}
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
// Force TabManager/TabLifecycleUnitSource creation.
g_browser_process->GetTabManager();
source_ = GetTabLifecycleUnitSource();
source_->AddObserver(&source_observer_);
source_->AddLifecycleObserver(&tab_observer_);
tab_strip_model_ =
std::make_unique<TabStripModel>(&tab_strip_model_delegate_, profile());
tab_strip_model_->AddObserver(source_);
}
void TearDown() override {
// Don't observe OnLifecycleUnitDestroyed notifications after the test.
source_->RemoveLifecycleObserver(&tab_observer_);
tab_strip_model_->CloseAllTabs();
tab_strip_model_.reset();
task_environment()->RunUntilIdle();
ChromeRenderViewHostTestHarness::TearDown();
}
// If |focus_tab_strip| is true, focuses the tab strip. Then, appends 2 tabs
// to the tab strip and returns the associated LifecycleUnits via
// |first_lifecycle_unit| and |second_lifecycle_unit|. The first tab is
// background and the second tab is active.
void CreateTwoTabs(bool focus_tab_strip,
LifecycleUnit** first_lifecycle_unit,
LifecycleUnit** second_lifecycle_unit) {
if (focus_tab_strip)
source_->SetFocusedTabStripModelForTesting(tab_strip_model_.get());
// Add a foreground tab to the tab strip.
task_environment()->FastForwardBy(kShortDelay);
auto time_before_first_tab = NowTicks();
EXPECT_CALL(source_observer_, OnLifecycleUnitCreated(_))
.WillOnce(::testing::Invoke([&](LifecycleUnit* lifecycle_unit) {
*first_lifecycle_unit = lifecycle_unit;
if (focus_tab_strip) {
EXPECT_TRUE(IsFocused(*first_lifecycle_unit));
} else {
EXPECT_EQ(time_before_first_tab,
(*first_lifecycle_unit)->GetLastFocusedTimeTicks());
}
}));
std::unique_ptr<content::WebContents> first_web_contents =
CreateAndNavigateWebContents();
content::WebContents* raw_first_web_contents = first_web_contents.get();
tab_strip_model_->AppendWebContents(std::move(first_web_contents), true);
::testing::Mock::VerifyAndClear(&source_observer_);
EXPECT_TRUE(source_->GetTabLifecycleUnitExternal(raw_first_web_contents));
base::RepeatingClosure run_loop_cb = base::BindRepeating(
&base::test::SingleThreadTaskEnvironment::RunUntilIdle,
base::Unretained(task_environment()));
// Add another foreground tab to the focused tab strip.
task_environment()->FastForwardBy(kShortDelay);
auto time_before_second_tab = NowTicks();
EXPECT_CALL(source_observer_, OnLifecycleUnitCreated(_))
.WillOnce(::testing::Invoke([&](LifecycleUnit* lifecycle_unit) {
*second_lifecycle_unit = lifecycle_unit;
if (focus_tab_strip) {
EXPECT_EQ(time_before_second_tab,
(*first_lifecycle_unit)->GetLastFocusedTimeTicks());
EXPECT_TRUE(IsFocused(*second_lifecycle_unit));
} else {
EXPECT_EQ(time_before_first_tab,
(*first_lifecycle_unit)->GetLastFocusedTimeTicks());
EXPECT_EQ(time_before_second_tab,
(*second_lifecycle_unit)->GetLastFocusedTimeTicks());
}
}));
std::unique_ptr<content::WebContents> second_web_contents =
CreateAndNavigateWebContents();
content::WebContents* raw_second_web_contents = second_web_contents.get();
tab_strip_model_->AppendWebContents(std::move(second_web_contents), true);
::testing::Mock::VerifyAndClear(&source_observer_);
EXPECT_TRUE(source_->GetTabLifecycleUnitExternal(raw_second_web_contents));
// TabStripModel doesn't update the visibility of its WebContents by itself.
raw_first_web_contents->WasHidden();
}
void TestAppendTabsToTabStrip(bool focus_tab_strip) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(focus_tab_strip, &first_lifecycle_unit,
&second_lifecycle_unit);
const base::TimeTicks first_tab_last_focused_time =
first_lifecycle_unit->GetLastFocusedTimeTicks();
const base::TimeTicks second_tab_last_focused_time =
second_lifecycle_unit->GetLastFocusedTimeTicks();
// Add a background tab to the focused tab strip.
task_environment()->FastForwardBy(kShortDelay);
LifecycleUnit* third_lifecycle_unit = nullptr;
EXPECT_CALL(source_observer_, OnLifecycleUnitCreated(_))
.WillOnce(::testing::Invoke([&](LifecycleUnit* lifecycle_unit) {
third_lifecycle_unit = lifecycle_unit;
if (focus_tab_strip) {
EXPECT_EQ(first_tab_last_focused_time,
first_lifecycle_unit->GetLastFocusedTimeTicks());
EXPECT_TRUE(IsFocused(second_lifecycle_unit));
} else {
EXPECT_EQ(first_tab_last_focused_time,
first_lifecycle_unit->GetLastFocusedTimeTicks());
EXPECT_EQ(second_tab_last_focused_time,
second_lifecycle_unit->GetLastFocusedTimeTicks());
}
EXPECT_EQ(NowTicks(),
third_lifecycle_unit->GetLastFocusedTimeTicks());
}));
std::unique_ptr<content::WebContents> third_web_contents =
CreateAndNavigateWebContents();
content::WebContents* raw_third_web_contents = third_web_contents.get();
tab_strip_model_->AppendWebContents(std::move(third_web_contents), false);
::testing::Mock::VerifyAndClear(&source_observer_);
EXPECT_TRUE(source_->GetTabLifecycleUnitExternal(raw_third_web_contents));
// Expect notifications when tabs are closed.
CloseTabsAndExpectNotifications(
tab_strip_model_.get(),
{first_lifecycle_unit, second_lifecycle_unit, third_lifecycle_unit});
}
void CloseTabsAndExpectNotifications(
TabStripModel* tab_strip_model,
std::vector<LifecycleUnit*> lifecycle_units) {
for (LifecycleUnit* lifecycle_unit : lifecycle_units) {
EXPECT_CALL(tab_observer_, OnLifecycleUnitDestroyed(lifecycle_unit));
}
tab_strip_model->CloseAllTabs();
}
void DiscardAndAttachTabHelpers(LifecycleUnit* lifecycle_unit) {}
void DetachWebContentsTest(LifecycleUnitDiscardReason reason) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &first_lifecycle_unit,
&second_lifecycle_unit);
// Advance time so tabs are urgent discardable.
task_environment()->AdvanceClock(kBackgroundUrgentProtectionTime);
// Detach the non-active tab. Verify that it can no longer be discarded.
ExpectCanDiscardTrueAllReasons(first_lifecycle_unit);
std::unique_ptr<tabs::TabModel> detached_tab =
tab_strip_model_->DetachTabAtForInsertion(0);
ExpectCanDiscardFalseTrivialAllReasons(first_lifecycle_unit);
// Create a second tab strip.
TestTabStripModelDelegate other_tab_strip_model_delegate;
TabStripModel other_tab_strip_model(&other_tab_strip_model_delegate,
profile());
other_tab_strip_model.AddObserver(source_);
// Make sure that the second tab strip has a foreground tab.
LifecycleUnit* third_lifecycle_unit = nullptr;
EXPECT_CALL(source_observer_, OnLifecycleUnitCreated(_))
.WillOnce(::testing::Invoke(
[&](LifecycleUnit* unit) { third_lifecycle_unit = unit; }));
other_tab_strip_model.AppendWebContents(CreateTestWebContents(),
/*foreground=*/true);
::testing::Mock::VerifyAndClear(&source_observer_);
// Insert the tab into the second tab strip without focusing it. Verify that
// it can be discarded.
other_tab_strip_model.AppendTab(std::move(detached_tab), false);
ExpectCanDiscardTrueAllReasons(first_lifecycle_unit);
EXPECT_EQ(LifecycleUnitState::ACTIVE, first_lifecycle_unit->GetState());
EXPECT_CALL(tab_observer_,
MockOnLifecycleUnitStateChanged(
first_lifecycle_unit, ::mojom::LifecycleUnitState::ACTIVE,
_, ::mojom::LifecycleUnitState::DISCARDED, reason));
first_lifecycle_unit->Discard(reason);
::testing::Mock::VerifyAndClear(&tab_observer_);
// Expect notifications when the tabs are closed.
CloseTabsAndExpectNotifications(
&other_tab_strip_model, {first_lifecycle_unit, third_lifecycle_unit});
}
void DiscardTest(LifecycleUnitDiscardReason reason) {
const base::TimeTicks kDummyLastActiveTime =
base::TimeTicks() + kShortDelay;
LifecycleUnit* background_lifecycle_unit = nullptr;
LifecycleUnit* foreground_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &background_lifecycle_unit,
&foreground_lifecycle_unit);
content::WebContents* initial_web_contents =
tab_strip_model_->GetWebContentsAt(0);
content::WebContentsTester::For(initial_web_contents)
->SetLastActiveTimeTicks(kDummyLastActiveTime);
// Advance time so tabs are urgent discardable.
task_environment()->AdvanceClock(kBackgroundUrgentProtectionTime);
// Discard the tab.
EXPECT_EQ(LifecycleUnitState::ACTIVE,
background_lifecycle_unit->GetState());
EXPECT_CALL(tab_observer_,
MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::ACTIVE, _,
::mojom::LifecycleUnitState::DISCARDED, reason));
background_lifecycle_unit->Discard(reason);
::testing::Mock::VerifyAndClear(&tab_observer_);
EXPECT_NE(initial_web_contents, tab_strip_model_->GetWebContentsAt(0));
EXPECT_FALSE(tab_strip_model_->GetWebContentsAt(0)
->GetController()
.GetPendingEntry());
EXPECT_EQ(kDummyLastActiveTime,
tab_strip_model_->GetWebContentsAt(0)->GetLastActiveTimeTicks());
source_->SetFocusedTabStripModelForTesting(nullptr);
}
void DiscardAndActivateTest(LifecycleUnitDiscardReason reason) {
LifecycleUnit* background_lifecycle_unit = nullptr;
LifecycleUnit* foreground_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &background_lifecycle_unit,
&foreground_lifecycle_unit);
content::WebContents* initial_web_contents =
tab_strip_model_->GetWebContentsAt(0);
// Advance time so tabs are urgent discardable.
task_environment()->AdvanceClock(kBackgroundUrgentProtectionTime);
// Discard the tab.
EXPECT_EQ(LifecycleUnitState::ACTIVE,
background_lifecycle_unit->GetState());
EXPECT_CALL(tab_observer_,
MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::ACTIVE, _,
::mojom::LifecycleUnitState::DISCARDED, reason));
background_lifecycle_unit->Discard(reason);
::testing::Mock::VerifyAndClear(&tab_observer_);
EXPECT_NE(initial_web_contents, tab_strip_model_->GetWebContentsAt(0));
EXPECT_FALSE(tab_strip_model_->GetWebContentsAt(0)
->GetController()
.GetPendingEntry());
// Focus the tab. Expect the state to be ACTIVE.
EXPECT_CALL(tab_observer_,
MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::DISCARDED, _,
::mojom::LifecycleUnitState::ACTIVE, reason));
tab_strip_model_->ActivateTabAt(
0, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
::testing::Mock::VerifyAndClear(&tab_observer_);
EXPECT_EQ(LifecycleUnitState::ACTIVE,
background_lifecycle_unit->GetState());
EXPECT_TRUE(tab_strip_model_->GetWebContentsAt(0)
->GetController()
.GetPendingEntry());
}
void DiscardAndExplicitlyReloadTest(LifecycleUnitDiscardReason reason) {
LifecycleUnit* background_lifecycle_unit = nullptr;
LifecycleUnit* foreground_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &background_lifecycle_unit,
&foreground_lifecycle_unit);
content::WebContents* initial_web_contents =
tab_strip_model_->GetWebContentsAt(0);
// Advance time so tabs are urgent discardable.
task_environment()->AdvanceClock(kBackgroundUrgentProtectionTime);
// Discard the tab.
EXPECT_EQ(LifecycleUnitState::ACTIVE,
background_lifecycle_unit->GetState());
EXPECT_CALL(tab_observer_,
MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::ACTIVE, _,
::mojom::LifecycleUnitState::DISCARDED, reason));
background_lifecycle_unit->Discard(reason);
::testing::Mock::VerifyAndClear(&tab_observer_);
EXPECT_NE(initial_web_contents, tab_strip_model_->GetWebContentsAt(0));
EXPECT_FALSE(tab_strip_model_->GetWebContentsAt(0)
->GetController()
.GetPendingEntry());
// Explicitly reload the tab. Expect the state to be ACTIVE.
EXPECT_CALL(tab_observer_,
MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::DISCARDED, _,
::mojom::LifecycleUnitState::ACTIVE, reason));
tab_strip_model_->GetWebContentsAt(0)->GetController().Reload(
content::ReloadType::NORMAL, false);
::testing::Mock::VerifyAndClear(&tab_observer_);
EXPECT_EQ(LifecycleUnitState::ACTIVE,
background_lifecycle_unit->GetState());
EXPECT_TRUE(tab_strip_model_->GetWebContentsAt(0)
->GetController()
.GetPendingEntry());
}
raw_ptr<TabLifecycleUnitSource> source_ = nullptr;
::testing::StrictMock<MockLifecycleUnitSourceObserver> source_observer_;
::testing::StrictMock<MockLifecycleUnitObserver> tab_observer_;
std::unique_ptr<TabStripModel> tab_strip_model_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
std::unique_ptr<content::WebContents> CreateAndNavigateWebContents() {
std::unique_ptr<content::WebContents> web_contents =
CreateTestWebContents();
// Attach the RC tab helper. In production code the browser
// WebContentsDelegate takes care of this.
ResourceCoordinatorTabHelper::CreateForWebContents(web_contents.get());
// Commit an URL to allow discarding.
content::WebContentsTester::For(web_contents.get())
->NavigateAndCommit(GURL("https://www.example.com"));
return web_contents;
}
private:
TestTabStripModelDelegate tab_strip_model_delegate_;
tabs::PreventTabFeatureInitialization prevent_;
};
} // namespace
TEST_F(TabLifecycleUnitSourceTest, AppendTabsToFocusedTabStrip) {
TestAppendTabsToTabStrip(true /* focus_tab_strip */);
}
TEST_F(TabLifecycleUnitSourceTest, AppendTabsToNonFocusedTabStrip) {
TestAppendTabsToTabStrip(false /* focus_tab_strip */);
}
TEST_F(TabLifecycleUnitSourceTest, SwitchTabInFocusedTabStrip) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &first_lifecycle_unit,
&second_lifecycle_unit);
// Activate the first tab.
task_environment()->FastForwardBy(kShortDelay);
auto time_before_activate = NowTicks();
tab_strip_model_->ActivateTabAt(
0, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
EXPECT_TRUE(IsFocused(first_lifecycle_unit));
EXPECT_EQ(time_before_activate,
second_lifecycle_unit->GetLastFocusedTimeTicks());
// Expect notifications when tabs are closed.
CloseTabsAndExpectNotifications(
tab_strip_model_.get(), {first_lifecycle_unit, second_lifecycle_unit});
}
TEST_F(TabLifecycleUnitSourceTest, CloseTabInFocusedTabStrip) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &first_lifecycle_unit,
&second_lifecycle_unit);
// Close the second tab. The first tab should be focused.
task_environment()->FastForwardBy(kShortDelay);
EXPECT_CALL(tab_observer_, OnLifecycleUnitDestroyed(second_lifecycle_unit));
tab_strip_model_->CloseWebContentsAt(1, 0);
::testing::Mock::VerifyAndClear(&tab_observer_);
EXPECT_TRUE(IsFocused(first_lifecycle_unit));
// Expect notifications when tabs are closed.
CloseTabsAndExpectNotifications(tab_strip_model_.get(),
{first_lifecycle_unit});
}
TEST_F(TabLifecycleUnitSourceTest, DiscardWebContents) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &first_lifecycle_unit,
&second_lifecycle_unit);
// Replace the WebContents in the active tab with a second WebContents. Expect
// GetTabLifecycleUnitExternal() to return the TabLifecycleUnitExternal when
// called with the second WebContents as argument.
content::WebContents* original_web_contents =
tab_strip_model_->GetWebContentsAt(1);
TabLifecycleUnitExternal* tab_lifecycle_unit_external =
source_->GetTabLifecycleUnitExternal(original_web_contents);
std::unique_ptr<content::WebContents> new_web_contents =
CreateTestWebContents();
content::WebContents* raw_new_web_contents = new_web_contents.get();
std::unique_ptr<content::WebContents> original_web_contents_deleter =
tab_strip_model_->DiscardWebContentsAt(1, std::move(new_web_contents));
EXPECT_EQ(original_web_contents, original_web_contents_deleter.get());
EXPECT_FALSE(source_->GetTabLifecycleUnitExternal(original_web_contents));
EXPECT_EQ(tab_lifecycle_unit_external,
source_->GetTabLifecycleUnitExternal(raw_new_web_contents));
original_web_contents_deleter.reset();
// Expect notifications when tabs are closed.
CloseTabsAndExpectNotifications(
tab_strip_model_.get(), {first_lifecycle_unit, second_lifecycle_unit});
}
// Tracks discard notifications for a tab's WebContents.
class TabDiscardNotificationsObserver : public content::MockWebContentsObserver,
public TabStripModelObserver {
public:
TabDiscardNotificationsObserver(TabStripModel* tab_strip_model,
content::WebContents* original_web_contents)
: content::MockWebContentsObserver(original_web_contents),
tab_strip_model_(tab_strip_model) {
tab_strip_model_->AddObserver(this);
}
~TabDiscardNotificationsObserver() override {
tab_strip_model_->RemoveObserver(this);
}
// TabStripModelObserver:
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override {
CHECK_EQ(change.type(), TabStripModelChange::kReplaced);
CHECK_EQ(change.GetReplace()->old_contents, web_contents());
Observe(change.GetReplace()->new_contents);
}
private:
raw_ptr<TabStripModel> tab_strip_model_;
};
TEST_F(TabLifecycleUnitSourceTest, PropagatesWebContentsDiscardNotifications) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &first_lifecycle_unit,
&second_lifecycle_unit);
content::WebContents* original_web_contents =
tab_strip_model_->GetWebContentsAt(1);
::testing::NiceMock<TabDiscardNotificationsObserver>
tab_discard_notifications_observer(tab_strip_model_.get(),
original_web_contents);
EXPECT_CALL(tab_discard_notifications_observer, AboutToBeDiscarded(_));
EXPECT_CALL(tab_discard_notifications_observer, WasDiscarded());
EXPECT_CALL(tab_observer_, MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::ACTIVE, _,
::mojom::LifecycleUnitState::DISCARDED,
LifecycleUnitDiscardReason::PROACTIVE));
EXPECT_TRUE(second_lifecycle_unit->Discard(
LifecycleUnitDiscardReason::PROACTIVE, 100));
}
TEST_F(TabLifecycleUnitSourceTest, UpdateMemorySavingsOnMultipleDiscards) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(/*focus_tab_strip=*/true, &first_lifecycle_unit,
&second_lifecycle_unit);
// Discard the tab.
EXPECT_CALL(tab_observer_, MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::ACTIVE, _,
::mojom::LifecycleUnitState::DISCARDED,
LifecycleUnitDiscardReason::PROACTIVE));
EXPECT_TRUE(second_lifecycle_unit->Discard(
LifecycleUnitDiscardReason::PROACTIVE, 100));
::testing::Mock::VerifyAndClear(&tab_observer_);
const auto* pre_discard_resource_usage =
PreDiscardResourceUsage::FromWebContents(
tab_strip_model_->GetWebContentsAt(1));
EXPECT_NE(pre_discard_resource_usage, nullptr);
EXPECT_EQ(pre_discard_resource_usage->memory_footprint_estimate_kb(), 100u);
// Navigate the tab so that it is no longer discarded.
EXPECT_CALL(tab_observer_, MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::DISCARDED, _,
::mojom::LifecycleUnitState::ACTIVE,
LifecycleUnitDiscardReason::PROACTIVE));
auto navigation = content::NavigationSimulator::CreateBrowserInitiated(
GURL("https://www.example.com"), tab_strip_model_->GetWebContentsAt(1));
navigation->SetKeepLoading(true);
navigation->Commit();
::testing::Mock::VerifyAndClear(&tab_observer_);
// Discarding the tab with a different memory usage should update the
// PreDiscardResourceUsage tab helper.
EXPECT_CALL(tab_observer_, MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::ACTIVE, _,
::mojom::LifecycleUnitState::DISCARDED,
LifecycleUnitDiscardReason::PROACTIVE));
EXPECT_TRUE(second_lifecycle_unit->Discard(
LifecycleUnitDiscardReason::PROACTIVE, 500));
pre_discard_resource_usage = PreDiscardResourceUsage::FromWebContents(
tab_strip_model_->GetWebContentsAt(1));
EXPECT_NE(pre_discard_resource_usage, nullptr);
EXPECT_EQ(pre_discard_resource_usage->memory_footprint_estimate_kb(), 500u);
::testing::Mock::VerifyAndClear(&tab_observer_);
// Expect notifications when tabs are closed.
CloseTabsAndExpectNotifications(
tab_strip_model_.get(), {first_lifecycle_unit, second_lifecycle_unit});
}
TEST_F(TabLifecycleUnitSourceTest, DetachWebContents_Urgent) {
DetachWebContentsTest(LifecycleUnitDiscardReason::URGENT);
}
TEST_F(TabLifecycleUnitSourceTest, DetachWebContents_External) {
DetachWebContentsTest(LifecycleUnitDiscardReason::EXTERNAL);
}
// Regression test for https://crbug.com/818454. Previously, TabLifecycleUnits
// were destroyed from TabStripModelObserver::TabClosingAt(). If a tab was
// detached (TabStripModel::DetachWebContentsAt) and its WebContents destroyed,
// the TabLifecycleUnit was never destroyed. This was solved by giving ownership
// of a tab lifecycleunit to a WebContentsUserData.
TEST_F(TabLifecycleUnitSourceTest, DetachAndDeleteWebContents) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(true /* focus_tab_strip */, &first_lifecycle_unit,
&second_lifecycle_unit);
// Detach and destroy the non-active tab. Verify that the LifecycleUnit is
// destroyed.
std::unique_ptr<tabs::TabModel> detached_tab =
tab_strip_model_->DetachTabAtForInsertion(0);
EXPECT_CALL(tab_observer_, OnLifecycleUnitDestroyed(first_lifecycle_unit));
detached_tab.reset();
::testing::Mock::VerifyAndClear(&tab_observer_);
}
// Tab discarding is tested here rather than in TabLifecycleUnitTest because
// collaboration from the TabLifecycleUnitSource is required to replace the
// WebContents in the TabLifecycleUnit.
TEST_F(TabLifecycleUnitSourceTest, Discard_Urgent) {
DiscardTest(LifecycleUnitDiscardReason::URGENT);
}
TEST_F(TabLifecycleUnitSourceTest, Discard_External) {
DiscardTest(LifecycleUnitDiscardReason::EXTERNAL);
}
TEST_F(TabLifecycleUnitSourceTest, DiscardAndActivate_Urgent) {
DiscardAndActivateTest(LifecycleUnitDiscardReason::URGENT);
}
TEST_F(TabLifecycleUnitSourceTest, DiscardAndActivate_External) {
DiscardAndActivateTest(LifecycleUnitDiscardReason::EXTERNAL);
}
TEST_F(TabLifecycleUnitSourceTest, DiscardAndExplicitlyReload_Urgent) {
DiscardAndExplicitlyReloadTest(LifecycleUnitDiscardReason::URGENT);
}
TEST_F(TabLifecycleUnitSourceTest, DiscardAndExplicitlyReload_External) {
DiscardAndExplicitlyReloadTest(LifecycleUnitDiscardReason::EXTERNAL);
}
TEST_F(TabLifecycleUnitSourceTest, Freeze) {
LifecycleUnit* first_lifecycle_unit = nullptr;
LifecycleUnit* second_lifecycle_unit = nullptr;
CreateTwoTabs(/*focus_tab_strip=*/true, &first_lifecycle_unit,
&second_lifecycle_unit);
// Pretend that the tab is frozen. The observer should be notified and the
// `LifecyleState` should become `FROZEN`.
EXPECT_CALL(tab_observer_, MockOnLifecycleUnitStateChanged(
_, ::mojom::LifecycleUnitState::ACTIVE, _,
::mojom::LifecycleUnitState::FROZEN, _));
TabLifecycleUnitSource::OnLifecycleStateChanged(
second_lifecycle_unit->AsTabLifecycleUnitExternal()->GetWebContents(),
performance_manager::mojom::LifecycleState::kFrozen);
EXPECT_EQ(second_lifecycle_unit->GetState(),
::mojom::LifecycleUnitState::FROZEN);
}
} // namespace resource_coordinator
|