diff options
Diffstat (limited to 'content')
-rw-r--r-- | content/DEPS | 1 | ||||
-rw-r--r-- | content/browser/geolocation/network_location_provider_unittest.cc | 4 | ||||
-rw-r--r-- | content/browser/speech/speech_recognition_request_unittest.cc | 11 | ||||
-rw-r--r-- | content/browser/speech/speech_recognizer_unittest.cc | 4 | ||||
-rw-r--r-- | content/common/url_fetcher.h | 32 | ||||
-rw-r--r-- | content/content_tests.gypi | 15 | ||||
-rw-r--r-- | content/test/test_url_fetcher_factory.cc (renamed from content/common/test_url_fetcher_factory.cc) | 25 | ||||
-rw-r--r-- | content/test/test_url_fetcher_factory.h (renamed from content/common/test_url_fetcher_factory.h) | 32 |
8 files changed, 78 insertions, 46 deletions
diff --git a/content/DEPS b/content/DEPS index 71029d0..954e378 100644 --- a/content/DEPS +++ b/content/DEPS @@ -18,6 +18,7 @@ include_rules = [ # directories in content/ so we disallow all of them. "-content", "+content/common", + "+content/test", "+grit", "+gpu", diff --git a/content/browser/geolocation/network_location_provider_unittest.cc b/content/browser/geolocation/network_location_provider_unittest.cc index 1192531..6f1e6d8 100644 --- a/content/browser/geolocation/network_location_provider_unittest.cc +++ b/content/browser/geolocation/network_location_provider_unittest.cc @@ -9,7 +9,7 @@ #include "base/values.h" #include "content/browser/geolocation/fake_access_token_store.h" #include "content/browser/geolocation/network_location_provider.h" -#include "content/common/test_url_fetcher_factory.h" +#include "content/test/test_url_fetcher_factory.h" #include "net/url_request/url_request_status.h" #include "testing/gtest/include/gtest/gtest.h" @@ -115,7 +115,6 @@ MockDeviceDataProviderImpl<DataType>::instance_ = NULL; class GeolocationNetworkProviderTest : public testing::Test { public: virtual void SetUp() { - URLFetcher::set_factory(&url_fetcher_factory_); access_token_store_ = new FakeAccessTokenStore; gateway_data_provider_ = MockDeviceDataProviderImpl<GatewayData>::CreateInstance(); @@ -129,7 +128,6 @@ class GeolocationNetworkProviderTest : public testing::Test { WifiDataProvider::ResetFactory(); RadioDataProvider::ResetFactory(); GatewayDataProvider::ResetFactory(); - URLFetcher::set_factory(NULL); } LocationProviderBase* CreateProvider(bool set_permission_granted) { diff --git a/content/browser/speech/speech_recognition_request_unittest.cc b/content/browser/speech/speech_recognition_request_unittest.cc index bb5e715..f41649c 100644 --- a/content/browser/speech/speech_recognition_request_unittest.cc +++ b/content/browser/speech/speech_recognition_request_unittest.cc @@ -4,7 +4,7 @@ #include "base/utf_string_conversions.h" #include "content/browser/speech/speech_recognition_request.h" -#include "content/common/test_url_fetcher_factory.h" +#include "content/test/test_url_fetcher_factory.h" #include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_status.h" #include "testing/gtest/include/gtest/gtest.h" @@ -27,15 +27,6 @@ class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate, result_ = result; } - // testing::Test methods. - virtual void SetUp() { - URLFetcher::set_factory(&url_fetcher_factory_); - } - - virtual void TearDown() { - URLFetcher::set_factory(NULL); - } - protected: MessageLoop message_loop_; TestURLFetcherFactory url_fetcher_factory_; diff --git a/content/browser/speech/speech_recognizer_unittest.cc b/content/browser/speech/speech_recognizer_unittest.cc index aefde20..d70df62 100644 --- a/content/browser/speech/speech_recognizer_unittest.cc +++ b/content/browser/speech/speech_recognizer_unittest.cc @@ -6,7 +6,7 @@ #include "content/browser/browser_thread.h" #include "content/browser/speech/speech_recognizer.h" -#include "content/common/test_url_fetcher_factory.h" +#include "content/test/test_url_fetcher_factory.h" #include "media/audio/test_audio_input_controller_factory.h" #include "net/base/net_errors.h" #include "net/url_request/url_request_status.h" @@ -75,12 +75,10 @@ class SpeechRecognizerTest : public SpeechRecognizerDelegate, // testing::Test methods. virtual void SetUp() { - URLFetcher::set_factory(&url_fetcher_factory_); AudioInputController::set_factory(&audio_input_controller_factory_); } virtual void TearDown() { - URLFetcher::set_factory(NULL); AudioInputController::set_factory(NULL); } diff --git a/content/common/url_fetcher.h b/content/common/url_fetcher.h index 1a54a69..a888868 100644 --- a/content/common/url_fetcher.h +++ b/content/common/url_fetcher.h @@ -11,8 +11,8 @@ // temporary situation. We will work on allowing support for multiple "io" // threads per process. -#ifndef CONTENT_COMMON_NET_URL_FETCHER_H_ -#define CONTENT_COMMON_NET_URL_FETCHER_H_ +#ifndef CONTENT_COMMON_URL_FETCHER_H_ +#define CONTENT_COMMON_URL_FETCHER_H_ #pragma once #include <string> @@ -117,20 +117,10 @@ class URLFetcher { // |request_type| is the type of request to make. // |d| the object that will receive the callback on fetch completion. URLFetcher(const GURL& url, RequestType request_type, Delegate* d); - virtual ~URLFetcher(); - // Sets the factory used by the static method Create to create a URLFetcher. - // URLFetcher does not take ownership of |factory|. A value of NULL results - // in a URLFetcher being created directly. -#if defined(UNIT_TEST) - static void set_factory(Factory* factory) { - factory_ = factory; - } -#endif - // Normally interception is disabled for URLFetcher, but you can use this - // to enable it for tests. Also see the set_factory method for another way + // to enable it for tests. Also see ScopedURLFetcherFactory for another way // of testing code that uses an URLFetcher. static void enable_interception_for_tests(bool enabled) { g_interception_enabled = enabled; @@ -295,13 +285,25 @@ class URLFetcher { virtual ResponseDestinationType GetResponseDestinationForTesting() const; private: - friend class URLFetcherTest; + friend class ScopedURLFetcherFactory; friend class TestURLFetcher; + friend class URLFetcherTest; // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects // actively running. static int GetNumFetcherCores(); + static Factory* factory() { return factory_; } + + // Sets the factory used by the static method Create to create a URLFetcher. + // URLFetcher does not take ownership of |factory|. A value of NULL results + // in a URLFetcher being created directly. + // + // NOTE: for safety, this should only be used through ScopedURLFetcherFactory! + static void set_factory(Factory* factory) { + factory_ = factory; + } + class Core; scoped_refptr<Core> core_; @@ -315,4 +317,4 @@ class URLFetcher { DISALLOW_COPY_AND_ASSIGN(URLFetcher); }; -#endif // CONTENT_COMMON_NET_URL_FETCHER_H_ +#endif // CONTENT_COMMON_URL_FETCHER_H_ diff --git a/content/content_tests.gypi b/content/content_tests.gypi index 5920993..15a48cd5 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -5,9 +5,24 @@ { 'targets': [ { + 'target_name': 'test_support_content', + 'type': 'static_library', + 'dependencies': [ + 'content_common', + ], + 'include_dirs': [ + '..', + ], + 'sources': [ + 'test/test_url_fetcher_factory.cc', + 'test/test_url_fetcher_factory.h', + ], + }, + { 'target_name': 'content_unittests', 'type': 'executable', 'dependencies': [ + 'test_support_content', '../base/base.gyp:test_support_base', ], 'include_dirs': [ diff --git a/content/common/test_url_fetcher_factory.cc b/content/test/test_url_fetcher_factory.cc index c92ae0f..ad6e512 100644 --- a/content/common/test_url_fetcher_factory.cc +++ b/content/test/test_url_fetcher_factory.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "content/common/test_url_fetcher_factory.h" +#include "content/test/test_url_fetcher_factory.h" #include <string> @@ -10,6 +10,16 @@ #include "base/message_loop.h" #include "net/url_request/url_request_status.h" +ScopedURLFetcherFactory::ScopedURLFetcherFactory(URLFetcher::Factory* factory) { + DCHECK(!URLFetcher::factory()); + URLFetcher::set_factory(factory); +} + +ScopedURLFetcherFactory::~ScopedURLFetcherFactory() { + DCHECK(URLFetcher::factory()); + URLFetcher::set_factory(NULL); +} + TestURLFetcher::TestURLFetcher(int id, const GURL& url, URLFetcher::RequestType request_type, @@ -62,7 +72,9 @@ bool TestURLFetcher::GetResponseAsFilePath( return true; } -TestURLFetcherFactory::TestURLFetcherFactory() {} +TestURLFetcherFactory::TestURLFetcherFactory() + : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { +} TestURLFetcherFactory::~TestURLFetcherFactory() {} @@ -146,10 +158,15 @@ class FakeURLFetcher : public URLFetcher { DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); }; -FakeURLFetcherFactory::FakeURLFetcherFactory() {} +FakeURLFetcherFactory::FakeURLFetcherFactory() + : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { +} FakeURLFetcherFactory::FakeURLFetcherFactory( - URLFetcher::Factory* default_factory) : default_factory_(default_factory) {} + URLFetcher::Factory* default_factory) + : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), + default_factory_(default_factory) { +} FakeURLFetcherFactory::~FakeURLFetcherFactory() {} diff --git a/content/common/test_url_fetcher_factory.h b/content/test/test_url_fetcher_factory.h index 70cf6f8..f402875 100644 --- a/content/common/test_url_fetcher_factory.h +++ b/content/test/test_url_fetcher_factory.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CONTENT_COMMON_TEST_URL_FETCHER_FACTORY_H_ -#define CONTENT_COMMON_TEST_URL_FETCHER_FACTORY_H_ +#ifndef CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ +#define CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ #pragma once #include <list> @@ -11,9 +11,21 @@ #include <string> #include <utility> +#include "base/threading/non_thread_safe.h" #include "content/common/url_fetcher.h" -#include "net/url_request/url_request_status.h" #include "googleurl/src/gurl.h" +#include "net/url_request/url_request_status.h" + +// Changes URLFetcher's Factory for the lifetime of the object. +// Note that this scoper cannot be nested (to make it even harder to misuse). +class ScopedURLFetcherFactory : public base::NonThreadSafe { + public: + explicit ScopedURLFetcherFactory(URLFetcher::Factory* factory); + virtual ~ScopedURLFetcherFactory(); + + private: + DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); +}; // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates @@ -26,9 +38,8 @@ // MessageLoopForUI message_loop; // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. // BrowserThread io_thread(BrowserThread::IO, &message_loop); -// // Create and register factory. +// // Create factory (it automatically sets itself as URLFetcher's factory). // TestURLFetcherFactory factory; -// URLFetcher::set_factory(&factory); // // Do something that triggers creation of a URLFetcher. // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); // DCHECK(fetcher); @@ -36,8 +47,6 @@ // fetcher->delegate()->OnURLFetchComplete(...); // // Make sure consumer of URLFetcher does the right thing. // ... -// // Reset factory. -// URLFetcher::set_factory(NULL); // // Note: if you don't know when your request objects will be created you // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes @@ -118,7 +127,8 @@ class TestURLFetcher : public URLFetcher { // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers // are registered in a map by the id passed to the create method. -class TestURLFetcherFactory : public URLFetcher::Factory { +class TestURLFetcherFactory : public URLFetcher::Factory, + public ScopedURLFetcherFactory { public: TestURLFetcherFactory(); virtual ~TestURLFetcherFactory(); @@ -157,7 +167,6 @@ class TestURLFetcherFactory : public URLFetcher::Factory { // // Example usage: // FakeURLFetcherFactory factory; -// URLFetcher::set_factory(&factory); // // // You know that class SomeService will request url http://a.com/ and you // // want to test the service class by returning an error. @@ -171,7 +180,8 @@ class TestURLFetcherFactory : public URLFetcher::Factory { // SomeService service; // service.Run(); // Will eventually request these two URLs. -class FakeURLFetcherFactory : public URLFetcher::Factory { +class FakeURLFetcherFactory : public URLFetcher::Factory, + public ScopedURLFetcherFactory { public: FakeURLFetcherFactory(); // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown @@ -224,4 +234,4 @@ class URLFetcherFactory : public URLFetcher::Factory { }; -#endif // CONTENT_COMMON_TEST_URL_FETCHER_FACTORY_H_ +#endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |