OLD | NEW |
(Empty) | |
| 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 |
| 5 #import "ios/web/webui/crw_web_ui_manager.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/logging.h" |
| 10 #import "base/mac/scoped_nsobject.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/strings/stringprintf.h" |
| 15 #import "base/strings/sys_string_conversions.h" |
| 16 #include "base/values.h" |
| 17 #include "ios/web/public/test/test_browser_state.h" |
| 18 #include "ios/web/web_state/web_state_impl.h" |
| 19 #import "ios/web/webui/crw_web_ui_page_builder.h" |
| 20 #include "ios/web/webui/url_fetcher_block_adapter.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "testing/gtest_mac.h" |
| 24 #include "testing/platform_test.h" |
| 25 |
| 26 namespace web { |
| 27 |
| 28 // Path for test favicon file. |
| 29 const char kFaviconPath[] = "ios/web/test/data/testfavicon.png"; |
| 30 // URL for mock WebUI page. |
| 31 const char kTestChromeUrl[] = "chrome://test"; |
| 32 // URL for mock favicon. |
| 33 const char kFaviconUrl[] = "chrome://favicon"; |
| 34 // HTML for mock WebUI page. |
| 35 NSString* kHtml = @"<html>Hello World</html>"; |
| 36 |
| 37 // Mock of WebStateImpl to check that LoadHtml and ExecuteJavaScriptAsync are |
| 38 // called as expected. |
| 39 class MockWebStateImpl : public WebStateImpl { |
| 40 public: |
| 41 MockWebStateImpl(BrowserState* browser_state) : WebStateImpl(browser_state) {} |
| 42 MOCK_METHOD2(LoadHtml, void(const base::string16& html, const GURL& url)); |
| 43 MOCK_METHOD1(ExecuteJavaScriptAsync, void(const base::string16& javascript)); |
| 44 }; |
| 45 |
| 46 // Mock of URLFetcherBlockAdapter to provide mock resources. |
| 47 class MockURLFetcherBlockAdapter : public URLFetcherBlockAdapter { |
| 48 public: |
| 49 MockURLFetcherBlockAdapter( |
| 50 const GURL& url, |
| 51 net::URLRequestContextGetter* request_context, |
| 52 URLFetcherBlockAdapterCompletion completion_handler) |
| 53 : URLFetcherBlockAdapter(url, request_context, completion_handler), |
| 54 url_(url), |
| 55 completion_handler_(completion_handler) {} |
| 56 |
| 57 void Start() override { |
| 58 if (url_.spec() == kTestChromeUrl) { |
| 59 completion_handler_.get()([kHtml dataUsingEncoding:NSUTF8StringEncoding], |
| 60 this); |
| 61 } else if (url_.spec() == kFaviconUrl) { |
| 62 base::FilePath favicon_path; |
| 63 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); |
| 64 favicon_path = favicon_path.AppendASCII(kFaviconPath); |
| 65 completion_handler_.get()( |
| 66 [NSData dataWithContentsOfFile:base::SysUTF8ToNSString( |
| 67 favicon_path.value())], |
| 68 this); |
| 69 } else { |
| 70 NOTREACHED(); |
| 71 } |
| 72 } |
| 73 |
| 74 private: |
| 75 // The URL to fetch. |
| 76 const GURL url_; |
| 77 // Callback for resource load. |
| 78 base::mac::ScopedBlock<URLFetcherBlockAdapterCompletion> completion_handler_; |
| 79 }; |
| 80 |
| 81 } // namespace web |
| 82 |
| 83 // Subclass of CRWWebUIManager for testing. |
| 84 @interface CRWTestWebUIManager : CRWWebUIManager |
| 85 // Use mock URLFetcherBlockAdapter. |
| 86 - (scoped_ptr<web::URLFetcherBlockAdapter>) |
| 87 fetcherForURL:(const GURL&)URL |
| 88 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler; |
| 89 @end |
| 90 |
| 91 @implementation CRWTestWebUIManager |
| 92 - (scoped_ptr<web::URLFetcherBlockAdapter>) |
| 93 fetcherForURL:(const GURL&)URL |
| 94 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler { |
| 95 return scoped_ptr<web::URLFetcherBlockAdapter>( |
| 96 new web::MockURLFetcherBlockAdapter(URL, nil, handler)); |
| 97 } |
| 98 @end |
| 99 |
| 100 namespace web { |
| 101 |
| 102 // Test fixture for testing CRWWebUIManager |
| 103 class CRWWebUIManagerTest : public PlatformTest { |
| 104 protected: |
| 105 void SetUp() override { |
| 106 test_browser_state_.reset(new TestBrowserState()); |
| 107 web_state_impl_.reset(new MockWebStateImpl(test_browser_state_.get())); |
| 108 web_ui_manager_.reset( |
| 109 [[CRWTestWebUIManager alloc] initWithWebState:web_state_impl_.get()]); |
| 110 } |
| 111 |
| 112 // TestBrowserState for creation of WebStateImpl. |
| 113 scoped_ptr<TestBrowserState> test_browser_state_; |
| 114 // MockWebStateImpl for detection of LoadHtml and EvaluateJavaScriptAync |
| 115 // calls. |
| 116 scoped_ptr<MockWebStateImpl> web_state_impl_; |
| 117 // WebUIManager for testing. |
| 118 base::scoped_nsobject<CRWTestWebUIManager> web_ui_manager_; |
| 119 }; |
| 120 |
| 121 // Tests that CRWWebUIManager observes provisional navigation and invokes an |
| 122 // HTML load in web state. |
| 123 TEST_F(CRWWebUIManagerTest, LoadWebUI) { |
| 124 base::string16 html(base::SysNSStringToUTF16(kHtml)); |
| 125 GURL url(kTestChromeUrl); |
| 126 EXPECT_CALL(*web_state_impl_, LoadHtml(html, url)); |
| 127 web_state_impl_->OnProvisionalNavigationStarted(url); |
| 128 } |
| 129 |
| 130 // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs |
| 131 // JavaScript to set favicon background. |
| 132 TEST_F(CRWWebUIManagerTest, HandleFaviconRequest) { |
| 133 GURL test_url(kTestChromeUrl); |
| 134 std::string favicon_url_string(kFaviconUrl); |
| 135 |
| 136 // Create mock JavaScript message to request favicon. |
| 137 base::ListValue* arguments(new base::ListValue()); |
| 138 arguments->AppendString(favicon_url_string); |
| 139 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
| 140 message->SetString("message", "history.requestFavicon"); |
| 141 message->Set("arguments", arguments); |
| 142 |
| 143 // Create expected JavaScript to call. |
| 144 base::FilePath favicon_path; |
| 145 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); |
| 146 favicon_path = favicon_path.AppendASCII(kFaviconPath); |
| 147 NSData* expected_data = [NSData |
| 148 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; |
| 149 base::string16 expected_javascript = base::SysNSStringToUTF16([NSString |
| 150 stringWithFormat: |
| 151 @"chrome.setFaviconBackground('%s', 'data:image/png;base64,%@');", |
| 152 favicon_url_string.c_str(), |
| 153 [expected_data base64EncodedStringWithOptions:0]]); |
| 154 |
| 155 EXPECT_CALL(*web_state_impl_, ExecuteJavaScriptAsync(expected_javascript)); |
| 156 web_state_impl_->OnScriptCommandReceived("history.requestFavicon", *message, |
| 157 test_url, false); |
| 158 } |
| 159 } // namespace web |
OLD | NEW |