| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" | 5 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" |
| 6 | 6 |
| 7 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #import "base/ios/weak_nsobject.h" | |
| 10 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 11 #include "ios/web/public/test/fakes/test_browser_state.h" | 10 #include "ios/web/public/test/fakes/test_browser_state.h" |
| 12 #include "ios/web/public/test/scoped_testing_web_client.h" | 11 #include "ios/web/public/test/scoped_testing_web_client.h" |
| 13 #import "ios/web/public/web_client.h" | 12 #import "ios/web/public/web_client.h" |
| 14 #import "ios/web/web_state/js/page_script_util.h" | 13 #import "ios/web/web_state/js/page_script_util.h" |
| 15 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" | 14 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #import "testing/gtest_mac.h" | 16 #import "testing/gtest_mac.h" |
| 18 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
| 19 | 18 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." |
| 21 #endif |
| 22 |
| 20 namespace web { | 23 namespace web { |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 class WKWebViewConfigurationProviderTest : public PlatformTest { | 26 class WKWebViewConfigurationProviderTest : public PlatformTest { |
| 24 public: | 27 public: |
| 25 WKWebViewConfigurationProviderTest() | 28 WKWebViewConfigurationProviderTest() |
| 26 : web_client_(base::WrapUnique(new web::WebClient)) {} | 29 : web_client_(base::WrapUnique(new web::WebClient)) {} |
| 27 | 30 |
| 28 protected: | 31 protected: |
| 29 // Returns WKWebViewConfigurationProvider associated with |browser_state_|. | 32 // Returns WKWebViewConfigurationProvider associated with |browser_state_|. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_); | 77 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_); |
| 75 WKWebViewConfiguration* config = provider.GetWebViewConfiguration(); | 78 WKWebViewConfiguration* config = provider.GetWebViewConfiguration(); |
| 76 ASSERT_TRUE(config); | 79 ASSERT_TRUE(config); |
| 77 EXPECT_FALSE(config.websiteDataStore.persistent); | 80 EXPECT_FALSE(config.websiteDataStore.persistent); |
| 78 } | 81 } |
| 79 | 82 |
| 80 // Tests that internal configuration object can not be changed by clients. | 83 // Tests that internal configuration object can not be changed by clients. |
| 81 TEST_F(WKWebViewConfigurationProviderTest, ConfigurationProtection) { | 84 TEST_F(WKWebViewConfigurationProviderTest, ConfigurationProtection) { |
| 82 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_); | 85 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_); |
| 83 WKWebViewConfiguration* config = provider.GetWebViewConfiguration(); | 86 WKWebViewConfiguration* config = provider.GetWebViewConfiguration(); |
| 84 base::scoped_nsobject<WKProcessPool> pool([[config processPool] retain]); | 87 WKProcessPool* pool = [config processPool]; |
| 85 base::scoped_nsobject<WKPreferences> prefs([[config preferences] retain]); | 88 WKPreferences* prefs = [config preferences]; |
| 86 base::scoped_nsobject<WKUserContentController> userContentController( | 89 WKUserContentController* userContentController = |
| 87 [[config userContentController] retain]); | 90 [config userContentController]; |
| 88 | 91 |
| 89 // Change the properties of returned configuration object. | 92 // Change the properties of returned configuration object. |
| 90 TestBrowserState other_browser_state; | 93 TestBrowserState other_browser_state; |
| 91 WKWebViewConfiguration* other_wk_web_view_configuration = | 94 WKWebViewConfiguration* other_wk_web_view_configuration = |
| 92 GetProvider(&other_browser_state).GetWebViewConfiguration(); | 95 GetProvider(&other_browser_state).GetWebViewConfiguration(); |
| 93 ASSERT_TRUE(other_wk_web_view_configuration); | 96 ASSERT_TRUE(other_wk_web_view_configuration); |
| 94 config.processPool = other_wk_web_view_configuration.processPool; | 97 config.processPool = other_wk_web_view_configuration.processPool; |
| 95 config.preferences = other_wk_web_view_configuration.preferences; | 98 config.preferences = other_wk_web_view_configuration.preferences; |
| 96 config.userContentController = | 99 config.userContentController = |
| 97 other_wk_web_view_configuration.userContentController; | 100 other_wk_web_view_configuration.userContentController; |
| 98 | 101 |
| 99 // Make sure that the properties of internal configuration were not changed. | 102 // Make sure that the properties of internal configuration were not changed. |
| 100 EXPECT_TRUE(provider.GetWebViewConfiguration().processPool); | 103 EXPECT_TRUE(provider.GetWebViewConfiguration().processPool); |
| 101 EXPECT_EQ(pool.get(), provider.GetWebViewConfiguration().processPool); | 104 EXPECT_EQ(pool, provider.GetWebViewConfiguration().processPool); |
| 102 EXPECT_TRUE(provider.GetWebViewConfiguration().preferences); | 105 EXPECT_TRUE(provider.GetWebViewConfiguration().preferences); |
| 103 EXPECT_EQ(prefs.get(), provider.GetWebViewConfiguration().preferences); | 106 EXPECT_EQ(prefs, provider.GetWebViewConfiguration().preferences); |
| 104 EXPECT_TRUE(provider.GetWebViewConfiguration().userContentController); | 107 EXPECT_TRUE(provider.GetWebViewConfiguration().userContentController); |
| 105 EXPECT_EQ(userContentController.get(), | 108 EXPECT_EQ(userContentController, |
| 106 provider.GetWebViewConfiguration().userContentController); | 109 provider.GetWebViewConfiguration().userContentController); |
| 107 } | 110 } |
| 108 | 111 |
| 109 // Tests that script message router is bound to correct user content controller. | 112 // Tests that script message router is bound to correct user content controller. |
| 110 TEST_F(WKWebViewConfigurationProviderTest, ScriptMessageRouter) { | 113 TEST_F(WKWebViewConfigurationProviderTest, ScriptMessageRouter) { |
| 111 ASSERT_TRUE(GetProvider().GetWebViewConfiguration().userContentController); | 114 ASSERT_TRUE(GetProvider().GetWebViewConfiguration().userContentController); |
| 112 EXPECT_EQ(GetProvider().GetWebViewConfiguration().userContentController, | 115 EXPECT_EQ(GetProvider().GetWebViewConfiguration().userContentController, |
| 113 GetProvider().GetScriptMessageRouter().userContentController); | 116 GetProvider().GetScriptMessageRouter().userContentController); |
| 114 } | 117 } |
| 115 | 118 |
| 116 // Tests that both configuration and script message router are deallocated after | 119 // Tests that both configuration and script message router are deallocated after |
| 117 // |Purge| call. | 120 // |Purge| call. |
| 118 TEST_F(WKWebViewConfigurationProviderTest, Purge) { | 121 TEST_F(WKWebViewConfigurationProviderTest, Purge) { |
| 119 base::WeakNSObject<id> config; | 122 __weak id config; |
| 120 base::WeakNSObject<id> router; | 123 __weak id router; |
| 121 @autoreleasepool { // Make sure that resulting copy is deallocated. | 124 @autoreleasepool { // Make sure that resulting copy is deallocated. |
| 122 config.reset(GetProvider().GetWebViewConfiguration()); | 125 config = GetProvider().GetWebViewConfiguration(); |
| 123 router.reset(GetProvider().GetScriptMessageRouter()); | 126 router = GetProvider().GetScriptMessageRouter(); |
| 124 ASSERT_TRUE(config); | 127 ASSERT_TRUE(config); |
| 125 ASSERT_TRUE(router); | 128 ASSERT_TRUE(router); |
| 126 } | 129 } |
| 127 | 130 |
| 128 // No configuration and router after |Purge| call. | 131 // No configuration and router after |Purge| call. |
| 129 GetProvider().Purge(); | 132 GetProvider().Purge(); |
| 130 EXPECT_FALSE(config); | 133 EXPECT_FALSE(config); |
| 131 EXPECT_FALSE(router); | 134 EXPECT_FALSE(router); |
| 132 } | 135 } |
| 133 | 136 |
| 134 // Tests that configuration's userContentController has only one script with the | 137 // Tests that configuration's userContentController has only one script with the |
| 135 // same content as web::GetEarlyPageScript() returns. | 138 // same content as web::GetEarlyPageScript() returns. |
| 136 TEST_F(WKWebViewConfigurationProviderTest, UserScript) { | 139 TEST_F(WKWebViewConfigurationProviderTest, UserScript) { |
| 137 WKWebViewConfiguration* config = GetProvider().GetWebViewConfiguration(); | 140 WKWebViewConfiguration* config = GetProvider().GetWebViewConfiguration(); |
| 138 NSArray* scripts = config.userContentController.userScripts; | 141 NSArray* scripts = config.userContentController.userScripts; |
| 139 EXPECT_EQ(1U, scripts.count); | 142 EXPECT_EQ(1U, scripts.count); |
| 140 NSString* early_script = GetEarlyPageScript(&browser_state_); | 143 NSString* early_script = GetEarlyPageScript(&browser_state_); |
| 141 // |earlyScript| is a substring of |userScripts|. The latter wraps the | 144 // |earlyScript| is a substring of |userScripts|. The latter wraps the |
| 142 // former with "if (!injected)" check to avoid double injections. | 145 // former with "if (!injected)" check to avoid double injections. |
| 143 EXPECT_LT(0U, [[scripts[0] source] rangeOfString:early_script].length); | 146 EXPECT_LT(0U, [[scripts[0] source] rangeOfString:early_script].length); |
| 144 } | 147 } |
| 145 | 148 |
| 146 } // namespace | 149 } // namespace |
| 147 } // namespace web | 150 } // namespace web |
| OLD | NEW |