UFO: Alien Invasion
test_ui_level2.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2022 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "test_shared.h"
27 #include "../client/cl_video.h" /* vid_imagePool */
28 #include "../client/cl_lua.h"
29 #include "../client/ui/ui_internal.h"
30 #include "../client/renderer/r_state.h" /* r_state */
31 
32 class UILevel2Test: public ::testing::Test {
33 protected:
34  static void SetUpTestCase() {
35  TEST_Init();
36  CL_InitLua();
37  UI_Init();
38 
39  vid_imagePool = Mem_CreatePool("Vid: Image system");
40 
42  }
43 
44  static void TearDownTestCase() {
45  UI_Shutdown();
46  TEST_Shutdown();
47  }
48 };
49 
51 static void TEST_ParseScript (const char* scriptName)
52 {
53  const char* type, *name, *text;
54  int nbWrongScripts = 0;
55 
56  /* parse ui node script */
57  Com_Printf("Load \"%s\": %i script file(s)\n", scriptName, FS_BuildFileList(scriptName));
58  FS_NextScriptHeader(nullptr, nullptr, nullptr);
59  text = nullptr;
60  while ((type = FS_NextScriptHeader(scriptName, &name, &text)) != nullptr) {
61  try {
62  CL_ParseClientData(type, name, &text);
63  } catch (const comDrop_t& e) {
64  ASSERT_TRUE(false) << "Failed to parse needed scripts for type: " << type << " and id " << name;
65  nbWrongScripts++;
66  }
67  }
68  ASSERT_TRUE(nbWrongScripts == 0);
69 }
70 
76 static void UFO_AnalyseTestWindow (const char* windowName)
77 {
78  const uiBehaviour_t* stringBehaviour = UI_GetNodeBehaviour("string");
79  const uiNode_t* node;
80  const uiNode_t* window = UI_GetWindow(windowName);
81  ASSERT_TRUE(window != nullptr);
82  ASSERT_TRUE(stringBehaviour != nullptr);
83 
84  for (node = window->firstChild; node != nullptr; node = node->next) {
85  bool isGreen;
86  bool isRed;
87  /* skip non "string" nodes */
88  if (node->behaviour != stringBehaviour)
89  continue;
90  if (node->invis)
91  continue;
92  /* skip nodes without "test" prefix */
93  if (!Q_strstart(node->name, "test"))
94  continue;
95 
96  isGreen = node->color[0] < 0.1 && node->color[1] > 0.9 && node->color[2] < 0.1;
97  isRed = node->color[0] > 0.9 && node->color[1] < 0.1 && node->color[2] < 0.1;
98 
99  if (isGreen) {
101  ASSERT_TRUE(true);
102  } else if (isRed) {
103  const char* message = va("%s.%s failed.", windowName, node->name);
104  ASSERT_TRUE(false) << message << " base/ufos/uitest/" << windowName << ".ufo";
105  } else {
106  const char* message = va("%s.%s have an unknown status.", windowName, node->name);
107  ASSERT_TRUE(false) << message << " base/ufos/uitest/" << windowName << ".ufo";
108  }
109  }
110 }
111 
115 static void UFO_ExecuteTestWindow (const char* windowName)
116 {
117  /* look and feed */
118  Com_Printf("\n");
119 
120  TEST_ParseScript(va("ufos/uitest/%s.ufo", windowName));
121 
122  Cmd_ExecuteString("ui_push %s", windowName);
123 
124  /* while the execution buffer is not empty */
125  for (int i = 0; i < 20; i++) {
126  Cbuf_Execute();
127  }
128 
129  UFO_AnalyseTestWindow(windowName);
130 
131  /* look and feed */
132  Com_Printf(" ---> ");
133 }
134 
138 TEST_F(UILevel2Test, KnownBehaviours)
139 {
140  uiBehaviour_t* behaviour;
141 
142  behaviour = UI_GetNodeBehaviour("button");
143  ASSERT_TRUE(behaviour != nullptr);
144  ASSERT_STREQ(behaviour->name, "button");
145 
146  /* first one */
147  behaviour = UI_GetNodeBehaviour("");
148  ASSERT_TRUE(behaviour != nullptr);
149 
150  /* last one */
151  behaviour = UI_GetNodeBehaviour("zone");
152  ASSERT_TRUE(behaviour != nullptr);
153  ASSERT_STREQ(behaviour->name, "zone");
154 
155  /* unknown */
156  behaviour = UI_GetNodeBehaviour("dahu");
157  ASSERT_TRUE(behaviour == nullptr);
158 }
159 
164 {
165  UFO_ExecuteTestWindow("test_action");
166 }
167 
172 {
173  UFO_ExecuteTestWindow("test_action2");
174 }
175 
179 TEST_F(UILevel2Test, Conditions)
180 {
181  UFO_ExecuteTestWindow("test_condition");
182 }
183 
187 TEST_F(UILevel2Test, Functions)
188 {
189  UFO_ExecuteTestWindow("test_function");
190 }
191 
196 {
197  UFO_ExecuteTestWindow("test_setter");
198 }
199 
204 {
205  UFO_ExecuteTestWindow("test_cvar");
206 }
207 
211 TEST_F(UILevel2Test, Components)
212 {
213  UFO_ExecuteTestWindow("test_component");
214 }
215 
220 TEST_F(UILevel2Test, InheritedConfunc)
221 {
222  TEST_ParseScript("ufos/uitest/_test_inheritedconfunc.ufo");
223  TEST_ParseScript("ufos/uitest/_test_inheritedconfunc_lua.ufo");
224  UFO_ExecuteTestWindow("test_inheritedconfunc");
225  UFO_ExecuteTestWindow("test_inheritedconfunc_lua");
226 }
227 
233 TEST_F(UILevel2Test, RuntimeError)
234 {
235  UFO_ExecuteTestWindow("test_runtimeerror");
236 }
237 
243 {
244  UFO_ExecuteTestWindow("test_video");
245 }
246 
252 {
253  UFO_ExecuteTestWindow("test_cvarlistener");
254 }
255 
261 {
262  //UFO_ExecuteTestWindow("test_keybinding");
263 }
264 
269 {
270  TEST_ParseScript("ufos/uisample/*.ufo");
271 }
bool CL_ParseClientData(const char *type, const char *name, const char **text)
Called at client startup.
Definition: cl_main.cpp:770
TEST_F(UILevel2Test, KnownBehaviours)
test behaviour name
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
uiNode_t * next
Definition: ui_nodes.h:91
void UI_Init(void)
Definition: ui_main.cpp:278
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don&#39;t need to have varargs versions of all text functi...
Definition: shared.cpp:410
const char * name
Definition: ui_behaviour.h:41
void UI_Shutdown(void)
Reset and free the UI data hunk.
Definition: ui_main.cpp:205
char name[MAX_VAR]
Definition: ui_nodes.h:82
uiBehaviour_t * behaviour
Definition: ui_nodes.h:83
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
int FS_BuildFileList(const char *fileList)
Build a filelist.
Definition: files.cpp:962
void Cmd_ExecuteString(const char *text,...)
A complete command line has been parsed, so try to execute it.
Definition: cmd.cpp:1007
bool invis
Definition: ui_nodes.h:101
static void SetUpTestCase()
static void TearDownTestCase()
void TEST_Shutdown(void)
Definition: test_shared.cpp:34
void CL_InitLua(void)
Initializes the ui-lua interfacing environment.
Definition: cl_lua.cpp:126
gltexunit_t * active_texunit
Definition: r_state.h:117
#define Mem_CreatePool(name)
Definition: mem.h:32
char const * Q_strstart(char const *str, char const *start)
Matches the start of a string.
Definition: shared.cpp:587
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
uiNode_t * UI_GetWindow(const char *name)
Searches all windows for the specified one.
Definition: ui_windows.cpp:567
#define texunit_diffuse
Definition: r_state.h:68
QGL_EXTERN GLint i
Definition: r_gl.h:113
node behaviour, how a node work
Definition: ui_behaviour.h:39
void TEST_Init(void)
Definition: test_shared.cpp:72
char * FS_NextScriptHeader(const char *files, const char **name, const char **text)
Definition: files.cpp:1196
uiBehaviour_t * UI_GetNodeBehaviour(const char *name)
Return a node behaviour by name.
Definition: ui_nodes.cpp:559
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
vec4_t color
Definition: ui_nodes.h:127
memPool_t * vid_imagePool
Definition: cl_main.cpp:88
rstate_t r_state
Definition: r_main.cpp:48
uiNode_t * firstChild
Definition: ui_nodes.h:89
void Cbuf_Execute(void)
Pulls off terminated lines of text from the command buffer and sends them through Cmd_ExecuteString...
Definition: cmd.cpp:214
static void UFO_AnalyseTestWindow(const char *windowName)
after execution of a unittest window, it analyse color of normalized indicator, and create asserts...
Listener for cvar changes.
Definition: cvar.h:98
static void TEST_ParseScript(const char *scriptName)
static void UFO_ExecuteTestWindow(const char *windowName)
Parse and execute a test windows.