| 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" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 namespace web { | 35 namespace web { |
| 36 | 36 |
| 37 // Path for test favicon file. | 37 // Path for test favicon file. |
| 38 const char kFaviconPath[] = "ios/web/test/data/testfavicon.png"; | 38 const char kFaviconPath[] = "ios/web/test/data/testfavicon.png"; |
| 39 // URL for mock WebUI page. | 39 // URL for mock WebUI page. |
| 40 const char kTestWebUIUrl[] = "testwebui://test/"; | 40 const char kTestWebUIUrl[] = "testwebui://test/"; |
| 41 // URL for mock favicon. | 41 // URL for mock favicon. |
| 42 const char kFaviconUrl[] = "testwebui://favicon/"; | 42 const char kFaviconUrl[] = "testwebui://favicon/"; |
| 43 // Name of test Mojo module. | |
| 44 const char kMojoModuleName[] = "test-mojo-module"; | |
| 45 // HTML for mock WebUI page. | 43 // HTML for mock WebUI page. |
| 46 NSString* kHtml = @"<html>Hello World</html>"; | 44 NSString* kHtml = @"<html>Hello World</html>"; |
| 47 // Mojo module for WebUI page. | |
| 48 NSString* kMojoModule = @"service_provider.connect('Test');"; | |
| 49 | 45 |
| 50 // Mock of WebStateImpl to check that LoadHtml and ExecuteJavaScript are called | 46 // Mock of WebStateImpl to check that LoadHtml and ExecuteJavaScript are called |
| 51 // as expected. | 47 // as expected. |
| 52 class MockWebStateImpl : public WebStateImpl { | 48 class MockWebStateImpl : public WebStateImpl { |
| 53 public: | 49 public: |
| 54 MockWebStateImpl(const WebState::CreateParams& params) | 50 MockWebStateImpl(const WebState::CreateParams& params) |
| 55 : WebStateImpl(params), last_committed_url_(kTestWebUIUrl) {} | 51 : WebStateImpl(params), last_committed_url_(kTestWebUIUrl) {} |
| 56 MOCK_METHOD2(LoadWebUIHtml, | 52 MOCK_METHOD2(LoadWebUIHtml, |
| 57 void(const base::string16& html, const GURL& url)); | 53 void(const base::string16& html, const GURL& url)); |
| 58 MOCK_METHOD1(ExecuteJavaScript, void(const base::string16& javascript)); | 54 MOCK_METHOD1(ExecuteJavaScript, void(const base::string16& javascript)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 76 completion_handler_([completion_handler copy]) {} | 72 completion_handler_([completion_handler copy]) {} |
| 77 | 73 |
| 78 void Start() override { | 74 void Start() override { |
| 79 if (url_.spec() == kFaviconUrl) { | 75 if (url_.spec() == kFaviconUrl) { |
| 80 base::FilePath favicon_path; | 76 base::FilePath favicon_path; |
| 81 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); | 77 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); |
| 82 favicon_path = favicon_path.AppendASCII(kFaviconPath); | 78 favicon_path = favicon_path.AppendASCII(kFaviconPath); |
| 83 NSData* favicon = [NSData | 79 NSData* favicon = [NSData |
| 84 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; | 80 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; |
| 85 completion_handler_(favicon, this); | 81 completion_handler_(favicon, this); |
| 86 } else if (url_.path().find(kMojoModuleName) != std::string::npos) { | |
| 87 completion_handler_([kMojoModule dataUsingEncoding:NSUTF8StringEncoding], | |
| 88 this); | |
| 89 | |
| 90 } else if (url_.scheme().find("test") != std::string::npos) { | 82 } else if (url_.scheme().find("test") != std::string::npos) { |
| 91 completion_handler_([kHtml dataUsingEncoding:NSUTF8StringEncoding], this); | 83 completion_handler_([kHtml dataUsingEncoding:NSUTF8StringEncoding], this); |
| 92 } else { | 84 } else { |
| 93 NOTREACHED(); | 85 NOTREACHED(); |
| 94 } | 86 } |
| 95 } | 87 } |
| 96 | 88 |
| 97 private: | 89 private: |
| 98 // The URL to fetch. | 90 // The URL to fetch. |
| 99 const GURL url_; | 91 const GURL url_; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 base::string16 html(base::SysNSStringToUTF16(kHtml)); | 141 base::string16 html(base::SysNSStringToUTF16(kHtml)); |
| 150 GURL url(kTestWebUIUrl); | 142 GURL url(kTestWebUIUrl); |
| 151 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); | 143 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); |
| 152 std::unique_ptr<web::NavigationContext> context = | 144 std::unique_ptr<web::NavigationContext> context = |
| 153 NavigationContextImpl::CreateNavigationContext( | 145 NavigationContextImpl::CreateNavigationContext( |
| 154 web_state_impl_.get(), url, | 146 web_state_impl_.get(), url, |
| 155 ui::PageTransition::PAGE_TRANSITION_AUTO_BOOKMARK); | 147 ui::PageTransition::PAGE_TRANSITION_AUTO_BOOKMARK); |
| 156 web_state_impl_->OnNavigationStarted(context.get()); | 148 web_state_impl_->OnNavigationStarted(context.get()); |
| 157 } | 149 } |
| 158 | 150 |
| 159 // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs | |
| 160 // JavaScript to fetch a mojo resource. | |
| 161 TEST_F(CRWWebUIManagerTest, HandleLoadMojoRequest) { | |
| 162 // Create mock JavaScript message to request a mojo resource. | |
| 163 std::unique_ptr<base::ListValue> arguments(new base::ListValue()); | |
| 164 arguments->AppendString(kMojoModuleName); | |
| 165 const char kTestLoadId[] = "test-load-id"; | |
| 166 arguments->AppendString(kTestLoadId); | |
| 167 base::DictionaryValue message; | |
| 168 message.SetString("message", "webui.loadMojo"); | |
| 169 message.Set("arguments", std::move(arguments)); | |
| 170 | |
| 171 // Create expected JavaScript to call. | |
| 172 std::string expected_javascript = base::StringPrintf( | |
| 173 "%s__crWeb.webUIModuleLoadNotifier.moduleLoadCompleted(\"%s\", \"%s\");", | |
| 174 base::SysNSStringToUTF8(kMojoModule).c_str(), kMojoModuleName, | |
| 175 kTestLoadId); | |
| 176 | |
| 177 EXPECT_CALL(*web_state_impl_, | |
| 178 ExecuteJavaScript(base::UTF8ToUTF16(expected_javascript))); | |
| 179 web_state_impl_->OnScriptCommandReceived("webui.loadMojo", message, | |
| 180 GURL(kTestWebUIUrl), false); | |
| 181 } | |
| 182 } // namespace web | 151 } // namespace web |
| OLD | NEW |