Skip to content

Commit c5532f3

Browse files
Clean-up Type Enumeration and Logger Usage
1 parent 3bf27d9 commit c5532f3

File tree

10 files changed

+101
-37
lines changed

10 files changed

+101
-37
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Diagnostics;
7+
using UITests.App.Pages;
78
using Windows.ApplicationModel.Activation;
89
using Windows.ApplicationModel.AppService;
910
using Windows.ApplicationModel.Background;
@@ -46,12 +47,14 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS
4647
{
4748
var pageName = message["Page"] as string;
4849

49-
Debug.WriteLine("[Host] Received request for Page: " + pageName);
50+
Log.Comment("Received request for Page: {0}", pageName);
5051

5152
ValueSet returnMessage = new ValueSet();
5253

5354
if (host != null)
5455
{
56+
// TODO: Think tried to make this async before but it blewup the runtime for some reason...
57+
// We'd like the OpenPage method to ensure the navigation has finished.
5558
if (host.OpenPage(pageName))
5659
{
5760
returnMessage.Add("Status", "OK");
@@ -68,7 +71,7 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS
6871
catch (Exception e)
6972
{
7073
// Your exception handling code here.
71-
Log("Exception processing request: " + e.Message);
74+
Log.Error("Exception processing request: {0}", e.Message);
7275
}
7376
finally
7477
{
@@ -88,11 +91,11 @@ private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, App
8891
_appServiceDeferral.Complete();
8992
}
9093

91-
public async void Log(string msg)
94+
public async void SendLogMessage(string level, string msg)
9295
{
9396
var message = new ValueSet();
9497
message.Add("Command", "Log");
95-
message.Add("Level", "Comment");
98+
message.Add("Level", level);
9699
message.Add("Message", msg);
97100

98101
await _appServiceConnection.SendMessageAsync(message);

UITests/UITests.App/App.xaml.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Reflection;
9+
using UITests.App.Pages;
910
using Windows.ApplicationModel;
1011
using Windows.ApplicationModel.Activation;
1112
using Windows.UI.Xaml;
@@ -16,8 +17,7 @@ namespace UITests.App
1617
{
1718
public sealed partial class App
1819
{
19-
internal Dictionary<string, Type> TestPages { get; } = new Dictionary<string, Type>();
20-
20+
// TODO: Create a better pattern here to connect these.
2121
internal MainTestHost host;
2222

2323
public App()
@@ -29,8 +29,8 @@ public App()
2929

3030
private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
3131
{
32-
// TODO: Log to a file?
33-
Log("Unhandled Exception: " + e.Message);
32+
// TODO: Also Log to a file?
33+
Log.Error("Unhandled Exception: " + e.Message);
3434
}
3535

3636
/// <summary>
@@ -40,21 +40,6 @@ private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExce
4040
/// <param name="e">Details about the launch request and process.</param>
4141
protected override void OnLaunched(LaunchActivatedEventArgs e)
4242
{
43-
var pages = Assembly.GetExecutingAssembly().GetTypes()
44-
.Where(t => t.IsSubclassOf(typeof(Page)));
45-
46-
foreach (var page in pages)
47-
{
48-
try
49-
{
50-
TestPages.Add(page.Name, page);
51-
}
52-
catch (ArgumentException ex)
53-
{
54-
throw new Exception("Two or more test pages share a name.", ex);
55-
}
56-
}
57-
5843
Frame rootFrame = Window.Current.Content as Frame;
5944

6045
// Do not repeat app initialization when the Window already has content,
@@ -97,6 +82,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
9782
/// <param name="e">Details about the navigation failure</param>
9883
private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
9984
{
85+
Log.Error("Failed to load root page: " + e.SourcePageType.FullName);
10086
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
10187
}
10288

UITests/UITests.App/MainTestHost.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<Grid>
1111
<testhelpers:TestAutomationHelpersPanel />
1212

13-
<Frame x:Name="navigationFrame" />
13+
<Frame x:Name="navigationFrame"
14+
Navigated="NavigationFrame_Navigated"
15+
NavigationFailed="NavigationFrame_NavigationFailed" />
1416
</Grid>
1517
</Page>

UITests/UITests.App/MainTestHost.xaml.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using Microsoft.Toolkit.Uwp.Extensions;
66
using System;
77
using System.Collections.Generic;
8+
using System.Reflection;
89
using System.Threading.Tasks;
10+
using UITests.App.Pages;
911
using Windows.System;
1012
using Windows.UI.Xaml;
1113
using Windows.UI.Xaml.Controls;
@@ -18,37 +20,60 @@ namespace UITests.App
1820
/// </summary>
1921
public sealed partial class MainTestHost
2022
{
21-
private readonly Dictionary<string, Type> pageMap;
23+
private DispatcherQueue _queue;
2224

23-
private DispatcherQueue queue;
25+
private Assembly _executingAssembly = Assembly.GetExecutingAssembly();
2426

2527
public MainTestHost()
2628
{
2729
InitializeComponent();
2830
((App)Application.Current).host = this;
29-
pageMap = ((App)Application.Current).TestPages;
30-
queue = DispatcherQueue.GetForCurrentThread();
31+
_queue = DispatcherQueue.GetForCurrentThread();
3132
}
3233

3334
internal bool OpenPage(string pageName)
3435
{
3536
try
3637
{
37-
((App)Application.Current).Log("Trying to Load Page: " + pageName);
38+
Log.Comment("Trying to Load Page: " + pageName);
3839

3940
// Ensure we're on the UI thread as we'll be called from the AppService now.
40-
queue.EnqueueAsync(() =>
41+
_queue.EnqueueAsync(() =>
4142
{
42-
navigationFrame.Navigate(pageMap[pageName]);
43+
navigationFrame.Navigate(FindPageType(pageName));
4344
});
4445
}
4546
catch (Exception e)
4647
{
47-
((App)Application.Current).Log(string.Format("Exception Loading Page {0}: {1} ", pageName, e.Message));
48+
Log.Error("Exception Finding Page {0}: {1} ", pageName, e.Message);
4849
return false;
4950
}
5051

5152
return true;
5253
}
54+
55+
private Type FindPageType(string pageName)
56+
{
57+
try
58+
{
59+
return _executingAssembly.GetType("UITests.App.Pages." + pageName);
60+
}
61+
catch (Exception e)
62+
{
63+
Log.Error("Exception Finding Page {0}: {1} ", pageName, e.Message);
64+
}
65+
66+
return null;
67+
}
68+
69+
private void NavigationFrame_Navigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
70+
{
71+
Log.Comment("Navigated to Page {0}", e.SourcePageType.FullName);
72+
}
73+
74+
private void NavigationFrame_NavigationFailed(object sender, Windows.UI.Xaml.Navigation.NavigationFailedEventArgs e)
75+
{
76+
Log.Error("Failed to navigate to page {0}", e.SourcePageType.FullName);
77+
}
5378
}
5479
}

UITests/UITests.App/TestInterop.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Windows.UI.Xaml;
7+
8+
namespace UITests.App.Pages
9+
{
10+
// TAEF has a different terms for the same concepts as compared with MSTest.
11+
// In order to allow both to use the same test files, we'll define these helper classes
12+
// to translate TAEF into MSTest.
13+
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:File name should match first type name", Justification = "Shim helpers")]
14+
public static class Log
15+
{
16+
public static void Comment(string format, params object[] args)
17+
{
18+
LogMessage("Comment", format, args);
19+
}
20+
21+
public static void Warning(string format, params object[] args)
22+
{
23+
LogMessage("Warning", "[Warning] " + format, args);
24+
}
25+
26+
public static void Error(string format, params object[] args)
27+
{
28+
LogMessage("Error", "[Error] " + format, args);
29+
}
30+
31+
private static void LogMessage(string level, string format, object[] args)
32+
{
33+
// string.Format() complains if we pass it something with braces, even if we have no arguments.
34+
// To account for that, we'll escape braces if we have no arguments.
35+
if (args.Length == 0)
36+
{
37+
format = format.Replace("{", "{{").Replace("}", "}}");
38+
}
39+
40+
// Send back to Test Harness via AppService
41+
// TODO: Make this a cleaner connection/pattern
42+
((App)Application.Current).SendLogMessage(level, string.Format(format, args));
43+
}
44+
}
45+
}

UITests/UITests.App/UITests.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<Compile Include="MainTestHost.xaml.cs">
138138
<DependentUpon>MainTestHost.xaml</DependentUpon>
139139
</Compile>
140+
<Compile Include="TestInterop.cs" />
140141
<Compile Include="Properties\AssemblyInfo.cs" />
141142
</ItemGroup>
142143
<ItemGroup>

UITests/UITests.Tests.MSTest/MSTestInterop.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public static void Comment(string format, params object[] args)
2121

2222
public static void Warning(string format, params object[] args)
2323
{
24-
LogMessage(format, args);
24+
LogMessage("[Warning] " + format, args);
2525
}
2626

2727
public static void Error(string format, params object[] args)
2828
{
29-
LogMessage(format, args);
29+
LogMessage("[Error] " + format, args);
3030
}
3131

3232
private static void LogMessage(string format, object[] args)

UITests/UITests.Tests.Shared/Controls/TextBoxMaskTestPage.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public TextBoxMaskTestPage()
3636
private void ChangeButton_Click(object sender, RoutedEventArgs e)
3737
{
3838
Value = NEW_VALUE;
39+
Log.Comment("Value Changed to {0}", Value);
3940
}
4041
}
4142
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +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!");
23+
Log.Comment("Button Clicked!");
2424
}
2525
}
2626
}

UITests/UITests.Tests.Shared/UITestBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public async Task TestInitialize()
158158
if (response.Message["Status"] as string == "OK")
159159
{
160160
Log.Comment("[Harness] Received Host Ready with Page: {0}", pageName);
161+
Wait.ForIdle();
162+
Log.Comment("[Harness] Starting Test for {0}...", pageName);
161163
return;
162164
}
163165
}
@@ -196,8 +198,7 @@ private void CommunicationService_RequestReceived(AppServiceConnection sender, A
196198
}
197199
catch (Exception e)
198200
{
199-
// Your exception handling code here.
200-
Debug.WriteLine("Exception receiving message: " + e.Message);
201+
Log.Error("Exception receiving message: {0}", e.Message);
201202
}
202203
finally
203204
{

0 commit comments

Comments
 (0)