File: back_forward_cache_limit_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (525 lines) | stat: -rw-r--r-- 21,838 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 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/functional/bind.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "content/browser/back_forward_cache_browsertest.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test_utils.h"

namespace content {

class BackgroundForegroundProcessLimitBackForwardCacheBrowserTest
    : public BackForwardCacheBrowserTest {
 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EnableFeatureAndSetParams(features::kBackForwardCache, "cache_size",
                              base::NumberToString(kBackForwardCacheSize));
    EnableFeatureAndSetParams(
        features::kBackForwardCache, "foreground_cache_size",
        base::NumberToString(kForegroundBackForwardCacheSize));
    BackForwardCacheBrowserTest::SetUpCommandLine(command_line);
  }

  void ExpectCached(const RenderFrameHostImplWrapper& rfh,
                    bool cached,
                    bool backgrounded) {
    EXPECT_FALSE(rfh.IsDestroyed());
    EXPECT_EQ(cached, rfh->IsInBackForwardCache());
    EXPECT_EQ(backgrounded, rfh->GetProcess()->GetPriority() ==
                                base::Process::Priority::kBestEffort);
  }
  // The number of pages the BackForwardCache can hold per tab.
  const size_t kBackForwardCacheSize = 4;
  const size_t kForegroundBackForwardCacheSize = 2;
  const size_t kPruneSize = 1u;
  const NotRestoredReason kPruneReason =
      NotRestoredReason::kCacheLimitPrunedOnModerateMemoryPressure;
};

// Test that a series of same-site navigations (which use the same process)
// uses the foreground limit.
IN_PROC_BROWSER_TEST_F(
    BackgroundForegroundProcessLimitBackForwardCacheBrowserTest,
    CacheEvictionSameSite) {
  ASSERT_TRUE(embedded_test_server()->Start());

  std::vector<RenderFrameHostImplWrapper> rfhs;

  for (size_t i = 0; i <= kBackForwardCacheSize * 2; ++i) {
    SCOPED_TRACE(i);
    GURL url(embedded_test_server()->GetURL(
        "a.com", base::StringPrintf("/title1.html?i=%zu", i)));
    ASSERT_TRUE(NavigateToURL(shell(), url));
    rfhs.emplace_back(current_frame_host());
    EXPECT_NE(rfhs.back()->GetProcess()->GetPriority(),
              base::Process::Priority::kBestEffort);

    for (size_t j = 0; j <= i; ++j) {
      SCOPED_TRACE(j);
      // The last page is active, the previous |kForegroundBackForwardCacheSize|
      // should be in the cache, any before that should be deleted.
      if (i - j <= kForegroundBackForwardCacheSize) {
        // All of the processes should be in the foreground.
        ExpectCached(rfhs[j], /*cached=*/i != j,
                     /*backgrounded=*/false);
      } else {
        ASSERT_TRUE(rfhs[j].WaitUntilRenderFrameDeleted());
      }
    }
  }

  // Navigate back but not to the initial about:blank.
  for (size_t i = 0; i <= kBackForwardCacheSize * 2 - 1; ++i) {
    SCOPED_TRACE(i);
    ASSERT_TRUE(HistoryGoBack(web_contents()));
    // The first |kBackForwardCacheSize| navigations should be restored from the
    // cache. The rest should not.
    if (i < kForegroundBackForwardCacheSize) {
      ExpectRestored(FROM_HERE);
    } else {
      ExpectNotRestored({NotRestoredReason::kForegroundCacheLimit}, {}, {}, {},
                        {}, FROM_HERE);
    }
  }
}

