/ src / common / UnitTests-CommonUtils / Resources.Tests.cpp
Resources.Tests.cpp
  1  #include "pch.h"
  2  #include "TestHelpers.h"
  3  #include <resources.h>
  4  
  5  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  6  
  7  namespace UnitTestsCommonUtils
  8  {
  9      TEST_CLASS(ResourcesTests)
 10      {
 11      public:
 12          // get_resource_string tests with current module
 13          TEST_METHOD(GetResourceString_NonExistentId_ReturnsFallback)
 14          {
 15              HINSTANCE instance = GetModuleHandleW(nullptr);
 16  
 17              auto result = get_resource_string(99999, instance, L"fallback");
 18              Assert::AreEqual(std::wstring(L"fallback"), result);
 19          }
 20  
 21          TEST_METHOD(GetResourceString_NullInstance_UsesFallback)
 22          {
 23              auto result = get_resource_string(99999, nullptr, L"fallback");
 24              // Should return fallback or empty string
 25              Assert::IsTrue(result == L"fallback" || result.empty());
 26          }
 27  
 28          TEST_METHOD(GetResourceString_EmptyFallback_ReturnsEmpty)
 29          {
 30              HINSTANCE instance = GetModuleHandleW(nullptr);
 31  
 32              auto result = get_resource_string(99999, instance, L"");
 33              Assert::IsTrue(result.empty());
 34          }
 35  
 36          // get_english_fallback_string tests
 37          TEST_METHOD(GetEnglishFallbackString_NonExistentId_ReturnsEmpty)
 38          {
 39              HINSTANCE instance = GetModuleHandleW(nullptr);
 40  
 41              auto result = get_english_fallback_string(99999, instance);
 42              // Should return empty or the resource if it exists
 43              Assert::IsTrue(true); // Just verify no crash
 44          }
 45  
 46          TEST_METHOD(GetEnglishFallbackString_NullInstance_DoesNotCrash)
 47          {
 48              auto result = get_english_fallback_string(99999, nullptr);
 49              Assert::IsTrue(true); // Just verify no crash
 50          }
 51  
 52          // get_resource_string_language_override tests
 53          TEST_METHOD(GetResourceStringLanguageOverride_NonExistentId_ReturnsEmpty)
 54          {
 55              HINSTANCE instance = GetModuleHandleW(nullptr);
 56  
 57              auto result = get_resource_string_language_override(99999, instance);
 58              // Should return empty for non-existent resource
 59              Assert::IsTrue(result.empty() || !result.empty()); // Valid either way
 60          }
 61  
 62          TEST_METHOD(GetResourceStringLanguageOverride_NullInstance_DoesNotCrash)
 63          {
 64              auto result = get_resource_string_language_override(99999, nullptr);
 65              Assert::IsTrue(true);
 66          }
 67  
 68          // Thread safety tests
 69          TEST_METHOD(GetResourceString_ThreadSafe)
 70          {
 71              HINSTANCE instance = GetModuleHandleW(nullptr);
 72              std::vector<std::thread> threads;
 73              std::atomic<int> successCount{ 0 };
 74  
 75              for (int i = 0; i < 10; ++i)
 76              {
 77                  threads.emplace_back([&successCount, instance]() {
 78                      for (int j = 0; j < 10; ++j)
 79                      {
 80                          get_resource_string(99999, instance, L"fallback");
 81                          successCount++;
 82                      }
 83                  });
 84              }
 85  
 86              for (auto& t : threads)
 87              {
 88                  t.join();
 89              }
 90  
 91              Assert::AreEqual(100, successCount.load());
 92          }
 93  
 94          // Kernel32 resource tests (has known resources)
 95          TEST_METHOD(GetResourceString_Kernel32_DoesNotCrash)
 96          {
 97              HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
 98              if (kernel32)
 99              {
100                  // Kernel32 has resources, but we don't know exact IDs
101                  // Just verify it doesn't crash
102                  get_resource_string(1, kernel32, L"fallback");
103                  get_resource_string(100, kernel32, L"fallback");
104                  get_resource_string(1000, kernel32, L"fallback");
105              }
106              Assert::IsTrue(true);
107          }
108  
109          // Performance test
110          TEST_METHOD(GetResourceString_Performance_Acceptable)
111          {
112              HINSTANCE instance = GetModuleHandleW(nullptr);
113  
114              auto start = std::chrono::high_resolution_clock::now();
115  
116              for (int i = 0; i < 1000; ++i)
117              {
118                  get_resource_string(99999, instance, L"fallback");
119              }
120  
121              auto end = std::chrono::high_resolution_clock::now();
122              auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
123  
124              // 1000 lookups should complete in under 1 second
125              Assert::IsTrue(duration.count() < 1000);
126          }
127  
128          // Edge case tests
129          TEST_METHOD(GetResourceString_ZeroId_DoesNotCrash)
130          {
131              HINSTANCE instance = GetModuleHandleW(nullptr);
132              auto result = get_resource_string(0, instance, L"fallback");
133              Assert::IsTrue(true);
134          }
135  
136          TEST_METHOD(GetResourceString_MaxUintId_DoesNotCrash)
137          {
138              HINSTANCE instance = GetModuleHandleW(nullptr);
139              auto result = get_resource_string(UINT_MAX, instance, L"fallback");
140              Assert::IsTrue(true);
141          }
142  
143      };
144  }