Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: chrome/browser/chromeos/drive/download_handler_unittest.cc

Issue 1414753005: Evict drive cache with file download from website if necessary. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Correct spell. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "chrome/browser/chromeos/drive/download_handler.h" 5 #include "chrome/browser/chromeos/drive/download_handler.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
8 #include "chrome/browser/chromeos/drive/file_system_util.h" 9 #include "chrome/browser/chromeos/drive/file_system_util.h"
9 #include "chrome/test/base/testing_profile.h" 10 #include "chrome/test/base/testing_profile.h"
10 #include "components/drive/dummy_file_system.h" 11 #include "components/drive/dummy_file_system.h"
11 #include "components/drive/file_system_core_util.h" 12 #include "components/drive/file_system_core_util.h"
12 #include "content/public/test/mock_download_item.h" 13 #include "content/public/test/mock_download_item.h"
13 #include "content/public/test/mock_download_manager.h" 14 #include "content/public/test/mock_download_manager.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 15 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_utils.h" 16 #include "content/public/test/test_utils.h"
16 #include "google_apis/drive/test_util.h" 17 #include "google_apis/drive/test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 17 matching lines...) Expand all
35 error_ == FILE_ERROR_OK ? new ResourceEntry : NULL)); 36 error_ == FILE_ERROR_OK ? new ResourceEntry : NULL));
36 } 37 }
37 38
38 void CreateDirectory(const base::FilePath& directory_path, 39 void CreateDirectory(const base::FilePath& directory_path,
39 bool is_exclusive, 40 bool is_exclusive,
40 bool is_recursive, 41 bool is_recursive,
41 const FileOperationCallback& callback) override { 42 const FileOperationCallback& callback) override {
42 callback.Run(error_); 43 callback.Run(error_);
43 } 44 }
44 45
46 void FreeDiskSpaceIfNeededFor(
47 int64 num_bytes,
48 const FreeDiskSpaceCallback& callback) override {
49 free_disk_space_if_needed_for_num_bytes_.push_back(num_bytes);
50 callback.Run(true);
51 }
52
53 std::vector<int64> free_disk_space_if_needed_for_num_bytes_;
54
45 private: 55 private:
46 FileError error_; 56 FileError error_;
47 }; 57 };
48 58
59 class DownloadHandlerTestDownloadManager : public content::MockDownloadManager {
60 public:
61 void GetAllDownloads(
62 content::DownloadManager::DownloadVector* downloads) override {
63 for (auto* test_download : test_downloads) {
64 downloads->push_back(test_download);
65 }
66 }
67
68 content::DownloadManager::DownloadVector test_downloads;
hashimoto 2015/10/30 09:33:43 nit: test_downloads_;
yawano 2015/11/02 07:26:20 Done.
69 };
70
49 } // namespace 71 } // namespace
50 72
51 class DownloadHandlerTest : public testing::Test { 73 class DownloadHandlerTest : public testing::Test {
52 public: 74 public:
53 DownloadHandlerTest() 75 DownloadHandlerTest()
54 : download_manager_(new content::MockDownloadManager) {} 76 : download_manager_(new DownloadHandlerTestDownloadManager) {}
55 77
56 void SetUp() override { 78 void SetUp() override {
57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
58 80
59 // Set expectations for download item. 81 // Set expectations for download item.
60 EXPECT_CALL(download_item_, GetState()) 82 EXPECT_CALL(download_item_, GetState())
61 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS)); 83 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS));
62 84
63 download_handler_.reset(new DownloadHandler(&test_file_system_)); 85 download_handler_.reset(new DownloadHandler(&test_file_system_));
64 download_handler_->Initialize(download_manager_.get(), temp_dir_.path()); 86 download_handler_->Initialize(download_manager_.get(), temp_dir_.path());
87 download_handler_->SetFreeDiskSpaceDelayForTesting(
88 base::TimeDelta::FromMilliseconds(0));
65 } 89 }
66 90
67 protected: 91 protected:
68 base::ScopedTempDir temp_dir_; 92 base::ScopedTempDir temp_dir_;
69 content::TestBrowserThreadBundle thread_bundle_; 93 content::TestBrowserThreadBundle thread_bundle_;
70 TestingProfile profile_; 94 TestingProfile profile_;
71 scoped_ptr<content::MockDownloadManager> download_manager_; 95 scoped_ptr<DownloadHandlerTestDownloadManager> download_manager_;
96 scoped_ptr<DownloadHandlerTestDownloadManager> incognito_download_manager_;
72 DownloadHandlerTestFileSystem test_file_system_; 97 DownloadHandlerTestFileSystem test_file_system_;
73 scoped_ptr<DownloadHandler> download_handler_; 98 scoped_ptr<DownloadHandler> download_handler_;
74 content::MockDownloadItem download_item_; 99 content::MockDownloadItem download_item_;
75 }; 100 };
76 101
77 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) { 102 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) {
78 const base::FilePath non_drive_path(FILE_PATH_LITERAL("/foo/bar")); 103 const base::FilePath non_drive_path(FILE_PATH_LITERAL("/foo/bar"));
79 ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path)); 104 ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path));
80 105
81 // Call SubstituteDriveDownloadPath() 106 // Call SubstituteDriveDownloadPath()
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // Call CheckForFileExistence again. 214 // Call CheckForFileExistence again.
190 download_handler_->CheckForFileExistence( 215 download_handler_->CheckForFileExistence(
191 &download_item_, 216 &download_item_,
192 google_apis::test_util::CreateCopyResultCallback(&file_exists)); 217 google_apis::test_util::CreateCopyResultCallback(&file_exists));
193 content::RunAllBlockingPoolTasksUntilIdle(); 218 content::RunAllBlockingPoolTasksUntilIdle();
194 219
195 // Check the result. 220 // Check the result.
196 EXPECT_FALSE(file_exists); 221 EXPECT_FALSE(file_exists);
197 } 222 }
198 223
224 TEST_F(DownloadHandlerTest, FreeDiskSpace) {
225 // Add a download item to download manager.
226 content::MockDownloadItem download_item_a;
hashimoto 2015/10/30 09:33:43 Could you implement something like DownloadHandler
yawano 2015/11/02 07:26:20 Done.
227 EXPECT_CALL(download_item_a, IsDone())
228 .WillOnce(testing::Return(false))
229 .WillOnce(testing::Return(false))
230 .WillOnce(testing::Return(false))
231 .WillRepeatedly(testing::Return(true));
232 EXPECT_CALL(download_item_a, GetTotalBytes())
233 .WillRepeatedly(testing::Return(100));
234 EXPECT_CALL(download_item_a, GetReceivedBytes())
235 .WillOnce(testing::Return(10))
236 .WillOnce(testing::Return(20))
237 .WillOnce(testing::Return(30));
238
239 download_manager_->test_downloads.push_back(&download_item_a);
240
241 download_handler_->FreeDiskSpaceIfNeededImmediately();
hashimoto 2015/10/30 09:33:43 Please describe the intention of each FreeDiskSpac
yawano 2015/11/02 07:26:20 Done.
242 ASSERT_EQ(1u,
243 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
244 ASSERT_EQ(90, test_file_system_.free_disk_space_if_needed_for_num_bytes_[0]);
245
246 // This call should be delayed.
247 download_handler_->FreeDiskSpaceIfNeeded();
248 ASSERT_EQ(1u,
249 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
250
251 base::RunLoop().RunUntilIdle();
252 ASSERT_EQ(2u,
253 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
254 ASSERT_EQ(80, test_file_system_.free_disk_space_if_needed_for_num_bytes_[1]);
255
256 // Observe incognito download manager and add a download item.
257 incognito_download_manager_.reset(new DownloadHandlerTestDownloadManager);
258 download_handler_->ObserveIncognitoDownloadManager(
259 incognito_download_manager_.get());
260
261 content::MockDownloadItem download_item_b;
262 EXPECT_CALL(download_item_b, IsDone())
263 .WillOnce(testing::Return(false))
264 .WillRepeatedly(testing::Return(true));
265 EXPECT_CALL(download_item_b, GetTotalBytes())
266 .WillRepeatedly(testing::Return(200));
267 EXPECT_CALL(download_item_b, GetReceivedBytes())
268 .WillRepeatedly(testing::Return(0));
269
270 incognito_download_manager_->test_downloads.push_back(&download_item_b);
271
272 download_handler_->FreeDiskSpaceIfNeededImmediately();
273 ASSERT_EQ(3u,
274 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
275 ASSERT_EQ(70 + 200,
276 test_file_system_.free_disk_space_if_needed_for_num_bytes_[2]);
277
278 download_handler_->FreeDiskSpaceIfNeeded();
279 base::RunLoop().RunUntilIdle();
280
281 // FreeDiskSpace should be called with 0 bytes to keep
282 // drive::internal::kMinFreeSpaceInBytes.
283 ASSERT_EQ(4u,
284 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
285 ASSERT_EQ(0, test_file_system_.free_disk_space_if_needed_for_num_bytes_[3]);
286 }
287
288 TEST_F(DownloadHandlerTest, CalculateRequestSpace) {
289 content::MockDownloadItem download_item_a;
290 content::MockDownloadItem download_item_b;
291
292 EXPECT_CALL(download_item_a, IsDone()).WillRepeatedly(testing::Return(false));
293 EXPECT_CALL(download_item_a, GetTotalBytes())
294 .WillRepeatedly(testing::Return(100));
295 EXPECT_CALL(download_item_a, GetReceivedBytes())
296 .WillOnce(testing::Return(0))
297 .WillRepeatedly(testing::Return(10));
298 EXPECT_CALL(download_item_b, IsDone())
299 .WillOnce(testing::Return(false))
300 .WillOnce(testing::Return(false))
301 .WillRepeatedly(testing::Return(true));
302 EXPECT_CALL(download_item_b, GetTotalBytes())
303 .WillRepeatedly(testing::Return(200));
304 EXPECT_CALL(download_item_b, GetReceivedBytes())
305 .WillRepeatedly(testing::Return(10));
306
307 content::DownloadManager::DownloadVector downloads;
308 downloads.push_back(&download_item_a);
309 downloads.push_back(&download_item_b);
310
311 ASSERT_EQ(290, download_handler_->CalculateRequestSpace(downloads));
hashimoto 2015/10/30 09:33:43 Please describe the intention of each CalculateReq
yawano 2015/11/02 07:26:20 Done.
312 ASSERT_EQ(280, download_handler_->CalculateRequestSpace(downloads));
313 ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
314
315 // Add unknown size download item.
316 content::MockDownloadItem download_item_c;
317 EXPECT_CALL(download_item_c, IsDone()).WillRepeatedly(testing::Return(false));
318 EXPECT_CALL(download_item_c, GetTotalBytes())
319 .WillRepeatedly(testing::Return(0));
320 downloads.push_back(&download_item_c);
321
322 // Unknown size download should be counted as 0 byte.
323 ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
324
325 // Add another unknown size download item.
326 content::MockDownloadItem download_item_d;
327 EXPECT_CALL(download_item_d, IsDone()).WillRepeatedly(testing::Return(false));
328 EXPECT_CALL(download_item_d, GetTotalBytes())
329 .WillRepeatedly(testing::Return(0));
330 downloads.push_back(&download_item_d);
331
332 ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
333 }
334
199 } // namespace drive 335 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698