Skip to content

Commit 0927e56

Browse files
authored
Merge branch 'main' into fix-weakreferencemessenger-bug
2 parents 928fc47 + fe30893 commit 0927e56

16 files changed

+573
-193
lines changed

UITests/UITests.App/App.AppService.xaml.cs

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Diagnostics;
6+
using System.Threading.Tasks;
77
using Microsoft.Toolkit.Mvvm.Messaging;
88
using UITests.App.Pages;
99
using Windows.ApplicationModel.Activation;
@@ -20,6 +20,9 @@ namespace UITests.App
2020
/// </summary>
2121
public sealed partial class App
2222
{
23+
private static readonly ValueSet BadResult = new() { { "Status", "BAD" } };
24+
private static readonly ValueSet OkResult = new() { { "Status", "OK" } };
25+
2326
private AppServiceConnection _appServiceConnection;
2427
private BackgroundTaskDeferral _appServiceDeferral;
2528

@@ -37,45 +40,39 @@ protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
3740

3841
private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
3942
{
40-
AppServiceDeferral messageDeferral = args.GetDeferral();
41-
ValueSet message = args.Request.Message;
42-
string cmd = message["Command"] as string;
43+
var messageDeferral = args.GetDeferral();
44+
var message = args.Request.Message;
45+
if(!TryGetValueAndLog(message, "Command", out var cmd))
46+
{
47+
await args.Request.SendResponseAsync(BadResult);
48+
messageDeferral.Complete();
49+
return;
50+
}
4351

44-
try
52+
switch (cmd)
4553
{
46-
// Return the data to the caller.
47-
if (cmd == "Start")
48-
{
49-
var pageName = message["Page"] as string;
54+
case "OpenPage":
55+
if (!TryGetValueAndLog(message, "Page", out var pageName))
56+
{
57+
await args.Request.SendResponseAsync(BadResult);
58+
break;
59+
}
5060

5161
Log.Comment("Received request for Page: {0}", pageName);
5262

53-
ValueSet returnMessage = new ValueSet();
54-
5563
// We await the OpenPage method to ensure the navigation has finished.
56-
if (await WeakReferenceMessenger.Default.Send(new RequestPageMessage(pageName)))
57-
{
58-
returnMessage.Add("Status", "OK");
59-
}
60-
else
61-
{
62-
returnMessage.Add("Status", "BAD");
63-
}
64+
var pageResponse = await WeakReferenceMessenger.Default.Send(new RequestPageMessage(pageName));
6465

65-
await args.Request.SendResponseAsync(returnMessage);
66-
}
67-
}
68-
catch (Exception e)
69-
{
70-
// Your exception handling code here.
71-
Log.Error("Exception processing request: {0}", e.Message);
72-
}
73-
finally
74-
{
75-
// Complete the deferral so that the platform knows that we're done responding to the app service call.
76-
// Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
77-
messageDeferral.Complete();
66+
await args.Request.SendResponseAsync(pageResponse ? OkResult : BadResult);
67+
68+
break;
69+
default:
70+
break;
7871
}
72+
73+
// Complete the deferral so that the platform knows that we're done responding to the app service call.
74+
// Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
75+
messageDeferral.Complete();
7976
}
8077

8178
private void OnAppServicesCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
@@ -88,16 +85,38 @@ private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, App
8885
_appServiceDeferral.Complete();
8986
}
9087

91-
public async void SendLogMessage(string level, string msg)
88+
public async Task SendLogMessage(string level, string msg)
9289
{
93-
var message = new ValueSet();
94-
message.Add("Command", "Log");
95-
message.Add("Level", level);
96-
message.Add("Message", msg);
90+
var message = new ValueSet
91+
{
92+
{ "Command", "Log" },
93+
{ "Level", level },
94+
{ "Message", msg }
95+
};
9796

9897
await _appServiceConnection.SendMessageAsync(message);
9998

10099
// TODO: do we care if we have a problem here?
101100
}
101+
102+
private static bool TryGetValueAndLog(ValueSet message, string key, out string value)
103+
{
104+
value = null;
105+
if (!message.TryGetValue(key, out var o))
106+
{
107+
Log.Error($"Could not find the key \"{key}\" in the message.");
108+
return false;
109+
}
110+
111+
if (o is not string s)
112+
{
113+
Log.Error($"{key}'s value is not a string");
114+
return false;
115+
}
116+
117+
value = s;
118+
119+
return true;
120+
}
102121
}
103122
}

