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 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.
| |
26 | |
27 std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( | |
28 const net::test_server::HttpRequest& request) { | |
29 DCHECK(base::StartsWith(request.relative_url, kPageHTMLBodyPath, | |
30 base::CompareCase::INSENSITIVE_ASCII)); | |
31 | |
32 std::string body = request.relative_url.substr(strlen(kPageHTMLBodyPath)); | |
33 | |
34 std::string unescaped_body; | |
35 base::Base64Decode(body, &unescaped_body); | |
36 std::string html = "<html><body>" + unescaped_body + "</body></html>"; | |
37 | |
38 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); | |
39 http_response->set_content(html); | |
40 return std::move(http_response); | |
41 } | |
42 | |
43 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( | |
44 const net::test_server::HttpRequest& request) { | |
45 if (base::StartsWith(request.relative_url, kPageHTMLBodyPath, | |
46 base::CompareCase::INSENSITIVE_ASCII)) { | |
47 return EchoPageHTMLBodyInResponse(request); | |
48 } | |
49 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.
| |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 namespace ios_web_view { | |
55 | |
56 ChromeWebViewTest::ChromeWebViewTest() {} | |
57 | |
58 ChromeWebViewTest::~ChromeWebViewTest() = default; | |
59 | |
60 void ChromeWebViewTest::SetUp() { | |
61 PlatformTest::SetUp(); | |
62 | |
63 test_server_ = base::MakeUnique<net::EmbeddedTestServer>( | |
64 net::test_server::EmbeddedTestServer::TYPE_HTTP); | |
65 net::test_server::RegisterDefaultHandlers(test_server_.get()); | |
66 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); | |
67 ASSERT_TRUE(test_server_->Start()); | |
68 } | |
69 | |
70 void ChromeWebViewTest::TearDown() { | |
71 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.
| |
72 PlatformTest::TearDown(); | |
73 } | |
74 | |
75 GURL ChromeWebViewTest::GetURLForPageWithTitle(const std::string& title) { | |
76 return test_server_->GetURL("/echotitle/" + title); | |
77 } | |
78 | |
79 GURL ChromeWebViewTest::GetURLForPageWithHTMLBody(const std::string& html) { | |
80 std::string base64_html; | |
81 base::Base64Encode(html, &base64_html); | |
82 return test_server_->GetURL(kPageHTMLBodyPath + base64_html); | |
83 } | |
84 | |
85 void ChromeWebViewTest::LoadURL(CWVWebView* web_view, NSURL* URL) { | |
86 [web_view loadRequest:[NSURLRequest requestWithURL:URL]]; | |
87 | |
88 WaitForPageLoadCompletion(web_view); | |
89 } | |
90 | |
91 void ChromeWebViewTest::WaitForPageLoadCompletion(CWVWebView* web_view) { | |
92 BOOL success = | |
93 testing::WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{ | |
94 return !web_view.isLoading; | |
95 }); | |
96 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.
| |
97 } | |
98 | |
99 } // namespace ios_web_view | |
OLD | NEW |