Flutter Engine
 
Loading...
Searching...
No Matches
cf_utils_unittests.mm
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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.
4
6
8
9namespace fml {
10
11// Test state used in CFTest.SupportsCustomRetainRelease.
16
17// Template specialization used in CFTest.SupportsCustomRetainRelease.
18template <>
20 static constexpr CFRefTestState* kNullValue = nullptr;
21 static void Retain(CFRefTestState* instance) { instance->retain_called = true; }
22 static void Release(CFRefTestState* instance) { instance->release_called = true; }
23};
24
25namespace testing {
26
27TEST(CFTest, CanCreateRefs) {
28 CFRef<CFMutableStringRef> string(CFStringCreateMutable(kCFAllocatorDefault, 100u));
29 // Cast
30 ASSERT_TRUE(static_cast<bool>(string));
31 ASSERT_TRUE(string);
32
33 const auto ref_count = CFGetRetainCount(string);
34
35 // Copy & Reset
36 {
37 CFRef<CFMutableStringRef> string2 = string;
38 ASSERT_TRUE(string2);
39 ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
40 ASSERT_EQ(CFGetRetainCount(string2), CFGetRetainCount(string));
41
42 string2.Reset();
43 ASSERT_FALSE(string2);
44 ASSERT_EQ(ref_count, CFGetRetainCount(string));
45 }
46
47 // Release
48 {
49 auto string3 = string;
50 ASSERT_TRUE(string3);
51 ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
52 auto raw_string3 = string3.Release();
53 ASSERT_FALSE(string3);
54 ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
55 CFRelease(raw_string3);
56 ASSERT_EQ(ref_count, CFGetRetainCount(string));
57 }
58
59 // Move
60 {
61 auto string_source = string;
62 ASSERT_TRUE(string_source);
63 auto string_move = std::move(string_source);
64 ASSERT_FALSE(string_source); // NOLINT(bugprone-use-after-move)
65 ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
66 string_move.Reset();
67 ASSERT_EQ(ref_count, CFGetRetainCount(string));
68 }
69
70 // Move assign.
71 {
72 auto string_move_assign = std::move(string);
73 ASSERT_FALSE(string); // NOLINT(bugprone-use-after-move)
74 ASSERT_EQ(ref_count, CFGetRetainCount(string_move_assign));
75 }
76}
77
78TEST(CFTest, GetReturnsUnderlyingObject) {
79 CFMutableStringRef cf_string = CFStringCreateMutable(kCFAllocatorDefault, 100u);
80 const CFIndex ref_count_before = CFGetRetainCount(cf_string);
81 CFRef<CFMutableStringRef> string_ref(cf_string);
82
83 CFMutableStringRef returned_string = string_ref.Get();
84 const CFIndex ref_count_after = CFGetRetainCount(cf_string);
85 EXPECT_EQ(cf_string, returned_string);
86 EXPECT_EQ(ref_count_before, ref_count_after);
87}
88
89TEST(CFTest, RetainSharesOwnership) {
90 CFMutableStringRef cf_string = CFStringCreateMutable(kCFAllocatorDefault, 100u);
91 const CFIndex ref_count_before = CFGetRetainCount(cf_string);
92
94 string_ref.Retain(cf_string);
95 const CFIndex ref_count_after = CFGetRetainCount(cf_string);
96
97 EXPECT_EQ(cf_string, string_ref);
98 EXPECT_EQ(ref_count_before + 1u, ref_count_after);
99}
100
101TEST(CFTest, SupportsCustomRetainRelease) {
104 ASSERT_EQ(ref.Get(), &instance);
105 ASSERT_FALSE(instance.retain_called);
106 ASSERT_FALSE(instance.release_called);
107 ref.Reset();
108 ASSERT_EQ(ref.Get(), nullptr);
109 ASSERT_FALSE(instance.retain_called);
110 ASSERT_TRUE(instance.release_called);
111
112 CFRefTestState other_instance{};
113 ref.Retain(&other_instance);
114 ASSERT_EQ(ref.Get(), &other_instance);
115 ASSERT_TRUE(other_instance.retain_called);
116 ASSERT_FALSE(other_instance.release_called);
117}
118
119} // namespace testing
120} // namespace fml
void Retain(T instance=CFRefTraits< T >::kNullValue)
Definition cf_utils.h:77
void Reset(T instance=CFRefTraits< T >::kNullValue)
Definition cf_utils.h:68
T Get() const
Definition cf_utils.h:101
VkInstance instance
Definition main.cc:64
TEST(BacktraceTest, CanGatherBacktrace)
static void Retain(CFRefTestState *instance)
static void Release(CFRefTestState *instance)
Default retain and release implementations for CFRef.
Definition cf_utils.h:16