summaryrefslogtreecommitdiff
path: root/base/i18n/bidi_line_iterator_unittest.cc
blob: d531313bf1d65645439922dcae923a491fd55b53 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/i18n/bidi_line_iterator.h"

#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace i18n {
namespace {

class BiDiLineIteratorTest : public testing::TestWithParam<TextDirection> {
 public:
  BiDiLineIteratorTest() = default;

  BiDiLineIterator* iterator() { return &iterator_; }

 private:
  BiDiLineIterator iterator_;

  DISALLOW_COPY_AND_ASSIGN(BiDiLineIteratorTest);
};

TEST_P(BiDiLineIteratorTest, OnlyLTR) {
  iterator()->Open(UTF8ToUTF16("abc 😁 测试"), GetParam(),
                   BiDiLineIterator::CustomBehavior::NONE);
  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() == TextDirection::RIGHT_TO_LEFT)
    EXPECT_EQ(2, level);
  else
    EXPECT_EQ(0, level);
}

TEST_P(BiDiLineIteratorTest, OnlyRTL) {
  iterator()->Open(UTF8ToUTF16("מה השעה"), GetParam(),
                   BiDiLineIterator::CustomBehavior::NONE);
  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(UTF8ToUTF16("אני משתמש ב- Chrome כדפדפן האינטרנט שלי"),
                   GetParam(), BiDiLineIterator::CustomBehavior::NONE);
  ASSERT_EQ(3, iterator()->CountRuns());

  // We'll get completely different results depending on the top-level paragraph
  // direction.
  if (GetParam() == 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(UTF8ToUTF16("א!ב\"ג#ד$ה%ו&ז'ח(ט)י*ך+כ,ל-ם.מ/"
                               "ן:נ;ס<ע=ף>פ?ץ@צ[ק\\ר]ש^ת_א`ב{ג|ד}ה~ו"),
                   GetParam(), BiDiLineIterator::CustomBehavior::NONE);

  // 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);
}

TEST_P(BiDiLineIteratorTest, RTLPunctuationAsURL) {
  // This string features Hebrew characters interleaved with ASCII punctuation.
  iterator()->Open(UTF8ToUTF16("א!ב\"ג#ד$ה%ו&ז'ח(ט)י*ך+כ,ל-ם.מ/"
                               "ן:נ;ס<ע=ף>פ?ץ@צ[ק\\ר]ש^ת_א`ב{ג|ד}ה~ו"),
                   GetParam(), BiDiLineIterator::CustomBehavior::AS_URL);

  const int kStringSize = 65;

  // Expect a primary RTL run, broken up by each of the 8 punctuation marks that
  // are considered strong LTR (17 runs total).
  struct {
    int start;
    UBiDiDirection dir;
  } expected_runs[] = {
      {0, UBIDI_RTL},  {5, UBIDI_LTR},   // '#'
      {6, UBIDI_RTL},  {11, UBIDI_LTR},  // '&'
      {12, UBIDI_RTL}, {27, UBIDI_LTR},  // '.'
      {28, UBIDI_RTL}, {29, UBIDI_LTR},  // '/'
      {30, UBIDI_RTL}, {31, UBIDI_LTR},  // ':'
      {32, UBIDI_RTL}, {37, UBIDI_LTR},  // '='
      {38, UBIDI_RTL}, {41, UBIDI_LTR},  // '?'
      {42, UBIDI_RTL}, {43, UBIDI_LTR},  // '@'
      {44, UBIDI_RTL},
  };

  ASSERT_EQ(arraysize(expected_runs),
            static_cast<size_t>(iterator()->CountRuns()));

  for (size_t i = 0; i < arraysize(expected_runs); ++i) {
    const auto& expected_run = expected_runs[i];
    int expected_run_end = i >= arraysize(expected_runs) - 1
                               ? kStringSize
                               : expected_runs[i + 1].start;

    size_t visual_index = GetParam() == TextDirection::RIGHT_TO_LEFT
                              ? arraysize(expected_runs) - 1 - i
                              : i;
    int start, length;
    EXPECT_EQ(expected_run.dir,
              iterator()->GetVisualRun(visual_index, &start, &length))
        << "(i = " << i << ")";
    EXPECT_EQ(expected_run.start, start) << "(i = " << i << ")";
    EXPECT_EQ(expected_run_end - expected_run.start, length)
        << "(i = " << i << ")";

    int expected_level =
        expected_run.dir == UBIDI_RTL
            ? 1
            : (GetParam() == TextDirection::RIGHT_TO_LEFT ? 2 : 0);
    int end;
    UBiDiLevel level;
    iterator()->GetLogicalRun(expected_run.start, &end, &level);
    EXPECT_EQ(expected_run_end, end) << "(i = " << i << ")";
    EXPECT_EQ(expected_level, level) << "(i = " << i << ")";
  }
}

INSTANTIATE_TEST_CASE_P(,
                        BiDiLineIteratorTest,
                        ::testing::Values(TextDirection::LEFT_TO_RIGHT,
                                          TextDirection::RIGHT_TO_LEFT));

}  // namespace
}  // namespace i18n
}  // namespace base