-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathIsolatedJavascriptEvaluation.cs
198 lines (162 loc) · 8.55 KB
/
IsolatedJavascriptEvaluation.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Threading;
using NUnit.Framework;
using static WebViewControl.WebView;
namespace Tests.WebView {
public class IsolatedJavascriptEvaluation : WebViewTestBase {
protected override void InitializeView() => base.InitializeView();
protected override Task AfterInitializeView() {
return Task.CompletedTask;
}
[Test(Description = "Evaluation timeouts when javascript engine is not initialized")]
public async Task JavascriptEngineInitializationTimeout() {
await Run(async () => {
await Load("<html><body></body></html>");
var exception = await Assertions.AssertThrows<JavascriptException>(async () => await TargetView.EvaluateScript<int>("1", timeout: TimeSpan.FromMilliseconds(0)));
Assert.IsNotNull(exception);
StringAssert.Contains("Timeout", exception.Message);
});
}
[Test(Description = "Evaluation works after engine is initialized")]
public async Task EvaluateAfterInitialization() {
await Run(async () => {
await Load("<html><script></script><body>1</body></html>");
await Load("<html><script></script><body>2</body></html>");
var result = await TargetView.EvaluateScript<int>("return 1", timeout: TimeSpan.FromSeconds(30));
Assert.AreEqual(result, 1);
});
}
[Test(Description = "Method interception function is called")]
public async Task RegisteredJsObjectMethodInterception() {
await Run(async () => {
const string DotNetObject = "DotNetObject";
var interceptorCalled = false;
var taskCompletionSource = new TaskCompletionSource<bool>();
Func<int> functionToCall = () => {
taskCompletionSource.SetResult(true);
return 10;
};
object Interceptor(Func<object> originalFunc) {
interceptorCalled = true;
return originalFunc();
}
RegisterJavascriptObject(DotNetObject, functionToCall, Interceptor);
var script = $"{DotNetObject}.invoke();";
await RunScript(script);
var functionCalled = await taskCompletionSource.Task;
Assert.IsTrue(functionCalled);
Assert.IsTrue(interceptorCalled);
});
}
[Test(Description = ".Net Method params serialization works with nulls")]
public async Task RegisteredJsObjectMethodNullParamsSerialization() {
await Run(async () => {
const string DotNetObject = "DotNetObject";
var taskCompletionSource = new TaskCompletionSource<Tuple<string, string[]>>();
Action<string, string[]> functionToCall = (string arg1, string[] arg2) => {
taskCompletionSource.SetResult(new Tuple<string, string[]>(arg1, arg2));
};
RegisterJavascriptObject(DotNetObject, functionToCall);
var script = $"{DotNetObject}.invoke(null, ['hello', null, 'world']);";
await RunScript(script);
var obtainedArgs = await taskCompletionSource.Task;
Assert.AreEqual(null, obtainedArgs.Item1);
Assert.That(new[] { "hello", null, "world" }, Is.EquivalentTo(obtainedArgs.Item2));
});
}
[Test(Description = ".Net Method returned objects serialization")]
public async Task RegisteredJsObjectReturnObjectSerialization() {
await Run(async () => {
const string DotNetObject = "DotNetObject";
const string DotNetSetResult = "DotNetSetResult";
var testObject = new TestObject() {
Age = 33,
Kind = Kind.B,
Name = "John",
Parent = new TestObject() {
Name = "John Parent",
Age = 66,
Kind = Kind.C
}
};
var taskCompletionSource = new TaskCompletionSource<TestObject>();
Func<TestObject> functionToCall = () => testObject;
Action<TestObject> setResult = (r) => {
taskCompletionSource.SetResult(r);
};
RegisterJavascriptObject(DotNetObject, functionToCall);
RegisterJavascriptObject(DotNetSetResult, setResult);
var script = $"var result = await {DotNetObject}.invoke(); {DotNetSetResult}.invoke(result);";
await RunScript(script);
var result = await taskCompletionSource.Task;
Assert.IsNotNull(result);
Assert.AreEqual(testObject.Name, result.Name);
Assert.AreEqual(testObject.Age, result.Age);
Assert.AreEqual(testObject.Kind, result.Kind);
Assert.IsNotNull(result.Parent);
Assert.AreEqual(testObject.Parent.Name, result.Parent.Name);
Assert.AreEqual(testObject.Parent.Age, result.Parent.Age);
Assert.AreEqual(testObject.Parent.Kind, result.Parent.Kind);
});
}
[Test(Description = "Dispose is scheduled when there are js pending calls")]
public async Task WebViewDisposeDoesNotBlockWhenHasPendingJSCalls() {
await Run(async () => {
const string DotNetObject = "DotNetObject";
var taskCompletionSourceFunction = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var taskCompletionSourceDispose = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
Func<int> functionToCall = () => {
TargetView.Dispose();
var disposeRan = taskCompletionSourceDispose.Task.IsCompleted;
taskCompletionSourceFunction.SetResult(disposeRan);
return 1;
};
TargetView.RegisterJavascriptObject(DotNetObject, functionToCall);
await Load($"<html><script>function test() {{ {DotNetObject}.invoke(); while(true); return 1; }}</script><body></body></html>");
TargetView.Disposed += () => taskCompletionSourceDispose.SetResult(true);
var result = await TargetView.EvaluateScriptFunction<int>("test");
Assert.AreEqual(0, result, "Script evaluation should be cancelled and default value returned");
var disposed = await taskCompletionSourceFunction.Task;
Assert.IsFalse(disposed, "Dispose should have been scheduled");
disposed = await taskCompletionSourceDispose.Task;
Assert.IsTrue(disposed);
});
}
[Test(Description = "Javascript evaluation returns default values after webview is disposed")]
public async Task JsEvaluationReturnsDefaultValuesAfterWebViewDispose() {
await Run(async () => {
var taskCompletionSource = new TaskCompletionSource<bool>();
await Load("<html><script>function test() { return 1; }</script><body></body></html>");
TargetView.Disposed += () => {
taskCompletionSource.SetResult(true);
};
TargetView.Dispose();
var disposeCalled = await taskCompletionSource.Task;
var result = await TargetView.EvaluateScriptFunction<int>("test");
Assert.IsTrue(disposeCalled);
Assert.AreEqual(result, 0);
});
}
[Test(Description = "Evaluation runs successfully on an iframe")]
public async Task JavascriptEvaluationOnIframe() {
await Run(async () => {
await Load(
"<html>" +
"<body>" +
"<script>" +
"var x = 1;" +
"</script>" +
"<iframe name='test' srcdoc='<html><body><script>var y = 2;</script></body></html>'></iframe>" +
"</body>" +
"</html>"
);
var x = await TargetView.EvaluateScript<int>("return x", "");
var y = await TargetView.EvaluateScript<int>("return test.y", "");
Assert.AreEqual(1, x);
Assert.AreEqual(2, y);
});
}
}
}