1 // ****************************************************************************************************
2 // Namespace: NUnitLite.TUnit
4 // Description: Tizen UnitTest Runner to run Tizen C# test-suite automatically
5 // Author: Nguyen Truong Duong <duong.nt1@samsung.com>
8 // Name: Date: Description:
9 // ****************************************************************************************************
12 #define NUNIT_FRAMEWORK
18 using System.Reflection;
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;
27 namespace NUnitLite.TUnit
31 private Assembly _testAssembly;
32 private TextRunner _textRunner;
33 private string[] args;
34 public static string TESTCASE_XML_NAME = "test";
35 private string pkgName = "";
39 * @summary: Declares event will be emitted when a single test execution done.
41 public event EventHandler<SingleTestDoneEventArgs> SingleTestDone;
44 * @summary: Forwards event when a single test execution done.
46 protected virtual void OnSingleTestDone(SingleTestDoneEventArgs e)
48 EventHandler<SingleTestDoneEventArgs> handler = SingleTestDone;
56 public TRunner(Assembly testAssembly)
58 _testAssembly = testAssembly;
59 Tizen.Log.Fatal("NUITEST", $"");
60 Tizen.Log.Fatal("NUITEST", $"TRunner() _testAssembly={_testAssembly}");
62 TSettings.GetInstance().ConnectTestkitStub();
66 public TRunner() : this(Assembly.GetEntryAssembly())
68 Tizen.Log.Fatal("NUITEST", $"");
69 Tizen.Log.Fatal("NUITEST", $"TRunner()");
73 /// Get the app name of the Tizen package
75 private string GetPackageName(string basePath)
77 // basePath = "/opt/home/owner/apps_rw/Tizen.Applications.Manual.Tests/bin/";
78 if (basePath.Contains("bin\\Debug"))
80 // If build on Window, return "Test"
81 //LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "Run on Window");
85 string[] delimiter = { "/" };
86 string[] delimiterDot = { "." };
88 string returnValue = "";
91 strAry = basePath.Split(delimiter, StringSplitOptions.None);
92 foreach (string str in strAry)
94 if (str.Contains("Tizen."))
96 returnValue = str.Substring(6, str.Length - 12); //remove Tizen. and .Tests
97 // LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
104 TLogger.WriteError(e.Message + e.ToString() + ". Please edit packageId as per guideline");
111 /// Execute the test suite automatically on the Tizen devidce
113 public void LoadTestsuite()
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);
123 Tizen.Log.Fatal("NUITEST", $"The package name is invalid!");
124 TLogger.WriteError("The package name is invalid!");
128 //TLogger.Write("Executing the application: " + pkgName + "...");
129 Tizen.Log.Fatal("NUITEST", $"Executing the application: {pkgName}");
130 string exeFilePathName = "";
131 if(dllPath.Contains("netcoreapp"))
133 exeFilePathName = string.Format(dllPath + "Tizen.{0}Tests.dll", pkgName);
137 exeFilePathName = string.Format(dllPath + "Debug/netcoreapp3.1/Tizen.{0}dll", pkgName);
139 //TLogger.Write("exeFilePathName : " + exeFilePathName);
140 Tizen.Log.Fatal("NUITEST", $"exeFilePathName : {exeFilePathName}");
142 AssemblyName asmName = new AssemblyName(GetAssemblyName(exeFilePathName));
143 _testAssembly = Assembly.LoadFrom(exeFilePathName);
145 string pkgShareDir = $"{dllPath}{pkgName}";
146 System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(pkgShareDir);
147 if (di.Exists == false)
152 string outputFilePathName = string.Format("{0}/{1}.xml", pkgShareDir, TESTCASE_XML_NAME);
154 TSettings.GetInstance().SetOutputFilePathName(outputFilePathName);
155 string[] s = new string[1] { exeFilePathName };
157 Tizen.Log.Fatal("NUITEST", $"outputFilePathName : {outputFilePathName}");
159 //new TextRunner(_testAssembly).Execute(s);
160 LoadTestsuite(s, outputFilePathName);
164 /// Execute the test suite automatically on the Tizen devidce
166 public void LoadTestsuite(string dllPath, string pkgName)
168 TSettings.CurTCIndex = 0;
171 TLogger.Write("The package name is invalid!");
174 TLogger.Write("Executing the application: " + pkgName + "...");
177 string exeFilePathName = string.Format(dllPath + "Tizen.{0}.Tests.exe", pkgName);
179 AssemblyName asmName = new AssemblyName(GetAssemblyName(exeFilePathName));
180 _testAssembly = Assembly.Load(asmName);
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)
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);
197 /// Execute the tests in the assembly, passing in
198 /// a list of arguments.
200 /// <param name="args">arguments for NUnitLite to use</param>
201 public void Execute()
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();
209 Task t = Task.Run(() =>
211 _textRunner.Execute(args);
212 asyncThreadMgr.SetData(null, null, null, null, false);
213 methodExecutionResetEvent.Set();
215 t.GetAwaiter().OnCompleted(() =>
217 OnSingleTestDone(TSettings.GetInstance().GetSingleTestDoneEventArgs());
219 methodExecutionResetEvent.WaitOne();
220 asyncThreadMgr.RunTestMethod();
223 new TextRunner(_testAssembly).Execute(args);
227 private string GetAssemblyName(string assemblyFullPath)
230 string[] delimiter1 = { "\\" };
231 string[] delimiter2 = { "/" };
232 string[] delimiterDot = { "." };
233 string[] delimiterExe = { ".exe" };
235 string returnValue = "";
238 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
240 if (strAry.Length < 2)
241 strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
243 foreach (string str in strAry)
245 if (str.Contains(".Tests.dll"))
247 string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
248 returnValue = strSplit[0];
249 //LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
252 if (str.Contains(".exe"))
254 string[] strSplit = str.Split(delimiterExe, StringSplitOptions.None);
255 returnValue = strSplit[0];
262 TLogger.WriteError(TLogger.ExceptionTag , e.Message + e.ToString() + ". Please edit packageId as per guideline");
268 public void LoadTestsuite(string[] args, string tcListPath = "")
271 _textRunner = new TextRunner(_testAssembly);
273 _textRunner.LoadTest(args);
274 //WriteTestList(tcListPath);
278 public Dictionary<string, ITest> GetTestList()
280 return _textRunner.TestcaseList;
283 public void WriteTestList(string path)
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";
295 List<TestcaseData> tcList = new List<TestcaseData>();
297 foreach (KeyValuePair<string, ITest> pair in _textRunner.TestcaseList)
299 TestcaseData testData = new TestcaseData();
300 testData.id = pair.Key;
301 if (pkgName.Contains("manual"))
302 testData.execution_type = "manual";
304 testData.execution_type = "auto";
305 tcList.Add(testData);
310 xmlUtils.writeResult("", path, testEnv, tcList);
314 TLogger.WriteError(TLogger.ExceptionTag, e.Message + e.ToString());
315 LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, e.Message + e.ToString());