Skip to content

Commit 3f0400c

Browse files
committed
update maui devicetests from upstream
1 parent 0d643ff commit 3f0400c

File tree

66 files changed

+4683
-2786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+4683
-2786
lines changed
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
using Microsoft.CodeAnalysis;
22

3-
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen;
4-
5-
[Generator]
6-
public class CodeBehindGenerator : ISourceGenerator
3+
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen
74
{
8-
public void Initialize(GeneratorInitializationContext context)
5+
[Generator]
6+
public class CodeBehindGenerator : ISourceGenerator
97
{
10-
//#if DEBUG
11-
//if (!System.Diagnostics.Debugger.IsAttached)
12-
// System.Diagnostics.Debugger.Launch();
13-
//#endif
14-
}
8+
public void Initialize(GeneratorInitializationContext context)
9+
{
10+
//#if DEBUG
11+
//if (!System.Diagnostics.Debugger.IsAttached)
12+
// System.Diagnostics.Debugger.Launch();
13+
//#endif
14+
}
1515

16-
public void Execute(GeneratorExecutionContext context)
17-
{
18-
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.TargetFramework", out var targetFramework))
19-
return;
16+
public void Execute(GeneratorExecutionContext context)
17+
{
18+
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.TargetFramework", out var targetFramework))
19+
return;
2020

21-
context.Log($"TargetFramework: {targetFramework}");
21+
context.Log($"TargetFramework: {targetFramework}");
2222

23-
var generator = new RunnerGenerator(context, targetFramework);
23+
var generator = new RunnerGenerator(context, targetFramework);
2424

25-
generator?.Generate();
25+
generator?.Generate();
26+
}
2627
}
2728
}
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
using System.Diagnostics;
22
using Microsoft.CodeAnalysis;
33

4-
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen;
5-
6-
static class GeneratorDiagnostics
4+
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen
75
{
8-
public static readonly DiagnosticDescriptor LoggingMessage = new DiagnosticDescriptor(
9-
id: "TST1001",
10-
title: "Logging Message",
11-
messageFormat: "{0}",
12-
category: "Logging",
13-
DiagnosticSeverity.Info,
14-
isEnabledByDefault: true);
6+
static class GeneratorDiagnostics
7+
{
8+
public static readonly DiagnosticDescriptor LoggingMessage = new DiagnosticDescriptor(
9+
id: "TST1001",
10+
title: "Logging Message",
11+
messageFormat: "{0}",
12+
category: "Logging",
13+
DiagnosticSeverity.Info,
14+
isEnabledByDefault: true);
1515

16-
[Conditional("DEBUG")]
17-
public static void Log(this GeneratorExecutionContext context, string message) =>
18-
context.ReportDiagnostic(Diagnostic.Create(LoggingMessage, Location.None, message));
16+
[Conditional("DEBUG")]
17+
public static void Log(this GeneratorExecutionContext context, string message) =>
18+
context.ReportDiagnostic(Diagnostic.Create(LoggingMessage, Location.None, message));
19+
}
1920
}

test/MauiTestUtils/DeviceTests.Runners.SourceGen/RunnerGenerator.cs

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,99 @@
33
using Microsoft.CodeAnalysis;
44
using Microsoft.CodeAnalysis.Text;
55

6-
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen;
7-
8-
public class RunnerGenerator
6+
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen
97
{
10-
public RunnerGenerator(GeneratorExecutionContext context, string targetFramework)
8+
public class RunnerGenerator
119
{
12-
Context = context;
10+
public RunnerGenerator(GeneratorExecutionContext context, string targetFramework)
11+
{
12+
Context = context;
1313

14-
TargetFramework = targetFramework;
14+
TargetFramework = targetFramework;
1515

16-
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationId", out var applicationId);
17-
context.Log($"ApplicationId: {applicationId}");
18-
ApplicationId = applicationId ?? throw new Exception("ApplicationId needs to be set.");
16+
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationId", out var applicationId);
17+
context.Log($"ApplicationId: {applicationId}");
18+
ApplicationId = applicationId ?? throw new Exception("ApplicationId needs to be set.");
1919

20-
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationTitle", out var applicationTitle);
21-
context.Log($"ApplicationTitle: {applicationTitle}");
22-
ApplicationTitle = applicationTitle ?? "Tests";
20+
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationTitle", out var applicationTitle);
21+
context.Log($"ApplicationTitle: {applicationTitle}");
22+
ApplicationTitle = applicationTitle ?? "Tests";
2323

24-
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace);
25-
context.Log($"RootNamespace: {rootNamespace}");
26-
RootNamespace = rootNamespace ?? "TestRunnerNamespace";
24+
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace);
25+
context.Log($"RootNamespace: {rootNamespace}");
26+
RootNamespace = rootNamespace ?? "TestRunnerNamespace";
2727

28-
ContainsSplashScreen = false;
29-
foreach (var file in context.AdditionalFiles)
30-
{
31-
var options = context.AnalyzerConfigOptions.GetOptions(file);
32-
if (options.TryGetValue("build_metadata.AdditionalFiles.IsMauiSplashScreen", out var isMauiSplashScreen) && bool.TryParse(isMauiSplashScreen, out var isSplash) && isSplash)
28+
ContainsSplashScreen = false;
29+
foreach (var file in context.AdditionalFiles)
3330
{
34-
ContainsSplashScreen = true;
35-
break;
31+
var options = context.AnalyzerConfigOptions.GetOptions(file);
32+
if (options.TryGetValue("build_metadata.AdditionalFiles.IsMauiSplashScreen", out var isMauiSplashScreen) && bool.TryParse(isMauiSplashScreen, out var isSplash) && isSplash)
33+
{
34+
ContainsSplashScreen = true;
35+
break;
36+
}
3637
}
38+
context.Log($"ContainsSplashScreen: {ContainsSplashScreen}");
3739
}
38-
context.Log($"ContainsSplashScreen: {ContainsSplashScreen}");
39-
}
4040

