using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.IO;
using Rilling.MhtmlLib.Media;
using Rilling.MhtmlLib.Media.Artifacts;
using Rilling.MhtmlLib.Media.Resolvers;
namespace Rilling.Web.Mhtml.TestHarness
{
[TestFixture()]
public class ImageLinkResolverTest
{
string[] testVals1 = new string[]
{
"<img src=\"myimage.jpg\"/>",
"<img src=\"myimage.jpg\" xxx=\"nothing\"/>",
"<img xxx=\"nothing\" src=\"myimage.jpg\"/>",
"<img src='myimage.jpg'/>",
"<img src=myimage.jpg/>",
"<input src=\"myimage.jpg\"/>",
"<input src=\"myimage.jpg\" xxx=\"nothing\"/>",
"<input xxx=\"nothing\" src=\"myimage.jpg\"/>",
"<input src='myimage.jpg'/>",
"<input src=myimage.jpg/>",
"<input src=\"myimage.jpg\"/>",
"<input src=\"myimage.jpg\" xxx=\"nothing\"/>",
"<input xxx=\"nothing\" src=\"myimage.jpg\"/>",
"<input src='myimage.jpg'/>",
"<input src=myimage.jpg/>",
"<input src=\"myimage.jpg\"/>",
"<input src=\"myimage.jpg\" xxx=\"nothing\"/>",
"<input xxx=\"nothing\" src=\"myimage.jpg\"/>",
"<input src='myimage.jpg'/>",
"<input src=myimage.jpg/>"
};
ImageLinkResolver testObj = null;
[SetUp]
public void InitTest()
{
testObj = new ImageLinkResolver();
}
[Test]
public void CheckDlt_EvaluateCssFiles()
{
Assert.IsTrue(testObj.EvaluateCssFiles);
}
[Test]
public void CheckDlt_EvaluateHtmlFiles()
{
Assert.IsTrue(testObj.EvaluateHtmlFiles);
}
[Test]
public void CheckDlt_IncludeBackgroundImages()
{
Assert.IsTrue(testObj.IncludeBackgroundImages);
}
[Test]
public void CheckDlt_IncludeForegroundImages()
{
Assert.IsTrue(testObj.IncludeForegroundImages);
}
[Test]
public void Set_EvaluateCssFiles()
{
bool testVal = !testObj.EvaluateCssFiles;
testObj.EvaluateCssFiles = testVal;
Assert.AreEqual(testVal, testObj.EvaluateCssFiles);
}
[Test]
public void Set_EvaluateHtmlFiles()
{
bool testVal = !testObj.EvaluateHtmlFiles;
testObj.EvaluateHtmlFiles = testVal;
Assert.AreEqual(testVal, testObj.EvaluateHtmlFiles);
}
[Test]
public void Set_IncludeBackgroundImages()
{
bool testVal = !testObj.IncludeBackgroundImages;
testObj.IncludeBackgroundImages = testVal;
Assert.AreEqual(testVal, testObj.IncludeBackgroundImages);
}
[Test]
public void Set_IncludeForegroundImages()
{
bool testVal = !testObj.IncludeForegroundImages;
testObj.IncludeForegroundImages = testVal;
Assert.AreEqual(testVal, testObj.IncludeForegroundImages);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Call_LocateReferences_NullParam_ContentData()
{
testObj.LocateReferences(null, new Uri("http://www.rilling.net/"));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Call_LocateReferences_NullParam_BaseUrl()
{
testObj.LocateReferences(new TextArtifact(new MemoryStream()), null);
}
private void CallLocateReferencesHelper(string baseUri,
string[] testVals,
string expected)
{
foreach (string testVal in testVals)
{
//
// Prepare the artifact.
//
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(testVal);
writer.Flush();
TextArtifact artifact = new TextArtifact(stream);
artifact.ContentType = ContentTypes.Html;
//
// Try to locate and validate the found link.
//
LinkIdentification[] results =
testObj.LocateReferences(artifact, new Uri(baseUri));
//
// Make sure it is as expected.
//
Assert.AreEqual(1, results.Length, "Unable to locate any links (" + testVal + ")");
Assert.AreEqual(expected, results[0].Url.ToString(), "Unable to locate any links (" + testVal + ")");
}
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Call_InitializeContext_NullParam()
{
testObj.InitializeContext(null);
}
[Test]
public void Call_InitializeContext()
{
XmlDocument testVal = new XmlDocument();
testVal.LoadXml("<context evalCss=\"False\" evalHtml=\"False\" includeBackImg=\"False\" includeForeImg=\"False\"/>");
testObj.InitializeContext(testVal.FirstChild);
Assert.AreEqual(false, testObj.EvaluateCssFiles);
Assert.AreEqual(false, testObj.EvaluateHtmlFiles);
Assert.AreEqual(false, testObj.IncludeBackgroundImages);
Assert.AreEqual(false, testObj.IncludeForegroundImages);
}
[Test()]
public void Call_LocateReferences_FolderBaseUri()
{
CallLocateReferencesHelper("http://www.rilling.net/nowhere/",
testVals1,
"http://www.rilling.net/nowhere/myimage.jpg");
}
[Test()]
public void Call_LocateReferences_FileBaseUri()
{
CallLocateReferencesHelper("http://www.rilling.net/nowhere",
testVals1,
"http://www.rilling.net/myimage.jpg");
}
}
}