File: btm_page_visit_observer_unittest.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 (178 lines) | stat: -rw-r--r-- 6,901 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
// 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 "content/browser/btm/btm_page_visit_observer.h"

#include <memory>

#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "content/browser/btm/btm_page_visit_observer_test_utils.h"
#include "content/public/browser/cookie_access_details.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "content/test/test_web_contents.h"
#include "net/base/net_errors.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"

using testing::AllOf;
using testing::ElementsAre;
using testing::IsEmpty;

namespace content {

class BtmPageVisitObserverTest : public testing::Test {
 public:
  WebContents* web_contents() { return web_contents_.get(); }

 protected:
  BrowserTaskEnvironment task_environment_;
  TestBrowserContext browser_context_;
  RenderViewHostTestEnabler rvh_test_enabler_;
  std::unique_ptr<WebContents> web_contents_ =
      WebContentsTester::CreateTestWebContents(&browser_context_,
                                               /*SiteInstance=*/nullptr);
};

TEST_F(BtmPageVisitObserverTest, PreviousPage) {
  auto* tester = WebContentsTester::For(web_contents());
  const GURL url1("http://a.test/");
  const GURL url2("http://b.test/");
  BtmPageVisitRecorder recorder(web_contents());

  tester->NavigateAndCommit(url1);
  ASSERT_TRUE(recorder.WaitForSize(1));
  EXPECT_EQ(recorder.visits()[0].prev_page.url, GURL());
  EXPECT_EQ(recorder.visits()[0].navigation.destination.url, url1);

  tester->NavigateAndCommit(url2);
  ASSERT_TRUE(recorder.WaitForSize(2));
  EXPECT_EQ(recorder.visits()[1].prev_page.url, url1);
  EXPECT_EQ(recorder.visits()[1].navigation.destination.url, url2);
}

TEST_F(BtmPageVisitObserverTest, ServerRedirects) {
  const GURL url1("http://a.test/");
  const GURL url2("http://b.test/");
  const GURL url3("http://c.test/");
  BtmPageVisitRecorder recorder(web_contents());

  // Navigate to url1.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  // Navigate to url2, but redirect to url3.
  auto nav = NavigationSimulator::CreateBrowserInitiated(url2, web_contents());
  nav->Start();
  nav->Redirect(url3);
  nav->Commit();
  ASSERT_TRUE(recorder.WaitForSize(2));

  // Two navigations are observed, the second with a redirect.
  const BtmNavigationInfo& navigation1 = recorder.visits()[0].navigation;
  EXPECT_EQ(navigation1.server_redirects.size(), 0u);
  EXPECT_EQ(navigation1.destination.url, url1);
  const BtmNavigationInfo& navigation2 = recorder.visits()[1].navigation;
  ASSERT_EQ(navigation2.server_redirects.size(), 1u);
  EXPECT_EQ(navigation2.server_redirects[0].url, url2);
  EXPECT_EQ(navigation2.destination.url, url3);
}

TEST_F(BtmPageVisitObserverTest, IgnoreUncommitted) {
  const GURL url1("http://a.test/");
  const GURL url2("http://b.test/");
  const GURL url3("http://c.test/");
  BtmPageVisitRecorder recorder(web_contents());

  // Navigate to url1 and commit.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  // Navigate to url2, but don't commit.
  auto nav = NavigationSimulator::CreateBrowserInitiated(url2, web_contents());
  nav->Start();
  nav->AbortCommit();
  // Navigate to url3 and commit.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url3);
  ASSERT_TRUE(recorder.WaitForSize(2));

  // Only url1 and url3 navigations are observed.
  EXPECT_EQ(recorder.visits()[0].navigation.destination.url, url1);
  EXPECT_EQ(recorder.visits()[1].prev_page.url, url1);
  EXPECT_EQ(recorder.visits()[1].navigation.destination.url, url3);
}

TEST_F(BtmPageVisitObserverTest, IgnoreSubframes) {
  const GURL url1("http://a.test/");
  const GURL url2("http://b.test/");
  const GURL url3("http://c.test/");
  BtmPageVisitRecorder recorder(web_contents());

  // Top-level navigation to url1.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  // Subframe navigation to url2.
  RenderFrameHost* iframe =
      RenderFrameHostTester::For(web_contents()->GetPrimaryMainFrame())
          ->AppendChild("iframe");
  NavigationSimulator::NavigateAndCommitFromDocument(url2, iframe);
  // Top-level navigation to url3.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url3);
  ASSERT_TRUE(recorder.WaitForSize(2));

  // Only url1 and url3 navigations are observed.
  EXPECT_EQ(recorder.visits()[0].navigation.destination.url, url1);
  EXPECT_EQ(recorder.visits()[1].prev_page.url, url1);
  EXPECT_EQ(recorder.visits()[1].navigation.destination.url, url3);
}

// Same-document navigations are ignored.
TEST_F(BtmPageVisitObserverTest, IgnoreSameDocument) {
  const GURL url1a("http://a.test/");
  const GURL url1b("http://a.test/#top");
  const GURL url2("http://b.test/");
  BtmPageVisitRecorder recorder(web_contents());

  // Navigate to url1a
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1a);
  // Navigate to same-document url1b.
  auto nav = NavigationSimulator::CreateBrowserInitiated(url1b, web_contents());
  nav->Start();
  nav->CommitSameDocument();
  // Navigate to url2.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url2);
  ASSERT_TRUE(recorder.WaitForSize(2));

  // Only the url1a and url2 navigations are observed.
  EXPECT_EQ(recorder.visits()[0].navigation.destination.url, url1a);
  EXPECT_EQ(recorder.visits()[1].prev_page.url, url1a);
  EXPECT_EQ(recorder.visits()[1].navigation.destination.url, url2);
}

TEST_F(BtmPageVisitObserverTest, FlushPendingVisitsAtDestruction) {
  int counter = 0;

  {
    BtmPageVisitObserver observer(
        web_contents(),
        base::BindLambdaForTesting(
            [&counter](BtmPageVisitInfo, BtmNavigationInfo) { ++counter; }));
    NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(),
                                                      GURL("http://a.test/"));
    NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(),
                                                      GURL("http://b.test/"));
    NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(),
                                                      GURL("http://c.test/"));
  }

  // If observer's pending visits isn't flushed, `counter` will (typically)
  // still be 0. But if they are, all three visits will be recorded.
  EXPECT_EQ(counter, 3);
}

}  // namespace content