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

Merged
merged 2 commits into from
Dec 9, 2024
Merged
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,11 @@ 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
}

void UpdateSearchBarColors()
{
UpdateBackgroundColor();
Expand Down Expand Up @@ -221,6 +227,7 @@ protected virtual void Dispose(bool disposing)
if (_searchHandler != null)
{
_searchHandler.PropertyChanged -= SearchHandlerPropertyChanged;
_editText.FocusChange -= EditTextFocusChange;
}
_searchHandler = null;
_control = null;
Expand Down
30 changes: 30 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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"
AutomationId="searchHandler"
Placeholder="searchHandler"
SearchBoxVisibility="Expanded"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to add AutomationId("searchHandler") and click this in the UI test, but Appium couldn't find it :/

Focused="SearchHandler_Focused"
Unfocused="SearchHandler_Unfocused"/>
</Shell.SearchHandler>

<StackLayout>
<Entry AutomationId="entry"/>
<Label x:Name="focusedLabel"
AutomationId="focusedLabel"
Text="Focused: False"/>
<Label x:Name="unfocusedLabel"
AutomationId="unfocusedLabel"
Text="Unfocused: False"/>
</StackLayout>
</ContentPage>
</ShellContent>
</Shell>
22 changes: 22 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, "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";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#if ANDROID
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("searchHandler");
App.Click("searchHandler");

// Click the entry below to trigger the unfocused event
App.Click("entry");

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"));
}
}
}
#endif
Loading