Commit 283e3821 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

Convert WebIntTests to net::EmbeddedTestServer

Bug: 891834
Change-Id: I2f7247085324fc1c31bd7092b51d181fbe4e2328
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2377512
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Auto-Submit: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802360}
parent ae16e962
...@@ -11,12 +11,11 @@ ...@@ -11,12 +11,11 @@
#import "ios/web/navigation/navigation_item_impl.h" #import "ios/web/navigation/navigation_item_impl.h"
#import "ios/web/public/navigation/navigation_item.h" #import "ios/web/public/navigation/navigation_item.h"
#import "ios/web/public/navigation/navigation_manager.h" #import "ios/web/public/navigation/navigation_manager.h"
#import "ios/web/public/test/http_server/http_server.h"
#include "ios/web/public/test/http_server/http_server_util.h"
#import "ios/web/public/test/web_view_interaction_test_util.h" #import "ios/web/public/test/web_view_interaction_test_util.h"
#import "ios/web/public/web_client.h" #import "ios/web/public/web_client.h"
#import "ios/web/public/web_state.h" #import "ios/web/public/web_state.h"
#import "ios/web/test/web_int_test.h" #import "ios/web/test/web_int_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
#include "url/url_canon.h" #include "url/url_canon.h"
...@@ -41,8 +40,7 @@ namespace { ...@@ -41,8 +40,7 @@ namespace {
// and is removed once a button is tapped. Verifying that the onload text is // and is removed once a button is tapped. Verifying that the onload text is
// visible after tapping a button is equivalent to checking that a load has // visible after tapping a button is equivalent to checking that a load has
// occurred as the result of the button tap. // occurred as the result of the button tap.
const char kHistoryStateOperationsTestUrl[] = const char kHistoryStateOperationsTestUrl[] = "/state_operations.html";
"http://ios/testing/data/http_server_files/state_operations.html";
// Button IDs used in the window.location test page. // Button IDs used in the window.location test page.
const char kPushStateId[] = "push-state"; const char kPushStateId[] = "push-state";
...@@ -67,8 +65,13 @@ class HistoryStateOperationsTest : public web::WebIntTest { ...@@ -67,8 +65,13 @@ class HistoryStateOperationsTest : public web::WebIntTest {
web::WebIntTest::SetUp(); web::WebIntTest::SetUp();
// Load the history state test page. // Load the history state test page.
test_server_ = std::make_unique<net::EmbeddedTestServer>();
test_server_->ServeFilesFromSourceDirectory(
base::FilePath("ios/testing/data/http_server_files/"));
ASSERT_TRUE(test_server_->Start());
state_operations_url_ = state_operations_url_ =
web::test::HttpServer::MakeUrl(kHistoryStateOperationsTestUrl); test_server_->GetURL(kHistoryStateOperationsTestUrl);
ASSERT_TRUE(LoadUrl(state_operations_url())); ASSERT_TRUE(LoadUrl(state_operations_url()));
} }
...@@ -123,6 +126,8 @@ class HistoryStateOperationsTest : public web::WebIntTest { ...@@ -123,6 +126,8 @@ class HistoryStateOperationsTest : public web::WebIntTest {
EXPECT_TRUE(completed) << "NoOp text failed to be visible."; EXPECT_TRUE(completed) << "NoOp text failed to be visible.";
} }
std::unique_ptr<net::EmbeddedTestServer> test_server_;
private: private:
GURL state_operations_url_; GURL state_operations_url_;
}; };
...@@ -198,8 +203,7 @@ TEST_F(HistoryStateOperationsTest, NoOpPushDifferentOrigin) { ...@@ -198,8 +203,7 @@ TEST_F(HistoryStateOperationsTest, NoOpPushDifferentOrigin) {
// occurred as the result of the pushState() call. // occurred as the result of the pushState() call.
std::string empty_state; std::string empty_state;
std::string empty_title; std::string empty_title;
std::string new_port_string = base::NumberToString( std::string new_port_string = base::NumberToString(test_server_->port() + 1);
web::test::HttpServer::GetSharedInstance().GetPort() + 1);
url::Replacements<char> port_replacement; url::Replacements<char> port_replacement;
port_replacement.SetPort(new_port_string.c_str(), port_replacement.SetPort(new_port_string.c_str(),
url::Component(0, new_port_string.length())); url::Component(0, new_port_string.length()));
...@@ -220,8 +224,7 @@ TEST_F(HistoryStateOperationsTest, NoOpReplaceDifferentOrigin) { ...@@ -220,8 +224,7 @@ TEST_F(HistoryStateOperationsTest, NoOpReplaceDifferentOrigin) {
// occurred as the result of the pushState() call. // occurred as the result of the pushState() call.
std::string empty_state; std::string empty_state;
std::string empty_title; std::string empty_title;
std::string new_port_string = base::NumberToString( std::string new_port_string = base::NumberToString(test_server_->port() + 1);
web::test::HttpServer::GetSharedInstance().GetPort() + 1);
url::Replacements<char> port_replacement; url::Replacements<char> port_replacement;
port_replacement.SetPort(new_port_string.c_str(), port_replacement.SetPort(new_port_string.c_str(),
url::Component(0, new_port_string.length())); url::Component(0, new_port_string.length()));
...@@ -451,12 +454,9 @@ TEST_F(HistoryStateOperationsTest, ReplaceStateNoHashChangeEvent) { ...@@ -451,12 +454,9 @@ TEST_F(HistoryStateOperationsTest, ReplaceStateNoHashChangeEvent) {
// Regression test for crbug.com/788464. // Regression test for crbug.com/788464.
TEST_F(HistoryStateOperationsTest, ReplaceStateThenReload) { TEST_F(HistoryStateOperationsTest, ReplaceStateThenReload) {
GURL url = web::test::HttpServer::MakeUrl( GURL url = test_server_->GetURL("/onload_replacestate_reload.html");
"http://ios/testing/data/http_server_files/"
"onload_replacestate_reload.html");
ASSERT_TRUE(LoadUrl(url)); ASSERT_TRUE(LoadUrl(url));
GURL new_url = web::test::HttpServer::MakeUrl( GURL new_url = test_server_->GetURL("/pony.html");
"http://ios/testing/data/http_server_files/pony.html");
BOOL completed = base::test::ios::WaitUntilConditionOrTimeout( BOOL completed = base::test::ios::WaitUntilConditionOrTimeout(
kWaitForStateUpdateTimeout, ^{ kWaitForStateUpdateTimeout, ^{
return GetLastCommittedItem()->GetURL() == new_url; return GetLastCommittedItem()->GetURL() == new_url;
......
...@@ -8,12 +8,11 @@ ...@@ -8,12 +8,11 @@
#import "base/test/ios/wait_util.h" #import "base/test/ios/wait_util.h"
#import "ios/web/public/navigation/navigation_item.h" #import "ios/web/public/navigation/navigation_item.h"
#import "ios/web/public/navigation/navigation_manager.h" #import "ios/web/public/navigation/navigation_manager.h"
#import "ios/web/public/test/http_server/http_server.h"
#include "ios/web/public/test/http_server/http_server_util.h"
#import "ios/web/public/test/web_view_interaction_test_util.h" #import "ios/web/public/test/web_view_interaction_test_util.h"
#import "ios/web/public/web_state.h" #import "ios/web/public/web_state.h"
#include "ios/web/public/web_state_observer.h" #include "ios/web/public/web_state_observer.h"
#import "ios/web/test/web_int_test.h" #import "ios/web/test/web_int_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
...@@ -35,8 +34,7 @@ namespace { ...@@ -35,8 +34,7 @@ namespace {
// and is removed once a button is tapped. Verifying that the onload text is // and is removed once a button is tapped. Verifying that the onload text is
// visible after tapping a button is equivalent to checking that a load has // visible after tapping a button is equivalent to checking that a load has
// occurred as the result of the button tap. // occurred as the result of the button tap.
const char kWindowLocationTestURL[] = const char kWindowLocationTestURL[] = "/window_location.html";
"http://ios/testing/data/http_server_files/window_location.html";
// Button IDs used in the window.location test page. // Button IDs used in the window.location test page.
const char kWindowLocationAssignID[] = "location-assign"; const char kWindowLocationAssignID[] = "location-assign";
...@@ -51,8 +49,7 @@ NSString* const kOnLoadCheckScript = @"isOnLoadTextVisible()"; ...@@ -51,8 +49,7 @@ NSString* const kOnLoadCheckScript = @"isOnLoadTextVisible()";
NSString* const kNoOpCheckScript = @"isNoOpTextVisible()"; NSString* const kNoOpCheckScript = @"isNoOpTextVisible()";
// URL of a sample file-based page. // URL of a sample file-based page.
const char kSampleFileBasedURL[] = const char kSampleFileBasedURL[] = "/chromium_logo_page.html";
"http://ios/testing/data/http_server_files/chromium_logo_page.html";
} // namespace } // namespace
...@@ -62,9 +59,13 @@ class WindowLocationTest : public web::WebIntTest { ...@@ -62,9 +59,13 @@ class WindowLocationTest : public web::WebIntTest {
void SetUp() override { void SetUp() override {
web::WebIntTest::SetUp(); web::WebIntTest::SetUp();
test_server_ = std::make_unique<net::EmbeddedTestServer>();
test_server_->ServeFilesFromSourceDirectory(
base::FilePath("ios/testing/data/http_server_files/"));
ASSERT_TRUE(test_server_->Start());
// Load the window.location test page. // Load the window.location test page.
window_location_url_ = window_location_url_ = test_server_->GetURL(kWindowLocationTestURL);
web::test::HttpServer::MakeUrl(kWindowLocationTestURL);
ASSERT_TRUE(LoadUrl(window_location_url())); ASSERT_TRUE(LoadUrl(window_location_url()));
} }
...@@ -102,6 +103,8 @@ class WindowLocationTest : public web::WebIntTest { ...@@ -102,6 +103,8 @@ class WindowLocationTest : public web::WebIntTest {
return [text_visible boolValue]; return [text_visible boolValue];
} }
std::unique_ptr<net::EmbeddedTestServer> test_server_;
private: private:
GURL window_location_url_; GURL window_location_url_;
}; };
...@@ -127,7 +130,7 @@ TEST_F(WindowLocationTest, MAYBE_Assign) { ...@@ -127,7 +130,7 @@ TEST_F(WindowLocationTest, MAYBE_Assign) {
// Set the window.location test URL and tap the window.location.assign() // Set the window.location test URL and tap the window.location.assign()
// button. // button.
GURL sample_url = web::test::HttpServer::MakeUrl(kSampleFileBasedURL); GURL sample_url = test_server_->GetURL(kSampleFileBasedURL);
SetWindowLocationUrl(sample_url); SetWindowLocationUrl(sample_url);
ASSERT_TRUE(ExecuteBlockAndWaitForLoad(sample_url, ^{ ASSERT_TRUE(ExecuteBlockAndWaitForLoad(sample_url, ^{
ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(),
...@@ -171,7 +174,7 @@ TEST_F(WindowLocationTest, DISABLED_Replace) { ...@@ -171,7 +174,7 @@ TEST_F(WindowLocationTest, DISABLED_Replace) {
// Set the window.location test URL and tap the window.location.replace() // Set the window.location test URL and tap the window.location.replace()
// button. // button.
GURL sample_url = web::test::HttpServer::MakeUrl(kSampleFileBasedURL); GURL sample_url = test_server_->GetURL(kSampleFileBasedURL);
SetWindowLocationUrl(sample_url); SetWindowLocationUrl(sample_url);
ASSERT_TRUE(ExecuteBlockAndWaitForLoad(sample_url, ^{ ASSERT_TRUE(ExecuteBlockAndWaitForLoad(sample_url, ^{
ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(),
...@@ -248,7 +251,7 @@ TEST_F(WindowLocationTest, MAYBE_WindowLocationSetToDOMString) { ...@@ -248,7 +251,7 @@ TEST_F(WindowLocationTest, MAYBE_WindowLocationSetToDOMString) {
// Set the window.location test URL and tap the window.location.assign() // Set the window.location test URL and tap the window.location.assign()
// button. // button.
GURL sample_url = web::test::HttpServer::MakeUrl(kSampleFileBasedURL); GURL sample_url = test_server_->GetURL(kSampleFileBasedURL);
SetWindowLocationUrl(sample_url); SetWindowLocationUrl(sample_url);
ASSERT_TRUE(ExecuteBlockAndWaitForLoad(sample_url, ^{ ASSERT_TRUE(ExecuteBlockAndWaitForLoad(sample_url, ^{
ASSERT_TRUE(web::test::TapWebViewElementWithId( ASSERT_TRUE(web::test::TapWebViewElementWithId(
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/base_paths.h"
#include "base/path_service.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h" #import "base/test/ios/wait_util.h"
#import "ios/web/public/test/http_server/http_server.h" #import "ios/web/public/test/http_server/http_server.h"
...@@ -21,29 +23,51 @@ ...@@ -21,29 +23,51 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace {
// Resonse body for requests sent to web::test::HttpServer.
const char kHelloWorld[] = "Hello World";
} // namespave
using web::test::HttpServer;
// A test fixture for verifying the behavior of web::test::HttpServer. // A test fixture for verifying the behavior of web::test::HttpServer.
typedef web::WebIntTest HttpServerTest; class HttpServerTest : public web::WebIntTest {
protected:
void SetUp() override {
web::WebIntTest::SetUp();
std::unique_ptr<web::StringResponseProvider> provider(
new web::StringResponseProvider(kHelloWorld));
HttpServer& server = HttpServer::GetSharedInstance();
base::FilePath test_data_dir;
ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
server.StartOrDie(test_data_dir.Append("."));
server.AddResponseProvider(std::move(provider));
}
~HttpServerTest() override {
HttpServer& server = HttpServer::GetSharedInstance();
if (server.IsRunning()) {
server.Stop();
}
}
};
// Tests that a web::test::HttpServer can be started and can send and receive // Tests that a web::test::HttpServer can be started and can send and receive
// requests and response from |TestResponseProvider|. // requests and response from |TestResponseProvider|.
TEST_F(HttpServerTest, StartAndInterfaceWithResponseProvider) { TEST_F(HttpServerTest, StartAndInterfaceWithResponseProvider) {
const std::string kHelloWorld = "Hello World";
std::unique_ptr<web::StringResponseProvider> provider(
new web::StringResponseProvider(kHelloWorld));
web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance();
ASSERT_TRUE(server.IsRunning());
server.AddResponseProvider(std::move(provider));
__block NSString* page_result; __block NSString* page_result;
id completion_handler = id completion_handler =
^(NSData* data, NSURLResponse* response, NSError* error) { ^(NSData* data, NSURLResponse* response, NSError* error) {
page_result = page_result =
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}; };
NSURL* url = net::NSURLWithGURL(server.MakeUrl("http://whatever")); GURL url = HttpServer::GetSharedInstance().MakeUrl("http://whatever");
NSURLSessionDataTask* data_task = NSURLSessionDataTask* data_task =
[[NSURLSession sharedSession] dataTaskWithURL:url [[NSURLSession sharedSession] dataTaskWithURL:net::NSURLWithGURL(url)
completionHandler:completion_handler]; completionHandler:completion_handler];
[data_task resume]; [data_task resume];
base::test::ios::WaitUntilCondition(^bool() { base::test::ios::WaitUntilCondition(^bool() {
......
...@@ -19,7 +19,7 @@ class GURL; ...@@ -19,7 +19,7 @@ class GURL;
namespace web { namespace web {
// A test fixture for integration tests that need to bring up the HttpServer. // A test fixture for integration tests that need a WebState which loads pages.
class WebIntTest : public WebTest { class WebIntTest : public WebTest {
protected: protected:
WebIntTest(); WebIntTest();
......
...@@ -4,14 +4,11 @@ ...@@ -4,14 +4,11 @@
#import "ios/web/test/web_int_test.h" #import "ios/web/test/web_int_test.h"
#include "base/base_paths.h"
#import "base/ios/block_types.h" #import "base/ios/block_types.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/path_service.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#import "base/test/ios/wait_util.h" #import "base/test/ios/wait_util.h"
#import "ios/web/common/web_view_creation_util.h" #import "ios/web/common/web_view_creation_util.h"
#import "ios/web/public/test/http_server/http_server.h"
#import "ios/web/public/test/js_test_util.h" #import "ios/web/public/test/js_test_util.h"
#include "ios/web/public/web_state_observer.h" #include "ios/web/public/web_state_observer.h"
...@@ -59,14 +56,6 @@ WebIntTest::~WebIntTest() {} ...@@ -59,14 +56,6 @@ WebIntTest::~WebIntTest() {}
void WebIntTest::SetUp() { void WebIntTest::SetUp() {
WebTest::SetUp(); WebTest::SetUp();
// Start the http server.
web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance();
ASSERT_FALSE(server.IsRunning());
base::FilePath test_data_dir;
ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
server.StartOrDie(test_data_dir.Append("."));
// Remove any previously existing WKWebView data. // Remove any previously existing WKWebView data.
RemoveWKWebViewCreatedData([WKWebsiteDataStore defaultDataStore], RemoveWKWebViewCreatedData([WKWebsiteDataStore defaultDataStore],
[WKWebsiteDataStore allWebsiteDataTypes]); [WKWebsiteDataStore allWebsiteDataTypes]);
...@@ -87,10 +76,6 @@ void WebIntTest::TearDown() { ...@@ -87,10 +76,6 @@ void WebIntTest::TearDown() {
RemoveWKWebViewCreatedData([WKWebsiteDataStore defaultDataStore], RemoveWKWebViewCreatedData([WKWebsiteDataStore defaultDataStore],
[WKWebsiteDataStore allWebsiteDataTypes]); [WKWebsiteDataStore allWebsiteDataTypes]);
web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance();
server.Stop();
EXPECT_FALSE(server.IsRunning());
WebTest::TearDown(); WebTest::TearDown();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment