OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "ui/gfx/win/text_analysis_source.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/gfx/win/direct_write.h" |
| 9 |
| 10 namespace mswr = Microsoft::WRL; |
| 11 |
| 12 namespace gfx { |
| 13 namespace win { |
| 14 |
| 15 namespace { |
| 16 |
| 17 DWRITE_READING_DIRECTION kReadingDirection = |
| 18 DWRITE_READING_DIRECTION_TOP_TO_BOTTOM; |
| 19 const wchar_t* kLocale = L"hi-in"; |
| 20 const wchar_t kText[] = L"sample text"; |
| 21 const size_t kTextLength = _countof(kText) - 1; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 class TextAnalysisSourceTest : public testing::Test { |
| 26 public: |
| 27 TextAnalysisSourceTest() { |
| 28 mswr::ComPtr<IDWriteFactory> factory; |
| 29 CreateDWriteFactory(&factory); |
| 30 |
| 31 mswr::ComPtr<IDWriteNumberSubstitution> number_substitution; |
| 32 factory->CreateNumberSubstitution(DWRITE_NUMBER_SUBSTITUTION_METHOD_NONE, |
| 33 kLocale, true /* ignoreUserOverride */, |
| 34 &number_substitution); |
| 35 |
| 36 mswr::MakeAndInitialize<TextAnalysisSource>( |
| 37 &source_, kText, kLocale, number_substitution.Get(), kReadingDirection); |
| 38 } |
| 39 |
| 40 protected: |
| 41 mswr::ComPtr<TextAnalysisSource> source_; |
| 42 |
| 43 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(TextAnalysisSourceTest); |
| 45 }; |
| 46 |
| 47 TEST_F(TextAnalysisSourceTest, TestGetLocaleName) { |
| 48 UINT32 length = 0; |
| 49 const wchar_t* locale_name = nullptr; |
| 50 HRESULT hr = E_FAIL; |
| 51 hr = source_->GetLocaleName(0, &length, &locale_name); |
| 52 |
| 53 EXPECT_TRUE(SUCCEEDED(hr)); |
| 54 EXPECT_EQ(kTextLength, length); |
| 55 EXPECT_STREQ(kLocale, locale_name); |
| 56 |
| 57 // Attempting to get a locale for a location that does not have a text chunk |
| 58 // should fail. |
| 59 hr = source_->GetLocaleName(kTextLength, &length, &locale_name); |
| 60 |
| 61 EXPECT_TRUE(FAILED(hr)); |
| 62 } |
| 63 |
| 64 TEST_F(TextAnalysisSourceTest, TestGetNumberSubstitution) { |
| 65 UINT32 length = 0; |
| 66 mswr::ComPtr<IDWriteNumberSubstitution> number_substitution; |
| 67 HRESULT hr = E_FAIL; |
| 68 hr = source_->GetNumberSubstitution(0, &length, &number_substitution); |
| 69 |
| 70 EXPECT_TRUE(SUCCEEDED(hr)); |
| 71 EXPECT_EQ(kTextLength, length); |
| 72 EXPECT_NE(nullptr, number_substitution.Get()); |
| 73 |
| 74 // Attempting to get a number substitution for a location that does not have |
| 75 // a text chunk should fail. |
| 76 hr = source_->GetNumberSubstitution(kTextLength, &length, |
| 77 &number_substitution); |
| 78 |
| 79 EXPECT_TRUE(FAILED(hr)); |
| 80 } |
| 81 |
| 82 TEST_F(TextAnalysisSourceTest, TestGetParagraphReadingDirection) { |
| 83 EXPECT_EQ(kReadingDirection, source_->GetParagraphReadingDirection()); |
| 84 } |
| 85 |
| 86 TEST_F(TextAnalysisSourceTest, TestGetTextAtPosition) { |
| 87 UINT32 length = 0; |
| 88 const wchar_t* text = nullptr; |
| 89 HRESULT hr = E_FAIL; |
| 90 hr = source_->GetTextAtPosition(0, &text, &length); |
| 91 |
| 92 EXPECT_TRUE(SUCCEEDED(hr)); |
| 93 EXPECT_EQ(kTextLength, length); |
| 94 EXPECT_STREQ(kText, text); |
| 95 |
| 96 hr = source_->GetTextAtPosition(5, &text, &length); |
| 97 |
| 98 EXPECT_TRUE(SUCCEEDED(hr)); |
| 99 EXPECT_EQ(kTextLength - 5, length); |
| 100 EXPECT_STREQ(kText + 5, text); |
| 101 |
| 102 // Trying to get a text chunk past the end should return null. |
| 103 hr = source_->GetTextAtPosition(kTextLength, &text, &length); |
| 104 |
| 105 EXPECT_TRUE(SUCCEEDED(hr)); |
| 106 EXPECT_EQ(nullptr, text); |
| 107 } |
| 108 |
| 109 TEST_F(TextAnalysisSourceTest, TestGetTextBeforePosition) { |
| 110 UINT32 length = 0; |
| 111 const wchar_t* text = nullptr; |
| 112 HRESULT hr = E_FAIL; |
| 113 // Trying to get a text chunk before the beginning should return null. |
| 114 hr = source_->GetTextBeforePosition(0, &text, &length); |
| 115 |
| 116 EXPECT_TRUE(SUCCEEDED(hr)); |
| 117 EXPECT_EQ(nullptr, text); |
| 118 |
| 119 hr = source_->GetTextBeforePosition(5, &text, &length); |
| 120 |
| 121 EXPECT_TRUE(SUCCEEDED(hr)); |
| 122 EXPECT_EQ(5u, length); |
| 123 EXPECT_STREQ(kText, text); |
| 124 |
| 125 hr = source_->GetTextBeforePosition(kTextLength, &text, &length); |
| 126 |
| 127 EXPECT_TRUE(SUCCEEDED(hr)); |
| 128 EXPECT_EQ(kTextLength, length); |
| 129 EXPECT_STREQ(kText, text); |
| 130 } |
| 131 |
| 132 } // namespace win |
| 133 } // namespace gfx |
OLD | NEW |