OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "base/mac/foundation_util.h" |
7 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
8 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
9 #import "ios/chrome/browser/find_in_page/find_in_page_controller.h" | 10 #import "ios/chrome/browser/find_in_page/find_in_page_model.h" |
| 11 #import "ios/chrome/browser/find_in_page/js_findinpage_manager.h" |
10 #import "ios/chrome/browser/web/chrome_web_test.h" | 12 #import "ios/chrome/browser/web/chrome_web_test.h" |
| 13 #import "ios/web/public/web_state/crw_web_view_proxy.h" |
| 14 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
11 #import "ios/web/public/web_state/web_state.h" | 15 #import "ios/web/public/web_state/web_state.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/gtest_mac.h" | 17 #include "testing/gtest_mac.h" |
14 #import "third_party/ocmock/OCMock/OCMock.h" | |
15 | 18 |
16 // Unit tests for the models/resources/find_in_page.js JavaScript file. | 19 // Unit tests for the find_in_page.js JavaScript file. |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 // JavaScript invocation to search for 'foo' (for 1000 milliseconds). | 23 // JavaScript invocation to search for 'foo' (for 1000 milliseconds). |
21 NSString* kJavaScriptToSearchForFoo = | 24 NSString* kJavaScriptToSearchForFoo = |
22 @"__gCrWeb.findInPage.highlightWord('foo', false, 1000)"; | 25 @"__gCrWeb.findInPage.highlightWord('foo', false, 1000)"; |
23 | 26 |
24 // Other JavaScript functions invoked by the tests. | 27 // Other JavaScript functions invoked by the tests. |
25 NSString* kJavaScriptIncrementIndex = @"__gCrWeb.findInPage.incrementIndex()"; | 28 NSString* kJavaScriptIncrementIndex = @"__gCrWeb.findInPage.incrementIndex()"; |
26 NSString* kJavaScriptDecrementIndex = @"__gCrWeb.findInPage.decrementIndex()"; | 29 NSString* kJavaScriptDecrementIndex = @"__gCrWeb.findInPage.decrementIndex()"; |
(...skipping 29 matching lines...) Expand all Loading... |
56 " <span style='display:none'>foo</span>" | 59 " <span style='display:none'>foo</span>" |
57 " <span style='display:none'>foo</span>" | 60 " <span style='display:none'>foo</span>" |
58 "</body></html>"; | 61 "</body></html>"; |
59 | 62 |
60 // Test fixture to test Find In Page JS. | 63 // Test fixture to test Find In Page JS. |
61 class FindInPageJsTest : public ChromeWebTest { | 64 class FindInPageJsTest : public ChromeWebTest { |
62 public: | 65 public: |
63 // Loads the given HTML, then loads the |findInPage| JavaScript. | 66 // Loads the given HTML, then loads the |findInPage| JavaScript. |
64 void LoadHtml(NSString* html) { | 67 void LoadHtml(NSString* html) { |
65 ChromeWebTest::LoadHtml(html); | 68 ChromeWebTest::LoadHtml(html); |
66 [findInPageController_ initFindInPage]; | 69 |
| 70 // Inject and initialize the find in page javascript. |
| 71 [findInPageJsManager_ inject]; |
| 72 CGRect frame = [web_state()->GetWebViewProxy() bounds]; |
| 73 [findInPageJsManager_ setWidth:frame.size.width height:frame.size.height]; |
67 } | 74 } |
68 | 75 |
69 // Runs the given JavaScript and asserts that the result matches the given | 76 // Runs the given JavaScript and asserts that the result matches the given |
70 // |expected_value|. | 77 // |expected_value|. |
71 void AssertJavaScriptValue(NSString* script, int expected_value) { | 78 void AssertJavaScriptValue(NSString* script, int expected_value) { |
72 id result = ExecuteJavaScript(script); | 79 id result = ExecuteJavaScript(script); |
73 EXPECT_TRUE(result) << " in script: " << base::SysNSStringToUTF8(script); | 80 EXPECT_TRUE(result) << " in script: " << base::SysNSStringToUTF8(script); |
74 EXPECT_EQ(expected_value, [result intValue]) | 81 EXPECT_EQ(expected_value, [result intValue]) |
75 << " in script: " << base::SysNSStringToUTF8(script); | 82 << " in script: " << base::SysNSStringToUTF8(script); |
76 } | 83 } |
(...skipping 10 matching lines...) Expand all Loading... |
87 | 94 |
88 // Search for 'foo'. Performing the search sets the index to point to the | 95 // Search for 'foo'. Performing the search sets the index to point to the |
89 // first visible occurence of 'foo'. | 96 // first visible occurence of 'foo'. |
90 ExecuteJavaScript(kJavaScriptToSearchForFoo); | 97 ExecuteJavaScript(kJavaScriptToSearchForFoo); |
91 AssertJavaScriptValue(kJavaScriptIndex, 1); | 98 AssertJavaScriptValue(kJavaScriptIndex, 1); |
92 AssertJavaScriptValue(kJavaScriptSpansLength, kNumberOfFoosInHtml); | 99 AssertJavaScriptValue(kJavaScriptSpansLength, kNumberOfFoosInHtml); |
93 } | 100 } |
94 | 101 |
95 void SetUp() override { | 102 void SetUp() override { |
96 ChromeWebTest::SetUp(); | 103 ChromeWebTest::SetUp(); |
97 mockDelegate_.reset([[OCMockObject | 104 findInPageModel_.reset([[FindInPageModel alloc] init]); |
98 niceMockForProtocol:@protocol(FindInPageControllerDelegate)] retain]); | 105 findInPageJsManager_.reset([base::mac::ObjCCastStrict<JsFindinpageManager>( |
99 findInPageController_.reset([[FindInPageController alloc] | 106 [web_state()->GetJSInjectionReceiver() |
100 initWithWebState:web_state() | 107 instanceOfClass:[JsFindinpageManager class]]) retain]); |
101 delegate:mockDelegate_]); | 108 findInPageJsManager_.get().findInPageModel = findInPageModel_; |
102 } | 109 } |
103 | 110 |
104 base::scoped_nsobject<FindInPageController> findInPageController_; | 111 base::scoped_nsobject<FindInPageModel> findInPageModel_; |
105 base::scoped_nsobject<id> mockDelegate_; | 112 base::scoped_nsobject<JsFindinpageManager> findInPageJsManager_; |
106 }; | 113 }; |
107 | 114 |
108 // Performs a search, then calls |incrementIndex| to loop through the | 115 // Performs a search, then calls |incrementIndex| to loop through the |
109 // matches, ensuring that when the end is reached the index wraps back to zero. | 116 // matches, ensuring that when the end is reached the index wraps back to zero. |
110 TEST_F(FindInPageJsTest, IncrementIndex) { | 117 TEST_F(FindInPageJsTest, IncrementIndex) { |
111 SearchForFoo(); | 118 SearchForFoo(); |
112 | 119 |
113 // Increment index until it hits the max index. | 120 // Increment index until it hits the max index. |
114 for (int i = 2; i < kNumberOfFoosInHtml; i++) { | 121 for (int i = 2; i < kNumberOfFoosInHtml; i++) { |
115 ExecuteJavaScript(kJavaScriptIncrementIndex); | 122 ExecuteJavaScript(kJavaScriptIncrementIndex); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 // point to the first visible occurence of the non-Ascii. | 217 // point to the first visible occurence of the non-Ascii. |
211 NSString* result = ExecuteJavaScript([NSString | 218 NSString* result = ExecuteJavaScript([NSString |
212 stringWithFormat:@"__gCrWeb.findInPage.highlightWord('%@', false, 1000)", | 219 stringWithFormat:@"__gCrWeb.findInPage.highlightWord('%@', false, 1000)", |
213 kNonAscii]); | 220 kNonAscii]); |
214 DCHECK(result); | 221 DCHECK(result); |
215 AssertJavaScriptValue(kJavaScriptIndex, 0); | 222 AssertJavaScriptValue(kJavaScriptIndex, 0); |
216 AssertJavaScriptValue(kJavaScriptSpansLength, 1); | 223 AssertJavaScriptValue(kJavaScriptSpansLength, 1); |
217 } | 224 } |
218 | 225 |
219 } // namespace | 226 } // namespace |
OLD | NEW |