[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System / Test / Microsoft.Win32 / SystemEventsCas.cs
1 //
2 // SystemEventsCas.cs - CAS unit tests for Microsoft.Win32.SystemEvents
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30
31 using System;
32 using System.Reflection;
33 using System.Security;
34 using System.Security.Permissions;
35 using Microsoft.Win32;
36
37 namespace MonoCasTests.Microsoft.Win32 {
38
39         [TestFixture]
40         [Category ("CAS")]
41         public class SystemEventsCas {
42
43                 [SetUp]
44                 public virtual void SetUp ()
45                 {
46                         if (!SecurityManager.SecurityEnabled)
47                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
48                 }
49
50                 private void TimerCallback (object o, TimerElapsedEventArgs args)
51                 {
52                 }
53
54                 [Test]
55                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
56                 public void Methods_Deny_Unrestricted ()
57                 {
58                         IntPtr timer = SystemEvents.CreateTimer (5000);
59                         SystemEvents.KillTimer (timer);
60
61                         try {
62                                 SystemEvents.InvokeOnEventsThread (new TimerElapsedEventHandler (TimerCallback));
63                         }
64                         catch (NotImplementedException) {
65                                 // mono
66                         }
67                 }
68
69                 private void EventCallback (object o, EventArgs args)
70                 {
71                 }
72
73                 [Test]
74                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
75                 public void DisplaySettingsChanged_Deny_Unrestricted ()
76                 {
77                         try {
78                                 SystemEvents.DisplaySettingsChanged += new EventHandler (EventCallback);
79                                 SystemEvents.DisplaySettingsChanged -= new EventHandler (EventCallback);
80                         }
81                         catch (NotImplementedException) {
82                                 // mono
83                         }
84                 }
85                 [Test]
86                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
87                 public void DisplaySettingsChanging_Deny_Unrestricted ()
88                 {
89                         try {
90                                 SystemEvents.DisplaySettingsChanging += new EventHandler (EventCallback);
91                                 SystemEvents.DisplaySettingsChanging -= new EventHandler (EventCallback);
92                         }
93                         catch (NotImplementedException) {
94                                 // mono
95                         }
96                 }
97                 [Test]
98                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
99                 public void EventsThreadShutdown_Deny_Unrestricted ()
100                 {
101                         try {
102                                 SystemEvents.EventsThreadShutdown += new EventHandler (EventCallback);
103                                 SystemEvents.EventsThreadShutdown -= new EventHandler (EventCallback);
104                         }
105                         catch (NotImplementedException) {
106                                 // mono
107                         }
108                 }
109
110                 [Test]
111                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
112                 public void InstalledFontsChanged_Deny_Unrestricted ()
113                 {
114                         try {
115                                 SystemEvents.InstalledFontsChanged += new EventHandler (EventCallback);
116                                 SystemEvents.InstalledFontsChanged -= new EventHandler (EventCallback);
117                         }
118                         catch (NotImplementedException) {
119                                 // mono
120                         }
121                 }
122
123                 [Test]
124                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
125                 public void LowMemory_Deny_Unrestricted ()
126                 {
127                         try {
128                                 SystemEvents.LowMemory += new EventHandler (EventCallback);
129                                 SystemEvents.LowMemory -= new EventHandler (EventCallback);
130                         }
131                         catch (NotImplementedException) {
132                                 // mono
133                         }
134                 }
135
136                 [Test]
137                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
138                 public void PaletteChanged_Deny_Unrestricted ()
139                 {
140                         try {
141                                 SystemEvents.PaletteChanged += new EventHandler (EventCallback);
142                                 SystemEvents.PaletteChanged -= new EventHandler (EventCallback);
143                         }
144                         catch (NotImplementedException) {
145                                 // mono
146                         }
147                 }
148
149                 private void PowerModeChangedCallback (object o, PowerModeChangedEventArgs args)
150                 {
151                 }
152
153                 [Test]
154                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
155                 public void PowerModeChanged_Deny_Unrestricted ()
156                 {
157                         try {
158                                 SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler (PowerModeChangedCallback);
159                                 SystemEvents.PowerModeChanged -= new PowerModeChangedEventHandler (PowerModeChangedCallback);
160                         }
161                         catch (NotImplementedException) {
162                                 // mono
163                         }
164                 }
165
166                 private void SessionEndedCallback (object o, SessionEndedEventArgs args)
167                 {
168                 }
169
170                 [Test]
171                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
172                 public void SessionEnded_Deny_Unrestricted ()
173                 {
174                         try {
175                                 SystemEvents.SessionEnded += new SessionEndedEventHandler (SessionEndedCallback);
176                                 SystemEvents.SessionEnded -= new SessionEndedEventHandler (SessionEndedCallback);
177                         }
178                         catch (NotImplementedException) {
179                                 // mono
180                         }
181                 }
182
183                 private void SessionEndingCallback (object o, SessionEndingEventArgs args)
184                 {
185                 }
186
187                 [Test]
188                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
189                 public void SessionEnding_Deny_Unrestricted ()
190                 {
191                         try {
192                                 SystemEvents.SessionEnding += new SessionEndingEventHandler (SessionEndingCallback);
193                                 SystemEvents.SessionEnding -= new SessionEndingEventHandler (SessionEndingCallback);
194                         }
195                         catch (NotImplementedException) {
196                                 // mono
197                         }
198                 }
199                 private void SessionSwitchCallback (object o, SessionSwitchEventArgs args)
200                 {
201                 }
202
203                 [Test]
204                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
205                 public void SessionSwitch_Deny_Unrestricted ()
206                 {
207                         try {
208                                 SystemEvents.SessionSwitch += new SessionSwitchEventHandler (SessionSwitchCallback);
209                                 SystemEvents.SessionSwitch -= new SessionSwitchEventHandler (SessionSwitchCallback);
210                         }
211                         catch (NotImplementedException) {
212                                 // mono
213                         }
214                 }
215                 [Test]
216                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
217                 public void TimeChanged_Deny_Unrestricted ()
218                 {
219                         try {
220                                 SystemEvents.TimeChanged += new EventHandler (EventCallback);
221                                 SystemEvents.TimeChanged -= new EventHandler (EventCallback);
222                         }
223                         catch (NotImplementedException) {
224                                 // mono
225                         }
226                 }
227
228                 private void TimerElapsedCallback (object o, TimerElapsedEventArgs args)
229                 {
230                 }
231
232                 [Test]
233                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
234                 public void TimerElapsed_Deny_Unrestricted ()
235                 {
236                         SystemEvents.TimerElapsed += new TimerElapsedEventHandler (TimerElapsedCallback);
237                         SystemEvents.TimerElapsed -= new TimerElapsedEventHandler (TimerElapsedCallback);
238                 }
239
240                 private void UserPreferenceChangedCallback (object o, UserPreferenceChangedEventArgs args)
241                 {
242                 }
243
244                 [Test]
245                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
246                 public void UserPreferenceChanged_Deny_Unrestricted ()
247                 {
248                         SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
249                         SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler (UserPreferenceChangedCallback);
250                 }
251
252                 private void UserPreferenceChangingCallback (object o, UserPreferenceChangingEventArgs args)
253                 {
254                 }
255
256                 [Test]
257                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
258                 public void UserPreferenceChanging_Deny_Unrestricted ()
259                 {
260                         try {
261                                 SystemEvents.UserPreferenceChanging += new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
262                                 SystemEvents.UserPreferenceChanging -= new UserPreferenceChangingEventHandler (UserPreferenceChangingCallback);
263                         }
264                         catch (NotImplementedException) {
265                                 // mono
266                         }
267                 }
268
269                 // LinkDemand
270
271                 // we use reflection to call this class as it is protected by a LinkDemand 
272                 // (which will be converted into full demand, i.e. a stack walk) when 
273                 // reflection is used (i.e. it gets testable).
274
275                 public virtual object Create ()
276                 {
277                         MethodInfo mi = typeof (SystemEvents).GetMethod ("CreateTimer");
278                         Assert.IsNotNull (mi, "CreateTimer");
279                         return mi.Invoke (null, new object[1] { 5000 });
280                 }
281
282                 [Test]
283                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
284                 [ExpectedException (typeof (SecurityException))]
285                 public void LinkDemand_Deny_Unrestricted ()
286                 {
287                         Assert.IsNotNull (Create ());
288                 }
289
290                 [Test]
291                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
292                 [ExpectedException (typeof (SecurityException))]
293                 public void LinkDemand_Deny_Anything ()
294                 {
295                         // denying any permissions -> not full trust!
296                         Assert.IsNotNull (Create ());
297                 }
298
299                 [Test]
300                 [PermissionSet (SecurityAction.PermitOnly, Unrestricted = true)]
301                 public void LinkDemand_PermitOnly_Unrestricted ()
302                 {
303                         Assert.IsNotNull (Create ());
304                 }
305         }
306 }