1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11//
12// vie_autotest_windows.cc
13//
14
15#include "webrtc/video_engine/test/auto_test/interface/vie_autotest_windows.h"
16
17#include "webrtc/video_engine/test/auto_test/interface/vie_autotest_defines.h"
18#include "webrtc/video_engine/test/auto_test/interface/vie_autotest_main.h"
19
20#include "webrtc/engine_configurations.h"
21#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
22#include "webrtc/system_wrappers/interface/thread_wrapper.h"
23
24#include <windows.h>
25
26#ifdef _DEBUG
27//#include "vld.h"
28#endif
29
30// Disable Visual studio warnings
31// 'this' : used in base member initializer list
32#pragma warning(disable: 4355)
33
34LRESULT CALLBACK ViEAutoTestWinProc(HWND hWnd, UINT uMsg, WPARAM wParam,
35                                    LPARAM lParam) {
36  switch (uMsg) {
37    case WM_DESTROY:
38      PostQuitMessage( WM_QUIT);
39      break;
40    case WM_COMMAND:
41      break;
42  }
43  return DefWindowProc(hWnd, uMsg, wParam, lParam);
44}
45
46ViEAutoTestWindowManager::ViEAutoTestWindowManager()
47    : _window1(NULL),
48      _window2(NULL),
49      _terminate(false),
50      _eventThread(*webrtc::ThreadWrapper::CreateThread(
51          EventProcess, this, webrtc::kNormalPriority,
52          "ViEAutotestEventThread")),
53      _crit(*webrtc::CriticalSectionWrapper::CreateCriticalSection()),
54      _hwnd1(NULL),
55      _hwnd2(NULL),
56      _hwnd1Size(),
57      _hwnd2Size(),
58      _hwnd1Title(),
59      _hwnd2Title() {
60}
61
62ViEAutoTestWindowManager::~ViEAutoTestWindowManager() {
63  if (_hwnd1) {
64    ViEDestroyWindow(_hwnd1);
65  }
66  if (_hwnd2) {
67    ViEDestroyWindow(_hwnd2);
68  }
69  delete &_crit;
70}
71
72void* ViEAutoTestWindowManager::GetWindow1() {
73  return _window1;
74}
75
76void* ViEAutoTestWindowManager::GetWindow2() {
77  return _window2;
78}
79
80int ViEAutoTestWindowManager::CreateWindows(AutoTestRect window1Size,
81                                            AutoTestRect window2Size,
82                                            void* window1Title,
83                                            void* window2Title) {
84  _hwnd1Size.Copy(window1Size);
85  _hwnd2Size.Copy(window2Size);
86  memcpy(_hwnd1Title, window1Title, TITLE_LENGTH);
87  memcpy(_hwnd2Title, window2Title, TITLE_LENGTH);
88
89  unsigned int tId = 0;
90  _eventThread.Start(tId);
91
92  do {
93    _crit.Enter();
94    if (_window1 != NULL) {
95      break;
96    }
97    _crit.Leave();
98    AutoTestSleep(10);
99  } while (true);
100  _crit.Leave();
101  return 0;
102}
103
104int ViEAutoTestWindowManager::TerminateWindows() {
105  _eventThread.SetNotAlive();
106
107  _terminate = true;
108  if (_eventThread.Stop()) {
109    _crit.Enter();
110    delete &_eventThread;
111    _crit.Leave();
112  }
113
114  return 0;
115}
116
117bool ViEAutoTestWindowManager::EventProcess(void* obj) {
118  return static_cast<ViEAutoTestWindowManager*> (obj)->EventLoop();
119}
120
121bool ViEAutoTestWindowManager::EventLoop() {
122  _crit.Enter();
123
124  ViECreateWindow(_hwnd1, _hwnd1Size.origin.x, _hwnd1Size.origin.y,
125                  _hwnd1Size.size.width, _hwnd1Size.size.height, _hwnd1Title);
126  ViECreateWindow(_hwnd2, _hwnd2Size.origin.x, _hwnd2Size.origin.y,
127                  _hwnd2Size.size.width, _hwnd2Size.size.height, _hwnd2Title);
128
129  _window1 = (void*) _hwnd1;
130  _window2 = (void*) _hwnd2;
131  MSG msg;
132  while (!_terminate) {
133    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
134      TranslateMessage(&msg);
135      DispatchMessage(&msg);
136    }
137    _crit.Leave();
138    AutoTestSleep(10);
139    _crit.Enter();
140  }
141  ViEDestroyWindow(_hwnd1);
142  ViEDestroyWindow(_hwnd2);
143  _crit.Leave();
144
145  return false;
146}
147
148int ViEAutoTestWindowManager::ViECreateWindow(HWND &hwndMain, int xPos,
149                                              int yPos, int width, int height,
150                                              TCHAR* className) {
151  HINSTANCE hinst = GetModuleHandle(0);
152  WNDCLASSEX wcx;
153  wcx.hInstance = hinst;
154  wcx.lpszClassName = className;
155  wcx.lpfnWndProc = (WNDPROC) ViEAutoTestWinProc;
156  wcx.style = CS_DBLCLKS;
157  wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
158  wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
159  wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
160  wcx.lpszMenuName = NULL;
161  wcx.cbSize = sizeof(WNDCLASSEX);
162  wcx.cbClsExtra = 0;
163  wcx.cbWndExtra = 0;
164  wcx.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
165
166  RegisterClassEx(&wcx);
167
168  // Create the main window.
169  hwndMain = CreateWindowEx(0,          // no extended styles
170                            className,  // class name
171                            className,  // window name
172                            WS_OVERLAPPED | WS_THICKFRAME,  // overlapped window
173                            xPos,    // horizontal position
174                            yPos,    // vertical position
175                            width,   // width
176                            height,  // height
177                            (HWND) NULL,   // no parent or owner window
178                            (HMENU) NULL,  // class menu used
179                            hinst,  // instance handle
180                            NULL);  // no window creation data
181
182  if (!hwndMain)
183    return -1;
184
185  // Show the window using the flag specified by the program
186  // that started the application, and send the application
187  // a WM_PAINT message.
188  ShowWindow(hwndMain, SW_SHOWDEFAULT);
189  UpdateWindow(hwndMain);
190
191  ::SetWindowPos(hwndMain, HWND_TOP, xPos, yPos, width, height,
192                 SWP_FRAMECHANGED);
193
194  return 0;
195}
196
197int ViEAutoTestWindowManager::ViEDestroyWindow(HWND& hwnd) {
198  ::DestroyWindow(hwnd);
199  return 0;
200}
201
202bool ViEAutoTestWindowManager::SetTopmostWindow() {
203  // Meant to put terminal window on top
204  return true;
205}
206
207int main(int argc, char* argv[]) {
208  ViEAutoTestMain auto_test;
209  return auto_test.RunTests(argc, argv);
210}
211