summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/SearchBarUnitTests.cs
blob: a09aa429305f4d02dc6b114639798e68ed1e82e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;

namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class SearchBarUnitTests : BaseTestFixture
	{
		[Test]
		public void TestConstructor ()
		{
			SearchBar searchBar = new SearchBar ();

			Assert.Null (searchBar.Placeholder);
			Assert.Null (searchBar.Text);
		}

		[Test]
		public void TestContentsChanged ()
		{
			SearchBar searchBar = new SearchBar ();

			bool thrown = false;

			searchBar.TextChanged += (sender, e) => thrown = true;

			searchBar.Text = "Foo";

			Assert.True (thrown);
		}

		[Test]
		public void TestSearchButtonPressed ()
		{
			SearchBar searchBar = new SearchBar ();

			bool thrown = false;
			searchBar.SearchButtonPressed += (sender, e) => thrown = true;

			searchBar.OnSearchButtonPressed ();

			Assert.True (thrown);
		}

		[Test]
		public void TestSearchCommandParameter ()
		{
			var searchBar = new SearchBar ();

			object param = "Testing";
			object result = null;
			searchBar.SearchCommand = new Command (p => { result = p; });
			searchBar.SearchCommandParameter = param;

			searchBar.OnSearchButtonPressed ();

			Assert.AreEqual (param, result);
		}

		[TestCase (null, "Text Changed")]
		[TestCase ("Initial Text", null)]
		[TestCase ("Initial Text", "Text Changed")]
		public void SearchBarTextChangedEventArgs (string initialText, string finalText)
		{
			var searchBar = new SearchBar {
				Text = initialText
			};

			SearchBar searchBarFromSender = null;
			string oldText = null;
			string newText = null;

			searchBar.TextChanged += (s, e) => {
				searchBarFromSender = (SearchBar)s;
				oldText = e.OldTextValue;
				newText = e.NewTextValue;
			};

			searchBar.Text = finalText;

			Assert.AreEqual (searchBar, searchBarFromSender);
			Assert.AreEqual (initialText, oldText);
			Assert.AreEqual (finalText, newText);
		}

		[Test]
		public void CommandCanExecuteUpdatesEnabled ()
		{
			var searchBar = new SearchBar ();

			bool result = false;

			var bindingContext = new {
				Command = new Command (() => { }, () => result)
			};

			searchBar.SetBinding (SearchBar.SearchCommandProperty, "Command");
			searchBar.BindingContext = bindingContext;

			Assert.False (searchBar.IsEnabled);

			result = true;

			bindingContext.Command.ChangeCanExecute ();

			Assert.True (searchBar.IsEnabled);
		}
	}
}