[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / PlatformHelper.cs
1 // ***********************************************************************
2 // Copyright (c) 2007-2016 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 #if !PORTABLE
30 using System;
31 using System.Linq;
32
33 namespace NUnit.Framework.Internal
34 {
35     /// <summary>
36     /// PlatformHelper class is used by the PlatformAttribute class to 
37     /// determine whether a platform is supported.
38     /// </summary>
39     public class PlatformHelper
40     {
41         private readonly OSPlatform _os;
42         private readonly RuntimeFramework _rt;
43
44         // Set whenever we fail to support a list of platforms
45         private string _reason = string.Empty;
46
47         const string CommonOSPlatforms =
48             "Win,Win32,Win32S,Win32NT,Win32Windows,WinCE,Win95,Win98,WinMe,NT3,NT4,NT5,NT6," +
49             "Win2008Server,Win2008ServerR2,Win2012Server,Win2012ServerR2," +
50             "Win2K,WinXP,Win2003Server,Vista,Win7,Windows7,Win8,Windows8,"+
51             "Win8.1,Windows8.1,Win10,Windows10,WindowsServer10,Unix,Linux";
52
53         /// <summary>
54         /// Comma-delimited list of all supported OS platform constants
55         /// </summary>
56 #if NETCF
57         public const string OSPlatforms = CommonOSPlatforms;
58 #else
59         public const string OSPlatforms = CommonOSPlatforms + ",Xbox,MacOSX";
60 #endif
61
62         /// <summary>
63         /// Comma-delimited list of all supported Runtime platform constants
64         /// </summary>
65         public static readonly string RuntimePlatforms =
66             "Net,NetCF,SSCLI,Rotor,Mono,MonoTouch";
67
68         /// <summary>
69         /// Default constructor uses the operating system and
70         /// common language runtime of the system.
71         /// </summary>
72         public PlatformHelper()
73         {
74             _os = OSPlatform.CurrentPlatform;
75             _rt = RuntimeFramework.CurrentFramework;
76         }
77
78         /// <summary>
79         /// Construct a PlatformHelper for a particular operating
80         /// system and common language runtime. Used in testing.
81         /// </summary>
82         /// <param name="os">OperatingSystem to be used</param>
83         /// <param name="rt">RuntimeFramework to be used</param>
84         public PlatformHelper( OSPlatform os, RuntimeFramework rt )
85         {
86             _os = os;
87             _rt = rt;
88         }
89
90         /// <summary>
91         /// Test to determine if one of a collection of platforms
92         /// is being used currently.
93         /// </summary>
94         /// <param name="platforms"></param>
95         /// <returns></returns>
96         public bool IsPlatformSupported( string[] platforms )
97         {
98             return platforms.Any( IsPlatformSupported );
99         }
100
101         /// <summary>
102         /// Tests to determine if the current platform is supported
103         /// based on a platform attribute.
104         /// </summary>
105         /// <param name="platformAttribute">The attribute to examine</param>
106         /// <returns></returns>
107         public bool IsPlatformSupported( PlatformAttribute platformAttribute )
108         {
109             string include = platformAttribute.Include;
110             string exclude = platformAttribute.Exclude;
111
112             return IsPlatformSupported( include, exclude );
113         }
114
115         /// <summary>
116         /// Tests to determine if the current platform is supported
117         /// based on a platform attribute.
118         /// </summary>
119         /// <param name="testCaseAttribute">The attribute to examine</param>
120         /// <returns></returns>
121         public bool IsPlatformSupported(TestCaseAttribute testCaseAttribute)
122         {
123             string include = testCaseAttribute.IncludePlatform;
124             string exclude = testCaseAttribute.ExcludePlatform;
125
126             return IsPlatformSupported(include, exclude);
127         }
128
129         private bool IsPlatformSupported(string include, string exclude)
130         {
131             try
132             {
133                 if (include != null && !IsPlatformSupported(include))
134                 {
135                     _reason = string.Format("Only supported on {0}", include);
136                     return false;
137                 }
138
139                 if (exclude != null && IsPlatformSupported(exclude))
140                 {
141                     _reason = string.Format("Not supported on {0}", exclude);
142                     return false;
143                 }
144             }
145             catch (Exception ex)
146             {
147                 _reason = ex.Message;
148                 return false;
149             }
150
151             return true;
152         }
153
154         /// <summary>
155         /// Test to determine if the a particular platform or comma-
156         /// delimited set of platforms is in use.
157         /// </summary>
158         /// <param name="platform">Name of the platform or comma-separated list of platform ids</param>
159         /// <returns>True if the platform is in use on the system</returns>
160         public bool IsPlatformSupported( string platform )
161         {
162             if ( platform.IndexOf( ',' ) >= 0 )
163                 return IsPlatformSupported( platform.Split(',') );
164
165             string platformName = platform.Trim();
166             bool isSupported;
167
168 //                      string versionSpecification = null;
169 //
170 //                      string[] parts = platformName.Split( new char[] { '-' } );
171 //                      if ( parts.Length == 2 )
172 //                      {
173 //                              platformName = parts[0];
174 //                              versionSpecification = parts[1];
175 //                      }
176
177             switch( platformName.ToUpper() )
178             {
179                 case "WIN":
180                 case "WIN32":
181                     isSupported = _os.IsWindows;
182                     break;
183                 case "WIN32S":
184                     isSupported = _os.IsWin32S;
185                     break;
186                 case "WIN32WINDOWS":
187                     isSupported = _os.IsWin32Windows;
188                     break;
189                 case "WIN32NT":
190                     isSupported = _os.IsWin32NT;
191                     break;
192                 case "WINCE":
193                     isSupported = _os.IsWinCE;
194                     break;
195                 case "WIN95":
196                     isSupported = _os.IsWin95;
197                     break;
198                 case "WIN98":
199                     isSupported = _os.IsWin98;
200                     break;
201                 case "WINME":
202                     isSupported = _os.IsWinME;
203                     break;
204                 case "NT3":
205                     isSupported = _os.IsNT3;
206                     break;
207                 case "NT4":
208                     isSupported = _os.IsNT4;
209                     break;
210                 case "NT5":
211                     isSupported = _os.IsNT5;
212                     break;
213                 case "WIN2K":
214                     isSupported = _os.IsWin2K;
215                     break;
216                 case "WINXP":
217                     isSupported = _os.IsWinXP;
218                     break;
219                 case "WIN2003SERVER":
220                     isSupported = _os.IsWin2003Server;
221                     break;
222                 case "NT6":
223                     isSupported = _os.IsNT6;
224                     break;
225                 case "VISTA":
226                     isSupported = _os.IsVista;
227                     break;
228                 case "WIN2008SERVER":
229                     isSupported = _os.IsWin2008Server;
230                     break;
231                 case "WIN2008SERVERR2":
232                     isSupported = _os.IsWin2008ServerR2;
233                     break;
234                 case "WIN2012SERVER":
235                     isSupported = _os.IsWin2012ServerR1 || _os.IsWin2012ServerR2;
236                     break;
237                 case "WIN2012SERVERR2":
238                     isSupported = _os.IsWin2012ServerR2;
239                     break;
240                 case "WIN7":
241                 case "WINDOWS7":
242                     isSupported = _os.IsWindows7;
243                     break;
244                 case "WINDOWS8":
245                 case "WIN8":
246                     isSupported = _os.IsWindows8;
247                     break;
248                 case "WINDOWS8.1":
249                 case "WIN8.1":
250                     isSupported = _os.IsWindows81;
251                     break;
252                 case "WINDOWS10":
253                 case "WIN10":
254                     isSupported = _os.IsWindows10;
255                     break;
256                 case "WINDOWSSERVER10":
257                     isSupported = _os.IsWindowsServer10;
258                     break;
259                 case "UNIX":
260                 case "LINUX":
261                     isSupported = _os.IsUnix;
262                     break;
263                 case "XBOX":
264                     isSupported = _os.IsXbox;
265                     break;
266                 case "MACOSX":
267                     isSupported = _os.IsMacOSX;
268                     break;
269                 // These bitness tests relate to the process, not the OS.
270                 // We can't use Environment.Is64BitProcess because it's
271                 // only supported in NET 4.0 and higher.
272                 case "64-BIT":
273                 case "64-BIT-PROCESS":
274                     isSupported = IntPtr.Size == 8;
275                     break;
276                 case "32-BIT":
277                 case "32-BIT-PROCESS":
278                     isSupported = IntPtr.Size == 4;
279                     break;
280
281 #if NET_4_0 || NET_4_5
282                 // We only support bitness tests of the OS in .NET 4.0 and up
283                 case "64-BIT-OS":
284                     isSupported = Environment.Is64BitOperatingSystem;
285                     break;
286                 case "32-BIT-OS":
287                     isSupported = !Environment.Is64BitOperatingSystem;
288                     break;
289 #endif
290
291                 default:
292                     isSupported = IsRuntimeSupported(platformName);
293                     break;
294             }
295
296             if (!isSupported)
297                 _reason = "Only supported on " + platform;
298
299             return isSupported;
300         }
301
302         /// <summary>
303         /// Return the last failure reason. Results are not
304         /// defined if called before IsSupported( Attribute )
305         /// is called.
306         /// </summary>
307         public string Reason
308         {
309             get { return _reason; }
310         }
311
312         private bool IsRuntimeSupported(string platformName)
313         {
314             string versionSpecification = null;
315             string[] parts = platformName.Split('-');
316             if (parts.Length == 2)
317             {
318                 platformName = parts[0];
319                 versionSpecification = parts[1];
320             }
321
322             switch (platformName.ToUpper())
323             {
324                 case "NET":
325                     return IsRuntimeSupported(RuntimeType.Net, versionSpecification);
326
327                 case "NETCF":
328                     return IsRuntimeSupported(RuntimeType.NetCF, versionSpecification);
329
330                 case "SSCLI":
331                 case "ROTOR":
332                     return IsRuntimeSupported(RuntimeType.SSCLI, versionSpecification);
333
334                 case "MONO":
335                     return IsRuntimeSupported(RuntimeType.Mono, versionSpecification);
336
337                 case "SL":
338                 case "SILVERLIGHT":
339                     return IsRuntimeSupported(RuntimeType.Silverlight, versionSpecification);
340
341                 case "MONOTOUCH":
342                     return IsRuntimeSupported(RuntimeType.MonoTouch, versionSpecification);
343
344                 default:
345                     throw new ArgumentException("Invalid platform name", platformName);
346             }
347         }
348
349         private bool IsRuntimeSupported(RuntimeType runtime, string versionSpecification)
350         {
351             Version version = versionSpecification == null
352                 ? RuntimeFramework.DefaultVersion
353                 : new Version(versionSpecification);
354
355             RuntimeFramework target = new RuntimeFramework(runtime, version);
356
357             return _rt.Supports(target);
358         }
359     }
360 }
361 #endif