Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / Integration / DelegateCompositionTests.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.ComponentModel.Composition;\r
6 using System.ComponentModel.Composition.Factories;\r
7 using System.ComponentModel.Composition.Hosting;\r
8 using System.ComponentModel.Composition.Primitives;\r
9 using System.Linq;\r
10 using System.UnitTesting;\r
11 using Microsoft.VisualStudio.TestTools.UnitTesting;\r
12 \r
13 namespace Tests.Integration\r
14 {\r
15     [TestClass]\r
16     public class DelegateCompositionTests\r
17     {\r
18         public delegate int SimpleDelegate();\r
19         public delegate object DoWorkDelegate(int i, ref object o, out string s);\r
20 \r
21         public class MethodExporter\r
22         {\r
23             [Export]\r
24             public int SimpleMethod()\r
25             {\r
26                 return 1;\r
27             }\r
28 \r
29             [Export(typeof(DoWorkDelegate))]\r
30             public object DoWork(int i, ref object o, out string s)\r
31             {\r
32                 s = "";\r
33                 return o;\r
34             }\r
35 \r
36             [Export("ActionWith8Arguments")]\r
37             [Export("ActionWith8Arguments", typeof(Delegate))]\r
38             public void Action(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)\r
39             {\r
40             }\r
41 \r
42             [Export("FunctionWith8Arguments")]\r
43             [Export("FunctionWith8Arguments", typeof(Delegate))]\r
44             public int Function(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)\r
45             {\r
46                 return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;\r
47             }\r
48 \r
49             [Export("ActionWith9Arguments")]\r
50             [Export("ActionWith9Arguments", typeof(Delegate))]\r
51             public void Action(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)\r
52             {\r
53             }\r
54 \r
55             [Export("FunctionWith9Arguments")]\r
56             [Export("FunctionWith9Arguments", typeof(Delegate))]\r
57             public int Function(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)\r
58             {\r
59                 return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;\r
60             }\r
61 \r
62 #if CLR40 && !SILVERLIGHT\r
63             [Export("FunctionWithDefaultValue")]\r
64             public int FunctionWithDefaultValue(int i, string s = "")\r
65             {\r
66                 return i; \r
67             }\r
68 #endif\r
69         }\r
70 \r
71         [TestMethod]\r
72         public void Export_SimpleCustomDelegate_ShouldWork()\r
73         {\r
74             var container = ContainerFactory.CreateWithAttributedCatalog(\r
75                 typeof(MethodExporter));\r
76 \r
77             var contractName = AttributedModelServices.GetContractName(typeof(SimpleDelegate));\r
78 \r
79             var export1 = container.GetExportedValue<SimpleDelegate>();\r
80             Assert.AreEqual(1, export1());\r
81 \r
82             var export2 = container.GetExportedValue<Func<int>>();\r
83             Assert.AreEqual(1, export1());\r
84 \r
85             var export3 = (ExportedDelegate)container.GetExportedValue<object>(contractName);\r
86             var export4 = (SimpleDelegate)export3.CreateDelegate(typeof(SimpleDelegate));\r
87             Assert.AreEqual(1, export4());\r
88         }\r
89 \r
90         [TestMethod]\r
91         public void Export_CustomDelegateWithOutRefParams_ShouldWork()\r
92         {\r
93             var container = ContainerFactory.CreateWithAttributedCatalog(\r
94                 typeof(MethodExporter));\r
95 \r
96             var export1 = container.GetExportedValue<DoWorkDelegate>();\r
97 \r
98             int i = 0;\r
99             object o = new object();\r
100             string s;\r
101 \r
102             export1(i, ref o, out s);\r
103         }\r
104 \r
105         [TestMethod]\r
106         public void Export_FunctionWith8Arguments_ShouldWorkFine()\r
107         {\r
108             var container = ContainerFactory.CreateWithAttributedCatalog(\r
109                 typeof(MethodExporter));\r
110 \r
111 #if CLR40 && !SILVERLIGHT\r
112             Assert.IsNotNull(container.GetExportedValue<Delegate>("FunctionWith8Arguments"));\r
113 #else\r
114             ExceptionAssert.Throws<CompositionContractMismatchException>(() =>\r
115                 container.GetExportedValue<Delegate>("FunctionWith8Arguments"));\r
116 #endif\r
117         }\r
118 \r
119         public delegate int FuncWith9Args(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9);\r
120 \r
121         [TestMethod]\r
122         public void Export_FuncWith9Arguments_ShouldThrowContractMismatch()\r
123         {\r
124             var container = ContainerFactory.CreateWithAttributedCatalog(\r
125                 typeof(MethodExporter));\r
126 \r
127             // Cannot Create a standard delegate that takes 9 arguments\r
128             // so the generic Delegate type will not work\r
129             ExceptionAssert.Throws<CompositionContractMismatchException>(() =>\r
130                 container.GetExportedValue<Delegate>("FunctionWith9Arguments"));\r
131 \r
132             // If a matching custom delegate type is used then it will work\r
133             Assert.IsNotNull(container.GetExportedValue<FuncWith9Args>("FunctionWith9Arguments"));\r
134         }\r
135 \r
136         [TestMethod]\r
137         public void Export_ActionWith8Arguments_ShouldWorkFine()\r
138         {\r
139             var container = ContainerFactory.CreateWithAttributedCatalog(\r
140                 typeof(MethodExporter));\r
141 \r
142 #if CLR40 && !SILVERLIGHT\r
143             Assert.IsNotNull(container.GetExportedValue<Delegate>("ActionWith8Arguments"));\r
144 #else\r
145             ExceptionAssert.Throws<CompositionContractMismatchException>(() =>\r
146                 container.GetExportedValue<Delegate>("ActionWith8Arguments"));\r
147 #endif\r
148         }\r
149 \r
150         public delegate void ActionWith9Args(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9);\r
151 \r
152         [TestMethod]\r
153         public void Export_ActionWith9Arguments_ShouldThrowContractMismatch()\r
154         {\r
155             var container = ContainerFactory.CreateWithAttributedCatalog(\r
156                 typeof(MethodExporter));\r
157 \r
158             // Cannot Create a standard delegate that takes 9 arguments\r
159             // so the generic Delegate type will not work\r
160             ExceptionAssert.Throws<CompositionContractMismatchException>(() =>\r
161                 container.GetExportedValue<Delegate>("ActionWith9Arguments"));\r
162 \r
163             // If a matching custom delegate type is used then it will work\r
164             Assert.IsNotNull(container.GetExportedValue<ActionWith9Args>("ActionWith9Arguments"));\r
165         }\r
166 \r
167 #if CLR40 && !SILVERLIGHT\r
168         [TestMethod]\r
169         public void Export_FunctionWithDefaultValue_ShouldWorkFine()\r
170         {\r
171             var container = ContainerFactory.CreateWithAttributedCatalog(\r
172                 typeof(MethodExporter));\r
173 \r
174             var export = container.GetExportedValue<Func<int, string, int>>("FunctionWithDefaultValue");\r
175             Assert.AreEqual(3, export(3, "a"));\r
176 \r
177             // Even though the string argument is optional it still cannot be cast to Func<int, int>.\r
178             var export2 = (ExportedDelegate)container.GetExportedValue<object>("FunctionWithDefaultValue");\r
179             var export3 = export2.CreateDelegate(typeof(Func<int, int>));\r
180 \r
181             Assert.IsNull(export3);\r
182         }\r
183 #endif\r
184 \r
185 \r
186         public delegate int DelegateOneArg(int i);\r
187         public delegate int DelegateTwoArgs(int i, int j);\r
188 \r
189         public class CustomExportedDelegate : ExportedDelegate\r
190         {\r
191             private Func<int, int, int> _func;\r
192 \r
193             public CustomExportedDelegate(Func<int, int, int> func)\r
194             {\r
195                 this._func = func;\r
196             }\r
197 \r
198             public override Delegate CreateDelegate(Type delegateType)\r
199             {\r
200                 if (delegateType == typeof(DelegateOneArg))\r
201                 {\r
202                     return (DelegateOneArg)((i) => this._func(i, 0));\r
203                 }\r
204                 else if (delegateType == typeof(DelegateTwoArgs))\r
205                 {\r
206                     return (DelegateTwoArgs)((i, j) => this._func(i, j));\r
207                 }\r
208 \r
209                 return null;\r
210             }\r
211         }\r
212 \r
213         public class ExportCustomExportedDelegates\r
214         {\r
215             [Export("CustomExportedDelegate", typeof(DelegateOneArg))]\r
216             [Export("CustomExportedDelegate", typeof(DelegateTwoArgs))]\r
217             public ExportedDelegate MyExportedDelegate\r
218             {\r
219                 get\r
220                 {\r
221                     return new CustomExportedDelegate(DoWork);\r
222                 }\r
223             }\r
224 \r
225             public int DoWork(int i, int j)\r
226             {\r
227                 return i + j;\r
228             }\r
229         }\r
230 \r
231         [Export]\r
232         public class ImportCustomExportedDelegates\r
233         {\r
234             [Import("CustomExportedDelegate")]\r
235             public DelegateOneArg DelegateOneArg { get; set; }\r
236 \r
237             [Import("CustomExportedDelegate")]\r
238             public DelegateTwoArgs DelegateTwoArgs { get; set; }\r
239 \r
240         }\r
241 \r
242         [TestMethod]\r
243         public void CustomExportedDelegate_ShouldWork()\r
244         {\r
245             var container = ContainerFactory.CreateWithAttributedCatalog(\r
246                 typeof(ExportCustomExportedDelegates),\r
247                 typeof(ImportCustomExportedDelegates));\r
248 \r
249             var importer = container.GetExportedValue<ImportCustomExportedDelegates>();\r
250 \r
251             Assert.AreEqual(1, importer.DelegateOneArg(1));\r
252             Assert.AreEqual(2, importer.DelegateTwoArgs(1, 1));\r
253         }\r
254 \r
255         public delegate void GetRef(ref int i);\r
256         public delegate void GetOut(out int i);\r
257 \r
258         public class RefOutMethodExporter\r
259         {\r
260             [Export]\r
261             public void MyGetOut(out int i)\r
262             {\r
263                 i = 29;\r
264             }\r
265         }\r
266 \r
267         [TestMethod]\r
268         public void MethodWithOutParam_ShouldWorkWithRefParam()\r
269         {\r
270             var container = ContainerFactory.CreateWithAttributedCatalog(\r
271                 typeof(RefOutMethodExporter));\r
272 \r
273             int i = 0;\r
274 \r
275             var export1 = container.GetExportedValue<GetRef>();\r
276             \r
277             export1(ref i);\r
278             Assert.AreEqual(29, i);\r
279             i = 0;\r
280 \r
281             var export2 = container.GetExportedValue<GetOut>();\r
282             \r
283             export2(out i);\r
284             Assert.AreEqual(29, i);\r
285         }\r
286     }\r
287 }\r