Skip to content

Commit

Permalink
Test case cleanup (#98)
Browse files Browse the repository at this point in the history
* Formatting
* Moved to a specific test case in EdgeCasesPage
  • Loading branch information
jonathanpeppers authored Oct 12, 2021
1 parent 020cead commit ca15efd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions glidex.forms.sample/Forms/EdgeCasesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
x:Class="Android.Glide.Sample.EdgeCasesPage">
<StackLayout x:Name="_stack" Padding="5">
<Label Text="This is just a bunch of stuff that shouldn't crash the app." />
<Label Text="Four images should load below." />
</StackLayout>
</sample:ProfilerPage>
7 changes: 6 additions & 1 deletion glidex.forms.sample/Forms/EdgeCasesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public EdgeCasesPage ()
{
InitializeComponent ();

_stack.Children.Add (new Image {
HeightRequest = 50,
// Named the same as an AndroidResource
Source = ImageSource.FromFile (Images.CopyToTempFile ("assetpatch2.jpg", "patch1.jpg")),
});
_stack.Children.Add (new Image {
HeightRequest = 50,
Source = Images.RandomSource (),
Expand Down Expand Up @@ -41,4 +46,4 @@ public EdgeCasesPage ()
});
}
}
}
}
28 changes: 18 additions & 10 deletions glidex.forms.sample/Forms/Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ public static class Images
{
static readonly Random random = new Random ();
static readonly string tempDir = Path.Combine (Path.GetTempPath (), "glide.forms.sample");
static readonly string tempPath = Path.Combine (tempDir, "patch1.jpg");
const int MaxImages = 12;

public static IEnumerable<ImageSource> RandomSources ()
{
var random = new Random ();
for (int i = 0; i < 100; i++) {
yield return RandomSource ();
}
Expand Down Expand Up @@ -44,14 +42,7 @@ public static ImageSource SourceById (int x)
return ImageSource.FromFile ("patch" + (x - 2));
//File path
case 7:
if (!File.Exists(tempPath)) {
Directory.CreateDirectory (tempDir);
using (var assetStream = Android.App.Application.Context.Assets.Open("assetpatch1.jpg"))
using (var fileStream = File.Create (tempPath)) {
assetStream.CopyTo (fileStream);
}
}
return ImageSource.FromFile (tempPath);
return ImageSource.FromFile (CopyToTempFile ("assetpatch1.jpg"));
//Stream
case 8:
return ImageSource.FromStream (() => Android.App.Application.Context.Assets.Open ("assetpatch2.jpg"));
Expand All @@ -67,6 +58,23 @@ public static ImageSource SourceById (int x)
}
}

/// <summary>
/// Copies an AndroidAsset to a temp file and returns the full path to it
/// </summary>
public static string CopyToTempFile (string assetName, string destinationName = "")
{
if (string.IsNullOrEmpty (destinationName))
destinationName = assetName;
string tempPath = Path.Combine (tempDir, destinationName);
if (!File.Exists (tempPath)) {
Directory.CreateDirectory (tempDir);
using var assetStream = Android.App.Application.Context.Assets.Open (assetName);
using var fileStream = File.Create (tempPath);
assetStream.CopyTo (fileStream);
}
return tempPath;
}

public static IEnumerable<ImageSource> GetPicsumImages (int count)
{
var width = (int) (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density);
Expand Down
2 changes: 1 addition & 1 deletion glidex.forms/GlideExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static RequestBuilder HandleFileImageSource (RequestManager request, FileImageSo
{
RequestBuilder builder;
var fileName = source.File;
var drawable = File.Exists(fileName) ? 0 : ResourceManager.GetDrawableByName(fileName);
var drawable = File.Exists (fileName) ? 0 : ResourceManager.GetDrawableByName (fileName);
if (drawable != 0) {
Forms.Debug ("Loading `{0}` as an Android resource", fileName);
builder = request.Load (drawable);
Expand Down

0 comments on commit ca15efd

Please sign in to comment.