OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "ios/chrome/browser/find_in_page/find_tab_helper.h" | 5 #import "ios/chrome/browser/find_in_page/find_tab_helper.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/run_loop.h" |
| 9 #import "base/test/ios/wait_util.h" |
| 10 #import "ios/chrome/browser/find_in_page/find_in_page_model.h" |
| 11 #import "ios/chrome/browser/web/chrome_web_test.h" |
8 #import "ios/web/public/test/fakes/test_web_state.h" | 12 #import "ios/web/public/test/fakes/test_web_state.h" |
9 #import "ios/web/public/test/web_test.h" | 13 #import "ios/web/public/test/web_test.h" |
10 | 14 |
11 #if !defined(__has_feature) || !__has_feature(objc_arc) | 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
12 #error "This file requires ARC support." | 16 #error "This file requires ARC support." |
13 #endif | 17 #endif |
14 | 18 |
| 19 namespace { |
| 20 const NSString* kTestString = @"Test string"; |
| 21 } |
| 22 |
15 // Test fixture for the FindTabHelper class. | 23 // Test fixture for the FindTabHelper class. |
16 class FindTabHelperTest : public web::WebTest { | 24 class FindTabHelperTest : public ChromeWebTest { |
17 public: | 25 public: |
18 FindTabHelperTest() { FindTabHelper::CreateForWebState(&web_state_, nil); } | 26 FindTabHelperTest() = default; |
19 ~FindTabHelperTest() override = default; | 27 ~FindTabHelperTest() override = default; |
20 | 28 |
21 protected: | 29 protected: |
22 web::TestWebState web_state_; | 30 void SetUp() override { |
| 31 ChromeWebTest::SetUp(); |
| 32 FindTabHelper::CreateForWebState(web_state(), nil); |
| 33 } |
| 34 |
| 35 void TearDown() override { |
| 36 // Stop any in-progress find operations when the test completes. |
| 37 __block BOOL completion_handler_block_was_called = NO; |
| 38 FindTabHelper::FromWebState(web_state())->StopFinding(^() { |
| 39 completion_handler_block_was_called = YES; |
| 40 }); |
| 41 base::test::ios::WaitUntilCondition(^bool() { |
| 42 return completion_handler_block_was_called; |
| 43 }); |
| 44 |
| 45 ChromeWebTest::TearDown(); |
| 46 } |
| 47 |
| 48 // Loads a test html page with the given number of repetitions of |
| 49 // |kTestString}. |
| 50 void LoadTestHtml(int test_string_count) { |
| 51 NSString* html = @"<html><body>"; |
| 52 for (int ii = 0; ii < test_string_count; ++ii) { |
| 53 NSString* test_string = [NSString |
| 54 stringWithFormat:@"%@ %d <br>", kTestString, test_string_count]; |
| 55 html = [html stringByAppendingString:test_string]; |
| 56 } |
| 57 html = [html stringByAppendingString:@"</body></html>"]; |
| 58 |
| 59 LoadHtml(html); |
| 60 } |
23 | 61 |
24 private: | 62 private: |
25 DISALLOW_COPY_AND_ASSIGN(FindTabHelperTest); | 63 DISALLOW_COPY_AND_ASSIGN(FindTabHelperTest); |
26 }; | 64 }; |
27 | 65 |
28 // Tests that the helper's FindInPageController exists. | 66 // Tests the StartFinding(), ContinueFinding(), and StopFinding() methods. |
29 TEST_F(FindTabHelperTest, ControllerExists) { | 67 TEST_F(FindTabHelperTest, FindInPage) { |
30 DCHECK(FindTabHelper::FromWebState(&web_state_)->GetController()); | 68 LoadTestHtml(5); |
31 } | 69 auto* helper = FindTabHelper::FromWebState(web_state()); |
| 70 ASSERT_TRUE(helper); |
| 71 |
| 72 __block BOOL completion_handler_block_was_called = NO; |
| 73 id wait_block = ^bool() { |
| 74 // Waits for |completion_handler_block_was_called| to be YES, but resets it |
| 75 // to NO before returning. |
| 76 BOOL success = completion_handler_block_was_called; |
| 77 if (success) { |
| 78 completion_handler_block_was_called = NO; |
| 79 } |
| 80 return success; |
| 81 }; |
| 82 |
| 83 // Search for "Test string" and verify that there are five matches. |
| 84 helper->StartFinding(@"Test string", ^(FindInPageModel* model) { |
| 85 EXPECT_EQ(5U, model.matches); |
| 86 EXPECT_EQ(1U, model.currentIndex); |
| 87 completion_handler_block_was_called = YES; |
| 88 }); |
| 89 base::test::ios::WaitUntilCondition(wait_block); |
| 90 |
| 91 // Search forward in the page for additional matches and verify that |
| 92 // |currentIndex| is updated. |
| 93 helper->ContinueFinding(FindTabHelper::FORWARD, ^(FindInPageModel* model) { |
| 94 EXPECT_EQ(5U, model.matches); |
| 95 EXPECT_EQ(2U, model.currentIndex); |
| 96 completion_handler_block_was_called = YES; |
| 97 }); |
| 98 base::test::ios::WaitUntilCondition(wait_block); |
| 99 |
| 100 helper->ContinueFinding(FindTabHelper::FORWARD, ^(FindInPageModel* model) { |
| 101 EXPECT_EQ(5U, model.matches); |
| 102 EXPECT_EQ(3U, model.currentIndex); |
| 103 completion_handler_block_was_called = YES; |
| 104 }); |
| 105 base::test::ios::WaitUntilCondition(wait_block); |
| 106 |
| 107 // Search backwards in the page for previous matches and verify that |
| 108 // |currentIndex| is updated. |
| 109 helper->ContinueFinding(FindTabHelper::REVERSE, ^(FindInPageModel* model) { |
| 110 EXPECT_EQ(5U, model.matches); |
| 111 EXPECT_EQ(2U, model.currentIndex); |
| 112 completion_handler_block_was_called = YES; |
| 113 }); |
| 114 base::test::ios::WaitUntilCondition(wait_block); |
| 115 |
| 116 // Stop finding and verify that the completion block was called properly. |
| 117 helper->StopFinding(^() { |
| 118 completion_handler_block_was_called = YES; |
| 119 }); |
| 120 base::test::ios::WaitUntilCondition(wait_block); |
| 121 } |
| 122 |
| 123 // Tests that ContinueFinding() wraps around when it reaches the last match. |
| 124 TEST_F(FindTabHelperTest, ContinueFindingWrapsAround) { |
| 125 LoadTestHtml(2); |
| 126 auto* helper = FindTabHelper::FromWebState(web_state()); |
| 127 ASSERT_TRUE(helper); |
| 128 |
| 129 __block BOOL completion_handler_block_was_called = NO; |
| 130 id wait_block = ^bool() { |
| 131 // Waits for |completion_handler_block_was_called| to be YES, but resets it |
| 132 // to NO before returning. |
| 133 BOOL success = completion_handler_block_was_called; |
| 134 if (success) { |
| 135 completion_handler_block_was_called = NO; |
| 136 } |
| 137 return success; |
| 138 }; |
| 139 |
| 140 // Search for "Test string". |
| 141 helper->StartFinding(@"Test string", ^(FindInPageModel* model) { |
| 142 EXPECT_EQ(2U, model.matches); |
| 143 EXPECT_EQ(1U, model.currentIndex); |
| 144 completion_handler_block_was_called = YES; |
| 145 }); |
| 146 base::test::ios::WaitUntilCondition(wait_block); |
| 147 |
| 148 // Search backwards in the page and verify that |currentIndex| wraps around to |
| 149 // the last match. |
| 150 helper->ContinueFinding(FindTabHelper::REVERSE, ^(FindInPageModel* model) { |
| 151 EXPECT_EQ(2U, model.matches); |
| 152 EXPECT_EQ(2U, model.currentIndex); |
| 153 completion_handler_block_was_called = YES; |
| 154 }); |
| 155 base::test::ios::WaitUntilCondition(wait_block); |
| 156 |
| 157 // Search forward in the page and verify that |currentIndex| wraps around to |
| 158 // the first match. |
| 159 helper->ContinueFinding(FindTabHelper::FORWARD, ^(FindInPageModel* model) { |
| 160 EXPECT_EQ(2U, model.matches); |
| 161 EXPECT_EQ(1U, model.currentIndex); |
| 162 completion_handler_block_was_called = YES; |
| 163 }); |
| 164 base::test::ios::WaitUntilCondition(wait_block); |
| 165 } |
| 166 |
| 167 // Tests that the FindInPageModel returned by GetFindResults() is updated to |
| 168 // reflect the results of the latest find operation. |
| 169 TEST_F(FindTabHelperTest, GetFindResults) { |
| 170 LoadTestHtml(2); |
| 171 auto* helper = FindTabHelper::FromWebState(web_state()); |
| 172 ASSERT_TRUE(helper); |
| 173 |
| 174 __block BOOL completion_handler_block_was_called = NO; |
| 175 id wait_block = ^bool() { |
| 176 // Waits for |completion_handler_block_was_called| to be YES, but resets it |
| 177 // to NO before returning. |
| 178 BOOL success = completion_handler_block_was_called; |
| 179 if (success) { |
| 180 completion_handler_block_was_called = NO; |
| 181 } |
| 182 return success; |
| 183 }; |
| 184 |
| 185 // Search for "Test string". |
| 186 helper->StartFinding(@"Test string", ^(FindInPageModel* model) { |
| 187 completion_handler_block_was_called = YES; |
| 188 }); |
| 189 base::test::ios::WaitUntilCondition(wait_block); |
| 190 { |
| 191 FindInPageModel* model = helper->GetFindResult(); |
| 192 EXPECT_EQ(2U, model.matches); |
| 193 EXPECT_EQ(1U, model.currentIndex); |
| 194 } |
| 195 |
| 196 // Search forward in the page and verify that |currentIndex| wraps around to |
| 197 // the first match. |
| 198 helper->ContinueFinding(FindTabHelper::FORWARD, ^(FindInPageModel* model) { |
| 199 completion_handler_block_was_called = YES; |
| 200 }); |
| 201 base::test::ios::WaitUntilCondition(wait_block); |
| 202 { |
| 203 FindInPageModel* model = helper->GetFindResult(); |
| 204 EXPECT_EQ(2U, model.matches); |
| 205 EXPECT_EQ(2U, model.currentIndex); |
| 206 } |
| 207 } |
| 208 |
| 209 // Tests the IsFindUIActive() getter and setter. |
| 210 TEST_F(FindTabHelperTest, IsFindUIActive) { |
| 211 auto* helper = FindTabHelper::FromWebState(web_state()); |
| 212 |
| 213 helper->SetFindUIActive(true); |
| 214 EXPECT_TRUE(helper->IsFindUIActive()); |
| 215 |
| 216 helper->SetFindUIActive(false); |
| 217 EXPECT_FALSE(helper->IsFindUIActive()); |
| 218 } |
| 219 |
| 220 // Tests that IsFindUIActive() is reset to false on page navigation. |
| 221 TEST_F(FindTabHelperTest, FindUIActiveIsResetOnPageNavigation) { |
| 222 LoadTestHtml(2); |
| 223 auto* helper = FindTabHelper::FromWebState(web_state()); |
| 224 helper->SetFindUIActive(true); |
| 225 EXPECT_TRUE(helper->IsFindUIActive()); |
| 226 |
| 227 LoadTestHtml(3); |
| 228 EXPECT_FALSE(helper->IsFindUIActive()); |
| 229 } |
OLD | NEW |