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

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 "content/test/net/url_request_prepackaged_interceptor.h" 18 #include "content/test/net/url_request_prepackaged_interceptor.h"
16 #include "libxml/globals.h" 19 #include "libxml/globals.h"
17 #include "net/base/upload_bytes_element_reader.h" 20 #include "net/base/upload_bytes_element_reader.h"
18 #include "net/url_request/url_fetcher.h" 21 #include "net/url_request/url_fetcher.h"
22 #include "net/url_request/url_request_test_util.h"
19 #include "url/gurl.h" 23 #include "url/gurl.h"
20 24
21 using content::BrowserThread; 25 using content::BrowserThread;
22 26
23 using ::testing::_; 27 using ::testing::_;
24 using ::testing::InSequence; 28 using ::testing::InSequence;
25 using ::testing::Mock; 29 using ::testing::Mock;
26 30
27 MockComponentObserver::MockComponentObserver() { 31 MockComponentObserver::MockComponentObserver() {
28 } 32 }
29 33
30 MockComponentObserver::~MockComponentObserver() { 34 MockComponentObserver::~MockComponentObserver() {
31 } 35 }
32 36
33 TestConfigurator::TestConfigurator() 37 TestConfigurator::TestConfigurator()
34 : times_(1), 38 : initial_time_(0),
39 times_(1),
35 recheck_time_(0), 40 recheck_time_(0),
36 ondemand_time_(0), 41 ondemand_time_(0),
37 cus_(NULL), 42 cus_(NULL),
38 context_(new net::TestURLRequestContextGetter( 43 context_(new net::TestURLRequestContextGetter(
39 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))) { 44 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))) {
40 } 45 }
41 46
42 TestConfigurator::~TestConfigurator() { 47 TestConfigurator::~TestConfigurator() {
43 } 48 }
44 49
45 int TestConfigurator::InitialDelay() { return 0; } 50 int TestConfigurator::InitialDelay() { return initial_time_; }
46 51
47 int TestConfigurator::NextCheckDelay() { 52 int TestConfigurator::NextCheckDelay() {
48 // This is called when a new full cycle of checking for updates is going 53 // This is called when a new full cycle of checking for updates is going
49 // to happen. In test we normally only test one cycle so it is a good 54 // to happen. In test we normally only test one cycle so it is a good
50 // time to break from the test messageloop Run() method so the test can 55 // time to break from the test messageloop Run() method so the test can
51 // finish. 56 // finish.
52 if (--times_ <= 0) { 57 if (--times_ <= 0) {
53 quit_closure_.Run(); 58 quit_closure_.Run();
54 return 0; 59 return 0;
55 } 60 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 116 }
112 117
113 void TestConfigurator::SetComponentUpdateService(ComponentUpdateService* cus) { 118 void TestConfigurator::SetComponentUpdateService(ComponentUpdateService* cus) {
114 cus_ = cus; 119 cus_ = cus;
115 } 120 }
116 121
117 void TestConfigurator::SetQuitClosure(const base::Closure& quit_closure) { 122 void TestConfigurator::SetQuitClosure(const base::Closure& quit_closure) {
118 quit_closure_ = quit_closure; 123 quit_closure_ = quit_closure;
119 } 124 }
120 125
126 void TestConfigurator::SetInitialDelay(int seconds) {
127 initial_time_ = seconds;
128 }
129
121 ComponentUpdaterTest::ComponentUpdaterTest() 130 ComponentUpdaterTest::ComponentUpdaterTest()
122 : test_config_(NULL), 131 : test_config_(NULL),
123 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 132 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
124 // The component updater instance under test. 133 // The component updater instance under test.
125 test_config_ = new TestConfigurator; 134 test_config_ = new TestConfigurator;
126 component_updater_.reset(ComponentUpdateServiceFactory(test_config_)); 135 component_updater_.reset(ComponentUpdateServiceFactory(test_config_));
127 test_config_->SetComponentUpdateService(component_updater_.get()); 136 test_config_->SetComponentUpdateService(component_updater_.get());
128 137
129 // The test directory is chrome/test/data/components. 138 // The test directory is chrome/test/data/components.
130 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); 139 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
(...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 // There may still be pings in the queue. 1117 // There may still be pings in the queue.
1109 RunThreadsUntilIdle(); 1118 RunThreadsUntilIdle();
1110 1119
1111 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); 1120 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
1112 EXPECT_EQ(2, static_cast<TestInstaller*>(com.installer)->install_count()); 1121 EXPECT_EQ(2, static_cast<TestInstaller*>(com.installer)->install_count());
1113 1122
1114 EXPECT_EQ(1, ping_checker.NumHits()) << ping_checker.GetPings(); 1123 EXPECT_EQ(1, ping_checker.NumHits()) << ping_checker.GetPings();
1115 EXPECT_EQ(1, ping_checker.NumMisses()) << ping_checker.GetPings(); 1124 EXPECT_EQ(1, ping_checker.NumMisses()) << ping_checker.GetPings();
1116 EXPECT_EQ(5, interceptor.GetHitCount()); 1125 EXPECT_EQ(5, interceptor.GetHitCount());
1117 } 1126 }
1127
1128 void RequestAndDeleteResourceThrottle(
1129 ComponentUpdateService* cus, const char* crx_id) {
1130 // By requesting a throttle and deleting it immediately we insure that we
waffles 2013/11/12 00:22:58 nitpicky: ensure. Also on line 1185.
1131 // excersize the case where the component updater tries to use the weak
Sorin Jianu 2013/10/31 21:33:04 exercise
1132 // pointer to a dead Resource throttle.
1133 class NoCallResourceController : public content::ResourceController {
1134 public:
1135 virtual ~NoCallResourceController() {}
1136 virtual void Cancel() OVERRIDE { CHECK(false); }
1137 virtual void CancelAndIgnore() OVERRIDE { CHECK(false); }
1138 virtual void CancelWithError(int error_code) OVERRIDE { CHECK(false); }
1139 virtual void Resume() OVERRIDE { CHECK(false); }
1140 };
1141
1142 net::TestURLRequestContext context;
1143 net::TestURLRequest url_request(
1144 GURL("http://foo.example.com/thing.bin"), NULL, &context, NULL);
1145
1146 content::ResourceThrottle* rt =
1147 cus->GetOnDemandResourceThrottle(&url_request, crx_id);
1148 NoCallResourceController controller;
1149 rt->set_controller_for_testing(&controller);
1150 delete rt;
1151 }
1152
1153 TEST_F(ComponentUpdaterTest, ResourceThrottleNoUpdate) {
1154 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
1155
1156 MockComponentObserver observer;
1157 EXPECT_CALL(observer,
1158 OnEvent(ComponentObserver::COMPONENT_UPDATER_STARTED, 0))
1159 .Times(1);
1160 EXPECT_CALL(observer,
1161 OnEvent(ComponentObserver::COMPONENT_UPDATER_SLEEPING, 0))
1162 .Times(1);
1163
1164 EXPECT_CALL(observer,
1165 OnEvent(ComponentObserver::COMPONENT_NOT_UPDATED, 0))
1166 .Times(1);
1167
1168 TestInstaller installer;
1169 CrxComponent com;
1170 com.observer = &observer;
1171 EXPECT_EQ(ComponentUpdateService::kOk,
1172 RegisterComponent(&com,
1173 kTestComponent_abag,
1174 Version("1.1"),
1175 &installer));
1176
1177 const GURL expected_update_url(
1178 "http://localhost/upd?extra=foo"
1179 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D1.1%26fp%3D%26uc"
1180 "%26installsource%3Dondemand");
1181
1182 interceptor.SetResponse(expected_update_url,
1183 test_file("updatecheck_reply_1.xml"));
1184
1185 // The following two calls insure that we don't do an update check via the
1186 // timer, so the only update check should be the on-demand one.
1187 test_configurator()->SetInitialDelay(1000000);
1188 test_configurator()->SetRecheckTime(1000000);
1189 test_configurator()->SetLoopCount(1);
1190 component_updater()->Start();
1191
1192 RunThreadsUntilIdle();
1193
1194 EXPECT_EQ(0, interceptor.GetHitCount());
1195
1196 BrowserThread::PostTask(
1197 BrowserThread::IO,
1198 FROM_HERE,
1199 base::Bind(&RequestAndDeleteResourceThrottle,
1200 component_updater(),
1201 "abagagagagagagagagagagagagagagag"));
1202
1203 RunThreads();
1204
1205 EXPECT_EQ(1, interceptor.GetHitCount());
1206 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
1207 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count());
1208
1209 component_updater()->Stop();
1210 }
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