summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/testing/weburl_loader_mock_factory_impl.h
blob: 3fdebfd51427dad8bdc9553f94ee9e1758e67daa (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WebURLLoaderMockFactoryImpl_h
#define WebURLLoaderMockFactoryImpl_h

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/platform/web_url_error.h"
#include "third_party/blink/public/platform/web_url_loader_mock_factory.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/platform/web_url_response.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/kurl_hash.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"

namespace network {
struct ResourceRequest;
}  // namespace network

namespace blink {

class TestingPlatformSupport;
class WebData;
class WebURLLoader;
class WebURLLoaderMock;
class WebURLLoaderTestDelegate;

// A factory that creates WebURLLoaderMock to simulate resource loading in
// tests. Since there are restriction and rules to follow, please read comments
// in WebURLLoaderMockFactory carefully to use this class correctly.
class WebURLLoaderMockFactoryImpl : public WebURLLoaderMockFactory {
 public:
  WebURLLoaderMockFactoryImpl(TestingPlatformSupport*);
  ~WebURLLoaderMockFactoryImpl() override;

  // WebURLLoaderMockFactory:
  std::unique_ptr<WebURLLoader> CreateURLLoader() override;
  void RegisterURL(const WebURL& url,
                   const WebURLResponse& response,
                   const WebString& file_path = WebString()) override;
  void RegisterErrorURL(const WebURL& url,
                        const WebURLResponse& response,
                        const WebURLError& error) override;
  void UnregisterURL(const WebURL& url) override;
  void RegisterURLProtocol(const WebString& protocol,
                           const WebURLResponse& response,
                           const WebString& file_path) override;
  void UnregisterURLProtocol(const WebString& protocol) override;
  void UnregisterAllURLsAndClearMemoryCache() override;
  void ServeAsynchronousRequests() override;
  void SetLoaderDelegate(WebURLLoaderTestDelegate* delegate) override {
    delegate_ = delegate;
  }
  void FillNavigationParamsResponse(WebNavigationParams*) override;

  // Returns true if |url| was registered for being mocked.
  bool IsMockedURL(const WebURL& url);

  // Called by the loader to load a resource.
  void LoadSynchronously(std::unique_ptr<network::ResourceRequest> request,
                         WebURLResponse* response,
                         base::Optional<WebURLError>* error,
                         WebData* data,
                         int64_t* encoded_data_length);
  void LoadAsynchronouly(std::unique_ptr<network::ResourceRequest> request,
                         WebURLLoaderMock* loader);

  // Removes the loader from the list of pending loaders.
  void CancelLoad(WebURLLoaderMock* loader);

 private:
  struct ResponseInfo {
    WebURLResponse response;
    base::FilePath file_path;
  };

  virtual void RunUntilIdle();

  // Loads the specified request and populates the response, error and data
  // accordingly.
  void LoadRequest(const WebURL& url,
                   WebURLResponse* response,
                   base::Optional<WebURLError>* error,
                   WebData* data);

  // Checks if the loader is pending. Otherwise, it may have been deleted.
  bool IsPending(base::WeakPtr<WebURLLoaderMock> loader);

  // Looks up an URL in the mock URL table.
  //
  // If the URL is found, returns true and sets |error| and |response_info|.
  bool LookupURL(const WebURL& url,
                 base::Optional<WebURLError>* error,
                 ResponseInfo* response_info);

  // Reads |m_filePath| and puts its content in |data|.
  // Returns true if it successfully read the file.
  static bool ReadFile(const base::FilePath& file_path, WebData* data);

  WebURLLoaderTestDelegate* delegate_ = nullptr;

  // The loaders that have not being served data yet.
  using LoaderToRequestMap =
      HashMap<WebURLLoaderMock*, std::unique_ptr<network::ResourceRequest>>;
  LoaderToRequestMap pending_loaders_;

  // All values must be valid, but we use Optional because HashMap requires
  // "empty value".
  typedef HashMap<KURL, base::Optional<WebURLError>> URLToErrorMap;
  URLToErrorMap url_to_error_info_;

  // Table of the registered URLs and the responses that they should receive.
  using URLToResponseMap = HashMap<KURL, ResponseInfo>;
  URLToResponseMap url_to_response_info_;

  // Table of the registered URL protocols and the responses that they should
  // receive.
  using ProtocolToResponseMap = HashMap<String, ResponseInfo>;
  ProtocolToResponseMap protocol_to_response_info_;

  TestingPlatformSupport* platform_;

  DISALLOW_COPY_AND_ASSIGN(WebURLLoaderMockFactoryImpl);
};

}  // namespace blink

#endif  // WebURLLoaderMockFactoryImpl_h