OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "chrome/browser/android/contextualsearch/search_action.h" |
| 6 |
| 7 #include "base/gtest_prod_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 using base::string16; |
| 12 using base::UTF8ToUTF16; |
| 13 using std::pair; |
| 14 |
| 15 // Tests parts of the SearchAction class. |
| 16 // This is part of the 2016-refactoring (crbug.com/624609, |
| 17 // go/cs-refactoring-2016). |
| 18 class SearchActionTest : public testing::Test { |
| 19 public: |
| 20 SearchActionTest() {} |
| 21 ~SearchActionTest() override {} |
| 22 }; |
| 23 |
| 24 TEST_F(SearchActionTest, IsValidCharacterTest) { |
| 25 EXPECT_TRUE(SearchAction::IsValidCharacter('a')); |
| 26 EXPECT_TRUE(SearchAction::IsValidCharacter('A')); |
| 27 EXPECT_TRUE(SearchAction::IsValidCharacter('0')); |
| 28 |
| 29 EXPECT_FALSE(SearchAction::IsValidCharacter(',')); |
| 30 EXPECT_FALSE(SearchAction::IsValidCharacter(' ')); |
| 31 EXPECT_FALSE(SearchAction::IsValidCharacter('-')); |
| 32 } |
| 33 |
| 34 TEST_F(SearchActionTest, FindFocusedWordTest) { |
| 35 // Test finding "word" within this sample string. |
| 36 string16 sample = UTF8ToUTF16("Sample word, text"); |
| 37 // Expected range when "word" is found. |
| 38 pair<int, int> expected_word(7, 11); |
| 39 // Expected range for an insertion right after "word". |
| 40 pair<int, int> expected_insertion_after_word(11, 11); |
| 41 |
| 42 // Any range inside the word but before the end should return the word. |
| 43 EXPECT_EQ(expected_word, SearchAction::FindFocusedWord(sample, 7, 7)); |
| 44 EXPECT_EQ(expected_word, SearchAction::FindFocusedWord(sample, 10, 10)); |
| 45 EXPECT_EQ(expected_word, SearchAction::FindFocusedWord(sample, 7, 11)); |
| 46 |
| 47 // A range just past the word returns an insertion after it. |
| 48 EXPECT_EQ(expected_insertion_after_word, |
| 49 SearchAction::FindFocusedWord(sample, 11, 11)); |
| 50 } |
| 51 |
| 52 TEST_F(SearchActionTest, SampleSurroundingsTest) { |
| 53 const int FOCUS_LENGTH = 5; // length of "focus" |
| 54 int f_s; // focus start |
| 55 int f_e; // focus end |
| 56 string16 sample; |
| 57 pair<string16, int> expected; |
| 58 |
| 59 // Sample big enough to include both ends. |
| 60 sample = UTF8ToUTF16("987654321focus123456789"); |
| 61 f_s = 9; |
| 62 f_e = f_s + FOCUS_LENGTH; |
| 63 expected = pair<string16, int>(sample, 0); |
| 64 EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 100)); |
| 65 |
| 66 // Sample must trim both ends, trimming 6 off each end. |
| 67 expected = pair<string16, int>(UTF8ToUTF16("321focus123"), 6); |
| 68 EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 12)); |
| 69 |
| 70 // With focus near the beginning, extra is shifted to the end. |
| 71 sample = UTF8ToUTF16("321focus123456789"); |
| 72 f_s = 3; |
| 73 f_e = f_s + FOCUS_LENGTH; |
| 74 expected = pair<string16, int>(UTF8ToUTF16("321focus12345"), 0); |
| 75 EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 13)); |
| 76 |
| 77 // With focus near the end, extra is shifted to the beginning. |
| 78 sample = UTF8ToUTF16("987654321focus123"); |
| 79 f_s = 9; |
| 80 f_e = f_s + FOCUS_LENGTH; |
| 81 expected = pair<string16, int>(UTF8ToUTF16("54321focus123"), 4); |
| 82 EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 13)); |
| 83 |
| 84 // Requesting less than the focus. |
| 85 sample = UTF8ToUTF16("focus"); |
| 86 f_s = 1; |
| 87 f_e = 2; |
| 88 expected = pair<string16, int>(UTF8ToUTF16("o"), 1); |
| 89 EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 1)); |
| 90 } |
OLD | NEW |