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
7 using System.ComponentModel.Composition.Factories;
\r
8 using System.ComponentModel.Composition.Hosting;
\r
10 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
12 namespace System.ComponentModel.Composition
\r
15 public class InitializationScopeTests
\r
18 public void SingleContainerSimpleCompose()
\r
20 var container = ContainerFactory.Create();
\r
21 ImportingComposablePart importPart;
\r
22 CompositionBatch batch = new CompositionBatch();
\r
24 batch.AddExportedValue("value1", "Hello");
\r
25 batch.AddExportedValue("value2", "World");
\r
26 batch.AddPart(importPart = PartFactory.CreateImporter("value1", "value2"));
\r
27 container.Compose(batch);
\r
29 Assert.AreEqual(2, importPart.ImportSatisfiedCount);
\r
30 Assert.AreEqual("Hello", importPart.GetImport("value1"));
\r
31 Assert.AreEqual("World", importPart.GetImport("value2"));
\r
35 public void ParentedContainerSimpleCompose()
\r
37 var container = ContainerFactory.Create();
\r
38 var importPart = PartFactory.CreateImporter("value1", "value2");
\r
40 CompositionBatch batch = new CompositionBatch();
\r
41 batch.AddExportedValue("value1", "Parent");
\r
43 var childContainer = new CompositionContainer(container);
\r
44 CompositionBatch childBatch = new CompositionBatch();
\r
45 childBatch.AddExportedValue("value2", "Child");
\r
46 childBatch.AddPart(importPart);
\r
48 Assert.AreEqual(0, importPart.ImportSatisfiedCount, "Import should not happen until outer scope is disposed");
\r
50 container.Compose(batch);
\r
51 childContainer.Compose(childBatch);
\r
53 Assert.AreEqual(2, importPart.ImportSatisfiedCount);
\r
54 Assert.AreEqual("Parent", importPart.GetImport("value1"));
\r
55 Assert.AreEqual("Child", importPart.GetImport("value2"));
\r
59 public void SingleContainerPartReplacement()
\r
61 var container = ContainerFactory.Create();
\r
62 var importPart = PartFactory.CreateImporter(true, "value1", "value2");
\r
64 CompositionBatch batch = new CompositionBatch();
\r
65 var export1Key = batch.AddExportedValue("value1", "Hello");
\r
66 batch.AddExportedValue("value2", "World");
\r
67 batch.AddPart(importPart);
\r
68 container.Compose(batch);
\r
70 Assert.AreEqual(2, importPart.ImportSatisfiedCount);
\r
71 Assert.AreEqual("Hello", importPart.GetImport("value1"));
\r
72 Assert.AreEqual("World", importPart.GetImport("value2"));
\r
74 importPart.ResetImportSatisfiedCount();
\r
76 batch = new CompositionBatch();
\r
77 batch.RemovePart(export1Key);
\r
78 batch.AddExportedValue("value1", "Goodbye");
\r
79 container.Compose(batch);
\r
81 Assert.AreEqual(1, importPart.ImportSatisfiedCount);
\r
82 Assert.AreEqual("Goodbye", importPart.GetImport("value1"));
\r
83 Assert.AreEqual("World", importPart.GetImport("value2"));
\r
87 public void ParentedContainerPartReplacement()
\r
89 var container = ContainerFactory.Create();
\r
90 CompositionBatch batch = new CompositionBatch();
\r
91 var importPart = PartFactory.CreateImporter(true, "value1", "value2");
\r
92 var exportKey = batch.AddExportedValue("value1", "Parent");
\r
94 var childContainer = new CompositionContainer(container);
\r
95 CompositionBatch childBatch = new CompositionBatch();
\r
96 childBatch.AddExportedValue("value2", "Child");
\r
97 childBatch.AddPart(importPart);
\r
99 Assert.AreEqual(0, importPart.ImportSatisfiedCount, "Should not import until outer scope is disposed");
\r
100 container.Compose(batch);
\r
101 childContainer.Compose(childBatch);
\r
103 Assert.AreEqual(2, importPart.ImportSatisfiedCount);
\r
104 Assert.AreEqual("Parent", importPart.GetImport("value1"));
\r
105 Assert.AreEqual("Child", importPart.GetImport("value2"));
\r
107 importPart.ResetImportSatisfiedCount();
\r
108 batch = new CompositionBatch();
\r
109 batch.RemovePart(exportKey);
\r
110 batch.AddExportedValue("value1", "New Parent");
\r
111 container.Compose(batch);
\r
113 Assert.AreEqual(1, importPart.ImportSatisfiedCount);
\r
114 Assert.AreEqual("New Parent", importPart.GetImport("value1"));
\r
115 Assert.AreEqual("Child", importPart.GetImport("value2"));
\r
119 public void SelectiveRecompose()
\r
121 var container = ContainerFactory.Create();
\r
122 var stableImporter = PartFactory.CreateImporter("stable");
\r
123 var dynamicImporter = PartFactory.CreateImporter("dynamic", true);
\r
124 CompositionBatch batch = new CompositionBatch();
\r
126 batch.AddPart(stableImporter);
\r
127 batch.AddPart(dynamicImporter);
\r
128 var exportKey = batch.AddExportedValue("dynamic", 1);
\r
129 batch.AddExportedValue("stable", 42);
\r
130 container.Compose(batch);
\r
132 Assert.AreEqual(1, stableImporter.ImportSatisfiedCount);
\r
133 Assert.AreEqual(stableImporter.GetImport("stable"), 42);
\r
134 Assert.AreEqual(1, dynamicImporter.ImportSatisfiedCount);
\r
135 Assert.AreEqual(dynamicImporter.GetImport("dynamic"), 1);
\r
137 batch = new CompositionBatch();
\r
138 stableImporter.ResetImportSatisfiedCount();
\r
139 dynamicImporter.ResetImportSatisfiedCount();
\r
140 batch.RemovePart(exportKey);
\r
141 batch.AddExportedValue("dynamic", 2);
\r
142 container.Compose(batch);
\r
144 Assert.AreEqual(0, stableImporter.ImportSatisfiedCount, "Should not have imported the stable import part");
\r
145 Assert.AreEqual(stableImporter.GetImport("stable"), 42);
\r
146 Assert.AreEqual(1, dynamicImporter.ImportSatisfiedCount);
\r
147 Assert.AreEqual(dynamicImporter.GetImport("dynamic"), 2);
\r