コード例 #1
0
        public void GetBytesOfReducedImage_LargeJpgImageReduced()
        {
            // LakePendOreille.jpg:        JPEG image data, JFIF standard 1.01, ... precision 8, 3264x2448, frames 3

            var path          = SIL.IO.FileLocator.GetFileDistributedWithApplication(_pathToTestImages, "LakePendOreille.jpg");
            var originalBytes = File.ReadAllBytes(path);
            var reducedBytes  = BookCompressor.GetBytesOfReducedImage(path);

            Assert.Greater(originalBytes.Length, reducedBytes.Length, "LakePendOreille.jpg is reduced from 3264x2448");
            using (var tempFile = TempFile.WithExtension(Path.GetExtension(path)))
            {
                var oldMetadata = Metadata.FromFile(path);
                RobustFile.WriteAllBytes(tempFile.Path, reducedBytes);
                var newMetadata = Metadata.FromFile(tempFile.Path);
                if (oldMetadata.IsEmpty)
                {
                    Assert.IsTrue(newMetadata.IsEmpty);
                }
                else
                {
                    Assert.IsFalse(newMetadata.IsEmpty);
                    Assert.AreEqual(oldMetadata.CopyrightNotice, newMetadata.CopyrightNotice, "copyright preserved for LakePendOreille.jpg");
                    Assert.AreEqual(oldMetadata.Creator, newMetadata.Creator, "creator preserved for LakePendOreille.jpg");
                    Assert.AreEqual(oldMetadata.License.ToString(), newMetadata.License.ToString(), "license preserved for LakePendOreille.jpg");
                }
            }
        }
コード例 #2
0
        public void GetBytesOfReducedImage_LargePngImageReduced()
        {
            // shirtWithTransparentBg.png: PNG image data, 2208 x 2400, 8-bit/color RGBA, non-interlaced

            var path          = SIL.IO.FileLocator.GetFileDistributedWithApplication(_pathToTestImages, "shirt.png");
            var originalBytes = File.ReadAllBytes(path);
            var reducedBytes  = BookCompressor.GetBytesOfReducedImage(path);

            Assert.Greater(originalBytes.Length, reducedBytes.Length, "shirt.png is reduced from 2208x2400");
            using (var tempFile = TempFile.WithExtension(Path.GetExtension(path)))
            {
                var oldMetadata = Metadata.FromFile(path);
                RobustFile.WriteAllBytes(tempFile.Path, reducedBytes);
                var newMetadata = Metadata.FromFile(tempFile.Path);
                if (oldMetadata.IsEmpty)
                {
                    Assert.IsTrue(newMetadata.IsEmpty);
                }
                else
                {
                    Assert.IsFalse(newMetadata.IsEmpty);
                    Assert.AreEqual(oldMetadata.CopyrightNotice, newMetadata.CopyrightNotice, "copyright preserved for shirt.png");
                    Assert.AreEqual(oldMetadata.Creator, newMetadata.Creator, "creator preserved for shirt.png");
                    Assert.AreEqual(oldMetadata.License.ToString(), newMetadata.License.ToString(), "license preserved for shirt.png");
                }
            }
        }
コード例 #3
0
        public void GetBytesOfReducedImage_SmallJpgImageStaysSame()
        {
            // man.jpg:                    JPEG image data, JFIF standard 1.01, ..., precision 8, 118x154, frames 3

            var path          = SIL.IO.FileLocator.GetFileDistributedWithApplication(_pathToTestImages, "man.jpg");
            var originalBytes = File.ReadAllBytes(path);
            var reducedBytes  = BookCompressor.GetBytesOfReducedImage(path);

            Assert.AreEqual(originalBytes, reducedBytes, "man.jpg is already small enough (118x154)");
            using (var tempFile = TempFile.WithExtension(Path.GetExtension(path)))
            {
                var oldMetadata = Metadata.FromFile(path);
                RobustFile.WriteAllBytes(tempFile.Path, reducedBytes);
                var newMetadata = Metadata.FromFile(tempFile.Path);
                if (oldMetadata.IsEmpty)
                {
                    Assert.IsTrue(newMetadata.IsEmpty);
                }
                else
                {
                    Assert.IsFalse(newMetadata.IsEmpty);
                    Assert.AreEqual(oldMetadata.CopyrightNotice, newMetadata.CopyrightNotice, "copyright preserved for man.jpg");
                    Assert.AreEqual(oldMetadata.Creator, newMetadata.Creator, "creator preserved for man.jpg");
                    Assert.AreEqual(oldMetadata.License.ToString(), newMetadata.License.ToString(), "license preserved for man.jpg");
                }
            }
        }
