summaryrefslogtreecommitdiffstats
path: root/ios/web/web_state/web_view_internal_creation_util_unittest.mm
blob: 6616a28d652426b3f8efefbb90bec91682710ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/web/web_state/web_view_internal_creation_util.h"

#import <CoreGraphics/CoreGraphics.h>
#import <WebKit/WebKit.h>

#include "base/mac/scoped_nsobject.h"
#include "base/message_loop/message_loop.h"
#include "ios/web/net/request_group_util.h"
#include "ios/web/public/test/scoped_testing_web_client.h"
#include "ios/web/public/test/test_browser_state.h"
#import "ios/web/public/test/test_web_client.h"
#include "ios/web/public/test/test_web_thread.h"
#import "ios/web/public/web_view_creation_util.h"
#import "ios/web/test/web_test.h"
#import "ios/web/web_state/ui/crw_simple_web_view_controller.h"
#import "ios/web/web_state/ui/crw_static_file_web_view.h"
#import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest_mac.h"

@interface CRWStaticFileWebView (Testing)
+ (BOOL)isStaticFileUserAgent:(NSString*)userAgent;
@end

namespace web {
namespace {

const CGRect kTestFrame = CGRectMake(5.0f, 10.0f, 15.0f, 20.0f);
NSString* const kTestRequestGroupID = @"100042";

// Returns user agent for given UIWebView.
NSString* GetWebViewUserAgent(UIWebView* web_view) {
  NSString* const kGetUserAgentJS = @"navigator.userAgent";
  return [web_view stringByEvaluatingJavaScriptFromString:kGetUserAgentJS];
}

// A WebClient that stubs PreWebViewCreation/PostWebViewCreation calls for
// testing purposes.
class CreationUtilsWebClient : public TestWebClient {
 public:
  MOCK_CONST_METHOD0(PreWebViewCreation, void());
  MOCK_CONST_METHOD1(PostWebViewCreation, void(UIWebView* web_view));
};

class WebViewCreationUtilsTest : public WebTest {
 public:
  WebViewCreationUtilsTest()
      : web_client_(make_scoped_ptr(new CreationUtilsWebClient)) {}

 protected:
  CreationUtilsWebClient* creation_utils_web_client() {
    return static_cast<CreationUtilsWebClient*>(web_client_.Get());
  }
  void SetUp() override {
    WebTest::SetUp();
    logJavaScriptPref_ =
        [[NSUserDefaults standardUserDefaults] boolForKey:@"LogJavascript"];
    creation_utils_web_client()->SetUserAgent("TestUA", false);
  }
  void TearDown() override {
    [[NSUserDefaults standardUserDefaults] setBool:logJavaScriptPref_
                                            forKey:@"LogJavascript"];
    WebTest::TearDown();
  }
  // Sets up expectation for WebClient::PreWebViewCreation and
  // WebClient::PostWebViewCreation calls. Captures UIWebView passed to
  // PostWebViewCreation into captured_web_view param.
  void ExpectWebClientCalls(UIWebView** captured_web_view) {
    EXPECT_CALL(*creation_utils_web_client(), PreWebViewCreation()).Times(1);
    EXPECT_CALL(*creation_utils_web_client(), PostWebViewCreation(testing::_))
        .Times(1)
        .WillOnce(testing::SaveArg<0>(captured_web_view));
  }

