File: session_restore_policy_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (682 lines) | stat: -rw-r--r-- 25,812 bytes parent folder | download | duplicates (6)
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
// 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/resource_coordinator/session_restore_policy.h"

#include <algorithm>
#include <vector>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/simple_test_tick_clock.h"
#include "build/build_config.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/performance_manager/test_support/site_data_utils.h"
#endif
#include "chrome/browser/resource_coordinator/tab_helper.h"
#include "chrome/browser/resource_coordinator/tab_manager_features.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/performance_manager/persistence/site_data/site_data_impl.h"
#include "components/performance_manager/persistence/site_data/site_data_writer.h"
#include "components/performance_manager/public/decorators/site_data_recorder.h"
#include "components/performance_manager/public/performance_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_permission_controller.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 {

// Delegate that exposes testing seams for testing SessionRestorePolicy.
class TestDelegate : public SessionRestorePolicy::Delegate {
 public:
  explicit TestDelegate(base::TickClock* clock) : clock_(clock) {}

  TestDelegate(const TestDelegate&) = delete;
  TestDelegate& operator=(const TestDelegate&) = delete;

  ~TestDelegate() override = default;

  size_t GetNumberOfCores() const override { return number_of_cores_; }
  size_t GetFreeMemoryMiB() const override { return free_memory_mb_; }
  base::TimeTicks NowTicks() const override { return clock_->NowTicks(); }

  size_t GetSiteEngagementScore(
      content::WebContents* unused_contents) const override {
    return site_engagement_score_;
  }

  void SetNumberOfCores(size_t number_of_cores) {
    number_of_cores_ = number_of_cores;
  }

  void SetFreeMemoryMiB(size_t free_memory_mb) {
    free_memory_mb_ = free_memory_mb;
  }

  void SetSiteEngagementScore(size_t site_engagement_score) {
    site_engagement_score_ = site_engagement_score;
  }

 private:
  size_t number_of_cores_ = 1;
  size_t free_memory_mb_ = 0;
  raw_ptr<base::TickClock> clock_ = nullptr;
  size_t site_engagement_score_ = 0;
};

class LenientTabScoreChangeMock {
 public:
  LenientTabScoreChangeMock() = default;
  ~LenientTabScoreChangeMock() = default;

  MOCK_METHOD2(NotifyTabScoreChanged, void(content::WebContents*, float));
};
using TabScoreChangeMock = ::testing::StrictMock<LenientTabScoreChangeMock>;

// Exposes testing functions on SessionRestorePolicy.
class TestSessionRestorePolicy : public SessionRestorePolicy {
 public:
  using SessionRestorePolicy::CalculateAgeScore;
  using SessionRestorePolicy::CalculateSimultaneousTabLoads;
  using SessionRestorePolicy::ScoreTab;
  using SessionRestorePolicy::SetTabLoadsStartedForTesting;
  using SessionRestorePolicy::TabData;
  using SessionRestorePolicy::UpdateSiteEngagementScoreForTesting;

  // Expose some member variables.
  using SessionRestorePolicy::tab_data_;

  // Expose parameters.
  using SessionRestorePolicy::cores_per_simultaneous_tab_load_;
  using SessionRestorePolicy::max_simultaneous_tab_loads_;
  using SessionRestorePolicy::max_tabs_to_restore_;
  using SessionRestorePolicy::max_time_since_last_use_to_restore_;
  using SessionRestorePolicy::mb_free_memory_per_tab_to_restore_;
  using SessionRestorePolicy::min_simultaneous_tab_loads_;
  using SessionRestorePolicy::min_site_engagement_to_restore_;
  using SessionRestorePolicy::min_tabs_to_restore_;
  using SessionRestorePolicy::simultaneous_tab_loads_;

  TestSessionRestorePolicy(bool policy_enabled, const Delegate* delegate)
      : SessionRestorePolicy(policy_enabled, delegate) {}

  TestSessionRestorePolicy(const TestSessionRestorePolicy&) = delete;
  TestSessionRestorePolicy& operator=(const TestSessionRestorePolicy&) = delete;

  ~TestSessionRestorePolicy() override = default;

