Commit ac34346a authored by Mohamad Ahmadi's avatar Mohamad Ahmadi Committed by Commit Bot

Allows constructing web::WebTest with an instance of web::WebClient

tests that depend on ChromeWebClient (for e.g., early script injection) to
run.

web: :WebTest uses an instance of web::TestWebClient by default. This allows
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I8e36ac709f5c445392a817219cabc5c5152aa164
Reviewed-on: https://chromium-review.googlesource.com/840300Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#526542}
parent 19eee98d
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
class TestChromeBrowserState; class TestChromeBrowserState;
namespace web {
class WebClient;
} // namespace web
// Test fixture that exposes a TestChromeBrowserState to allow configuring // Test fixture that exposes a TestChromeBrowserState to allow configuring
// the BrowserState in tests. // the BrowserState in tests.
class ChromeWebTest : public web::WebTestWithWebState { class ChromeWebTest : public web::WebTestWithWebState {
...@@ -19,6 +23,7 @@ class ChromeWebTest : public web::WebTestWithWebState { ...@@ -19,6 +23,7 @@ class ChromeWebTest : public web::WebTestWithWebState {
protected: protected:
ChromeWebTest(); ChromeWebTest();
explicit ChromeWebTest(std::unique_ptr<web::WebClient> web_client);
// WebTest implementation. // WebTest implementation.
void SetUp() override; void SetUp() override;
void TearDown() override; void TearDown() override;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#include "ios/chrome/browser/passwords/ios_chrome_password_store_factory.h" #include "ios/chrome/browser/passwords/ios_chrome_password_store_factory.h"
#import "ios/chrome/browser/ui/fullscreen/legacy_fullscreen_controller.h" #import "ios/chrome/browser/ui/fullscreen/legacy_fullscreen_controller.h"
#import "ios/web/public/web_client.h"
#import "ios/web/public/web_state/web_state.h" #import "ios/web/public/web_state/web_state.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -17,10 +18,12 @@ ...@@ -17,10 +18,12 @@
ChromeWebTest::~ChromeWebTest() {} ChromeWebTest::~ChromeWebTest() {}
ChromeWebTest::ChromeWebTest() : web::WebTestWithWebState() { ChromeWebTest::ChromeWebTest(std::unique_ptr<web::WebClient> web_client)
TestChromeBrowserState::Builder browser_state_builder; : web::WebTestWithWebState(std::move(web_client)),
chrome_browser_state_ = browser_state_builder.Build(); chrome_browser_state_(TestChromeBrowserState::Builder().Build()) {}
}
ChromeWebTest::ChromeWebTest()
: chrome_browser_state_(TestChromeBrowserState::Builder().Build()) {}
void ChromeWebTest::SetUp() { void ChromeWebTest::SetUp() {
web::WebTestWithWebState::SetUp(); web::WebTestWithWebState::SetUp();
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef IOS_WEB_PUBLIC_TEST_WEB_TEST_H_ #ifndef IOS_WEB_PUBLIC_TEST_WEB_TEST_H_
#define IOS_WEB_PUBLIC_TEST_WEB_TEST_H_ #define IOS_WEB_PUBLIC_TEST_WEB_TEST_H_
#include <memory>
#include "ios/web/public/test/fakes/test_browser_state.h" #include "ios/web/public/test/fakes/test_browser_state.h"
#include "ios/web/public/test/scoped_testing_web_client.h" #include "ios/web/public/test/scoped_testing_web_client.h"
#include "ios/web/public/test/test_web_thread_bundle.h" #include "ios/web/public/test/test_web_thread_bundle.h"
...@@ -13,7 +15,7 @@ ...@@ -13,7 +15,7 @@
namespace web { namespace web {
class BrowserState; class BrowserState;
class TestWebClient; class WebClient;
class WebTestRenderProcessCrashObserver; class WebTestRenderProcessCrashObserver;
// A test fixture for web tests that need a minimum environment set up that // A test fixture for web tests that need a minimum environment set up that
...@@ -21,10 +23,11 @@ class WebTestRenderProcessCrashObserver; ...@@ -21,10 +23,11 @@ class WebTestRenderProcessCrashObserver;
class WebTest : public PlatformTest { class WebTest : public PlatformTest {
protected: protected:
WebTest(); WebTest();
explicit WebTest(std::unique_ptr<web::WebClient> web_client);
~WebTest() override; ~WebTest() override;
// Returns the WebClient that is used for testing. // Returns the WebClient that is used for testing.
TestWebClient* GetWebClient(); virtual web::WebClient* GetWebClient();
// Returns the BrowserState that is used for testing. // Returns the BrowserState that is used for testing.
virtual BrowserState* GetBrowserState(); virtual BrowserState* GetBrowserState();
......
...@@ -24,14 +24,16 @@ class WebTestRenderProcessCrashObserver : public GlobalWebStateObserver { ...@@ -24,14 +24,16 @@ class WebTestRenderProcessCrashObserver : public GlobalWebStateObserver {
} }
}; };
WebTest::WebTest() WebTest::WebTest() : WebTest(base::WrapUnique(new TestWebClient)) {}
: web_client_(base::WrapUnique(new TestWebClient)),
WebTest::WebTest(std::unique_ptr<web::WebClient> web_client)
: web_client_(std::move(web_client)),
crash_observer_(base::MakeUnique<WebTestRenderProcessCrashObserver>()) {} crash_observer_(base::MakeUnique<WebTestRenderProcessCrashObserver>()) {}
WebTest::~WebTest() {} WebTest::~WebTest() {}
TestWebClient* WebTest::GetWebClient() { web::WebClient* WebTest::GetWebClient() {
return static_cast<TestWebClient*>(web_client_.Get()); return web_client_.Get();
} }
BrowserState* WebTest::GetBrowserState() { BrowserState* WebTest::GetBrowserState() {
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_ #ifndef IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_
#define IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_ #define IOS_WEB_PUBLIC_TEST_WEB_TEST_WITH_WEB_STATE_H_
#include <memory>
#import "base/ios/block_types.h" #import "base/ios/block_types.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "ios/web/public/test/web_test.h" #include "ios/web/public/test/web_test.h"
...@@ -13,6 +15,7 @@ ...@@ -13,6 +15,7 @@
namespace web { namespace web {
class WebClient;
class WebState; class WebState;
// Base test fixture that provides WebState for testing. // Base test fixture that provides WebState for testing.
...@@ -20,6 +23,7 @@ class WebTestWithWebState : public WebTest, ...@@ -20,6 +23,7 @@ class WebTestWithWebState : public WebTest,
public base::MessageLoop::TaskObserver { public base::MessageLoop::TaskObserver {
protected: protected:
WebTestWithWebState(); WebTestWithWebState();
explicit WebTestWithWebState(std::unique_ptr<web::WebClient> web_client);
~WebTestWithWebState() override; ~WebTestWithWebState() override;
// WebTest overrides. // WebTest overrides.
......
...@@ -36,6 +36,10 @@ namespace web { ...@@ -36,6 +36,10 @@ namespace web {
WebTestWithWebState::WebTestWithWebState() {} WebTestWithWebState::WebTestWithWebState() {}
WebTestWithWebState::WebTestWithWebState(
std::unique_ptr<web::WebClient> web_client)
: WebTest(std::move(web_client)) {}
WebTestWithWebState::~WebTestWithWebState() {} WebTestWithWebState::~WebTestWithWebState() {}
void WebTestWithWebState::SetUp() { void WebTestWithWebState::SetUp() {
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef IOS_WEB_TEST_WEB_TEST_WITH_WEB_CONTROLLER_H_ #ifndef IOS_WEB_TEST_WEB_TEST_WITH_WEB_CONTROLLER_H_
#define IOS_WEB_TEST_WEB_TEST_WITH_WEB_CONTROLLER_H_ #define IOS_WEB_TEST_WEB_TEST_WITH_WEB_CONTROLLER_H_
#include <memory>
#import "ios/web/public/test/web_test_with_web_state.h" #import "ios/web/public/test/web_test_with_web_state.h"
#import "ios/web/web_state/ui/crw_web_controller.h" #import "ios/web/web_state/ui/crw_web_controller.h"
...@@ -14,6 +16,7 @@ namespace web { ...@@ -14,6 +16,7 @@ namespace web {
class WebTestWithWebController : public WebTestWithWebState { class WebTestWithWebController : public WebTestWithWebState {
protected: protected:
WebTestWithWebController(); WebTestWithWebController();
explicit WebTestWithWebController(std::unique_ptr<web::WebClient> web_client);
~WebTestWithWebController() override; ~WebTestWithWebController() override;
// Returns web controller for testing. // Returns web controller for testing.
CRWWebController* web_controller(); CRWWebController* web_controller();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#import "ios/web/test/web_test_with_web_controller.h" #import "ios/web/test/web_test_with_web_controller.h"
#import "ios/web/public/web_client.h"
#import "ios/web/web_state/web_state_impl.h" #import "ios/web/web_state/web_state_impl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -14,6 +15,10 @@ namespace web { ...@@ -14,6 +15,10 @@ namespace web {
WebTestWithWebController::WebTestWithWebController() {} WebTestWithWebController::WebTestWithWebController() {}
WebTestWithWebController::WebTestWithWebController(
std::unique_ptr<web::WebClient> web_client)
: WebTestWithWebState(std::move(web_client)) {}
WebTestWithWebController::~WebTestWithWebController() {} WebTestWithWebController::~WebTestWithWebController() {}
CRWWebController* WebTestWithWebController::web_controller() { CRWWebController* WebTestWithWebController::web_controller() {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <WebKit/WebKit.h> #import <WebKit/WebKit.h>
#include <memory>
#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"
...@@ -24,7 +25,14 @@ namespace web { ...@@ -24,7 +25,14 @@ namespace web {
namespace { namespace {
// A test fixture for testing the page_script_util methods. // A test fixture for testing the page_script_util methods.
typedef WebTest PageScriptUtilTest; class PageScriptUtilTest : public WebTest {
protected:
PageScriptUtilTest() : WebTest(std::make_unique<TestWebClient>()) {}
TestWebClient* GetWebClient() override {
return static_cast<TestWebClient*>(WebTest::GetWebClient());
}
};
// Tests that WKWebView early page script is a valid script that injects global // Tests that WKWebView early page script is a valid script that injects global
// __gCrWeb object. // __gCrWeb object.
......
...@@ -118,6 +118,9 @@ void WaitForZoomRendering(CRWWebController* webController, ...@@ -118,6 +118,9 @@ void WaitForZoomRendering(CRWWebController* webController,
// Test fixture for testing CRWWebController. Stubs out web view. // Test fixture for testing CRWWebController. Stubs out web view.
class CRWWebControllerTest : public WebTestWithWebController { class CRWWebControllerTest : public WebTestWithWebController {
protected: protected:
CRWWebControllerTest()
: WebTestWithWebController(std::make_unique<TestWebClient>()) {}
void SetUp() override { void SetUp() override {
WebTestWithWebController::SetUp(); WebTestWithWebController::SetUp();
mock_web_view_ = CreateMockWebView(); mock_web_view_ = CreateMockWebView();
...@@ -137,6 +140,11 @@ class CRWWebControllerTest : public WebTestWithWebController { ...@@ -137,6 +140,11 @@ class CRWWebControllerTest : public WebTestWithWebController {
WebTestWithWebController::TearDown(); WebTestWithWebController::TearDown();
} }
TestWebClient* GetWebClient() override {
return static_cast<TestWebClient*>(
WebTestWithWebController::GetWebClient());
}
// The value for web view OCMock objects to expect for |-setFrame:|. // The value for web view OCMock objects to expect for |-setFrame:|.
CGRect GetExpectedWebViewFrame() const { CGRect GetExpectedWebViewFrame() const {
CGSize container_view_size = [UIScreen mainScreen].bounds.size; CGSize container_view_size = [UIScreen mainScreen].bounds.size;
......
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