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

Unified 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, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/drive/download_handler_unittest.cc
diff --git a/chrome/browser/chromeos/drive/download_handler_unittest.cc b/chrome/browser/chromeos/drive/download_handler_unittest.cc
index 52bde9123f12669dcd1911528fab184285e241ac..5d204c026705e2869d4ce774bef51ab6f81636ac 100644
--- a/chrome/browser/chromeos/drive/download_handler_unittest.cc
+++ b/chrome/browser/chromeos/drive/download_handler_unittest.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/chromeos/drive/download_handler.h"
#include "base/files/scoped_temp_dir.h"
+#include "base/run_loop.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/drive/dummy_file_system.h"
@@ -42,16 +43,37 @@ class DownloadHandlerTestFileSystem : public DummyFileSystem {
callback.Run(error_);
}
+ void FreeDiskSpaceIfNeededFor(
+ int64 num_bytes,
+ const FreeDiskSpaceCallback& callback) override {
+ free_disk_space_if_needed_for_num_bytes_.push_back(num_bytes);
+ callback.Run(true);
+ }
+
+ std::vector<int64> free_disk_space_if_needed_for_num_bytes_;
+
private:
FileError error_;
};
+class DownloadHandlerTestDownloadManager : public content::MockDownloadManager {
+ public:
+ void GetAllDownloads(
+ content::DownloadManager::DownloadVector* downloads) override {
+ for (auto* test_download : test_downloads) {
+ downloads->push_back(test_download);
+ }
+ }
+
+ content::DownloadManager::DownloadVector test_downloads;
hashimoto 2015/10/30 09:33:43 nit: test_downloads_;
yawano 2015/11/02 07:26:20 Done.
+};
+
} // namespace
class DownloadHandlerTest : public testing::Test {
public:
DownloadHandlerTest()
- : download_manager_(new content::MockDownloadManager) {}
+ : download_manager_(new DownloadHandlerTestDownloadManager) {}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
@@ -62,13 +84,16 @@ class DownloadHandlerTest : public testing::Test {
download_handler_.reset(new DownloadHandler(&test_file_system_));
download_handler_->Initialize(download_manager_.get(), temp_dir_.path());
+ download_handler_->SetFreeDiskSpaceDelayForTesting(
+ base::TimeDelta::FromMilliseconds(0));
}
protected:
base::ScopedTempDir temp_dir_;
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
- scoped_ptr<content::MockDownloadManager> download_manager_;
+ scoped_ptr<DownloadHandlerTestDownloadManager> download_manager_;
+ scoped_ptr<DownloadHandlerTestDownloadManager> incognito_download_manager_;
DownloadHandlerTestFileSystem test_file_system_;
scoped_ptr<DownloadHandler> download_handler_;
content::MockDownloadItem download_item_;
@@ -196,4 +221,115 @@ TEST_F(DownloadHandlerTest, CheckForFileExistence) {
EXPECT_FALSE(file_exists);
}
+TEST_F(DownloadHandlerTest, FreeDiskSpace) {
+ // Add a download item to download manager.
+ 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.
+ EXPECT_CALL(download_item_a, IsDone())
+ .WillOnce(testing::Return(false))
+ .WillOnce(testing::Return(false))
+ .WillOnce(testing::Return(false))
+ .WillRepeatedly(testing::Return(true));
+ EXPECT_CALL(download_item_a, GetTotalBytes())
+ .WillRepeatedly(testing::Return(100));
+ EXPECT_CALL(download_item_a, GetReceivedBytes())
+ .WillOnce(testing::Return(10))
+ .WillOnce(testing::Return(20))
+ .WillOnce(testing::Return(30));
+
+ download_manager_->test_downloads.push_back(&download_item_a);
+
+ 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.
+ ASSERT_EQ(1u,
+ test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
+ ASSERT_EQ(90, test_file_system_.free_disk_space_if_needed_for_num_bytes_[0]);
+
+ // This call should be delayed.
+ download_handler_->FreeDiskSpaceIfNeeded();
+ ASSERT_EQ(1u,
+ test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
+
+ base::RunLoop().RunUntilIdle();
+ ASSERT_EQ(2u,
+ test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
+ ASSERT_EQ(80, test_file_system_.free_disk_space_if_needed_for_num_bytes_[1]);
+
+ // Observe incognito download manager and add a download item.
+ incognito_download_manager_.reset(new DownloadHandlerTestDownloadManager);
+ download_handler_->ObserveIncognitoDownloadManager(
+ incognito_download_manager_.get());
+
+ content::MockDownloadItem download_item_b;
+ EXPECT_CALL(download_item_b, IsDone())
+ .WillOnce(testing::Return(false))
+ .WillRepeatedly(testing::Return(true));
+ EXPECT_CALL(download_item_b, GetTotalBytes())
+ .WillRepeatedly(testing::Return(200));
+ EXPECT_CALL(download_item_b, GetReceivedBytes())
+ .WillRepeatedly(testing::Return(0));
+
+ incognito_download_manager_->test_downloads.push_back(&download_item_b);
+
+ download_handler_->FreeDiskSpaceIfNeededImmediately();
+ ASSERT_EQ(3u,
+ test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
+ ASSERT_EQ(70 + 200,
+ test_file_system_.free_disk_space_if_needed_for_num_bytes_[2]);
+
+ download_handler_->FreeDiskSpaceIfNeeded();
+ base::RunLoop().RunUntilIdle();
+
+ // FreeDiskSpace should be called with 0 bytes to keep
+ // drive::internal::kMinFreeSpaceInBytes.
+ ASSERT_EQ(4u,
+ test_file_system_.free_disk_space_if_needed_for_num_bytes_.size());
+ ASSERT_EQ(0, test_file_system_.free_disk_space_if_needed_for_num_bytes_[3]);
+}
+
+TEST_F(DownloadHandlerTest, CalculateRequestSpace) {
+ content::MockDownloadItem download_item_a;
+ content::MockDownloadItem download_item_b;
+
+ EXPECT_CALL(download_item_a, IsDone()).WillRepeatedly(testing::Return(false));
+ EXPECT_CALL(download_item_a, GetTotalBytes())
+ .WillRepeatedly(testing::Return(100));
+ EXPECT_CALL(download_item_a, GetReceivedBytes())
+ .WillOnce(testing::Return(0))
+ .WillRepeatedly(testing::Return(10));
+ EXPECT_CALL(download_item_b, IsDone())
+ .WillOnce(testing::Return(false))
+ .WillOnce(testing::Return(false))
+ .WillRepeatedly(testing::Return(true));
+ EXPECT_CALL(download_item_b, GetTotalBytes())
+ .WillRepeatedly(testing::Return(200));
+ EXPECT_CALL(download_item_b, GetReceivedBytes())
+ .WillRepeatedly(testing::Return(10));
+
+ content::DownloadManager::DownloadVector downloads;
+ downloads.push_back(&download_item_a);
+ downloads.push_back(&download_item_b);
+
+ 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.
+ ASSERT_EQ(280, download_handler_->CalculateRequestSpace(downloads));
+ ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
+
+ // Add unknown size download item.
+ content::MockDownloadItem download_item_c;
+ EXPECT_CALL(download_item_c, IsDone()).WillRepeatedly(testing::Return(false));
+ EXPECT_CALL(download_item_c, GetTotalBytes())
+ .WillRepeatedly(testing::Return(0));
+ downloads.push_back(&download_item_c);
+
+ // Unknown size download should be counted as 0 byte.
+ ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
+
+ // Add another unknown size download item.
+ content::MockDownloadItem download_item_d;
+ EXPECT_CALL(download_item_d, IsDone()).WillRepeatedly(testing::Return(false));
+ EXPECT_CALL(download_item_d, GetTotalBytes())
+ .WillRepeatedly(testing::Return(0));
+ downloads.push_back(&download_item_d);
+
+ ASSERT_EQ(90, download_handler_->CalculateRequestSpace(downloads));
+}
+
} // namespace drive

Powered by Google App Engine
This is Rietveld 408576698