File: pdf_url_loader_request_interceptor_unittest.cc

package info (click to toggle)
chromium 138.0.7204.92-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,576 kB
  • sloc: cpp: 34,933,512; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,956; 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 (134 lines) | stat: -rw-r--r-- 4,755 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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/pdf/browser/pdf_url_loader_request_interceptor.h"

#include <memory>
#include <utility>

#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "components/pdf/browser/fake_pdf_stream_delegate.h"
#include "components/pdf/browser/pdf_stream_delegate.h"
#include "content/public/browser/url_loader_request_interceptor.h"
#include "content/public/test/test_renderer_host.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "services/network/test/mock_url_loader_client.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace pdf {

namespace {

using ::network::MockURLLoaderClient;

using ::testing::NiceMock;

class PdfURLLoaderRequestInterceptorTest
    : public content::RenderViewHostTestHarness {
 protected:
  PdfURLLoaderRequestInterceptorTest() {
    resource_request_.mode = network::mojom::RequestMode::kNavigate;
    resource_request_.url = GURL(FakePdfStreamDelegate::kDefaultOriginalUrl);
  }

  void SetUp() override {
    content::RenderViewHostTestHarness::SetUp();

    content::RenderFrameHostTester* tester =
        content::RenderFrameHostTester::For(main_rfh());
    tester->InitializeRenderFrameIfNeeded();
    child_frame_tree_node_id_ =
        tester->AppendChild("PDF content frame")->GetFrameTreeNodeId();
  }

  std::unique_ptr<PdfURLLoaderRequestInterceptor> CreateInterceptor() {
    return std::make_unique<PdfURLLoaderRequestInterceptor>(
        child_frame_tree_node_id_, std::move(stream_delegate_));
  }

  std::unique_ptr<FakePdfStreamDelegate> stream_delegate_ =
      std::make_unique<FakePdfStreamDelegate>();

  network::ResourceRequest resource_request_;
  base::MockCallback<content::URLLoaderRequestInterceptor::LoaderCallback>
      loader_callback_;
  content::FrameTreeNodeId child_frame_tree_node_id_;
};

void RunRequestHandler(
    content::URLLoaderRequestInterceptor::RequestHandler request_handler) {
  base::RunLoop run_loop;

  NiceMock<MockURLLoaderClient> mock_client;
  EXPECT_CALL(mock_client, OnReceiveResponse).WillOnce([&run_loop]() {
    run_loop.Quit();
  });

  mojo::Receiver<network::mojom::URLLoaderClient> client_receiver(&mock_client);
  std::move(request_handler)
      .Run({}, {}, client_receiver.BindNewPipeAndPassRemote());

  run_loop.Run();
}

}  // namespace

TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateInterceptor) {
  EXPECT_TRUE(PdfURLLoaderRequestInterceptor::MaybeCreateInterceptor(
      child_frame_tree_node_id_, std::move(stream_delegate_)));
}

TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoader) {
  EXPECT_CALL(loader_callback_, Run(base::test::IsNotNullCallback()))
      .WillOnce(RunRequestHandler);

  auto interceptor = CreateInterceptor();
  interceptor->MaybeCreateLoader(resource_request_, browser_context(),
                                 loader_callback_.Get());
}

TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderNotNavigate) {
  EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));

  auto interceptor = CreateInterceptor();
  resource_request_.mode = network::mojom::RequestMode::kCors;
  interceptor->MaybeCreateLoader(resource_request_, browser_context(),
                                 loader_callback_.Get());
}

TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderDeleteContents) {
  EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));

  auto interceptor = CreateInterceptor();
  DeleteContents();
  interceptor->MaybeCreateLoader(resource_request_, browser_context(),
                                 loader_callback_.Get());
}

TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderNoStreamInfo) {
  EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));

  stream_delegate_->clear_stream_info();
  auto interceptor = CreateInterceptor();
  interceptor->MaybeCreateLoader(resource_request_, browser_context(),
                                 loader_callback_.Get());
}

TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderOtherUrl) {
  EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));

  auto interceptor = CreateInterceptor();
  resource_request_.url = GURL("https://example.test");
  interceptor->MaybeCreateLoader(resource_request_, browser_context(),
                                 loader_callback_.Get());
}

}  // namespace pdf