public void ContentPageWithMissingClass() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" x:Class=""Microsoft.Maui.Controls.Xaml.UnitTests.CustomView"" />" ; Assert.That(XamlLoader.Create(xaml, true), Is.TypeOf <ContentPage>()); }
public void ViewWithMissingClass() { var xaml = @" <ContentView xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" x:Class=""Microsoft.Maui.Controls.Xaml.UnitTests.CustomView"" />" ; Assert.That(XamlLoader.Create(xaml, true), Is.TypeOf <ContentView>()); }
public void CreateFromXamlDoesntFailOnMissingEventHandler() { var xaml = @" <Button xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" Clicked=""handleClick""> </Button>" ; Button button = null; Assert.DoesNotThrow(() => button = XamlLoader.Create(xaml, true) as Button); Assert.NotNull(button); }
public void UnknownMarkupExtensionOnMissingType() { XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(MockView); var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly""> <local:MyCustomButton Bar=""{local:Foo}"" /> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <MockView>()); }
public void CreateFromXaml() { var xaml = @" <ContentView xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" x:Class=""Xamarin.Forms.Xaml.UnitTests.FOO""> <Label Text=""Foo"" x:Name=""label""/> </ContentView>" ; var view = XamlLoader.Create(xaml); Assert.That(view, Is.TypeOf <ContentView> ()); Assert.AreEqual("Foo", ((Label)((ContentView)view).Content).Text); }
public void IgnoreMarkupExtensionException() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""> <ListView ItemsSource=""{x:Static Foo}"" /> </ContentPage>" ; var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.GreaterThan(1)); }
public void UnknownGenericType() { XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(MockView); var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""> <local:MyCustomButton x:TypeArguments=""local:MyCustomType"" /> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <MockView>()); }
public void CanIgnoreSettingPropertyThatThrows() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests""> <local:SettingPropertyThrows TestValue=""Test"" TestBP=""bar""/> </ContentPage>" ; var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.EqualTo(2)); }
public void IgnoreConverterException() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests""> <Label BackgroundColor=""AlmostPink"" /> </ContentPage>" ; var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.EqualTo(1)); }
public void CanProvideInstanceWhenReplacedTypeConstructorInvalid() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests;assembly=Microsoft.Maui.Controls.Xaml.UnitTests""> <StackLayout> <local:ReplacedType x:FactoryMethod=""CreateInstance"" /> </StackLayout> </ContentPage>" ; XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(Button); XamlLoader.InstantiationFailedCallback = (xmltype, type, exception) => new Button(); Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); }
public void UnknownMarkupExtensionKnownType() { XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(MockView); var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly""> <Button Text=""{local:Foo}"" /> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <Button>()); }
public void MissingGenericRootTypeProvidesCorrectTypeName() { var xaml = @" <local:GenericContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:MissingNamespace"" x:TypeArguments=""x:Object"" />" ; XamlLoader.FallbackTypeResolver = (p, type) => { Assert.That(p.Select(i => i.TypeName), Has.Some.EqualTo("GenericContentPage`1")); return(typeof(ContentPage)); }; Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); }
public void StaticResourceKeyNotFound() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms""> <Button Style=""{StaticResource MissingStyle}"" /> </ContentPage>" ; var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <Button>()); Assert.That(exceptions.Count, Is.EqualTo(2)); }
public void UnknownGenericType() { XamlLoader.FallbackTypeResolver = (p, type) => type ?? (p.Any(i => i.TypeName == "MyCustomButton`1") ? typeof(ProxyGenericButton <>) : typeof(MockView)); var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""> <local:MyCustomButton x:TypeArguments=""local:MyCustomType"" /> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <ProxyGenericButton <MockView> >()); }
public void IgnoreFindByNameInvalidCastException() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""> <Label x:Name=""MyName"" /> </ContentPage>" ; var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; var content = (ContentPage)XamlLoader.Create(xaml, true); Assert.DoesNotThrow(() => content.FindByName <Button>("MyName")); Assert.That(exceptions.Count, Is.GreaterThanOrEqualTo(1)); }
public void IgnoreConverterException() { var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests;assembly=Microsoft.Maui.Controls.Xaml.UnitTests""> <Label BackgroundColor=""AlmostPink"" /> </ContentPage>" ; var exceptions = new List <Exception>(); #pragma warning disable CS0618 // Type or member is obsolete Microsoft.Maui.Controls.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; #pragma warning restore CS0618 // Type or member is obsolete Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.EqualTo(1)); }
public void CanIgnoreSettingPropertyThatThrows() { var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests;assembly=Microsoft.Maui.Controls.Xaml.UnitTests""> <local:SettingPropertyThrows TestValue=""Test"" TestBP=""bar""/> </ContentPage>" ; var exceptions = new List <Exception>(); #pragma warning disable CS0618 // Type or member is obsolete Microsoft.Maui.Controls.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; #pragma warning restore CS0618 // Type or member is obsolete Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.EqualTo(2)); }
public void IgnoreFindByNameInvalidCastException() { var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""> <Label x:Name=""MyName"" /> </ContentPage>" ; var exceptions = new List <Exception>(); #pragma warning disable CS0618 // Type or member is obsolete Microsoft.Maui.Controls.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; #pragma warning restore CS0618 // Type or member is obsolete var content = (ContentPage)XamlLoader.Create(xaml, true); Assert.DoesNotThrow(() => content.FindByName <Button>("MyName")); Assert.That(exceptions.Count, Is.GreaterThanOrEqualTo(1)); }
public void ContentPageWithMissingTypeMockviewReplacement() { XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(MockView); var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <ContentPage.Content> <local:MyCustomButton /> </ContentPage.Content> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <MockView>()); }
public void StaticResourceKeyNotFound() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms""> <Button Style=""{StaticResource MissingStyle}"" /> </ContentPage>" ; var exceptions = new List <Exception>(); #pragma warning disable CS0618 // Type or member is obsolete Microsoft.Maui.Controls.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; #pragma warning restore CS0618 // Type or member is obsolete var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <Button>()); Assert.That(exceptions.Count, Is.EqualTo(2)); }
public void IgnoreNamedMissingTypeException() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests""> <StackLayout> <local:Missing x:Name=""MyName"" /> <Button x:Name=""button"" /> <Button x:Name=""button"" /> </StackLayout> </ContentPage>" ; var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.GreaterThan(1)); }
public void IgnoreMarkupExtensionException() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests""> <StackLayout> <ListView ItemsSource=""{x:Static Foo}"" /> <ListView ItemsSource=""{local:Missing Test}"" /> <Label Text=""{Binding Foo"" /> </StackLayout> </ContentPage>" ; var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.GreaterThan(1)); }
public void IgnoreMarkupExtensionException() { var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests;assembly=Microsoft.Maui.Controls.Xaml.UnitTests""> <StackLayout> <ListView ItemsSource=""{x:Static Foo}"" /> <ListView ItemsSource=""{local:Missing Test}"" /> <Label Text=""{Binding Foo"" /> </StackLayout> </ContentPage>" ; var exceptions = new List <Exception>(); #pragma warning disable CS0618 // Type or member is obsolete Microsoft.Maui.Controls.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; #pragma warning restore CS0618 // Type or member is obsolete Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.GreaterThan(1)); }
public void IgnoreNamedMissingTypeException() { var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests;assembly=Microsoft.Maui.Controls.Xaml.UnitTests""> <StackLayout> <local:Missing x:Name=""MyName"" /> <Button x:Name=""button"" /> <Button x:Name=""button"" /> </StackLayout> </ContentPage>" ; var exceptions = new List <Exception>(); #pragma warning disable CS0618 // Type or member is obsolete Microsoft.Maui.Controls.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; #pragma warning restore CS0618 // Type or member is obsolete Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.GreaterThan(1)); }
public void TextAsRandomContent() { var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests""> <StackLayout> <Label>xyz</Label> <StackLayout>foo</StackLayout> <Label><Label.FormattedText>bar</Label.FormattedText></Label> </StackLayout> </ContentPage>" ; XamlLoader.Create(xaml, true); var exceptions = new List <Exception>(); Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.GreaterThanOrEqualTo(1)); }
public void InvalidGenericType() { int exceptionCount = 0; #pragma warning disable 0618 // Type or member is obsolete Maui.Controls.Internals.ResourceLoader.ExceptionHandler = _ => exceptionCount++; XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(MockView); #pragma warning restore 0618 // Type or member is obsolete var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""> <local:MyCustomButton x:TypeArguments=""local:MyCustomType"" /> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.Null); Assert.That(exceptionCount, Is.EqualTo(1)); }
public void CssStyleTargetingRealTypeNotAppliedToMissingType() { XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(Button); XamlLoader.ValueCreatedCallback = (x, v) => { if (x.XmlTypeName == "MyCustomButton" && v is Element e) { e._cssFallbackTypeName = "MyCustomButton"; } if (x.XmlTypeName == "MyCustomButton" && v is VisualElement ve) { ve._mergedStyle.ReRegisterImplicitStyles("MissingNamespace.MyCustomButton"); } }; var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly""> <ContentPage.Resources> <StyleSheet> <![CDATA[ button { background-color: red; } ]]> </StyleSheet> </ContentPage.Resources> <StackLayout> <Button /> <MyCustomButton /> </StackLayout> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); var button = ((StackLayout)page.Content).Children[0]; var myButton = ((StackLayout)page.Content).Children[1]; Assert.That(button.BackgroundColor, Is.EqualTo(Colors.Red)); Assert.That(myButton.BackgroundColor, Is.Not.EqualTo(Colors.Red)); }
public void ContentPageWithMissingTypeNoReplacement() { XamlLoader.FallbackTypeResolver = (p, type) => type; var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <ContentPage.Content> <local:MyCustomButton Foo=""Bar""> <local:MyCustomButton.Qux> <Label /> </local:MyCustomButton.Qux> </local:MyCustomButton> </ContentPage.Content> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.Null); }
public void TextAsRandomContent() { var xaml = @" <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests;assembly=Microsoft.Maui.Controls.Xaml.UnitTests""> <StackLayout> <Label>xyz</Label> <StackLayout>foo</StackLayout> <Label><Label.FormattedText>bar</Label.FormattedText></Label> </StackLayout> </ContentPage>" ; XamlLoader.Create(xaml, true); var exceptions = new List <Exception>(); #pragma warning disable CS0618 // Type or member is obsolete Microsoft.Maui.Controls.Internals.ResourceLoader.ExceptionHandler = exceptions.Add; #pragma warning restore CS0618 // Type or member is obsolete Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true)); Assert.That(exceptions.Count, Is.GreaterThanOrEqualTo(1)); }
public void MissingTypeWithUnknownProperty() { XamlLoader.FallbackTypeResolver = (p, type) => type ?? typeof(Button); XamlLoader.ValueCreatedCallback = (x, v) => { if (x.XmlTypeName == "MyCustomButton" && v is VisualElement ve) { ve._mergedStyle.ReRegisterImplicitStyles("MissingNamespace.MyCustomButton"); } }; var xaml = @" <ContentPage xmlns=""http://xamarin.com/schemas/2014/forms"" xmlns:local=""clr-namespace:MissingNamespace;assembly=MissingAssembly"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <ContentPage.Content> <local:MyCustomButton MyColor=""Red"" /> </ContentPage.Content> </ContentPage>" ; var page = (ContentPage)XamlLoader.Create(xaml, true); Assert.That(page.Content, Is.TypeOf <Button>()); }