PageRenderTime 28ms CodeModel.GetById 9ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19using System;
 20using System.Windows;
 21using System.Windows.Controls;
 22
 23using ICSharpCode.PackageManagement;
 24using NUnit.Framework;
 25using PackageManagement.Tests.Helpers;
 26
 27namespace PackageManagement.Tests
 28{
 29	[TestFixture]
 30	public class SelectedListBoxItemScrollingBehaviourTests
 31	{
 32		DependencyPropertyChangedEventArgs eventArgs;
 33		ListBox listBox;
 34		TestableSelectedListBoxItemScrollingBehaviour behaviour;
 35		
 36		[SetUp]
 37		public void Init()
 38		{
 39			listBox = new ListBox();
 40		}
 41		
 42		void CreateEventArgs(object oldValue, object newValue)
 43		{
 44			eventArgs = new DependencyPropertyChangedEventArgs(ListBoxBehaviour.IsSelectedItemScrolledIntoViewProperty, oldValue, newValue);
 45		}
 46		
 47		void CreateBehaviour()
 48		{
 49			CreateBehaviour(listBox);
 50		}
 51		
 52		void CreateBehaviour(DependencyObject dependencyObject)
 53		{
 54			behaviour = new TestableSelectedListBoxItemScrollingBehaviour(dependencyObject, eventArgs);
 55		}
 56		
 57		[Test]
 58		public void Update_DependencyPropertyChangedFromFalseToTrue_SelectionChangedEventRegistered()
 59		{
 60			CreateEventArgs(false, true);
 61			CreateBehaviour();
 62			
 63			behaviour.Update();
 64			
 65			Assert.IsTrue(behaviour.IsRegisterSelectionChangedHandlerCalled);
 66		}
 67		
 68		[Test]
 69		public void Update_DependencyPropertyChangedFromFalseToTrue_ListBoxPassedToRegisterSelectionChangedHandler()
 70		{
 71			CreateEventArgs(false, true);
 72			CreateBehaviour();
 73			
 74			behaviour.Update();
 75			
 76			Assert.AreEqual(listBox, behaviour.ListBoxPassedToRegisterSelectionChangedHandler);
 77		}
 78		
 79		[Test]
 80		public void Update_DependencyPropertyChangedFromTrueToFalse_ListBoxPassedToUnregisterSelectionChangedHandler()
 81		{
 82			CreateEventArgs(true, false);
 83			CreateBehaviour();
 84			
 85			behaviour.Update();
 86			
 87			Assert.AreEqual(listBox, behaviour.ListBoxPassedToUnregisterSelectionChangedHandler);
 88		}
 89		
 90		[Test]
 91		public void Update_DependencyPropertyNewValueIsNotBoolean_SelectionChangedEventHandlerNotRegisteredOrUnregistered()
 92		{
 93			CreateEventArgs(true, new object());
 94			CreateBehaviour();
 95			behaviour.Update();
 96			
 97			Assert.IsFalse(behaviour.IsRegisterSelectionChangedHandlerCalled);
 98			Assert.IsFalse(behaviour.IsUnregisterSelectionChangedHandlerCalled);
 99		}
100		
101		[Test]
102		public void Update_DependencyObjectIsNotListBox_SelectionChangedEventHandlerNotRegisteredOrUnregistered()
103		{
104			CreateEventArgs(true, false);
105			CreateBehaviour(new DependencyObject());
106			behaviour.Update();
107			
108			Assert.IsFalse(behaviour.IsRegisterSelectionChangedHandlerCalled);
109			Assert.IsFalse(behaviour.IsUnregisterSelectionChangedHandlerCalled);
110		}
111		
112		[Test]
113		public void OnListBoxSelectionChanged_OneItemInSelection_ScrolledIntoViewIsCalled()
114		{
115			behaviour.CallOnListBoxSelectionChanged(listBox);
116			
117			Assert.IsTrue(behaviour.IsScrollListBoxItemIntoViewCalled);
118		}
119		
120		[Test]
121		public void OnListBoxSelectionChanged_OneItemInSelection_ListBoxPassedToScrolledIntoView()
122		{
123			CreateBehaviour();
124			behaviour.CallOnListBoxSelectionChanged(listBox);
125			
126			Assert.AreEqual(listBox, behaviour.ListBoxPassedToScrollListBoxItemIntoView);
127		}
128		
129		[Test]
130		public void OnListBoxSelectionChanged_OneItemInSelection_SelectedItemPassedToScrolledIntoView()
131		{
132			CreateBehaviour();
133			string selectedItem = "a";
134			behaviour.CallOnListBoxSelectionChanged(listBox, selectedItem);
135			
136			Assert.AreEqual(selectedItem, behaviour.ItemPassedToScrollListBoxItemIntoView);
137		}
138		
139		[Test]
140		public void OnListBoxSelectionChanged_SenderAndOriginalSourceAreDifferentObjects_ScrolledIntoViewNotCalled()
141		{
142			CreateBehaviour();
143			ListBox sender = new ListBox();
144			ListBox originalSource = new ListBox();
145			string[] addedItems = new string[] { "a" };
146			behaviour.CallOnListBoxSelectionChanged(sender, originalSource, addedItems);
147			
148			Assert.IsFalse(behaviour.IsScrollListBoxItemIntoViewCalled);
149		}
150		
151		[Test]
152		public void OnListBoxSelectionChanged_NoItemsSelected_ScrolledIntoViewNotCalled()
153		{
154			CreateBehaviour();
155			string[] addedItems = new string[0];
156			behaviour.CallOnListBoxSelectionChanged(listBox, listBox, addedItems);
157			
158			Assert.IsFalse(behaviour.IsScrollListBoxItemIntoViewCalled);
159		}
160		
161		[Test]
162		public void OnListBoxSelectionChanged_SenderAndOriginalSourceAreSameObjectButNotListBoxes_ScrolledIntoViewNotCalled()
163		{
164			CreateBehaviour();
165			object sender = new object();
166			string[] addedItems = new string[] { "a" };
167			behaviour.CallOnListBoxSelectionChanged(sender, sender, addedItems);
168			
169			Assert.IsFalse(behaviour.IsScrollListBoxItemIntoViewCalled);
170		}
171	}
172}