summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/platform/fonts/OrientationIteratorTest.cpp
blob: 47ccac7c433a50c3ffca4e22390e6f3b9d35ba22 (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
// Copyright 2015 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 "platform/fonts/OrientationIterator.h"

#include "platform/Logging.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <string>

namespace blink {

struct TestRun {
    std::string text;
    OrientationIterator::RenderOrientation code;
};

struct ExpectedRun {
    unsigned limit;
    OrientationIterator::RenderOrientation renderOrientation;

    ExpectedRun(unsigned theLimit, OrientationIterator::RenderOrientation theRenderOrientation)
        : limit(theLimit)
        , renderOrientation(theRenderOrientation)
    {
    }
};

class OrientationIteratorTest : public testing::Test {
protected:
#if !LOG_DISABLED
    static void SetUpTestCase()
    {
        LogFonts = { WTFLogChannelOn };
    }
#endif

    void CheckRuns(const Vector<TestRun>& runs)
    {
        String text(emptyString16Bit());
        Vector<ExpectedRun> expect;
        for (auto& run : runs) {
            text.append(String::fromUTF8(run.text.c_str()));
            expect.append(ExpectedRun(text.length(), run.code));
        }
        OrientationIterator orientationIterator(text.characters16(), text.length(), FontOrientation::VerticalMixed);
        VerifyRuns(&orientationIterator, expect);
    }

    void VerifyRuns(OrientationIterator* orientationIterator,
        const Vector<ExpectedRun>& expect)
    {
        unsigned limit;
        OrientationIterator::RenderOrientation renderOrientation;
        unsigned long runCount = 0;
        while (orientationIterator->consume(&limit, &renderOrientation)) {
            ASSERT_LT(runCount, expect.size());
            ASSERT_EQ(expect[runCount].limit, limit);
            ASSERT_EQ(expect[runCount].renderOrientation, renderOrientation);
            ++runCount;
        }
        WTF_LOG(Fonts, "Expected %zu runs, got %lu ", expect.size(), runCount);
        ASSERT_EQ(expect.size(), runCount);
    }
};

// TODO(esprehn): WTF::Vector should allow initialization from a literal.
#define CHECK_RUNS(...) \
    static const TestRun runsArray[] = __VA_ARGS__; \
    Vector<TestRun> runs; \
    runs.append(runsArray, sizeof(runsArray) / sizeof(*runsArray)); \
    CheckRuns(runs);

TEST_F(OrientationIteratorTest, Empty)
{
    String empty(emptyString16Bit());
    OrientationIterator orientationIterator(empty.characters16(), empty.length(), FontOrientation::VerticalMixed);
    unsigned limit = 0;
    OrientationIterator::RenderOrientation orientation = OrientationIterator::OrientationInvalid;
    ASSERT(!orientationIterator.consume(&limit, &orientation));
    ASSERT_EQ(limit, 0u);
    ASSERT_EQ(orientation, OrientationIterator::OrientationInvalid);
}

TEST_F(OrientationIteratorTest, OneCharLatin)
{
    CHECK_RUNS({ { "A", OrientationIterator::OrientationRotateSideways } });
}

TEST_F(OrientationIteratorTest, OneAceOfSpades)
{
    CHECK_RUNS({ { "πŸ‚‘", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, OneEthiopicSyllable)
{
    CHECK_RUNS({ { "αŒ€", OrientationIterator::OrientationRotateSideways } });
}

TEST_F(OrientationIteratorTest, JapaneseLetterlikeEnd)
{
    CHECK_RUNS({ { "いろは", OrientationIterator::OrientationKeep },
        { "ℐℒℐℒℐℒℐℒℐℒℐℒℐℒ", OrientationIterator::OrientationRotateSideways } });
}

TEST_F(OrientationIteratorTest, LetterlikeJapaneseEnd)
{
    CHECK_RUNS({ { "ℐ", OrientationIterator::OrientationRotateSideways },
        { "いろは", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, OneCharJapanese)
{
    CHECK_RUNS({ { "い", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, Japanese)
{
    CHECK_RUNS({ { "いろはにほへと", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, IVS)
{
    CHECK_RUNS({ { "愉\xF3\xA0\x84\x81", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, MarkAtFirstCharRotated)
{
    // Unicode General Category M should be combined with the previous base
    // character, but they have their own orientation if they appear at the
    // beginning of a run.
    // http://www.unicode.org/reports/tr50/#grapheme_clusters
    // https://drafts.csswg.org/css-writing-modes-3/#vertical-orientations
    // U+0300 COMBINING GRAVE ACCENT is Mn (Mark, Nonspacing) with Rotated.
    CHECK_RUNS({ { "\xCC\x80", OrientationIterator::OrientationRotateSideways } });
}

TEST_F(OrientationIteratorTest, MarkAtFirstCharUpright)
{
    // U+20DD COMBINING ENCLOSING CIRCLE is Me (Mark, Enclosing) with Upright.
    CHECK_RUNS({ { "\xE2\x83\x9D", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, MarksAtFirstCharUpright)
{
    // U+20DD COMBINING ENCLOSING CIRCLE is Me (Mark, Enclosing) with Upright.
    // U+0300 COMBINING GRAVE ACCENT is Mn (Mark, Nonspacing) with Rotated.
    CHECK_RUNS({ { "\xE2\x83\x9D\xCC\x80", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, MarksAtFirstCharUprightThenBase)
{
    // U+20DD COMBINING ENCLOSING CIRCLE is Me (Mark, Enclosing) with Upright.
    // U+0300 COMBINING GRAVE ACCENT is Mn (Mark, Nonspacing) with Rotated.
    CHECK_RUNS({ { "\xE2\x83\x9D\xCC\x80", OrientationIterator::OrientationKeep },
        { "ABC\xE2\x83\x9D", OrientationIterator::OrientationRotateSideways } });
}

TEST_F(OrientationIteratorTest, JapaneseLatinMixedInside)
{
    CHECK_RUNS({ { "いろはに", OrientationIterator::OrientationKeep },
        { "Abc", OrientationIterator::OrientationRotateSideways },
        { "ほへと", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, PunctuationJapanese)
{
    CHECK_RUNS({ { ".…‘", OrientationIterator::OrientationRotateSideways },
        { "ほへと", OrientationIterator::OrientationKeep } });
}

TEST_F(OrientationIteratorTest, JapaneseLatinMixedOutside)
{
    CHECK_RUNS({ { "Abc", OrientationIterator::OrientationRotateSideways },
        { "ほへと", OrientationIterator::OrientationKeep },
        { "Xyz", OrientationIterator::OrientationRotateSideways } });
}

TEST_F(OrientationIteratorTest, JapaneseMahjonggMixed)
{
    CHECK_RUNS({ { "γ„γ‚γ―γ«πŸ€€γ»γΈγ¨", OrientationIterator::OrientationKeep } });
}

} // namespace blink