hstring_compare_unittest.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_compare.h"
  5. #include "base/win/hstring_reference.h"
  6. #include "base/win/windows_version.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace base {
  9. namespace win {
  10. namespace {
  11. constexpr wchar_t kTestString12[] = L"12";
  12. constexpr wchar_t kTestString123[] = L"123";
  13. constexpr wchar_t kTestString1234[] = L"1234";
  14. } // namespace
  15. TEST(HStringCompareTest, WorksOnWindows8AndAbove) {
  16. INT32 result;
  17. HRESULT hr = HStringCompare(nullptr, nullptr, &result);
  18. // HStringCompare requires WinRT core functions, which are not available in
  19. // older versions.
  20. if (GetVersion() < Version::WIN8)
  21. EXPECT_HRESULT_FAILED(hr);
  22. else
  23. EXPECT_HRESULT_SUCCEEDED(hr);
  24. }
  25. TEST(HStringCompareTest, FirstStringBeforeSecondString) {
  26. if (GetVersion() < Version::WIN8)
  27. return;
  28. ASSERT_TRUE(HStringReference::ResolveCoreWinRTStringDelayload());
  29. const HStringReference string12(kTestString12);
  30. const HStringReference string123(kTestString123);
  31. INT32 result;
  32. HRESULT hr = HStringCompare(string12.Get(), string123.Get(), &result);
  33. EXPECT_HRESULT_SUCCEEDED(hr);
  34. EXPECT_EQ(-1, result);
  35. }
  36. TEST(HStringCompareTest, StringsEqual) {
  37. if (GetVersion() < Version::WIN8)
  38. return;
  39. ASSERT_TRUE(HStringReference::ResolveCoreWinRTStringDelayload());
  40. const HStringReference string123(kTestString123);
  41. INT32 result;
  42. HRESULT hr = HStringCompare(string123.Get(), string123.Get(), &result);
  43. EXPECT_HRESULT_SUCCEEDED(hr);
  44. EXPECT_EQ(0, result);
  45. }
  46. TEST(HStringCompareTest, FirstStringAfterSecondString) {
  47. if (GetVersion() < Version::WIN8)
  48. return;
  49. ASSERT_TRUE(HStringReference::ResolveCoreWinRTStringDelayload());
  50. const HStringReference string123(kTestString123);
  51. const HStringReference string1234(kTestString1234);
  52. INT32 result;
  53. HRESULT hr = HStringCompare(string1234.Get(), string123.Get(), &result);
  54. EXPECT_HRESULT_SUCCEEDED(hr);
  55. EXPECT_EQ(1, result);
  56. }
  57. } // namespace win
  58. } // namespace base