Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / component_updater / test / update_checker_unittest.cc
blobc031488e900b9f9be4728ee1fac55bf56db6aa5a
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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/file_util.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/version.h"
13 #include "chrome/browser/component_updater/crx_update_item.h"
14 #include "chrome/browser/component_updater/test/component_updater_service_unittest.h"
15 #include "chrome/browser/component_updater/test/url_request_post_interceptor.h"
16 #include "chrome/browser/component_updater/update_checker.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using content::BrowserThread;
26 namespace component_updater {
28 namespace {
30 base::FilePath test_file(const char* file) {
31 base::FilePath path;
32 PathService::Get(chrome::DIR_TEST_DATA, &path);
33 return path.AppendASCII("components").AppendASCII(file);
36 } // namespace
38 class UpdateCheckerTest : public testing::Test {
39 public:
40 UpdateCheckerTest();
41 virtual ~UpdateCheckerTest();
43 // Overrides from testing::Test.
44 virtual void SetUp() OVERRIDE;
45 virtual void TearDown() OVERRIDE;
47 void UpdateCheckComplete(int error,
48 const std::string& error_message,
49 const UpdateResponse::Results& results);
51 net::URLRequestContextGetter* context() {
52 return context_.get();
55 protected:
56 void Quit();
57 void RunThreads();
58 void RunThreadsUntilIdle();
60 CrxUpdateItem BuildCrxUpdateItem();
62 scoped_ptr<UpdateChecker> update_checker_;
64 scoped_ptr<InterceptorFactory> interceptor_factory_;
65 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory.
67 int error_;
68 std::string error_message_;
69 UpdateResponse::Results results_;
71 private:
72 scoped_refptr<net::TestURLRequestContextGetter> context_;
73 content::TestBrowserThreadBundle thread_bundle_;
74 base::FilePath test_data_dir_;
75 base::Closure quit_closure_;
77 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
80 UpdateCheckerTest::UpdateCheckerTest()
81 : error_(0),
82 context_(new net::TestURLRequestContextGetter(
83 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))),
84 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
85 // The test directory is chrome/test/data/components.
86 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
87 test_data_dir_ = test_data_dir_.AppendASCII("components");
89 net::URLFetcher::SetEnableInterceptionForTests(true);
92 UpdateCheckerTest::~UpdateCheckerTest() {
93 net::URLFetcher::SetEnableInterceptionForTests(false);
94 context_ = NULL;
97 void UpdateCheckerTest::SetUp() {
98 interceptor_factory_.reset(new InterceptorFactory);
99 post_interceptor_ = interceptor_factory_->CreateInterceptor();
100 EXPECT_TRUE(post_interceptor_);
102 update_checker_.reset();
104 error_ = 0;
105 error_message_.clear();
106 results_ = UpdateResponse::Results();
109 void UpdateCheckerTest::TearDown() {
110 update_checker_.reset();
112 post_interceptor_ = NULL;
113 interceptor_factory_.reset();
116 void UpdateCheckerTest::RunThreads() {
117 base::RunLoop runloop;
118 quit_closure_ = runloop.QuitClosure();
119 runloop.Run();
121 // Since some tests need to drain currently enqueued tasks such as network
122 // intercepts on the IO thread, run the threads until they are
123 // idle. The component updater service won't loop again until the loop count
124 // is set and the service is started.
125 RunThreadsUntilIdle();
128 void UpdateCheckerTest::RunThreadsUntilIdle() {
129 base::RunLoop().RunUntilIdle();
132 void UpdateCheckerTest::Quit() {
133 if (!quit_closure_.is_null())
134 quit_closure_.Run();
137 void UpdateCheckerTest::UpdateCheckComplete(
138 int error,
139 const std::string& error_message,
140 const UpdateResponse::Results& results) {
141 error_ = error;
142 error_message_ = error_message;
143 results_ = results;
144 Quit();
147 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() {
148 CrxComponent crx_component;
149 crx_component.name = "test_jebg";
150 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
151 crx_component.installer = NULL;
152 crx_component.observer = NULL;
153 crx_component.version = base::Version("0.9");
154 crx_component.fingerprint = "fp1";
156 CrxUpdateItem crx_update_item;
157 crx_update_item.status = CrxUpdateItem::kNew;
158 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf";
159 crx_update_item.component = crx_component;
161 return crx_update_item;
164 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) {
165 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch(
166 "updatecheck"), test_file("updatecheck_reply_1.xml")));
168 update_checker_ = UpdateChecker::Create(
169 GURL("http://localhost2/update2"),
170 context(),
171 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
172 base::Unretained(this))).Pass();
174 CrxUpdateItem item(BuildCrxUpdateItem());
175 std::vector<CrxUpdateItem*> items_to_check;
176 items_to_check.push_back(&item);
178 update_checker_->CheckForUpdates(items_to_check, "extra=\"params\"");
180 RunThreads();
182 EXPECT_EQ(1, post_interceptor_->GetHitCount())
183 << post_interceptor_->GetRequestsAsString();
184 EXPECT_EQ(1, post_interceptor_->GetCount())
185 << post_interceptor_->GetRequestsAsString();
187 // Sanity check the request.
188 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
189 "request protocol=\"3.0\" extra=\"params\""));
190 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
191 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
192 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>"));
194 // Sanity check the arguments of the callback after parsing.
195 EXPECT_EQ(0, error_);
196 EXPECT_TRUE(error_message_.empty());
197 EXPECT_EQ(1ul, results_.list.size());
198 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf",
199 results_.list[0].extension_id.c_str());
200 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str());
203 TEST_F(UpdateCheckerTest, UpdateNetworkError) {
204 // Setting this expectation simulates a network error since the
205 // file is not found. Since setting the expectation fails, this function
206 // owns |request_matcher|.
207 scoped_ptr<PartialMatch> request_matcher( new PartialMatch("updatecheck"));
208 EXPECT_FALSE(post_interceptor_->ExpectRequest(request_matcher.get(),
209 test_file("no such file")));
211 update_checker_ = UpdateChecker::Create(
212 GURL("http://localhost2/update2"),
213 context(),
214 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
215 base::Unretained(this))).Pass();
217 CrxUpdateItem item(BuildCrxUpdateItem());
218 std::vector<CrxUpdateItem*> items_to_check;
219 items_to_check.push_back(&item);
221 update_checker_->CheckForUpdates(items_to_check, "");
223 RunThreads();
225 EXPECT_EQ(0, post_interceptor_->GetHitCount())
226 << post_interceptor_->GetRequestsAsString();
227 EXPECT_EQ(1, post_interceptor_->GetCount())
228 << post_interceptor_->GetRequestsAsString();
230 EXPECT_NE(0, error_);
231 EXPECT_STREQ("network error", error_message_.c_str());
232 EXPECT_EQ(0ul, results_.list.size());
235 } // namespace component_updater