public void ControlCollection_ControlsCreatedOnLoad() { var innerInitCalled = false; var innerLoadCalled = false; var root = new ControlLifeCycleMock { LoadAction = (control, context) => { // generate a child control in the Load phase control.Children.Add(new ControlLifeCycleMock() { InitAction = (control2, context2) => { // test whether the Init on the inner control is called innerInitCalled = true; }, LoadAction = (control2, context2) => { // test whether the Load on the inner control is called innerLoadCalled = true; } }); } }; root.SetValue(Internal.RequestContextProperty, DotvvmTestHelper.CreateContext()); DotvvmControlCollection.InvokePageLifeCycleEventRecursive(root, LifeCycleEventType.Init); DotvvmControlCollection.InvokePageLifeCycleEventRecursive(root, LifeCycleEventType.Load); Assert.IsTrue(innerInitCalled); Assert.IsTrue(innerLoadCalled); }
public void ControlCollection_InitializedAncestorModified() { var innerInitCalled = false; var root = new ControlLifeCycleMock { InitAction = (control, context) => { // generate a child control in the Init phase control.Children.Add(new ControlLifeCycleMock()); // generate a second child that adds elements inside the first child control.Children.Add(new ControlLifeCycleMock() { InitAction = (control2, context2) => { control2.Parent.CastTo <DotvvmControl>().Children.First().Children.Add(new ControlLifeCycleMock() { InitAction = (control3, context3) => { // verify that the lately added control still calls the Init innerInitCalled = true; } }); } }); } }; root.SetValue(Internal.RequestContextProperty, DotvvmTestHelper.CreateContext()); DotvvmControlCollection.InvokePageLifeCycleEventRecursive(root, LifeCycleEventType.Init); Assert.IsTrue(innerInitCalled); }
public void ControlCollection_AddingToOptimizedControl() { var eventLog = new List <ControlLifeCycleEvent>(); var root = new ControlLifeCycleMock(eventLog, "root") { InitAction = (control, context) => { control.Children.Add(new ControlLifeCycleMock(eventLog, "root_a") { LifecycleRequirements = ControlLifecycleRequirements.None, }); }, }; root.SetValue(Internal.RequestContextProperty, DotvvmTestHelper.CreateContext()); DotvvmControlCollection.InvokePageLifeCycleEventRecursive(root, LifeCycleEventType.PreRender); var root_New = new ControlLifeCycleMock(eventLog, "root_New"); root.Children.Add(root_New); var root_a_New = new ControlLifeCycleMock(eventLog, "root_a_New"); root.Children.First().Children.Add(root_a_New); Assert.IsTrue(eventLog.Count(e => e.Control == root_a_New) == eventLog.Count(e => e.Control == root_New), "Control added to root has not the same number of event as control added to the optimized one."); }
private TestDotvvmRequestContext PrepareRequest( string fileName, PostbackRequestModel postback = null ) { var context = DotvvmTestHelper.CreateContext(configuration); context.CsrfToken = null; context.Route = new Framework.Routing.DotvvmRoute( "testpage", fileName, null, null, configuration); var httpContext = (TestHttpContext)context.HttpContext; if (postback is object) { httpContext.Request.Method = "POST"; httpContext.Request.Headers["X-DotVVM-PostBack"] = new[] { "true" }; httpContext.Request.Body = new MemoryStream( new UTF8Encoding(false).GetBytes( JsonConvert.SerializeObject(postback) ) ); } return(context); }
public void LocalizablePresenter_RedirectsOnInvalidLanguageCode() { var config = DotvvmConfiguration.CreateDefault(); var presenterFactory = LocalizablePresenter.BasedOnParameter("Lang"); config.RouteTable.Add("Test", "test/Lang", "test", new { Lang = "en" }, presenterFactory); var context = DotvvmTestHelper.CreateContext(config); context.Parameters["Lang"] = "cz"; context.Route = config.RouteTable.First(); var httpRequest = new TestHttpContext(); httpRequest.Request = new TestHttpRequest(httpRequest) { PathBase = "" }; httpRequest.Request.Headers.Add(HostingConstants.SpaContentPlaceHolderHeaderName, new string[0]); context.HttpContext = httpRequest; var localizablePresenter = presenterFactory(config.ServiceProvider); Assert.ThrowsException <DotvvmInterruptRequestExecutionException>(() => localizablePresenter.ProcessRequest(context)); }
private DotvvmControl CompileMarkup(string markup, Dictionary <string, string> markupFiles = null, bool compileTwice = false, [CallerMemberName] string fileName = null) { if (markupFiles == null) { markupFiles = new Dictionary <string, string>(); } markupFiles[fileName + ".dothtml"] = markup; var config = DotvvmTestHelper.CreateConfiguration(services => { services.AddSingleton <IMarkupFileLoader>(new FakeMarkupFileLoader(markupFiles)); services.AddSingleton <CustomControlFactory>((s, t) => t == typeof(TestCustomDependencyInjectionControl) ? new TestCustomDependencyInjectionControl("") { IsCorrectlyCreated = true } : throw new Exception()); }); context = DotvvmTestHelper.CreateContext(config); context.Configuration.ApplicationPhysicalPath = Path.GetTempPath(); context.Configuration.Markup.Controls.Add(new DotvvmControlConfiguration() { TagPrefix = "cc", TagName = "Test1", Src = "test1.dothtml" }); context.Configuration.Markup.Controls.Add(new DotvvmControlConfiguration() { TagPrefix = "cc", TagName = "Test2", Src = "test2.dothtml" }); context.Configuration.Markup.Controls.Add(new DotvvmControlConfiguration() { TagPrefix = "cc", TagName = "Test3", Src = "test3.dothtml" }); context.Configuration.Markup.Controls.Add(new DotvvmControlConfiguration() { TagPrefix = "cc", TagName = "Test4", Src = "test4.dothtml" }); context.Configuration.Markup.Controls.Add(new DotvvmControlConfiguration() { TagPrefix = "cc", TagName = "Test5", Src = "test5.dothtml" }); context.Configuration.Markup.AddCodeControls("ff", typeof(TestControl)); context.Configuration.Markup.AddAssembly(typeof(DefaultViewCompilerTests).GetTypeInfo().Assembly.GetName().Name); var controlBuilderFactory = context.Services.GetRequiredService <IControlBuilderFactory>(); var(_, controlBuilder) = controlBuilderFactory.GetControlBuilder(fileName + ".dothtml"); var result = controlBuilder.Value.BuildControl(controlBuilderFactory, context.Services); if (compileTwice) { result = controlBuilder.Value.BuildControl(controlBuilderFactory, context.Services); } result.SetValue(Internal.RequestContextProperty, context); return(result); }
public void ControlCollection_NestedControlsWithNoneRequirementParent() { var initCalled = false; var loadCalled = false; var preRenderCalled = false; var firstChild = new ControlLifeCycleMock() { LifecycleRequirements = ControlLifecycleRequirements.All, }; var secondChild = new ControlLifeCycleMock() { LifecycleRequirements = ControlLifecycleRequirements.All, InitAction = (control, _) => { initCalled = true; }, LoadAction = (control, _) => { loadCalled = true; }, PreRenderAction = (control, _) => { preRenderCalled = true; } }; var root = new ControlLifeCycleMock(); root.RenderAction = (control, _) => { var innerRoot = new ControlLifeCycleMock() { LifecycleRequirements = ControlLifecycleRequirements.None }; root.Children.Add(innerRoot); var td1 = new ControlLifeCycleMock() { LifecycleRequirements = ControlLifecycleRequirements.None }; innerRoot.Children.Add(td1); td1.Children.Add(firstChild); var td2 = new ControlLifeCycleMock() { LifecycleRequirements = ControlLifecycleRequirements.None }; innerRoot.Children.Add(td2); td2.Children.Add(secondChild); }; root.SetValue(Internal.RequestContextProperty, DotvvmTestHelper.CreateContext()); DotvvmControlCollection.InvokePageLifeCycleEventRecursive(root, LifeCycleEventType.PreRenderComplete); root.Render(null, null); Assert.IsTrue(initCalled); Assert.IsTrue(loadCalled); Assert.IsTrue(preRenderCalled); }
public void MustEncodeUrls(string forbiddenString) { var resources = new IResource [] { new ScriptResource(new UrlResourceLocation("http://server.com/" + forbiddenString + "somethingelse")), new StylesheetResource(new UrlResourceLocation("file:///" + forbiddenString)) { LocationFallback = new ResourceLocationFallback("true", new UrlResourceLocation(forbiddenString), new UrlResourceLocation("http://" + forbiddenString)) } }; var cx = DotvvmTestHelper.CreateContext(DotvvmTestHelper.DefaultConfig); var output = new StringWriter(); var w = new HtmlWriter(output, cx); foreach (var a in resources) { a.Render(w, cx, forbiddenString); } Assert.IsFalse(output.ToString().Contains(forbiddenString)); }
public void ControlCollection_ControlsDeepNesting() { var eventLog = new List <ControlLifeCycleEvent>(); var root = new ControlLifeCycleMock(eventLog, "root") { LoadAction = (control, context) => { control.Children.Add(new ControlLifeCycleMock(eventLog, "root_a") { LoadAction = (control2, context2) => { control2.Children.Add(new ControlLifeCycleMock(eventLog, "root_a_b") { LoadAction = (control3, context3) => { control3.Children.Add(new ControlLifeCycleMock(eventLog, "root_a_b_c") { InitAction = (control4, context4) => { control4.Children.Add(new ControlLifeCycleMock(eventLog, "root_a_b_c_d")); } }); } }); } }); } }; root.SetValue(Internal.RequestContextProperty, DotvvmTestHelper.CreateContext()); DotvvmControlCollection.InvokePageLifeCycleEventRecursive(root, LifeCycleEventType.PreRender); var index = 0; Action <string, LifeCycleEventType, bool> verifyAction = (name, eventType, isEntering) => { Assert.AreEqual(name, eventLog[index].Control.Name); Assert.AreEqual(eventType, eventLog[index].EventType); Assert.AreEqual(isEntering, eventLog[index].IsEntering); index++; }; verifyAction("root", LifeCycleEventType.PreInit, true); verifyAction("root", LifeCycleEventType.PreInit, false); verifyAction("root", LifeCycleEventType.Init, true); verifyAction("root", LifeCycleEventType.Init, false); verifyAction("root", LifeCycleEventType.Load, true); verifyAction("root_a", LifeCycleEventType.PreInit, true); verifyAction("root_a", LifeCycleEventType.PreInit, false); verifyAction("root_a", LifeCycleEventType.Init, true); verifyAction("root_a", LifeCycleEventType.Init, false); verifyAction("root", LifeCycleEventType.Load, false); verifyAction("root_a", LifeCycleEventType.Load, true); verifyAction("root_a_b", LifeCycleEventType.PreInit, true); verifyAction("root_a_b", LifeCycleEventType.PreInit, false); verifyAction("root_a_b", LifeCycleEventType.Init, true); verifyAction("root_a_b", LifeCycleEventType.Init, false); verifyAction("root_a", LifeCycleEventType.Load, false); verifyAction("root_a_b", LifeCycleEventType.Load, true); verifyAction("root_a_b_c", LifeCycleEventType.PreInit, true); verifyAction("root_a_b_c", LifeCycleEventType.PreInit, false); verifyAction("root_a_b_c", LifeCycleEventType.Init, true); verifyAction("root_a_b_c_d", LifeCycleEventType.PreInit, true); verifyAction("root_a_b_c_d", LifeCycleEventType.PreInit, false); verifyAction("root_a_b_c", LifeCycleEventType.Init, false); verifyAction("root_a_b_c_d", LifeCycleEventType.Init, true); verifyAction("root_a_b_c_d", LifeCycleEventType.Init, false); verifyAction("root_a_b", LifeCycleEventType.Load, false); verifyAction("root_a_b_c", LifeCycleEventType.Load, true); verifyAction("root_a_b_c", LifeCycleEventType.Load, false); verifyAction("root_a_b_c_d", LifeCycleEventType.Load, true); verifyAction("root_a_b_c_d", LifeCycleEventType.Load, false); verifyAction("root", LifeCycleEventType.PreRender, true); verifyAction("root", LifeCycleEventType.PreRender, false); verifyAction("root_a", LifeCycleEventType.PreRender, true); verifyAction("root_a", LifeCycleEventType.PreRender, false); verifyAction("root_a_b", LifeCycleEventType.PreRender, true); verifyAction("root_a_b", LifeCycleEventType.PreRender, false); verifyAction("root_a_b_c", LifeCycleEventType.PreRender, true); verifyAction("root_a_b_c", LifeCycleEventType.PreRender, false); verifyAction("root_a_b_c_d", LifeCycleEventType.PreRender, true); verifyAction("root_a_b_c_d", LifeCycleEventType.PreRender, false); }