Commit 383f0038 authored by Hiroshi Ichikawa's avatar Hiroshi Ichikawa Committed by Commit Bot

Add inttest for CWVUIDelegate

Bug: 862537
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: Ib7f92055fb4bdcf1b27b1801be2d6f1dad969282
Reviewed-on: https://chromium-review.googlesource.com/1143100
Commit-Queue: Hiroshi Ichikawa <ichikawa@chromium.org>
Reviewed-by: default avatarJohn Wu <jzw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576784}
parent dc2450ff
...@@ -11,6 +11,7 @@ source_set("inttests") { ...@@ -11,6 +11,7 @@ source_set("inttests") {
sources = [ sources = [
"navigation_delegate_inttest.mm", "navigation_delegate_inttest.mm",
"scroll_view_kvo_inttest.mm", "scroll_view_kvo_inttest.mm",
"ui_delegate_inttest.mm",
"web_view_autofill_inttest.mm", "web_view_autofill_inttest.mm",
"web_view_inttest.mm", "web_view_inttest.mm",
"web_view_inttest_base.h", "web_view_inttest_base.h",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <ChromeWebView/ChromeWebView.h>
#import <Foundation/Foundation.h>
#import "base/test/ios/wait_util.h"
#import "ios/web_view/test/web_view_inttest_base.h"
#import "ios/web_view/test/web_view_test_util.h"
#import "net/base/mac/url_conversions.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest_mac.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using base::test::ios::kWaitForUIElementTimeout;
namespace ios_web_view {
// Tests CWVUIDelegate.
class UIDelegateTest : public ios_web_view::WebViewInttestBase {
public:
void SetUp() override {
mock_delegate_ = OCMProtocolMock(@protocol(CWVUIDelegate));
web_view_.UIDelegate = mock_delegate_;
ASSERT_TRUE(test_server_->Start());
url_ = net::NSURLWithGURL(GetUrlForPageWithTitleAndBody("Title", "Body"));
}
id<CWVUIDelegate> mock_delegate_;
NSURL* url_;
};
// Tests -webView:createWebViewWithConfiguration:forNavigationAction:
TEST_F(UIDelegateTest, CreateWebView) {
id expected_navigation_action =
[OCMArg checkWithBlock:^(CWVNavigationAction* action) {
return
[action.request.URL.absoluteString isEqual:@"http://example.com/"];
}];
OCMExpect([mock_delegate_ webView:web_view_
createWebViewWithConfiguration:web_view_.configuration
forNavigationAction:expected_navigation_action]);
ASSERT_TRUE(test::LoadUrl(web_view_, url_));
NSError* error = nil;
EXPECT_NE(nil, test::EvaluateJavaScript(
web_view_, @"open('http://example.com/')", &error));
EXPECT_EQ(nil, error);
[(id)mock_delegate_ verify];
}
// Tests -webView:runJavaScriptAlertPanelWithMessage:pageURL:completionHandler:
TEST_F(UIDelegateTest, RunJavaScriptAlertPanel) {
id mock_completion_handler =
[OCMArg checkWithBlock:^(void (^completionHandler)(void)) {
completionHandler();
return YES;
}];
OCMExpect([mock_delegate_ webView:web_view_
runJavaScriptAlertPanelWithMessage:@"message"
pageURL:url_
completionHandler:mock_completion_handler]);
ASSERT_TRUE(test::LoadUrl(web_view_, url_));
NSError* error = nil;
test::EvaluateJavaScript(web_view_, @"alert('message')", &error);
EXPECT_EQ(nil, error);
[(id)mock_delegate_ verify];
}
// Tests
// -webView:runJavaScriptConfirmPanelWithMessage:pageURL:completionHandler:
TEST_F(UIDelegateTest, RunJavaScriptConfirmPanel) {
id mock_completion_handler =
[OCMArg checkWithBlock:^(void (^completionHandler)(BOOL)) {
completionHandler(YES);
return YES;
}];
OCMExpect([mock_delegate_ webView:web_view_
runJavaScriptConfirmPanelWithMessage:@"message"
pageURL:url_
completionHandler:mock_completion_handler]);
ASSERT_TRUE(test::LoadUrl(web_view_, url_));
NSError* error = nil;
EXPECT_NSEQ(@(YES), test::EvaluateJavaScript(web_view_, @"confirm('message')",
&error));
EXPECT_EQ(nil, error);
[(id)mock_delegate_ verify];
}
// Tests
// -webView:runJavaScriptTextInputPanelWithPrompt:pageURL:completionHandler:
TEST_F(UIDelegateTest, RunJavaScriptTextInputPanel) {
id mock_completion_handler =
[OCMArg checkWithBlock:^(void (^completionHandler)(NSString*)) {
completionHandler(@"input");
return YES;
}];
OCMExpect([mock_delegate_ webView:web_view_
runJavaScriptTextInputPanelWithPrompt:@"prompt"
defaultText:@"default"
pageURL:url_
completionHandler:mock_completion_handler]);
ASSERT_TRUE(test::LoadUrl(web_view_, url_));
NSError* error = nil;
EXPECT_NSEQ(@"input", test::EvaluateJavaScript(
web_view_, @"prompt('prompt', 'default')", &error));
EXPECT_EQ(nil, error);
[(id)mock_delegate_ verify];
}
// Tests -webView:didLoadFavicons:
TEST_F(UIDelegateTest, DidLoadFavicons) {
NSURL* page_url = net::NSURLWithGURL(GetUrlForPageWithHtml(R"(
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="/testfavicon.png">
</head>
<body></body>
</html>
)"));
// This file does not exist, but it doesn't matter for this test.
NSURL* favicon_url =
net::NSURLWithGURL(test_server_->GetURL("/testfavicon.png"));
__block NSArray<CWVFavicon*>* favicons = nil;
id favicons_arg = [OCMArg checkWithBlock:^(NSArray<CWVFavicon*>* value) {
favicons = value;
return YES;
}];
OCMExpect([mock_delegate_ webView:web_view_ didLoadFavicons:favicons_arg]);
ASSERT_TRUE(test::LoadUrl(web_view_, page_url));
[(id)mock_delegate_ verifyWithDelay:kWaitForUIElementTimeout];
ASSERT_EQ(1u, favicons.count);
EXPECT_EQ(CWVFaviconTypeFavicon, favicons[0].type);
EXPECT_NSEQ(favicon_url, favicons[0].URL);
}
} // namespace ios_web_view
...@@ -50,6 +50,12 @@ class WebViewInttestBase : public PlatformTest { ...@@ -50,6 +50,12 @@ class WebViewInttestBase : public PlatformTest {
GURL GetUrlForPageWithTitleAndBody(const std::string& title, GURL GetUrlForPageWithTitleAndBody(const std::string& title,
const std::string& body); const std::string& body);
// Returns URL to an html page with |html|. |html| contains entire html of the
// page.
//
// Call ASSERT_TRUE(test_server_->Start()) before accessing the returned URL.
GURL GetUrlForPageWithHtml(const std::string& html);
// CWVWebView created with default configuration and frame equal to screen // CWVWebView created with default configuration and frame equal to screen
// bounds. // bounds.
CWVWebView* web_view_; CWVWebView* web_view_;
......
...@@ -25,22 +25,20 @@ namespace { ...@@ -25,22 +25,20 @@ namespace {
// Test server path which renders a basic html page. // Test server path which renders a basic html page.
const char kPageHtmlPath[] = "/PageHtml?"; const char kPageHtmlPath[] = "/PageHtml?";
// URL parameter for entire html. Value must be base64 encoded.
// kPageHtmlBodyParamName and kPageHtmlTitleParamName are ignored when this is
// given.
const char kPageHtmlParamName[] = "html";
// URL parameter for html body. Value must be base64 encoded. // URL parameter for html body. Value must be base64 encoded.
const char kPageHtmlBodyParamName[] = "body"; const char kPageHtmlBodyParamName[] = "body";
// URL parameter for page title. Value must be base64 encoded. // URL parameter for page title. Value must be base64 encoded.
const char kPageHtmlTitleParamName[] = "title"; const char kPageHtmlTitleParamName[] = "title";
// Generates an html response. // Generates html from title and body.
std::unique_ptr<net::test_server::HttpResponse> CreatePageHTMLResponse( std::string CreatePageHTML(const std::string& title, const std::string& body) {
const std::string& title, return base::StringPrintf(
const std::string& body) {
std::string html = base::StringPrintf(
"<html><head><title>%s</title></head><body>%s</body></html>", "<html><head><title>%s</title></head><body>%s</body></html>",
title.c_str(), body.c_str()); title.c_str(), body.c_str());
auto http_response = std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_content(html);
return std::move(http_response);
} }
// Returns true if |string| starts with |prefix|. String comparison is case // Returns true if |string| starts with |prefix|. String comparison is case
...@@ -70,24 +68,37 @@ std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( ...@@ -70,24 +68,37 @@ std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler(
if (StartsWith(request.relative_url, kPageHtmlPath)) { if (StartsWith(request.relative_url, kPageHtmlPath)) {
std::string title; std::string title;
std::string body; std::string body;
std::string html;
GURL request_url = request.GetURL(); GURL request_url = request.GetURL();
std::string encoded_title; std::string encoded_html;
bool title_found = net::GetValueForKeyInQuery( bool html_found = net::GetValueForKeyInQuery(
request_url, kPageHtmlTitleParamName, &encoded_title); request_url, kPageHtmlParamName, &encoded_html);
if (title_found) { if (html_found) {
title = DecodeQueryParamValue(encoded_title); html = DecodeQueryParamValue(encoded_html);
} else {
std::string encoded_title;
bool title_found = net::GetValueForKeyInQuery(
request_url, kPageHtmlTitleParamName, &encoded_title);
if (title_found) {
title = DecodeQueryParamValue(encoded_title);
}
std::string encoded_body;
bool body_found = net::GetValueForKeyInQuery(
request_url, kPageHtmlBodyParamName, &encoded_body);
if (body_found) {
body = DecodeQueryParamValue(encoded_body);
}
html = CreatePageHTML(title, body);
} }
std::string encoded_body; auto http_response =
bool body_found = net::GetValueForKeyInQuery( std::make_unique<net::test_server::BasicHttpResponse>();
request_url, kPageHtmlBodyParamName, &encoded_body); http_response->set_content(html);
if (body_found) { return std::move(http_response);
body = DecodeQueryParamValue(encoded_body);
}
return CreatePageHTMLResponse(title, body);
} }
return nullptr; return nullptr;
} }
...@@ -130,4 +141,15 @@ GURL WebViewInttestBase::GetUrlForPageWithTitleAndBody( ...@@ -130,4 +141,15 @@ GURL WebViewInttestBase::GetUrlForPageWithTitleAndBody(
return url; return url;
} }
GURL WebViewInttestBase::GetUrlForPageWithHtml(const std::string& html) {
GURL url = test_server_->GetURL(kPageHtmlPath);
// Encode |html| in url query in order to build the server
// response later in TestRequestHandler.
std::string encoded_html = EncodeQueryParamValue(html);
url = net::AppendQueryParameter(url, kPageHtmlParamName, encoded_html);
return url;
}
} // namespace ios_web_view } // namespace ios_web_view
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment