1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description('Unittests for private scripts.');
if (!internals || !internals.privateScriptTest())
debug('This test needs window.internals.privateScriptTest().');
var privateScriptTest = internals.privateScriptTest();
privateScriptTest.doNothing();
shouldBe('privateScriptTest.return123()', '123');
shouldBe('privateScriptTest.echoInteger(111)', '111');
shouldBeEqualToString('privateScriptTest.echoString("foo")', 'foo')
shouldBe('privateScriptTest.addInteger(111, 222)', '333');
shouldBeEqualToString('privateScriptTest.addString("foo", "bar")', 'foobar')
shouldBe('privateScriptTest.getIntegerFromPrototype()', '0');
privateScriptTest.setIntegerToPrototype(123);
shouldBe('privateScriptTest.getIntegerFromPrototype()', '123');
shouldBe('privateScriptTest.getIntegerFromDocument(document)', '0');
privateScriptTest.setIntegerToDocument(document, 123);
shouldBe('privateScriptTest.getIntegerFromDocument(document)', '123');
var node1 = privateScriptTest.createElement(document);
var node2 = privateScriptTest.createElement(document);
var node3 = privateScriptTest.createElement(document);
var node4 = privateScriptTest.createElement(document);
privateScriptTest.appendChild(node1, node2);
privateScriptTest.appendChild(node1, node3);
privateScriptTest.appendChild(node1, node4);
shouldBe('privateScriptTest.firstChild(node1)', 'node2');
shouldBe('privateScriptTest.nextSibling(node2)', 'node3');
shouldBe('privateScriptTest.nextSibling(node3)', 'node4');
shouldBe('privateScriptTest.nextSibling(node4)', 'null');
var node5 = privateScriptTest.createElement(document);
shouldBeEqualToString('privateScriptTest.innerHTML(node5)', '')
privateScriptTest.setInnerHTML(node5, '<div>foo</div>');
shouldBeEqualToString('privateScriptTest.innerHTML(node5)', '<div>foo</div>')
var node6 = privateScriptTest.firstChild(node5);
shouldBeEqualToString('privateScriptTest.innerHTML(node6)', 'foo');
var node7 = privateScriptTest.createElement(document);
shouldBeEqualToString('privateScriptTest.innerHTML(node7)', '')
privateScriptTest.addClickListener(node7);
privateScriptTest.clickNode(document, node7);
shouldBeEqualToString('privateScriptTest.innerHTML(node7)', 'clicked')
shouldBe('privateScriptTest.readonlyShortAttribute', '123');
shouldBe('privateScriptTest.shortAttribute', '-1');
privateScriptTest.shortAttribute = 111;
shouldBe('privateScriptTest.shortAttribute', '111');
shouldBeEqualToString('privateScriptTest.stringAttribute', 'xxx');
privateScriptTest.stringAttribute = "foo";
shouldBeEqualToString('privateScriptTest.stringAttribute', 'foo');
shouldBe('privateScriptTest.nodeAttribute', 'null');
var node8 = privateScriptTest.createElement(document);
privateScriptTest.nodeAttribute = node8;
shouldBe('privateScriptTest.nodeAttribute', 'node8');
shouldThrow('privateScriptTest.nodeAttributeThrowsIndexSizeError');
shouldThrow('privateScriptTest.nodeAttributeThrowsIndexSizeError = null');
shouldThrow('privateScriptTest.voidMethodThrowsDOMSyntaxError()');
shouldThrow('privateScriptTest.voidMethodThrowsError()');
shouldThrow('privateScriptTest.voidMethodThrowsTypeError()');
shouldThrow('privateScriptTest.voidMethodThrowsRangeError()');
shouldThrow('privateScriptTest.voidMethodThrowsSyntaxError()');
shouldThrow('privateScriptTest.voidMethodThrowsReferenceError()');
shouldThrow('privateScriptTest.voidMethodThrowsStackOverflowError()');
shouldBe('privateScriptTest.addIntegerImplementedInCPP(111, 222)', '333');
shouldBeEqualToString('privateScriptTest.stringAttributeImplementedInCPP', 'undefined');
privateScriptTest.stringAttributeImplementedInCPP = "foo";
shouldBeEqualToString('privateScriptTest.stringAttributeImplementedInCPP', 'foo');
// These tests are important. [OnlyExposedToPrivateScript] APIs should not be visible to user's script.
shouldBeUndefined('privateScriptTest.addIntegerImplementedInCPPForPrivateScriptOnly');
shouldBeUndefined('privateScriptTest.stringAttributeImplementedInCPPForPrivateScriptOnly');
shouldBe('privateScriptTest.addIntegerInPartial(111, 222)', '333');
shouldBe('privateScriptTest.addInteger2InPartial(111, 222)', '333');
privateScriptTest.stringAttributeInPartial = "foo";
shouldBeEqualToString('privateScriptTest.stringAttributeInPartial', 'foo');
document.onload = function (event) {
shouldBeTrue('event.bubbles');
shouldBeTrue('event.cancelable');
// Object properties set in private scripts should not be visible in user's script.
shouldBeUndefined('event.valueInPrivateScript');
}
privateScriptTest.dispatchDocumentOnload(document);
var exception;
function testThrows(expression, type, code)
{
exception = undefined;
// Test that `expression` throws a userscript visible exception of `type`, optionally with
// exception code `code`
try {
eval(expression);
} catch (e) {
exception = e;
}
if (type === DOMException && typeof code === "string" && code in DOMException)
code = DOMException[code];
if (exception === undefined) {
testFailed("`" + expression + "` should throw");
} else {
shouldBeType("exception", type);
if (code !== undefined)
shouldBeEqualToNumber("exception.code", code);
}
}
testThrows("privateScriptTest.nodeAttributeThrowsIndexSizeError", DOMException, "INDEX_SIZE_ERR");
testThrows("privateScriptTest.nodeAttributeThrowsIndexSizeError = null", DOMException, "INDEX_SIZE_ERR");
testThrows("privateScriptTest.voidMethodThrowsDOMSyntaxError()", DOMException, "SYNTAX_ERR");
testThrows("privateScriptTest.voidMethodThrowsError()", Error);
testThrows("privateScriptTest.voidMethodThrowsTypeError()", TypeError);
testThrows("privateScriptTest.voidMethodThrowsRangeError()", RangeError);
testThrows("privateScriptTest.voidMethodThrowsSyntaxError()", SyntaxError);
testThrows("privateScriptTest.voidMethodThrowsReferenceError()", ReferenceError);
</script>
</body>
</html>
|