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_creation_utils.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/web_state/ui/crw_debug_web_view.h"
18 #import "ios/web/web_state/ui/crw_simple_web_view_controller.h"
19 #import "ios/web/web_state/ui/crw_static_file_web_view.h"
20 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest_mac.h"
23 #include "testing/platform_test.h"
25 @interface CRWStaticFileWebView (Testing)
26 + (BOOL)isStaticFileUserAgent:(NSString*)userAgent;
32 const CGRect kTestFrame = CGRectMake(5.0f, 10.0f, 15.0f, 20.0f);
33 NSString* const kTestRequestGroupID = @"100042";
35 // Returns user agent for given UIWebView.
36 NSString* GetWebViewUserAgent(UIWebView* web_view) {
37 NSString* const kGetUserAgentJS = @"navigator.userAgent";
38 return [web_view stringByEvaluatingJavaScriptFromString:kGetUserAgentJS];
41 // A WebClient that stubs PreWebViewCreation/PostWebViewCreation calls for
43 class CreationUtilsWebClient : public TestWebClient {
45 MOCK_CONST_METHOD0(PreWebViewCreation, void());
46 MOCK_CONST_METHOD1(PostWebViewCreation, void(UIWebView* web_view));
49 class WebViewCreationUtilsTest : public PlatformTest {
51 WebViewCreationUtilsTest() : ui_thread_(WebThread::UI, &message_loop_) {}
54 void SetUp() override {
55 PlatformTest::SetUp();
56 logJavaScriptPref_ = [[NSUserDefaults standardUserDefaults]
57 boolForKey:@"LogJavascript"];
58 SetWebClient(&creation_utils_web_client_);
59 creation_utils_web_client_.SetUserAgent("TestUA", false);
61 void TearDown() override {
62 SetWebClient(nullptr);
63 [[NSUserDefaults standardUserDefaults] setBool:logJavaScriptPref_
64 forKey:@"LogJavascript"];
65 PlatformTest::TearDown();
67 // Sets up expectation for WebClient::PreWebViewCreation and
68 // WebClient::PostWebViewCreation calls. Captures UIWebView passed to
69 // PostWebViewCreation into captured_web_view param.
70 void ExpectWebClientCalls(UIWebView** captured_web_view) const {
71 EXPECT_CALL(creation_utils_web_client_, PreWebViewCreation()).Times(1);
72 EXPECT_CALL(creation_utils_web_client_, PostWebViewCreation(
73 testing::_)).Times(1).WillOnce(testing::SaveArg<0>(captured_web_view));
76 // BrowserState, UIThread and MessageLoop required by factory functions.
77 web::TestBrowserState browser_state_;
78 base::MessageLoop message_loop_;
79 web::TestWebThread ui_thread_;
81 // Original value of @"LogJavascript" pref from NSUserDefaults.
82 BOOL logJavaScriptPref_;
83 // WebClient that stubs PreWebViewCreation/PostWebViewCreation.
84 CreationUtilsWebClient creation_utils_web_client_;
87 // Tests that a web view created with a certain id returns the same
88 // requestGroupID in the user agent string.
89 TEST_F(WebViewCreationUtilsTest, CreationWithRequestGroupID) {
90 UIWebView* captured_web_view = nil;
91 ExpectWebClientCalls(&captured_web_view);
93 base::scoped_nsobject<UIWebView> web_view(
94 CreateWebView(kTestFrame, kTestRequestGroupID, NO));
95 EXPECT_TRUE([web_view isMemberOfClass:[UIWebView class]]);
96 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
97 EXPECT_NSEQ(web_view, captured_web_view);
99 NSString* const kExpectedUserAgent = [NSString stringWithFormat:
100 @"TestUA (%@)", kTestRequestGroupID];
101 NSString* const kActualUserAgent = GetWebViewUserAgent(web_view);
102 EXPECT_NSEQ(kExpectedUserAgent, kActualUserAgent);
103 EXPECT_TRUE([ExtractRequestGroupIDFromUserAgent(kActualUserAgent)
104 isEqualToString:kTestRequestGroupID]);
107 // Tests that a web view not created for displaying static file content from
108 // the application bundle does not return a user agent that allows static file
110 TEST_F(WebViewCreationUtilsTest, CRWStaticFileWebViewFalse) {
111 base::scoped_nsobject<UIWebView> web_view(
112 CreateWebView(CGRectZero, kTestRequestGroupID, NO));
113 EXPECT_FALSE([CRWStaticFileWebView isStaticFileUserAgent:
114 GetWebViewUserAgent(web_view)]);
117 // Tests web::CreateWebView function that it correctly returns a UIWebView with
118 // the correct frame and calls WebClient::PreWebViewCreation/
119 // WebClient::PostWebViewCreation methods.
120 TEST_F(WebViewCreationUtilsTest, Creation) {
121 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"LogJavascript"];
123 UIWebView* captured_web_view = nil;
124 ExpectWebClientCalls(&captured_web_view);
126 base::scoped_nsobject<UIWebView> web_view(CreateWebView(kTestFrame));
127 EXPECT_TRUE([web_view isMemberOfClass:[UIWebView class]]);
128 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
129 EXPECT_NSEQ(web_view, captured_web_view);
132 // Tests web::CreateWKWebView function that it correctly returns a WKWebView
133 // with the correct frame and WKProcessPool.
134 TEST_F(WebViewCreationUtilsTest, WKWebViewCreationWithBrowserState) {
135 CR_TEST_REQUIRES_WK_WEB_VIEW();
137 base::scoped_nsobject<WKWebView> web_view(
138 CreateWKWebView(kTestFrame, &browser_state_));
140 EXPECT_TRUE([web_view isKindOfClass:[WKWebView class]]);
141 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
143 // Make sure that web view's configuration shares the same process pool with
144 // browser state's configuration. Otherwise cookie will not be immediately
145 // shared between different web views.
146 WKWebViewConfigurationProvider& config_provider =
147 WKWebViewConfigurationProvider::FromBrowserState(&browser_state_);
148 EXPECT_EQ(config_provider.GetWebViewConfiguration().processPool,
149 [[web_view configuration] processPool]);
152 // Tests that web::CreateWKWebView always returns a web view with the same
154 TEST_F(WebViewCreationUtilsTest, WKWebViewsShareProcessPool) {
155 CR_TEST_REQUIRES_WK_WEB_VIEW();
157 base::scoped_nsobject<WKWebView> web_view(
158 CreateWKWebView(kTestFrame, &browser_state_));
159 ASSERT_TRUE(web_view);
160 base::scoped_nsobject<WKWebView> web_view2(
161 CreateWKWebView(kTestFrame, &browser_state_));
162 ASSERT_TRUE(web_view2);
164 // Make sure that web views share the same non-nil process pool. Otherwise
165 // cookie will not be immediately shared between different web views.
166 EXPECT_TRUE([[web_view configuration] processPool]);
167 EXPECT_EQ([[web_view configuration] processPool],
168 [[web_view2 configuration] processPool]);
173 // Tests web::CreateWebView function that it correctly returns a CRWDebugWebView
174 // with the correct frame and calls WebClient::PreWebViewCreation/
175 // WebClient::PostWebViewCreation methods.
176 TEST_F(WebViewCreationUtilsTest, DebugCreation) {
177 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LogJavascript"];
179 UIWebView* captured_web_view = nil;
180 ExpectWebClientCalls(&captured_web_view);
182 base::scoped_nsobject<UIWebView> web_view(CreateWebView(kTestFrame));
183 EXPECT_TRUE([web_view isMemberOfClass:[CRWDebugWebView class]]);
184 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
185 EXPECT_NSEQ(web_view, captured_web_view);
188 // Tests that getting a WKWebView from the util methods correctly maintains
189 // the global active wkwebview count (which is debug-only).
190 TEST_F(WebViewCreationUtilsTest, GetActiveWKWebViewsCount) {
191 CR_TEST_REQUIRES_WK_WEB_VIEW();
192 base::scoped_nsobject<WKWebView> web_view1(
193 CreateWKWebView(CGRectZero, &browser_state_));
194 EXPECT_EQ(1U, GetActiveWKWebViewsCount());
195 base::scoped_nsobject<WKWebView> web_view2(
196 CreateWKWebView(CGRectZero, &browser_state_));
197 EXPECT_EQ(2U, GetActiveWKWebViewsCount());
199 EXPECT_EQ(1U, GetActiveWKWebViewsCount());
201 EXPECT_EQ(0U, GetActiveWKWebViewsCount());
204 #endif // defined(NDEBUG)
206 // Tests web::CreateStaticFileWebView that it correctly returns a
207 // CRWStaticFileWebView with the correct frame, user agent, and calls
208 // WebClient::PreWebViewCreation/WebClient::PostWebViewCreation methods.
209 TEST_F(WebViewCreationUtilsTest, TestNewStaticFileWebViewTrue) {
210 UIWebView* captured_web_view = nil;
211 ExpectWebClientCalls(&captured_web_view);
213 base::scoped_nsobject<UIWebView> web_view(
214 CreateStaticFileWebView(kTestFrame, &browser_state_));
215 ASSERT_TRUE([web_view isMemberOfClass:[CRWStaticFileWebView class]]);
216 EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
217 EXPECT_NSEQ(web_view, captured_web_view);
219 NSString* user_agent = GetWebViewUserAgent(web_view);
220 EXPECT_TRUE([CRWStaticFileWebView isStaticFileUserAgent:user_agent]);
223 // Tests web::CreateSimpleWebViewController returns a CRWSimpleWebViewController
224 // instance with a web view.
225 TEST_F(WebViewCreationUtilsTest, CreateSimpleWebViewController) {
226 base::scoped_nsprotocol<id<CRWSimpleWebViewController>>
227 simpleWebViewController(
228 CreateSimpleWebViewController(CGRectZero, nullptr, UI_WEB_VIEW_TYPE));
229 EXPECT_TRUE([simpleWebViewController view]);