Exemplo n.º 1
0
        public void ReturnAmpImageElementWithLayoutAttributeSetToResponsive_IfTheOriginalImageElementHasNoWidthAndHeightButShouldDownloadImagesEqualsTrue()
        {
            // Arrange
            var runContext = new RunContext(new RunConfiguration {
                ShouldDownloadImages = true
            });

            const string ExpectedResult = "responsive";
            var          imageElement   = ElementFactory.CreateImage();

            imageElement.Source = "http://www.mywebsite.com/img1.jpg";

            ElementFactory.Document.Body.Append(imageElement);

            var imageSanitizer = new ImageSanitizerTestDouble();

            imageSanitizer.DownloadImageResult = (imageUrl) => new Bitmap(100, 200);
            imageSanitizer.Configure(runContext);

            // Act
            var actualResult = imageSanitizer.Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.GetAttribute("layout"));
        }
Exemplo n.º 2
0
        public void PutImageWidthAndHeightInTheCache_WhenTheyAreNotSpecifiedAndAreMissingInTheCache()
        {
            // Arrange
            var runContext = new RunContext(new RunConfiguration()
            {
                RelativeUrlsHost = "http://mywebsite.com"
            });

            var imageElement = ElementFactory.CreateImage();

            imageElement.Source = "/images/logo1.png";

            var imageSanitizer = new ImageSanitizerTestDouble();

            imageSanitizer.Configure(runContext);
            imageSanitizer.DownloadImageResult = (imageUrl) => new Bitmap(100, 200);

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = imageSanitizer.Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(1, runContext.ImagesCache.Count);
            Assert.AreEqual(100, runContext.ImagesCache["/images/logo1.png"].Width);
            Assert.AreEqual(200, runContext.ImagesCache["/images/logo1.png"].Height);
        }
Exemplo n.º 3
0
        // TODO: Remove "Ignore" attribute when we set the absolute url of images always.
        public void ResolveImageUrl_WhenSourceAttributeIsRelative()
        {
            // Arrange
            const string ExpectedResult = "http://mywebsite.com/images/logo.png";
            var          runContext     = new RunContext(new RunConfiguration()
            {
                RelativeUrlsHost = "http://mywebsite.com"
            });

            var imageElement = ElementFactory.CreateImage();

            imageElement.Source = "/images/logo.png";

            var imageSanitizer = new ImageSanitizerTestDouble();

            imageSanitizer.Configure(runContext);
            imageSanitizer.DownloadImageResult = (imageUrl) => null;

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = imageSanitizer.Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("src"));
        }
Exemplo n.º 4
0
        public void NotCallDownloadImageMethod_WhenWidthAndHeightOfThatImageArePresentInTheCache()
        {
            // Arrange
            var runContext = new RunContext(new RunConfiguration()
            {
                RelativeUrlsHost = "http://mywebsite.com"
            });

            runContext.ImagesCache["/images/logo1.png"] = new Models.ImageSize()
            {
                Width = 100, Height = 200
            };

            var imageElement = ElementFactory.CreateImage();

            imageElement.Source = "/images/logo1.png";

            var imageSanitizer = new ImageSanitizerSpy();

            imageSanitizer.Configure(runContext);
            imageSanitizer.DownloadImageResult = (imageUrl) => new Bitmap(100, 200);

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = imageSanitizer.Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.IsFalse(imageSanitizer.DownloadImageIsCalled);
        }
Exemplo n.º 5
0
        public void SetImageHeight_WhenItIsNotSpecifiedAndTheImageUrlIsValid()
        {
            // Arrange
            const int ExpectedResult = 200;
            var       runContext     = new RunContext(new RunConfiguration()
            {
                RelativeUrlsHost = "http://mywebsite.com"
            });

            var imageElement = ElementFactory.CreateImage();

            imageElement.Source = "/images/logo.png";

            var imageSanitizer = new ImageSanitizerTestDouble();

            imageSanitizer.Configure(runContext);
            imageSanitizer.DownloadImageResult = (imageUrl) => new Bitmap(100, 200);

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = imageSanitizer.Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, int.Parse(ampElement.GetAttribute("height")));
        }
