Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1086)

Side by Side Diff: chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc

Issue 1205033005: Adds selection expansion support for Contextual Search. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed java tests Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" 5 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
10 #include "chrome/browser/android/contextualsearch/contextual_search_context.h" 11 #include "chrome/browser/android/contextualsearch/contextual_search_context.h"
11 #include "components/search_engines/template_url_service.h" 12 #include "components/search_engines/template_url_service.h"
12 #include "net/url_request/test_url_fetcher_factory.h" 13 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h" 14 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
17 using base::ListValue;
18
16 namespace { 19 namespace {
17 20
18 const char kSomeSpecificBasePage[] = "http://some.specific.host.name.com"; 21 const char kSomeSpecificBasePage[] = "http://some.specific.host.name.com";
19 22
20 } // namespace 23 } // namespace
21 24
22 class ContextualSearchDelegateTest : public testing::Test { 25 class ContextualSearchDelegateTest : public testing::Test {
23 public: 26 public:
24 ContextualSearchDelegateTest() {} 27 ContextualSearchDelegateTest() {}
25 ~ContextualSearchDelegateTest() override {} 28 ~ContextualSearchDelegateTest() override {}
(...skipping 30 matching lines...) Expand all
56 "{google:contextualSearchVersion}{google:contextualSearchContextData}"; 59 "{google:contextualSearchVersion}{google:contextualSearchContextData}";
57 TemplateURL* template_url = new TemplateURL(data); 60 TemplateURL* template_url = new TemplateURL(data);
58 // Takes ownership of |template_url|. 61 // Takes ownership of |template_url|.
59 TemplateURLService* template_url_service = new TemplateURLService(NULL, 0); 62 TemplateURLService* template_url_service = new TemplateURLService(NULL, 0);
60 template_url_service->Add(template_url); 63 template_url_service->Add(template_url);
61 template_url_service->SetUserSelectedDefaultSearchProvider(template_url); 64 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
62 return template_url_service; 65 return template_url_service;
63 } 66 }
64 67
65 void CreateDefaultSearchContextAndRequestSearchTerm() { 68 void CreateDefaultSearchContextAndRequestSearchTerm() {
69 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
70 CreateSearchContextAndRequestSearchTerm("Barack Obama", surrounding, 0, 6);
71 }
72
73 void CreateSearchContextAndRequestSearchTerm(
74 const std::string& selected_text,
75 const base::string16& surrounding_text,
76 int start_offset,
77 int end_offset) {
66 test_context_ = new ContextualSearchContext( 78 test_context_ = new ContextualSearchContext(
67 "Barack Obama", true, GURL(kSomeSpecificBasePage), "utf-8"); 79 selected_text, true, GURL(kSomeSpecificBasePage), "utf-8");
68 // ContextualSearchDelegate class takes ownership of the context. 80 // ContextualSearchDelegate class takes ownership of the context.
69 delegate_->set_context_for_testing(test_context_); 81 delegate_->set_context_for_testing(test_context_);
70 RequestSearchTerm(); 82
83 test_context_->start_offset = start_offset;
84 test_context_->end_offset = end_offset;
85 test_context_->surrounding_text = surrounding_text;
86 delegate_->ContinueSearchTermResolutionRequest();
87 fetcher_ = test_factory_.GetFetcherByID(
88 ContextualSearchDelegate::kContextualSearchURLFetcherID);
89 ASSERT_TRUE(fetcher_);
90 ASSERT_TRUE(fetcher());
91 }
92
93 void SetResponseStringAndFetch(const std::string& selected_text,
94 const std::string& mentions_start,
95 const std::string& mentions_end) {
96 fetcher()->set_response_code(200);
97 fetcher()->SetResponseString(
98 ")]}'\n"
99 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
100 "\"info_text\":\"44th U.S. President\","
101 "\"display_text\":\"Barack Obama\","
102 "\"mentions\":[" + mentions_start + ","+ mentions_end + "],"
103 "\"selected_text\":\"" + selected_text + "\","
104 "\"resolved_term\":\"barack obama\"}");
105 fetcher()->delegate()->OnURLFetchComplete(fetcher());
71 } 106 }
72 107
73 void SetSurroundingContext(const base::string16& surrounding_text, 108 void SetSurroundingContext(const base::string16& surrounding_text,
74 int start_offset, 109 int start_offset,
75 int end_offset) { 110 int end_offset) {
76 test_context_ = new ContextualSearchContext( 111 test_context_ = new ContextualSearchContext(
77 "Bogus", true, GURL(kSomeSpecificBasePage), "utf-8"); 112 "Bogus", true, GURL(kSomeSpecificBasePage), "utf-8");
78 test_context_->surrounding_text = surrounding_text; 113 test_context_->surrounding_text = surrounding_text;
79 test_context_->start_offset = start_offset; 114 test_context_->start_offset = start_offset;
80 test_context_->end_offset = end_offset; 115 test_context_->end_offset = end_offset;
81 // ContextualSearchDelegate class takes ownership of the context. 116 // ContextualSearchDelegate class takes ownership of the context.
82 delegate_->set_context_for_testing(test_context_); 117 delegate_->set_context_for_testing(test_context_);
83 } 118 }
84 119
85 void RequestSearchTerm() {
86 test_context_->start_offset = 0;
87 test_context_->end_offset = 6;
88 test_context_->surrounding_text =
89 base::UTF8ToUTF16("Barack Obama just spoke.");
90 delegate_->ContinueSearchTermResolutionRequest();
91 fetcher_ = test_factory_.GetFetcherByID(
92 ContextualSearchDelegate::kContextualSearchURLFetcherID);
93 ASSERT_TRUE(fetcher_);
94 ASSERT_TRUE(fetcher());
95 }
96
97 bool DoesRequestContainOurSpecificBasePage() { 120 bool DoesRequestContainOurSpecificBasePage() {
98 return fetcher()->GetOriginalURL().spec().find( 121 return fetcher()->GetOriginalURL().spec().find(
99 specific_base_page_URL_escaped()) != std::string::npos; 122 specific_base_page_URL_escaped()) != std::string::npos;
100 } 123 }
101 124
102 std::string specific_base_page_URL_escaped() { 125 std::string specific_base_page_URL_escaped() {
103 return net::EscapeQueryParamValue(kSomeSpecificBasePage, true); 126 return net::EscapeQueryParamValue(kSomeSpecificBasePage, true);
104 } 127 }
105 128
106 net::TestURLFetcher* fetcher() { return fetcher_; } 129 net::TestURLFetcher* fetcher() { return fetcher_; }
107 bool is_invalid() { return is_invalid_; } 130 bool is_invalid() { return is_invalid_; }
108 int response_code() { return response_code_; } 131 int response_code() { return response_code_; }
109 std::string search_term() { return search_term_; } 132 std::string search_term() { return search_term_; }
110 std::string display_text() { return display_text_; } 133 std::string display_text() { return display_text_; }
111 std::string alternate_term() { return alternate_term_; } 134 std::string alternate_term() { return alternate_term_; }
112 bool do_prevent_preload() { return prevent_preload_; } 135 bool do_prevent_preload() { return prevent_preload_; }
113 std::string before_text() { return before_text_; } 136 std::string before_text() { return before_text_; }
114 std::string after_text() { return after_text_; } 137 std::string after_text() { return after_text_; }
138 int start_adjust() { return start_adjust_; }
139 int end_adjust() { return end_adjust_; }
115 140
116 // The delegate under test. 141 // The delegate under test.
117 scoped_ptr<ContextualSearchDelegate> delegate_; 142 scoped_ptr<ContextualSearchDelegate> delegate_;
118 143
119 private: 144 private:
120 void recordSearchTermResolutionResponse(bool is_invalid, 145 void recordSearchTermResolutionResponse(bool is_invalid,
121 int response_code, 146 int response_code,
122 const std::string& search_term, 147 const std::string& search_term,
123 const std::string& display_text, 148 const std::string& display_text,
124 const std::string& alternate_term, 149 const std::string& alternate_term,
125 bool prevent_preload) { 150 bool prevent_preload,
151 int start_adjust,
152 int end_adjust) {
126 is_invalid_ = is_invalid; 153 is_invalid_ = is_invalid;
127 response_code_ = response_code; 154 response_code_ = response_code;
128 search_term_ = search_term; 155 search_term_ = search_term;
129 display_text_ = display_text; 156 display_text_ = display_text;
130 alternate_term_ = alternate_term; 157 alternate_term_ = alternate_term;
131 prevent_preload_ = prevent_preload; 158 prevent_preload_ = prevent_preload;
159 start_adjust_ = start_adjust;
160 end_adjust_ = end_adjust;
132 } 161 }
133 162
134 void recordSurroundingText(const std::string& before_text, 163 void recordSurroundingText(const std::string& before_text,
135 const std::string& after_text) { 164 const std::string& after_text) {
136 before_text_ = before_text; 165 before_text_ = before_text;
137 after_text_ = after_text; 166 after_text_ = after_text;
138 } 167 }
139 168
140 void recordIcingSelectionAvailable(const std::string& encoding, 169 void recordIcingSelectionAvailable(const std::string& encoding,
141 const base::string16& surrounding_text, 170 const base::string16& surrounding_text,
142 size_t start_offset, 171 size_t start_offset,
143 size_t end_offset) { 172 size_t end_offset) {
144 // unused. 173 // unused.
145 } 174 }
146 175
147 bool is_invalid_; 176 bool is_invalid_;
148 int response_code_; 177 int response_code_;
149 std::string search_term_; 178 std::string search_term_;
150 std::string display_text_; 179 std::string display_text_;
151 std::string alternate_term_; 180 std::string alternate_term_;
152 bool prevent_preload_; 181 bool prevent_preload_;
182 int start_adjust_;
183 int end_adjust_;
153 std::string before_text_; 184 std::string before_text_;
154 std::string after_text_; 185 std::string after_text_;
155 186
156 base::MessageLoopForIO io_message_loop_; 187 base::MessageLoopForIO io_message_loop_;
157 net::TestURLFetcherFactory test_factory_; 188 net::TestURLFetcherFactory test_factory_;
158 net::TestURLFetcher* fetcher_; 189 net::TestURLFetcher* fetcher_;
159 scoped_ptr<TemplateURLService> template_url_service_; 190 scoped_ptr<TemplateURLService> template_url_service_;
160 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 191 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
161 192
162 // Will be owned by the delegate. 193 // Will be owned by the delegate.
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 281
251 TEST_F(ContextualSearchDelegateTest, InvalidResponse) { 282 TEST_F(ContextualSearchDelegateTest, InvalidResponse) {
252 CreateDefaultSearchContextAndRequestSearchTerm(); 283 CreateDefaultSearchContextAndRequestSearchTerm();
253 fetcher()->set_response_code(net::URLFetcher::RESPONSE_CODE_INVALID); 284 fetcher()->set_response_code(net::URLFetcher::RESPONSE_CODE_INVALID);
254 fetcher()->delegate()->OnURLFetchComplete(fetcher()); 285 fetcher()->delegate()->OnURLFetchComplete(fetcher());
255 286
256 EXPECT_FALSE(do_prevent_preload()); 287 EXPECT_FALSE(do_prevent_preload());
257 EXPECT_TRUE(is_invalid()); 288 EXPECT_TRUE(is_invalid());
258 } 289 }
259 290
291 TEST_F(ContextualSearchDelegateTest, ExpandSelectionToEnd) {
292 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
293 std::string selected_text = "Barack";
294 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding, 0, 6);
295 SetResponseStringAndFetch(selected_text, "0", "12");
296
297 EXPECT_EQ(0, start_adjust());
298 EXPECT_EQ(6, end_adjust());
299 }
300
301 TEST_F(ContextualSearchDelegateTest, ExpandSelectionToStart) {
302 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
303 std::string selected_text = "Obama";
304 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding, 7, 12);
305 SetResponseStringAndFetch(selected_text, "0", "12");
306
307 EXPECT_EQ(-7, start_adjust());
308 EXPECT_EQ(0, end_adjust());
309 }
310
311 TEST_F(ContextualSearchDelegateTest, ExpandSelectionBothDirections) {
312 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
313 std::string selected_text = "Ob";
314 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding, 7, 9);
315 SetResponseStringAndFetch(selected_text, "0", "12");
316
317 EXPECT_EQ(-7, start_adjust());
318 EXPECT_EQ(3, end_adjust());
319 }
320
321 TEST_F(ContextualSearchDelegateTest, ExpandSelectionInvalidRange) {
322 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
323 std::string selected_text = "Ob";
324 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding, 7, 9);
325 SetResponseStringAndFetch(selected_text, "0", "200");
326
327 EXPECT_EQ(0, start_adjust());
328 EXPECT_EQ(0, end_adjust());
329 }
330
331 TEST_F(ContextualSearchDelegateTest, ExpandSelectionInvalidDistantStart) {
332 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
333 std::string selected_text = "Ob";
334 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding,
335 0xffffffff, 0xffffffff - 2);
336 SetResponseStringAndFetch(selected_text, "0", "12");
337
338 EXPECT_EQ(0, start_adjust());
339 EXPECT_EQ(0, end_adjust());
340 }
341
342 TEST_F(ContextualSearchDelegateTest, ExpandSelectionInvalidNoOverlap) {
343 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
344 std::string selected_text = "Ob";
345 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding, 0, 12);
346 SetResponseStringAndFetch(selected_text, "12", "14");
347
348 EXPECT_EQ(0, start_adjust());
349 EXPECT_EQ(0, end_adjust());
350 }
351
352 TEST_F(ContextualSearchDelegateTest, ExpandSelectionInvalidDistantEndAndRange) {
353 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
354 std::string selected_text = "Ob";
355 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding,
356 0xffffffff, 0xffffffff - 2);
357 SetResponseStringAndFetch(selected_text, "0", "268435455");
358
359 EXPECT_EQ(0, start_adjust());
360 EXPECT_EQ(0, end_adjust());
361 }
362
363 TEST_F(ContextualSearchDelegateTest, ExpandSelectionLargeNumbers) {
364 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
365 std::string selected_text = "Ob";
366 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding,
367 268435450, 268435455);
368 SetResponseStringAndFetch(selected_text, "268435440", "268435455");
369
370 EXPECT_EQ(-10, start_adjust());
371 EXPECT_EQ(0, end_adjust());
372 }
373
374 TEST_F(ContextualSearchDelegateTest, ContractSelectionValid) {
375 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
376 std::string selected_text = "Barack Obama just";
377 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding, 0, 17);
378 SetResponseStringAndFetch(selected_text, "0", "12");
379
380 EXPECT_EQ(0, start_adjust());
381 EXPECT_EQ(-5, end_adjust());
382 }
383
384 TEST_F(ContextualSearchDelegateTest, ContractSelectionInvalid) {
385 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
386 std::string selected_text = "Barack Obama just";
387 CreateSearchContextAndRequestSearchTerm(selected_text, surrounding, 0, 17);
388 SetResponseStringAndFetch(selected_text, "5", "5");
389
390 EXPECT_EQ(0, start_adjust());
391 EXPECT_EQ(0, end_adjust());
392 }
393
260 TEST_F(ContextualSearchDelegateTest, SurroundingTextHighMaximum) { 394 TEST_F(ContextualSearchDelegateTest, SurroundingTextHighMaximum) {
261 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee"); 395 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee");
262 SetSurroundingContext(surrounding, 6, 11); 396 SetSurroundingContext(surrounding, 6, 11);
263 delegate_->SendSurroundingText(30); // High maximum # of surrounding chars. 397 delegate_->SendSurroundingText(30); // High maximum # of surrounding chars.
264 EXPECT_EQ("aa bb", before_text()); 398 EXPECT_EQ("aa bb", before_text());
265 EXPECT_EQ("dd ee", after_text()); 399 EXPECT_EQ("dd ee", after_text());
266 } 400 }
267 401
268 TEST_F(ContextualSearchDelegateTest, SurroundingTextLowMaximum) { 402 TEST_F(ContextualSearchDelegateTest, SurroundingTextLowMaximum) {
269 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee"); 403 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee");
270 SetSurroundingContext(surrounding, 6, 11); 404 SetSurroundingContext(surrounding, 6, 11);
271 delegate_->SendSurroundingText(3); // Low maximum # of surrounding chars. 405 delegate_->SendSurroundingText(3); // Low maximum # of surrounding chars.
272 // Whitespaces are trimmed. 406 // Whitespaces are trimmed.
273 EXPECT_EQ("bb", before_text()); 407 EXPECT_EQ("bb", before_text());
274 EXPECT_EQ("dd", after_text()); 408 EXPECT_EQ("dd", after_text());
275 } 409 }
276 410
277 TEST_F(ContextualSearchDelegateTest, SurroundingTextNoBeforeText) { 411 TEST_F(ContextualSearchDelegateTest, SurroundingTextNoBeforeText) {
278 base::string16 surrounding = base::ASCIIToUTF16("Bogus ee ff gg"); 412 base::string16 surrounding = base::ASCIIToUTF16("Bogus ee ff gg");
279 SetSurroundingContext(surrounding, 0, 5); 413 SetSurroundingContext(surrounding, 0, 5);
280 delegate_->SendSurroundingText(5); 414 delegate_->SendSurroundingText(5);
281 EXPECT_EQ("", before_text()); 415 EXPECT_EQ("", before_text());
282 EXPECT_EQ("ee f", after_text()); 416 EXPECT_EQ("ee f", after_text());
283 } 417 }
284 418
419 TEST_F(ContextualSearchDelegateTest, ExtractMentionsStartEnd) {
420 ListValue mentions_list;
421 mentions_list.AppendInteger(1);
422 mentions_list.AppendInteger(2);
423 int start = 0;
424 int end = 0;
425 delegate_->ExtractMentionsStartEnd(mentions_list, &start, &end);
426 EXPECT_EQ(1, start);
427 EXPECT_EQ(2, end);
428 }
429
285 TEST_F(ContextualSearchDelegateTest, SurroundingTextForIcing) { 430 TEST_F(ContextualSearchDelegateTest, SurroundingTextForIcing) {
286 base::string16 sample = base::ASCIIToUTF16("this is Barack Obama in office."); 431 base::string16 sample = base::ASCIIToUTF16("this is Barack Obama in office.");
287 int limit_each_side = 3; 432 int limit_each_side = 3;
288 size_t start = 8; 433 size_t start = 8;
289 size_t end = 20; 434 size_t end = 20;
290 base::string16 result = 435 base::string16 result =
291 delegate_->SurroundingTextForIcing(sample, limit_each_side, &start, &end); 436 delegate_->SurroundingTextForIcing(sample, limit_each_side, &start, &end);
292 EXPECT_EQ(static_cast<size_t>(3), start); 437 EXPECT_EQ(static_cast<size_t>(3), start);
293 EXPECT_EQ(static_cast<size_t>(15), end); 438 EXPECT_EQ(static_cast<size_t>(15), end);
294 EXPECT_EQ(base::ASCIIToUTF16("is Barack Obama in"), result); 439 EXPECT_EQ(base::ASCIIToUTF16("is Barack Obama in"), result);
(...skipping 15 matching lines...) Expand all
310 std::string json_with_escape = 455 std::string json_with_escape =
311 ")]}'\n" 456 ")]}'\n"
312 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," 457 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
313 "\"info_text\":\"44th U.S. President\"," 458 "\"info_text\":\"44th U.S. President\","
314 "\"display_text\":\"Barack Obama\", \"mentions\":[0,15]," 459 "\"display_text\":\"Barack Obama\", \"mentions\":[0,15],"
315 "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}"; 460 "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}";
316 std::string search_term; 461 std::string search_term;
317 std::string display_text; 462 std::string display_text;
318 std::string alternate_term; 463 std::string alternate_term;
319 std::string prevent_preload; 464 std::string prevent_preload;
465 int mention_start;
466 int mention_end;
320 delegate_->DecodeSearchTermsFromJsonResponse(json_with_escape, &search_term, 467 delegate_->DecodeSearchTermsFromJsonResponse(json_with_escape, &search_term,
321 &display_text, &alternate_term, 468 &display_text, &alternate_term,
322 &prevent_preload); 469 &prevent_preload, &mention_start,
470 &mention_end);
323 EXPECT_EQ("obama", search_term); 471 EXPECT_EQ("obama", search_term);
324 EXPECT_EQ("Barack Obama", display_text); 472 EXPECT_EQ("Barack Obama", display_text);
325 EXPECT_EQ("barack obama", alternate_term); 473 EXPECT_EQ("barack obama", alternate_term);
326 EXPECT_EQ("", prevent_preload); 474 EXPECT_EQ("", prevent_preload);
327 } 475 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698