File: chained_back_navigation_tracker_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (176 lines) | stat: -rw-r--r-- 7,208 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
// 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 "chrome/browser/chained_back_navigation_tracker.h"

#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/navigation_simulator.h"

class ChainedBackNavigationTrackerTest
    : public ChromeRenderViewHostTestHarness {
 public:
  ChainedBackNavigationTrackerTest()
      : ChromeRenderViewHostTestHarness(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

 protected:
  std::vector<GURL> test_urls() {
    std::vector<GURL> urls;
    for (uint32_t i = 0; i < min_navigation_cnt_ * 2; ++i) {
      urls.push_back(GURL("http://foo/" + base::NumberToString(i)));
    }
    return urls;
  }

  const uint32_t min_navigation_cnt_ =
      ChainedBackNavigationTracker::kMinimumChainedBackNavigationLength;
  const int64_t max_navigation_interval_ = ChainedBackNavigationTracker::
      kMaxChainedBackNavigationIntervalInMilliseconds;
};

TEST_F(ChainedBackNavigationTrackerTest, ChainedBackNavigationStatus) {
  const std::vector<GURL> urls = test_urls();
  for (const GURL& url : urls) {
    NavigateAndCommit(url);
  }

  ChainedBackNavigationTracker::CreateForWebContents(web_contents());
  const ChainedBackNavigationTracker* tracker =
      ChainedBackNavigationTracker::FromWebContents(web_contents());
  ASSERT_TRUE(tracker);

  // Before any back navigation, the return value for these two checker
  // functions should be false.
  ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());

  for (uint32_t i = 1; i < urls.size(); ++i) {
    content::NavigationSimulator::GoBack(web_contents());
    // Since `RecordBackButtonClickForChainedBackNavigation()` is never called,
    // `IsBackButtonChainedBackNavigationRecentlyPerformed()` should always
    // returns false.
    ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());
    // The check should only return true when the number of consecutive back
    // navigation is smaller than `kMinimumChainedBackNavigationLength`.
    if (i < min_navigation_cnt_) {
      ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
    } else {
      ASSERT_TRUE(tracker->IsChainedBackNavigationRecentlyPerformed());
    }
  }
}

TEST_F(ChainedBackNavigationTrackerTest,
       ChainedBackNavigationStatus_ResetCountIfIntervalIsTooLong) {
  const std::vector<GURL> urls = test_urls();
  for (const GURL& url : urls) {
    NavigateAndCommit(url);
  }

  ChainedBackNavigationTracker::CreateForWebContents(web_contents());
  const ChainedBackNavigationTracker* tracker =
      ChainedBackNavigationTracker::FromWebContents(web_contents());
  ASSERT_TRUE(tracker);

  // Before any back navigation, the return value for these two checker
  // functions should be false.
  ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());

  for (uint32_t i = 1; i < min_navigation_cnt_; ++i) {
    content::NavigationSimulator::GoBack(web_contents());
    // The checks should always return false since the number of consecutive
    // back navigation is smaller than `kMinimumChainedBackNavigationLength`.
    ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
    ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());
  }

  // After waiting for sufficiently long interval, the counter should be reset
  // so the checks should always return false.
  task_environment()->FastForwardBy(
      base::Milliseconds(max_navigation_interval_ * 2));
  ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());

  for (uint32_t i = 1; i < min_navigation_cnt_; ++i) {
    content::NavigationSimulator::GoBack(web_contents());
    ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());
    ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  }
}

TEST_F(
    ChainedBackNavigationTrackerTest,
    ChainedBackNavigationStatus_ResetCountIfNonBackForwardNavigationHappens) {
  const std::vector<GURL> urls = test_urls();
  for (const GURL& url : urls) {
    NavigateAndCommit(url);
  }

  ChainedBackNavigationTracker::CreateForWebContents(web_contents());
  const ChainedBackNavigationTracker* tracker =
      ChainedBackNavigationTracker::FromWebContents(web_contents());
  ASSERT_TRUE(tracker);

  // Before any back navigation, the return value for these two checker
  // functions should be false.
  ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());

  for (uint32_t i = 1; i < min_navigation_cnt_; ++i) {
    content::NavigationSimulator::GoBack(web_contents());
    ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());
    ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  }

  // After performing another non history navigation, the counter should be
  // reset so the checks should always return false.
  NavigateAndCommit(GURL("http://bar/1"));
  ASSERT_EQ(0u, tracker->chained_back_navigation_count_);

  for (uint32_t i = 1; i < min_navigation_cnt_; ++i) {
    content::NavigationSimulator::GoBack(web_contents());
    ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());
    ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  }
}

TEST_F(ChainedBackNavigationTrackerTest,
       ChainedBackNavigationStatus_BackButtonClicked) {
  const std::vector<GURL> urls = test_urls();
  for (const GURL& url : urls) {
    NavigateAndCommit(url);
  }

  ChainedBackNavigationTracker::CreateForWebContents(web_contents());
  ChainedBackNavigationTracker* tracker =
      ChainedBackNavigationTracker::FromWebContents(web_contents());
  ASSERT_TRUE(tracker);

  // Before any back navigation, the return value for these two checker
  // functions should be false.
  ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
  ASSERT_FALSE(tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());

  for (uint32_t i = 1; i < urls.size(); ++i) {
    tracker->RecordBackButtonClickForChainedBackNavigation();
    content::NavigationSimulator::GoBack(web_contents());
    // The checks should only return true when the number of consecutive back
    // navigation is greater than or equal to
    // `kMinimumChainedBackNavigationLength`.
    if (i >= min_navigation_cnt_) {
      ASSERT_TRUE(tracker->IsChainedBackNavigationRecentlyPerformed());
      ASSERT_TRUE(
          tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());
    } else {
      ASSERT_FALSE(tracker->IsChainedBackNavigationRecentlyPerformed());
      ASSERT_FALSE(
          tracker->IsBackButtonChainedBackNavigationRecentlyPerformed());
    }
  }
}