OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_LOADER_MOCK_RESOURCE_LOADER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_MOCK_RESOURCE_LOADER_H_ |
6 #define CONTENT_BROWSER_LOADER_MOCK_RESOURCE_LOADER_H_ | 6 #define CONTENT_BROWSER_LOADER_MOCK_RESOURCE_LOADER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
15 #include "content/browser/loader/resource_controller.h" | 16 #include "content/browser/loader/resource_controller.h" |
| 17 #include "content/browser/loader/resource_handler.h" |
16 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
18 | 20 |
19 class GURL; | 21 class GURL; |
20 | 22 |
21 namespace net { | 23 namespace net { |
22 struct RedirectInfo; | 24 struct RedirectInfo; |
23 class URLRequestStatus; | 25 class URLRequestStatus; |
24 } | 26 } |
25 | 27 |
26 namespace content { | 28 namespace content { |
27 class ResourceHandler; | |
28 struct ResourceResponse; | 29 struct ResourceResponse; |
29 | 30 |
30 // Class that takes the place of the ResourceLoader for tests. It simplifies | 31 // Class that takes the place of the ResourceLoader for tests. It simplifies |
31 // testing ResourceHandlers by managing callbacks and performing basic sanity | 32 // testing ResourceHandlers by managing callbacks and performing basic sanity |
32 // checks. The test fixture is responsible for advancing states. | 33 // checks. The test fixture is responsible for advancing states. |
33 class MockResourceLoader : public ResourceController { | 34 class MockResourceLoader : public ResourceHandler::Delegate { |
34 public: | 35 public: |
35 explicit MockResourceLoader(ResourceHandler* resource_handler); | 36 explicit MockResourceLoader(ResourceHandler* resource_handler); |
36 ~MockResourceLoader() override; | 37 ~MockResourceLoader() override; |
37 | 38 |
38 // Idle means the ResourceHandler is waiting for the next call from the | 39 // Idle means the ResourceHandler is waiting for the next call from the |
39 // TestFixture/ResourceLoader, CALLBACK_PENDING means that the ResourceHandler | 40 // TestFixture/ResourceLoader, CALLBACK_PENDING means that the ResourceHandler |
40 // will resume the request at some future point to resume the request, and | 41 // will resume the request at some future point to resume the request, and |
41 // CANCELED means the ResourceHandler cancelled the request. | 42 // CANCELED means the ResourceHandler cancelled the request. |
42 enum class Status { | 43 enum class Status { |
43 // The MockLoader is waiting for more data from hte test fixture. | 44 // The MockLoader is waiting for more data from hte test fixture. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // Network error passed to the first CancelWithError() / Cancel() call, which | 79 // Network error passed to the first CancelWithError() / Cancel() call, which |
79 // is the one the real code uses in the case of multiple cancels. | 80 // is the one the real code uses in the case of multiple cancels. |
80 int error_code() const { return error_code_; } | 81 int error_code() const { return error_code_; } |
81 | 82 |
82 // Returns IOBuffer returned by last call to OnWillRead. The IOBuffer is | 83 // Returns IOBuffer returned by last call to OnWillRead. The IOBuffer is |
83 // release on read complete, on response complete, and on cancel. | 84 // release on read complete, on response complete, and on cancel. |
84 net::IOBuffer* io_buffer() const { return io_buffer_.get(); } | 85 net::IOBuffer* io_buffer() const { return io_buffer_.get(); } |
85 int io_buffer_size() const { return io_buffer_size_; } | 86 int io_buffer_size() const { return io_buffer_size_; } |
86 | 87 |
87 private: | 88 private: |
88 // ResourceController implementation. | 89 class TestResourceController; |
89 void Cancel() override; | 90 |
90 void CancelAndIgnore() override; | 91 // ResourceHandler::Delegate implementation: |
91 void CancelWithError(int error_code) override; | 92 void OutOfBandCancel(int error_code, bool tell_renderer) override; |
92 void Resume() override; | 93 |
| 94 void OnCancel(int error_code); |
| 95 void OnResume(); |
93 | 96 |
94 ResourceHandler* const resource_handler_; | 97 ResourceHandler* const resource_handler_; |
95 | 98 |
96 Status status_ = Status::IDLE; | 99 Status status_ = Status::IDLE; |
97 int error_code_ = net::OK; | 100 int error_code_ = net::OK; |
| 101 bool canceled_out_of_band_ = false; |
98 | 102 |
99 scoped_refptr<net::IOBuffer> io_buffer_; | 103 scoped_refptr<net::IOBuffer> io_buffer_; |
100 int io_buffer_size_ = 0; | 104 int io_buffer_size_ = 0; |
101 | 105 |
102 std::unique_ptr<base::RunLoop> canceled_or_idle_run_loop_; | 106 std::unique_ptr<base::RunLoop> canceled_or_idle_run_loop_; |
103 | 107 |
| 108 base::WeakPtrFactory<MockResourceLoader> weak_factory_; |
| 109 |
104 DISALLOW_COPY_AND_ASSIGN(MockResourceLoader); | 110 DISALLOW_COPY_AND_ASSIGN(MockResourceLoader); |
105 }; | 111 }; |
106 | 112 |
107 } // namespace content | 113 } // namespace content |
108 | 114 |
109 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_WRAPPER_H_ | 115 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_WRAPPER_H_ |
OLD | NEW |