/src/AddIns/Misc/PackageManagement/Test/Src/SelectedListBoxItemScrollingBehaviourTests.cs

https://github.com/ajadex/SharpDevelop · C# · 172 lines · 123 code · 32 blank · 17 comment · 0 complexity · c8fb84d03d17a9f061a1d1dd269e9331 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using ICSharpCode.PackageManagement;
  22. using NUnit.Framework;
  23. using PackageManagement.Tests.Helpers;
  24. namespace PackageManagement.Tests
  25. {
  26. [TestFixture]
  27. public class SelectedListBoxItemScrollingBehaviourTests
  28. {
  29. DependencyPropertyChangedEventArgs eventArgs;
  30. ListBox listBox;
  31. TestableSelectedListBoxItemScrollingBehaviour behaviour;
  32. [SetUp]
  33. public void Init()
  34. {
  35. listBox = new ListBox();
  36. }
  37. void CreateEventArgs(object oldValue, object newValue)
  38. {
  39. eventArgs = new DependencyPropertyChangedEventArgs(ListBoxBehaviour.IsSelectedItemScrolledIntoViewProperty, oldValue, newValue);
  40. }
  41. void CreateBehaviour()
  42. {
  43. CreateBehaviour(listBox);
  44. }
  45. void CreateBehaviour(DependencyObject dependencyObject)
  46. {
  47. behaviour = new TestableSelectedListBoxItemScrollingBehaviour(dependencyObject, eventArgs);
  48. }
  49. [Test]
  50. public void Update_DependencyPropertyChangedFromFalseToTrue_SelectionChangedEventRegistered()
  51. {
  52. CreateEventArgs(false, true);
  53. CreateBehaviour();
  54. behaviour.Update();
  55. Assert.IsTrue(behaviour.IsRegisterSelectionChangedHandlerCalled);
  56. }
  57. [Test]
  58. public void Update_DependencyPropertyChangedFromFalseToTrue_ListBoxPassedToRegisterSelectionChangedHandler()
  59. {
  60. CreateEventArgs(false, true);
  61. CreateBehaviour();
  62. behaviour.Update();
  63. Assert.AreEqual(listBox, behaviour.ListBoxPassedToRegisterSelectionChangedHandler);
  64. }
  65. [Test]
  66. public void Update_DependencyPropertyChangedFromTrueToFalse_ListBoxPassedToUnregisterSelectionChangedHandler()
  67. {
  68. CreateEventArgs(true, false);
  69. CreateBehaviour();
  70. behaviour.Update();
  71. Assert.AreEqual(listBox, behaviour.ListBoxPassedToUnregisterSelectionChangedHandler);
  72. }
  73. [Test]
  74. public void Update_DependencyPropertyNewValueIsNotBoolean_SelectionChangedEventHandlerNotRegisteredOrUnregistered()
  75. {
  76. CreateEventArgs(true, new object());
  77. CreateBehaviour();
  78. behaviour.Update();
  79. Assert.IsFalse(behaviour.IsRegisterSelectionChangedHandlerCalled);
  80. Assert.IsFalse(behaviour.IsUnregisterSelectionChangedHandlerCalled);
  81. }
  82. [Test]
  83. public void Update_DependencyObjectIsNotListBox_SelectionChangedEventHandlerNotRegisteredOrUnregistered()
  84. {
  85. CreateEventArgs(true, false);
  86. CreateBehaviour(new DependencyObject());
  87. behaviour.Update();
  88. Assert.IsFalse(behaviour.IsRegisterSelectionChangedHandlerCalled);
  89. Assert.IsFalse(behaviour.IsUnregisterSelectionChangedHandlerCalled);
  90. }
  91. [Test]
  92. public void OnListBoxSelectionChanged_OneItemInSelection_ScrolledIntoViewIsCalled()
  93. {
  94. behaviour.CallOnListBoxSelectionChanged(listBox);
  95. Assert.IsTrue(behaviour.IsScrollListBoxItemIntoViewCalled);
  96. }
  97. [Test]
  98. public void OnListBoxSelectionChanged_OneItemInSelection_ListBoxPassedToScrolledIntoView()
  99. {
  100. CreateBehaviour();
  101. behaviour.CallOnListBoxSelectionChanged(listBox);
  102. Assert.AreEqual(listBox, behaviour.ListBoxPassedToScrollListBoxItemIntoView);
  103. }
  104. [Test]
  105. public void OnListBoxSelectionChanged_OneItemInSelection_SelectedItemPassedToScrolledIntoView()
  106. {
  107. CreateBehaviour();
  108. string selectedItem = "a";
  109. behaviour.CallOnListBoxSelectionChanged(listBox, selectedItem);
  110. Assert.AreEqual(selectedItem, behaviour.ItemPassedToScrollListBoxItemIntoView);
  111. }
  112. [Test]
  113. public void OnListBoxSelectionChanged_SenderAndOriginalSourceAreDifferentObjects_ScrolledIntoViewNotCalled()
  114. {
  115. CreateBehaviour();
  116. ListBox sender = new ListBox();
  117. ListBox originalSource = new ListBox();
  118. string[] addedItems = new string[] { "a" };
  119. behaviour.CallOnListBoxSelectionChanged(sender, originalSource, addedItems);
  120. Assert.IsFalse(behaviour.IsScrollListBoxItemIntoViewCalled);
  121. }
  122. [Test]
  123. public void OnListBoxSelectionChanged_NoItemsSelected_ScrolledIntoViewNotCalled()
  124. {
  125. CreateBehaviour();
  126. string[] addedItems = new string[0];
  127. behaviour.CallOnListBoxSelectionChanged(listBox, listBox, addedItems);
  128. Assert.IsFalse(behaviour.IsScrollListBoxItemIntoViewCalled);
  129. }
  130. [Test]
  131. public void OnListBoxSelectionChanged_SenderAndOriginalSourceAreSameObjectButNotListBoxes_ScrolledIntoViewNotCalled()
  132. {
  133. CreateBehaviour();
  134. object sender = new object();
  135. string[] addedItems = new string[] { "a" };
  136. behaviour.CallOnListBoxSelectionChanged(sender, sender, addedItems);
  137. Assert.IsFalse(behaviour.IsScrollListBoxItemIntoViewCalled);
  138. }
  139. }
  140. }