OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_COMMON_TEST_URL_FETCHER_FACTORY_H_ | 5 #ifndef CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
6 #define CONTENT_COMMON_TEST_URL_FETCHER_FACTORY_H_ | 6 #define CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
| 14 #include "base/threading/non_thread_safe.h" |
14 #include "content/common/url_fetcher.h" | 15 #include "content/common/url_fetcher.h" |
| 16 #include "googleurl/src/gurl.h" |
15 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
16 #include "googleurl/src/gurl.h" | 18 |
| 19 // Changes URLFetcher's Factory for the lifetime of the object. |
| 20 // Note that this scoper cannot be nested (to make it even harder to misuse). |
| 21 class ScopedURLFetcherFactory : public base::NonThreadSafe { |
| 22 public: |
| 23 explicit ScopedURLFetcherFactory(URLFetcher::Factory* factory); |
| 24 virtual ~ScopedURLFetcherFactory(); |
| 25 |
| 26 private: |
| 27 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); |
| 28 }; |
17 | 29 |
18 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of | 30 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of |
19 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates | 31 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates |
20 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is | 32 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is |
21 // expected that you'll grab the delegate from the TestURLFetcher and invoke | 33 // expected that you'll grab the delegate from the TestURLFetcher and invoke |
22 // the callback method when appropriate. In this way it's easy to mock a | 34 // the callback method when appropriate. In this way it's easy to mock a |
23 // URLFetcher. | 35 // URLFetcher. |
24 // Typical usage: | 36 // Typical usage: |
25 // // TestURLFetcher requires a MessageLoop: | 37 // // TestURLFetcher requires a MessageLoop: |
26 // MessageLoopForUI message_loop; | 38 // MessageLoopForUI message_loop; |
27 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. | 39 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. |
28 // BrowserThread io_thread(BrowserThread::IO, &message_loop); | 40 // BrowserThread io_thread(BrowserThread::IO, &message_loop); |
29 // // Create and register factory. | 41 // // Create factory (it automatically sets itself as URLFetcher's factory). |
30 // TestURLFetcherFactory factory; | 42 // TestURLFetcherFactory factory; |
31 // URLFetcher::set_factory(&factory); | |
32 // // Do something that triggers creation of a URLFetcher. | 43 // // Do something that triggers creation of a URLFetcher. |
33 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); | 44 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); |
34 // DCHECK(fetcher); | 45 // DCHECK(fetcher); |
35 // // Notify delegate with whatever data you want. | 46 // // Notify delegate with whatever data you want. |
36 // fetcher->delegate()->OnURLFetchComplete(...); | 47 // fetcher->delegate()->OnURLFetchComplete(...); |
37 // // Make sure consumer of URLFetcher does the right thing. | 48 // // Make sure consumer of URLFetcher does the right thing. |
38 // ... | 49 // ... |
39 // // Reset factory. | |
40 // URLFetcher::set_factory(NULL); | |
41 // | 50 // |
42 // Note: if you don't know when your request objects will be created you | 51 // Note: if you don't know when your request objects will be created you |
43 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes | 52 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes |
44 // below. | 53 // below. |
45 | 54 |
46 class TestURLFetcher : public URLFetcher { | 55 class TestURLFetcher : public URLFetcher { |
47 public: | 56 public: |
48 TestURLFetcher(int id, | 57 TestURLFetcher(int id, |
49 const GURL& url, | 58 const GURL& url, |
50 RequestType request_type, | 59 RequestType request_type, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 net::URLRequestStatus fake_status_; | 120 net::URLRequestStatus fake_status_; |
112 int fake_response_code_; | 121 int fake_response_code_; |
113 std::string fake_response_string_; | 122 std::string fake_response_string_; |
114 FilePath fake_response_file_path_; | 123 FilePath fake_response_file_path_; |
115 | 124 |
116 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | 125 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); |
117 }; | 126 }; |
118 | 127 |
119 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers | 128 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers |
120 // are registered in a map by the id passed to the create method. | 129 // are registered in a map by the id passed to the create method. |
121 class TestURLFetcherFactory : public URLFetcher::Factory { | 130 class TestURLFetcherFactory : public URLFetcher::Factory, |
| 131 public ScopedURLFetcherFactory { |
122 public: | 132 public: |
123 TestURLFetcherFactory(); | 133 TestURLFetcherFactory(); |
124 virtual ~TestURLFetcherFactory(); | 134 virtual ~TestURLFetcherFactory(); |
125 | 135 |
126 virtual URLFetcher* CreateURLFetcher(int id, | 136 virtual URLFetcher* CreateURLFetcher(int id, |
127 const GURL& url, | 137 const GURL& url, |
128 URLFetcher::RequestType request_type, | 138 URLFetcher::RequestType request_type, |
129 URLFetcher::Delegate* d); | 139 URLFetcher::Delegate* d); |
130 TestURLFetcher* GetFetcherByID(int id) const; | 140 TestURLFetcher* GetFetcherByID(int id) const; |
131 void RemoveFetcherFromMap(int id); | 141 void RemoveFetcherFromMap(int id); |
(...skipping 18 matching lines...) Expand all Loading... |
150 // | 160 // |
151 // This class is not thread-safe. You should not call SetFakeResponse or | 161 // This class is not thread-safe. You should not call SetFakeResponse or |
152 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is | 162 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is |
153 // OK to start URLFetcher objects while setting or clearning fake responses | 163 // OK to start URLFetcher objects while setting or clearning fake responses |
154 // since already created URLFetcher objects will not be affected by any changes | 164 // since already created URLFetcher objects will not be affected by any changes |
155 // made to the fake responses (once a URLFetcher object is created you cannot | 165 // made to the fake responses (once a URLFetcher object is created you cannot |
156 // change its fake response). | 166 // change its fake response). |
157 // | 167 // |
158 // Example usage: | 168 // Example usage: |
159 // FakeURLFetcherFactory factory; | 169 // FakeURLFetcherFactory factory; |
160 // URLFetcher::set_factory(&factory); | |
161 // | 170 // |
162 // // You know that class SomeService will request url http://a.com/ and you | 171 // // You know that class SomeService will request url http://a.com/ and you |
163 // // want to test the service class by returning an error. | 172 // // want to test the service class by returning an error. |
164 // factory.SetFakeResponse("http://a.com/", "", false); | 173 // factory.SetFakeResponse("http://a.com/", "", false); |
165 // // But if the service requests http://b.com/asdf you want to respond with | 174 // // But if the service requests http://b.com/asdf you want to respond with |
166 // // a simple html page and an HTTP/200 code. | 175 // // a simple html page and an HTTP/200 code. |
167 // factory.SetFakeResponse("http://b.com/asdf", | 176 // factory.SetFakeResponse("http://b.com/asdf", |
168 // "<html><body>hello world</body></html>", | 177 // "<html><body>hello world</body></html>", |
169 // true); | 178 // true); |
170 // | 179 // |
171 // SomeService service; | 180 // SomeService service; |
172 // service.Run(); // Will eventually request these two URLs. | 181 // service.Run(); // Will eventually request these two URLs. |
173 | 182 |
174 class FakeURLFetcherFactory : public URLFetcher::Factory { | 183 class FakeURLFetcherFactory : public URLFetcher::Factory, |
| 184 public ScopedURLFetcherFactory { |
175 public: | 185 public: |
176 FakeURLFetcherFactory(); | 186 FakeURLFetcherFactory(); |
177 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown | 187 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown |
178 // url to the given factory. | 188 // url to the given factory. |
179 explicit FakeURLFetcherFactory(URLFetcher::Factory* default_factory); | 189 explicit FakeURLFetcherFactory(URLFetcher::Factory* default_factory); |
180 virtual ~FakeURLFetcherFactory(); | 190 virtual ~FakeURLFetcherFactory(); |
181 | 191 |
182 // If no fake response is set for the given URL this method will delegate the | 192 // If no fake response is set for the given URL this method will delegate the |
183 // call to |default_factory_| if it is not NULL, or return NULL if it is | 193 // call to |default_factory_| if it is not NULL, or return NULL if it is |
184 // NULL. | 194 // NULL. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 virtual ~URLFetcherFactory(); | 227 virtual ~URLFetcherFactory(); |
218 | 228 |
219 // This method will create a real URLFetcher. | 229 // This method will create a real URLFetcher. |
220 virtual URLFetcher* CreateURLFetcher(int id, | 230 virtual URLFetcher* CreateURLFetcher(int id, |
221 const GURL& url, | 231 const GURL& url, |
222 URLFetcher::RequestType request_type, | 232 URLFetcher::RequestType request_type, |
223 URLFetcher::Delegate* d); | 233 URLFetcher::Delegate* d); |
224 | 234 |
225 }; | 235 }; |
226 | 236 |
227 #endif // CONTENT_COMMON_TEST_URL_FETCHER_FACTORY_H_ | 237 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
OLD | NEW |