1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/bidi_line_iterator.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ui {
namespace gfx {
namespace {
class BiDiLineIteratorTest
: public testing::TestWithParam<base::i18n::TextDirection> {
public:
BiDiLineIteratorTest() = default;
BiDiLineIteratorTest(const BiDiLineIteratorTest&) = delete;
BiDiLineIteratorTest& operator=(const BiDiLineIteratorTest&) = delete;
BiDiLineIterator* iterator() { return &iterator_; }
private:
BiDiLineIterator iterator_;
};
TEST_P(BiDiLineIteratorTest, OnlyLTR) {
iterator()->Open(u"abc 馃榿 娴嬭瘯", GetParam());
ASSERT_EQ(1, iterator()->CountRuns());
int start, length;
EXPECT_EQ(UBIDI_LTR, iterator()->GetVisualRun(0, &start, &length));
EXPECT_EQ(0, start);
EXPECT_EQ(9, length);
int end;
UBiDiLevel level;
iterator()->GetLogicalRun(0, &end, &level);
EXPECT_EQ(9, end);
if (GetParam() == base::i18n::TextDirection::RIGHT_TO_LEFT)
EXPECT_EQ(2, level);
else
EXPECT_EQ(0, level);
}
TEST_P(BiDiLineIteratorTest, OnlyRTL) {
iterator()->Open(u"诪讛 讛砖注讛", GetParam());
ASSERT_EQ(1, iterator()->CountRuns());
int start, length;
EXPECT_EQ(UBIDI_RTL, iterator()->GetVisualRun(0, &start, &length));
EXPECT_EQ(0, start);
EXPECT_EQ(7, length);
int end;
UBiDiLevel level;
iterator()->GetLogicalRun(0, &end, &level);
EXPECT_EQ(7, end);
EXPECT_EQ(1, level);
}
TEST_P(BiDiLineIteratorTest, Mixed) {
iterator()->Open(u"讗谞讬 诪砖转诪砖 讘- Chrome 讻讚驻讚驻谉 讛讗讬谞讟专谞讟 砖诇讬", GetParam());
ASSERT_EQ(3, iterator()->CountRuns());
// We'll get completely different results depending on the top-level paragraph
// direction.
if (GetParam() == base::i18n::TextDirection::RIGHT_TO_LEFT) {
// If para direction is RTL, expect the LTR substring "Chrome" to be nested
// within the surrounding RTL text.
int start, length;
EXPECT_EQ(UBIDI_RTL, iterator()->GetVisualRun(0, &start, &length));
EXPECT_EQ(19, start);
EXPECT_EQ(20, length);
EXPECT_EQ(UBIDI_LTR, iterator()->GetVisualRun(1, &start, &length));
EXPECT_EQ(13, start);
EXPECT_EQ(6, length);
EXPECT_EQ(UBIDI_RTL, iterator()->GetVisualRun(2, &start, &length));
EXPECT_EQ(0, start);
EXPECT_EQ(13, length);
int end;
UBiDiLevel level;
iterator()->GetLogicalRun(0, &end, &level);
EXPECT_EQ(13, end);
EXPECT_EQ(1, level);
iterator()->GetLogicalRun(13, &end, &level);
EXPECT_EQ(19, end);
EXPECT_EQ(2, level);
iterator()->GetLogicalRun(19, &end, &level);
EXPECT_EQ(39, end);
EXPECT_EQ(1, level);
} else {
// If the para direction is LTR, expect the LTR substring "- Chrome " to be
// at the top level, with two nested RTL runs on either side.
int start, length;
EXPECT_EQ(UBIDI_RTL, iterator()->GetVisualRun(0, &start, &length));
EXPECT_EQ(0, start);
EXPECT_EQ(11, length);
EXPECT_EQ(UBIDI_LTR, iterator()->GetVisualRun(1, &start, &length));
EXPECT_EQ(11, start);
EXPECT_EQ(9, length);
EXPECT_EQ(UBIDI_RTL, iterator()->GetVisualRun(2, &start, &length));
EXPECT_EQ(20, start);
EXPECT_EQ(19, length);
int end;
UBiDiLevel level;
iterator()->GetLogicalRun(0, &end, &level);
EXPECT_EQ(11, end);
EXPECT_EQ(1, level);
iterator()->GetLogicalRun(11, &end, &level);
EXPECT_EQ(20, end);
EXPECT_EQ(0, level);
iterator()->GetLogicalRun(20, &end, &level);
EXPECT_EQ(39, end);
EXPECT_EQ(1, level);
}
}
TEST_P(BiDiLineIteratorTest, RTLPunctuationNoCustomBehavior) {
// This string features Hebrew characters interleaved with ASCII punctuation.
iterator()->Open(
u"讗!讘\"讙#讚$讛%讜&讝'讞(讟)讬*讱+讻,诇-诐.诪/"
u"谉:谞;住<注=祝>驻?抓@爪[拽\\专]砖^转_讗`讘{讙|讚}讛~讜",
GetParam());
// Expect a single RTL run.
ASSERT_EQ(1, iterator()->CountRuns());
int start, length;
EXPECT_EQ(UBIDI_RTL, iterator()->GetVisualRun(0, &start, &length));
EXPECT_EQ(0, start);
EXPECT_EQ(65, length);
int end;
UBiDiLevel level;
iterator()->GetLogicalRun(0, &end, &level);
EXPECT_EQ(65, end);
EXPECT_EQ(1, level);
}
INSTANTIATE_TEST_SUITE_P(
All,
BiDiLineIteratorTest,
::testing::Values(base::i18n::TextDirection::LEFT_TO_RIGHT,
base::i18n::TextDirection::RIGHT_TO_LEFT));
} // namespace
} // namespace gfx
} // namespace ui
|