// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Linq; using System.Web.Mvc.Test; using Microsoft.Web.UnitTestUtil; using Xunit; using Assert = Microsoft.TestCommon.AssertEx; namespace System.Web.Mvc.Html.Test { public class SelectExtensionsTest { private static readonly ViewDataDictionary _listBoxViewData = new ViewDataDictionary { { "foo", new[] { "Bravo" } } }; private static readonly ViewDataDictionary _dropDownListViewData = new ViewDataDictionary { { "foo", "Bravo" } }; private static readonly ViewDataDictionary _nonIEnumerableViewData = new ViewDataDictionary { { "foo", 1 } }; private static ViewDataDictionary GetViewDataWithSelectList() { ViewDataDictionary viewData = new ViewDataDictionary(); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); viewData["foo"] = selectList; viewData["foo.bar"] = selectList; return viewData; } // DropDownList [Fact] public void DropDownListUsesExplicitValueIfNotProvidedInViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListUsesExplicitValueIfNotProvidedInViewData_Unobtrusive() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListUsesViewDataDefaultValue() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie"); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListUsesViewDataDefaultValueNoOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie"); // Act MvcHtmlString html = helper.DropDownList("foo", selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithAttributesDictionary() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithEmptyNameThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act & Assert Assert.ThrowsArgumentNullOrEmpty( delegate { helper.DropDownList(String.Empty, (SelectList)null /* selectList */, (string)null /* optionLabel */); }, "name"); } [Fact] public void DropDownListWithErrors() { // Arrange SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); ViewDataDictionary viewData = GetViewDataWithErrors(); HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithErrorsAndCustomClass() { // Arrange SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); ViewDataDictionary viewData = GetViewDataWithErrors(); HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, new { @class = "foo-class" }); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithNullNameThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act & Assert Assert.ThrowsArgumentNullOrEmpty( delegate { helper.DropDownList(null /* name */, (SelectList)null /* selectList */, (string)null /* optionLabel */); }, "name"); } [Fact] public void DropDownListWithNullSelectListUsesViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.DropDownList("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithObjectDictionary() { // Arrange SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); ViewDataDictionary viewData = new ViewDataDictionary(); HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithObjectDictionaryWithUnderscores() { // Arrange SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); ViewDataDictionary viewData = new ViewDataDictionary(); HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectUnderscoresDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithObjectDictionaryAndSelectList() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithObjectDictionaryAndSelectListNoOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithObjectDictionaryWithUnderscoresAndSelectListNoOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithObjectDictionaryAndEmptyOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, String.Empty /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithObjectDictionaryAndTitle() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownList("foo", selectList, "[Select Something]", HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListUsesViewDataSelectList() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithSelectList()); // Act MvcHtmlString html = helper.DropDownList("foo", (string)null /* optionLabel */); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListUsesModelState() { // Arrange SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); ViewDataDictionary viewData = GetViewDataWithErrors(); viewData["foo"] = selectList; HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); // Act MvcHtmlString html = helper.DropDownList("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListUsesViewDataSelectListNoOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithSelectList()); // Act MvcHtmlString html = helper.DropDownList("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithDotReplacementForId() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithSelectList()); // Act MvcHtmlString html = helper.DropDownList("foo.bar"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithIEnumerableSelectListItem() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleIEnumerableObjects() } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.DropDownList("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithIEnumerableSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.DropDownList("foo", MultiSelectListTest.GetSampleIEnumerableObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithListOfSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.DropDownList("foo", MultiSelectListTest.GetSampleListObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithListOfSelectListItem() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleListObjects() } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.DropDownList("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithNullViewDataValueThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act Assert.Throws( delegate { helper.DropDownList("foo", (string)null /* optionLabel */); }, "There is no ViewData item of type 'IEnumerable' that has the key 'foo'."); } [Fact] public void DropDownListWithWrongViewDataTypeValueThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary { { "foo", 123 } }); // Act Assert.Throws( delegate { helper.DropDownList("foo", (string)null /* optionLabel */); }, "The ViewData item that has the key 'foo' is of type 'System.Int32' but must be of type 'IEnumerable'."); } [Fact] public void DropDownListWithPrefix() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.DropDownList("foo", selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithPrefixAndEmptyName() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.DropDownList("", selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListWithPrefixAndNullSelectListUsesViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.DropDownList("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } // DropDownListFor [Fact] public void DropDownListForWithNullExpressionThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); // Act & Assert Assert.ThrowsArgumentNull( () => helper.DropDownListFor(null /* expression */, selectList), "expression" ); } [Fact] public void DropDownListForUsesExplicitValueIfNotProvidedInViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, (string)null /* optionLabel */); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForUsesExplicitValueIfNotProvidedInViewData_Unobtrusive() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, (string)null /* optionLabel */); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithEnumerableModel_Unobtrusive() { // Arrange HtmlHelper> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary>()); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); // Act MvcHtmlString html = helper.DropDownListFor(m => m.ElementAt(0).foo, selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForUsesViewDataDefaultValue() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie"); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, (string)null /* optionLabel */); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForUsesViewDataDefaultValueNoOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie"); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithAttributesDictionary() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithErrors() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithErrors()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithErrorsAndCustomClass() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithErrors()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, new { @class = "foo-class" }); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithObjectDictionary() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithObjectDictionaryWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectUnderscoresDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithObjectDictionaryAndSelectListNoOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithObjectDictionaryWithUnderscoresAndSelectListNoOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithObjectDictionaryAndEmptyOptionLabel() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, String.Empty /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithObjectDictionaryAndTitle() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, "[Select Something]", HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithIEnumerableSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, MultiSelectListTest.GetSampleIEnumerableObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithListOfSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, MultiSelectListTest.GetSampleListObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithPrefix() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithPrefixAndEmptyName() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.DropDownListFor(m => m, selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void DropDownListForWithPrefixAndIEnumerableSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } }; vdd.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.DropDownListFor(m => m.foo, MultiSelectListTest.GetSampleIEnumerableObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } // ListBox [Fact] public void ListBoxUsesExplicitValueIfNotProvidedInViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", new[] { "A", "C" }); // Act MvcHtmlString html = helper.ListBox("foo", selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxUsesExplicitValueIfNotProvidedInViewData_Unobtrusive() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", new[] { "A", "C" }); // Act MvcHtmlString html = helper.ListBox("foo", selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxUsesViewDataDefaultValue() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(_listBoxViewData); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBox("foo", selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithErrors() { // Arrange ViewDataDictionary viewData = GetViewDataWithErrors(); HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); MultiSelectList list = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBox("foo", list); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithErrorsAndCustomClass() { // Arrange ViewDataDictionary viewData = GetViewDataWithErrors(); HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBox("foo", selectList, new { @class = "foo-class" }); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithNameOnly() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBox("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithAttributesDictionary() { // Arrange ViewDataDictionary viewData = new ViewDataDictionary(); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); //viewData["foo"] = selectList; HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); // Act MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithAttributesDictionaryAndMultiSelectList() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithAttributesDictionaryOverridesName() { // DevDiv Bugs #217602: // SelectInternal() should override the user-provided 'name' attribute // Arrange ViewDataDictionary viewData = new ViewDataDictionary(); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData); // Act MvcHtmlString html = helper.ListBox("foo", selectList, new { myAttr = "myValue", name = "theName" }); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithEmptyNameThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act & Assert Assert.ThrowsArgumentNullOrEmpty( delegate { helper.ListBox(String.Empty, (MultiSelectList)null /* selectList */); }, "name"); } [Fact] public void ListBoxWithNullNameThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act & Assert Assert.ThrowsArgumentNullOrEmpty( delegate { helper.ListBox(null /* name */, (MultiSelectList)null /* selectList */); }, "name"); } [Fact] public void ListBoxWithNullSelectListUsesViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBox("foo", null); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithObjectDictionary() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithObjectDictionaryWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithIEnumerableSelectListItem() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleIEnumerableObjects() } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.ListBox("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxThrowsWhenExpressionDoesNotEvaluateToIEnumerable() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", 123456789 } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act & Assert Assert.Throws( () => helper.ListBox("foo", MultiSelectListTest.GetSampleIEnumerableObjects()), @"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed." ); } [Fact] public void ListBoxThrowsWhenExpressionEvaluatesToString() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act & Assert Assert.Throws( () => helper.ListBox("foo", MultiSelectListTest.GetSampleIEnumerableObjects()), @"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed." ); } [Fact] public void ListBoxWithListOfSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", new string[] { "123456789", "111111111" } } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.ListBox("foo", MultiSelectListTest.GetSampleListObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithListOfSelectListItem() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleListObjects() } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.ListBox("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithPrefix() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithPrefixAndEmptyName() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.ListBox("", selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxWithPrefixAndNullSelectListUsesViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.ListBox("foo"); // Assert Assert.Equal( @"", html.ToHtmlString()); } // ListBoxFor [Fact] public void ListBoxForWithNullExpressionThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act & Assert Assert.ThrowsArgumentNull( () => helper.ListBoxFor(null, null), "expression"); } [Fact] public void ListBoxForUsesExplicitValueIfNotProvidedInViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", new[] { "A", "C" }); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForUsesExplicitValueIfNotProvidedInViewData_Unobtrusive() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", new[] { "A", "C" }); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithEnumerableModel_Unobtrusive() { // Arrange HtmlHelper> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary>()); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C"); // Act MvcHtmlString html = helper.ListBoxFor(m => m.ElementAt(0).foo, selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForUsesViewDataDefaultValue() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(_listBoxViewData); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithErrors() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithErrors()); MultiSelectList list = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, list); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithErrorsAndCustomClass() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithErrors()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList, new { @class = "foo-class" }); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithAttributesDictionary() { // Arrange MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList, HtmlHelperTest.AttributesDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithAttributesDictionaryOverridesName() { // DevDiv Bugs #217602: // SelectInternal() should override the user-provided 'name' attribute // Arrange MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList, new { myAttr = "myValue", name = "theName" }); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithNullSelectListUsesViewData() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); helper.ViewContext.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" }); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, null); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithObjectDictionary() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithObjectDictionaryWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithIEnumerableSelectListItem() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleIEnumerableObjects() } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, null); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForThrowsWhenExpressionDoesNotEvaluateToIEnumerable() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", 123456789 } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act & Assert Assert.Throws( () => helper.ListBoxFor(m => m.foo, MultiSelectListTest.GetSampleIEnumerableObjects()), @"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed." ); } [Fact] public void ListBoxForThrowsWhenExpressionEvaluatesToString() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act & Assert Assert.Throws( () => helper.ListBoxFor(m => m.foo, MultiSelectListTest.GetSampleIEnumerableObjects()), @"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed." ); } [Fact] public void ListBoxForWithListOfSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", new string[] { "123456789", "111111111" } } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, MultiSelectListTest.GetSampleListObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithListOfSelectListItem() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleListObjects() } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, null); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithPrefix() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectDictionary); // Assert Assert.Equal( @"", html.ToHtmlString()); } [Fact] public void ListBoxForWithPrefixAndListOfSelectListItemSelectsDefaultFromViewData() { // Arrange ViewDataDictionary vdd = new ViewDataDictionary { { "foo", new string[] { "123456789", "111111111" } } }; HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.ListBoxFor(m => m.foo, MultiSelectListTest.GetSampleListObjects()); // Assert Assert.Equal( @"", html.ToHtmlString()); } // Culture tests [Fact] public void SelectHelpersUseCurrentCultureToConvertValues() { // Arrange HtmlHelper defaultValueHelper = MvcHelper.GetHtmlHelper(new ViewDataDictionary { { "foo", new[] { new DateTime(1900, 1, 1, 0, 0, 1) } }, { "bar", new DateTime(1900, 1, 1, 0, 0, 1) } }); HtmlHelper helper = MvcHelper.GetHtmlHelper(); SelectList selectList = new SelectList(GetSampleCultureAnonymousObjects(), "Date", "FullWord", new DateTime(1900, 1, 1, 0, 0, 0)); var tests = new[] { // DropDownList(name, selectList, optionLabel) new { Html = @"", Action = new Func(() => helper.DropDownList("foo", selectList, (string)null)) }, // DropDownList(name, selectList, optionLabel) (With default value selected from ViewData) new { Html = @"", Action = new Func(() => defaultValueHelper.DropDownList("bar", selectList, (string)null)) }, // ListBox(name, selectList) new { Html = @"", Action = new Func(() => helper.ListBox("foo", selectList)) }, // ListBox(name, selectList) (With default value selected from ViewData) new { Html = @"", Action = new Func(() => defaultValueHelper.ListBox("foo", selectList)) } }; // Act && Assert using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US")) { foreach (var test in tests) { Assert.Equal(test.Html, test.Action().ToHtmlString()); } } } // Helpers private class FooModel { public string foo { get; set; } } private class FooBarModel : FooModel { public string bar { get; set; } } private class NonIEnumerableModel { public int foo { get; set; } } private static ViewDataDictionary GetViewDataWithErrors() { ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } }; viewData.Model = new FooBarModel { foo = "ViewItemFoo", bar = "ViewItemBar" }; ModelState modelStateFoo = new ModelState(); modelStateFoo.Errors.Add(new ModelError("foo error 1")); modelStateFoo.Errors.Add(new ModelError("foo error 2")); viewData.ModelState["foo"] = modelStateFoo; modelStateFoo.Value = new ValueProviderResult(new string[] { "Bravo", "Charlie" }, "Bravo", CultureInfo.InvariantCulture); return viewData; } internal static IEnumerable GetSampleCultureAnonymousObjects() { return new[] { new { Date = new DateTime(1900, 1, 1, 0, 0, 0), FullWord = "Alpha" }, new { Date = new DateTime(1900, 1, 1, 0, 0, 1), FullWord = "Bravo" }, new { Date = new DateTime(1900, 1, 1, 0, 0, 2), FullWord = "Charlie" } }; } private class RequiredModel { [Required] public string foo { get; set; } } } }