Skip to content

Commit 7911885

Browse files
Adding Bi-directional communication pipe
1 parent 2a42bfd commit 7911885

File tree

6 files changed

+116
-33
lines changed

6 files changed

+116
-33
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Windows.ApplicationModel.AppService;
99
using Windows.ApplicationModel.Background;
1010
using Windows.Foundation.Collections;
11+
using Windows.UI.Xaml;
12+
using Windows.UI.Xaml.Controls;
1113

1214
namespace UITests.App
1315
{
@@ -47,13 +49,26 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS
4749
Debug.WriteLine("[Host] Received request for Page: " + pageName);
4850

4951
ValueSet returnMessage = new ValueSet();
50-
returnMessage.Add("Status", "OK");
52+
53+
if (host != null)
54+
{
55+
if (host.OpenPage(pageName))
56+
{
57+
returnMessage.Add("Status", "OK");
58+
}
59+
else
60+
{
61+
returnMessage.Add("Status", "BAD");
62+
}
63+
}
64+
5165
await args.Request.SendResponseAsync(returnMessage);
5266
}
5367
}
5468
catch (Exception e)
5569
{
5670
// Your exception handling code here.
71+
Log("Exception processing request: " + e.Message);
5772
}
5873
finally
5974
{
@@ -72,5 +87,17 @@ private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, App
7287
{
7388
_appServiceDeferral.Complete();
7489
}
90+
91+
public async void Log(string msg)
92+
{
93+
var message = new ValueSet();
94+
message.Add("Command", "Log");
95+
message.Add("Level", "Comment");
96+
message.Add("Message", msg);
97+
98+
await _appServiceConnection.SendMessageAsync(message);
99+
100+
// TODO: do we care if we have a problem here?
101+
}
75102
}
76103
}

UITests/UITests.App/App.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ public sealed partial class App
1818
{
1919
internal Dictionary<string, Type> TestPages { get; } = new Dictionary<string, Type>();
2020

21+
internal MainTestHost host;
22+
2123
public App()
2224
{
2325
this.InitializeComponent();
2426
this.Suspending += OnSuspending;
27+
this.UnhandledException += this.App_UnhandledException;
28+
}
29+
30+
private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
31+
{
32+
// TODO: Log to a file?
33+
Log("Unhandled Exception: " + e.Message);
2534
}
2635

2736
/// <summary>

UITests/UITests.App/MainTestHost.xaml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
<Page
2-
x:Class="UITests.App.MainTestHost"
3-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:testhelpers="using:AppTestAutomationHelpers"
6-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8-
mc:Ignorable="d"
9-
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1+
<Page x:Class="UITests.App.MainTestHost"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:testhelpers="using:AppTestAutomationHelpers"
7+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
8+
mc:Ignorable="d">
109

1110
<Grid>
12-
<Grid.RowDefinitions>
13-
<RowDefinition Height="*" />
14-
<RowDefinition Height="Auto" />
15-
</Grid.RowDefinitions>
1611
<testhelpers:TestAutomationHelpersPanel />
1712

1813
<Frame x:Name="navigationFrame" />
19-
20-
<StackPanel Orientation="Horizontal" Grid.Row="1" >
21-
<TextBox x:Name="PageName" KeyDown="PageName_KeyDown"/>
22-
</StackPanel>
2314
</Grid>
2415
</Page>

UITests/UITests.App/MainTestHost.xaml.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Toolkit.Uwp.Extensions;
56
using System;
67
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
using Windows.System;
710
using Windows.UI.Xaml;
811
using Windows.UI.Xaml.Controls;
912
using Windows.UI.Xaml.Input;
@@ -17,30 +20,35 @@ public sealed partial class MainTestHost
1720
{
1821
private readonly Dictionary<string, Type> pageMap;
1922

23+
private DispatcherQueue queue;
24+
2025
public MainTestHost()
2126
{
2227
InitializeComponent();
28+
((App)Application.Current).host = this;
2329
pageMap = ((App)Application.Current).TestPages;
30+
queue = DispatcherQueue.GetForCurrentThread();
2431
}
2532

26-
private void PageName_KeyDown(object sender, KeyRoutedEventArgs e)
27-
{
28-
if (e.Key == Windows.System.VirtualKey.Enter && sender is TextBox s)
29-
{
30-
OpenPage(s.Text);
31-
}
32-
}
33-
34-
private void OpenPage(string pageName)
33+
internal bool OpenPage(string pageName)
3534
{
3635
try
3736
{
38-
navigationFrame.Navigate(pageMap[pageName]);
37+
((App)Application.Current).Log("Trying to Load Page: " + pageName);
38+
39+
// Ensure we're on the UI thread as we'll be called from the AppService now.
40+
queue.EnqueueAsync(() =>
41+
{
42+
navigationFrame.Navigate(pageMap[pageName]);
43+
});
3944
}
40-
catch (KeyNotFoundException ex)
45+
catch (Exception e)
4146
{
42-
throw new Exception("Cannot find page.", ex);
47+
((App)Application.Current).Log(string.Format("Exception Loading Page {0}: {1} ", pageName, e.Message));
48+
return false;
4349
}
50+
51+
return true;
4452
}
4553
}
4654
}

