Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: third_party/WebKit/Source/wtf/MakeCancellableTest.cpp

Issue 2177283005: Add WTF::makeCancellable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +test, +DCHECK Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium 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 "wtf/MakeCancellable.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace WTF {
10 namespace {
11
12 void add(int* x, int y) { *x += y; }
13
14 class DestructionCounter {
15 public:
16 explicit DestructionCounter(int* counter) : m_counter(counter) { }
17 ~DestructionCounter()
18 {
19 if (m_counter)
20 ++*m_counter;
21 }
22
23 DestructionCounter(DestructionCounter&& other) : m_counter(other.m_counter)
24 {
25 other.m_counter = nullptr;
26 }
27
28 private:
29 int* m_counter;
30 };
31
32 } // namespace
33
34 TEST(MakeCancellableTest, NotCancelled)
35 {
36 int v = 0;
37 auto f = bind(&add, unretained(&v));
38 FunctionCanceller canceller;
39 std::tie(f, canceller) = makeCancellable(std::move(f));
40
41 EXPECT_EQ(0, v);
42 (*f)(3);
43 EXPECT_EQ(3, v);
44 }
45
46 TEST(MakeCancellableTest, ExplicitCancel)
47 {
48 int v = 0;
49 auto f = bind(&add, unretained(&v));
50 FunctionCanceller canceller;
51 std::tie(f, canceller) = makeCancellable(std::move(f));
52
53 canceller.cancel();
54 EXPECT_EQ(0, v);
55 (*f)(3);
56 EXPECT_EQ(0, v);
57 }
58
59 TEST(MakeCancellableTest, ScopeOutCancel)
60 {
61 int v = 0;
62 auto f = bind(&add, unretained(&v));
63 {
64 FunctionCanceller canceller;
65 std::tie(f, canceller) = makeCancellable(std::move(f));
66 }
67
68 EXPECT_EQ(0, v);
69 (*f)(3);
70 EXPECT_EQ(0, v);
71 }
72
73 TEST(MakeCancellableTest, Detach)
74 {
75 int v = 0;
76 auto f = bind(&add, unretained(&v));
77 {
78 FunctionCanceller canceller;
79 std::tie(f, canceller) = makeCancellable(std::move(f));
80 canceller.detach();
81 }
82
83 EXPECT_EQ(0, v);
84 (*f)(3);
85 EXPECT_EQ(3, v);
86 }
87
88 TEST(MakeCancellableTest, CancellerMove)
89 {
90 int v = 0;
91 auto f = bind(&add, unretained(&v));
92 FunctionCanceller canceller;
93 std::tie(f, canceller) = makeCancellable(std::move(f));
94
95 FunctionCanceller moved = std::move(canceller);
96
97 canceller.cancel(); // This should take no effect.
98
99 EXPECT_EQ(0, v);
100 (*f)(3);
101 EXPECT_EQ(3, v);
102
103 std::tie(f, canceller) = makeCancellable(bind(&add, unretained(&v)));
104 moved = std::move(canceller);
105 moved.cancel();
106
107 EXPECT_EQ(3, v);
108 (*f)(4);
109 EXPECT_EQ(3, v);
110 }
111
112 TEST(MakeCancellableTest, MultiCall)
113 {
114 int v = 0;
115 auto f = bind(&add, unretained(&v));
116 FunctionCanceller canceller;
117 std::tie(f, canceller) = makeCancellable(std::move(f));
118
119 EXPECT_EQ(0, v);
120 (*f)(2);
121 EXPECT_EQ(2, v);
122 (*f)(3);
123 EXPECT_EQ(5, v);
124 }
125
126 TEST(MakeCancellableTest, DestroyOnCancel)
127 {
128 int counter = 0;
129 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter) );
130 FunctionCanceller canceller;
131 std::tie(f, canceller) = makeCancellable(std::move(f));
132
133 EXPECT_EQ(0, counter);
134 canceller.cancel();
135 EXPECT_EQ(1, counter);
136 }
137
138 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698