Commit 097d3a4a authored by tommycli's avatar tommycli Committed by Commit bot

Componentize component_updater: Break content/ dependency for rest of component_updater tests.

BUG=371463

Review URL: https://codereview.chromium.org/514473002

Cr-Commit-Position: refs/heads/master@{#292202}
parent fb47659a
......@@ -44,10 +44,16 @@ MockServiceObserver::~MockServiceObserver() {
}
ComponentUpdaterTest::ComponentUpdaterTest()
: test_config_(NULL),
: post_interceptor_(NULL),
test_config_(NULL),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
// The component updater instance under test.
test_config_ = new TestConfigurator;
test_config_ = new TestConfigurator(
BrowserThread::GetBlockingPool()
->GetSequencedTaskRunnerWithShutdownBehavior(
BrowserThread::GetBlockingPool()->GetSequenceToken(),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
component_updater_.reset(ComponentUpdateServiceFactory(test_config_));
net::URLFetcher::SetEnableInterceptionForTests(true);
......@@ -59,7 +65,8 @@ ComponentUpdaterTest::~ComponentUpdaterTest() {
void ComponentUpdaterTest::SetUp() {
get_interceptor_.reset(new GetInterceptor);
interceptor_factory_.reset(new InterceptorFactory);
interceptor_factory_.reset(new InterceptorFactory(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
post_interceptor_ = interceptor_factory_->CreateInterceptor();
EXPECT_TRUE(post_interceptor_);
}
......
......@@ -8,7 +8,6 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/values.h"
......@@ -17,7 +16,6 @@
#include "components/component_updater/component_updater_service.h"
#include "components/component_updater/test/component_patcher_unittest.h"
#include "components/component_updater/test/test_installer.h"
#include "content/public/browser/browser_thread.h"
#include "courgette/courgette.h"
#include "courgette/third_party/bsdiff.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -68,8 +66,7 @@ ComponentPatcherOperationTest::ComponentPatcherOperationTest() {
EXPECT_TRUE(input_dir_.CreateUniqueTempDir());
EXPECT_TRUE(installed_dir_.CreateUniqueTempDir());
installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path()));
task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::FILE);
task_runner_ = base::MessageLoop::current()->task_runner();
}
ComponentPatcherOperationTest::~ComponentPatcherOperationTest() {
......
......@@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
#ifndef COMPONENTS_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
#define COMPONENTS_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "base/message_loop/message_loop.h"
#include "courgette/courgette.h"
#include "courgette/third_party/bsdiff.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -34,9 +34,9 @@ class ComponentPatcherOperationTest : public testing::Test {
scoped_refptr<base::SequencedTaskRunner> task_runner_;
private:
content::TestBrowserThreadBundle thread_bundle_;
base::MessageLoopForIO loop_;
};
} // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
#endif // COMPONENTS_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
......@@ -3,19 +3,16 @@
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/version.h"
#include "components/component_updater/component_updater_ping_manager.h"
#include "components/component_updater/crx_update_item.h"
#include "components/component_updater/test/test_configurator.h"
#include "components/component_updater/test/url_request_post_interceptor.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace component_updater {
class ComponentUpdaterPingManagerTest : public testing::Test {
......@@ -34,12 +31,12 @@ class ComponentUpdaterPingManagerTest : public testing::Test {
scoped_ptr<PingManager> ping_manager_;
private:
content::TestBrowserThreadBundle thread_bundle_;
base::MessageLoopForIO loop_;
};
ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest()
: config_(new TestConfigurator),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
: config_(new TestConfigurator(base::MessageLoopProxy::current(),
base::MessageLoopProxy::current())) {
}
void ComponentUpdaterPingManagerTest::SetUp() {
......@@ -57,7 +54,8 @@ void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() {
// Test is flaky: http://crbug.com/349547
TEST_F(ComponentUpdaterPingManagerTest, DISABLED_PingManagerTest) {
scoped_ptr<InterceptorFactory> interceptor_factory(new InterceptorFactory);
scoped_ptr<InterceptorFactory> interceptor_factory(
new InterceptorFactory(base::MessageLoopProxy::current()));
URLRequestPostInterceptor* interceptor =
interceptor_factory->CreateInterceptor();
EXPECT_TRUE(interceptor);
......
......@@ -8,20 +8,19 @@
#include "base/version.h"
#include "components/component_updater/component_patcher_operation.h"
#include "components/component_updater/test/test_configurator.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"
using content::BrowserThread;
namespace component_updater {
TestConfigurator::TestConfigurator()
: initial_time_(0),
TestConfigurator::TestConfigurator(
const scoped_refptr<base::SequencedTaskRunner>& worker_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
: worker_task_runner_(worker_task_runner),
initial_time_(0),
times_(1),
recheck_time_(0),
ondemand_time_(0),
context_(new net::TestURLRequestContextGetter(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))) {
context_(new net::TestURLRequestContextGetter(network_task_runner)) {
}
TestConfigurator::~TestConfigurator() {
......@@ -133,16 +132,14 @@ void TestConfigurator::SetInitialDelay(int seconds) {
scoped_refptr<base::SequencedTaskRunner>
TestConfigurator::GetSequencedTaskRunner() const {
return content::BrowserThread::GetBlockingPool()
->GetSequencedTaskRunnerWithShutdownBehavior(
content::BrowserThread::GetBlockingPool()->GetSequenceToken(),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
DCHECK(worker_task_runner_.get());
return worker_task_runner_;
}
scoped_refptr<base::SingleThreadTaskRunner>
TestConfigurator::GetSingleThreadTaskRunner() const {
return content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::FILE);
// This is NULL because tests do not use the background downloader.
return NULL;
}
} // namespace component_updater
......@@ -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 CHROME_BROWSER_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
#ifndef COMPONENTS_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
#define COMPONENTS_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
#include <string>
#include <utility>
......@@ -25,7 +25,9 @@ struct CrxComponent;
class TestConfigurator : public Configurator {
public:
TestConfigurator();
TestConfigurator(
const scoped_refptr<base::SequencedTaskRunner>& worker_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner);
virtual ~TestConfigurator();
// Overrrides for Configurator.
......@@ -61,6 +63,9 @@ class TestConfigurator : public Configurator {
void SetInitialDelay(int seconds);
private:
scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
int initial_time_;
int times_;
int recheck_time_;
......@@ -74,4 +79,4 @@ class TestConfigurator : public Configurator {
} // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
#endif // COMPONENTS_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
......@@ -7,6 +7,7 @@
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/version.h"
......@@ -14,14 +15,10 @@
#include "components/component_updater/test/test_configurator.h"
#include "components/component_updater/test/url_request_post_interceptor.h"
#include "components/component_updater/update_checker.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace component_updater {
namespace {
......@@ -67,16 +64,13 @@ class UpdateCheckerTest : public testing::Test {
UpdateResponse::Results results_;
private:
content::TestBrowserThreadBundle thread_bundle_;
base::MessageLoopForIO loop_;
base::Closure quit_closure_;
DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
};
UpdateCheckerTest::UpdateCheckerTest()
: config_(new TestConfigurator),
error_(0),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) {
net::URLFetcher::SetEnableInterceptionForTests(true);
}
......@@ -85,7 +79,10 @@ UpdateCheckerTest::~UpdateCheckerTest() {
}
void UpdateCheckerTest::SetUp() {
interceptor_factory_.reset(new InterceptorFactory);
config_.reset(new TestConfigurator(base::MessageLoopProxy::current(),
base::MessageLoopProxy::current()));
interceptor_factory_.reset(
new InterceptorFactory(base::MessageLoopProxy::current()));
post_interceptor_ = interceptor_factory_->CreateInterceptor();
EXPECT_TRUE(post_interceptor_);
......@@ -103,6 +100,10 @@ void UpdateCheckerTest::TearDown() {
interceptor_factory_.reset();
config_.reset();
// The PostInterceptor requires the message loop to run to destruct correctly.
// TODO: This is fragile and should be fixed.
RunThreadsUntilIdle();
}
void UpdateCheckerTest::RunThreads() {
......
......@@ -8,7 +8,6 @@
#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "components/component_updater/test/test_configurator.h"
#include "content/public/test/test_browser_thread.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_filter.h"
......@@ -16,8 +15,6 @@
#include "net/url_request/url_request_simple_job.h"
#include "net/url_request/url_request_test_util.h"
using content::BrowserThread;
namespace component_updater {
// Returns a canned response.
......@@ -49,12 +46,14 @@ class URLRequestMockJob : public net::URLRequestSimpleJob {
DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob);
};
URLRequestPostInterceptor::URLRequestPostInterceptor(const GURL& url)
: url_(url), hit_count_(0) {
URLRequestPostInterceptor::URLRequestPostInterceptor(
const GURL& url,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: url_(url), io_task_runner_(io_task_runner), hit_count_(0) {
}
URLRequestPostInterceptor::~URLRequestPostInterceptor() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
ClearExpectations();
}
......@@ -125,17 +124,19 @@ void URLRequestPostInterceptor::Reset() {
class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
public:
Delegate(const std::string& scheme, const std::string& hostname)
: scheme_(scheme), hostname_(hostname) {}
Delegate(const std::string& scheme,
const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {}
void Register() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this));
}
void Unregister() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
for (InterceptorMap::iterator it = interceptors_.begin();
it != interceptors_.end();
++it)
......@@ -145,7 +146,7 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
}
void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end());
interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor));
......@@ -157,7 +158,7 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
virtual net::URLRequestJob* MaybeInterceptRequest(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
// Only intercepts POST.
if (!request->has_upload())
......@@ -212,26 +213,29 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
const std::string scheme_;
const std::string hostname_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
DISALLOW_COPY_AND_ASSIGN(Delegate);
};
URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory(
const std::string& scheme,
const std::string& hostname)
const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: scheme_(scheme),
hostname_(hostname),
delegate_(new URLRequestPostInterceptor::Delegate(scheme, hostname)) {
BrowserThread::PostTask(
BrowserThread::IO,
io_task_runner_(io_task_runner),
delegate_(new URLRequestPostInterceptor::Delegate(scheme,
hostname,
io_task_runner)) {
io_task_runner_->PostTask(
FROM_HERE,
base::Bind(&URLRequestPostInterceptor::Delegate::Register,
base::Unretained(delegate_)));
}
URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() {
BrowserThread::PostTask(
BrowserThread::IO,
io_task_runner_->PostTask(
FROM_HERE,
base::Bind(&URLRequestPostInterceptor::Delegate::Unregister,
base::Unretained(delegate_)));
......@@ -243,9 +247,8 @@ URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor(
base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str()));
GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII()));
URLRequestPostInterceptor* interceptor(
new URLRequestPostInterceptor(absolute_url));
bool res = BrowserThread::PostTask(
BrowserThread::IO,
new URLRequestPostInterceptor(absolute_url, io_task_runner_));
bool res = io_task_runner_->PostTask(
FROM_HERE,
base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor,
base::Unretained(delegate_),
......@@ -262,9 +265,11 @@ bool PartialMatch::Match(const std::string& actual) const {
return actual.find(expected_) != std::string::npos;
}
InterceptorFactory::InterceptorFactory()
InterceptorFactory::InterceptorFactory(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME,
POST_INTERCEPT_HOSTNAME) {
POST_INTERCEPT_HOSTNAME,
io_task_runner) {
}
InterceptorFactory::~InterceptorFactory() {
......
......@@ -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 CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
#ifndef COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
#define COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
#include <map>
#include <queue>
......@@ -12,11 +12,13 @@
#include <vector>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "url/gurl.h"
namespace base {
class FilePath;
class SequencedTaskRunner;
}
namespace net {
......@@ -92,11 +94,14 @@ class URLRequestPostInterceptor {
friend class URLRequestPostInterceptorFactory;
typedef std::pair<const RequestMatcher*, std::string> Expectation;
explicit URLRequestPostInterceptor(const GURL& url);
URLRequestPostInterceptor(
const GURL& url,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
~URLRequestPostInterceptor();
void ClearExpectations();
const GURL url_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
mutable base::Lock interceptor_lock_;
mutable int hit_count_;
......@@ -108,8 +113,10 @@ class URLRequestPostInterceptor {
class URLRequestPostInterceptorFactory {
public:
URLRequestPostInterceptorFactory(const std::string& scheme,
const std::string& hostname);
URLRequestPostInterceptorFactory(
const std::string& scheme,
const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
~URLRequestPostInterceptorFactory();
// Creates an interceptor object for the specified url path. Returns NULL
......@@ -120,6 +127,7 @@ class URLRequestPostInterceptorFactory {
private:
const std::string scheme_;
const std::string hostname_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
// After creation, |delegate_| lives on the IO thread and it is owned by
// a URLRequestFilter after registration. A task to unregister it and
......@@ -132,7 +140,8 @@ class URLRequestPostInterceptorFactory {
// Intercepts HTTP POST requests sent to "localhost2".
class InterceptorFactory : public URLRequestPostInterceptorFactory {
public:
InterceptorFactory();
explicit InterceptorFactory(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
~InterceptorFactory();
URLRequestPostInterceptor* CreateInterceptor();
......@@ -154,4 +163,4 @@ class PartialMatch : public URLRequestPostInterceptor::RequestMatcher {
} // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
#endif // COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment