1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.Collections.Generic;
\r
6 using System.ComponentModel.Composition;
\r
8 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
9 using System.ComponentModel.Composition.Factories;
\r
10 using System.ComponentModel.Composition.Hosting;
\r
11 using System.ComponentModel.Composition.UnitTesting;
\r
12 using System.ComponentModel.Composition.AttributedModel;
\r
13 using System.UnitTesting;
\r
15 namespace System.ComponentModel.Composition
\r
18 public class AdvancedValueComposition
\r
21 public void RepeatedContainerUse()
\r
23 var container = ContainerFactory.Create();
\r
24 TrivialExporter e = new TrivialExporter();
\r
26 CompositionBatch batch = new CompositionBatch();
\r
28 container.Compose(batch);
\r
30 batch = new CompositionBatch();
\r
31 batch.AddPart(new TrivialImporter());
\r
32 container.Compose(batch);
\r
34 Assert.IsTrue(e.done, "Initialization of importer should have set the done flag on E");
\r
38 public void FunctionsFieldsAndProperties()
\r
41 var container = ContainerFactory.Create();
\r
43 CompositionBatch batch = new CompositionBatch();
\r
44 batch.AddPart(new RealAddProvider());
\r
45 batch.AddPart(c = new Consumer());
\r
46 container.Compose(batch);
\r
48 Assert.AreEqual(3, c.op(c.a, c.b), "1 + 2 == 3");
\r
51 public void FunctionsFieldsAndProperties2()
\r
54 var container = ContainerFactory.Create();
\r
56 CompositionBatch batch = new CompositionBatch();
\r
57 batch.AddPart(new SubtractProvider());
\r
58 batch.AddPart(c = new Consumer());
\r
59 container.Compose(batch);
\r
61 Assert.AreEqual(-1, c.op(c.a, c.b), "1 - 2 == -1");
\r
65 public void FunctionsFieldsAndProperties2_WithCatalog()
\r
67 var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
69 ConsumerOfMultiple c = new ConsumerOfMultiple();
\r
70 CompositionBatch batch = new CompositionBatch();
\r
72 container.Compose(batch);
\r
74 foreach (var export in c.opInfo)
\r
76 if ((string)export.Metadata["Var1"] == "add")
\r
78 Assert.AreEqual(3, export.Value(1, 2), "1 + 2 == 3");
\r
80 else if ((string)export.Metadata["Var1"] == "sub")
\r
82 Assert.AreEqual(-1, export.Value(1, 2), "1 - 2 == -1");
\r
86 Assert.Fail("Unexpected value");
\r
92 // Silverlight doesn't support strongly typed metadata
\r
94 public void FunctionsFieldsAndProperties2_StronglyTypedMetadata()
\r
96 var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
98 var exports = container.GetExports<Func<int, int, int>, IExportableTest>("Add");
\r
100 foreach (var export in exports)
\r
102 if (export.Metadata.Var1 == "add")
\r
104 Assert.AreEqual(3, export.Value(1, 2), "1 + 2 == 3");
\r
106 else if (export.Metadata.Var1 == "sub")
\r
108 Assert.AreEqual(-1, export.Value(1, 2), "1 - 2 == -1");
\r
112 Assert.Fail("Unexpected value");
\r
118 public void InAdditionToCatalogTest()
\r
120 var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
122 IDictionary<string, object> multMetadata = new Dictionary<string, object>();
\r
123 multMetadata["Var1"]= "mult";
\r
124 multMetadata[CompositionConstants.ExportTypeIdentityMetadataName] = AttributedModelServices.GetTypeIdentity(typeof(Func<int, int, int>));
\r
125 var basicValue = ExportFactory.Create("Add", multMetadata, (() => (Func<int, int, int>)delegate(int a, int b) { return a * b; }));
\r
127 CompositionBatch batch = new CompositionBatch();
\r
128 batch.AddExport(basicValue);
\r
129 container.Compose(batch);
\r
131 var exports = container.GetExports<Func<int, int, int>, IExportableTest>("Add");
\r
133 Assert.AreEqual(3, exports.Count(), "There should be 3 entries for 'Add'");
\r
135 foreach (var export in exports)
\r
137 if (export.Metadata.Var1 == "mult")
\r
139 Assert.AreEqual(2, export.Value(1, 2), "1 * 2 == 2");
\r
141 else if (export.Metadata.Var1 == "add")
\r
143 Assert.AreEqual(3, export.Value(1, 2), "1 + 2 == 3");
\r
145 else if (export.Metadata.Var1 == "sub")
\r
147 Assert.AreEqual(-1, export.Value(1, 2), "1 - 2 == -1");
\r
151 Assert.Fail("Unexpected value");
\r
157 public void CollectionMetadataPropertyTest()
\r
159 var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
160 var export = container.GetExport<ComponentWithCollectionProperty, ICollectionOfStrings>();
\r
162 Assert.IsNotNull(export.Metadata, "Should have metadata");
\r
163 Assert.IsNotNull(export.Metadata.Values, "MetadataView should have collection of values");
\r
164 Assert.AreEqual(export.Metadata.Values.Count(), 3, "Should have 3 elements");
\r
165 Assert.AreEqual(export.Metadata.Values.First(), "One", "First should be 'One'");
\r
166 Assert.AreEqual(export.Metadata.Values.Skip(1).First(), "two", "First should be 'two'");
\r
167 Assert.AreEqual(export.Metadata.Values.Skip(2).First(), "3", "First should be '3'");
\r
169 #endif //!SIVLERLIGHT
\r
172 public void ImportExportSansNameTest()
\r
174 var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
176 UnnamedImportAndExport unnamed = container.GetExportedValue<UnnamedImportAndExport>();
\r
177 Assert.IsNotNull(unnamed, "Should have found UnnamedImportAndExport component");
\r
178 Assert.IsNotNull(unnamed.ImportedValue, "Component's unnamed import should have been fulfilled");
\r
182 public void MultipleInstantiationOfStaticCatalogItem()
\r
184 var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
185 var unnamedVI = container.GetExport<StaticExport, object>();
\r
187 StaticExport first = unnamedVI.Value;
\r
188 StaticExport second = unnamedVI.Value;
\r
190 Assert.IsNotNull(first, "Should have created an instance");
\r
191 Assert.IsNotNull(second, "Should have created a second instance");
\r
192 Assert.IsTrue(object.ReferenceEquals(first, second), "Instances should be the same");
\r
194 var exports = container.GetExports<StaticExport, object>();
\r
196 Assert.AreEqual(1, exports.Count(), "There should still only be one exported value");
\r
200 public void MultipleInstantiationOfNonStaticCatalogItem()
\r
202 var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
203 var export1 = container.GetExport<NonStaticExport, object>();
\r
204 var export2 = container.GetExport<NonStaticExport, object>();
\r
206 NonStaticExport first = export1.Value;
\r
207 NonStaticExport second = export2.Value;
\r
209 Assert.IsNotNull(first, "Should have created an instance");
\r
210 Assert.IsNotNull(second, "Should have created a second instance");
\r
211 Assert.IsFalse(object.ReferenceEquals(first, second), "Instances should be different");
\r
215 public void ImportIntoUntypedExportTest()
\r
217 var container = ContainerFactory.Create();
\r
219 CompositionBatch batch = new CompositionBatch();
\r
220 batch.AddExportedValue("untyped", 42);
\r
222 var u = new UntypedExportImporter();
\r
223 var rb = AttributedModelServices.CreatePart(u);
\r
226 container.Compose(batch);
\r
228 Assert.AreEqual(42, u.Export.Value);
\r
230 var us = new UntypedExportsImporter();
\r
231 batch = new CompositionBatch();
\r
232 batch.AddExportedValue("untyped", 19);
\r
233 batch.RemovePart(rb);
\r
235 container.Compose(batch);
\r
237 Assert.IsNotNull(us.Exports, "Should have an enumeration");
\r
238 Assert.AreEqual(2, us.Exports.Count(), "Should have 2 values");
\r
242 public void ImportIntoDerivationOfExportException()
\r
244 var container = ContainerFactory.Create();
\r
246 CompositionBatch batch = new CompositionBatch();
\r
247 batch.AddExportedValue("derived", typeof(DerivedExport), 42);
\r
248 var d = new DerivedExportImporter();
\r
251 CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
\r
252 ErrorId.ReflectionModel_ImportNotAssignableFromExport, RetryMode.DoNotRetry, () =>
\r
254 container.Compose(batch);
\r
259 public void ImportIntoDerivationOfExportsException()
\r
261 var container = ContainerFactory.Create();
\r
263 CompositionBatch batch = new CompositionBatch();
\r
264 batch.AddExportedValue("derived", typeof(DerivedExport), 42);
\r
265 var d = new DerivedExportsImporter();
\r
268 CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
\r
269 ErrorId.ReflectionModel_ImportNotAssignableFromExport, RetryMode.DoNotRetry, () =>
\r
271 container.Compose(batch);
\r