Как насчет извлечения INativeMethods
?
Неполный список того, что мы получим бесплатно:
- Полная поддержка TDD
- Вам не нужно запускать приложение для проверки большинства случаев
- Действительно легко моделировать и добавлять поддержку для различных сред \ ОС
- Профилирование производительности, измерение количества вызовов в WinApi
Покажите мне код
Интерфейс идентичен WinApi:
internal interface INativeMethods
{
IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wparam, StringBuilder lparam);
bool GetWindowRect(IntPtr hwnd, out Rect rect);
IntPtr GetWindow(IntPtr hwnd, uint cmd);
bool IsWindowVisible(IntPtr hwnd);
long GetTickCount64();
int GetClassName(IntPtr hwnd, StringBuilder classNameBuffer, int maxCount);
int DwmGetWindowAttribute(IntPtr hwnd, int attribute, out Rect rect, int sizeOfRect);
bool GetWindowPlacement(IntPtr hwnd, ref WindowPlacement pointerToWindowPlacement);
int GetDeviceCaps(IntPtr hdc, int index);
}
Реализация - тонкий прокси для статического класса:
internal class NativeMethodsWraper : INativeMethods
{
public IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wparam, StringBuilder lparam)
{
return NativeMethods.SendMessage(hwnd, msg, wparam, lparam);
}
public bool GetWindowRect(IntPtr hwnd, out Rect rect)
{
return NativeMethods.GetWindowRect(hwnd, out rect);
}
public IntPtr GetWindow(IntPtr hwnd, uint cmd)
{
return NativeMethods.GetWindow(hwnd, cmd);
}
public bool IsWindowVisible(IntPtr hwnd)
{
return NativeMethods.IsWindowVisible(hwnd);
}
public long GetTickCount64()
{
return NativeMethods.GetTickCount64();
}
public int GetClassName(IntPtr hwnd, StringBuilder classNameBuffer, int maxCount)
{
return NativeMethods.GetClassName(hwnd, classNameBuffer, maxCount);
}
public int DwmGetWindowAttribute(IntPtr hwnd, int attribute, out Rect rect, int sizeOfRect)
{
return NativeMethods.DwmGetWindowAttribute(hwnd, attribute, out rect, sizeOfRect);
}
public bool GetWindowPlacement(IntPtr hwnd, ref WindowPlacement pointerToWindowPlacement)
{
return NativeMethods.GetWindowPlacement(hwnd, ref pointerToWindowPlacement);
}
public int GetDeviceCaps(IntPtr hdc, int index)
{
return NativeMethods.GetDeviceCaps(hdc, index);
}
}
Давайте дополним это импортом P \ Invoke
internal static class NativeMethods
{
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetDeviceCaps(IntPtr hdc, int index);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, [Out] StringBuilder lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("kernel32.dll")]
public static extern long GetTickCount64();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);
[DllImport(@"dwmapi.dll")]
public static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out Rect pvAttribute, int cbAttribute);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl);
}
Пример использования
internal class GetTickCount64TimeProvider : ITimeProvider
{
private readonly INativeMethods _nativeMethods;
public GetTickCount64TimeProvider(INativeMethods nativeMethods)
{
_nativeMethods = nativeMethods;
}
public Timestamp Now()
{
var gtc = _nativeMethods.GetTickCount64();
var getTickCountStamp = Timestamp.FromMilliseconds(gtc);
return getTickCountStamp;
}
}
Модульное тестирование
Трудно поверить, но можно проверить любые ожидания, издеваясь над WinApi
[Test]
public void GetTickCount64_ShouldCall_NativeMethod()
{
var nativeMock = MockRepository.GenerateMock<INativeMethods>();
var target = GetTarget(nativeMock);
target.Now();
nativeMock.AssertWasCalled(_ => _.GetTickCount64());
}
[Test]
public void Now_ShouldReturn_Microseconds()
{
var expected = Timestamp.FromMicroseconds((long) int.MaxValue * 1000);
var nativeStub = MockRepository.GenerateStub<INativeMethods>();
nativeStub.Stub(_ => _.GetTickCount64()).Return(int.MaxValue);
var target = GetTarget(nativeStub);
var actual = target.Now();
Assert.AreEqual(expected, actual);
}
private static GetTickCount64TimeProvider GetTarget(INativeMethods nativeMock)
{
return new GetTickCount64TimeProvider(nativeMock);
}
Насмешливые out\ref
параметры могут вызвать головную боль, поэтому вот код для дальнейшего использования:
[Test]
public void When_WindowIsMaximized_PaddingBordersShouldBeExcludedFromArea()
{
// Top, Left are -8 when window is maximized but should be 0,0
// http://blogs.msdn.com/b/oldnewthing/archive/2012/03/26/10287385.aspx
INativeMethods nativeMock = MockRepository.GenerateStub<INativeMethods>();
var windowRectangle = new Rect() {Left = -8, Top = -8, Bottom = 1216, Right = 1936};
var expectedScreenBounds = new Rect() {Left = 0, Top = 0, Bottom = 1200, Right = 1920};
_displayInfo.Stub(_ => _.GetScreenBoundsFromWindow(windowRectangle.ToRectangle())).Return(expectedScreenBounds.ToRectangle());
var hwnd = RandomNativeHandle();
StubForMaximizedWindowState(nativeMock, hwnd);
StubForDwmRectangle(nativeMock, hwnd, windowRectangle);
WindowCoverageReader target = GetTarget(nativeMock);
var window = target.GetWindowFromHandle(hwnd);
Assert.AreEqual(WindowState.Maximized, window.WindowState);
Assert.AreEqual(expectedScreenBounds.ToRectangle(), window.Area);
}
private void StubForDwmRectangle(INativeMethods nativeMock, IntPtr hwnd, Rect rectToReturnFromWinApi)
{
var sizeOf = Marshal.SizeOf(rect);
var rect = new Rect();
nativeMock.Stub(_ =>
{
_.DwmGetWindowAttribute(
hwnd,
(int)DwmWindowAttribute.DwmwaExtendedFrameBounds,
out rect, // called with zeroed object
sizeOf);
}).OutRef(rectToReturnFromWinApi).Return(0);
}
private IntPtr RandomNativeHandle()
{
return new IntPtr(_random.Next());
}
private void StubForMaximizedWindowState(INativeMethods nativeMock, IntPtr hwnd)
{
var maximizedFlag = 3;
WindowPlacement pointerToWindowPlacement = new WindowPlacement() {ShowCmd = maximizedFlag};
nativeMock.Stub(_ => { _.GetWindowPlacement(Arg<IntPtr>.Is.Equal(hwnd), ref Arg<WindowPlacement>.Ref(new Anything(), pointerToWindowPlacement).Dummy); }).Return(true);
}