3 <script src='test.js'
></script>
4 <script src='call_function.js'
></script>
7 function clearCache() {
8 getPageCache().cache_
= {};
11 function testCallFunctionNoArgs() {
14 var result
= callFunction(function() { return 1; }, []);
15 assertEquals(0, result
.status
);
16 assertEquals(1, result
.value
);
19 function testCallFunctionThrows() {
22 var result
= callFunction(function() { throw new Error('fake error'); }, []);
23 assertEquals(StatusCode
.UNKNOWN_ERROR
, result
.status
);
24 assertEquals('fake error', result
.value
);
26 result
= callFunction(function() {
27 var e
= new Error('fake error');
32 assertEquals(77, result
.status
);
33 assertEquals('CUSTOM', result
.value
);
36 function testCallFunctionArgs() {
39 function func(primitive
, elem
) {
40 return [primitive
, elem
.querySelector('div')];
42 var result
= callFunction(func
, [1, wrap(document
)]);
43 assertEquals(0, result
.status
);
44 assertEquals(1, result
.value
[0]);
45 var cache
= getPageCache();
46 assertEquals(document
.querySelector('div'), unwrap(result
.value
[1], cache
));
49 function testCallFunctionArgsUnwrappedReturn() {
53 return elem
.querySelector('div');
55 var result
= callFunction(func
, [wrap(document
)], true);
56 assertEquals(document
.querySelector('div'), result
);
59 function testCacheWrap() {
62 assertEquals(1, wrap(1));
63 assertEquals(1, unwrap(1));
64 assertEquals("1", wrap("1"));
65 assertEquals("1", unwrap("1"));
66 assertEquals(false, wrap(false));
67 assertEquals(false, unwrap(false));
68 assertEquals(null, wrap(null));
69 assertEquals(null, unwrap(null));
70 assertEquals(undefined, wrap(undefined));
71 assertEquals(undefined, unwrap(undefined));
73 assertEquals(func
, wrap(func
));
74 assertEquals(func
, unwrap(func
));
76 var cache
= getPageCache();
77 var arr
= [1, new Array(1, new Object({a
: 1, b
: {a
: 1, b
: {}, c
: 3}}), 3)];
78 var originalJson
= JSON
.stringify(arr
);
79 arr
[1][1].b
.b
[ELEMENT_KEY
] = cache
.idPrefix_
+ ':' + cache
.nextId_
;
80 var wrappedJson
= JSON
.stringify(arr
);
81 arr
[1][1].b
.b
= document
;
82 assertEquals(wrappedJson
, JSON
.stringify(wrap(arr
)));
83 var unwrapped
= unwrap(JSON
.parse(wrappedJson
), cache
);
84 assertEquals(document
, unwrapped
[1][1].b
.b
);
85 unwrapped
[1][1].b
.b
= {};
86 assertEquals(originalJson
, JSON
.stringify(unwrapped
));
89 function testCacheDoubleWrap() {
92 assertEquals(wrap(document
)[ELEMENT_KEY
], wrap(document
)[ELEMENT_KEY
]);
95 function testCacheUnwrapThrows() {
100 wrapped
[ELEMENT_KEY
] = '1';
101 unwrap(wrapped
, getPageCache());
107 function testClearStale() {
111 var div
= doc
.querySelector('div');
112 var span
= doc
.querySelector('span');
114 var wrappedDoc
= wrap(doc
);
115 var wrappedDiv
= wrap(div
);
116 var wrappedSpan
= wrap(span
);
118 var cache
= getPageCache();
120 assertEquals(doc
, unwrap(wrappedDoc
, cache
));
121 assertEquals(div
, unwrap(wrappedDiv
, cache
));
122 assertEquals(span
, unwrap(wrappedSpan
, cache
));
124 div
.removeChild(span
);
126 assertEquals(doc
, unwrap(wrappedDoc
, cache
));
127 assertEquals(div
, unwrap(wrappedDiv
, cache
));
129 unwrap(wrappedSpan
, cache
);
135 function testCacheQuerySelector() {
138 var cache
= getPageCache();
139 assertEquals(document
.querySelector('div'),
140 unwrap(wrap(document
.querySelector('div')), cache
));
141 assertEquals(document
.querySelectorAll('div')[0],
142 unwrap(wrap(document
.querySelectorAll('div')), cache
)[0]);
145 function testCacheStaleRef() {
148 var cache
= getPageCache();
149 var img
= document
.createElement('img');
150 document
.body
.appendChild(img
);
151 var wrappedImg
= wrap(img
);
152 document
.body
.removeChild(img
);
155 unwrap(wrappedImg
, cache
);
158 assertEquals(StatusCode
.STALE_ELEMENT_REFERENCE
, e
.code
);
164 <div><span></span></div>