diff --git a/Examples/Package.appxmanifest b/Examples/Package.appxmanifest index f091038..8558340 100644 --- a/Examples/Package.appxmanifest +++ b/Examples/Package.appxmanifest @@ -24,6 +24,5 @@ - \ No newline at end of file diff --git a/ImagePipeline/ImagePipeline.Tests/Core/ImagePipelineTests.cs b/ImagePipeline/ImagePipeline.Tests/Core/ImagePipelineTests.cs index 10827f8..d718e5a 100644 --- a/ImagePipeline/ImagePipeline.Tests/Core/ImagePipelineTests.cs +++ b/ImagePipeline/ImagePipeline.Tests/Core/ImagePipelineTests.cs @@ -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 @@ -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; + /// + /// Global Initialize + /// + [ClassInitialize] + public static void GlobalInitialize(TestContext testContext) + { + _imagePipeline = ImagePipelineFactory.Instance.GetImagePipeline(); + } + /// /// Initialize /// [TestInitialize] public void Initialize() { - _imagePipeline = ImagePipelineFactory.Instance.GetImagePipeline(); _requestBuilder = ImageRequestBuilder.NewBuilderWithSource(IMAGE_URL); } @@ -60,61 +69,146 @@ public async Task TestClearCaches() /// Tests out fetching an encoded image. /// [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>( + async response => + { + CloseableReference 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.CloseSafely(reference); + completion.Set(); + } + } + else + { + Assert.Fail(); + completion.Set(); + } + }, + response => + { + Assert.Fail(); + completion.Set(); + }); + + dataSource.Subscribe(dataSubscriber, CallerThreadExecutor.Instance); + completion.WaitOne(); } /// /// Tests out fetching an encoded image with wrong uri. /// [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>( + 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(); } /// /// Tests out fetching a decoded image. /// [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>( + async response => + { + CloseableReference 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.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(); } /// /// Tests out fetching a decoded image with wrong uri. /// [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>( + 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(); } /// diff --git a/ImagePipeline/ImagePipeline.Tests/Producers/ThreadHandoffProducerTests.cs b/ImagePipeline/ImagePipeline.Tests/Producers/ThreadHandoffProducerTests.cs index 76e0166..c7314a1 100644 --- a/ImagePipeline/ImagePipeline.Tests/Producers/ThreadHandoffProducerTests.cs +++ b/ImagePipeline/ImagePipeline.Tests/Producers/ThreadHandoffProducerTests.cs @@ -5,6 +5,7 @@ using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; using System; using System.Collections.Generic; +using System.Threading; namespace ImagePipeline.Tests.Producers { @@ -35,7 +36,7 @@ public sealed class ThreadHandoffProducerTests : IDisposable private string _internalProducerName; private IDictionary _internalExtraMap; private int _consumerOnCancellationCount; - private bool _finishRunning; + private ManualResetEvent _completion; /// /// Initialize @@ -48,17 +49,19 @@ public void Initialize() { _internalConsumer = consumer; _internalProducerContext = (SettableProducerContext)producerContext; - _finishRunning = true; + _completion.Set(); }); + _consumer = new BaseConsumerImpl( (_, __) => { }, (_) => { }, () => { ++_consumerOnCancellationCount; - _finishRunning = true; + _completion.Set(); }, (_) => { }); + _producerListener = new ProducerListenerImpl( (requestId, producerName) => { @@ -80,7 +83,7 @@ public void Initialize() (_, __, ___, ____) => { ++_onProducerFinishWithFailureCount; - _finishRunning = true; + _completion.Set(); }, (requestId, producerName, extraMap) => { @@ -109,7 +112,8 @@ public void Initialize() _threadHandoffProducer = new ThreadHandoffProducer( _inputProducer, new ThreadHandoffProducerQueue(_testExecutorService)); - _finishRunning = false; + + _completion = new ManualResetEvent(false); } /// @@ -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); @@ -154,7 +158,7 @@ public void TestCancellation() _producerContext.Cancel(); // Wait until finish - while (!_finishRunning); + _completion.WaitOne(); Assert.IsNull(_internalConsumer); Assert.IsNull(_internalProducerContext); diff --git a/ImagePipeline/ImagePipelineBase/Cache/Disk/DefaultDiskStorage.cs b/ImagePipeline/ImagePipelineBase/Cache/Disk/DefaultDiskStorage.cs index eec694c..70b2e15 100644 --- a/ImagePipeline/ImagePipelineBase/Cache/Disk/DefaultDiskStorage.cs +++ b/ImagePipeline/ImagePipelineBase/Cache/Disk/DefaultDiskStorage.cs @@ -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)