[NUI] Add DisposeTest, Add DebugFileLogging Test
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / TUnit / TAsyncThreadMgr.cs
1 #define PORTABLE
2 #define TIZEN
3 #define NUNIT_FRAMEWORK
4 #define NUNITLITE
5 #define NET_4_5
6 #define PARALLEL
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12 using System.Reflection;
13 using NUnit.Framework.Internal;
14 using System.Threading;
15 using NUnit.Framework.Internal.Commands;
16
17 namespace NUnit.Framework.TUnit
18 {
19     #region tronghieu.d - added class
20     public class TAsyncThreadMgr
21     {
22         private static TAsyncThreadMgr _instance;
23         private static object lockObject = new object();
24
25         public static TAsyncThreadMgr GetInstance()
26         {
27             lock (lockObject)
28             {
29                 if (_instance == null)
30                 {
31                     _instance = new TAsyncThreadMgr();
32                 }
33             }
34             return _instance;
35         }
36
37         private TAsyncThreadMgr()
38         {
39             this.testCommand = null;
40             this.testMethod = null;
41             this.arguments = null;
42             this.context = null;
43         }
44
45         public void SetData(TestCommand testCommand, TestMethod testMethod, object[] arguments, TestExecutionContext context, bool IsAsyncOperation)
46         {
47             this.testCommand = testCommand;
48             this.testMethod = testMethod;
49             this.arguments = arguments;
50             this.context = context;
51             this.IsAsyncOperation = IsAsyncOperation;
52             this.exception = null;
53         }
54         #region nguyen.vtan setData for setup & teardown to run on main thread
55         public void SetData(SetUpTearDownItem setupteardownItem)
56         {
57             this._setupteardownItem.Add(setupteardownItem);
58         }
59         public void SetTearDownData(SetUpTearDownItem setupteardownItem)
60         {
61             this._teardownItem.Add(setupteardownItem);
62         }
63         public void ClearSetUpTearDown()
64         {
65             _setupteardownItem.Clear();
66             _teardownItem.Clear();
67         }
68         #endregion
69
70
71         private TestCommand testCommand;
72         private TestMethod testMethod;
73         private object[] arguments;
74         private TestExecutionContext context;
75         private bool IsAsyncOperation;
76         private object result;
77         private Exception exception;
78         readonly List<SetUpTearDownItem> _setupteardownItem = new List<SetUpTearDownItem>();
79         readonly List<SetUpTearDownItem> _teardownItem = new List<SetUpTearDownItem>();
80         //ManualResetEvent _singnalOneTimeTearDown;
81
82         private readonly ManualResetEvent _methodExecutionResetEvent = new ManualResetEvent(false);
83
84         public ManualResetEvent GetMethodExecutionResetEvent()
85         {
86             return _methodExecutionResetEvent;
87         }
88
89         /* Invoke async test method in main thread*/
90         public bool RunTestMethod()
91         {
92             if (testCommand == null || testMethod == null || context == null)
93             {
94                 return false;
95             }
96             TLogger.Write("##### RunTestMethod in TAsyncThreadMgr class #####");
97             if (IsAsyncOperation)
98                 RunAsyncTestMethod();
99             else
100                 RunNonAsyncTestMethod();
101
102             return true;
103         }
104
105         public void RunAsyncTestMethod()
106         {
107             TLogger.Write("##### RunAsyncTestMethod in TAsyncThreadMgr class #####");
108             try
109             {
110                 result = null;
111                 #region nguyen.vtan add Setup
112                 runSetup();
113                 #endregion
114                 result = Reflect.InvokeMethod(testMethod.Method.MethodInfo, context.TestObject, arguments);
115             }
116             catch (Exception e)
117             {
118                 exception = e;
119                 // Console.WriteLine(e.Message);
120             }
121             finally
122             {
123                 if (result == null)
124                 {
125                     #region nguyen.vtan add Setup
126                     runTearDown();
127                     Thread.Sleep(50);
128                     #endregion
129                     testCommand._testMethodRunComplete.Set();
130                     _methodExecutionResetEvent.Reset();
131                     _methodExecutionResetEvent.WaitOne();
132                     RunTestMethod();
133                 }
134                 else
135                 {
136                     ((Task)result).GetAwaiter().OnCompleted(() =>
137                     {
138                         #region nguyen.vtan add TearDown
139                         runTearDown();
140                         Thread.Sleep(50);
141                         #endregion
142                         testCommand._testMethodRunComplete.Set();
143                         _methodExecutionResetEvent.Reset();
144                         _methodExecutionResetEvent.WaitOne();
145                         RunTestMethod();
146                     });
147                 }
148             }
149         }
150
151         public void RunNonAsyncTestMethod()
152         {
153             TLogger.Write("##### RunNonAsyncTestMethod in TAsyncThreadMgr class #####\n");
154             try
155             {
156                 runSetup();
157                 result = testMethod.Method.Invoke(context.TestObject, arguments);
158             }
159             catch (Exception ex)
160             {
161                 exception = ex;
162             }
163             #region nguyen.vtan add TearDown
164             runTearDown();
165             #endregion
166             testCommand._testMethodRunComplete.Set();
167             _methodExecutionResetEvent.Reset();
168             _methodExecutionResetEvent.WaitOne();
169         }
170
171         public object GetResult()
172         {
173             return result;
174         }
175
176         public Exception GetNonAsyncMethodException()
177         {
178             return exception;
179         }
180
181         #region add by nguyen.vtan rewrite setup & teardown method to run on main thread
182         public void runSetup()
183         {
184             TLogger.Write("##### runSetup in TAsyncThreadMgr class #####");
185             foreach (var item in _setupteardownItem)
186             {
187                 if (item?._setUpMethods != null)
188                     foreach (MethodInfo setUpMethod in item._setUpMethods)
189                     {
190                         item.RunSetUpOrTearDownMethod(context, setUpMethod);
191                     }
192             }
193         }
194         public void runTearDown()
195         {
196
197             TLogger.Write("##### runTearDown in TAsyncThreadMgr class #####");
198             if (context?.ExecutionStatus == TestExecutionStatus.AbortRequested)
199             {
200                 return;
201             }
202
203             try
204             {
205                 foreach (var item in _teardownItem)
206                 {
207                     // Even though we are only running one level at a time, we
208                     // run the teardowns in reverse order to provide consistency.
209                     if (item?._tearDownMethods != null)
210                     {
211                         int index = item._tearDownMethods.Count;
212                         while (--index >= 0)
213                         {
214                             item.RunSetUpOrTearDownMethod(context, item._tearDownMethods[index]);
215                         }
216                     }
217                 }
218             }
219             catch (Exception ex)
220             {
221                 context.CurrentResult.RecordTearDownException(ex);
222             }
223         }
224         #endregion
225     }
226     #endregion
227 }