[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / TUnit / TTestMethodCommand.cs
1 // ****************************************************************************************************
2 // Namespace:       NUnit.Framework.TUnit
3 // Class:           TTestMethodCommand
4 // Description:     Tizen TestMethod Command
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 NUnit.Framework.Interfaces;
18 using System.Threading;
19 using NUnit.Framework.Internal;
20 using NUnit.Framework.Internal.Commands;
21
22 namespace NUnit.Framework.TUnit
23 {
24     /// <summary>
25     /// TTestMethodCommand is the lowest level concrete command
26     /// used to run actual test cases.
27     /// </summary>
28     public class TTestMethodCommand : TestCommand
29     {
30         private readonly TestMethod testMethod;
31         private readonly object[] arguments;
32
33         /// <summary>
34         /// Initializes a new instance of the <see cref="TTestMethodCommand"/> class.
35         /// </summary>
36         /// <param name="testMethod">The test.</param>
37         public TTestMethodCommand(TestMethod testMethod)
38             : base(testMethod)
39         {
40             this.testMethod = testMethod;
41             this.arguments = testMethod.Arguments;
42         }
43
44         /// <summary>
45         /// Runs the test, saving a TestResult in the execution context, as
46         /// well as returning it. If the test has an expected result, it
47         /// is asserts on that value. Since failed tests and errors throw
48         /// an exception, this command must be wrapped in an outer command,
49         /// will handle that exception and records the failure. This role
50         /// is usually played by the SetUpTearDown command.
51         /// </summary>
52         /// <param name="context">The execution context</param>
53         public override TestResult Execute(TestExecutionContext context)
54         {
55             // TODO: Decide if we should handle exceptions here
56             object result = RunTestMethod(context);
57
58             if (testMethod.HasExpectedResult)
59                 NUnit.Framework.Assert.AreEqual(testMethod.ExpectedResult, result);
60
61             context.CurrentResult.SetResult(ResultState.Success);
62             // TODO: Set assert count here?
63             //context.CurrentResult.AssertCount = context.AssertCount;
64             return context.CurrentResult;
65         }
66
67         private object RunTestMethod(TestExecutionContext context)
68         {
69             // [DuongNT]: Sleep 50 ms to reduce CPU consumption
70             ++TSettings.CurTCIndex;
71             if (TSettings.CurTCIndex % 10 == 0)
72                 Thread.Sleep(2000);
73             else
74                 Thread.Sleep(TSettings.GetInstance().GetDefaultTCDelay());
75
76             TUnit.TLogger.Write("##### Running Test Case [" + TSettings.CurTCIndex + "]: " + testMethod.Method.TypeInfo.FullName + "." +
77                 testMethod.Method.Name);
78
79 #if NET_4_0 || NET_4_5 || PORTABLE
80             if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method.MethodInfo))
81                 return RunAsyncTestMethod(context);
82             else
83 #endif
84                 return RunNonAsyncTestMethod(context);
85         }
86
87 #if NET_4_0 || NET_4_5 || PORTABLE || TIZEN
88         private object RunAsyncTestMethod(TestExecutionContext context)
89         {
90             TLogger.Write("##### RunAsyncTestMethod in TTestMethodCommand class #####");
91             #region tronghieu.d - invoke async test method in application thread. This thread is blocked for waiting result.
92             using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(testMethod.Method.MethodInfo))
93             {
94                 TAsyncThreadMgr asyncThreadMgr = TAsyncThreadMgr.GetInstance();
95                 asyncThreadMgr.SetData(this, testMethod, arguments, context, true);
96                 asyncThreadMgr.GetMethodExecutionResetEvent().Set(); // release main thread to invoke method
97                 _testMethodRunComplete.WaitOne(); // wait for result in current thread
98                 if (asyncThreadMgr.GetNonAsyncMethodException() != null)
99                     throw asyncThreadMgr.GetNonAsyncMethodException();
100                 try
101                 {
102                     if (asyncThreadMgr.GetResult() == null)
103                     {
104                         return asyncThreadMgr.GetResult();
105                     }
106                     return region.WaitForPendingOperationsToComplete(asyncThreadMgr.GetResult());
107                 }
108                 catch (Exception e)
109                 {
110                     throw new NUnitException("Rethrown", e);
111                 }
112             }
113             #endregion
114         }
115 #endif
116
117         private object RunNonAsyncTestMethod(TestExecutionContext context)
118         {
119             TLogger.Write("##### RunNonAsyncTestMethod in TTestMethodCommand class #####");
120             #region tronghieu.d - invoke async test method in application thread. This thread is blocked for waiting result.
121             TAsyncThreadMgr asyncThreadMgr = TAsyncThreadMgr.GetInstance();
122             asyncThreadMgr.SetData(this, testMethod, arguments, context, false);
123             asyncThreadMgr.GetMethodExecutionResetEvent().Set(); // release main thread to invoke method
124             _testMethodRunComplete.WaitOne(); // wait for result in current thread
125
126             if (asyncThreadMgr.GetNonAsyncMethodException() != null)
127                 throw asyncThreadMgr.GetNonAsyncMethodException();
128
129             return asyncThreadMgr.GetResult();
130             #endregion
131             //return testMethod.Method.Invoke(context.TestObject, arguments);
132         }
133     }
134 }