UITests/UITests.App/HomePage.xaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Page x:Class="UITests.App.HomePage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
5+
<TextBlock HorizontalAlignment="Center"
6+
VerticalAlignment="Center"
7+
Text="No test loaded." />
8+
</Page>

UITests/UITests.App/HomePage.xaml.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.UI.Xaml.Controls;
6+
7+
namespace UITests.App
8+
{
9+
/// <summary>
10+
/// An empty page that can be used on its own or navigated to within a Frame.
11+
/// </summary>
12+
public sealed partial class HomePage
13+
{
14+
public HomePage()
15+
{
16+
this.InitializeComponent();
17+
}
18+
}
19+
}

UITests/UITests.App/MainTestHost.xaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,35 @@
66
xmlns:testhelpers="using:AppTestAutomationHelpers"
77
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
88
mc:Ignorable="d">
9+
<Page.Resources>
10+
<Style x:Key="AutomationHelperStyle"
11+
TargetType="Control">
12+
<Setter Property="IsTabStop" Value="False" />
13+
<Setter Property="IsHitTestVisible" Value="False" />
14+
<Setter Property="Width" Value="0" />
15+
<Setter Property="Height" Value="0" />
16+
<Setter Property="Opacity" Value="0" />
17+
</Style>
18+
</Page.Resources>
919

1020
<Grid>
1121
<testhelpers:TestAutomationHelpersPanel />
12-
22+
<StackPanel Width="0"
23+
Height="0"
24+
HorizontalAlignment="Stretch"
25+
VerticalAlignment="Top"
26+
Orientation="Horizontal">
27+
<Button x:Name="GoBackInvokerButton"
28+
AutomationProperties.AutomationId="__GoBackInvoker"
29+
Click="GoBackInvokerButton_Click"
30+
Style="{StaticResource AutomationHelperStyle}" />
31+
<Button x:Name="CloseAppInvokerButton"
32+
AutomationProperties.AutomationId="__CloseAppInvoker"
33+
Click="CloseAppInvokerButton_Click"
34+
Style="{StaticResource AutomationHelperStyle}" />
35+
</StackPanel>
1336
<Frame x:Name="navigationFrame"
37+
IsNavigationStackEnabled="False"
1438
Navigated="NavigationFrame_Navigated"
1539
NavigationFailed="NavigationFrame_NavigationFailed" />
1640
</Grid>

UITests/UITests.App/MainTestHost.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,17 @@ private void NavigationFrame_NavigationFailed(object sender, Windows.UI.Xaml.Nav
114114
Log.Error("Failed to navigate to page {0}", e.SourcePageType.FullName);
115115
_loadingStateTask.SetResult(false);
116116
}
117+
118+
private void GoBackInvokerButton_Click(object sender, RoutedEventArgs e)
119+
{
120+
Log.Comment("Go Back Clicked. Navigating to Page...");
121+
navigationFrame.Navigate(typeof(HomePage));
122+
Log.Comment("Navigated to Page.");
123+
}
124+
125+
private void CloseAppInvokerButton_Click(object sender, RoutedEventArgs e)
126+
{
127+
App.Current.Exit();
128+
}
117129
}
118130
}

UITests/UITests.App/UITests.App.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<GenerateAppxPackageOnBuild>true</GenerateAppxPackageOnBuild>
2828
<AppxPackageSigningEnabled>True</AppxPackageSigningEnabled>
2929
<IsTestHost>true</IsTestHost>
30+
<LangVersion>9.0</LangVersion>
3031
</PropertyGroup>
3132
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
3233
<DebugSymbols>true</DebugSymbols>
@@ -131,6 +132,9 @@
131132
<Compile Include="App.AppService.xaml.cs">
132133
<DependentUpon>App.xaml</DependentUpon>
133134
</Compile>
135+
<Compile Include="HomePage.xaml.cs">
136+
<DependentUpon>HomePage.xaml</DependentUpon>
137+
</Compile>
134138
<Compile Include="MainTestHost.xaml.cs">
135139
<DependentUpon>MainTestHost.xaml</DependentUpon>
136140
</Compile>
@@ -158,6 +162,10 @@
158162
<Generator>MSBuild:Compile</Generator>
159163
<SubType>Designer</SubType>
160164
</ApplicationDefinition>
165+
<Page Include="HomePage.xaml">
166+
<SubType>Designer</SubType>
167+
<Generator>MSBuild:Compile</Generator>
168+
</Page>
161169
<Page Include="MainTestHost.xaml">
162170
<Generator>MSBuild:Compile</Generator>
163171
<SubType>Designer</SubType>
@@ -207,6 +215,10 @@
207215
<Project>{daeb9cec-c817-33b2-74b2-bc379380db72}</Project>
208216
<Name>Microsoft.Toolkit.Uwp.UI.Controls.DataGrid</Name>
209217
</ProjectReference>
218+
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Controls.Input\Microsoft.Toolkit.Uwp.UI.Controls.Input.csproj">
219+
<Project>{af1be4e9-e2e1-4729-b076-b3725d8e21ee}</Project>
220+
<Name>Microsoft.Toolkit.Uwp.UI.Controls.Input</Name>
221+
</ProjectReference>
210222
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Controls.Layout\Microsoft.Toolkit.Uwp.UI.Controls.Layout.csproj">
211223
<Project>{cb444381-18ba-4a51-bb32-3a498bcc1e99}</Project>
212224
<Name>Microsoft.Toolkit.Uwp.UI.Controls.Layout</Name>

