Flutter Engine
The Flutter Engine
weak_nsobject_arc_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 "flutter/fml/message_loop.h"
6#include "flutter/fml/platform/darwin/scoped_nsobject.h"
7#include "flutter/fml/platform/darwin/weak_nsobject.h"
8#include "flutter/fml/task_runner.h"
9#include "flutter/fml/thread.h"
10#include "gtest/gtest.h"
11
12#if !defined(__has_feature) || !__has_feature(objc_arc)
13#error "This file requires ARC support."
14#endif
15
16namespace fml {
17namespace {
18
19TEST(WeakNSObjectTestARC, WeakNSObject) {
20 scoped_nsobject<NSObject> p1;
21 WeakNSObject<NSObject> w1;
22 @autoreleasepool {
23 p1.reset(([[NSObject alloc] init]));
24 WeakNSObjectFactory factory(p1.get());
25 w1 = factory.GetWeakNSObject();
26 EXPECT_TRUE(w1);
27 p1.reset();
28 }
29 EXPECT_FALSE(w1);
30}
31
32TEST(WeakNSObjectTestARC, MultipleWeakNSObject) {
33 scoped_nsobject<NSObject> p1;
34 WeakNSObject<NSObject> w1;
35 WeakNSObject<NSObject> w2;
36 @autoreleasepool {
37 p1.reset([[NSObject alloc] init]);
38 WeakNSObjectFactory factory(p1.get());
39 w1 = factory.GetWeakNSObject();
40 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
41 w2 = w1;
42 EXPECT_TRUE(w1);
43 EXPECT_TRUE(w2);
44 EXPECT_TRUE(w1.get() == w2.get());
45 p1.reset();
46 }
47 EXPECT_FALSE(w1);
48 EXPECT_FALSE(w2);
49}
50
51TEST(WeakNSObjectTestARC, WeakNSObjectDies) {
52 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
53 WeakNSObjectFactory factory(p1.get());
54 {
55 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
56 EXPECT_TRUE(w1);
57 }
58}
59
60TEST(WeakNSObjectTestARC, WeakNSObjectReset) {
61 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
62 WeakNSObjectFactory factory(p1.get());
63 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
64 EXPECT_TRUE(w1);
65 w1.reset();
66 EXPECT_FALSE(w1);
67 EXPECT_TRUE(p1);
68 EXPECT_TRUE([p1 description]);
69}
70
71TEST(WeakNSObjectTestARC, WeakNSObjectEmpty) {
72 scoped_nsobject<NSObject> p1;
73 WeakNSObject<NSObject> w1;
74 @autoreleasepool {
75 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
76 EXPECT_FALSE(w1);
77 WeakNSObjectFactory factory(p1.get());
78 w1 = factory.GetWeakNSObject();
79 EXPECT_TRUE(w1);
80 p1.reset();
81 }
82 EXPECT_FALSE(w1);
83}
84
85TEST(WeakNSObjectTestARC, WeakNSObjectCopy) {
86 scoped_nsobject<NSObject> p1;
87 WeakNSObject<NSObject> w1;
88 WeakNSObject<NSObject> w2;
89 @autoreleasepool {
90 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
91 WeakNSObjectFactory factory(p1.get());
92 w1 = factory.GetWeakNSObject();
93 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
94 w2 = w1;
95 EXPECT_TRUE(w1);
96 EXPECT_TRUE(w2);
97 p1.reset();
98 }
99 EXPECT_FALSE(w1);
100 EXPECT_FALSE(w2);
101}
102
103TEST(WeakNSObjectTestARC, WeakNSObjectAssignment) {
104 scoped_nsobject<NSObject> p1;
105 WeakNSObject<NSObject> w1;
106 WeakNSObject<NSObject> w2;
107 @autoreleasepool {
108 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
109 WeakNSObjectFactory factory(p1.get());
110 w1 = factory.GetWeakNSObject();
111 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
112 w2 = w1;
113 EXPECT_TRUE(w1);
114 EXPECT_TRUE(w2);
115 p1.reset();
116 }
117 EXPECT_FALSE(w1);
118 EXPECT_FALSE(w2);
119}
120} // namespace
121} // namespace fml
static bool init()
Definition: ascii_trie.cc:9
TEST(MallocMapping, EmptyContructor)
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678