scoped_hstring_unittest.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2017 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. #include "base/win/scoped_hstring.h"
  5. #include <winstring.h>
  6. #include <string>
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "base/win/core_winrt_util.h"
  9. #include "base/win/windows_version.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace base {
  12. namespace win {
  13. namespace {
  14. constexpr wchar_t kTestString1[] = L"123";
  15. constexpr wchar_t kTestString2[] = L"456789";
  16. } // namespace
  17. TEST(ScopedHStringTest, Init) {
  18. // ScopedHString requires WinRT core functions, which are not available in
  19. // older versions.
  20. if (GetVersion() < Version::WIN8) {
  21. EXPECT_FALSE(ScopedHString::ResolveCoreWinRTStringDelayload());
  22. return;
  23. }
  24. EXPECT_TRUE(ScopedHString::ResolveCoreWinRTStringDelayload());
  25. ScopedHString hstring = ScopedHString::Create(kTestString1);
  26. std::string buffer = hstring.GetAsUTF8();
  27. EXPECT_EQ(kTestString1, UTF8ToWide(buffer));
  28. WStringPiece contents = hstring.Get();
  29. EXPECT_EQ(kTestString1, contents);
  30. hstring.reset();
  31. EXPECT_TRUE(hstring == nullptr);
  32. EXPECT_EQ(nullptr, hstring.get());
  33. hstring = ScopedHString::Create(kTestString2);
  34. buffer = hstring.GetAsUTF8();
  35. EXPECT_EQ(kTestString2, UTF8ToWide(buffer));
  36. contents = hstring.Get();
  37. EXPECT_EQ(kTestString2, contents);
  38. }
  39. } // namespace win
  40. } // namespace base