2006-11-21 Igor Zelmanovich <igorz@mainsoft.com>
[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 #if NET_2_0
31
32 using System;
33 using System.Collections.Generic;
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     [TestFixture]
99         public class ControlParameterTest
100         {
101                 [Test]
102                 public void ControlParameter_DefaultProperties ()
103                 {
104                         ControlParameterPoker ctrlParam1 = new ControlParameterPoker ();
105                         Assert.AreEqual ("",ctrlParam1.ControlID, "ControlIdDefault");
106                         Assert.AreEqual ("", ctrlParam1.PropertyName, "PropertyNameDefault");
107                         ControlParameterPoker ctrlParam2 = new ControlParameterPoker ("ControlTest","ControlID");
108                         Assert.AreEqual ("ControlID", ctrlParam2.ControlID, "OverloadConstructorControlId1");
109                         Assert.AreEqual ("ControlTest", ctrlParam2.Name, "OverloadConstructorPropertyName1");
110                         ControlParameterPoker ctrlParam3 = new ControlParameterPoker ("Age",TypeCode.Int32,"Label1","Text");
111                         Assert.AreEqual ("Age", ctrlParam3.Name, "OverloadConstructorName2");
112                         Assert.AreEqual (TypeCode.Int32, ctrlParam3.Type, "OverloadConstructorType2");
113                         Assert.AreEqual ("Label1", ctrlParam3.ControlID, "OverloadConstructorControlID2");
114                         Assert.AreEqual ("Text", ctrlParam3.PropertyName, "OverloadConstructorPropertyName2");
115                         ControlParameterPoker ctrlParam4 = new ControlParameterPoker ("Age","Label1","Text");
116                         Assert.AreEqual ("Age", ctrlParam4.Name, "OverloadConstructorName3");                   
117                         Assert.AreEqual ("Label1", ctrlParam4.ControlID, "OverloadConstructorControlID3");
118                         Assert.AreEqual ("Text", ctrlParam4.PropertyName, "OverloadConstructorPropertyName3");
119                         ControlParameterPoker ctrlParam5 = new ControlParameterPoker (ctrlParam3);
120                         Assert.AreEqual ("Age", ctrlParam3.Name, "OverloadConstructorName4");
121                         Assert.AreEqual (TypeCode.Int32, ctrlParam3.Type, "OverloadConstructorType4");
122                         Assert.AreEqual ("Label1", ctrlParam3.ControlID, "OverloadConstructorControlID4");
123                         Assert.AreEqual ("Text", ctrlParam3.PropertyName, "OverloadConstructorPropertyName4");                  
124                  }
125
126                 [Test]
127                 public void ControlParameter_AssignToDefaultProperties ()
128                 {
129                         ControlParameterPoker ctrlParam = new ControlParameterPoker ();
130                         ctrlParam.PropertyName = "Text";
131                         Assert.AreEqual ("Text",ctrlParam.PropertyName ,"AssignToPropertyName");
132                         ctrlParam.ControlID ="Label";
133                         Assert.AreEqual ("Label",ctrlParam.ControlID ,"AssignToPropertyName"); 
134                  }
135
136             //Protected Methods
137
138                 [Test]
139                 public void ControlParameter_Clone ()
140                 {
141                         ControlParameterPoker ctrlParam = new ControlParameterPoker ("Salary", TypeCode.Int64, "TextBox1", "Text");
142                         ControlParameter clonedParam = (ControlParameter)ctrlParam.DoClone ();
143                         Assert.AreEqual ("Salary", clonedParam.Name, "ClonedParamName");
144                         Assert.AreEqual (TypeCode.Int64, clonedParam.Type, "ClonedParamType");
145                         Assert.AreEqual ("TextBox1", clonedParam.ControlID, "ClonedParamControlID");
146                         Assert.AreEqual ("Text", clonedParam.PropertyName, "ClonedParamPropertyName");
147
148                 }
149                 [Test]
150                 public void ControlParameter_Evaluate ()
151                 {
152                         ControlParameterPoker ctrlParam = new ControlParameterPoker ("Salary",TypeCode.Int64,"Label1","Text");
153                         Page page = new Page ();
154                         Label label1 = new Label ();
155                         label1.ID = "Label1";
156                         label1.Text = "2000";
157                         page.Controls.Add (label1);                     
158                         string value=(string)ctrlParam.DoEvaluate (HttpContext.Current,label1);
159                         Assert.AreEqual ("2000", value, "EvaluateValue1");
160                         label1.Text = "TestNewValue";
161                         ctrlParam.Type = TypeCode.String;
162                         value = (string) ctrlParam.DoEvaluate (HttpContext.Current, label1);
163                         Assert.AreEqual ("TestNewValue", value, "EvaluateValue2");
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (ArgumentException))]
168                 public void EvaluateArgumemtException ()
169                 {
170                         ControlParameterPoker ctrlParam = new ControlParameterPoker ();
171                         TextBox textBox1 = new TextBox ();
172                         textBox1.ID = "textbox1";                       
173                         Page page = new Page ();
174                         page.Controls.Add (textBox1); 
175                         ctrlParam.DoEvaluate (HttpContext.Current, textBox1); 
176                 }
177                 [Test]
178                 [ExpectedException (typeof (InvalidOperationException))]
179                 public void EvaluateInvalidOperationException ()
180                 {
181                         ControlParameterPoker ctrlParam = new ControlParameterPoker ("age", "Button", "parameter");
182                         Button b = new Button ();
183                         Page page = new Page ();
184                         page.Controls.Add (b);
185                         ctrlParam.DoEvaluate (HttpContext.Current, b);  
186                 }
187
188
189         }
190 }
191 #endif