PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/InitializationScopeTests.cs

https://github.com/iainlane/mono
C# | 150 lines | 121 code | 26 blank | 3 comment | 0 complexity | 78feafbeab286fe7eee849c1de35f02b MD5 | raw file
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.Composition;
  7. using System.ComponentModel.Composition.Factories;
  8. using System.ComponentModel.Composition.Hosting;
  9. using System.Linq;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. namespace System.ComponentModel.Composition
  12. {
  13. [TestClass]
  14. public class InitializationScopeTests
  15. {
  16. [TestMethod]
  17. public void SingleContainerSimpleCompose()
  18. {
  19. var container = ContainerFactory.Create();
  20. ImportingComposablePart importPart;
  21. CompositionBatch batch = new CompositionBatch();
  22. batch.AddExportedValue("value1", "Hello");
  23. batch.AddExportedValue("value2", "World");
  24. batch.AddPart(importPart = PartFactory.CreateImporter("value1", "value2"));
  25. container.Compose(batch);
  26. Assert.AreEqual(2, importPart.ImportSatisfiedCount);
  27. Assert.AreEqual("Hello", importPart.GetImport("value1"));
  28. Assert.AreEqual("World", importPart.GetImport("value2"));
  29. }
  30. [TestMethod]
  31. public void ParentedContainerSimpleCompose()
  32. {
  33. var container = ContainerFactory.Create();
  34. var importPart = PartFactory.CreateImporter("value1", "value2");
  35. CompositionBatch batch = new CompositionBatch();
  36. batch.AddExportedValue("value1", "Parent");
  37. var childContainer = new CompositionContainer(container);
  38. CompositionBatch childBatch = new CompositionBatch();
  39. childBatch.AddExportedValue("value2", "Child");
  40. childBatch.AddPart(importPart);
  41. Assert.AreEqual(0, importPart.ImportSatisfiedCount, "Import should not happen until outer scope is disposed");
  42. container.Compose(batch);
  43. childContainer.Compose(childBatch);
  44. Assert.AreEqual(2, importPart.ImportSatisfiedCount);
  45. Assert.AreEqual("Parent", importPart.GetImport("value1"));
  46. Assert.AreEqual("Child", importPart.GetImport("value2"));
  47. }
  48. [TestMethod]
  49. public void SingleContainerPartReplacement()
  50. {
  51. var container = ContainerFactory.Create();
  52. var importPart = PartFactory.CreateImporter(true, "value1", "value2");
  53. CompositionBatch batch = new CompositionBatch();
  54. var export1Key = batch.AddExportedValue("value1", "Hello");
  55. batch.AddExportedValue("value2", "World");
  56. batch.AddPart(importPart);
  57. container.Compose(batch);
  58. Assert.AreEqual(2, importPart.ImportSatisfiedCount);
  59. Assert.AreEqual("Hello", importPart.GetImport("value1"));
  60. Assert.AreEqual("World", importPart.GetImport("value2"));
  61. importPart.ResetImportSatisfiedCount();
  62. batch = new CompositionBatch();
  63. batch.RemovePart(export1Key);
  64. batch.AddExportedValue("value1", "Goodbye");
  65. container.Compose(batch);
  66. Assert.AreEqual(1, importPart.ImportSatisfiedCount);
  67. Assert.AreEqual("Goodbye", importPart.GetImport("value1"));
  68. Assert.AreEqual("World", importPart.GetImport("value2"));
  69. }
  70. [TestMethod]
  71. public void ParentedContainerPartReplacement()
  72. {
  73. var container = ContainerFactory.Create();
  74. CompositionBatch batch = new CompositionBatch();
  75. var importPart = PartFactory.CreateImporter(true, "value1", "value2");
  76. var exportKey = batch.AddExportedValue("value1", "Parent");
  77. var childContainer = new CompositionContainer(container);
  78. CompositionBatch childBatch = new CompositionBatch();
  79. childBatch.AddExportedValue("value2", "Child");
  80. childBatch.AddPart(importPart);
  81. Assert.AreEqual(0, importPart.ImportSatisfiedCount, "Should not import until outer scope is disposed");
  82. container.Compose(batch);
  83. childContainer.Compose(childBatch);
  84. Assert.AreEqual(2, importPart.ImportSatisfiedCount);
  85. Assert.AreEqual("Parent", importPart.GetImport("value1"));
  86. Assert.AreEqual("Child", importPart.GetImport("value2"));
  87. importPart.ResetImportSatisfiedCount();
  88. batch = new CompositionBatch();
  89. batch.RemovePart(exportKey);
  90. batch.AddExportedValue("value1", "New Parent");
  91. container.Compose(batch);
  92. Assert.AreEqual(1, importPart.ImportSatisfiedCount);
  93. Assert.AreEqual("New Parent", importPart.GetImport("value1"));
  94. Assert.AreEqual("Child", importPart.GetImport("value2"));
  95. }
  96. [TestMethod]
  97. public void SelectiveRecompose()
  98. {
  99. var container = ContainerFactory.Create();
  100. var stableImporter = PartFactory.CreateImporter("stable");
  101. var dynamicImporter = PartFactory.CreateImporter("dynamic", true);
  102. CompositionBatch batch = new CompositionBatch();
  103. batch.AddPart(stableImporter);
  104. batch.AddPart(dynamicImporter);
  105. var exportKey = batch.AddExportedValue("dynamic", 1);
  106. batch.AddExportedValue("stable", 42);
  107. container.Compose(batch);
  108. Assert.AreEqual(1, stableImporter.ImportSatisfiedCount);
  109. Assert.AreEqual(stableImporter.GetImport("stable"), 42);
  110. Assert.AreEqual(1, dynamicImporter.ImportSatisfiedCount);
  111. Assert.AreEqual(dynamicImporter.GetImport("dynamic"), 1);
  112. batch = new CompositionBatch();
  113. stableImporter.ResetImportSatisfiedCount();
  114. dynamicImporter.ResetImportSatisfiedCount();
  115. batch.RemovePart(exportKey);
  116. batch.AddExportedValue("dynamic", 2);
  117. container.Compose(batch);
  118. Assert.AreEqual(0, stableImporter.ImportSatisfiedCount, "Should not have imported the stable import part");
  119. Assert.AreEqual(stableImporter.GetImport("stable"), 42);
  120. Assert.AreEqual(1, dynamicImporter.ImportSatisfiedCount);
  121. Assert.AreEqual(dynamicImporter.GetImport("dynamic"), 2);
  122. }
  123. }
  124. }