diff --git a/UITests/UITests.App/App.AppService.xaml.cs b/UITests/UITests.App/App.AppService.xaml.cs new file mode 100644 index 00000000000..9a1cf41b08a --- /dev/null +++ b/UITests/UITests.App/App.AppService.xaml.cs @@ -0,0 +1,103 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using Microsoft.Toolkit.Mvvm.Messaging; +using UITests.App.Pages; +using Windows.ApplicationModel.Activation; +using Windows.ApplicationModel.AppService; +using Windows.ApplicationModel.Background; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +namespace UITests.App +{ + /// + /// This file contains part of the app related to the AppService for communication between this test host and the test harness processes. + /// + public sealed partial class App + { + private AppServiceConnection _appServiceConnection; + private BackgroundTaskDeferral _appServiceDeferral; + + protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) + { + base.OnBackgroundActivated(args); + IBackgroundTaskInstance taskInstance = args.TaskInstance; + AppServiceTriggerDetails appService = taskInstance.TriggerDetails as AppServiceTriggerDetails; + _appServiceDeferral = taskInstance.GetDeferral(); + taskInstance.Canceled += OnAppServicesCanceled; + _appServiceConnection = appService.AppServiceConnection; + _appServiceConnection.RequestReceived += OnAppServiceRequestReceived; + _appServiceConnection.ServiceClosed += AppServiceConnection_ServiceClosed; + } + + private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) + { + AppServiceDeferral messageDeferral = args.GetDeferral(); + ValueSet message = args.Request.Message; + string cmd = message["Command"] as string; + + try + { + // Return the data to the caller. + if (cmd == "Start") + { + var pageName = message["Page"] as string; + + Log.Comment("Received request for Page: {0}", pageName); + + ValueSet returnMessage = new ValueSet(); + + // We await the OpenPage method to ensure the navigation has finished. + if (await WeakReferenceMessenger.Default.Send(new RequestPageMessage(pageName))) + { + returnMessage.Add("Status", "OK"); + } + else + { + returnMessage.Add("Status", "BAD"); + } + + await args.Request.SendResponseAsync(returnMessage); + } + } + catch (Exception e) + { + // Your exception handling code here. + Log.Error("Exception processing request: {0}", e.Message); + } + finally + { + // Complete the deferral so that the platform knows that we're done responding to the app service call. + // Note: for error handling: this must be called even if SendResponseAsync() throws an exception. + messageDeferral.Complete(); + } + } + + private void OnAppServicesCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) + { + _appServiceDeferral.Complete(); + } + + private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args) + { + _appServiceDeferral.Complete(); + } + + public async void SendLogMessage(string level, string msg) + { + var message = new ValueSet(); + message.Add("Command", "Log"); + message.Add("Level", level); + message.Add("Message", msg); + + await _appServiceConnection.SendMessageAsync(message); + + // TODO: do we care if we have a problem here? + } + } +} diff --git a/UITests/UITests.App/App.xaml b/UITests/UITests.App/App.xaml index 74e9cd2df91..1b9cedbffb7 100644 --- a/UITests/UITests.App/App.xaml +++ b/UITests/UITests.App/App.xaml @@ -1,7 +1,4 @@ - - - + diff --git a/UITests/UITests.App/App.xaml.cs b/UITests/UITests.App/App.xaml.cs index 23f5035b6d5..16e9e174077 100644 --- a/UITests/UITests.App/App.xaml.cs +++ b/UITests/UITests.App/App.xaml.cs @@ -3,6 +3,10 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using UITests.App.Pages; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.UI.Xaml; @@ -11,12 +15,22 @@ namespace UITests.App { + /// + /// Application class for hosting UI pages to test. + /// public sealed partial class App { public App() { this.InitializeComponent(); this.Suspending += OnSuspending; + this.UnhandledException += this.App_UnhandledException; + } + + private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e) + { + // TODO: Also Log to a file? + Log.Error("Unhandled Exception: " + e.Message); } /// @@ -53,7 +67,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter - rootFrame.Navigate(typeof(MainPage), e.Arguments); + rootFrame.Navigate(typeof(MainTestHost), e.Arguments); } // Ensure the current window is active @@ -68,6 +82,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) /// Details about the navigation failure private void OnNavigationFailed(object sender, NavigationFailedEventArgs e) { + Log.Error("Failed to load root page: " + e.SourcePageType.FullName); throw new Exception("Failed to load Page " + e.SourcePageType.FullName); } diff --git a/UITests/UITests.App/MainPage.xaml b/UITests/UITests.App/MainPage.xaml deleted file mode 100644 index d38db07e3e3..00000000000 --- a/UITests/UITests.App/MainPage.xaml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - -