1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 2 3 using System.Collections; 4 using System.Web; 5 using System.Web.Helpers.Test; 6 using System.Web.TestUtil; 7 using Moq; 8 using Xunit; 9 using Assert = Microsoft.TestCommon.AssertEx; 10 11 namespace Microsoft.Web.Helpers.Test 12 { 13 public class FileUploadTest 14 { 15 private const string _fileUploadScript = "<script type=\"text/javascript\"> if (!window[\"FileUploadHelper\"]) window[\"FileUploadHelper\"] = {}; FileUploadHelper.addInputElement = function(index, name) { var inputElem = document.createElement(\"input\"); inputElem.type = \"file\"; inputElem.name = name; var divElem = document.createElement(\"div\"); divElem.appendChild(inputElem.cloneNode(false)); var inputs = document.getElementById(\"file-upload-\" + index); inputs.appendChild(divElem); } </script>"; 16 17 [Fact] RenderThrowsWhenNumberOfFilesIsLessThanZero()18 public void RenderThrowsWhenNumberOfFilesIsLessThanZero() 19 { 20 // Act and Assert 21 Assert.ThrowsArgumentGreaterThanOrEqualTo( 22 () => FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: -2, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: "", uploadText: "").ToString(), 23 "initialNumberOfFiles", "0"); 24 } 25 26 [Fact] ResultIncludesFormTagAndSubmitButtonWhenRequested()27 public void ResultIncludesFormTagAndSubmitButtonWhenRequested() 28 { 29 // Arrange 30 string expectedResult = @"<form action="""" enctype=""multipart/form-data"" method=""post""><div class=""file-upload"" id=""file-upload-0"">" 31 + @"<div><input name=""fileUpload"" type=""file"" /></div></div>" 32 + @"<div class=""file-upload-buttons""><input type=""submit"" value=""Upload"" /></div></form>"; 33 34 // Act 35 var actualResult = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: true, addText: null, uploadText: null); 36 37 // Assert 38 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, actualResult.ToString()); 39 } 40 41 [Fact] ResultDoesNotIncludeFormTagAndSubmitButtonWhenNotRequested()42 public void ResultDoesNotIncludeFormTagAndSubmitButtonWhenNotRequested() 43 { 44 // Arrange 45 string expectedResult = @"<div class=""file-upload"" id=""file-upload-0""><div><input name=""fileUpload"" type=""file"" /></div></div>"; 46 47 // Act 48 var actualResult = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: null, uploadText: null); 49 50 // Assert 51 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, actualResult.ToString()); 52 } 53 54 [Fact] ResultIncludesCorrectNumberOfInputFields()55 public void ResultIncludesCorrectNumberOfInputFields() 56 { 57 // Arrange 58 string expectedResult = @"<div class=""file-upload"" id=""file-upload-0""><div><input name=""fileUpload"" type=""file"" /></div><div><input name=""fileUpload"" type=""file"" /></div>" 59 + @"<div><input name=""fileUpload"" type=""file"" /></div></div>"; 60 61 // Act 62 var actualResult = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 3, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: null, uploadText: null); 63 64 // Assert 65 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, actualResult.ToString()); 66 } 67 68 [Fact] ResultIncludesAnchorTagWithCorrectAddText()69 public void ResultIncludesAnchorTagWithCorrectAddText() 70 { 71 // Arrange 72 string customAddText = "Add More"; 73 string expectedResult = _fileUploadScript 74 + @"<div class=""file-upload"" id=""file-upload-0""><div><input name=""fileUpload"" type=""file"" /></div></div>" 75 + @"<div class=""file-upload-buttons""><a href=""#"" onclick=""FileUploadHelper.addInputElement(0, "fileUpload"); return false;"">" + customAddText + "</a></div>"; 76 77 // Act 78 var result = FileUpload._GetHtml(GetContext(), name: null, allowMoreFilesToBeAdded: true, includeFormTag: false, addText: customAddText, initialNumberOfFiles: 1, uploadText: null); 79 80 // Assert 81 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString()); 82 } 83 84 [Fact] ResultDoesNotIncludeAnchorTagNorAddTextWhenNotRequested()85 public void ResultDoesNotIncludeAnchorTagNorAddTextWhenNotRequested() 86 { 87 // Arrange 88 string customAddText = "Add More"; 89 string expectedResult = @"<div class=""file-upload"" id=""file-upload-0""><div><input name=""fileUpload"" type=""file"" /></div></div>"; 90 91 // Act 92 var result = FileUpload._GetHtml(GetContext(), name: null, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: customAddText, uploadText: null, initialNumberOfFiles: 1); 93 94 // Assert 95 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString()); 96 } 97 98 [Fact] ResultIncludesSubmitInputTagWithCustomUploadText()99 public void ResultIncludesSubmitInputTagWithCustomUploadText() 100 { 101 // Arrange 102 string customUploadText = "Now!"; 103 string expectedResult = @"<form action="""" enctype=""multipart/form-data"" method=""post""><div class=""file-upload"" id=""file-upload-0"">" 104 + @"<div><input name=""fileUpload"" type=""file"" /></div></div>" 105 + @"<div class=""file-upload-buttons""><input type=""submit"" value=""" + customUploadText + @""" /></div></form>"; 106 107 // Act 108 var result = FileUpload._GetHtml(GetContext(), name: null, includeFormTag: true, uploadText: customUploadText, allowMoreFilesToBeAdded: false, initialNumberOfFiles: 1, addText: null); 109 110 // Assert 111 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString()); 112 } 113 114 [Fact] FileUploadGeneratesUniqueIdsForMultipleCallsForCommonRequest()115 public void FileUploadGeneratesUniqueIdsForMultipleCallsForCommonRequest() 116 { 117 // Arrange 118 var context = GetContext(); 119 string expectedResult1 = @"<div class=""file-upload"" id=""file-upload-0""><div><input name=""fileUpload"" type=""file"" /></div><div><input name=""fileUpload"" type=""file"" /></div>" 120 + @"<div><input name=""fileUpload"" type=""file"" /></div></div>"; 121 string expectedResult2 = @"<form action="""" enctype=""multipart/form-data"" method=""post""><div class=""file-upload"" id=""file-upload-1""><div><input name=""fileUpload"" type=""file"" /></div>" 122 + @"<div><input name=""fileUpload"" type=""file"" /></div></div><div class=""file-upload-buttons""><input type=""submit"" value=""Upload"" /></div></form>"; 123 124 // Act 125 var result1 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 3, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: null, uploadText: null); 126 var result2 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 2, allowMoreFilesToBeAdded: false, includeFormTag: true, addText: null, uploadText: null); 127 128 // Assert 129 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult1, result1.ToString()); 130 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult2, result2.ToString()); 131 } 132 133 [Fact] FileUploadGeneratesScriptOncePerRequest()134 public void FileUploadGeneratesScriptOncePerRequest() 135 { 136 // Arrange 137 var context = GetContext(); 138 string expectedResult1 = _fileUploadScript 139 + @"<div class=""file-upload"" id=""file-upload-0""><div><input name=""fileUpload"" type=""file"" /></div></div>" 140 + @"<div class=""file-upload-buttons""><a href=""#"" onclick=""FileUploadHelper.addInputElement(0, "fileUpload"); return false;"">Add more files</a></div>"; 141 string expectedResult2 = @"<form action="""" enctype=""multipart/form-data"" method=""post""><div class=""file-upload"" id=""file-upload-1""><div><input name=""fileUpload"" type=""file"" /></div></div>" 142 + @"<div class=""file-upload-buttons""><a href=""#"" onclick=""FileUploadHelper.addInputElement(1, "fileUpload"); return false;"">Add more files</a><input type=""submit"" value=""Upload"" /></div></form>"; 143 string expectedResult3 = @"<form action="""" enctype=""multipart/form-data"" method=""post""><div class=""file-upload"" id=""file-upload-2"">" 144 + @"<div><input name=""fileUpload"" type=""file"" /></div></div>" 145 + @"<div class=""file-upload-buttons""><input type=""submit"" value=""Upload"" /></div></form>"; 146 147 // Act 148 var result1 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, addText: null, uploadText: null); 149 var result2 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: true, addText: null, uploadText: null); 150 var result3 = FileUpload._GetHtml(context, name: null, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: true, addText: null, uploadText: null); 151 152 // Assert 153 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult1, result1.ToString()); 154 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult2, result2.ToString()); 155 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult3, result3.ToString()); 156 } 157 158 [Fact] FileUploadUsesNamePropertyInJavascript()159 public void FileUploadUsesNamePropertyInJavascript() 160 { 161 // Arrange 162 var context = GetContext(); 163 string name = "foobar"; 164 string expectedResult = _fileUploadScript 165 + @"<form action="""" enctype=""multipart/form-data"" method=""post""><div class=""file-upload"" id=""file-upload-0""><div><input name=""foobar"" type=""file"" /></div></div>" 166 + @"<div class=""file-upload-buttons""><a href=""#"" onclick=""FileUploadHelper.addInputElement(0, "foobar"); return false;"">Add more files</a><input type=""submit"" value=""Upload"" /></div></form>"; 167 168 // Act 169 var result = FileUpload._GetHtml(context, name: name, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: true, addText: null, uploadText: null); 170 171 // Assert 172 UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedResult, result.ToString()); 173 } 174 175 [Fact] _GetHtmlWithDefaultArgumentsProducesValidXhtml()176 public void _GetHtmlWithDefaultArgumentsProducesValidXhtml() 177 { 178 // Act 179 var result = FileUpload._GetHtml(GetContext(), name: null, initialNumberOfFiles: 1, includeFormTag: false, allowMoreFilesToBeAdded: false, addText: null, uploadText: null); 180 181 // Assert 182 XhtmlAssert.Validate1_1(result, "div"); 183 } 184 185 [Fact] _GetHtmlWithoutFormTagProducesValidXhtml()186 public void _GetHtmlWithoutFormTagProducesValidXhtml() 187 { 188 // Act 189 var result = FileUpload._GetHtml(GetContext(), name: null, includeFormTag: false, initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, addText: null, uploadText: null); 190 191 XhtmlAssert.Validate1_1(result, "div"); 192 } 193 GetContext()194 private HttpContextBase GetContext() 195 { 196 var context = new Mock<HttpContextBase>(); 197 context.Setup(c => c.Items).Returns(new Hashtable()); 198 return context.Object; 199 } 200 } 201 } 202