| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/web/webui/crw_web_ui_manager.h" | 5 #import "ios/web/webui/crw_web_ui_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #import "base/mac/scoped_nsobject.h" | |
| 13 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 15 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 16 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 17 #import "base/strings/sys_string_conversions.h" | 16 #import "base/strings/sys_string_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/values.h" | 18 #include "base/values.h" |
| 20 #include "ios/web/public/test/fakes/test_browser_state.h" | 19 #include "ios/web/public/test/fakes/test_browser_state.h" |
| 21 #import "ios/web/public/test/fakes/test_web_client.h" | 20 #import "ios/web/public/test/fakes/test_web_client.h" |
| 22 #include "ios/web/public/test/scoped_testing_web_client.h" | 21 #include "ios/web/public/test/scoped_testing_web_client.h" |
| 23 #include "ios/web/public/test/web_test.h" | 22 #include "ios/web/public/test/web_test.h" |
| 24 #import "ios/web/web_state/navigation_context_impl.h" | 23 #import "ios/web/web_state/navigation_context_impl.h" |
| 25 #import "ios/web/web_state/web_state_impl.h" | 24 #import "ios/web/web_state/web_state_impl.h" |
| 26 #import "ios/web/webui/crw_web_ui_page_builder.h" | 25 #import "ios/web/webui/crw_web_ui_page_builder.h" |
| 27 #import "ios/web/webui/url_fetcher_block_adapter.h" | 26 #import "ios/web/webui/url_fetcher_block_adapter.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #import "testing/gtest_mac.h" | 29 #import "testing/gtest_mac.h" |
| 31 | 30 |
| 31 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 32 #error "This file requires ARC support." |
| 33 #endif |
| 34 |
| 32 namespace web { | 35 namespace web { |
| 33 | 36 |
| 34 // Path for test favicon file. | 37 // Path for test favicon file. |
| 35 const char kFaviconPath[] = "ios/web/test/data/testfavicon.png"; | 38 const char kFaviconPath[] = "ios/web/test/data/testfavicon.png"; |
| 36 // URL for mock WebUI page. | 39 // URL for mock WebUI page. |
| 37 const char kTestWebUIUrl[] = "testwebui://test/"; | 40 const char kTestWebUIUrl[] = "testwebui://test/"; |
| 38 // URL for mock favicon. | 41 // URL for mock favicon. |
| 39 const char kFaviconUrl[] = "testwebui://favicon/"; | 42 const char kFaviconUrl[] = "testwebui://favicon/"; |
| 40 // Name of test Mojo module. | 43 // Name of test Mojo module. |
| 41 const char kMojoModuleName[] = "test-mojo-module"; | 44 const char kMojoModuleName[] = "test-mojo-module"; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 72 url_(url), | 75 url_(url), |
| 73 completion_handler_([completion_handler copy]) {} | 76 completion_handler_([completion_handler copy]) {} |
| 74 | 77 |
| 75 void Start() override { | 78 void Start() override { |
| 76 if (url_.spec() == kFaviconUrl) { | 79 if (url_.spec() == kFaviconUrl) { |
| 77 base::FilePath favicon_path; | 80 base::FilePath favicon_path; |
| 78 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); | 81 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); |
| 79 favicon_path = favicon_path.AppendASCII(kFaviconPath); | 82 favicon_path = favicon_path.AppendASCII(kFaviconPath); |
| 80 NSData* favicon = [NSData | 83 NSData* favicon = [NSData |
| 81 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; | 84 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; |
| 82 completion_handler_.get()(favicon, this); | 85 completion_handler_(favicon, this); |
| 83 } else if (url_.path().find(kMojoModuleName) != std::string::npos) { | 86 } else if (url_.path().find(kMojoModuleName) != std::string::npos) { |
| 84 completion_handler_.get()( | 87 completion_handler_([kMojoModule dataUsingEncoding:NSUTF8StringEncoding], |
| 85 [kMojoModule dataUsingEncoding:NSUTF8StringEncoding], this); | 88 this); |
| 86 | 89 |
| 87 } else if (url_.scheme().find("test") != std::string::npos) { | 90 } else if (url_.scheme().find("test") != std::string::npos) { |
| 88 completion_handler_.get()([kHtml dataUsingEncoding:NSUTF8StringEncoding], | 91 completion_handler_([kHtml dataUsingEncoding:NSUTF8StringEncoding], this); |
| 89 this); | |
| 90 } else { | 92 } else { |
| 91 NOTREACHED(); | 93 NOTREACHED(); |
| 92 } | 94 } |
| 93 } | 95 } |
| 94 | 96 |
| 95 private: | 97 private: |
| 96 // The URL to fetch. | 98 // The URL to fetch. |
| 97 const GURL url_; | 99 const GURL url_; |
| 98 // Callback for resource load. | 100 // Callback for resource load. |
| 99 base::mac::ScopedBlock<URLFetcherBlockAdapterCompletion> completion_handler_; | 101 URLFetcherBlockAdapterCompletion completion_handler_; |
| 100 }; | 102 }; |
| 101 | 103 |
| 102 } // namespace web | 104 } // namespace web |
| 103 | 105 |
| 104 // Subclass of CRWWebUIManager for testing. | 106 // Subclass of CRWWebUIManager for testing. |
| 105 @interface CRWTestWebUIManager : CRWWebUIManager | 107 @interface CRWTestWebUIManager : CRWWebUIManager |
| 106 // Use mock URLFetcherBlockAdapter. | 108 // Use mock URLFetcherBlockAdapter. |
| 107 - (std::unique_ptr<web::URLFetcherBlockAdapter>) | 109 - (std::unique_ptr<web::URLFetcherBlockAdapter>) |
| 108 fetcherForURL:(const GURL&)URL | 110 fetcherForURL:(const GURL&)URL |
| 109 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler; | 111 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 121 namespace web { | 123 namespace web { |
| 122 | 124 |
| 123 // Test fixture for testing CRWWebUIManager | 125 // Test fixture for testing CRWWebUIManager |
| 124 class CRWWebUIManagerTest : public web::WebTest { | 126 class CRWWebUIManagerTest : public web::WebTest { |
| 125 protected: | 127 protected: |
| 126 void SetUp() override { | 128 void SetUp() override { |
| 127 PlatformTest::SetUp(); | 129 PlatformTest::SetUp(); |
| 128 test_browser_state_.reset(new TestBrowserState()); | 130 test_browser_state_.reset(new TestBrowserState()); |
| 129 WebState::CreateParams params(test_browser_state_.get()); | 131 WebState::CreateParams params(test_browser_state_.get()); |
| 130 web_state_impl_.reset(new MockWebStateImpl(params)); | 132 web_state_impl_.reset(new MockWebStateImpl(params)); |
| 131 web_ui_manager_.reset( | 133 web_ui_manager_ = |
| 132 [[CRWTestWebUIManager alloc] initWithWebState:web_state_impl_.get()]); | 134 [[CRWTestWebUIManager alloc] initWithWebState:web_state_impl_.get()]; |
| 133 } | 135 } |
| 134 | 136 |
| 135 // TestBrowserState for creation of WebStateImpl. | 137 // TestBrowserState for creation of WebStateImpl. |
| 136 std::unique_ptr<TestBrowserState> test_browser_state_; | 138 std::unique_ptr<TestBrowserState> test_browser_state_; |
| 137 // MockWebStateImpl for detection of LoadHtml and EvaluateJavaScriptAync | 139 // MockWebStateImpl for detection of LoadHtml and EvaluateJavaScriptAync |
| 138 // calls. | 140 // calls. |
| 139 std::unique_ptr<MockWebStateImpl> web_state_impl_; | 141 std::unique_ptr<MockWebStateImpl> web_state_impl_; |
| 140 // WebUIManager for testing. | 142 // WebUIManager for testing. |
| 141 base::scoped_nsobject<CRWTestWebUIManager> web_ui_manager_; | 143 CRWTestWebUIManager* web_ui_manager_; |
| 142 }; | 144 }; |
| 143 | 145 |
| 144 // Tests that CRWWebUIManager observes provisional navigation and invokes an | 146 // Tests that CRWWebUIManager observes provisional navigation and invokes an |
| 145 // HTML load in web state. | 147 // HTML load in web state. |
| 146 TEST_F(CRWWebUIManagerTest, LoadWebUI) { | 148 TEST_F(CRWWebUIManagerTest, LoadWebUI) { |
| 147 base::string16 html(base::SysNSStringToUTF16(kHtml)); | 149 base::string16 html(base::SysNSStringToUTF16(kHtml)); |
| 148 GURL url(kTestWebUIUrl); | 150 GURL url(kTestWebUIUrl); |
| 149 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); | 151 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); |
| 150 std::unique_ptr<web::NavigationContext> context = | 152 std::unique_ptr<web::NavigationContext> context = |
| 151 NavigationContextImpl::CreateNavigationContext( | 153 NavigationContextImpl::CreateNavigationContext( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 171 "%s__crWeb.webUIModuleLoadNotifier.moduleLoadCompleted(\"%s\", \"%s\");", | 173 "%s__crWeb.webUIModuleLoadNotifier.moduleLoadCompleted(\"%s\", \"%s\");", |
| 172 base::SysNSStringToUTF8(kMojoModule).c_str(), kMojoModuleName, | 174 base::SysNSStringToUTF8(kMojoModule).c_str(), kMojoModuleName, |
| 173 kTestLoadId); | 175 kTestLoadId); |
| 174 | 176 |
| 175 EXPECT_CALL(*web_state_impl_, | 177 EXPECT_CALL(*web_state_impl_, |
| 176 ExecuteJavaScript(base::UTF8ToUTF16(expected_javascript))); | 178 ExecuteJavaScript(base::UTF8ToUTF16(expected_javascript))); |
| 177 web_state_impl_->OnScriptCommandReceived("webui.loadMojo", message, | 179 web_state_impl_->OnScriptCommandReceived("webui.loadMojo", message, |
| 178 GURL(kTestWebUIUrl), false); | 180 GURL(kTestWebUIUrl), false); |
| 179 } | 181 } |
| 180 } // namespace web | 182 } // namespace web |
| OLD | NEW |