1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Web.Routing;
using System.Web.TestUtil;
using System.Web.UI;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Test
{
public class ViewUserControlTest
{
[Fact]
public void ModelProperty()
{
// Arrange
object model = new object();
ViewDataDictionary viewData = new ViewDataDictionary(model);
ViewUserControl viewUserControl = new ViewUserControl();
viewUserControl.ViewData = viewData;
// Act
object viewPageModel = viewUserControl.Model;
// Assert
Assert.Equal(model, viewPageModel);
Assert.Equal(model, viewUserControl.ViewData.Model);
}
[Fact]
public void ModelPropertyStronglyTyped()
{
// Arrange
FooModel model = new FooModel();
ViewDataDictionary<FooModel> viewData = new ViewDataDictionary<FooModel>(model);
ViewUserControl<FooModel> viewUserControl = new ViewUserControl<FooModel>();
viewUserControl.ViewData = viewData;
// Act
object viewPageModelObject = ((ViewUserControl)viewUserControl).Model;
FooModel viewPageModelPerson = viewUserControl.Model;
// Assert
Assert.Equal(model, viewPageModelObject);
Assert.Equal(model, viewPageModelPerson);
}
[Fact]
public void RenderViewAndRestoreContentType()
{
// Arrange
Mock<ViewContext> mockViewContext = new Mock<ViewContext>();
mockViewContext.SetupProperty(c => c.HttpContext.Response.ContentType);
ViewContext vc = mockViewContext.Object;
Mock<ViewPage> mockViewPage = new Mock<ViewPage>();
mockViewPage.Setup(vp => vp.RenderView(vc)).Callback(() => vc.HttpContext.Response.ContentType = "newContentType");
// Act
vc.HttpContext.Response.ContentType = "oldContentType";
ViewUserControl.RenderViewAndRestoreContentType(mockViewPage.Object, vc);
string postContentType = vc.HttpContext.Response.ContentType;
// Assert
Assert.Equal("oldContentType", postContentType);
}
[Fact]
public void SetViewItem()
{
// Arrange
ViewUserControl vuc = new ViewUserControl();
object viewItem = new object();
vuc.ViewData = new ViewDataDictionary(viewItem);
// Act
vuc.ViewData.Model = viewItem;
object newViewItem = vuc.ViewData.Model;
// Assert
Assert.Same(viewItem, newViewItem);
}
[Fact]
public void SetViewItemOnBaseClassPropagatesToDerivedClass()
{
// Arrange
ViewUserControl<object> vucInt = new ViewUserControl<object>();
ViewUserControl vuc = vucInt;
vuc.ViewData = new ViewDataDictionary();
object o = new object();
// Act
vuc.ViewData.Model = o;
// Assert
Assert.Equal(o, vucInt.ViewData.Model);
Assert.Equal(o, vuc.ViewData.Model);
}
[Fact]
public void SetViewItemOnDerivedClassPropagatesToBaseClass()
{
// Arrange
ViewUserControl<object> vucInt = new ViewUserControl<object>();
ViewUserControl vuc = vucInt;
vucInt.ViewData = new ViewDataDictionary<object>();
object o = new object();
// Act
vucInt.ViewData.Model = o;
// Assert
Assert.Equal(o, vucInt.ViewData.Model);
Assert.Equal(o, vuc.ViewData.Model);
}
[Fact]
public void SetViewItemToWrongTypeThrows()
{
// Arrange
ViewUserControl<string> vucString = new ViewUserControl<string>();
vucString.ViewData = new ViewDataDictionary<string>();
ViewUserControl vuc = vucString;
// Act & Assert
Assert.Throws<InvalidOperationException>(
delegate { vuc.ViewData.Model = 50; },
"The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.");
}
[Fact]
public void GetViewDataWhenNoPageSetThrows()
{
ViewUserControl vuc = new ViewUserControl();
vuc.AppRelativeVirtualPath = "~/Foo.ascx";
Assert.Throws<InvalidOperationException>(
delegate { var foo = vuc.ViewData["Foo"]; },
"The ViewUserControl '~/Foo.ascx' cannot find an IViewDataContainer object. The ViewUserControl must be inside a ViewPage, a ViewMasterPage, or another ViewUserControl.");
}
[Fact]
public void GetViewDataWhenRegularPageSetThrows()
{
Page p = new Page();
p.Controls.Add(new Control());
ViewUserControl vuc = new ViewUserControl();
p.Controls[0].Controls.Add(vuc);
vuc.AppRelativeVirtualPath = "~/Foo.ascx";
Assert.Throws<InvalidOperationException>(
delegate { var foo = vuc.ViewData["Foo"]; },
"The ViewUserControl '~/Foo.ascx' cannot find an IViewDataContainer object. The ViewUserControl must be inside a ViewPage, a ViewMasterPage, or another ViewUserControl.");
}
[Fact]
public void GetViewDataFromViewPage()
{
// Arrange
ViewPage p = new ViewPage();
p.Controls.Add(new Control());
ViewUserControl vuc = new ViewUserControl();
p.Controls[0].Controls.Add(vuc);
p.ViewData = new ViewDataDictionary { { "FirstName", "Joe" }, { "LastName", "Schmoe" } };
// Act
object firstName = vuc.ViewData.Eval("FirstName");
object lastName = vuc.ViewData.Eval("LastName");
// Assert
Assert.Equal("Joe", firstName);
Assert.Equal("Schmoe", lastName);
}
[Fact]
public void GetViewDataFromViewPageWithViewDataKeyPointingToObject()
{
// Arrange
ViewDataDictionary vdd = new ViewDataDictionary()
{
{ "Foo", "FooParent" },
{ "Bar", "BarParent" },
{ "Child", new object() }
};
ViewPage p = new ViewPage();
p.Controls.Add(new Control());
ViewUserControl vuc = new ViewUserControl() { ViewDataKey = "Child" };
p.Controls[0].Controls.Add(vuc);
p.ViewData = vdd;
// Act
object oFoo = vuc.ViewData.Eval("Foo");
object oBar = vuc.ViewData.Eval("Bar");
// Assert
Assert.Equal(vdd["Child"], vuc.ViewData.Model);
Assert.Equal("FooParent", oFoo);
Assert.Equal("BarParent", oBar);
}
[Fact]
public void GetViewDataFromViewPageWithViewDataKeyPointingToViewDataDictionary()
{
// Arrange
ViewDataDictionary vdd = new ViewDataDictionary()
{
{ "Foo", "FooParent" },
{ "Bar", "BarParent" },
{
"Child",
new ViewDataDictionary()
{
{ "Foo", "FooChild" },
{ "Bar", "BarChild" }
}
}
};
ViewPage p = new ViewPage();
p.Controls.Add(new Control());
ViewUserControl vuc = new ViewUserControl() { ViewDataKey = "Child" };
p.Controls[0].Controls.Add(vuc);
p.ViewData = vdd;
// Act
object oFoo = vuc.ViewData.Eval("Foo");
object oBar = vuc.ViewData.Eval("Bar");
// Assert
Assert.Equal(vdd["Child"], vuc.ViewData);
Assert.Equal("FooChild", oFoo);
Assert.Equal("BarChild", oBar);
}
[Fact]
public void GetViewDataFromViewUserControl()
{
// Arrange
ViewPage p = new ViewPage();
p.Controls.Add(new Control());
ViewUserControl outerVuc = new ViewUserControl();
p.Controls[0].Controls.Add(outerVuc);
outerVuc.Controls.Add(new Control());
ViewUserControl vuc = new ViewUserControl();
outerVuc.Controls[0].Controls.Add(vuc);
p.ViewData = new ViewDataDictionary { { "FirstName", "Joe" }, { "LastName", "Schmoe" } };
// Act
object firstName = vuc.ViewData.Eval("FirstName");
object lastName = vuc.ViewData.Eval("LastName");
// Assert
Assert.Equal("Joe", firstName);
Assert.Equal("Schmoe", lastName);
}
[Fact]
public void GetViewDataFromViewUserControlWithViewDataKeyOnInnerControl()
{
// Arrange
ViewPage p = new ViewPage();
p.Controls.Add(new Control());
ViewUserControl outerVuc = new ViewUserControl();
p.Controls[0].Controls.Add(outerVuc);
outerVuc.Controls.Add(new Control());
ViewUserControl vuc = new ViewUserControl() { ViewDataKey = "SubData" };
outerVuc.Controls[0].Controls.Add(vuc);
p.ViewData = new ViewDataDictionary { { "FirstName", "Joe" }, { "LastName", "Schmoe" } };
p.ViewData["SubData"] = new ViewDataDictionary { { "FirstName", "SubJoe" }, { "LastName", "SubSchmoe" } };
// Act
object firstName = vuc.ViewData.Eval("FirstName");
object lastName = vuc.ViewData.Eval("LastName");
// Assert
Assert.Equal("SubJoe", firstName);
Assert.Equal("SubSchmoe", lastName);
}
[Fact]
public void GetViewDataFromViewUserControlWithViewDataKeyOnOuterControl()
{
// Arrange
ViewPage p = new ViewPage();
p.Controls.Add(new Control());
ViewUserControl outerVuc = new ViewUserControl() { ViewDataKey = "SubData" };
p.Controls[0].Controls.Add(outerVuc);
outerVuc.Controls.Add(new Control());
ViewUserControl vuc = new ViewUserControl();
outerVuc.Controls[0].Controls.Add(vuc);
p.ViewData = new ViewDataDictionary { { "FirstName", "Joe" }, { "LastName", "Schmoe" } };
p.ViewData["SubData"] = new ViewDataDictionary { { "FirstName", "SubJoe" }, { "LastName", "SubSchmoe" } };
// Act
object firstName = vuc.ViewData.Eval("FirstName");
object lastName = vuc.ViewData.Eval("LastName");
// Assert
Assert.Equal("SubJoe", firstName);
Assert.Equal("SubSchmoe", lastName);
}
[Fact]
public void ViewDataKeyProperty()
{
MemberHelper.TestStringProperty(new ViewUserControl(), "ViewDataKey", String.Empty, testDefaultValueAttribute: true);
}
[Fact]
public void GetWrongGenericViewItemTypeThrows()
{
// Arrange
ViewPage p = new ViewPage();
p.ViewData = new ViewDataDictionary();
p.ViewData["Foo"] = new DummyViewData { MyInt = 123, MyString = "Whatever" };
MockViewUserControl<MyViewData> vuc = new MockViewUserControl<MyViewData>() { ViewDataKey = "FOO" };
vuc.AppRelativeVirtualPath = "~/Foo.aspx";
p.Controls.Add(new Control());
p.Controls[0].Controls.Add(vuc);
// Act
Assert.Throws<InvalidOperationException>(
delegate { var foo = vuc.ViewData.Model.IntProp; },
@"The model item passed into the dictionary is of type 'System.Web.Mvc.Test.ViewUserControlTest+DummyViewData', but this dictionary requires a model item of type 'System.Web.Mvc.Test.ViewUserControlTest+MyViewData'.");
}
[Fact]
public void GetGenericViewItemType()
{
// Arrange
ViewPage p = new ViewPage();
p.Controls.Add(new Control());
MockViewUserControl<MyViewData> vuc = new MockViewUserControl<MyViewData>() { ViewDataKey = "FOO" };
p.Controls[0].Controls.Add(vuc);
p.ViewData = new ViewDataDictionary();
p.ViewData["Foo"] = new MyViewData { IntProp = 123, StringProp = "miao" };
// Act
int intProp = vuc.ViewData.Model.IntProp;
string stringProp = vuc.ViewData.Model.StringProp;
// Assert
Assert.Equal(123, intProp);
Assert.Equal("miao", stringProp);
}
[Fact]
public void GetHtmlHelperFromViewPage()
{
// Arrange
ViewUserControl vuc = new ViewUserControl();
ViewPage containerPage = new ViewPage();
containerPage.Controls.Add(vuc);
ViewContext vc = new Mock<ViewContext>().Object;
vuc.ViewContext = vc;
// Act
HtmlHelper htmlHelper = vuc.Html;
// Assert
Assert.Equal(vc, htmlHelper.ViewContext);
Assert.Equal(vuc, htmlHelper.ViewDataContainer);
}
[Fact]
public void GetHtmlHelperFromRegularPage()
{
// Arrange
ViewUserControl vuc = new ViewUserControl();
Page containerPage = new Page();
containerPage.Controls.Add(vuc);
// Assert
Assert.Throws<InvalidOperationException>(
delegate { HtmlHelper foo = vuc.Html; },
"A ViewUserControl can be used only in pages that derive from ViewPage or ViewPage<TModel>.");
}
[Fact]
public void GetUrlHelperFromViewPage()
{
// Arrange
ViewUserControl vuc = new ViewUserControl();
ViewPage containerPage = new ViewPage();
containerPage.Controls.Add(vuc);
RequestContext rc = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
UrlHelper urlHelper = new UrlHelper(rc);
containerPage.Url = urlHelper;
// Assert
Assert.Equal(vuc.Url, urlHelper);
}
[Fact]
public void GetUrlHelperFromRegularPage()
{
// Arrange
ViewUserControl vuc = new ViewUserControl();
Page containerPage = new Page();
containerPage.Controls.Add(vuc);
// Assert
Assert.Throws<InvalidOperationException>(
delegate { UrlHelper foo = vuc.Url; },
"A ViewUserControl can be used only in pages that derive from ViewPage or ViewPage<TModel>.");
}
[Fact]
public void GetWriterFromViewPage()
{
// Arrange
MockViewUserControl vuc = new MockViewUserControl();
MockViewUserControlContainerPage containerPage = new MockViewUserControlContainerPage(vuc);
bool triggered = false;
HtmlTextWriter writer = new HtmlTextWriter(TextWriter.Null);
containerPage.RenderCallback = delegate()
{
triggered = true;
Assert.Equal(writer, vuc.Writer);
};
// Act & Assert
Assert.Null(vuc.Writer);
containerPage.RenderControl(writer);
Assert.Null(vuc.Writer);
Assert.True(triggered);
}
[Fact]
public void GetWriterFromRegularPageThrows()
{
// Arrange
MockViewUserControl vuc = new MockViewUserControl();
Page containerPage = new Page();
containerPage.Controls.Add(vuc);
// Act
Assert.Throws<InvalidOperationException>(
delegate { HtmlTextWriter writer = vuc.Writer; },
"A ViewUserControl can be used only in pages that derive from ViewPage or ViewPage<TModel>.");
}
[Fact]
public void ViewBagProperty_ReflectsViewData()
{
// Arrange
ViewPage containerPage = new ViewPage();
ViewUserControl userControl = new ViewUserControl();
containerPage.Controls.Add(userControl);
userControl.ViewData["A"] = 1;
// Act & Assert
Assert.NotNull(userControl.ViewBag);
Assert.Equal(1, userControl.ViewBag.A);
}
[Fact]
public void ViewBagProperty_ReflectsNewViewDataInstance()
{
// Arrange
ViewPage containerPage = new ViewPage();
ViewUserControl userControl = new ViewUserControl();
containerPage.Controls.Add(userControl);
userControl.ViewData["A"] = 1;
userControl.ViewData = new ViewDataDictionary() { { "A", "bar" } };
// Act & Assert
Assert.Equal("bar", userControl.ViewBag.A);
}
[Fact]
public void ViewBagProperty_PropagatesChangesToViewData()
{
// Arrange
ViewPage containerPage = new ViewPage();
ViewUserControl userControl = new ViewUserControl();
containerPage.Controls.Add(userControl);
userControl.ViewData["A"] = 1;
// Act
userControl.ViewBag.A = "foo";
userControl.ViewBag.B = 2;
// Assert
Assert.Equal("foo", userControl.ViewData["A"]);
Assert.Equal(2, userControl.ViewData["B"]);
}
private sealed class DummyViewData
{
public int MyInt { get; set; }
public string MyString { get; set; }
}
private sealed class MockViewUserControlContainerPage : ViewPage
{
public Action RenderCallback { get; set; }
public MockViewUserControlContainerPage(ViewUserControl userControl)
{
Controls.Add(userControl);
}
protected override void RenderChildren(HtmlTextWriter writer)
{
if (RenderCallback != null)
{
RenderCallback();
}
base.RenderChildren(writer);
}
}
private sealed class MockViewUserControl : ViewUserControl
{
}
private sealed class MockViewUserControl<TViewData> : ViewUserControl<TViewData>
{
}
private sealed class MyViewData
{
public int IntProp { get; set; }
public string StringProp { get; set; }
}
private sealed class FooModel
{
}
}
}
|