1#include "../Dialogs/HudDiagnostics.h"
2#include "../Dialogs/DialogBase.h"
3#include "../Logger.h"
4#ifdef ChatEnter
5#include <RakNetTypes.h>
6#endif
7
8#include <cstdio>
9#include <cstring>
10#include <cwchar>
11#include <new>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
16namespace
17{
18 bool poisonNextEdit = false;
19 int submissions = 0;
20 int buttonClicks = 0;
21 std::vector<std::string> messages;
22
23 void Check(bool result, const char* description)
24 {
25 if (!result) throw std::runtime_error(description);
26 }
27
28 void SeedEnterHotkey(void* memory, std::size_t size)
29 {
30
31
32 const UINT enter = VK_RETURN;
33 std::memset(memory, 0, size);
34 for (std::size_t offset = 0; offset + sizeof enter <= size; offset += sizeof enter)
35 std::memcpy(static_cast<char*>(memory) + offset, &enter, sizeof enter);
36 }
37
38 void CALLBACK OnEvent(UINT event, int, CDXUTControl*, void*)
39 {
42 }
43
44 void CheckEditInput(CDXUTDialog& hud)
45 {
46 submissions = 0;
47 hud.EnableKeyboardInput(true);
48 hud.SetCallback(OnEvent);
49 for (int id = 1; id <= 2; ++id)
50 {
51 Check(hud.GetEditBox(id)->GetHotkey() == 0, "Edit hotkey was not initialized");
52 hud.GetEditBox(id)->SetVisible(false);
53 }
54 CDXUTDialog::ClearFocus();
55 Check(!hud.MsgProc(nullptr, WM_CHAR, VK_RETURN, 1), "REGRESSION: hidden edit swallowed global Enter");
56 Check(submissions == 0, "Hidden edit submitted a message");
57 Check(!hud.MsgProc(nullptr, WM_CHAR, VK_RETURN, (1L << 24) | 1), "Hidden edit swallowed numpad Enter");
58 Check(!hud.MsgProc(nullptr, WM_SYSCHAR, VK_RETURN, 1), "Hidden edit swallowed Alt+Enter");
59
60 auto* edit = hud.GetEditBox(1);
61 edit->SetVisible(true);
62 hud.RequestFocus(edit);
63 Check(edit->m_bHasFocus, "Visible edit did not acquire focus");
64 Check(hud.MsgProc(nullptr, WM_CHAR, L'x', 1), "Active edit did not accept typing");
65 Check(std::wcscmp(edit->GetText(), L"x") == 0, "Active edit text changed unexpectedly");
66 Check(hud.MsgProc(nullptr, WM_CHAR, VK_RETURN, 1) && submissions == 1, "Active edit did not submit Enter exactly once");
67 edit->SetVisible(false);
68 Check(!hud.MsgProc(nullptr, WM_CHAR, VK_RETURN, 1), "Hidden formerly focused edit swallowed next Enter");
69 Check(submissions == 1, "Hidden formerly focused edit submitted again");
70 CDXUTDialog::ClearFocus();
71 }
72}
73
74
75void* operator new(std::size_t size, const std::nothrow_t&) noexcept
76{
77 try
78 {
79 void* memory = ::operator new(size);
80 if (poisonNextEdit)
81 {
82 poisonNextEdit = false;
83 SeedEnterHotkey(memory, size);
84 }
85 return memory;
86 }
87 catch (...) { return nullptr; }
88}
89
90void operator delete(void* memory, const std::nothrow_t&) noexcept { ::operator delete(memory); }
91
92
100#ifdef ChatEnter
101RakNet::SystemAddress::SystemAddress() {}
102#endif
103
105{
106 try
107 {
108 CDXUTDialogResourceManager manager;
110 {
111 CDXUTDialog hud;
112 hud.Init(&manager, true);
113 hud.EnableKeyboardInput(true);
114 void* controlMemory = ::operator new(sizeof(CDXUTControl));
115 SeedEnterHotkey(controlMemory, sizeof(CDXUTControl));
116 auto* control = new (controlMemory) CDXUTControl(&hud);
117 const UINT defaultHotkey = control->GetHotkey();
118 delete control;
119 Check(defaultHotkey == 0, "REGRESSION: shared DXUT constructor retained stale Enter hotkey");
120
121 void* memory = ::operator new(sizeof(CDXUTEditBox));
122 SeedEnterHotkey(memory, sizeof(CDXUTEditBox));
123 auto* edit = new (memory) CDXUTEditBox(&hud);
124 Check(SUCCEEDED(hud.AddControl(edit)), "Direct edit initialization failed");
125 edit->SetVisible(false);
126 CDXUTDialog::ClearFocus();
127 Check(edit->GetHotkey() == 0, "REGRESSION: native edit retained stale Enter hotkey");
128 Check(!hud.MsgProc(nullptr, WM_CHAR, VK_RETURN, 1), "REGRESSION: native hidden edit swallowed Enter");
129 }
130
131 {
132 CDXUTDialog hud;
133 hud.Init(&manager, true);
134 for (int id = 1; id <= 2; ++id)
135 Check(SUCCEEDED(hud.AddEditBox(id, L"", 0, 0, 100, 20, id == 1, false)), "Native AddEditBox failed");
136 CheckEditInput(hud);
137 Check(SUCCEEDED(hud.AddButton(3, L"Hotkey", 0, 30, 100, 20, L'g')), "Button initialization failed");
138 hud.EnableNonUserEvents(true);
139 Check(hud.GetButton(3)->GetHotkey() == L'g', "Explicit button hotkey was lost");
140 Check(hud.MsgProc(nullptr, WM_CHAR, L'g', 1) && buttonClicks == 1, "Explicit hotkey did not invoke the button");
141 CDXUTDialog::ClearFocus();
142 puts("PASS: native AddEditBox and shared constructors; explicit button hotkey preserved.");
143 }
144
145 CDXUTDialog hud;
146 hud.Init(&manager, true);
147 for (int id = 1; id <= 2; ++id)
148 {
149 poisonNextEdit = true;
151 Check(!poisonNextEdit, "Test did not seed the application edit allocation");
152 }
153 CheckEditInput(hud);
154
155#ifndef ChatEnter
156 Check(
messages.empty(),
"ChatEnter diagnostics leaked into the default build");
157#else
158 Check(!
messages.empty(),
"ChatEnter diagnostics were not enabled");
159#endif
160 CDXUTDialog::ClearFocus();
161 puts("PASS: hidden controls let Enter through; focused input still types/submits; diagnostics match build flag.");
162 return 0;
163 }
164 catch (const std::exception& error)
165 {
166 fprintf(stderr, "FAIL: %s\n", error.what());
167 return 1;
168 }
169}
#define EVENT_EDITBOX_STRING
#define EVENT_BUTTON_CLICKED
static DialogBase * dialogs[D_ENUMERATION]
void Log(const char *msg, Level level=Info, int errorCode=0)
void AddToCallStack(const char *msg)
HRESULT AddEditBox(RiseDialog &hud, int dialogId, int controlId, bool isDefault, Logger &logger)