Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Make fml::ScopedCleanupClosure std::move-able and add unit tests. #45772

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
../../../flutter/fml/ascii_trie_unittests.cc
../../../flutter/fml/backtrace_unittests.cc
../../../flutter/fml/base32_unittest.cc
../../../flutter/fml/closure_unittests.cc
../../../flutter/fml/command_line_unittest.cc
../../../flutter/fml/container_unittests.cc
../../../flutter/fml/endianness_unittests.cc
Expand Down
1 change: 1 addition & 0 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ if (enable_unittests) {
"ascii_trie_unittests.cc",
"backtrace_unittests.cc",
"base32_unittest.cc",
"closure_unittests.cc",
"command_line_unittest.cc",
"container_unittests.cc",
"endianness_unittests.cc",
Expand Down
11 changes: 10 additions & 1 deletion fml/closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ using closure = std::function<void()>;
/// automatically at the end of the scope. This covers the cases
/// where there are early returns as well.
///
class ScopedCleanupClosure {
class ScopedCleanupClosure final {
public:
ScopedCleanupClosure() = default;

ScopedCleanupClosure(ScopedCleanupClosure&& other) {
closure_ = other.Release();
}

ScopedCleanupClosure& operator=(ScopedCleanupClosure&& other) {
closure_ = other.Release();
return *this;
}

explicit ScopedCleanupClosure(const fml::closure& closure)
: closure_(closure) {}

Expand Down
71 changes: 71 additions & 0 deletions fml/closure_unittests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fml/closure.h"
#include "gtest/gtest.h"

TEST(ScopedCleanupClosureTest, DestructorDoesNothingWhenNoClosureSet) {
fml::ScopedCleanupClosure cleanup;

// Nothing should happen.
}

TEST(ScopedCleanupClosureTest, ReleaseDoesNothingWhenNoClosureSet) {
fml::ScopedCleanupClosure cleanup;

// Nothing should happen.
EXPECT_EQ(nullptr, cleanup.Release());
}

TEST(ScopedCleanupClosureTest, ClosureInvokedOnDestructorWhenSetInConstructor) {
auto invoked = false;

{
fml::ScopedCleanupClosure cleanup([&invoked]() { invoked = true; });

EXPECT_FALSE(invoked);
}

EXPECT_TRUE(invoked);
}

TEST(ScopedCleanupClosureTest, ClosureInvokedOnDestructorWhenSet) {
auto invoked = false;

{
fml::ScopedCleanupClosure cleanup;
cleanup.SetClosure([&invoked]() { invoked = true; });

EXPECT_FALSE(invoked);
}

EXPECT_TRUE(invoked);
}

TEST(ScopedCleanupClosureTest, ClosureNotInvokedWhenMoved) {
auto invoked = 0;

{
fml::ScopedCleanupClosure cleanup([&invoked]() { invoked++; });
fml::ScopedCleanupClosure cleanup2(std::move(cleanup));

EXPECT_EQ(0, invoked);
}

EXPECT_EQ(1, invoked);
}

TEST(ScopedCleanupClosureTest, ClosureNotInvokedWhenMovedViaAssignment) {
auto invoked = 0;

{
fml::ScopedCleanupClosure cleanup([&invoked]() { invoked++; });
fml::ScopedCleanupClosure cleanup2;
cleanup2 = std::move(cleanup);

EXPECT_EQ(0, invoked);
}

EXPECT_EQ(1, invoked);
}