Index: chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc |
diff --git a/chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc b/chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc |
index 26c8f1d748e3d1293133165c03fff20e9280daca..df329d408fccaf9f486de1d39771eee559b7dca5 100644 |
--- a/chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc |
+++ b/chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc |
@@ -7,12 +7,15 @@ |
#include "base/memory/scoped_ptr.h" |
#include "base/message_loop/message_loop.h" |
#include "base/strings/utf_string_conversions.h" |
+#include "base/values.h" |
#include "chrome/browser/android/contextualsearch/contextual_search_context.h" |
#include "components/search_engines/template_url_service.h" |
#include "net/url_request/test_url_fetcher_factory.h" |
#include "net/url_request/url_request_test_util.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+using base::ListValue; |
+ |
namespace { |
const char kSomeSpecificBasePage[] = "http://some.specific.host.name.com"; |
@@ -63,11 +66,28 @@ class ContextualSearchDelegateTest : public testing::Test { |
} |
void CreateDefaultSearchContextAndRequestSearchTerm() { |
+ base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
+ CreateSearchContextAndRequestSearchTerm("Barack Obama", surrounding, 0, 6); |
+ } |
+ |
+ void CreateSearchContextAndRequestSearchTerm( |
+ const std::string& selected_text, |
+ const base::string16& surrounding_text, |
+ int start_offset, |
+ int end_offset) { |
test_context_ = new ContextualSearchContext( |
- "Barack Obama", true, GURL(kSomeSpecificBasePage), "utf-8"); |
+ selected_text, true, GURL(kSomeSpecificBasePage), "utf-8"); |
// ContextualSearchDelegate class takes ownership of the context. |
delegate_->set_context_for_testing(test_context_); |
- RequestSearchTerm(); |
+ |
+ test_context_->start_offset = start_offset; |
+ test_context_->end_offset = end_offset; |
+ test_context_->surrounding_text = surrounding_text; |
+ delegate_->ContinueSearchTermResolutionRequest(); |
+ fetcher_ = test_factory_.GetFetcherByID( |
+ ContextualSearchDelegate::kContextualSearchURLFetcherID); |
+ ASSERT_TRUE(fetcher_); |
+ ASSERT_TRUE(fetcher()); |
} |
void SetSurroundingContext(const base::string16& surrounding_text, |
@@ -82,18 +102,6 @@ class ContextualSearchDelegateTest : public testing::Test { |
delegate_->set_context_for_testing(test_context_); |
} |
- void RequestSearchTerm() { |
- test_context_->start_offset = 0; |
- test_context_->end_offset = 6; |
- test_context_->surrounding_text = |
- base::UTF8ToUTF16("Barack Obama just spoke."); |
- delegate_->ContinueSearchTermResolutionRequest(); |
- fetcher_ = test_factory_.GetFetcherByID( |
- ContextualSearchDelegate::kContextualSearchURLFetcherID); |
- ASSERT_TRUE(fetcher_); |
- ASSERT_TRUE(fetcher()); |
- } |
- |
bool DoesRequestContainOurSpecificBasePage() { |
return fetcher()->GetOriginalURL().spec().find( |
specific_base_page_URL_escaped()) != std::string::npos; |
@@ -112,6 +120,8 @@ class ContextualSearchDelegateTest : public testing::Test { |
bool do_prevent_preload() { return prevent_preload_; } |
std::string before_text() { return before_text_; } |
std::string after_text() { return after_text_; } |
+ int start_adjust() { return start_adjust_; } |
+ int end_adjust() { return end_adjust_; } |
// The delegate under test. |
scoped_ptr<ContextualSearchDelegate> delegate_; |
@@ -122,13 +132,17 @@ class ContextualSearchDelegateTest : public testing::Test { |
const std::string& search_term, |
const std::string& display_text, |
const std::string& alternate_term, |
- bool prevent_preload) { |
+ bool prevent_preload, |
+ int start_adjust, |
+ int end_adjust) { |
is_invalid_ = is_invalid; |
response_code_ = response_code; |
search_term_ = search_term; |
display_text_ = display_text; |
alternate_term_ = alternate_term; |
prevent_preload_ = prevent_preload; |
+ start_adjust_ = start_adjust; |
+ end_adjust_ = end_adjust; |
} |
void recordSurroundingText(const std::string& before_text, |
@@ -150,6 +164,8 @@ class ContextualSearchDelegateTest : public testing::Test { |
std::string display_text_; |
std::string alternate_term_; |
bool prevent_preload_; |
+ int start_adjust_; |
+ int end_adjust_; |
std::string before_text_; |
std::string after_text_; |
@@ -257,6 +273,112 @@ TEST_F(ContextualSearchDelegateTest, InvalidResponse) { |
EXPECT_TRUE(is_invalid()); |
} |
+TEST_F(ContextualSearchDelegateTest, ExpandSelectionToEnd) { |
+ base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
+ CreateSearchContextAndRequestSearchTerm("Barack", surrounding, 0, 6); |
Donn Denman
2015/07/10 17:47:03
Looks like this line is indented too much on all t
palmer
2015/07/10 17:53:38
Nit: Indentation
aurimas (slooooooooow)
2015/07/10 19:06:00
Done
|
+ |
+ fetcher()->set_response_code(200); |
+ fetcher()->SetResponseString( |
Donn Denman
2015/07/10 17:47:03
If all the response strings for these tests are th
aurimas (slooooooooow)
2015/07/10 19:06:00
Done
|
+ ")]}'\n" |
+ "{\"mid\":\"/m/02mjmr\", \"search_term\":\"barack\"," |
+ "\"info_text\":\"44th U.S. President\"," |
+ "\"display_text\":\"Barack Obama\", \"mentions\":[0,12]," |
+ "\"selected_text\":\"barack\", \"resolved_term\":\"barack obama\"}"); |
+ fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
+ |
+ EXPECT_EQ(0, start_adjust()); |
+ EXPECT_EQ(6, end_adjust()); |
+} |
+ |
+TEST_F(ContextualSearchDelegateTest, ExpandSelectionToStart) { |
+ base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
+ CreateSearchContextAndRequestSearchTerm("Obama", surrounding, 7, 12); |
+ |
+ fetcher()->set_response_code(200); |
+ fetcher()->SetResponseString( |
+ ")]}'\n" |
+ "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," |
+ "\"info_text\":\"44th U.S. President\"," |
+ "\"display_text\":\"Barack Obama\", \"mentions\":[0,12]," |
+ "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}"); |
+ fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
+ |
+ EXPECT_EQ(-7, start_adjust()); |
+ EXPECT_EQ(0, end_adjust()); |
+} |
+ |
+TEST_F(ContextualSearchDelegateTest, ExpandSelectionBothDirections) { |
+ base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
+ CreateSearchContextAndRequestSearchTerm("Ob", surrounding, 7, 9); |
+ |
+ fetcher()->set_response_code(200); |
+ fetcher()->SetResponseString( |
+ ")]}'\n" |
+ "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," |
+ "\"info_text\":\"44th U.S. President\"," |
+ "\"display_text\":\"Barack Obama\", \"mentions\":[0,12]," |
+ "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}"); |
+ fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
+ |
+ EXPECT_EQ(-7, start_adjust()); |
+ EXPECT_EQ(3, end_adjust()); |
+} |
+ |
+TEST_F(ContextualSearchDelegateTest, ExpandSelectionInvalid) { |
+ base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
+ CreateSearchContextAndRequestSearchTerm("Ob", surrounding, 7, 9); |
+ |
+ fetcher()->set_response_code(200); |
+ fetcher()->SetResponseString( |
+ ")]}'\n" |
+ "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," |
+ "\"info_text\":\"44th U.S. President\"," |
+ "\"display_text\":\"Barack Obama\", \"mentions\":[0,200]," |
+ "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}"); |
+ fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
+ |
+ EXPECT_EQ(0, start_adjust()); |
+ EXPECT_EQ(0, end_adjust()); |
+} |
+ |
+TEST_F(ContextualSearchDelegateTest, ContractSelectionValid) { |
+ base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
+ CreateSearchContextAndRequestSearchTerm( |
+ "Barack Obama just", surrounding, 0, 17); |
+ |
+ fetcher()->set_response_code(200); |
+ fetcher()->SetResponseString( |
+ ")]}'\n" |
+ "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," |
+ "\"info_text\":\"44th U.S. President\"," |
+ "\"display_text\":\"Barack Obama\", \"mentions\":[0,12]," |
+ "\"selected_text\":\"obama obama just\"," |
+ "\"resolved_term\":\"barack obama\"}"); |
+ fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
+ |
+ EXPECT_EQ(0, start_adjust()); |
+ EXPECT_EQ(-5, end_adjust()); |
+} |
+ |
+TEST_F(ContextualSearchDelegateTest, ContractSelectionInvalid) { |
+ base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
+ CreateSearchContextAndRequestSearchTerm( |
+ "Barack Obama just", surrounding, 0, 17); |
palmer
2015/07/10 17:53:38
What happens if you do crazy things like
Cre
aurimas (slooooooooow)
2015/07/10 19:06:00
Added a few more cases.
|
+ |
+ fetcher()->set_response_code(200); |
+ fetcher()->SetResponseString( |
+ ")]}'\n" |
+ "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," |
+ "\"info_text\":\"44th U.S. President\"," |
+ "\"display_text\":\"Barack Obama\", \"mentions\":[5,5]," |
+ "\"selected_text\":\"obama obama just\"," |
+ "\"resolved_term\":\"barack obama\"}"); |
+ fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
+ |
+ EXPECT_EQ(0, start_adjust()); |
+ EXPECT_EQ(0, end_adjust()); |
+} |
+ |
TEST_F(ContextualSearchDelegateTest, SurroundingTextHighMaximum) { |
base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee"); |
SetSurroundingContext(surrounding, 6, 11); |
@@ -282,6 +404,17 @@ TEST_F(ContextualSearchDelegateTest, SurroundingTextNoBeforeText) { |
EXPECT_EQ("ee f", after_text()); |
} |
+TEST_F(ContextualSearchDelegateTest, ExtractMentionsStartEnd) { |
+ ListValue mentions_list; |
+ mentions_list.AppendInteger(1); |
+ mentions_list.AppendInteger(2); |
+ int start = 0; |
+ int end = 0; |
+ delegate_->ExtractMentionsStartEnd(mentions_list, &start, &end); |
+ EXPECT_EQ(1, start); |
+ EXPECT_EQ(2, end); |
+} |
+ |
TEST_F(ContextualSearchDelegateTest, SurroundingTextForIcing) { |
base::string16 sample = base::ASCIIToUTF16("this is Barack Obama in office."); |
int limit_each_side = 3; |
@@ -317,9 +450,12 @@ TEST_F(ContextualSearchDelegateTest, DecodeSearchTermsFromJsonResponse) { |
std::string display_text; |
std::string alternate_term; |
std::string prevent_preload; |
+ int mention_start; |
+ int mention_end; |
delegate_->DecodeSearchTermsFromJsonResponse(json_with_escape, &search_term, |
&display_text, &alternate_term, |
- &prevent_preload); |
+ &prevent_preload, &mention_start, |
+ &mention_end); |
EXPECT_EQ("obama", search_term); |
EXPECT_EQ("Barack Obama", display_text); |
EXPECT_EQ("barack obama", alternate_term); |