summaryrefslogtreecommitdiffstats
path: root/ceee/testing/utils/mock_win32.h
blob: e022964cb29212c504cf7efbe22f631a0c0c55f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Win32 mocks.

#ifndef CEEE_TESTING_UTILS_MOCK_WIN32_H_
#define CEEE_TESTING_UTILS_MOCK_WIN32_H_

// atlwin.h re#defines SetWindowLongPtr and it caused a build error here.
#ifdef __ATLWIN_H__
#error "Do not include <atlwin.h> ahead of this."
#endif

#include <windows.h>

#include "ceee/testing/sidestep/auto_testing_hook.h"
#include "ceee/testing/utils/mock_static.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace testing {

// Mock object for some Kernel32 functions.
MOCK_STATIC_CLASS_BEGIN(MockKernel32)
  MOCK_STATIC_INIT_BEGIN(MockKernel32)
    MOCK_STATIC_INIT(GetModuleFileName);
    MOCK_STATIC_INIT(GetNativeSystemInfo);
    MOCK_STATIC_INIT(OpenThread);
    MOCK_STATIC_INIT(GetCommandLine);
  MOCK_STATIC_INIT_END()

  MOCK_STATIC3(DWORD, WINAPI, GetModuleFileName, HMODULE, LPTSTR, DWORD);
  MOCK_STATIC1(void, WINAPI, GetNativeSystemInfo, LPSYSTEM_INFO);
  MOCK_STATIC3(HANDLE, CALLBACK, OpenThread, DWORD, BOOL, DWORD);
  MOCK_STATIC0(LPTSTR, WINAPI, GetCommandLine);
MOCK_STATIC_CLASS_END(MockKernel32)

// Separate from MockKernel32 since here be dragons.
// First off, putting the LoadLibrary functions with the rest of MockUser32
// is not a good idea since we want to avoid hooking these functions whenever
// the others are used. Also, for some undetermined reason, they just don't
// work when coupled in the MockUser32 class. So chances are you cannot use both
// MockLoadLibrary and MockUser32 in the same test...
MOCK_STATIC_CLASS_BEGIN(MockLoadLibrary)
  MOCK_STATIC_INIT_BEGIN(MockLoadLibrary)
    MOCK_STATIC_INIT(LoadLibrary);
    MOCK_STATIC_INIT(GetProcAddress);
    MOCK_STATIC_INIT(FreeLibrary);
  MOCK_STATIC_INIT_END()

  MOCK_STATIC1(HINSTANCE, WINAPI, LoadLibrary, LPCTSTR);
  MOCK_STATIC2(FARPROC, WINAPI, GetProcAddress, HMODULE, LPCSTR);
  MOCK_STATIC1(BOOL, WINAPI, FreeLibrary, HMODULE);
MOCK_STATIC_CLASS_END(MockLoadLibrary)

// Mock object for some User32 functions.
MOCK_STATIC_CLASS_BEGIN(MockUser32)
  MOCK_STATIC_INIT_BEGIN(MockUser32)
    MOCK_STATIC_INIT(DefWindowProc);
    MOCK_STATIC_INIT(EnumChildWindows);
    MOCK_STATIC_INIT(EnumWindows);
    MOCK_STATIC_INIT(IsWindow);
    MOCK_STATIC_INIT(IsWindowVisible);
    MOCK_STATIC_INIT(GetClassName);
    MOCK_STATIC_INIT(GetForegroundWindow);
    MOCK_STATIC_INIT(GetParent);
    MOCK_STATIC_INIT(GetTopWindow);
    MOCK_STATIC_INIT(GetWindow);
    MOCK_STATIC_INIT(GetWindowRect);
    MOCK_STATIC_INIT(GetWindowThreadProcessId);
    MOCK_STATIC_INIT(MoveWindow);
    MOCK_STATIC_INIT(PostMessage);
    MOCK_STATIC_INIT(SendMessage);
    MOCK_STATIC_INIT(SetProp);
    MOCK_STATIC_INIT(SetWindowLongPtr);
    MOCK_STATIC_INIT(SetWindowPos);
  MOCK_STATIC_INIT_END()

  MOCK_STATIC4(LRESULT, CALLBACK, DefWindowProc, HWND, UINT, WPARAM, LPARAM);
  MOCK_STATIC3(BOOL, CALLBACK, EnumChildWindows, HWND, WNDENUMPROC, LPARAM);
  MOCK_STATIC2(BOOL, CALLBACK, EnumWindows, WNDENUMPROC, LPARAM);
  MOCK_STATIC1(BOOL, CALLBACK, IsWindow, HWND);
  MOCK_STATIC1(BOOL, CALLBACK, IsWindowVisible, HWND);
  MOCK_STATIC3(int, CALLBACK, GetClassName, HWND, LPTSTR, int);
  MOCK_STATIC0(HWND, CALLBACK, GetForegroundWindow);
  MOCK_STATIC1(HWND, CALLBACK, GetParent, HWND);
  MOCK_STATIC1(HWND, CALLBACK, GetTopWindow, HWND);
  MOCK_STATIC2(HWND, CALLBACK, GetWindow, HWND, UINT);
  MOCK_STATIC2(BOOL, CALLBACK, GetWindowRect, HWND, LPRECT);
  MOCK_STATIC2(DWORD, CALLBACK, GetWindowThreadProcessId, HWND, LPDWORD);
  MOCK_STATIC6(BOOL, CALLBACK, MoveWindow,
               HWND, int, int, int, int, BOOL);
  MOCK_STATIC4(BOOL, CALLBACK, PostMessage, HWND, UINT, WPARAM, LPARAM);
  MOCK_STATIC4(LRESULT, CALLBACK, SendMessage, HWND, UINT, WPARAM, LPARAM);
  MOCK_STATIC3(BOOL, CALLBACK, SetProp, HWND, LPCWSTR, HANDLE);
  MOCK_STATIC3(LONG_PTR, CALLBACK, SetWindowLongPtr, HWND, int, LONG_PTR);
  MOCK_STATIC7(BOOL, CALLBACK, SetWindowPos,
               HWND, HWND, int, int, int, int, UINT);
MOCK_STATIC_CLASS_END(MockUser32)

// Static functions for (effectively) mocking GetNativeSystemInfo
class NativeSystemInfoMockTool {
 public:
  static void SetEnvironment_x86(LPSYSTEM_INFO psys_info) {
    memset(psys_info, 0, sizeof(*psys_info));
    psys_info->wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
    psys_info->dwNumberOfProcessors = 1;
  }

  static void SetEnvironment_x64(LPSYSTEM_INFO psys_info) {
    memset(psys_info, 0, sizeof(*psys_info));
    psys_info->wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64;
    psys_info->dwNumberOfProcessors = 4;
  }
};

}  // namespace testing

#endif  // CEEE_TESTING_UTILS_MOCK_WIN32_H_