  using RescoreTabCallback =
      base::RepeatingCallback<bool(content::WebContents*, TabData*)>;

  void SetRescoreTabCallback(RescoreTabCallback rescore_tab_callback) {
    rescore_tab_callback_ = rescore_tab_callback;
  }

  bool RescoreTabAfterDataLoaded(content::WebContents* contents,
                                 TabData* tab_data) override {
    // Invoke the callback if one is provided.
    if (!rescore_tab_callback_.is_null())
      return rescore_tab_callback_.Run(contents, tab_data);
    // Otherwise defer to the default implementation.
    return SessionRestorePolicy::RescoreTabAfterDataLoaded(contents, tab_data);
  }

  float GetTabScore(content::WebContents* contents) const {
    auto it = tab_data_.find(contents);
    return it->second->score;
  }

 private:
  RescoreTabCallback rescore_tab_callback_;
};

}  // namespace

class SessionRestorePolicyTest : public ChromeRenderViewHostTestHarness {
 public:
  SessionRestorePolicyTest() : delegate_(&clock_) {}

  SessionRestorePolicyTest(const SessionRestorePolicyTest&) = delete;
  SessionRestorePolicyTest& operator=(const SessionRestorePolicyTest&) = delete;

  ~SessionRestorePolicyTest() override = default;

  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();

#if !BUILDFLAG(IS_ANDROID)
    // Some tests requires the SiteData database to be initialized.
    site_data_harness_.SetUp();
#endif

    // Set some reasonable delegate constants.
    delegate_.SetNumberOfCores(4);
    delegate_.SetFreeMemoryMiB(1024);
    delegate_.SetSiteEngagementScore(30);

    // Put the clock in the future so that we can LastActiveTimes in the past.
    clock_.Advance(base::Days(1));

    CreateTestContents();
  }

  void TearDown() override {
#if !BUILDFLAG(IS_ANDROID)
    performance_manager::MarkWebContentsAsUnloadedInBackgroundInSiteDataDb(
        contents1_.get());
    performance_manager::MarkWebContentsAsUnloadedInBackgroundInSiteDataDb(
        contents2_.get());
    performance_manager::MarkWebContentsAsUnloadedInBackgroundInSiteDataDb(
        contents3_.get());
#endif
    if (policy_)
      policy_.reset();

    // The WebContents must be deleted before the test harness deletes the
    // RenderProcessHost.
    contents1_.reset();
    contents2_.reset();
    contents3_.reset();

    tab_for_scoring_.clear();

#if !BUILDFLAG(IS_ANDROID)
    site_data_harness_.TearDown(profile());
#endif
    ChromeRenderViewHostTestHarness::TearDown();
  }

  void CreateTestContents() {
    contents1_ = CreateAndInitTestWebContents(
        GURL("https://a.com"), clock_.NowTicks() - base::Hours(1));
    contents2_ = CreateAndInitTestWebContents(
        GURL("https://b.com"), clock_.NowTicks() - base::Hours(2));
    contents3_ = CreateAndInitTestWebContents(
        GURL("https://c.com"), clock_.NowTicks() - base::Hours(3));

    tab_for_scoring_ = {contents1_.get(), contents2_.get(), contents3_.get()};
  }

  std::unique_ptr<content::WebContents> CreateAndInitTestWebContents(
      const GURL& url,
      const base::TimeTicks& last_active) {
    auto contents = CreateTestWebContents();
    auto* tester = content::WebContentsTester::For(contents.get());
    tester->SetLastActiveTimeTicks(last_active);

#if !BUILDFLAG(IS_ANDROID)
    tester->NavigateAndCommit(url);
    performance_manager::MarkWebContentsAsLoadedInBackgroundInSiteDataDb(
        contents.get());
    performance_manager::ExpireSiteDataObservationWindowsForWebContents(
        contents.get());
#endif
    return contents;
  }

  void CreatePolicy(bool policy_enabled) {
    policy_ =
        std::make_unique<TestSessionRestorePolicy>(policy_enabled, &delegate_);

    // Set some reasonable initial parameters.
    policy_->min_simultaneous_tab_loads_ = 1;
    policy_->max_simultaneous_tab_loads_ = 4;
    policy_->cores_per_simultaneous_tab_load_ = 2;
    policy_->min_tabs_to_restore_ = 2;
    policy_->max_tabs_to_restore_ = 30;
    policy_->mb_free_memory_per_tab_to_restore_ = 150;
    policy_->max_time_since_last_use_to_restore_ = base::Hours(6);
    policy_->min_site_engagement_to_restore_ =
        SessionRestorePolicy::kMinSiteEngagementToRestore;

    // Ensure the simultaneous tab loads is properly calculated wrt the above
    // parameters.
    policy_->CalculateSimultaneousTabLoadsForTesting();

    policy_->SetTabScoreChangedCallback(base::BindRepeating(
        &TabScoreChangeMock::NotifyTabScoreChanged, base::Unretained(&mock_)));

    for (content::WebContents* tab : tab_for_scoring_) {
      policy_->AddTabForScoring(tab);
    }
  }

  void WaitForFinalTabScores() {
    base::RunLoop run_loop;
    EXPECT_CALL(mock_, NotifyTabScoreChanged(nullptr, 0.0))
        .WillOnce(::testing::Invoke(
            [&run_loop](content::WebContents*, float) { run_loop.Quit(); }));
    run_loop.Run();
  }

  void AddExtraTabForScoring(content::WebContents* contents) {
    tab_for_scoring_.push_back(contents);
  }

 protected:
  base::SimpleTestTickClock clock_;
  TestDelegate delegate_;

