1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/drive/write_on_cache_file.h"
8 #include "chrome/browser/chromeos/drive/dummy_file_system.h"
9 #include "chrome/browser/chromeos/drive/test_util.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 const base::FilePath::CharType kDrivePath[] =
18 FILE_PATH_LITERAL("drive/root/file.txt");
19 const base::FilePath::CharType kInvalidPath[] =
20 FILE_PATH_LITERAL("drive/invalid/path");
21 const base::FilePath::CharType kLocalPath[] =
22 FILE_PATH_LITERAL("/tmp/local.txt");
24 class TestFileSystem : public DummyFileSystem {
26 TestFileSystem() : num_closed_(0) {
29 int num_closed() const { return num_closed_; }
31 // Mimics OpenFile. It fails if the |file_path| points to a hosted document.
32 virtual void OpenFile(const base::FilePath& file_path,
34 const std::string& mime_type,
35 const OpenFileCallback& callback) OVERRIDE {
36 EXPECT_EQ(OPEN_OR_CREATE_FILE, open_mode);
38 // Emulate a case of opening a hosted document.
39 if (file_path == base::FilePath(kInvalidPath)) {
40 callback.Run(FILE_ERROR_INVALID_OPERATION, base::FilePath(),
45 callback.Run(FILE_ERROR_OK, base::FilePath(kLocalPath),
46 base::Bind(&TestFileSystem::CloseFile,
47 base::Unretained(this)));
61 TEST(WriteOnCacheFileTest, PrepareFileForWritingSuccess) {
62 content::TestBrowserThreadBundle thread_bundle;
63 TestFileSystem test_file_system;
65 FileError error = FILE_ERROR_FAILED;
67 // The file should successfully be opened.
70 base::FilePath(kDrivePath),
71 std::string(), // mime_type
72 google_apis::test_util::CreateCopyResultCallback(&error, &path));
73 test_util::RunBlockingPoolTask();
75 EXPECT_EQ(FILE_ERROR_OK, error);
76 EXPECT_EQ(kLocalPath, path.value());
78 // Make sure that the file is actually closed.
79 EXPECT_EQ(1, test_file_system.num_closed());
82 TEST(WriteOnCacheFileTest, PrepareFileForWritingCreateFail) {
83 content::TestBrowserThreadBundle thread_bundle;
84 TestFileSystem test_file_system;
86 FileError error = FILE_ERROR_FAILED;
88 // Access to kInvalidPath should fail, and FileWriteHelper should not try to
89 // open or close the file.
92 base::FilePath(kInvalidPath),
93 std::string(), // mime_type
94 google_apis::test_util::CreateCopyResultCallback(&error, &path));
95 test_util::RunBlockingPoolTask();
97 EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error);
98 EXPECT_TRUE(path.empty());