41-
public GeneratorExecutionContext Context { get; }
41+
public GeneratorExecutionContext Context { get; }
4242

43-
public string TargetFramework { get; }
43+
public string TargetFramework { get; }
4444

45-
public string RootNamespace { get; }
45+
public string RootNamespace { get; }
4646

47-
public string ApplicationId { get; }
47+
public string ApplicationId { get; }
4848

49-
public string ApplicationTitle { get; }
49+
public string ApplicationTitle { get; }
5050

51-
public bool ContainsSplashScreen { get; }
51+
public bool ContainsSplashScreen { get; }
5252

53-
public void Generate()
54-
{
55-
Context.Log($"Generating runners...");
56-
57-
if (TargetFramework.IndexOf("-android", StringComparison.OrdinalIgnoreCase) != -1)
53+
public void Generate()
5854
{
59-
var code = GenerateAndroidSource();
60-
var name = "TestRunner.Android.sg.cs";
55+
Context.Log($"Generating runners...");
6156

62-
AddSource(name, code);
63-
}
64-
else if (TargetFramework.IndexOf("-ios", StringComparison.OrdinalIgnoreCase) != -1)
65-
{
66-
var code = GenerateIosSource();
67-
var name = "TestRunner.iOS.sg.cs";
57+
if (TargetFramework.IndexOf("-android", StringComparison.OrdinalIgnoreCase) != -1)
58+
{
59+
var code = GenerateAndroidSource();
60+
var name = "TestRunner.Android.sg.cs";
6861

69-
AddSource(name, code);
70-
}
71-
else if (TargetFramework.IndexOf("-maccatalyst", StringComparison.OrdinalIgnoreCase) != -1)
72-
{
73-
var code = GenerateIosSource();
74-
var name = "TestRunner.MacCatalyst.sg.cs";
62+
AddSource(name, code);
63+
}
64+
else if (TargetFramework.IndexOf("-ios", StringComparison.OrdinalIgnoreCase) != -1)
65+
{
66+
var code = GenerateIosSource();
67+
var name = "TestRunner.iOS.sg.cs";
7568

76-
AddSource(name, code);
69+
AddSource(name, code);
70+
}
71+
else if (TargetFramework.IndexOf("-maccatalyst", StringComparison.OrdinalIgnoreCase) != -1)
72+
{
73+
var code = GenerateIosSource();
74+
var name = "TestRunner.MacCatalyst.sg.cs";
75+
76+
AddSource(name, code);
77+
}
7778
}
78-
}
7979

80-
protected void AddSource(string filename, string contents)
81-
{
82-
Context.Log($"AddSource: {filename}");
83-
Context.AddSource(filename, SourceText.From(contents, Encoding.UTF8));
84-
}
80+
protected void AddSource(string filename, string contents)
81+
{
82+
Context.Log($"AddSource: {filename}");
83+
Context.AddSource(filename, SourceText.From(contents, Encoding.UTF8));
84+
}
8585

86-
string GenerateAndroidSource()
87-
{
88-
var mauiProgramName = "MauiProgram";
89-
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
90-
var splash = ContainsSplashScreen ? @"Theme = ""@style/Maui.SplashTheme""," : "";
86+
string GenerateAndroidSource()
87+
{
88+
var mauiProgramName = "MauiProgram";
89+
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
90+
var splash = ContainsSplashScreen ? @"Theme = ""@style/Maui.SplashTheme""," : "";
9191

92-
var appName = "MainApplication";
93-
var visualActivityName = "MainActivity";
92+
var appName = "MainApplication";
93+
var visualActivityName = "MainActivity";
9494

95-
var instrumentationName = "TestInstrumentation";
96-
var headlessActivityName = "TestActivity";
95+
var instrumentationName = "TestInstrumentation";
96+
var headlessActivityName = "TestActivity";
9797

98-
return @"
98+
return @"
9999
#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_APPLICATION_GENERATION
100100
namespace " + RootNamespace + @"
101101
{
@@ -162,16 +162,16 @@ public partial class " + headlessActivityName + @" : global::Microsoft.Maui.Test
162162
}
163163
#endif
164164
";
165-
}
165+
}
166166

