nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 7 | #include "base/strings/utf_string_conversions.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | #include "ui/accessibility/ax_text_utils.h" |
| 10 | |
| 11 | namespace ui { |
| 12 | |
nektar | 2a054040 | 2016-03-10 00:09:07 | [diff] [blame^] | 13 | TEST(AXTextUtils, FindAccessibleTextBoundaryWord) { |
| 14 | const base::string16 text = |
| 15 | base::UTF8ToUTF16("Hello there.This/is\ntesting."); |
| 16 | const size_t text_length = text.length(); |
| 17 | std::vector<int> line_start_offsets; |
| 18 | line_start_offsets.push_back(19); |
| 19 | size_t result; |
| 20 | |
| 21 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 22 | 0, FORWARDS_DIRECTION); |
| 23 | EXPECT_EQ(6UL, result); |
| 24 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 25 | 5, BACKWARDS_DIRECTION); |
| 26 | EXPECT_EQ(0UL, result); |
| 27 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 28 | 6, FORWARDS_DIRECTION); |
| 29 | EXPECT_EQ(12UL, result); |
| 30 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 31 | 11, BACKWARDS_DIRECTION); |
| 32 | EXPECT_EQ(6UL, result); |
| 33 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 34 | 12, BACKWARDS_DIRECTION); |
| 35 | EXPECT_EQ(12UL, result); |
| 36 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 37 | 15, FORWARDS_DIRECTION); |
| 38 | EXPECT_EQ(17UL, result); |
| 39 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 40 | 15, BACKWARDS_DIRECTION); |
| 41 | EXPECT_EQ(12UL, result); |
| 42 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 43 | 16, FORWARDS_DIRECTION); |
| 44 | EXPECT_EQ(17UL, result); |
| 45 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 46 | 17, FORWARDS_DIRECTION); |
| 47 | EXPECT_EQ(20UL, result); |
| 48 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 49 | 20, FORWARDS_DIRECTION); |
| 50 | EXPECT_EQ(text_length, result); |
| 51 | result = FindAccessibleTextBoundary(text, line_start_offsets, WORD_BOUNDARY, |
| 52 | text_length, BACKWARDS_DIRECTION); |
| 53 | EXPECT_EQ(20UL, result); |
| 54 | } |
| 55 | |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 56 | TEST(AXTextUtils, FindAccessibleTextBoundaryLine) { |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 57 | const base::string16 text = base::UTF8ToUTF16("Line 1.\nLine 2\n\t"); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 58 | const size_t text_length = text.length(); |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 59 | std::vector<int> line_start_offsets; |
| 60 | line_start_offsets.push_back(8); |
| 61 | line_start_offsets.push_back(15); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 62 | size_t result; |
| 63 | |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 64 | // Basic cases. |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 65 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 66 | 5, FORWARDS_DIRECTION); |
| 67 | EXPECT_EQ(8UL, result); |
| 68 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 69 | 9, BACKWARDS_DIRECTION); |
| 70 | EXPECT_EQ(8UL, result); |
| 71 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 72 | 10, FORWARDS_DIRECTION); |
| 73 | EXPECT_EQ(15UL, result); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 74 | |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 75 | // Edge cases. |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 76 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 77 | text_length, BACKWARDS_DIRECTION); |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 78 | EXPECT_EQ(15UL, result); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 79 | |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 80 | // When the start_offset is at the start of the next line and we are searching |
| 81 | // backwards, it should not move. |
| 82 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 83 | 15, BACKWARDS_DIRECTION); |
| 84 | EXPECT_EQ(15UL, result); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 85 | |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 86 | // 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. |
| 88 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 89 | 14, BACKWARDS_DIRECTION); |
| 90 | EXPECT_EQ(8UL, result); |
| 91 | |
| 92 | // 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. |
| 94 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 95 | 8, FORWARDS_DIRECTION); |
| 96 | EXPECT_EQ(15UL, result); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 97 | |
| 98 | // When there is no previous line break and we are searching backwards, |
| 99 | // it should return 0. |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 100 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 101 | 4, BACKWARDS_DIRECTION); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 102 | EXPECT_EQ(0UL, result); |
| 103 | |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 104 | // When we are at the start of the last line and we are searching forwards. |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 105 | // it should return the text length. |
nektar | 28f5f64 | 2015-04-30 00:31:14 | [diff] [blame] | 106 | result = FindAccessibleTextBoundary(text, line_start_offsets, LINE_BOUNDARY, |
| 107 | 15, FORWARDS_DIRECTION); |
nektar | 22e0015b | 2015-01-12 22:25:06 | [diff] [blame] | 108 | EXPECT_EQ(text_length, result); |
| 109 | } |
| 110 | |
nektar | 2a054040 | 2016-03-10 00:09:07 | [diff] [blame^] | 111 | } // namespace ui |