Add integration tests for interacting with promises from native code.

This commit is contained in:
Josh Matthews 2016-08-12 12:46:26 -04:00
parent ab168204ed
commit f89355b85d
5 changed files with 98 additions and 5 deletions

View file

@ -5,9 +5,10 @@
// check-tidy: no specs after this line
use core::nonzero::NonZero;
use dom::bindings::callback::ExceptionHandling;
use dom::bindings::codegen::Bindings::EventListenerBinding::EventListener;
use dom::bindings::codegen::Bindings::FunctionBinding::Function;
use dom::bindings::codegen::Bindings::TestBindingBinding;
use dom::bindings::codegen::Bindings::TestBindingBinding::{self, SimpleCallback};
use dom::bindings::codegen::Bindings::TestBindingBinding::{TestBindingMethods, TestDictionary};
use dom::bindings::codegen::Bindings::TestBindingBinding::{TestDictionaryDefaults, TestEnum};
use dom::bindings::codegen::UnionTypes;
@ -20,7 +21,7 @@ use dom::bindings::codegen::UnionTypes::{HTMLElementOrUnsignedLongOrStringOrBool
use dom::bindings::codegen::UnionTypes::{StringOrLongSequence, StringOrStringSequence, StringSequenceOrUnsignedLong};
use dom::bindings::codegen::UnionTypes::{StringOrUnsignedLong, StringOrBoolean, UnsignedLongOrBoolean};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::global::{GlobalRef, global_root_from_context};
use dom::bindings::js::Root;
use dom::bindings::mozmap::MozMap;
use dom::bindings::num::Finite;
@ -29,6 +30,7 @@ use dom::bindings::str::{ByteString, DOMString, USVString};
use dom::bindings::weakref::MutableWeakRef;
use dom::blob::{Blob, BlobImpl};
use dom::promise::Promise;
use dom::promisenativehandler::{PromiseNativeHandler, Callback};
use dom::url::URL;
use js::jsapi::{HandleObject, HandleValue, JSContext, JSObject};
use js::jsapi::{JS_NewPlainObject, JS_NewUint8ClampedArray};
@ -649,8 +651,44 @@ impl TestBindingMethods for TestBinding {
fn ReceiveAnyMozMap(&self) -> MozMap<JSVal> { MozMap::new() }
#[allow(unrooted_must_root)]
fn ReturnPromise(&self) -> Rc<Promise> {
Promise::new(self.global().r())
fn ReturnResolvedPromise(&self, cx: *mut JSContext, v: HandleValue) -> Fallible<Rc<Promise>> {
Promise::Resolve(self.global().r(), cx, v)
}
#[allow(unrooted_must_root)]
fn ReturnRejectedPromise(&self, cx: *mut JSContext, v: HandleValue) -> Fallible<Rc<Promise>> {
Promise::Reject(self.global().r(), cx, v)
}
#[allow(unrooted_must_root)]
fn PromiseNativeHandler(&self,
resolve: Option<Rc<SimpleCallback>>,
reject: Option<Rc<SimpleCallback>>) -> Rc<Promise> {
let global = self.global();
let handler = PromiseNativeHandler::new(global.r(),
resolve.map(|r| SimpleHandler::new(r)),
reject.map(|r| SimpleHandler::new(r)));
let p = Promise::new(global.r());
p.append_native_handler(&handler);
return p;
#[derive(JSTraceable, HeapSizeOf)]
struct SimpleHandler {
#[ignore_heap_size_of = "Rc has unclear ownership semantics"]
handler: Rc<SimpleCallback>,
}
impl SimpleHandler {
fn new(callback: Rc<SimpleCallback>) -> Box<Callback> {
box SimpleHandler { handler: callback }
}
}
impl Callback for SimpleHandler {
#[allow(unsafe_code)]
fn callback(&self, cx: *mut JSContext, v: HandleValue) {
let global = unsafe { global_root_from_context(cx) };
let _ = self.handler.Call_(&global.r(), v, ExceptionHandling::Report);
}
}
}
#[allow(unrooted_must_root)]