UITests/UITests.Tests.MSTest/UITests.Tests.MSTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
77
<TargetFrameworkVersion>v3.1</TargetFrameworkVersion>
88
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>
9-
<LangVersion>8.0</LangVersion>
9+
<LangVersion>9.0</LangVersion>
1010

1111
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
1212
<PlatformTarget>x86</PlatformTarget>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Windows.Apps.Test.Foundation.Controls;
6+
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
7+
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra;
8+
9+
#if USING_TAEF
10+
using WEX.Logging.Interop;
11+
using WEX.TestExecution;
12+
using WEX.TestExecution.Markup;
13+
#else
14+
using Microsoft.VisualStudio.TestTools.UnitTesting;
15+
#endif
16+
17+
namespace UITests.Tests
18+
{
19+
20+
[TestClass]
21+
public class RangeSelectorTest : UITestBase
22+
{
23+
[ClassInitialize]
24+
[TestProperty("RunAs", "User")]
25+
[TestProperty("Classification", "ScenarioTestSuite")]
26+
[TestProperty("Platform", "Any")]
27+
public static void ClassInitialize(TestContext testContext)
28+
{
29+
TestEnvironment.Initialize(testContext, WinUICsUWPSampleApp);
30+
}
31+
32+
[TestMethod]
33+
[TestPage("RangeSelectorTestPage")]
34+
public void SimpleTestMethod2()
35+
{
36+
var inputStepFrequency = new Edit(FindElement.ById("inputStepFrequency"));
37+
var inputMinimum = new Edit(FindElement.ById("inputMinimum"));
38+
var inputRangeStart = new Edit(FindElement.ById("inputRangeStart"));
39+
var inputRangeEnd = new Edit(FindElement.ById("inputRangeEnd"));
40+
var inputMaximum = new Edit(FindElement.ById("inputMaximum"));
41+
42+
var submitStepFrequency = new Button(FindElement.ById("submitStepFrequency"));
43+
var submitMinimum = new Button(FindElement.ById("submitMinimum"));
44+
var submitRangeStart = new Button(FindElement.ById("submitRangeStart"));
45+
var submitRangeEnd = new Button(FindElement.ById("submitRangeEnd"));
46+
var submitMaximum = new Button(FindElement.ById("submitMaximum"));
47+
var submitAll = new Button(FindElement.ById("submitAll"));
48+
49+
KeyboardHelper.EnterText(inputStepFrequency, "1");
50+
KeyboardHelper.EnterText(inputMinimum, "0");
51+
KeyboardHelper.EnterText(inputRangeStart, "10");
52+
KeyboardHelper.EnterText(inputRangeEnd, "90");
53+
KeyboardHelper.EnterText(inputMaximum, "100");
54+
55+
submitAll.Click();
56+
Wait.ForIdle();
57+
58+
var currentStepFrequency = new TextBlock(FindElement.ById("currentStepFrequency"));
59+
var currentMinimum = new TextBlock(FindElement.ById("currentMinimum"));
60+
var currentRangeStart = new TextBlock(FindElement.ById("currentRangeStart"));
61+
var currentRangeEnd = new TextBlock(FindElement.ById("currentRangeEnd"));
62+
var currentMaximum = new TextBlock(FindElement.ById("currentMaximum"));
63+
64+
Verify.AreEqual("1", currentStepFrequency.GetText());
65+
Verify.AreEqual("0", currentMinimum.GetText());
66+
Verify.AreEqual("10", currentRangeStart.GetText());
67+
Verify.AreEqual("90", currentRangeEnd.GetText());
68+
Verify.AreEqual("100", currentMaximum.GetText());
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)