Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / System / Test / System.ComponentModel / LicenseManagerTests.cs
1 //
2 // System.ComponentModel.LicenseManagerTests test cases
3 //
4 // Authors:
5 //      Ivan Hamilton (ivan@chimerical.com.au)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Martin Willemoes Hansen (mwh@sysrq.dk)
8 //
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2003 Martin Willemoes Hansen
11 // (c) 2004 Ivan Hamilton
12
13 #if !MOBILE
14
15 using NUnit.Framework;
16 using System;
17 using System.ComponentModel;
18 using System.ComponentModel.Design;
19
20 namespace MonoTests.System.ComponentModel 
21 {
22         public class UnlicensedObject 
23         {
24         }
25
26         [LicenseProvider (typeof (TestLicenseProvider))]
27         public class LicensedObject
28         {
29         }
30
31         [LicenseProvider (typeof (TestLicenseProvider))]
32         public class InvalidLicensedObject
33         {
34         }
35
36         [LicenseProvider (typeof (TestLicenseProvider))]
37         public class RuntimeLicensedObject
38         {
39                 public RuntimeLicensedObject () 
40                 {
41                         LicenseManager.Validate (typeof (RuntimeLicensedObject));
42                 }
43                 public RuntimeLicensedObject (int a): this () {}
44         }
45
46         [LicenseProvider (typeof (TestLicenseProvider))]
47         public class DesigntimeLicensedObject
48         {
49                 public DesigntimeLicensedObject () 
50                 {
51                         LicenseManager.Validate (typeof (DesigntimeLicensedObject));
52                 }
53         }
54
55         public class TestLicenseProvider : LicenseProvider 
56         {
57
58                 private class TestLicense : License 
59                 {
60                         public override void Dispose ()
61                         {
62                         }
63
64                         public override string LicenseKey 
65                         {
66                                 get { return "YourLicenseKey"; }
67                         }
68                 }
69
70                 public TestLicenseProvider () : base ()
71                 {
72                 }
73
74                 public override License GetLicense (LicenseContext context,
75                         Type type,
76                         object instance,
77                         bool allowExceptions)
78                 {
79                         if (type.Name.Equals ("RuntimeLicensedObject")) {
80                                 if (context.UsageMode != LicenseUsageMode.Runtime)
81                                         if (allowExceptions)
82                                                 throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
83                                         else
84                                                 return null;
85                                 return new TestLicense ();
86                         }
87
88                         if (type.Name.Equals ("DesigntimeLicensedObject")) 
89                         {
90                                 if (context.UsageMode != LicenseUsageMode.Designtime)
91                                         if (allowExceptions)
92                                                 throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
93                                         else
94                                                 return null;
95                                 return new TestLicense ();
96                         }
97
98                         if (type.Name.Equals ("LicensedObject"))
99                                 return new TestLicense ();
100
101                         if (allowExceptions)
102                                 throw new LicenseException (type, instance, "License fails because of class name.");
103                         else
104                                 return null;
105                 }
106         }
107
108
109         [TestFixture]
110         public class LicenseManagerTests
111         {
112                 [SetUp]
113                 public void GetReady ()
114                 {
115                 }
116
117                 [Test]
118                 public void Test () 
119                 {
120                         object lockObject = new object ();
121                         //**DEFAULT CONTEXT & LicenseUsageMode**
122                         //Get CurrentContext, check default type
123                         Assert.AreEqual ("System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString(), "LicenseManager #1");
124                         //Read default LicenseUsageMode, check against CurrentContext (LicCont).UsageMode
125                         Assert.AreEqual (LicenseManager.CurrentContext.UsageMode, LicenseManager.UsageMode, "LicenseManager #2");
126
127                         //**CHANGING CONTEXT**
128                         //Change the context and check it changes
129                         LicenseContext oldcontext = LicenseManager.CurrentContext;
130                         LicenseContext newcontext = new DesigntimeLicenseContext();
131                         LicenseManager.CurrentContext = newcontext;
132                         Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #3");
133                         //Check the UsageMode changed too
134                         Assert.AreEqual (newcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #4");
135                         //Set Context back to original
136                         LicenseManager.CurrentContext = oldcontext;
137                         //Check it went back
138                         Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #5");
139                         //Check the UsageMode changed too
140                         Assert.AreEqual (oldcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #6");
141
142                         //**CONTEXT LOCKING**
143                         //Lock the context
144                         LicenseManager.LockContext(lockObject);
145                         //Try and set new context again, should throw System.InvalidOperationException: The CurrentContext property of the LicenseManager is currently locked and cannot be changed.
146                         bool exceptionThrown = false;
147                         try 
148                         {
149                                 LicenseManager.CurrentContext=newcontext;
150                         } 
151                         catch (Exception e) 
152                         {
153                                 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(), "LicenseManager #7");
154                                 exceptionThrown = true;
155                         } 
156                         //Check the exception was thrown
157                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #8");
158                         //Check the context didn't change
159                         Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #9");
160                         //Unlock it
161                         LicenseManager.UnlockContext(lockObject);
162                         //Now's unlocked, change it
163                         LicenseManager.CurrentContext=newcontext;
164                         Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #10");
165                         //Change it back
166                         LicenseManager.CurrentContext = oldcontext;
167
168
169                         //Lock the context
170                         LicenseManager.LockContext(lockObject);
171                         //Unlock with different "user" should throw System.ArgumentException: The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.
172                         object wrongLockObject = new object();
173                         exceptionThrown = false;
174                         try 
175                         {
176                                 LicenseManager.UnlockContext(wrongLockObject);
177                         } 
178                         catch (Exception e) 
179                         {
180                                 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "LicenseManager #11");
181                                 exceptionThrown = true;
182                         } 
183                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #12");
184                         //Unlock it
185                         LicenseManager.UnlockContext(lockObject);
186
187                         //** bool IsValid(Type);
188                         Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (UnlicensedObject)), "LicenseManager #13");
189                         Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (LicensedObject)), "LicenseManager #14");
190                         Assert.AreEqual (false, LicenseManager.IsLicensed (typeof (InvalidLicensedObject)), "LicenseManager #15");
191
192                         Assert.AreEqual (true, LicenseManager.IsValid (typeof (UnlicensedObject)), "LicenseManager #16");
193                         Assert.AreEqual (true, LicenseManager.IsValid (typeof (LicensedObject)), "LicenseManager #17");
194                         Assert.AreEqual (false, LicenseManager.IsValid (typeof (InvalidLicensedObject)), "LicenseManager #18");
195
196                         UnlicensedObject unlicensedObject = new UnlicensedObject ();
197                         LicensedObject licensedObject = new LicensedObject ();
198                         InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();
199
200                         //** bool IsValid(Type, object, License);
201                         License license=null;
202                         Assert.AreEqual (true, LicenseManager.IsValid (unlicensedObject.GetType (), unlicensedObject, out license), "LicenseManager #19");
203                         Assert.AreEqual (null, license, "LicenseManager #20");
204
205                         license=null;
206                         Assert.AreEqual (true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license), "LicenseManager #21");
207                         Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #22");
208
209                         license=null;
210                         Assert.AreEqual (false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license), "LicenseManager #23");
211                         Assert.AreEqual (null, license, "LicenseManager #24");
212
213                         //** void Validate(Type);
214                         //Shouldn't throw exception
215                         LicenseManager.Validate (typeof (UnlicensedObject));
216                         //Shouldn't throw exception
217                         LicenseManager.Validate (typeof (LicensedObject));
218                         //Should throw exception
219                         exceptionThrown=false;
220                         try 
221                         {
222                                 LicenseManager.Validate(typeof(InvalidLicensedObject));
223                         } 
224                         catch (Exception e) 
225                         {
226                                 Assert.AreEqual (typeof(LicenseException), e.GetType(), "LicenseManager #25");
227                                 exceptionThrown=true;
228                         } 
229                         //Check the exception was thrown
230                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #26");
231
232                         //** License Validate(Type, object);
233                         //Shouldn't throw exception, returns null license
234                         license=LicenseManager.Validate (typeof (UnlicensedObject), unlicensedObject);
235                         Assert.AreEqual (null, license, "LicenseManager #27");
236
237                         //Shouldn't throw exception, returns TestLicense license
238                         license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
239                         Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #28");
240
241                         //Should throw exception, returns null license
242                         exceptionThrown=false;
243                         try 
244                         {
245                                 license=null;
246                                 license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
247                         } 
248                         catch (Exception e) 
249                         {
250                                 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #29");
251                                 exceptionThrown=true;
252                         } 
253                         //Check the exception was thrown
254                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #30");
255                         Assert.AreEqual (null, license, "LicenseManager #31");
256
257
258                         //** object CreateWithContext (Type, LicenseContext);
259                         object cwc = null;
260                         //Test we can create an unlicensed object with no context
261                         cwc = LicenseManager.CreateWithContext (typeof (UnlicensedObject), null);
262                         Assert.AreEqual ("UnlicensedObject", cwc.GetType ().Name, "LicenseManager #32");
263                         //Test we can create RunTime with CurrentContext (runtime)
264                         cwc = null;
265                         cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
266                                 LicenseManager.CurrentContext);
267                         Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #33");
268                         //Test we can't create DesignTime with CurrentContext (runtime)
269                         exceptionThrown=false;
270                         try 
271                         {
272                                 cwc = null;
273                                 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
274                         } 
275                         catch (Exception e) 
276                         {
277                                 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #34");
278                                 exceptionThrown=true;
279                         } 
280                         //Check the exception was thrown
281                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #35");
282                         //Test we can create DesignTime with A new DesignTimeContext 
283                         cwc = null;
284                         cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
285                                 new DesigntimeLicenseContext ());
286                         Assert.AreEqual ("DesigntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #36");
287
288                         //** object CreateWithContext(Type, LicenseContext, object[]);
289                         //Test we can create RunTime with CurrentContext (runtime)
290                         cwc = null;
291                         cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
292                                 LicenseManager.CurrentContext, new object [] { 7 });
293                         Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #37");
294
295                 }
296         }
297 }
298
299 #endif