summaryrefslogtreecommitdiffstats
path: root/dom/fs/test/gtest/shared/TestFileSystemHelpers.cpp
blob: ed380266347cd562d0319c136d707e8fe95d4b27 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- 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 "gtest/gtest.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/FileSystemHelpers.h"

namespace mozilla::dom::fs {

namespace {

class TestObject {
 public:
  explicit TestObject(uint32_t aExpectedAddRefCnt = 0,
                      uint32_t aExpectedAddRegCnt = 0)
      : mExpectedAddRefCnt(aExpectedAddRefCnt),
        mExpectedAddRegCnt(aExpectedAddRegCnt),
        mAddRefCnt(0),
        mAddRegCnt(0),
        mRefCnt(0),
        mRegCnt(0),
        mClosed(false) {}

  uint32_t AddRef() {
    mRefCnt++;
    mAddRefCnt++;
    return mRefCnt;
  }

  uint32_t Release() {
    EXPECT_TRUE(mRefCnt > 0);
    mRefCnt--;
    if (mRefCnt == 0) {
      delete this;
      return 0;
    }
    return mRefCnt;
  }

  void Register() {
    EXPECT_FALSE(mClosed);
    mRegCnt++;
    mAddRegCnt++;
  }

  void Unregister() {
    EXPECT_FALSE(mClosed);
    EXPECT_TRUE(mRegCnt > 0);
    mRegCnt--;
    if (mRegCnt == 0) {
      mClosed = true;
    }
  }

  void Foo() const {}

 private:
  ~TestObject() {
    if (mExpectedAddRefCnt > 0) {
      EXPECT_EQ(mAddRefCnt, mExpectedAddRefCnt);
    }
    if (mExpectedAddRegCnt > 0) {
      EXPECT_EQ(mAddRegCnt, mExpectedAddRegCnt);
    }
  }

  uint32_t mExpectedAddRefCnt;
  uint32_t mExpectedAddRegCnt;
  uint32_t mAddRefCnt;
  uint32_t mAddRegCnt;
  uint32_t mRefCnt;
  uint32_t mRegCnt;
  bool mClosed;
};

}  // namespace

TEST(TestFileSystemHelpers_Registered, Construct_Default)
{
  { Registered<TestObject> testObject; }
}

TEST(TestFileSystemHelpers_Registered, Construct_Copy)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(2, 2));
    Registered<TestObject> testObject2(testObject1);
    testObject2 = nullptr;
  }
}

TEST(TestFileSystemHelpers_Registered, Construct_Move)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(1, 1));
    Registered<TestObject> testObject2(std::move(testObject1));
  }
}

TEST(TestFileSystemHelpers_Registered, Construct_FromRefPtr)
{
  { Registered<TestObject> testObject(MakeRefPtr<TestObject>(1, 1)); }
}

TEST(TestFileSystemHelpers_Registered, Operator_Assign_FromNullPtr)
{
  {
    Registered<TestObject> testObject;
    testObject = nullptr;
  }
}

TEST(TestFileSystemHelpers_Registered, Operator_Assign_Copy)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(2, 2));
    Registered<TestObject> testObject2;
    testObject2 = testObject1;
  }
}

TEST(TestFileSystemHelpers_Registered, Operator_Assign_Move)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(1, 1));
    Registered<TestObject> testObject2;
    testObject2 = std::move(testObject1);
  }
}

TEST(TestFileSystemHelpers_Registered, Method_Inspect)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(1, 1));
    const RefPtr<TestObject>& testObject2 = testObject1.inspect();
    Unused << testObject2;
  }
}

TEST(TestFileSystemHelpers_Registered, Method_Unwrap)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(1, 1));
    RefPtr<TestObject> testObject2 = testObject1.unwrap();
    Unused << testObject2;
  }
}

TEST(TestFileSystemHelpers_Registered, Method_Get)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(1, 1));
    TestObject* testObject2 = testObject1.get();
    Unused << testObject2;
  }
}

TEST(TestFileSystemHelpers_Registered, Operator_Conversion_ToRawPtr)
{
  {
    Registered<TestObject> testObject1(MakeRefPtr<TestObject>(1, 1));
    TestObject* testObject2 = testObject1;
    Unused << testObject2;
  }
}

TEST(TestFileSystemHelpers_Registered, Operator_Dereference_ToRawPtr)
{
  {
    Registered<TestObject> testObject(MakeRefPtr<TestObject>(1, 1));
    testObject->Foo();
  }
}

}  // namespace mozilla::dom::fs