| Index: chrome/browser/chromeos/gdata/gdata_file_writing_unittest.cc
|
| diff --git a/chrome/browser/chromeos/gdata/gdata_file_writing_unittest.cc b/chrome/browser/chromeos/gdata/gdata_file_writing_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cf25cf2c8be72f4258653682f06a6f91f2e28f1c
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/gdata/gdata_file_writing_unittest.cc
|
| @@ -0,0 +1,149 @@
|
| +// 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_webapps_registry.h"
|
| +#include "chrome/browser/chromeos/gdata/gdata.pb.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_util.h"
|
| +#include "chrome/browser/chromeos/gdata/mock_directory_change_observer.h"
|
| +#include "chrome/browser/chromeos/gdata/mock_gdata_cache_observer.h"
|
| +#include "chrome/browser/chromeos/gdata/mock_gdata_documents_service.h"
|
| +#include "chrome/common/chrome_paths.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| +*/
|
| +#include "base/bind.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/threading/thread_restrictions.h"
|
| +#include "chrome/browser/chromeos/gdata/gdata_file_writing.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 GDataFileWritingTest : public testing::Test {
|
| + public:
|
| + GDataFileWritingTest()
|
| + : 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(GDataFileWritingTest, 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));
|
| +
|
| + GDataFileWriting file_writing(mock_file_system_.get());
|
| + GDataFileError error = GDATA_FILE_ERROR_FAILED;
|
| + FilePath path;
|
| + file_writing.PrepareWritableFileAndRun(
|
| + kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
|
| + test_util::RunBlockingPoolTask();
|
| +
|
| + EXPECT_EQ(GDATA_FILE_OK, error);
|
| + EXPECT_EQ(kLocalPath, path);
|
| +}
|
| +
|
| +TEST_F(GDataFileWritingTest, 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);
|
| +
|
| + GDataFileWriting file_writing(mock_file_system_.get());
|
| + GDataFileError error = GDATA_FILE_ERROR_FAILED;
|
| + FilePath path;
|
| + file_writing.PrepareWritableFileAndRun(
|
| + kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
|
| + test_util::RunBlockingPoolTask();
|
| +
|
| + EXPECT_EQ(GDATA_FILE_ERROR_ACCESS_DENIED, error);
|
| + EXPECT_EQ(FilePath(), path);
|
| +}
|
| +
|
| +TEST_F(GDataFileWritingTest, 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);
|
| +
|
| + GDataFileWriting file_writing(mock_file_system_.get());
|
| + GDataFileError error = GDATA_FILE_ERROR_FAILED;
|
| + FilePath path;
|
| + file_writing.PrepareWritableFileAndRun(
|
| + kDrivePath, base::Bind(&RecordOpenFileCallbackArguments, &error, &path));
|
| + test_util::RunBlockingPoolTask();
|
| +
|
| + EXPECT_EQ(GDATA_FILE_ERROR_IN_USE, error);
|
| + EXPECT_EQ(FilePath(), path);
|
| +}
|
| +
|
| +} // namespace gdata
|
|
|