#if !BUILDFLAG(IS_ANDROID)
  performance_manager::SiteDataTestHarness site_data_harness_;
#endif

  TabScoreChangeMock mock_;
  std::unique_ptr<TestSessionRestorePolicy> policy_;

  std::unique_ptr<content::WebContents> contents1_;
  std::unique_ptr<content::WebContents> contents2_;
  std::unique_ptr<content::WebContents> contents3_;

  std::vector<raw_ptr<content::WebContents, VectorExperimental>>
      tab_for_scoring_;
};

TEST_F(SessionRestorePolicyTest, CalculateSimultaneousTabLoads) {
  using TSRP = TestSessionRestorePolicy;

  // Test the minimum is enforced.
  EXPECT_EQ(10u, TSRP::CalculateSimultaneousTabLoads(10 /* min */, 20 /* max */,
                                                     1 /* cores_per_load */,
                                                     1 /* cores */));

  // Test the maximum is enforced.
  EXPECT_EQ(20u, TSRP::CalculateSimultaneousTabLoads(10 /* min */, 20 /* max */,
                                                     1 /* cores_per_load */,
                                                     30 /* cores */));

  // Test the per-core calculation is correct.
  EXPECT_EQ(15u, TSRP::CalculateSimultaneousTabLoads(10 /* min */, 20 /* max */,
                                                     1 /* cores_per_load */,
                                                     15 /* cores */));
  EXPECT_EQ(15u, TSRP::CalculateSimultaneousTabLoads(10 /* min */, 20 /* max */,
                                                     2 /* cores_per_load */,
                                                     30 /* cores */));

  // If no per-core is specified then max is returned.
  EXPECT_EQ(5u, TSRP::CalculateSimultaneousTabLoads(1 /* min */, 5 /* max */,
                                                    0 /* cores_per_load */,
                                                    10 /* cores */));

  // If no per-core and no max is applied, then "max" is returned.
  EXPECT_EQ(
      std::numeric_limits<size_t>::max(),
      TSRP::CalculateSimultaneousTabLoads(
          3 /* min */, 0 /* max */, 0 /* cores_per_load */, 4 /* cores */));
}

