OLD | NEW |
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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
8 #include "content/browser/download/byte_stream.h" | 8 #include "content/browser/download/byte_stream.h" |
9 #include "content/browser/download/download_create_info.h" | 9 #include "content/browser/download/download_create_info.h" |
10 #include "content/browser/download/download_file_manager.h" | 10 #include "content/browser/download/download_file_manager.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 MockDownloadFileManager::MockDownloadFileManager() | 124 MockDownloadFileManager::MockDownloadFileManager() |
125 : DownloadFileManager(new MockDownloadFileFactory) { | 125 : DownloadFileManager(new MockDownloadFileFactory) { |
126 } | 126 } |
127 | 127 |
128 } // namespace | 128 } // namespace |
129 | 129 |
130 class DownloadItemTest : public testing::Test { | 130 class DownloadItemTest : public testing::Test { |
131 public: | 131 public: |
132 class MockObserver : public DownloadItem::Observer { | 132 class MockObserver : public DownloadItem::Observer { |
133 public: | 133 public: |
134 explicit MockObserver(DownloadItem* item) | 134 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { |
135 : item_(item), | |
136 removed_(false), | |
137 destroyed_(false), | |
138 updated_(false) { | |
139 item_->AddObserver(this); | 135 item_->AddObserver(this); |
140 } | 136 } |
141 | 137 ~MockObserver() { item_->RemoveObserver(this); } |
142 virtual ~MockObserver() { | |
143 if (item_) item_->RemoveObserver(this); | |
144 } | |
145 | |
146 virtual void OnDownloadRemoved(DownloadItem* download) { | |
147 removed_ = true; | |
148 } | |
149 | 138 |
150 virtual void OnDownloadUpdated(DownloadItem* download) { | 139 virtual void OnDownloadUpdated(DownloadItem* download) { |
151 updated_ = true; | 140 updated_ = true; |
152 } | 141 } |
153 | 142 |
154 virtual void OnDownloadOpened(DownloadItem* download) { | 143 virtual void OnDownloadOpened(DownloadItem* download) { } |
155 } | |
156 | |
157 virtual void OnDownloadDestroyed(DownloadItem* download) { | |
158 destroyed_ = true; | |
159 item_->RemoveObserver(this); | |
160 item_ = NULL; | |
161 } | |
162 | |
163 bool CheckRemoved() { | |
164 return removed_; | |
165 } | |
166 | |
167 bool CheckDestroyed() { | |
168 return destroyed_; | |
169 } | |
170 | 144 |
171 bool CheckUpdated() { | 145 bool CheckUpdated() { |
172 bool was_updated = updated_; | 146 bool was_updated = updated_; |
173 updated_ = false; | 147 updated_ = false; |
174 return was_updated; | 148 return was_updated; |
175 } | 149 } |
176 | 150 |
177 private: | 151 private: |
178 DownloadItem* item_; | 152 DownloadItem* item_; |
179 bool removed_; | |
180 bool destroyed_; | |
181 bool updated_; | 153 bool updated_; |
182 }; | 154 }; |
183 | 155 |
184 DownloadItemTest() | 156 DownloadItemTest() |
185 : ui_thread_(BrowserThread::UI, &loop_), | 157 : ui_thread_(BrowserThread::UI, &loop_), |
186 file_thread_(BrowserThread::FILE, &loop_), | 158 file_thread_(BrowserThread::FILE, &loop_), |
187 file_manager_(new MockDownloadFileManager), | 159 file_manager_(new MockDownloadFileManager), |
188 delegate_(file_manager_.get()) { | 160 delegate_(file_manager_.get()) { |
189 } | 161 } |
190 | 162 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 } | 295 } |
324 | 296 |
325 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 297 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
326 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 298 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
327 MockObserver observer(item); | 299 MockObserver observer(item); |
328 | 300 |
329 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 301 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
330 ASSERT_TRUE(observer.CheckUpdated()); | 302 ASSERT_TRUE(observer.CheckUpdated()); |
331 } | 303 } |
332 | 304 |
333 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | |
334 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | |
335 MockObserver observer(item); | |
336 | |
337 DestroyDownloadItem(item); | |
338 ASSERT_TRUE(observer.CheckDestroyed()); | |
339 } | |
340 | |
341 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 305 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
342 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 306 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
343 MockObserver observer(item); | 307 MockObserver observer(item); |
344 | 308 |
345 item->Remove(); | 309 item->Remove(); |
346 ASSERT_TRUE(observer.CheckUpdated()); | 310 ASSERT_TRUE(observer.CheckUpdated()); |
347 ASSERT_TRUE(observer.CheckRemoved()); | |
348 } | 311 } |
349 | 312 |
350 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 313 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
351 // Setting to NOT_DANGEROUS does not trigger a notification. | 314 // Setting to NOT_DANGEROUS does not trigger a notification. |
352 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 315 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
353 MockObserver safe_observer(safe_item); | 316 MockObserver safe_observer(safe_item); |
354 | 317 |
355 safe_item->OnAllDataSaved(1, ""); | 318 safe_item->OnAllDataSaved(1, ""); |
356 EXPECT_TRUE(safe_observer.CheckUpdated()); | 319 EXPECT_TRUE(safe_observer.CheckUpdated()); |
357 safe_item->OnContentCheckCompleted( | 320 safe_item->OnContentCheckCompleted( |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 563 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
601 | 564 |
602 EXPECT_FALSE(item->GetFileExternallyRemoved()); | 565 EXPECT_FALSE(item->GetFileExternallyRemoved()); |
603 item->OnDownloadedFileRemoved(); | 566 item->OnDownloadedFileRemoved(); |
604 EXPECT_TRUE(item->GetFileExternallyRemoved()); | 567 EXPECT_TRUE(item->GetFileExternallyRemoved()); |
605 } | 568 } |
606 | 569 |
607 TEST(MockDownloadItem, Compiles) { | 570 TEST(MockDownloadItem, Compiles) { |
608 MockDownloadItem mock_item; | 571 MockDownloadItem mock_item; |
609 } | 572 } |
OLD | NEW |