| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ | 5 #ifndef CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ |
| 6 #define CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ | 6 #define CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "chrome/common/net/url_fetcher.h" | 12 #include "chrome/common/net/url_fetcher.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 | 14 |
| 15 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of | 15 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of |
| 16 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates | 16 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates |
| 17 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is | 17 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is |
| 18 // expected that you'll grab the delegate from the TestURLFetcher and invoke | 18 // expected that you'll grab the delegate from the TestURLFetcher and invoke |
| 19 // the callback method when appropriate. In this way it's easy to mock a | 19 // the callback method when appropriate. In this way it's easy to mock a |
| 20 // URLFetcher. | 20 // URLFetcher. |
| 21 // Typical usage: | 21 // Typical usage: |
| 22 // // TestURLFetcher requires a MessageLoop: | 22 // // TestURLFetcher requires a MessageLoop: |
| 23 // MessageLoopForUI message_loop; | 23 // MessageLoopForUI message_loop; |
| 24 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. |
| 25 // ChromeThread io_thread(ChromeThread::IO, &message_loop); |
| 24 // // Create and register factory. | 26 // // Create and register factory. |
| 25 // TestURLFetcherFactory factory; | 27 // TestURLFetcherFactory factory; |
| 26 // URLFetcher::set_factory(&factory); | 28 // URLFetcher::set_factory(&factory); |
| 27 // // Do something that triggers creation of a URLFetcher. | 29 // // Do something that triggers creation of a URLFetcher. |
| 28 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); | 30 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); |
| 29 // DCHECK(fetcher); | 31 // DCHECK(fetcher); |
| 30 // // Notify delegate with whatever data you want. | 32 // // Notify delegate with whatever data you want. |
| 31 // fetcher->delegate()->OnURLFetchComplete(...); | 33 // fetcher->delegate()->OnURLFetchComplete(...); |
| 32 // // Make sure consumer of URLFetcher does the right thing. | 34 // // Make sure consumer of URLFetcher does the right thing. |
| 33 // ... | 35 // ... |
| (...skipping 12 matching lines...) Expand all Loading... |
| 46 virtual void Start() {} | 48 virtual void Start() {} |
| 47 | 49 |
| 48 // URL we were created with. Because of how we're using URLFetcher url() | 50 // URL we were created with. Because of how we're using URLFetcher url() |
| 49 // always returns an empty URL. Chances are you'll want to use original_url() | 51 // always returns an empty URL. Chances are you'll want to use original_url() |
| 50 // in your tests. | 52 // in your tests. |
| 51 const GURL& original_url() const { return original_url_; } | 53 const GURL& original_url() const { return original_url_; } |
| 52 | 54 |
| 53 // Returns the data uploaded on this URLFetcher. | 55 // Returns the data uploaded on this URLFetcher. |
| 54 const std::string& upload_data() const { return URLFetcher::upload_data(); } | 56 const std::string& upload_data() const { return URLFetcher::upload_data(); } |
| 55 | 57 |
| 56 // Overriden to do nothing. URLFetcher implementation add reference | |
| 57 // to request_context_getter in core_, but it might not be released | |
| 58 // because we wouldn't call Core::CancelURLRequest. | |
| 59 // Without this, we'll see leaks of URLRequestContext in test. | |
| 60 virtual void set_request_context( | |
| 61 URLRequestContextGetter* request_context_getter) {} | |
| 62 | |
| 63 private: | 58 private: |
| 64 const GURL original_url_; | 59 const GURL original_url_; |
| 65 | 60 |
| 66 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | 61 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); |
| 67 }; | 62 }; |
| 68 | 63 |
| 69 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers | 64 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers |
| 70 // are registered in a map by the id passed to the create method. | 65 // are registered in a map by the id passed to the create method. |
| 71 class TestURLFetcherFactory : public URLFetcher::Factory { | 66 class TestURLFetcherFactory : public URLFetcher::Factory { |
| 72 public: | 67 public: |
| 73 TestURLFetcherFactory() {} | 68 TestURLFetcherFactory() {} |
| 74 | 69 |
| 75 virtual URLFetcher* CreateURLFetcher(int id, | 70 virtual URLFetcher* CreateURLFetcher(int id, |
| 76 const GURL& url, | 71 const GURL& url, |
| 77 URLFetcher::RequestType request_type, | 72 URLFetcher::RequestType request_type, |
| 78 URLFetcher::Delegate* d); | 73 URLFetcher::Delegate* d); |
| 79 | 74 |
| 80 TestURLFetcher* GetFetcherByID(int id) const; | 75 TestURLFetcher* GetFetcherByID(int id) const; |
| 81 | 76 |
| 82 private: | 77 private: |
| 83 // Maps from id passed to create to the returned URLFetcher. | 78 // Maps from id passed to create to the returned URLFetcher. |
| 84 typedef std::map<int, TestURLFetcher*> Fetchers; | 79 typedef std::map<int, TestURLFetcher*> Fetchers; |
| 85 Fetchers fetchers_; | 80 Fetchers fetchers_; |
| 86 | 81 |
| 87 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); | 82 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); |
| 88 }; | 83 }; |
| 89 | 84 |
| 90 #endif // CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ | 85 #endif // CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ |
| OLD | NEW |