Skip to content

Commit f62a60b

Browse files
Merge pull request #4090 from michael-hawker/user/mhawker/gridsplitter-tests
🧪 Add UI Tests for Gridsplitter
2 parents ca7a31b + 9a338f7 commit f62a60b

File tree

17 files changed

+645
-28
lines changed

17 files changed

+645
-28
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Layout/GridSplitter/GridSplitter.xaml

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,37 @@
44

55
<ResourceDictionary.ThemeDictionaries>
66
<ResourceDictionary x:Key="Default">
7-
<SolidColorBrush x:Key="SystemControlSplitterPointerOver" Color="{ThemeResource SystemBaseLowColor}" />
8-
<SolidColorBrush x:Key="SystemControlSplitterPressed" Color="{ThemeResource SystemBaseHighColor}" />
7+
<SolidColorBrush x:Key="SystemControlSplitterPointerOver"
8+
Color="{ThemeResource SystemBaseLowColor}" />
9+
<SolidColorBrush x:Key="SystemControlSplitterPressed"
10+
Color="{ThemeResource SystemBaseHighColor}" />
911
</ResourceDictionary>
1012
<ResourceDictionary x:Key="HighContrast">
11-
<SolidColorBrush x:Key="SystemControlSplitterPointerOver" Color="{ThemeResource SystemColorHighlightColor}" />
12-
<SolidColorBrush x:Key="SystemControlSplitterPressed" Color="{ThemeResource SystemColorHighlightColor}" />
13+
<SolidColorBrush x:Key="SystemControlSplitterPointerOver"
14+
Color="{ThemeResource SystemColorHighlightColor}" />
15+
<SolidColorBrush x:Key="SystemControlSplitterPressed"
16+
Color="{ThemeResource SystemColorHighlightColor}" />
1317
</ResourceDictionary>
1418
</ResourceDictionary.ThemeDictionaries>
1519

1620
<Style TargetType="local:GridSplitter">
17-
<Setter Property="IsTabStop" Value="True"></Setter>
18-
<Setter Property="UseSystemFocusVisuals" Value="True"></Setter>
19-
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
20-
<Setter Property="VerticalAlignment" Value="Stretch"></Setter>
21-
<Setter Property="IsFocusEngagementEnabled" Value="True"></Setter>
22-
<Setter Property="MinWidth" Value="16"></Setter>
23-
<Setter Property="MinHeight" Value="16"></Setter>
24-
<Setter Property="Background" Value="{ThemeResource SystemControlHighlightChromeHighBrush}"></Setter>
21+
<Setter Property="IsTabStop" Value="True" />
22+
<Setter Property="UseSystemFocusVisuals" Value="True" />
23+
<Setter Property="HorizontalAlignment" Value="Stretch" />
24+
<Setter Property="VerticalAlignment" Value="Stretch" />
25+
<Setter Property="IsFocusEngagementEnabled" Value="True" />
26+
<Setter Property="MinWidth" Value="16" />
27+
<Setter Property="MinHeight" Value="16" />
28+
<Setter Property="Background" Value="{ThemeResource SystemControlHighlightChromeHighBrush}" />
2529
<Setter Property="GripperForeground" Value="{ThemeResource SystemControlForegroundAltHighBrush}" />
2630
<Setter Property="Template">
2731
<Setter.Value>
2832
<ControlTemplate TargetType="local:GridSplitter">
29-
<Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
33+
<Grid x:Name="RootGrid"
34+
Background="{TemplateBinding Background}">
35+
<ContentPresenter HorizontalContentAlignment="Stretch"
36+
VerticalContentAlignment="Stretch"
37+
Content="{TemplateBinding Element}" />
3038
<VisualStateManager.VisualStateGroups>
3139
<VisualStateGroup x:Name="GridSplitterStates">
3240
<VisualState x:Name="Normal" />
@@ -42,7 +50,6 @@
4250
</VisualState>
4351
</VisualStateGroup>
4452
</VisualStateManager.VisualStateGroups>
45-
<ContentPresenter Content="{TemplateBinding Element}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
4653
</Grid>
4754
</ControlTemplate>
4855
</Setter.Value>

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

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

55
using System;
6+
using System.Collections.Generic;
67
using System.Threading.Tasks;
78
using Microsoft.Toolkit.Mvvm.Messaging;
89
using UITests.App.Pages;
@@ -49,6 +50,8 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS
4950
return;
5051
}
5152

