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

Unified Diff: chrome/browser/chromeos/gdata/stale_cache_files_remover_unittest.cc

Issue 10832241: Drive: Removes unused cache files after the initial feed fetch. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move the ownership of |stale_cache_files_remover_| to GDataSystemService (reverting Patch Set 15) Created 8 years, 4 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
« no previous file with comments | « chrome/browser/chromeos/gdata/stale_cache_files_remover.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/gdata/stale_cache_files_remover_unittest.cc
diff --git a/chrome/browser/chromeos/gdata/stale_cache_files_remover_unittest.cc b/chrome/browser/chromeos/gdata/stale_cache_files_remover_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7f9823dd4be9b72aa8d45f6ba7df03a9d072e191
--- /dev/null
+++ b/chrome/browser/chromeos/gdata/stale_cache_files_remover_unittest.cc
@@ -0,0 +1,340 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+#include <vector>
+
+#include "base/bind.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/json/json_file_value_serializer.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop.h"
+#include "base/path_service.h"
+#include "base/scoped_temp_dir.h"
+#include "base/stringprintf.h"
+#include "base/threading/sequenced_worker_pool.h"
+#include "base/values.h"
+#include "chrome/browser/chromeos/cros/cros_library.h"
+#include "chrome/browser/chromeos/gdata/drive.pb.h"
+#include "chrome/browser/chromeos/gdata/drive_api_parser.h"
+#include "chrome/browser/chromeos/gdata/drive_webapps_registry.h"
+#include "chrome/browser/chromeos/gdata/gdata_file_system.h"
+#include "chrome/browser/chromeos/gdata/gdata_test_util.h"
+#include "chrome/browser/chromeos/gdata/gdata_uploader.h"
+#include "chrome/browser/chromeos/gdata/gdata_util.h"
+#include "chrome/browser/chromeos/gdata/mock_directory_change_observer.h"
+#include "chrome/browser/chromeos/gdata/mock_drive_cache_observer.h"
+#include "chrome/browser/chromeos/gdata/mock_drive_service.h"
+#include "chrome/browser/chromeos/gdata/stale_cache_files_remover.h"
+#include "chrome/common/chrome_paths.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/test/test_browser_thread.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using ::testing::AtLeast;
+using ::testing::Eq;
+using ::testing::NotNull;
+using ::testing::Return;
+using ::testing::StrictMock;
+using ::testing::_;
+
+namespace gdata {
+namespace {
+
+const int64 kLotsOfSpace = kMinFreeSpace * 10;
+
+// Callback for DriveCache::StoreOnUIThread used in RemoveStaleCacheFiles test.
+// Verifies that the result is not an error.
+void VerifyCacheFileState(GDataFileError error,
+ const std::string& resource_id,
+ const std::string& md5) {
+ EXPECT_EQ(GDATA_FILE_OK, error);
+}
+
+void GetEntryInfoByPathCallback(
satorux1 2012/08/28 15:38:02 I thought we had this in gdata_test_util.h. If so,
yoshiki 2012/08/28 18:11:44 Thanks, I didn't know that. Done.
+ GDataFileError* error_out,
+ scoped_ptr<DriveEntryProto>* entry_proto_out,
+ GDataFileError error_in,
+ scoped_ptr<DriveEntryProto> entry_proto_in) {
+ *error_out = error_in;
+ *entry_proto_out = entry_proto_in.Pass();
+}
+
+void GetEntryInfoByResourceIdCallback(
satorux1 2012/08/28 15:38:02 ditto.
yoshiki 2012/08/28 18:11:44 Done.
+ GDataFileError* error_out,
+ scoped_ptr<DriveEntryProto>* entry_proto_out,
+ GDataFileError error_in,
+ const FilePath& gdata_file_path,
+ scoped_ptr<DriveEntryProto> entry_proto_in) {
+ *error_out = error_in;
+ *entry_proto_out = entry_proto_in.Pass();
+}
+
+// Returns the absolute path for a test file stored under
+// chrome/test/data/chromeos/gdata.
+FilePath GetTestFilePath(const FilePath::StringType& base_name) {
+ FilePath path;
+ std::string error;
+ PathService::Get(chrome::DIR_TEST_DATA, &path);
+ path = path.AppendASCII("chromeos")
+ .AppendASCII("gdata")
+ .AppendASCII(base_name.c_str());
+ EXPECT_TRUE(file_util::PathExists(path)) <<
+ "Couldn't find " << path.value();
+ return path;
+}
+
+// Loads a test JSON file as a base::Value.
+base::Value* LoadJSONFile(const std::string& base_name) {
+ FilePath path = GetTestFilePath(base_name);
+
+ std::string error;
+ JSONFileValueSerializer serializer(path);
+ base::Value* value = serializer.Deserialize(NULL, &error);
+ EXPECT_TRUE(value) <<
+ "Parse error " << path.value() << ": " << error;
+ return value;
+}
+
+} // namespace
+
+class MockFreeDiskSpaceGetter : public FreeDiskSpaceGetterInterface {
satorux1 2012/08/28 15:38:02 do we define the same mock class elsewhere? if so,
yoshiki 2012/08/28 18:11:44 Done. Moved to gdata_test_util.h
+ public:
+ virtual ~MockFreeDiskSpaceGetter() {}
+ MOCK_CONST_METHOD0(AmountOfFreeDiskSpace, int64());
+};
+
+class MockGDataUploader : public GDataUploaderInterface {
satorux1 2012/08/28 15:38:02 ditto.
yoshiki 2012/08/28 18:11:44 Done. Moved to gdata_test_util.h
+ public:
+ virtual ~MockGDataUploader() {}
+ // This function is not mockable by gmock.
+ virtual int UploadNewFile(
+ scoped_ptr<UploadFileInfo> upload_file_info) OVERRIDE {
+ const int kUploadId = 123;
+ return kUploadId;
+ }
+
+ // This function is not mockable by gmock.
+ virtual int StreamExistingFile(
+ scoped_ptr<UploadFileInfo> upload_file_info) OVERRIDE { return 0; }
+
+ MOCK_METHOD6(UploadExistingFile,
+ int(const GURL& upload_location,
+ const FilePath& gdata_file_path,
+ const FilePath& local_file_path,
+ int64 file_size,
+ const std::string& content_type,
+ const UploadFileInfo::UploadCompletionCallback& callback));
+
+ MOCK_METHOD2(UpdateUpload, void(int upload_id,
+ content::DownloadItem* download));
+ MOCK_CONST_METHOD1(GetUploadedBytes, int64(int upload_id));
+};
+
+class MockDriveWebAppsRegistry : public DriveWebAppsRegistryInterface {
satorux1 2012/08/28 15:38:02 ditto.
yoshiki 2012/08/28 18:11:44 Done. Moved to gdata_test_util.h
+ public:
+ virtual ~MockDriveWebAppsRegistry() {}
+
+ MOCK_METHOD3(GetWebAppsForFile, void(const FilePath& file,
+ const std::string& mime_type,
+ ScopedVector<DriveWebAppInfo>* apps));
+ MOCK_METHOD1(GetExtensionsForWebStoreApp,
+ std::set<std::string>(const std::string& web_store_id));
+ MOCK_METHOD1(UpdateFromFeed, void(const AccountMetadataFeed& metadata));
+ MOCK_METHOD1(UpdateFromApplicationList, void(const AppList& applist));
+};
+
+class StaleCacheFileRemover : public testing::Test {
satorux1 2012/08/28 15:38:02 StaleCacheFilesRemoverTest
yoshiki 2012/08/28 18:11:44 Done.
+ protected:
+ StaleCacheFileRemover()
+ : ui_thread_(content::BrowserThread::UI, &message_loop_),
+ io_thread_(content::BrowserThread::IO),
+ cache_(NULL),
+ file_system_(NULL),
+ mock_drive_service_(NULL),
+ mock_webapps_registry_(NULL),
+ root_feed_changestamp_(0) {
+ }
+
+ virtual void SetUp() OVERRIDE {
+ chromeos::CrosLibrary::Initialize(true /* use_stub */);
+ io_thread_.StartIOThread();
+
+ profile_.reset(new TestingProfile);
+
+ // Allocate and keep a pointer to the mock, and inject it into the
+ // GDataFileSystem object, which will own the mock object.
+ mock_drive_service_ = new StrictMock<MockDriveService>;
+
+ EXPECT_CALL(*mock_drive_service_, Initialize(profile_.get())).Times(1);
+
+ // Likewise, this will be owned by GDataFileSystem.
+ mock_free_disk_space_checker_ = new StrictMock<MockFreeDiskSpaceGetter>;
+ SetFreeDiskSpaceGetterForTesting(mock_free_disk_space_checker_);
+
+ scoped_refptr<base::SequencedWorkerPool> pool =
+ content::BrowserThread::GetBlockingPool();
+ blocking_task_runner_ =
+ pool->GetSequencedTaskRunner(pool->GetSequenceToken());
+
+ cache_ = DriveCache::CreateDriveCacheOnUIThread(
+ DriveCache::GetCacheRootPath(profile_.get()), blocking_task_runner_);
+
+ mock_uploader_.reset(new StrictMock<MockGDataUploader>);
+ mock_webapps_registry_.reset(new StrictMock<MockDriveWebAppsRegistry>);
+
+ ASSERT_FALSE(file_system_);
+ file_system_ = new GDataFileSystem(profile_.get(),
+ cache_,
+ mock_drive_service_,
+ mock_uploader_.get(),
+ mock_webapps_registry_.get(),
+ blocking_task_runner_);
+
+ mock_cache_observer_.reset(new StrictMock<MockDriveCacheObserver>);
+ cache_->AddObserver(mock_cache_observer_.get());
+
+ mock_directory_observer_.reset(new StrictMock<MockDirectoryChangeObserver>);
+ file_system_->AddObserver(mock_directory_observer_.get());
+
+ file_system_->Initialize();
+ cache_->RequestInitializeOnUIThreadForTesting();
+
+ stale_cache_files_remover_.reset(new StaleCacheFilesRemover(file_system_,
+ cache_));
+
+ test_util::RunBlockingPoolTask();
+ }
+
+ virtual void TearDown() OVERRIDE {
+ ASSERT_TRUE(file_system_);
+ stale_cache_files_remover_.reset();
+ EXPECT_CALL(*mock_drive_service_, CancelAll()).Times(1);
+ delete file_system_;
+ file_system_ = NULL;
+ delete mock_drive_service_;
+ mock_drive_service_ = NULL;
+ SetFreeDiskSpaceGetterForTesting(NULL);
+ cache_->DestroyOnUIThread();
+ // The cache destruction requires to post a task to the blocking pool.
+ test_util::RunBlockingPoolTask();
+
+ profile_.reset(NULL);
+ chromeos::CrosLibrary::Shutdown();
+ }
+
+ // Loads test json file as root ("/drive") element.
+ void LoadRootFeedDocument(const std::string& filename) {
+ LoadChangeFeed(filename, 0);
+ }
+
+ void LoadChangeFeed(const std::string& filename,
satorux1 2012/08/28 15:38:02 if this is used elsewhere, please put this in gdat
yoshiki 2012/08/28 18:11:44 Moved it to test_util and removed LoadChangeFeed()
+ int largest_changestamp) {
+ std::string error;
+ scoped_ptr<Value> document(LoadJSONFile(filename));
+ ASSERT_TRUE(document.get());
+ ASSERT_TRUE(document->GetType() == Value::TYPE_DICTIONARY);
+ scoped_ptr<DocumentFeed> document_feed(
+ DocumentFeed::ExtractAndParse(*document));
+ ASSERT_TRUE(document_feed.get());
+ std::vector<DocumentFeed*> feed_list;
+ feed_list.push_back(document_feed.get());
+ ASSERT_TRUE(UpdateContent(feed_list, largest_changestamp));
+ }
+
+ // Updates the content of directory under |directory_path| with parsed feed
+ // |value|.
+ bool UpdateContent(const std::vector<DocumentFeed*>& list,
+ int largest_changestamp) {
+ GURL unused;
+ return file_system_->UpdateFromFeedForTesting(
+ list,
+ largest_changestamp,
+ root_feed_changestamp_++) == GDATA_FILE_OK;
+ }
+
+ MessageLoopForUI message_loop_;
+ // The order of the test threads is important, do not change the order.
+ // See also content/browser/browser_thread_impl.cc.
+ content::TestBrowserThread ui_thread_;
+ content::TestBrowserThread io_thread_;
+ scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
+ scoped_ptr<TestingProfile> profile_;
+ DriveCache* cache_;
+ scoped_ptr<StrictMock<MockGDataUploader> > mock_uploader_;
+ GDataFileSystem* file_system_;
+ StrictMock<MockDriveService>* mock_drive_service_;
+ scoped_ptr<StrictMock<MockDriveWebAppsRegistry> > mock_webapps_registry_;
+ StrictMock<MockFreeDiskSpaceGetter>* mock_free_disk_space_checker_;
+ scoped_ptr<StrictMock<MockDriveCacheObserver> > mock_cache_observer_;
+ scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_;
+ scoped_ptr<StaleCacheFilesRemover> stale_cache_files_remover_;
+
+ int root_feed_changestamp_;
+};
+
+TEST_F(StaleCacheFileRemover, RemoveStaleCacheFiles) {
+ FilePath dummy_file = GetTestFilePath("root_feed.json");
+ std::string resource_id("pdf:1a2b3c");
+ std::string md5("abcdef0123456789");
+
+ EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace())
+ .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace));
+
+ // Create a stale cache file.
+ cache_->StoreOnUIThread(resource_id, md5, dummy_file,
+ DriveCache::FILE_OPERATION_COPY,
+ base::Bind(&gdata::VerifyCacheFileState));
+ test_util::RunBlockingPoolTask();
+
+ // Verify that the cache file exists.
+ FilePath path = cache_->GetCacheFilePath(resource_id,
+ md5,
+ DriveCache::CACHE_TYPE_TMP,
+ DriveCache::CACHED_FILE_FROM_SERVER);
+ EXPECT_TRUE(file_util::PathExists(path));
+
+ // Verify that the corresponding file entry doesn't exist.
+ EXPECT_CALL(*mock_drive_service_, GetAccountMetadata(_)).Times(1);
+ EXPECT_CALL(*mock_drive_service_, GetDocuments(Eq(GURL()), _, "", _, _))
+ .Times(1);
+ EXPECT_CALL(*mock_webapps_registry_, UpdateFromFeed(_)).Times(1);
+
+ GDataFileError error(GDATA_FILE_OK);
+ scoped_ptr<DriveEntryProto> entry_proto;
+ file_system_->GetEntryInfoByResourceId(
+ resource_id,
+ base::Bind(&GetEntryInfoByResourceIdCallback,
+ &error,
+ &entry_proto));
+ test_util::RunBlockingPoolTask();
+ EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error);
+
+ file_system_->GetEntryInfoByPath(
+ path,
+ base::Bind(&GetEntryInfoByPathCallback,
+ &error,
+ &entry_proto));
+ test_util::RunBlockingPoolTask();
+ EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error);
+ EXPECT_FALSE(entry_proto.get());
+
+ // Load a root feed.
+ LoadRootFeedDocument("root_feed.json");
+
+ // Wait for StaleCacheFilesRemover to finish cleaning up the stale file.
+ test_util::RunBlockingPoolTask();
+
+ // Verify that the cache file is deleted.
+ path = cache_->GetCacheFilePath(resource_id,
+ md5,
+ DriveCache::CACHE_TYPE_TMP,
+ DriveCache::CACHED_FILE_FROM_SERVER);
+ EXPECT_FALSE(file_util::PathExists(path));
+}
+
+} // namespace gdata
« no previous file with comments | « chrome/browser/chromeos/gdata/stale_cache_files_remover.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698