1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\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
11 namespace System.ComponentModel.Composition
\r
14 public class CatalogFilteringTests
\r
17 public void FilteredCatalog_ScopeA()
\r
19 var cat = GetCatalog();
\r
20 var contA = new CompositionContainer(ScopeCatalog(cat, "A"));
\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
29 public void FilteredCatalog_ScopeB()
\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
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
42 public void FilteredCatalog_ScopeC()
\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
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
58 public void FilteredCatalog_EventsFired()
\r
60 var aggCatalog = CatalogFactory.CreateAggregateCatalog();
\r
61 var cat1 = CatalogFactory.CreateAttributed(typeof(ScopeAComponent1), typeof(ScopeBComponent));
\r
63 var filteredCatalog = CatalogFactory.CreateFiltered(aggCatalog,
\r
64 partDef => partDef.Metadata.ContainsKey("Scope") &&
\r
65 partDef.Metadata["Scope"].ToString() == "A");
\r
67 var container = ContainerFactory.Create(filteredCatalog);
\r
69 Assert.IsFalse(container.IsPresent<ScopeAComponent1>(), "sa before add");
\r
70 Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb before add");
\r
72 aggCatalog.Catalogs.Add(cat1);
\r
74 Assert.IsTrue(container.IsPresent<ScopeAComponent1>(), "sa after add");
\r
75 Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb after add");
\r
77 aggCatalog.Catalogs.Remove(cat1);
\r
79 Assert.IsFalse(container.IsPresent<ScopeAComponent1>(), "sa after remove");
\r
80 Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb after remove");
\r
83 private ComposablePartCatalog GetCatalog()
\r
85 return CatalogFactory.CreateAttributed(
\r
86 typeof(ScopeAComponent1),
\r
87 typeof(ScopeAComponent2),
\r
88 typeof(ScopeBComponent),
\r
89 typeof(ScopeCComponent));
\r
92 private ComposablePartCatalog ScopeCatalog(ComposablePartCatalog catalog, string scope)
\r
94 return CatalogFactory.CreateFiltered(catalog,
\r
95 partDef => partDef.Metadata.ContainsKey("Scope") &&
\r
96 partDef.Metadata["Scope"].ToString() == scope);
\r
100 [PartMetadata("Scope", "A")]
\r
101 public class ScopeAComponent1
\r
106 [PartMetadata("Scope", "A")]
\r
107 public class ScopeAComponent2
\r
110 public ScopeAComponent1 ScopeA { get; set; }
\r
114 [PartMetadata("Scope", "B")]
\r
115 public class ScopeBComponent
\r
118 public ScopeAComponent1 ScopeA { get; set; }
\r
122 [PartMetadata("Scope", "C")]
\r
123 public class ScopeCComponent
\r
126 public ScopeBComponent ScopeB { get; set; }
\r