Index: chrome/browser/android/contextualsearch/search_action_unittest.cc |
diff --git a/chrome/browser/android/contextualsearch/search_action_unittest.cc b/chrome/browser/android/contextualsearch/search_action_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bfa8bc6bc289945351344ad5e32e7f19cab6d1cc |
--- /dev/null |
+++ b/chrome/browser/android/contextualsearch/search_action_unittest.cc |
@@ -0,0 +1,81 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/android/contextualsearch/search_action.h" |
+ |
+#include "base/gtest_prod_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using base::string16; |
+using base::UTF8ToUTF16; |
+using std::pair; |
+ |
+// Tests parts of the SearchAction class. |
+class SearchActionTest : public testing::Test { |
+ public: |
+ SearchActionTest() {} |
+ ~SearchActionTest() override {} |
+}; |
+ |
+TEST_F(SearchActionTest, IsValidCharacterTest) { |
+ EXPECT_TRUE(SearchAction::IsValidCharacter('a')); |
+ EXPECT_TRUE(SearchAction::IsValidCharacter('A')); |
+ EXPECT_TRUE(SearchAction::IsValidCharacter('0')); |
+ |
+ EXPECT_FALSE(SearchAction::IsValidCharacter(',')); |
+ EXPECT_FALSE(SearchAction::IsValidCharacter(' ')); |
+ EXPECT_FALSE(SearchAction::IsValidCharacter('-')); |
+} |
+ |
+TEST_F(SearchActionTest, FindFocusedWordTest) { |
+ // Test finding "word" within this sample string. |
+ string16 sample = UTF8ToUTF16("Sample word, text"); |
+ // Expected range when "word" is found. |
+ pair<int, int> expected_word(7, 11); |
+ // Expected range for an insertion right after "word". |
+ pair<int, int> expected_insertion_after_word(11, 11); |
+ |
+ // Any range inside the word but before the end should return the word. |
+ EXPECT_EQ(expected_word, SearchAction::FindFocusedWord(sample, 7, 7)); |
+ EXPECT_EQ(expected_word, SearchAction::FindFocusedWord(sample, 10, 10)); |
+ EXPECT_EQ(expected_word, SearchAction::FindFocusedWord(sample, 7, 11)); |
+ |
+ // A range just past the word returns an insertion after it. |
+ EXPECT_EQ(expected_insertion_after_word, |
+ SearchAction::FindFocusedWord(sample, 11, 11)); |
+} |
+ |
+TEST_F(SearchActionTest, SampleSurroundingsTest) { |
+ const int FOCUS_LENGTH = 5; // length of "focus" |
+ int f_s; // focus start |
+ int f_e; // focus end |
+ string16 sample; |
+ pair<string16, int> expected; |
+ |
+ // Sample big enough to include both ends. |
+ sample = UTF8ToUTF16("987654321focus123456789"); |
+ f_s = 9; |
+ f_e = f_s + FOCUS_LENGTH; |
+ expected = pair<string16, int>(sample, 0); |
+ EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 100)); |
+ |
+ // Sample must trim both ends, trimming 6 off each end. |
+ expected = pair<string16, int>(UTF8ToUTF16("321focus123"), 6); |
+ EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 12)); |
+ |
+ // With focus near the beginning, extra is shifted to the end. |
+ sample = UTF8ToUTF16("321focus123456789"); |
+ f_s = 3; |
+ f_e = f_s + FOCUS_LENGTH; |
+ expected = pair<string16, int>(UTF8ToUTF16("321focus12345"), 0); |
+ EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 13)); |
+ |
+ // With focus near the end, extra is shifted to the beginning. |
+ sample = UTF8ToUTF16("987654321focus123"); |
+ f_s = 9; |
+ f_e = f_s + FOCUS_LENGTH; |
+ expected = pair<string16, int>(UTF8ToUTF16("54321focus123"), 4); |
+ EXPECT_EQ(expected, SearchAction::SampleSurroundings(sample, f_s, f_e, 13)); |
+} |