// Test that a series of cross-site navigations (which use different processes)
// use the background limit.
//
// TODO(crbug.com/40179515): This test is flaky. It has been re-enabled with
// improved failure output (https://crrev.com/c/2862346). It's OK to disable it
// again when it fails.
IN_PROC_BROWSER_TEST_F(
    BackgroundForegroundProcessLimitBackForwardCacheBrowserTest,
    CacheEvictionCrossSite) {
  ASSERT_TRUE(embedded_test_server()->Start());

  std::vector<RenderFrameHostImplWrapper> rfhs;

  for (size_t i = 0; i <= kBackForwardCacheSize * 2; ++i) {
    SCOPED_TRACE(i);
    // Note: do NOT use .com domains here because a4.com is on the HSTS preload
    // list, which will cause our test requests to timeout.
    GURL url(embedded_test_server()->GetURL(base::StringPrintf("a%zu.test", i),
                                            "/title1.html"));
    ASSERT_TRUE(NavigateToURL(shell(), url));
    rfhs.emplace_back(current_frame_host());
    EXPECT_NE(rfhs.back()->GetProcess()->GetPriority(),
              base::Process::Priority::kBestEffort);

    for (size_t j = 0; j <= i; ++j) {
      SCOPED_TRACE(j);
      // The last page is active, the previous |kBackgroundBackForwardCacheSize|
      // should be in the cache, any before that should be deleted.
      if (i - j <= kBackForwardCacheSize) {
        EXPECT_FALSE(rfhs[j].IsDestroyed());
        // Pages except the active one should be cached and in the background.
        ExpectCached(rfhs[j], /*cached=*/i != j,
                     /*backgrounded=*/i != j);
      } else {
        ASSERT_TRUE(rfhs[j].WaitUntilRenderFrameDeleted());
      }
    }
  }

  // Navigate back but not to the initial about:blank.
  for (size_t i = 0; i <= kBackForwardCacheSize * 2 - 1; ++i) {
    SCOPED_TRACE(i);
    ASSERT_TRUE(HistoryGoBack(web_contents()));
    // The first |kBackForwardCacheSize| navigations should be restored from the
    // cache. The rest should not.
    if (i < kBackForwardCacheSize) {
      ExpectRestored(FROM_HERE);
    } else {
      ExpectNotRestored({NotRestoredReason::kCacheLimit}, {}, {}, {}, {},
                        FROM_HERE);
    }
  }
}

// Test that pruning a series of cross-site navigations (which use different
// processes) evicts the right entries with the right reason.
IN_PROC_BROWSER_TEST_F(
    BackgroundForegroundProcessLimitBackForwardCacheBrowserTest,
    PruneCrossSite) {
  ASSERT_TRUE(embedded_test_server()->Start());

  std::vector<RenderFrameHostImplWrapper> rfhs;

  for (size_t i = 0; i < kBackForwardCacheSize; ++i) {
    SCOPED_TRACE(i);
    // Note: do NOT use .com domains here because a4.com is on the HSTS preload
    // list, which will cause our test requests to timeout.
    GURL url(embedded_test_server()->GetURL(base::StringPrintf("a%zu.test", i),
                                            "/title1.html"));
    ASSERT_TRUE(NavigateToURL(shell(), url));
    rfhs.emplace_back(current_frame_host());
    EXPECT_NE(rfhs.back()->GetProcess()->GetPriority(),
              base::Process::Priority::kBestEffort);
  }

  CHECK_LE(kPruneSize, kBackForwardCacheSize);

  // Prune the BFCache entries.
  web_contents()->GetController().GetBackForwardCache().Prune(kPruneSize,
                                                              kPruneReason);

  for (int i = kBackForwardCacheSize - 1 - 1 - kPruneSize; i >= 0; --i) {
    SCOPED_TRACE(i);
    ASSERT_TRUE(rfhs[i].WaitUntilRenderFrameDeleted());
  }

  // Navigate back but not to the initial about:blank.
  for (size_t i = 0; i < kBackForwardCacheSize - 1; ++i) {
    SCOPED_TRACE(i);
    ASSERT_TRUE(HistoryGoBack(web_contents()));
    // The first `kPruneSize` navigation should be restored from the cache. The
    // rest should not.
    if (i < kPruneSize) {
      ExpectRestored(FROM_HERE);
    } else {
      ExpectNotRestored({kPruneReason}, {}, {}, {}, {}, FROM_HERE);
    }
  }
}

namespace {

const char kPrioritizedPageURL[] = "search.result";

}  // namespace

