OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import <UIKit/UIKit.h> | 5 #import <UIKit/UIKit.h> |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/mac/scoped_nsobject.h" | |
12 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
14 #import "base/test/ios/wait_util.h" | 13 #import "base/test/ios/wait_util.h" |
15 #include "base/values.h" | 14 #include "base/values.h" |
16 #include "components/pref_registry/pref_registry_syncable.h" | 15 #include "components/pref_registry/pref_registry_syncable.h" |
17 #include "components/prefs/pref_registry_simple.h" | 16 #include "components/prefs/pref_registry_simple.h" |
18 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 17 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
19 #import "ios/chrome/browser/ui/contextual_search/contextual_search_controller.h" | 18 #import "ios/chrome/browser/ui/contextual_search/contextual_search_controller.h" |
20 #import "ios/chrome/browser/ui/contextual_search/js_contextual_search_manager.h" | 19 #import "ios/chrome/browser/ui/contextual_search/js_contextual_search_manager.h" |
21 #import "ios/chrome/browser/ui/contextual_search/touch_to_search_permissions_med
iator+testing.h" | 20 #import "ios/chrome/browser/ui/contextual_search/touch_to_search_permissions_med
iator+testing.h" |
22 #import "ios/chrome/browser/web/chrome_web_test.h" | 21 #import "ios/chrome/browser/web/chrome_web_test.h" |
23 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | 22 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" |
24 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 23 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
25 #import "ios/web/public/web_state/web_state.h" | 24 #import "ios/web/public/web_state/web_state.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "testing/gtest_mac.h" | 26 #include "testing/gtest_mac.h" |
28 #import "third_party/ocmock/OCMock/OCMock.h" | 27 #import "third_party/ocmock/OCMock/OCMock.h" |
29 | 28 |
| 29 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 30 #error "This file requires ARC support." |
| 31 #endif |
| 32 |
30 // Unit tests for the resources/contextualsearch.js JavaScript file. | 33 // Unit tests for the resources/contextualsearch.js JavaScript file. |
31 | 34 |
32 struct ContextualSearchStruct { | 35 struct ContextualSearchStruct { |
33 std::string url; | 36 std::string url; |
34 std::string selectedText; | 37 std::string selectedText; |
35 std::string surroundingText; | 38 std::string surroundingText; |
36 int offsetStart; | 39 int offsetStart; |
37 int offsetEnd; | 40 int offsetEnd; |
38 }; | 41 }; |
39 | 42 |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 longString = [longString stringByAppendingString:sentence]; | 219 longString = [longString stringByAppendingString:sentence]; |
217 if (newLine) { | 220 if (newLine) { |
218 longString = [longString stringByAppendingString:@"<br/>"]; | 221 longString = [longString stringByAppendingString:@"<br/>"]; |
219 } | 222 } |
220 } | 223 } |
221 return longString; | 224 return longString; |
222 } | 225 } |
223 | 226 |
224 void SetUp() override { | 227 void SetUp() override { |
225 ChromeWebTest::SetUp(); | 228 ChromeWebTest::SetUp(); |
226 mockDelegate_.reset([[OCMockObject | 229 mockDelegate_ = [OCMockObject |
227 niceMockForProtocol:@protocol(ContextualSearchControllerDelegate)] | 230 niceMockForProtocol:@protocol(ContextualSearchControllerDelegate)]; |
228 retain]); | |
229 jsUnittestsAdditions_ = static_cast<JsContextualSearchAdditionsManager*>( | 231 jsUnittestsAdditions_ = static_cast<JsContextualSearchAdditionsManager*>( |
230 [web_state()->GetJSInjectionReceiver() | 232 [web_state()->GetJSInjectionReceiver() |
231 instanceOfClass:[JsContextualSearchAdditionsManager class]]); | 233 instanceOfClass:[JsContextualSearchAdditionsManager class]]); |
232 TestChromeBrowserState::Builder test_cbs_builder; | 234 TestChromeBrowserState::Builder test_cbs_builder; |
233 chrome_browser_state_ = test_cbs_builder.Build(); | 235 chrome_browser_state_ = test_cbs_builder.Build(); |
234 controller_.reset([[ContextualSearchController alloc] | 236 controller_ = [[ContextualSearchController alloc] |
235 initWithBrowserState:chrome_browser_state_.get() | 237 initWithBrowserState:chrome_browser_state_.get() |
236 delegate:mockDelegate_]); | 238 delegate:mockDelegate_]; |
237 [controller_ | 239 [controller_ |
238 setPermissions:[[MockTouchToSearchPermissionsMediator alloc] | 240 setPermissions:[[MockTouchToSearchPermissionsMediator alloc] |
239 initWithBrowserState:chrome_browser_state_.get()]]; | 241 initWithBrowserState:chrome_browser_state_.get()]]; |
240 [controller_ setWebState:web_state()]; | 242 [controller_ setWebState:web_state()]; |
241 [controller_ enableContextualSearch:YES]; | 243 [controller_ enableContextualSearch:YES]; |
242 } | 244 } |
243 | 245 |
244 void TearDown() override { | 246 void TearDown() override { |
245 [controller_ close]; | 247 [controller_ close]; |
246 // Need to tear down the controller so it deregisters its JS handlers | 248 // Need to tear down the controller so it deregisters its JS handlers |
247 // before |webController_| is destroyed. | 249 // before |webController_| is destroyed. |
248 controller_.reset(); | 250 controller_ = nil; |
249 ChromeWebTest::TearDown(); | 251 ChromeWebTest::TearDown(); |
250 } | 252 } |
251 | 253 |
252 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 254 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
253 __unsafe_unretained JsContextualSearchAdditionsManager* jsUnittestsAdditions_; | 255 __unsafe_unretained JsContextualSearchAdditionsManager* jsUnittestsAdditions_; |
254 base::scoped_nsobject<ContextualSearchController> controller_; | 256 ContextualSearchController* controller_; |
255 base::scoped_nsobject<id> mockDelegate_; | 257 id mockDelegate_; |
256 base::scoped_nsobject<id> mockToolbarDelegate_; | 258 id mockToolbarDelegate_; |
257 }; | 259 }; |
258 | 260 |
259 // Test that ignored elements do not trigger CS when tapped. | 261 // Test that ignored elements do not trigger CS when tapped. |
260 TEST_F(ContextualSearchJsTest, TestIgnoreTapsOnElements) { | 262 TEST_F(ContextualSearchJsTest, TestIgnoreTapsOnElements) { |
261 LoadHtml(kHTMLSentenceWithLabel); | 263 LoadHtml(kHTMLSentenceWithLabel); |
262 ContextualSearchStruct searchContext; | 264 ContextualSearchStruct searchContext; |
263 ASSERT_FALSE(GetContextFromId(@"taphere", &searchContext)); | 265 ASSERT_FALSE(GetContextFromId(@"taphere", &searchContext)); |
264 }; | 266 }; |
265 | 267 |
266 // Test that ingnored element are not included in highlight or surrounding text. | 268 // Test that ingnored element are not included in highlight or surrounding text. |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 NSString* stringHTML = BuildLongTestString(0, 10, 5, true); | 486 NSString* stringHTML = BuildLongTestString(0, 10, 5, true); |
485 NSString* expectedHTML = BuildLongTestString(0, 10, -1, false); | 487 NSString* expectedHTML = BuildLongTestString(0, 10, -1, false); |
486 // LoadHtml will trim the last space. | 488 // LoadHtml will trim the last space. |
487 LoadHtml( | 489 LoadHtml( |
488 [NSString stringWithFormat:@"<html><body>%@</body></html>", stringHTML]); | 490 [NSString stringWithFormat:@"<html><body>%@</body></html>", stringHTML]); |
489 ContextualSearchStruct searchContext; | 491 ContextualSearchStruct searchContext; |
490 ASSERT_TRUE(GetContextFromId(@"taphere", &searchContext)); | 492 ASSERT_TRUE(GetContextFromId(@"taphere", &searchContext)); |
491 CheckContextOffsets(searchContext, base::SysNSStringToUTF8(expectedHTML)); | 493 CheckContextOffsets(searchContext, base::SysNSStringToUTF8(expectedHTML)); |
492 }; | 494 }; |
493 } // namespace | 495 } // namespace |
OLD | NEW |