| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); | 83 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
| 84 CreateSearchContextAndRequestSearchTerm("Barack Obama", surrounding, 0, 6); | 84 CreateSearchContextAndRequestSearchTerm("Barack Obama", surrounding, 0, 6); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void CreateSearchContextAndRequestSearchTerm( | 87 void CreateSearchContextAndRequestSearchTerm( |
| 88 const std::string& selected_text, | 88 const std::string& selected_text, |
| 89 const base::string16& surrounding_text, | 89 const base::string16& surrounding_text, |
| 90 int start_offset, | 90 int start_offset, |
| 91 int end_offset) { | 91 int end_offset) { |
| 92 test_context_ = new ContextualSearchContext( | 92 test_context_ = new ContextualSearchContext( |
| 93 selected_text, std::string(), GURL(kSomeSpecificBasePage), "utf-8"); | 93 std::string(), GURL(kSomeSpecificBasePage), "utf-8"); |
| 94 // ContextualSearchDelegate class takes ownership of the context. | 94 // ContextualSearchDelegate class takes ownership of the context. |
| 95 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); | 95 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); |
| 96 | 96 |
| 97 test_context_->SetSelectionSurroundings(start_offset, end_offset, | 97 test_context_->SetSelectionSurroundings(start_offset, end_offset, |
| 98 surrounding_text); | 98 surrounding_text); |
| 99 delegate_->ResolveSearchTermFromContext(); | 99 delegate_->ResolveSearchTermFromContext(); |
| 100 fetcher_ = test_factory_.GetFetcherByID( | 100 fetcher_ = test_factory_.GetFetcherByID( |
| 101 ContextualSearchDelegate::kContextualSearchURLFetcherID); | 101 ContextualSearchDelegate::kContextualSearchURLFetcherID); |
| 102 ASSERT_TRUE(fetcher_); | 102 ASSERT_TRUE(fetcher_); |
| 103 ASSERT_TRUE(fetcher()); | 103 ASSERT_TRUE(fetcher()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 124 EXPECT_EQ(200, response_code()); | 124 EXPECT_EQ(200, response_code()); |
| 125 EXPECT_EQ("obama", search_term()); | 125 EXPECT_EQ("obama", search_term()); |
| 126 } | 126 } |
| 127 | 127 |
| 128 //------------------------------------------------------------------- | 128 //------------------------------------------------------------------- |
| 129 // Helper methods that call private ContextualSearchDelegate methods. | 129 // Helper methods that call private ContextualSearchDelegate methods. |
| 130 // The ContextualSearchDelegate methods cannot be called directly | 130 // The ContextualSearchDelegate methods cannot be called directly |
| 131 // from tests, but can be called here because this is a friend class. | 131 // from tests, but can be called here because this is a friend class. |
| 132 //------------------------------------------------------------------- | 132 //------------------------------------------------------------------- |
| 133 void CreateTestContext() { | 133 void CreateTestContext() { |
| 134 test_context_ = | 134 test_context_ = new ContextualSearchContext( |
| 135 new ContextualSearchContext(std::string("word"), std::string(), | 135 std::string(), GURL(kSomeSpecificBasePage), "utf-8"); |
| 136 GURL(kSomeSpecificBasePage), "utf-8"); | |
| 137 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); | 136 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); |
| 138 } | 137 } |
| 139 | 138 |
| 140 void DestroyTestContext() { delete test_context_; } | 139 void DestroyTestContext() { delete test_context_; } |
| 141 | 140 |
| 142 // Call the OnTextSurroundingSelectionAvailable. | 141 // Call the OnTextSurroundingSelectionAvailable. |
| 143 // Cannot be in an actual test because OnTextSurroundingSelectionAvailable | 142 // Cannot be in an actual test because OnTextSurroundingSelectionAvailable |
| 144 // is private. | 143 // is private. |
| 145 void CallOnTextSurroundingSelectionAvailable() { | 144 void CallOnTextSurroundingSelectionAvailable() { |
| 146 delegate_->OnTextSurroundingSelectionAvailable(base::string16(), 1, 2); | 145 delegate_->OnTextSurroundingSelectionAvailable(base::string16(), 1, 2); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 169 "\"mentions\":[" + mentions_start + ","+ mentions_end + "]," | 168 "\"mentions\":[" + mentions_start + ","+ mentions_end + "]," |
| 170 "\"selected_text\":\"" + selected_text + "\"," | 169 "\"selected_text\":\"" + selected_text + "\"," |
| 171 "\"resolved_term\":\"barack obama\"}"); | 170 "\"resolved_term\":\"barack obama\"}"); |
| 172 fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 171 fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
| 173 } | 172 } |
| 174 | 173 |
| 175 void SetSurroundingContext(const base::string16& surrounding_text, | 174 void SetSurroundingContext(const base::string16& surrounding_text, |
| 176 int start_offset, | 175 int start_offset, |
| 177 int end_offset) { | 176 int end_offset) { |
| 178 test_context_ = new ContextualSearchContext( | 177 test_context_ = new ContextualSearchContext( |
| 179 "Bogus", std::string(), GURL(kSomeSpecificBasePage), "utf-8"); | 178 std::string(), GURL(kSomeSpecificBasePage), "utf-8"); |
| 180 test_context_->SetSelectionSurroundings(start_offset, end_offset, | 179 test_context_->SetSelectionSurroundings(start_offset, end_offset, |
| 181 surrounding_text); | 180 surrounding_text); |
| 182 // ContextualSearchDelegate class takes ownership of the context. | 181 // ContextualSearchDelegate class takes ownership of the context. |
| 183 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); | 182 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); |
| 184 } | 183 } |
| 185 | 184 |
| 186 // Gets the Client Discourse Context proto from the request header. | 185 // Gets the Client Discourse Context proto from the request header. |
| 187 discourse_context::ClientDiscourseContext GetDiscourseContextFromRequest() { | 186 discourse_context::ClientDiscourseContext GetDiscourseContextFromRequest() { |
| 188 discourse_context::ClientDiscourseContext cdc; | 187 discourse_context::ClientDiscourseContext cdc; |
| 189 // Make sure we can get the actual raw headers from the fake fetcher. | 188 // Make sure we can get the actual raw headers from the fake fetcher. |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 DestroyTestContext(); | 619 DestroyTestContext(); |
| 621 CallOnURLFetchComplete(); | 620 CallOnURLFetchComplete(); |
| 622 } | 621 } |
| 623 | 622 |
| 624 // Test that we can destroy the context while gathering surrounding text. | 623 // Test that we can destroy the context while gathering surrounding text. |
| 625 TEST_F(ContextualSearchDelegateTest, DestroyContextDuringGatherSurroundings) { | 624 TEST_F(ContextualSearchDelegateTest, DestroyContextDuringGatherSurroundings) { |
| 626 CreateTestContext(); | 625 CreateTestContext(); |
| 627 DestroyTestContext(); | 626 DestroyTestContext(); |
| 628 CallOnTextSurroundingSelectionAvailable(); | 627 CallOnTextSurroundingSelectionAvailable(); |
| 629 } | 628 } |
| OLD | NEW |