Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
closure_unittests.cc
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 "fml/closure.h"
6#include "gtest/gtest.h"
7
8TEST(ScopedCleanupClosureTest, DestructorDoesNothingWhenNoClosureSet) {
10
11 // Nothing should happen.
12}
13
14TEST(ScopedCleanupClosureTest, ReleaseDoesNothingWhenNoClosureSet) {
16
17 // Nothing should happen.
18 EXPECT_EQ(nullptr, cleanup.Release());
19}
20
21TEST(ScopedCleanupClosureTest, ClosureInvokedOnDestructorWhenSetInConstructor) {
22 auto invoked = false;
23
24 {
25 fml::ScopedCleanupClosure cleanup([&invoked]() { invoked = true; });
26
27 EXPECT_FALSE(invoked);
28 }
29
30 EXPECT_TRUE(invoked);
31}
32
33TEST(ScopedCleanupClosureTest, ClosureInvokedOnDestructorWhenSet) {
34 auto invoked = false;
35
36 {
38 cleanup.SetClosure([&invoked]() { invoked = true; });
39
40 EXPECT_FALSE(invoked);
41 }
42
43 EXPECT_TRUE(invoked);
44}
45
46TEST(ScopedCleanupClosureTest, ClosureNotInvokedWhenMoved) {
47 auto invoked = 0;
48
49 {
50 fml::ScopedCleanupClosure cleanup([&invoked]() { invoked++; });
51 fml::ScopedCleanupClosure cleanup2(std::move(cleanup));
52
53 EXPECT_EQ(0, invoked);
54 }
55
56 EXPECT_EQ(1, invoked);
57}
58
59TEST(ScopedCleanupClosureTest, ClosureNotInvokedWhenMovedViaAssignment) {
60 auto invoked = 0;
61
62 {
63 fml::ScopedCleanupClosure cleanup([&invoked]() { invoked++; });
65 cleanup2 = std::move(cleanup);
66
67 EXPECT_EQ(0, invoked);
68 }
69
70 EXPECT_EQ(1, invoked);
71}
#define TEST(S, s, D, expected)
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition closure.h:32
fml::closure SetClosure(const fml::closure &closure)
Definition closure.h:54
fml::closure Release()
Definition closure.h:60
#define EXPECT_TRUE(handle)
Definition unit_test.h:685