OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" |
10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "base/version.h" | 13 #include "base/version.h" |
13 #include "components/component_updater/crx_update_item.h" | 14 #include "components/component_updater/crx_update_item.h" |
14 #include "components/component_updater/test/test_configurator.h" | 15 #include "components/component_updater/test/test_configurator.h" |
15 #include "components/component_updater/test/url_request_post_interceptor.h" | 16 #include "components/component_updater/test/url_request_post_interceptor.h" |
16 #include "components/component_updater/update_checker.h" | 17 #include "components/component_updater/update_checker.h" |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "content/public/test/test_browser_thread_bundle.h" | |
19 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
22 | 21 |
23 using content::BrowserThread; | |
24 | |
25 namespace component_updater { | 22 namespace component_updater { |
26 | 23 |
27 namespace { | 24 namespace { |
28 | 25 |
29 base::FilePath test_file(const char* file) { | 26 base::FilePath test_file(const char* file) { |
30 base::FilePath path; | 27 base::FilePath path; |
31 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 28 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
32 return path.AppendASCII("components").AppendASCII("test").AppendASCII("data") | 29 return path.AppendASCII("components").AppendASCII("test").AppendASCII("data") |
33 .AppendASCII("component_updater").AppendASCII(file); | 30 .AppendASCII("component_updater").AppendASCII(file); |
34 } | 31 } |
(...skipping 25 matching lines...) Expand all Loading... |
60 scoped_ptr<UpdateChecker> update_checker_; | 57 scoped_ptr<UpdateChecker> update_checker_; |
61 | 58 |
62 scoped_ptr<InterceptorFactory> interceptor_factory_; | 59 scoped_ptr<InterceptorFactory> interceptor_factory_; |
63 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. | 60 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. |
64 | 61 |
65 int error_; | 62 int error_; |
66 std::string error_message_; | 63 std::string error_message_; |
67 UpdateResponse::Results results_; | 64 UpdateResponse::Results results_; |
68 | 65 |
69 private: | 66 private: |
70 content::TestBrowserThreadBundle thread_bundle_; | 67 base::MessageLoopForIO loop_; |
71 base::Closure quit_closure_; | 68 base::Closure quit_closure_; |
72 | 69 |
73 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); | 70 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); |
74 }; | 71 }; |
75 | 72 |
76 UpdateCheckerTest::UpdateCheckerTest() | 73 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) { |
77 : config_(new TestConfigurator), | |
78 error_(0), | |
79 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | |
80 net::URLFetcher::SetEnableInterceptionForTests(true); | 74 net::URLFetcher::SetEnableInterceptionForTests(true); |
81 } | 75 } |
82 | 76 |
83 UpdateCheckerTest::~UpdateCheckerTest() { | 77 UpdateCheckerTest::~UpdateCheckerTest() { |
84 net::URLFetcher::SetEnableInterceptionForTests(false); | 78 net::URLFetcher::SetEnableInterceptionForTests(false); |
85 } | 79 } |
86 | 80 |
87 void UpdateCheckerTest::SetUp() { | 81 void UpdateCheckerTest::SetUp() { |
88 interceptor_factory_.reset(new InterceptorFactory); | 82 config_.reset(new TestConfigurator(base::MessageLoopProxy::current(), |
| 83 base::MessageLoopProxy::current())); |
| 84 interceptor_factory_.reset( |
| 85 new InterceptorFactory(base::MessageLoopProxy::current())); |
89 post_interceptor_ = interceptor_factory_->CreateInterceptor(); | 86 post_interceptor_ = interceptor_factory_->CreateInterceptor(); |
90 EXPECT_TRUE(post_interceptor_); | 87 EXPECT_TRUE(post_interceptor_); |
91 | 88 |
92 update_checker_.reset(); | 89 update_checker_.reset(); |
93 | 90 |
94 error_ = 0; | 91 error_ = 0; |
95 error_message_.clear(); | 92 error_message_.clear(); |
96 results_ = UpdateResponse::Results(); | 93 results_ = UpdateResponse::Results(); |
97 } | 94 } |
98 | 95 |
99 void UpdateCheckerTest::TearDown() { | 96 void UpdateCheckerTest::TearDown() { |
100 update_checker_.reset(); | 97 update_checker_.reset(); |
101 | 98 |
102 post_interceptor_ = NULL; | 99 post_interceptor_ = NULL; |
103 interceptor_factory_.reset(); | 100 interceptor_factory_.reset(); |
104 | 101 |
105 config_.reset(); | 102 config_.reset(); |
| 103 |
| 104 // The PostInterceptor requires the message loop to run to destruct correctly. |
| 105 // TODO: This is fragile and should be fixed. |
| 106 RunThreadsUntilIdle(); |
106 } | 107 } |
107 | 108 |
108 void UpdateCheckerTest::RunThreads() { | 109 void UpdateCheckerTest::RunThreads() { |
109 base::RunLoop runloop; | 110 base::RunLoop runloop; |
110 quit_closure_ = runloop.QuitClosure(); | 111 quit_closure_ = runloop.QuitClosure(); |
111 runloop.Run(); | 112 runloop.Run(); |
112 | 113 |
113 // Since some tests need to drain currently enqueued tasks such as network | 114 // Since some tests need to drain currently enqueued tasks such as network |
114 // intercepts on the IO thread, run the threads until they are | 115 // intercepts on the IO thread, run the threads until they are |
115 // idle. The component updater service won't loop again until the loop count | 116 // idle. The component updater service won't loop again until the loop count |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 << post_interceptor_->GetRequestsAsString(); | 223 << post_interceptor_->GetRequestsAsString(); |
223 EXPECT_EQ(1, post_interceptor_->GetCount()) | 224 EXPECT_EQ(1, post_interceptor_->GetCount()) |
224 << post_interceptor_->GetRequestsAsString(); | 225 << post_interceptor_->GetRequestsAsString(); |
225 | 226 |
226 EXPECT_NE(0, error_); | 227 EXPECT_NE(0, error_); |
227 EXPECT_STREQ("network error", error_message_.c_str()); | 228 EXPECT_STREQ("network error", error_message_.c_str()); |
228 EXPECT_EQ(0ul, results_.list.size()); | 229 EXPECT_EQ(0ul, results_.list.size()); |
229 } | 230 } |
230 | 231 |
231 } // namespace component_updater | 232 } // namespace component_updater |
OLD | NEW |