Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: ios/web_view/test/chrome_web_view_test.mm

Issue 2902403002: Add KVO compliant title property to CWVWebView. (Closed)
Patch Set: Respond to comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/test/embedded_test_server/embedded_test_server.h" 14 #include "net/test/embedded_test_server/embedded_test_server.h"
15 #include "net/test/embedded_test_server/http_request.h" 15 #include "net/test/embedded_test_server/http_request.h"
16 #include "net/test/embedded_test_server/http_response.h" 16 #include "net/test/embedded_test_server/http_response.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 #if !defined(__has_feature) || !__has_feature(objc_arc) 19 #if !defined(__has_feature) || !__has_feature(objc_arc)
20 #error "This file requires ARC support." 20 #error "This file requires ARC support."
21 #endif 21 #endif
22 22
23 namespace { 23 namespace {
24 24
25 // Test server path which echos the remainder of the url as html. The html 25 // Test server path which renders a basic html page.
26 // must be base64 encoded. 26 const char kPageHTMLPath[] = "/PageHtml?";
Eugene But (OOO till 7-30) 2017/05/31 22:10:01 s/HTML/Html
michaeldo 2017/06/01 15:45:53 Done.
27 const char kPageHtmlBodyPath[] = "/PageHtmlBody?"; 27 // URL parameter for html body. Value must be base64 encoded.
28 const char kPageHTMLBodyParamName[] = "body=";
29 // URL parameter for page title. Value must be base64 encoded.
30 const char kPageHTMLTitleParamName[] = "title=";
28 31
29 // Generates an html response from a request to |kPageHtmlBodyPath|. 32 // Generates an html response.
30 std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( 33 std::unique_ptr<net::test_server::HttpResponse> CreatePageHTMLResponse(
31 const net::test_server::HttpRequest& request) { 34 const std::string& title,
32 DCHECK(base::StartsWith(request.relative_url, kPageHtmlBodyPath, 35 const std::string& body) {
33 base::CompareCase::INSENSITIVE_ASCII)); 36 std::string html = base::StringPrintf(
34 37 "<html><head><title>%s</title></head><body>%s</body></html>",
35 std::string body = request.relative_url.substr(strlen(kPageHtmlBodyPath)); 38 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 39
41 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); 40 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>();
42 http_response->set_content(html); 41 http_response->set_content(html);
43 return std::move(http_response); 42 return std::move(http_response);
44 } 43 }
45 44
45 // Splits a URL's |query_string| into separate key=value strings.
46 std::vector<std::string> SplitQueryString(std::string query_string) {
Eugene But (OOO till 7-30) 2017/05/31 22:10:01 How about using query_parser.h API?
michaeldo 2017/06/01 15:45:54 From components/query_parser/query_parser.h? Based
47 return base::SplitString(query_string, "&", base::KEEP_WHITESPACE,
48 base::SPLIT_WANT_ALL);
49 }
50
51 // Returns true if |string| starts with |prefix|. String comparison is case
52 // insensitive.
53 bool StartsWith(std::string string, std::string prefix) {
54 return base::StartsWith(string, prefix, base::CompareCase::INSENSITIVE_ASCII);
Eugene But (OOO till 7-30) 2017/05/31 22:10:01 Why do we need case insensitive comparison?
michaeldo 2017/06/01 15:45:53 It doesn't need to be insensitive, changed to sens
55 }
56
57 // Encodes the |string| for use as the value of a url parameter.
58 std::string EncodeQueryParamValue(std::string string) {
59 std::string encoded_string;
60 base::Base64Encode(string, &encoded_string);
61 return encoded_string;
62 }
63
64 // Decodes the |encoded_string|. Undoes the encoding performed by
65 // |EncodeQueryParamValue|.
66 std::string DecodeQueryParamValue(std::string encoded_string) {
67 std::string decoded_string;
68 base::Base64Decode(encoded_string, &decoded_string);
69 return decoded_string;
70 }
71
46 // Maps test server requests to responses. 72 // Maps test server requests to responses.
47 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( 73 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler(
48 const net::test_server::HttpRequest& request) { 74 const net::test_server::HttpRequest& request) {
49 if (base::StartsWith(request.relative_url, kPageHtmlBodyPath, 75 if (StartsWith(request.relative_url, kPageHTMLPath)) {
50 base::CompareCase::INSENSITIVE_ASCII)) { 76 std::string title;
51 return EchoPageHTMLBodyInResponse(request); 77 std::string body;
78
79 GURL request_url = request.GetURL();
80 if (request_url.has_query()) {
81 for (const auto& query : SplitQueryString(request_url.query())) {
82 if (StartsWith(query, kPageHTMLTitleParamName)) {
83 std::string encoded_title =
84 query.substr(strlen(kPageHTMLTitleParamName));
85 title = DecodeQueryParamValue(encoded_title);
86 } else if (StartsWith(query, kPageHTMLBodyParamName)) {
87 std::string encoded_body =
88 query.substr(strlen(kPageHTMLBodyParamName));
89 body = DecodeQueryParamValue(encoded_body);
90 }
91 }
92 }
93 return CreatePageHTMLResponse(title, body);
52 } 94 }
53 return nullptr; 95 return nullptr;
54 } 96 }
55 97
56 } // namespace 98 } // namespace
57 99
58 namespace ios_web_view { 100 namespace ios_web_view {
59 101
60 ChromeWebViewTest::ChromeWebViewTest() 102 ChromeWebViewTest::ChromeWebViewTest()
61 : test_server_(base::MakeUnique<net::EmbeddedTestServer>( 103 : test_server_(base::MakeUnique<net::EmbeddedTestServer>(
62 net::test_server::EmbeddedTestServer::TYPE_HTTP)) { 104 net::test_server::EmbeddedTestServer::TYPE_HTTP)) {
63 net::test_server::RegisterDefaultHandlers(test_server_.get());
64 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); 105 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler));
65 } 106 }
66 107
67 ChromeWebViewTest::~ChromeWebViewTest() = default; 108 ChromeWebViewTest::~ChromeWebViewTest() = default;
68 109
69 void ChromeWebViewTest::SetUp() { 110 void ChromeWebViewTest::SetUp() {
70 PlatformTest::SetUp(); 111 PlatformTest::SetUp();
71 ASSERT_TRUE(test_server_->Start()); 112 ASSERT_TRUE(test_server_->Start());
72 } 113 }
73 114
74 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { 115 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) {
75 return test_server_->GetURL("/echotitle/" + title); 116 return GetUrlForPageWithTitleAndBody(title, std::string());
76 } 117 }
77 118
78 GURL ChromeWebViewTest::GetUrlForPageWithHTMLBody(const std::string& html) { 119 GURL ChromeWebViewTest::GetUrlForPageWithHTMLBody(const std::string& html) {
79 std::string base64_html; 120 return GetUrlForPageWithTitleAndBody(std::string(), html);
80 base::Base64Encode(html, &base64_html); 121 }
81 return test_server_->GetURL(kPageHtmlBodyPath + base64_html); 122
123 GURL ChromeWebViewTest::GetUrlForPageWithTitleAndBody(const std::string& title,
124 const std::string& body) {
125 GURL url = test_server_->GetURL(kPageHTMLPath);
126
127 // Encode |title| and |body| in url query in order to build the server
128 // response later in TestRequestHandler.
129 std::string url_params = std::string();
130 url_params.append(kPageHTMLTitleParamName);
Eugene But (OOO till 7-30) 2017/05/31 22:10:01 How about using AppendQueryParameter?
michaeldo 2017/06/01 15:45:53 Yes! much cleaner. Done.
131 url_params.append(EncodeQueryParamValue(title));
132 url_params.append("&");
133 url_params.append(kPageHTMLBodyParamName);
134 url_params.append(EncodeQueryParamValue(body));
135
136 GURL::Replacements replacements;
137 replacements.SetQueryStr(url_params);
138 return url.ReplaceComponents(replacements);
82 } 139 }
83 140
84 void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { 141 void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) {
85 [web_view loadRequest:[NSURLRequest requestWithURL:url]]; 142 [web_view loadRequest:[NSURLRequest requestWithURL:url]];
86 143
87 WaitForPageLoadCompletion(web_view); 144 WaitForPageLoadCompletion(web_view);
88 } 145 }
89 146
90 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { 147 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) {
91 BOOL success = 148 BOOL success =
92 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ 149 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{
93 return !web_view.isLoading; 150 return !web_view.isLoading;
94 }); 151 });
95 ASSERT_TRUE(success); 152 ASSERT_TRUE(success);
96 } 153 }
97 154
98 } // namespace ios_web_view 155 } // namespace ios_web_view
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698