class BackForwardCacheLimitForPrioritizedPagesBrowserTest
    : public BackgroundForegroundProcessLimitBackForwardCacheBrowserTest,
      public testing::WithParamInterface<std::string> {
 protected:
  // Mock subclass of ContentBrowserClient that will determine if the url is
  // prioritized by checking against `kPrioritizedPageURL`.
  class MockContentBrowserClientWithPrioritizedBackForwardCacheEntry
      : public ContentBrowserTestContentBrowserClient {
   public:
    // ContentBrowserClient overrides:
    bool ShouldPrioritizeForBackForwardCache(BrowserContext* browser_context,
                                             const GURL& url) override {
      return url.DomainIs(kPrioritizedPageURL);
    }
  };

  void SetUpOnMainThread() override {
    BackgroundForegroundProcessLimitBackForwardCacheBrowserTest::
        SetUpOnMainThread();
    test_client_ = std::make_unique<
        MockContentBrowserClientWithPrioritizedBackForwardCacheEntry>();
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    EnableFeatureAndSetParams(kBackForwardCachePrioritizedEntry, "level",
                              GetParam());
    BackgroundForegroundProcessLimitBackForwardCacheBrowserTest::
        SetUpCommandLine(command_line);
  }

  bool ShouldPrioritizeWhenClearAllUnlessNoEviction() {
    return GetParam() == "prioritize-unless-should-clear-all-and-no-eviction";
  }

 private:
  std::unique_ptr<MockContentBrowserClientWithPrioritizedBackForwardCacheEntry>
      test_client_;
};

INSTANTIATE_TEST_SUITE_P(
    All,
    BackForwardCacheLimitForPrioritizedPagesBrowserTest,
    testing::Values("prioritize-unless-should-clear-all",
                    "prioritize-unless-should-clear-all-and-no-eviction"));

// Test that both when pruning with size 0, if no other eviction happens, the
// prioritized entry would be evicted.
IN_PROC_BROWSER_TEST_P(BackForwardCacheLimitForPrioritizedPagesBrowserTest,
                       PruneToZero_NoOtherEvictionHappens) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // We need at least 1 entries in the BFCache list for this test.
  CHECK_GE(kBackForwardCacheSize, 1u);

  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title1.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("b.test", "/title1.html")));

  // Now the BFCache entry list is: [pp, b].
  // Prune the BFCache entries to 0.
  web_contents()->GetController().GetBackForwardCache().Prune(0, kPruneReason);
  // All the entries should be evicted
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectNotRestored({kPruneReason}, {}, {}, {}, {}, FROM_HERE);
}

// Test that both when pruning with size 0, if there is another entry evicted,
// the prioritized entry would be:
// - evicted if the level is prioritize-unless-should-clear-all
// - not evicted if the level is
// prioritize-unless-should-clear-all-and-no-eviction.
IN_PROC_BROWSER_TEST_P(BackForwardCacheLimitForPrioritizedPagesBrowserTest,
                       PruneToZero_OtherEvictionHappens) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // We need at least 2 entries in the BFCache list for this test.
  CHECK_GE(kBackForwardCacheSize, 2u);

  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("a.test", "/title1.html")));
  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title1.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("b.test", "/title1.html")));

  // Now the BFCache entry list is: [a, pp, b].
  // Prune the BFCache entries to 0.
  web_contents()->GetController().GetBackForwardCache().Prune(0, kPruneReason);
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  if (ShouldPrioritizeWhenClearAllUnlessNoEviction()) {
    // If the level is prioritize-unless-should-clear-all-and-no-eviction, the
    // prioritized entry should be restored since the some other eviction
    // happens.
    ExpectRestored(FROM_HERE);
  } else {
    // Otherwise the prioritized entry should be evicted as well.
    ExpectNotRestored({kPruneReason}, {}, {}, {}, {}, FROM_HERE);
  }
  // The non-prioritized entry should always be evicted.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectNotRestored({kPruneReason}, {}, {}, {}, {}, FROM_HERE);
}

