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

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: +comment 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 #include "wtf/Compiler.h"
9
10 namespace WTF {
11 namespace {
12
13 void add(int* x, int y) { *x += y; }
14
15 class DestructionCounter {
16 public:
17 explicit DestructionCounter(int* counter) : m_counter(counter) { }
18 ~DestructionCounter()
19 {
20 if (m_counter)
21 ++*m_counter;
22 }
23
24 DestructionCounter(DestructionCounter&& other) : m_counter(other.m_counter)
25 {
26 other.m_counter = nullptr;
27 }
28
29 private:
30 int* m_counter;
31 };
32
33 class RefCountedDestructionCounter : public RefCounted<RefCountedDestructionCoun ter>, public DestructionCounter {
34 public:
35 explicit RefCountedDestructionCounter(int* counter) : DestructionCounter(cou nter) {}
36 };
37
38 } // namespace
39
40 TEST(MakeCancellableTest, NotCancelled)
41 {
42 int v = 0;
43 auto f = bind(&add, unretained(&v));
44 auto result = makeCancellable(std::move(f));
45
46 EXPECT_EQ(0, v);
47 (*result.function)(3);
48 EXPECT_EQ(3, v);
49 }
50
51 TEST(MakeCancellableTest, ExplicitCancel)
52 {
53 int v = 0;
54 auto f = bind(&add, unretained(&v));
55 auto result = makeCancellable(std::move(f));
56
57 EXPECT_TRUE(result.canceller.isActive());
58 result.canceller.cancel();
59 EXPECT_FALSE(result.canceller.isActive());
60
61 EXPECT_EQ(0, v);
62 (*result.function)(3);
63 EXPECT_EQ(0, v);
64 }
65
66 TEST(MakeCancellableTest, ScopeOutCancel)
67 {
68 int v = 0;
69 auto f = bind(&add, unretained(&v));
70 {
71 auto result = makeCancellable(std::move(f));
72 f = std::move(result.function);
73
74 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller);
75 EXPECT_TRUE(scopedCanceller.isActive());
76 }
77
78 EXPECT_EQ(0, v);
79 (*f)(3);
80 EXPECT_EQ(0, v);
81 }
82
83 TEST(MakeCancellableTest, Detach)
84 {
85 int v = 0;
86 auto f = bind(&add, unretained(&v));
87 {
88 auto result = makeCancellable(std::move(f));
89 f = std::move(result.function);
90
91 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller);
92 EXPECT_TRUE(scopedCanceller.isActive());
93 scopedCanceller.detach();
94 EXPECT_FALSE(scopedCanceller.isActive());
95 }
96
97 EXPECT_EQ(0, v);
98 (*f)(3);
99 EXPECT_EQ(3, v);
100 }
101
102 TEST(MakeCancellableTest, MultiCall)
103 {
104 int v = 0;
105 auto f = bind(&add, unretained(&v));
106 auto result = makeCancellable(std::move(f));
107
108 EXPECT_EQ(0, v);
109 (*result.function)(2);
110 EXPECT_EQ(2, v);
111 (*result.function)(3);
112 EXPECT_EQ(5, v);
113 }
114
115 TEST(MakeCancellableTest, DestroyOnCancel)
116 {
117 int counter = 0;
118 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter) );
119 auto result = makeCancellable(std::move(f));
120
121 EXPECT_EQ(0, counter);
122 result.canceller.cancel();
123 EXPECT_EQ(1, counter);
124 }
125
126 TEST(MakeCancellableTest, DestroyOnWrapperDestruction)
127 {
128 int counter = 0;
129 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter) );
130 auto result = makeCancellable(std::move(f));
131
132 EXPECT_TRUE(result.canceller.isActive());
133
134 EXPECT_EQ(0, counter);
135 result.function = nullptr;
136 EXPECT_EQ(1, counter);
137
138 EXPECT_FALSE(result.canceller.isActive());
139 }
140
141 TEST(MakeCancellableTest, SelfAssignment)
142 {
143 int v = 0;
144 auto f = bind(&add, unretained(&v));
145 auto result = makeCancellable(std::move(f));
146
147 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller);
148 EXPECT_TRUE(scopedCanceller.isActive());
149
150 #if COMPILER(CLANG)
151 #pragma clang diagnostic push
152 #pragma clang diagnostic ignored "-Wself-move"
153 scopedCanceller = std::move(scopedCanceller);
154 #pragma clang diagnostic pop
155 #else
156 scopedCanceller = std::move(scopedCanceller);
157 #endif
158
159 EXPECT_TRUE(scopedCanceller.isActive());
160 EXPECT_EQ(0, v);
161 (*result.function)(1);
162 EXPECT_EQ(1, v);
163 }
164
165 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/MakeCancellable.cpp ('k') | third_party/WebKit/Source/wtf/wtf.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698