TEST_F(SessionRestorePolicyTest, ShouldLoadFeatureEnabled) {
  CreatePolicy(true);
  EXPECT_TRUE(policy_->policy_enabled());
  EXPECT_EQ(2u, policy_->simultaneous_tab_loads());

  WaitForFinalTabScores();

  // By default all the tabs should be loadable.
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->NotifyTabLoadStarted();
  EXPECT_TRUE(policy_->ShouldLoad(contents2_.get()));
  policy_->NotifyTabLoadStarted();
  EXPECT_TRUE(policy_->ShouldLoad(contents3_.get()));
  policy_->NotifyTabLoadStarted();

  // Reset and set a maximum number of tabs to load policy.
  policy_->SetTabLoadsStartedForTesting(0);
  policy_->max_tabs_to_restore_ = 2;
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->NotifyTabLoadStarted();
  EXPECT_TRUE(policy_->ShouldLoad(contents2_.get()));
  policy_->NotifyTabLoadStarted();
  EXPECT_FALSE(policy_->ShouldLoad(contents3_.get()));

  // Disable the number of tab load limits entirely.
  policy_->min_tabs_to_restore_ = 0;
  policy_->max_tabs_to_restore_ = 0;

  // Reset and impose a memory policy.
  policy_->SetTabLoadsStartedForTesting(0);
  constexpr size_t kFreeMemoryLimit = 150;
  policy_->mb_free_memory_per_tab_to_restore_ = kFreeMemoryLimit;
  delegate_.SetFreeMemoryMiB(kFreeMemoryLimit);
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->NotifyTabLoadStarted();
  delegate_.SetFreeMemoryMiB(kFreeMemoryLimit - 1);
  EXPECT_FALSE(policy_->ShouldLoad(contents2_.get()));
  delegate_.SetFreeMemoryMiB(kFreeMemoryLimit + 1);
  EXPECT_TRUE(policy_->ShouldLoad(contents3_.get()));
  policy_->NotifyTabLoadStarted();

  // Disable memory limits to not interfere with later tests.
  policy_->mb_free_memory_per_tab_to_restore_ = 0;

  // Reset and impose a max time since use policy. The contents have ages of 1,
  // 2 and 3 hours respectively.
  policy_->SetTabLoadsStartedForTesting(0);
  policy_->max_time_since_last_use_to_restore_ = base::Minutes(90);
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->NotifyTabLoadStarted();
  EXPECT_FALSE(policy_->ShouldLoad(contents2_.get()));
  EXPECT_FALSE(policy_->ShouldLoad(contents3_.get()));

  // Disable the age limits entirely.
  policy_->max_time_since_last_use_to_restore_ = base::TimeDelta();

  // Reset and impose a site engagement policy.
  policy_->SetTabLoadsStartedForTesting(0);
  policy_->min_site_engagement_to_restore_ =
      SessionRestorePolicy::kMinSiteEngagementToRestore;
  policy_->UpdateSiteEngagementScoreForTesting(
      contents1_.get(), SessionRestorePolicy::kMinSiteEngagementToRestore + 1);
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->UpdateSiteEngagementScoreForTesting(
      contents1_.get(), SessionRestorePolicy::kMinSiteEngagementToRestore);
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->UpdateSiteEngagementScoreForTesting(
      contents1_.get(), SessionRestorePolicy::kMinSiteEngagementToRestore - 1);
  EXPECT_FALSE(policy_->ShouldLoad(contents1_.get()));
}

TEST_F(SessionRestorePolicyTest, ShouldLoadFeatureDisabled) {
  CreatePolicy(false);
  EXPECT_FALSE(policy_->policy_enabled());
  EXPECT_EQ(std::numeric_limits<size_t>::max(),
            policy_->simultaneous_tab_loads());

  WaitForFinalTabScores();

  // Set everything aggressive so it would return false if the feature was
  // enabled.
  policy_->min_tabs_to_restore_ = 0;
  policy_->max_tabs_to_restore_ = 1;
  policy_->mb_free_memory_per_tab_to_restore_ = 1024;
  policy_->max_time_since_last_use_to_restore_ = base::Minutes(1);
  policy_->min_site_engagement_to_restore_ = 100;

  // Make the system look like its effectively out of memory as well.
  delegate_.SetFreeMemoryMiB(1);

  // Everything should still be allowed to load, as the policy engine is
  // disabled.
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->NotifyTabLoadStarted();
  EXPECT_TRUE(policy_->ShouldLoad(contents2_.get()));
  policy_->NotifyTabLoadStarted();
  EXPECT_TRUE(policy_->ShouldLoad(contents3_.get()));
  policy_->NotifyTabLoadStarted();
}

