File: browser_switcher_navigation_throttle_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 (170 lines) | stat: -rw-r--r-- 6,289 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
// 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/browser_switcher/browser_switcher_navigation_throttle.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/browser_switcher/browser_switcher_prefs.h"
#include "chrome/browser/browser_switcher/browser_switcher_service.h"
#include "chrome/browser/browser_switcher/browser_switcher_service_factory.h"
#include "chrome/browser/browser_switcher/browser_switcher_sitelist.h"
#include "chrome/browser/browser_switcher/ieem_sitelist_parser.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/mock_navigation_throttle_registry.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

using content::MockNavigationHandle;
using content::NavigationThrottle;

using ::testing::_;
using ::testing::NiceMock;
using ::testing::Return;

namespace browser_switcher {

namespace {

class MockBrowserSwitcherSitelist : public BrowserSwitcherSitelist {
 public:
  MockBrowserSwitcherSitelist() = default;
  ~MockBrowserSwitcherSitelist() override = default;

  MOCK_CONST_METHOD1(GetDecision, Decision(const GURL&));
  MOCK_METHOD1(SetIeemSitelist, void(RawRuleSet&&));
  MOCK_METHOD1(SetExternalSitelist, void(RawRuleSet&&));
  MOCK_METHOD1(SetExternalGreylist, void(RawRuleSet&&));
  MOCK_CONST_METHOD0(GetIeemSitelist, const RuleSet*());
  MOCK_CONST_METHOD0(GetExternalSitelist, const RuleSet*());
  MOCK_CONST_METHOD0(GetExternalGreylist, const RuleSet*());
};

}  // namespace

class BrowserSwitcherNavigationThrottleTest
    : public ChromeRenderViewHostTestHarness {
 public:
  BrowserSwitcherNavigationThrottleTest() = default;

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

    BrowserSwitcherService* service =
        BrowserSwitcherServiceFactory::GetForBrowserContext(
            web_contents()->GetBrowserContext());

    std::unique_ptr<MockBrowserSwitcherSitelist> sitelist =
        std::make_unique<MockBrowserSwitcherSitelist>();
    sitelist_ = sitelist.get();
    service->SetSitelistForTesting(std::move(sitelist));
  }

  void TearDown() override {
    sitelist_ = nullptr;
    ChromeRenderViewHostTestHarness::TearDown();
  }

  std::unique_ptr<MockNavigationHandle> CreateMockNavigationHandle(
      const GURL& url) {
    return std::make_unique<NiceMock<MockNavigationHandle>>(url, main_rfh());
  }

  void CreateNavigationThrottle(content::NavigationThrottleRegistry& registry) {
    BrowserSwitcherNavigationThrottle::MaybeCreateAndAdd(registry);
  }

  MockBrowserSwitcherSitelist* sitelist() { return sitelist_; }

  Decision stay() { return {kStay, kDefault, nullptr}; }

  Decision go() { return {kGo, kSitelist, bogus_rule_.get()}; }

 private:
  raw_ptr<MockBrowserSwitcherSitelist> sitelist_ = nullptr;

  std::unique_ptr<Rule> bogus_rule_ =
      CanonicalizeRule("//example.com/", ParsingMode::kDefault);
};

TEST_F(BrowserSwitcherNavigationThrottleTest, ShouldIgnoreNavigation) {
  EXPECT_CALL(*sitelist(), GetDecision(_)).WillOnce(Return(stay()));
  std::unique_ptr<MockNavigationHandle> handle =
      CreateMockNavigationHandle(GURL("https://example.com/"));
  content::MockNavigationThrottleRegistry registry(
      handle.get(),
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  CreateNavigationThrottle(registry);
  ASSERT_EQ(1u, registry.throttles().size());
  EXPECT_EQ(NavigationThrottle::PROCEED,
            registry.throttles()[0]->WillStartRequest());
}

TEST_F(BrowserSwitcherNavigationThrottleTest, LaunchesOnStartRequest) {
  EXPECT_CALL(*sitelist(), GetDecision(_)).WillOnce(Return(go()));
  std::unique_ptr<MockNavigationHandle> handle =
      CreateMockNavigationHandle(GURL("https://example.com/"));
  content::MockNavigationThrottleRegistry registry(
      handle.get(),
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  CreateNavigationThrottle(registry);
  ASSERT_EQ(1u, registry.throttles().size());
  EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE,
            registry.throttles()[0]->WillStartRequest());
  base::RunLoop().RunUntilIdle();
}

TEST_F(BrowserSwitcherNavigationThrottleTest, LaunchesOnRedirectRequest) {
  EXPECT_CALL(*sitelist(), GetDecision(_))
      .WillOnce(Return(stay()))
      .WillOnce(Return(go()));
  std::unique_ptr<MockNavigationHandle> handle =
      CreateMockNavigationHandle(GURL("https://yahoo.com/"));
  ON_CALL(*handle, WasServerRedirect()).WillByDefault(Return(false));

  content::MockNavigationThrottleRegistry registry(
      handle.get(),
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  CreateNavigationThrottle(registry);
  EXPECT_EQ(1u, registry.throttles().size());
  EXPECT_EQ(NavigationThrottle::PROCEED,
            registry.throttles()[0]->WillStartRequest());

  ON_CALL(*handle, WasServerRedirect()).WillByDefault(Return(true));

  handle->set_url(GURL("https://bing.com/"));
  EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE,
            registry.throttles()[0]->WillRedirectRequest());
  base::RunLoop().RunUntilIdle();
}

TEST_F(BrowserSwitcherNavigationThrottleTest,
       DoNotCreateThrottleOnNonPrimaryMainFrame) {
  std::unique_ptr<MockNavigationHandle> handle =
      CreateMockNavigationHandle(GURL("https://fencedframe.com/"));
  handle->set_has_committed(true);
  handle->set_is_in_primary_main_frame(false);

  content::MockNavigationThrottleRegistry registry(
      handle.get(),
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  CreateNavigationThrottle(registry);
  EXPECT_EQ(0u, registry.throttles().size());

  handle->set_is_in_primary_main_frame(true);
  CreateNavigationThrottle(registry);
  EXPECT_EQ(1u, registry.throttles().size());
}

}  // namespace browser_switcher