OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import <Foundation/Foundation.h> | |
6 | |
7 #import "base/test/ios/wait_util.h" | |
8 #import "ios/chrome/browser/find_in_page/js_findinpage_manager.h" | |
9 #import "ios/chrome/browser/web/chrome_web_test.h" | |
10 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | |
11 #import "ios/web/public/web_state/web_state.h" | |
12 #import "testing/gtest_mac.h" | |
13 | |
14 // Test fixture to test Find In Page JS. | |
15 class JsFindinpageManagerTest : public ChromeWebTest { | |
16 protected: | |
17 // Loads the given HTML and initializes the findInPage JS scripts. | |
18 void LoadHtml(NSString* html) { | |
19 ChromeWebTest::LoadHtml(html); | |
20 manager_ = | |
21 static_cast<JsFindinpageManager*>([web_state()->GetJSInjectionReceiver() | |
22 instanceOfClass:[JsFindinpageManager class]]); | |
23 [manager_ inject]; | |
24 } | |
25 JsFindinpageManager* manager_; | |
26 }; | |
27 | |
28 // Tests that findString script reports a match when appropriate. | |
29 TEST_F(JsFindinpageManagerTest, FindInPageSucceeds) { | |
30 LoadHtml(@"<html><body><p>Target phrase</p></body></html>"); | |
31 __block BOOL completion_handler_block_was_called = NO; | |
32 id completion_handler_block = ^(BOOL success, CGPoint scrollPosition) { | |
33 ASSERT_TRUE(success); | |
34 // 'scrollPosition' is updated if the string is found. | |
35 EXPECT_NE(FLT_MAX, scrollPosition.x); | |
36 completion_handler_block_was_called = YES; | |
37 }; | |
38 [manager_ findString:@"Target phrase" | |
39 completionHandler:completion_handler_block]; | |
40 base::test::ios::WaitUntilCondition(^bool() { | |
41 return completion_handler_block_was_called; | |
42 }); | |
43 } | |
44 | |
45 // Tests that findString script does not report a match when appropriate. | |
46 TEST_F(JsFindinpageManagerTest, FindInPageFails) { | |
47 LoadHtml(@"<html><body><p>Target phrase</p></body></html>"); | |
48 __block BOOL completion_handler_block_was_called = NO; | |
49 id completion_handler_block = ^(BOOL success, CGPoint scrollPosition) { | |
50 // findString should return YES even if the target phrase is not found. | |
51 ASSERT_TRUE(success); | |
52 // 'point' is *not* updated if the string is not found. | |
53 EXPECT_TRUE(CGPointEqualToPoint(CGPointZero, scrollPosition)); | |
54 completion_handler_block_was_called = YES; | |
55 }; | |
56 [manager_ findString:@"Non-included phrase" | |
57 completionHandler:completion_handler_block]; | |
58 base::test::ios::WaitUntilCondition(^bool() { | |
59 return completion_handler_block_was_called; | |
60 }); | |
61 } | |
62 | |
63 // Attepting to break out of the script and inject new script fails. | |
64 TEST_F(JsFindinpageManagerTest, InjectionTest) { | |
65 LoadHtml(@"<html><body><p>Target phrase</p></body></html>"); | |
66 __block BOOL completion_handler_block_was_called = NO; | |
67 id completion_handler_block = ^(BOOL success, CGPoint scrollPosition) { | |
68 [manager_ executeJavaScript:@"token" | |
69 completionHandler:^(NSString* result, NSError*) { | |
70 EXPECT_NSNE(@YES, result); | |
71 completion_handler_block_was_called = YES; | |
72 }]; | |
73 }; | |
74 [manager_ findString:@"');token=true;('" | |
75 completionHandler:completion_handler_block]; | |
76 base::test::ios::WaitUntilCondition(^bool() { | |
77 return completion_handler_block_was_called; | |
78 }); | |
79 } | |
OLD | NEW |