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

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: palmer's comments 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());
71 } 91 }
72 92
73 void SetSurroundingContext(const base::string16& surrounding_text, 93 void SetSurroundingContext(const base::string16& surrounding_text,
74 int start_offset, 94 int start_offset,
75 int end_offset) { 95 int end_offset) {
76 test_context_ = new ContextualSearchContext( 96 test_context_ = new ContextualSearchContext(
77 "Bogus", true, GURL(kSomeSpecificBasePage), "utf-8"); 97 "Bogus", true, GURL(kSomeSpecificBasePage), "utf-8");
78 test_context_->surrounding_text = surrounding_text; 98 test_context_->surrounding_text = surrounding_text;
79 test_context_->start_offset = start_offset; 99 test_context_->start_offset = start_offset;
80 test_context_->end_offset = end_offset; 100 test_context_->end_offset = end_offset;
81 // ContextualSearchDelegate class takes ownership of the context. 101 // ContextualSearchDelegate class takes ownership of the context.
82 delegate_->set_context_for_testing(test_context_); 102 delegate_->set_context_for_testing(test_context_);
83 } 103 }
84 104
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() { 105 bool DoesRequestContainOurSpecificBasePage() {
98 return fetcher()->GetOriginalURL().spec().find( 106 return fetcher()->GetOriginalURL().spec().find(
99 specific_base_page_URL_escaped()) != std::string::npos; 107 specific_base_page_URL_escaped()) != std::string::npos;
100 } 108 }
101 109
102 std::string specific_base_page_URL_escaped() { 110 std::string specific_base_page_URL_escaped() {
103 return net::EscapeQueryParamValue(kSomeSpecificBasePage, true); 111 return net::EscapeQueryParamValue(kSomeSpecificBasePage, true);
104 } 112 }
105 113
106 net::TestURLFetcher* fetcher() { return fetcher_; } 114 net::TestURLFetcher* fetcher() { return fetcher_; }
107 bool is_invalid() { return is_invalid_; } 115 bool is_invalid() { return is_invalid_; }
108 int response_code() { return response_code_; } 116 int response_code() { return response_code_; }
109 std::string search_term() { return search_term_; } 117 std::string search_term() { return search_term_; }
110 std::string display_text() { return display_text_; } 118 std::string display_text() { return display_text_; }
111 std::string alternate_term() { return alternate_term_; } 119 std::string alternate_term() { return alternate_term_; }
112 bool do_prevent_preload() { return prevent_preload_; } 120 bool do_prevent_preload() { return prevent_preload_; }
113 std::string before_text() { return before_text_; } 121 std::string before_text() { return before_text_; }
114 std::string after_text() { return after_text_; } 122 std::string after_text() { return after_text_; }
123 int start_adjust() { return start_adjust_; }
124 int end_adjust() { return end_adjust_; }
115 125
116 // The delegate under test. 126 // The delegate under test.
117 scoped_ptr<ContextualSearchDelegate> delegate_; 127 scoped_ptr<ContextualSearchDelegate> delegate_;
118 128
119 private: 129 private:
120 void recordSearchTermResolutionResponse(bool is_invalid, 130 void recordSearchTermResolutionResponse(bool is_invalid,
121 int response_code, 131 int response_code,
122 const std::string& search_term, 132 const std::string& search_term,
123 const std::string& display_text, 133 const std::string& display_text,
124 const std::string& alternate_term, 134 const std::string& alternate_term,
125 bool prevent_preload) { 135 bool prevent_preload,
136 int start_adjust,
137 int end_adjust) {
126 is_invalid_ = is_invalid; 138 is_invalid_ = is_invalid;
127 response_code_ = response_code; 139 response_code_ = response_code;
128 search_term_ = search_term; 140 search_term_ = search_term;
129 display_text_ = display_text; 141 display_text_ = display_text;
130 alternate_term_ = alternate_term; 142 alternate_term_ = alternate_term;
131 prevent_preload_ = prevent_preload; 143 prevent_preload_ = prevent_preload;
144 start_adjust_ = start_adjust;
145 end_adjust_ = end_adjust;
132 } 146 }
133 147
134 void recordSurroundingText(const std::string& before_text, 148 void recordSurroundingText(const std::string& before_text,
135 const std::string& after_text) { 149 const std::string& after_text) {
136 before_text_ = before_text; 150 before_text_ = before_text;
137 after_text_ = after_text; 151 after_text_ = after_text;
138 } 152 }
139 153
140 void recordIcingSelectionAvailable(const std::string& encoding, 154 void recordIcingSelectionAvailable(const std::string& encoding,
141 const base::string16& surrounding_text, 155 const base::string16& surrounding_text,
142 size_t start_offset, 156 size_t start_offset,
143 size_t end_offset) { 157 size_t end_offset) {
144 // unused. 158 // unused.
145 } 159 }
146 160
147 bool is_invalid_; 161 bool is_invalid_;
148 int response_code_; 162 int response_code_;
149 std::string search_term_; 163 std::string search_term_;
150 std::string display_text_; 164 std::string display_text_;
151 std::string alternate_term_; 165 std::string alternate_term_;
152 bool prevent_preload_; 166 bool prevent_preload_;
167 int start_adjust_;
168 int end_adjust_;
153 std::string before_text_; 169 std::string before_text_;
154 std::string after_text_; 170 std::string after_text_;
155 171
156 base::MessageLoopForIO io_message_loop_; 172 base::MessageLoopForIO io_message_loop_;
157 net::TestURLFetcherFactory test_factory_; 173 net::TestURLFetcherFactory test_factory_;
158 net::TestURLFetcher* fetcher_; 174 net::TestURLFetcher* fetcher_;
159 scoped_ptr<TemplateURLService> template_url_service_; 175 scoped_ptr<TemplateURLService> template_url_service_;
160 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 176 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
161 177
162 // Will be owned by the delegate. 178 // Will be owned by the delegate.
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 266
251 TEST_F(ContextualSearchDelegateTest, InvalidResponse) { 267 TEST_F(ContextualSearchDelegateTest, InvalidResponse) {
252 CreateDefaultSearchContextAndRequestSearchTerm(); 268 CreateDefaultSearchContextAndRequestSearchTerm();
253 fetcher()->set_response_code(net::URLFetcher::RESPONSE_CODE_INVALID); 269 fetcher()->set_response_code(net::URLFetcher::RESPONSE_CODE_INVALID);
254 fetcher()->delegate()->OnURLFetchComplete(fetcher()); 270 fetcher()->delegate()->OnURLFetchComplete(fetcher());
255 271
256 EXPECT_FALSE(do_prevent_preload()); 272 EXPECT_FALSE(do_prevent_preload());
257 EXPECT_TRUE(is_invalid()); 273 EXPECT_TRUE(is_invalid());
258 } 274 }
259 275
276 TEST_F(ContextualSearchDelegateTest, ExpandSelectionToEnd) {
277 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
278 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
279
280 fetcher()->set_response_code(200);
281 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
282 ")]}'\n"
283 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"barack\","
284 "\"info_text\":\"44th U.S. President\","
285 "\"display_text\":\"Barack Obama\", \"mentions\":[0,12],"
286 "\"selected_text\":\"barack\", \"resolved_term\":\"barack obama\"}");
287 fetcher()->delegate()->OnURLFetchComplete(fetcher());
288
289 EXPECT_EQ(0, start_adjust());
290 EXPECT_EQ(6, end_adjust());
291 }
292
293 TEST_F(ContextualSearchDelegateTest, ExpandSelectionToStart) {
294 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
295 CreateSearchContextAndRequestSearchTerm("Obama", surrounding, 7, 12);
296
297 fetcher()->set_response_code(200);
298 fetcher()->SetResponseString(
299 ")]}'\n"
300 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
301 "\"info_text\":\"44th U.S. President\","
302 "\"display_text\":\"Barack Obama\", \"mentions\":[0,12],"
303 "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}");
304 fetcher()->delegate()->OnURLFetchComplete(fetcher());
305
306 EXPECT_EQ(-7, start_adjust());
307 EXPECT_EQ(0, end_adjust());
308 }
309
310 TEST_F(ContextualSearchDelegateTest, ExpandSelectionBothDirections) {
311 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
312 CreateSearchContextAndRequestSearchTerm("Ob", surrounding, 7, 9);
313
314 fetcher()->set_response_code(200);
315 fetcher()->SetResponseString(
316 ")]}'\n"
317 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
318 "\"info_text\":\"44th U.S. President\","
319 "\"display_text\":\"Barack Obama\", \"mentions\":[0,12],"
320 "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}");
321 fetcher()->delegate()->OnURLFetchComplete(fetcher());
322
323 EXPECT_EQ(-7, start_adjust());
324 EXPECT_EQ(3, end_adjust());
325 }
326
327 TEST_F(ContextualSearchDelegateTest, ExpandSelectionInvalid) {
328 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
329 CreateSearchContextAndRequestSearchTerm("Ob", surrounding, 7, 9);
330
331 fetcher()->set_response_code(200);
332 fetcher()->SetResponseString(
333 ")]}'\n"
334 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
335 "\"info_text\":\"44th U.S. President\","
336 "\"display_text\":\"Barack Obama\", \"mentions\":[0,200],"
337 "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}");
338 fetcher()->delegate()->OnURLFetchComplete(fetcher());
339
340 EXPECT_EQ(0, start_adjust());
341 EXPECT_EQ(0, end_adjust());
342 }
343
344 TEST_F(ContextualSearchDelegateTest, ContractSelectionValid) {
345 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
346 CreateSearchContextAndRequestSearchTerm(
347 "Barack Obama just", surrounding, 0, 17);
348
349 fetcher()->set_response_code(200);
350 fetcher()->SetResponseString(
351 ")]}'\n"
352 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
353 "\"info_text\":\"44th U.S. President\","
354 "\"display_text\":\"Barack Obama\", \"mentions\":[0,12],"
355 "\"selected_text\":\"obama obama just\","
356 "\"resolved_term\":\"barack obama\"}");
357 fetcher()->delegate()->OnURLFetchComplete(fetcher());
358
359 EXPECT_EQ(0, start_adjust());
360 EXPECT_EQ(-5, end_adjust());
361 }
362
363 TEST_F(ContextualSearchDelegateTest, ContractSelectionInvalid) {
364 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke.");
365 CreateSearchContextAndRequestSearchTerm(
366 "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.
367
368 fetcher()->set_response_code(200);
369 fetcher()->SetResponseString(
370 ")]}'\n"
371 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
372 "\"info_text\":\"44th U.S. President\","
373 "\"display_text\":\"Barack Obama\", \"mentions\":[5,5],"
374 "\"selected_text\":\"obama obama just\","
375 "\"resolved_term\":\"barack obama\"}");
376 fetcher()->delegate()->OnURLFetchComplete(fetcher());
377
378 EXPECT_EQ(0, start_adjust());
379 EXPECT_EQ(0, end_adjust());
380 }
381
260 TEST_F(ContextualSearchDelegateTest, SurroundingTextHighMaximum) { 382 TEST_F(ContextualSearchDelegateTest, SurroundingTextHighMaximum) {
261 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee"); 383 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee");
262 SetSurroundingContext(surrounding, 6, 11); 384 SetSurroundingContext(surrounding, 6, 11);
263 delegate_->SendSurroundingText(30); // High maximum # of surrounding chars. 385 delegate_->SendSurroundingText(30); // High maximum # of surrounding chars.
264 EXPECT_EQ("aa bb", before_text()); 386 EXPECT_EQ("aa bb", before_text());
265 EXPECT_EQ("dd ee", after_text()); 387 EXPECT_EQ("dd ee", after_text());
266 } 388 }
267 389
268 TEST_F(ContextualSearchDelegateTest, SurroundingTextLowMaximum) { 390 TEST_F(ContextualSearchDelegateTest, SurroundingTextLowMaximum) {
269 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee"); 391 base::string16 surrounding = base::ASCIIToUTF16("aa bb Bogus dd ee");
270 SetSurroundingContext(surrounding, 6, 11); 392 SetSurroundingContext(surrounding, 6, 11);
271 delegate_->SendSurroundingText(3); // Low maximum # of surrounding chars. 393 delegate_->SendSurroundingText(3); // Low maximum # of surrounding chars.
272 // Whitespaces are trimmed. 394 // Whitespaces are trimmed.
273 EXPECT_EQ("bb", before_text()); 395 EXPECT_EQ("bb", before_text());
274 EXPECT_EQ("dd", after_text()); 396 EXPECT_EQ("dd", after_text());
275 } 397 }
276 398
277 TEST_F(ContextualSearchDelegateTest, SurroundingTextNoBeforeText) { 399 TEST_F(ContextualSearchDelegateTest, SurroundingTextNoBeforeText) {
278 base::string16 surrounding = base::ASCIIToUTF16("Bogus ee ff gg"); 400 base::string16 surrounding = base::ASCIIToUTF16("Bogus ee ff gg");
279 SetSurroundingContext(surrounding, 0, 5); 401 SetSurroundingContext(surrounding, 0, 5);
280 delegate_->SendSurroundingText(5); 402 delegate_->SendSurroundingText(5);
281 EXPECT_EQ("", before_text()); 403 EXPECT_EQ("", before_text());
282 EXPECT_EQ("ee f", after_text()); 404 EXPECT_EQ("ee f", after_text());
283 } 405 }
284 406
407 TEST_F(ContextualSearchDelegateTest, ExtractMentionsStartEnd) {
408 ListValue mentions_list;
409 mentions_list.AppendInteger(1);
410 mentions_list.AppendInteger(2);
411 int start = 0;
412 int end = 0;
413 delegate_->ExtractMentionsStartEnd(mentions_list, &start, &end);
414 EXPECT_EQ(1, start);
415 EXPECT_EQ(2, end);
416 }
417
285 TEST_F(ContextualSearchDelegateTest, SurroundingTextForIcing) { 418 TEST_F(ContextualSearchDelegateTest, SurroundingTextForIcing) {
286 base::string16 sample = base::ASCIIToUTF16("this is Barack Obama in office."); 419 base::string16 sample = base::ASCIIToUTF16("this is Barack Obama in office.");
287 int limit_each_side = 3; 420 int limit_each_side = 3;
288 size_t start = 8; 421 size_t start = 8;
289 size_t end = 20; 422 size_t end = 20;
290 base::string16 result = 423 base::string16 result =
291 delegate_->SurroundingTextForIcing(sample, limit_each_side, &start, &end); 424 delegate_->SurroundingTextForIcing(sample, limit_each_side, &start, &end);
292 EXPECT_EQ(static_cast<size_t>(3), start); 425 EXPECT_EQ(static_cast<size_t>(3), start);
293 EXPECT_EQ(static_cast<size_t>(15), end); 426 EXPECT_EQ(static_cast<size_t>(15), end);
294 EXPECT_EQ(base::ASCIIToUTF16("is Barack Obama in"), result); 427 EXPECT_EQ(base::ASCIIToUTF16("is Barack Obama in"), result);
(...skipping 15 matching lines...) Expand all
310 std::string json_with_escape = 443 std::string json_with_escape =
311 ")]}'\n" 444 ")]}'\n"
312 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," 445 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\","
313 "\"info_text\":\"44th U.S. President\"," 446 "\"info_text\":\"44th U.S. President\","
314 "\"display_text\":\"Barack Obama\", \"mentions\":[0,15]," 447 "\"display_text\":\"Barack Obama\", \"mentions\":[0,15],"
315 "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}"; 448 "\"selected_text\":\"obama\", \"resolved_term\":\"barack obama\"}";
316 std::string search_term; 449 std::string search_term;
317 std::string display_text; 450 std::string display_text;
318 std::string alternate_term; 451 std::string alternate_term;
319 std::string prevent_preload; 452 std::string prevent_preload;
453 int mention_start;
454 int mention_end;
320 delegate_->DecodeSearchTermsFromJsonResponse(json_with_escape, &search_term, 455 delegate_->DecodeSearchTermsFromJsonResponse(json_with_escape, &search_term,
321 &display_text, &alternate_term, 456 &display_text, &alternate_term,
322 &prevent_preload); 457 &prevent_preload, &mention_start,
458 &mention_end);
323 EXPECT_EQ("obama", search_term); 459 EXPECT_EQ("obama", search_term);
324 EXPECT_EQ("Barack Obama", display_text); 460 EXPECT_EQ("Barack Obama", display_text);
325 EXPECT_EQ("barack obama", alternate_term); 461 EXPECT_EQ("barack obama", alternate_term);
326 EXPECT_EQ("", prevent_preload); 462 EXPECT_EQ("", prevent_preload);
327 } 463 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698