scoped_os_info_override_win.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_SCOPED_OS_INFO_OVERRIDE_WIN_H_
  5. #define BASE_TEST_SCOPED_OS_INFO_OVERRIDE_WIN_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. namespace base {
  9. namespace win {
  10. class OSInfo;
  11. } // namespace win
  12. } // namespace base
  13. namespace base {
  14. namespace test {
  15. // Helper class to override info returned by base::win::OSInfo::GetIntance()
  16. // for the lifetime of this object. Upon destruction, the original info at time
  17. // of object creation is restored.
  18. class ScopedOSInfoOverride {
  19. public:
  20. // Types of windows machines that can be used for overriding. Add new
  21. // machine types as needed.
  22. enum class Type {
  23. kWin11Pro,
  24. kWin11Home,
  25. kWinServer2022,
  26. kWin10Pro21H1,
  27. kWin10Pro,
  28. kWin10Home,
  29. kWinServer2016,
  30. kWin81Pro,
  31. kWinServer2012R2,
  32. kWin7ProSP1,
  33. };
  34. explicit ScopedOSInfoOverride(Type type);
  35. ScopedOSInfoOverride(const ScopedOSInfoOverride&) = delete;
  36. ScopedOSInfoOverride& operator=(const ScopedOSInfoOverride&) = delete;
  37. ~ScopedOSInfoOverride();
  38. private:
  39. using UniqueOsInfo =
  40. std::unique_ptr<base::win::OSInfo, void (*)(base::win::OSInfo*)>;
  41. static UniqueOsInfo CreateInfoOfType(Type type);
  42. // The OSInfo taken by this instance at construction and restored at
  43. // destruction.
  44. raw_ptr<base::win::OSInfo> original_info_;
  45. // The OSInfo owned by this scoped object and which overrides
  46. // base::win::OSInfo::GetIntance() for the lifespan of the object.
  47. UniqueOsInfo overriding_info_;
  48. // Because the dtor of OSInfo is private, a custom deleter is needed to use
  49. // unique_ptr.
  50. static void deleter(base::win::OSInfo* info);
  51. };
  52. } // namespace test
  53. } // namespace base
  54. #endif // BASE_TEST_SCOPED_OS_INFO_OVERRIDE_WIN_H_