Remove excessive shortcut key matching in ToolStrip
[mono.git] / mcs / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / ComponentModel / Composition / ExportCollectionTests.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.Linq;\r
6 using System.Collections.Generic;\r
7 using System.Collections.ObjectModel;\r
8 using System.ComponentModel.Composition.Factories;\r
9 using System.ComponentModel.Composition.Hosting;\r
10 using System.ComponentModel.Composition.Primitives;\r
11 using System.ComponentModel.Composition.UnitTesting;\r
12 using System.UnitTesting;\r
13 using Microsoft.VisualStudio.TestTools.UnitTesting;\r
14 \r
15 namespace System.ComponentModel.Composition\r
16 {\r
17     [TestClass]\r
18     public class ExportCollectionTests\r
19     {\r
20         public interface ICustomMetadata\r
21         {\r
22             bool PropertyName { get; }\r
23         }\r
24 \r
25         public class Importer\r
26         {\r
27             [ImportMany("Value")]\r
28             public Collection<Lazy<object>> CollectionPlain { get; set; }\r
29 \r
30             [ImportMany("Value")]\r
31             public Collection<Lazy<object, IDictionary<string, object>>> CollectionPlainRawMetadata { get; set; }\r
32 \r
33             [ImportMany("EmptyValue")]\r
34             public Collection<Lazy<object>> CollectionPlainEmpty { get; set; }\r
35 \r
36             [ImportMany("EmptyValue")]\r
37             public Collection<Lazy<object, IDictionary<string, object>>> CollectionPlainEmptyRawMetadata { get; set; }\r
38 \r
39             [ImportMany("Value")]\r
40             public Collection<Lazy<int>> CollectionTyped { get; set; }\r
41 \r
42             [ImportMany("Value")]\r
43             public Collection<Lazy<int, IDictionary<string, object>>> CollectionTypedRawMetadata { get; set; }\r
44 \r
45             [ImportMany("EmptyValue")]\r
46             public Collection<Lazy<int>> CollectionTypedEmpty { get; set; }\r
47 \r
48             [ImportMany("Value")]\r
49             public Collection<Lazy<int, ICustomMetadata>> CollectionTypedMetadata { get; set; }\r
50 \r
51             [ImportMany("EmptyValue")]\r
52             public Collection<Lazy<int, ICustomMetadata>> CollectionTypedMetadataEmpty { get; set; }\r
53 \r
54             [ImportMany("Value")]\r
55             public IEnumerable<int> ReadWriteEnumerable { get; set; }\r
56 \r
57             [ImportMany("EmptyValue")]\r
58             public IEnumerable<int> ReadWriteEnumerableEmpty { get; set; }\r
59 \r
60             [ImportMany("Value")]\r
61             public IEnumerable<Lazy<object>> MetadataUntypedEnumerable { get; set; }\r
62 \r
63             [ImportMany("Value")]\r
64             public IEnumerable<Lazy<object, IDictionary<string, object>>> MetadataUntypedEnumerableRawMetadata { get; set; }\r
65 \r
66             [ImportMany("EmptyValue")]\r
67             public IEnumerable<Lazy<object>> MetadataUntypedEnumerableEmpty { get; set; }\r
68 \r
69             [ImportMany("EmptyValue")]\r
70             public IEnumerable<Lazy<object, IDictionary<string, object>>> MetadataUntypedEnumerableEmptyRawMetadata { get; set; }\r
71 \r
72             [ImportMany("Value")]\r
73             public IEnumerable<Lazy<int>> MetadataTypedEnumerable { get; set; }\r
74 \r
75             [ImportMany("Value")]\r
76             public IEnumerable<Lazy<int, IDictionary<string, object>>> MetadataTypedEnumerableRawMetadata { get; set; }\r
77 \r
78             [ImportMany("EmptyValue")]\r
79             public IEnumerable<Lazy<int>> MetadataTypedEnumerableEmpty { get; set; }\r
80 \r
81             [ImportMany("Value")]\r
82             public IEnumerable<Lazy<int, ICustomMetadata>> MetadataFullyTypedEnumerable { get; set; }\r
83 \r
84             [ImportMany("EmptyValue")]\r
85             public IEnumerable<Lazy<int, ICustomMetadata>> MetadataFullyTypedEnumerableEmpty { get; set; }\r
86 \r
87             public void VerifyImport(params int[] expectedValues)\r
88             {\r
89                 object[] untypedExpectedValues = expectedValues.Cast<object>().ToArray();\r
90 \r
91                 ExportsAssert.AreEqual(CollectionPlain, untypedExpectedValues);\r
92                 ExportsAssert.AreEqual(CollectionPlainRawMetadata, untypedExpectedValues);\r
93                 EnumerableAssert.IsTrueForAll(CollectionPlainRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));\r
94                 EnumerableAssert.IsEmpty(CollectionPlainEmpty);\r
95                 EnumerableAssert.IsEmpty(CollectionPlainEmptyRawMetadata);\r
96 \r
97                 // Add a new Export to this collection to ensure that it doesn't\r
98                 // modifiy the other collections because they should each have there \r
99                 // own collection instance\r
100                 CollectionPlain.Add(ExportFactory.Create<object>("Value"));\r
101 \r
102                 ExportsAssert.AreEqual(CollectionTyped, expectedValues);\r
103                 ExportsAssert.AreEqual(CollectionTypedRawMetadata, expectedValues);\r
104                 EnumerableAssert.IsTrueForAll(CollectionTypedRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));\r
105                 EnumerableAssert.IsEmpty(CollectionTypedEmpty);\r
106 \r
107                 ExportsAssert.AreEqual(CollectionTypedMetadata, expectedValues);\r
108 #if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata\r
109                 EnumerableAssert.IsTrueForAll(CollectionTypedMetadata, i => true == i.Metadata.PropertyName);\r
110 #endif //!SILVERLIGHT\r
111                 EnumerableAssert.IsEmpty(CollectionTypedMetadataEmpty);\r
112 \r
113                 EnumerableAssert.AreEqual(ReadWriteEnumerable, expectedValues);\r
114                 EnumerableAssert.IsEmpty(ReadWriteEnumerableEmpty);\r
115 \r
116                 ExportsAssert.AreEqual(MetadataUntypedEnumerable, untypedExpectedValues);\r
117                 ExportsAssert.AreEqual(MetadataUntypedEnumerableRawMetadata, untypedExpectedValues);\r
118                 EnumerableAssert.IsTrueForAll(MetadataUntypedEnumerableRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));\r
119                 EnumerableAssert.IsEmpty(MetadataUntypedEnumerableEmpty);\r
120                 EnumerableAssert.IsEmpty(MetadataUntypedEnumerableEmptyRawMetadata);\r
121 \r
122                 ExportsAssert.AreEqual(MetadataTypedEnumerable, expectedValues);\r
123                 ExportsAssert.AreEqual(MetadataTypedEnumerableRawMetadata, expectedValues);\r
124                 EnumerableAssert.IsTrueForAll(MetadataTypedEnumerableRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));\r
125                 EnumerableAssert.IsEmpty(MetadataTypedEnumerableEmpty);\r
126 \r
127                 ExportsAssert.AreEqual(MetadataFullyTypedEnumerable, expectedValues);\r
128 #if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata\r
129                 EnumerableAssert.IsTrueForAll(MetadataFullyTypedEnumerable, i => true == i.Metadata.PropertyName);\r
130 #endif //!SILVERLIGHT\r
131                 EnumerableAssert.IsEmpty(MetadataFullyTypedEnumerableEmpty);\r
132             }\r
133         }\r
134 \r
135         public class ExporterDefault21\r
136         {\r
137             public ExporterDefault21() { Value = 21; }\r
138             public ExporterDefault21(int v) { Value = v; }\r
139 \r
140             [Export("Value")]\r
141             [ExportMetadata("PropertyName", true)]\r
142             public int Value { get; set; }\r
143         }\r
144 \r
145         public class ExporterDefault42\r
146         {\r
147             public ExporterDefault42() { Value = 42; }\r
148             public ExporterDefault42(int v) { Value = v; }\r
149 \r
150             [Export("Value")]\r
151             [ExportMetadata("PropertyName", true)]\r
152             public int Value { get; set; }\r
153         }\r
154 \r
155 \r
156         [TestMethod]\r
157         [TestProperty("Type", "Integration")]\r
158         public void ImportCollectionsFromContainerOnly()\r
159         {\r
160             var container = ContainerFactory.Create();\r
161             Importer importer = new Importer();\r
162 \r
163             CompositionBatch batch = new CompositionBatch();\r
164             batch.AddParts(importer\r
165                 , new ExporterDefault21()\r
166                 , new ExporterDefault21(22)\r
167                 , new ExporterDefault42()\r
168                 , new ExporterDefault42(43));\r
169 \r
170             container.Compose(batch);\r
171 \r
172             importer.VerifyImport(21, 22, 42, 43);\r
173         }\r
174 \r
175         [TestMethod]\r
176         [TestProperty("Type", "Integration")]\r
177         public void ImportCollectionsFromCatalogOnly()\r
178         {\r
179             var cat = CatalogFactory.CreateDefaultAttributed();\r
180             var container = new CompositionContainer(cat);\r
181             Importer importer = new Importer();\r
182 \r
183             CompositionBatch batch = new CompositionBatch();\r
184             batch.AddParts(importer);\r
185             container.Compose(batch);\r
186 \r
187             importer.VerifyImport(21, 42);\r
188         }\r
189 \r
190         [TestMethod]\r
191         [TestProperty("Type", "Integration")]\r
192         public void ImportCollectionsFormContainerAndCatalog()\r
193         {\r
194             var cat = CatalogFactory.CreateDefaultAttributed();\r
195             var container = new CompositionContainer(cat);\r
196             Importer importer = new Importer();\r
197 \r
198             CompositionBatch batch = new CompositionBatch();\r
199             batch.AddParts(importer\r
200                 , new ExporterDefault21(22)\r
201                 , new ExporterDefault42(43));\r
202 \r
203             container.Compose(batch);\r
204 \r
205             importer.VerifyImport(22, 43, 21, 42);\r
206         }\r
207     }\r
208 }\r