forked from microsoft/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJScriptCoreEngineTest.Windows.cs
79 lines (65 loc) · 2.71 KB
/
JScriptCoreEngineTest.Windows.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Threading;
using System.Windows.Threading;
using Microsoft.ClearScript.Windows.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.ClearScript.Test
{
public partial class JScriptCoreEngineTest
{
#region test methods
[TestMethod, TestCategory("JScriptCoreEngine")]
public void JScriptCoreEngine_AddCOMType_XMLHTTP()
{
var status = 0;
string data = null;
var checkpoint = new ManualResetEventSlim();
Dispatcher dispatcher = null;
var thread = new Thread(() =>
{
using (var testEngine = new JScriptEngine(Windows.WindowsScriptEngineFlags.EnableDebugging | Windows.WindowsScriptEngineFlags.EnableStandardsMode, NullSyncInvoker.Instance))
{
dispatcher = Dispatcher.CurrentDispatcher;
checkpoint.Set();
testEngine.Script.onComplete = new Action<int, string>((xhrStatus, xhrData) =>
{
status = xhrStatus;
data = xhrData;
Dispatcher.ExitAllFrames();
});
dispatcher.BeginInvoke(new Action(() =>
{
// ReSharper disable AccessToDisposedClosure
testEngine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
testEngine.Execute($@"
xhr = new XMLHttpRequest();
xhr.open('POST', '{HttpBinUrl}/post', true);
xhr.onreadystatechange = function () {{
if (xhr.readyState == 4) {{
onComplete(xhr.status, JSON.parse(xhr.responseText).data);
}}
}};
xhr.send('Hello, world!');
");
// ReSharper restore AccessToDisposedClosure
}));
Dispatcher.Run();
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
checkpoint.Wait();
if (!thread.Join(TimeSpan.FromSeconds(10)))
{
dispatcher.Invoke(Dispatcher.ExitAllFrames);
thread.Join();
Assert.Inconclusive("The Httpbin service request timed out");
}
Assert.AreEqual(200, status);
Assert.AreEqual("Hello, world!", data);
}
#endregion
}
}