From b07283fe544aafcb1d11feaaafeb2166bf15c87f Mon Sep 17 00:00:00 2001 From: shreyasv Date: Mon, 27 Jul 2015 17:48:24 -0700 Subject: [PATCH] Moving a few web unittests to use WebTest test fixture A lot of the boilerplate in these unittests is already present in this test fixture. Also added a method to return the current test WebClient BUG=480507 Review URL: https://codereview.chromium.org/1252923006 Cr-Commit-Position: refs/heads/master@{#340618} --- ios/web/test/web_test.h | 3 ++ ios/web/web_state/js/page_script_util_unittest.mm | 22 +++++--------- .../crw_wk_simple_web_view_controller_unittest.mm | 19 +++++------- .../ui/crw_wk_web_view_crash_detector_unittest.mm | 18 ++---------- ios/web/web_state/ui/web_view_js_utils_unittest.mm | 21 ++----------- .../web_view_internal_creation_util_unittest.mm | 34 ++++++++++------------ 6 files changed, 39 insertions(+), 78 deletions(-) diff --git a/ios/web/test/web_test.h b/ios/web/test/web_test.h index 2d10828ad0cc..13ae8efefc95 100644 --- a/ios/web/test/web_test.h +++ b/ios/web/test/web_test.h @@ -42,6 +42,9 @@ class WebTest : public PlatformTest { void SetUp() override; void TearDown() override; + // Returns the WebClient that is used for testing. + TestWebClient* GetWebClient() { return &client_; } + // Returns the BrowserState that is used for testing. BrowserState* GetBrowserState() { return &browser_state_; } diff --git a/ios/web/web_state/js/page_script_util_unittest.mm b/ios/web/web_state/js/page_script_util_unittest.mm index 54b8f5045469..719de80a8078 100644 --- a/ios/web/web_state/js/page_script_util_unittest.mm +++ b/ios/web/web_state/js/page_script_util_unittest.mm @@ -15,22 +15,14 @@ #import "ios/web/public/test/test_web_client.h" #include "ios/web/public/test/web_test_util.h" #import "ios/web/public/web_view_creation_util.h" +#include "ios/web/test/web_test.h" #include "testing/gtest_mac.h" -#include "testing/platform_test.h" namespace web { namespace { -class PageScriptUtilTest : public PlatformTest { - protected: - void SetUp() override { SetWebClient(&web_client_); } - - void TearDown() override { SetWebClient(nullptr); } - - // WebClient and BrowserState required for web view creation. - TestWebClient web_client_; - TestBrowserState browser_state_; -}; +// A test fixture for testing the page_script_util methods. +typedef WebTest PageScriptUtilTest; // Tests that UIWebView early page script is a valid script that injects global // __gCrWeb object. @@ -46,14 +38,14 @@ TEST_F(PageScriptUtilTest, UIWebViewEarlyPageScript) { TEST_F(PageScriptUtilTest, WKWebViewEarlyPageScript) { CR_TEST_REQUIRES_WK_WEB_VIEW(); base::scoped_nsobject web_view( - CreateWKWebView(CGRectZero, &browser_state_)); + CreateWKWebView(CGRectZero, GetBrowserState())); EvaluateJavaScript(web_view, GetEarlyPageScript(WK_WEB_VIEW_TYPE)); EXPECT_NSEQ(@"object", EvaluateJavaScript(web_view, @"typeof __gCrWeb")); } // Tests that embedder's UIWebView script is included into early script. TEST_F(PageScriptUtilTest, UIEmbedderScript) { - web_client_.SetEarlyPageScript(@"__gCrEmbedder = {};", UI_WEB_VIEW_TYPE); + GetWebClient()->SetEarlyPageScript(@"__gCrEmbedder = {};", UI_WEB_VIEW_TYPE); base::scoped_nsobject web_view([[UIWebView alloc] init]); EvaluateJavaScriptAsString(web_view, GetEarlyPageScript(UI_WEB_VIEW_TYPE)); EXPECT_NSEQ(@"object", @@ -63,9 +55,9 @@ TEST_F(PageScriptUtilTest, UIEmbedderScript) { // Tests that embedder's WKWebView script is included into early script. TEST_F(PageScriptUtilTest, WKEmbedderScript) { CR_TEST_REQUIRES_WK_WEB_VIEW(); - web_client_.SetEarlyPageScript(@"__gCrEmbedder = {};", WK_WEB_VIEW_TYPE); + GetWebClient()->SetEarlyPageScript(@"__gCrEmbedder = {};", WK_WEB_VIEW_TYPE); base::scoped_nsobject web_view( - CreateWKWebView(CGRectZero, &browser_state_)); + CreateWKWebView(CGRectZero, GetBrowserState())); EvaluateJavaScript(web_view, GetEarlyPageScript(WK_WEB_VIEW_TYPE)); EXPECT_NSEQ(@"object", EvaluateJavaScript(web_view, @"typeof __gCrEmbedder")); } diff --git a/ios/web/web_state/ui/crw_wk_simple_web_view_controller_unittest.mm b/ios/web/web_state/ui/crw_wk_simple_web_view_controller_unittest.mm index 4db1bd1d28cd..72cb95fd071e 100644 --- a/ios/web/web_state/ui/crw_wk_simple_web_view_controller_unittest.mm +++ b/ios/web/web_state/ui/crw_wk_simple_web_view_controller_unittest.mm @@ -13,19 +13,20 @@ #import "ios/web/public/test/test_web_client.h" #include "ios/web/public/test/web_test_util.h" #import "ios/web/public/web_view_creation_util.h" +#include "ios/web/test/web_test.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest_mac.h" -#include "testing/platform_test.h" #import "third_party/ocmock/OCMock/OCMock.h" #include "third_party/ocmock/gtest_support.h" namespace { -class CRWWKSimpleWebViewControllerTest : public PlatformTest { +// A test fixture for testing CRWWKSimpleWebViewController. +class CRWWKSimpleWebViewControllerTest : public web::WebTest { protected: void SetUp() override { CR_TEST_REQUIRES_WK_WEB_VIEW(); - web::SetWebClient(&web_client_); + web::WebTest::SetUp(); mock_web_view_.reset( [[OCMockObject niceMockForClass:[WKWebView class]] retain]); web_view_controller_.reset([[CRWWKSimpleWebViewController alloc] @@ -37,7 +38,7 @@ class CRWWKSimpleWebViewControllerTest : public PlatformTest { void TearDown() override { CR_TEST_REQUIRES_WK_WEB_VIEW(); - web::SetWebClient(&web_client_); + web::WebTest::TearDown(); } // Tests that |shouldStartLoadWithRequest:| decision by the delegate is @@ -70,12 +71,6 @@ class CRWWKSimpleWebViewControllerTest : public PlatformTest { base::scoped_nsobject mock_web_view_; base::scoped_nsobject mock_delegate_; base::scoped_nsobject web_view_controller_; - // BrowserState, required for WKWebView creation. - web::TestBrowserState browser_state_; - - private: - // WebClient for testing. - web::TestWebClient web_client_; }; // Tests to make sure a CRWWKSimpleWebViewController correctly sets the backing @@ -90,7 +85,7 @@ TEST_F(CRWWKSimpleWebViewControllerTest, View) { TEST_F(CRWWKSimpleWebViewControllerTest, Delegate) { CR_TEST_REQUIRES_WK_WEB_VIEW(); base::scoped_nsobject web_view( - web::CreateWKWebView(CGRectZero, &browser_state_)); + web::CreateWKWebView(CGRectZero, GetBrowserState())); ASSERT_TRUE(web_view); web_view_controller_.reset([[CRWWKSimpleWebViewController alloc] initWithWKWebView:web_view]); @@ -187,7 +182,7 @@ TEST_F(CRWWKSimpleWebViewControllerTest, ShouldStartLoadRespectedNo) { TEST_F(CRWWKSimpleWebViewControllerTest, TitleMayHaveChanged) { CR_TEST_REQUIRES_WK_WEB_VIEW(); base::scoped_nsobject web_view( - web::CreateWKWebView(CGRectZero, &browser_state_)); + web::CreateWKWebView(CGRectZero, GetBrowserState())); base::scoped_nsobject mock_web_view( [[OCMockObject partialMockForObject:web_view] retain]); web_view_controller_.reset( diff --git a/ios/web/web_state/ui/crw_wk_web_view_crash_detector_unittest.mm b/ios/web/web_state/ui/crw_wk_web_view_crash_detector_unittest.mm index 6722598f33df..47d8626da81a 100644 --- a/ios/web/web_state/ui/crw_wk_web_view_crash_detector_unittest.mm +++ b/ios/web/web_state/ui/crw_wk_web_view_crash_detector_unittest.mm @@ -7,25 +7,13 @@ #include "base/mac/scoped_nsobject.h" #import "ios/web/public/test/test_web_client.h" #include "ios/web/public/test/web_test_util.h" +#include "ios/web/test/web_test.h" #import "ios/web/test/wk_web_view_crash_utils.h" -#include "testing/platform_test.h" namespace { -class CRWWKWebViewCrashDetectorTest : public PlatformTest { - public: - void SetUp() override { - PlatformTest::SetUp(); - web::SetWebClient(&web_client_); - } - void TearDown() override { - web::SetWebClient(nullptr); - PlatformTest::TearDown(); - } - private: - // WebClient for testing. - web::TestWebClient web_client_; -}; +// A test fixture for testing CRWWKWebViewCrashDetector. +typedef web::WebTest CRWWKWebViewCrashDetectorTest; // Tests that crash is reported for WKWebView if // WKErrorWebContentProcessTerminated happend during JavaScript evaluation. diff --git a/ios/web/web_state/ui/web_view_js_utils_unittest.mm b/ios/web/web_state/ui/web_view_js_utils_unittest.mm index 3db44942ada3..b0f699e61621 100644 --- a/ios/web/web_state/ui/web_view_js_utils_unittest.mm +++ b/ios/web/web_state/ui/web_view_js_utils_unittest.mm @@ -12,6 +12,7 @@ #include "ios/web/public/test/web_test_util.h" #import "ios/web/public/web_view_creation_util.h" #import "ios/web/web_state/web_view_internal_creation_util.h" +#import "ios/web/test/web_test.h" #include "testing/gtest_mac.h" #include "testing/platform_test.h" @@ -34,23 +35,7 @@ NSString* EvaluateJavaScript(WebView web_view, NSString* js) { } // Base test fixture for web::EvaluateJavaScript testing. -class WebViewJSUtilsTest : public PlatformTest { - protected: - void SetUp() override { - PlatformTest::SetUp(); - web::SetWebClient(&test_web_client_); - } - void TearDown() override { - web::SetWebClient(nullptr); - PlatformTest::TearDown(); - } - // BrowserState required for web view creation. - web::TestBrowserState browser_state_; - - private: - // Required by web::CreateWebView/web::CreateWKWebView functions. - web::TestWebClient test_web_client_; -}; +typedef web::WebTest WebViewJSUtilsTest; // Test fixture for web::EvaluateJavaScript(UIWebView*..) testing. class UIWebViewJSUtilsTest : public WebViewJSUtilsTest { @@ -70,7 +55,7 @@ class WKWebViewJSUtilsTest : public WebViewJSUtilsTest { // SetUp crashes on iOS 7. CR_TEST_REQUIRES_WK_WEB_VIEW(); WebViewJSUtilsTest::SetUp(); - web_view_.reset(web::CreateWKWebView(CGRectZero, &browser_state_)); + web_view_.reset(web::CreateWKWebView(CGRectZero, GetBrowserState())); } // WKWebView created for testing. base::scoped_nsobject web_view_; diff --git a/ios/web/web_state/web_view_internal_creation_util_unittest.mm b/ios/web/web_state/web_view_internal_creation_util_unittest.mm index caa1acb84d8e..4b48c1daf2cf 100644 --- a/ios/web/web_state/web_view_internal_creation_util_unittest.mm +++ b/ios/web/web_state/web_view_internal_creation_util_unittest.mm @@ -19,9 +19,9 @@ #import "ios/web/web_state/ui/crw_simple_web_view_controller.h" #import "ios/web/web_state/ui/crw_static_file_web_view.h" #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" +#include "ios/web/test/web_test.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest_mac.h" -#include "testing/platform_test.h" @interface CRWStaticFileWebView (Testing) + (BOOL)isStaticFileUserAgent:(NSString*)userAgent; @@ -47,23 +47,24 @@ class CreationUtilsWebClient : public TestWebClient { MOCK_CONST_METHOD1(PostWebViewCreation, void(UIWebView* web_view)); }; -class WebViewCreationUtilsTest : public PlatformTest { +class WebViewCreationUtilsTest : public WebTest { public: - WebViewCreationUtilsTest() : ui_thread_(WebThread::UI, &message_loop_) {} + WebViewCreationUtilsTest() {} protected: void SetUp() override { - PlatformTest::SetUp(); + WebTest::SetUp(); logJavaScriptPref_ = [[NSUserDefaults standardUserDefaults] boolForKey:@"LogJavascript"]; + original_web_client_ = GetWebClient(); SetWebClient(&creation_utils_web_client_); creation_utils_web_client_.SetUserAgent("TestUA", false); } void TearDown() override { - SetWebClient(nullptr); + SetWebClient(original_web_client_); [[NSUserDefaults standardUserDefaults] setBool:logJavaScriptPref_ forKey:@"LogJavascript"]; - PlatformTest::TearDown(); + WebTest::TearDown(); } // Sets up expectation for WebClient::PreWebViewCreation and // WebClient::PostWebViewCreation calls. Captures UIWebView passed to @@ -75,16 +76,13 @@ class WebViewCreationUtilsTest : public PlatformTest { .WillOnce(testing::SaveArg<0>(captured_web_view)); } - // BrowserState, UIThread and MessageLoop required by factory functions. - web::TestBrowserState browser_state_; - base::MessageLoop message_loop_; - web::TestWebThread ui_thread_; - private: // Original value of @"LogJavascript" pref from NSUserDefaults. BOOL logJavaScriptPref_; // WebClient that stubs PreWebViewCreation/PostWebViewCreation. CreationUtilsWebClient creation_utils_web_client_; + // The WebClient that was set before this test was run. + WebClient* original_web_client_; }; // Tests that a web view created with a certain id returns the same @@ -138,7 +136,7 @@ TEST_F(WebViewCreationUtilsTest, WKWebViewCreationWithBrowserState) { CR_TEST_REQUIRES_WK_WEB_VIEW(); base::scoped_nsobject web_view( - CreateWKWebView(kTestFrame, &browser_state_)); + CreateWKWebView(kTestFrame, GetBrowserState())); EXPECT_TRUE([web_view isKindOfClass:[WKWebView class]]); EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame])); @@ -147,7 +145,7 @@ TEST_F(WebViewCreationUtilsTest, WKWebViewCreationWithBrowserState) { // browser state's configuration. Otherwise cookie will not be immediately // shared between different web views. WKWebViewConfigurationProvider& config_provider = - WKWebViewConfigurationProvider::FromBrowserState(&browser_state_); + WKWebViewConfigurationProvider::FromBrowserState(GetBrowserState()); EXPECT_EQ(config_provider.GetWebViewConfiguration().processPool, [[web_view configuration] processPool]); } @@ -158,10 +156,10 @@ TEST_F(WebViewCreationUtilsTest, WKWebViewsShareProcessPool) { CR_TEST_REQUIRES_WK_WEB_VIEW(); base::scoped_nsobject web_view( - CreateWKWebView(kTestFrame, &browser_state_)); + CreateWKWebView(kTestFrame, GetBrowserState())); ASSERT_TRUE(web_view); base::scoped_nsobject web_view2( - CreateWKWebView(kTestFrame, &browser_state_)); + CreateWKWebView(kTestFrame, GetBrowserState())); ASSERT_TRUE(web_view2); // Make sure that web views share the same non-nil process pool. Otherwise @@ -193,10 +191,10 @@ TEST_F(WebViewCreationUtilsTest, DebugCreation) { TEST_F(WebViewCreationUtilsTest, GetActiveWKWebViewsCount) { CR_TEST_REQUIRES_WK_WEB_VIEW(); base::scoped_nsobject web_view1( - CreateWKWebView(CGRectZero, &browser_state_)); + CreateWKWebView(CGRectZero, GetBrowserState())); EXPECT_EQ(1U, GetActiveWKWebViewsCount()); base::scoped_nsobject web_view2( - CreateWKWebView(CGRectZero, &browser_state_)); + CreateWKWebView(CGRectZero, GetBrowserState())); EXPECT_EQ(2U, GetActiveWKWebViewsCount()); web_view2.reset(); EXPECT_EQ(1U, GetActiveWKWebViewsCount()); @@ -214,7 +212,7 @@ TEST_F(WebViewCreationUtilsTest, TestNewStaticFileWebViewTrue) { ExpectWebClientCalls(&captured_web_view); base::scoped_nsobject web_view( - CreateStaticFileWebView(kTestFrame, &browser_state_)); + CreateStaticFileWebView(kTestFrame, GetBrowserState())); ASSERT_TRUE([web_view isMemberOfClass:[CRWStaticFileWebView class]]); EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame])); EXPECT_NSEQ(web_view, captured_web_view); -- 2.11.4.GIT