| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 MockDownloadFileManager::MockDownloadFileManager() | 116 MockDownloadFileManager::MockDownloadFileManager() |
| 117 : DownloadFileManager(new MockDownloadFileFactory) { | 117 : DownloadFileManager(new MockDownloadFileFactory) { |
| 118 } | 118 } |
| 119 | 119 |
| 120 } // namespace | 120 } // namespace |
| 121 | 121 |
| 122 class DownloadItemTest : public testing::Test { | 122 class DownloadItemTest : public testing::Test { |
| 123 public: | 123 public: |
| 124 class MockObserver : public DownloadItem::Observer { | 124 class MockObserver : public DownloadItem::Observer { |
| 125 public: | 125 public: |
| 126 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { | 126 explicit MockObserver(DownloadItem* item) |
| 127 : item_(item), |
| 128 removed_(false), |
| 129 destroyed_(false), |
| 130 updated_(false) { |
| 127 item_->AddObserver(this); | 131 item_->AddObserver(this); |
| 128 } | 132 } |
| 129 ~MockObserver() { item_->RemoveObserver(this); } | 133 |
| 134 virtual ~MockObserver() { |
| 135 if (item_) item_->RemoveObserver(this); |
| 136 } |
| 137 |
| 138 virtual void OnDownloadRemoved(DownloadItem* download) { |
| 139 removed_ = true; |
| 140 } |
| 130 | 141 |
| 131 virtual void OnDownloadUpdated(DownloadItem* download) { | 142 virtual void OnDownloadUpdated(DownloadItem* download) { |
| 132 updated_ = true; | 143 updated_ = true; |
| 133 } | 144 } |
| 134 | 145 |
| 135 virtual void OnDownloadOpened(DownloadItem* download) { } | 146 virtual void OnDownloadOpened(DownloadItem* download) { |
| 147 } |
| 148 |
| 149 virtual void OnDownloadDestroyed(DownloadItem* download) { |
| 150 destroyed_ = true; |
| 151 item_->RemoveObserver(this); |
| 152 item_ = NULL; |
| 153 } |
| 154 |
| 155 bool CheckRemoved() { |
| 156 return removed_; |
| 157 } |
| 158 |
| 159 bool CheckDestroyed() { |
| 160 return destroyed_; |
| 161 } |
| 136 | 162 |
| 137 bool CheckUpdated() { | 163 bool CheckUpdated() { |
| 138 bool was_updated = updated_; | 164 bool was_updated = updated_; |
| 139 updated_ = false; | 165 updated_ = false; |
| 140 return was_updated; | 166 return was_updated; |
| 141 } | 167 } |
| 142 | 168 |
| 143 private: | 169 private: |
| 144 DownloadItem* item_; | 170 DownloadItem* item_; |
| 171 bool removed_; |
| 172 bool destroyed_; |
| 145 bool updated_; | 173 bool updated_; |
| 146 }; | 174 }; |
| 147 | 175 |
| 148 DownloadItemTest() | 176 DownloadItemTest() |
| 149 : ui_thread_(BrowserThread::UI, &loop_), | 177 : ui_thread_(BrowserThread::UI, &loop_), |
| 150 file_thread_(BrowserThread::FILE, &loop_) { | 178 file_thread_(BrowserThread::FILE, &loop_) { |
| 151 } | 179 } |
| 152 | 180 |
| 153 ~DownloadItemTest() { | 181 ~DownloadItemTest() { |
| 154 } | 182 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } | 308 } |
| 281 | 309 |
| 282 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 310 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
| 283 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 311 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 284 MockObserver observer(item); | 312 MockObserver observer(item); |
| 285 | 313 |
| 286 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 314 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
| 287 ASSERT_TRUE(observer.CheckUpdated()); | 315 ASSERT_TRUE(observer.CheckUpdated()); |
| 288 } | 316 } |
| 289 | 317 |
| 318 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
| 319 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 320 MockObserver observer(item); |
| 321 |
| 322 DestroyDownloadItem(item); |
| 323 ASSERT_TRUE(observer.CheckDestroyed()); |
| 324 } |
| 325 |
| 290 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 326 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
| 291 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 327 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 292 MockObserver observer(item); | 328 MockObserver observer(item); |
| 293 | 329 |
| 294 item->Remove(); | 330 item->Remove(); |
| 295 ASSERT_TRUE(observer.CheckUpdated()); | 331 ASSERT_TRUE(observer.CheckUpdated()); |
| 332 ASSERT_TRUE(observer.CheckRemoved()); |
| 296 } | 333 } |
| 297 | 334 |
| 298 TEST_F(DownloadItemTest, NotificationAfterOnTargetPathDetermined) { | 335 TEST_F(DownloadItemTest, NotificationAfterOnTargetPathDetermined) { |
| 299 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 336 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 300 MockObserver safe_observer(safe_item); | 337 MockObserver safe_observer(safe_item); |
| 301 | 338 |
| 302 // Calling OnTargetPathDetermined does not trigger notification if danger type | 339 // Calling OnTargetPathDetermined does not trigger notification if danger type |
| 303 // is NOT_DANGEROUS. | 340 // is NOT_DANGEROUS. |
| 304 safe_item->OnTargetPathDetermined( | 341 safe_item->OnTargetPathDetermined( |
| 305 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 342 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 620 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 584 | 621 |
| 585 EXPECT_FALSE(item->GetFileExternallyRemoved()); | 622 EXPECT_FALSE(item->GetFileExternallyRemoved()); |
| 586 item->OnDownloadedFileRemoved(); | 623 item->OnDownloadedFileRemoved(); |
| 587 EXPECT_TRUE(item->GetFileExternallyRemoved()); | 624 EXPECT_TRUE(item->GetFileExternallyRemoved()); |
| 588 } | 625 } |
| 589 | 626 |
| 590 TEST(MockDownloadItem, Compiles) { | 627 TEST(MockDownloadItem, Compiles) { |
| 591 MockDownloadItem mock_item; | 628 MockDownloadItem mock_item; |
| 592 } | 629 } |
| OLD | NEW |