Skip to content

Commit

Permalink
Fixed some unit test issues in release mode. Disabled check for the e…
Browse files Browse the repository at this point in the history
…xternal storage.
  • Loading branch information
phongcao committed Feb 21, 2017
1 parent ca84637 commit 5ea8da6
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 43 deletions.
1 change: 0 additions & 1 deletion Examples/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<uap:Capability Name="removableStorage" />
</Capabilities>
</Package>
162 changes: 128 additions & 34 deletions ImagePipeline/ImagePipeline.Tests/Core/ImagePipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.UI.Xaml.Media.Imaging;

namespace ImagePipeline.Tests.Core
Expand All @@ -27,16 +28,24 @@ public class ImagePipelineTests
private readonly Uri IMAGE5_URL = new Uri("https://unsplash.it/800/600?image=5");
private readonly Uri FAILURE_URL = new Uri("https://httpbin.org/image_not_found.png");

private ImagePipelineCore _imagePipeline;
private static ImagePipelineCore _imagePipeline;
private ImageRequestBuilder _requestBuilder;

/// <summary>
/// Global Initialize
/// </summary>
[ClassInitialize]
public static void GlobalInitialize(TestContext testContext)
{
_imagePipeline = ImagePipelineFactory.Instance.GetImagePipeline();
}

/// <summary>
/// Initialize
/// </summary>
[TestInitialize]
public void Initialize()
{
_imagePipeline = ImagePipelineFactory.Instance.GetImagePipeline();
_requestBuilder = ImageRequestBuilder.NewBuilderWithSource(IMAGE_URL);
}

Expand All @@ -60,61 +69,146 @@ public async Task TestClearCaches()
/// Tests out fetching an encoded image.
/// </summary>
[TestMethod, Timeout(5000)]
public async Task TestFetchEncodedImageSuccess()
public void TestFetchEncodedImageSuccess()
{
var image = await _imagePipeline.FetchEncodedBitmapImageAsync(IMAGE_URL).ConfigureAwait(false);
Assert.IsTrue(image.GetType() == typeof(BitmapImage));
Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));
var completion = new ManualResetEvent(false);
var dataSource = _imagePipeline.FetchEncodedImage(ImageRequest.FromUri(IMAGE_URL), null);
var dataSubscriber = new BaseDataSubscriberImpl<CloseableReference<IPooledByteBuffer>>(
async response =>
{
CloseableReference<IPooledByteBuffer> reference = response.GetResult();
if (reference != null)
{
IPooledByteBuffer inputStream = reference.Get();

try
{
Assert.IsTrue(inputStream.Size != 0);
Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));
}
catch (Exception)
{
Assert.Fail();
}
finally
{
CloseableReference<IPooledByteBuffer>.CloseSafely(reference);
completion.Set();
}
}
else
{
Assert.Fail();
completion.Set();
}
},
response =>
{
Assert.Fail();
completion.Set();
});

dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
completion.WaitOne();
}

/// <summary>
/// Tests out fetching an encoded image with wrong uri.
/// </summary>
[TestMethod, Timeout(5000)]
public async Task TestFetchEncodedImageFail()
public void TestFetchEncodedImageFail()
{
try
{
var image = await _imagePipeline.FetchEncodedBitmapImageAsync(FAILURE_URL).ConfigureAwait(false);
Assert.Fail();
}
catch (IOException)
{
// This is expected
}
var completion = new ManualResetEvent(false);
var dataSource = _imagePipeline.FetchEncodedImage(ImageRequest.FromUri(FAILURE_URL), null);
var dataSubscriber = new BaseDataSubscriberImpl<CloseableReference<IPooledByteBuffer>>(
response =>
{
Assert.Fail();
completion.Set();
return Task.CompletedTask;
},
response =>
{
Assert.IsTrue(response.GetFailureCause().GetType() == typeof(IOException));
completion.Set();
});

dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
completion.WaitOne();
}

/// <summary>
/// Tests out fetching a decoded image.
/// </summary>
[TestMethod, Timeout(5000)]
public async Task TestFetchDecodedImageSuccess()
public void TestFetchDecodedImageSuccess()
{
var request = ImageRequest.FromUri(IMAGE_URL);
var image = await _imagePipeline.FetchDecodedBitmapImageAsync(request).ConfigureAwait(false);
var completion = new ManualResetEvent(false);
var dataSource = _imagePipeline.FetchDecodedImage(ImageRequest.FromUri(IMAGE_URL), null);
var dataSubscriber = new BaseDataSubscriberImpl<CloseableReference<CloseableImage>>(
async response =>
{
CloseableReference<CloseableImage> reference = response.GetResult();
if (reference != null)
{
SoftwareBitmap bitmap = ((CloseableBitmap)reference.Get()).UnderlyingBitmap;

try
{
Assert.IsTrue(bitmap.PixelWidth != 0);
Assert.IsTrue(bitmap.PixelHeight != 0);
Assert.IsTrue(_imagePipeline.IsInBitmapMemoryCache(ImageRequest.FromUri(IMAGE_URL)));
Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(IMAGE_URL).ConfigureAwait(false));
}
catch (Exception)
{
Assert.Fail();
}
finally
{
CloseableReference<CloseableImage>.CloseSafely(reference);
completion.Set();
}
}
else
{
Assert.Fail();
completion.Set();
}
},
response =>
{
Assert.Fail();
completion.Set();
});

