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

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: Fix nits. 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
« no previous file with comments | « chrome/browser/chromeos/drive/download_handler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_;
69 };
70
71 class DownloadHandlerTestDownloadItem : public content::MockDownloadItem {
72 public:
73 bool IsDone() const override { return is_done_; }
74
75 int64 GetTotalBytes() const override { return total_bytes_; }
76
77 int64 GetReceivedBytes() const override { return received_bytes_; }
78
79 bool is_done_ = false;
80 int64 total_bytes_ = 0;
81 int64 received_bytes_ = 0;
82 };
83
49 } // namespace 84 } // namespace
50 85
51 class DownloadHandlerTest : public testing::Test { 86 class DownloadHandlerTest : public testing::Test {
52 public: 87 public:
53 DownloadHandlerTest() 88 DownloadHandlerTest()
54 : download_manager_(new content::MockDownloadManager) {} 89 : download_manager_(new DownloadHandlerTestDownloadManager) {}
55 90
56 void SetUp() override { 91 void SetUp() override {
57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 92 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
58 93
59 // Set expectations for download item. 94 // Set expectations for download item.
60 EXPECT_CALL(download_item_, GetState()) 95 EXPECT_CALL(download_item_, GetState())
61 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS)); 96 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS));
62 97
63 download_handler_.reset(new DownloadHandler(&test_file_system_)); 98 download_handler_.reset(new DownloadHandler(&test_file_system_));
64 download_handler_->Initialize(download_manager_.get(), temp_dir_.path()); 99 download_handler_->Initialize(download_manager_.get(), temp_dir_.path());
100 download_handler_->SetFreeDiskSpaceDelayForTesting(
101 base::TimeDelta::FromMilliseconds(0));
65 } 102 }
66 103
67 protected: 104 protected:
68 base::ScopedTempDir temp_dir_; 105 base::ScopedTempDir temp_dir_;
69 content::TestBrowserThreadBundle thread_bundle_; 106 content::TestBrowserThreadBundle thread_bundle_;
70 TestingProfile profile_; 107 TestingProfile profile_;
71 scoped_ptr<content::MockDownloadManager> download_manager_; 108 scoped_ptr<DownloadHandlerTestDownloadManager> download_manager_;
109 scoped_ptr<DownloadHandlerTestDownloadManager> incognito_download_manager_;
72 DownloadHandlerTestFileSystem test_file_system_; 110 DownloadHandlerTestFileSystem test_file_system_;
73 scoped_ptr<DownloadHandler> download_handler_; 111 scoped_ptr<DownloadHandler> download_handler_;
74 content::MockDownloadItem download_item_; 112 content::MockDownloadItem download_item_;
75 }; 113 };
76 114
77 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) { 115 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) {
78 const base::FilePath non_drive_path(FILE_PATH_LITERAL("/foo/bar")); 116 const base::FilePath non_drive_path(FILE_PATH_LITERAL("/foo/bar"));
79 ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path)); 117 ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path));
80 118
81 // Call SubstituteDriveDownloadPath() 119 // Call SubstituteDriveDownloadPath()
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // Call CheckForFileExistence again. 227 // Call CheckForFileExistence again.
190 download_handler_->CheckForFileExistence( 228 download_handler_->CheckForFileExistence(
191 &download_item_, 229 &download_item_,
192 google_apis::test_util::CreateCopyResultCallback(&file_exists)); 230 google_apis::test_util::CreateCopyResultCallback(&file_exists));
193 content::RunAllBlockingPoolTasksUntilIdle(); 231 content::RunAllBlockingPoolTasksUntilIdle();
194 232
195 // Check the result. 233 // Check the result.
196 EXPECT_FALSE(file_exists); 234 EXPECT_FALSE(file_exists);
197 } 235 }
198 236
237 TEST_F(DownloadHandlerTest, FreeDiskSpace) {
238 // Add a download item to download manager.
239 DownloadHandlerTestDownloadItem download_item_a;
240 download_item_a.is_done_ = false;
241 download_item_a.total_bytes_ = 100;
242 download_item_a.received_bytes_ = 10;
243 download_manager_->test_downloads_.push_back(&download_item_a);
244
245 // Free disk space for download_item_a.
246 download_handler_->FreeDiskSpaceIfNeededImmediately();
247 ASSERT_EQ(1u,
248 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
249 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_,
250 test_file_system_.free_disk_space_if_needed_for_num_bytes_[0]);
251
252 // Confirm that FreeDiskSpaceIfNeeded is rate limited by calling it twice.
253 download_handler_->FreeDiskSpaceIfNeeded();
254 download_handler_->FreeDiskSpaceIfNeeded();
255 ASSERT_EQ(1u,
256 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
257
258 download_item_a.received_bytes_ = 20;
259
260 base::RunLoop().RunUntilIdle();
261 ASSERT_EQ(2u,
262 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
263 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_,
264 test_file_system_.free_disk_space_if_needed_for_num_bytes_[1]);
265
266 // Observe incognito download manager and add another download item.
267 // FreeDiskSpace should be called with considering both download items.
268 incognito_download_manager_.reset(new DownloadHandlerTestDownloadManager);
269 download_handler_->ObserveIncognitoDownloadManager(
270 incognito_download_manager_.get());
271
272 DownloadHandlerTestDownloadItem download_item_b;
273 download_item_b.is_done_ = false;
274 download_item_b.total_bytes_ = 200;
275 download_item_b.received_bytes_ = 0;
276 incognito_download_manager_->test_downloads_.push_back(&download_item_b);
277
278 download_item_a.received_bytes_ = 30;
279
280 download_handler_->FreeDiskSpaceIfNeededImmediately();
281 ASSERT_EQ(3u,
282 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
283 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_ +
284 download_item_b.total_bytes_ - download_item_b.received_bytes_,
285 test_file_system_.free_disk_space_if_needed_for_num_bytes_[2]);
286
287 // Free disk space after making both items completed. In this case
288 // FreeDiskSpace should be called with 0 byte to keep
289 // drive::internal::kMinFreeSpaceInBytes.
290 download_item_a.is_done_ = true;
291 download_item_b.is_done_ = true;
292
293 download_handler_->FreeDiskSpaceIfNeeded();
294 base::RunLoop().RunUntilIdle();
295
296 ASSERT_EQ(4u,
297 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
298 ASSERT_EQ(0, test_file_system_.free_disk_space_if_needed_for_num_bytes_[3]);
299 }
300
301 TEST_F(DownloadHandlerTest, CalculateRequestSpace) {
302 DownloadHandlerTestDownloadItem download_item_a;
303 download_item_a.is_done_ = false;
304 download_item_a.total_bytes_ = 100;
305 download_item_a.received_bytes_ = 0;
306
307 DownloadHandlerTestDownloadItem download_item_b;
308 download_item_b.is_done_ = false;
309 download_item_b.total_bytes_ = 200;
310 download_item_b.received_bytes_ = 10;
311
312 content::DownloadManager::DownloadVector downloads;
313 downloads.push_back(&download_item_a);
314 downloads.push_back(&download_item_b);
315
316 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_ +
317 download_item_b.total_bytes_ - download_item_b.received_bytes_,
318 download_handler_->CalculateRequestSpace(downloads));
319
320 download_item_a.received_bytes_ = 10;
321
322 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_ +
323 download_item_b.total_bytes_ - download_item_b.received_bytes_,
324 download_handler_->CalculateRequestSpace(downloads));
325
326 download_item_b.is_done_ = true;
327
328 // Since download_item_b is completed, it shouldn't be counted.
329 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_,
330 download_handler_->CalculateRequestSpace(downloads));
331
332 // Add unknown size download item.
333 DownloadHandlerTestDownloadItem download_item_c;
334 download_item_c.is_done_ = false;
335 download_item_c.total_bytes_ = 0;
336 downloads.push_back(&download_item_c);
337
338 // Unknown size download should be counted as 0 byte.
339 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_,
340 download_handler_->CalculateRequestSpace(downloads));
341
342 // Add another unknown size download item.
343 DownloadHandlerTestDownloadItem download_item_d;
344 download_item_d.is_done_ = false;
345 download_item_d.total_bytes_ = 0;
346 downloads.push_back(&download_item_d);
347
348 // Unknown size downloads should be counted as 0 byte.
349 ASSERT_EQ(download_item_a.total_bytes_ - download_item_a.received_bytes_,
350 download_handler_->CalculateRequestSpace(downloads));
351 }
352
199 } // namespace drive 353 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/download_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698