File: browser_switcher_navigation_throttle_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (154 lines) | stat: -rw-r--r-- 5,717 bytes parent folder | download
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
// 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/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));
  }

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

  std::unique_ptr<NavigationThrottle> CreateNavigationThrottle(
      content::NavigationHandle* handle) {
    return BrowserSwitcherNavigationThrottle::MaybeCreateThrottleFor(handle);
  }

  MockBrowserSwitcherSitelist* sitelist() { return sitelist_; }

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

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

 private:
  raw_ptr<MockBrowserSwitcherSitelist, DanglingUntriaged> sitelist_;

  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/"));
  std::unique_ptr<NavigationThrottle> throttle =
      CreateNavigationThrottle(handle.get());
  EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest());
}

TEST_F(BrowserSwitcherNavigationThrottleTest, LaunchesOnStartRequest) {
  EXPECT_CALL(*sitelist(), GetDecision(_)).WillOnce(Return(go()));
  std::unique_ptr<MockNavigationHandle> handle =
      CreateMockNavigationHandle(GURL("https://example.com/"));
  std::unique_ptr<NavigationThrottle> throttle =
      CreateNavigationThrottle(handle.get());
  EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE,
            throttle->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));

  std::unique_ptr<NavigationThrottle> throttle =
      CreateNavigationThrottle(handle.get());
  EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest());

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

  handle->set_url(GURL("https://bing.com/"));
  EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE,
            throttle->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);

  std::unique_ptr<NavigationThrottle> throttle_non_primary_main_frame =
      CreateNavigationThrottle(handle.get());
  EXPECT_EQ(nullptr, throttle_non_primary_main_frame.get());

  handle->set_is_in_primary_main_frame(true);
  std::unique_ptr<NavigationThrottle> throttle_in_primary_main_frame =
      CreateNavigationThrottle(handle.get());
  EXPECT_NE(nullptr, throttle_in_primary_main_frame.get());
}

}  // namespace browser_switcher