Skip to content

Commit

Permalink
Fix broken Android platform tests (xamarin#14590)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Sep 9, 2021
1 parent 8fcf470 commit 0b7b8c4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,52 @@
Margin="12, 0"
Padding="12">
<Label
Text="BoxView"/>
Text="Color BoxView"
FontSize="Micro"/>
<BoxView
x:Name="SolidBoxView"
HeightRequest="60"
WidthRequest="120"
CornerRadius="12"
BackgroundColor="Red"/>
<Label
Text="Gradient BoxView"
FontSize="Micro"/>
<BoxView
x:Name="GradientBoxView"
HeightRequest="120"
HeightRequest="60"
WidthRequest="120"
CornerRadius="12"
Background="{StaticResource LinearBrush}"/>
<Label
Text="Frame"/>
Text="Color Frame"
FontSize="Micro"/>
<Frame
x:Name="SolidFrame"
HasShadow="True"
HeightRequest="60"
WidthRequest="120"
CornerRadius="12"
Padding="6"
BackgroundColor="Red">
<Grid>
<Label
HorizontalOptions="Center"
VerticalOptions="Center"
TextColor="White"
Text="Color"/>
</Grid>
</Frame>
<Label
Text="Gradient Frame"
FontSize="Micro"/>
<Frame
x:Name="GradientFrame"
HasShadow="True"
HeightRequest="120"
HeightRequest="60"
WidthRequest="120"
CornerRadius="12"
Padding="6"
Background="{StaticResource LinearBrush}">
<Grid>
<Label
Expand Down
13 changes: 13 additions & 0 deletions Xamarin.Forms.Platform.Android.UnitTests/AssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ public static AColor ColorAtPoint(this Bitmap bitmap, int x, int y, bool include
}
}

public static void ForceDraw(this AView view)
{
var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888);

var canvas = new Canvas(bitmap);
canvas.Save();
canvas.Translate(0, 0);
view.Draw(canvas);
canvas.Restore();

bitmap.Dispose();
}

public static Bitmap ToBitmap(this AView view)
{
var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888);
Expand Down
3 changes: 3 additions & 0 deletions Xamarin.Forms.Platform.Android.UnitTests/CornerRadiusTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public async Task CheckCornerRadius(VisualElement visualElement)
var view = renderer.View;
Layout(visualElement, view);

if (view.Background is GradientStrokeDrawable)
view.ForceDraw();

// Need to parent the Frame for it to work on lower APIs (below Marshmallow)
ParentView(view);
var image = view.ToBitmap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ async Task<TProperty> GetRendererPropertyWithLayout<TProperty>(VisualElement ele
{
var view = renderer.View;
Layout(element, view);

if(view.Background is GradientStrokeDrawable)
view.ForceDraw();

return getProperty(renderer);
}
});
Expand Down
3 changes: 2 additions & 1 deletion Xamarin.Forms.Platform.Android/GradientStrokeDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ protected override void OnDraw(Shape shape, Canvas canvas, Paint paint)
if (_backgroundColor.HasValue)
paint.Color = _backgroundColor.Value;

shape.Draw(canvas, _strokePaint);
if (_strokePaint.StrokeWidth > 0)
shape.Draw(canvas, _strokePaint);
}

public abstract class GradientShader
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.Android/Renderers/BoxRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void UpdateCornerRadius()
{
Shape = new RectShape()
});
if (Background is GradientDrawable backgroundGradient)
if (Background is GradientStrokeDrawable backgroundGradient)
{
var cornerRadii = new[] {
(float)(Context.ToPixels(cornerRadius.TopLeft)),
Expand Down

0 comments on commit 0b7b8c4

Please sign in to comment.