1 // Copyright 2019 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 #include "base/win/hstring_reference.h"
6
7 #include <string>
8 #include <string_view>
9
10 #include "base/win/scoped_hstring.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace base::win {
14
15 namespace {
16
17 constexpr wchar_t kTestString[] = L"123";
18 constexpr wchar_t kEmptyString[] = L"";
19
VerifyHSTRINGEquals(HSTRING hstring,const wchar_t * test_string)20 void VerifyHSTRINGEquals(HSTRING hstring, const wchar_t* test_string) {
21 const ScopedHString scoped_hstring(hstring);
22 const std::wstring_view hstring_contents = scoped_hstring.Get();
23 EXPECT_EQ(hstring_contents.compare(test_string), 0);
24 }
25
26 } // namespace
27
TEST(HStringReferenceTest,Init)28 TEST(HStringReferenceTest, Init) {
29 const HStringReference string(kTestString);
30 EXPECT_NE(string.Get(), nullptr);
31 VerifyHSTRINGEquals(string.Get(), kTestString);
32
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
38 // Passing a null string should also return a null HSTRING.
39 const HStringReference null_string(nullptr);
40 EXPECT_EQ(null_string.Get(), nullptr);
41 VerifyHSTRINGEquals(null_string.Get(), kEmptyString);
42 }
43
44 } // namespace base::win
45