 private:
  // Original value of @"LogJavascript" pref from NSUserDefaults.
  BOOL logJavaScriptPref_;
  // WebClient that stubs PreWebViewCreation/PostWebViewCreation.
  web::ScopedTestingWebClient web_client_;
};

// Tests that a web view created with a certain id returns the same
// requestGroupID in the user agent string.
TEST_F(WebViewCreationUtilsTest, CreationWithRequestGroupID) {
  UIWebView* captured_web_view = nil;
  ExpectWebClientCalls(&captured_web_view);

  base::scoped_nsobject<UIWebView> web_view(
      CreateWebView(kTestFrame, kTestRequestGroupID, NO));
  EXPECT_TRUE([web_view isMemberOfClass:[UIWebView class]]);
  EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
  EXPECT_NSEQ(web_view, captured_web_view);

  NSString* const kExpectedUserAgent =
      [NSString stringWithFormat:@"TestUA (%@)", kTestRequestGroupID];
  NSString* const kActualUserAgent = GetWebViewUserAgent(web_view);
  EXPECT_NSEQ(kExpectedUserAgent, kActualUserAgent);
  EXPECT_TRUE([ExtractRequestGroupIDFromUserAgent(kActualUserAgent)
      isEqualToString:kTestRequestGroupID]);
}

// Tests that a web view not created for displaying static file content from
// the application bundle does not return a user agent that allows static file
// content display.
TEST_F(WebViewCreationUtilsTest, CRWStaticFileWebViewFalse) {
  base::scoped_nsobject<UIWebView> web_view(
      CreateWebView(CGRectZero, kTestRequestGroupID, NO));
  EXPECT_FALSE([CRWStaticFileWebView
      isStaticFileUserAgent:GetWebViewUserAgent(web_view)]);
}

// Tests web::CreateWebView function that it correctly returns a UIWebView with
// the correct frame and calls WebClient::PreWebViewCreation/
// WebClient::PostWebViewCreation methods.
TEST_F(WebViewCreationUtilsTest, Creation) {
  [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"LogJavascript"];

  UIWebView* captured_web_view = nil;
  ExpectWebClientCalls(&captured_web_view);

  base::scoped_nsobject<UIWebView> web_view(CreateWebView(kTestFrame));
  EXPECT_TRUE([web_view isMemberOfClass:[UIWebView class]]);
  EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));
  EXPECT_NSEQ(web_view, captured_web_view);
}

// Tests web::CreateWKWebView function that it correctly returns a WKWebView
// with the correct frame and WKProcessPool.
TEST_F(WebViewCreationUtilsTest, WKWebViewCreationWithBrowserState) {
  base::scoped_nsobject<WKWebView> web_view(
      CreateWKWebView(kTestFrame, GetBrowserState()));

  EXPECT_TRUE([web_view isKindOfClass:[WKWebView class]]);
  EXPECT_TRUE(CGRectEqualToRect(kTestFrame, [web_view frame]));

  // Make sure that web view's configuration shares the same process pool with
  // browser state's configuration. Otherwise cookie will not be immediately
  // shared between different web views.
  WKWebViewConfigurationProvider& config_provider =
      WKWebViewConfigurationProvider::FromBrowserState(GetBrowserState());
  EXPECT_EQ(config_provider.GetWebViewConfiguration().processPool,
            [[web_view configuration] processPool]);
}

// Tests that web::CreateWKWebView always returns a web view with the same
// processPool.
TEST_F(WebViewCreationUtilsTest, WKWebViewsShareProcessPool) {
  base::scoped_nsobject<WKWebView> web_view(
      CreateWKWebView(kTestFrame, GetBrowserState()));
  ASSERT_TRUE(web_view);
  base::scoped_nsobject<WKWebView> web_view2(
      CreateWKWebView(kTestFrame, GetBrowserState()));
  ASSERT_TRUE(web_view2);

  // Make sure that web views share the same non-nil process pool. Otherwise
  // cookie will not be immediately shared between different web views.
  EXPECT_TRUE([[web_view configuration] processPool]);
  EXPECT_EQ([[web_view configuration] processPool],
            [[web_view2 configuration] processPool]);
}

#if !defined(NDEBUG)

// Tests that getting a WKWebView from the util methods correctly maintains
// the global active wkwebview count (which is debug-only).
TEST_F(WebViewCreationUtilsTest, GetActiveWKWebViewsCount) {
  base::scoped_nsobject<WKWebView> web_view1(
      CreateWKWebView(CGRectZero, GetBrowserState()));
  EXPECT_EQ(1U, GetActiveWKWebViewsCount());
  base::scoped_nsobject<WKWebView> web_view2(
      CreateWKWebView(CGRectZero, GetBrowserState()));
  EXPECT_EQ(2U, GetActiveWKWebViewsCount());
  web_view2.reset();
  EXPECT_EQ(1U, GetActiveWKWebViewsCount());
  web_view1.reset();
  EXPECT_EQ(0U, GetActiveWKWebViewsCount());
}

#endif  // defined(NDEBUG)

// Tests web::CreateSimpleWebViewController returns a CRWSimpleWebViewController
// instance with a web view.
TEST_F(WebViewCreationUtilsTest, CreateSimpleWebViewController) {
  base::scoped_nsprotocol<id<CRWSimpleWebViewController>>
      simpleWebViewController(
          CreateSimpleWebViewController(CGRectZero, GetBrowserState()));
  EXPECT_TRUE([simpleWebViewController view]);
}

}  // namespace
}  // namespace web