hstring_reference_unittest.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 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/hstring_reference.h"
  5. #include <string>
  6. #include "base/strings/string_piece.h"
  7. #include "base/win/scoped_hstring.h"
  8. #include "base/win/windows_version.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace base {
  11. namespace win {
  12. namespace {
  13. constexpr wchar_t kTestString[] = L"123";
  14. constexpr wchar_t kEmptyString[] = L"";
  15. void VerifyHSTRINGEquals(HSTRING hstring, const wchar_t* test_string) {
  16. const ScopedHString scoped_hstring(hstring);
  17. const WStringPiece hstring_contents = scoped_hstring.Get();
  18. EXPECT_EQ(hstring_contents.compare(test_string), 0);
  19. }
  20. } // namespace
  21. TEST(HStringReferenceTest, Init) {
  22. // ScopedHString requires WinRT core functions, which are not available in
  23. // older versions.
  24. if (GetVersion() < Version::WIN8) {
  25. EXPECT_FALSE(HStringReference::ResolveCoreWinRTStringDelayload());
  26. return;
  27. }
  28. EXPECT_TRUE(HStringReference::ResolveCoreWinRTStringDelayload());
  29. EXPECT_TRUE(ScopedHString::ResolveCoreWinRTStringDelayload());
  30. const HStringReference string(kTestString);
  31. EXPECT_NE(string.Get(), nullptr);
  32. VerifyHSTRINGEquals(string.Get(), kTestString);
  33. // Empty strings come back as null HSTRINGs, a valid HSTRING.
  34. const HStringReference empty_string(kEmptyString);
  35. EXPECT_EQ(empty_string.Get(), nullptr);
  36. VerifyHSTRINGEquals(empty_string.Get(), kEmptyString);
  37. // Passing a zero length and null string should also return a null HSTRING.
  38. const HStringReference null_string(nullptr, 0);
  39. EXPECT_EQ(null_string.Get(), nullptr);
  40. VerifyHSTRINGEquals(null_string.Get(), kEmptyString);
  41. }
  42. } // namespace win
  43. } // namespace base