|
| 1 | +namespace AngleSharp.Js.Tests |
| 2 | +{ |
| 3 | + using AngleSharp.Attributes; |
| 4 | + using AngleSharp.Dom; |
| 5 | + using NUnit.Framework; |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Numerics; |
| 9 | + using System.Threading.Tasks; |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// What a library can give to scripts besides objects of the DOM: namespaces with interfaces |
| 13 | + /// of their own, functions, BigInts, shared buffers, promises and errors of its own. |
| 14 | + /// </summary> |
| 15 | + [TestFixture] |
| 16 | + public class InteropTests |
| 17 | + { |
| 18 | + private static async Task<String> Run(String body) |
| 19 | + { |
| 20 | + // a service of this assembly makes it one of the libraries of the scripts |
| 21 | + var cfg = Configuration.Default.WithJs().WithEventLoop().WithOnly<InteropService>(_ => new InteropService()); |
| 22 | + var script = $"(async function () {{ {body} }})().then(function (r) {{ document.title = 'ok:' + r; }}, function (e) {{ document.title = 'error:' + e; }});"; |
| 23 | + var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content($"<!doctype html><title></title><script>{script}</script>")); |
| 24 | + |
| 25 | + for (var i = 0; i < 200 && String.IsNullOrEmpty(document.Title); i++) |
| 26 | + { |
| 27 | + await Task.Delay(20); |
| 28 | + } |
| 29 | + |
| 30 | + return document.Title; |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public async Task LibraryOfAServiceGivenByACreatorIsFound() |
| 35 | + { |
| 36 | + Assert.AreEqual("ok:object", await Run("return typeof Interop;")); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public async Task NamespaceIsAnObjectWithItsMembersAndInterfaces() |
| 41 | + { |
| 42 | + var result = await Run(@" |
| 43 | + return [typeof Interop, Object.prototype.toString.call(Interop), Interop.twice(21), typeof Interop.Thing, |
| 44 | + new Interop.Thing(5).size, new Interop.Thing(5) instanceof Interop.Thing, typeof Thing, 'twice' in window, |
| 45 | + Object.keys(window).indexOf('Interop')].join();"); |
| 46 | + |
| 47 | + Assert.AreEqual("ok:object,[object Interop],42,function,5,true,undefined,false,-1", result); |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + public async Task DelegateIsAFunction() |
| 52 | + { |
| 53 | + var result = await Run(@" |
| 54 | + var add = Interop.adder(); |
| 55 | + var join = Interop.joiner(); |
| 56 | + return [typeof add, add(2, 3), add.length, Interop.adder() === add, join(1, 'a', undefined, true), join.length, Interop.echo(join) === join, Interop.isJoiner(join)].join();"); |
| 57 | + |
| 58 | + Assert.AreEqual("ok:function,5,2,true,Double:1|String:a|Missing|Boolean:True,0,true,true", result); |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public async Task FunctionOfAScriptIsAVariadicDelegate() |
| 63 | + { |
| 64 | + var result = await Run(@" |
| 65 | + var f = function () { return Array.prototype.join.call(arguments, '+'); }; |
| 66 | + return [Interop.call(f), Interop.echo(f) === f].join();"); |
| 67 | + |
| 68 | + Assert.AreEqual("ok:1+x+,true", result); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public async Task BigIntegerIsABigInt() |
| 73 | + { |
| 74 | + var result = await Run("var b = Interop.big(); return [typeof b, b === 2n ** 70n, Interop.bigOf(12n)].join();"); |
| 75 | + Assert.AreEqual("ok:bigint,true,BigInteger:12", result); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public async Task MemoryIsASharedArrayBuffer() |
| 80 | + { |
| 81 | + var result = await Run(@" |
| 82 | + var buffer = Interop.buffer(); |
| 83 | + new Uint8Array(buffer)[1] = 7; |
| 84 | + return [buffer instanceof ArrayBuffer, buffer.byteLength, buffer === Interop.buffer(), Interop.byteAt(1)].join();"); |
| 85 | + |
| 86 | + Assert.AreEqual("ok:true,4,true,7", result); |
| 87 | + } |
| 88 | + |
| 89 | + [Test] |
| 90 | + public async Task PromiseIsATask() |
| 91 | + { |
| 92 | + var result = await Run(@" |
| 93 | + var a = await Interop.later(Promise.resolve('given')); |
| 94 | + var b = await Interop.later('value'); |
| 95 | + var c = await Interop.later(Promise.reject(new Error('no'))).catch(function (e) { return 'caught ' + e.message; }); |
| 96 | + return [a, b, c].join();"); |
| 97 | + |
| 98 | + Assert.AreEqual("ok:given!,value!,caught no", result); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public async Task ExceptionOfTheDomIsAnErrorOfItsClass() |
| 103 | + { |
| 104 | + var result = await Run(@" |
| 105 | + try { Interop.fail(); } catch (e) { |
| 106 | + return [e instanceof Interop.Failure, e instanceof Error, e.message, e.name, new Interop.Failure('made') instanceof Error].join(); |
| 107 | + }"); |
| 108 | + |
| 109 | + Assert.AreEqual("ok:true,true,it broke,Failure,true", result); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public async Task ExceptionOfTheDomCanBeAwaited() |
| 114 | + { |
| 115 | + var result = await Run(@" |
| 116 | + var made = new Interop.Failure('made'); |
| 117 | + try { await Promise.reject(made); } catch (e) { return [e === made, e.message, typeof e.stack].join(); }"); |
| 118 | + |
| 119 | + Assert.AreEqual("ok:true,made,string", result); |
| 120 | + } |
| 121 | + |
| 122 | + [Test] |
| 123 | + public async Task FunctionOfADelegateIsTheDelegateForAnObject() |
| 124 | + { |
| 125 | + Assert.AreEqual("ok:Variadic", await Run("return Interop.typeOf(Interop.joiner());")); |
| 126 | + } |
| 127 | + |
| 128 | + [Test] |
| 129 | + public async Task CssIsANamespace() |
| 130 | + { |
| 131 | + var document = await BrowsingContext.New(Helpers.GetCssConfig()).OpenAsync(m => m.Content("<!doctype html><title></title><script>document.title = [typeof CSS, CSS.escape('a b'), 'supports' in window].join()</script>")); |
| 132 | + Assert.AreEqual("object,a\\ b,false", document.Title); |
| 133 | + } |
| 134 | + |
| 135 | + [Test] |
| 136 | + public async Task MixinHasNoInterfaceObject() |
| 137 | + { |
| 138 | + Assert.AreEqual("undefined,function", await "[typeof WindowBase64, typeof btoa].join()".EvalScriptAsync()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public sealed class InteropService |
| 143 | + { |
| 144 | + } |
| 145 | + |
| 146 | + public delegate Object Variadic(Object[] arguments); |
| 147 | + |
| 148 | + [DomName("Interop")] |
| 149 | + [DomExposed("Window")] |
| 150 | + public static class InteropNamespace |
| 151 | + { |
| 152 | + private static readonly Byte[] Bytes = new Byte[4]; |
| 153 | + private static readonly Func<Double, Double, Double> Add = (a, b) => a + b; |
| 154 | + private static readonly Variadic Join = arguments => String.Join("|", Array.ConvertAll(arguments, m => m == System.Reflection.Missing.Value ? "Missing" : m.GetType().Name + ":" + m)); |
| 155 | + |
| 156 | + [DomName("twice")] |
| 157 | + public static Double Twice(this IWindow window, Double value) => 2 * value; |
| 158 | + |
| 159 | + [DomName("adder")] |
| 160 | + public static Func<Double, Double, Double> Adder() => Add; |
| 161 | + |
| 162 | + [DomName("joiner")] |
| 163 | + public static Variadic Joiner() => Join; |
| 164 | + |
| 165 | + [DomName("call")] |
| 166 | + public static Object Call(Variadic callback) => callback(new Object[] { 1, "x", null }); |
| 167 | + |
| 168 | + [DomName("isJoiner")] |
| 169 | + public static Boolean IsJoiner(Variadic callback) => ReferenceEquals(callback, Join); |
| 170 | + |
| 171 | + [DomName("echo")] |
| 172 | + public static Variadic Echo(Variadic callback) => callback; |
| 173 | + |
| 174 | + [DomName("big")] |
| 175 | + public static Object Big() => BigInteger.Pow(2, 70); |
| 176 | + |
| 177 | + [DomName("bigOf")] |
| 178 | + public static String BigOf(Object value) => value.GetType().Name + ":" + value; |
| 179 | + |
| 180 | + [DomName("buffer")] |
| 181 | + public static Object Buffer() => new Memory<Byte>(Bytes); |
| 182 | + |
| 183 | + [DomName("byteAt")] |
| 184 | + public static Int32 ByteAt(Int32 index) => Bytes[index]; |
| 185 | + |
| 186 | + [DomName("later")] |
| 187 | + public static async Task<String> Later(Task<String> value) => await value + "!"; |
| 188 | + |
| 189 | + [DomName("typeOf")] |
| 190 | + public static String TypeOf(Object value) => value.GetType().Name; |
| 191 | + |
| 192 | + [DomName("fail")] |
| 193 | + public static void Fail() => throw new InteropFailure("it broke"); |
| 194 | + } |
| 195 | + |
| 196 | + [DomName("Thing")] |
| 197 | + [DomExposed("Interop")] |
| 198 | + public sealed class InteropThing |
| 199 | + { |
| 200 | + [DomConstructor] |
| 201 | + public InteropThing(Int32 size) |
| 202 | + { |
| 203 | + Size = size; |
| 204 | + } |
| 205 | + |
| 206 | + [DomName("size")] |
| 207 | + public Int32 Size { get; } |
| 208 | + } |
| 209 | + |
| 210 | + [DomName("Failure")] |
| 211 | + [DomExposed("Interop")] |
| 212 | + public sealed class InteropFailure : Exception |
| 213 | + { |
| 214 | + [DomConstructor] |
| 215 | + public InteropFailure(String message) |
| 216 | + : base(message) |
| 217 | + { |
| 218 | + } |
| 219 | + |
| 220 | + [DomName("name")] |
| 221 | + public String Name => "Failure"; |
| 222 | + } |
| 223 | +} |
0 commit comments