Covellite++  Version: 2.3.0 Revision: ??? Platform: x64 Build: 23:13 04.01.2025
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
OpenGLCommon_test.hpp
1
2#pragma once
3#include <Platform/Windows.mock.hpp>
4
5inline static ::std::vector<float> ARGBtoFloat4(const uint32_t _HexColor)
6{
7 return
8 {
9 ((_HexColor & 0x00FF0000) >> 16) / 255.0f,
10 ((_HexColor & 0x0000FF00) >> 8) / 255.0f,
11 ((_HexColor & 0x000000FF) >> 0) / 255.0f,
12 ((_HexColor & 0xFF000000) >> 24) / 255.0f,
13 };
14};
15
16namespace
17{
18
19// ************************************************************************** //
20TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_NoEqMatrix)
21{
22 // При определении некоторых макросов glm оператор == возвращает true для
23 // разных матриц, поэтому в тестовых проектах используется отдельный
24 // заголовочный файл, проверяем это:
25 EXPECT_FALSE(::glm::mat4{ 1.0f } == ::glm::mat4{ 2.0f });
26 EXPECT_TRUE(::glm::mat4{ 1.0f } != ::glm::mat4{ 2.0f });
27}
28
29// ************************************************************************** //
30TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Blend)
31{
32 using GLProxy_t = ::mock::GLProxy;
33 GLProxy_t GLProxy;
34 GLProxy_t::GetInstance() = &GLProxy;
35
36 const Tested_t Example{ Data_t{} };
37 const ITested_t & IExample = Example;
38
39 auto itCreator = IExample.GetCreators().find(uT("State"));
40 ASSERT_NE(IExample.GetCreators().end(), itCreator);
41
42 auto Render = itCreator->second(Component_t::Make(
43 {
44 { uT("kind"), uT("Blend") }
45 }));
46 ASSERT_NE(nullptr, Render);
47
48 using namespace ::testing;
49
50 InSequence Dummy;
51
52 EXPECT_CALL(GLProxy, Enable(GL_BLEND))
53 .Times(1);
54
55 EXPECT_CALL(GLProxy, BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA))
56 .Times(1);
57
58 Render();
59}
60
61// ************************************************************************** //
62TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Sampler)
63{
64 using GLProxy_t = ::mock::GLProxy;
65 GLProxy_t GLProxy;
66 GLProxy_t::GetInstance() = &GLProxy;
67
68 const Tested_t Example{ Data_t{} };
69 const ITested_t & IExample = Example;
70
71 auto itStateCreator = IExample.GetCreators().find(uT("State"));
72 ASSERT_NE(IExample.GetCreators().end(), itStateCreator);
73
74 auto itTextureCreator = IExample.GetCreators().find(uT("Texture"));
75 ASSERT_NE(IExample.GetCreators().end(), itTextureCreator);
76
77 auto SamplerRender = itStateCreator->second(Component_t::Make(
78 {
79 { uT("kind"), uT("Sampler") }
80 }));
81 ASSERT_NE(nullptr, SamplerRender);
82
83 auto TextureRender = itTextureCreator->second(Component_t::Make({ }));
84 ASSERT_NE(nullptr, TextureRender);
85
86 using namespace ::testing;
87
88 EXPECT_CALL(GLProxy, TexParameteri(_, _, _))
89 .Times(0);
90
91 SamplerRender();
92
93 InSequence Dummy;
94
95 EXPECT_CALL(GLProxy, BindTexture(GL_TEXTURE_2D, _))
96 .Times(1);
97
98 EXPECT_CALL(GLProxy, TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
99 GL_LINEAR))
100 .Times(1);
101
102 EXPECT_CALL(GLProxy, TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
103 GL_LINEAR))
104 .Times(1);
105
106 EXPECT_CALL(GLProxy, TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT))
107 .Times(1);
108
109 EXPECT_CALL(GLProxy, TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT))
110 .Times(1);
111
112 TextureRender();
113}
114
115// ************************************************************************** //
116TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Scissor_Enable)
117{
118 using GLProxy_t = ::mock::GLProxy;
119 GLProxy_t GLProxy;
120 GLProxy_t::GetInstance() = &GLProxy;
121
122 const Tested_t Example{ Data_t{} };
123 const ITested_t & IExample = Example;
124
125 const auto pScissorData = Component_t::Make(
126 {
127 { uT("kind"), uT("Rect") },
128 });
129
130 auto itCreatorState = IExample.GetCreators().find(uT("State"));
131 ASSERT_NE(IExample.GetCreators().end(), itCreatorState);
132
133 const auto pScissor = Component_t::Make(
134 {
135 { uT("kind"), uT("Scissor") },
136 { uT("enabled"), true },
137 { uT("service"), Object_t{ pScissorData } },
138 });
139
140 auto Render = itCreatorState->second(pScissor);
141 ASSERT_NE(nullptr, Render);
142
143 auto TestCallRender =
144 [&](int _X, int _Y, int _Width, int _Height, int _WindowHeight)
145 {
146 const int Yo = _WindowHeight - (_Y + _Height);
147
148 (*pScissorData)[uT("left")] = _X;
149 (*pScissorData)[uT("top")] = _Y;
150 (*pScissorData)[uT("right")] = _X + _Width;
151 (*pScissorData)[uT("bottom")] = _Y + _Height;
152
153 using namespace ::testing;
154
155 InSequence Dummy;
156
157 EXPECT_CALL(GLProxy, Enable(GL_SCISSOR_TEST))
158 .Times(1);
159
160 const int Viewport[4] = { 0, 0, 0, _WindowHeight };
161
162 EXPECT_CALL(GLProxy, GetIntegerv(GL_VIEWPORT))
163 .Times(1)
164 .WillOnce(Return(Viewport));
165
166 EXPECT_CALL(GLProxy, Scissor(_X, Yo, _Width, _Height))
167 .Times(1);
168
169 Render();
170 };
171
172 // Два вызова, чтобы убедиться, что изменение исходных данных приводит
173 // к изменению результата рендеринга.
174 TestCallRender(1509, 1510, 1511, 1512, 1513);
175 TestCallRender(1514, 1515, 1516, 1517, 1518);
176}
177
178// ************************************************************************** //
179TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Scissor_Disable)
180{
181 using GLProxy_t = ::mock::GLProxy;
182 GLProxy_t GLProxy;
183 GLProxy_t::GetInstance() = &GLProxy;
184
185 const Tested_t Example{ Data_t{} };
186 const ITested_t & IExample = Example;
187
188 auto itCreator = IExample.GetCreators().find(uT("State"));
189 ASSERT_NE(IExample.GetCreators().end(), itCreator);
190
191 const auto pComponent = Component_t::Make(
192 {
193 { uT("kind"), uT("Scissor") },
194 { uT("enabled"), false },
195 });
196
197 auto Render = itCreator->second(pComponent);
198 ASSERT_NE(nullptr, Render);
199
200 using namespace ::testing;
201
202 EXPECT_CALL(GLProxy, Disable(GL_SCISSOR_TEST))
203 .Times(1);
204
205 Render();
206}
207
208// ************************************************************************** //
209TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Depth_Disabled)
210{
211 using GLProxy_t = ::mock::GLProxy;
212 GLProxy_t GLProxy;
213 GLProxy_t::GetInstance() = &GLProxy;
214
215 const Tested_t Example{ Data_t{} };
216 const ITested_t & IExample = Example;
217
218 auto itCreator = IExample.GetCreators().find(uT("State"));
219 ASSERT_NE(IExample.GetCreators().end(), itCreator);
220
221 using namespace ::testing;
222
223 const auto TestCallRender = [&](const Component_t::ComponentPtr_t & _pState)
224 {
225 auto Render = itCreator->second(_pState);
226 ASSERT_NE(nullptr, Render);
227
228 EXPECT_CALL(GLProxy, Disable(GL_DEPTH_TEST))
229 .Times(1);
230
231 EXPECT_CALL(GLProxy, Enable(GL_DEPTH_TEST))
232 .Times(0);
233
234 EXPECT_CALL(GLProxy, DepthFunc(_))
235 .Times(0);
236
237 EXPECT_CALL(GLProxy, ClearDepth(_))
238 .Times(0);
239
240 EXPECT_CALL(GLProxy, Clear(GL_DEPTH_BUFFER_BIT))
241 .Times(0);
242
243 Render();
244 };
245
246 TestCallRender(Component_t::Make(
247 {
248 { uT("kind"), uT("Depth") },
249 }));
250
251 TestCallRender(Component_t::Make(
252 {
253 { uT("kind"), uT("Depth") },
254 { uT("enabled"), false },
255 }));
256}
257
258// ************************************************************************** //
259TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Depth_Enable_NoClear_Overwrite)
260{
261 using GLProxy_t = ::mock::GLProxy;
262 GLProxy_t GLProxy;
263 GLProxy_t::GetInstance() = &GLProxy;
264
265 const Tested_t Example{ Data_t{} };
266 const ITested_t & IExample = Example;
267
268 auto itCreator = IExample.GetCreators().find(uT("State"));
269 ASSERT_NE(IExample.GetCreators().end(), itCreator);
270
271 using namespace ::testing;
272
273 const auto Render = itCreator->second(Component_t::Make(
274 {
275 { uT("kind"), uT("Depth") },
276 { uT("enabled"), true },
277 { uT("clear"), false },
278 }));
279 ASSERT_NE(nullptr, Render);
280
281 EXPECT_CALL(GLProxy, Disable(GL_DEPTH_TEST))
282 .Times(0);
283
284 EXPECT_CALL(GLProxy, Enable(GL_DEPTH_TEST))
285 .Times(1);
286
287 EXPECT_CALL(GLProxy, DepthMask(GL_TRUE))
288 .Times(1);
289
290 EXPECT_CALL(GLProxy, DepthFunc(GL_GREATER))
291 .Times(1);
292
293 EXPECT_CALL(GLProxy, ClearDepth(_))
294 .Times(0);
295
296 EXPECT_CALL(GLProxy, Clear(GL_DEPTH_BUFFER_BIT))
297 .Times(0);
298
299 Render();
300}
301
302// ************************************************************************** //
303TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Depth_Enable_Clear_Overwrite)
304{
305 using GLProxy_t = ::mock::GLProxy;
306 GLProxy_t GLProxy;
307 GLProxy_t::GetInstance() = &GLProxy;
308
309 const Tested_t Example{ Data_t{} };
310 const ITested_t & IExample = Example;
311
312 auto itCreator = IExample.GetCreators().find(uT("State"));
313 ASSERT_NE(IExample.GetCreators().end(), itCreator);
314
315 using namespace ::testing;
316
317 const auto Render = itCreator->second(Component_t::Make(
318 {
319 { uT("kind"), uT("Depth") },
320 { uT("enabled"), true },
321 { uT("clear"), true },
322 }));
323 ASSERT_NE(nullptr, Render);
324
325 EXPECT_CALL(GLProxy, Disable(GL_DEPTH_TEST))
326 .Times(0);
327
328 EXPECT_CALL(GLProxy, Enable(GL_DEPTH_TEST))
329 .Times(1);
330
331 EXPECT_CALL(GLProxy, DepthMask(GL_TRUE))
332 .Times(1);
333
334 EXPECT_CALL(GLProxy, DepthFunc(GL_GREATER))
335 .Times(1);
336
337 EXPECT_CALL(GLProxy, ClearDepth(0.0f))
338 .Times(1);
339
340 EXPECT_CALL(GLProxy, Clear(GL_DEPTH_BUFFER_BIT))
341 .Times(1);
342
343 Render();
344}
345
346// ************************************************************************** //
347TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Depth_Enable_NoClear_NoOverwrite)
348{
349 using GLProxy_t = ::mock::GLProxy;
350 GLProxy_t GLProxy;
351 GLProxy_t::GetInstance() = &GLProxy;
352
353 const Tested_t Example{ Data_t{} };
354 const ITested_t & IExample = Example;
355
356 auto itCreator = IExample.GetCreators().find(uT("State"));
357 ASSERT_NE(IExample.GetCreators().end(), itCreator);
358
359 using namespace ::testing;
360
361 const auto Render = itCreator->second(Component_t::Make(
362 {
363 { uT("kind"), uT("Depth") },
364 { uT("enabled"), true },
365 { uT("clear"), false },
366 { uT("overwrite"), false },
367 }));
368 ASSERT_NE(nullptr, Render);
369
370 EXPECT_CALL(GLProxy, Disable(GL_DEPTH_TEST))
371 .Times(0);
372
373 EXPECT_CALL(GLProxy, Enable(GL_DEPTH_TEST))
374 .Times(1);
375
376 EXPECT_CALL(GLProxy, DepthMask(GL_FALSE))
377 .Times(1);
378
379 EXPECT_CALL(GLProxy, DepthFunc(GL_GREATER))
380 .Times(1);
381
382 EXPECT_CALL(GLProxy, ClearDepth(_))
383 .Times(0);
384
385 EXPECT_CALL(GLProxy, Clear(GL_DEPTH_BUFFER_BIT))
386 .Times(0);
387
388 Render();
389}
390
391// ************************************************************************** //
392TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Depth_Enable_Clear_NoOverwrite)
393{
394 using GLProxy_t = ::mock::GLProxy;
395 GLProxy_t GLProxy;
396 GLProxy_t::GetInstance() = &GLProxy;
397
398 const Tested_t Example{ Data_t{} };
399 const ITested_t & IExample = Example;
400
401 auto itCreator = IExample.GetCreators().find(uT("State"));
402 ASSERT_NE(IExample.GetCreators().end(), itCreator);
403
404 using namespace ::testing;
405
406 const auto Render = itCreator->second(Component_t::Make(
407 {
408 { uT("kind"), uT("Depth") },
409 { uT("enabled"), true },
410 { uT("clear"), true },
411 { uT("overwrite"), false },
412 }));
413 ASSERT_NE(nullptr, Render);
414
415 EXPECT_CALL(GLProxy, Disable(GL_DEPTH_TEST))
416 .Times(0);
417
418 EXPECT_CALL(GLProxy, Enable(GL_DEPTH_TEST))
419 .Times(1);
420
421 EXPECT_CALL(GLProxy, DepthMask(GL_FALSE))
422 .Times(1);
423
424 EXPECT_CALL(GLProxy, DepthFunc(GL_GREATER))
425 .Times(1);
426
427 EXPECT_CALL(GLProxy, ClearDepth(0.0f))
428 .Times(1);
429
430 EXPECT_CALL(GLProxy, Clear(GL_DEPTH_BUFFER_BIT))
431 .Times(1);
432
433 Render();
434}
435
436// ************************************************************************** //
437TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_State_Clear)
438{
439 using Color_t = ::std::vector<float>;
440
441 using GLProxy_t = ::mock::GLProxy;
442 GLProxy_t GLProxy;
443 GLProxy_t::GetInstance() = &GLProxy;
444
445 Tested_t Example{ Data_t{ } };
446 ITested_t & IExample = Example;
447
448 auto itCreator = IExample.GetCreators().find(uT("State"));
449 ASSERT_NE(IExample.GetCreators().end(), itCreator);
450
451 const auto TestCallRender = [&](
452 const Component_t::ComponentPtr_t & _pState,
453 const Color_t & _ExpectedColor)
454 {
455 const auto Render = itCreator->second(_pState);
456 ASSERT_NE(nullptr, Render);
457
458 using namespace ::testing;
459
460 InSequence Dummy;
461
462 ASSERT_EQ(4, _ExpectedColor.size());
463
464 EXPECT_CALL(GLProxy, ClearColor(_ExpectedColor[0], _ExpectedColor[1],
465 _ExpectedColor[2], _ExpectedColor[3]))
466 .Times(1);
467
468 EXPECT_CALL(GLProxy, Clear(GL_COLOR_BUFFER_BIT))
469 .Times(1);
470
471 Render();
472 };
473
474 {
475 const ::std::vector<FLOAT> DefaultColor =
476 {
477 0.0f,
478 0.0f,
479 0.0f,
480 1.0f,
481 };
482
483 TestCallRender(Component_t::Make(
484 {
485 { uT("kind"), uT("Clear") },
486 }), DefaultColor);
487 }
488
489 {
490 const ::std::vector<FLOAT> Color =
491 {
492 0.86274509803921568627450980392157f, // DC
493 0.72941176470588235294117647058824f, // BA
494 0.5960784313725490196078431372549f, // 98
495 0.9960784313725490196078431372549f, // FE
496 };
497
498 TestCallRender(Component_t::Make(
499 {
500 { uT("kind"), uT("Clear") },
501 { uT("color"), 0xFEDCBA98 },
502 }), Color);
503 }
504}
505
506// ************************************************************************** //
507TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_Texture_glTexImage2D_Fail)
508{
509 using GLProxy_t = ::mock::GLProxy;
510 GLProxy_t GLProxy;
511 GLProxy_t::GetInstance() = &GLProxy;
512
513 const ::mock::GLuint TextureId = 1612182301;
514
515 const Tested_t Example{ Data_t{} };
516 const ITested_t & IExample = Example;
517
518 auto itCreator = IExample.GetCreators().find(uT("Texture"));
519 ASSERT_NE(IExample.GetCreators().end(), itCreator);
520
521 using namespace ::testing;
522
523 InSequence Dummy;
524
525 EXPECT_CALL(GLProxy, GenTextures(1))
526 .Times(1)
527 .WillOnce(Return(TextureId));
528
529 EXPECT_CALL(GLProxy, BindTexture(GL_TEXTURE_2D, TextureId))
530 .Times(1);
531
532 EXPECT_CALL(GLProxy, TexImage2D(_, _, _, _, _, _, _, _, _))
533 .Times(1);
534
535 EXPECT_CALL(GLProxy, BindTexture(GL_TEXTURE_2D, 0))
536 .Times(1);
537
538 EXPECT_CALL(GLProxy, GetError())
539 .Times(1)
540 .WillOnce(Return(1808261916));
541
542 EXPECT_THROW(itCreator->second(Component_t::Make({})), ::std::exception);
543}
544
545// ************************************************************************** //
546TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_Texture_UnknownDestination)
547{
548 using GLProxy_t = ::mock::GLProxy;
549 GLProxy_t GLProxy;
550 GLProxy_t::GetInstance() = &GLProxy;
551
552 const Tested_t Example{ Data_t{} };
553 const ITested_t & IExample = Example;
554
555 auto itCreator = IExample.GetCreators().find(uT("Texture"));
556 ASSERT_NE(IExample.GetCreators().end(), itCreator);
557
558 const auto TestCall = [&](const Component_t::ComponentPtr_t & _pTexture)
559 {
560 const ::mock::GLuint TextureId = 1812181809;
561
562 using namespace ::testing;
563
564 InSequence Dummy;
565
566 EXPECT_CALL(GLProxy, GenTextures(_))
567 .Times(0);
568
569 EXPECT_CALL(GLProxy, BindTexture(_, _))
570 .Times(0);
571
572 EXPECT_CALL(GLProxy, TexImage2D(_, _, _, _, _, _, _, _, _))
573 .Times(0);
574
575 EXPECT_CALL(GLProxy, GetError())
576 .Times(0);
577
578 EXPECT_THROW(itCreator->second(_pTexture), ::std::exception);
579 };
580
581 {
582 const auto pTexture = Component_t::Make(
583 {
584 { uT("destination"), uT("1907251102") },
585 });
586
587 TestCall(pTexture);
588 }
589
590 {
591 const auto pData = Component_t::Make(
592 {
593 { uT("kind"), uT("Texture") },
594 { uT("destination"), uT("1907251103") },
595 });
596
597 TestCall(Component_t::Make({ { uT("service"), Object_t{ pData } } }));
598 }
599}
600
601// ************************************************************************** //
602TEST_F(OpenGLCommon_test, /*DISABLED_*/Test_Buffer_UnknownType)
603{
604 const ::std::vector<float> Source = { 0.0f };
605
606 const Tested_t Example{ Data_t{} };
607 const ITested_t & IExample = Example;
608
609 auto itCreator = IExample.GetCreators().find(uT("Buffer"));
610 ASSERT_NE(IExample.GetCreators().end(), itCreator);
611
612 // ***************** Передача данных в объекте компонента ***************** //
613
614 {
615 const auto pBuffer = Component_t::Make(
616 {
617 { uT("id"), uT("id.1905081956") },
618 { uT("type"), uT("type.1905081956") },
619 { uT("kind"), uT("kind.1905081956") },
620 { uT("content"), Source },
621 });
622
623 EXPECT_STDEXCEPTION(itCreator->second(pBuffer),
624 ".+\\.cpp\\([0-9]+\\): Unexpected buffer format \\["
625 "id: id.1905081956, "
626 "type: type.1905081956, "
627 "kind: kind.1905081956\\]\\.");
628 }
629
630 // ************** Передача данных в объекте компонента Data *************** //
631
632 {
633 const auto pData = Component_t::Make(
634 {
635 { uT("kind"), uT("Buffer") },
636 { uT("content"), Source },
637 });
638
639 const auto pBuffer = Component_t::Make(
640 {
641 { uT("id"), uT("id.1905082000") },
642 { uT("type"), uT("type.1905082000") },
643 { uT("kind"), uT("kind.1905082000") },
644 { uT("service"), Object_t{ pData } }
645 });
646
647 EXPECT_STDEXCEPTION(itCreator->second(pBuffer),
648 ".+\\.cpp\\([0-9]+\\): Unexpected buffer format \\["
649 "id: id.1905082000, "
650 "type: type.1905082000, "
651 "kind: kind.1905082000\\]\\.");
652 }
653}
654
655} // unnamed namespace