PowerToys/src/modules/previewpane/UnitTests-PreviewHandlerCommon/FormHandlerControlTests.cs

200 lines
6.0 KiB
C#
Raw Normal View History

2020-08-18 01:00:56 +08:00
// 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 System;
using System.Drawing;
2020-09-30 01:43:18 +08:00
2020-08-18 01:00:56 +08:00
using System.Windows.Forms;
using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
2020-09-30 01:43:18 +08:00
namespace PreviewHandlerCommonUnitTests
2020-08-18 01:00:56 +08:00
{
[TestClass]
public class FormHandlerControlTests
{
private class TestFormControl : FormHandlerControl
{
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldCreateHandleOnInitialization()
2020-08-18 01:00:56 +08:00
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
{
// Assert
Assert.IsTrue(testFormHandlerControl.IsHandleCreated);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetVisibleFalseOnInitialization()
2020-08-18 01:00:56 +08:00
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
{
// Assert
Assert.IsFalse(testFormHandlerControl.Visible);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetFormBorderStyleOnInitialization()
2020-08-18 01:00:56 +08:00
{
// Arrange and act
using (var testFormHandlerControl = new TestFormControl())
{
// Assert
Assert.AreEqual(FormBorderStyle.None, testFormHandlerControl.FormBorderStyle);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldReturnValidHandleWhenGetHandleCalled()
2020-08-18 01:00:56 +08:00
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
// Act
var handle = testFormHandlerControl.Handle;
2020-08-18 01:00:56 +08:00
// Assert
Assert.AreEqual(testFormHandlerControl.Handle, handle);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetBackgroundColorWhenSetBackgroundColorCalled()
2020-08-18 01:00:56 +08:00
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var color = Color.Navy;
// Act
testFormHandlerControl.SetBackgroundColor(color);
// Assert
Assert.AreEqual(color, testFormHandlerControl.BackColor);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetFontWhenSetFontCalled()
2020-08-18 01:00:56 +08:00
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
2020-09-30 01:43:18 +08:00
using (var font = new Font("Arial", 20))
{
// Act
testFormHandlerControl.SetFont(font);
// Assert
Assert.AreEqual(font, testFormHandlerControl.Font);
}
2020-08-18 01:00:56 +08:00
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldUpdateBoundsWhenSetRectCalled()
2020-08-18 01:00:56 +08:00
{
// 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]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetTextColorWhenSetTextColorCalled()
2020-08-18 01:00:56 +08:00
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
var color = Color.Navy;
// Act
testFormHandlerControl.SetTextColor(color);
// Assert
Assert.AreEqual(color, testFormHandlerControl.ForeColor);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldClearAllControlsWhenUnloadCalled()
2020-08-18 01:00:56 +08:00
{
// 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]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetVisibleFalseWhenUnloadCalled()
2020-08-18 01:00:56 +08:00
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
// Act
testFormHandlerControl.Unload();
// Assert
Assert.IsFalse(testFormHandlerControl.Visible);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetVisibletrueWhenDoPreviewCalled()
2020-08-18 01:00:56 +08:00
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
// Act
testFormHandlerControl.DoPreview("valid-path");
// Assert
Assert.IsTrue(testFormHandlerControl.Visible);
}
}
[TestMethod]
2020-09-30 01:43:18 +08:00
public void FormHandlerControlShouldSetParentHandleWhenSetWindowCalled()
2020-08-18 01:00:56 +08:00
{
// Arrange
using (var testFormHandlerControl = new TestFormControl())
{
2020-09-30 01:43:18 +08:00
using (var parentFormWindow = new UserControl())
{
var parentHwnd = parentFormWindow.Handle;
var rect = new Rectangle(2, 2, 4, 4);
// Act
testFormHandlerControl.SetWindow(parentHwnd, rect);
var actualParentHwnd = NativeMethods.GetAncestor(testFormHandlerControl.Handle, 1); // GA_PARENT 1
// Assert
Assert.AreEqual(parentHwnd, actualParentHwnd);
}
2020-08-18 01:00:56 +08:00
}
}
}
}