コード例 #1
0
        public async Task LoadNativeThumbnailAsync_ReturnExceptionsThrownByImageLoader()
        {
            var entity = new FileEntity("test");

            _fileSystem
            .Setup(mock => mock.ReadAllBytes(entity.Path))
            .Returns(new byte[] { 0x42 });
            _imageLoader
            .Setup(mock => mock.LoadImage(entity, It.Is <Stream>(stream => stream.ReadByte() == 0x42 && stream.ReadByte() < 0)))
            .Throws(new Exception("loader exception"));

            var exception = "";

            try
            {
                var image = await _loader.LoadNativeThumbnailAsync(entity, new Size(1, 1), CancellationToken.None);
            }
            catch (Exception e)
            {
                exception = e.Message;
            }

            Assert.AreEqual("loader exception", exception);
            Assert.IsNull(entity.GetValue <ImageValue>(ExifAttributeReaderFactory.Thumbnail));
        }
コード例 #2
0
        public void GetModified_ReventRemovesEntityFromTheSavingList()
        {
            var entity = new FileEntity("test")
                         .SetAttribute(new Attribute("a", new IntValue(1), AttributeSource.Custom));

            // add entity to the modified list
            _entityManager.SetEntity(entity, true);

            // move it to the saving list
            var snapshot = _entityManager.GetModified();

            Assert.AreEqual(1, snapshot.Count);
            Assert.AreEqual(entity.Path, snapshot[0].Path);

            var loaded = _entityManager.GetEntity("test");

            Assert.AreEqual(entity, loaded);

            // remove attribute "a"
            entity.RemoveAttribute("a");
            Assert.IsFalse(entity.Any());

            // revert the entity to its initial state
            snapshot[0].Revert();
            Assert.IsTrue(entity.Any());
            Assert.AreEqual(1, entity.GetValue <IntValue>("a").Value);

            loaded = _entityManager.GetEntity("test");
            Assert.IsNull(loaded);
        }
コード例 #3
0
        public async Task LoadNativeThumbnailAsync_CancelAfterAnImageHasBeenDecoded()
        {
            var cancellation  = new CancellationTokenSource();
            var originalImage = new BitmapMock();
            var entity        = new FileEntity("test");

            _fileSystem
            .Setup(mock => mock.ReadAllBytes(entity.Path))
            .Returns(new byte[] { 0x42 });
            _imageLoader
            .Setup(mock => mock.LoadImage(entity, It.Is <Stream>(stream => stream.ReadByte() == 0x42 && stream.ReadByte() < 0)))
            .Returns(() =>
            {
                cancellation.Cancel();
                return(originalImage);
            });

            Assert.IsFalse(originalImage.IsDisposed);
            var isCanceled = false;

            try
            {
                var image = await _loader.LoadNativeThumbnailAsync(entity, new Size(1, 1), cancellation.Token);
            }
            catch (OperationCanceledException)
            {
                isCanceled = true;
            }

            Assert.IsTrue(isCanceled);
            Assert.IsNull(entity.GetValue <ImageValue>(ExifAttributeReaderFactory.Thumbnail));
            Assert.IsTrue(originalImage.IsDisposed);
        }
コード例 #4
0
        public async Task LoadNativeThumbnailAsync_DisposeOriginalImageIfGeneratorthrows()
        {
            var originalImage = new BitmapMock();
            var entity        = new FileEntity("test");

            _fileSystem
            .Setup(mock => mock.ReadAllBytes(entity.Path))
            .Returns(new byte[] { 0x42 });
            _imageLoader
            .Setup(mock => mock.LoadImage(entity, It.Is <Stream>(stream => stream.ReadByte() == 0x42 && stream.ReadByte() < 0)))
            .Returns(originalImage);
            _thumbnailGenerator
            .Setup(mock => mock.GetThumbnail(originalImage, new Size(1, 1)))
            .Throws(new Exception("test"));

            Assert.IsFalse(originalImage.IsDisposed);
            var exception = "";

            try
            {
                var image = await _loader.LoadNativeThumbnailAsync(entity, new Size(1, 1), CancellationToken.None);
            }
            catch (Exception e)
            {
                exception = e.Message;
            }

            Assert.AreEqual("test", exception);
            Assert.IsNull(entity.GetValue <ImageValue>(ExifAttributeReaderFactory.Thumbnail));
            Assert.IsTrue(originalImage.IsDisposed);
        }
コード例 #5
0
        public void Indexer_SetValue()
        {
            IEntity attrs = new FileEntity("test");

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(42), AttributeSource.Custom));

            attrs = attrs.SetAttribute(new Attribute("test", new StringValue("value"), AttributeSource.Custom));
            Assert.AreEqual("value", attrs.GetValue <StringValue>("test").Value);
        }
コード例 #6
0
        public void GetAttribute_IntAttribute()
        {
            IEntity attrs = new FileEntity("test");

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(42), AttributeSource.Custom));

            var attr = attrs.GetValue <IntValue>("test").Value;

            Assert.AreEqual(42, attr.Value);
        }
コード例 #7
0
        public void SetAttribute_NewAttribute()
        {
            IEntity attrs = new FileEntity("test");

            Assert.IsNull(attrs.GetAttribute("test"));

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(42), AttributeSource.Custom));
            Assert.IsNotNull(attrs.GetAttribute("test"));
            Assert.AreEqual(42, attrs.GetValue <IntValue>("test").Value);
        }
コード例 #8
0
        public async Task LoadNativeThumbnailAsync_ReturnExceptionsThrownByFileSystem()
        {
            var entity = new FileEntity("test");

            _fileSystem
            .Setup(mock => mock.ReadAllBytes(entity.Path))
            .Throws(new Exception("IO exception"));

            var exception = "";

            try
            {
                var image = await _loader.LoadNativeThumbnailAsync(entity, new Size(1, 1), CancellationToken.None);
            }
            catch (Exception e)
            {
                exception = e.Message;
            }

            Assert.AreEqual("IO exception", exception);
            Assert.IsNull(entity.GetValue <ImageValue>(ExifAttributeReaderFactory.Thumbnail));
        }
コード例 #9
0
        public async Task LoadNativeThumbnailAsync_DisposeIntermediateResults()
        {
            var originalImage  = new BitmapMock();
            var thumbnailImage = new BitmapMock();
            var entity         = new FileEntity("test");

            _fileSystem
            .Setup(mock => mock.ReadAllBytes(entity.Path))
            .Returns(new byte[] { 0x42 });
            _imageLoader
            .Setup(mock => mock.LoadImage(entity, It.Is <Stream>(stream => stream.ReadByte() == 0x42 && stream.ReadByte() < 0)))
            .Returns(originalImage);
            _thumbnailGenerator
            .Setup(mock => mock.GetThumbnail(originalImage, new Size(1, 1)))
            .Returns(thumbnailImage);

            Assert.IsFalse(originalImage.IsDisposed);
            Assert.IsFalse(thumbnailImage.IsDisposed);
            var image = await _loader.LoadNativeThumbnailAsync(entity, new Size(1, 1), CancellationToken.None);

            Assert.IsNotNull(entity.GetValue <ImageValue>(ExifAttributeReaderFactory.Thumbnail));
            Assert.IsTrue(originalImage.IsDisposed);
            Assert.IsTrue(thumbnailImage.IsDisposed);
        }