Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / Integration / CatalogFilteringTests.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.ComponentModel.Composition;\r
6 using System.ComponentModel.Composition.Factories;\r
7 using System.ComponentModel.Composition.Hosting;\r
8 using System.ComponentModel.Composition.Primitives;\r
9 using Microsoft.VisualStudio.TestTools.UnitTesting;\r
10 \r
11 namespace System.ComponentModel.Composition\r
12 {\r
13     [TestClass]\r
14     public class CatalogFilteringTests\r
15     {\r
16         [TestMethod]\r
17         public void FilteredCatalog_ScopeA()\r
18         {\r
19             var cat = GetCatalog();\r
20             var contA = new CompositionContainer(ScopeCatalog(cat, "A"));\r
21 \r
22             Assert.IsTrue(contA.IsPresent<ScopeAComponent1>());\r
23             Assert.IsTrue(contA.IsPresent<ScopeAComponent2>());\r
24             Assert.IsFalse(contA.IsPresent<ScopeBComponent>());\r
25             Assert.IsFalse(contA.IsPresent<ScopeCComponent>());\r
26         }\r
27 \r
28         [TestMethod]\r
29         public void FilteredCatalog_ScopeB()\r
30         {\r
31             var cat = GetCatalog();\r
32             var contA = new CompositionContainer(ScopeCatalog(cat, "A"));\r
33             var contB = new CompositionContainer(ScopeCatalog(cat, "B"), contA);\r
34 \r
35             Assert.IsTrue(contB.IsPresent<ScopeAComponent1>());\r
36             Assert.IsTrue(contB.IsPresent<ScopeAComponent2>());\r
37             Assert.IsTrue(contB.IsPresent<ScopeBComponent>());\r
38             Assert.IsFalse(contB.IsPresent<ScopeCComponent>());\r
39         }\r
40 \r
41         [TestMethod]\r
42         public void FilteredCatalog_ScopeC()\r
43         {\r
44             var cat = GetCatalog();\r
45             var contA = new CompositionContainer(ScopeCatalog(cat, "A"));\r
46             var contB = new CompositionContainer(ScopeCatalog(cat, "B"), contA);\r
47             var contC = new CompositionContainer(ScopeCatalog(cat, "C"), contB);\r
48 \r
49             Assert.IsTrue(contC.IsPresent<ScopeAComponent1>());\r
50             Assert.IsTrue(contC.IsPresent<ScopeAComponent2>());\r
51             Assert.IsTrue(contC.IsPresent<ScopeBComponent>());\r
52             Assert.IsTrue(contC.IsPresent<ScopeCComponent>());\r
53         }\r
54 \r
55         [TestMethod]\r
56         [Ignore]\r
57         [WorkItem(812029)]\r
58         public void FilteredCatalog_EventsFired()\r
59         {\r
60             var aggCatalog = CatalogFactory.CreateAggregateCatalog();\r
61             var cat1 = CatalogFactory.CreateAttributed(typeof(ScopeAComponent1), typeof(ScopeBComponent));\r
62 \r
63             var filteredCatalog = CatalogFactory.CreateFiltered(aggCatalog, \r
64                 partDef => partDef.Metadata.ContainsKey("Scope") &&\r
65                                     partDef.Metadata["Scope"].ToString() == "A");\r
66 \r
67             var container = ContainerFactory.Create(filteredCatalog);\r
68 \r
69             Assert.IsFalse(container.IsPresent<ScopeAComponent1>(), "sa before add");\r
70             Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb before add");\r
71 \r
72             aggCatalog.Catalogs.Add(cat1);\r
73 \r
74             Assert.IsTrue(container.IsPresent<ScopeAComponent1>(), "sa after add");\r
75             Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb after add");\r
76 \r
77             aggCatalog.Catalogs.Remove(cat1);\r
78 \r
79             Assert.IsFalse(container.IsPresent<ScopeAComponent1>(), "sa after remove");\r
80             Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb after remove");\r
81         }\r
82 \r
83         private ComposablePartCatalog GetCatalog()\r
84         {\r
85             return CatalogFactory.CreateAttributed(\r
86                 typeof(ScopeAComponent1),\r
87                 typeof(ScopeAComponent2),\r
88                 typeof(ScopeBComponent),\r
89                 typeof(ScopeCComponent));\r
90         }\r
91 \r
92         private ComposablePartCatalog ScopeCatalog(ComposablePartCatalog catalog, string scope)\r
93         {\r
94             return CatalogFactory.CreateFiltered(catalog,\r
95                          partDef => partDef.Metadata.ContainsKey("Scope") &&\r
96                                     partDef.Metadata["Scope"].ToString() == scope);\r
97         }\r
98 \r
99         [Export]\r
100         [PartMetadata("Scope", "A")]\r
101         public class ScopeAComponent1\r
102         {\r
103         }\r
104 \r
105         [Export]\r
106         [PartMetadata("Scope", "A")]\r
107         public class ScopeAComponent2\r
108         {\r
109             [Import]\r
110             public ScopeAComponent1 ScopeA { get; set; }\r
111         }\r
112 \r
113         [Export]\r
114         [PartMetadata("Scope", "B")]\r
115         public class ScopeBComponent\r
116         {\r
117             [Import]\r
118             public ScopeAComponent1 ScopeA { get; set; }\r
119         }\r
120 \r
121         [Export]\r
122         [PartMetadata("Scope", "C")]\r
123         public class ScopeCComponent\r
124         {\r
125             [Import]\r
126             public ScopeBComponent ScopeB { get; set; }\r
127         }\r
128     }\r
129 }\r