Chromium Code Reviews| 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 #import "ios/testing/wait_util.h" | 12 #import "ios/testing/wait_util.h" |
| 13 #include "net/test/embedded_test_server/default_handlers.h" | |
| 14 #include "net/test/embedded_test_server/embedded_test_server.h" | 13 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 15 #include "net/test/embedded_test_server/http_request.h" | 14 #include "net/test/embedded_test_server/http_request.h" |
| 16 #include "net/test/embedded_test_server/http_response.h" | 15 #include "net/test/embedded_test_server/http_response.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
| 21 #endif | 20 #endif |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 // Test server path which echos the remainder of the url as html. The html | 24 // Test server path which renders a basic html page. |
| 26 // must be base64 encoded. | 25 const char kPageHTMLPath[] = "/PageHtml?"; |
| 27 const char kPageHtmlBodyPath[] = "/PageHtmlBody?"; | 26 // URL parameter for html body. Value must be base64 encoded. |
| 27 const char kPageHTMLBodyParamName[] = "body="; | |
| 28 // URL parameter for page title. Value must be base64 encoded. | |
| 29 const char kPageHTMLTitleParamName[] = "title="; | |
| 28 | 30 |
| 29 // Generates an html response from a request to |kPageHtmlBodyPath|. | 31 // Generates an html response. |
| 30 std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( | 32 std::unique_ptr<net::test_server::HttpResponse> CreatePageHTMLResponse( |
| 31 const net::test_server::HttpRequest& request) { | 33 const std::string& title, |
| 32 DCHECK(base::StartsWith(request.relative_url, kPageHtmlBodyPath, | 34 const std::string& body) { |
| 33 base::CompareCase::INSENSITIVE_ASCII)); | 35 std::string html = "<html><head><title>" + title + "</title></head><body>" + |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
Please use StringPrint instead of +
michaeldo
2017/05/26 14:59:02
Done.
| |
| 34 | 36 body + "</body></html>"; |
| 35 std::string body = request.relative_url.substr(strlen(kPageHtmlBodyPath)); | |
| 36 | |
| 37 std::string unescaped_body; | |
| 38 base::Base64Decode(body, &unescaped_body); | |
| 39 std::string html = "<html><body>" + unescaped_body + "</body></html>"; | |
| 40 | 37 |
| 41 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); | 38 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); |
| 42 http_response->set_content(html); | 39 http_response->set_content(html); |
| 43 return std::move(http_response); | 40 return std::move(http_response); |
| 44 } | 41 } |
| 45 | 42 |
| 46 // Maps test server requests to responses. | 43 // Maps test server requests to responses. |
| 47 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( | 44 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( |
| 48 const net::test_server::HttpRequest& request) { | 45 const net::test_server::HttpRequest& request) { |
| 49 if (base::StartsWith(request.relative_url, kPageHtmlBodyPath, | 46 if (base::StartsWith(request.relative_url, kPageHTMLPath, |
| 50 base::CompareCase::INSENSITIVE_ASCII)) { | 47 base::CompareCase::INSENSITIVE_ASCII)) { |
| 51 return EchoPageHTMLBodyInResponse(request); | 48 std::string title = std::string(); |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
It's quite hard to tell what this method does. Do
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
s/std::string title = std::string();/std::string t
michaeldo
2017/05/26 14:59:01
Done.
michaeldo
2017/05/26 14:59:01
I agree, this method went through many local revis
michaeldo
2017/05/26 14:59:02
I agree, this method went through many local revis
| |
| 49 std::string body = std::string(); | |
| 50 | |
| 51 GURL request_url = request.GetURL(); | |
| 52 if (request_url.has_query()) { | |
| 53 std::vector<std::string> query_list = | |
| 54 base::SplitString(request_url.query(), "&", base::KEEP_WHITESPACE, | |
| 55 base::SPLIT_WANT_ALL); | |
| 56 | |
| 57 for (const auto& query : query_list) { | |
| 58 if (base::StartsWith(query, kPageHTMLTitleParamName, | |
| 59 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 60 std::string encoded_title = | |
| 61 query.substr(strlen(kPageHTMLTitleParamName)); | |
| 62 std::string decoded_title; | |
| 63 base::Base64Decode(encoded_title, &decoded_title); | |
| 64 title = decoded_title; | |
| 65 } else if (base::StartsWith(query, kPageHTMLBodyParamName, | |
| 66 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 67 std::string encoded_body = | |
| 68 query.substr(strlen(kPageHTMLBodyParamName)); | |
| 69 std::string decoded_body; | |
| 70 base::Base64Decode(encoded_body, &decoded_body); | |
| 71 body = decoded_body; | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 return CreatePageHTMLResponse(title, body); | |
| 52 } | 76 } |
| 53 return nullptr; | 77 return nullptr; |
| 54 } | 78 } |
| 55 | 79 |
| 56 } // namespace | 80 } // namespace |
| 57 | 81 |
| 58 namespace ios_web_view { | 82 namespace ios_web_view { |
| 59 | 83 |
| 60 ChromeWebViewTest::ChromeWebViewTest() | 84 ChromeWebViewTest::ChromeWebViewTest() |
| 61 : test_server_(base::MakeUnique<net::EmbeddedTestServer>( | 85 : test_server_(base::MakeUnique<net::EmbeddedTestServer>( |
| 62 net::test_server::EmbeddedTestServer::TYPE_HTTP)) { | 86 net::test_server::EmbeddedTestServer::TYPE_HTTP)) { |
| 63 net::test_server::RegisterDefaultHandlers(test_server_.get()); | |
| 64 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); | 87 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); |
| 65 } | 88 } |
| 66 | 89 |
| 67 ChromeWebViewTest::~ChromeWebViewTest() = default; | 90 ChromeWebViewTest::~ChromeWebViewTest() = default; |
| 68 | 91 |
| 69 void ChromeWebViewTest::SetUp() { | 92 void ChromeWebViewTest::SetUp() { |
| 70 PlatformTest::SetUp(); | 93 PlatformTest::SetUp(); |
| 71 ASSERT_TRUE(test_server_->Start()); | 94 ASSERT_TRUE(test_server_->Start()); |
| 72 } | 95 } |
| 73 | 96 |
| 74 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { | 97 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { |
| 75 return test_server_->GetURL("/echotitle/" + title); | 98 return GetUrlForPageWithTitleAndBody(title, std::string()); |
| 76 } | 99 } |
| 77 | 100 |
| 78 GURL ChromeWebViewTest::GetUrlForPageWithHTMLBody(const std::string& html) { | 101 GURL ChromeWebViewTest::GetUrlForPageWithHTMLBody(const std::string& html) { |
| 79 std::string base64_html; | 102 return GetUrlForPageWithTitleAndBody(std::string(), html); |
| 80 base::Base64Encode(html, &base64_html); | 103 } |
| 81 return test_server_->GetURL(kPageHtmlBodyPath + base64_html); | 104 |
| 105 GURL ChromeWebViewTest::GetUrlForPageWithTitleAndBody(const std::string& title, | |
| 106 const std::string& body) { | |
| 107 std::string url_params = std::string(); | |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
Please describe in comments how query is used.
michaeldo
2017/05/26 14:59:02
Done.
| |
| 108 std::string base64_title; | |
| 109 base::Base64Encode(title, &base64_title); | |
| 110 url_params += kPageHTMLTitleParamName + base64_title; | |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
Please use GURL API to construct the url.
michaeldo
2017/05/26 14:59:02
Done.
| |
| 111 | |
| 112 url_params += "&"; | |
| 113 | |
| 114 std::string base64_body; | |
| 115 base::Base64Encode(body, &base64_body); | |
| 116 url_params += kPageHTMLBodyParamName + base64_body; | |
| 117 | |
| 118 return test_server_->GetURL(kPageHTMLPath + url_params); | |
| 82 } | 119 } |
| 83 | 120 |
| 84 void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { | 121 void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { |
| 85 [web_view loadRequest:[NSURLRequest requestWithURL:url]]; | 122 [web_view loadRequest:[NSURLRequest requestWithURL:url]]; |
| 86 | 123 |
| 87 WaitForPageLoadCompletion(web_view); | 124 WaitForPageLoadCompletion(web_view); |
| 88 } | 125 } |
| 89 | 126 |
| 90 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { | 127 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { |
| 91 BOOL success = | 128 BOOL success = |
| 92 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ | 129 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ |
| 93 return !web_view.isLoading; | 130 return !web_view.isLoading; |
| 94 }); | 131 }); |
| 95 ASSERT_TRUE(success); | 132 ASSERT_TRUE(success); |
| 96 } | 133 } |
| 97 | 134 |
| 98 } // namespace ios_web_view | 135 } // namespace ios_web_view |
| OLD | NEW |