Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / ComponentModel / Composition / InitializationScopeTests.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\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
9 using System.Linq;\r
10 using Microsoft.VisualStudio.TestTools.UnitTesting;\r
11 \r
12 namespace System.ComponentModel.Composition\r
13 {\r
14     [TestClass]\r
15     public class InitializationScopeTests\r
16     {\r
17         [TestMethod]\r
18         public void SingleContainerSimpleCompose()\r
19         {\r
20             var container = ContainerFactory.Create();\r
21             ImportingComposablePart importPart;\r
22             CompositionBatch batch = new CompositionBatch();\r
23 \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
28 \r
29             Assert.AreEqual(2, importPart.ImportSatisfiedCount);\r
30             Assert.AreEqual("Hello", importPart.GetImport("value1"));\r
31             Assert.AreEqual("World", importPart.GetImport("value2"));\r
32         }\r
33 \r
34         [TestMethod]\r
35         public void ParentedContainerSimpleCompose()\r
36         {\r
37             var container = ContainerFactory.Create();\r
38             var importPart = PartFactory.CreateImporter("value1", "value2");\r
39 \r
40             CompositionBatch batch = new CompositionBatch();\r
41             batch.AddExportedValue("value1", "Parent");\r
42 \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
47 \r
48             Assert.AreEqual(0, importPart.ImportSatisfiedCount, "Import should not happen until outer scope is disposed");\r
49 \r
50             container.Compose(batch);\r
51             childContainer.Compose(childBatch);\r
52 \r
53             Assert.AreEqual(2, importPart.ImportSatisfiedCount);\r
54             Assert.AreEqual("Parent", importPart.GetImport("value1"));\r
55             Assert.AreEqual("Child", importPart.GetImport("value2"));\r
56         }\r
57 \r
58         [TestMethod]\r
59         public void SingleContainerPartReplacement()\r
60         {\r
61             var container = ContainerFactory.Create();\r
62             var importPart = PartFactory.CreateImporter(true, "value1", "value2");\r
63 \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
69 \r
70             Assert.AreEqual(2, importPart.ImportSatisfiedCount);\r
71             Assert.AreEqual("Hello", importPart.GetImport("value1"));\r
72             Assert.AreEqual("World", importPart.GetImport("value2"));\r
73 \r
74             importPart.ResetImportSatisfiedCount();\r
75 \r
76             batch = new CompositionBatch();\r
77             batch.RemovePart(export1Key);\r
78             batch.AddExportedValue("value1", "Goodbye");\r
79             container.Compose(batch);\r
80 \r
81             Assert.AreEqual(1, importPart.ImportSatisfiedCount);\r
82             Assert.AreEqual("Goodbye", importPart.GetImport("value1"));\r
83             Assert.AreEqual("World", importPart.GetImport("value2"));\r
84         }\r
85 \r
86         [TestMethod]\r
87         public void ParentedContainerPartReplacement()\r
88         {\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
93 \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
98 \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
102 \r
103             Assert.AreEqual(2, importPart.ImportSatisfiedCount);\r
104             Assert.AreEqual("Parent", importPart.GetImport("value1"));\r
105             Assert.AreEqual("Child", importPart.GetImport("value2"));\r
106 \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
112 \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
116         }\r
117 \r
118         [TestMethod]\r
119         public void SelectiveRecompose()\r
120         {\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
125 \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
131 \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
136 \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
143 \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
148         }\r
149     }\r
150 }\r