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 "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/platform_file.h"
11 #include "base/run_loop.h"
12 #include "content/public/test/test_file_system_context.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webkit/browser/fileapi/file_system_context.h"
15 #include "webkit/browser/fileapi/file_system_operation_context.h"
16 #include "webkit/browser/fileapi/isolated_context.h"
17 #include "webkit/browser/fileapi/transient_file_util.h"
18 #include "webkit/common/blob/scoped_file.h"
20 using fileapi::FileSystemURL
;
24 class TransientFileUtilTest
: public testing::Test
{
26 TransientFileUtilTest() {}
27 virtual ~TransientFileUtilTest() {}
29 virtual void SetUp() OVERRIDE
{
30 file_system_context_
= CreateFileSystemContextForTesting(
31 NULL
, base::FilePath(FILE_PATH_LITERAL("dummy")));
32 transient_file_util_
.reset(new fileapi::TransientFileUtil
);
34 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
37 virtual void TearDown() OVERRIDE
{
38 file_system_context_
= NULL
;
39 base::RunLoop().RunUntilIdle();
42 void CreateAndRegisterTemporaryFile(
43 FileSystemURL
* file_url
,
44 base::FilePath
* file_path
) {
45 EXPECT_TRUE(base::CreateTemporaryFileInDir(data_dir_
.path(), file_path
));
46 fileapi::IsolatedContext
* isolated_context
=
47 fileapi::IsolatedContext::GetInstance();
48 std::string name
= "tmp";
49 std::string fsid
= isolated_context
->RegisterFileSystemForPath(
50 fileapi::kFileSystemTypeForTransientFile
,
53 ASSERT_TRUE(!fsid
.empty());
54 base::FilePath virtual_path
= isolated_context
->CreateVirtualRootPath(
55 fsid
).AppendASCII(name
);
56 *file_url
= file_system_context_
->CreateCrackedFileSystemURL(
58 fileapi::kFileSystemTypeIsolated
,
62 scoped_ptr
<fileapi::FileSystemOperationContext
> NewOperationContext() {
63 return make_scoped_ptr(
64 new fileapi::FileSystemOperationContext(file_system_context_
.get()));
67 fileapi::FileSystemFileUtil
* file_util() {
68 return transient_file_util_
.get();
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_
;
77 DISALLOW_COPY_AND_ASSIGN(TransientFileUtilTest
);
80 TEST_F(TransientFileUtilTest
, TransientFile
) {
81 FileSystemURL temp_url
;
82 base::FilePath temp_path
;
84 CreateAndRegisterTemporaryFile(&temp_url
, &temp_path
);
86 base::File::Error error
;
87 base::File::Info file_info
;
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
));
95 // Create a snapshot file.
97 webkit_blob::ScopedFile scoped_file
=
98 file_util()->CreateSnapshotFile(NewOperationContext().get(),
103 ASSERT_EQ(base::File::FILE_OK
, error
);
104 ASSERT_EQ(temp_path
, path
);
105 ASSERT_FALSE(file_info
.is_directory
);
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
);
116 // The file's now scoped out.
117 base::RunLoop().RunUntilIdle();
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
));
126 } // namespace content