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.
5 #import "ios/web/web_state/web_view_internal_creation_util.h"
7 #import <CoreGraphics/CoreGraphics.h>
8 #import <WebKit/WebKit.h>
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/message_loop/message_loop.h"
12 #include "ios/web/net/request_group_util.h"
13 #include "ios/web/public/test/test_browser_state.h"
14 #import "ios/web/public/test/test_web_client.h"
15 #include "ios/web/public/test/test_web_thread.h"
16 #include "ios/web/public/test/web_test_util.h"
17 #import "ios/web/public/web_view_creation_util.h"
18 #import "ios/web/web_state/ui/crw_debug_web_view.h"
19 #import "ios/web/web_state/ui/crw_simple_web_view_controller.h"
20 #import "ios/web/web_state/ui/crw_static_file_web_view.h"
21 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest_mac.h"
24 #include "testing/platform_test.h"
26 @interface CRWStaticFileWebView (Testing)
27 + (BOOL)isStaticFileUserAgent:(NSString*)userAgent;
33 const CGRect kTestFrame = CGRectMake(5.0f, 10.0f, 15.0f, 20.0f);
34 NSString* const kTestRequestGroupID = @"100042";
36 // Returns user agent for given UIWebView.
37 NSString* GetWebViewUserAgent(UIWebView* web_view) {
38 NSString* const kGetUserAgentJS = @"navigator.userAgent";
39 return [web_view stringByEvaluatingJavaScriptFromString:kGetUserAgentJS];
42 // A WebClient that stubs PreWebViewCreation/PostWebViewCreation calls for
44 class CreationUtilsWebClient : public TestWebClient {
46 MOCK_CONST_METHOD0(PreWebViewCreation, void());
47 MOCK_CONST_METHOD1(PostWebViewCreation, void(UIWebView* web_view));
50 class WebViewCreationUtilsTest : public PlatformTest {
52 WebViewCreationUtilsTest() : ui_thread_(WebThread::UI, &message_loop_) {}
55 void SetUp() override {
56 PlatformTest::SetUp();
58 [[NSUserDefaults standardUserDefaults] boolForKey:@"LogJavascript"];
59 SetWebClient(&creation_utils_web_client_);
60 creation_utils_web_client_.SetUserAgent("TestUA", false);
62 void TearDown() override {
63 SetWebClient(nullptr);
64 [[NSUserDefaults standardUserDefaults] setBool:logJavaScriptPref_
65 forKey:@"LogJavascript"];
66 PlatformTest::TearDown();
68 // Sets up expectation for WebClient::PreWebViewCreation and
69 // WebClient::PostWebViewCreation calls. Captures UIWebView passed to
70 // PostWebViewCreation into captured_web_view param.
71 void ExpectWebClientCalls(UIWebView** captured_web_view) const {
72 EXPECT_CALL(creation_utils_web_client_, PreWebViewCreation()).Times(1);
73 EXPECT_CALL(creation_utils_web_client_, PostWebViewCreation(testing::_))
75 .WillOnce(testing::SaveArg<0>(captured_web_view));
78 // BrowserState, UIThread and MessageLoop required by factory functions.
79 web::TestBrowserState browser_state_;
80 base::MessageLoop message_loop_;
81 web::TestWebThread ui_thread_;
84 // Original value of @"LogJavascript" pref from NSUserDefaults.
85 BOOL logJavaScriptPref_;
86 // WebClient that stubs PreWebViewCreation/PostWebViewCreation.
87 CreationUtilsWebClient creation_utils_web_client_;
90 // Tests that a web view created with a certain id returns the same
91 // requestGroupID in the user agent string.
92 TEST_F(WebViewCreationUtilsTest, CreationWithRequestGroupID) {
93 UIWebView* captured_web_view = nil;
94 ExpectWebClientCalls(&captured_web_view);
96 base::scoped_nsobject<UIWebView> web_view(
97 CreateWebView(kTestFrame, kTestRequestGroupID, NO));
98 EXPECT_TRUE([web_view isMemberOfClass:[UIWebView class]]);
99 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
100 EXPECT_NSEQ(web_view, captured_web_view);
102 NSString* const kExpectedUserAgent =
103 [NSString stringWithFormat:@"TestUA (%@)", kTestRequestGroupID];
104 NSString* const kActualUserAgent = GetWebViewUserAgent(web_view);
105 EXPECT_NSEQ(kExpectedUserAgent, kActualUserAgent);
106 EXPECT_TRUE([ExtractRequestGroupIDFromUserAgent(kActualUserAgent)
107 isEqualToString:kTestRequestGroupID]);
110 // Tests that a web view not created for displaying static file content from
111 // the application bundle does not return a user agent that allows static file
113 TEST_F(WebViewCreationUtilsTest, CRWStaticFileWebViewFalse) {
114 base::scoped_nsobject<UIWebView> web_view(
115 CreateWebView(CGRectZero, kTestRequestGroupID, NO));
116 EXPECT_FALSE([CRWStaticFileWebView
117 isStaticFileUserAgent:GetWebViewUserAgent(web_view)]);
120 // Tests web::CreateWebView function that it correctly returns a UIWebView with
121 // the correct frame and calls WebClient::PreWebViewCreation/
122 // WebClient::PostWebViewCreation methods.
123 TEST_F(WebViewCreationUtilsTest, Creation) {
124 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"LogJavascript"];
126 UIWebView* captured_web_view = nil;
127 ExpectWebClientCalls(&captured_web_view);
129 base::scoped_nsobject<UIWebView> web_view(CreateWebView(kTestFrame));
130 EXPECT_TRUE([web_view isMemberOfClass:[UIWebView class]]);
131 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
132 EXPECT_NSEQ(web_view, captured_web_view);
135 // Tests web::CreateWKWebView function that it correctly returns a WKWebView
136 // with the correct frame and WKProcessPool.
137 TEST_F(WebViewCreationUtilsTest, WKWebViewCreationWithBrowserState) {
138 CR_TEST_REQUIRES_WK_WEB_VIEW();
140 base::scoped_nsobject<WKWebView> web_view(
141 CreateWKWebView(kTestFrame, &browser_state_));
143 EXPECT_TRUE([web_view isKindOfClass:[WKWebView class]]);
144 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
146 // Make sure that web view's configuration shares the same process pool with
147 // browser state's configuration. Otherwise cookie will not be immediately
148 // shared between different web views.
149 WKWebViewConfigurationProvider& config_provider =
150 WKWebViewConfigurationProvider::FromBrowserState(&browser_state_);
151 EXPECT_EQ(config_provider.GetWebViewConfiguration().processPool,
152 [[web_view configuration] processPool]);
155 // Tests that web::CreateWKWebView always returns a web view with the same
157 TEST_F(WebViewCreationUtilsTest, WKWebViewsShareProcessPool) {
158 CR_TEST_REQUIRES_WK_WEB_VIEW();
160 base::scoped_nsobject<WKWebView> web_view(
161 CreateWKWebView(kTestFrame, &browser_state_));
162 ASSERT_TRUE(web_view);
163 base::scoped_nsobject<WKWebView> web_view2(
164 CreateWKWebView(kTestFrame, &browser_state_));
165 ASSERT_TRUE(web_view2);
167 // Make sure that web views share the same non-nil process pool. Otherwise
168 // cookie will not be immediately shared between different web views.
169 EXPECT_TRUE([[web_view configuration] processPool]);
170 EXPECT_EQ([[web_view configuration] processPool],
171 [[web_view2 configuration] processPool]);
176 // Tests web::CreateWebView function that it correctly returns a CRWDebugWebView
177 // with the correct frame and calls WebClient::PreWebViewCreation/
178 // WebClient::PostWebViewCreation methods.
179 TEST_F(WebViewCreationUtilsTest, DebugCreation) {
180 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LogJavascript"];
182 UIWebView* captured_web_view = nil;
183 ExpectWebClientCalls(&captured_web_view);
185 base::scoped_nsobject<UIWebView> web_view(CreateWebView(kTestFrame));
186 EXPECT_TRUE([web_view isMemberOfClass:[CRWDebugWebView class]]);
187 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
188 EXPECT_NSEQ(web_view, captured_web_view);
191 // Tests that getting a WKWebView from the util methods correctly maintains
192 // the global active wkwebview count (which is debug-only).
193 TEST_F(WebViewCreationUtilsTest, GetActiveWKWebViewsCount) {
194 CR_TEST_REQUIRES_WK_WEB_VIEW();
195 base::scoped_nsobject<WKWebView> web_view1(
196 CreateWKWebView(CGRectZero, &browser_state_));
197 EXPECT_EQ(1U, GetActiveWKWebViewsCount());
198 base::scoped_nsobject<WKWebView> web_view2(
199 CreateWKWebView(CGRectZero, &browser_state_));
200 EXPECT_EQ(2U, GetActiveWKWebViewsCount());
202 EXPECT_EQ(1U, GetActiveWKWebViewsCount());
204 EXPECT_EQ(0U, GetActiveWKWebViewsCount());
207 #endif // defined(NDEBUG)
209 // Tests web::CreateStaticFileWebView that it correctly returns a
210 // CRWStaticFileWebView with the correct frame, user agent, and calls
211 // WebClient::PreWebViewCreation/WebClient::PostWebViewCreation methods.
212 TEST_F(WebViewCreationUtilsTest, TestNewStaticFileWebViewTrue) {
213 UIWebView* captured_web_view = nil;
214 ExpectWebClientCalls(&captured_web_view);
216 base::scoped_nsobject<UIWebView> web_view(
217 CreateStaticFileWebView(kTestFrame, &browser_state_));
218 ASSERT_TRUE([web_view isMemberOfClass:[CRWStaticFileWebView class]]);
219 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
220 EXPECT_NSEQ(web_view, captured_web_view);
222 NSString* user_agent = GetWebViewUserAgent(web_view);
223 EXPECT_TRUE([CRWStaticFileWebView isStaticFileUserAgent:user_agent]);
226 // Tests web::CreateSimpleWebViewController returns a CRWSimpleWebViewController
227 // instance with a web view.
228 TEST_F(WebViewCreationUtilsTest, CreateSimpleWebViewController) {
229 base::scoped_nsprotocol<id<CRWSimpleWebViewController>>
230 simpleWebViewController(
231 CreateSimpleWebViewController(CGRectZero, nullptr, UI_WEB_VIEW_TYPE));
232 EXPECT_TRUE([simpleWebViewController view]);