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