// Test that when pruning with a positive number size, the last prioritized
// entry outside the limit will not be evicted.
IN_PROC_BROWSER_TEST_P(BackForwardCacheLimitForPrioritizedPagesBrowserTest,
                       PruneToNonZero_PrioritizedEntryOutsideLimit) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // We need at least 4 entries in the BFCache list for this test.
  CHECK_GE(kBackForwardCacheSize, 4u);

  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title1.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("a.test", "/title1.html")));
  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title2.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("b.test", "/title1.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("c.test", "/title1.html")));

  // Now the BFCache entry list is: [pe1, a, pe2, b].
  // Prune the BFCache entries to 1, the result should be:
  // [pe1(evicted), a(evicted), pe2(prioritized entry special rule), b].
  web_contents()->GetController().GetBackForwardCache().Prune(1, kPruneReason);

  // The last non-prioritized entry should be restored because it's within the
  // cache limit.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);
  // The last prioritized entry should be restored since it's the special
  // prioritized entry.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);
  // The other entries (including the prioritized one) should not be restored.
  for (size_t i = 0; i < 2; ++i) {
    ASSERT_TRUE(HistoryGoBack(web_contents()));
    ExpectNotRestored({kPruneReason}, {}, {}, {}, {}, FROM_HERE);
  }
}

// Test that when pruning with a positive number size, the last prioritized
// entry inside the limit should be counted as the regular cache.
IN_PROC_BROWSER_TEST_P(BackForwardCacheLimitForPrioritizedPagesBrowserTest,
                       PruneToNonZero_PrioritizedEntryInsideLimit) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // We need at least 2 entries in the BFCache list for this test.
  CHECK_GE(kBackForwardCacheSize, 2u);

  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("a.test", "/title1.html")));
  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title2.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("b.test", "/title1.html")));

  // Now the BFCache entry list is: [a, pe].
  // Prune the BFCache entries to 1, the result should be:
  // [a(evicted), pe].
  web_contents()->GetController().GetBackForwardCache().Prune(1, kPruneReason);
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectNotRestored({kPruneReason}, {}, {}, {}, {}, FROM_HERE);
}

// Test that when pruning with a positive number size while there is already an
// old prioritized entry kept in cache before, it will be replaced by the newer
// prioritized entry.
IN_PROC_BROWSER_TEST_P(BackForwardCacheLimitForPrioritizedPagesBrowserTest,
                       PruneToNonZeroTwice) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // We need at least 4 entries in the BFCache list for this test.
  CHECK_LE(kBackForwardCacheSize, 4u);

  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title1.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("a.test", "/title1.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("b.test", "/title1.html")));

  // Now the BFCache entry list is: [pe1, a].
  // Prune the BFCache entries to 1, the result should still be
  // [pe1(prioritized entry special rule), a].
  web_contents()->GetController().GetBackForwardCache().Prune(1, kPruneReason);

  // The last non-prioritized entry should be restored because it's within the
  // cache limit.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);
  // The last prioritized entry should be restored since it's the special
  // prioritized entry.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);

  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("c.test", "/title1.html")));
  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title2.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("d.test", "/title1.html")));
  ASSERT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("e.test", "/title1.html")));

  // Now the BFCache entry list is: [pe1, c, pe2, d].
  // Prune the BFCache entries to 1, the result should still be
  // [pe1(evicted), a(evicted), pe2(prioritized entry special rule), d].
  web_contents()->GetController().GetBackForwardCache().Prune(1, kPruneReason);

  // The last non-prioritized entry should be restored because it's within the
  // cache limit.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);
  // The last prioritized entry should be restored since it's the special
  // prioritized entry.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);
  // The other entries (including the prioritized one) should not be restored.
  for (size_t i = 0; i < 2; ++i) {
    ASSERT_TRUE(HistoryGoBack(web_contents()));
    ExpectNotRestored({kPruneReason}, {}, {}, {}, {}, FROM_HERE);
  }
}

