Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / update_client / update_checker_unittest.cc
blob0bea798595da79d75701ae8b2ffc42cc0574e20f
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/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/compiler_specific.h"
8 #include "base/files/file_util.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/run_loop.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/version.h"
16 #include "components/update_client/crx_update_item.h"
17 #include "components/update_client/test_configurator.h"
18 #include "components/update_client/update_checker.h"
19 #include "components/update_client/url_request_post_interceptor.h"
20 #include "net/url_request/url_request_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h"
24 using std::string;
26 namespace update_client {
28 namespace {
30 base::FilePath test_file(const char* file) {
31 base::FilePath path;
32 PathService::Get(base::DIR_SOURCE_ROOT, &path);
33 return path.AppendASCII("components")
34 .AppendASCII("test")
35 .AppendASCII("data")
36 .AppendASCII("update_client")
37 .AppendASCII(file);
40 } // namespace
42 class UpdateCheckerTest : public testing::Test {
43 public:
44 UpdateCheckerTest();
45 ~UpdateCheckerTest() override;
47 // Overrides from testing::Test.
48 void SetUp() override;
49 void TearDown() override;
51 void UpdateCheckComplete(const GURL& original_url,
52 int error,
53 const std::string& error_message,
54 const UpdateResponse::Results& results);
56 protected:
57 void Quit();
58 void RunThreads();
59 void RunThreadsUntilIdle();
61 CrxUpdateItem BuildCrxUpdateItem();
63 scoped_refptr<TestConfigurator> config_;
65 scoped_ptr<UpdateChecker> update_checker_;
67 scoped_ptr<InterceptorFactory> interceptor_factory_;
68 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory.
70 GURL original_url_;
71 int error_;
72 std::string error_message_;
73 UpdateResponse::Results results_;
75 private:
76 base::MessageLoopForIO loop_;
77 base::Closure quit_closure_;
79 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
82 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) {
85 UpdateCheckerTest::~UpdateCheckerTest() {
88 void UpdateCheckerTest::SetUp() {
89 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(),
90 base::ThreadTaskRunnerHandle::Get());
91 interceptor_factory_.reset(
92 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get()));
93 post_interceptor_ = interceptor_factory_->CreateInterceptor();
94 EXPECT_TRUE(post_interceptor_);
96 update_checker_.reset();
98 error_ = 0;
99 error_message_.clear();
100 results_ = UpdateResponse::Results();
103 void UpdateCheckerTest::TearDown() {
104 update_checker_.reset();
106 post_interceptor_ = NULL;
107 interceptor_factory_.reset();
109 config_ = nullptr;
111 // The PostInterceptor requires the message loop to run to destruct correctly.
112 // TODO(sorin): This is fragile and should be fixed.
113 RunThreadsUntilIdle();
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 const GURL& original_url,
139 int error,
140 const std::string& error_message,
141 const UpdateResponse::Results& results) {
142 original_url_ = original_url;
143 error_ = error;
144 error_message_ = error_message;
145 results_ = results;
146 Quit();
149 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() {
150 CrxComponent crx_component;
151 crx_component.name = "test_jebg";
152 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
153 crx_component.installer = NULL;
154 crx_component.version = base::Version("0.9");
155 crx_component.fingerprint = "fp1";
157 CrxUpdateItem crx_update_item;
158 crx_update_item.state = CrxUpdateItem::State::kNew;
159 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf";
160 crx_update_item.component = crx_component;
162 return crx_update_item;
165 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) {
166 EXPECT_TRUE(post_interceptor_->ExpectRequest(
167 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
169 update_checker_ = UpdateChecker::Create(*config_).Pass();
171 CrxUpdateItem item(BuildCrxUpdateItem());
172 std::vector<CrxUpdateItem*> items_to_check;
173 items_to_check.push_back(&item);
175 update_checker_->CheckForUpdates(
176 items_to_check, "extra=\"params\"",
177 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
178 base::Unretained(this)));
180 RunThreads();
182 EXPECT_EQ(1, post_interceptor_->GetHitCount())
183 << post_interceptor_->GetRequestsAsString();
184 ASSERT_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(
191 string::npos,
192 post_interceptor_->GetRequests()[0].find(
193 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
194 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>"));
196 EXPECT_NE(string::npos,
197 post_interceptor_->GetRequests()[0].find("<hw physmemory="));
199 // Sanity check the arguments of the callback after parsing.
200 ASSERT_FALSE(config_->UpdateUrl().empty());
201 EXPECT_EQ(config_->UpdateUrl().front(), original_url_);
202 EXPECT_EQ(0, error_);
203 EXPECT_TRUE(error_message_.empty());
204 EXPECT_EQ(1ul, results_.list.size());
205 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf",
206 results_.list[0].extension_id.c_str());
207 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str());
210 // Simulates a 403 server response error.
211 TEST_F(UpdateCheckerTest, UpdateCheckError) {
212 EXPECT_TRUE(
213 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403));
215 update_checker_ = UpdateChecker::Create(*config_).Pass();
217 CrxUpdateItem item(BuildCrxUpdateItem());
218 std::vector<CrxUpdateItem*> items_to_check;
219 items_to_check.push_back(&item);
221 update_checker_->CheckForUpdates(
222 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
223 base::Unretained(this)));
225 RunThreads();
227 EXPECT_EQ(1, post_interceptor_->GetHitCount())
228 << post_interceptor_->GetRequestsAsString();
229 EXPECT_EQ(1, post_interceptor_->GetCount())
230 << post_interceptor_->GetRequestsAsString();
232 ASSERT_FALSE(config_->UpdateUrl().empty());
233 EXPECT_EQ(config_->UpdateUrl().front(), original_url_);
234 EXPECT_EQ(403, error_);
235 EXPECT_STREQ("network error", error_message_.c_str());
236 EXPECT_EQ(0ul, results_.list.size());
239 } // namespace update_client