TEST_F(SessionRestorePolicyTest, ShouldLoadBackgroundData) {
  using TabData = TestSessionRestorePolicy::TabData;

  CreatePolicy(true);
  EXPECT_TRUE(policy_->policy_enabled());
  EXPECT_EQ(2u, policy_->simultaneous_tab_loads());

  WaitForFinalTabScores();

  // Disable other limit mechanisms.
  policy_->min_tabs_to_restore_ = 0;
  policy_->max_tabs_to_restore_ = 0;
  policy_->mb_free_memory_per_tab_to_restore_ = 0;
  policy_->max_time_since_last_use_to_restore_ = base::TimeDelta();

  policy_->min_site_engagement_to_restore_ =
      SessionRestorePolicy::kMinSiteEngagementToRestore;
  policy_->UpdateSiteEngagementScoreForTesting(
      contents1_.get(), SessionRestorePolicy::kMinSiteEngagementToRestore + 1);
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->UpdateSiteEngagementScoreForTesting(
      contents1_.get(), SessionRestorePolicy::kMinSiteEngagementToRestore);
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
  policy_->UpdateSiteEngagementScoreForTesting(
      contents1_.get(), SessionRestorePolicy::kMinSiteEngagementToRestore - 1);
  EXPECT_FALSE(policy_->ShouldLoad(contents1_.get()));

  // Mark the tab as using background communication mechanisms, and expect the
  // site engagement policy to no longer be applied.
  auto iter = policy_->tab_data_.insert(
      std::make_pair(contents1_.get(), std::make_unique<TabData>()));
  TabData* tab_data = iter.first->second.get();
  tab_data->used_in_bg = true;
  EXPECT_TRUE(policy_->ShouldLoad(contents1_.get()));
}

TEST_F(SessionRestorePolicyTest, NotificationPermissionSetUsedInBgBit) {
  CreatePolicy(true);
  WaitForFinalTabScores();

  auto iter = policy_->tab_data_.find(contents1_.get());
  EXPECT_TRUE(iter != policy_->tab_data_.end());
  EXPECT_FALSE(iter->second->UsedInBg());

  // Allow |contents1_| to display notifications, this should cause the
  // |used_in_bg| bit to change to true.
  GetBrowserContext()->SetPermissionControllerForTesting(
      std::make_unique<content::MockPermissionController>());

  // Adding/Removing the tab for scoring will cause the callback to be called a
  // few times, ignore this.
  EXPECT_CALL(mock_, NotifyTabScoreChanged(::testing::_, ::testing::_))
      .Times(::testing::AnyNumber());

  policy_->RemoveTabForScoring(contents1_.get());
  policy_->AddTabForScoring(contents1_.get());
  WaitForFinalTabScores();

  iter = policy_->tab_data_.find(contents1_.get());
  EXPECT_TRUE(iter != policy_->tab_data_.end());
  EXPECT_TRUE(iter->second->UsedInBg());
}

TEST_F(SessionRestorePolicyTest, MultipleAllTabsDoneCallbacks) {
  CreatePolicy(true);
  WaitForFinalTabScores();

  // Another "all tabs scored" notification should be sent after more tabs
  // are added to the policy engine.
  std::unique_ptr<content::WebContents> contents4 =
      CreateAndInitTestWebContents(GURL("https://d.com"),
                                   base::TimeTicks::Now());
  std::unique_ptr<content::WebContents> contents5 =
      CreateAndInitTestWebContents(GURL("https://e.com"),
                                   base::TimeTicks::Now());
  policy_->AddTabForScoring(contents4.get());
  policy_->AddTabForScoring(contents5.get());
  WaitForFinalTabScores();

  performance_manager::MarkWebContentsAsUnloadedInBackgroundInSiteDataDb(
      contents4.get());
  performance_manager::MarkWebContentsAsUnloadedInBackgroundInSiteDataDb(
      contents5.get());
}

