| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/public/test/web_test_with_web_state.h" | 5 #import "ios/web/public/test/web_test_with_web_state.h" |
| 6 | 6 |
| 7 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/test/ios/wait_util.h" | 11 #include "base/test/ios/wait_util.h" |
| 12 #import "ios/testing/ocmock_complex_type_helper.h" | 12 #import "ios/testing/ocmock_complex_type_helper.h" |
| 13 #import "ios/web/navigation/crw_session_controller.h" | 13 #import "ios/web/navigation/crw_session_controller.h" |
| 14 #import "ios/web/public/web_state/url_verification_constants.h" | 14 #import "ios/web/public/web_state/url_verification_constants.h" |
| 15 #include "ios/web/public/web_state/web_state_observer.h" |
| 15 #import "ios/web/web_state/ui/crw_web_controller.h" | 16 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 16 #import "ios/web/web_state/web_state_impl.h" | 17 #import "ios/web/web_state/web_state_impl.h" |
| 17 | 18 |
| 18 // Helper Mock to stub out API with C++ objects in arguments. | 19 // Helper Mock to stub out API with C++ objects in arguments. |
| 19 @interface WebDelegateMock : OCMockComplexTypeHelper | 20 @interface WebDelegateMock : OCMockComplexTypeHelper |
| 20 @end | 21 @end |
| 21 | 22 |
| 22 @implementation WebDelegateMock | 23 @implementation WebDelegateMock |
| 23 // Stub implementation always returns YES. | 24 // Stub implementation always returns YES. |
| 24 - (BOOL)webController:(CRWWebController*)webController | 25 - (BOOL)webController:(CRWWebController*)webController |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 const base::PendingTask& pending_task) { | 68 const base::PendingTask& pending_task) { |
| 68 // Nothing to do. | 69 // Nothing to do. |
| 69 } | 70 } |
| 70 | 71 |
| 71 void WebTestWithWebState::DidProcessTask( | 72 void WebTestWithWebState::DidProcessTask( |
| 72 const base::PendingTask& pending_task) { | 73 const base::PendingTask& pending_task) { |
| 73 processed_a_task_ = true; | 74 processed_a_task_ = true; |
| 74 } | 75 } |
| 75 | 76 |
| 76 void WebTestWithWebState::LoadHtml(NSString* html, const GURL& url) { | 77 void WebTestWithWebState::LoadHtml(NSString* html, const GURL& url) { |
| 77 ASSERT_FALSE(web_state()->IsLoading()); | 78 // Sets MIME type to "text/html" once navigation is committed. |
| 79 class MimeTypeUpdater : public WebStateObserver { |
| 80 public: |
| 81 explicit MimeTypeUpdater(WebState* web_state) |
| 82 : WebStateObserver(web_state) {} |
| 83 // WebStateObserver overrides: |
| 84 void NavigationItemCommitted(const LoadCommittedDetails&) override { |
| 85 // loadHTML:forURL: does not notify web view delegate about received |
| 86 // response, so web controller does not get a chance to properly update |
| 87 // MIME type and it should be set manually after navigation is committed |
| 88 // but before WebState signal load completion and clients will start |
| 89 // checking if MIME type is in fact HTML. |
| 90 static_cast<WebStateImpl*>(web_state())->SetContentsMimeType("text/html"); |
| 91 } |
| 92 }; |
| 93 MimeTypeUpdater mime_type_updater(web_state()); |
| 78 | 94 |
| 95 // Initiate asynchronous HTML load. |
| 79 CRWWebController* web_controller = GetWebController(web_state()); | 96 CRWWebController* web_controller = GetWebController(web_state()); |
| 97 ASSERT_EQ(PAGE_LOADED, web_controller.loadPhase); |
| 80 [web_controller loadHTML:html forURL:url]; | 98 [web_controller loadHTML:html forURL:url]; |
| 81 | |
| 82 // Wait until the navigation is committed to update MIME type. | |
| 83 ASSERT_EQ(LOAD_REQUESTED, web_controller.loadPhase); | 99 ASSERT_EQ(LOAD_REQUESTED, web_controller.loadPhase); |
| 84 base::TimeDelta spin_delay = base::TimeDelta::FromMilliseconds(1); | |
| 85 while (web_controller.loadPhase != PAGE_LOADING) { | |
| 86 ASSERT_NE(PAGE_LOADED, web_controller.loadPhase); | |
| 87 base::test::ios::SpinRunLoopWithMaxDelay(spin_delay); | |
| 88 } | |
| 89 | |
| 90 // loadHTML:forURL: does not notify web view delegate about received response, | |
| 91 // so web controller does not get a chance to properly update MIME type and it | |
| 92 // should be set manually after navigation is committed but before WebState | |
| 93 // signal load completion and clients will start checking if MIME type is in | |
| 94 // fact HTML. | |
| 95 [web_controller webStateImpl]->SetContentsMimeType("text/html"); | |
| 96 | 100 |
| 97 // Wait until the page is loaded. | 101 // Wait until the page is loaded. |
| 98 ASSERT_EQ(PAGE_LOADING, web_controller.loadPhase); | 102 base::test::ios::WaitUntilCondition(^{ |
| 99 while (web_controller.loadPhase != PAGE_LOADED) | 103 return web_controller.loadPhase == PAGE_LOADED; |
| 100 base::test::ios::SpinRunLoopWithMaxDelay(spin_delay); | 104 }); |
| 101 | 105 |
| 102 // Wait until scripts execution becomes possible. | 106 // Wait until scripts execution becomes possible. |
| 103 base::test::ios::WaitUntilCondition(^bool { | 107 base::test::ios::WaitUntilCondition(^bool { |
| 104 return [ExecuteJavaScript(@"0;") isEqual:@0]; | 108 return [ExecuteJavaScript(@"0;") isEqual:@0]; |
| 105 }); | 109 }); |
| 106 } | 110 } |
| 107 | 111 |
| 108 void WebTestWithWebState::LoadHtml(NSString* html) { | 112 void WebTestWithWebState::LoadHtml(NSString* html) { |
| 109 GURL url("https://chromium.test/"); | 113 GURL url("https://chromium.test/"); |
| 110 LoadHtml(html, url); | 114 LoadHtml(html, url); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 const web::WebState* WebTestWithWebState::web_state() const { | 177 const web::WebState* WebTestWithWebState::web_state() const { |
| 174 return web_state_.get(); | 178 return web_state_.get(); |
| 175 } | 179 } |
| 176 | 180 |
| 177 NSString* WebTestWithWebState::CreateLoadCheck() { | 181 NSString* WebTestWithWebState::CreateLoadCheck() { |
| 178 return [NSString stringWithFormat:@"<p style=\"display: none;\">%d</p>", | 182 return [NSString stringWithFormat:@"<p style=\"display: none;\">%d</p>", |
| 179 s_html_load_count++]; | 183 s_html_load_count++]; |
| 180 } | 184 } |
| 181 | 185 |
| 182 } // namespace web | 186 } // namespace web |
| OLD | NEW |