[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / TestProgressReporter.cs
1 // ***********************************************************************
2 // Copyright (c) 2010 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 using System.Web.UI;
31 using NUnit.Framework.Interfaces;
32
33 namespace NUnit.Framework.Internal
34 {
35     /// <summary>
36     /// TestProgressReporter translates ITestListener events into
37     /// the async callbacks that are used to inform the client
38     /// software about the progress of a test run.
39     /// </summary>
40     public class TestProgressReporter : ITestListener
41     {
42         static Logger log = InternalTrace.GetLogger("TestProgressReporter");
43
44         private ICallbackEventHandler handler;
45
46         /// <summary>
47         /// Initializes a new instance of the <see cref="TestProgressReporter"/> class.
48         /// </summary>
49         /// <param name="handler">The callback handler to be used for reporting progress.</param>
50         public TestProgressReporter(ICallbackEventHandler handler)
51         {
52             this.handler = handler;
53         }
54
55         #region ITestListener Members
56
57         /// <summary>
58         /// Called when a test has just started
59         /// </summary>
60         /// <param name="test">The test that is starting</param>
61         public void TestStarted(ITest test)
62         {
63             string startElement = test is TestSuite
64                 ? "start-suite"
65                 : "start-test";
66
67             var parent = GetParent(test);
68             try
69             {
70                 string report = string.Format(
71                     "<{0} id=\"{1}\" parentId=\"{2}\" name=\"{3}\" fullname=\"{4}\"/>",
72                     startElement,
73                     test.Id,
74                     parent != null ? parent.Id : string.Empty,
75                     FormatAttributeValue(test.Name),
76                     FormatAttributeValue(test.FullName));
77
78                 handler.RaiseCallbackEvent(report);
79             }
80             catch (Exception ex)
81             {
82                 log.Error("Exception processing " + test.FullName + NUnit.Env.NewLine + ex.ToString());
83             }
84         }
85
86         /// <summary>
87         /// Called when a test has finished. Sends a result summary to the callback.
88         /// to 
89         /// </summary>
90         /// <param name="result">The result of the test</param>
91         public void TestFinished(ITestResult result)
92         {
93             try
94             {
95                 var node = result.ToXml(false);
96                 var parent = GetParent(result.Test);
97                 node.Attributes.Add("parentId", parent != null ? parent.Id : string.Empty);
98                 handler.RaiseCallbackEvent(node.OuterXml);                
99             }
100             catch (Exception ex)
101             {
102                 log.Error("Exception processing " + result.FullName + NUnit.Env.NewLine + ex.ToString());
103             }
104         }
105
106         /// <summary>
107         /// Called when a test produces output for immediate display
108         /// </summary>
109         /// <param name="output">A TestOutput object containing the text to display</param>
110         public void TestOutput(TestOutput output)
111         {
112             try
113             {
114                 handler.RaiseCallbackEvent(output.ToXml());
115             }
116             catch (Exception ex)
117             {
118                 log.Error("Exception processing TestOutput event" + NUnit.Env.NewLine + ex.ToString());
119             }
120         }
121
122         #endregion
123
124         #region Helper Methods
125
126         /// <summary>
127         /// Returns the parent test item for the targer test item if it exists
128         /// </summary>
129         /// <param name="test"></param>
130         /// <returns>parent test item</returns>
131         private static ITest GetParent(ITest test)
132         {
133             if (test == null || test.Parent == null)
134             {
135                 return null;
136             }
137
138             return test.Parent.IsSuite ? test.Parent : GetParent(test.Parent);
139         }
140
141         /// <summary>
142         /// Makes a string safe for use as an attribute, replacing
143         /// characters characters that can't be used with their
144         /// corresponding xml representations.
145         /// </summary>
146         /// <param name="original">The string to be used</param>
147         /// <returns>A new string with the _values replaced</returns>
148         private static string FormatAttributeValue(string original)
149         {
150             return original
151                 .Replace("&", "&amp;")
152                 .Replace("\"", "&quot;")
153                 .Replace("'", "&apos;")
154                 .Replace("<", "&lt;")
155                 .Replace(">", "&gt;");
156         }
157
158         #endregion
159     }
160 }