| Index: ios/web/test/web_int_test.mm
|
| diff --git a/ios/web/test/web_int_test.mm b/ios/web/test/web_int_test.mm
|
| index 92edfc8e4d51e4d7e63562ef8f46d4ffecb20761..365d2b0de8be99a6b653b7cc77a56763ee21211b 100644
|
| --- a/ios/web/test/web_int_test.mm
|
| +++ b/ios/web/test/web_int_test.mm
|
| @@ -5,25 +5,73 @@
|
| #import "ios/web/test/web_int_test.h"
|
|
|
| #import "base/ios/block_types.h"
|
| -
|
| +#include "base/memory/ptr_util.h"
|
| #import "base/test/ios/wait_util.h"
|
| #import "ios/web/public/test/http_server.h"
|
| +#import "ios/web/public/test/js_test_util.h"
|
| +#include "ios/web/public/web_state/web_state_observer.h"
|
| #import "ios/web/public/web_view_creation_util.h"
|
|
|
| namespace web {
|
|
|
| +#pragma mark - IntTestWebStateObserver
|
| +
|
| +// WebStateObserver class that is used to track when page loads finish.
|
| +class IntTestWebStateObserver : public web::WebStateObserver {
|
| + public:
|
| + IntTestWebStateObserver(web::WebState* web_state)
|
| + : web::WebStateObserver(web_state), page_loaded_(false) {}
|
| +
|
| + // Instructs the observer to listen for page loads for |url|.
|
| + void ExpectPageLoad(const GURL& url) {
|
| + expected_url_ = url;
|
| + page_loaded_ = false;
|
| + }
|
| +
|
| + // Whether |expected_url_| has been loaded successfully.
|
| + bool IsExpectedPageLoaded() { return page_loaded_; }
|
| +
|
| + // WebStateObserver methods:
|
| + void PageLoaded(
|
| + web::PageLoadCompletionStatus load_completion_status) override {
|
| + ASSERT_EQ(load_completion_status == web::PageLoadCompletionStatus::SUCCESS,
|
| + expected_url_.is_valid());
|
| + page_loaded_ = true;
|
| + }
|
| +
|
| + private:
|
| + GURL expected_url_;
|
| + bool page_loaded_;
|
| +};
|
| +
|
| +#pragma mark - WebIntTest
|
| +
|
| WebIntTest::WebIntTest() {}
|
| WebIntTest::~WebIntTest() {}
|
|
|
| void WebIntTest::SetUp() {
|
| WebTest::SetUp();
|
|
|
| + // Start the http server.
|
| web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance();
|
| ASSERT_FALSE(server.IsRunning());
|
| server.StartOrDie();
|
|
|
| + // Remove any previously existing WKWebView data.
|
| RemoveWKWebViewCreatedData([WKWebsiteDataStore defaultDataStore],
|
| [WKWebsiteDataStore allWebsiteDataTypes]);
|
| +
|
| + // Create the WebState and its WebStateObserver.
|
| + web::WebState::CreateParams web_state_create_params(GetBrowserState());
|
| + web_state_ = web::WebState::Create(web_state_create_params);
|
| + observer_ = base::WrapUnique(new IntTestWebStateObserver(web_state()));
|
| +
|
| + // Resize the webview so that pages can be properly rendered.
|
| + web_state()->GetView().frame =
|
| + [UIApplication sharedApplication].keyWindow.bounds;
|
| +
|
| + // Enable web usage for the WebState.
|
| + web_state()->SetWebUsageEnabled(true);
|
| }
|
|
|
| void WebIntTest::TearDown() {
|
| @@ -37,6 +85,27 @@
|
| WebTest::TearDown();
|
| }
|
|
|
| +id WebIntTest::ExecuteJavaScript(NSString* script) {
|
| + return web::ExecuteJavaScript(web_state()->GetJSInjectionReceiver(), script);
|
| +}
|
| +
|
| +void WebIntTest::ExecuteBlockAndWaitForLoad(const GURL& url,
|
| + ProceduralBlock block) {
|
| + DCHECK(block);
|
| + observer_->ExpectPageLoad(url);
|
| + block();
|
| + base::test::ios::WaitUntilCondition(^bool {
|
| + return observer_->IsExpectedPageLoaded();
|
| + });
|
| +}
|
| +
|
| +void WebIntTest::LoadUrl(const GURL& url) {
|
| + ExecuteBlockAndWaitForLoad(url, ^{
|
| + web::NavigationManager::WebLoadParams params(url);
|
| + navigation_manager()->LoadURLWithParams(params);
|
| + });
|
| +}
|
| +
|
| void WebIntTest::RemoveWKWebViewCreatedData(WKWebsiteDataStore* data_store,
|
| NSSet* websiteDataTypes) {
|
| __block bool data_removed = false;
|
| @@ -71,4 +140,13 @@
|
| });
|
| }
|
|
|
| +NSInteger WebIntTest::GetIndexOfNavigationItem(
|
| + const web::NavigationItem* item) {
|
| + for (NSInteger i = 0; i < navigation_manager()->GetItemCount(); ++i) {
|
| + if (navigation_manager()->GetItemAtIndex(i) == item)
|
| + return i;
|
| + }
|
| + return NSNotFound;
|
| +}
|
| +
|
| } // namespace web
|
|
|