| 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 "ios/web/test/web_test.h" | 5 #import "ios/web/test/web_test.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 #pragma mark - | 61 #pragma mark - |
| 62 | 62 |
| 63 WebTestWithWebController::WebTestWithWebController() {} | 63 WebTestWithWebController::WebTestWithWebController() {} |
| 64 | 64 |
| 65 WebTestWithWebController::~WebTestWithWebController() {} | 65 WebTestWithWebController::~WebTestWithWebController() {} |
| 66 | 66 |
| 67 static int s_html_load_count; | 67 static int s_html_load_count; |
| 68 | 68 |
| 69 void WebTestWithWebController::SetUp() { | 69 void WebTestWithWebController::SetUp() { |
| 70 WebTest::SetUp(); | 70 WebTest::SetUp(); |
| 71 webController_.reset(this->CreateWebController()); | 71 web_state_impl_.reset(new WebStateImpl(GetBrowserState())); |
| 72 web_state_impl_->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, |
| 73 0); |
| 74 webController_.reset(web_state_impl_->GetWebController()); |
| 72 | 75 |
| 73 [webController_ setWebUsageEnabled:YES]; | 76 [webController_ setWebUsageEnabled:YES]; |
| 74 // Force generation of child views; necessary for some tests. | 77 // Force generation of child views; necessary for some tests. |
| 75 [webController_ triggerPendingLoad]; | 78 [webController_ triggerPendingLoad]; |
| 76 s_html_load_count = 0; | 79 s_html_load_count = 0; |
| 77 } | 80 } |
| 78 | 81 |
| 79 void WebTestWithWebController::TearDown() { | 82 void WebTestWithWebController::TearDown() { |
| 80 [webController_ close]; | 83 web_state_impl_.reset(); |
| 81 WebTest::TearDown(); | 84 WebTest::TearDown(); |
| 82 } | 85 } |
| 83 | 86 |
| 84 void WebTestWithWebController::LoadHtml(NSString* html) { | 87 void WebTestWithWebController::LoadHtml(NSString* html) { |
| 85 LoadHtml([html UTF8String]); | 88 LoadHtml([html UTF8String]); |
| 86 } | 89 } |
| 87 | 90 |
| 88 void WebTestWithWebController::LoadHtml(const std::string& html) { | 91 void WebTestWithWebController::LoadHtml(const std::string& html) { |
| 89 NSString* load_check = CreateLoadCheck(); | 92 NSString* load_check = CreateLoadCheck(); |
| 90 std::string marked_html = html + [load_check UTF8String]; | 93 std::string marked_html = html + [load_check UTF8String]; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 NSDictionary* dictionary = [NSJSONSerialization | 219 NSDictionary* dictionary = [NSJSONSerialization |
| 217 JSONObjectWithData:evaluationData | 220 JSONObjectWithData:evaluationData |
| 218 options:0 | 221 options:0 |
| 219 error:&error]; | 222 error:&error]; |
| 220 DCHECK(dictionary && !error); | 223 DCHECK(dictionary && !error); |
| 221 NSString* exception = [dictionary objectForKey:@"exception"]; | 224 NSString* exception = [dictionary objectForKey:@"exception"]; |
| 222 CHECK(!exception) << "Script error: " << [exception UTF8String]; | 225 CHECK(!exception) << "Script error: " << [exception UTF8String]; |
| 223 return [dictionary objectForKey:@"result"]; | 226 return [dictionary objectForKey:@"result"]; |
| 224 } | 227 } |
| 225 | 228 |
| 226 CRWWebController* WebTestWithWebController::CreateWebController() { | |
| 227 std::unique_ptr<WebStateImpl> web_state_impl( | |
| 228 new WebStateImpl(GetBrowserState())); | |
| 229 return [[CRWWKWebViewWebController alloc] | |
| 230 initWithWebState:std::move(web_state_impl)]; | |
| 231 } | |
| 232 | |
| 233 void WebTestWithWebController::WillProcessTask( | 229 void WebTestWithWebController::WillProcessTask( |
| 234 const base::PendingTask& pending_task) { | 230 const base::PendingTask& pending_task) { |
| 235 // Nothing to do. | 231 // Nothing to do. |
| 236 } | 232 } |
| 237 | 233 |
| 238 void WebTestWithWebController::DidProcessTask( | 234 void WebTestWithWebController::DidProcessTask( |
| 239 const base::PendingTask& pending_task) { | 235 const base::PendingTask& pending_task) { |
| 240 processed_a_task_ = true; | 236 processed_a_task_ = true; |
| 241 } | 237 } |
| 242 | 238 |
| 243 bool WebTestWithWebController::ResetPageIfNavigationStalled( | 239 bool WebTestWithWebController::ResetPageIfNavigationStalled( |
| 244 NSString* load_check) { | 240 NSString* load_check) { |
| 245 NSString* inner_html = RunJavaScript( | 241 NSString* inner_html = RunJavaScript( |
| 246 @"(document && document.body && document.body.innerHTML) || 'undefined'"); | 242 @"(document && document.body && document.body.innerHTML) || 'undefined'"); |
| 247 if ([inner_html rangeOfString:load_check].location == NSNotFound) { | 243 if ([inner_html rangeOfString:load_check].location == NSNotFound) { |
| 248 [webController_ setWebUsageEnabled:NO]; | 244 [webController_ setWebUsageEnabled:NO]; |
| 249 [webController_ setWebUsageEnabled:YES]; | 245 [webController_ setWebUsageEnabled:YES]; |
| 250 [webController_ triggerPendingLoad]; | 246 [webController_ triggerPendingLoad]; |
| 251 return true; | 247 return true; |
| 252 } | 248 } |
| 253 return false; | 249 return false; |
| 254 } | 250 } |
| 255 | 251 |
| 256 NSString* WebTestWithWebController::CreateLoadCheck() { | 252 NSString* WebTestWithWebController::CreateLoadCheck() { |
| 257 return [NSString stringWithFormat:@"<p style=\"display: none;\">%d</p>", | 253 return [NSString stringWithFormat:@"<p style=\"display: none;\">%d</p>", |
| 258 s_html_load_count++]; | 254 s_html_load_count++]; |
| 259 } | 255 } |
| 260 | 256 |
| 261 } // namespace web | 257 } // namespace web |
| OLD | NEW |