| 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) : item_(item), updated_(false) { | 134 explicit MockObserver(DownloadItem* item) |
| 135 : item_(item), |
| 136 removed_(false), |
| 137 destroyed_(false), |
| 138 updated_(false) { |
| 135 item_->AddObserver(this); | 139 item_->AddObserver(this); |
| 136 } | 140 } |
| 137 ~MockObserver() { item_->RemoveObserver(this); } | 141 |
| 142 virtual ~MockObserver() { |
| 143 if (item_) item_->RemoveObserver(this); |
| 144 } |
| 145 |
| 146 virtual void OnDownloadRemoved(DownloadItem* download) { |
| 147 removed_ = true; |
| 148 } |
| 138 | 149 |
| 139 virtual void OnDownloadUpdated(DownloadItem* download) { | 150 virtual void OnDownloadUpdated(DownloadItem* download) { |
| 140 updated_ = true; | 151 updated_ = true; |
| 141 } | 152 } |
| 142 | 153 |
| 143 virtual void OnDownloadOpened(DownloadItem* download) { } | 154 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 } |
| 144 | 170 |
| 145 bool CheckUpdated() { | 171 bool CheckUpdated() { |
| 146 bool was_updated = updated_; | 172 bool was_updated = updated_; |
| 147 updated_ = false; | 173 updated_ = false; |
| 148 return was_updated; | 174 return was_updated; |
| 149 } | 175 } |
| 150 | 176 |
| 151 private: | 177 private: |
| 152 DownloadItem* item_; | 178 DownloadItem* item_; |
| 179 bool removed_; |
| 180 bool destroyed_; |
| 153 bool updated_; | 181 bool updated_; |
| 154 }; | 182 }; |
| 155 | 183 |
| 156 DownloadItemTest() | 184 DownloadItemTest() |
| 157 : ui_thread_(BrowserThread::UI, &loop_), | 185 : ui_thread_(BrowserThread::UI, &loop_), |
| 158 file_thread_(BrowserThread::FILE, &loop_), | 186 file_thread_(BrowserThread::FILE, &loop_), |
| 159 file_manager_(new MockDownloadFileManager), | 187 file_manager_(new MockDownloadFileManager), |
| 160 delegate_(file_manager_.get()) { | 188 delegate_(file_manager_.get()) { |
| 161 } | 189 } |
| 162 | 190 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 } | 323 } |
| 296 | 324 |
| 297 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 325 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
| 298 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 326 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 299 MockObserver observer(item); | 327 MockObserver observer(item); |
| 300 | 328 |
| 301 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 329 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
| 302 ASSERT_TRUE(observer.CheckUpdated()); | 330 ASSERT_TRUE(observer.CheckUpdated()); |
| 303 } | 331 } |
| 304 | 332 |
| 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 |
| 305 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 341 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
| 306 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 342 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 307 MockObserver observer(item); | 343 MockObserver observer(item); |
| 308 | 344 |
| 309 item->Remove(); | 345 item->Remove(); |
| 310 ASSERT_TRUE(observer.CheckUpdated()); | 346 ASSERT_TRUE(observer.CheckUpdated()); |
| 347 ASSERT_TRUE(observer.CheckRemoved()); |
| 311 } | 348 } |
| 312 | 349 |
| 313 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 350 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
| 314 // Setting to NOT_DANGEROUS does not trigger a notification. | 351 // Setting to NOT_DANGEROUS does not trigger a notification. |
| 315 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 352 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 316 MockObserver safe_observer(safe_item); | 353 MockObserver safe_observer(safe_item); |
| 317 | 354 |
| 318 safe_item->OnAllDataSaved(1, ""); | 355 safe_item->OnAllDataSaved(1, ""); |
| 319 EXPECT_TRUE(safe_observer.CheckUpdated()); | 356 EXPECT_TRUE(safe_observer.CheckUpdated()); |
| 320 safe_item->OnContentCheckCompleted( | 357 safe_item->OnContentCheckCompleted( |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 533 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 497 | 534 |
| 498 EXPECT_FALSE(item->GetFileExternallyRemoved()); | 535 EXPECT_FALSE(item->GetFileExternallyRemoved()); |
| 499 item->OnDownloadedFileRemoved(); | 536 item->OnDownloadedFileRemoved(); |
| 500 EXPECT_TRUE(item->GetFileExternallyRemoved()); | 537 EXPECT_TRUE(item->GetFileExternallyRemoved()); |
| 501 } | 538 } |
| 502 | 539 |
| 503 TEST(MockDownloadItem, Compiles) { | 540 TEST(MockDownloadItem, Compiles) { |
| 504 MockDownloadItem mock_item; | 541 MockDownloadItem mock_item; |
| 505 } | 542 } |
| OLD | NEW |