2 // Tests for System.Web.UI.WebControls.FormView.cs
5 // Merav Sudri (meravs@mainsoft.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
35 using NUnit.Framework;
38 using System.Web.UI.WebControls;
40 namespace MonoTests.System.Web.UI.WebControls
42 public class ControlParameterPoker : ControlParameter
44 public ControlParameterPoker (ControlParameter control)
49 public ControlParameterPoker (string name,TypeCode type, string controlID,string propertyName)
50 :base(name,type,controlID,propertyName)
54 public ControlParameterPoker (string name, string controlId, string propertyName)
55 : base (name, controlId, propertyName)
60 public ControlParameterPoker (string name, string controlId)
61 : base (name, controlId)
65 public ControlParameterPoker() // constructor
70 public object DoEvaluate (HttpContext context,Control control)
72 return base.Evaluate (context,control);
75 public Parameter DoClone ()
80 public object SaveState ()
82 return SaveViewState ();
86 public void LoadState (object o)
91 public StateBag StateBag
93 get { return base.ViewState; }
98 class TestControl : Control
102 public DataKey Values {
103 get { return _values; }
106 public TestControl (DataKey values)
108 this._values = values;
113 public class ControlParameterTest
116 public void ControlParameter_DefaultProperties ()
118 ControlParameterPoker ctrlParam1 = new ControlParameterPoker ();
119 Assert.AreEqual ("",ctrlParam1.ControlID, "ControlIdDefault");
120 Assert.AreEqual ("", ctrlParam1.PropertyName, "PropertyNameDefault");
121 ControlParameterPoker ctrlParam2 = new ControlParameterPoker ("ControlTest","ControlID");
122 Assert.AreEqual ("ControlID", ctrlParam2.ControlID, "OverloadConstructorControlId1");
123 Assert.AreEqual ("ControlTest", ctrlParam2.Name, "OverloadConstructorPropertyName1");
124 ControlParameterPoker ctrlParam3 = new ControlParameterPoker ("Age",TypeCode.Int32,"Label1","Text");
125 Assert.AreEqual ("Age", ctrlParam3.Name, "OverloadConstructorName2");
126 Assert.AreEqual (TypeCode.Int32, ctrlParam3.Type, "OverloadConstructorType2");
127 Assert.AreEqual ("Label1", ctrlParam3.ControlID, "OverloadConstructorControlID2");
128 Assert.AreEqual ("Text", ctrlParam3.PropertyName, "OverloadConstructorPropertyName2");
129 ControlParameterPoker ctrlParam4 = new ControlParameterPoker ("Age","Label1","Text");
130 Assert.AreEqual ("Age", ctrlParam4.Name, "OverloadConstructorName3");
131 Assert.AreEqual ("Label1", ctrlParam4.ControlID, "OverloadConstructorControlID3");
132 Assert.AreEqual ("Text", ctrlParam4.PropertyName, "OverloadConstructorPropertyName3");
133 ControlParameterPoker ctrlParam5 = new ControlParameterPoker (ctrlParam3);
134 Assert.AreEqual ("Age", ctrlParam3.Name, "OverloadConstructorName4");
135 Assert.AreEqual (TypeCode.Int32, ctrlParam3.Type, "OverloadConstructorType4");
136 Assert.AreEqual ("Label1", ctrlParam3.ControlID, "OverloadConstructorControlID4");
137 Assert.AreEqual ("Text", ctrlParam3.PropertyName, "OverloadConstructorPropertyName4");
141 public void ControlParameter_AssignToDefaultProperties ()
143 ControlParameterPoker ctrlParam = new ControlParameterPoker ();
144 ctrlParam.PropertyName = "Text";
145 Assert.AreEqual ("Text",ctrlParam.PropertyName ,"AssignToPropertyName");
146 ctrlParam.ControlID ="Label";
147 Assert.AreEqual ("Label",ctrlParam.ControlID ,"AssignToPropertyName");
153 public void ControlParameter_Clone ()
155 ControlParameterPoker ctrlParam = new ControlParameterPoker ("Salary", TypeCode.Int64, "TextBox1", "Text");
156 ControlParameter clonedParam = (ControlParameter)ctrlParam.DoClone ();
157 Assert.AreEqual ("Salary", clonedParam.Name, "ClonedParamName");
158 Assert.AreEqual (TypeCode.Int64, clonedParam.Type, "ClonedParamType");
159 Assert.AreEqual ("TextBox1", clonedParam.ControlID, "ClonedParamControlID");
160 Assert.AreEqual ("Text", clonedParam.PropertyName, "ClonedParamPropertyName");
164 public void ControlParameter_Evaluate ()
166 ControlParameterPoker ctrlParam = new ControlParameterPoker ("Salary",TypeCode.Int64,"Label1","Text");
167 Page page = new Page ();
168 Label label1 = new Label ();
169 label1.ID = "Label1";
170 label1.Text = "2000";
171 page.Controls.Add (label1);
172 string value=(string)ctrlParam.DoEvaluate (HttpContext.Current,label1);
173 Assert.AreEqual ("2000", value, "EvaluateValue1");
174 label1.Text = "TestNewValue";
175 ctrlParam.Type = TypeCode.String;
176 value = (string) ctrlParam.DoEvaluate (HttpContext.Current, label1);
177 Assert.AreEqual ("TestNewValue", value, "EvaluateValue2");
181 public void ControlParameter_EvaluateComplex ()
183 ControlParameterPoker ctrlParam = new ControlParameterPoker ("Test", "TestControl1", "Values['one']");
184 Page page = new Page ();
186 OrderedDictionary dict = new OrderedDictionary ();
187 dict.Add ("one", "1");
189 DataKey values = new DataKey (dict);
190 TestControl test = new TestControl (values);
191 test.ID = "TestControl1";
192 page.Controls.Add (test);
193 string value = ctrlParam.DoEvaluate (HttpContext.Current, test) as string;
194 Assert.AreEqual ("1", value, "#1");
198 [ExpectedException (typeof (ArgumentException))]
199 public void EvaluateArgumemtException ()
201 ControlParameterPoker ctrlParam = new ControlParameterPoker ();
202 TextBox textBox1 = new TextBox ();
203 textBox1.ID = "textbox1";
204 Page page = new Page ();
205 page.Controls.Add (textBox1);
206 ctrlParam.DoEvaluate (HttpContext.Current, textBox1);
209 [ExpectedException (typeof (InvalidOperationException))]
210 public void EvaluateInvalidOperationException ()
212 ControlParameterPoker ctrlParam = new ControlParameterPoker ("age", "Button", "parameter");
213 Button b = new Button ();
214 Page page = new Page ();
215 page.Controls.Add (b);
216 ctrlParam.DoEvaluate (HttpContext.Current, b);