1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_PUBLIC_TEST_BACK_FORWARD_CACHE_UTIL_H_
6 #define CONTENT_PUBLIC_TEST_BACK_FORWARD_CACHE_UTIL_H_
7 
8 #include <memory>
9 
10 #include "base/strings/string_piece.h"
11 #include "content/public/browser/back_forward_cache.h"
12 
13 namespace content {
14 class WebContents;
15 
16 // This is a helper class to check in the tests that back-forward cache
17 // was disabled for a particular reason.
18 //
19 // This class should be created in the beginning of the test and will
20 // know about all BackForwardCache::DisableForRenderFrameHost which
21 // happened during its lifetime.
22 //
23 // Typical usage pattern:
24 //
25 // BackForwardCacheDisabledTester helper;
26 // NavigateToURL(page_with_feature);
27 // NavigateToURL(away);
28 // EXPECT_TRUE/FALSE(helper.IsDisabledForFrameWithReason());
29 
30 class BackForwardCacheDisabledTester {
31  public:
32   BackForwardCacheDisabledTester();
33   ~BackForwardCacheDisabledTester();
34 
35   bool IsDisabledForFrameWithReason(int process_id,
36                                     int frame_routing_id,
37                                     base::StringPiece reason);
38 
39  private:
40   // Impl has to inherit from BackForwardCacheImpl, which is
41   // a content/-internal concept, so we can include it only from
42   // .cc file.
43   class Impl;
44   std::unique_ptr<Impl> impl_;
45 };
46 
47 // Helper function to be used when the tests are interested in covering the
48 // scenarios when back-forward cache is not used. This is similar to method
49 // BackForwardCache::DisableForTesting(), but it takes a WebContents instead of
50 // a BackForwardCache. This method disables BackForwardCache for a given
51 // WebContents with the reason specified.
52 //
53 // Note that it is preferred to make the test work with BackForwardCache when
54 // feasible, or have a standalone test with BackForwardCache enabled to test
55 // the functionality when necessary.
56 void DisableBackForwardCacheForTesting(
57     WebContents* web_contents,
58     BackForwardCache::DisableForTestingReason reason);
59 
60 }  // namespace content
61 
62 #endif  // CONTENT_PUBLIC_TEST_BACK_FORWARD_CACHE_UTIL_H_
63