PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web.DynamicData/Test/System.Web.DynamicData/FieldTemplateUserControlTest.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 240 lines | 138 code | 29 blank | 73 comment | 0 complexity | 0a3ea4a087f0b148bba6977bf3312c1a MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // FieldTemplateUserControlTest.cs
  3. //
  4. // Authors:
  5. // Marek Habersack <mhabersack@novell.com>
  6. //
  7. // Copyright (C) 2009 Novell Inc. http://novell.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.ObjectModel;
  33. using System.Collections.Specialized;
  34. using System.ComponentModel.DataAnnotations;
  35. using System.Data.SqlClient;
  36. using System.Data.Linq;
  37. using System.Data.Linq.Mapping;
  38. using System.Globalization;
  39. using System.Linq;
  40. using System.Net;
  41. using System.Reflection;
  42. using System.Security.Permissions;
  43. using System.Security.Principal;
  44. using System.Web;
  45. using System.Web.UI;
  46. using System.Web.DynamicData;
  47. using System.Web.DynamicData.ModelProviders;
  48. using System.Web.Routing;
  49. using System.Web.UI.HtmlControls;
  50. using System.Web.UI.WebControls;
  51. using System.IO;
  52. using NUnit.Framework;
  53. using NUnit.Mocks;
  54. using MonoTests.stand_alone.WebHarness;
  55. using MonoTests.SystemWeb.Framework;
  56. using MonoTests.Common;
  57. using MonoTests.DataSource;
  58. using MonoTests.DataObjects;
  59. using MonoTests.ModelProviders;
  60. using MetaModel = System.Web.DynamicData.MetaModel;
  61. using MetaTable = System.Web.DynamicData.MetaTable;
  62. namespace MonoTests.System.Web.DynamicData
  63. {
  64. [TestFixture]
  65. public class FieldTemplateUserControlTest
  66. {
  67. DynamicDataContainerModelProvider<TestDataContext> dynamicModelProvider;
  68. [TestFixtureSetUp]
  69. public void SetUp ()
  70. {
  71. Type type = GetType ();
  72. WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_01.aspx", "ListView_DynamicControl_01.aspx");
  73. WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_01.aspx.cs", "ListView_DynamicControl_01.aspx.cs");
  74. WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_10.aspx", "ListView_DynamicControl_10.aspx");
  75. WebTest.CopyResource (type, "MonoTests.WebPages.ListView_DynamicControl_10.aspx.cs", "ListView_DynamicControl_10.aspx.cs");
  76. dynamicModelProvider = new DynamicDataContainerModelProvider<TestDataContext> ();
  77. Utils.RegisterContext (dynamicModelProvider, new ContextConfiguration () { ScaffoldAllTables = true });
  78. }
  79. [TestFixtureTearDown]
  80. public void TearDown ()
  81. {
  82. WebTest.Unload ();
  83. }
  84. [Test]
  85. public void Defaults ()
  86. {
  87. var field = new PokerFieldTemplateUserControl ();
  88. // And for another time - not a single null check in the code...
  89. //
  90. // All the asserts commented out below throw a NREX
  91. //
  92. //Assert.IsNull (field.ChildrenColumn, "#A1");
  93. //Assert.IsNull (field.GetChildrenPath (), "#A2");
  94. //Assert.IsNull (field.Column, "#A3");
  95. Assert.IsNull (field.DataControl, "#A4");
  96. //Assert.IsNull (field.FieldValue, "#A5");
  97. //Assert.IsNull (field.FieldValueEditString, "#A6");
  98. //Assert.IsNull (field.FieldValueString, "#A7");
  99. //Assert.IsNull (field.ForeignKeyColumn, "#A8");
  100. //Assert.IsNull (field.GetForeignKeyPath (), "#A9");
  101. Assert.IsNull (field.Host, "#A10");
  102. //Assert.IsNull (field.MetadataAttributes, "#A11");
  103. //Assert.IsNull (field.Mode, "#A12");
  104. //Assert.IsNull (field.Row, "#A13");
  105. //Assert.IsNull (field.Table, "#A14");
  106. }
  107. [Test]
  108. public void Defaults_WithHost ()
  109. {
  110. var test = new WebTest ("ListView_DynamicControl_01.aspx");
  111. test.Invoker = PageInvoker.CreateOnLoad (Defaults_WithHost_OnLoad);
  112. var p = test.Run ();
  113. Assert.IsNotNull (test.Response, "#X1");
  114. Assert.AreNotEqual (HttpStatusCode.NotFound, test.Response.StatusCode, "#X1-1{0}Returned HTML:{0}{1}", Environment.NewLine, p);
  115. Assert.AreNotEqual (HttpStatusCode.InternalServerError, test.Response.StatusCode, "#X1-2{0}Returned HTML:{0}{1}", Environment.NewLine, p);
  116. Assert.IsFalse (String.IsNullOrEmpty (p), "#X1-3");
  117. }
  118. static void Defaults_WithHost_OnLoad (Page p)
  119. {
  120. // Not many "defaults" here
  121. var lc = p.FindControl ("ListView1") as ListView;
  122. Assert.IsNotNull (lc, "#A1");
  123. var dc = lc.FindChild<DynamicControl> ("FirstName");
  124. Assert.IsNotNull (dc, "#B1-1");
  125. Assert.AreEqual ("FirstName", dc.ID, "#B1-2");
  126. var field = new PokerFieldTemplateUserControl ();
  127. // Without it Row NREXes... However, with Page set, we still get an exception
  128. // this time from Page.GetDataItem () as we're not bound to data. So, there is
  129. // no way to test the "defaults" as if we used a field control retrieved from
  130. // DataControl it would already be initialized
  131. field.Page = p;
  132. ((IFieldTemplate) field).SetHost (dc);
  133. AssertExtensions.Throws<Exception> (() => {
  134. var f = field.ChildrenColumn;
  135. }, "#C1");
  136. // The FirstName column is not a children one
  137. AssertExtensions.Throws<Exception> (() => {
  138. field.GetChildrenPath ();
  139. }, "#C2");
  140. Assert.IsNotNull (field.Column, "#C3");
  141. Assert.AreEqual (dc.Column, field.Column, "#C3-1");
  142. Assert.IsNull (field.DataControl, "#C4");
  143. // The tests below fail with exception:
  144. //
  145. // MonoTests.System.Web.DynamicData.FieldTemplateUserControlTest.Defaults_WithHost:
  146. // System.InvalidOperationException : System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
  147. // at System.Web.UI.Page.GetDataItem()
  148. // at System.Web.DynamicData.FieldTemplateUserControl.get_Row()
  149. // at System.Web.DynamicData.FieldTemplateUserControl.GetColumnValue(MetaColumn column)
  150. // at System.Web.DynamicData.FieldTemplateUserControl.get_FieldValue()
  151. // at System.Web.DynamicData.FieldTemplateUserControl.get_FieldValueEditString()
  152. // at MonoTests.System.Web.DynamicData.FieldTemplateUserControlTest.Defaults_WithHost_OnLoad(Page p)
  153. // at MonoTests.SystemWeb.Framework.PageInvoker.Invoke(PageDelegate callback)
  154. // at MonoTests.SystemWeb.Framework.PageInvoker.OnLoad(Object sender, EventArgs a)
  155. // at System.EventHandler.Invoke(Object sender, EventArgs e)
  156. // at System.Web.UI.Control.OnLoad(EventArgs e)
  157. // at System.Web.UI.Control.LoadRecursive()
  158. // at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  159. // ----> System.InvalidOperationException : Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
  160. //Assert.IsNull (field.FieldValue, "#C5");
  161. //Assert.IsNull (field.FieldValueEditString, "#C6");
  162. //Assert.IsNull (field.FieldValueString, "#C7");
  163. // The FirstName column is not a foreign key one
  164. AssertExtensions.Throws<Exception> (() => {
  165. var f = field.ForeignKeyColumn;
  166. }, "#C8");
  167. AssertExtensions.Throws<Exception> (() => {
  168. var f = field.GetForeignKeyPath ();
  169. }, "#C9");
  170. Assert.IsNotNull (field.Host, "#C10");
  171. Assert.AreEqual (dc, field.Host, "#C10-1");
  172. Assert.IsNotNull (field.MetadataAttributes, "#C11");
  173. Assert.AreEqual (dc.Column.Attributes, field.MetadataAttributes, "#C11-1");
  174. Assert.AreEqual (DataBoundControlMode.ReadOnly, field.Mode, "#C12");
  175. Assert.AreEqual (field.Host.Mode, field.Mode, "#C12-1");
  176. // Failure with the same exception as above
  177. //Assert.IsNull (field.Row, "#C13");
  178. Assert.IsNotNull (field.Table, "#C14");
  179. Assert.AreEqual (dc.Table, field.Table, "#C14-1");
  180. }
  181. [Test]
  182. public void ChildrenPath ()
  183. {
  184. var test = new WebTest ("ListView_DynamicControl_10.aspx");
  185. test.Invoker = PageInvoker.CreateOnInit (ChildrenPath_OnInit);
  186. var p = test.Run ();
  187. Assert.IsNotNull (test.Response, "#X1");
  188. Assert.AreNotEqual (HttpStatusCode.NotFound, test.Response.StatusCode, "#X1-1{0}Returned HTML:{0}{1}", Environment.NewLine, p);
  189. Assert.AreNotEqual (HttpStatusCode.InternalServerError, test.Response.StatusCode, "#X1-2{0}Returned HTML:{0}{1}", Environment.NewLine, p);
  190. Assert.IsFalse (String.IsNullOrEmpty (p), "#X1-3");
  191. }
  192. static void ChildrenPath_OnInit (Page p)
  193. {
  194. var lc = p.FindControl ("ListView1") as ListView;
  195. Assert.IsNotNull (lc, "#A1");
  196. var page = p as TestsBasePage<TestDataContext3>;
  197. Assert.IsNotNull (p, "#A1-1");
  198. page.ItemDataBinding += new EventHandler(ChildrenPath_ListControl_OnDataBound);
  199. }
  200. static void ChildrenPath_ListControl_OnDataBound (object sender, EventArgs e)
  201. {
  202. var dc = sender as DynamicControl;
  203. Assert.IsNotNull (dc, "#B1");
  204. Assert.AreEqual ("PrimaryKeyColumn2", dc.ID, "#B1-1");
  205. Assert.AreEqual (typeof (MetaChildrenColumn), dc.Column.GetType (), "#B1-2");
  206. var field = dc.FieldTemplate as PokerFieldTemplateUserControl;
  207. Assert.IsNotNull (field, "#C1");
  208. Assert.AreEqual ("/NunitWeb/AssociatedBarTable/List.aspx", field.GetChildrenPath (), "#C1-1");
  209. }
  210. }
  211. }