Chromium Code Reviews| 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 3c37e34ba1de1d150c4b2181860e4ae5841a23df..c10c4447240e1435ab1164c701ba743b3a6a524f 100644 |
| --- a/chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc |
| +++ b/chrome/browser/android/contextualsearch/contextual_search_delegate_unittest.cc |
| @@ -6,7 +6,9 @@ |
| #include <stddef.h> |
| +#include <algorithm> |
| #include <memory> |
| +#include <string> |
| #include "base/base64.h" |
| #include "base/macros.h" |
| @@ -15,6 +17,7 @@ |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/values.h" |
| #include "chrome/browser/android/contextualsearch/contextual_search_context.h" |
| +#include "chrome/browser/android/contextualsearch/contextual_search_field_trial.h" |
| #include "chrome/browser/android/contextualsearch/resolved_search_term.h" |
| #include "chrome/browser/android/proto/client_discourse_context.pb.h" |
| #include "components/search_engines/template_url_service.h" |
| @@ -37,6 +40,12 @@ class ContextualSearchDelegateTest : public testing::Test { |
| ContextualSearchDelegateTest() {} |
| ~ContextualSearchDelegateTest() override {} |
| + // Inner class that enables Now on Tap Bar integration. |
| + class ContextualSearchFieldTrialStubbed : public ContextualSearchFieldTrial { |
| + public: |
| + bool IsNowOnTapBarIntegrationEnabled() override { return true; } |
|
Theresa
2016/09/20 19:51:40
I don't think this is necessary. There should be a
Donn Denman
2016/09/20 21:16:37
Oh, I feel bad that I didn't figure this out on my
|
| + }; |
| + |
| protected: |
| void SetUp() override { |
| request_context_ = |
| @@ -50,7 +59,9 @@ class ContextualSearchDelegateTest : public testing::Test { |
| base::Bind(&ContextualSearchDelegateTest::recordSurroundingText, |
| base::Unretained(this)), |
| base::Bind(&ContextualSearchDelegateTest::recordIcingSelectionAvailable, |
| - base::Unretained(this)))); |
| + base::Unretained(this)), |
| + std::unique_ptr<ContextualSearchFieldTrial>( |
| + new ContextualSearchFieldTrialStubbed()))); |
| } |
| void TearDown() override { |
| @@ -101,6 +112,34 @@ class ContextualSearchDelegateTest : public testing::Test { |
| ASSERT_TRUE(fetcher()); |
| } |
| + // Allows using the vertical bar "|" as a quote character, which makes |
| + // test cases more readable versus the escaped double quote that is otherwise |
| + // needed for JSON literals. |
| + std::string escapeBarQuoted(std::string bar_quoted) { |
| + std::replace(bar_quoted.begin(), bar_quoted.end(), '|', '\"'); |
| + return bar_quoted; |
| + } |
| + |
| + void CreateDefaultSearchWithContextualCardsData( |
| + const std::string contextual_cards_data) { |
| + CreateDefaultSearchContextAndRequestSearchTerm(); |
| + fetcher()->set_response_code(200); |
| + std::string response = |
| + escapeBarQuoted("{|search_term|:|obama|" + contextual_cards_data + "}"); |
| + fetcher()->SetResponseString(response); |
| + fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
| + |
| + EXPECT_FALSE(is_invalid()); |
| + EXPECT_EQ(200, response_code()); |
| + EXPECT_EQ("obama", search_term()); |
| + } |
| + |
| + void CreateDefaultSearchWithContextualCardsValue( |
| + const std::string contextual_cards_value) { |
| + CreateDefaultSearchWithContextualCardsData(", |contextual_cards|:" + |
| + contextual_cards_value); |
| + } |
| + |
| void SetResponseStringAndFetch(const std::string& selected_text, |
| const std::string& mentions_start, |
| const std::string& mentions_end) { |
| @@ -172,6 +211,8 @@ class ContextualSearchDelegateTest : public testing::Test { |
| std::string display_text() { return display_text_; } |
| std::string alternate_term() { return alternate_term_; } |
| std::string mid() { return mid_; } |
| + std::string caption() { return caption_; } |
| + std::string thumbnail_url() { return thumbnail_url_; } |
| bool do_prevent_preload() { return prevent_preload_; } |
| std::string after_text() { return after_text_; } |
| int start_adjust() { return start_adjust_; } |
| @@ -190,6 +231,8 @@ class ContextualSearchDelegateTest : public testing::Test { |
| display_text_ = resolved_search_term.display_text; |
| alternate_term_ = resolved_search_term.alternate_term; |
| mid_ = resolved_search_term.mid; |
| + thumbnail_url_ = resolved_search_term.thumbnail_url; |
| + caption_ = resolved_search_term.caption; |
| prevent_preload_ = resolved_search_term.prevent_preload; |
| start_adjust_ = resolved_search_term.selection_start_adjust; |
| end_adjust_ = resolved_search_term.selection_end_adjust; |
| @@ -213,6 +256,8 @@ class ContextualSearchDelegateTest : public testing::Test { |
| std::string display_text_; |
| std::string alternate_term_; |
| std::string mid_; |
| + std::string thumbnail_url_; |
| + std::string caption_; |
| bool prevent_preload_; |
| int start_adjust_; |
| int end_adjust_; |
| @@ -543,3 +588,105 @@ TEST_F(ContextualSearchDelegateTest, HeaderContainsBasePageUrl) { |
| CreateDefaultSearchContextAndRequestSearchTerm(); |
| EXPECT_EQ(kSomeSpecificBasePage, getBasePageUrlFromRequest()); |
| } |
| + |
| +// Tests a response with Now on Tap data. |
| +TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseSingleCard) { |
| + CreateDefaultSearchContextAndRequestSearchTerm(); |
| + fetcher()->set_response_code(200); |
| + fetcher()->SetResponseString( |
| + "{\"search_term\":\"obama\"," |
|
Theresa
2016/09/20 19:51:40
Can we use escapeBarQuoted() here too?
Donn Denman
2016/09/20 21:16:37
I'll just remove this one.
|
| + "\"contextual_cards\":{\"cards\":[{\"singleCard\":{ " |
| + "\"subtitle\": \"president\", " |
| + "\"thumbnail\":{\"uri\":\"https://t0.gstatic.com/images?q=tbn:ANd9\"}" |
| + "}}]}}"); |
| + fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
| + |
| + EXPECT_FALSE(is_invalid()); |
| + EXPECT_EQ(200, response_code()); |
| + EXPECT_EQ("obama", search_term()); |
| + EXPECT_EQ("president", caption()); |
| + EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); |
| +} |
| + |
| +// Same test using helpers and "|" quoting (for readability). |
|
Theresa
2016/09/20 19:51:40
Does duplicating the test give us any extra code c
Donn Denman
2016/09/20 21:16:37
Done.
|
| +TEST_F(ContextualSearchDelegateTest, |
| + ContextualCardsResponseSingleCardBarQuotedTest) { |
| + CreateDefaultSearchWithContextualCardsValue( |
| + "{|cards|:[{|singleCard|:{|subtitle|: |president|," |
| + "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}]}"); |
| + EXPECT_EQ("president", caption()); |
| + EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); |
| +} |
| + |
| +// Missing all Now on Tap data. |
| +TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithNoData) { |
| + CreateDefaultSearchWithContextualCardsData(""); |
| + EXPECT_EQ("", caption()); |
| + EXPECT_EQ("", thumbnail_url()); |
| +} |
| + |
| +// Missing subtitle (for caption). |
| +TEST_F(ContextualSearchDelegateTest, |
| + ContextualCardsResponseWithMissingCaption) { |
| + CreateDefaultSearchWithContextualCardsValue( |
| + "{|cards|:[{|singleCard|:{|stubtitlemisspelled|: |president|," |
| + "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}]}"); |
| + EXPECT_EQ("", caption()); |
| + EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); |
| +} |
| + |
| +// Missing the Thumbnail URI. |
| +TEST_F(ContextualSearchDelegateTest, |
| + ContextualCardsResponseWithMissingThumbnailUri) { |
| + CreateDefaultSearchWithContextualCardsValue( |
| + "{|cards|:[{|singleCard|:{|subtitle|: |president|," |
| + "|thumbnail|:{}}}]}"); |
| + EXPECT_EQ("president", caption()); |
| + EXPECT_EQ("", thumbnail_url()); |
| +} |
| + |
| +// Missing the whole Thumbnail. |
| +TEST_F(ContextualSearchDelegateTest, |
| + ContextualCardsResponseWithMissingThumbnail) { |
| + CreateDefaultSearchWithContextualCardsValue( |
| + "{|cards|:[{|singleCard|:{|subtitle|: |president|," |
| + "|ignored key|:|ignored value|}}]}"); |
| + EXPECT_EQ("president", caption()); |
| + EXPECT_EQ("", thumbnail_url()); |
| +} |
| + |
| +// Empty cards list. |
| +TEST_F(ContextualSearchDelegateTest, |
| + ContextualCardsResponseWithMissingSingleCard) { |
| + CreateDefaultSearchWithContextualCardsValue("{|cards|:[]}"); |
| + EXPECT_EQ("", caption()); |
| + EXPECT_EQ("", thumbnail_url()); |
| +} |
| + |
| +// Tests carouselCard followed by singleCard. |
| +TEST_F(ContextualSearchDelegateTest, |
| + ContextualCardsResponseWithSingleAndCarouselCards) { |
| + CreateDefaultSearchWithContextualCardsValue( |
| + "{|cards|:[{|carouselCard|:{}},{|singleCard|:{|subtitle|: |president|," |
| + "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}]}"); |
| + EXPECT_EQ("president", caption()); |
| + EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); |
| +} |
| + |
| +// Missing cards. |
| +TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithMissingCards) { |
| + CreateDefaultSearchWithContextualCardsValue("{}"); |
| + EXPECT_EQ("", caption()); |
| + EXPECT_EQ("", thumbnail_url()); |
| +} |
| + |
| +// Multiple cards (latter should be ignored). |
| +TEST_F(ContextualSearchDelegateTest, ContextualCardsResponseWithMultipleCards) { |
| + CreateDefaultSearchWithContextualCardsValue( |
| + "{|cards|:[{|singleCard|:{|subtitle|: |president|," |
| + "|thumbnail|:{|uri|:|https://t0.gstatic.com/images?q=tbn:ANd9|}}}," |
| + "{|singleCard|:{|subtitle|:|wrong subtitle|," |
| + "|thumbnail|:{|uri|:|https://t0.gstatic.com/wrongThumbnail|}}}]}"); |
| + EXPECT_EQ("president", caption()); |
| + EXPECT_EQ("https://t0.gstatic.com/images?q=tbn:ANd9", thumbnail_url()); |
| +} |