[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / fileapi / transient_file_util_unittest.cc
blob2ed8b74649569939fe5c017815baf91dbffb4e54
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/files/file_path.h"
7 #include "base/files/file_util.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 "storage/browser/blob/scoped_file.h"
13 #include "storage/browser/fileapi/file_system_context.h"
14 #include "storage/browser/fileapi/file_system_operation_context.h"
15 #include "storage/browser/fileapi/isolated_context.h"
16 #include "storage/browser/fileapi/transient_file_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using storage::FileSystemURL;
21 namespace content {
23 class TransientFileUtilTest : public testing::Test {
24 public:
25 TransientFileUtilTest() {}
26 ~TransientFileUtilTest() override {}
28 void SetUp() override {
29 file_system_context_ = CreateFileSystemContextForTesting(
30 NULL, base::FilePath(FILE_PATH_LITERAL("dummy")));
31 transient_file_util_.reset(new storage::TransientFileUtil);
33 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
36 void TearDown() override {
37 file_system_context_ = NULL;
38 base::RunLoop().RunUntilIdle();
41 void CreateAndRegisterTemporaryFile(
42 FileSystemURL* file_url,
43 base::FilePath* file_path) {
44 EXPECT_TRUE(base::CreateTemporaryFileInDir(data_dir_.path(), file_path));
45 storage::IsolatedContext* isolated_context =
46 storage::IsolatedContext::GetInstance();
47 std::string name = "tmp";
48 std::string fsid = isolated_context->RegisterFileSystemForPath(
49 storage::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"), storage::kFileSystemTypeIsolated, virtual_path);
60 scoped_ptr<storage::FileSystemOperationContext> NewOperationContext() {
61 return make_scoped_ptr(
62 new storage::FileSystemOperationContext(file_system_context_.get()));
65 storage::FileSystemFileUtil* file_util() {
66 return transient_file_util_.get();
69 private:
70 base::MessageLoop message_loop_;
71 base::ScopedTempDir data_dir_;
72 scoped_refptr<storage::FileSystemContext> file_system_context_;
73 scoped_ptr<storage::TransientFileUtil> transient_file_util_;
75 DISALLOW_COPY_AND_ASSIGN(TransientFileUtilTest);
78 TEST_F(TransientFileUtilTest, TransientFile) {
79 FileSystemURL temp_url;
80 base::FilePath temp_path;
82 CreateAndRegisterTemporaryFile(&temp_url, &temp_path);
84 base::File::Error error;
85 base::File::Info file_info;
86 base::FilePath path;
88 // Make sure the file is there.
89 ASSERT_TRUE(temp_url.is_valid());
90 ASSERT_TRUE(base::PathExists(temp_path));
91 ASSERT_FALSE(base::DirectoryExists(temp_path));
93 // Create a snapshot file.
95 storage::ScopedFile scoped_file = file_util()->CreateSnapshotFile(
96 NewOperationContext().get(), temp_url, &error, &file_info, &path);
97 ASSERT_EQ(base::File::FILE_OK, error);
98 ASSERT_EQ(temp_path, path);
99 ASSERT_FALSE(file_info.is_directory);
101 // The file should be still there.
102 ASSERT_TRUE(base::PathExists(temp_path));
103 ASSERT_EQ(base::File::FILE_OK,
104 file_util()->GetFileInfo(NewOperationContext().get(),
105 temp_url, &file_info, &path));
106 ASSERT_EQ(temp_path, path);
107 ASSERT_FALSE(file_info.is_directory);
110 // The file's now scoped out.
111 base::RunLoop().RunUntilIdle();
113 // Now the temporary file and the transient filesystem must be gone too.
114 ASSERT_FALSE(base::PathExists(temp_path));
115 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND,
116 file_util()->GetFileInfo(NewOperationContext().get(),
117 temp_url, &file_info, &path));
120 } // namespace content