Exemplo n.º 6
0
        public void ReturnFalse_WhenHtmlElementIsNotIHtmlAnchor()
        {
            // Act
            var actualResult = new HrefJavaScriptSanitizer().CanSanitize(ElementFactory.CreateImage());

            // Assert
            Assert.IsFalse(actualResult);
        }
Exemplo n.º 7
0
        public void ReturnFalse_WhenElementIsNotIHtmlLinkElement()
        {
            // Act
            var actualResult = new TargetAttributeSanitizer().CanSanitize(ElementFactory.CreateImage());

            // Assert
            Assert.IsFalse(actualResult);
        }
Exemplo n.º 8
0
        public void ReturnFalse_WhenHtmlElementIsNotScirptElement()
        {
            // Arrange
            var imageElement = ElementFactory.CreateImage();

            // Act
            var actualResult = new ScriptElementSanitizer().CanSanitize(imageElement);

            // Assert
            Assert.IsFalse(actualResult);
        }
Exemplo n.º 9
0
        public void ReturnTrue_WhenElementIsImageElement()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateImage();

            // Act
            var actualResult = new ImageSanitizer().CanSanitize(htmlElement);

            // Assert
            Assert.IsTrue(actualResult);
        }
Exemplo n.º 10
0
        public void ReturnAmpImageElementWithLayoutAttributeSetToFill_IfTheOriginalImageElementHasNoWidthAndHeightAttributesAndShouldDownloadImagesIsNotSpecified()
        {
            // Arrange
            const string ExpectedResult = "fill";
            var          imageElement   = ElementFactory.CreateImage();

            imageElement.Source = "http://www.mywebsite.com/img1.jpg";

            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var actualResult = new ImageSanitizer().Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.GetAttribute("layout"));
        }
Exemplo n.º 11
0
        public void ReturnAmpImageElementWithLayoutEqualToNoDisplay_WhenTheImageHasAttributeStyleVisibilityHidden()
        {
            // Arrange
            const string ExpectedResult = "nodisplay";
            var          imageElement   = ElementFactory.CreateImage();

            imageElement.SetAttribute("style", "visibility:hidden");

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = new ImageSanitizer().Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("layout"));
        }
Exemplo n.º 12
0
        public void ReturnAmpImageElementWithLayoutAttributeSetToResponsive_IfTheOriginalImageElementHasBothWidthAndHeightAttributes()
        {
            // Arrange
            const string ExpectedResult = "responsive";
            var          imageElement   = ElementFactory.CreateImage();

            imageElement.Source        = "http://www.mywebsite.com/img1.jpg";
            imageElement.DisplayWidth  = 100;
            imageElement.DisplayHeight = 100;

            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var actualResult = new ImageSanitizer().Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, actualResult.GetAttribute("layout"));
        }
Exemplo n.º 13
0
        public void ReturnAmpImageElementWithLayoutEqualToResponsive_WhenWidthAndHeightAreSpecified()
        {
            // Arrange
            const string ExpectedResult = "responsive";
            const int    ImageSize      = 100;
            var          imageElement   = ElementFactory.CreateImage();

            imageElement.DisplayWidth  = ImageSize;
            imageElement.DisplayHeight = ImageSize;

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = new ImageSanitizer().Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.GetAttribute("layout"));
        }
Exemplo n.º 14
0
        public void ReturnAmpImageElement()
        {
            // Arrange
            const int    ImageSize      = 100;
            const string ExpectedResult = "AMP-IMG";
            var          imageElement   = ElementFactory.CreateImage();

            imageElement.DisplayWidth  = ImageSize;
            imageElement.DisplayHeight = ImageSize;

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = new ImageSanitizer().Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.TagName);
        }
