| Index: third_party/WebKit/Source/wtf/MakeCancellableTest.cpp
|
| diff --git a/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp b/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a3b05f4e093b05ab077fbe824a92261147db7527
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp
|
| @@ -0,0 +1,117 @@
|
| +// Copyright 2016 The Chromium 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 "wtf/MakeCancellable.h"
|
| +
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace WTF {
|
| +namespace {
|
| +
|
| +void add(int* x, int y) { *x += y; }
|
| +
|
| +class DestructionCounter {
|
| +public:
|
| + explicit DestructionCounter(int* counter) : m_counter(counter) { }
|
| + ~DestructionCounter()
|
| + {
|
| + if (m_counter)
|
| + ++*m_counter;
|
| + }
|
| +
|
| + DestructionCounter(DestructionCounter&& other) : m_counter(other.m_counter)
|
| + {
|
| + other.m_counter = nullptr;
|
| + }
|
| +
|
| +private:
|
| + int* m_counter;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +TEST(MakeCancellableTest, NotCancelled)
|
| +{
|
| + int v = 0;
|
| + auto f = bind(&add, unretained(&v));
|
| + RefPtr<FunctionCanceller> canceller;
|
| + std::tie(f, canceller) = makeCancellable(std::move(f));
|
| +
|
| + EXPECT_EQ(0, v);
|
| + (*f)(3);
|
| + EXPECT_EQ(3, v);
|
| +}
|
| +
|
| +TEST(MakeCancellableTest, ExplicitCancel)
|
| +{
|
| + int v = 0;
|
| + auto f = bind(&add, unretained(&v));
|
| + RefPtr<FunctionCanceller> canceller;
|
| + std::tie(f, canceller) = makeCancellable(std::move(f));
|
| +
|
| + canceller->cancel();
|
| + EXPECT_EQ(0, v);
|
| + (*f)(3);
|
| + EXPECT_EQ(0, v);
|
| +}
|
| +
|
| +TEST(MakeCancellableTest, ScopeOutCancel)
|
| +{
|
| + int v = 0;
|
| + auto f = bind(&add, unretained(&v));
|
| + {
|
| + RefPtr<FunctionCanceller> canceller;
|
| + std::tie(f, canceller) = makeCancellable(std::move(f));
|
| +
|
| + ScopedFunctionCanceller scopedCanceller(canceller);
|
| + }
|
| +
|
| + EXPECT_EQ(0, v);
|
| + (*f)(3);
|
| + EXPECT_EQ(0, v);
|
| +}
|
| +
|
| +TEST(MakeCancellableTest, Detach)
|
| +{
|
| + int v = 0;
|
| + auto f = bind(&add, unretained(&v));
|
| + {
|
| + RefPtr<FunctionCanceller> canceller;
|
| + std::tie(f, canceller) = makeCancellable(std::move(f));
|
| +
|
| + ScopedFunctionCanceller scopedCanceller(canceller);
|
| + scopedCanceller.detach();
|
| + }
|
| +
|
| + EXPECT_EQ(0, v);
|
| + (*f)(3);
|
| + EXPECT_EQ(3, v);
|
| +}
|
| +
|
| +TEST(MakeCancellableTest, MultiCall)
|
| +{
|
| + int v = 0;
|
| + auto f = bind(&add, unretained(&v));
|
| + std::tie(f, std::ignore) = makeCancellable(std::move(f));
|
| +
|
| + EXPECT_EQ(0, v);
|
| + (*f)(2);
|
| + EXPECT_EQ(2, v);
|
| + (*f)(3);
|
| + EXPECT_EQ(5, v);
|
| +}
|
| +
|
| +TEST(MakeCancellableTest, DestroyOnCancel)
|
| +{
|
| + int counter = 0;
|
| + auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter));
|
| + RefPtr<FunctionCanceller> canceller;
|
| + std::tie(f, canceller) = makeCancellable(std::move(f));
|
| +
|
| + EXPECT_EQ(0, counter);
|
| + canceller->cancel();
|
| + EXPECT_EQ(1, counter);
|
| +}
|
| +
|
| +} // namespace WTF
|
|
|