Skip to content

Commit 2a42bfd

Browse files
Initial App Service Connection Test
1 parent 8897408 commit 2a42bfd

File tree

6 files changed

+136
-6
lines changed

6 files changed

+136
-6
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 System;
6+
using System.Diagnostics;
7+
using Windows.ApplicationModel.Activation;
8+
using Windows.ApplicationModel.AppService;
9+
using Windows.ApplicationModel.Background;
10+
using Windows.Foundation.Collections;
11+
12+
namespace UITests.App
13+
{
14+
/// <summary>
15+
/// This file contains part of the app related to the AppService for communication between this test host and the test harness processes.
16+
/// </summary>
17+
public sealed partial class App
18+
{
19+
private AppServiceConnection _appServiceConnection;
20+
private BackgroundTaskDeferral _appServiceDeferral;
21+
22+
protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
23+
{
24+
base.OnBackgroundActivated(args);
25+
IBackgroundTaskInstance taskInstance = args.TaskInstance;
26+
AppServiceTriggerDetails appService = taskInstance.TriggerDetails as AppServiceTriggerDetails;
27+
_appServiceDeferral = taskInstance.GetDeferral();
28+
taskInstance.Canceled += OnAppServicesCanceled;
29+
_appServiceConnection = appService.AppServiceConnection;
30+
_appServiceConnection.RequestReceived += OnAppServiceRequestReceived;
31+
_appServiceConnection.ServiceClosed += AppServiceConnection_ServiceClosed;
32+
}
33+
34+
private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
35+
{
36+
AppServiceDeferral messageDeferral = args.GetDeferral();
37+
ValueSet message = args.Request.Message;
38+
string cmd = message["Command"] as string;
39+
40+
try
41+
{
42+
// Return the data to the caller.
43+
if (cmd == "Start")
44+
{
45+
var pageName = message["Page"] as string;
46+
47+
Debug.WriteLine("[Host] Received request for Page: " + pageName);
48+
49+
ValueSet returnMessage = new ValueSet();
50+
returnMessage.Add("Status", "OK");
51+
await args.Request.SendResponseAsync(returnMessage);
52+
}
53+
}
54+
catch (Exception e)
55+
{
56+
// Your exception handling code here.
57+
}
58+
finally
59+
{
60+
// Complete the deferral so that the platform knows that we're done responding to the app service call.
61+
// Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
62+
messageDeferral.Complete();
63+
}
64+
}
65+
66+
private void OnAppServicesCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
67+
{
68+
_appServiceDeferral.Complete();
69+
}
70+
71+
private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
72+
{
73+
_appServiceDeferral.Complete();
74+
}
75+
}
76+
}

UITests/UITests.App/Package.appxmanifest

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
4141
<uap:SplashScreen Image="Assets\SplashScreen.png" />
4242
</uap:VisualElements>
43+
<Extensions>
44+
<uap:Extension Category="windows.appService">
45+
<uap:AppService Name="TestHarnessCommunicationService"/>
46+
</uap:Extension>
47+
</Extensions>
4348
</Application>
4449
</Applications>
4550

UITests/UITests.App/UITests.App.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131
<Compile Include="App.xaml.cs">
132132
<DependentUpon>App.xaml</DependentUpon>
133133
</Compile>
134+
<Compile Include="App.AppService.xaml.cs">
135+
<DependentUpon>App.xaml</DependentUpon>
136+
</Compile>
134137
<Compile Include="MainTestHost.xaml.cs">
135138
<DependentUpon>MainTestHost.xaml</DependentUpon>
136139
</Compile>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<PropertyGroup>
55

6-
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
77
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
88
<TargetFrameworkVersion>v3.1</TargetFrameworkVersion>
99
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>

UITests/UITests.Tests.Shared/UITestBase.cs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
using System.IO;
77
using System.Linq;
88
using System.Reflection;
9+
using System.Threading.Tasks;
910
using Microsoft.Windows.Apps.Test.Foundation.Controls;
11+
using Windows.ApplicationModel.AppService;
12+
using Windows.Foundation.Collections;
1013
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
1114
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra;
15+
using System.Diagnostics;
1216

1317
#if USING_TAEF
1418
using WEX.Logging.Interop;
@@ -78,8 +82,10 @@ public static TestSetupHelper.TestSetupHelperOptions TestSetupHelperOptions
7882

7983
public TestContext TestContext { get; set; }
8084

85+
private AppServiceConnection CommunicationService { get; set; }
86+
8187
[TestInitialize]
82-
public void TestInitalize()
88+
public async Task TestInitialize()
8389
{
8490
#if USING_TAEF
8591
var fullTestName = TestContext.TestName;
@@ -109,9 +115,49 @@ public void TestInitalize()
109115
return;
110116
}
111117

112-
var pageTextBox = FindElement.ById<Edit>("PageName");
113-
pageTextBox.SetValue(attribute.XamlFile);
114-
KeyboardHelper.PressKey(Key.Enter);
118+
var pageName = attribute.XamlFile;
119+
120+
Log.Comment("[Harness] Sending Hose Page Request: {0}", pageName);
121+
122+
// Make the connection if we haven't already.
123+
if (CommunicationService == null)
124+
{
125+
CommunicationService = new AppServiceConnection();
126+
127+
// Here, we use the app service name defined in the app service
128+
// provider's Package.appxmanifest file in the <Extension> section.
129+
CommunicationService.AppServiceName = "TestHarnessCommunicationService";
130+
131+
// Use Windows.ApplicationModel.Package.Current.Id.FamilyName
132+
// within the app service provider to get this value.
133+
CommunicationService.PackageFamilyName = "3568ebdf-5b6b-4ddd-bb17-462d614ba50f_gspb8g6x97k2t";
134+
135+
var status = await CommunicationService.OpenAsync();
136+
137+
if (status != AppServiceConnectionStatus.Success)
138+
{
139+
Log.Error("Failed to connect to App Service host.");
140+
CommunicationService = null;
141+
throw new Exception("Failed to connect to App Service host.");
142+
}
143+
}
144+
145+
// Call the service.
146+
var message = new ValueSet();
147+
message.Add("Command", "Start");
148+
message.Add("Page", pageName);
149+
150+
AppServiceResponse response = await CommunicationService.SendMessageAsync(message);
151+
string result = string.Empty;
152+
153+
if (response.Status == AppServiceResponseStatus.Success)
154+
{
155+
// Get the data that the service sent to us.
156+
if (response.Message["Status"] as string == "OK")
157+
{
158+
result = response.Message["Result"] as string;
159+
}
160+
}
115161
}
116162
}
117163
}

UITests/UITests.Tests.TAEF/UITests.Tests.TAEF.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44

5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
66
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
77
<TargetFrameworkVersion>v3.1</TargetFrameworkVersion>
88
<LangVersion>8.0</LangVersion>

0 commit comments

Comments
 (0)