1 // ****************************************************************************************************
2 // Namespace: NUnit.Framework.TUnit
3 // Class: TTestMethodCommand
4 // Description: Tizen TestMethod Command
5 // Author: Nguyen Truong Duong <duong.nt1@samsung.com>
8 // Name: Date: Description:
9 // ****************************************************************************************************
12 #define NUNIT_FRAMEWORK
17 using NUnit.Framework.Interfaces;
18 using System.Threading;
19 using NUnit.Framework.Internal;
20 using NUnit.Framework.Internal.Commands;
22 namespace NUnit.Framework.TUnit
25 /// TTestMethodCommand is the lowest level concrete command
26 /// used to run actual test cases.
28 public class TTestMethodCommand : TestCommand
30 private readonly TestMethod testMethod;
31 private readonly object[] arguments;
34 /// Initializes a new instance of the <see cref="TTestMethodCommand"/> class.
36 /// <param name="testMethod">The test.</param>
37 public TTestMethodCommand(TestMethod testMethod)
40 this.testMethod = testMethod;
41 this.arguments = testMethod.Arguments;
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.
52 /// <param name="context">The execution context</param>
53 public override TestResult Execute(TestExecutionContext context)
55 // TODO: Decide if we should handle exceptions here
56 object result = RunTestMethod(context);
58 if (testMethod.HasExpectedResult)
59 NUnit.Framework.Assert.AreEqual(testMethod.ExpectedResult, result);
61 context.CurrentResult.SetResult(ResultState.Success);
62 // TODO: Set assert count here?
63 //context.CurrentResult.AssertCount = context.AssertCount;
64 return context.CurrentResult;
67 private object RunTestMethod(TestExecutionContext context)
69 // [DuongNT]: Sleep 50 ms to reduce CPU consumption
70 ++TSettings.CurTCIndex;
71 if (TSettings.CurTCIndex % 10 == 0)
74 Thread.Sleep(TSettings.GetInstance().GetDefaultTCDelay());
76 TUnit.TLogger.Write("##### Running Test Case [" + TSettings.CurTCIndex + "]: " + testMethod.Method.TypeInfo.FullName + "." +
77 testMethod.Method.Name);
79 #if NET_4_0 || NET_4_5 || PORTABLE
80 if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method.MethodInfo))
81 return RunAsyncTestMethod(context);
84 return RunNonAsyncTestMethod(context);
87 #if NET_4_0 || NET_4_5 || PORTABLE || TIZEN
88 private object RunAsyncTestMethod(TestExecutionContext context)
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))
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();
102 if (asyncThreadMgr.GetResult() == null)
104 return asyncThreadMgr.GetResult();
106 return region.WaitForPendingOperationsToComplete(asyncThreadMgr.GetResult());
110 throw new NUnitException("Rethrown", e);
117 private object RunNonAsyncTestMethod(TestExecutionContext context)
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
126 if (asyncThreadMgr.GetNonAsyncMethodException() != null)
127 throw asyncThreadMgr.GetNonAsyncMethodException();
129 return asyncThreadMgr.GetResult();
131 //return testMethod.Method.Invoke(context.TestObject, arguments);