aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit_tests/shared/worker.hpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-10-24 21:33:44 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-10-27 00:11:41 +0300
commita864099ba77157090c4cd12817245122c163ec24 (patch)
treef15b1f2801219fa9af6111fa28591858d87711ec /test/unit_tests/shared/worker.hpp
parentProcess: add termination methods (diff)
downloadwinapi-common-a864099ba77157090c4cd12817245122c163ec24.tar.gz
winapi-common-a864099ba77157090c4cd12817245122c163ec24.zip
rework Process API & tests
* Add separate classes ProcessParameters & ShellParameters for Process::create() and Process::shell() methods. * Add a separate "worker" executable. It's used in tests via a fairly complicated scheme, receiving orders to execute via a shared memory region. * Add tests that utilize the Console API, reading console window's screen buffer directly, making for more reliable tests & broader coverage.
Diffstat (limited to 'test/unit_tests/shared/worker.hpp')
-rw-r--r--test/unit_tests/shared/worker.hpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/test/unit_tests/shared/worker.hpp b/test/unit_tests/shared/worker.hpp
new file mode 100644
index 0000000..320a924
--- /dev/null
+++ b/test/unit_tests/shared/worker.hpp
@@ -0,0 +1,86 @@
+// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "winapi-common" project.
+// For details, see https://github.com/egor-tensin/winapi-common.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include "command.hpp"
+
+#include <winapi/process.hpp>
+
+#include <windows.h>
+
+#include <exception>
+#include <string>
+#include <utility>
+#include <vector>
+
+namespace worker {
+
+class Worker {
+public:
+ Worker(winapi::Process&& process) : m_cmd{Command::create()}, m_process{std::move(process)} {}
+
+ ~Worker() {
+ try {
+ if (m_process.is_running()) {
+ exit();
+ }
+ } catch (const std::exception&) {
+ }
+ }
+
+ HWND get_console_window() {
+ HWND ret = NULL;
+ m_cmd->get_result(Command::GET_CONSOLE_WINDOW,
+ [&ret](const Command::Result& result) { ret = result.console_window; });
+ return ret;
+ }
+
+ bool is_window_visible() {
+ bool ret = false;
+ m_cmd->get_result(Command::IS_WINDOW_VISIBLE, [&ret](const Command::Result& result) {
+ ret = result.is_window_visible;
+ });
+ return ret;
+ }
+
+ StdHandles get_std_handles() {
+ StdHandles ret;
+ m_cmd->get_result(Command::GET_STD_HANDLES,
+ [&ret](const Command::Result& result) { ret = result.std_handles; });
+ return ret;
+ }
+
+ StdHandles test_write() {
+ StdHandles ret;
+ m_cmd->get_result(Command::TEST_WRITE,
+ [&ret](const Command::Result& result) { ret = result.std_handles; });
+ return ret;
+ }
+
+ std::vector<std::string> read_last_lines(std::size_t numof_lines) {
+ std::vector<std::string> ret;
+ const auto set_args = [numof_lines](Command::Args& args) {
+ args.numof_lines = numof_lines;
+ };
+ const auto read_result = [&ret](const Command::Result& result) {
+ ret = result.console_buffer.extract();
+ };
+ m_cmd->get_result(Command::GET_CONSOLE_BUFFER, set_args, read_result);
+ return ret;
+ }
+
+ int exit() {
+ m_cmd->get_result(Command::EXIT);
+ m_process.wait();
+ return m_process.get_exit_code();
+ }
+
+private:
+ Command::Shared m_cmd;
+ winapi::Process m_process;
+};
+
+} // namespace worker