コード例 #4
0
        public void GetBytesOfReducedImage_SmallPngImageMadeTransparent()
        {
            // bird.png:                   PNG image data, 274 x 300, 8-bit/color RGBA, non-interlaced

            var path = SIL.IO.FileLocator.GetFileDistributedWithApplication(_pathToTestImages, "bird.png");

            byte[] originalBytes = File.ReadAllBytes(path);
            byte[] reducedBytes  = BookCompressor.GetBytesOfReducedImage(path);
            Assert.That(reducedBytes, Is.Not.EqualTo(originalBytes));             // no easy way to check it was made transparent, but should be changed.
            // Size should not change much.
            Assert.That(reducedBytes.Length, Is.LessThan(originalBytes.Length * 11 / 10));
            Assert.That(reducedBytes.Length, Is.GreaterThan(originalBytes.Length * 9 / 10));
            using (var tempFile = TempFile.WithExtension(Path.GetExtension(path)))
            {
                var oldMetadata = Metadata.FromFile(path);
                RobustFile.WriteAllBytes(tempFile.Path, reducedBytes);
                var newMetadata = Metadata.FromFile(tempFile.Path);
                if (oldMetadata.IsEmpty)
                {
                    Assert.IsTrue(newMetadata.IsEmpty);
                }
                else
                {
                    Assert.IsFalse(newMetadata.IsEmpty);
                    Assert.AreEqual(oldMetadata.CopyrightNotice, newMetadata.CopyrightNotice, "copyright preserved for bird.png");
                    Assert.AreEqual(oldMetadata.Creator, newMetadata.Creator, "creator preserved for bird.png");
                    Assert.AreEqual(oldMetadata.License.ToString(), newMetadata.License.ToString(), "license preserved for bird.png");
                }
            }
        }
コード例 #5
0
        public void HandleModifiedFile_CheckedOutToMe_ContentChangedRemotely_RaisesCheckedOutByNoneAndErrorMessage()
        {
            // Setup //
            // Simulate a book that was checked out and modified by me, but then we get a remote change
            // notification.
            const string bookFolderName = "My conflicting change book";
            var          bookBuilder    = new BookFolderBuilder()
                                          .WithRootFolder(_collectionFolder.FolderPath)
                                          .WithTitle(bookFolderName)
                                          .WithHtm("We will be simulating a remote change to this.")
                                          .Build();

            string bookFolderPath = bookBuilder.BuiltBookFolderPath;
            string bookPath       = bookBuilder.BuiltBookHtmPath;

            _collection.PutBook(bookFolderPath);
            var pathToBookFileInRepo = _collection.GetPathToBookFileInRepo(bookFolderName);
            // Save the data we will eventually write back to the .bloom file to simulate the remote change.
            var remoteContent = RobustFile.ReadAllBytes(pathToBookFileInRepo);

            _collection.AttemptLock(bookFolderName);
            RobustFile.WriteAllText(bookPath, "Pretend this was the state when we checked it out.");
            _collection.PutBook(bookFolderPath);

            RobustFile.WriteAllText(bookPath, "This is a further change locally, not checked in anywhere");

            // But now it's been changed remotely to the other state. (Ignore the fact that it was a previous local state;
            // that was just a trick to get a valid alternative state.)
            RobustFile.WriteAllBytes(pathToBookFileInRepo, remoteContent);

            var prevMessages = _tcLog.Messages.Count;

            // System Under Test //
            _collection.HandleModifiedFile(new BookRepoChangeEventArgs()
            {
                BookFileName = $"{bookFolderName}.bloom"
            });

            // Verification
            var eventArgs = (BookStatusChangeEventArgs)_mockTcManager.Invocations[0].Arguments[0];

            Assert.That(eventArgs.CheckedOutByWhom, Is.EqualTo(CheckedOutBy.None));

            Assert.That(_tcLog.Messages[prevMessages].MessageType, Is.EqualTo(MessageAndMilestoneType.Error));
            Assert.That(_tcLog.Messages[prevMessages].L10NId, Is.EqualTo("TeamCollection.EditedFileChangedRemotely"));
        }
コード例 #6
0
        public void GetBytesOfReducedImage_LargePng24bImageReduced()
        {
            // lady24b.png:        PNG image data, 24bit depth, 3632w x 3872h

            var path          = FileLocator.GetFileDistributedWithApplication(_pathToTestImages, "lady24b.png");
            var originalBytes = File.ReadAllBytes(path);
            var reducedBytes  = BookCompressor.GetBytesOfReducedImage(path);

            // Is it reduced, even tho' we switched from 24bit depth to 32bit depth?
            Assert.Greater(originalBytes.Length, reducedBytes.Length, "lady24b.png is reduced from 3632x3872");
            using (var tempFile = TempFile.WithExtension(Path.GetExtension(path)))
            {
                RobustFile.WriteAllBytes(tempFile.Path, reducedBytes);
                using (var newImage = PalasoImage.FromFileRobustly(tempFile.Path))
                    Assert.AreEqual(PixelFormat.Format32bppArgb, newImage.Image.PixelFormat, "should have switched to 32bit depth");
            }
        }