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/file_write_helper_unittest.cc

Issue 15753004: Introduce drive::DummyFileSystem and rewrite some tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 7 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/drive/dummy_file_system.h ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/drive/file_write_helper_unittest.cc
diff --git a/chrome/browser/chromeos/drive/file_write_helper_unittest.cc b/chrome/browser/chromeos/drive/file_write_helper_unittest.cc
index bd02b55343dbce837f248937db9dbd87ea9551bb..bb68996d5fa71873c0f6448c181c209a1f43d646 100644
--- a/chrome/browser/chromeos/drive/file_write_helper_unittest.cc
+++ b/chrome/browser/chromeos/drive/file_write_helper_unittest.cc
@@ -7,43 +7,65 @@
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/threading/thread_restrictions.h"
-#include "chrome/browser/chromeos/drive/mock_file_system.h"
+#include "chrome/browser/chromeos/drive/dummy_file_system.h"
#include "chrome/browser/chromeos/drive/test_util.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::StrictMock;
-using ::testing::_;
-
namespace drive {
namespace {
-ACTION_P(MockCreateFile, error) {
- DCHECK(!arg2.is_null());
- arg2.Run(error);
-}
+const base::FilePath::CharType kDrivePath[] =
+ FILE_PATH_LITERAL("drive/root/file.txt");
+const base::FilePath::CharType kInvalidPath[] =
+ FILE_PATH_LITERAL("drive/invalid/path");
+const base::FilePath::CharType kLocalPath[] =
+ FILE_PATH_LITERAL("/tmp/local.txt");
-ACTION_P2(MockOpenFile, error, local_path) {
- DCHECK(!arg1.is_null());
- arg1.Run(error, local_path);
-}
+class TestFileSystem : public DummyFileSystem {
+ public:
+ // Mimics CreateFile. It always succeeds unless kInvalidPath is passed.
+ virtual void CreateFile(const base::FilePath& file_path,
+ bool is_exclusive,
+ const FileOperationCallback& callback) OVERRIDE {
+ if (file_path == base::FilePath(kInvalidPath)) {
+ callback.Run(FILE_ERROR_ACCESS_DENIED);
+ return;
+ }
+ created.insert(file_path);
+ callback.Run(FILE_ERROR_OK);
+ }
-ACTION_P(MockCloseFile, error) {
- DCHECK(!arg1.is_null());
- arg1.Run(error);
-}
+ // Mimics OpenFile. It fails if the |file_path| is a path that is not
+ // passed to CreateFile before. This tests that FileWriteHelper always
+ // ensures file existence before trying to open. It also fails if the
+ // path is already opened, to match the behavior of real FileSystem.
+ virtual void OpenFile(const base::FilePath& file_path,
+ const OpenFileCallback& callback) OVERRIDE {
+ // Files failed to create should never be opened.
+ EXPECT_TRUE(created.count(file_path));
+ created.erase(file_path);
+ if (opened.count(file_path)) {
+ callback.Run(FILE_ERROR_IN_USE, base::FilePath());
+ } else {
+ opened.insert(file_path);
+ callback.Run(FILE_ERROR_OK, base::FilePath(kLocalPath));
+ }
+ }
-void RecordOpenFileCallbackArguments(FileError* error,
- base::FilePath* path,
- FileError error_arg,
- const base::FilePath& path_arg) {
- base::ThreadRestrictions::AssertIOAllowed();
- *error = error_arg;
- *path = path_arg;
-}
+ // Mimics CloseFile. It fails if it is passed a path not OpenFile'd.
+ virtual void CloseFile(const base::FilePath& file_path,
+ const FileOperationCallback& callback) OVERRIDE {
+ // Files failed to open should never be closed.
+ EXPECT_TRUE(opened.count(file_path));
+ opened.erase(file_path);
+ callback.Run(FILE_ERROR_OK);
+ }
+ std::set<base::FilePath> created;
+ std::set<base::FilePath> opened;
+};
} // namespace
@@ -51,74 +73,69 @@ class FileWriteHelperTest : public testing::Test {
public:
FileWriteHelperTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_),
- mock_file_system_(new StrictMock<MockFileSystem>) {
+ test_file_system_(new TestFileSystem) {
}
protected:
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
- scoped_ptr< StrictMock<MockFileSystem> > mock_file_system_;
+ scoped_ptr<TestFileSystem> test_file_system_;
};
TEST_F(FileWriteHelperTest, PrepareFileForWritingSuccess) {
- const base::FilePath kDrivePath(FILE_PATH_LITERAL("/drive/file.txt"));
- const base::FilePath kLocalPath(FILE_PATH_LITERAL("/tmp/dummy.txt"));
-
- EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
- .WillOnce(MockCreateFile(FILE_ERROR_OK));
- EXPECT_CALL(*mock_file_system_, OpenFile(kDrivePath, _))
- .WillOnce(MockOpenFile(FILE_ERROR_OK, kLocalPath));
- EXPECT_CALL(*mock_file_system_, CloseFile(kDrivePath, _))
- .WillOnce(MockCloseFile(FILE_ERROR_OK));
-
- FileWriteHelper file_write_helper(mock_file_system_.get());
+ FileWriteHelper file_write_helper(test_file_system_.get());
FileError error = FILE_ERROR_FAILED;
base::FilePath path;
+ // The file should successfully be opened.
file_write_helper.PrepareWritableFileAndRun(
- kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
+ base::FilePath(kDrivePath),
+ google_apis::test_util::CreateCopyResultCallback(&error, &path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
- EXPECT_EQ(kLocalPath, path);
+ EXPECT_EQ(kLocalPath, path.value());
}
TEST_F(FileWriteHelperTest, PrepareFileForWritingCreateFail) {
- const base::FilePath kDrivePath(FILE_PATH_LITERAL("/drive/file.txt"));
-
- EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
- .WillOnce(MockCreateFile(FILE_ERROR_ACCESS_DENIED));
- EXPECT_CALL(*mock_file_system_, OpenFile(_, _)).Times(0);
- EXPECT_CALL(*mock_file_system_, CloseFile(_, _)).Times(0);
-
- FileWriteHelper file_write_helper(mock_file_system_.get());
+ FileWriteHelper file_write_helper(test_file_system_.get());
FileError error = FILE_ERROR_FAILED;
base::FilePath path;
+ // Access to kInvalidPath should fail, and FileWriteHelper should not try to
+ // open or close the file.
file_write_helper.PrepareWritableFileAndRun(
- kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
+ base::FilePath(kInvalidPath),
+ google_apis::test_util::CreateCopyResultCallback(&error, &path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_ACCESS_DENIED, error);
- EXPECT_EQ(base::FilePath(), path);
+ EXPECT_TRUE(path.empty());
}
TEST_F(FileWriteHelperTest, PrepareFileForWritingOpenFail) {
- const base::FilePath kDrivePath(FILE_PATH_LITERAL("/drive/file.txt"));
-
- EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
- .WillOnce(MockCreateFile(FILE_ERROR_OK));
- EXPECT_CALL(*mock_file_system_, OpenFile(kDrivePath, _))
- .WillOnce(MockOpenFile(FILE_ERROR_IN_USE, base::FilePath()));
- EXPECT_CALL(*mock_file_system_, CloseFile(_, _)).Times(0);
-
- FileWriteHelper file_write_helper(mock_file_system_.get());
+ // Externally open the path beforehand.
FileError error = FILE_ERROR_FAILED;
base::FilePath path;
+ test_file_system_->CreateFile(
+ base::FilePath(kDrivePath),
+ false,
+ google_apis::test_util::CreateCopyResultCallback(&error));
+ ASSERT_EQ(FILE_ERROR_OK, error);
+ error = FILE_ERROR_FAILED;
+ test_file_system_->OpenFile(
+ base::FilePath(kDrivePath),
+ google_apis::test_util::CreateCopyResultCallback(&error, &path));
+ ASSERT_EQ(FILE_ERROR_OK, error);
+
+ // Run FileWriteHelper on a file already opened in somewhere else.
+ // It should fail to open the file, and should not try to close it.
+ FileWriteHelper file_write_helper(test_file_system_.get());
file_write_helper.PrepareWritableFileAndRun(
- kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
+ base::FilePath(kDrivePath),
+ google_apis::test_util::CreateCopyResultCallback(&error, &path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_IN_USE, error);
- EXPECT_EQ(base::FilePath(), path);
+ EXPECT_TRUE(path.empty());
}
} // namespace drive
« no previous file with comments | « chrome/browser/chromeos/drive/dummy_file_system.h ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698