Chromium Code Reviews| Index: ios/web/web_state/navigation_callbacks_inttest.mm |
| diff --git a/ios/web/web_state/navigation_callbacks_inttest.mm b/ios/web/web_state/navigation_callbacks_inttest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9daa82aca2f1df3e5c23187c2254e7d0a45993bd |
| --- /dev/null |
| +++ b/ios/web/web_state/navigation_callbacks_inttest.mm |
| @@ -0,0 +1,159 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <memory> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#import "ios/web/public/test/http_server.h" |
| +#include "ios/web/public/test/http_server_util.h" |
| +#include "ios/web/public/web_state/navigation_handle.h" |
| +#include "ios/web/public/web_state/web_state_observer.h" |
| +#include "ios/web/test/test_url_constants.h" |
| +#import "ios/web/test/web_int_test.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| +#include "url/scheme_host_port.h" |
| + |
| +namespace web { |
| + |
| +namespace { |
| + |
| +// Verifies correctness of |NavigationHandle| for new page navigation passed to |
| +// |DidFinishNavigation|. |
| +ACTION_P2(VerifyNewPageHandle, web_state, url) { |
|
kkhorimoto
2017/02/22 01:42:38
s/Page/Document
Eugene But (OOO till 7-30)
2017/02/22 17:54:33
Acknowledged.
|
| + NavigationHandle* handle = arg0; |
| + ASSERT_TRUE(handle); |
| + EXPECT_EQ(web_state, handle->GetWebState()); |
| + EXPECT_EQ(url, handle->GetUrl()); |
| + EXPECT_FALSE(handle->IsSamePage()); |
| + EXPECT_FALSE(handle->IsErrorPage()); |
| +} |
| + |
| +// Verifies correctness of |NavigationHandle| for same page navigation passed to |
| +// |DidFinishNavigation|. |
| +ACTION_P2(VerifySamePageHandle, web_state, url) { |
|
kkhorimoto
2017/02/22 01:42:38
s/Page/Document
Eugene But (OOO till 7-30)
2017/02/22 17:54:33
Acknowledged.
|
| + NavigationHandle* handle = arg0; |
| + ASSERT_TRUE(handle); |
| + EXPECT_EQ(web_state, handle->GetWebState()); |
| + EXPECT_EQ(url, handle->GetUrl()); |
| + EXPECT_TRUE(handle->IsSamePage()); |
| + EXPECT_FALSE(handle->IsErrorPage()); |
| +} |
| + |
| +// Mocks DidFinishNavigation navigation callback. |
| +class WebStateObserverMock : public WebStateObserver { |
| + public: |
| + WebStateObserverMock(WebState* web_state) : WebStateObserver(web_state) {} |
| + MOCK_METHOD1(DidFinishNavigation, void(NavigationHandle* handle)); |
| +}; |
| + |
| +} // namespace |
| + |
| +using test::HttpServer; |
| +using testing::_; |
| + |
| +// Test fixture for WebStateDelegate::DidFinishNavigation integration tests. |
| +class DidFinishNavigationTest : public WebIntTest { |
| + void SetUp() override { |
| + WebIntTest::SetUp(); |
| + observer_ = base::MakeUnique<WebStateObserverMock>(web_state()); |
| + } |
| + |
| + protected: |
| + std::unique_ptr<WebStateObserverMock> observer_; |
| +}; |
| + |
| +// Tests successful navigation to a new page. |
| +TEST_F(DidFinishNavigationTest, NewPageNavigation) { |
| + const GURL url = HttpServer::MakeUrl("http://chromium.test"); |
| + std::map<GURL, std::string> responses; |
| + responses[url] = "Chromium Test"; |
| + web::test::SetUpSimpleHttpServer(responses); |
| + |
| + // Perform new page navigation. |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifyNewPageHandle(web_state(), url)); |
| + LoadUrl(url); |
| +} |
| + |
| +// Tests user-initiated hash change. |
| +TEST_F(DidFinishNavigationTest, UserInitiatedHashChangeNavigation) { |
| + const GURL url = HttpServer::MakeUrl("http://chromium.test"); |
| + std::map<GURL, std::string> responses; |
| + responses[url] = "Chromium Test"; |
| + web::test::SetUpSimpleHttpServer(responses); |
| + |
| + // Perform new page navigation. |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifyNewPageHandle(web_state(), url)); |
| + LoadUrl(url); |
| + |
| + // Perform same-page navigation. |
| + const GURL hash_url = HttpServer::MakeUrl("http://chromium.test#1"); |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifySamePageHandle(web_state(), hash_url)); |
| + LoadUrl(hash_url); |
| + |
| + // Perform same-page navigation by going back. |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifySamePageHandle(web_state(), url)); |
| + ExecuteBlockAndWaitForLoad(url, ^{ |
| + navigation_manager()->GoBack(); |
| + }); |
| +} |
| + |
| +// Tests renderer-initiated hash change. |
| +TEST_F(DidFinishNavigationTest, RendererInitiatedHashChangeNavigation) { |
| + const GURL url = HttpServer::MakeUrl("http://chromium.test"); |
| + std::map<GURL, std::string> responses; |
| + responses[url] = "Chromium Test"; |
| + web::test::SetUpSimpleHttpServer(responses); |
| + |
| + // Perform new page navigation. |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifyNewPageHandle(web_state(), url)); |
| + LoadUrl(url); |
| + |
| + // Perform same-page navigation using JavaScript. |
| + const GURL hash_url = HttpServer::MakeUrl("http://chromium.test#1"); |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifySamePageHandle(web_state(), hash_url)); |
| + ExecuteJavaScript(@"window.location.hash = '#1'"); |
| +} |
| + |
| +// Tests state change. |
| +TEST_F(DidFinishNavigationTest, StateNavigation) { |
| + const GURL url = HttpServer::MakeUrl("http://chromium.test"); |
| + std::map<GURL, std::string> responses; |
| + responses[url] = "Chromium Test"; |
| + web::test::SetUpSimpleHttpServer(responses); |
| + |
| + // Perform new page navigation. |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifyNewPageHandle(web_state(), url)); |
| + LoadUrl(url); |
| + |
| + // Perform push state using JavaScript. |
| + const GURL push_url = HttpServer::MakeUrl("http://chromium.test/test.html"); |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifySamePageHandle(web_state(), push_url)); |
| + ExecuteJavaScript(@"window.history.pushState('', 'Test', 'test.html')"); |
| + |
| + // Perform replace state using JavaScript. |
| + const GURL replace_url = HttpServer::MakeUrl("http://chromium.test/1.html"); |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifySamePageHandle(web_state(), replace_url)); |
| + ExecuteJavaScript(@"window.history.replaceState('', 'Test', '1.html')"); |
| +} |
| + |
| +// Tests native content navigation. |
| +TEST_F(DidFinishNavigationTest, NativeContentNavigation) { |
| + GURL url(url::SchemeHostPort(kTestNativeContentScheme, "ui", 0).Serialize()); |
| + EXPECT_CALL(*observer_, DidFinishNavigation(_)) |
| + .WillOnce(VerifyNewPageHandle(web_state(), url)); |
| + LoadUrl(url); |
| +} |
| + |
|
Eugene But (OOO till 7-30)
2017/02/21 23:31:45
I could not write tests for error page navigation,
|
| +} // namespace web |