[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Common / ColorConsole.cs
1 // ***********************************************************************
2 // Copyright (c) 2014 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 using System;
30
31 namespace NUnit.Common
32 {
33     /// <summary>
34     /// Sets the console color in the constructor and resets it in the dispose
35     /// </summary>
36     public class ColorConsole : IDisposable
37     {
38 #if !SILVERLIGHT && !NETCF
39         private ConsoleColor _originalColor;
40 #endif
41
42         /// <summary>
43         /// Initializes a new instance of the <see cref="ColorConsole"/> class.
44         /// </summary>
45         /// <param name="style">The color style to use.</param>
46         public ColorConsole(ColorStyle style)
47         {
48 #if !SILVERLIGHT && !NETCF
49             _originalColor = Console.ForegroundColor;
50             Console.ForegroundColor = GetColor(style);
51 #endif
52         }
53
54 #if !SILVERLIGHT && !NETCF
55         /// <summary>
56         /// By using styles, we can keep everything consistent
57         /// </summary>
58         /// <param name="style"></param>
59         /// <returns></returns>
60         public static ConsoleColor GetColor(ColorStyle style)
61         {
62             ConsoleColor color = GetColorForStyle(style);
63             ConsoleColor bg = Console.BackgroundColor;
64
65             if (color == bg || color == ConsoleColor.Red && bg == ConsoleColor.Magenta)
66                 return bg == ConsoleColor.Black
67                     ? ConsoleColor.White
68                     : ConsoleColor.Black;
69
70             return color;
71         }
72
73         private static ConsoleColor GetColorForStyle(ColorStyle style)
74         {
75             switch (Console.BackgroundColor)
76             {
77                 case ConsoleColor.White:
78                     switch (style)
79                     {
80                         case ColorStyle.Header:
81                             return ConsoleColor.Black;
82                         case ColorStyle.SubHeader:
83                             return ConsoleColor.Black;
84                         case ColorStyle.SectionHeader:
85                             return ConsoleColor.Blue;
86                         case ColorStyle.Label:
87                             return ConsoleColor.Black;
88                         case ColorStyle.Value:
89                             return ConsoleColor.Blue;
90                         case ColorStyle.Pass:
91                             return ConsoleColor.Green;
92                         case ColorStyle.Failure:
93                             return ConsoleColor.Red;
94                         case ColorStyle.Warning:
95                             return ConsoleColor.Black;
96                         case ColorStyle.Error:
97                             return ConsoleColor.Red;
98                         case ColorStyle.Output:
99                             return ConsoleColor.Black;
100                         case ColorStyle.Help:
101                             return ConsoleColor.Black;
102                         case ColorStyle.Default:
103                         default:
104                             return ConsoleColor.Black;
105                     }
106
107                 case ConsoleColor.Cyan:
108                 case ConsoleColor.Green:
109                 case ConsoleColor.Red:
110                 case ConsoleColor.Magenta:
111                 case ConsoleColor.Yellow:
112                     switch (style)
113                     {
114                         case ColorStyle.Header:
115                             return ConsoleColor.Black;
116                         case ColorStyle.SubHeader:
117                             return ConsoleColor.Black;
118                         case ColorStyle.SectionHeader:
119                             return ConsoleColor.Blue;
120                         case ColorStyle.Label:
121                             return ConsoleColor.Black;
122                         case ColorStyle.Value:
123                             return ConsoleColor.Black;
124                         case ColorStyle.Pass:
125                             return ConsoleColor.Black;
126                         case ColorStyle.Failure:
127                             return ConsoleColor.Red;
128                         case ColorStyle.Warning:
129                             return ConsoleColor.Yellow;
130                         case ColorStyle.Error:
131                             return ConsoleColor.Red;
132                         case ColorStyle.Output:
133                             return ConsoleColor.Black;
134                         case ColorStyle.Help:
135                             return ConsoleColor.Black;
136                         case ColorStyle.Default:
137                         default:
138                             return ConsoleColor.Black;
139                     }
140
141                 default:
142                     switch (style)
143                     {
144                         case ColorStyle.Header:
145                             return ConsoleColor.White;
146                         case ColorStyle.SubHeader:
147                             return ConsoleColor.Gray;
148                         case ColorStyle.SectionHeader:
149                             return ConsoleColor.Cyan;
150                         case ColorStyle.Label:
151                             return ConsoleColor.Green;
152                         case ColorStyle.Value:
153                             return ConsoleColor.White;
154                         case ColorStyle.Pass:
155                             return ConsoleColor.Green;
156                         case ColorStyle.Failure:
157                             return ConsoleColor.Red;
158                         case ColorStyle.Warning:
159                             return ConsoleColor.Yellow;
160                         case ColorStyle.Error:
161                             return ConsoleColor.Red;
162                         case ColorStyle.Output:
163                             return ConsoleColor.Gray;
164                         case ColorStyle.Help:
165                             return ConsoleColor.Green;
166                         case ColorStyle.Default:
167                         default:
168                             return ConsoleColor.Green;
169                     }
170             }
171         }
172 #endif
173
174         #region Implementation of IDisposable
175
176         /// <summary>
177         /// If color is enabled, restores the console colors to their defaults
178         /// </summary>
179         public void Dispose()
180         {
181 #if !SILVERLIGHT && !NETCF
182             Console.ForegroundColor = _originalColor;
183 #endif
184         }
185
186         #endregion
187     }
188 }