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 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/prebind.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; |
215 | 214 |
216 BrowserThread file_thread(BrowserThread::FILE, &message_loop_); | 215 BrowserThread file_thread(BrowserThread::FILE, &message_loop_); |
217 | 216 |
218 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { | 217 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { |
219 // |info| will be destroyed in download_manager_. | 218 // |info| will be destroyed in download_manager_. |
220 DownloadCreateInfo* info(new DownloadCreateInfo); | 219 DownloadCreateInfo* info(new DownloadCreateInfo); |
221 info->download_id = static_cast<int>(i); | 220 info->download_id = static_cast<int>(i); |
222 info->prompt_user_for_save_location = false; | 221 info->prompt_user_for_save_location = false; |
223 info->is_dangerous = kDownloadRenameCases[i].is_dangerous; | 222 info->is_dangerous = kDownloadRenameCases[i].is_dangerous; |
224 FilePath new_path(kDownloadRenameCases[i].suggested_path); | 223 FilePath new_path(kDownloadRenameCases[i].suggested_path); |
225 | 224 |
226 MockDownloadFile* download(new MockDownloadFile(info)); | 225 MockDownloadFile* download(new MockDownloadFile(info)); |
227 AddDownloadToFileManager(info->download_id, download); | 226 AddDownloadToFileManager(info->download_id, download); |
228 | 227 |
229 // |download| is owned by DownloadFileManager. | 228 // |download| is owned by DownloadFileManager. |
230 ::testing::Mock::AllowLeak(download); | 229 ::testing::Mock::AllowLeak(download); |
231 EXPECT_CALL(*download, Destructed()).Times(1); | 230 EXPECT_CALL(*download, Destructed()).Times(1); |
232 | 231 |
233 if (kDownloadRenameCases[i].expected_rename_count == 1) { | 232 if (kDownloadRenameCases[i].expected_rename_count == 1) { |
234 EXPECT_CALL(*download, Rename(new_path, true)).WillOnce(Return(true)); | 233 EXPECT_CALL(*download, Rename(new_path, true)).WillOnce(Return(true)); |
235 } else { | 234 } else { |
236 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); | 235 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); |
237 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); | 236 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); |
| 237 // Use tr1 bind to replace the "Mutant" calls with gmock. Demonstrates |
| 238 // partial binding using placeholders. |
238 EXPECT_CALL(*download, Rename(_, _)) | 239 EXPECT_CALL(*download, Rename(_, _)) |
239 .WillOnce(testing::WithArgs<0, 1>(Invoke(CreateFunctor( | 240 .WillOnce(testing::WithArgs<0, 1>(Invoke( |
240 download, &MockDownloadFile::TestMultipleRename, | 241 base::Prebind(&MockDownloadFile::TestMultipleRename, |
241 1, false, crdownload)))) | 242 base::Unretained(download), 1, false, crdownload)))) |
242 .WillOnce(testing::WithArgs<0, 1>(Invoke(CreateFunctor( | 243 .WillOnce(testing::WithArgs<0, 1>(Invoke( |
243 download, &MockDownloadFile::TestMultipleRename, | 244 base::Prebind(&MockDownloadFile::TestMultipleRename, |
244 2, true, new_path)))); | 245 base::Unretained(download), 2, true, ::std::tr1::ref
(new_path))))); |
245 } | 246 } |
246 | 247 |
247 if (kDownloadRenameCases[i].will_delete_crdownload) | 248 if (kDownloadRenameCases[i].will_delete_crdownload) |
248 EXPECT_CALL(*download, DeleteCrDownload()).Times(1); | 249 EXPECT_CALL(*download, DeleteCrDownload()).Times(1); |
249 | 250 |
250 download_manager_->CreateDownloadItem(info); | 251 download_manager_->CreateDownloadItem(info); |
251 | 252 |
252 if (kDownloadRenameCases[i].finish_before_rename) { | 253 if (kDownloadRenameCases[i].finish_before_rename) { |
253 download_manager_->OnAllDataSaved(i, 1024); | 254 download_manager_->OnAllDataSaved(i, 1024); |
254 download_manager_->FileSelected(new_path, i, info); | 255 download_manager_->FileSelected(new_path, i, info); |
255 } else { | 256 } else { |
256 download_manager_->FileSelected(new_path, i, info); | 257 download_manager_->FileSelected(new_path, i, info); |
257 download_manager_->OnAllDataSaved(i, 1024); | 258 download_manager_->OnAllDataSaved(i, 1024); |
258 } | 259 } |
259 | 260 |
260 message_loop_.RunAllPending(); | 261 message_loop_.RunAllPending(); |
261 } | 262 } |
262 } | 263 } |
OLD | NEW |