Skip to content

Commit

Permalink
fix(timepicker): Fix timepicker flyout NRE when unloading
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 3, 2020
1 parent c820ccb commit 106ab50
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,27 @@ public void TimePicker_Flyout()
// Dismiss the flyout
_app.TapCoordinates(10, 10);
}

[Test]
[AutoRetry]
[ActivePlatforms(Platform.iOS)]
public void TimePicker_Flyout_Reloaded()
{
Run("UITests.Shared.Windows_UI_Xaml_Controls.TimePicker.TimePicker_Flyout_Automated_Reload", skipInitialScreenshot: true);

var picker = _app.Marked("TestTimePicker");

_app.WaitForElement(picker);

picker.FastTap();

// Wait for the picker to appear
_app.WaitForElement(x => x.Class("UIPickerView"));

TakeScreenshot("TimePicker - Flyout", ignoreInSnapshotCompare: true);

// Dismiss the flyout
_app.Tap(x => x.Marked("AcceptButton"));
}
}
}
13 changes: 10 additions & 3 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TimePicker\TimePicker_Flyout_Automated_Reload.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TimePicker\TimePicker_TimePickerFlyoutStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -3386,7 +3390,7 @@
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_Storage\FilePickerTests.xaml">
<SubType>Designer</SubType>
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_ViewManagement\ApplicationViewSizing.xaml">
Expand Down Expand Up @@ -4090,6 +4094,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ThumbTests\Thumb_DragEvents.xaml.cs">
<DependentUpon>Thumb_DragEvents.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TimePicker\TimePicker_Flyout_Automated_Reload.xaml.cs">
<DependentUpon>TimePicker_Flyout_Automated_Reload.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TimePicker\TimePicker_TimePickerFlyoutStyle.xaml.cs">
<DependentUpon>TimePicker_TimePickerFlyoutStyle.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -5969,7 +5976,7 @@
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_Storage\FilePickerTests.xaml.cs">
<DependentUpon>FilePickerTests.xaml</DependentUpon>
</Compile>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_ViewManagement\ApplicationViewSizing.xaml.cs">
<DependentUpon>ApplicationViewSizing.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -6105,4 +6112,4 @@
<None Include="$(MSBuildThisFileDirectory)ItemExclusions.props" />
</ItemGroup>
<Import Project="ItemExclusions.props" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl x:Class="UITests.Shared.Windows_UI_Xaml_Controls.TimePicker.TimePicker_Flyout_Automated_Reload"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Shared.Windows_UI_Xaml_Controls.TimePicker"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid x:Name="root">
<TimePicker x:Name="TestTimePicker" />
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace UITests.Shared.Windows_UI_Xaml_Controls.TimePicker
{

[SampleControlInfo("Time Picker")]
public sealed partial class TimePicker_Flyout_Automated_Reload : UserControl
{
public TimePicker_Flyout_Automated_Reload()
{
this.InitializeComponent();

_ = Dispatcher.RunAsync(
Windows.UI.Core.CoreDispatcherPriority.Normal,
() => {
root.Children.Remove(TestTimePicker);
_ = Dispatcher.RunAsync(
Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
root.Children.Add(TestTimePicker);
this.TestTimePicker.Time = new TimeSpan(3, 12, 0);
});
}
);
}
}
}
27 changes: 16 additions & 11 deletions src/Uno.UI/UI/Xaml/Controls/TimePicker/TimePickerFlyout.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if XAMARIN_IOS
#nullable enable

#if XAMARIN_IOS

using CoreGraphics;
using UIKit;
Expand All @@ -15,8 +17,9 @@ public partial class TimePickerFlyout : PickerFlyoutBase
{
private readonly SerialDisposable _onLoad = new SerialDisposable();
private readonly SerialDisposable _onUnloaded = new SerialDisposable();
internal protected TimePickerSelector _timeSelector;
internal protected FrameworkElement _headerUntapZone;
internal protected TimePickerSelector? _timeSelector;
internal protected FrameworkElement? _headerUntapZone;
private TimePickerFlyoutPresenter? _timePickerPresenter;
private bool _isInitialized;

public TimePickerFlyout()
Expand Down Expand Up @@ -103,13 +106,12 @@ internal IFrameworkElement Content
typeof(IFrameworkElement),
typeof(TimePickerFlyout),
new FrameworkPropertyMetadata(default(IFrameworkElement), FrameworkPropertyMetadataOptions.AffectsMeasure, OnContentChanged));
private TimePickerFlyoutPresenter _timePickerPresenter;

private static void OnContentChanged(object dependencyObject, DependencyPropertyChangedEventArgs args)
{
var flyout = dependencyObject as TimePickerFlyout;

if (flyout._timePickerPresenter != null)
if (flyout?._timePickerPresenter != null)
{
if (args.NewValue is IDependencyObjectStoreProvider binder)
{
Expand All @@ -133,8 +135,11 @@ void onLoad(object sender, RoutedEventArgs e)
{
_headerUntapZone = _timePickerPresenter?.FindName("HeaderUntapableZone") as FrameworkElement;

AttachAcceptCommand(_timePickerPresenter);
AttachDismissCommand(_timePickerPresenter);
if (_timePickerPresenter != null)
{
AttachAcceptCommand(_timePickerPresenter);
AttachDismissCommand(_timePickerPresenter);
}

_onLoad.Disposable = null;
}
Expand All @@ -154,7 +159,7 @@ void onUnload(object sender, RoutedEventArgs e)
_timePickerPresenter.Unloaded += onUnload;
}

return _timePickerPresenter;
return _timePickerPresenter!;
}

private void OnTap(object sender, Input.PointerRoutedEventArgs e) => e.Handled = true;
Expand All @@ -163,7 +168,7 @@ protected internal override void Open()
{
InitializeContent();

_timeSelector.Initialize();
_timeSelector?.Initialize();

//Gobbling pressed tap on the flyout header background so that it doesn't close the flyout popup.
if (_headerUntapZone != null)
Expand All @@ -181,7 +186,7 @@ protected internal override void Close()
_headerUntapZone.PointerPressed -= OnTap;
}

_timeSelector.Cancel();
_timeSelector?.Cancel();

base.Close();
}
Expand Down Expand Up @@ -210,7 +215,7 @@ private void AttachDismissCommand(IFrameworkElement control)

private void Accept()
{
_timeSelector.SaveTime();
_timeSelector?.SaveTime();
Hide(false);
}

Expand Down

0 comments on commit 106ab50

Please sign in to comment.