summaryrefslogtreecommitdiffstats
path: root/content/browser/fileapi/transient_file_util_unittest.cc
blob: 5be21fa9d8703887d7e468540f9b12d1ef82d6ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "content/public/test/test_file_system_context.h"
#include "storage/browser/fileapi/file_system_context.h"
#include "storage/browser/fileapi/file_system_operation_context.h"
#include "storage/browser/fileapi/isolated_context.h"
#include "storage/browser/fileapi/transient_file_util.h"
#include "storage/common/blob/scoped_file.h"
#include "testing/gtest/include/gtest/gtest.h"

using storage::FileSystemURL;

namespace content {

class TransientFileUtilTest : public testing::Test {
 public:
  TransientFileUtilTest() {}
  virtual ~TransientFileUtilTest() {}

  virtual void SetUp() OVERRIDE {
    file_system_context_ = CreateFileSystemContextForTesting(
        NULL, base::FilePath(FILE_PATH_LITERAL("dummy")));
    transient_file_util_.reset(new storage::TransientFileUtil);

    ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
  }

  virtual void TearDown() OVERRIDE {
    file_system_context_ = NULL;
    base::RunLoop().RunUntilIdle();
  }

  void CreateAndRegisterTemporaryFile(
      FileSystemURL* file_url,
      base::FilePath* file_path) {
    EXPECT_TRUE(base::CreateTemporaryFileInDir(data_dir_.path(), file_path));
    storage::IsolatedContext* isolated_context =
        storage::IsolatedContext::GetInstance();
    std::string name = "tmp";
    std::string fsid = isolated_context->RegisterFileSystemForPath(
        storage::kFileSystemTypeForTransientFile,
        std::string(),
        *file_path,
        &name);
    ASSERT_TRUE(!fsid.empty());
    base::FilePath virtual_path = isolated_context->CreateVirtualRootPath(
        fsid).AppendASCII(name);
    *file_url = file_system_context_->CreateCrackedFileSystemURL(
        GURL("http://foo"), storage::kFileSystemTypeIsolated, virtual_path);
  }

  scoped_ptr<storage::FileSystemOperationContext> NewOperationContext() {
    return make_scoped_ptr(
        new storage::FileSystemOperationContext(file_system_context_.get()));
  }

  storage::FileSystemFileUtil* file_util() {
    return transient_file_util_.get();
  }

 private:
  base::MessageLoop message_loop_;
  base::ScopedTempDir data_dir_;
  scoped_refptr<storage::FileSystemContext> file_system_context_;
  scoped_ptr<storage::TransientFileUtil> transient_file_util_;

  DISALLOW_COPY_AND_ASSIGN(TransientFileUtilTest);
};

TEST_F(TransientFileUtilTest, TransientFile) {
  FileSystemURL temp_url;
  base::FilePath temp_path;

  CreateAndRegisterTemporaryFile(&temp_url, &temp_path);

  base::File::Error error;
  base::File::Info file_info;
  base::FilePath path;

  // Make sure the file is there.
  ASSERT_TRUE(temp_url.is_valid());
  ASSERT_TRUE(base::PathExists(temp_path));
  ASSERT_FALSE(base::DirectoryExists(temp_path));

  // Create a snapshot file.
  {
    storage::ScopedFile scoped_file = file_util()->CreateSnapshotFile(
        NewOperationContext().get(), temp_url, &error, &file_info, &path);
    ASSERT_EQ(base::File::FILE_OK, error);
    ASSERT_EQ(temp_path, path);
    ASSERT_FALSE(file_info.is_directory);

    // The file should be still there.
    ASSERT_TRUE(base::PathExists(temp_path));
    ASSERT_EQ(base::File::FILE_OK,
              file_util()->GetFileInfo(NewOperationContext().get(),
                                       temp_url, &file_info, &path));
    ASSERT_EQ(temp_path, path);
    ASSERT_FALSE(file_info.is_directory);
  }

  // The file's now scoped out.
  base::RunLoop().RunUntilIdle();

  // Now the temporary file and the transient filesystem must be gone too.
  ASSERT_FALSE(base::PathExists(temp_path));
  ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND,
            file_util()->GetFileInfo(NewOperationContext().get(),
                                     temp_url, &file_info, &path));
}

}  // namespace content