| 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 EXPECT_CALL(*item, IsInProgress()) | 115 EXPECT_CALL(*item, IsInProgress()) |
| 116 .WillRepeatedly(Return(i < in_progress_count)); | 116 .WillRepeatedly(Return(i < in_progress_count)); |
| 117 EXPECT_CALL(*item, AddObserver(_)) | 117 EXPECT_CALL(*item, AddObserver(_)) |
| 118 .WillOnce(Return()); | 118 .WillOnce(Return()); |
| 119 manager_items_[manager_index].push_back(item); | 119 manager_items_[manager_index].push_back(item); |
| 120 } | 120 } |
| 121 EXPECT_CALL(*manager, GetAllDownloads(_)) | 121 EXPECT_CALL(*manager, GetAllDownloads(_)) |
| 122 .WillRepeatedly(SetArgPointee<0>(manager_items_[manager_index])); | 122 .WillRepeatedly(SetArgPointee<0>(manager_items_[manager_index])); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Return the specified manager. | 125 // Return the specified manager. |
| 126 content::MockDownloadManager* Manager(int manager_index) { | 126 content::MockDownloadManager* Manager(int manager_index) { |
| 127 DCHECK_GT(managers_.size(), static_cast<size_t>(manager_index)); | 127 DCHECK_GT(managers_.size(), static_cast<size_t>(manager_index)); |
| 128 return managers_[manager_index].get(); | 128 return managers_[manager_index]; |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Return the specified item. | 131 // Return the specified item. |
| 132 content::MockDownloadItem* Item(int manager_index, int item_index) { | 132 content::MockDownloadItem* Item(int manager_index, int item_index) { |
| 133 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index)); | 133 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index)); |
| 134 DCHECK_GT(manager_items_[manager_index].size(), | 134 DCHECK_GT(manager_items_[manager_index].size(), |
| 135 static_cast<size_t>(item_index)); | 135 static_cast<size_t>(item_index)); |
| 136 // All DownloadItems in manager_items_ are MockDownloadItems. | 136 // All DownloadItems in manager_items_ are MockDownloadItems. |
| 137 return static_cast<content::MockDownloadItem*>( | 137 return static_cast<content::MockDownloadItem*>( |
| 138 manager_items_[manager_index][item_index]); | 138 manager_items_[manager_index][item_index]); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 154 // Transition specified item to completed. | 154 // Transition specified item to completed. |
| 155 void CompleteItem(int manager_index, int item_index) { | 155 void CompleteItem(int manager_index, int item_index) { |
| 156 content::MockDownloadItem* item(Item(manager_index, item_index)); | 156 content::MockDownloadItem* item(Item(manager_index, item_index)); |
| 157 EXPECT_CALL(*item, IsInProgress()) | 157 EXPECT_CALL(*item, IsInProgress()) |
| 158 .WillRepeatedly(Return(false)); | 158 .WillRepeatedly(Return(false)); |
| 159 updater_->OnDownloadUpdated(managers_[manager_index], item); | 159 updater_->OnDownloadUpdated(managers_[manager_index], item); |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Verify and clear all mocks expectations. | 162 // Verify and clear all mocks expectations. |
| 163 void VerifyAndClearExpectations() { | 163 void VerifyAndClearExpectations() { |
| 164 for (std::vector<scoped_refptr<content::MockDownloadManager> >::iterator it | 164 for (ScopedVector<content::MockDownloadManager>::iterator it |
| 165 = managers_.begin(); it != managers_.end(); ++it) | 165 = managers_.begin(); it != managers_.end(); ++it) |
| 166 Mock::VerifyAndClearExpectations(it->get()); | 166 Mock::VerifyAndClearExpectations(*it); |
| 167 for (std::vector<Items>::iterator it = manager_items_.begin(); | 167 for (std::vector<Items>::iterator it = manager_items_.begin(); |
| 168 it != manager_items_.end(); ++it) | 168 it != manager_items_.end(); ++it) |
| 169 for (Items::iterator sit = it->begin(); sit != it->end(); ++sit) | 169 for (Items::iterator sit = it->begin(); sit != it->end(); ++sit) |
| 170 Mock::VerifyAndClearExpectations(*sit); | 170 Mock::VerifyAndClearExpectations(*sit); |
| 171 } | 171 } |
| 172 | 172 |
| 173 std::vector<scoped_refptr<content::MockDownloadManager> > managers_; | 173 ScopedVector<content::MockDownloadManager> managers_; |
| 174 // DownloadItem so that it can be assigned to the result of SearchDownloads. | 174 // DownloadItem so that it can be assigned to the result of SearchDownloads. |
| 175 typedef std::vector<content::DownloadItem*> Items; | 175 typedef std::vector<content::DownloadItem*> Items; |
| 176 std::vector<Items> manager_items_; | 176 std::vector<Items> manager_items_; |
| 177 int manager_observer_index_; | 177 int manager_observer_index_; |
| 178 | 178 |
| 179 std::vector<content::DownloadManager::Observer*> manager_observers_; | 179 std::vector<content::DownloadManager::Observer*> manager_observers_; |
| 180 | 180 |
| 181 // Pointer so we can verify that destruction triggers appropriate | 181 // Pointer so we can verify that destruction triggers appropriate |
| 182 // changes. | 182 // changes. |
| 183 TestDownloadStatusUpdater *updater_; | 183 TestDownloadStatusUpdater *updater_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 201 | 201 |
| 202 // Test updater with null manager. | 202 // Test updater with null manager. |
| 203 TEST_F(DownloadStatusUpdaterTest, OneManagerNoItems) { | 203 TEST_F(DownloadStatusUpdaterTest, OneManagerNoItems) { |
| 204 SetupManagers(1); | 204 SetupManagers(1); |
| 205 AddItems(0, 0, 0); | 205 AddItems(0, 0, 0); |
| 206 LinkManager(0); | 206 LinkManager(0); |
| 207 VerifyAndClearExpectations(); | 207 VerifyAndClearExpectations(); |
| 208 | 208 |
| 209 float progress = -1; | 209 float progress = -1; |
| 210 int download_count = -1; | 210 int download_count = -1; |
| 211 EXPECT_CALL(*managers_[0].get(), GetAllDownloads(_)) | 211 EXPECT_CALL(*managers_[0], GetAllDownloads(_)) |
| 212 .WillRepeatedly(SetArgPointee<0>(manager_items_[0])); | 212 .WillRepeatedly(SetArgPointee<0>(manager_items_[0])); |
| 213 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 213 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
| 214 EXPECT_FLOAT_EQ(0.0f, progress); | 214 EXPECT_FLOAT_EQ(0.0f, progress); |
| 215 EXPECT_EQ(0, download_count); | 215 EXPECT_EQ(0, download_count); |
| 216 } | 216 } |
| 217 | 217 |
| 218 // Test updater with non-null manager, including transition an item to | 218 // Test updater with non-null manager, including transition an item to |
| 219 // |content::DownloadItem::COMPLETE| and adding a new item. | 219 // |content::DownloadItem::COMPLETE| and adding a new item. |
| 220 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) { | 220 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) { |
| 221 SetupManagers(1); | 221 SetupManagers(1); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 SetItemValues(0, 0, 10, 20, false); | 338 SetItemValues(0, 0, 10, 20, false); |
| 339 SetItemValues(0, 1, 50, 60, false); | 339 SetItemValues(0, 1, 50, 60, false); |
| 340 SetItemValues(1, 0, 80, 90, false); | 340 SetItemValues(1, 0, 80, 90, false); |
| 341 | 341 |
| 342 float progress = -1; | 342 float progress = -1; |
| 343 int download_count = -1; | 343 int download_count = -1; |
| 344 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 344 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
| 345 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress); | 345 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress); |
| 346 EXPECT_EQ(3, download_count); | 346 EXPECT_EQ(3, download_count); |
| 347 } | 347 } |
| OLD | NEW |