1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\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
15 namespace System.ComponentModel.Composition
\r
18 public class ExportCollectionTests
\r
20 public interface ICustomMetadata
\r
22 bool PropertyName { get; }
\r
25 public class Importer
\r
27 [ImportMany("Value")]
\r
28 public Collection<Lazy<object>> CollectionPlain { get; set; }
\r
30 [ImportMany("Value")]
\r
31 public Collection<Lazy<object, IDictionary<string, object>>> CollectionPlainRawMetadata { get; set; }
\r
33 [ImportMany("EmptyValue")]
\r
34 public Collection<Lazy<object>> CollectionPlainEmpty { get; set; }
\r
36 [ImportMany("EmptyValue")]
\r
37 public Collection<Lazy<object, IDictionary<string, object>>> CollectionPlainEmptyRawMetadata { get; set; }
\r
39 [ImportMany("Value")]
\r
40 public Collection<Lazy<int>> CollectionTyped { get; set; }
\r
42 [ImportMany("Value")]
\r
43 public Collection<Lazy<int, IDictionary<string, object>>> CollectionTypedRawMetadata { get; set; }
\r
45 [ImportMany("EmptyValue")]
\r
46 public Collection<Lazy<int>> CollectionTypedEmpty { get; set; }
\r
48 [ImportMany("Value")]
\r
49 public Collection<Lazy<int, ICustomMetadata>> CollectionTypedMetadata { get; set; }
\r
51 [ImportMany("EmptyValue")]
\r
52 public Collection<Lazy<int, ICustomMetadata>> CollectionTypedMetadataEmpty { get; set; }
\r
54 [ImportMany("Value")]
\r
55 public IEnumerable<int> ReadWriteEnumerable { get; set; }
\r
57 [ImportMany("EmptyValue")]
\r
58 public IEnumerable<int> ReadWriteEnumerableEmpty { get; set; }
\r
60 [ImportMany("Value")]
\r
61 public IEnumerable<Lazy<object>> MetadataUntypedEnumerable { get; set; }
\r
63 [ImportMany("Value")]
\r
64 public IEnumerable<Lazy<object, IDictionary<string, object>>> MetadataUntypedEnumerableRawMetadata { get; set; }
\r
66 [ImportMany("EmptyValue")]
\r
67 public IEnumerable<Lazy<object>> MetadataUntypedEnumerableEmpty { get; set; }
\r
69 [ImportMany("EmptyValue")]
\r
70 public IEnumerable<Lazy<object, IDictionary<string, object>>> MetadataUntypedEnumerableEmptyRawMetadata { get; set; }
\r
72 [ImportMany("Value")]
\r
73 public IEnumerable<Lazy<int>> MetadataTypedEnumerable { get; set; }
\r
75 [ImportMany("Value")]
\r
76 public IEnumerable<Lazy<int, IDictionary<string, object>>> MetadataTypedEnumerableRawMetadata { get; set; }
\r
78 [ImportMany("EmptyValue")]
\r
79 public IEnumerable<Lazy<int>> MetadataTypedEnumerableEmpty { get; set; }
\r
81 [ImportMany("Value")]
\r
82 public IEnumerable<Lazy<int, ICustomMetadata>> MetadataFullyTypedEnumerable { get; set; }
\r
84 [ImportMany("EmptyValue")]
\r
85 public IEnumerable<Lazy<int, ICustomMetadata>> MetadataFullyTypedEnumerableEmpty { get; set; }
\r
87 public void VerifyImport(params int[] expectedValues)
\r
89 object[] untypedExpectedValues = expectedValues.Cast<object>().ToArray();
\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
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
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
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
113 EnumerableAssert.AreEqual(ReadWriteEnumerable, expectedValues);
\r
114 EnumerableAssert.IsEmpty(ReadWriteEnumerableEmpty);
\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
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
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
135 public class ExporterDefault21
\r
137 public ExporterDefault21() { Value = 21; }
\r
138 public ExporterDefault21(int v) { Value = v; }
\r
141 [ExportMetadata("PropertyName", true)]
\r
142 public int Value { get; set; }
\r
145 public class ExporterDefault42
\r
147 public ExporterDefault42() { Value = 42; }
\r
148 public ExporterDefault42(int v) { Value = v; }
\r
151 [ExportMetadata("PropertyName", true)]
\r
152 public int Value { get; set; }
\r
157 [TestProperty("Type", "Integration")]
\r
158 public void ImportCollectionsFromContainerOnly()
\r
160 var container = ContainerFactory.Create();
\r
161 Importer importer = new Importer();
\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
170 container.Compose(batch);
\r
172 importer.VerifyImport(21, 22, 42, 43);
\r
176 [TestProperty("Type", "Integration")]
\r
177 public void ImportCollectionsFromCatalogOnly()
\r
179 var cat = CatalogFactory.CreateDefaultAttributed();
\r
180 var container = new CompositionContainer(cat);
\r
181 Importer importer = new Importer();
\r
183 CompositionBatch batch = new CompositionBatch();
\r
184 batch.AddParts(importer);
\r
185 container.Compose(batch);
\r
187 importer.VerifyImport(21, 42);
\r
191 [TestProperty("Type", "Integration")]
\r
192 public void ImportCollectionsFormContainerAndCatalog()
\r
194 var cat = CatalogFactory.CreateDefaultAttributed();
\r
195 var container = new CompositionContainer(cat);
\r
196 Importer importer = new Importer();
\r
198 CompositionBatch batch = new CompositionBatch();
\r
199 batch.AddParts(importer
\r
200 , new ExporterDefault21(22)
\r
201 , new ExporterDefault42(43));
\r
203 container.Compose(batch);
\r
205 importer.VerifyImport(22, 43, 21, 42);
\r