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

Unified 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 side-by-side diff with in-line comments
Download patch
« Source/bindings/v8/ScriptPromise.h ('K') | « Source/bindings/v8/ScriptPromise.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/bindings/v8/ScriptPromiseTest.cpp
diff --git a/Source/bindings/v8/ScriptPromiseTest.cpp b/Source/bindings/v8/ScriptPromiseTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..89bc39cb5766411d2c698b9f7e16019c26ccd2e4
--- /dev/null
+++ b/Source/bindings/v8/ScriptPromiseTest.cpp
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2013 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "bindings/v8/ScriptPromise.h"
+
+#include "bindings/v8/ScriptPromiseResolver.h"
+#include "bindings/v8/V8Binding.h"
+
+#include <gtest/gtest.h>
+#include <v8.h>
+
+
+namespace WebCore {
+
+namespace {
+
+class ScriptPromiseTest : public testing::Test {
+public:
+ ScriptPromiseTest()
+ : m_isolate(v8::Isolate::GetCurrent())
+ , m_handleScope(m_isolate)
+ , m_context(v8::Context::New(m_isolate))
+ , m_contextScope(m_context.newLocal(m_isolate))
+ {
+ }
+
+ void SetUp()
+ {
+ m_perContextData = V8PerContextData::create(m_context.newLocal(m_isolate));
+ m_perContextData->init();
+ m_resolver = ScriptPromiseResolver::create();
+ // XXX should just fix m_resolver to return a ScriptPromise?
+ m_promise.Reset(m_resolver->promise().v8Object(), m_isolate);
+ }
+
+ void TearDown()
+ {
+ m_resolver = 0;
+ m_promise.clear();
+ m_perContextData.clear();
+ }
+
+protected:
+ v8::Isolate* m_isolate;
+ v8::HandleScope m_handleScope;
+ ScopedPersistent<v8::Context> m_context;
+ v8::Context::Scope m_contextScope;
+ RefPtr<ScriptPromiseResolver> m_resolver;
+ ScriptPromise m_promise;
+ OwnPtr<V8PerContextData> m_perContextData;
+};
+
+class ThenCallback : public ScriptFunction {
+public:
+ ThenCallback() : m_resolved(false) { }
+ virtual ~ThenCallback() { }
+ virtual ScriptValue call(ScriptValue arg) OVERRIDE
+ {
+ m_resolved = true;
+ m_result = arg;
+ return arg;
+ }
+
+ ScriptValue result() const { return m_result; }
+ bool resolved() const { return m_resolved; }
+private:
+ ScriptValue m_result;
+ bool m_resolved;
+};
+
+TEST_F(ScriptPromiseTest, Then)
+{
+ EXPECT_TRUE(m_resolver->isPending());
+
+ RefPtr<ThenCallback> callback = adoptRef(new ThenCallback());
+ m_promise.then(callback.get(), 0);
+ EXPECT_FALSE(callback->resolved());
+ EXPECT_TRUE(callback->result().hasNoValue());
+
+ // XXX we can't actually guarantee this is ready until the next turn
+ // of task loop.
+ m_resolver->resolve(ScriptValue(v8::Integer::New(3)));
+ EXPECT_TRUE(callback->resolved());
+ EXPECT_TRUE(callback->result().v8Value()->IsNumber());
+}
+
+} // namespace
+
+} // namespace WebCore
« 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