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