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> |
| 8 |
7 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #import "base/mac/scoped_nsobject.h" | 12 #import "base/mac/scoped_nsobject.h" |
11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/ptr_util.h" |
12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
15 #import "base/strings/sys_string_conversions.h" | 17 #import "base/strings/sys_string_conversions.h" |
16 #include "base/values.h" | 18 #include "base/values.h" |
17 #include "ios/web/public/test/scoped_testing_web_client.h" | 19 #include "ios/web/public/test/scoped_testing_web_client.h" |
18 #include "ios/web/public/test/test_browser_state.h" | 20 #include "ios/web/public/test/test_browser_state.h" |
19 #import "ios/web/public/test/test_web_client.h" | 21 #import "ios/web/public/test/test_web_client.h" |
20 #include "ios/web/web_state/web_state_impl.h" | 22 #include "ios/web/web_state/web_state_impl.h" |
21 #import "ios/web/webui/crw_web_ui_page_builder.h" | 23 #import "ios/web/webui/crw_web_ui_page_builder.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 bool IsAppSpecificURL(const GURL& url) const override { | 88 bool IsAppSpecificURL(const GURL& url) const override { |
87 return url.SchemeIs("chrome"); | 89 return url.SchemeIs("chrome"); |
88 } | 90 } |
89 }; | 91 }; |
90 | 92 |
91 } // namespace web | 93 } // namespace web |
92 | 94 |
93 // Subclass of CRWWebUIManager for testing. | 95 // Subclass of CRWWebUIManager for testing. |
94 @interface CRWTestWebUIManager : CRWWebUIManager | 96 @interface CRWTestWebUIManager : CRWWebUIManager |
95 // Use mock URLFetcherBlockAdapter. | 97 // Use mock URLFetcherBlockAdapter. |
96 - (scoped_ptr<web::URLFetcherBlockAdapter>) | 98 - (std::unique_ptr<web::URLFetcherBlockAdapter>) |
97 fetcherForURL:(const GURL&)URL | 99 fetcherForURL:(const GURL&)URL |
98 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler; | 100 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler; |
99 @end | 101 @end |
100 | 102 |
101 @implementation CRWTestWebUIManager | 103 @implementation CRWTestWebUIManager |
102 - (scoped_ptr<web::URLFetcherBlockAdapter>) | 104 - (std::unique_ptr<web::URLFetcherBlockAdapter>) |
103 fetcherForURL:(const GURL&)URL | 105 fetcherForURL:(const GURL&)URL |
104 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler { | 106 completionHandler:(web::URLFetcherBlockAdapterCompletion)handler { |
105 return scoped_ptr<web::URLFetcherBlockAdapter>( | 107 return std::unique_ptr<web::URLFetcherBlockAdapter>( |
106 new web::MockURLFetcherBlockAdapter(URL, nil, handler)); | 108 new web::MockURLFetcherBlockAdapter(URL, nil, handler)); |
107 } | 109 } |
108 @end | 110 @end |
109 | 111 |
110 namespace web { | 112 namespace web { |
111 | 113 |
112 // Test fixture for testing CRWWebUIManager | 114 // Test fixture for testing CRWWebUIManager |
113 class CRWWebUIManagerTest : public PlatformTest { | 115 class CRWWebUIManagerTest : public PlatformTest { |
114 public: | 116 public: |
115 CRWWebUIManagerTest() | 117 CRWWebUIManagerTest() |
116 : web_client_(make_scoped_ptr(new web::AppSpecificTestWebClient)) {} | 118 : web_client_(base::WrapUnique(new web::AppSpecificTestWebClient)) {} |
117 | 119 |
118 protected: | 120 protected: |
119 void SetUp() override { | 121 void SetUp() override { |
120 PlatformTest::SetUp(); | 122 PlatformTest::SetUp(); |
121 test_browser_state_.reset(new TestBrowserState()); | 123 test_browser_state_.reset(new TestBrowserState()); |
122 web_state_impl_.reset(new MockWebStateImpl(test_browser_state_.get())); | 124 web_state_impl_.reset(new MockWebStateImpl(test_browser_state_.get())); |
123 web_ui_manager_.reset( | 125 web_ui_manager_.reset( |
124 [[CRWTestWebUIManager alloc] initWithWebState:web_state_impl_.get()]); | 126 [[CRWTestWebUIManager alloc] initWithWebState:web_state_impl_.get()]); |
125 } | 127 } |
126 | 128 |
127 // TestBrowserState for creation of WebStateImpl. | 129 // TestBrowserState for creation of WebStateImpl. |
128 scoped_ptr<TestBrowserState> test_browser_state_; | 130 std::unique_ptr<TestBrowserState> test_browser_state_; |
129 // MockWebStateImpl for detection of LoadHtml and EvaluateJavaScriptAync | 131 // MockWebStateImpl for detection of LoadHtml and EvaluateJavaScriptAync |
130 // calls. | 132 // calls. |
131 scoped_ptr<MockWebStateImpl> web_state_impl_; | 133 std::unique_ptr<MockWebStateImpl> web_state_impl_; |
132 // WebUIManager for testing. | 134 // WebUIManager for testing. |
133 base::scoped_nsobject<CRWTestWebUIManager> web_ui_manager_; | 135 base::scoped_nsobject<CRWTestWebUIManager> web_ui_manager_; |
134 // The WebClient used in tests. | 136 // The WebClient used in tests. |
135 web::ScopedTestingWebClient web_client_; | 137 web::ScopedTestingWebClient web_client_; |
136 }; | 138 }; |
137 | 139 |
138 // Tests that CRWWebUIManager observes provisional navigation and invokes an | 140 // Tests that CRWWebUIManager observes provisional navigation and invokes an |
139 // HTML load in web state. | 141 // HTML load in web state. |
140 TEST_F(CRWWebUIManagerTest, LoadWebUI) { | 142 TEST_F(CRWWebUIManagerTest, LoadWebUI) { |
141 base::string16 html(base::SysNSStringToUTF16(kHtml)); | 143 base::string16 html(base::SysNSStringToUTF16(kHtml)); |
142 GURL url(kTestChromeUrl); | 144 GURL url(kTestChromeUrl); |
143 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); | 145 EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url)); |
144 web_state_impl_->OnProvisionalNavigationStarted(url); | 146 web_state_impl_->OnProvisionalNavigationStarted(url); |
145 } | 147 } |
146 | 148 |
147 // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs | 149 // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs |
148 // JavaScript to set favicon background. | 150 // JavaScript to set favicon background. |
149 TEST_F(CRWWebUIManagerTest, HandleFaviconRequest) { | 151 TEST_F(CRWWebUIManagerTest, HandleFaviconRequest) { |
150 GURL test_url(kTestChromeUrl); | 152 GURL test_url(kTestChromeUrl); |
151 std::string favicon_url_string(kFaviconUrl); | 153 std::string favicon_url_string(kFaviconUrl); |
152 | 154 |
153 // Create mock JavaScript message to request favicon. | 155 // Create mock JavaScript message to request favicon. |
154 base::ListValue* arguments(new base::ListValue()); | 156 base::ListValue* arguments(new base::ListValue()); |
155 arguments->AppendString(favicon_url_string); | 157 arguments->AppendString(favicon_url_string); |
156 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 158 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
157 message->SetString("message", "webui.requestFavicon"); | 159 message->SetString("message", "webui.requestFavicon"); |
158 message->Set("arguments", arguments); | 160 message->Set("arguments", arguments); |
159 | 161 |
160 // Create expected JavaScript to call. | 162 // Create expected JavaScript to call. |
161 base::FilePath favicon_path; | 163 base::FilePath favicon_path; |
162 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); | 164 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path)); |
163 favicon_path = favicon_path.AppendASCII(kFaviconPath); | 165 favicon_path = favicon_path.AppendASCII(kFaviconPath); |
164 NSData* expected_data = [NSData | 166 NSData* expected_data = [NSData |
165 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; | 167 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; |
166 base::string16 expected_javascript = base::SysNSStringToUTF16([NSString | 168 base::string16 expected_javascript = base::SysNSStringToUTF16([NSString |
167 stringWithFormat: | 169 stringWithFormat: |
168 @"chrome.setFaviconBackground('%s', 'data:image/png;base64,%@');", | 170 @"chrome.setFaviconBackground('%s', 'data:image/png;base64,%@');", |
169 favicon_url_string.c_str(), | 171 favicon_url_string.c_str(), |
170 [expected_data base64EncodedStringWithOptions:0]]); | 172 [expected_data base64EncodedStringWithOptions:0]]); |
171 | 173 |
172 EXPECT_CALL(*web_state_impl_, ExecuteJavaScriptAsync(expected_javascript)); | 174 EXPECT_CALL(*web_state_impl_, ExecuteJavaScriptAsync(expected_javascript)); |
173 web_state_impl_->OnScriptCommandReceived("webui.requestFavicon", *message, | 175 web_state_impl_->OnScriptCommandReceived("webui.requestFavicon", *message, |
174 test_url, false); | 176 test_url, false); |
175 } | 177 } |
176 } // namespace web | 178 } // namespace web |
OLD | NEW |