| 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_vector.h" |
| 6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "chrome/browser/download/download_status_updater.h" | 9 #include "chrome/browser/download/download_status_updater.h" |
| 10 #include "content/public/test/mock_download_item.h" | 10 #include "content/public/test/mock_download_item.h" |
| 11 #include "content/public/test/mock_download_manager.h" | 11 #include "content/public/test/mock_download_manager.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 loop_.RunUntilIdle(); // Allow DownloadManager destruction. | 72 loop_.RunUntilIdle(); // Allow DownloadManager destruction. |
| 73 } | 73 } |
| 74 | 74 |
| 75 protected: | 75 protected: |
| 76 // Attach some number of DownloadManagers to the updater. | 76 // Attach some number of DownloadManagers to the updater. |
| 77 void SetupManagers(int manager_count) { | 77 void SetupManagers(int manager_count) { |
| 78 DCHECK_EQ(0U, managers_.size()); | 78 DCHECK_EQ(0U, managers_.size()); |
| 79 for (int i = 0; i < manager_count; ++i) { | 79 for (int i = 0; i < manager_count; ++i) { |
| 80 content::MockDownloadManager* mgr = | 80 content::MockDownloadManager* mgr = |
| 81 new StrictMock<content::MockDownloadManager>; | 81 new StrictMock<content::MockDownloadManager>; |
| 82 managers_.push_back(make_scoped_refptr(mgr)); | 82 managers_.push_back(mgr); |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 | 85 |
| 86 void SetObserver(content::DownloadManager::Observer* observer) { | 86 void SetObserver(content::DownloadManager::Observer* observer) { |
| 87 manager_observers_[manager_observer_index_] = observer; | 87 manager_observers_[manager_observer_index_] = observer; |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Hook the specified manager into the updater. | 90 // Hook the specified manager into the updater. |
| 91 void LinkManager(int i) { | 91 void LinkManager(int i) { |
| 92 content::MockDownloadManager* mgr = managers_[i].get(); | 92 content::MockDownloadManager* mgr = managers_[i]; |
| 93 manager_observer_index_ = i; | 93 manager_observer_index_ = i; |
| 94 while (manager_observers_.size() <= static_cast<size_t>(i)) { | 94 while (manager_observers_.size() <= static_cast<size_t>(i)) { |
| 95 manager_observers_.push_back(NULL); | 95 manager_observers_.push_back(NULL); |
| 96 } | 96 } |
| 97 EXPECT_CALL(*mgr, AddObserver(_)) | 97 EXPECT_CALL(*mgr, AddObserver(_)) |
| 98 .WillOnce(WithArg<0>(Invoke( | 98 .WillOnce(WithArg<0>(Invoke( |
| 99 this, &DownloadStatusUpdaterTest::SetObserver))); | 99 this, &DownloadStatusUpdaterTest::SetObserver))); |
| 100 updater_->AddManager(mgr); | 100 updater_->AddManager(mgr); |
| 101 } | 101 } |
| 102 | 102 |
| 103 // Add some number of Download items to a particular manager. | 103 // Add some number of Download items to a particular manager. |
| 104 void AddItems(int manager_index, int item_count, int in_progress_count) { | 104 void AddItems(int manager_index, int item_count, int in_progress_count) { |
| 105 DCHECK_GT(managers_.size(), static_cast<size_t>(manager_index)); | 105 DCHECK_GT(managers_.size(), static_cast<size_t>(manager_index)); |
| 106 content::MockDownloadManager* manager = managers_[manager_index].get(); | 106 content::MockDownloadManager* manager = managers_[manager_index]; |
| 107 | 107 |
| 108 if (manager_items_.size() <= static_cast<size_t>(manager_index)) | 108 if (manager_items_.size() <= static_cast<size_t>(manager_index)) |
| 109 manager_items_.resize(manager_index+1); | 109 manager_items_.resize(manager_index+1); |
| 110 | 110 |
| 111 std::vector<content::DownloadItem*> item_list; | 111 std::vector<content::DownloadItem*> item_list; |
| 112 for (int i = 0; i < item_count; ++i) { | 112 for (int i = 0; i < item_count; ++i) { |
| 113 content::MockDownloadItem* item = | 113 content::MockDownloadItem* item = |
| 114 new StrictMock<content::MockDownloadItem>; | 114 new StrictMock<content::MockDownloadItem>; |
| 115 content::DownloadItem::DownloadState state = | 115 content::DownloadItem::DownloadState state = |
| 116 i < in_progress_count ? content::DownloadItem::IN_PROGRESS | 116 i < in_progress_count ? content::DownloadItem::IN_PROGRESS |
| 117 : content::DownloadItem::CANCELLED; | 117 : content::DownloadItem::CANCELLED; |
| 118 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(state)); | 118 EXPECT_CALL(*item, GetState()).WillRepeatedly(Return(state)); |
| 119 EXPECT_CALL(*item, AddObserver(_)) | 119 EXPECT_CALL(*item, AddObserver(_)) |
| 120 .WillOnce(Return()); | 120 .WillOnce(Return()); |
| 121 manager_items_[manager_index].push_back(item); | 121 manager_items_[manager_index].push_back(item); |
| 122 } | 122 } |
| 123 EXPECT_CALL(*manager, GetAllDownloads(_)) | 123 EXPECT_CALL(*manager, GetAllDownloads(_)) |
| 124 .WillRepeatedly(SetArgPointee<0>(manager_items_[manager_index])); | 124 .WillRepeatedly(SetArgPointee<0>(manager_items_[manager_index])); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Return the specified manager. | 127 // Return the specified manager. |
| 128 content::MockDownloadManager* Manager(int manager_index) { | 128 content::MockDownloadManager* Manager(int manager_index) { |
| 129 DCHECK_GT(managers_.size(), static_cast<size_t>(manager_index)); | 129 DCHECK_GT(managers_.size(), static_cast<size_t>(manager_index)); |
| 130 return managers_[manager_index].get(); | 130 return managers_[manager_index]; |
| 131 } | 131 } |
| 132 | 132 |
| 133 // Return the specified item. | 133 // Return the specified item. |
| 134 content::MockDownloadItem* Item(int manager_index, int item_index) { | 134 content::MockDownloadItem* Item(int manager_index, int item_index) { |
| 135 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index)); | 135 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index)); |
| 136 DCHECK_GT(manager_items_[manager_index].size(), | 136 DCHECK_GT(manager_items_[manager_index].size(), |
| 137 static_cast<size_t>(item_index)); | 137 static_cast<size_t>(item_index)); |
| 138 // All DownloadItems in manager_items_ are MockDownloadItems. | 138 // All DownloadItems in manager_items_ are MockDownloadItems. |
| 139 return static_cast<content::MockDownloadItem*>( | 139 return static_cast<content::MockDownloadItem*>( |
| 140 manager_items_[manager_index][item_index]); | 140 manager_items_[manager_index][item_index]); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 156 // Transition specified item to completed. | 156 // Transition specified item to completed. |
| 157 void CompleteItem(int manager_index, int item_index) { | 157 void CompleteItem(int manager_index, int item_index) { |
| 158 content::MockDownloadItem* item(Item(manager_index, item_index)); | 158 content::MockDownloadItem* item(Item(manager_index, item_index)); |
| 159 EXPECT_CALL(*item, GetState()) | 159 EXPECT_CALL(*item, GetState()) |
| 160 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | 160 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); |
| 161 updater_->OnDownloadUpdated(managers_[manager_index], item); | 161 updater_->OnDownloadUpdated(managers_[manager_index], item); |
| 162 } | 162 } |
| 163 | 163 |
| 164 // Verify and clear all mocks expectations. | 164 // Verify and clear all mocks expectations. |
| 165 void VerifyAndClearExpectations() { | 165 void VerifyAndClearExpectations() { |
| 166 for (std::vector<scoped_refptr<content::MockDownloadManager> >::iterator it | 166 for (ScopedVector<content::MockDownloadManager>::iterator it |
| 167 = managers_.begin(); it != managers_.end(); ++it) | 167 = managers_.begin(); it != managers_.end(); ++it) |
| 168 Mock::VerifyAndClearExpectations(it->get()); | 168 Mock::VerifyAndClearExpectations(*it); |
| 169 for (std::vector<Items>::iterator it = manager_items_.begin(); | 169 for (std::vector<Items>::iterator it = manager_items_.begin(); |
| 170 it != manager_items_.end(); ++it) | 170 it != manager_items_.end(); ++it) |
| 171 for (Items::iterator sit = it->begin(); sit != it->end(); ++sit) | 171 for (Items::iterator sit = it->begin(); sit != it->end(); ++sit) |
| 172 Mock::VerifyAndClearExpectations(*sit); | 172 Mock::VerifyAndClearExpectations(*sit); |
| 173 } | 173 } |
| 174 | 174 |
| 175 std::vector<scoped_refptr<content::MockDownloadManager> > managers_; | 175 ScopedVector<content::MockDownloadManager> managers_; |
| 176 // DownloadItem so that it can be assigned to the result of SearchDownloads. | 176 // DownloadItem so that it can be assigned to the result of SearchDownloads. |
| 177 typedef std::vector<content::DownloadItem*> Items; | 177 typedef std::vector<content::DownloadItem*> Items; |
| 178 std::vector<Items> manager_items_; | 178 std::vector<Items> manager_items_; |
| 179 int manager_observer_index_; | 179 int manager_observer_index_; |
| 180 | 180 |
| 181 std::vector<content::DownloadManager::Observer*> manager_observers_; | 181 std::vector<content::DownloadManager::Observer*> manager_observers_; |
| 182 | 182 |
| 183 // Pointer so we can verify that destruction triggers appropriate | 183 // Pointer so we can verify that destruction triggers appropriate |
| 184 // changes. | 184 // changes. |
| 185 TestDownloadStatusUpdater *updater_; | 185 TestDownloadStatusUpdater *updater_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 203 | 203 |
| 204 // Test updater with null manager. | 204 // Test updater with null manager. |
| 205 TEST_F(DownloadStatusUpdaterTest, OneManagerNoItems) { | 205 TEST_F(DownloadStatusUpdaterTest, OneManagerNoItems) { |
| 206 SetupManagers(1); | 206 SetupManagers(1); |
| 207 AddItems(0, 0, 0); | 207 AddItems(0, 0, 0); |
| 208 LinkManager(0); | 208 LinkManager(0); |
| 209 VerifyAndClearExpectations(); | 209 VerifyAndClearExpectations(); |
| 210 | 210 |
| 211 float progress = -1; | 211 float progress = -1; |
| 212 int download_count = -1; | 212 int download_count = -1; |
| 213 EXPECT_CALL(*managers_[0].get(), GetAllDownloads(_)) | 213 EXPECT_CALL(*managers_[0], GetAllDownloads(_)) |
| 214 .WillRepeatedly(SetArgPointee<0>(manager_items_[0])); | 214 .WillRepeatedly(SetArgPointee<0>(manager_items_[0])); |
| 215 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 215 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
| 216 EXPECT_FLOAT_EQ(0.0f, progress); | 216 EXPECT_FLOAT_EQ(0.0f, progress); |
| 217 EXPECT_EQ(0, download_count); | 217 EXPECT_EQ(0, download_count); |
| 218 } | 218 } |
| 219 | 219 |
| 220 // Test updater with non-null manager, including transition an item to | 220 // Test updater with non-null manager, including transition an item to |
| 221 // |content::DownloadItem::COMPLETE| and adding a new item. | 221 // |content::DownloadItem::COMPLETE| and adding a new item. |
| 222 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) { | 222 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) { |
| 223 SetupManagers(1); | 223 SetupManagers(1); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 SetItemValues(0, 0, 10, 20, false); | 340 SetItemValues(0, 0, 10, 20, false); |
| 341 SetItemValues(0, 1, 50, 60, false); | 341 SetItemValues(0, 1, 50, 60, false); |
| 342 SetItemValues(1, 0, 80, 90, false); | 342 SetItemValues(1, 0, 80, 90, false); |
| 343 | 343 |
| 344 float progress = -1; | 344 float progress = -1; |
| 345 int download_count = -1; | 345 int download_count = -1; |
| 346 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 346 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
| 347 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress); | 347 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress); |
| 348 EXPECT_EQ(3, download_count); | 348 EXPECT_EQ(3, download_count); |
| 349 } | 349 } |
| OLD | NEW |