TEST_F(SessionRestorePolicyTest, CalculateAgeScore) {
  using TabData = TestSessionRestorePolicy::TabData;
  constexpr int kMonthInSeconds = 60 * 60 * 24 * 31;

  // Generate a bunch of random tab ages.
  std::vector<std::unique_ptr<TabData>> tab_data;
  tab_data.reserve(1000);

  for (size_t i = 0; i < 1000; ++i)
    tab_data.push_back(std::make_unique<TabData>());

  // Generate some known edge cases.
  size_t i = 0;
  tab_data[i++]->last_active = base::Milliseconds(-1001);
  tab_data[i++]->last_active = base::Milliseconds(-1000);
  tab_data[i++]->last_active = base::Milliseconds(-999);
  tab_data[i++]->last_active = base::Milliseconds(-500);
  tab_data[i++]->last_active = base::Milliseconds(0);
  tab_data[i++]->last_active = base::Milliseconds(500);
  tab_data[i++]->last_active = base::Milliseconds(999);
  tab_data[i++]->last_active = base::Milliseconds(1000);
  tab_data[i++]->last_active = base::Milliseconds(1001);

  // Generate a logarithmic selection of ages to test the whole range.
  tab_data[i++]->last_active = base::Seconds(-1000000);
  tab_data[i++]->last_active = base::Seconds(-100000);
  tab_data[i++]->last_active = base::Seconds(-10000);
  tab_data[i++]->last_active = base::Seconds(-1000);
  tab_data[i++]->last_active = base::Seconds(-100);
  tab_data[i++]->last_active = base::Seconds(-10);
  tab_data[i++]->last_active = base::Seconds(10);
  tab_data[i++]->last_active = base::Seconds(100);
  tab_data[i++]->last_active = base::Seconds(1000);
  tab_data[i++]->last_active = base::Seconds(10000);
  tab_data[i++]->last_active = base::Seconds(100000);
  tab_data[i++]->last_active = base::Seconds(1000000);
  tab_data[i++]->last_active = base::Seconds(10000000);

  // Generate a bunch more random ages.
  for (; i < tab_data.size(); ++i) {
    tab_data[i]->last_active =
        base::Seconds(base::RandInt(-kMonthInSeconds, kMonthInSeconds));
  }

  // Calculate the tab scores.
  for (i = 0; i < tab_data.size(); ++i) {
    tab_data[i]->score =
        TestSessionRestorePolicy::CalculateAgeScore(tab_data[i].get());
  }

  // Sort tabs by increasing last active time.
  std::sort(tab_data.begin(), tab_data.end(),
            [](const std::unique_ptr<TabData>& td1,
               const std::unique_ptr<TabData>& td2) {
              return td1->last_active < td2->last_active;
            });

  // The scores should be in decreasing order (>= is necessary because some
  // last active times collapse to the same score).
  for (i = 1; i < tab_data.size(); ++i)
    ASSERT_GE(tab_data[i - 1]->score, tab_data[i]->score);
}

TEST_F(SessionRestorePolicyTest, ScoreTab) {
  using TabData = TestSessionRestorePolicy::TabData;

  TabData td_bg;
  td_bg.used_in_bg = true;
  td_bg.last_active = base::Days(30);
  EXPECT_TRUE(TestSessionRestorePolicy::ScoreTab(&td_bg));

  TabData td_normal_young;
  TabData td_normal_old;
  td_normal_young.last_active = base::Seconds(1);
  td_normal_old.last_active = base::Days(7);
  EXPECT_TRUE(TestSessionRestorePolicy::ScoreTab(&td_normal_young));
  EXPECT_TRUE(TestSessionRestorePolicy::ScoreTab(&td_normal_old));

  TabData td_internal;
  td_internal.is_internal = true;
  EXPECT_TRUE(TestSessionRestorePolicy::ScoreTab(&td_internal));

  // Check the score produces the expected ordering of tabs.
  EXPECT_LT(td_internal.score, td_normal_old.score);
  EXPECT_LT(td_normal_old.score, td_normal_young.score);
  EXPECT_LT(td_normal_young.score, td_bg.score);
}

