web_view_internal_creation_util_unittest.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2014 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. #import "ios/web/web_state/web_view_internal_creation_util.h"
  5. #import <CoreGraphics/CoreGraphics.h>
  6. #import <WebKit/WebKit.h>
  7. #include "base/memory/ptr_util.h"
  8. #import "ios/web/common/web_view_creation_util.h"
  9. #import "ios/web/public/test/fakes/fake_web_client.h"
  10. #include "ios/web/public/test/scoped_testing_web_client.h"
  11. #include "ios/web/public/test/web_test.h"
  12. #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #import "testing/gtest_mac.h"
  15. #if !defined(__has_feature) || !__has_feature(objc_arc)
  16. #error "This file requires ARC support."
  17. #endif
  18. namespace {
  19. // An arbitrary sized frame for testing web view creation.
  20. const CGRect kTestFrame = CGRectMake(5.0f, 10.0f, 15.0f, 20.0f);
  21. // A WebClient that stubs PreWebViewCreation call for testing purposes.
  22. class CreationUtilsWebClient : public web::FakeWebClient {
  23. public:
  24. MOCK_CONST_METHOD0(PreWebViewCreation, void());
  25. };
  26. } // namespace
  27. namespace web {
  28. // Test fixture for testing web view creation.
  29. class WebViewCreationUtilsTest : public WebTest {
  30. public:
  31. WebViewCreationUtilsTest()
  32. : web_client_(base::WrapUnique(new CreationUtilsWebClient)) {}
  33. protected:
  34. CreationUtilsWebClient* creation_utils_web_client() {
  35. return static_cast<CreationUtilsWebClient*>(web_client_.Get());
  36. }
  37. private:
  38. // WebClient that stubs PreWebViewCreation.
  39. web::ScopedTestingWebClient web_client_;
  40. };
  41. // Tests web::BuildWKWebView function that it correctly returns a WKWebView
  42. // with the correct frame, WKProcessPool and calls WebClient::PreWebViewCreation
  43. // method.
  44. TEST_F(WebViewCreationUtilsTest, WKWebViewCreationWithBrowserState) {
  45. EXPECT_CALL(*creation_utils_web_client(), PreWebViewCreation()).Times(1);
  46. WKWebView* web_view = BuildWKWebView(kTestFrame, GetBrowserState());
  47. EXPECT_TRUE([web_view isKindOfClass:[WKWebView class]]);
  48. EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
  49. // Make sure that web view's configuration shares the same process pool with
  50. // browser state's configuration. Otherwise cookie will not be immediately
  51. // shared between different web views.
  52. WKWebViewConfigurationProvider& config_provider =
  53. WKWebViewConfigurationProvider::FromBrowserState(GetBrowserState());
  54. EXPECT_EQ(config_provider.GetWebViewConfiguration().processPool,
  55. [[web_view configuration] processPool]);
  56. }
  57. // Tests web::BuildWKWebView function that it correctly returns a WKWebView
  58. // with the correct frame, WKProcessPool and calls WebClient::PreWebViewCreation
  59. // method.
  60. TEST_F(WebViewCreationUtilsTest, BuildWKWebViewForQueries) {
  61. EXPECT_CALL(*creation_utils_web_client(), PreWebViewCreation()).Times(0);
  62. WKWebViewConfigurationProvider& config_provider =
  63. WKWebViewConfigurationProvider::FromBrowserState(GetBrowserState());
  64. WKWebView* web_view = BuildWKWebViewForQueries(
  65. config_provider.GetWebViewConfiguration(), GetBrowserState());
  66. EXPECT_TRUE([web_view isKindOfClass:[WKWebView class]]);
  67. EXPECT_TRUE(CGRectEqualToRect(CGRectZero, [web_view frame]));
  68. EXPECT_EQ(config_provider.GetWebViewConfiguration().processPool,
  69. [[web_view configuration] processPool]);
  70. }
  71. // Tests that web::BuildWKWebView always returns a web view with the same
  72. // processPool.
  73. TEST_F(WebViewCreationUtilsTest, WKWebViewsShareProcessPool) {
  74. WKWebView* web_view = BuildWKWebView(kTestFrame, GetBrowserState());
  75. ASSERT_TRUE(web_view);
  76. WKWebView* web_view2 = BuildWKWebView(kTestFrame, GetBrowserState());
  77. ASSERT_TRUE(web_view2);
  78. // Make sure that web views share the same non-nil process pool. Otherwise
  79. // cookie will not be immediately shared between different web views.
  80. EXPECT_TRUE([[web_view configuration] processPool]);
  81. EXPECT_EQ([[web_view configuration] processPool],
  82. [[web_view2 configuration] processPool]);
  83. }
  84. } // namespace web