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. 4 5 #include "chrome/browser/chromeos/drive/write_on_cache_file.h" 6 7 #include "base/bind.h" 8 #include "chrome/browser/chromeos/drive/dummy_file_system.h" 9 #include "content/public/test/test_browser_thread_bundle.h" 10 #include "content/public/test/test_utils.h" 11 #include "google_apis/drive/test_util.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace drive { 15 16 namespace { 17 18 const base::FilePath::CharType kDrivePath[] = 19 FILE_PATH_LITERAL("drive/root/file.txt"); 20 const base::FilePath::CharType kInvalidPath[] = 21 FILE_PATH_LITERAL("drive/invalid/path"); 22 const base::FilePath::CharType kLocalPath[] = 23 FILE_PATH_LITERAL("/tmp/local.txt"); 24 25 class TestFileSystem : public DummyFileSystem { 26 public: 27 TestFileSystem() : num_closed_(0) { 28 } 29 30 int num_closed() const { return num_closed_; } 31 32 // Mimics OpenFile. It fails if the |file_path| points to a hosted document. 33 virtual void OpenFile(const base::FilePath& file_path, 34 OpenMode open_mode, 35 const std::string& mime_type, 36 const OpenFileCallback& callback) OVERRIDE { 37 EXPECT_EQ(OPEN_OR_CREATE_FILE, open_mode); 38 39 // Emulate a case of opening a hosted document. 40 if (file_path == base::FilePath(kInvalidPath)) { 41 callback.Run(FILE_ERROR_INVALID_OPERATION, base::FilePath(), 42 base::Closure()); 43 return; 44 } 45 46 callback.Run(FILE_ERROR_OK, base::FilePath(kLocalPath), 47 base::Bind(&TestFileSystem::CloseFile, 48 base::Unretained(this))); 49 } 50 51 private: 52 53 void CloseFile() { 54 ++num_closed_; 55 } 56 57 int num_closed_; 58 }; 59 60 } // namespace 61 62 TEST(WriteOnCacheFileTest, PrepareFileForWritingSuccess) { 63 content::TestBrowserThreadBundle thread_bundle; 64 TestFileSystem test_file_system; 65 66 FileError error = FILE_ERROR_FAILED; 67 base::FilePath path; 68 // The file should successfully be opened. 69 WriteOnCacheFile( 70 &test_file_system, 71 base::FilePath(kDrivePath), 72 std::string(), // mime_type 73 google_apis::test_util::CreateCopyResultCallback(&error, &path)); 74 content::RunAllBlockingPoolTasksUntilIdle(); 75 76 EXPECT_EQ(FILE_ERROR_OK, error); 77 EXPECT_EQ(kLocalPath, path.value()); 78 79 // Make sure that the file is actually closed. 80 EXPECT_EQ(1, test_file_system.num_closed()); 81 } 82 83 TEST(WriteOnCacheFileTest, PrepareFileForWritingCreateFail) { 84 content::TestBrowserThreadBundle thread_bundle; 85 TestFileSystem test_file_system; 86 87 FileError error = FILE_ERROR_FAILED; 88 base::FilePath path; 89 // Access to kInvalidPath should fail, and FileWriteHelper should not try to 90 // open or close the file. 91 WriteOnCacheFile( 92 &test_file_system, 93 base::FilePath(kInvalidPath), 94 std::string(), // mime_type 95 google_apis::test_util::CreateCopyResultCallback(&error, &path)); 96 content::RunAllBlockingPoolTasksUntilIdle(); 97 98 EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error); 99 EXPECT_TRUE(path.empty()); 100 } 101 102 } // namespace drive 103