|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "js/StructuredClone.h" |
|
6 |
|
7 #include "jsapi-tests/tests.h" |
|
8 |
|
9 BEGIN_TEST(testStructuredClone_object) |
|
10 { |
|
11 JS::RootedObject g1(cx, createGlobal()); |
|
12 JS::RootedObject g2(cx, createGlobal()); |
|
13 CHECK(g1); |
|
14 CHECK(g2); |
|
15 |
|
16 JS::RootedValue v1(cx); |
|
17 |
|
18 { |
|
19 JSAutoCompartment ac(cx, g1); |
|
20 JS::RootedValue prop(cx, JS::Int32Value(1337)); |
|
21 |
|
22 JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); |
|
23 v1 = JS::ObjectOrNullValue(obj); |
|
24 CHECK(v1.isObject()); |
|
25 CHECK(JS_SetProperty(cx, obj, "prop", prop)); |
|
26 } |
|
27 |
|
28 { |
|
29 JSAutoCompartment ac(cx, g2); |
|
30 JS::RootedValue v2(cx); |
|
31 |
|
32 CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr)); |
|
33 CHECK(v2.isObject()); |
|
34 JS::RootedObject obj(cx, &v2.toObject()); |
|
35 |
|
36 JS::RootedValue prop(cx); |
|
37 CHECK(JS_GetProperty(cx, obj, "prop", &prop)); |
|
38 CHECK(prop.isInt32()); |
|
39 CHECK(&v1.toObject() != obj); |
|
40 CHECK_EQUAL(prop.toInt32(), 1337); |
|
41 } |
|
42 |
|
43 return true; |
|
44 } |
|
45 END_TEST(testStructuredClone_object) |
|
46 |
|
47 BEGIN_TEST(testStructuredClone_string) |
|
48 { |
|
49 JS::RootedObject g1(cx, createGlobal()); |
|
50 JS::RootedObject g2(cx, createGlobal()); |
|
51 CHECK(g1); |
|
52 CHECK(g2); |
|
53 |
|
54 JS::RootedValue v1(cx); |
|
55 |
|
56 { |
|
57 JSAutoCompartment ac(cx, g1); |
|
58 JS::RootedValue prop(cx, JS::Int32Value(1337)); |
|
59 |
|
60 v1 = JS::StringValue(JS_NewStringCopyZ(cx, "Hello World!")); |
|
61 CHECK(v1.isString()); |
|
62 CHECK(v1.toString()); |
|
63 } |
|
64 |
|
65 { |
|
66 JSAutoCompartment ac(cx, g2); |
|
67 JS::RootedValue v2(cx); |
|
68 |
|
69 CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr)); |
|
70 CHECK(v2.isString()); |
|
71 CHECK(v2.toString()); |
|
72 |
|
73 JS::RootedValue expected(cx, JS::StringValue( |
|
74 JS_NewStringCopyZ(cx, "Hello World!"))); |
|
75 CHECK_SAME(v2, expected); |
|
76 } |
|
77 |
|
78 return true; |
|
79 } |
|
80 END_TEST(testStructuredClone_string) |