53+
Log.Comment("Received Command: {0}", cmd);
54+
5255
switch (cmd)
5356
{
5457
case "OpenPage":
@@ -65,6 +68,44 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS
6568

6669
await args.Request.SendResponseAsync(pageResponse ? OkResult : BadResult);
6770

71+
break;
72+
case "Custom":
73+
if (!TryGetValueAndLog(message, "Id", out var id) || !_customCommands.ContainsKey(id))
74+
{
75+
await args.Request.SendResponseAsync(BadResult);
76+
break;
77+
}
78+
79+
Log.Comment("Received request for custom command: {0}", id);
80+
81+
try
82+
{
83+
ValueSet response = await _customCommands[id].Invoke(message);
84+
85+
if (response != null)
86+
{
87+
response.Add("Status", "OK");
88+
}
89+
else
90+
{
91+
await args.Request.SendResponseAsync(BadResult);
92+
break;
93+
}
94+
95+
await args.Request.SendResponseAsync(response);
96+
}
97+
catch (Exception e)
98+
{
99+
ValueSet errmsg = new() { { "Status", "BAD" }, { "Exception", e.Message }, { "StackTrace", e.StackTrace } };
100+
if (e.InnerException != null)
101+
{
102+
errmsg.Add("InnerException", e.InnerException.Message);
103+
errmsg.Add("InnerExceptionStackTrace", e.InnerException.StackTrace);
104+
}
105+
106+
await args.Request.SendResponseAsync(errmsg);
107+
}
108+
68109
break;
69110
default:
70111
break;
@@ -77,11 +118,15 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS
77118

78119
private void OnAppServicesCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
79120
{
121+
Log.Error("Background Task Instance Canceled. Reason: {0}", reason.ToString());
122+
80123
_appServiceDeferral.Complete();
81124
}
82125

83126
private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
84127
{
128+
Log.Error("AppServiceConnection Service Closed. AppServicesClosedStatus: {0}", args.Status.ToString());
129+
85130
_appServiceDeferral.Complete();
86131
}
87132

@@ -118,5 +163,12 @@ private static bool TryGetValueAndLog(ValueSet message, string key, out string v
118163

119164
return true;
120165
}
166+
167+
private Dictionary<string, Func<ValueSet, Task<ValueSet>>> _customCommands = new();
168+
169+
internal void RegisterCustomCommand(string id, Func<ValueSet, Task<ValueSet>> customCommandFunction)
170+
{
171+
_customCommands.Add(id, customCommandFunction);
172+
}
121173
}
122-
}
174+
}

UITests/UITests.App/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Reflection;
9+
using UITests.App.Commands;
910
using UITests.App.Pages;
1011
using Windows.ApplicationModel;
1112
using Windows.ApplicationModel.Activation;
13+
using Windows.System;
1214
using Windows.UI.Xaml;
1315
using Windows.UI.Xaml.Controls;
1416
using Windows.UI.Xaml.Navigation;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.Linq;
7+
using System.Reflection;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization;
10+
using System.Threading.Tasks;
11+
using Microsoft.Toolkit;
12+
using Microsoft.Toolkit.Uwp;
13+
using Microsoft.Toolkit.Uwp.UI;
14+
using UITests.App.Pages;
15+
using Windows.Foundation.Collections;
16+
using Windows.System;
17+
using Windows.UI.Xaml;
18+
using Windows.UI.Xaml.Controls;
19+
20+
namespace UITests.App.Commands
21+
{
22+
public static class VisualTreeHelperCommands
23+
{
24+
private static DispatcherQueue Queue { get; set; }
25+
26+
private static JsonSerializerOptions SerializerOptions { get; } = new JsonSerializerOptions(JsonSerializerDefaults.General)
27+
{
28+
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
29+
};
30+
31+
public static void Initialize(DispatcherQueue uiThread)
32+
{
33+
Queue = uiThread;
34+
35+
(App.Current as App).RegisterCustomCommand("VisualTreeHelper.FindElementProperty", FindElementProperty);
36+
}
37+
38+
public static async Task<ValueSet> FindElementProperty(ValueSet arguments)
39+
{
40+
ValueSet results = new ValueSet();
41+
42+
if (Queue == null)
43+
{
44+
Log.Error("VisualTreeHelper - Missing UI DispatcherQueue");
45+
return null;
46+
}
47+
48+
await Queue.EnqueueAsync(() =>
49+
{
50+
// Dispatch?
51+
var content = Window.Current.Content as Frame;
52+
53+
if (content == null)
54+
{
55+
Log.Error("VisualTreeHelper.FindElementProperty - Window has no content.");
56+
return;
57+
}
58+
59+
if (arguments.TryGetValue("ElementName", out object value) && value is string name &&
60+
arguments.TryGetValue("Property", out object value2) && value2 is string propertyName)
61+
{
62+
Log.Comment("VisualTreeHelper.FindElementProperty('{0}', '{1}')", name, propertyName);
63+
64+
// 1. Find Element in Visual Tree
65+
var element = content.FindDescendant(name);
66+
67+
try
68+
{
69+
Log.Comment("VisualTreeHelper.FindElementProperty - Found Element? {0}", element != null);
70+
71+
var typeinfo = element.GetType().GetTypeInfo();
72+
73+
Log.Comment("Element Type: {0}", typeinfo.FullName);
74+
75+
var prop = element.GetType().GetTypeInfo().GetProperty(propertyName);
76+
77+
if (prop == null)
78+
{
79+
Log.Error("VisualTreeHelper.FindElementProperty - Couldn't find Property named {0} on type {1}", propertyName, typeinfo.FullName);
80+
return;
81+
}
82+
83+
// 2. Get the property using reflection
84+
var propValue = prop.GetValue(element);
85+
86+
// 3. Serialize and return the result
87+
results.Add("Result", JsonSerializer.Serialize(propValue, SerializerOptions));
88+
}
89+
catch (Exception e)
90+
{
91+
Log.Error("Error {0}", e.Message);
92+
Log.Error("StackTrace:\n{0}", e.StackTrace);
93+
}
94+
}
95+
});
96+
97+
if (results.Count > 0)
98+
{
99+
return results;
100+
}
101+
102+
return null; // Failure
103+
}
104+
}
105+
}

