| 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" | 12 #import "base/mac/scoped_nsobject.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #import "base/strings/sys_string_conversions.h" | 17 #import "base/strings/sys_string_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "ios/web/public/test/scoped_testing_web_client.h" | 20 #include "ios/web/public/test/scoped_testing_web_client.h" |
| 20 #include "ios/web/public/test/test_browser_state.h" | 21 #include "ios/web/public/test/test_browser_state.h" |
| 21 #import "ios/web/public/test/test_web_client.h" | 22 #import "ios/web/public/test/test_web_client.h" |
| 22 #import "ios/web/test/web_test.h" | 23 #import "ios/web/test/web_test.h" |
| 23 #include "ios/web/web_state/web_state_impl.h" | 24 #include "ios/web/web_state/web_state_impl.h" |
| 24 #import "ios/web/webui/crw_web_ui_page_builder.h" | 25 #import "ios/web/webui/crw_web_ui_page_builder.h" |
| 25 #include "ios/web/webui/url_fetcher_block_adapter.h" | 26 #include "ios/web/webui/url_fetcher_block_adapter.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "testing/gtest_mac.h" | 29 #include "testing/gtest_mac.h" |
| 29 | 30 |
| 30 namespace web { | 31 namespace web { |
| 31 | 32 |
| 32 // Path for test favicon file. | 33 // Path for test favicon file. |
| 33 const char kFaviconPath[] = "ios/web/test/data/testfavicon.png"; | 34 const char kFaviconPath[] = "ios/web/test/data/testfavicon.png"; |
| 34 // URL for mock WebUI page. | 35 // URL for mock WebUI page. |
| 35 const char kTestChromeUrl[] = "chrome://test"; | 36 const char kTestWebUIUrl[] = "testwebui://test/"; |
| 36 // URL for mock favicon. | 37 // URL for mock favicon. |
| 37 const char kFaviconUrl[] = "chrome://favicon"; | 38 const char kFaviconUrl[] = "testwebui://favicon/"; |
| 39 // Name of test Mojo module. |
| 40 const char kMojoModuleName[] = "test-mojo-module"; |
| 38 // HTML for mock WebUI page. | 41 // HTML for mock WebUI page. |
| 39 NSString* kHtml = @"<html>Hello World</html>"; | 42 NSString* kHtml = @"<html>Hello World</html>"; |
| 43 // Mojo module for WebUI page. |
| 44 NSString* kMojoModule = @"service_provider.connect('Test');"; |
| 40 | 45 |
| 41 // Mock of WebStateImpl to check that LoadHtml and ExecuteJavaScriptAsync are | 46 // Mock of WebStateImpl to check that LoadHtml and ExecuteJavaScriptAsync are |
| 42 // called as expected. | 47 // called as expected. |
| 43 class MockWebStateImpl : public WebStateImpl { | 48 class MockWebStateImpl : public WebStateImpl { |
| 44 public: | 49 public: |
| 45 MockWebStateImpl(BrowserState* browser_state) : WebStateImpl(browser_state) {} | 50 MockWebStateImpl(BrowserState* browser_state) |
| 51 : WebStateImpl(browser_state), last_committed_url_(kTestWebUIUrl) {} |
| 46 MOCK_METHOD2(LoadWebUIHtml, | 52 MOCK_METHOD2(LoadWebUIHtml, |
| 47 void(const base::string16& html, const GURL& url)); | 53 void(const base::string16& html, const GURL& url)); |
| 48 MOCK_METHOD1(ExecuteJavaScriptAsync, void(const base::string16& javascript)); | 54 MOCK_METHOD1(ExecuteJavaScript, void(const base::string16& javascript)); |
| 55 const GURL& GetLastCommittedURL() const override { |
| 56 return last_committed_url_; |
| 57 } |
| 58 |
| 59 private: |
| 60 GURL last_committed_url_; |
| 49 }; | 61 }; |
| 50 | 62 |
| 51 // Mock of URLFetcherBlockAdapter to provide mock resources. | 63 // Mock of URLFetcherBlockAdapter to provide mock resources. |
| 52 class MockURLFetcherBlockAdapter : public URLFetcherBlockAdapter { | 64 class MockURLFetcherBlockAdapter : public URLFetcherBlockAdapter { |
| 53 public: | 65 public: |
| 54 MockURLFetcherBlockAdapter( | 66 MockURLFetcherBlockAdapter( |
| 55 const GURL& url, | 67 const GURL& url, |
| 56 net::URLRequestContextGetter* request_context, | 68 net::URLRequestContextGetter* request_context, |
| 57 URLFetcherBlockAdapterCompletion completion_handler) | 69 URLFetcherBlockAdapterCompletion completion_handler) |
| 58 : URLFetcherBlockAdapter(url, request_context, completion_handler), | 70 : URLFetcherBlockAdapter(url, request_context, completion_handler), |
| 59 url_(url), | 71 url_(url), |
| 60 completion_handler_(completion_handler) {} | 72 completion_handler_(completion_handler) {} |
| 61 | 73 |
| 62 void Start() override { | 74 void Start() override { |
| 63 if (url_.spec() == kTestChromeUrl) { | 75 if (url_.spec() == kFaviconUrl) { |
| 64 completion_handler_.get()([kHtml dataUsingEncoding:NSUTF8StringEncoding], | |
| 65 this); | |
| 66 } else if (url_.spec() == kFaviconUrl) { | |
| 67 base::FilePath favicon_path; | 76 base::FilePath favicon_path; |
| 68 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); | 77 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); |
| 69 favicon_path = favicon_path.AppendASCII(kFaviconPath); | 78 favicon_path = favicon_path.AppendASCII(kFaviconPath); |
| 79 NSData* favicon = [NSData |
| 80 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; |
| 81 completion_handler_.get()(favicon, this); |
| 82 } else if (url_.path().find(kMojoModuleName) != std::string::npos) { |
| 70 completion_handler_.get()( | 83 completion_handler_.get()( |
| 71 [NSData dataWithContentsOfFile:base::SysUTF8ToNSString( | 84 [kMojoModule dataUsingEncoding:NSUTF8StringEncoding], this); |
| 72 favicon_path.value())], | 85 |
| 73 this); | 86 } else if (url_.scheme().find("test") != std::string::npos) { |
| 87 completion_handler_.get()([kHtml dataUsingEncoding:NSUTF8StringEncoding], |
| 88 this); |
| 74 } else { | 89 } else { |
| 75 NOTREACHED(); | 90 NOTREACHED(); |
| 76 } | 91 } |
| 77 } | 92 } |
| 78 | 93 |
| 79 private: | 94 private: |
| 80 // The URL to fetch. | 95 // The URL to fetch. |
| 81 const GURL url_; | 96 const GURL url_; |
| 82 // Callback for resource load. | 97 // Callback for resource load. |
| 83 base::mac::ScopedBlock<URLFetcherBlockAdapterCompletion> completion_handler_; | 98 base::mac::ScopedBlock<URLFetcherBlockAdapterCompletion> completion_handler_; |
| 84 }; | 99 }; |
| 85 | 100 |
| 86 class AppSpecificTestWebClient : public TestWebClient { | |
| 87 public: | |
| 88 bool IsAppSpecificURL(const GURL& url) const override { | |
| 89 return url.SchemeIs("chrome"); | |
| 90 } | |
| 91 }; | |
| 92 | |
| 93 } // namespace web | 101 } // namespace web |
| 94 | 102 |
| 95 // Subclass of CRWWebUIManager for testing. | 103 // Subclass of CRWWebUIManager for testing. |
| 96 @interface CRWTestWebUIManager : CRWWebUIManager | 104 @interface CRWTestWebUIManager : CRWWebUIManager |
| 97 // Use mock URLFetcherBlockAdapter. | 105 // Use mock URLFetcherBlockAdapter. |
| 98 - (std::unique_ptr<web::URLFetcherBlockAdapter>) | 106 - (std::unique_ptr<web::URLFetcherBlockAdapter>) |
| 99 fetcherForURL:(const GURL&)URL | 107 fetcherForURL:(const GURL&)URL |
| 100 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler; | 108 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler; |
| 101 @end | 109 @end |
| 102 | 110 |
| 103 @implementation CRWTestWebUIManager | 111 @implementation CRWTestWebUIManager |
| 104 - (std::unique_ptr<web::URLFetcherBlockAdapter>) | 112 - (std::unique_ptr<web::URLFetcherBlockAdapter>) |
| 105 fetcherForURL:(const GURL&)URL | 113 fetcherForURL:(const GURL&)URL |
| 106 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler { | 114 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler { |
| 107 return std::unique_ptr<web::URLFetcherBlockAdapter>( | 115 return std::unique_ptr<web::URLFetcherBlockAdapter>( |
| 108 new web::MockURLFetcherBlockAdapter(URL, nil, handler)); | 116 new web::MockURLFetcherBlockAdapter(URL, nil, handler)); |
| 109 } | 117 } |
| 110 @end | 118 @end |
| 111 | 119 |
| 112 namespace web { | 120 namespace web { |
| 113 | 121 |
| 114 // Test fixture for testing CRWWebUIManager | 122 // Test fixture for testing CRWWebUIManager |
| 115 class CRWWebUIManagerTest : public web::WebTest { | 123 class CRWWebUIManagerTest : public web::WebTest { |
| 116 public: | |
| 117 CRWWebUIManagerTest() | |
| 118 : web_client_(base::WrapUnique(new web::AppSpecificTestWebClient)) {} | |
| 119 | |
| 120 protected: | 124 protected: |
| 121 void SetUp() override { | 125 void SetUp() override { |
| 122 PlatformTest::SetUp(); | 126 PlatformTest::SetUp(); |
| 123 test_browser_state_.reset(new TestBrowserState()); | 127 test_browser_state_.reset(new TestBrowserState()); |
| 124 web_state_impl_.reset(new MockWebStateImpl(test_browser_state_.get())); | 128 web_state_impl_.reset(new MockWebStateImpl(test_browser_state_.get())); |
| 125 web_ui_manager_.reset( | 129 web_ui_manager_.reset( |
| 126 [[CRWTestWebUIManager alloc] initWithWebState:web_state_impl_.get()]); | 130 [[CRWTestWebUIManager alloc] initWithWebState:web_state_impl_.get()]); |
| 127 } | 131 } |
| 128 | 132 |
| 129 // TestBrowserState for creation of WebStateImpl. | 133 // TestBrowserState for creation of WebStateImpl. |
| 130 std::unique_ptr<TestBrowserState> test_browser_state_; | 134 std::unique_ptr<TestBrowserState> test_browser_state_; |
| 131 // MockWebStateImpl for detection of LoadHtml and EvaluateJavaScriptAync | 135 // MockWebStateImpl for detection of LoadHtml and EvaluateJavaScriptAync |
| 132 // calls. | 136 // calls. |
| 133 std::unique_ptr<MockWebStateImpl> web_state_impl_; | 137 std::unique_ptr<MockWebStateImpl> web_state_impl_; |
| 134 // WebUIManager for testing. | 138 // WebUIManager for testing. |
| 135 base::scoped_nsobject<CRWTestWebUIManager> web_ui_manager_; | 139 base::scoped_nsobject<CRWTestWebUIManager> web_ui_manager_; |
| 136 // The WebClient used in tests. | |
| 137 web::ScopedTestingWebClient web_client_; | |
| 138 }; | 140 }; |
| 139 | 141 |
| 140 // Tests that CRWWebUIManager observes provisional navigation and invokes an | 142 // Tests that CRWWebUIManager observes provisional navigation and invokes an |
| 141 // HTML load in web state. | 143 // HTML load in web state. |
| 142 TEST_F(CRWWebUIManagerTest, LoadWebUI) { | 144 TEST_F(CRWWebUIManagerTest, LoadWebUI) { |
| 143 base::string16 html(base::SysNSStringToUTF16(kHtml)); | 145 base::string16 html(base::SysNSStringToUTF16(kHtml)); |
| 144 GURL url(kTestChromeUrl); | 146 GURL url(kTestWebUIUrl); |
| 145 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); | 147 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); |
| 146 web_state_impl_->OnProvisionalNavigationStarted(url); | 148 web_state_impl_->OnProvisionalNavigationStarted(url); |
| 147 } | 149 } |
| 148 | 150 |
| 149 // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs | 151 // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs |
| 150 // JavaScript to set favicon background. | 152 // JavaScript to set favicon background. |
| 151 TEST_F(CRWWebUIManagerTest, HandleFaviconRequest) { | 153 TEST_F(CRWWebUIManagerTest, HandleFaviconRequest) { |
| 152 GURL test_url(kTestChromeUrl); | 154 GURL test_url(kTestWebUIUrl); |
| 153 std::string favicon_url_string(kFaviconUrl); | 155 std::string favicon_url_string(kFaviconUrl); |
| 154 | 156 |
| 155 // Create mock JavaScript message to request favicon. | 157 // Create mock JavaScript message to request favicon. |
| 156 base::ListValue* arguments(new base::ListValue()); | 158 base::ListValue* arguments(new base::ListValue()); |
| 157 arguments->AppendString(favicon_url_string); | 159 arguments->AppendString(favicon_url_string); |
| 158 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 160 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
| 159 message->SetString("message", "webui.requestFavicon"); | 161 message->SetString("message", "webui.requestFavicon"); |
| 160 message->Set("arguments", arguments); | 162 message->Set("arguments", arguments); |
| 161 | 163 |
| 162 // Create expected JavaScript to call. | 164 // Create expected JavaScript to call. |
| 163 base::FilePath favicon_path; | 165 base::FilePath favicon_path; |
| 164 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); | 166 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); |
| 165 favicon_path = favicon_path.AppendASCII(kFaviconPath); | 167 favicon_path = favicon_path.AppendASCII(kFaviconPath); |
| 166 NSData* expected_data = [NSData | 168 NSData* expected_data = [NSData |
| 167 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; | 169 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; |
| 168 base::string16 expected_javascript = base::SysNSStringToUTF16([NSString | 170 base::string16 expected_javascript = base::SysNSStringToUTF16([NSString |
| 169 stringWithFormat: | 171 stringWithFormat: |
| 170 @"chrome.setFaviconBackground('%s', 'data:image/png;base64,%@');", | 172 @"chrome.setFaviconBackground('%s', 'data:image/png;base64,%@');", |
| 171 favicon_url_string.c_str(), | 173 favicon_url_string.c_str(), |
| 172 [expected_data base64EncodedStringWithOptions:0]]); | 174 [expected_data base64EncodedStringWithOptions:0]]); |
| 173 | 175 |
| 174 EXPECT_CALL(*web_state_impl_, ExecuteJavaScriptAsync(expected_javascript)); | 176 EXPECT_CALL(*web_state_impl_, ExecuteJavaScript(expected_javascript)); |
| 175 web_state_impl_->OnScriptCommandReceived("webui.requestFavicon", *message, | 177 web_state_impl_->OnScriptCommandReceived("webui.requestFavicon", *message, |
| 176 test_url, false); | 178 test_url, false); |
| 177 } | 179 } |
| 180 |
| 181 // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs |
| 182 // JavaScript to fetch a mojo resource. |
| 183 TEST_F(CRWWebUIManagerTest, HandleLoadMojoRequest) { |
| 184 // Create mock JavaScript message to request a mojo resource. |
| 185 std::unique_ptr<base::ListValue> arguments(new base::ListValue()); |
| 186 arguments->AppendString(kMojoModuleName); |
| 187 const char kTestLoadId[] = "test-load-id"; |
| 188 arguments->AppendString(kTestLoadId); |
| 189 base::DictionaryValue message; |
| 190 message.SetString("message", "webui.loadMojo"); |
| 191 message.Set("arguments", std::move(arguments)); |
| 192 |
| 193 // Create expected JavaScript to call. |
| 194 std::string expected_javascript = base::StringPrintf( |
| 195 "%s__crWeb.webUIModuleLoadNotifier.moduleLoadCompleted(\"%s\", \"%s\");", |
| 196 base::SysNSStringToUTF8(kMojoModule).c_str(), kMojoModuleName, |
| 197 kTestLoadId); |
| 198 |
| 199 EXPECT_CALL(*web_state_impl_, |
| 200 ExecuteJavaScript(base::UTF8ToUTF16(expected_javascript))); |
| 201 web_state_impl_->OnScriptCommandReceived("webui.loadMojo", message, |
| 202 GURL(kTestWebUIUrl), false); |
| 203 } |
| 178 } // namespace web | 204 } // namespace web |
| OLD | NEW |