public override Task <IImageSourceServiceResult <Drawable> > GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken = default)
            {
                if (imageSource is not ICustomImageSourceStub imageSourceStub)
                {
                    return(Task.FromResult <IImageSourceServiceResult <Drawable> >(null));
                }

                var color = imageSourceStub.Color;

                var drawable = _cache.Get(color);

                var result = new ImageSourceServiceResult(drawable, () => _cache.Return(color));

                return(Task.FromResult <IImageSourceServiceResult <Drawable> >(result));
            }
        public void ResultsDisposeCorrectlyAndOnce()
        {
            var dispose = 0;

            var cache = new CustomImageCacheStub();
            var image = cache.Get(Colors.Red);

            var result = new ImageSourceServiceResult(image, () => dispose++);

            Assert.False(result.IsDisposed);
            Assert.Equal(0, dispose);

            result.Dispose();

            Assert.True(result.IsDisposed);
            Assert.Equal(1, dispose);

            result.Dispose();

            Assert.True(result.IsDisposed);
            Assert.Equal(1, dispose);

            cache.Return(Colors.Red);
        }
示例#3
0
        public async Task ResultsDisposeCorrectlyAndOnce()
        {
            var dispose = 0;

            var cache = new CustomImageCacheStub();
            var image = await InvokeOnMainThreadAsync(() => cache.Get(Colors.Red));

            var result = new ImageSourceServiceResult(image, () => dispose++);

            Assert.False(result.IsDisposed);
            Assert.Equal(0, dispose);

            result.Dispose();

            Assert.True(result.IsDisposed);
            Assert.Equal(1, dispose);

            result.Dispose();

            Assert.True(result.IsDisposed);
            Assert.Equal(1, dispose);

            cache.Return(Colors.Red);
        }