[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / RuntimeFramework.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.Collections.Generic;
32 using System.IO;
33 using System.Linq;
34 using System.Reflection;
35 using Microsoft.Win32;
36
37 namespace NUnit.Framework.Internal
38 {
39     /// <summary>
40     /// Enumeration identifying a common language
41     /// runtime implementation.
42     /// </summary>
43     public enum RuntimeType
44     {
45         /// <summary>Any supported runtime framework</summary>
46         Any,
47         /// <summary>Microsoft .NET Framework</summary>
48         Net,
49         /// <summary>Microsoft .NET Compact Framework</summary>
50         NetCF,
51         /// <summary>Microsoft Shared Source CLI</summary>
52         SSCLI,
53         /// <summary>Mono</summary>
54         Mono,
55         /// <summary>Silverlight</summary>
56         Silverlight,
57         /// <summary>MonoTouch</summary>
58         MonoTouch
59     }
60
61     /// <summary>
62     /// RuntimeFramework represents a particular version
63     /// of a common language runtime implementation.
64     /// </summary>
65     [Serializable]
66     public sealed class RuntimeFramework
67     {
68         // NOTE: This version of RuntimeFramework is for use
69         // within the NUnit framework assembly. It is simpler
70         // than the version in the test engine because it does
71         // not need to know what frameworks are available,
72         // only what framework is currently running.
73 #region Static and Instance Fields
74
75         /// <summary>
76         /// DefaultVersion is an empty Version, used to indicate that
77         /// NUnit should select the CLR version to use for the test.
78         /// </summary>
79         public static readonly Version DefaultVersion = new Version(0,0);
80
81         private static readonly Lazy<RuntimeFramework> currentFramework = new Lazy<RuntimeFramework>(() =>
82         {
83 #if SILVERLIGHT
84             var currentFramework = new RuntimeFramework(
85                 RuntimeType.Silverlight,
86                 new Version(Environment.Version.Major, Environment.Version.Minor));
87 #else
88             Type monoRuntimeType = Type.GetType("Mono.Runtime", false);
89             Type monoTouchType = Type.GetType("MonoTouch.UIKit.UIApplicationDelegate,monotouch");
90             bool isMonoTouch = monoTouchType != null;
91             bool isMono = monoRuntimeType != null;
92
93             RuntimeType runtime = isMonoTouch
94                 ? RuntimeType.MonoTouch
95                 : isMono
96                     ? RuntimeType.Mono
97                     : Environment.OSVersion.Platform == PlatformID.WinCE
98                         ? RuntimeType.NetCF
99                         : RuntimeType.Net;
100
101             int major = Environment.Version.Major;
102             int minor = Environment.Version.Minor;
103
104             if (isMono)
105             {
106                 switch (major)
107                 {
108                     case 1:
109                         minor = 0;
110                         break;
111                     case 2:
112                         major = 3;
113                         minor = 5;
114                         break;
115                 }
116             }
117             else /* It's windows */
118             if (major == 2)
119             {
120                 using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework"))
121                 {
122                     if (key != null)
123                     {
124                         string installRoot = key.GetValue("InstallRoot") as string;
125                         if (installRoot != null)
126                         {
127                             if (Directory.Exists(Path.Combine(installRoot, "v3.5")))
128                             {
129                                 major = 3;
130                                 minor = 5;
131                             }
132                             else if (Directory.Exists(Path.Combine(installRoot, "v3.0")))
133                             {
134                                 major = 3;
135                                 minor = 0;
136                             }
137                         }
138                     }
139                 }
140             }
141             else if (major == 4 && Type.GetType("System.Reflection.AssemblyMetadataAttribute") != null)
142             {
143                 minor = 5;
144             }
145
146             var currentFramework = new RuntimeFramework( runtime, new Version (major, minor) )
147             {
148                 ClrVersion = Environment.Version
149             };
150
151             if (isMono)
152             {
153                 MethodInfo getDisplayNameMethod = monoRuntimeType.GetMethod(
154                     "GetDisplayName", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding);
155                 if (getDisplayNameMethod != null)
156                     currentFramework.DisplayName = (string)getDisplayNameMethod.Invoke(null, new object[0]);
157             }
158 #endif
159             return currentFramework;
160         });
161
162 #endregion
163
164 #region Constructor
165
166         /// <summary>
167         /// Construct from a runtime type and version. If the version has
168         /// two parts, it is taken as a framework version. If it has three
169         /// or more, it is taken as a CLR version. In either case, the other
170         /// version is deduced based on the runtime type and provided version.
171         /// </summary>
172         /// <param name="runtime">The runtime type of the framework</param>
173         /// <param name="version">The version of the framework</param>
174         public RuntimeFramework( RuntimeType runtime, Version version)
175         {
176             Runtime = runtime;
177
178             if (version.Build < 0)
179                 InitFromFrameworkVersion(version);
180             else
181                 InitFromClrVersion(version);
182
183             DisplayName = GetDefaultDisplayName(runtime, version);
184         }
185
186         private void InitFromFrameworkVersion(Version version)
187         {
188             FrameworkVersion = ClrVersion = version;
189
190             if (version.Major > 0) // 0 means any version
191                 switch (Runtime)
192                 {
193                     case RuntimeType.Net:
194                     case RuntimeType.Mono:
195                     case RuntimeType.Any:
196                         switch (version.Major)
197                         {
198                             case 1:
199                                 switch (version.Minor)
200                                 {
201                                     case 0:
202                                         ClrVersion = Runtime == RuntimeType.Mono
203                                             ? new Version(1, 1, 4322)
204                                             : new Version(1, 0, 3705);
205                                         break;
206                                     case 1:
207                                         if (Runtime == RuntimeType.Mono)
208                                             FrameworkVersion = new Version(1, 0);
209                                         ClrVersion = new Version(1, 1, 4322);
210                                         break;
211                                     default:
212                                         ThrowInvalidFrameworkVersion(version);
213                                         break;
214                                 }
215                                 break;
216                             case 2:
217                             case 3:
218                                 ClrVersion = new Version(2, 0, 50727);
219                                 break;
220                             case 4:
221                                 ClrVersion = new Version(4, 0, 30319);
222                                 break;
223                             default:
224                                 ThrowInvalidFrameworkVersion(version);
225                                 break;
226                         }
227                         break;
228
229                     case RuntimeType.Silverlight:
230                         ClrVersion =  version.Major >= 4
231                             ? new Version(4, 0, 60310)
232                             : new Version(2, 0, 50727);
233                         break;
234
235                     case RuntimeType.NetCF:
236                         switch (version.Major)
237                         {
238                             case 3:
239                                 switch (version.Minor)
240                                 {
241                                     case 5:
242                                         ClrVersion = new Version(3, 5, 7283);
243                                         break;
244                                 }
245                                 break;
246                         }
247                         break;
248                 }
249         }
250
251         private static void ThrowInvalidFrameworkVersion(Version version)
252         {
253             throw new ArgumentException("Unknown framework version " + version, "version");
254         }
255
256         private void InitFromClrVersion(Version version)
257         {
258             FrameworkVersion = new Version(version.Major, version.Minor);
259             ClrVersion = version;
260             if (Runtime == RuntimeType.Mono && version.Major == 1)
261                 FrameworkVersion = new Version(1, 0);
262         }
263
264 #endregion
265
266 #region Properties
267         /// <summary>
268         /// Static method to return a RuntimeFramework object
269         /// for the framework that is currently in use.
270         /// </summary>
271         public static RuntimeFramework CurrentFramework
272         {
273             get
274             {
275                 return currentFramework.Value;
276             }
277         }
278
279         /// <summary>
280         /// The type of this runtime framework
281         /// </summary>
282         public RuntimeType Runtime { get; private set; }
283
284         /// <summary>
285         /// The framework version for this runtime framework
286         /// </summary>
287         public Version FrameworkVersion { get; private set; }
288
289         /// <summary>
290         /// The CLR version for this runtime framework
291         /// </summary>
292         public Version ClrVersion { get; private set; }
293
294         /// <summary>
295         /// Return true if any CLR version may be used in
296         /// matching this RuntimeFramework object.
297         /// </summary>
298         public bool AllowAnyVersion
299         {
300             get { return ClrVersion == DefaultVersion; }
301         }
302
303         /// <summary>
304         /// Returns the Display name for this framework
305         /// </summary>
306         public string DisplayName { get; private set; }
307
308 #endregion
309
310 #region Public Methods
311
312         /// <summary>
313         /// Parses a string representing a RuntimeFramework.
314         /// The string may be just a RuntimeType name or just
315         /// a Version or a hyphenated RuntimeType-Version or
316         /// a Version prefixed by 'versionString'.
317         /// </summary>
318         /// <param name="s"></param>
319         /// <returns></returns>
320         public static RuntimeFramework Parse(string s)
321         {
322             RuntimeType runtime = RuntimeType.Any;
323             Version version = DefaultVersion;
324
325             string[] parts = s.Split('-');
326             if (parts.Length == 2)
327             {
328                 runtime = (RuntimeType)Enum.Parse(typeof(RuntimeType), parts[0], true);
329                 string vstring = parts[1];
330                 if (vstring != "")
331                     version = new Version(vstring);
332             }
333             else if (char.ToLower(s[0]) == 'v')
334             {
335                 version = new Version(s.Substring(1));
336             }
337             else if (IsRuntimeTypeName(s))
338             {
339                 runtime = (RuntimeType)Enum.Parse(typeof(RuntimeType), s, true);
340             }
341             else
342             {
343                 version = new Version(s);
344             }
345
346             return new RuntimeFramework(runtime, version);
347         }
348
349         /// <summary>
350         /// Overridden to return the short name of the framework
351         /// </summary>
352         /// <returns></returns>
353         public override string ToString()
354         {
355             if (AllowAnyVersion)
356             {
357                 return Runtime.ToString().ToLower();
358             }
359             else
360             {
361                 string vstring = FrameworkVersion.ToString();
362                 if (Runtime == RuntimeType.Any)
363                     return "v" + vstring;
364                 else
365                     return Runtime.ToString().ToLower() + "-" + vstring;
366             }
367         }
368
369         /// <summary>
370         /// Returns true if the current framework matches the
371         /// one supplied as an argument. Two frameworks match
372         /// if their runtime types are the same or either one
373         /// is RuntimeType.Any and all specified version components
374         /// are equal. Negative (i.e. unspecified) version
375         /// components are ignored.
376         /// </summary>
377         /// <param name="target">The RuntimeFramework to be matched.</param>
378         /// <returns>True on match, otherwise false</returns>
379         public bool Supports(RuntimeFramework target)
380         {
381             if (Runtime != RuntimeType.Any
382                 && target.Runtime != RuntimeType.Any
383                 && Runtime != target.Runtime)
384                 return false;
385
386             if (AllowAnyVersion || target.AllowAnyVersion)
387                 return true;
388
389             if (!VersionsMatch(ClrVersion, target.ClrVersion))
390                 return false;
391
392             return Runtime == RuntimeType.Silverlight
393                 ? FrameworkVersion.Major == target.FrameworkVersion.Major && FrameworkVersion.Minor == target.FrameworkVersion.Minor
394                 : FrameworkVersion.Major >= target.FrameworkVersion.Major && FrameworkVersion.Minor >= target.FrameworkVersion.Minor;
395         }
396
397 #endregion
398
399 #region Helper Methods
400
401         private static bool IsRuntimeTypeName(string name)
402         {
403             return TypeHelper.GetEnumNames( typeof(RuntimeType)).Any( item => item.ToLower() == name.ToLower() );
404         }
405
406         private static string GetDefaultDisplayName(RuntimeType runtime, Version version)
407         {
408             if (version == DefaultVersion)
409                 return runtime.ToString();
410             else if (runtime == RuntimeType.Any)
411                 return "v" + version;
412             else
413                 return runtime + " " + version;
414         }
415
416         private static bool VersionsMatch(Version v1, Version v2)
417         {
418             return v1.Major == v2.Major &&
419                    v1.Minor == v2.Minor &&
420                   (v1.Build < 0 || v2.Build < 0 || v1.Build == v2.Build) &&
421                   (v1.Revision < 0 || v2.Revision < 0 || v1.Revision == v2.Revision);
422         }
423
424 #endregion
425     }
426 }
427 #endif