TEST_F(SessionRestorePolicyTest, RescoringSendsNotification) {
  using TabData = TestSessionRestorePolicy::TabData;

  // Inject code that causes some tabs to receive updated scores.
  CreatePolicy(true);
  policy_->SetRescoreTabCallback(base::BindLambdaForTesting(
      [&](content::WebContents* contents, TabData* tab_data) {
        float delta = 0;
        if (contents == contents1_.get())
          delta = 1.0;
        else if (contents == contents2_.get())
          delta = 2.0;
        tab_data->score += delta;
        return delta != 0;
      }));

  // Get the current scores.
  float score1 = policy_->GetTabScore(contents1_.get());
  float score2 = policy_->GetTabScore(contents2_.get());

  // Expect tab score change notifications for the first 2 tabs, but not the
  // third.
  EXPECT_CALL(mock_, NotifyTabScoreChanged(contents1_.get(), score1 + 1.0));
  EXPECT_CALL(mock_, NotifyTabScoreChanged(contents2_.get(), score2 + 2.0));
  WaitForFinalTabScores();
}

#if !BUILDFLAG(IS_ANDROID)
TEST_F(SessionRestorePolicyTest, FeatureUsageSetUsedInBgBit) {
  CreatePolicy(true);
  WaitForFinalTabScores();

  auto iter = policy_->tab_data_.find(contents1_.get());
  EXPECT_TRUE(iter != policy_->tab_data_.end());
  EXPECT_FALSE(iter->second->UsedInBg());

  // Indicates that |contents1_| might update its title while in background,
  // this should set the |used_in_bg_| bit.
  base::WeakPtr<performance_manager::PageNode> page_node =
      performance_manager::PerformanceManager::GetPrimaryPageNodeForWebContents(
          contents1_.get());
  EXPECT_TRUE(page_node);
  auto* impl = performance_manager::GetSiteDataImplForPageNode(page_node.get());
  EXPECT_TRUE(impl);
  impl->NotifyUpdatesTitleInBackground();

  // Adding/Removing the tab for scoring will cause the callback to be called a
  // few times, ignore this.
  EXPECT_CALL(mock_, NotifyTabScoreChanged(::testing::_, ::testing::_))
      .Times(::testing::AnyNumber());

  policy_->RemoveTabForScoring(contents1_.get());
  policy_->AddTabForScoring(contents1_.get());
  WaitForFinalTabScores();

  iter = policy_->tab_data_.find(contents1_.get());
  EXPECT_TRUE(iter != policy_->tab_data_.end());
  EXPECT_TRUE(iter->second->UsedInBg());
}

TEST_F(SessionRestorePolicyTest, UnknownUsageSetUsedInBgBit) {
  auto contents = CreateTestWebContents();
  auto* tester = content::WebContentsTester::For(contents.get());
  ResourceCoordinatorTabHelper::CreateForWebContents(contents.get());
  tester->NavigateAndCommit(GURL("https://d.com"));

  // Adding/Removing the tab for scoring will cause the callback to be called a
  // few times, ignore this.
  EXPECT_CALL(mock_, NotifyTabScoreChanged(::testing::_, ::testing::_))
      .Times(::testing::AnyNumber());
  AddExtraTabForScoring(contents.get());

  CreatePolicy(true);
  WaitForFinalTabScores();

  base::WeakPtr<performance_manager::PageNode> page_node =
      performance_manager::PerformanceManager::GetPrimaryPageNodeForWebContents(
          contents.get());
  EXPECT_TRUE(page_node);
  auto* impl = performance_manager::GetSiteDataImplForPageNode(page_node.get());
  EXPECT_TRUE(impl);
  performance_manager::SiteFeatureUsage title_feature_usage =
      impl->UpdatesTitleInBackground();
  EXPECT_EQ(performance_manager::SiteFeatureUsage::kSiteFeatureUsageUnknown,
            title_feature_usage);

  auto iter = policy_->tab_data_.find(contents.get());
  EXPECT_TRUE(iter != policy_->tab_data_.end());
  EXPECT_TRUE(iter->second->UsedInBg());
}
#endif

}  // namespace resource_coordinator