UITests/UITests.Tests.Shared/Examples/SimpleTestPage.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public SimpleTestPage()
2020
private void Button_Click(object sender, RoutedEventArgs e)
2121
{
2222
textBlock.Text = "Clicked";
23+
(App.Current as App).Log("Button Clicked!");
2324
}
2425
}
2526
}

UITests/UITests.Tests.Shared/UITestBase.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,16 @@ public async Task TestInitialize()
117117

118118
var pageName = attribute.XamlFile;
119119

120-
Log.Comment("[Harness] Sending Hose Page Request: {0}", pageName);
120+
Log.Comment("[Harness] Sending Host Page Request: {0}", pageName);
121121

122122
// Make the connection if we haven't already.
123123
if (CommunicationService == null)
124124
{
125125
CommunicationService = new AppServiceConnection();
126126

127-
// Here, we use the app service name defined in the app service
127+
CommunicationService.RequestReceived += this.CommunicationService_RequestReceived;
128+
129+
// Here, we use the app service name defined in the app service
128130
// provider's Package.appxmanifest file in the <Extension> section.
129131
CommunicationService.AppServiceName = "TestHarnessCommunicationService";
130132

@@ -155,9 +157,54 @@ public async Task TestInitialize()
155157
// Get the data that the service sent to us.
156158
if (response.Message["Status"] as string == "OK")
157159
{
158-
result = response.Message["Result"] as string;
160+
Log.Comment("[Harness] Received Host Ready with Page: {0}", pageName);
161+
return;
159162
}
160163
}
164+
165+
// Error case, we didn't get confirmation of test starting.
166+
throw new InvalidOperationException("Test host didn't confirm test ready to execute page: " + pageName);
167+
}
168+
169+
private void CommunicationService_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
170+
{
171+
AppServiceDeferral messageDeferral = args.GetDeferral();
172+
ValueSet message = args.Request.Message;
173+
string cmd = message["Command"] as string;
174+
175+
try
176+
{
177+
// Return the data to the caller.
178+
if (cmd == "Log")
179+
{
180+
string level = message["Level"] as string;
181+
string msg = message["Message"] as string;
182+
183+
switch (level)
184+
{
185+
case "Comment":
186+
Log.Comment("[Host] {0}", msg);
187+
break;
188+
case "Warning":
189+
Log.Warning("[Host] {0}", msg);
190+
break;
191+
case "Error":
192+
Log.Error("[Host] {0}", msg);
193+
break;
194+
}
195+
}
196+
}
197+
catch (Exception e)
198+
{
199+
// Your exception handling code here.
200+
Debug.WriteLine("Exception receiving message: " + e.Message);
201+
}
202+
finally
203+
{
204+
// Complete the deferral so that the platform knows that we're done responding to the app service call.
205+
// Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
206+
messageDeferral.Complete();
207+
}
161208
}
162209
}
163210
}

0 commit comments

Comments
 (0)