]> git.lizzy.rs Git - irrlicht.git/blob - examples/AutomatedTest/main.cpp
Add unittests for irrString
[irrlicht.git] / examples / AutomatedTest / main.cpp
1 #include <iostream>\r
2 #include <irrlicht.h>\r
3 #include "exampleHelper.h"\r
4 \r
5 using namespace irr;\r
6 \r
7 static IrrlichtDevice *device = nullptr;\r
8 static int test_fail = 0;\r
9 \r
10 void test_irr_array();\r
11 void test_irr_string();\r
12 \r
13 static video::E_DRIVER_TYPE chooseDriver(const char *arg_)\r
14 {\r
15         if (core::stringc(arg_) == "null")\r
16                 return video::EDT_NULL;\r
17 \r
18         if (IrrlichtDevice::isDriverSupported(video::EDT_OGLES1))\r
19                 return video::EDT_OGLES1;\r
20         if (IrrlichtDevice::isDriverSupported(video::EDT_OGLES2))\r
21                 return video::EDT_OGLES2;\r
22         return video::EDT_OPENGL;\r
23 }\r
24 \r
25 static inline void check(bool ok, const char *msg)\r
26 {\r
27         if (!ok)\r
28         {\r
29                 test_fail++;\r
30                 device->getLogger()->log((core::stringc("FAILED TEST: ") + msg).c_str(), ELL_ERROR);\r
31         }\r
32 }\r
33 \r
34 void run_unit_tests() {\r
35         std::cout << "Running unit tests:" << std::endl;\r
36         try {\r
37                 test_irr_array();\r
38                 test_irr_string();\r
39         } catch (const std::exception &e) {\r
40                 test_fail++;\r
41         }\r
42 }\r
43 \r
44 int main(int argc, char *argv[])\r
45 {\r
46         run_unit_tests();\r
47 \r
48         SIrrlichtCreationParameters p;\r
49         p.DriverType = chooseDriver(argc > 1 ? argv[1] : "");\r
50         p.WindowSize = core::dimension2du(640, 480);\r
51         p.Vsync = true;\r
52         p.LoggingLevel = ELL_DEBUG;\r
53 \r
54         device = createDeviceEx(p);\r
55         if (!device)\r
56                 return 1;\r
57 \r
58         {\r
59                 u32 total = 0;\r
60                 device->getOSOperator()->getSystemMemory(&total, nullptr);\r
61                 core::stringc message = core::stringc("Total RAM in MiB: ") + core::stringc(total >> 10);\r
62                 device->getLogger()->log(message.c_str(), ELL_INFORMATION);\r
63                 check(total > 130 * 1024, "RAM amount");\r
64         }\r
65 \r
66         device->setWindowCaption(L"Hello World!");\r
67         device->setResizable(true);\r
68 \r
69         video::IVideoDriver* driver = device->getVideoDriver();\r
70         scene::ISceneManager* smgr = device->getSceneManager();\r
71         gui::IGUIEnvironment* guienv = device->getGUIEnvironment();\r
72 \r
73         guienv->addStaticText(L"sample text", core::rect<s32>(10,10,110,22), false);\r
74 \r
75         gui::IGUIButton* button = guienv->addButton(\r
76                 core::rect<s32>(10,30,110,30 + 32), 0, -1, L"sample button",\r
77                 L"sample tooltip");\r
78 \r
79         gui::IGUIEditBox* editbox = guienv->addEditBox(L"",\r
80                 core::rect<s32>(10,70,60,70 + 16));\r
81 \r
82         const io::path mediaPath = getExampleMediaPath();\r
83 \r
84         scene::IAnimatedMesh* mesh = smgr->getMesh(mediaPath + "coolguy_opt.x");\r
85         check(mesh, "mesh loading");\r
86         if (mesh)\r
87         {\r
88                 video::ITexture* tex = driver->getTexture(mediaPath + "cooltexture.png");\r
89                 check(tex, "texture loading");\r
90                 scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);\r
91                 if (node)\r
92                 {\r
93                         node->setMaterialFlag(video::EMF_LIGHTING, false);\r
94                         node->setFrameLoop(0, 29);\r
95                         node->setAnimationSpeed(30);\r
96                         node->setMaterialTexture(0, tex);\r
97                 }\r
98         }\r
99 \r
100         smgr->addCameraSceneNode(0, core::vector3df(0,4,5), core::vector3df(0,2,0));\r
101 \r
102         s32 n = 0;\r
103         SEvent event;\r
104         device->getTimer()->start();\r
105 \r
106         while (device->run())\r
107         {\r
108                 if (device->getTimer()->getTime() >= 1000)\r
109                 {\r
110                         device->getTimer()->setTime(0);\r
111                         ++n;\r
112                         if (n == 1) // Tooltip display\r
113                         {\r
114                                 bzero(&event, sizeof(SEvent));\r
115                                 event.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
116                                 event.MouseInput.Event = irr::EMIE_MOUSE_MOVED;\r
117                                 event.MouseInput.X = button->getAbsolutePosition().getCenter().X;\r
118                                 event.MouseInput.Y = button->getAbsolutePosition().getCenter().Y;\r
119                                 device->postEventFromUser(event);\r
120                         }\r
121                         else if (n == 2) // Text input focus\r
122                                 guienv->setFocus(editbox);\r
123                         else if (n == 3) // Keypress for Text input\r
124                         {\r
125                                 bzero(&event, sizeof(SEvent));\r
126                                 event.EventType = irr::EET_KEY_INPUT_EVENT;\r
127                                 event.KeyInput.Char = L'a';\r
128                                 event.KeyInput.Key = KEY_KEY_A;\r
129                                 event.KeyInput.PressedDown = true;\r
130                                 device->postEventFromUser(event);\r
131                                 event.KeyInput.PressedDown = false;\r
132                                 device->postEventFromUser(event);\r
133                         }\r
134                         else\r
135                                 device->closeDevice();\r
136                 }\r
137 \r
138                 driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH,\r
139                         video::SColor(255,100,100,150));\r
140                 smgr->drawAll();\r
141                 guienv->drawAll();\r
142                 driver->endScene();\r
143         }\r
144 \r
145         check(core::stringw(L"a") == editbox->getText(), "EditBox text");\r
146 \r
147         device->getLogger()->log("Done.", ELL_INFORMATION);\r
148         device->drop();\r
149         return test_fail > 0 ? 1 : 0;\r
150 }\r