|
| 1 | +namespace AngleSharp.Js.Tests |
| 2 | +{ |
| 3 | + using AngleSharp.Dom; |
| 4 | + using AngleSharp.Io; |
| 5 | + using AngleSharp.Js.Tests.Mocks; |
| 6 | + using AngleSharp.Scripting; |
| 7 | + using Jint; |
| 8 | + using Jint.Runtime; |
| 9 | + using NUnit.Framework; |
| 10 | + using System; |
| 11 | + using System.Collections.Concurrent; |
| 12 | + using System.Threading; |
| 13 | + using System.Threading.Tasks; |
| 14 | + |
| 15 | + public class EngineConfigurationTests |
| 16 | + { |
| 17 | + [Test] |
| 18 | + public async Task ConstraintsAreInstalledBeforeInlineScriptsRun() |
| 19 | + { |
| 20 | + var config = Configuration.Default |
| 21 | + .With(new EngineCreator((window, options) => new Engine(options.MaxStatements(64)))) |
| 22 | + .WithJs() |
| 23 | + .WithEventLoop(); |
| 24 | + |
| 25 | + using (var context = BrowsingContext.New(config)) |
| 26 | + { |
| 27 | + var document = await context.OpenAsync(request => request.Content( |
| 28 | + "<div id='result'>before</div><script>for (var i = 0; i < 1000; i++) {} " + |
| 29 | + "document.getElementById('result').textContent = 'after';</script>")).ConfigureAwait(false); |
| 30 | + |
| 31 | + Assert.AreEqual("before", document.QuerySelector("#result").TextContent); |
| 32 | + Assert.Throws<StatementsCountOverflowException>(() => document.ExecuteScript( |
| 33 | + "for (var i = 0; i < 1000; i++) {}")); |
| 34 | + Assert.AreEqual("before", document.ExecuteScript("document.getElementById('result').textContent")); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + [Test] |
| 39 | + public async Task CreatorServiceCanBeCreatedForEachBrowsingContext() |
| 40 | + { |
| 41 | + var services = 0; |
| 42 | + Func<IBrowsingContext, EngineCreator> createService = _ => |
| 43 | + { |
| 44 | + services++; |
| 45 | + var limit = services == 1 ? 64 : 2000; |
| 46 | + return (window, options) => new Engine(options.MaxStatements(limit)); |
| 47 | + }; |
| 48 | + var config = Configuration.Default.With(createService).WithJs().WithEventLoop(); |
| 49 | + |
| 50 | + using (var first = BrowsingContext.New(config)) |
| 51 | + using (var second = BrowsingContext.New(config)) |
| 52 | + { |
| 53 | + var firstDocument = await first.OpenNewAsync().ConfigureAwait(false); |
| 54 | + var secondDocument = await second.OpenNewAsync().ConfigureAwait(false); |
| 55 | + Assert.Throws<StatementsCountOverflowException>(() => firstDocument.ExecuteScript( |
| 56 | + "for (var i = 0; i < 1000; i++) {}")); |
| 57 | + Assert.AreEqual(100, secondDocument.ExecuteScript("var i = 0; for (; i < 100; i++) {} i;")); |
| 58 | + Assert.AreEqual(2, services); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public async Task CreatorRunsOncePerWindowAndRetainsDomWrapping() |
| 64 | + { |
| 65 | + var calls = 0; |
| 66 | + var windows = new ConcurrentQueue<IWindow>(); |
| 67 | + var config = Configuration.Default.With(new EngineCreator((window, options) => |
| 68 | + { |
| 69 | + calls++; |
| 70 | + windows.Enqueue(window); |
| 71 | + return new Engine(options.MaxStatements(1000)); |
| 72 | + })).WithJs().WithEventLoop(); |
| 73 | + |
| 74 | + using (var firstContext = BrowsingContext.New(config)) |
| 75 | + using (var secondContext = BrowsingContext.New(config)) |
| 76 | + { |
| 77 | + var first = await firstContext.OpenAsync(request => request.Content("<p>first</p>")).ConfigureAwait(false); |
| 78 | + var second = await secondContext.OpenAsync(request => request.Content("<p>second</p>")).ConfigureAwait(false); |
| 79 | + |
| 80 | + Assert.AreEqual("first", first.ExecuteScript("document.querySelector('p').textContent")); |
| 81 | + Assert.AreEqual("first", first.ExecuteScript("document.querySelector('p').textContent")); |
| 82 | + Assert.AreEqual("second", second.ExecuteScript("document.querySelector('p').textContent")); |
| 83 | + Assert.AreEqual(2, calls); |
| 84 | + CollectionAssert.AreEqual(new[] { first.DefaultView, second.DefaultView }, windows.ToArray()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public async Task CreatorReturnsTheEngineUsedForInlineScriptsAndDomBindings() |
| 90 | + { |
| 91 | + Engine created = null; |
| 92 | + var config = Configuration.Default.With(new EngineCreator((window, options) => |
| 93 | + { |
| 94 | + created = new Engine(options.Configure(engine => engine.SetValue("hostValue", "configured"))); |
| 95 | + return created; |
| 96 | + })).WithJs().WithEventLoop(); |
| 97 | + |
| 98 | + using (var context = BrowsingContext.New(config)) |
| 99 | + { |
| 100 | + var document = await context.OpenAsync(request => request.Content( |
| 101 | + "<p>before</p><script>document.querySelector('p').textContent = hostValue;</script>")).ConfigureAwait(false); |
| 102 | + Assert.AreEqual("configured", document.QuerySelector("p").TextContent); |
| 103 | + Assert.AreSame(created, context.GetService<JsScriptingService>().GetOrCreateJint(document)); |
| 104 | + created.SetValue("hostNode", document.QuerySelector("p")); |
| 105 | + Assert.AreEqual(true, document.ExecuteScript("hostNode === document.querySelector('p')")); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + [TestCase(false)] |
| 110 | + [TestCase(true)] |
| 111 | + public async Task WorkersInheritTheEngineCreatorService(Boolean contextFactory) |
| 112 | + { |
| 113 | + var services = 0; |
| 114 | + var windows = new ConcurrentQueue<IWindow>(); |
| 115 | + EngineCreator CreateEngine(String hostValue) => (window, options) => |
| 116 | + { |
| 117 | + windows.Enqueue(window); |
| 118 | + return new Engine(options.MaxStatements(256) |
| 119 | + .Configure(engine => engine.SetValue("hostValue", hostValue))); |
| 120 | + }; |
| 121 | + Object registration = contextFactory |
| 122 | + ? (Object)new Func<IBrowsingContext, EngineCreator>(_ => CreateEngine("context-" + Interlocked.Increment(ref services))) |
| 123 | + : CreateEngine("configured"); |
| 124 | + var config = Configuration.Default |
| 125 | + .With(registration) |
| 126 | + .WithJs() |
| 127 | + .WithEventLoop() |
| 128 | + .With(new DelayedRequester(0, "self.__host = hostValue; for (var i = 0; i < 10000; i++) {} self.__after = true;")) |
| 129 | + .WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true }); |
| 130 | + |
| 131 | + using (var context = BrowsingContext.New(config)) |
| 132 | + { |
| 133 | + var document = await context.OpenNewAsync().ConfigureAwait(false); |
| 134 | + Assert.AreEqual(contextFactory ? "context-1" : "configured", document.ExecuteScript("hostValue")); |
| 135 | + var worker = new Dom.Worker(document.DefaultView, "https://example.com/worker.js"); |
| 136 | + |
| 137 | + try |
| 138 | + { |
| 139 | + for (var retries = 200; retries > 0 && !worker.IsInitialized && worker.StartupError is null; retries--) |
| 140 | + { |
| 141 | + await Task.Delay(10).ConfigureAwait(false); |
| 142 | + } |
| 143 | + |
| 144 | + Assert.IsInstanceOf<StatementsCountOverflowException>(worker.StartupError); |
| 145 | + Assert.AreEqual(contextFactory ? "context-2" : "configured", worker.EvaluateInWorker("self.__host")); |
| 146 | + Assert.AreEqual("undefined", worker.EvaluateInWorker("typeof self.__after")); |
| 147 | + var createdWindows = windows.ToArray(); |
| 148 | + Assert.AreEqual(2, createdWindows.Length); |
| 149 | + Assert.AreSame(document.DefaultView, createdWindows[0]); |
| 150 | + Assert.AreNotSame(document.DefaultView, createdWindows[1]); |
| 151 | + Assert.AreNotSame(context, createdWindows[1].Document.Context); |
| 152 | + Assert.AreSame(createdWindows[1], createdWindows[1].Document.DefaultView); |
| 153 | + if (contextFactory) |
| 154 | + { |
| 155 | + Assert.AreEqual(2, services); |
| 156 | + } |
| 157 | + } |
| 158 | + finally |
| 159 | + { |
| 160 | + worker.Terminate(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments