OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_LOADER_MOCK_RESOURCE_LOADER_H_ |
| 6 #define CONTENT_BROWSER_LOADER_MOCK_RESOURCE_LOADER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "content/browser/loader/resource_controller.h" |
| 14 #include "net/base/net_errors.h" |
| 15 |
| 16 class GURL; |
| 17 |
| 18 namespace net { |
| 19 struct RedirectInfo; |
| 20 class URLRequestStatus; |
| 21 } |
| 22 |
| 23 namespace content { |
| 24 class ResourceHandler; |
| 25 struct ResourceResponse; |
| 26 |
| 27 // Class that takes the place of the ResourceLoader for tests. It simplifies |
| 28 // testing ResourceHandlers by managing callbacks and performing basic sanity |
| 29 // checks. The test fixture is responsible for advancing states. |
| 30 class MockResourceLoader : public ResourceController { |
| 31 public: |
| 32 explicit MockResourceLoader(ResourceHandler* resource_handler); |
| 33 ~MockResourceLoader() override; |
| 34 |
| 35 // Idle means the ResourceHandler is waiting for the next call from the |
| 36 // TestFixture/ResourceLoader, CALLBACK_PENDING means that the ResourceHandler |
| 37 // will resume the request at some future point to resume the request, and |
| 38 // CANCELED means the ResourceHandler cancelled the request. |
| 39 enum class Status { |
| 40 // The MockLoader is waiting for more data from hte test fixture. |
| 41 IDLE, |
| 42 // The MockLoader is currently in the middle of a call to a handler. Will |
| 43 // switch to CALLBACK_PENDING if the handler defers handling the request. |
| 44 CALLING_HANDLER, |
| 45 // The MockLoader is waiting for a callback from the ResourceHandler. |
| 46 CALLBACK_PENDING, |
| 47 // The request was cancelled. |
| 48 CANCELED, |
| 49 }; |
| 50 |
| 51 // These all run the corresponding methods on ResourceHandler, along with |
| 52 // basic sanity checks for the behavior of the handler. Each check returns the |
| 53 // current status of the ResourceLoader. |
| 54 Status OnWillStart(const GURL& url); |
| 55 Status OnRequestRedirected(const net::RedirectInfo& redirect_info, |
| 56 scoped_refptr<ResourceResponse> response); |
| 57 Status OnResponseStarted(scoped_refptr<ResourceResponse> response); |
| 58 Status OnWillRead(int min_size); |
| 59 Status OnReadCompleted(int bytes_read); |
| 60 Status OnResponseCompleted(const net::URLRequestStatus& status); |
| 61 |
| 62 Status status() const { return status_; } |
| 63 |
| 64 // Network error passed to the first CancelWithError() / Cancel() call, which |
| 65 // is the one the real code uses in the case of multiple cancels. |
| 66 int error_code() const { return error_code_; } |
| 67 |
| 68 private: |
| 69 // ResourceController implementation. |
| 70 void Cancel() override; |
| 71 void CancelAndIgnore() override; |
| 72 void CancelWithError(int error_code) override; |
| 73 void Resume() override; |
| 74 |
| 75 ResourceHandler* const resource_handler_; |
| 76 |
| 77 Status status_ = Status::IDLE; |
| 78 int error_code_ = net::OK; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(MockResourceLoader); |
| 81 }; |
| 82 |
| 83 } // namespace content |
| 84 |
| 85 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_WRAPPER_H_ |
OLD | NEW |