1#include "../KeyboardChart.h"
2#include "NativeUiTest11.h"
3#include <cassert>
4#include <iostream>
5#include <set>
6#include <crtdbg.h>
7#include <cmath>
8#include <limits>
9
11{
12 if (FAILED(hr))
13 {
14 std::cerr << "D3D check failed: 0x" << std::hex << hr << '\n';
15 std::exit(1);
16 }
17}
18
19int wmain(int argc, wchar_t** argv)
20{
21 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
22 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
36 assert(
Keychart::Marquee(std::numeric_limits<double>::infinity(), 174, 66).offset == 0);
37 std::cout << "PASS: marquee start pause, steady speed, length-dependent duration, end pause, repeat and fitting text\n";
41 std::vector<KeyBinding> bindings = {
46 {
TOGA, DIK_OEM_102,
false},
48 };
50 const auto name = std::to_wstring(static_cast<int>(f));
52 };
54 assert(labels[DIK_W][0].full == translate(
WALK).full);
55 assert(labels[DIK_G][0].full.empty());
56 assert(labels[DIK_F2][0].full == translate(
BINDINGSMAP).full);
57 assert(labels[DIK_F1][0].full.empty());
58 assert(labels[DIK_X][0].full.empty());
60 assert(labels[DIK_W][0].full == translate(
GAS).full);
61 assert(labels[DIK_G][1].full.empty());
63 assert(labels[DIK_W][0].full == translate(
PITCH_DOWN).full);
64 assert(labels[DIK_G][0].full == translate(
GEAR_CYCLE).full);
65 assert(labels[DIK_G][1].full.empty());
66 assert(labels[DIK_F4][1].full.empty());
67 assert(labels[DIK_NUMPAD7][0].full != labels[DIK_HOME][0].full);
68 assert(labels[DIK_OEM_102][0].full == translate(
TOGA).full);
71 bindings[4].scancode = DIK_J;
73 assert(labels[DIK_G][0].full.empty() && labels[DIK_J][0].full == translate(
GEAR_CYCLE).full);
74 bindings.erase(bindings.begin() + 4);
76 assert(labels[DIK_J][0].full.empty());
77 bindings.emplace_back(
POWER_CYCLE,
static_cast<short>(DIK_W),
false);
80 assert(labels[DIK_W][0].shortName == translate(
PITCH_DOWN).shortName + L
" / " + translate(
POWER_CYCLE).shortName);
82 assert(labels[DIK_F2][0].full == L"\x822a\x7a7a & test");
83
84 assert(labels[DIK_F2][0].shortName == labels[DIK_F2][0].full);
86 assert(labels[DIK_W][0].shortName == L"Full action / Full action");
88 assert(labels[DIK_F2][0].full.empty() && labels[DIK_F2][0].shortName.empty());
89
90 std::vector<KeyBinding> controlBindings = {
98 };
100 for (size_t i = 0; i < 6; ++i)
101 {
102 const auto& binding = controlBindings[i];
103 const int row = binding.shift ? 3 : 2;
104 assert(labels[binding.scancode][row].full == translate(binding.function).full + L" (10x)");
105 assert(labels[binding.scancode][row].shortName == translate(binding.function).shortName + L" x10");
106 }
107 assert(labels[DIK_DIVIDE][2].full == L"TVM/DOR -10 km/s");
108 assert(labels[DIK_MULTIPLY][2].full == L"TVM/DOR +10 km/s");
109 assert(labels[DIK_SUBTRACT][2].full == L"TVM/DOR -10 m/s");
110 assert(labels[DIK_ADD][2].full == L"TVM/DOR +10 m/s");
111 assert(labels[DIK_SCROLL][2].full.empty());
112 assert(labels[DIK_SCROLL][3].full == L"TCAS system test");
113 assert(labels[DIK_G][2].full.empty() && labels[DIK_G][3].full.empty());
114 for (const char profile : {interior, ground})
115 {
117 for (const auto& key : labels)
118 assert(key[2].full.empty() && key[3].full.empty());
119 }
120
123 assert(labels[DIK_SCROLL][3].full.empty());
124 assert(labels[DIK_F2][2].full == L"TCAS system test" && labels[DIK_F2][3].full.empty());
125 controlBindings.erase(controlBindings.begin() + 10);
127 assert(labels[DIK_F2][2].full.empty());
128 controlBindings.emplace_back(
VERTICALMODE_UP,
static_cast<short>(DIK_LBRACKET),
false);
129 controlBindings.emplace_back(
POWER_CYCLE,
static_cast<short>(DIK_LBRACKET),
false);
131 assert(labels[DIK_LBRACKET][2].full == translate(
LATERALMODE_LEFT).full + L
" (10x) / " +
135 assert(labels[DIK_LBRACKET][2].shortName == L"Translated action x10 / Translated action x10");
136 std::cout << "PASS: supported Control actions, Control + Shift, TVM/DOR units, TCAS, remaps, removals, profiles and conflicts\n";
137
138 std::set<short> codes;
140 {
141 assert(codes.insert(key.scancode).second);
144 assert(key.bounds.right > key.bounds.left &&
147 {
148 RECT overlap;
149 if (key.scancode != other.scancode)
150 assert(!IntersectRect(&overlap, &key.bounds, &other.bounds));
151 }
152 }
153 assert(codes.size() == 105);
154 for (const auto& binding : bindings)
155 if (binding.scancode > 0 && binding.scancode < 256)
156 assert(codes.count(binding.scancode));
157 std::cout << "PASS: remaps, removals, three contexts, Shift, admin, invalid input, conflicts, Unicode and 105 non-overlapping keys\n";
158
159 if (argc < 2 || std::wstring(argv[1]) != L"--render") return 0;
160 NativeUiTest11 fixture(1920,1080);
161 auto* device=&fixture.device;
167 labels = {};
169 {
170 labels[key.scancode][0] = {L"Normal action with a long translated name", L""};
171 labels[key.scancode][1] = {L"Shift action with a long translated name", L""};
172 labels[key.scancode][2] = {L"Control action with a long translated name", L""};
173 labels[key.scancode][3] = {L"Control + Shift action with a long translated name", L""};
174 }
175 labels[DIK_SPACE][0] = {L"Brakes", L"B"};
176 labels[DIK_SPACE][1] = {L"Parking Brake - On/Off", L"P.Brake"};
177 labels[DIK_SPACE][2] = labels[DIK_SPACE][3] = {};
179 {
180 labels[DIK_A][shifted] = {L"Parking Brake - On/Off", L"P.Brake"};
181 labels[DIK_B][shifted].shortName = L"Short translated action";
182 labels[DIK_D][shifted].shortName = L"//";
183 labels[DIK_E][shifted] = {L"Walk", L"W"};
184 }
185 auto expectedLabels = labels;
187 {
188 expectedLabels[DIK_A][shifted].full = L"P.Brake";
189 expectedLabels[DIK_B][shifted].full = L"Short translated action";
190 }
191 for (auto& key : expectedLabels)
192 for (auto& label : key) label.shortName.clear();
194 {
201
202
204 Check(dialogState.Apply());
205 const RECT priorClip = {5, 5, size.cx - 5, size.cy - 5};
206 Check(device->SetScissorRect(&priorClip));
210 activeChart.
Draw(sprite, activeLabels, L
"Keyboard bindings - truncation and device reset check", size.cx, size.cy);
211 Check(sprite->
GetTransform(&after)); assert(before == after);
212 RECT restoredClip;
DWORD restoredClipEnabled;
213 Check(device->GetScissorRect(&restoredClip));
215 assert(EqualRect(&priorClip, &restoredClip) && restoredClipEnabled);
216 Check(sprite->
End()); Check(device->EndScene());
217 auto frame=fixture.Pixels();
218 if(!
path.empty())fixture.Save(path);
219 fixture.DebugClean();
220 return frame;
221 };
222 const SIZE sizes[] = {{1920,1080}, {1280,720}, {2560,720}, {800,600}};
223 for (auto size : sizes)
224 {
226 fixture.Resize(size.cx,size.cy);
228 std::vector<DWORD> initialPixels;
229 for (int phase = 0; phase < 5; ++phase)
230 {
231 if (phase == 0 || phase == 3)
232 {
235 referenceChart.
Update(
false, 0);
236 referenceChart.
Update(
true, 0);
237 }
238 else
239 {
240 chart.
Update(
true, phase == 1 ? 2.0f : 3.0f);
241 referenceChart.
Update(
true, phase == 1 ? 2.0f : 3.0f);
242 }
243 if (phase == 4)
244 {
245
247 {
248 labels[DIK_A][shifted].shortName = labels[DIK_A][shifted].shortName == L"P.Brake" ? L"Park" : L"P.Brake";
249 expectedLabels[DIK_A][shifted].full = labels[DIK_A][shifted].shortName;
250 }
251 }
252 const auto path = L
"Debug/keychart-" + std::to_wstring(size.cx) + L
"-phase-" + std::to_wstring(phase) + L
".png";
253 const auto framePixels = render(chart, labels, size, path);
254 const auto expectedPixels = render(referenceChart, expectedLabels, size, path + L"-expected.png");
255
256 for (size_t i = 0; i < framePixels.size(); ++i)
257 for (int channel = 0; channel < 32; channel += 8)
258 {
259 const int actual = (framePixels[i] >> channel) & 255;
260 const int expected = (expectedPixels[i] >> channel) & 255;
261 if (std::abs(actual - expected) > 1)
262 {
263 std::cerr << "Label selection mismatch at " << size.cx << " pixels, phase " << phase
264 << ", position " << (i % size.cx) << "," << (i / size.cx) << "\n";
265 std::exit(1);
266 }
267 }
268 int normalPixels = 0, shiftedPixels = 0, controlPixels = 0, controlShiftPixels = 0;
269 std::vector<bool> canMove(static_cast<size_t>(size.cx) * size.cy, false);
274 {
275 if (key.scancode == DIK_SPACE || key.scancode == DIK_A || key.scancode == DIK_E)
276 continue;
278 {
279 RECT region = {
280 static_cast<LONG>(std::floor((key.bounds.left + 3) * scale + left)),
282 static_cast<LONG>(std::ceil((key.bounds.right - 3) * scale + left)),
284 for (LONG y = region.top; y < region.bottom; ++y)
285 for (LONG x = region.left; x < region.right; ++x)
286 canMove[static_cast<size_t>(y) * size.cx + x] = true;
287 }
288 }
289 for (
DWORD pixel : framePixels)
290 {
291 const int r = (pixel >> 16) & 255, g = (pixel >> 8) & 255, b = pixel & 255;
292 if (r > 180 && g > 180 && b > 180) ++normalPixels;
293 if (r > 180 && g > 130 && b < 140) ++shiftedPixels;
294 if (r < 150 && g > 170 && b > 200) ++controlPixels;
295 if (r > 130 && r < 190 && g > 200 && b < 170) ++controlShiftPixels;
296 }
297 assert(normalPixels > 100 && shiftedPixels > 100);
298 assert(controlPixels > 100 && controlShiftPixels > 100);
299 if (phase == 0) initialPixels = framePixels;
300 else if (phase == 1 || phase == 3) assert(initialPixels == framePixels);
301 else if (phase == 2)
302 {
303 int changed = 0;
304 for (size_t i = 0; i < framePixels.size(); ++i)
305 {
306 if (framePixels[i] == initialPixels[i]) continue;
307 if (!canMove[i])
308 {
309 std::cerr << "Marquee escaped at " << size.cx << " pixels, position " << i%size.cx << "," << i/size.cx
310 << ", initial=" << std::hex << initialPixels[i] << ", current=" << framePixels[i] << '\n';
311 std::exit(1);
312 }
313 ++changed;
314 }
315 assert(changed > 100);
316 }
317 }
318 }
320
322 sprite->
Release(); fixture.DebugClean();
323 std::cout << "PASS: native D3D11 rendering at four sizes, clipped scrolling, initial hold, reopening, full/short label selection, missing abbreviations, stationary fitting labels, short-label changes, device reset/recreation and render-state restoration\n";
324}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
@ THRUST_MINUS10_TVMMINUS
@ ADMIN_RELOADVEHICLEPROFILE
void Draw(Rendering::RenderSprite *sprite, const Keychart::Labels &labels, const std::wstring &title, int displayWidth, int displayHeight)
void Update(bool visible, float elapsedSeconds)
HRESULT OnCreateDevice(Rendering::RenderDevice *device)
HRESULT Begin(DWORD flags)
HRESULT SetTransform(const RiseMath::Matrix *value)
HRESULT GetTransform(RiseMath::Matrix *value) const
const std::vector< Key > & Layout()
MarqueePosition Marquee(double elapsedSeconds, LONG textWidth, LONG availableWidth)
Labels BuildLabels(const std::vector< KeyBinding > &bindings, char profile, bool admin, Translate translate)
std::array< std::array< Label, ModifierCount >, 256 > Labels
constexpr int ModifierCount
constexpr int LabelHeight
constexpr uint32_t TARGET
constexpr uint32_t STENCILFUNC
constexpr uint32_t CULLMODE
constexpr uint32_t STENCILENABLE
constexpr uint32_t SCISSORTESTENABLE
constexpr DWORD AlphaBlend
HRESULT CreateSprite(RenderDevice *owner, RenderSprite **result)
Matrix * MatrixTranslation(Matrix *out, float x, float y, float z)