Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/KeyboardChartTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

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
10static void Check(HRESULT hr)
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);
23 assert(Keychart::Marquee(0, 174, 66).showEllipsis);
24 assert(Keychart::Marquee(2.99, 174, 66).showEllipsis);
25 assert(!Keychart::Marquee(3, 174, 66).showEllipsis);
26 assert(Keychart::Marquee(3, 174, 66).offset == 0);
27 assert(Keychart::Marquee(4, 174, 66).offset == 36);
28 assert(Keychart::Marquee(6, 174, 66).offset == 108);
29 assert(Keychart::Marquee(6.99, 174, 66).offset == 108);
30 assert(Keychart::Marquee(7, 174, 66).showEllipsis);
31 assert(Keychart::Marquee(7, 174, 66).offset == 0);
32 assert(Keychart::Marquee(7, 282, 66).offset == 144); // Longer text continues at the same speed.
33 assert(Keychart::Marquee(5, 66, 66).offset == 0);
34 assert(Keychart::Marquee(5, 20, 66).offset == 0);
35 assert(Keychart::Marquee(-1, 174, 66).offset == 0);
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";
38 const char interior = ProfileFlags::GLOBAL | ProfileFlags::INTERIOR;
39 const char ground = ProfileFlags::GLOBAL | ProfileFlags::GV;
41 std::vector<KeyBinding> bindings = {
42 {WALK, DIK_W, false}, {GAS, DIK_W, false}, {PITCH_DOWN, DIK_W, false},
43 {BINDINGSMAP, DIK_F2, false}, {GEAR_CYCLE, DIK_G, false},
44 {GPWSGSINHIBIT_CYCLE, DIK_G, true}, {ADMIN_RELOADVEHICLEPROFILE, DIK_F4, true},
45 {RCS_LEFT, DIK_NUMPAD7, false}, {RCS_FORWARD, DIK_HOME, false},
46 {TOGA, DIK_OEM_102, false},
47 {ENUMERATION, DIK_X, false}, {WALK, -1, false}, {WALK, 256, false}
48 };
49 auto translate = [](BindingEnum f) {
50 const auto name = std::to_wstring(static_cast<int>(f));
51 return Keychart::Label{name, L"Short " + name};
52 };
53 auto labels = Keychart::BuildLabels(bindings, interior, false, translate);
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()); // Remapped help must appear on its new key.
58 assert(labels[DIK_X][0].full.empty());
59 labels = Keychart::BuildLabels(bindings, ground, false, translate);
60 assert(labels[DIK_W][0].full == translate(GAS).full);
61 assert(labels[DIK_G][1].full.empty());
62 labels = Keychart::BuildLabels(bindings, air, false, translate);
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()); // Retired standalone GS control is never exposed.
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);
69 labels = Keychart::BuildLabels(bindings, air, true, translate);
70 assert(labels[DIK_F4][1].full == translate(ADMIN_RELOADVEHICLEPROFILE).full);
71 bindings[4].scancode = DIK_J;
72 labels = Keychart::BuildLabels(bindings, air, false, translate);
73 assert(labels[DIK_G][0].full.empty() && labels[DIK_J][0].full == translate(GEAR_CYCLE).full);
74 bindings.erase(bindings.begin() + 4);
75 labels = Keychart::BuildLabels(bindings, air, false, translate);
76 assert(labels[DIK_J][0].full.empty());
77 bindings.emplace_back(POWER_CYCLE, static_cast<short>(DIK_W), false);
78 labels = Keychart::BuildLabels(bindings, air, false, translate);
79 assert(labels[DIK_W][0].full == translate(PITCH_DOWN).full + L" / " + translate(POWER_CYCLE).full);
80 assert(labels[DIK_W][0].shortName == translate(PITCH_DOWN).shortName + L" / " + translate(POWER_CYCLE).shortName);
81 labels = Keychart::BuildLabels(bindings, air, false, [](BindingEnum) { return Keychart::Label{L"\x822a\x7a7a & test", L""}; });
82 assert(labels[DIK_F2][0].full == L"\x822a\x7a7a & test");
83
84 assert(labels[DIK_F2][0].shortName == labels[DIK_F2][0].full); // Missing abbreviation.
85 labels = Keychart::BuildLabels(bindings, air, false, [](BindingEnum) { return Keychart::Label{L"Full action", L"//"}; });
86 assert(labels[DIK_W][0].shortName == L"Full action / Full action"); // Placeholder preserves both actions.
87 labels = Keychart::BuildLabels(bindings, air, false, [](BindingEnum) { return Keychart::Label{L"", L"Ignored"}; });
88 assert(labels[DIK_F2][0].full.empty() && labels[DIK_F2][0].shortName.empty());
89
90 std::vector<KeyBinding> controlBindings = {
91 {LATERALMODE_LEFT, DIK_LBRACKET, false}, {LATERALMODE_RIGHT, DIK_RBRACKET, false},
92 {VERTICALMODE_UP, DIK_SEMICOLON, false}, {VERTICALMODE_DOWN, DIK_APOSTROPHE, false},
93 {PITCHTRIM_UP, DIK_SEMICOLON, true}, {PITCHTRIM_DOWN, DIK_APOSTROPHE, true},
94 {THRUST0TVMMINUS1KM, DIK_DIVIDE, false}, {THRUST100TVMPLUS1KM, DIK_MULTIPLY, false},
95 {THRUST_MINUS10_TVMMINUS, DIK_SUBTRACT, false}, {THRUST_PLUS10_TVMPLUS, DIK_ADD, false},
96 {AURALWARNINGTEST, DIK_SCROLL, true}, {GEAR_CYCLE, DIK_G, false},
97 {DOWNSHIFT, DIK_DIVIDE, false}, {UPSHIFT, DIK_MULTIPLY, false}
98 };
99 labels = Keychart::BuildLabels(controlBindings, air, false, translate);
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 {
116 labels = Keychart::BuildLabels(controlBindings, profile, false, translate);
117 for (const auto& key : labels)
118 assert(key[2].full.empty() && key[3].full.empty());
119 }
120 // The Control variant follows both parts of a user remap, including Shift.
121 controlBindings[10] = {AURALWARNINGTEST, DIK_F2, false};
122 labels = Keychart::BuildLabels(controlBindings, air, false, translate);
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);
126 labels = Keychart::BuildLabels(controlBindings, air, false, translate);
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);
130 labels = Keychart::BuildLabels(controlBindings, air, false, translate);
131 assert(labels[DIK_LBRACKET][2].full == translate(LATERALMODE_LEFT).full + L" (10x) / " +
132 translate(VERTICALMODE_UP).full + L" (10x)");
133 labels = Keychart::BuildLabels(controlBindings, air, false,
134 [](BindingEnum) { return Keychart::Label{L"Translated action", L"//"}; });
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;
139 for (const auto& key : Keychart::Layout())
140 {
141 assert(codes.insert(key.scancode).second);
142 assert(key.bounds.left >= 0 && key.bounds.right <= Keychart::Width);
143 assert(key.bounds.top >= 0 && key.bounds.bottom <= Keychart::Height);
144 assert(key.bounds.right > key.bounds.left &&
145 key.bounds.bottom - key.bounds.top >= Keychart::LabelTop + Keychart::ModifierCount * Keychart::LabelHeight);
146 for (const auto& other : Keychart::Layout())
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;
162 KeyboardChart chart, referenceChart;
163 Check(chart.OnCreateDevice(device));
164 Check(referenceChart.OnCreateDevice(device));
165 Rendering::RenderSprite* sprite = nullptr;
166 Check(Rendering::CreateSprite(device, &sprite));
167 labels = {};
168 for (const auto& key : Keychart::Layout())
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] = {};
178 for (int shifted = 0; shifted < Keychart::ModifierCount; ++shifted)
179 {
180 labels[DIK_A][shifted] = {L"Parking Brake - On/Off", L"P.Brake"};
181 labels[DIK_B][shifted].shortName = L"Short translated action"; // Still overflows.
182 labels[DIK_D][shifted].shortName = L"//";
183 labels[DIK_E][shifted] = {L"Walk", L"W"};
184 }
185 auto expectedLabels = labels;
186 for (int shifted = 0; shifted < Keychart::ModifierCount; ++shifted)
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();
193 auto render = [&](KeyboardChart& activeChart, const Keychart::Labels& activeLabels, SIZE size, const std::wstring& path)
194 {
195 Check(device->SetRenderState(Rendering::RenderState::SCISSORTESTENABLE, FALSE));
196 Check(device->SetRenderState(Rendering::RenderState::CULLMODE, Rendering::Cull::CW));
197 Check(device->SetRenderState(Rendering::RenderState::STENCILENABLE, TRUE));
199 Check(device->Clear(0, nullptr, Rendering::ClearFlags::TARGET, 0xFF384652, 1, 0));
200 Check(device->BeginScene()); Check(sprite->Begin(Rendering::SpriteFlags::AlphaBlend));
201 // Dialog controls restore the device state while the outer UI sprite
202 // batch is still active. The keyboard chart is drawn after that restore.
203 Rendering::RenderStateSnapshot dialogState(*device);
204 Check(dialogState.Apply());
205 const RECT priorClip = {5, 5, size.cx - 5, size.cy - 5};
206 Check(device->SetScissorRect(&priorClip));
207 Check(device->SetRenderState(Rendering::RenderState::SCISSORTESTENABLE, TRUE));
208 RiseMath::Matrix before, after; RiseMath::MatrixTranslation(&before, 7, 11, 0);
209 Check(sprite->SetTransform(&before));
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));
214 Check(device->GetRenderState(Rendering::RenderState::SCISSORTESTENABLE, &restoredClipEnabled));
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 {
225 chart.OnLostDevice(); referenceChart.OnLostDevice(); Check(sprite->OnLostDevice());
226 fixture.Resize(size.cx,size.cy);
227 chart.OnResetDevice(); referenceChart.OnResetDevice(); Check(sprite->OnResetDevice());
228 std::vector<DWORD> initialPixels;
229 for (int phase = 0; phase < 5; ++phase)
230 {
231 if (phase == 0 || phase == 3)
232 {
233 chart.Update(false, 0);
234 chart.Update(true, 0);
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 // Changing only the short translation must invalidate layout and restart the hold.
246 for (int shifted = 0; shifted < Keychart::ModifierCount; ++shifted)
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 // Separate font atlases can round filtered texels by one color level.
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);
270 const float scale = (std::min)(size.cx / static_cast<float>(Keychart::Width), size.cy / static_cast<float>(Keychart::Height)) * 0.98f;
271 const float left = (size.cx - Keychart::Width * scale) * 0.5f;
272 const float top = (size.cy - Keychart::Height * scale) * 0.5f;
273 for (const auto& key : Keychart::Layout())
274 {
275 if (key.scancode == DIK_SPACE || key.scancode == DIK_A || key.scancode == DIK_E)
276 continue; // Fitting full descriptions and abbreviations must remain stationary.
277 for (int shifted = 0; shifted < Keychart::ModifierCount; ++shifted)
278 {
279 RECT region = {
280 static_cast<LONG>(std::floor((key.bounds.left + 3) * scale + left)),
281 static_cast<LONG>(std::floor((key.bounds.top + Keychart::LabelTop + shifted * Keychart::LabelHeight) * scale + top)),
282 static_cast<LONG>(std::ceil((key.bounds.right - 3) * scale + left)),
283 static_cast<LONG>(std::ceil((key.bounds.top + Keychart::LabelTop + (shifted + 1) * Keychart::LabelHeight) * scale + top))};
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); // Catch a blank chart even when D3D calls succeed.
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]) // No marquee pixels may escape into adjacent keys or captions.
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 }
319 chart.OnDestroyDevice(); referenceChart.OnDestroyDevice();
320 // Device destruction/recreation must also rebuild the texture and font.
321 Check(chart.OnCreateDevice(device)); chart.OnDestroyDevice();
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
BindingEnum
Definition binding.h:6
@ LATERALMODE_LEFT
Definition binding.h:93
@ THRUST0TVMMINUS1KM
Definition binding.h:21
@ DOWNSHIFT
Definition binding.h:19
@ THRUST_MINUS10_TVMMINUS
Definition binding.h:69
@ ADMIN_RELOADVEHICLEPROFILE
Definition binding.h:13
@ PITCH_DOWN
Definition binding.h:84
@ THRUST_PLUS10_TVMPLUS
Definition binding.h:70
@ GPWSGSINHIBIT_CYCLE
Definition binding.h:11
@ BINDINGSMAP
Definition binding.h:71
@ GAS
Definition binding.h:85
@ PITCHTRIM_UP
Definition binding.h:97
@ UPSHIFT
Definition binding.h:20
@ GEAR_CYCLE
Definition binding.h:10
@ LATERALMODE_RIGHT
Definition binding.h:94
@ RCS_LEFT
Definition binding.h:65
@ TOGA
Definition binding.h:100
@ ENUMERATION
Definition binding.h:106
@ RCS_FORWARD
Definition binding.h:63
@ THRUST100TVMPLUS1KM
Definition binding.h:22
@ POWER_CYCLE
Definition binding.h:43
@ AURALWARNINGTEST
Definition binding.h:72
@ VERTICALMODE_DOWN
Definition binding.h:96
@ PITCHTRIM_DOWN
Definition binding.h:98
@ VERTICALMODE_UP
Definition binding.h:95
@ WALK
Definition binding.h:83
@ AIRSPACE
Definition binding.h:114
@ GV
Definition binding.h:113
@ INTERIOR
Definition binding.h:112
@ GLOBAL
Definition binding.h:111
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)
constexpr int LabelTop
constexpr int Width
std::array< std::array< Label, ModifierCount >, 256 > Labels
constexpr int Height
constexpr int ModifierCount
constexpr int LabelHeight
constexpr uint32_t TARGET
constexpr uint32_t NEVER
constexpr uint32_t CW
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)
Definition ClientMath.h:125
path
Definition erin.py:153