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/js/crw_js_early_script_manager.h"
7 #include "base/mac/scoped_nsobject.h"
8 #import "ios/web/public/test/crw_test_js_injection_receiver.h"
9 #include "ios/web/public/web_client.h"
10 #import "ios/web/web_state/js/page_script_util.h"
11 #import "testing/gtest_mac.h"
12 #include "testing/platform_test.h"
17 class CRWJSEarlyScriptManagerTest : public PlatformTest {
19 void SetUp() override {
20 PlatformTest::SetUp();
21 SetWebClient(&web_client_);
22 receiver_.reset([[CRWTestJSInjectionReceiver alloc] init]);
23 earlyScriptManager_.reset(static_cast<CRWJSEarlyScriptManager*>(
24 [[receiver_ instanceOfClass:[CRWJSEarlyScriptManager class]] retain]));
26 void TearDown() override {
27 SetWebClient(nullptr);
28 PlatformTest::TearDown();
30 // Required for CRWJSEarlyScriptManager creation.
31 base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver_;
32 // Testable CRWJSEarlyScriptManager.
33 base::scoped_nsobject<CRWJSEarlyScriptManager> earlyScriptManager_;
34 // WebClient required for getting early page script.
35 WebClient web_client_;
38 // Tests that CRWJSEarlyScriptManager's content is the same as returned by
39 // web::GetEarlyPageScript.
40 TEST_F(CRWJSEarlyScriptManagerTest, Content) {
41 NSString* injectionContent = [earlyScriptManager_ staticInjectionContent];
42 NSString* earlyScript = GetEarlyPageScript([receiver_ webViewType]);
43 // |earlyScript| is a substring of |injectionContent|. The latter wraps the
44 // former with "if (typeof __gCrWeb !== 'object')" check to avoid multiple
46 EXPECT_NE(NSNotFound, [injectionContent rangeOfString:earlyScript].location);