/ src / common / UnitTests-CommonUtils / GameMode.Tests.cpp
GameMode.Tests.cpp
 1  #include "pch.h"
 2  #include "TestHelpers.h"
 3  #include <game_mode.h>
 4  
 5  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
 6  
 7  namespace UnitTestsCommonUtils
 8  {
 9      TEST_CLASS(GameModeTests)
10      {
11      public:
12          TEST_METHOD(DetectGameMode_ReturnsBoolean)
13          {
14              // This function queries Windows game mode status
15              bool result = detect_game_mode();
16  
17              // Result depends on current system state, but should be a valid boolean
18              Assert::IsTrue(result == true || result == false);
19          }
20  
21          TEST_METHOD(DetectGameMode_ConsistentResults)
22          {
23              // Multiple calls should return consistent results (unless game mode changes)
24              bool result1 = detect_game_mode();
25              bool result2 = detect_game_mode();
26              bool result3 = detect_game_mode();
27  
28              // Results should be consistent across rapid calls
29              Assert::AreEqual(result1, result2);
30              Assert::AreEqual(result2, result3);
31          }
32  
33          TEST_METHOD(DetectGameMode_DoesNotCrash)
34          {
35              // Call multiple times to ensure no crash or memory leak
36              for (int i = 0; i < 100; ++i)
37              {
38                  detect_game_mode();
39              }
40              Assert::IsTrue(true);
41          }
42  
43          TEST_METHOD(DetectGameMode_ThreadSafe)
44          {
45              // Test that calling from multiple threads is safe
46              std::vector<std::thread> threads;
47              std::atomic<int> successCount{ 0 };
48  
49              for (int i = 0; i < 10; ++i)
50              {
51                  threads.emplace_back([&successCount]() {
52                      for (int j = 0; j < 10; ++j)
53                      {
54                          detect_game_mode();
55                          successCount++;
56                      }
57                  });
58              }
59  
60              for (auto& t : threads)
61              {
62                  t.join();
63              }
64  
65              Assert::AreEqual(100, successCount.load());
66          }
67      };
68  }