| Index: ios/web/webui/crw_web_ui_manager_unittest.mm
|
| diff --git a/ios/web/webui/crw_web_ui_manager_unittest.mm b/ios/web/webui/crw_web_ui_manager_unittest.mm
|
| index 41a3cff0099ddffcc8fb52a0810d207a2c8f0dbb..d9efbec99bc2c82daaee130590d01830164fcadb 100644
|
| --- a/ios/web/webui/crw_web_ui_manager_unittest.mm
|
| +++ b/ios/web/webui/crw_web_ui_manager_unittest.mm
|
| @@ -15,6 +15,7 @@
|
| #include "base/path_service.h"
|
| #include "base/strings/stringprintf.h"
|
| #import "base/strings/sys_string_conversions.h"
|
| +#include "base/strings/utf_string_conversions.h"
|
| #include "base/values.h"
|
| #include "ios/web/public/test/scoped_testing_web_client.h"
|
| #include "ios/web/public/test/test_browser_state.h"
|
| @@ -32,20 +33,31 @@ namespace web {
|
| // Path for test favicon file.
|
| const char kFaviconPath[] = "ios/web/test/data/testfavicon.png";
|
| // URL for mock WebUI page.
|
| -const char kTestChromeUrl[] = "chrome://test";
|
| +const char kTestWebUIUrl[] = "testwebui://test/";
|
| // URL for mock favicon.
|
| -const char kFaviconUrl[] = "chrome://favicon";
|
| +const char kFaviconUrl[] = "testwebui://favicon/";
|
| +// Name of test Mojo module.
|
| +const char kMojoModuleName[] = "test-mojo-module";
|
| // HTML for mock WebUI page.
|
| NSString* kHtml = @"<html>Hello World</html>";
|
| +// Mojo module for WebUI page.
|
| +NSString* kMojoModule = @"service_provider.connect('Test');";
|
|
|
| // Mock of WebStateImpl to check that LoadHtml and ExecuteJavaScriptAsync are
|
| // called as expected.
|
| class MockWebStateImpl : public WebStateImpl {
|
| public:
|
| - MockWebStateImpl(BrowserState* browser_state) : WebStateImpl(browser_state) {}
|
| + MockWebStateImpl(BrowserState* browser_state)
|
| + : WebStateImpl(browser_state), last_committed_url_(kTestWebUIUrl) {}
|
| MOCK_METHOD2(LoadWebUIHtml,
|
| void(const base::string16& html, const GURL& url));
|
| - MOCK_METHOD1(ExecuteJavaScriptAsync, void(const base::string16& javascript));
|
| + MOCK_METHOD1(ExecuteJavaScript, void(const base::string16& javascript));
|
| + const GURL& GetLastCommittedURL() const override {
|
| + return last_committed_url_;
|
| + }
|
| +
|
| + private:
|
| + GURL last_committed_url_;
|
| };
|
|
|
| // Mock of URLFetcherBlockAdapter to provide mock resources.
|
| @@ -60,17 +72,20 @@ class MockURLFetcherBlockAdapter : public URLFetcherBlockAdapter {
|
| completion_handler_(completion_handler) {}
|
|
|
| void Start() override {
|
| - if (url_.spec() == kTestChromeUrl) {
|
| - completion_handler_.get()([kHtml dataUsingEncoding:NSUTF8StringEncoding],
|
| - this);
|
| - } else if (url_.spec() == kFaviconUrl) {
|
| + if (url_.spec() == kFaviconUrl) {
|
| base::FilePath favicon_path;
|
| ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &favicon_path));
|
| favicon_path = favicon_path.AppendASCII(kFaviconPath);
|
| + NSData* favicon = [NSData
|
| + dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())];
|
| + completion_handler_.get()(favicon, this);
|
| + } else if (url_.path().find(kMojoModuleName) != std::string::npos) {
|
| completion_handler_.get()(
|
| - [NSData dataWithContentsOfFile:base::SysUTF8ToNSString(
|
| - favicon_path.value())],
|
| - this);
|
| + [kMojoModule dataUsingEncoding:NSUTF8StringEncoding], this);
|
| +
|
| + } else if (url_.scheme().find("test") != std::string::npos) {
|
| + completion_handler_.get()([kHtml dataUsingEncoding:NSUTF8StringEncoding],
|
| + this);
|
| } else {
|
| NOTREACHED();
|
| }
|
| @@ -83,13 +98,6 @@ class MockURLFetcherBlockAdapter : public URLFetcherBlockAdapter {
|
| base::mac::ScopedBlock<URLFetcherBlockAdapterCompletion> completion_handler_;
|
| };
|
|
|
| -class AppSpecificTestWebClient : public TestWebClient {
|
| -public:
|
| - bool IsAppSpecificURL(const GURL& url) const override {
|
| - return url.SchemeIs("chrome");
|
| - }
|
| -};
|
| -
|
| } // namespace web
|
|
|
| // Subclass of CRWWebUIManager for testing.
|
| @@ -113,10 +121,6 @@ namespace web {
|
|
|
| // Test fixture for testing CRWWebUIManager
|
| class CRWWebUIManagerTest : public web::WebTest {
|
| - public:
|
| - CRWWebUIManagerTest()
|
| - : web_client_(base::WrapUnique(new web::AppSpecificTestWebClient)) {}
|
| -
|
| protected:
|
| void SetUp() override {
|
| PlatformTest::SetUp();
|
| @@ -133,15 +137,13 @@ class CRWWebUIManagerTest : public web::WebTest {
|
| std::unique_ptr<MockWebStateImpl> web_state_impl_;
|
| // WebUIManager for testing.
|
| base::scoped_nsobject<CRWTestWebUIManager> web_ui_manager_;
|
| - // The WebClient used in tests.
|
| - web::ScopedTestingWebClient web_client_;
|
| };
|
|
|
| // Tests that CRWWebUIManager observes provisional navigation and invokes an
|
| // HTML load in web state.
|
| TEST_F(CRWWebUIManagerTest, LoadWebUI) {
|
| base::string16 html(base::SysNSStringToUTF16(kHtml));
|
| - GURL url(kTestChromeUrl);
|
| + GURL url(kTestWebUIUrl);
|
| EXPECT_CALL(*web_state_impl_, LoadWebUIHtml(html, url));
|
| web_state_impl_->OnProvisionalNavigationStarted(url);
|
| }
|
| @@ -149,7 +151,7 @@ TEST_F(CRWWebUIManagerTest, LoadWebUI) {
|
| // Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs
|
| // JavaScript to set favicon background.
|
| TEST_F(CRWWebUIManagerTest, HandleFaviconRequest) {
|
| - GURL test_url(kTestChromeUrl);
|
| + GURL test_url(kTestWebUIUrl);
|
| std::string favicon_url_string(kFaviconUrl);
|
|
|
| // Create mock JavaScript message to request favicon.
|
| @@ -171,8 +173,32 @@ TEST_F(CRWWebUIManagerTest, HandleFaviconRequest) {
|
| favicon_url_string.c_str(),
|
| [expected_data base64EncodedStringWithOptions:0]]);
|
|
|
| - EXPECT_CALL(*web_state_impl_, ExecuteJavaScriptAsync(expected_javascript));
|
| + EXPECT_CALL(*web_state_impl_, ExecuteJavaScript(expected_javascript));
|
| web_state_impl_->OnScriptCommandReceived("webui.requestFavicon", *message,
|
| test_url, false);
|
| }
|
| +
|
| +// Tests that CRWWebUIManager responds to OnScriptCommandReceieved call and runs
|
| +// JavaScript to fetch a mojo resource.
|
| +TEST_F(CRWWebUIManagerTest, HandleLoadMojoRequest) {
|
| + // Create mock JavaScript message to request a mojo resource.
|
| + std::unique_ptr<base::ListValue> arguments(new base::ListValue());
|
| + arguments->AppendString(kMojoModuleName);
|
| + const char kTestLoadId[] = "test-load-id";
|
| + arguments->AppendString(kTestLoadId);
|
| + base::DictionaryValue message;
|
| + message.SetString("message", "webui.loadMojo");
|
| + message.Set("arguments", std::move(arguments));
|
| +
|
| + // Create expected JavaScript to call.
|
| + std::string expected_javascript = base::StringPrintf(
|
| + "%s__crWeb.webUIModuleLoadNotifier.moduleLoadCompleted(\"%s\", \"%s\");",
|
| + base::SysNSStringToUTF8(kMojoModule).c_str(), kMojoModuleName,
|
| + kTestLoadId);
|
| +
|
| + EXPECT_CALL(*web_state_impl_,
|
| + ExecuteJavaScript(base::UTF8ToUTF16(expected_javascript)));
|
| + web_state_impl_->OnScriptCommandReceived("webui.loadMojo", message,
|
| + GURL(kTestWebUIUrl), false);
|
| +}
|
| } // namespace web
|
|
|