summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/dom/weak_identifier_map_test.cc
blob: fe6f60247f79f548a9132f20594bacd5428d4185 (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
// Copyright 2019 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 "third_party/blink/renderer/core/dom/weak_identifier_map.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

class WeakIdentifierMapTest : public ::testing::Test {
 public:
  class TestClass final : public GarbageCollected<TestClass> {
   public:
    virtual void Trace(Visitor*) const {}
  };

  using TestMap = WeakIdentifierMap<TestClass>;

  void SetUp() override;
  void TearDown() override;

  void CollectGarbage() {
    ThreadState::Current()->CollectAllGarbageForTesting(
        BlinkGC::kNoHeapPointersOnStack);
  }
};

DECLARE_WEAK_IDENTIFIER_MAP(WeakIdentifierMapTest::TestClass);
DEFINE_WEAK_IDENTIFIER_MAP(WeakIdentifierMapTest::TestClass)

void WeakIdentifierMapTest::SetUp() {
  EXPECT_EQ(0u, TestMap::GetSizeForTesting());
}

void WeakIdentifierMapTest::TearDown() {
  CollectGarbage();
  EXPECT_EQ(0u, TestMap::GetSizeForTesting());
}

TEST_F(WeakIdentifierMapTest, Basic) {
  auto* a = MakeGarbageCollected<TestClass>();
  auto* b = MakeGarbageCollected<TestClass>();

  auto id_a = TestMap::Identifier(a);
  EXPECT_NE(0, id_a);
  EXPECT_EQ(id_a, TestMap::Identifier(a));
  EXPECT_EQ(a, TestMap::Lookup(id_a));

  auto id_b = TestMap::Identifier(b);
  EXPECT_NE(0, id_b);
  EXPECT_NE(id_a, id_b);
  EXPECT_EQ(id_b, TestMap::Identifier(b));
  EXPECT_EQ(b, TestMap::Lookup(id_b));

  EXPECT_EQ(id_a, TestMap::Identifier(a));
  EXPECT_EQ(a, TestMap::Lookup(id_a));

  EXPECT_EQ(2u, TestMap::GetSizeForTesting());
}

TEST_F(WeakIdentifierMapTest, NotifyObjectDestroyed) {
  auto* a = MakeGarbageCollected<TestClass>();
  auto id_a = TestMap::Identifier(a);
  TestMap::NotifyObjectDestroyed(a);
  EXPECT_EQ(nullptr, TestMap::Lookup(id_a));

  // Simulate that an object is newly allocated at the same address.
  EXPECT_NE(id_a, TestMap::Identifier(a));
}

TEST_F(WeakIdentifierMapTest, GarbageCollected) {
  auto* a = MakeGarbageCollected<TestClass>();
  auto id_a = TestMap::Identifier(a);

  a = nullptr;
  CollectGarbage();
  EXPECT_EQ(nullptr, TestMap::Lookup(id_a));
}

TEST_F(WeakIdentifierMapTest, UnusedID) {
  auto* a = MakeGarbageCollected<TestClass>();
  auto id_a = TestMap::Identifier(a);
  EXPECT_EQ(nullptr, TestMap::Lookup(id_a + 1));
}

TEST_F(WeakIdentifierMapTest, Overflow) {
  TestMap::SetLastIdForTesting(0);
  auto* a = MakeGarbageCollected<TestClass>();
  EXPECT_EQ(1, TestMap::Identifier(a));
  EXPECT_EQ(a, TestMap::Lookup(1));

  TestMap::SetLastIdForTesting(INT_MAX - 1);
  auto* b = MakeGarbageCollected<TestClass>();
  EXPECT_EQ(INT_MAX, TestMap::Identifier(b));
  EXPECT_EQ(b, TestMap::Lookup(INT_MAX));

  auto* c = MakeGarbageCollected<TestClass>();
  EXPECT_EQ(2, TestMap::Identifier(c));
  EXPECT_EQ(c, TestMap::Lookup(2));

  DCHECK_EQ(3u, TestMap::GetSizeForTesting());
}

}  // namespace blink