OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string> | 5 #include <string> |
| 6 #include <tr1/functional> |
6 | 7 |
7 #include "base/string_util.h" | 8 #include "base/string_util.h" |
8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
9 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
10 #include "chrome/browser/download/download_file.h" | 11 #include "chrome/browser/download/download_file.h" |
11 #include "chrome/browser/download/download_file_manager.h" | 12 #include "chrome/browser/download/download_file_manager.h" |
12 #include "chrome/browser/download/download_manager.h" | 13 #include "chrome/browser/download/download_manager.h" |
13 #include "chrome/browser/download/download_prefs.h" | 14 #include "chrome/browser/download/download_prefs.h" |
14 #include "chrome/browser/download/download_status_updater.h" | 15 #include "chrome/browser/download/download_status_updater.h" |
15 #include "chrome/browser/download/download_util.h" | 16 #include "chrome/browser/download/download_util.h" |
16 #include "chrome/browser/history/download_create_info.h" | 17 #include "chrome/browser/history/download_create_info.h" |
17 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
19 #include "chrome/test/testing_profile.h" | 20 #include "chrome/test/testing_profile.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gmock_mutant.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 class MockDownloadManager : public DownloadManager { | 24 class MockDownloadManager : public DownloadManager { |
25 public: | 25 public: |
26 explicit MockDownloadManager(DownloadStatusUpdater* updater) | 26 explicit MockDownloadManager(DownloadStatusUpdater* updater) |
27 : DownloadManager(updater) { | 27 : DownloadManager(updater) { |
28 } | 28 } |
29 | 29 |
30 // Override some functions. | 30 // Override some functions. |
31 virtual void UpdateHistoryForDownload(DownloadItem*) { } | 31 virtual void UpdateHistoryForDownload(DownloadItem*) { } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 202 } |
203 | 203 |
204 private: | 204 private: |
205 int renamed_count_; | 205 int renamed_count_; |
206 }; | 206 }; |
207 | 207 |
208 } // namespace | 208 } // namespace |
209 | 209 |
210 TEST_F(DownloadManagerTest, DownloadRenameTest) { | 210 TEST_F(DownloadManagerTest, DownloadRenameTest) { |
211 using ::testing::_; | 211 using ::testing::_; |
212 using ::testing::CreateFunctor; | |
213 using ::testing::Invoke; | 212 using ::testing::Invoke; |
214 using ::testing::Return; | 213 using ::testing::Return; |
| 214 using ::std::tr1::bind; |
| 215 using ::std::tr1::placeholders::_1; |
| 216 using ::std::tr1::placeholders::_2; |
215 | 217 |
216 BrowserThread file_thread(BrowserThread::FILE, &message_loop_); | 218 BrowserThread file_thread(BrowserThread::FILE, &message_loop_); |
217 | 219 |
218 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { | 220 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { |
219 // |info| will be destroyed in download_manager_. | 221 // |info| will be destroyed in download_manager_. |
220 DownloadCreateInfo* info(new DownloadCreateInfo); | 222 DownloadCreateInfo* info(new DownloadCreateInfo); |
221 info->download_id = static_cast<int>(i); | 223 info->download_id = static_cast<int>(i); |
222 info->prompt_user_for_save_location = false; | 224 info->prompt_user_for_save_location = false; |
223 info->is_dangerous = kDownloadRenameCases[i].is_dangerous; | 225 info->is_dangerous = kDownloadRenameCases[i].is_dangerous; |
224 FilePath new_path(kDownloadRenameCases[i].suggested_path); | 226 FilePath new_path(kDownloadRenameCases[i].suggested_path); |
225 | 227 |
226 MockDownloadFile* download(new MockDownloadFile(info)); | 228 MockDownloadFile* download(new MockDownloadFile(info)); |
227 AddDownloadToFileManager(info->download_id, download); | 229 AddDownloadToFileManager(info->download_id, download); |
228 | 230 |
229 // |download| is owned by DownloadFileManager. | 231 // |download| is owned by DownloadFileManager. |
230 ::testing::Mock::AllowLeak(download); | 232 ::testing::Mock::AllowLeak(download); |
231 EXPECT_CALL(*download, Destructed()).Times(1); | 233 EXPECT_CALL(*download, Destructed()).Times(1); |
232 | 234 |
233 if (kDownloadRenameCases[i].expected_rename_count == 1) { | 235 if (kDownloadRenameCases[i].expected_rename_count == 1) { |
234 EXPECT_CALL(*download, Rename(new_path, true)).WillOnce(Return(true)); | 236 EXPECT_CALL(*download, Rename(new_path, true)).WillOnce(Return(true)); |
235 } else { | 237 } else { |
236 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); | 238 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); |
237 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); | 239 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); |
| 240 // Use tr1 bind to replace the "Mutant" calls with gmock. Demonstrates |
| 241 // partial binding using placeholders. |
238 EXPECT_CALL(*download, Rename(_, _)) | 242 EXPECT_CALL(*download, Rename(_, _)) |
239 .WillOnce(testing::WithArgs<0, 1>(Invoke(CreateFunctor( | 243 .WillOnce(testing::WithArgs<0, 1>(Invoke( |
240 download, &MockDownloadFile::TestMultipleRename, | 244 bind(&MockDownloadFile::TestMultipleRename, |
241 1, false, crdownload)))) | 245 download, 1, false, crdownload, _1, _2)))) |
242 .WillOnce(testing::WithArgs<0, 1>(Invoke(CreateFunctor( | 246 .WillOnce(testing::WithArgs<0, 1>(Invoke( |
243 download, &MockDownloadFile::TestMultipleRename, | 247 bind(&MockDownloadFile::TestMultipleRename, |
244 2, true, new_path)))); | 248 download, 2, true, new_path, _1, _2)))); |
245 } | 249 } |
246 | 250 |
247 if (kDownloadRenameCases[i].will_delete_crdownload) | 251 if (kDownloadRenameCases[i].will_delete_crdownload) |
248 EXPECT_CALL(*download, DeleteCrDownload()).Times(1); | 252 EXPECT_CALL(*download, DeleteCrDownload()).Times(1); |
249 | 253 |
250 download_manager_->CreateDownloadItem(info); | 254 download_manager_->CreateDownloadItem(info); |
251 | 255 |
252 if (kDownloadRenameCases[i].finish_before_rename) { | 256 if (kDownloadRenameCases[i].finish_before_rename) { |
253 download_manager_->OnAllDataSaved(i, 1024); | 257 download_manager_->OnAllDataSaved(i, 1024); |
254 download_manager_->FileSelected(new_path, i, info); | 258 download_manager_->FileSelected(new_path, i, info); |
255 } else { | 259 } else { |
256 download_manager_->FileSelected(new_path, i, info); | 260 download_manager_->FileSelected(new_path, i, info); |
257 download_manager_->OnAllDataSaved(i, 1024); | 261 download_manager_->OnAllDataSaved(i, 1024); |
258 } | 262 } |
259 | 263 |
260 message_loop_.RunAllPending(); | 264 message_loop_.RunAllPending(); |
261 } | 265 } |
262 } | 266 } |
OLD | NEW |