[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / ActionsHelper.cs
1 // ***********************************************************************
2 // Copyright (c) 2012 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;
31 using System.Collections.Generic;
32 using System.Reflection;
33 using NUnit.Compatibility;
34
35 #if PORTABLE
36 using System.Linq;
37 #endif
38
39 namespace NUnit.Framework.Internal
40 {
41     using Interfaces;
42
43     internal class ActionsHelper
44     {
45         public static void ExecuteBeforeActions(IEnumerable<ITestAction> actions, ITest test)
46         {
47             ExecuteActions(ActionPhase.Before, actions, test);
48         }
49
50         public static void ExecuteAfterActions(IEnumerable<ITestAction> actions, ITest test)
51         {
52             ExecuteActions(ActionPhase.After, actions, test);
53         }
54
55         private static void ExecuteActions(ActionPhase phase, IEnumerable<ITestAction> actions, ITest test)
56         {
57             if (actions == null)
58                 return;
59
60             foreach (ITestAction action in GetFilteredAndSortedActions(actions, phase))
61             {
62                 if(phase == ActionPhase.Before)
63                     action.BeforeTest(test);
64                 else
65                     action.AfterTest(test);
66             }
67         }
68         
69 #if PORTABLE
70         //public static ITestAction[] GetActionsFromAttributeProvider(Assembly attributeProvider)
71         //{
72         //    if (attributeProvider == null)
73         //        return new ITestAction[0];
74
75         //    var actions = attributeProvider.GetAttributes<ITestAction>().ToList();
76         //    actions.Sort(SortByTargetDescending);
77
78         //    return actions.ToArray();
79         //}
80
81         //public static ITestAction[] GetActionsFromAttributeProvider(MemberInfo attributeProvider)
82         //{
83         //    if (attributeProvider == null)
84         //        return new ITestAction[0];
85
86         //    var actions = attributeProvider.GetAttributes<ITestAction>(false).ToList();
87         //    actions.Sort(SortByTargetDescending);
88
89         //    return actions.ToArray();
90         //}
91 #else
92         public static ITestAction[] GetActionsFromAttributeProvider(ICustomAttributeProvider attributeProvider)
93         {
94             if (attributeProvider == null)
95                 return new ITestAction[0];
96
97             var actions = new List<ITestAction>((ITestAction[])attributeProvider.GetCustomAttributes(typeof(ITestAction), false));
98             actions.Sort(SortByTargetDescending);
99
100             return actions.ToArray();
101         }
102 #endif
103
104         //public static ITestAction[] GetActionsFromTypesAttributes(Type type)
105         //{
106         //    if(type == null)
107         //        return new ITestAction[0];
108
109         //    if(type == typeof(object))
110         //        return new ITestAction[0];
111
112         //    var actions = new List<ITestAction>();
113
114         //    actions.AddRange(GetActionsFromTypesAttributes(type.GetTypeInfo().BaseType));
115
116         //    Type[] declaredInterfaces = GetDeclaredInterfaces(type);
117
118         //    foreach(Type interfaceType in declaredInterfaces)
119         //        actions.AddRange(GetActionsFromAttributeProvider(interfaceType.GetTypeInfo()));
120
121         //    actions.AddRange(GetActionsFromAttributeProvider(type.GetTypeInfo()));
122
123         //    return actions.ToArray();
124         //}
125
126         private static Type[] GetDeclaredInterfaces(Type type)
127         {
128             List<Type> interfaces = new List<Type>(type.GetInterfaces());
129
130             if (type.GetTypeInfo().BaseType == typeof(object))
131                 return interfaces.ToArray();
132
133             List<Type> baseInterfaces = new List<Type>(type.GetTypeInfo().BaseType.GetInterfaces());
134             List<Type> declaredInterfaces = new List<Type>();
135
136             foreach (Type interfaceType in interfaces)
137             {
138                 if (!baseInterfaces.Contains(interfaceType))
139                     declaredInterfaces.Add(interfaceType);
140             }
141
142             return declaredInterfaces.ToArray();
143         }
144
145         private static ITestAction[] GetFilteredAndSortedActions(IEnumerable<ITestAction> actions, ActionPhase phase)
146         {
147             var filteredActions = new List<ITestAction>();
148             foreach (var actionItem in actions)
149             {
150                 if (filteredActions.Contains(actionItem) != true)
151                     filteredActions.Add(actionItem);
152             }
153
154             if(phase == ActionPhase.After)
155                 filteredActions.Reverse();
156
157             return filteredActions.ToArray();
158         }
159
160         private static int SortByTargetDescending(ITestAction x, ITestAction y)
161         {
162             return y.Targets.CompareTo(x.Targets);
163         }
164
165         private enum ActionPhase
166         {
167             Before,
168             After
169         }
170     }
171 }