OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "bindings/core/v8/ScriptPromiseResolver.h" | 6 #include "bindings/core/v8/ScriptPromiseResolver.h" |
7 | 7 |
8 #include "bindings/core/v8/ScriptFunction.h" | 8 #include "bindings/core/v8/ScriptFunction.h" |
9 #include "bindings/core/v8/ScriptValue.h" | 9 #include "bindings/core/v8/ScriptValue.h" |
10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 | 73 |
74 TEST_F(ScriptPromiseResolverTest, construct) | 74 TEST_F(ScriptPromiseResolverTest, construct) |
75 { | 75 { |
76 ASSERT_FALSE(executionContext()->activeDOMObjectsAreStopped()); | 76 ASSERT_FALSE(executionContext()->activeDOMObjectsAreStopped()); |
77 ScriptState::Scope scope(scriptState()); | 77 ScriptState::Scope scope(scriptState()); |
78 ScriptPromiseResolver::create(scriptState()); | 78 ScriptPromiseResolver::create(scriptState()); |
79 } | 79 } |
80 | 80 |
81 TEST_F(ScriptPromiseResolverTest, resolve) | 81 TEST_F(ScriptPromiseResolverTest, resolve) |
82 { | 82 { |
83 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = nullptr; | 83 ScriptPromiseResolver* resolver = nullptr; |
84 ScriptPromise promise; | 84 ScriptPromise promise; |
85 { | 85 { |
86 ScriptState::Scope scope(scriptState()); | 86 ScriptState::Scope scope(scriptState()); |
87 resolver = ScriptPromiseResolver::create(scriptState()); | 87 resolver = ScriptPromiseResolver::create(scriptState()); |
88 promise = resolver->promise(); | 88 promise = resolver->promise(); |
89 } | 89 } |
90 | 90 |
91 String onFulfilled, onRejected; | 91 String onFulfilled, onRejected; |
92 ASSERT_FALSE(promise.isEmpty()); | 92 ASSERT_FALSE(promise.isEmpty()); |
93 { | 93 { |
(...skipping 27 matching lines...) Expand all Loading... | |
121 resolver->resolve("bye"); | 121 resolver->resolve("bye"); |
122 resolver->reject("bye"); | 122 resolver->reject("bye"); |
123 isolate()->RunMicrotasks(); | 123 isolate()->RunMicrotasks(); |
124 | 124 |
125 EXPECT_EQ("hello", onFulfilled); | 125 EXPECT_EQ("hello", onFulfilled); |
126 EXPECT_EQ(String(), onRejected); | 126 EXPECT_EQ(String(), onRejected); |
127 } | 127 } |
128 | 128 |
129 TEST_F(ScriptPromiseResolverTest, reject) | 129 TEST_F(ScriptPromiseResolverTest, reject) |
130 { | 130 { |
131 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = nullptr; | 131 ScriptPromiseResolver* resolver = nullptr; |
132 ScriptPromise promise; | 132 ScriptPromise promise; |
133 { | 133 { |
134 ScriptState::Scope scope(scriptState()); | 134 ScriptState::Scope scope(scriptState()); |
135 resolver = ScriptPromiseResolver::create(scriptState()); | 135 resolver = ScriptPromiseResolver::create(scriptState()); |
136 promise = resolver->promise(); | 136 promise = resolver->promise(); |
137 } | 137 } |
138 | 138 |
139 String onFulfilled, onRejected; | 139 String onFulfilled, onRejected; |
140 ASSERT_FALSE(promise.isEmpty()); | 140 ASSERT_FALSE(promise.isEmpty()); |
141 { | 141 { |
(...skipping 27 matching lines...) Expand all Loading... | |
169 resolver->resolve("bye"); | 169 resolver->resolve("bye"); |
170 resolver->reject("bye"); | 170 resolver->reject("bye"); |
171 isolate()->RunMicrotasks(); | 171 isolate()->RunMicrotasks(); |
172 | 172 |
173 EXPECT_EQ(String(), onFulfilled); | 173 EXPECT_EQ(String(), onFulfilled); |
174 EXPECT_EQ("hello", onRejected); | 174 EXPECT_EQ("hello", onRejected); |
175 } | 175 } |
176 | 176 |
177 TEST_F(ScriptPromiseResolverTest, stop) | 177 TEST_F(ScriptPromiseResolverTest, stop) |
178 { | 178 { |
179 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = nullptr; | 179 ScriptPromiseResolver* resolver = nullptr; |
180 ScriptPromise promise; | 180 ScriptPromise promise; |
181 { | 181 { |
182 ScriptState::Scope scope(scriptState()); | 182 ScriptState::Scope scope(scriptState()); |
183 resolver = ScriptPromiseResolver::create(scriptState()); | 183 resolver = ScriptPromiseResolver::create(scriptState()); |
184 promise = resolver->promise(); | 184 promise = resolver->promise(); |
185 } | 185 } |
186 | 186 |
187 String onFulfilled, onRejected; | 187 String onFulfilled, onRejected; |
188 ASSERT_FALSE(promise.isEmpty()); | 188 ASSERT_FALSE(promise.isEmpty()); |
189 { | 189 { |
190 ScriptState::Scope scope(scriptState()); | 190 ScriptState::Scope scope(scriptState()); |
191 promise.then(Function::createFunction(scriptState(), &onFulfilled), Func tion::createFunction(scriptState(), &onRejected)); | 191 promise.then(Function::createFunction(scriptState(), &onFulfilled), Func tion::createFunction(scriptState(), &onRejected)); |
192 } | 192 } |
193 | 193 |
194 executionContext()->stopActiveDOMObjects(); | 194 executionContext()->stopActiveDOMObjects(); |
195 { | 195 { |
196 ScriptState::Scope scope(scriptState()); | 196 ScriptState::Scope scope(scriptState()); |
197 EXPECT_TRUE(resolver->promise().isEmpty()); | 197 EXPECT_TRUE(resolver->promise().isEmpty()); |
198 } | 198 } |
199 | 199 |
200 resolver->resolve("hello"); | 200 resolver->resolve("hello"); |
201 isolate()->RunMicrotasks(); | 201 isolate()->RunMicrotasks(); |
202 | 202 |
203 EXPECT_EQ(String(), onFulfilled); | 203 EXPECT_EQ(String(), onFulfilled); |
204 EXPECT_EQ(String(), onRejected); | 204 EXPECT_EQ(String(), onRejected); |
205 } | 205 } |
206 | 206 |
207 #if !ENABLE(OILPAN) | 207 namespace { |
yhirano
2015/07/16 11:14:14
You're already in an unnamed namespace, so you don
sof
2015/07/16 11:57:43
Dropped.
| |
208 | |
209 class ScriptPromiseResolverKeepAlive : public ScriptPromiseResolver { | |
210 public: | |
211 static ScriptPromiseResolverKeepAlive* create(ScriptState* scriptState) | |
212 { | |
213 ScriptPromiseResolverKeepAlive* resolver = new ScriptPromiseResolverKeep Alive(scriptState); | |
214 resolver->suspendIfNeeded(); | |
215 return resolver; | |
216 } | |
217 | |
218 ~ScriptPromiseResolverKeepAlive() override | |
219 { | |
220 s_destructorCalls++; | |
221 } | |
222 | |
223 static void reset() { s_destructorCalls = 0; } | |
224 static bool isAlive() { return !s_destructorCalls; } | |
225 | |
226 static int s_destructorCalls; | |
227 | |
228 private: | |
229 ScriptPromiseResolverKeepAlive(ScriptState* scriptState) | |
230 : ScriptPromiseResolver(scriptState) | |
231 { | |
232 } | |
233 }; | |
234 | |
235 int ScriptPromiseResolverKeepAlive::s_destructorCalls = 0; | |
236 | |
237 } // namespace | |
238 | |
208 TEST_F(ScriptPromiseResolverTest, keepAliveUntilResolved) | 239 TEST_F(ScriptPromiseResolverTest, keepAliveUntilResolved) |
209 { | 240 { |
210 RefPtr<ScriptPromiseResolver> resolver; | 241 ScriptPromiseResolverKeepAlive::reset(); |
242 ScriptPromiseResolver* resolver = nullptr; | |
211 { | 243 { |
212 ScriptState::Scope scope(scriptState()); | 244 ScriptState::Scope scope(scriptState()); |
213 resolver = ScriptPromiseResolver::create(scriptState()); | 245 resolver = ScriptPromiseResolverKeepAlive::create(scriptState()); |
214 } | 246 } |
215 EXPECT_EQ(1, resolver->refCount()); | |
216 resolver->keepAliveWhilePending(); | 247 resolver->keepAliveWhilePending(); |
217 EXPECT_EQ(2, resolver->refCount()); | 248 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
249 EXPECT_TRUE(ScriptPromiseResolverKeepAlive::isAlive()); | |
yhirano
2015/07/16 11:14:14
ASSERT_TRUE? |resolver| will be a dangling pointer
sof
2015/07/16 11:57:43
Good idea, done (here & elsewhere.)
| |
218 | 250 |
219 resolver->resolve("hello"); | 251 resolver->resolve("hello"); |
220 EXPECT_EQ(1, resolver->refCount()); | 252 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
253 EXPECT_FALSE(ScriptPromiseResolverKeepAlive::isAlive()); | |
221 } | 254 } |
222 | 255 |
223 TEST_F(ScriptPromiseResolverTest, keepAliveUntilRejected) | 256 TEST_F(ScriptPromiseResolverTest, keepAliveUntilRejected) |
224 { | 257 { |
225 RefPtr<ScriptPromiseResolver> resolver; | 258 ScriptPromiseResolverKeepAlive::reset(); |
259 ScriptPromiseResolver* resolver = nullptr; | |
226 { | 260 { |
227 ScriptState::Scope scope(scriptState()); | 261 ScriptState::Scope scope(scriptState()); |
228 resolver = ScriptPromiseResolver::create(scriptState()); | 262 resolver = ScriptPromiseResolverKeepAlive::create(scriptState()); |
229 } | 263 } |
230 EXPECT_EQ(1, resolver->refCount()); | |
231 resolver->keepAliveWhilePending(); | 264 resolver->keepAliveWhilePending(); |
232 EXPECT_EQ(2, resolver->refCount()); | 265 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
266 EXPECT_TRUE(ScriptPromiseResolverKeepAlive::isAlive()); | |
233 | 267 |
234 resolver->reject("hello"); | 268 resolver->reject("hello"); |
235 EXPECT_EQ(1, resolver->refCount()); | 269 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
270 EXPECT_FALSE(ScriptPromiseResolverKeepAlive::isAlive()); | |
236 } | 271 } |
237 | 272 |
238 TEST_F(ScriptPromiseResolverTest, keepAliveUntilStopped) | 273 TEST_F(ScriptPromiseResolverTest, keepAliveUntilStopped) |
239 { | 274 { |
240 RefPtr<ScriptPromiseResolver> resolver; | 275 ScriptPromiseResolverKeepAlive::reset(); |
276 ScriptPromiseResolver* resolver = nullptr; | |
241 { | 277 { |
242 ScriptState::Scope scope(scriptState()); | 278 ScriptState::Scope scope(scriptState()); |
243 resolver = ScriptPromiseResolver::create(scriptState()); | 279 resolver = ScriptPromiseResolverKeepAlive::create(scriptState()); |
244 } | 280 } |
245 EXPECT_EQ(1, resolver->refCount()); | |
246 resolver->keepAliveWhilePending(); | 281 resolver->keepAliveWhilePending(); |
247 EXPECT_EQ(2, resolver->refCount()); | 282 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
283 EXPECT_TRUE(ScriptPromiseResolverKeepAlive::isAlive()); | |
248 | 284 |
249 executionContext()->stopActiveDOMObjects(); | 285 executionContext()->stopActiveDOMObjects(); |
250 EXPECT_EQ(1, resolver->refCount()); | 286 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
287 EXPECT_FALSE(ScriptPromiseResolverKeepAlive::isAlive()); | |
251 } | 288 } |
252 | 289 |
253 TEST_F(ScriptPromiseResolverTest, suspend) | 290 TEST_F(ScriptPromiseResolverTest, suspend) |
254 { | 291 { |
255 RefPtr<ScriptPromiseResolver> resolver; | 292 ScriptPromiseResolverKeepAlive::reset(); |
293 ScriptPromiseResolver* resolver = nullptr; | |
256 { | 294 { |
257 ScriptState::Scope scope(scriptState()); | 295 ScriptState::Scope scope(scriptState()); |
258 resolver = ScriptPromiseResolver::create(scriptState()); | 296 resolver = ScriptPromiseResolverKeepAlive::create(scriptState()); |
259 } | 297 } |
260 EXPECT_EQ(1, resolver->refCount()); | |
261 resolver->keepAliveWhilePending(); | 298 resolver->keepAliveWhilePending(); |
262 EXPECT_EQ(2, resolver->refCount()); | 299 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
300 EXPECT_TRUE(ScriptPromiseResolverKeepAlive::isAlive()); | |
301 | |
263 executionContext()->suspendActiveDOMObjects(); | 302 executionContext()->suspendActiveDOMObjects(); |
264 resolver->resolve("hello"); | 303 resolver->resolve("hello"); |
265 EXPECT_EQ(3, resolver->refCount()); | 304 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
305 EXPECT_TRUE(ScriptPromiseResolverKeepAlive::isAlive()); | |
266 | 306 |
267 executionContext()->stopActiveDOMObjects(); | 307 executionContext()->stopActiveDOMObjects(); |
268 EXPECT_EQ(1, resolver->refCount()); | 308 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith Sweep, Heap::ForcedGC); |
309 EXPECT_FALSE(ScriptPromiseResolverKeepAlive::isAlive()); | |
269 } | 310 } |
270 #endif | |
271 | 311 |
272 TEST_F(ScriptPromiseResolverTest, resolveVoid) | 312 TEST_F(ScriptPromiseResolverTest, resolveVoid) |
273 { | 313 { |
274 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = nullptr; | 314 ScriptPromiseResolver* resolver = nullptr; |
275 ScriptPromise promise; | 315 ScriptPromise promise; |
276 { | 316 { |
277 ScriptState::Scope scope(scriptState()); | 317 ScriptState::Scope scope(scriptState()); |
278 resolver = ScriptPromiseResolver::create(scriptState()); | 318 resolver = ScriptPromiseResolver::create(scriptState()); |
279 promise = resolver->promise(); | 319 promise = resolver->promise(); |
280 } | 320 } |
281 | 321 |
282 String onFulfilled, onRejected; | 322 String onFulfilled, onRejected; |
283 ASSERT_FALSE(promise.isEmpty()); | 323 ASSERT_FALSE(promise.isEmpty()); |
284 { | 324 { |
285 ScriptState::Scope scope(scriptState()); | 325 ScriptState::Scope scope(scriptState()); |
286 promise.then(Function::createFunction(scriptState(), &onFulfilled), Func tion::createFunction(scriptState(), &onRejected)); | 326 promise.then(Function::createFunction(scriptState(), &onFulfilled), Func tion::createFunction(scriptState(), &onRejected)); |
287 } | 327 } |
288 | 328 |
289 resolver->resolve(); | 329 resolver->resolve(); |
290 isolate()->RunMicrotasks(); | 330 isolate()->RunMicrotasks(); |
291 | 331 |
292 EXPECT_EQ("undefined", onFulfilled); | 332 EXPECT_EQ("undefined", onFulfilled); |
293 EXPECT_EQ(String(), onRejected); | 333 EXPECT_EQ(String(), onRejected); |
294 } | 334 } |
295 | 335 |
296 TEST_F(ScriptPromiseResolverTest, rejectVoid) | 336 TEST_F(ScriptPromiseResolverTest, rejectVoid) |
297 { | 337 { |
298 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = nullptr; | 338 ScriptPromiseResolver* resolver = nullptr; |
299 ScriptPromise promise; | 339 ScriptPromise promise; |
300 { | 340 { |
301 ScriptState::Scope scope(scriptState()); | 341 ScriptState::Scope scope(scriptState()); |
302 resolver = ScriptPromiseResolver::create(scriptState()); | 342 resolver = ScriptPromiseResolver::create(scriptState()); |
303 promise = resolver->promise(); | 343 promise = resolver->promise(); |
304 } | 344 } |
305 | 345 |
306 String onFulfilled, onRejected; | 346 String onFulfilled, onRejected; |
307 ASSERT_FALSE(promise.isEmpty()); | 347 ASSERT_FALSE(promise.isEmpty()); |
308 { | 348 { |
309 ScriptState::Scope scope(scriptState()); | 349 ScriptState::Scope scope(scriptState()); |
310 promise.then(Function::createFunction(scriptState(), &onFulfilled), Func tion::createFunction(scriptState(), &onRejected)); | 350 promise.then(Function::createFunction(scriptState(), &onFulfilled), Func tion::createFunction(scriptState(), &onRejected)); |
311 } | 351 } |
312 | 352 |
313 resolver->reject(); | 353 resolver->reject(); |
314 isolate()->RunMicrotasks(); | 354 isolate()->RunMicrotasks(); |
315 | 355 |
316 EXPECT_EQ(String(), onFulfilled); | 356 EXPECT_EQ(String(), onFulfilled); |
317 EXPECT_EQ("undefined", onRejected); | 357 EXPECT_EQ("undefined", onRejected); |
318 } | 358 } |
319 | 359 |
320 } // namespace | 360 } // namespace |
321 | 361 |
322 } // namespace blink | 362 } // namespace blink |
OLD | NEW |