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 "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 "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.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_id.h" | 10 #include "content/browser/download/download_id.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 virtual void SetUp() { | 84 virtual void SetUp() { |
85 } | 85 } |
86 | 86 |
87 virtual void TearDown() { | 87 virtual void TearDown() { |
88 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); | 88 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); |
89 STLDeleteElements(&allocated_downloads_); | 89 STLDeleteElements(&allocated_downloads_); |
90 allocated_downloads_.clear(); | 90 allocated_downloads_.clear(); |
91 } | 91 } |
92 | 92 |
93 // This class keeps ownership of the created download item; it will | 93 // This class keeps ownership of the created download item; it will |
94 // be torn down at the end of the test. | 94 // be torn down at the end of the test unless DestroyDownloadItem is |
| 95 // called. |
95 DownloadItem* CreateDownloadItem(DownloadItem::DownloadState state) { | 96 DownloadItem* CreateDownloadItem(DownloadItem::DownloadState state) { |
96 // Normally, the download system takes ownership of info, and is | 97 // Normally, the download system takes ownership of info, and is |
97 // responsible for deleting it. In these unit tests, however, we | 98 // responsible for deleting it. In these unit tests, however, we |
98 // don't call the function that deletes it, so we do so ourselves. | 99 // don't call the function that deletes it, so we do so ourselves. |
99 scoped_ptr<DownloadCreateInfo> info_; | 100 scoped_ptr<DownloadCreateInfo> info_; |
100 | 101 |
101 info_.reset(new DownloadCreateInfo()); | 102 info_.reset(new DownloadCreateInfo()); |
102 info_->download_id = id_factory_->GetNextId(); | 103 info_->download_id = id_factory_->GetNextId(); |
103 info_->prompt_user_for_save_location = false; | 104 info_->prompt_user_for_save_location = false; |
104 info_->url_chain.push_back(GURL()); | 105 info_->url_chain.push_back(GURL()); |
105 info_->state = state; | 106 info_->state = state; |
106 | 107 |
107 MockRequestHandle* request_handle = | 108 MockRequestHandle* request_handle = |
108 new testing::NiceMock<MockRequestHandle>; | 109 new testing::NiceMock<MockRequestHandle>; |
109 DownloadItem* download = | 110 DownloadItem* download = |
110 new DownloadItemImpl(&delegate_, *(info_.get()), | 111 new DownloadItemImpl(&delegate_, *(info_.get()), |
111 request_handle, false); | 112 request_handle, false); |
112 allocated_downloads_.push_back(download); | 113 allocated_downloads_.insert(download); |
113 return download; | 114 return download; |
114 } | 115 } |
115 | 116 |
| 117 // Destroy a previously created download item. |
| 118 void DestroyDownloadItem(DownloadItem* item) { |
| 119 allocated_downloads_.erase(item); |
| 120 delete item; |
| 121 } |
| 122 |
116 protected: | 123 protected: |
117 DownloadStatusUpdater download_status_updater_; | 124 DownloadStatusUpdater download_status_updater_; |
118 | 125 |
119 private: | 126 private: |
120 scoped_refptr<DownloadIdFactory> id_factory_; | 127 scoped_refptr<DownloadIdFactory> id_factory_; |
121 MessageLoopForUI loop_; | 128 MessageLoopForUI loop_; |
122 // UI thread. | 129 // UI thread. |
123 content::TestBrowserThread ui_thread_; | 130 content::TestBrowserThread ui_thread_; |
124 testing::NiceMock<MockDelegate> delegate_; | 131 testing::NiceMock<MockDelegate> delegate_; |
125 std::vector<DownloadItem*> allocated_downloads_; | 132 std::set<DownloadItem*> allocated_downloads_; |
126 }; | 133 }; |
127 | 134 |
128 namespace { | 135 namespace { |
129 | 136 |
130 const int kDownloadChunkSize = 1000; | 137 const int kDownloadChunkSize = 1000; |
131 const int kDownloadSpeed = 1000; | 138 const int kDownloadSpeed = 1000; |
132 const int kDummyDBHandle = 10; | 139 const int kDummyDBHandle = 10; |
133 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 140 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
134 | 141 |
135 } // namespace | 142 } // namespace |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 274 DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
268 MockObserver observer(item); | 275 MockObserver observer(item); |
269 | 276 |
270 item->TogglePause(); | 277 item->TogglePause(); |
271 ASSERT_TRUE(observer.CheckUpdated()); | 278 ASSERT_TRUE(observer.CheckUpdated()); |
272 | 279 |
273 item->TogglePause(); | 280 item->TogglePause(); |
274 ASSERT_TRUE(observer.CheckUpdated()); | 281 ASSERT_TRUE(observer.CheckUpdated()); |
275 } | 282 } |
276 | 283 |
| 284 static char external_data_test_string[] = "External data test"; |
| 285 static int destructor_called = 0; |
| 286 |
| 287 class TestExternalData : public DownloadItem::ExternalData { |
| 288 public: |
| 289 int value; |
| 290 virtual ~TestExternalData() { |
| 291 destructor_called++; |
| 292 } |
| 293 }; |
| 294 |
| 295 TEST_F(DownloadItemTest, ExternalData) { |
| 296 DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 297 |
| 298 // Shouldn't be anything there before set. |
| 299 EXPECT_EQ(NULL, item->GetExternalData(&external_data_test_string)); |
| 300 |
| 301 TestExternalData* test1(new TestExternalData()); |
| 302 test1->value = 2; |
| 303 |
| 304 // Should be able to get back what you set. |
| 305 item->SetExternalData(&external_data_test_string, test1); |
| 306 TestExternalData* test_result = |
| 307 static_cast<TestExternalData*>( |
| 308 item->GetExternalData(&external_data_test_string)); |
| 309 EXPECT_EQ(test1, test_result); |
| 310 |
| 311 // Destructor should be called if value overwritten. New value |
| 312 // should then be retrievable. |
| 313 TestExternalData* test2(new TestExternalData()); |
| 314 test2->value = 3; |
| 315 EXPECT_EQ(0, destructor_called); |
| 316 item->SetExternalData(&external_data_test_string, test2); |
| 317 EXPECT_EQ(1, destructor_called); |
| 318 EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2), |
| 319 item->GetExternalData(&external_data_test_string)); |
| 320 |
| 321 // Overwriting with the same value shouldn't do anything. |
| 322 EXPECT_EQ(1, destructor_called); |
| 323 item->SetExternalData(&external_data_test_string, test2); |
| 324 EXPECT_EQ(1, destructor_called); |
| 325 EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2), |
| 326 item->GetExternalData(&external_data_test_string)); |
| 327 |
| 328 // Overwriting with NULL should result in destruction. |
| 329 item->SetExternalData(&external_data_test_string, NULL); |
| 330 EXPECT_EQ(2, destructor_called); |
| 331 |
| 332 // Destroying the download item should destroy the external data. |
| 333 |
| 334 TestExternalData* test3(new TestExternalData()); |
| 335 item->SetExternalData(&external_data_test_string, test3); |
| 336 EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test3), |
| 337 item->GetExternalData(&external_data_test_string)); |
| 338 DestroyDownloadItem(item); |
| 339 EXPECT_EQ(3, destructor_called); |
| 340 } |
| 341 |
277 TEST(MockDownloadItem, Compiles) { | 342 TEST(MockDownloadItem, Compiles) { |
278 MockDownloadItem mock_item; | 343 MockDownloadItem mock_item; |
279 } | 344 } |
OLD | NEW |