summaryrefslogtreecommitdiffstats
path: root/dom/fs/test/gtest/api/TestFileSystemFileHandle.cpp
blob: 263c1f2ed1dc2312e1ded656f3676c5bb87fcac6 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FileSystemMocks.h"
#include "fs/FileSystemChildFactory.h"
#include "gtest/gtest.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/FileSystemFileHandle.h"
#include "mozilla/dom/FileSystemFileHandleBinding.h"
#include "mozilla/dom/FileSystemHandle.h"
#include "mozilla/dom/FileSystemHandleBinding.h"
#include "mozilla/dom/FileSystemManager.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/StorageManager.h"
#include "nsIGlobalObject.h"

namespace mozilla::dom::fs::test {

class TestFileSystemFileHandle : public ::testing::Test {
 protected:
  void SetUp() override {
    // TODO: Fix the test to not depend on CreateFileSystemManagerParent
    // failure because of the pref set to false.
    nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
    prefs->SetBoolPref("dom.fs.enabled", false);

    mRequestHandler = MakeUnique<MockFileSystemRequestHandler>();
    mMetadata =
        FileSystemEntryMetadata("file"_ns, u"File"_ns, /* directory */ false);
    mManager = MakeAndAddRef<FileSystemManager>(mGlobal, nullptr);
  }

  void TearDown() override {
    nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
    prefs->SetBoolPref("dom.fs.enabled", true);

    if (!mManager->IsShutdown()) {
      mManager->Shutdown();
    }
  }

  nsIGlobalObject* mGlobal = GetGlobal();
  UniquePtr<MockFileSystemRequestHandler> mRequestHandler;
  FileSystemEntryMetadata mMetadata;
  RefPtr<FileSystemManager> mManager;
};

TEST_F(TestFileSystemFileHandle, constructFileHandleRefPointer) {
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);
}

TEST_F(TestFileSystemFileHandle, isHandleKindFile) {
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);

  ASSERT_EQ(FileSystemHandleKind::File, fileHandle->Kind());
}

TEST_F(TestFileSystemFileHandle, isFileReturned) {
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);

  IgnoredErrorResult rv;
  RefPtr<Promise> promise = fileHandle->GetFile(rv);

  ASSERT_TRUE(rv.ErrorCodeIs(NS_OK));
}

TEST_F(TestFileSystemFileHandle, doesGetFileFailOnNullGlobal) {
  mGlobal = nullptr;
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);

  IgnoredErrorResult rv;
  RefPtr<Promise> promise = fileHandle->GetFile(rv);

  ASSERT_TRUE(rv.ErrorCodeIs(NS_ERROR_UNEXPECTED));
}

TEST_F(TestFileSystemFileHandle, isWritableReturned) {
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);

  FileSystemCreateWritableOptions options;
  IgnoredErrorResult rv;
  RefPtr<Promise> promise = fileHandle->CreateWritable(options, rv);

  ASSERT_TRUE(rv.ErrorCodeIs(NS_OK));
}

TEST_F(TestFileSystemFileHandle, doesCreateWritableFailOnNullGlobal) {
  mGlobal = nullptr;
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);

  FileSystemCreateWritableOptions options;
  IgnoredErrorResult rv;
  RefPtr<Promise> promise = fileHandle->CreateWritable(options, rv);

  ASSERT_TRUE(rv.ErrorCodeIs(NS_ERROR_UNEXPECTED));
}

TEST_F(TestFileSystemFileHandle, isSyncAccessHandleReturned) {
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);

  IgnoredErrorResult rv;
  RefPtr<Promise> promise = fileHandle->CreateSyncAccessHandle(rv);

  ASSERT_TRUE(rv.ErrorCodeIs(NS_OK));
}

TEST_F(TestFileSystemFileHandle, doesCreateSyncAccessHandleFailOnNullGlobal) {
  mGlobal = nullptr;
  RefPtr<FileSystemFileHandle> fileHandle = MakeAndAddRef<FileSystemFileHandle>(
      mGlobal, mManager, mMetadata, mRequestHandler.release());

  ASSERT_TRUE(fileHandle);

  IgnoredErrorResult rv;
  RefPtr<Promise> promise = fileHandle->CreateSyncAccessHandle(rv);

  ASSERT_TRUE(rv.ErrorCodeIs(NS_ERROR_UNEXPECTED));
}

}  // namespace mozilla::dom::fs::test