Flutter Engine
The Flutter Engine
weak_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 "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
12namespace fml {
13namespace {
14
15TEST(WeakNSObjectTest, WeakNSObject) {
16 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
17 WeakNSObjectFactory factory(p1.get());
18 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
19 EXPECT_TRUE(w1);
20 p1.reset();
21 EXPECT_FALSE(w1);
22}
23
24TEST(WeakNSObjectTest, MultipleWeakNSObject) {
25 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
26 WeakNSObjectFactory factory(p1.get());
27 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
28 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
29 WeakNSObject<NSObject> w2(w1);
30 EXPECT_TRUE(w1);
31 EXPECT_TRUE(w2);
32 EXPECT_TRUE(w1.get() == w2.get());
33 p1.reset();
34 EXPECT_FALSE(w1);
35 EXPECT_FALSE(w2);
36}
37
38TEST(WeakNSObjectTest, WeakNSObjectDies) {
39 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
40 WeakNSObjectFactory factory(p1.get());
41 {
42 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
43 EXPECT_TRUE(w1);
44 }
45}
46
47TEST(WeakNSObjectTest, WeakNSObjectReset) {
48 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
49 WeakNSObjectFactory factory(p1.get());
50 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
51 EXPECT_TRUE(w1);
52 w1.reset();
53 EXPECT_FALSE(w1);
54 EXPECT_TRUE(p1);
55 EXPECT_TRUE([p1 description]);
56}
57
58TEST(WeakNSObjectTest, WeakNSObjectEmpty) {
59 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
60 WeakNSObject<NSObject> w1;
61 EXPECT_FALSE(w1);
62 WeakNSObjectFactory factory(p1.get());
63 w1 = factory.GetWeakNSObject();
64 EXPECT_TRUE(w1);
65 p1.reset();
66 EXPECT_FALSE(w1);
67}
68
69TEST(WeakNSObjectTest, WeakNSObjectCopy) {
70 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
71 WeakNSObjectFactory factory(p1.get());
72 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
73 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
74 WeakNSObject<NSObject> w2(w1);
75 EXPECT_TRUE(w1);
76 EXPECT_TRUE(w2);
77 p1.reset();
78 EXPECT_FALSE(w1);
79 EXPECT_FALSE(w2);
80}
81
82TEST(WeakNSObjectTest, WeakNSObjectAssignment) {
83 scoped_nsobject<NSObject> p1([[NSObject alloc] init]);
84 WeakNSObjectFactory factory(p1.get());
85 WeakNSObject<NSObject> w1 = factory.GetWeakNSObject();
86 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
87 WeakNSObject<NSObject> w2 = w1;
88 EXPECT_TRUE(w1);
89 EXPECT_TRUE(w2);
90 p1.reset();
91 EXPECT_FALSE(w1);
92 EXPECT_FALSE(w2);
93}
94} // namespace
95} // namespace fml
static bool init()
Definition: ascii_trie.cc:9
TEST(MallocMapping, EmptyContructor)
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678