PowerToys/src/modules/previewpane/UnitTests-PreviewHandlerCommon/FormHandlerControlTests.cs
udit3333 38d537bb8a
Fix preview handlers failing unit tests in CI (#1856)
* Enable Unit tests for PreviewPane Test Assemblies in CI

* Added Preview handlers unit tests project back

* throw for Svg to debug tests

* Removed failing test

* Removed throw statement

* Added MD unitests in yml

* Run ui tests on STA thread

* Throw svg to debug failing tests

* Updated test adaptor

* Removed failing test to validate CI

* throw svg failing test with full stack trace

* Check if mock stream is causing the issue

* narrow down the failing tests code - 1

* narrow down the failing tests code -2

* dispose all controls

* Restore SVg preview control

* Update test adaptor and dependency to latest
2020-04-03 08:12:12 -07:00

200 lines
6.3 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace UnitTests_PreviewHandlerCommon
{
[TestClass]
public class FormHandlerControlTests
{
private class TestFormControl : FormHandlerControl
{ }
[TestMethod]
public void FormHandlerControl_ShouldCreateHandle_OnIntialization()
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
{
// Assert
Assert.IsTrue(testFormHandlerControl.IsHandleCreated);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetVisibleFalse_OnIntialization()
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
{
// Assert
Assert.IsFalse(testFormHandlerControl.Visible);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetFormBorderStyle_OnIntialization()
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
{
// Assert
Assert.AreEqual(FormBorderStyle.None, testFormHandlerControl.FormBorderStyle);
}
}
[TestMethod]
public void FormHandlerControl_ShouldReturnValidHandle_WhenGetHandleCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
// Act
var handle = testFormHandlerControl.GetHandle();
// Assert
Assert.AreEqual(testFormHandlerControl.Handle, handle);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetBackgroundColor_WhenSetBackgroundColorCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var color = Color.Navy;
// Act
testFormHandlerControl.SetBackgroundColor(color);
// Assert
Assert.AreEqual(color, testFormHandlerControl.BackColor);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetFont_WhenSetFontCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var font = new Font("Arial", 20);
// Act
testFormHandlerControl.SetFont(font);
// Assert
Assert.AreEqual(font, testFormHandlerControl.Font);
}
}
[TestMethod]
public void FormHandlerControl_ShouldUpdateBounds_WhenSetRectCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var bounds = new Rectangle(2, 2, 4, 4);
// Act
testFormHandlerControl.SetRect(bounds);
// Assert
Assert.AreEqual(bounds, testFormHandlerControl.Bounds);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetTextColor_WhenSetTextColorCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var color = Color.Navy;
// Act
testFormHandlerControl.SetTextColor(color);
// Assert
Assert.AreEqual(color, testFormHandlerControl.ForeColor);
}
}
[TestMethod]
public void FormHandlerControl_ShouldClearAllControls_WhenUnloadCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
testFormHandlerControl.Controls.Add(new TextBox());
testFormHandlerControl.Controls.Add(new RichTextBox());
// Act
testFormHandlerControl.Unload();
// Assert
Assert.AreEqual(0, testFormHandlerControl.Controls.Count);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetVisibleFalse_WhenUnloadCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
// Act
testFormHandlerControl.Unload();
// Assert
Assert.IsFalse(testFormHandlerControl.Visible);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetVisibletrue_WhenDoPreviewCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
// Act
testFormHandlerControl.DoPreview("valid-path");
// Assert
Assert.IsTrue(testFormHandlerControl.Visible);
}
}
[TestMethod]
public void FormHandlerControl_ShouldSetParentHandle_WhenSetWindowCalled()
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var parentFormWindow = new UserControl();
var parentHwnd = parentFormWindow.Handle;
var rect = new Rectangle(2, 2, 4, 4);
// Act
testFormHandlerControl.SetWindow(parentHwnd, rect);
var actualParentHwnd = GetAncestor(testFormHandlerControl.Handle, 1); // GA_PARENT 1
// Assert
Assert.AreEqual(parentHwnd, actualParentHwnd);
}
}
// Gets the ancestor window: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getancestor
[DllImport("user32.dll")]
private static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
}
}