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")); }
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); }
// 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")); }
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); }
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"))); }
public void ReturnFalse_WhenHtmlElementIsNotIHtmlAnchor() { // Act var actualResult = new HrefJavaScriptSanitizer().CanSanitize(ElementFactory.CreateImage()); // Assert Assert.IsFalse(actualResult); }
public void ReturnFalse_WhenElementIsNotIHtmlLinkElement() { // Act var actualResult = new TargetAttributeSanitizer().CanSanitize(ElementFactory.CreateImage()); // Assert Assert.IsFalse(actualResult); }
public void ReturnFalse_WhenHtmlElementIsNotScirptElement() { // Arrange var imageElement = ElementFactory.CreateImage(); // Act var actualResult = new ScriptElementSanitizer().CanSanitize(imageElement); // Assert Assert.IsFalse(actualResult); }
public void ReturnTrue_WhenElementIsImageElement() { // Arrange var htmlElement = ElementFactory.CreateImage(); // Act var actualResult = new ImageSanitizer().CanSanitize(htmlElement); // Assert Assert.IsTrue(actualResult); }
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")); }
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")); }
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")); }
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")); }
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); }
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); }
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); }
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); }
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"))); }
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); }
public void ThrowArgumentException_WhenArgumentVideoParamsIsNull() { // Assert Ensure.ArgumentExceptionIsThrown(() => new YouTubeVideoSanitizerAccessor().SetVideoParams(ElementFactory.CreateImage(), null), "videoParams"); }
public void ThowArgumentNullException_WhenDocumentArgumentIsNull() { // Assert Ensure.ArgumentExceptionIsThrown(() => new ImageSanitizer().Sanitize(null, ElementFactory.CreateImage()), "document"); }