Skip to content

Commit

Permalink
Incorporate DataCollectionService from microsoft/vs-extension-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Mar 21, 2023
1 parent 9294aad commit fa9edb3
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 71 deletions.
2 changes: 2 additions & 0 deletions eng/pipelines/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ jobs:
/p:TestRunnerAdditionalArguments='-notrait Category=IgnoreForCI -notrait Category=failing'
/bl:$(BUILD.SOURCESDIRECTORY)\artifacts\log\$(_BuildConfig)\IntegrationTest-${{ parameters.targetArchitecture }}.binlog
/m:1
env:
XUNIT_LOGS: $(BUILD.SOURCESDIRECTORY)\artifacts\log\$(_BuildConfig)
displayName: Run Integration Tests

# Create Nuget package, sign, and publish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Threading;
Expand All @@ -18,19 +16,22 @@ namespace System.Windows.Forms.UITests
public abstract class ControlTestBase : IAsyncLifetime, IDisposable
{
private const int SPIF_SENDCHANGE = 0x0002;
private static string? _logPath;
private readonly string _testName;

private bool _clientAreaAnimation;
private DenyExecutionSynchronizationContext? _denyExecutionSynchronizationContext;
private JoinableTaskCollection _joinableTaskCollection = null!;

static ControlTestBase()
{
DataCollectionService.InstallFirstChanceExceptionHandler();
}

protected ControlTestBase(ITestOutputHelper testOutputHelper)
{
TestOutputHelper = testOutputHelper;

_testName = GetTestName(out ITest test);
_logPath ??= GetLogDir(test);

Application.EnableVisualStyles();

Expand All @@ -47,27 +48,6 @@ string GetTestName(out ITest test)
int index = test.DisplayName.IndexOf("("); // Trim arguments from test name.
return index == -1 ? test.DisplayName : test.DisplayName[..(index - 1)];
}

string? GetLogDir(ITest test)
{
try
{
IAssemblyInfo assembly = test.TestCase.TestMethod.TestClass.TestCollection.TestAssembly.Assembly!;
string assemblyPath = ((Xunit.Sdk.ReflectionAssemblyInfo)assembly).Assembly.Location!;

int index = assemblyPath.IndexOf("bin\\");
string config = Debugger.IsAttached ? "Debug" : "Release";
string path = Path.Join(assemblyPath[..index], "log", config, "screenshots");
Directory.CreateDirectory(path);
return path;
}
catch (Exception ex)
{
testOutputHelper.WriteLine($"Failed to get or create log directory. {ex}");
}

return null;
}
}

protected ITestOutputHelper TestOutputHelper { get; }
Expand Down Expand Up @@ -278,9 +258,9 @@ protected async Task RunFormAsync<T>(Func<(Form dialog, T control)> createDialog
await TrySetForegroundWindowAsync(dialog!);
await testDriverAsync(dialog!, control!);
}
catch
catch (Exception ex)
{
TrySaveScreenshot();
DataCollectionService.TryLog(ex);
throw;
}
finally
Expand Down Expand Up @@ -319,9 +299,9 @@ protected async Task RunFormWithoutControlAsync<TForm>(Func<TForm> createForm, F
await TrySetForegroundWindowAsync(dialog!);
await testDriverAsync(dialog!);
}
catch
catch (Exception ex)
{
TrySaveScreenshot();
DataCollectionService.TryLog(ex);
throw;
}
finally
Expand All @@ -343,48 +323,6 @@ protected async Task RunFormWithoutControlAsync<TForm>(Func<TForm> createForm, F
await test.JoinAsync();
}

private void TrySaveScreenshot()
{
if (_logPath is null)
{
return;
}

try
{
var bounds = Screen.PrimaryScreen!.Bounds;

if (bounds.Width <= 0 || bounds.Height <= 0)
{
// Don't try to take a screenshot if there is no screen.
// This may not be an interactive session.
return;
}

using var bitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(
sourceX: bounds.X,
sourceY: bounds.Y,
destinationX: 0,
destinationY: 0,
blockRegionSize: bitmap.Size,
copyPixelOperation: CopyPixelOperation.SourceCopy);
}

int index = _testName.LastIndexOf('.');
string screenshot = $@"{_logPath}\{_testName[(index + 1)..]}_{DateTimeOffset.Now:MMddyyyyhhmmsstt}.png";
bitmap.Save(screenshot);

TestOutputHelper.WriteLine($"Screenshot saved at {screenshot}");
}
catch (Exception ex)
{
TestOutputHelper.WriteLine($"Failed to save screenshot: {ex}.");
}
}

internal struct VoidResult
{
}
Expand Down
Loading

0 comments on commit fa9edb3

Please sign in to comment.