| Index: chrome/browser/chromeos/gdata/gdata_file_write_helper_unittest.cc
|
| diff --git a/chrome/browser/chromeos/gdata/gdata_file_write_helper_unittest.cc b/chrome/browser/chromeos/gdata/gdata_file_write_helper_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8c0454cf436d7c2de4333c88c5189557dce6dadd
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/gdata/gdata_file_write_helper_unittest.cc
|
| @@ -0,0 +1,124 @@
|
| +// 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 "chrome/browser/chromeos/gdata/gdata_file_write_helper.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/threading/thread_restrictions.h"
|
| +#include "chrome/browser/chromeos/gdata/gdata_test_util.h"
|
| +#include "chrome/browser/chromeos/gdata/mock_gdata_file_system.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 gdata {
|
| +
|
| +namespace {
|
| +
|
| +ACTION_P(MockCreateFile, error) {
|
| + if (!arg2.is_null())
|
| + arg2.Run(error);
|
| +}
|
| +
|
| +ACTION_P2(MockOpenFile, error, local_path) {
|
| + if (!arg1.is_null())
|
| + arg1.Run(error, local_path);
|
| +}
|
| +
|
| +ACTION_P(MockCloseFile, error) {
|
| + if (!arg1.is_null())
|
| + arg1.Run(error);
|
| +}
|
| +
|
| +void RecordOpenFileCallbackArguments(GDataFileError* error,
|
| + FilePath* path,
|
| + GDataFileError error_arg,
|
| + const FilePath& path_arg) {
|
| + base::ThreadRestrictions::AssertIOAllowed();
|
| + *error = error_arg;
|
| + *path = path_arg;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class GDataFileWriteHelperTest : public testing::Test {
|
| + public:
|
| + GDataFileWriteHelperTest()
|
| + : ui_thread_(content::BrowserThread::UI, &message_loop_),
|
| + mock_file_system_(new StrictMock<MockGDataFileSystem>) {
|
| + }
|
| +
|
| + protected:
|
| + MessageLoopForUI message_loop_;
|
| + content::TestBrowserThread ui_thread_;
|
| + scoped_ptr< StrictMock<MockGDataFileSystem> > mock_file_system_;
|
| +};
|
| +
|
| +TEST_F(GDataFileWriteHelperTest, PrepareFileForWritingSuccess) {
|
| + const FilePath kDrivePath("/drive/file.txt");
|
| + const FilePath kLocalPath("/tmp/dummy.txt");
|
| +
|
| + EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
|
| + .WillOnce(MockCreateFile(GDATA_FILE_OK));
|
| + EXPECT_CALL(*mock_file_system_, OpenFile(kDrivePath, _))
|
| + .WillOnce(MockOpenFile(GDATA_FILE_OK, kLocalPath));
|
| + EXPECT_CALL(*mock_file_system_, CloseFile(kDrivePath, _))
|
| + .WillOnce(MockCloseFile(GDATA_FILE_OK));
|
| +
|
| + GDataFileWriteHelper file_write_helper(mock_file_system_.get());
|
| + GDataFileError error = GDATA_FILE_ERROR_FAILED;
|
| + FilePath path;
|
| + file_write_helper.PrepareWritableFileAndRun(
|
| + kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
|
| + test_util::RunBlockingPoolTask();
|
| +
|
| + EXPECT_EQ(GDATA_FILE_OK, error);
|
| + EXPECT_EQ(kLocalPath, path);
|
| +}
|
| +
|
| +TEST_F(GDataFileWriteHelperTest, PrepareFileForWritingCreateFail) {
|
| + const FilePath kDrivePath("/drive/file.txt");
|
| +
|
| + EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
|
| + .WillOnce(MockCreateFile(GDATA_FILE_ERROR_ACCESS_DENIED));
|
| + EXPECT_CALL(*mock_file_system_, OpenFile(_, _)).Times(0);
|
| + EXPECT_CALL(*mock_file_system_, CloseFile(_, _)).Times(0);
|
| +
|
| + GDataFileWriteHelper file_write_helper(mock_file_system_.get());
|
| + GDataFileError error = GDATA_FILE_ERROR_FAILED;
|
| + FilePath path;
|
| + file_write_helper.PrepareWritableFileAndRun(
|
| + kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
|
| + test_util::RunBlockingPoolTask();
|
| +
|
| + EXPECT_EQ(GDATA_FILE_ERROR_ACCESS_DENIED, error);
|
| + EXPECT_EQ(FilePath(), path);
|
| +}
|
| +
|
| +TEST_F(GDataFileWriteHelperTest, PrepareFileForWritingOpenFail) {
|
| + const FilePath kDrivePath("/drive/file.txt");
|
| +
|
| + EXPECT_CALL(*mock_file_system_, CreateFile(kDrivePath, false, _))
|
| + .WillOnce(MockCreateFile(GDATA_FILE_OK));
|
| + EXPECT_CALL(*mock_file_system_, OpenFile(kDrivePath, _))
|
| + .WillOnce(MockOpenFile(GDATA_FILE_ERROR_IN_USE, FilePath()));
|
| + EXPECT_CALL(*mock_file_system_, CloseFile(_, _)).Times(0);
|
| +
|
| + GDataFileWriteHelper file_write_helper(mock_file_system_.get());
|
| + GDataFileError error = GDATA_FILE_ERROR_FAILED;
|
| + FilePath path;
|
| + file_write_helper.PrepareWritableFileAndRun(
|
| + kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
|
| + test_util::RunBlockingPoolTask();
|
| +
|
| + EXPECT_EQ(GDATA_FILE_ERROR_IN_USE, error);
|
| + EXPECT_EQ(FilePath(), path);
|
| +}
|
| +
|
| +} // namespace gdata
|
|
|