[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / Commands / TestActionCommand.cs
1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Collections.Generic;
31 using System.Threading;
32
33 namespace NUnit.Framework.Internal.Commands
34 {
35     using Interfaces;
36
37     /// <summary>
38     /// TestActionCommand runs the BeforeTest actions for a test,
39     /// then runs the test and finally runs the AfterTestActions.
40     /// </summary>
41     public class TestActionCommand : DelegatingTestCommand
42     {
43         private IList<TestActionItem> _actions = new List<TestActionItem>();
44
45         /// <summary>
46         /// Initializes a new instance of the <see cref="TestActionCommand"/> class.
47         /// </summary>
48         /// <param name="innerCommand">The inner command.</param>
49         public TestActionCommand(TestCommand innerCommand)
50             : base(innerCommand)
51         {
52             Guard.ArgumentValid(innerCommand.Test is TestMethod, "TestActionCommand may only apply to a TestMethod", "innerCommand");
53         }
54
55         /// <summary>
56         /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
57         /// </summary>
58         /// <param name="context">The context in which the test should run.</param>
59         /// <returns>A TestResult</returns>
60         public override TestResult Execute(TestExecutionContext context)
61         {
62             if (Test.Fixture == null)
63                 Test.Fixture = context.TestObject;
64
65             // In the current implementation, upstream actions only apply to tests. If that should change in the future,
66             // then actions would have to be tested for here. For now we simply assert it in Debug. We allow 
67             // ActionTargets.Default, because it is passed down by ParameterizedMethodSuite.
68             foreach (ITestAction action in context.UpstreamActions)
69             {
70                 System.Diagnostics.Debug.Assert(
71                     action.Targets == ActionTargets.Default || (action.Targets & ActionTargets.Test) == ActionTargets.Test,
72                     "Invalid target on upstream action: " + action.Targets.ToString());
73
74                 _actions.Add(new TestActionItem(action));
75             }
76
77             //foreach (ITestAction action in ActionsHelper.GetActionsFromAttributeProvider(((TestMethod)Test).Method.MethodInfo))
78             //    if (action.Targets == ActionTargets.Default || (action.Targets & ActionTargets.Test) == ActionTargets.Test)
79             //        _actions.Add(new TestActionItem(action));
80
81             try
82             {
83                 for (int i = 0; i < _actions.Count; i++)
84                     _actions[i].BeforeTest(Test);
85
86                 context.CurrentResult = innerCommand.Execute(context);
87             }
88             catch (Exception ex)
89             {
90 #if !NETCF && !SILVERLIGHT && !PORTABLE
91                 if (ex is ThreadAbortException)
92                     Thread.ResetAbort();
93 #endif
94                 context.CurrentResult.RecordException(ex);
95             }
96             finally
97             {
98                 if (context.ExecutionStatus != TestExecutionStatus.AbortRequested)
99                     for (int i = _actions.Count - 1; i >= 0; i--)
100                         _actions[i].AfterTest(Test);
101             }
102
103             return context.CurrentResult;
104         }
105     }
106 }