Flutter Engine
The Flutter Engine
scoped_nsobject_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
5#include <vector>
6
7#include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
8#include "flutter/fml/platform/darwin/scoped_nsobject.h"
9#include "gtest/gtest.h"
10
11namespace {
12
13// This is to suppress the bugprone-use-after-move warning.
14// This strategy is recommanded here:
15// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/use-after-move.html#silencing-erroneous-warnings
16template <class T>
17void IS_INITIALIZED(T&) {}
18
19TEST(ScopedNSObjectTest, ScopedNSObject) {
20 fml::scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
21 ASSERT_TRUE(p1.get());
22 ASSERT_EQ(1u, [p1 retainCount]);
24 ASSERT_EQ(p1.get(), p2.get());
25 ASSERT_EQ(2u, [p1 retainCount]);
26 p2.reset();
27 ASSERT_EQ(nil, p2.get());
28 ASSERT_EQ(1u, [p1 retainCount]);
29 {
31 ASSERT_EQ(p1.get(), p3.get());
32 ASSERT_EQ(2u, [p1 retainCount]);
33 p3 = p1;
34 ASSERT_EQ(p1.get(), p3.get());
35 ASSERT_EQ(2u, [p1 retainCount]);
36 }
37 ASSERT_EQ(1u, [p1 retainCount]);
38 fml::scoped_nsobject<NSObject> p4([p1.get() retain]);
39 ASSERT_EQ(2u, [p1 retainCount]);
40 ASSERT_TRUE(p1 == p1.get());
41 ASSERT_TRUE(p1 == p1);
42 ASSERT_FALSE(p1 != p1);
43 ASSERT_FALSE(p1 != p1.get());
44 fml::scoped_nsobject<NSObject> p5([[NSObject alloc] init]);
45 ASSERT_TRUE(p1 != p5);
46 ASSERT_TRUE(p1 != p5.get());
47 ASSERT_FALSE(p1 == p5);
48 ASSERT_FALSE(p1 == p5.get());
49
51 ASSERT_EQ(3u, [p6 retainCount]);
52 {
54 p6.autorelease();
55 ASSERT_EQ(nil, p6.get());
56 ASSERT_EQ(3u, [p1 retainCount]);
57 }
58 ASSERT_EQ(2u, [p1 retainCount]);
59
60 fml::scoped_nsobject<NSObject> p7([[NSObject alloc] init]);
61 fml::scoped_nsobject<NSObject> p8(std::move(p7));
62 ASSERT_TRUE(p8);
63 ASSERT_EQ(1u, [p8 retainCount]);
64 IS_INITIALIZED(p7);
65 ASSERT_FALSE(p7.get());
66}
67
68// Instantiating scoped_nsobject<> with T=NSAutoreleasePool should trip a
69// static_assert.
70#if 0
71TEST(ScopedNSObjectTest, FailToCreateScopedNSObjectAutoreleasePool) {
73}
74#endif
75
76TEST(ScopedNSObjectTest, ScopedNSObjectInContainer) {
77 fml::scoped_nsobject<id> p([[NSObject alloc] init]);
78 ASSERT_TRUE(p.get());
79 ASSERT_EQ(1u, [p retainCount]);
80 {
81 std::vector<fml::scoped_nsobject<id>> objects;
82 objects.push_back(p);
83 ASSERT_EQ(2u, [p retainCount]);
84 ASSERT_EQ(p.get(), objects[0].get());
85 objects.push_back(fml::scoped_nsobject<id>([[NSObject alloc] init]));
86 ASSERT_TRUE(objects[1].get());
87 ASSERT_EQ(1u, [objects[1] retainCount]);
88 }
89 ASSERT_EQ(1u, [p retainCount]);
90}
91
92TEST(ScopedNSObjectTest, ScopedNSObjectFreeFunctions) {
93 fml::scoped_nsobject<id> p1([[NSObject alloc] init]);
94 id o1 = p1.get();
95 ASSERT_TRUE(o1 == p1);
96 ASSERT_FALSE(o1 != p1);
97 fml::scoped_nsobject<id> p2([[NSObject alloc] init]);
98 ASSERT_TRUE(o1 != p2);
99 ASSERT_FALSE(o1 == p2);
100 id o2 = p2.get();
101 swap(p1, p2);
102 ASSERT_EQ(o2, p1.get());
103 ASSERT_EQ(o1, p2.get());
104}
105
106} // namespace
AutoreleasePool pool
#define TEST(S, s, D, expected)
Definition: FitsInTest.cpp:15
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
T get() const __attribute((ns_returns_not_retained))
NST autorelease() __attribute((ns_returns_not_retained))
static bool init()
const myers::Point & get(const myers::Segment &)
#define T
Definition: precompiler.cc:65