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