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

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: Address comments. 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_;
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(90, test_file_system_.free_disk_space_if_needed_for_num_bytes_[0]);
hashimoto 2015/11/02 20:57:59 nit: How about replacing "90" with download_item_a
yawano 2015/11/05 03:51:26 Done.
250
251 // Confirm that FreeDiskSpaceIfNeeded is rate limited by calling it twice.
252 download_handler_->FreeDiskSpaceIfNeeded();
253 download_handler_->FreeDiskSpaceIfNeeded();
254 ASSERT_EQ(1u,
255 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
256
257 download_item_a.received_bytes_ = 20;
258
259 base::RunLoop().RunUntilIdle();
260 ASSERT_EQ(2u,
261 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
262 ASSERT_EQ(80, test_file_system_.free_disk_space_if_needed_for_num_bytes_[1]);
263
264 // Observe incognito download manager and add another download item.
265 // FreeDiskSpace should be called with considering both download items.
266 incognito_download_manager_.reset(new DownloadHandlerTestDownloadManager);
267 download_handler_->ObserveIncognitoDownloadManager(
268 incognito_download_manager_.get());
269
270 DownloadHandlerTestDownloadItem download_item_b;
271 download_item_b.is_done_ = false;
272 download_item_b.total_bytes_ = 200;
273 download_item_b.received_bytes_ = 0;
274 incognito_download_manager_->test_downloads_.push_back(&download_item_b);
275
276 download_item_a.received_bytes_ = 30;
277
278 download_handler_->FreeDiskSpaceIfNeededImmediately();
279 ASSERT_EQ(3u,
280 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
281 ASSERT_EQ(70 + 200,
282 test_file_system_.free_disk_space_if_needed_for_num_bytes_[2]);
283
284 // Free disk space after making both items completed. In this case
285 // FreeDiskSpace should be called with 0 byte to keep
286 // drive::internal::kMinFreeSpaceInBytes.
287 download_item_a.is_done_ = true;
288 download_item_b.is_done_ = true;
289
290 download_handler_->FreeDiskSpaceIfNeeded();
291 base::RunLoop().RunUntilIdle();
292
293 ASSERT_EQ(4u,
294 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
295 ASSERT_EQ(0, test_file_system_.free_disk_space_if_needed_for_num_bytes_[3]);
296 }
297
298 TEST_F(DownloadHandlerTest, CalculateRequestSpace) {
299 DownloadHandlerTestDownloadItem download_item_a;
300 download_item_a.is_done_ = false;
301 download_item_a.total_bytes_ = 100;
302 download_item_a.received_bytes_ = 0;
303
304 DownloadHandlerTestDownloadItem download_item_b;
305 download_item_b.is_done_ = false;
306 download_item_b.total_bytes_ = 200;
307 download_item_b.received_bytes_ = 10;
308
309 content::DownloadManager::DownloadVector downloads;
310 downloads.push_back(&download_item_a);
311 downloads.push_back(&download_item_b);
312
313 // 290 = 100 (download_item_a) + 190 (download_item_b)
hashimoto 2015/11/02 20:57:59 nit:You can write this relationship directly into
yawano 2015/11/05 03:51:25 Done.
314 ASSERT_EQ(290, download_handler_->CalculateRequestSpace(downloads));
315
316 download_item_a.received_bytes_ = 10;
317
318 // 280 = 90 (download_item_a) + 190 (download_item_b)
319 ASSERT_EQ(280, download_handler_->CalculateRequestSpace(downloads));
320
321 download_item_b.is_done_ = true;
322
323 // 90 = 90 (download_item_a) + 0 (download_item_b, 0 since it's completed)
324 ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
325
326 // Add unknown size download item.
327 DownloadHandlerTestDownloadItem download_item_c;
328 download_item_c.is_done_ = false;
329 download_item_c.total_bytes_ = 0;
330 downloads.push_back(&download_item_c);
331
332 // Unknown size download should be counted as 0 byte.
333 ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
334
335 // Add another unknown size download item.
336 DownloadHandlerTestDownloadItem download_item_d;
337 download_item_d.is_done_ = false;
338 download_item_d.total_bytes_ = 0;
339 downloads.push_back(&download_item_d);
340
341 // Unknown size downloads should be counted as 0 byte.
342 ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
343 }
344
199 } // namespace drive 345 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698