scoped_os_info_override_win.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/macros.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. kWin10Pro,
  24. kWin10Home,
  25. kWinServer2016,
  26. kWin81Pro,
  27. kWinServer2012R2,
  28. kWin7ProSP1,
  29. };
  30. explicit ScopedOSInfoOverride(Type type);
  31. ~ScopedOSInfoOverride();
  32. private:
  33. using UniqueOsInfo =
  34. std::unique_ptr<base::win::OSInfo, void (*)(base::win::OSInfo*)>;
  35. static UniqueOsInfo CreateInfoOfType(Type type);
  36. // The OSInfo taken by this instance at construction and restored at
  37. // destruction.
  38. base::win::OSInfo* original_info_;
  39. // The OSInfo owned by this scoped object and which overrides
  40. // base::win::OSInfo::GetIntance() for the lifespan of the object.
  41. UniqueOsInfo overriding_info_;
  42. // Because the dtor of OSInfo is private, a custom deleter is needed to use
  43. // unique_ptr.
  44. static void deleter(base::win::OSInfo* info);
  45. DISALLOW_COPY_AND_ASSIGN(ScopedOSInfoOverride);
  46. };
  47. } // namespace test
  48. } // namespace base
  49. #endif // BASE_TEST_SCOPED_OS_INFO_OVERRIDE_WIN_H_