OLD | NEW |
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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 } | 108 } |
109 | 109 |
110 // Allows using the vertical bar "|" as a quote character, which makes | 110 // Allows using the vertical bar "|" as a quote character, which makes |
111 // test cases more readable versus the escaped double quote that is otherwise | 111 // test cases more readable versus the escaped double quote that is otherwise |
112 // needed for JSON literals. | 112 // needed for JSON literals. |
113 std::string escapeBarQuoted(std::string bar_quoted) { | 113 std::string escapeBarQuoted(std::string bar_quoted) { |
114 std::replace(bar_quoted.begin(), bar_quoted.end(), '|', '\"'); | 114 std::replace(bar_quoted.begin(), bar_quoted.end(), '|', '\"'); |
115 return bar_quoted; | 115 return bar_quoted; |
116 } | 116 } |
117 | 117 |
118 void CreateDefaultSearchWithContextualCardsData( | 118 void CreateDefaultSearchWithAdditionalJsonData( |
119 const std::string contextual_cards_data) { | 119 const std::string additional_json_data) { |
120 CreateDefaultSearchContextAndRequestSearchTerm(); | 120 CreateDefaultSearchContextAndRequestSearchTerm(); |
121 fetcher()->set_response_code(200); | 121 fetcher()->set_response_code(200); |
122 std::string response = | 122 std::string response = |
123 escapeBarQuoted("{|search_term|:|obama|" + contextual_cards_data + "}"); | 123 escapeBarQuoted("{|search_term|:|obama|" + additional_json_data + "}"); |
124 fetcher()->SetResponseString(response); | 124 fetcher()->SetResponseString(response); |
125 fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 125 fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
126 | 126 |
127 EXPECT_FALSE(is_invalid()); | 127 EXPECT_FALSE(is_invalid()); |
128 EXPECT_EQ(200, response_code()); | 128 EXPECT_EQ(200, response_code()); |
129 EXPECT_EQ("obama", search_term()); | 129 EXPECT_EQ("obama", search_term()); |
130 } | 130 } |
131 | 131 |
132 void CreateDefaultSearchWithContextualCardsValue( | |
133 const std::string contextual_cards_value) { | |
134 CreateDefaultSearchWithContextualCardsData(", |contextual_cards|:" + | |
135 contextual_cards_value); | |
136 } | |
137 | |
138 void SetResponseStringAndFetch(const std::string& selected_text, | 132 void SetResponseStringAndFetch(const std::string& selected_text, |
139 const std::string& mentions_start, | 133 const std::string& mentions_start, |
140 const std::string& mentions_end) { | 134 const std::string& mentions_end) { |
141 fetcher()->set_response_code(200); | 135 fetcher()->set_response_code(200); |
142 fetcher()->SetResponseString( | 136 fetcher()->SetResponseString( |
143 ")]}'\n" | 137 ")]}'\n" |
144 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," | 138 "{\"mid\":\"/m/02mjmr\", \"search_term\":\"obama\"," |
145 "\"info_text\":\"44th U.S. President\"," | 139 "\"info_text\":\"44th U.S. President\"," |
146 "\"display_text\":\"Barack Obama\"," | 140 "\"display_text\":\"Barack Obama\"," |
147 "\"mentions\":[" + mentions_start + ","+ mentions_end + "]," | 141 "\"mentions\":[" + mentions_start + ","+ mentions_end + "]," |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 EXPECT_EQ("/m/02mjmr", mid()); | 571 EXPECT_EQ("/m/02mjmr", mid()); |
578 EXPECT_TRUE(do_prevent_preload()); | 572 EXPECT_TRUE(do_prevent_preload()); |
579 EXPECT_EQ("de", context_language()); | 573 EXPECT_EQ("de", context_language()); |
580 } | 574 } |
581 | 575 |
582 TEST_F(ContextualSearchDelegateTest, HeaderContainsBasePageUrl) { | 576 TEST_F(ContextualSearchDelegateTest, HeaderContainsBasePageUrl) { |
583 CreateDefaultSearchContextAndRequestSearchTerm(); | 577 CreateDefaultSearchContextAndRequestSearchTerm(); |
584 EXPECT_EQ(kSomeSpecificBasePage, getBasePageUrlFromRequest()); | 578 EXPECT_EQ(kSomeSpecificBasePage, getBasePageUrlFromRequest()); |
585 } | 579 } |
586 | 580 |
587 // Tests a response with a single card from Contextual Cards. | |
588 TEST_F(ContextualSearchDelegateTest, | |
589 ContextualCardsResponseSingleCardBarQuotedTest) { | |
590 CreateDefaultSearchWithContextualCardsValue( | |
591 "{|cards|:[{|singleCard|:{|subtitle|: |president|," | |
592 "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}]}"); | |
593 EXPECT_EQ("president", caption()); | |
594 EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); | |
595 } | |
596 | |
597 // Missing all Contextual Cards data. | 581 // Missing all Contextual Cards data. |
598 TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithNoData) { | 582 TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithNoData) { |
599 CreateDefaultSearchWithContextualCardsData(""); | 583 CreateDefaultSearchWithAdditionalJsonData(""); |
600 EXPECT_EQ("", caption()); | 584 EXPECT_EQ("", caption()); |
601 EXPECT_EQ("", thumbnail_url()); | 585 EXPECT_EQ("", thumbnail_url()); |
602 } | 586 } |
603 | 587 |
604 // Missing subtitle (for caption). | 588 // Test just the root level caption. |
605 TEST_F(ContextualSearchDelegateTest, | 589 TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithCaption) { |
606 ContextualCardsResponseWithMissingCaption) { | 590 CreateDefaultSearchWithAdditionalJsonData(", |caption|:|aCaption|"); |
607 CreateDefaultSearchWithContextualCardsValue( | 591 EXPECT_EQ("aCaption", caption()); |
608 "{|cards|:[{|singleCard|:{|stubtitlemisspelled|: |president|," | 592 EXPECT_EQ("", thumbnail_url()); |
609 "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}]}"); | 593 } |
| 594 |
| 595 // Test just the root level thumbnail. |
| 596 TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithThumbnail) { |
| 597 CreateDefaultSearchWithAdditionalJsonData( |
| 598 ", |thumbnail|:|https://t0.gstatic.com/images?q=tbn:ANd9|"); |
610 EXPECT_EQ("", caption()); | 599 EXPECT_EQ("", caption()); |
611 EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); | 600 EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); |
612 } | 601 } |
613 | |
614 // Missing the Thumbnail URI. | |
615 TEST_F(ContextualSearchDelegateTest, | |
616 ContextualCardsResponseWithMissingThumbnailUri) { | |
617 CreateDefaultSearchWithContextualCardsValue( | |
618 "{|cards|:[{|singleCard|:{|subtitle|: |president|," | |
619 "|thumbnail|:{}}}]}"); | |
620 EXPECT_EQ("president", caption()); | |
621 EXPECT_EQ("", thumbnail_url()); | |
622 } | |
623 | |
624 // Missing the whole Thumbnail. | |
625 TEST_F(ContextualSearchDelegateTest, | |
626 ContextualCardsResponseWithMissingThumbnail) { | |
627 CreateDefaultSearchWithContextualCardsValue( | |
628 "{|cards|:[{|singleCard|:{|subtitle|: |president|," | |
629 "|ignored key|:|ignored value|}}]}"); | |
630 EXPECT_EQ("president", caption()); | |
631 EXPECT_EQ("", thumbnail_url()); | |
632 } | |
633 | |
634 // Empty cards list. | |
635 TEST_F(ContextualSearchDelegateTest, | |
636 ContextualCardsResponseWithMissingSingleCard) { | |
637 CreateDefaultSearchWithContextualCardsValue("{|cards|:[]}"); | |
638 EXPECT_EQ("", caption()); | |
639 EXPECT_EQ("", thumbnail_url()); | |
640 } | |
641 | |
642 // Tests carouselCard followed by singleCard. | |
643 TEST_F(ContextualSearchDelegateTest, | |
644 ContextualCardsResponseWithSingleAndCarouselCards) { | |
645 CreateDefaultSearchWithContextualCardsValue( | |
646 "{|cards|:[{|carouselCard|:{}},{|singleCard|:{|subtitle|: |president|," | |
647 "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}]}"); | |
648 EXPECT_EQ("president", caption()); | |
649 EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); | |
650 } | |
651 | |
652 // Missing cards. | |
653 TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithMissingCards) { | |
654 CreateDefaultSearchWithContextualCardsValue("{}"); | |
655 EXPECT_EQ("", caption()); | |
656 EXPECT_EQ("", thumbnail_url()); | |
657 } | |
658 | |
659 // Multiple cards (latter should be ignored). | |
660 TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithMultipleCards) { | |
661 CreateDefaultSearchWithContextualCardsValue( | |
662 "{|cards|:[{|singleCard|:{|subtitle|: |president|," | |
663 "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}," | |
664 "{|singleCard|:{|subtitle|:|wrong subtitle|," | |
665 "|thumbnail|:{|uri|:|https://t0.gstatic.com/wrongThumbnail|}}}]}"); | |
666 EXPECT_EQ("president", caption()); | |
667 EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); | |
668 } | |
OLD | NEW |