[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunitlite / TUnit / TRunner.cs
1 // ****************************************************************************************************
2 // Namespace:       NUnitLite.TUnit
3 // Class:           TRunner
4 // Description:     Tizen UnitTest Runner to run Tizen C# test-suite automatically
5 // Author:          Nguyen Truong Duong <duong.nt1@samsung.com>
6 // Notes:          
7 // Revision History:
8 // Name:           Date:        Description:
9 // ****************************************************************************************************
10 #define PORTABLE
11 #define TIZEN
12 #define NUNIT_FRAMEWORK
13 #define NUNITLITE
14 #define NET_4_5
15 #define PARALLEL
16 using System;
17 using System.IO;
18 using System.Reflection;
19 using System.Xml;
20 using System.Threading;
21 using System.Threading.Tasks;
22 using NUnit.Framework.TUnit;
23 using NUnit.Framework.Interfaces;
24 using System.Collections.Generic;
25 using System.ComponentModel;
26
27 namespace NUnitLite.TUnit
28 {
29     public class TRunner
30     {
31         private Assembly _testAssembly;
32         private TextRunner _textRunner;
33         private string[] args;
34         public static string TESTCASE_XML_NAME = "test";
35         private string pkgName = "";
36
37 #if TIZEN
38         /**
39          * @summary: Declares event will be emitted when a single test execution done.
40          */
41         public event EventHandler<SingleTestDoneEventArgs> SingleTestDone;
42
43         /**
44          * @summary: Forwards event when a single test execution done.
45          */
46         protected virtual void OnSingleTestDone(SingleTestDoneEventArgs e)
47         {
48             EventHandler<SingleTestDoneEventArgs> handler = SingleTestDone;
49             if (handler != null)
50             {
51                 handler(this, e);
52             }
53         }
54 #endif
55
56         public TRunner(Assembly testAssembly)
57         {
58             _testAssembly = testAssembly;
59             Tizen.Log.Fatal("NUITEST", $"");
60             Tizen.Log.Fatal("NUITEST", $"TRunner() _testAssembly={_testAssembly}");
61 #if TIZEN
62             TSettings.GetInstance().ConnectTestkitStub();
63 #endif
64         }
65
66         public TRunner() : this(Assembly.GetEntryAssembly())
67         {
68             Tizen.Log.Fatal("NUITEST", $"");
69             Tizen.Log.Fatal("NUITEST", $"TRunner()");
70         }
71
72         /// <summary>
73         /// Get the app name of the Tizen package
74         /// </summary>
75         private string GetPackageName(string basePath)
76         {
77             // basePath = "/opt/home/owner/apps_rw/Tizen.Applications.Manual.Tests/bin/";
78             if (basePath.Contains("bin\\Debug"))
79             {
80                 // If build on Window, return "Test"
81                 //LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "Run on Window");
82                 return "Test";
83             }
84
85             string[] delimiter = { "/" };
86             string[] delimiterDot = { "." };
87             string[] strAry;
88             string returnValue = "";
89             try
90             {
91                 strAry = basePath.Split(delimiter, StringSplitOptions.None);
92                 foreach (string str in strAry)
93                 {
94                     if (str.Contains("Tizen."))
95                     {
96                         returnValue = str.Substring(6, str.Length - 12); //remove Tizen. and .Tests
97                         // LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
98                         break;
99                     }
100                 }
101             }
102             catch (Exception e)
103             {
104                 TLogger.WriteError(e.Message + e.ToString() + ". Please edit packageId as per guideline");
105             }
106
107             return returnValue;
108         }
109
110         /// <summary>
111         /// Execute the test suite automatically on the Tizen devidce
112         /// </summary>
113         public void LoadTestsuite()
114         {
115             Tizen.Log.Fatal("NUITEST", $"");
116             Tizen.Log.Fatal("NUITEST", $"LoadTestsuite()");
117             TSettings.CurTCIndex = 0;
118             string cache_path = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
119             string dllPath = cache_path.Replace("res", "bin");//Cache->res
120             string pkgName = GetPackageName(dllPath);
121             if (pkgName == "")
122             {
123                 Tizen.Log.Fatal("NUITEST", $"The package name is invalid!");
124                 TLogger.WriteError("The package name is invalid!");
125                 return;
126             }
127
128             //TLogger.Write("Executing the application: " + pkgName + "...");
129             Tizen.Log.Fatal("NUITEST", $"Executing the application: {pkgName}");
130             string exeFilePathName = "";
131             if(dllPath.Contains("netcoreapp"))
132             {
133                 exeFilePathName = string.Format(dllPath + "Tizen.{0}Tests.dll", pkgName);
134             }
135             else
136             {
137                 exeFilePathName = string.Format(dllPath + "Debug/netcoreapp3.1/Tizen.{0}dll", pkgName);
138             }
139             //TLogger.Write("exeFilePathName : " + exeFilePathName);
140             Tizen.Log.Fatal("NUITEST", $"exeFilePathName : {exeFilePathName}");
141
142             AssemblyName asmName = new AssemblyName(GetAssemblyName(exeFilePathName));
143             _testAssembly = Assembly.LoadFrom(exeFilePathName);
144
145             string pkgShareDir = $"{dllPath}{pkgName}";
146             System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(pkgShareDir);
147             if (di.Exists == false)
148             {
149                 di.Create();
150             }
151
152             string outputFilePathName = string.Format("{0}/{1}.xml", pkgShareDir, TESTCASE_XML_NAME);
153             
154             TSettings.GetInstance().SetOutputFilePathName(outputFilePathName);
155             string[] s = new string[1] { exeFilePathName };
156
157             Tizen.Log.Fatal("NUITEST", $"outputFilePathName : {outputFilePathName}");
158             
159             //new TextRunner(_testAssembly).Execute(s);
160             LoadTestsuite(s, outputFilePathName);
161         }
162
163         /// <summary>
164         /// Execute the test suite automatically on the Tizen devidce
165         /// </summary>
166         public void LoadTestsuite(string dllPath, string pkgName)
167         {
168             TSettings.CurTCIndex = 0;
169             if (pkgName == "")
170             {
171                 TLogger.Write("The package name is invalid!");
172                 return;
173             }
174             TLogger.Write("Executing the application: " + pkgName + "...");
175
176
177             string exeFilePathName = string.Format(dllPath + "Tizen.{0}.Tests.exe", pkgName);
178
179             AssemblyName asmName = new AssemblyName(GetAssemblyName(exeFilePathName));
180             _testAssembly = Assembly.Load(asmName);
181
182             string pkgShareDir = string.Format("/home/owner/share/{0}", pkgName);
183             System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(pkgShareDir);
184             if (di.Exists == false)
185             {
186                 di.Create();
187             }
188
189             string outputFilePathName = string.Format("{0}/{1}.xml", pkgShareDir, TESTCASE_XML_NAME);
190             TSettings.GetInstance().SetOutputFilePathName(outputFilePathName);
191             string[] s = new string[1] { exeFilePathName };
192             //new TextRunner(_testAssembly).Execute(s);
193             LoadTestsuite(s, outputFilePathName);
194         }
195
196         /// <summary>
197         /// Execute the tests in the assembly, passing in
198         /// a list of arguments.
199         /// </summary>
200         /// <param name="args">arguments for NUnitLite to use</param>
201         public void Execute()
202         {
203 #if TIZEN
204             #region tronghieu.d - Create new thread to run test and mainthread waiting for invoke test method.
205             TAsyncThreadMgr asyncThreadMgr = TAsyncThreadMgr.GetInstance();
206             ManualResetEvent methodExecutionResetEvent = asyncThreadMgr.GetMethodExecutionResetEvent();
207             methodExecutionResetEvent.Reset();
208
209             Task t = Task.Run(() =>
210                 {
211                     _textRunner.Execute(args);
212                     asyncThreadMgr.SetData(null, null, null, null, false);
213                     methodExecutionResetEvent.Set();
214                 });
215             t.GetAwaiter().OnCompleted(() =>
216                 {
217                     OnSingleTestDone(TSettings.GetInstance().GetSingleTestDoneEventArgs());
218                 });
219             methodExecutionResetEvent.WaitOne();
220             asyncThreadMgr.RunTestMethod();
221             #endregion
222 #else
223             new TextRunner(_testAssembly).Execute(args);
224 #endif
225         }
226
227         private string GetAssemblyName(string assemblyFullPath)
228         {
229
230             string[] delimiter1 = { "\\" };
231             string[] delimiter2 = { "/" };
232             string[] delimiterDot = { "." };
233             string[] delimiterExe = { ".exe" };
234             string[] strAry;
235             string returnValue = "";
236             try
237             {
238                 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
239
240                 if (strAry.Length < 2)
241                     strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
242
243                 foreach (string str in strAry)
244                 {
245                     if (str.Contains(".Tests.dll"))
246                     {
247                         string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
248                         returnValue = strSplit[0];
249                         //LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
250                         break;
251                     }
252                     if (str.Contains(".exe"))
253                     {
254                         string[] strSplit = str.Split(delimiterExe, StringSplitOptions.None);
255                         returnValue = strSplit[0];
256                         break;
257                     }
258                 }
259             }
260             catch (Exception e)
261             {
262                 TLogger.WriteError(TLogger.ExceptionTag , e.Message + e.ToString() + ". Please edit packageId as per guideline");
263             }
264
265             return returnValue;
266         }
267
268         public void LoadTestsuite(string[] args, string tcListPath = "")
269         {
270             this.args = args;
271             _textRunner = new TextRunner(_testAssembly);
272 #if TIZEN
273             _textRunner.LoadTest(args);
274             //WriteTestList(tcListPath);
275 #endif
276         }
277 #if TIZEN
278         public Dictionary<string, ITest> GetTestList()
279         {
280             return _textRunner.TestcaseList;
281         }
282
283         public void WriteTestList(string path)
284         {
285             LogUtils.Write(LogUtils.INFO, LogUtils.TAG, "In WriteTestList");
286             XMLUtils xmlUtils = new XMLUtils();
287             TestcaseEnvironment testEnv = new TestcaseEnvironment();
288             testEnv.build_id = "test";
289             testEnv.device_id = "csharp_device";
290             testEnv.device_model = "";
291             testEnv.device_name = "N/A";
292             testEnv.host = "N/A";
293             testEnv.resolution = "N/A";
294
295             List<TestcaseData> tcList = new List<TestcaseData>();
296
297             foreach (KeyValuePair<string, ITest> pair in _textRunner.TestcaseList)
298             {
299                 TestcaseData testData = new TestcaseData();
300                 testData.id = pair.Key;
301                 if (pkgName.Contains("manual"))
302                     testData.execution_type = "manual";
303                 else
304                     testData.execution_type = "auto";
305                 tcList.Add(testData);
306             }
307
308             try
309             {
310                 xmlUtils.writeResult("", path, testEnv, tcList);
311             }
312             catch (Exception e)
313             {
314                 TLogger.WriteError(TLogger.ExceptionTag, e.Message + e.ToString());
315                 LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, e.Message + e.ToString());
316             }
317         }
318
319 #endif
320     }
321 }