[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ControlParameterTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.FormView.cs 
3 //
4 // Author:
5 //      Merav Sudri (meravs@mainsoft.com)
6 //
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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
30
31 using System;
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
34 using System.Text;
35 using NUnit.Framework;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.UI.WebControls;
39
40 namespace MonoTests.System.Web.UI.WebControls
41 {
42         public class ControlParameterPoker : ControlParameter
43         {
44                 public ControlParameterPoker (ControlParameter control)
45                         : base (control)
46                 {
47                 }
48             
49                 public ControlParameterPoker (string name,TypeCode type, string controlID,string propertyName)
50                         :base(name,type,controlID,propertyName)
51                 {
52                 }
53
54                 public ControlParameterPoker (string name, string controlId, string propertyName)
55                         : base (name, controlId, propertyName)
56                 {
57                 }
58
59         
60                 public ControlParameterPoker (string name, string controlId)
61                         : base (name, controlId)        
62                 {       
63                 }
64        
65                 public ControlParameterPoker() // constructor       
66                 {        
67                         TrackViewState ();       
68                 }
69
70                 public object DoEvaluate (HttpContext context,Control control)
71                 {
72                         return base.Evaluate (context,control);
73                 }
74
75                 public Parameter DoClone ()
76                 {
77                         return base.Clone ();
78                 }
79         
80                 public object SaveState ()              
81                 {       
82                         return SaveViewState ();                
83                 }
84
85        
86                 public void LoadState (object o)       
87                 {       
88                         LoadViewState (o);      
89                 }
90        
91                 public StateBag StateBag       
92                 {       
93                         get { return base.ViewState; }      
94                 }
95        
96         }
97
98         class TestControl : Control 
99         {
100                 DataKey _values;
101                 
102                 public DataKey Values {
103                         get { return _values; }
104                 }
105
106                 public TestControl (DataKey values)
107                 {
108                         this._values = values;
109                 }
110         }
111         
112         [TestFixture]
113         public class ControlParameterTest
114         {
115                 [Test]
116                 public void ControlParameter_DefaultProperties ()
117                 {
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");                  
138                  }
139
140                 [Test]
141                 public void ControlParameter_AssignToDefaultProperties ()
142                 {
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"); 
148                  }
149
150             //Protected Methods
151
152                 [Test]
153                 public void ControlParameter_Clone ()
154                 {
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");
161
162                 }
163                 [Test]
164                 public void ControlParameter_Evaluate ()
165                 {
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");
178                 }
179
180                 [Test]
181                 public void ControlParameter_EvaluateComplex ()
182                 {
183                         ControlParameterPoker ctrlParam = new ControlParameterPoker ("Test", "TestControl1", "Values['one']");
184                         Page page = new Page ();
185                         
186                         OrderedDictionary dict = new OrderedDictionary ();
187                         dict.Add ("one", "1");
188                         
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");
195                 }
196                 
197                 [Test]
198                 [ExpectedException (typeof (ArgumentException))]
199                 public void EvaluateArgumemtException ()
200                 {
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); 
207                 }
208                 [Test]
209                 [ExpectedException (typeof (InvalidOperationException))]
210                 public void EvaluateInvalidOperationException ()
211                 {
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);  
217                 }
218
219
220         }
221 }