Assert.IsTrue(image.GetType() == typeof(WriteableBitmap));
Assert.IsTrue(_imagePipeline.IsInBitmapMemoryCache(request));
Assert.IsTrue(await _imagePipeline.IsInDiskCacheAsync(request).ConfigureAwait(false));
dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
completion.WaitOne();
}

/// <summary>
/// Tests out fetching a decoded image with wrong uri.
/// </summary>
[TestMethod, Timeout(5000)]
public async Task TestFetchDecodedImageFail()
public void TestFetchDecodedImageFail()
{
try
{
var image = await _imagePipeline.FetchDecodedBitmapImageAsync(
ImageRequest.FromUri(FAILURE_URL)).ConfigureAwait(false);

Assert.Fail();
}
catch (IOException)
{
// This is expected
}
var completion = new ManualResetEvent(false);
var dataSource = _imagePipeline.FetchDecodedImage(ImageRequest.FromUri(FAILURE_URL), null);
var dataSubscriber = new BaseDataSubscriberImpl<CloseableReference<CloseableImage>>(
response =>
{
Assert.Fail();
completion.Set();
return Task.CompletedTask;
},
response =>
{
Assert.IsTrue(response.GetFailureCause().GetType() == typeof(IOException));
completion.Set();
});

dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance);
completion.WaitOne();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using System;
using System.Collections.Generic;
using System.Threading;

namespace ImagePipeline.Tests.Producers
{
Expand Down Expand Up @@ -35,7 +36,7 @@ public sealed class ThreadHandoffProducerTests : IDisposable
private string _internalProducerName;
private IDictionary<string, string> _internalExtraMap;
private int _consumerOnCancellationCount;
private bool _finishRunning;
private ManualResetEvent _completion;

/// <summary>
/// Initialize
Expand All @@ -48,17 +49,19 @@ public void Initialize()
{
_internalConsumer = consumer;
_internalProducerContext = (SettableProducerContext)producerContext;
_finishRunning = true;
_completion.Set();
});

_consumer = new BaseConsumerImpl<object>(
(_, __) => { },
(_) => { },
() =>
{
++_consumerOnCancellationCount;
_finishRunning = true;
_completion.Set();
},
(_) => { });

_producerListener = new ProducerListenerImpl(
(requestId, producerName) =>
{
Expand All @@ -80,7 +83,7 @@ public void Initialize()
(_, __, ___, ____) =>
{
++_onProducerFinishWithFailureCount;
_finishRunning = true;
_completion.Set();
},
(requestId, producerName, extraMap) =>
{
Expand Down Expand Up @@ -109,7 +112,8 @@ public void Initialize()
_threadHandoffProducer = new ThreadHandoffProducer<object>(
_inputProducer,
new ThreadHandoffProducerQueue(_testExecutorService));
_finishRunning = false;

_completion = new ManualResetEvent(false);
}

/// <summary>
Expand All @@ -129,7 +133,7 @@ public void TestSuccess()
_threadHandoffProducer.ProduceResults(_consumer, _producerContext);

// Wait until finish
while (!_finishRunning);
_completion.WaitOne();

Assert.AreSame(_internalConsumer, _consumer);
Assert.AreSame(_internalProducerContext, _producerContext);
Expand All @@ -154,7 +158,7 @@ public void TestCancellation()
_producerContext.Cancel();

// Wait until finish
while (!_finishRunning);
_completion.WaitOne();

Assert.IsNull(_internalConsumer);
Assert.IsNull(_internalProducerContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public DefaultDiskStorage(
Preconditions.CheckNotNull(rootDirectory);

_rootDirectory = rootDirectory;
_isExternal = CheckExternal(rootDirectory, cacheErrorLogger);

// Phong Cao: Checking external storage requires 'Removable devices' permission in the
// app manifest, skip it for now
_isExternal = false; // CheckExternal(rootDirectory, cacheErrorLogger);

// _versionDirectory's name identifies:
// - the cache structure's version (sharded)
Expand Down

0 comments on commit 5ea8da6

Please sign in to comment.