OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/component_updater_service.h" | 5 #include "chrome/browser/component_updater/component_updater_service.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
24 #include "libxml/globals.h" | 24 #include "libxml/globals.h" |
25 | 25 |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 | 27 |
28 namespace { | 28 namespace { |
29 // Overrides some of the component updater behaviors so it is easier to test | 29 // Overrides some of the component updater behaviors so it is easier to test |
30 // and loops faster. In actual usage it takes hours do to a full cycle. | 30 // and loops faster. In actual usage it takes hours do to a full cycle. |
31 class TestConfigurator : public ComponentUpdateService::Configurator { | 31 class TestConfigurator : public ComponentUpdateService::Configurator { |
32 public: | 32 public: |
| 33 TestConfigurator() : times_(1) { |
| 34 } |
| 35 |
33 virtual int InitialDelay() OVERRIDE { return 0; } | 36 virtual int InitialDelay() OVERRIDE { return 0; } |
34 | 37 |
35 virtual int NextCheckDelay() OVERRIDE { | 38 virtual int NextCheckDelay() OVERRIDE { |
36 // This is called when a new full cycle of checking for updates is going | 39 // This is called when a new full cycle of checking for updates is going |
37 // to happen. In test we normally only test one cycle so it is a good | 40 // to happen. In test we normally only test one cycle so it is a good |
38 // time to break from the test messageloop Run() method so the test can | 41 // time to break from the test messageloop Run() method so the test can |
39 // finish. | 42 // finish. |
| 43 if (--times_ > 0) |
| 44 return 1; |
| 45 |
40 MessageLoop::current()->Quit(); | 46 MessageLoop::current()->Quit(); |
41 return 0; | 47 return 0; |
42 } | 48 } |
43 | 49 |
44 virtual int StepDelay() OVERRIDE { | 50 virtual int StepDelay() OVERRIDE { |
45 return 0; | 51 return 0; |
46 } | 52 } |
47 | 53 |
48 virtual int MinimumReCheckWait() OVERRIDE { | 54 virtual int MinimumReCheckWait() OVERRIDE { |
49 return 0; | 55 return 0; |
50 } | 56 } |
51 | 57 |
52 virtual GURL UpdateUrl() OVERRIDE { return GURL("http://localhost/upd"); } | 58 virtual GURL UpdateUrl() OVERRIDE { return GURL("http://localhost/upd"); } |
53 | 59 |
54 virtual size_t UrlSizeLimit() OVERRIDE { return 256; } | 60 virtual size_t UrlSizeLimit() OVERRIDE { return 256; } |
55 | 61 |
56 virtual net::URLRequestContextGetter* RequestContext() OVERRIDE { | 62 virtual net::URLRequestContextGetter* RequestContext() OVERRIDE { |
57 return new TestURLRequestContextGetter(); | 63 return new TestURLRequestContextGetter(); |
58 } | 64 } |
59 | 65 |
60 // Don't use the utility process to decode files. | 66 // Don't use the utility process to decode files. |
61 virtual bool InProcess() OVERRIDE { return true; } | 67 virtual bool InProcess() OVERRIDE { return true; } |
| 68 |
| 69 // Set how many update checks are called, the default value is just once. |
| 70 void SetLoopCount(int times) { times_ = times; } |
| 71 |
| 72 private: |
| 73 int times_; |
62 }; | 74 }; |
63 | 75 |
64 class TestInstaller : public ComponentInstaller { | 76 class TestInstaller : public ComponentInstaller { |
65 public : | 77 public : |
66 explicit TestInstaller() | 78 explicit TestInstaller() |
67 : error_(0), install_count_(0) { | 79 : error_(0), install_count_(0) { |
68 } | 80 } |
69 | 81 |
70 virtual void OnUpdateError(int error) OVERRIDE { | 82 virtual void OnUpdateError(int error) OVERRIDE { |
71 EXPECT_NE(0, error); | 83 EXPECT_NE(0, error); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 115 |
104 const char header_ok_reply[] = | 116 const char header_ok_reply[] = |
105 "HTTP/1.1 200 OK\0" | 117 "HTTP/1.1 200 OK\0" |
106 "Content-type: text/html\0" | 118 "Content-type: text/html\0" |
107 "\0"; | 119 "\0"; |
108 } // namespace | 120 } // namespace |
109 | 121 |
110 // Common fixture for all the component updater tests. | 122 // Common fixture for all the component updater tests. |
111 class ComponentUpdaterTest : public TestingBrowserProcessTest { | 123 class ComponentUpdaterTest : public TestingBrowserProcessTest { |
112 public: | 124 public: |
113 ComponentUpdaterTest() : component_updater_(NULL) { | 125 ComponentUpdaterTest() : component_updater_(NULL), test_config_(NULL) { |
114 // The component updater instance under test. | 126 // The component updater instance under test. |
115 component_updater_.reset( | 127 test_config_ = new TestConfigurator; |
116 ComponentUpdateServiceFactory(new TestConfigurator)); | 128 component_updater_.reset(ComponentUpdateServiceFactory(test_config_)); |
117 // The test directory is chrome/test/data/components. | 129 // The test directory is chrome/test/data/components. |
118 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); | 130 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
119 test_data_dir_ = test_data_dir_.AppendASCII("components"); | 131 test_data_dir_ = test_data_dir_.AppendASCII("components"); |
120 | 132 |
121 // Subscribe to all component updater notifications. | 133 // Subscribe to all component updater notifications. |
122 const int notifications[] = { | 134 const int notifications[] = { |
123 chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, | 135 chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, |
124 chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, | 136 chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, |
125 chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, | 137 chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, |
126 chrome::NOTIFICATION_COMPONENT_UPDATE_READY | 138 chrome::NOTIFICATION_COMPONENT_UPDATE_READY |
(...skipping 16 matching lines...) Expand all Loading... |
143 | 155 |
144 // Makes the full path to a component updater test file. | 156 // Makes the full path to a component updater test file. |
145 const FilePath test_file(const char* file) { | 157 const FilePath test_file(const char* file) { |
146 return test_data_dir_.AppendASCII(file); | 158 return test_data_dir_.AppendASCII(file); |
147 } | 159 } |
148 | 160 |
149 TestNotificationTracker& notification_tracker() { | 161 TestNotificationTracker& notification_tracker() { |
150 return notification_tracker_; | 162 return notification_tracker_; |
151 } | 163 } |
152 | 164 |
| 165 TestConfigurator* test_configurator() { |
| 166 return test_config_; |
| 167 } |
| 168 |
153 private: | 169 private: |
154 scoped_ptr<ComponentUpdateService> component_updater_; | 170 scoped_ptr<ComponentUpdateService> component_updater_; |
155 FilePath test_data_dir_; | 171 FilePath test_data_dir_; |
156 TestNotificationTracker notification_tracker_; | 172 TestNotificationTracker notification_tracker_; |
| 173 TestConfigurator* test_config_; |
157 }; | 174 }; |
158 | 175 |
159 // Verify that our test fixture work and the component updater can | 176 // Verify that our test fixture work and the component updater can |
160 // be created and destroyed with no side effects. | 177 // be created and destroyed with no side effects. |
161 TEST_F(ComponentUpdaterTest, VerifyFixture) { | 178 TEST_F(ComponentUpdaterTest, VerifyFixture) { |
162 EXPECT_TRUE(component_updater() != NULL); | 179 EXPECT_TRUE(component_updater() != NULL); |
163 EXPECT_EQ(0ul, notification_tracker().size()); | 180 EXPECT_EQ(0ul, notification_tracker().size()); |
164 } | 181 } |
165 | 182 |
166 // Verify that the component updater can be caught in a quick | 183 // Verify that the component updater can be caught in a quick |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 component_updater()->RegisterComponent(com); | 218 component_updater()->RegisterComponent(com); |
202 | 219 |
203 const char expected_update_url[] = | 220 const char expected_update_url[] = |
204 "http://localhost/upd?x=id%3D" | 221 "http://localhost/upd?x=id%3D" |
205 "abagagagagagagagagagagagagagagag%26v%3D1.1%26uc"; | 222 "abagagagagagagagagagagagagagagag%26v%3D1.1%26uc"; |
206 | 223 |
207 interceptor->SetResponse(expected_update_url, | 224 interceptor->SetResponse(expected_update_url, |
208 header_ok_reply, | 225 header_ok_reply, |
209 test_file("updatecheck_reply_1.xml")); | 226 test_file("updatecheck_reply_1.xml")); |
210 | 227 |
| 228 // We loop twice, but there are no updates so we expect two sleep messages. |
| 229 test_configurator()->SetLoopCount(2); |
211 component_updater()->Start(); | 230 component_updater()->Start(); |
212 | 231 |
213 ASSERT_EQ(1ul, notification_tracker().size()); | 232 ASSERT_EQ(1ul, notification_tracker().size()); |
214 TestNotificationTracker::Event ev1 = notification_tracker().at(0); | 233 TestNotificationTracker::Event ev1 = notification_tracker().at(0); |
215 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type); | 234 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type); |
216 | 235 |
217 message_loop.Run(); | 236 message_loop.Run(); |
218 | 237 |
219 ASSERT_EQ(2ul, notification_tracker().size()); | 238 ASSERT_EQ(3ul, notification_tracker().size()); |
220 TestNotificationTracker::Event ev2 = notification_tracker().at(1); | 239 TestNotificationTracker::Event ev2 = notification_tracker().at(1); |
221 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); | 240 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 241 TestNotificationTracker::Event ev3 = notification_tracker().at(2); |
| 242 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 243 EXPECT_EQ(2, interceptor->hit_count()); |
| 244 |
| 245 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); |
| 246 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); |
| 247 |
| 248 component_updater()->Stop(); |
| 249 |
| 250 // Loop twice again but this case we simulate a server error by returning |
| 251 // an empty file. |
| 252 |
| 253 interceptor->SetResponse(expected_update_url, |
| 254 header_ok_reply, |
| 255 test_file("updatecheck_reply_empty")); |
| 256 |
| 257 notification_tracker().Reset(); |
| 258 test_configurator()->SetLoopCount(2); |
| 259 component_updater()->Start(); |
| 260 |
| 261 message_loop.Run(); |
| 262 |
| 263 ASSERT_EQ(3ul, notification_tracker().size()); |
| 264 ev1 = notification_tracker().at(0); |
| 265 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type); |
| 266 ev2 = notification_tracker().at(1); |
| 267 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 268 ev3 = notification_tracker().at(2); |
| 269 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 270 EXPECT_EQ(4, interceptor->hit_count()); |
222 | 271 |
223 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); | 272 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); |
224 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); | 273 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); |
225 | 274 |
226 component_updater()->Stop(); | 275 component_updater()->Stop(); |
227 | 276 |
228 delete com.installer; | 277 delete com.installer; |
229 xmlCleanupGlobals(); | 278 xmlCleanupGlobals(); |
230 } | 279 } |
231 | 280 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev3.type); | 336 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev3.type); |
288 | 337 |
289 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); | 338 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); |
290 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count()); | 339 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count()); |
291 | 340 |
292 component_updater()->Stop(); | 341 component_updater()->Stop(); |
293 delete com1.installer; | 342 delete com1.installer; |
294 delete com2.installer; | 343 delete com2.installer; |
295 xmlCleanupGlobals(); | 344 xmlCleanupGlobals(); |
296 } | 345 } |
OLD | NEW |