Exemplo n.º 15
0
        public void ReturnAmpImageElementWithPredefinedHeight_WhenWidthAndHeightAreSpecified()
        {
            // Arrange
            const int ImageSize    = 100;
            var       imageElement = ElementFactory.CreateImage();

            imageElement.DisplayWidth  = ImageSize;
            imageElement.DisplayHeight = ImageSize;

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = new ImageSanitizer().Sanitize(ElementFactory.Document, imageElement);

            // Assert
            int actualHeight = int.Parse(ampElement.GetAttribute("height"));

            Assert.AreEqual(ImageSize, actualHeight);
        }
Exemplo n.º 16
0
        public void ReturnAmpAnimElement_WhenSourceExtensionIsGIF()
        {
            // Arrange
            const string ExpectedResult = "AMP-ANIM";
            const int    ImageSize      = 100;
            var          imageElement   = ElementFactory.CreateImage();

            imageElement.DisplayWidth  = ImageSize;
            imageElement.DisplayHeight = ImageSize;

            imageElement.Source = "http://mysite.com/my-animation.gif";

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = new ImageSanitizer().Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.AreEqual(ExpectedResult, ampElement.TagName);
        }
Exemplo n.º 17
0
        public void SetImageSizeMethodIsCalled_WhenHeightAndWeightAreNotSpecifiedAndShouldDownloadImagesEqualsTrue()
        {
            // Arrange
            var runContext = new RunContext(new RunConfiguration {
                ShouldDownloadImages = true
            });

            var imageSanitizerSpy = new ImageSanitizerSpy();

            imageSanitizerSpy.Configure(runContext);
            var imageElement = ElementFactory.CreateImage();

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = imageSanitizerSpy.Sanitize(ElementFactory.Document, imageElement);

            // Assert
            Assert.IsTrue(imageSanitizerSpy.SetImageSizeCalled);
        }
Exemplo n.º 18
0
        public void CopyAllAttributesFromTheOriginalImageElementToTheAmpElement_Always()
        {
            // Arrange
            var htmlElement = ElementFactory.CreateImage();

            htmlElement.Source        = "https://www.example.com/img1.png";
            htmlElement.Id            = "imgId";
            htmlElement.ClassName     = "someClassName";
            htmlElement.DisplayWidth  = 100;
            htmlElement.DisplayHeight = 200;
            ElementFactory.Document.Body.Append(htmlElement);

            // Act
            var actualResult = new ImageSanitizer().Sanitize(ElementFactory.Document, htmlElement);

            // Assert
            Assert.AreEqual("https://www.example.com/img1.png", actualResult.GetAttribute("src"));
            Assert.AreEqual("imgId", actualResult.Id);
            Assert.AreEqual("someClassName", actualResult.ClassName);
            Assert.AreEqual(100, int.Parse(actualResult.GetAttribute("width")));
            Assert.AreEqual(200, int.Parse(actualResult.GetAttribute("height")));
        }
Exemplo n.º 19
0
        public void ThrowInvalidOperationException_WhenTheImageHasNoWidthAndHeightAndTheImageUrlIsInvalid()
        {
            // Arrange
            var runContext = new RunContext(new RunConfiguration {
                ShouldDownloadImages = true
            });

            var imageElement = ElementFactory.CreateImage();

            imageElement.Source = "/images/logo.png";

            var imageSanitizer = new ImageSanitizerTestDouble();

            imageSanitizer.Configure(runContext);
            imageSanitizer.DownloadImageResult = (imageUrl) => null;

            // Adding image element to the document in order to simulate real herarchy
            ElementFactory.Document.Body.Append(imageElement);

            // Act
            var ampElement = imageSanitizer.Sanitize(ElementFactory.Document, imageElement);
        }
Exemplo n.º 20
0
 public void ThrowArgumentException_WhenArgumentVideoParamsIsNull()
 {
     // Assert
     Ensure.ArgumentExceptionIsThrown(() => new YouTubeVideoSanitizerAccessor().SetVideoParams(ElementFactory.CreateImage(), null), "videoParams");
 }
Exemplo n.º 21
0
 public void ThowArgumentNullException_WhenDocumentArgumentIsNull()
 {
     // Assert
     Ensure.ArgumentExceptionIsThrown(() => new ImageSanitizer().Sanitize(null, ElementFactory.CreateImage()), "document");
 }