// Test that the prioritized BFCache entry will not be evicted even when another
// entry is stored and exceeds the limit.
IN_PROC_BROWSER_TEST_P(BackForwardCacheLimitForPrioritizedPagesBrowserTest,
                       CacheLimitReached) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // We need at least 1 entry in the BFCache list for this test.
  CHECK_GE(kBackForwardCacheSize, 1u);

  ASSERT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
                                         kPrioritizedPageURL, "/title1.html")));

  // Fill the BFCache with more entry and make it just exceeds the limit, the
  // result should be:
  // [pe(prioritized entry special rule), a0, a1, ...].
  for (size_t i = 0; i <= kBackForwardCacheSize; ++i) {
    ASSERT_TRUE(NavigateToURL(
        shell(), embedded_test_server()->GetURL(
                     base::StringPrintf("a%zu.test", i), "/title1.html")));
  }
  // For the entries within cache size limit, they should be restored.
  for (size_t i = 0; i < kBackForwardCacheSize; ++i) {
    ASSERT_TRUE(HistoryGoBack(web_contents()));
    ExpectRestored(FROM_HERE);
  }
  // The prioritized entry should be restored as well even if it's outside the
  // limit.
  ASSERT_TRUE(HistoryGoBack(web_contents()));
  ExpectRestored(FROM_HERE);
}

// Test that the cache responds to processes switching from background to
// foreground. We set things up so that we have
// Cached sites:
//   a0.test
//   a1.test
//   a2.test
//   a3.test
// and the active page is a4.test. Then set the process for a[1-3] to
// foregrounded so that there are 3 entries whose processes are foregrounded.
// BFCache should evict the eldest (a1) leaving a0 because despite being older,
// it is backgrounded. Setting the priority directly is not ideal but there is
// no reliable way to cause the processes to go into the foreground just by
// navigating because proactive browsing instance swap makes it impossible to
// reliably create a new a1.test renderer in the same process as the old
// a1.test.
//
// Note that we do NOT use .com domains because a4.com is on the HSTS preload
// list.  Since our test server doesn't use HTTPS, using a4.com results in the
// test timing out.
IN_PROC_BROWSER_TEST_F(
    BackgroundForegroundProcessLimitBackForwardCacheBrowserTest,
    ChangeToForeground) {
  ASSERT_TRUE(embedded_test_server()->Start());

  std::vector<RenderFrameHostImplWrapper> rfhs;

  // Navigate through a[0-3].com.
  for (size_t i = 0; i < kBackForwardCacheSize; ++i) {
    SCOPED_TRACE(i);
    GURL url(embedded_test_server()->GetURL(base::StringPrintf("a%zu.test", i),
                                            "/title1.html"));
    ASSERT_TRUE(NavigateToURL(shell(), url));
    rfhs.emplace_back(current_frame_host());
    EXPECT_NE(rfhs.back()->GetProcess()->GetPriority(),
              base::Process::Priority::kBestEffort);
  }
  // Check that a0-2 are cached and backgrounded.
  for (size_t i = 0; i < kBackForwardCacheSize - 1; ++i) {
    SCOPED_TRACE(i);
    ExpectCached(rfhs[i], /*cached=*/true, /*backgrounded=*/true);
  }

  // Navigate to a page which causes the processes for a[1-3] to be
  // foregrounded.
  GURL url(embedded_test_server()->GetURL("a4.test", "/title1.html"));
  ASSERT_TRUE(NavigateToURL(shell(), url));

  // Assert that we really have set up the situation we want where the processes
  // are shared and in the foreground.
  RenderFrameHostImpl* rfh = current_frame_host();
  ASSERT_NE(rfh->GetProcess()->GetPriority(),
            base::Process::Priority::kBestEffort);

  rfhs[1]->GetProcess()->OnMediaStreamAdded();
  rfhs[2]->GetProcess()->OnMediaStreamAdded();
  rfhs[3]->GetProcess()->OnMediaStreamAdded();

  // The page should be evicted.
  ASSERT_TRUE(rfhs[1].WaitUntilRenderFrameDeleted());

  // Check that a0 is cached and backgrounded.
  ExpectCached(rfhs[0], /*cached=*/true, /*backgrounded=*/true);
  // Check that a2-3 are cached and foregrounded.
  ExpectCached(rfhs[2], /*cached=*/true, /*backgrounded=*/false);
  ExpectCached(rfhs[3], /*cached=*/true, /*backgrounded=*/false);
}

}  // namespace content