1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testStructuredClone.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "js/StructuredClone.h" 1.9 + 1.10 +#include "jsapi-tests/tests.h" 1.11 + 1.12 +BEGIN_TEST(testStructuredClone_object) 1.13 +{ 1.14 + JS::RootedObject g1(cx, createGlobal()); 1.15 + JS::RootedObject g2(cx, createGlobal()); 1.16 + CHECK(g1); 1.17 + CHECK(g2); 1.18 + 1.19 + JS::RootedValue v1(cx); 1.20 + 1.21 + { 1.22 + JSAutoCompartment ac(cx, g1); 1.23 + JS::RootedValue prop(cx, JS::Int32Value(1337)); 1.24 + 1.25 + JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); 1.26 + v1 = JS::ObjectOrNullValue(obj); 1.27 + CHECK(v1.isObject()); 1.28 + CHECK(JS_SetProperty(cx, obj, "prop", prop)); 1.29 + } 1.30 + 1.31 + { 1.32 + JSAutoCompartment ac(cx, g2); 1.33 + JS::RootedValue v2(cx); 1.34 + 1.35 + CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr)); 1.36 + CHECK(v2.isObject()); 1.37 + JS::RootedObject obj(cx, &v2.toObject()); 1.38 + 1.39 + JS::RootedValue prop(cx); 1.40 + CHECK(JS_GetProperty(cx, obj, "prop", &prop)); 1.41 + CHECK(prop.isInt32()); 1.42 + CHECK(&v1.toObject() != obj); 1.43 + CHECK_EQUAL(prop.toInt32(), 1337); 1.44 + } 1.45 + 1.46 + return true; 1.47 +} 1.48 +END_TEST(testStructuredClone_object) 1.49 + 1.50 +BEGIN_TEST(testStructuredClone_string) 1.51 +{ 1.52 + JS::RootedObject g1(cx, createGlobal()); 1.53 + JS::RootedObject g2(cx, createGlobal()); 1.54 + CHECK(g1); 1.55 + CHECK(g2); 1.56 + 1.57 + JS::RootedValue v1(cx); 1.58 + 1.59 + { 1.60 + JSAutoCompartment ac(cx, g1); 1.61 + JS::RootedValue prop(cx, JS::Int32Value(1337)); 1.62 + 1.63 + v1 = JS::StringValue(JS_NewStringCopyZ(cx, "Hello World!")); 1.64 + CHECK(v1.isString()); 1.65 + CHECK(v1.toString()); 1.66 + } 1.67 + 1.68 + { 1.69 + JSAutoCompartment ac(cx, g2); 1.70 + JS::RootedValue v2(cx); 1.71 + 1.72 + CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr)); 1.73 + CHECK(v2.isString()); 1.74 + CHECK(v2.toString()); 1.75 + 1.76 + JS::RootedValue expected(cx, JS::StringValue( 1.77 + JS_NewStringCopyZ(cx, "Hello World!"))); 1.78 + CHECK_SAME(v2, expected); 1.79 + } 1.80 + 1.81 + return true; 1.82 +} 1.83 +END_TEST(testStructuredClone_string)