UITests/UITests.App/MainTestHost.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Microsoft.Toolkit.Mvvm.Messaging;
99
using Microsoft.Toolkit.Uwp;
10+
using UITests.App.Commands;
1011
using UITests.App.Pages;
1112
using Windows.System;
1213
using Windows.UI.Xaml;
@@ -33,6 +34,9 @@ public MainTestHost()
3334
WeakReferenceMessenger.Default.Register<RequestPageMessage>(this);
3435

3536
_queue = DispatcherQueue.GetForCurrentThread();
37+
38+
// Initialize Custom Commands for AppService
39+
VisualTreeHelperCommands.Initialize(_queue);
3640
}
3741

3842
public void Receive(RequestPageMessage message)

UITests/UITests.App/Package.appxmanifest

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
33
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
44
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
5-
IgnorableNamespaces="uap mp">
5+
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
6+
IgnorableNamespaces="uap mp rescap">
67

78
<Identity
89
Name="3568ebdf-5b6b-4ddd-bb17-462d614ba50f"
@@ -48,5 +49,7 @@
4849

4950
<Capabilities>
5051
<Capability Name="internetClient" />
52+
<!-- Our AppService Background Connection will Timeout after a period of time otherwise. -->
53+
<rescap:Capability Name="extendedBackgroundTaskTime"/>
5154
</Capabilities>
5255
</Package>

UITests/UITests.App/Properties/Default.rd.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
the application package. The asterisks are not wildcards.
2222
-->
2323
<Assembly Name="*Application*" Dynamic="Required All" />
24+
<Namespace Name="Windows.UI.Xaml.Controls" Dynamic="Required All" />
25+
<Namespace Name="Microsoft.UI.Xaml.Controls" Dynamic="Required All" />
2426
<!-- Add your application specific runtime directives here. -->
2527
</Application>
2628
</Directives>

UITests/UITests.App/TestInterop.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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 System;
5+
using System.Diagnostics;
66
using Windows.UI.Xaml;
77

88
namespace UITests.App.Pages
@@ -37,9 +37,13 @@ private static void LogMessage(string level, string format, object[] args)
3737
format = format.Replace("{", "{{").Replace("}", "}}");
3838
}
3939

40+
var message = string.Format(format, args);
41+
42+
Debug.WriteLine(message);
43+
4044
// Send back to Test Harness via AppService
4145
// TODO: Make this a cleaner connection/pattern
42-
((App)Application.Current).SendLogMessage(level, string.Format(format, args));
46+
_ = ((App)Application.Current).SendLogMessage(level, message);
4347
}
4448
}
4549
}

UITests/UITests.App/UITests.App.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<Compile Include="App.AppService.xaml.cs">
133133
<DependentUpon>App.xaml</DependentUpon>
134134
</Compile>
135+
<Compile Include="Commands\VisualTreeHelperCommands.cs" />
135136
<Compile Include="HomePage.xaml.cs">
136137
<DependentUpon>HomePage.xaml</DependentUpon>
137138
</Compile>
@@ -178,6 +179,9 @@
178179
<PackageReference Include="MUXAppTestHelpers">
179180
<Version>0.0.4</Version>
180181
</PackageReference>
182+
<PackageReference Include="System.Text.Json">
183+
<Version>5.0.2</Version>
184+
</PackageReference>
181185
</ItemGroup>
182186
<ItemGroup>
183187
<None Include="UITests.App.pfx" />

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
3131
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
3232
<PackageReference Include="Microsoft.Windows.Apps.Test" Version="1.0.181205002" />
33+
<PackageReference Include="System.Text.Json" Version="5.0.2" />
3334
</ItemGroup>
3435

3536
<Import Project="..\UITests.Tests.Shared\UITests.Tests.Shared.projitems" Label="Shared" />

0 commit comments

Comments
 (0)