| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/accessibility/ax_enums.h" |
| 9 #include "ui/accessibility/ax_text_utils.h" | 10 #include "ui/accessibility/ax_text_utils.h" |
| 10 | 11 |
| 11 namespace ui { | 12 namespace ui { |
| 12 | 13 |
| 13 TEST(AXTextUtils, FindAccessibleTextBoundaryWord) { | 14 TEST(AXTextUtils, FindAccessibleTextBoundaryWord) { |
| 14 const base::string16 text = | 15 const base::string16 text = |
| 15 base::UTF8ToUTF16("Hello there.This/is\ntesting."); | 16 base::UTF8ToUTF16("Hello there.This/is\ntesting."); |
| 16 const size_t text_length = text.length(); | 17 const size_t text_length = text.length(); |
| 17 std::vector<int> line_start_offsets; | 18 std::vector<int> line_start_offsets; |
| 18 line_start_offsets.push_back(19); | 19 line_start_offsets.push_back(19); |
| 19 size_t result; | 20 size_t result; |
| 20 | 21 |
| 21 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 22 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 22 0, FORWARDS_DIRECTION); | 23 0, FORWARDS_DIRECTION, |
| 24 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 23 EXPECT_EQ(6UL, result); | 25 EXPECT_EQ(6UL, result); |
| 24 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 26 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 25 5, BACKWARDS_DIRECTION); | 27 5, BACKWARDS_DIRECTION, |
| 28 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 26 EXPECT_EQ(0UL, result); | 29 EXPECT_EQ(0UL, result); |
| 27 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 30 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 28 6, FORWARDS_DIRECTION); | 31 6, FORWARDS_DIRECTION, |
| 32 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 29 EXPECT_EQ(12UL, result); | 33 EXPECT_EQ(12UL, result); |
| 30 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 34 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 31 11, BACKWARDS_DIRECTION); | 35 11, BACKWARDS_DIRECTION, |
| 36 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 32 EXPECT_EQ(6UL, result); | 37 EXPECT_EQ(6UL, result); |
| 33 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 38 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 34 12, BACKWARDS_DIRECTION); | 39 12, BACKWARDS_DIRECTION, |
| 40 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 35 EXPECT_EQ(12UL, result); | 41 EXPECT_EQ(12UL, result); |
| 36 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 42 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 37 15, FORWARDS_DIRECTION); | 43 15, FORWARDS_DIRECTION, |
| 44 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 38 EXPECT_EQ(17UL, result); | 45 EXPECT_EQ(17UL, result); |
| 39 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 46 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 40 15, BACKWARDS_DIRECTION); | 47 15, BACKWARDS_DIRECTION, |
| 48 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 41 EXPECT_EQ(12UL, result); | 49 EXPECT_EQ(12UL, result); |
| 42 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 50 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 43 16, FORWARDS_DIRECTION); | 51 16, FORWARDS_DIRECTION, |
| 52 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 44 EXPECT_EQ(17UL, result); | 53 EXPECT_EQ(17UL, result); |
| 45 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 54 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 46 17, FORWARDS_DIRECTION); | 55 17, FORWARDS_DIRECTION, |
| 56 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 47 EXPECT_EQ(20UL, result); | 57 EXPECT_EQ(20UL, result); |
| 48 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 58 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 49 20, FORWARDS_DIRECTION); | 59 20, FORWARDS_DIRECTION, |
| 60 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 50 EXPECT_EQ(text_length, result); | 61 EXPECT_EQ(text_length, result); |
| 51 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, | 62 result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 52 text_length, BACKWARDS_DIRECTION); | 63 text_length, BACKWARDS_DIRECTION, |
| 64 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 53 EXPECT_EQ(20UL, result); | 65 EXPECT_EQ(20UL, result); |
| 54 } | 66 } |
| 55 | 67 |
| 56 TEST(AXTextUtils, FindAccessibleTextBoundaryLine) { | 68 TEST(AXTextUtils, FindAccessibleTextBoundaryLine) { |
| 57 const base::string16 text = base::UTF8ToUTF16("Line 1.\nLine 2\n\t"); | 69 const base::string16 text = base::UTF8ToUTF16("Line 1.\nLine 2\n\t"); |
| 58 const size_t text_length = text.length(); | 70 const size_t text_length = text.length(); |
| 59 std::vector<int> line_start_offsets; | 71 std::vector<int> line_start_offsets; |
| 60 line_start_offsets.push_back(8); | 72 line_start_offsets.push_back(8); |
| 61 line_start_offsets.push_back(15); | 73 line_start_offsets.push_back(15); |
| 62 size_t result; | 74 size_t result; |
| 63 | 75 |
| 64 // Basic cases. | 76 // Basic cases. |
| 65 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 77 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 66 5, FORWARDS_DIRECTION); | 78 5, FORWARDS_DIRECTION, |
| 79 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 67 EXPECT_EQ(8UL, result); | 80 EXPECT_EQ(8UL, result); |
| 68 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 81 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 69 9, BACKWARDS_DIRECTION); | 82 9, BACKWARDS_DIRECTION, |
| 83 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 70 EXPECT_EQ(8UL, result); | 84 EXPECT_EQ(8UL, result); |
| 71 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 85 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 72 10, FORWARDS_DIRECTION); | 86 10, FORWARDS_DIRECTION, |
| 87 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 73 EXPECT_EQ(15UL, result); | 88 EXPECT_EQ(15UL, result); |
| 74 | 89 |
| 75 // Edge cases. | 90 // Edge cases. |
| 76 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 91 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 77 text_length, BACKWARDS_DIRECTION); | 92 text_length, BACKWARDS_DIRECTION, |
| 93 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 78 EXPECT_EQ(15UL, result); | 94 EXPECT_EQ(15UL, result); |
| 79 | 95 |
| 80 // When the start_offset is at the start of the next line and we are searching | 96 // When the start_offset is at the start of the next line and we are searching |
| 81 // backwards, it should not move. | 97 // backwards, it should not move. |
| 82 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 98 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 83 15, BACKWARDS_DIRECTION); | 99 15, BACKWARDS_DIRECTION, |
| 100 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 84 EXPECT_EQ(15UL, result); | 101 EXPECT_EQ(15UL, result); |
| 85 | 102 |
| 86 // When the start_offset is at a hard line break and we are searching | 103 // When the start_offset is at a hard line break and we are searching |
| 87 // backwards, it should return the start of the previous line. | 104 // backwards, it should return the start of the previous line. |
| 88 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 105 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 89 14, BACKWARDS_DIRECTION); | 106 14, BACKWARDS_DIRECTION, |
| 107 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 90 EXPECT_EQ(8UL, result); | 108 EXPECT_EQ(8UL, result); |
| 91 | 109 |
| 92 // When the start_offset is at the start of a line and we are searching | 110 // When the start_offset is at the start of a line and we are searching |
| 93 // forwards, it should return the start of the next line. | 111 // forwards, it should return the start of the next line. |
| 94 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 112 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 95 8, FORWARDS_DIRECTION); | 113 8, FORWARDS_DIRECTION, |
| 114 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 96 EXPECT_EQ(15UL, result); | 115 EXPECT_EQ(15UL, result); |
| 97 | 116 |
| 98 // When there is no previous line break and we are searching backwards, | 117 // When there is no previous line break and we are searching backwards, |
| 99 // it should return 0. | 118 // it should return 0. |
| 100 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 119 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 101 4, BACKWARDS_DIRECTION); | 120 4, BACKWARDS_DIRECTION, |
| 121 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 102 EXPECT_EQ(0UL, result); | 122 EXPECT_EQ(0UL, result); |
| 103 | 123 |
| 104 // When we are at the start of the last line and we are searching forwards. | 124 // When we are at the start of the last line and we are searching forwards. |
| 105 // it should return the text length. | 125 // it should return the text length. |
| 106 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, | 126 result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 107 15, FORWARDS_DIRECTION); | 127 15, FORWARDS_DIRECTION, |
| 128 AX_TEXT_AFFINITY_DOWNSTREAM); |
| 108 EXPECT_EQ(text_length, result); | 129 EXPECT_EQ(text_length, result); |
| 109 } | 130 } |
| 110 | 131 |
| 111 } // namespace ui | 132 } // namespace ui |
| OLD | NEW |