File: juce_AccessibilityTextHelpers_test.cpp

package info (click to toggle)
juce 8.0.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 78,204 kB
  • sloc: cpp: 521,891; ansic: 159,819; java: 2,996; javascript: 847; xml: 273; python: 224; sh: 162; makefile: 84
file content (164 lines) | stat: -rw-r--r-- 11,716 bytes parent folder | download | duplicates (3)
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
/*
  ==============================================================================

   This file is part of the JUCE framework.
   Copyright (c) Raw Material Software Limited

   JUCE is an open source framework subject to commercial or open source
   licensing.

   By downloading, installing, or using the JUCE framework, or combining the
   JUCE framework with any other source code, object code, content or any other
   copyrightable work, you agree to the terms of the JUCE End User Licence
   Agreement, and all incorporated terms including the JUCE Privacy Policy and
   the JUCE Website Terms of Service, as applicable, which will bind you. If you
   do not agree to the terms of these agreements, we will not license the JUCE
   framework to you, and you must discontinue the installation or download
   process and cease use of the JUCE framework.

   JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
   JUCE Privacy Policy: https://juce.com/juce-privacy-policy
   JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/

   Or:

   You may also use this code under the terms of the AGPLv3:
   https://www.gnu.org/licenses/agpl-3.0.en.html

   THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
   WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.

  ==============================================================================
*/

namespace juce
{

struct AccessibilityTextHelpersTest final : public UnitTest
{
    AccessibilityTextHelpersTest()
        : UnitTest ("AccessibilityTextHelpers", UnitTestCategories::gui) {}