167-
string GenerateIosSource()
168-
{
169-
var mauiProgramName = "MauiProgram";
170-
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
171-
var visualDelegateName = "VisualRunnerAppDelegate";
172-
var headlessDelegateName = "HeadlessRunnerAppDelegate";
167+
string GenerateIosSource()
168+
{
169+
var mauiProgramName = "MauiProgram";
170+
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
171+
var visualDelegateName = "VisualRunnerAppDelegate";
172+
var headlessDelegateName = "HeadlessRunnerAppDelegate";
173173

174-
return @"
174+
return @"
175175
#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_RUNNER_PROGRAM_GENERATION
176176
namespace " + RootNamespace + @"
177177
{
@@ -219,5 +219,6 @@ partial class " + headlessDelegateName + @" : global::Microsoft.Maui.TestUtils.D
219219
}
220220
#endif
221221
";
222+
}
222223
}
223224
}

test/MauiTestUtils/DeviceTests.Runners.SourceGen/TestUtils.DeviceTests.Runners.SourceGen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>

test/MauiTestUtils/DeviceTests.Runners/AppHostBuilderExtensions.cs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,50 @@
66
using Microsoft.Maui.TestUtils.DeviceTests.Runners.HeadlessRunner;
77
using Microsoft.Maui.TestUtils.DeviceTests.Runners.VisualRunner;
88

9-
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners;
10-
11-
public static class AppHostBuilderExtensions
9+
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners
1210
{
13-
public static MauiAppBuilder ConfigureTests(this MauiAppBuilder appHostBuilder, TestOptions options)
11+
public static class AppHostBuilderExtensions
1412
{
15-
appHostBuilder.Services.AddSingleton(options);
16-
17-
return appHostBuilder;
18-
}
13+
public static MauiAppBuilder ConfigureTests(this MauiAppBuilder appHostBuilder, TestOptions options)
14+
{
15+
appHostBuilder.Services.AddSingleton(options);
16+
17+
return appHostBuilder;
18+
}
19+
20+
public static MauiAppBuilder UseVisualRunner(this MauiAppBuilder appHostBuilder)
21+
{
22+
appHostBuilder.UseMauiApp(svc => new MauiVisualRunnerApp(
23+
svc.GetRequiredService<TestOptions>(),
24+
svc.GetRequiredService<ILoggerFactory>().CreateLogger("TestRun")));
25+
26+
return appHostBuilder;
27+
}
28+
29+
public static MauiAppBuilder UseHeadlessRunner(this MauiAppBuilder appHostBuilder, HeadlessRunnerOptions options)
30+
{
31+
appHostBuilder.Services.AddSingleton(options);
32+
33+
#if __ANDROID__ || __IOS__ || MACCATALYST || WINDOWS
34+
appHostBuilder.Services.AddTransient(svc => new HeadlessTestRunner(
35+
svc.GetRequiredService<HeadlessRunnerOptions>(),
36+
svc.GetRequiredService<TestOptions>()));
37+
#endif
1938

20-
public static MauiAppBuilder UseVisualRunner(this MauiAppBuilder appHostBuilder)
21-
{
22-
appHostBuilder.UseMauiApp(svc => new MauiVisualRunnerApp(
23-
svc.GetRequiredService<TestOptions>(),
24-
svc.GetRequiredService<ILoggerFactory>().CreateLogger("TestRun")));
39+
return appHostBuilder;
40+
}
2541

26-
return appHostBuilder;
27-
}
42+
#if WINDOWS
43+
public static MauiAppBuilder UseControlsHeadlessRunner(this MauiAppBuilder appHostBuilder, HeadlessRunnerOptions options)
44+
{
45+
appHostBuilder.Services.AddSingleton(options);
2846

29-
public static MauiAppBuilder UseHeadlessRunner(this MauiAppBuilder appHostBuilder, HeadlessRunnerOptions options)
30-
{
31-
appHostBuilder.Services.AddSingleton(options);
47+
appHostBuilder.Services.AddTransient(svc => new ControlsHeadlessTestRunner(
48+
svc.GetRequiredService<HeadlessRunnerOptions>(),
49+
svc.GetRequiredService<TestOptions>()));
3250

33-
#if __ANDROID__ || __IOS__ || MACCATALYST
34-
appHostBuilder.Services.AddTransient(svc => new HeadlessTestRunner(
35-
svc.GetRequiredService<HeadlessRunnerOptions>(),
36-
svc.GetRequiredService<TestOptions>()));
51+
return appHostBuilder;
52+
}
3753
#endif
38-
39-
return appHostBuilder;
4054
}
4155
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global using Microsoft.Maui;
2+
global using Microsoft.Maui.Graphics;
3+
global using Microsoft.Maui.Handlers;
4+
global using Microsoft.Maui.Platform;

0 commit comments

Comments
 (0)