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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); | 84 base::string16 surrounding = base::UTF8ToUTF16("Barack Obama just spoke."); |
85 CreateSearchContextAndRequestSearchTerm("Barack Obama", surrounding, 0, 6); | 85 CreateSearchContextAndRequestSearchTerm("Barack Obama", surrounding, 0, 6); |
86 } | 86 } |
87 | 87 |
88 void CreateSearchContextAndRequestSearchTerm( | 88 void CreateSearchContextAndRequestSearchTerm( |
89 const std::string& selected_text, | 89 const std::string& selected_text, |
90 const base::string16& surrounding_text, | 90 const base::string16& surrounding_text, |
91 int start_offset, | 91 int start_offset, |
92 int end_offset) { | 92 int end_offset) { |
93 test_context_ = new ContextualSearchContext( | 93 test_context_ = new ContextualSearchContext( |
94 selected_text, std::string(), GURL(kSomeSpecificBasePage), "utf-8"); | 94 std::string(), GURL(kSomeSpecificBasePage), "utf-8"); |
95 // ContextualSearchDelegate class takes ownership of the context. | 95 // ContextualSearchDelegate class takes ownership of the context. |
96 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); | 96 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); |
97 | 97 |
98 test_context_->SetSelectionSurroundings(start_offset, end_offset, | 98 test_context_->SetSelectionSurroundings(start_offset, end_offset, |
99 surrounding_text); | 99 surrounding_text); |
100 delegate_->ResolveSearchTermFromContext(); | 100 delegate_->ResolveSearchTermFromContext(); |
101 fetcher_ = test_factory_.GetFetcherByID( | 101 fetcher_ = test_factory_.GetFetcherByID( |
102 ContextualSearchDelegate::kContextualSearchURLFetcherID); | 102 ContextualSearchDelegate::kContextualSearchURLFetcherID); |
103 ASSERT_TRUE(fetcher_); | 103 ASSERT_TRUE(fetcher_); |
104 ASSERT_TRUE(fetcher()); | 104 ASSERT_TRUE(fetcher()); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 "\"mentions\":[" + mentions_start + ","+ mentions_end + "]," | 170 "\"mentions\":[" + mentions_start + ","+ mentions_end + "]," |
171 "\"selected_text\":\"" + selected_text + "\"," | 171 "\"selected_text\":\"" + selected_text + "\"," |
172 "\"resolved_term\":\"barack obama\"}"); | 172 "\"resolved_term\":\"barack obama\"}"); |
173 fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 173 fetcher()->delegate()->OnURLFetchComplete(fetcher()); |
174 } | 174 } |
175 | 175 |
176 void SetSurroundingContext(const base::string16& surrounding_text, | 176 void SetSurroundingContext(const base::string16& surrounding_text, |
177 int start_offset, | 177 int start_offset, |
178 int end_offset) { | 178 int end_offset) { |
179 test_context_ = new ContextualSearchContext( | 179 test_context_ = new ContextualSearchContext( |
180 "Bogus", std::string(), GURL(kSomeSpecificBasePage), "utf-8"); | 180 std::string(), GURL(kSomeSpecificBasePage), "utf-8"); |
181 test_context_->SetSelectionSurroundings(start_offset, end_offset, | 181 test_context_->SetSelectionSurroundings(start_offset, end_offset, |
182 surrounding_text); | 182 surrounding_text); |
183 // ContextualSearchDelegate class takes ownership of the context. | 183 // ContextualSearchDelegate class takes ownership of the context. |
184 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); | 184 delegate_->SetContextForTesting(test_context_->GetWeakPtr()); |
185 } | 185 } |
186 | 186 |
187 // Gets the Client Discourse Context proto from the request header. | 187 // Gets the Client Discourse Context proto from the request header. |
188 discourse_context::ClientDiscourseContext GetDiscourseContextFromRequest() { | 188 discourse_context::ClientDiscourseContext GetDiscourseContextFromRequest() { |
189 discourse_context::ClientDiscourseContext cdc; | 189 discourse_context::ClientDiscourseContext cdc; |
190 // Make sure we can get the actual raw headers from the fake fetcher. | 190 // Make sure we can get the actual raw headers from the fake fetcher. |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 DestroyTestContext(); | 641 DestroyTestContext(); |
642 CallOnURLFetchComplete(); | 642 CallOnURLFetchComplete(); |
643 } | 643 } |
644 | 644 |
645 // Test that we can destroy the context while gathering surrounding text. | 645 // Test that we can destroy the context while gathering surrounding text. |
646 TEST_F(ContextualSearchDelegateTest, DestroyContextDuringGatherSurroundings) { | 646 TEST_F(ContextualSearchDelegateTest, DestroyContextDuringGatherSurroundings) { |
647 CreateTestContext(); | 647 CreateTestContext(); |
648 DestroyTestContext(); | 648 DestroyTestContext(); |
649 CallOnTextSurroundingSelectionAvailable(); | 649 CallOnTextSurroundingSelectionAvailable(); |
650 } | 650 } |
OLD | NEW |