page_script_util_unittest.mm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.
  4. #import "ios/web/js_messaging/page_script_util.h"
  5. #import <UIKit/UIKit.h>
  6. #import <WebKit/WebKit.h>
  7. #include <memory>
  8. #include "base/strings/sys_string_conversions.h"
  9. #import "base/test/ios/wait_util.h"
  10. #import "ios/web/common/web_view_creation_util.h"
  11. #import "ios/web/public/test/fakes/fake_web_client.h"
  12. #import "ios/web/public/test/js_test_util.h"
  13. #include "ios/web/public/test/web_test.h"
  14. #import "ios/web/test/js_test_util_internal.h"
  15. #import "testing/gtest_mac.h"
  16. #if !defined(__has_feature) || !__has_feature(objc_arc)
  17. #error "This file requires ARC support."
  18. #endif
  19. using base::test::ios::WaitUntilConditionOrTimeout;
  20. using base::test::ios::kWaitForPageLoadTimeout;
  21. namespace {
  22. NSString* GetSharedScripts() {
  23. // Scripts must be all injected at once because as soon as __gCrWeb exists,
  24. // injection is assumed to be done and __gCrWeb.message is used.
  25. return [NSString stringWithFormat:@"%@; %@; %@",
  26. web::test::GetPageScript(@"gcrweb"),
  27. web::test::GetPageScript(@"common"),
  28. web::test::GetPageScript(@"message")];
  29. }
  30. void AddSharedScriptsToWebView(WKWebView* web_view) {
  31. web::test::ExecuteJavaScript(web_view, GetSharedScripts());
  32. }
  33. } // namespace
  34. namespace web {
  35. namespace {
  36. // A test fixture for testing the page_script_util methods.
  37. class PageScriptUtilTest : public WebTest {
  38. protected:
  39. PageScriptUtilTest() : WebTest(std::make_unique<FakeWebClient>()) {}
  40. FakeWebClient* GetWebClient() override {
  41. return static_cast<FakeWebClient*>(WebTest::GetWebClient());
  42. }
  43. };
  44. // Tests that |MakeScriptInjectableOnce| prevents a script from being injected
  45. // twice.
  46. TEST_F(PageScriptUtilTest, MakeScriptInjectableOnce) {
  47. WKWebView* web_view = BuildWKWebView(CGRectZero, GetBrowserState());
  48. NSString* identifier = @"script_id";
  49. test::ExecuteJavaScript(
  50. web_view, MakeScriptInjectableOnce(identifier, @"var value = 1;"));
  51. EXPECT_NSEQ(@(1), test::ExecuteJavaScript(web_view, @"value"));
  52. test::ExecuteJavaScript(web_view,
  53. MakeScriptInjectableOnce(identifier, @"value = 2;"));
  54. EXPECT_NSEQ(@(1), test::ExecuteJavaScript(web_view, @"value"));
  55. }
  56. // Tests that WKWebView early page script is a valid script that injects global
  57. // __gCrWeb object.
  58. TEST_F(PageScriptUtilTest, WKWebViewEarlyPageScript) {
  59. WKWebView* web_view = BuildWKWebView(CGRectZero, GetBrowserState());
  60. AddSharedScriptsToWebView(web_view);
  61. test::ExecuteJavaScript(
  62. web_view, GetDocumentStartScriptForAllFrames(GetBrowserState()));
  63. EXPECT_NSEQ(@"object", test::ExecuteJavaScript(web_view, @"typeof __gCrWeb"));
  64. }
  65. // Tests that WKWebView shared scripts are valid scripts that injects global
  66. // __gCrWeb object in an isolated world.
  67. TEST_F(PageScriptUtilTest, WKWebViewEarlyPageScriptIsolatedWorld) {
  68. if (@available(iOS 14, *)) {
  69. WKWebView* web_view = BuildWKWebView(CGRectZero, GetBrowserState());
  70. WKContentWorld* content_world = WKContentWorld.defaultClientWorld;
  71. web::test::ExecuteJavaScript(web_view, content_world, GetSharedScripts());
  72. test::ExecuteJavaScript(
  73. web_view, content_world,
  74. GetDocumentStartScriptForAllFrames(GetBrowserState()));
  75. EXPECT_NSEQ(@"object", test::ExecuteJavaScript(web_view, content_world,
  76. @"typeof __gCrWeb"));
  77. }
  78. }
  79. // Tests that embedder's WKWebView script is included into early script.
  80. TEST_F(PageScriptUtilTest, WKEmbedderScript) {
  81. GetWebClient()->SetEarlyPageScript(@"__gCrEmbedder = {};");
  82. WKWebView* web_view = BuildWKWebView(CGRectZero, GetBrowserState());
  83. AddSharedScriptsToWebView(web_view);
  84. test::ExecuteJavaScript(
  85. web_view, GetDocumentStartScriptForAllFrames(GetBrowserState()));
  86. test::ExecuteJavaScript(
  87. web_view, GetDocumentStartScriptForMainFrame(GetBrowserState()));
  88. EXPECT_NSEQ(@"object",
  89. test::ExecuteJavaScript(web_view, @"typeof __gCrEmbedder"));
  90. }
  91. } // namespace
  92. } // namespace web