Commit 0bd79a00 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

[iOS] Replace web::HttpServer in QRScannerViewControllerTest

Changes being made in crrev.com/c/2353092 are breaking these tests when
using web::HttpServer. So this CL converts them to the now recommended
EmbeddedTestServer.

Bug: 891834
Change-Id: Ifa5b28483d8d35038c677fca6cb82608d25cfcfc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2362976Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799777}
parent d7905341
...@@ -65,8 +65,8 @@ source_set("eg2_tests") { ...@@ -65,8 +65,8 @@ source_set("eg2_tests") {
"//ios/chrome/test/earl_grey:eg_test_support+eg2", "//ios/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2", "//ios/testing/earl_grey:eg_test_support+eg2",
"//ios/third_party/earl_grey2:test_lib", "//ios/third_party/earl_grey2:test_lib",
"//ios/web/public/test/http_server",
"//net", "//net",
"//net:test_support",
"//third_party/ocmock", "//third_party/ocmock",
"//ui/base", "//ui/base",
] ]
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "base/ios/ios_util.h" #include "base/ios/ios_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "ios/chrome/browser/ui/qr_scanner/qr_scanner_app_interface.h" #import "ios/chrome/browser/ui/qr_scanner/qr_scanner_app_interface.h"
#include "ios/chrome/browser/ui/scanner/camera_state.h" #include "ios/chrome/browser/ui/scanner/camera_state.h"
...@@ -15,9 +16,10 @@ ...@@ -15,9 +16,10 @@
#import "ios/chrome/test/earl_grey/chrome_test_case.h" #import "ios/chrome/test/earl_grey/chrome_test_case.h"
#include "ios/chrome/test/earl_grey/earl_grey_scoped_block_swizzler.h" #include "ios/chrome/test/earl_grey/earl_grey_scoped_block_swizzler.h"
#import "ios/testing/earl_grey/earl_grey_test.h" #import "ios/testing/earl_grey/earl_grey_test.h"
#import "ios/web/public/test/http_server/http_server.h"
#include "ios/web/public/test/http_server/http_server_util.h"
#import "net/base/mac/url_conversions.h" #import "net/base/mac/url_conversions.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#import "third_party/ocmock/OCMock/OCMock.h" #import "third_party/ocmock/OCMock/OCMock.h"
#import "ui/base/l10n/l10n_util.h" #import "ui/base/l10n/l10n_util.h"
#import "ui/base/l10n/l10n_util_mac.h" #import "ui/base/l10n/l10n_util_mac.h"
...@@ -64,15 +66,15 @@ GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(QRScannerAppInterface); ...@@ -64,15 +66,15 @@ GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(QRScannerAppInterface);
namespace { namespace {
char kTestURL[] = "http://testurl"; char kTestURL[] = "/testurl";
char kTestURLResponse[] = "Test URL page"; char kTestURLResponse[] = "Test URL page";
char kTestQuery[] = "testquery"; char kTestQuery[] = "testquery";
char kTestQueryURL[] = "http://searchurl/testquery"; char kTestQueryURL[] = "/searchurl/testquery";
char kTestQueryResponse[] = "Test query page"; char kTestQueryResponse[] = "Test query page";
char kTestURLEdited[] = "http://testuredited"; char kTestURLEdited[] = "/testuredited";
char kTestURLEditedResponse[] = "Test URL edited page"; char kTestURLEditedResponse[] = "Test URL edited page";
char kTestQueryEditedURL[] = "http://searchurl/testqueredited"; char kTestQueryEditedURL[] = "/searchurl/testqueredited";
char kTestQueryEditedResponse[] = "Test query edited page"; char kTestQueryEditedResponse[] = "Test query edited page";
// The GREYCondition timeout used for calls to waitWithTimeout:pollInterval:. // The GREYCondition timeout used for calls to waitWithTimeout:pollInterval:.
...@@ -161,6 +163,38 @@ void TapKeyboardReturnKeyInOmniboxWithText(std::string text) { ...@@ -161,6 +163,38 @@ void TapKeyboardReturnKeyInOmniboxWithText(std::string text) {
performAction:grey_typeText(@"\n")]; performAction:grey_typeText(@"\n")];
} }
// Provides responses for the test page URLs.
std::unique_ptr<net::test_server::HttpResponse> StandardResponse(
const net::test_server::HttpRequest& request) {
std::unique_ptr<net::test_server::BasicHttpResponse> http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
char* body_content = nullptr;
if (base::StartsWith(request.relative_url, kTestURL,
base::CompareCase::SENSITIVE)) {
body_content = kTestURLResponse;
} else if (base::StartsWith(request.relative_url, kTestQueryURL,
base::CompareCase::SENSITIVE)) {
body_content = kTestQueryResponse;
} else if (base::StartsWith(request.relative_url, kTestURLEdited,
base::CompareCase::SENSITIVE)) {
body_content = kTestURLEditedResponse;
} else if (base::StartsWith(request.relative_url, kTestQueryEditedURL,
base::CompareCase::SENSITIVE)) {
body_content = kTestQueryEditedResponse;
} else {
return nullptr;
}
if (body_content) {
http_response->set_content(
base::StringPrintf("<html><body>%s</body></html>", body_content));
}
return std::move(http_response);
}
} // namespace } // namespace
#pragma mark - Test Case #pragma mark - Test Case
...@@ -183,37 +217,17 @@ void TapKeyboardReturnKeyInOmniboxWithText(std::string text) { ...@@ -183,37 +217,17 @@ void TapKeyboardReturnKeyInOmniboxWithText(std::string text) {
_load_GURL_from_location_bar_swizzler; _load_GURL_from_location_bar_swizzler;
} }
#if defined(CHROME_EARL_GREY_1) - (void)setUp {
+ (void)setUp {
[super setUp]; [super setUp];
[self setUpHelper];
}
#elif defined(CHROME_EARL_GREY_2)
+ (void)setUpForTestCase {
[super setUpForTestCase];
[self setUpHelper];
}
#else
#error Must define either CHROME_EARL_GREY_1 or CHROME_EARL_GREY_2.
#endif
+ (void)setUpHelper { self.testServer->RegisterRequestHandler(
std::map<GURL, std::string> responses; base::BindRepeating(&StandardResponse));
responses[web::test::HttpServer::MakeUrl(kTestURL)] = kTestURLResponse; GREYAssertTrue(self.testServer->Start(), @"Server did not start.");
responses[web::test::HttpServer::MakeUrl(kTestQueryURL)] = kTestQueryResponse;
responses[web::test::HttpServer::MakeUrl(kTestURLEdited)] =
kTestURLEditedResponse;
responses[web::test::HttpServer::MakeUrl(kTestQueryEditedURL)] =
kTestQueryEditedResponse;
web::test::SetUpSimpleHttpServer(responses);
}
- (void)setUp { _testURL = self.testServer->GetURL(kTestURL);
[super setUp]; _testURLEdited = self.testServer->GetURL(kTestURLEdited);
_testURL = web::test::HttpServer::MakeUrl(kTestURL); _testQuery = self.testServer->GetURL(kTestQueryURL);
_testURLEdited = web::test::HttpServer::MakeUrl(kTestURLEdited); _testQueryEdited = self.testServer->GetURL(kTestQueryEditedURL);
_testQuery = web::test::HttpServer::MakeUrl(kTestQueryURL);
_testQueryEdited = web::test::HttpServer::MakeUrl(kTestQueryEditedURL);
} }
- (void)tearDown { - (void)tearDown {
...@@ -775,7 +789,7 @@ void TapKeyboardReturnKeyInOmniboxWithText(std::string text) { ...@@ -775,7 +789,7 @@ void TapKeyboardReturnKeyInOmniboxWithText(std::string text) {
[self doTestReceivingResult:_testURL.GetContent() [self doTestReceivingResult:_testURL.GetContent()
response:kTestURLEditedResponse response:kTestURLEditedResponse
edit:@"\b\bedited/"]; edit:@"\bedited/"];
} }
// Test that the correct page is loaded if the scanner result is a search query. // Test that the correct page is loaded if the scanner result is a search query.
......
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