/EnhancedReminders/EnhancedReminders/Code/EnhancedReminders/UnitTests/OutlookAddInUnitTests/NotificationRulesUnitTests/TimeControlTest.cs

# · C# · 190 lines · 86 code · 32 blank · 72 comment · 2 complexity · 826b74833fc05d5f87aa0812fbdf0c60 MD5 · raw file

  1. //++
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Module Name:
  6. //
  7. // TimeControlTest.cs
  8. //
  9. // Abstract:
  10. //
  11. // Unit tests for the time control.
  12. //
  13. // Classes:
  14. //
  15. // Microsoft.EnhancedReminders.OutlookAddIn.UnitTests.TimeControlUnitTest
  16. //
  17. //--
  18. using System.Windows;
  19. using System.Windows.Shapes;
  20. using System.Collections;
  21. using Microsoft.EnhancedReminders.OutlookAddIn;
  22. using Microsoft.VisualStudio.TestTools.UnitTesting;
  23. namespace Microsoft.EnhancedReminders.OutlookAddIn.UnitTests
  24. {
  25. /// <summary>
  26. ///This is a test class for TimeControlTest and is intended
  27. ///to contain all TimeControlTest Unit Tests
  28. ///</summary>
  29. [TestClass()]
  30. public class TimeControlTest
  31. {
  32. private TestContext testContextInstance;
  33. /// <summary>
  34. ///Gets or sets the test context which provides
  35. ///information about and functionality for the current test run.
  36. ///</summary>
  37. public TestContext TestContext
  38. {
  39. get
  40. {
  41. return testContextInstance;
  42. }
  43. set
  44. {
  45. testContextInstance = value;
  46. }
  47. }
  48. #region Additional test attributes
  49. //
  50. //You can use the following additional attributes as you write your tests:
  51. //
  52. //Use ClassInitialize to run code before running the first test in the class
  53. //[ClassInitialize()]
  54. //public static void MyClassInitialize(TestContext testContext)
  55. //{
  56. //}
  57. //
  58. //Use ClassCleanup to run code after all tests in a class have run
  59. //[ClassCleanup()]
  60. //public static void MyClassCleanup()
  61. //{
  62. //}
  63. //
  64. //Use TestInitialize to run code before running each test
  65. //[TestInitialize()]
  66. //public void MyTestInitialize()
  67. //{
  68. //}
  69. //
  70. //Use TestCleanup to run code after each test has run
  71. //[TestCleanup()]
  72. //public void MyTestCleanup()
  73. //{
  74. //}
  75. //
  76. #endregion
  77. /// <summary>
  78. /// Verify initialization of the time control.
  79. /// </summary>
  80. [TestMethod]
  81. public void InitializeTimeGrid()
  82. {
  83. TimeControl_Accessor target = new TimeControl_Accessor();
  84. // Create a time bit array with bits 7 and 234 arbitrarily "on".
  85. Assert.IsTrue(target.NumCells > 234, "Unexpected cell count");
  86. BitArray times = new BitArray(target.NumCells);
  87. times.SetAll(false);
  88. times[7] = true;
  89. times[234] = true;
  90. // Initialize the time control.
  91. target.Initialize(times);
  92. // Verify it has filled in the correct cells.
  93. Assert.IsNotNull(target._cells, "_cells");
  94. Assert.AreEqual(target._cells.Count, times.Count, "_cells.Count");
  95. for (int i = 0; i < times.Count; i++)
  96. {
  97. Assert.AreEqual(times[i], target._cells[i].Fill == target.CellOn, "Cell not initialised correctly");
  98. }
  99. }
  100. /// <summary>
  101. /// Verify a random co-ordinate (101, 202) on the grid is mapped to the correct cell index.
  102. ///</summary>
  103. [TestMethod()]
  104. [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
  105. public void GetIndexFromPositionTest()
  106. {
  107. TimeControl_Accessor target = new TimeControl_Accessor();
  108. // Force the size to a fixed width and height
  109. target.grid.RenderSize = new Size(/*width*/600, /*height*/400);
  110. Point positionInGrid = new Point(/*X*/101, /*Y*/202); // An arbitrary point.
  111. int expectedRow = (int)((202.0 / (400.0 / (double)target.grid.RowDefinitions.Count))) - 1;
  112. int expectedColumn = (int)((101.0 / (600.0 / (double)target.grid.ColumnDefinitions.Count))) - 1;
  113. int expectedIndex = expectedColumn * target.NumRows + expectedRow;
  114. int timeIndex = target.GetTimeIndexFromPosition(positionInGrid);
  115. Assert.AreEqual(expectedIndex, timeIndex, "Unexpected index from co-ordinate");
  116. }
  117. /// <summary>
  118. /// Verify a co-ordinate outside the grid has a cell index -1.
  119. ///</summary>
  120. [TestMethod()]
  121. [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
  122. public void GetIndexWhenOutsideOfGridTest()
  123. {
  124. TimeControl_Accessor target = new TimeControl_Accessor();
  125. // Force the size to a fixed width and height
  126. target.grid.RenderSize = new Size(/*width*/600, /*height*/400);
  127. Point positionInGrid = new Point(/*X*/123, /*Y*/456); // A point outside the grid.
  128. Assert.AreEqual(-1, target.GetTimeIndexFromPosition(positionInGrid), "Unexpected index from co-ordinate");
  129. }
  130. /// <summary>
  131. /// Verify the co-ordinate (0, 0) which is in the header region maps to -1.
  132. ///</summary>
  133. [TestMethod()]
  134. [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
  135. public void GetIndexFromPositionTest2()
  136. {
  137. TimeControl_Accessor target = new TimeControl_Accessor();
  138. target.grid.RenderSize = new Size(/*width*/600, /*height*/400);
  139. Point positionInGrid = new Point(/*X*/0, /*Y*/0);
  140. int timeIndex = target.GetTimeIndexFromPosition(positionInGrid);
  141. Assert.AreEqual(-1, timeIndex, "Unexpected time index");
  142. }
  143. /// <summary>
  144. /// Verify the bottom right corner point on the grid maps to the last index for time array.
  145. ///</summary>
  146. [TestMethod()]
  147. [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
  148. public void GetIndexFromPositionTest3()
  149. {
  150. TimeControl_Accessor target = new TimeControl_Accessor();
  151. target.grid.RenderSize = new Size(/*width*/600, /*height*/400);
  152. Point positionInGrid = new Point(/*X*/599, /*Y*/399); // In the bottom-right-most cell.
  153. int timeIndex = target.GetTimeIndexFromPosition(positionInGrid);
  154. int expectedIndex = target.NumRows * target.NumColumns - 1;
  155. Assert.AreEqual(expectedIndex, timeIndex, "Unexpected index");
  156. }
  157. }
  158. }