| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_view/test/chrome_web_view_test.h" | 5 #import "ios/web_view/test/chrome_web_view_test.h" |
| 6 | 6 |
| 7 #import <ChromeWebView/ChromeWebView.h> | 7 #import <ChromeWebView/ChromeWebView.h> |
| 8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #import "base/memory/ptr_util.h" | 11 #import "base/memory/ptr_util.h" |
| 12 #include "base/strings/stringprintf.h" |
| 12 #import "ios/testing/wait_util.h" | 13 #import "ios/testing/wait_util.h" |
| 13 #include "net/test/embedded_test_server/default_handlers.h" | 14 #include "net/base/url_util.h" |
| 14 #include "net/test/embedded_test_server/embedded_test_server.h" | 15 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 15 #include "net/test/embedded_test_server/http_request.h" | 16 #include "net/test/embedded_test_server/http_request.h" |
| 16 #include "net/test/embedded_test_server/http_response.h" | 17 #include "net/test/embedded_test_server/http_response.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) | 20 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." | 21 #error "This file requires ARC support." |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Test server path which echos the remainder of the url as html. The html | 26 // Test server path which renders a basic html page. |
| 26 // must be base64 encoded. | 27 const char kPageHtmlPath[] = "/PageHtml?"; |
| 27 const char kPageHtmlBodyPath[] = "/PageHtmlBody?"; | 28 // URL parameter for html body. Value must be base64 encoded. |
| 29 const char kPageHtmlBodyParamName[] = "body"; |
| 30 // URL parameter for page title. Value must be base64 encoded. |
| 31 const char kPageHtmlTitleParamName[] = "title"; |
| 28 | 32 |
| 29 // Generates an html response from a request to |kPageHtmlBodyPath|. | 33 // Generates an html response. |
| 30 std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( | 34 std::unique_ptr<net::test_server::HttpResponse> CreatePageHTMLResponse( |
| 31 const net::test_server::HttpRequest& request) { | 35 const std::string& title, |
| 32 DCHECK(base::StartsWith(request.relative_url, kPageHtmlBodyPath, | 36 const std::string& body) { |
| 33 base::CompareCase::INSENSITIVE_ASCII)); | 37 std::string html = base::StringPrintf( |
| 34 | 38 "<html><head><title>%s</title></head><body>%s</body></html>", |
| 35 std::string body = request.relative_url.substr(strlen(kPageHtmlBodyPath)); | 39 title.c_str(), body.c_str()); |
| 36 | |
| 37 std::string unescaped_body; | |
| 38 base::Base64Decode(body, &unescaped_body); | |
| 39 std::string html = "<html><body>" + unescaped_body + "</body></html>"; | |
| 40 | 40 |
| 41 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); | 41 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); |
| 42 http_response->set_content(html); | 42 http_response->set_content(html); |
| 43 return std::move(http_response); | 43 return std::move(http_response); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Returns true if |string| starts with |prefix|. String comparison is case |
| 47 // insensitive. |
| 48 bool StartsWith(std::string string, std::string prefix) { |
| 49 return base::StartsWith(string, prefix, base::CompareCase::SENSITIVE); |
| 50 } |
| 51 |
| 52 // Encodes the |string| for use as the value of a url parameter. |
| 53 std::string EncodeQueryParamValue(std::string string) { |
| 54 std::string encoded_string; |
| 55 base::Base64Encode(string, &encoded_string); |
| 56 return encoded_string; |
| 57 } |
| 58 |
| 59 // Decodes the |encoded_string|. Undoes the encoding performed by |
| 60 // |EncodeQueryParamValue|. |
| 61 std::string DecodeQueryParamValue(std::string encoded_string) { |
| 62 std::string decoded_string; |
| 63 base::Base64Decode(encoded_string, &decoded_string); |
| 64 return decoded_string; |
| 65 } |
| 66 |
| 46 // Maps test server requests to responses. | 67 // Maps test server requests to responses. |
| 47 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( | 68 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( |
| 48 const net::test_server::HttpRequest& request) { | 69 const net::test_server::HttpRequest& request) { |
| 49 if (base::StartsWith(request.relative_url, kPageHtmlBodyPath, | 70 if (StartsWith(request.relative_url, kPageHtmlPath)) { |
| 50 base::CompareCase::INSENSITIVE_ASCII)) { | 71 std::string title; |
| 51 return EchoPageHTMLBodyInResponse(request); | 72 std::string body; |
| 73 |
| 74 GURL request_url = request.GetURL(); |
| 75 |
| 76 std::string encoded_title; |
| 77 bool title_found = net::GetValueForKeyInQuery( |
| 78 request_url, kPageHtmlTitleParamName, &encoded_title); |
| 79 if (title_found) { |
| 80 title = DecodeQueryParamValue(encoded_title); |
| 81 } |
| 82 |
| 83 std::string encoded_body; |
| 84 bool body_found = net::GetValueForKeyInQuery( |
| 85 request_url, kPageHtmlBodyParamName, &encoded_body); |
| 86 if (body_found) { |
| 87 body = DecodeQueryParamValue(encoded_body); |
| 88 } |
| 89 |
| 90 return CreatePageHTMLResponse(title, body); |
| 52 } | 91 } |
| 53 return nullptr; | 92 return nullptr; |
| 54 } | 93 } |
| 55 | 94 |
| 56 } // namespace | 95 } // namespace |
| 57 | 96 |
| 58 namespace ios_web_view { | 97 namespace ios_web_view { |
| 59 | 98 |
| 60 ChromeWebViewTest::ChromeWebViewTest() | 99 ChromeWebViewTest::ChromeWebViewTest() |
| 61 : test_server_(base::MakeUnique<net::EmbeddedTestServer>( | 100 : test_server_(base::MakeUnique<net::EmbeddedTestServer>( |
| 62 net::test_server::EmbeddedTestServer::TYPE_HTTP)) { | 101 net::test_server::EmbeddedTestServer::TYPE_HTTP)) { |
| 63 net::test_server::RegisterDefaultHandlers(test_server_.get()); | |
| 64 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); | 102 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); |
| 65 } | 103 } |
| 66 | 104 |
| 67 ChromeWebViewTest::~ChromeWebViewTest() = default; | 105 ChromeWebViewTest::~ChromeWebViewTest() = default; |
| 68 | 106 |
| 69 void ChromeWebViewTest::SetUp() { | 107 void ChromeWebViewTest::SetUp() { |
| 70 PlatformTest::SetUp(); | 108 PlatformTest::SetUp(); |
| 71 ASSERT_TRUE(test_server_->Start()); | 109 ASSERT_TRUE(test_server_->Start()); |
| 72 } | 110 } |
| 73 | 111 |
| 74 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { | 112 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { |
| 75 return test_server_->GetURL("/echotitle/" + title); | 113 return GetUrlForPageWithTitleAndBody(title, std::string()); |
| 76 } | 114 } |
| 77 | 115 |
| 78 GURL ChromeWebViewTest::GetUrlForPageWithHTMLBody(const std::string& html) { | 116 GURL ChromeWebViewTest::GetUrlForPageWithHtmlBody(const std::string& html) { |
| 79 std::string base64_html; | 117 return GetUrlForPageWithTitleAndBody(std::string(), html); |
| 80 base::Base64Encode(html, &base64_html); | 118 } |
| 81 return test_server_->GetURL(kPageHtmlBodyPath + base64_html); | 119 |
| 120 GURL ChromeWebViewTest::GetUrlForPageWithTitleAndBody(const std::string& title, |
| 121 const std::string& body) { |
| 122 GURL url = test_server_->GetURL(kPageHtmlPath); |
| 123 |
| 124 // Encode |title| and |body| in url query in order to build the server |
| 125 // response later in TestRequestHandler. |
| 126 std::string encoded_title = EncodeQueryParamValue(title); |
| 127 url = net::AppendQueryParameter(url, kPageHtmlTitleParamName, encoded_title); |
| 128 std::string encoded_body = EncodeQueryParamValue(body); |
| 129 url = net::AppendQueryParameter(url, kPageHtmlBodyParamName, encoded_body); |
| 130 |
| 131 return url; |
| 82 } | 132 } |
| 83 | 133 |
| 84 void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { | 134 void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { |
| 85 [web_view loadRequest:[NSURLRequest requestWithURL:url]]; | 135 [web_view loadRequest:[NSURLRequest requestWithURL:url]]; |
| 86 | 136 |
| 87 WaitForPageLoadCompletion(web_view); | 137 WaitForPageLoadCompletion(web_view); |
| 88 } | 138 } |
| 89 | 139 |
| 90 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { | 140 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { |
| 91 BOOL success = | 141 BOOL success = |
| 92 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ | 142 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ |
| 93 return !web_view.isLoading; | 143 return !web_view.isLoading; |
| 94 }); | 144 }); |
| 95 ASSERT_TRUE(success); | 145 ASSERT_TRUE(success); |
| 96 } | 146 } |
| 97 | 147 |
| 98 } // namespace ios_web_view | 148 } // namespace ios_web_view |
| OLD | NEW |