    void runTest() override
    {
        using ATH = AccessibilityTextHelpers;

        beginTest ("Android find word end");
        {
            const auto testMultiple = [this] (String str,
                                              int start,
                                              const std::vector<int>& collection)
            {
                auto it = collection.begin();

                for (const auto direction : { ATH::Direction::forwards, ATH::Direction::backwards })
                {
                    for (const auto includeBoundary : { ATH::IncludeThisBoundary::no, ATH::IncludeThisBoundary::yes })
                    {
                        for (const auto includeWhitespace : { ATH::IncludeWhitespaceAfterWords::no, ATH::IncludeWhitespaceAfterWords::yes })
                        {
                            const auto actual = ATH::findNextWordEndOffset (str.begin(), str.end(), str.begin() + start, direction, includeBoundary, includeWhitespace);
                            const auto expected = *it++;
                            expect (expected == actual);
                        }
                    }
                }
            };

            // Character Indices         0  3 56       13                                                                    50          51
            //                           |  | ||       |                                                                     |           |
            const auto string = String ("hello world \r\n with some  spaces in this sentence ") + String (CharPointer_UTF8 ("\xe2\x88\xae E\xe2\x8b\x85""da = Q"));
            // Direction                 forwards    forwards    forwards    forwards    backwards   backwards   backwards   backwards
            // IncludeBoundary           no          no          yes         yes         no          no          yes         yes
            // IncludeWhitespace         no          yes         no          yes         no          yes         no          yes
            testMultiple (string,   0, { 5,          6,          5,          0,           0,          0,          0,          0 });
            testMultiple (string,   3, { 2,          3,          2,          3,          -3,         -3,         -3,         -3 });
            testMultiple (string,   5, { 6,          1,          0,          1,          -5,         -5,         -5,          0 });
            testMultiple (string,   6, { 5,          9,          5,          0,          -6,         -1,          0,         -1 });
            testMultiple (string,  13, { 6,          2,          6,          2,          -7,         -2,         -7,         -2 });
            testMultiple (string,  50, { 1,          2,          1,          0,          -9,         -1,          0,         -1 });
            testMultiple (string,  51, { 5,          1,          0,          1,          -1,         -2,         -1,          0 });

            testMultiple ("  a b ", 0, { 3,          2,          0,          2,           0,          0,          0,          0 });
            testMultiple ("  a b ", 1, { 2,          1,          2,          1,          -1,         -1,         -1,         -1 });
        }

        beginTest ("Android text range adjustment");
        {
            const auto testMultiple = [this] (String str,
                                              Range<int> initial,
                                              auto boundary,
                                              const std::vector<Range<int>>& collection)
            {
                auto it = collection.begin();

                for (auto extend : { ATH::ExtendSelection::no, ATH::ExtendSelection::yes })
                {
                    for (auto direction : { ATH::Direction::forwards, ATH::Direction::backwards })
                    {
                        for (auto insert : { CursorPosition::begin, CursorPosition::end })
                        {
                            const MockAccessibilityTextInterface mock { str, initial, insert };
                            const auto actual = ATH::findNewSelectionRangeAndroid (mock, boundary, extend, direction);
                            const auto expected = *it++;
                            expect (expected == actual);
                        }
                    }
                }
            };

            // Extend                                                                       no              no              no              no              yes             yes             yes             yes
            // Direction                                                                    forwards        forwards        backwards       backwards       forwards        forwards        backwards       backwards
            // Insert                                                                       begin           end             begin           end             begin           end             begin           end
            testMultiple ("hello world", {  5,  5 }, ATH::BoundaryType::character,        { { 6,  6 },      { 6, 6 },       { 4, 4 },       { 4, 4 },       { 5, 6 },       { 5, 6 },       { 4, 5 },       { 4, 5 } });
            testMultiple ("hello world", {  0,  0 }, ATH::BoundaryType::character,        { { 1, 1 },       { 1, 1 },       { 0, 0 },       { 0, 0 },       { 0, 1 },       { 0, 1 },       { 0, 0 },       { 0, 0 } });
            testMultiple ("hello world", { 11, 11 }, ATH::BoundaryType::character,        { { 11, 11 },     { 11, 11 },     { 10, 10 },     { 10, 10 },     { 11, 11 },     { 11, 11 },     { 10, 11 },     { 10, 11 } });
            testMultiple ("hello world", {  4,  5 }, ATH::BoundaryType::character,        { { 5, 5 },       { 6, 6 },       { 3, 3 },       { 4, 4 },       { 5, 5 },       { 4, 6 },       { 3, 5 },       { 4, 4 } });
            testMultiple ("hello world", {  0,  1 }, ATH::BoundaryType::character,        { { 1, 1 },       { 2, 2 },       { 0, 0 },       { 0, 0 },       { 1, 1 },       { 0, 2 },       { 0, 1 },       { 0, 0 } });
            testMultiple ("hello world", { 10, 11 }, ATH::BoundaryType::character,        { { 11, 11 },     { 11, 11 },     { 9, 9 },       { 10, 10 },     { 11, 11 },     { 10, 11 },     { 9, 11 },      { 10, 10 } });

            testMultiple ("foo  bar  baz", { 0, 0 }, ATH::BoundaryType::word,             { { 3, 3 },       { 3, 3 },       { 0, 0 },       { 0, 0 },       { 0, 3 },       { 0, 3 },       { 0, 0 },       { 0, 0 } });
            testMultiple ("foo  bar  baz", { 1, 6 }, ATH::BoundaryType::word,             { { 3, 3 },       { 8, 8 },       { 0, 0 },       { 5, 5 },       { 3, 6 },       { 1, 8 },       { 0, 6 },       { 1, 5 } });
            testMultiple ("foo  bar  baz", { 3, 3 }, ATH::BoundaryType::word,             { { 8, 8 },       { 8, 8 },       { 0, 0 },       { 0, 0 },       { 3, 8 },       { 3, 8 },       { 0, 3 },       { 0, 3 } });
            testMultiple ("foo  bar  baz", { 3, 5 }, ATH::BoundaryType::word,             { { 8, 8 },       { 8, 8 },       { 0, 0 },       { 0, 0 },       { 5, 8 },       { 3, 8 },       { 0, 5 },       { 0, 3 } });

            testMultiple ("foo bar\n\n\na b\nc d e", { 0, 0 }, ATH::BoundaryType::line,   { { 8, 8 },       { 8, 8 },       { 0, 0 },       { 0, 0 },       { 0, 8 },       { 0, 8 },       { 0, 0 },       { 0, 0 } });
            testMultiple ("foo bar\n\n\na b\nc d e", { 7, 7 }, ATH::BoundaryType::line,   { { 8, 8 },       { 8, 8 },       { 0, 0 },       { 0, 0 },       { 7, 8 },       { 7, 8 },       { 0, 7 },       { 0, 7 } });
            testMultiple ("foo bar\n\n\na b\nc d e", { 8, 8 }, ATH::BoundaryType::line,   { { 9, 9 },       { 9, 9 },       { 0, 0 },       { 0, 0 },       { 8, 9 },       { 8, 9 },       { 0, 8 },       { 0, 8 } });

            testMultiple ("foo bar\r\na b\r\nxyz", {  0,  0 }, ATH::BoundaryType::line,   { { 9, 9 },       { 9, 9 },       { 0, 0 },       { 0, 0 },       { 0, 9 },       { 0, 9 },       { 0, 0 },       { 0, 0 } });
            testMultiple ("foo bar\r\na b\r\nxyz", { 10, 10 }, ATH::BoundaryType::line,   { { 14, 14 },     { 14, 14 },     { 9, 9 },       { 9, 9 },       { 10, 14 },     { 10, 14 },     { 9, 10 },      { 9, 10 } });
        }
    }

    enum class CursorPosition { begin, end };

    class MockAccessibilityTextInterface final : public AccessibilityTextInterface
    {
    public:
        MockAccessibilityTextInterface (String stringIn, Range<int> selectionIn, CursorPosition insertIn)
            : string (stringIn), selection (selectionIn), insert (insertIn) {}

        bool isDisplayingProtectedText()                  const override { return false; }
        bool isReadOnly()                                 const override { return false; }
        int getTotalNumCharacters()                       const override { return string.length(); }
        Range<int> getSelection()                         const override { return selection; }
        int getTextInsertionOffset()                      const override { return insert == CursorPosition::begin ? selection.getStart() : selection.getEnd(); }
        String getText (Range<int> range)                 const override { return string.substring (range.getStart(), range.getEnd()); }
        RectangleList<int> getTextBounds (Range<int>)     const override { return {}; }
        int getOffsetAtPoint (Point<int>)                 const override { return 0; }

        void setSelection (Range<int> newRange)                 override { selection = newRange; }
        void setText (const String& newText)                    override { string = newText; }

    private:
        String string;
        Range<int> selection;
        CursorPosition insert;
    };
};

static AccessibilityTextHelpersTest accessibilityTextHelpersTest;

} // namespace juce