Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] SearchHandler - added focus/unfocus support #24852

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public SearchHandlerAppearanceTracker(IShellSearchView searchView, IShellContext
_control = searchView.View;
_searchHandler.PropertyChanged += SearchHandlerPropertyChanged;
_editText = (_control as ViewGroup).GetChildrenOfType<EditText>().FirstOrDefault();
_editText.FocusChange += EditTextFocusChange;
UpdateSearchBarColors();
UpdateFont();
UpdateHorizontalTextAlignment();
Expand Down Expand Up @@ -83,6 +84,16 @@ protected virtual void SearchHandlerPropertyChanged(object sender, System.Compon
}
}

void EditTextFocusChange(object s, AView.FocusChangeEventArgs args)
{
_searchHandler.SetIsFocused(_editText.IsFocused);
kubaflo marked this conversation as resolved.
Show resolved Hide resolved

if (_editText.IsFocused)
MauiContext.Context.ShowKeyboard(_editText);
else
MauiContext.Context.ShowKeyboard(_editText);
}

void UpdateSearchBarColors()
{
UpdateBackgroundColor();
Expand Down Expand Up @@ -221,6 +232,7 @@ protected virtual void Dispose(bool disposing)
if (_searchHandler != null)
{
_searchHandler.PropertyChanged -= SearchHandlerPropertyChanged;
_editText.FocusChange -= EditTextFocusChange;
}
_searchHandler = null;
_control = null;
Expand Down
27 changes: 27 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24670.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.ManualTest, "24670", "SearchHandler.Focused event never fires", PlatformAffected.All)]
public partial class Issue24670 : Shell
{
public Issue24670()
{
InitializeComponent();
}

private void SearchHandler_Focused(object sender, EventArgs e)
{
focusedLabel.Text = "Focused: True";
}

private void SearchHandler_Unfocused(object sender, EventArgs e)
{
unfocusedLabel.Text = "Unfocused: True";
}

private void Button_Clicked(object sender, EventArgs e)
{
searchHandler.SetIsFocused(!searchHandler.IsFocused);
}
}
}
31 changes: 31 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue24670">
<ShellContent
Title="Home"
Route="MainPage">
<ContentPage>
<Shell.SearchHandler>
<SearchHandler
x:Name="searchHandler"
SearchBoxVisibility="Expanded"
Focused="SearchHandler_Focused"
Unfocused="SearchHandler_Unfocused"
Placeholder="Click to focus.."/>
</Shell.SearchHandler>

<StackLayout>
<Button AutomationId="button"
Clicked="Button_Clicked"
Text="Click to focus/unfocus search handler"/>
<Label x:Name="focusedLabel"
AutomationId="focusedLabel"
Text="Focused: False"/>
<Label x:Name="unfocusedLabel"
AutomationId="unfocusedLabel"
Text="Unfocused: False"/>
</StackLayout>
</ContentPage>
</ShellContent>
</Shell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue24670 : _IssuesUITest
{
public override string Issue => "SearchHandler.Focused event never fires";

public Issue24670(TestDevice testDevice) : base(testDevice)
{
}

[Test]
[Category(UITestCategories.Shell)]
[Category(UITestCategories.SearchBar)]
public void SearchHandlerFocusAndUnfocusEventsShouldWork()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is failing on Android. Maybe need to use WaitForElement before Click the Button.

System.NullReferenceException : Object reference not set to an instance of an object.
 at UITest.Appium.HelperExtensions.Click(IUIElement element) in /_/src/TestUtils/src/UITest.Appium/HelperExtensions.cs:line 154
   at UITest.Appium.HelperExtensions.Click(IApp app, String element) in /_/src/TestUtils/src/UITest.Appium/HelperExtensions.cs:line 34
   at Microsoft.Maui.TestCases.Tests.Issues.Issue24670.SearchHandlerFocusAndUnfocusEventsShouldWork() in /_/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24670.cs:line 20
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

{
App.WaitForElement("button");
App.Click("button");
App.Click("button");

var focusedLabelText = App.WaitForElement("focusedLabel").GetText();
var unfocusedLabelText = App.WaitForElement("unfocusedLabel").GetText();

Assert.That(focusedLabelText, Is.EqualTo("Focused: True"));
Assert.That(unfocusedLabelText, Is.EqualTo("Unfocused: True"));
}
}
}
Loading