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