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
3 // found in the LICENSE file.
5 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
7 #import <WebKit/WebKit.h>
9 #include "base/ios/ios_util.h"
10 #import "base/ios/weak_nsobject.h"
11 #include "ios/web/public/test/test_browser_state.h"
12 #include "ios/web/public/test/web_test_util.h"
13 #include "ios/web/public/web_client.h"
14 #import "ios/web/web_state/js/page_script_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/gtest_mac.h"
17 #include "testing/platform_test.h"
22 class WKWebViewConfigurationProviderTest : public PlatformTest {
24 void SetUp() override {
25 PlatformTest::SetUp();
26 SetWebClient(&web_client_);
28 void TearDown() override {
29 SetWebClient(nullptr);
30 PlatformTest::TearDown();
32 // Returns WKWebViewConfigurationProvider associated with |browser_state_|.
33 WKWebViewConfigurationProvider& GetProvider() {
34 return GetProvider(&browser_state_);
36 // Returns WKWebViewConfigurationProvider for given |browser_state|.
37 WKWebViewConfigurationProvider& GetProvider(
38 BrowserState* browser_state) const {
39 return WKWebViewConfigurationProvider::FromBrowserState(browser_state);
41 // BrowserState required for WKWebViewConfigurationProvider creation.
42 TestBrowserState browser_state_;
45 // WebClient required for getting early page script.
46 WebClient web_client_;
49 // Tests that each WKWebViewConfigurationProvider has own, non-nil
50 // configuration and configurations returned by the same provider will always
51 // have the same process pool.
52 TEST_F(WKWebViewConfigurationProviderTest, ConfigurationOwnerhip) {
53 CR_TEST_REQUIRES_WK_WEB_VIEW();
55 // Configuration is not nil.
56 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_);
57 ASSERT_TRUE(provider.GetWebViewConfiguration());
59 // Same non-nil WKProcessPool for the same provider.
60 ASSERT_TRUE(provider.GetWebViewConfiguration().processPool);
61 EXPECT_EQ(provider.GetWebViewConfiguration().processPool,
62 provider.GetWebViewConfiguration().processPool);
64 // Different WKProcessPools for different providers.
65 TestBrowserState other_browser_state;
66 WKWebViewConfigurationProvider& other_provider =
67 GetProvider(&other_browser_state);
68 EXPECT_NE(provider.GetWebViewConfiguration().processPool,
69 other_provider.GetWebViewConfiguration().processPool);
72 // TODO(eugenebut): Cleanup this macro, once all bots switched to iOS9 SDK
73 // (crbug.com/523365).
74 #if defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
76 // Tests Non-OffTheRecord configuration.
77 TEST_F(WKWebViewConfigurationProviderTest, NoneOffTheRecordConfiguration) {
78 CR_TEST_REQUIRES_WK_WEB_VIEW();
79 if (!base::ios::IsRunningOnIOS9OrLater())
82 browser_state_.SetOffTheRecord(false);
83 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_);
84 EXPECT_TRUE(provider.GetWebViewConfiguration().websiteDataStore.persistent);
87 // Tests OffTheRecord configuration.
88 TEST_F(WKWebViewConfigurationProviderTest, OffTheRecordConfiguration) {
89 CR_TEST_REQUIRES_WK_WEB_VIEW();
90 if (!base::ios::IsRunningOnIOS9OrLater())
93 browser_state_.SetOffTheRecord(true);
94 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_);
95 WKWebViewConfiguration* config = provider.GetWebViewConfiguration();
97 EXPECT_FALSE(config.websiteDataStore.persistent);
100 #endif // defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >=
103 // Tests that internal configuration object can not be changed by clients.
104 TEST_F(WKWebViewConfigurationProviderTest, ConfigurationProtection) {
105 CR_TEST_REQUIRES_WK_WEB_VIEW();
107 WKWebViewConfigurationProvider& provider = GetProvider(&browser_state_);
108 WKWebViewConfiguration* config = provider.GetWebViewConfiguration();
109 base::scoped_nsobject<WKProcessPool> pool([[config processPool] retain]);
110 base::scoped_nsobject<WKPreferences> prefs([[config preferences] retain]);
111 base::scoped_nsobject<WKUserContentController> userContentController(
112 [[config userContentController] retain]);
114 // Change the properties of returned configuration object.
115 TestBrowserState other_browser_state;
116 WKWebViewConfiguration* other_wk_web_view_configuration =
117 GetProvider(&other_browser_state).GetWebViewConfiguration();
118 ASSERT_TRUE(other_wk_web_view_configuration);
119 config.processPool = other_wk_web_view_configuration.processPool;
120 config.preferences = other_wk_web_view_configuration.preferences;
121 config.userContentController =
122 other_wk_web_view_configuration.userContentController;
124 // Make sure that the properties of internal configuration were not changed.
125 EXPECT_TRUE(provider.GetWebViewConfiguration().processPool);
126 EXPECT_EQ(pool.get(), provider.GetWebViewConfiguration().processPool);
127 EXPECT_TRUE(provider.GetWebViewConfiguration().preferences);
128 EXPECT_EQ(prefs.get(), provider.GetWebViewConfiguration().preferences);
129 EXPECT_TRUE(provider.GetWebViewConfiguration().userContentController);
130 EXPECT_EQ(userContentController.get(),
131 provider.GetWebViewConfiguration().userContentController);
134 // Tests that configuration is deallocated after |Purge| call.
135 TEST_F(WKWebViewConfigurationProviderTest, Purge) {
136 CR_TEST_REQUIRES_WK_WEB_VIEW();
138 base::WeakNSObject<id> config;
139 @autoreleasepool { // Make sure that resulting copy is deallocated.
140 config.reset(GetProvider().GetWebViewConfiguration());
144 // No configuration after |Purge| call.
145 GetProvider().Purge();
146 EXPECT_FALSE(config);
149 // Tests that configuration's userContentController has only one script with the
150 // same content as web::GetEarlyPageScript(WK_WEB_VIEW_TYPE) returns.
151 TEST_F(WKWebViewConfigurationProviderTest, UserScript) {
152 CR_TEST_REQUIRES_WK_WEB_VIEW();
154 WKWebViewConfiguration* config = GetProvider().GetWebViewConfiguration();
155 NSArray* scripts = config.userContentController.userScripts;
156 EXPECT_EQ(1U, scripts.count);
157 NSString* early_script = GetEarlyPageScript(WK_WEB_VIEW_TYPE);
158 // |earlyScript| is a substring of |userScripts|. The latter wraps the
159 // former with "if (!injected)" check to avoid double injections.
160 EXPECT_LT(0U, [[scripts[0] source] rangeOfString:early_script].length);