// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Web;
using System.Web.Helpers.Test;
using System.Web.TestUtil;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace Microsoft.Web.Helpers.Test
{
public class FileUploadTest
{
private const string _fileUploadScript = "";
[Fact]
public void RenderThrowsWhenNumberOfFilesIsLessThanZero()
{
// Act and Assert
Assert.ThrowsArgumentGreaterThanOrEqualTo(
() => FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: -2, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: "", uploadText: "").ToString(),
"initialNumberOfFiles", "0");
}
[Fact]
public void ResultIncludesFormTagAndSubmitButtonWhenRequested()
{
// Arrange
string expectedResult = @"
";
// Act
var actualResult = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: true, addText: null, uploadText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, actualResult.ToString());
}
[Fact]
public void ResultDoesNotIncludeFormTagAndSubmitButtonWhenNotRequested()
{
// Arrange
string expectedResult = @"";
// Act
var actualResult = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: null, uploadText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, actualResult.ToString());
}
[Fact]
public void ResultIncludesCorrectNumberOfInputFields()
{
// Arrange
string expectedResult = @"";
// Act
var actualResult = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 3, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: null, uploadText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, actualResult.ToString());
}
[Fact]
public void ResultIncludesAnchorTagWithCorrectAddText()
{
// Arrange
string customAddText = "Add More";
string expectedResult = _fileUploadScript
+ @""
+ @"";
// Act
var result = FileUpload._GetHtml(GetContext(), name: null, allowMoreFilesToBeAdded: true, includeFormTag: false, addText: customAddText, initialNumberOfFiles: 1, uploadText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString());
}
[Fact]
public void ResultDoesNotIncludeAnchorTagNorAddTextWhenNotRequested()
{
// Arrange
string customAddText = "Add More";
string expectedResult = @"";
// Act
var result = FileUpload._GetHtml(GetContext(), name: null, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: customAddText, uploadText: null, initialNumberOfFiles: 1);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString());
}
[Fact]
public void ResultIncludesSubmitInputTagWithCustomUploadText()
{
// Arrange
string customUploadText = "Now!";
string expectedResult = @"";
// Act
var result = FileUpload._GetHtml(GetContext(), name: null, includeFormTag: true, uploadText: customUploadText, allowMoreFilesToBeAdded: false, initialNumberOfFiles: 1, addText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString());
}
[Fact]
public void FileUploadGeneratesUniqueIdsForMultipleCallsForCommonRequest()
{
// Arrange
var context = GetContext();
string expectedResult1 = @"";
string expectedResult2 = @"";
// Act
var result1 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 3, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: null, uploadText: null);
var result2 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 2, allowMoreFilesToBeAdded: false, includeFormTag: true, addText: null, uploadText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult1, result1.ToString());
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult2, result2.ToString());
}
[Fact]
public void FileUploadGeneratesScriptOncePerRequest()
{
// Arrange
var context = GetContext();
string expectedResult1 = _fileUploadScript
+ @""
+ @"";
string expectedResult2 = @"";
string expectedResult3 = @"";
// Act
var result1 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, addText: null, uploadText: null);
var result2 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: true, addText: null, uploadText: null);
var result3 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: true, addText: null, uploadText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult1, result1.ToString());
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult2, result2.ToString());
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult3, result3.ToString());
}
[Fact]
public void FileUploadUsesNamePropertyInJavascript()
{
// Arrange
var context = GetContext();
string name = "foobar";
string expectedResult = _fileUploadScript
+ @"";
// Act
var result = FileUpload._GetHtml(context, name: name, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: true, addText: null, uploadText: null);
// Assert
UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString());
}
[Fact]
public void _GetHtmlWithDefaultArgumentsProducesValidXhtml()
{
// Act
var result = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 1, includeFormTag: false, allowMoreFilesToBeAdded: false, addText: null, uploadText: null);
// Assert
XhtmlAssert.Validate1_1(result, "div");
}
[Fact]
public void _GetHtmlWithoutFormTagProducesValidXhtml()
{
// Act
var result = FileUpload._GetHtml(GetContext(), name: null, includeFormTag: false, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, addText: null, uploadText: null);
XhtmlAssert.Validate1_1(result, "div");
}
private HttpContextBase GetContext()
{
var context = new Mock();
context.Setup(c => c.Items).Returns(new Hashtable());
return context.Object;
}
}
}