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

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: Free disk space immediately in OnDownloadCreated. 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
49 } // namespace 59 } // namespace
50 60
51 class DownloadHandlerTest : public testing::Test { 61 class DownloadHandlerTest : public testing::Test {
52 public: 62 public:
53 DownloadHandlerTest() 63 DownloadHandlerTest()
54 : download_manager_(new content::MockDownloadManager) {} 64 : download_manager_(new content::MockDownloadManager) {}
55 65
56 void SetUp() override { 66 void SetUp() override {
57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
58 68
59 // Set expectations for download item. 69 // Set expectations for download item.
60 EXPECT_CALL(download_item_, GetState()) 70 EXPECT_CALL(download_item_, GetState())
61 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS)); 71 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS));
62 72
63 download_handler_.reset(new DownloadHandler(&test_file_system_)); 73 download_handler_.reset(new DownloadHandler(&test_file_system_));
64 download_handler_->Initialize(download_manager_.get(), temp_dir_.path()); 74 download_handler_->Initialize(download_manager_.get(), temp_dir_.path());
75 download_handler_->SetFreeDiskSpaceDelayForTesting(
76 base::TimeDelta::FromMilliseconds(0));
65 } 77 }
66 78
67 protected: 79 protected:
68 base::ScopedTempDir temp_dir_; 80 base::ScopedTempDir temp_dir_;
69 content::TestBrowserThreadBundle thread_bundle_; 81 content::TestBrowserThreadBundle thread_bundle_;
70 TestingProfile profile_; 82 TestingProfile profile_;
71 scoped_ptr<content::MockDownloadManager> download_manager_; 83 scoped_ptr<content::MockDownloadManager> download_manager_;
72 DownloadHandlerTestFileSystem test_file_system_; 84 DownloadHandlerTestFileSystem test_file_system_;
73 scoped_ptr<DownloadHandler> download_handler_; 85 scoped_ptr<DownloadHandler> download_handler_;
74 content::MockDownloadItem download_item_; 86 content::MockDownloadItem download_item_;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // Call CheckForFileExistence again. 201 // Call CheckForFileExistence again.
190 download_handler_->CheckForFileExistence( 202 download_handler_->CheckForFileExistence(
191 &download_item_, 203 &download_item_,
192 google_apis::test_util::CreateCopyResultCallback(&file_exists)); 204 google_apis::test_util::CreateCopyResultCallback(&file_exists));
193 content::RunAllBlockingPoolTasksUntilIdle(); 205 content::RunAllBlockingPoolTasksUntilIdle();
194 206
195 // Check the result. 207 // Check the result.
196 EXPECT_FALSE(file_exists); 208 EXPECT_FALSE(file_exists);
197 } 209 }
198 210
211 TEST_F(DownloadHandlerTest, FreeDiskSpaceForUnknownSizeDownload) {
212 const int kUnknownSizeDownloadRequestSize = 512 * (1 << 20); // 512MB
213 ASSERT_EQ(0u,
214 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
215 EXPECT_CALL(download_item_, IsDone()).WillRepeatedly(testing::Return(false));
216 EXPECT_CALL(download_item_, GetTotalBytes())
217 .WillRepeatedly(testing::Return(0));
218
219 download_handler_->FreeDiskSpaceIfNeeded(&download_item_,
220 true /* immediate */);
221 ASSERT_EQ(1u,
222 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
223 ASSERT_EQ(kUnknownSizeDownloadRequestSize,
224 test_file_system_.free_disk_space_if_needed_for_num_bytes_[0]);
225
226 download_handler_->FreeDiskSpaceIfNeeded(&download_item_,
227 false /* not immediate */);
228
229 // FreeDiskSpaceIfNeededFor should be rate limited.
230 ASSERT_EQ(1u,
231 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
232
233 download_handler_->FreeDiskSpaceIfNeeded(&download_item_,
234 false /* not immediate */);
235 base::RunLoop().RunUntilIdle();
236
237 ASSERT_EQ(2u,
238 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
239 ASSERT_EQ(kUnknownSizeDownloadRequestSize,
240 test_file_system_.free_disk_space_if_needed_for_num_bytes_[1]);
241 }
242
243 TEST_F(DownloadHandlerTest, FreeDiskSpaceForKnownSizeDownload) {
244 const int kKnownDownloadSize = 5 * (1 << 20); // 5MB
245 ASSERT_EQ(0u,
246 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
247 EXPECT_CALL(download_item_, IsDone()).WillRepeatedly(testing::Return(false));
248 EXPECT_CALL(download_item_, GetTotalBytes())
249 .WillRepeatedly(testing::Return(kKnownDownloadSize));
250 EXPECT_CALL(download_item_, GetReceivedBytes())
251 .WillOnce(testing::Return(0))
252 .WillOnce(testing::Return(100));
253
254 download_handler_->FreeDiskSpaceIfNeeded(&download_item_,
255 true /* immediate */);
256
257 ASSERT_EQ(1u,
258 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
259 ASSERT_EQ(kKnownDownloadSize,
260 test_file_system_.free_disk_space_if_needed_for_num_bytes_[0]);
261
262 download_handler_->FreeDiskSpaceIfNeeded(&download_item_,
263 false /* not immediate */);
264 base::RunLoop().RunUntilIdle();
265
266 ASSERT_EQ(2u,
267 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
268 ASSERT_EQ(kKnownDownloadSize - 100,
269 test_file_system_.free_disk_space_if_needed_for_num_bytes_[1]);
270 }
271
272 TEST_F(DownloadHandlerTest, FreeDiskSpaceForAlreadyCompletedDownload) {
273 ASSERT_EQ(0u,
274 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
275 EXPECT_CALL(download_item_, IsDone()).WillOnce(testing::Return(true));
276
277 download_handler_->FreeDiskSpaceIfNeeded(&download_item_,
278 true /* immediate */);
279
280 ASSERT_EQ(0u,
281 test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
282 }
283
199 } // namespace drive 284 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698