Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: chrome/browser/component_updater/test/component_updater_service_unittest.cc

Issue 55303002: Unittest for Component updater throttles (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/component_updater/test/component_updater_service_unittest.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/component_updater/test/component_updater_service_unitte st.h" 5 #include "chrome/browser/component_updater/test/component_updater_service_unitte st.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/browser/component_updater/test/test_installer.h" 12 #include "chrome/browser/component_updater/test/test_installer.h"
13 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/chrome_paths.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/resource_controller.h"
16 #include "content/public/browser/resource_request_info.h"
17 #include "content/public/browser/resource_throttle.h"
15 #include "libxml/globals.h" 18 #include "libxml/globals.h"
16 #include "net/base/upload_bytes_element_reader.h" 19 #include "net/base/upload_bytes_element_reader.h"
17 #include "net/url_request/url_fetcher.h" 20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request_test_util.h"
18 #include "url/gurl.h" 22 #include "url/gurl.h"
19 23
20 using content::BrowserThread; 24 using content::BrowserThread;
21 25
22 using ::testing::_; 26 using ::testing::_;
23 using ::testing::InSequence; 27 using ::testing::InSequence;
24 using ::testing::Mock; 28 using ::testing::Mock;
25 29
26 namespace component_updater { 30 namespace component_updater {
27 31
28 #define POST_INTERCEPT_SCHEME "http" 32 #define POST_INTERCEPT_SCHEME "http"
29 #define POST_INTERCEPT_HOSTNAME "localhost2" 33 #define POST_INTERCEPT_HOSTNAME "localhost2"
30 #define POST_INTERCEPT_PATH "/update2" 34 #define POST_INTERCEPT_PATH "/update2"
31 35
32 MockComponentObserver::MockComponentObserver() { 36 MockComponentObserver::MockComponentObserver() {
33 } 37 }
34 38
35 MockComponentObserver::~MockComponentObserver() { 39 MockComponentObserver::~MockComponentObserver() {
36 } 40 }
37 41
38 TestConfigurator::TestConfigurator() 42 TestConfigurator::TestConfigurator()
39 : times_(1), 43 : initial_time_(0),
44 times_(1),
40 recheck_time_(0), 45 recheck_time_(0),
41 ondemand_time_(0), 46 ondemand_time_(0),
42 cus_(NULL), 47 cus_(NULL),
43 context_(new net::TestURLRequestContextGetter( 48 context_(new net::TestURLRequestContextGetter(
44 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))) { 49 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))) {
45 } 50 }
46 51
47 TestConfigurator::~TestConfigurator() { 52 TestConfigurator::~TestConfigurator() {
48 } 53 }
49 54
50 int TestConfigurator::InitialDelay() { return 0; } 55 int TestConfigurator::InitialDelay() { return initial_time_; }
51 56
52 int TestConfigurator::NextCheckDelay() { 57 int TestConfigurator::NextCheckDelay() {
53 // This is called when a new full cycle of checking for updates is going 58 // This is called when a new full cycle of checking for updates is going
54 // to happen. In test we normally only test one cycle so it is a good 59 // to happen. In test we normally only test one cycle so it is a good
55 // time to break from the test messageloop Run() method so the test can 60 // time to break from the test messageloop Run() method so the test can
56 // finish. 61 // finish.
57 if (--times_ <= 0) { 62 if (--times_ <= 0) {
58 quit_closure_.Run(); 63 quit_closure_.Run();
59 return 0; 64 return 0;
60 } 65 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 } 122 }
118 123
119 void TestConfigurator::SetComponentUpdateService(ComponentUpdateService* cus) { 124 void TestConfigurator::SetComponentUpdateService(ComponentUpdateService* cus) {
120 cus_ = cus; 125 cus_ = cus;
121 } 126 }
122 127
123 void TestConfigurator::SetQuitClosure(const base::Closure& quit_closure) { 128 void TestConfigurator::SetQuitClosure(const base::Closure& quit_closure) {
124 quit_closure_ = quit_closure; 129 quit_closure_ = quit_closure;
125 } 130 }
126 131
132 void TestConfigurator::SetInitialDelay(int seconds) {
133 initial_time_ = seconds;
134 }
127 135
128 InterceptorFactory::InterceptorFactory() 136 InterceptorFactory::InterceptorFactory()
129 : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME, 137 : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME,
130 POST_INTERCEPT_HOSTNAME) {} 138 POST_INTERCEPT_HOSTNAME) {}
131 139
132 InterceptorFactory::~InterceptorFactory() {} 140 InterceptorFactory::~InterceptorFactory() {}
133 141
134 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() { 142 URLRequestPostInterceptor* InterceptorFactory::CreateInterceptor() {
135 return URLRequestPostInterceptorFactory::CreateInterceptor( 143 return URLRequestPostInterceptorFactory::CreateInterceptor(
136 base::FilePath::FromUTF8Unsafe(POST_INTERCEPT_PATH)); 144 base::FilePath::FromUTF8Unsafe(POST_INTERCEPT_PATH));
(...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 "diffresult=\"0\" differrorcat=\"2\" " 1187 "diffresult=\"0\" differrorcat=\"2\" "
1180 "differrorcode=\"14\" diffextracode1=\"305\" " 1188 "differrorcode=\"14\" diffextracode1=\"305\" "
1181 "previousfp=\"1\" nextfp=\"22\"/></app>")) 1189 "previousfp=\"1\" nextfp=\"22\"/></app>"))
1182 << post_interceptor_->GetRequestsAsString(); 1190 << post_interceptor_->GetRequestsAsString();
1183 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[4].find( 1191 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[4].find(
1184 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">" 1192 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">"
1185 "<updatecheck /><packages><package fp=\"22\"/></packages></app>")) 1193 "<updatecheck /><packages><package fp=\"22\"/></packages></app>"))
1186 << post_interceptor_->GetRequestsAsString(); 1194 << post_interceptor_->GetRequestsAsString();
1187 } 1195 }
1188 1196
1197 void RequestAndDeleteResourceThrottle(
1198 ComponentUpdateService* cus, const char* crx_id) {
1199 // By requesting a throttle and deleting it immediately we ensure that we
1200 // hit the case where the component updater tries to use the weak
1201 // pointer to a dead Resource throttle.
1202 class NoCallResourceController : public content::ResourceController {
1203 public:
1204 virtual ~NoCallResourceController() {}
1205 virtual void Cancel() OVERRIDE { CHECK(false); }
1206 virtual void CancelAndIgnore() OVERRIDE { CHECK(false); }
1207 virtual void CancelWithError(int error_code) OVERRIDE { CHECK(false); }
1208 virtual void Resume() OVERRIDE { CHECK(false); }
1209 };
1210
1211 net::TestURLRequestContext context;
1212 net::TestURLRequest url_request(
1213 GURL("http://foo.example.com/thing.bin"),
1214 net::DEFAULT_PRIORITY,
1215 NULL,
1216 &context);
1217
1218 content::ResourceThrottle* rt =
1219 cus->GetOnDemandResourceThrottle(&url_request, crx_id);
1220 NoCallResourceController controller;
1221 rt->set_controller_for_testing(&controller);
1222 delete rt;
1223 }
1224
1225 TEST_F(ComponentUpdaterTest, ResourceThrottleNoUpdate) {
1226 MockComponentObserver observer;
1227 EXPECT_CALL(observer,
1228 OnEvent(ComponentObserver::COMPONENT_UPDATER_STARTED, 0))
1229 .Times(1);
1230 EXPECT_CALL(observer,
1231 OnEvent(ComponentObserver::COMPONENT_UPDATER_SLEEPING, 0))
1232 .Times(1);
1233
1234 EXPECT_CALL(observer,
1235 OnEvent(ComponentObserver::COMPONENT_NOT_UPDATED, 0))
1236 .Times(1);
1237
1238 TestInstaller installer;
1239 CrxComponent com;
1240 com.observer = &observer;
1241 EXPECT_EQ(ComponentUpdateService::kOk,
1242 RegisterComponent(&com,
1243 kTestComponent_abag,
1244 Version("1.1"),
1245 &installer));
1246
1247 const GURL expected_update_url(
1248 "http://localhost/upd?extra=foo"
1249 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D1.1%26fp%3D%26uc"
1250 "%26installsource%3Dondemand");
1251 // The following two calls ensure that we don't do an update check via the
1252 // timer, so the only update check should be the on-demand one.
1253 test_configurator()->SetInitialDelay(1000000);
1254 test_configurator()->SetRecheckTime(1000000);
1255 test_configurator()->SetLoopCount(1);
1256 component_updater()->Start();
1257
1258 RunThreadsUntilIdle();
1259
1260 EXPECT_EQ(0, post_interceptor_->GetHitCount());
1261
1262 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch(
1263 "updatecheck"), test_file("updatecheck_reply_1.xml")));
1264
1265 BrowserThread::PostTask(
1266 BrowserThread::IO,
1267 FROM_HERE,
1268 base::Bind(&RequestAndDeleteResourceThrottle,
1269 component_updater(),
1270 "abagagagagagagagagagagagagagagag"));
1271
1272 RunThreads();
1273
1274 EXPECT_EQ(1, post_interceptor_->GetHitCount());
1275 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
1276 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count());
1277
1278 component_updater()->Stop();
1279 }
1280
1189 } // namespace component_updater 1281 } // namespace component_updater
1190 1282
OLDNEW
« no previous file with comments | « chrome/browser/component_updater/test/component_updater_service_unittest.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698