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

Side by Side Diff: Source/bindings/v8/ScriptPromiseTest.cpp

Issue 22470008: Implement ScriptPromise and ScriptFunction (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "bindings/v8/ScriptPromise.h"
33
34 #include "bindings/v8/ScriptPromiseResolver.h"
35 #include "bindings/v8/V8Binding.h"
36
37 #include <gtest/gtest.h>
38 #include <v8.h>
39
40
41 namespace WebCore {
42
43 namespace {
44
45 class ScriptPromiseTest : public testing::Test {
46 public:
47 ScriptPromiseTest()
48 : m_isolate(v8::Isolate::GetCurrent())
49 , m_handleScope(m_isolate)
50 , m_context(v8::Context::New(m_isolate))
51 , m_contextScope(m_context.newLocal(m_isolate))
52 {
53 }
54
55 void SetUp()
56 {
57 m_perContextData = V8PerContextData::create(m_context.newLocal(m_isolate ));
58 m_perContextData->init();
59 m_resolver = ScriptPromiseResolver::create();
60 // XXX should just fix m_resolver to return a ScriptPromise?
61 m_promise.Reset(m_resolver->promise().v8Object(), m_isolate);
62 }
63
64 void TearDown()
65 {
66 m_resolver = 0;
67 m_promise.clear();
68 m_perContextData.clear();
69 }
70
71 protected:
72 v8::Isolate* m_isolate;
73 v8::HandleScope m_handleScope;
74 ScopedPersistent<v8::Context> m_context;
75 v8::Context::Scope m_contextScope;
76 RefPtr<ScriptPromiseResolver> m_resolver;
77 ScriptPromise m_promise;
78 OwnPtr<V8PerContextData> m_perContextData;
79 };
80
81 class ThenCallback : public ScriptFunction {
82 public:
83 ThenCallback() : m_resolved(false) { }
84 virtual ~ThenCallback() { }
85 virtual ScriptValue call(ScriptValue arg) OVERRIDE
86 {
87 m_resolved = true;
88 m_result = arg;
89 return arg;
90 }
91
92 ScriptValue result() const { return m_result; }
93 bool resolved() const { return m_resolved; }
94 private:
95 ScriptValue m_result;
96 bool m_resolved;
97 };
98
99 TEST_F(ScriptPromiseTest, Then)
100 {
101 EXPECT_TRUE(m_resolver->isPending());
102
103 RefPtr<ThenCallback> callback = adoptRef(new ThenCallback());
104 m_promise.then(callback.get(), 0);
105 EXPECT_FALSE(callback->resolved());
106 EXPECT_TRUE(callback->result().hasNoValue());
107
108 // XXX we can't actually guarantee this is ready until the next turn
109 // of task loop.
110 m_resolver->resolve(ScriptValue(v8::Integer::New(3)));
111 EXPECT_TRUE(callback->resolved());
112 EXPECT_TRUE(callback->result().v8Value()->IsNumber());
113 }
114
115 } // namespace
116
117 } // namespace WebCore
OLDNEW
« Source/bindings/v8/ScriptPromise.h ('K') | « Source/bindings/v8/ScriptPromise.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698