OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ios/web_view/test/chrome_web_view_test.h" | |
6 | |
7 #import <ChromeWebView/ChromeWebView.h> | |
8 #import <Foundation/Foundation.h> | |
9 | |
10 #include "base/base64.h" | |
11 #import "base/memory/ptr_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" | |
15 #include "net/test/embedded_test_server/http_request.h" | |
16 #include "net/test/embedded_test_server/http_response.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
20 #error "This file requires ARC support." | |
21 #endif | |
22 | |
23 namespace { | |
24 | |
25 // Test server path which echos the remainder of the url as html. The html | |
26 // must be base64 encoded. | |
27 const char kPageHtmlBodyPath[] = "/PageHtmlBody?"; | |
28 | |
29 // Generates an html response from a request to |kPageHtmlBodyPath|. | |
30 std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( | |
31 const net::test_server::HttpRequest& request) { | |
32 DCHECK(base::StartsWith(request.relative_url, kPageHtmlBodyPath, | |
33 base::CompareCase::INSENSITIVE_ASCII)); | |
34 | |
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 | |
41 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); | |
42 http_response->set_content(html); | |
43 return std::move(http_response); | |
44 } | |
45 | |
46 // Maps test server requests to responses. | |
47 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( | |
48 const net::test_server::HttpRequest& request) { | |
49 if (base::StartsWith(request.relative_url, kPageHtmlBodyPath, | |
50 base::CompareCase::INSENSITIVE_ASCII)) { | |
51 return EchoPageHTMLBodyInResponse(request); | |
52 } | |
53 return nullptr; | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 namespace ios_web_view { | |
59 | |
60 ChromeWebViewTest::ChromeWebViewTest() | |
61 : test_server_(base::MakeUnique<net::EmbeddedTestServer>( | |
62 net::test_server::EmbeddedTestServer::TYPE_HTTP)) {} | |
63 | |
64 ChromeWebViewTest::~ChromeWebViewTest() = default; | |
65 | |
66 void ChromeWebViewTest::SetUp() { | |
67 PlatformTest::SetUp(); | |
68 | |
69 net::test_server::RegisterDefaultHandlers(test_server_.get()); | |
Eugene But (OOO till 7-30)
2017/05/24 15:32:14
nit: It is preferable to have SetUp code in constr
michaeldo
2017/05/24 16:37:08
Makes sense, I left the Start call here, but the c
| |
70 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); | |
71 ASSERT_TRUE(test_server_->Start()); | |
72 } | |
73 | |
74 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { | |
75 return test_server_->GetURL("/echotitle/" + title); | |
76 } | |
77 | |
78 GURL ChromeWebViewTest::GetUrlForPageWithHTMLBody(const std::string& html) { | |
79 std::string base64_html; | |
80 base::Base64Encode(html, &base64_html); | |
81 return test_server_->GetURL(kPageHtmlBodyPath + base64_html); | |
82 } | |
83 | |
84 void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { | |
85 [web_view loadRequest:[NSURLRequest requestWithURL:url]]; | |
86 | |
87 WaitForPageLoadCompletion(web_view); | |
88 } | |
89 | |
90 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { | |
91 BOOL success = | |
92 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ | |
93 return !web_view.isLoading; | |
94 }); | |
95 ASSERT_TRUE(success); | |
96 } | |
97 | |
98 } // namespace ios_web_view | |
OLD | NEW |