Chromium Code Reviews| Index: ios/web_view/test/chrome_web_view_test.mm |
| diff --git a/ios/web_view/test/chrome_web_view_test.mm b/ios/web_view/test/chrome_web_view_test.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5a6909979969aacff9e51206c21337dfbf5023cb |
| --- /dev/null |
| +++ b/ios/web_view/test/chrome_web_view_test.mm |
| @@ -0,0 +1,99 @@ |
| +// Copyright 2017 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 "ios/web_view/test/chrome_web_view_test.h" |
| + |
| +#import <ChromeWebView/ChromeWebView.h> |
| +#import <Foundation/Foundation.h> |
| + |
| +#include "base/base64.h" |
| +#import "base/memory/ptr_util.h" |
| +#import "ios/testing/wait_util.h" |
| +#include "net/test/embedded_test_server/default_handlers.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "net/test/embedded_test_server/http_request.h" |
| +#include "net/test/embedded_test_server/http_response.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| + |
| +const char kPageHTMLBodyPath[] = "/PageHTMLBody?"; |
|
Eugene But (OOO till 7-30)
2017/05/23 19:49:34
s/HTML/Html
Eugene But (OOO till 7-30)
2017/05/23 19:49:34
Please add comments for helper constant and method
michaeldo
2017/05/24 15:02:35
Done.
|
| + |
| +std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( |
| + const net::test_server::HttpRequest& request) { |
| + DCHECK(base::StartsWith(request.relative_url, kPageHTMLBodyPath, |
| + base::CompareCase::INSENSITIVE_ASCII)); |
| + |
| + std::string body = request.relative_url.substr(strlen(kPageHTMLBodyPath)); |
| + |
| + std::string unescaped_body; |
| + base::Base64Decode(body, &unescaped_body); |
| + std::string html = "<html><body>" + unescaped_body + "</body></html>"; |
| + |
| + auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); |
| + http_response->set_content(html); |
| + return std::move(http_response); |
| +} |
| + |
| +std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( |
| + const net::test_server::HttpRequest& request) { |
| + if (base::StartsWith(request.relative_url, kPageHTMLBodyPath, |
| + base::CompareCase::INSENSITIVE_ASCII)) { |
| + return EchoPageHTMLBodyInResponse(request); |
| + } |
| + return base::MakeUnique<net::test_server::BasicHttpResponse>(); |
|
mmenke
2017/05/23 20:48:27
If you don't handle something, convention is just
michaeldo
2017/05/24 15:02:35
Done.
|
| +} |
| + |
| +} // namespace |
| + |
| +namespace ios_web_view { |
| + |
| +ChromeWebViewTest::ChromeWebViewTest() {} |
| + |
| +ChromeWebViewTest::~ChromeWebViewTest() = default; |
| + |
| +void ChromeWebViewTest::SetUp() { |
| + PlatformTest::SetUp(); |
| + |
| + test_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
| + net::test_server::EmbeddedTestServer::TYPE_HTTP); |
| + net::test_server::RegisterDefaultHandlers(test_server_.get()); |
| + test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); |
| + ASSERT_TRUE(test_server_->Start()); |
| +} |
| + |
| +void ChromeWebViewTest::TearDown() { |
| + test_server_.reset(); |
|
mmenke
2017/05/23 20:48:27
optional: I believe dynamic allocation is discour
michaeldo
2017/05/24 15:02:35
Done.
|
| + PlatformTest::TearDown(); |
| +} |
| + |
| +GURL ChromeWebViewTest::GetURLForPageWithTitle(const std::string& title) { |
| + return test_server_->GetURL("/echotitle/" + title); |
| +} |
| + |
| +GURL ChromeWebViewTest::GetURLForPageWithHTMLBody(const std::string& html) { |
| + std::string base64_html; |
| + base::Base64Encode(html, &base64_html); |
| + return test_server_->GetURL(kPageHTMLBodyPath + base64_html); |
| +} |
| + |
| +void ChromeWebViewTest::LoadURL(CWVWebView* web_view, NSURL* URL) { |
| + [web_view loadRequest:[NSURLRequest requestWithURL:URL]]; |
| + |
| + WaitForPageLoadCompletion(web_view); |
| +} |
| + |
| +void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { |
| + BOOL success = |
| + testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ |
| + return !web_view.isLoading; |
| + }); |
| + EXPECT_TRUE(success); |
|
Eugene But (OOO till 7-30)
2017/05/23 19:49:33
ASSERT_TRUE per API comments?
michaeldo
2017/05/24 15:02:35
Done.
|
| +} |
| + |
| +} // namespace ios_web_view |