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 "base/basictypes.h" 6 #include "base/file_util.h" 7 #include "base/files/file_path.h" 8 #include "base/files/scoped_temp_dir.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/run_loop.h" 11 #include "content/public/test/test_file_system_context.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "webkit/browser/fileapi/file_system_context.h" 14 #include "webkit/browser/fileapi/file_system_operation_context.h" 15 #include "webkit/browser/fileapi/isolated_context.h" 16 #include "webkit/browser/fileapi/transient_file_util.h" 17 #include "webkit/common/blob/scoped_file.h" 18 19 using fileapi::FileSystemURL; 20 21 namespace content { 22 23 class TransientFileUtilTest : public testing::Test { 24 public: 25 TransientFileUtilTest() {} 26 virtual ~TransientFileUtilTest() {} 27 28 virtual void SetUp() OVERRIDE { 29 file_system_context_ = CreateFileSystemContextForTesting( 30 NULL, base::FilePath(FILE_PATH_LITERAL("dummy"))); 31 transient_file_util_.reset(new fileapi::TransientFileUtil); 32 33 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 34 } 35 36 virtual void TearDown() OVERRIDE { 37 file_system_context_ = NULL; 38 base::RunLoop().RunUntilIdle(); 39 } 40 41 void CreateAndRegisterTemporaryFile( 42 FileSystemURL* file_url, 43 base::FilePath* file_path) { 44 EXPECT_TRUE(base::CreateTemporaryFileInDir(data_dir_.path(), file_path)); 45 fileapi::IsolatedContext* isolated_context = 46 fileapi::IsolatedContext::GetInstance(); 47 std::string name = "tmp"; 48 std::string fsid = isolated_context->RegisterFileSystemForPath( 49 fileapi::kFileSystemTypeForTransientFile, 50 std::string(), 51 *file_path, 52 &name); 53 ASSERT_TRUE(!fsid.empty()); 54 base::FilePath virtual_path = isolated_context->CreateVirtualRootPath( 55 fsid).AppendASCII(name); 56 *file_url = file_system_context_->CreateCrackedFileSystemURL( 57 GURL("http://foo"), 58 fileapi::kFileSystemTypeIsolated, 59 virtual_path); 60 } 61 62 scoped_ptr<fileapi::FileSystemOperationContext> NewOperationContext() { 63 return make_scoped_ptr( 64 new fileapi::FileSystemOperationContext(file_system_context_.get())); 65 } 66 67 fileapi::FileSystemFileUtil* file_util() { 68 return transient_file_util_.get(); 69 } 70 71 private: 72 base::MessageLoop message_loop_; 73 base::ScopedTempDir data_dir_; 74 scoped_refptr<fileapi::FileSystemContext> file_system_context_; 75 scoped_ptr<fileapi::TransientFileUtil> transient_file_util_; 76 77 DISALLOW_COPY_AND_ASSIGN(TransientFileUtilTest); 78 }; 79 80 TEST_F(TransientFileUtilTest, TransientFile) { 81 FileSystemURL temp_url; 82 base::FilePath temp_path; 83 84 CreateAndRegisterTemporaryFile(&temp_url, &temp_path); 85 86 base::File::Error error; 87 base::File::Info file_info; 88 base::FilePath path; 89 90 // Make sure the file is there. 91 ASSERT_TRUE(temp_url.is_valid()); 92 ASSERT_TRUE(base::PathExists(temp_path)); 93 ASSERT_FALSE(base::DirectoryExists(temp_path)); 94 95 // Create a snapshot file. 96 { 97 webkit_blob::ScopedFile scoped_file = 98 file_util()->CreateSnapshotFile(NewOperationContext().get(), 99 temp_url, 100 &error, 101 &file_info, 102 &path); 103 ASSERT_EQ(base::File::FILE_OK, error); 104 ASSERT_EQ(temp_path, path); 105 ASSERT_FALSE(file_info.is_directory); 106 107 // The file should be still there. 108 ASSERT_TRUE(base::PathExists(temp_path)); 109 ASSERT_EQ(base::File::FILE_OK, 110 file_util()->GetFileInfo(NewOperationContext().get(), 111 temp_url, &file_info, &path)); 112 ASSERT_EQ(temp_path, path); 113 ASSERT_FALSE(file_info.is_directory); 114 } 115 116 // The file's now scoped out. 117 base::RunLoop().RunUntilIdle(); 118 119 // Now the temporary file and the transient filesystem must be gone too. 120 ASSERT_FALSE(base::PathExists(temp_path)); 121 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, 122 file_util()->GetFileInfo(NewOperationContext().get(), 123 temp_url, &file_info, &path)); 124 } 125 126 } // namespace content 127