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 // The class under test. | |
24 std::unique_ptr<SearchAction> search_action_; | |
25 | |
26 protected: | |
27 void SetUp() override { search_action_.reset(new SearchAction()); } | |
28 void TearDown() override {} | |
29 | |
30 // Helper to set the context to the given sample and focus on |focus|. | |
31 void SetContext(std::string sample, std::string focus); | |
32 }; | |
33 | |
34 void SearchActionTest::SetContext(std::string sample, std::string focus) { | |
35 size_t offset = sample.find(focus); | |
36 ASSERT_NE(offset, std::string::npos); | |
37 search_action_->SetContext(sample, offset, offset + focus.length()); | |
38 } | |
39 | |
40 TEST_F(SearchActionTest, IsValidCharacterTest) { | |
41 EXPECT_TRUE(search_action_->IsValidCharacter('a')); | |
42 EXPECT_TRUE(search_action_->IsValidCharacter('A')); | |
43 EXPECT_TRUE(search_action_->IsValidCharacter('0')); | |
44 | |
45 EXPECT_FALSE(search_action_->IsValidCharacter(',')); | |
46 EXPECT_FALSE(search_action_->IsValidCharacter(' ')); | |
47 EXPECT_FALSE(search_action_->IsValidCharacter('-')); | |
48 } | |
49 | |
50 TEST_F(SearchActionTest, FindFocusedWordTest) { | |
51 // Test finding "word" within this sample string. | |
52 std::string sample = "Sample word, text"; | |
53 | |
54 // Any range inside the word but before the end should return the word. | |
55 search_action_->SetContext(sample, 7, 7); | |
56 EXPECT_EQ("word", search_action_->FindFocusedWord()); | |
57 search_action_->SetContext(sample, 10, 10); | |
58 EXPECT_EQ("word", search_action_->FindFocusedWord()); | |
59 search_action_->SetContext(sample, 7, 11); | |
60 EXPECT_EQ("word", search_action_->FindFocusedWord()); | |
61 | |
62 // A range just past the word returns an empty string. | |
63 search_action_->SetContext(sample, 11, 11); | |
64 EXPECT_EQ("", search_action_->FindFocusedWord()); | |
65 } | |
66 | |
67 TEST_F(SearchActionTest, SampleSurroundingsTest) { | |
68 std::string focus = "focus"; | |
69 std::string sample = "987654321focus123456789"; | |
70 | |
71 // Sample big enough to include both ends. | |
72 SetContext(sample, focus); | |
73 EXPECT_EQ(sample, search_action_->GetSampleText(100)); | |
74 | |
75 // Must trim both ends, trimming 6 off each end. | |
76 EXPECT_EQ("321focus123", search_action_->GetSampleText(12)); | |
77 | |
78 // With focus near the beginning, extra is shifted to the end. | |
79 SetContext("321focus123456789", focus); | |
80 EXPECT_EQ("321focus12345", search_action_->GetSampleText(13)); | |
81 | |
82 // With focus near the end, extra is shifted to the beginning. | |
83 SetContext("987654321focus123", focus); | |
84 EXPECT_EQ("54321focus123", search_action_->GetSampleText(13)); | |
85 | |
86 // Requesting less than the focus. | |
87 SetContext("focus", focus); | |
88 EXPECT_EQ("c", search_action_->GetSampleText(1)); | |
89 } | |
OLD | NEW |